Skip to content

Commit

Permalink
Fix and improve PostMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2brain committed Sep 5, 2023
1 parent 492dc69 commit 5f80a91
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
31 changes: 21 additions & 10 deletions examples/postmessage/postmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"encoding/json"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"math/rand"
"net/http"
"runtime/cgo"

"github.com/gen2brain/iup-go/iup"
)
Expand Down Expand Up @@ -42,10 +43,8 @@ func main() {
iup.MainLoop()
}

func messageCb(ih iup.Ihandle, s string, i int, f float64, p *cgo.Handle) int {
b := p.Value().([]byte)
defer p.Delete()

func messageCb(ih iup.Ihandle, s string, i int, p any) int {
b := p.([]byte)
img, _, err := image.Decode(bytes.NewReader(b))
if err != nil {
log.Fatalln(err)
Expand All @@ -68,32 +67,44 @@ func messageCb(ih iup.Ihandle, s string, i int, f float64, p *cgo.Handle) int {
func buttonCb(ih iup.Ihandle) int {
ih.SetAttribute("ACTIVE", "NO")

type xkcd struct {
Id int64 `json:"num,omitempty"`
Title string `json:"title,omitempty"`
Alt string `json:"alt,omitempty"`
Img string `json:"img,omitempty"`
}

go func() {
res, err := http.Get("https://random-xkcd-img.herokuapp.com/")
random := rand.Intn(2800-1) + 1

res, err := http.Get(fmt.Sprintf("https://xkcd.com/%d/info.0.json", random))
if err != nil {
log.Println(err)
return
}
defer res.Body.Close()

var ret map[string]string
var ret xkcd
err = json.NewDecoder(res.Body).Decode(&ret)
if err != nil {
log.Println(err)
return
}

img, err := http.Get(ret["url"])
img, err := http.Get(ret.Img)
if err != nil {
log.Println(err)
return
}
defer img.Body.Close()

b, err := ioutil.ReadAll(img.Body)
if err != nil {
log.Println(err)
return
}

h := cgo.NewHandle(b)
iup.PostMessage(iup.GetHandle("label"), ret["title"], len(b), 1.0, h)
iup.PostMessage(iup.GetHandle("label"), ret.Alt, 0, b)
}()

return iup.DEFAULT
Expand Down
13 changes: 9 additions & 4 deletions iup/bind_callbacks.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package iup

import (
"fmt"
"runtime/cgo"
"sync"
"sync/atomic"
"unsafe"
)

Expand Down Expand Up @@ -399,6 +401,7 @@ import "C"

var (
callbacks sync.Map
messages atomic.Int64
)

//--------------------
Expand Down Expand Up @@ -1279,7 +1282,7 @@ func setSpinFunc(ih Ihandle, f SpinFunc) {
//--------------------

// PostMessageFunc for POSTMESSAGE_CB callback.
type PostMessageFunc func(Ihandle, string, int, float64, *cgo.Handle) int
type PostMessageFunc func(Ihandle, string, int, any) int

//export goIupPostMessageCB
func goIupPostMessageCB(ih unsafe.Pointer, s unsafe.Pointer, i C.int, d C.double, p unsafe.Pointer) C.int {
Expand All @@ -1292,10 +1295,12 @@ func goIupPostMessageCB(ih unsafe.Pointer, s unsafe.Pointer, i C.int, d C.double
ch := h.(cgo.Handle)
f := ch.Value().(PostMessageFunc)

goS := C.GoString((*C.char)(s))
goP := (*cgo.Handle)(p)
m, ok := callbacks.LoadAndDelete(fmt.Sprintf("POSTMESSAGE_MSG_%s_%d", uuid, int(d)))
if !ok {
panic("cannot load and delete message " + fmt.Sprintf("POSTMESSAGE_MSG_%s_%d", uuid, int(d)))
}

return C.int(f((Ihandle)(ih), goS, int(i), float64(d), goP))
return C.int(f((Ihandle)(ih), C.GoString((*C.char)(s)), int(i), m))
}

// setPostMessageFunc for POSTMESSAGE_CB.
Expand Down
6 changes: 5 additions & 1 deletion iup/bind_events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iup

import (
"fmt"
"runtime/cgo"
"unsafe"
)
Expand Down Expand Up @@ -65,7 +66,10 @@ func PostMessage(ih Ihandle, s string, i int, d float64, p cgo.Handle) {
cS := C.CString(s)
defer C.free(unsafe.Pointer(cS))

C.IupPostMessage(ih.ptr(), cS, C.int(i), C.double(d), unsafe.Pointer(&p))
count := messages.Add(1)
callbacks.Store(fmt.Sprintf("POSTMESSAGE_MSG_%s_%d", ih.GetAttribute("UUID"), count), p)

C.IupPostMessage(ih.ptr(), cS, C.int(i), C.double(count), nil)
}

// Flush processes all pending messages in the message queue.
Expand Down
6 changes: 5 additions & 1 deletion iup/bind_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
#include <stdlib.h>
#include "iup.h"
static uintptr_t ih(Ihandle* i) {
return (uintptr_t)(i);
}
static Ihandle* pih(uintptr_t p) {
return (Ihandle*)(p);
}
Expand All @@ -24,7 +28,7 @@ import "C"
type Ihandle uintptr

func mkih(p *C.Ihandle) Ihandle {
return Ihandle(unsafe.Pointer(p))
return Ihandle(C.ih(p))
}

func cih(ih Ihandle) *C.char {
Expand Down

0 comments on commit 5f80a91

Please sign in to comment.