forked from overlordtm/trayhost
-
Notifications
You must be signed in to change notification settings - Fork 19
/
trayhost_exports.go
76 lines (63 loc) · 1.52 KB
/
trayhost_exports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package trayhost
import (
"log"
"reflect"
"unsafe"
)
/*
#include <stdlib.h>
#include "platform/common.h"
*/
import "C"
//export tray_callback
func tray_callback(itemId C.int) {
if itemId < 0 {
log.Println("tray click")
return
}
item := menuItems[itemId]
if item.Handler != nil {
item.Handler()
}
}
//export tray_enabled
func tray_enabled(itemId C.int) C.int {
item := menuItems[itemId]
return cbool(item.Enabled == nil || item.Enabled())
}
//export notification_callback
func notification_callback(notificationId C.int) {
if notificationId < 0 {
log.Println("notificationId < 0:", notificationId)
return
}
notification := notifications[notificationId]
if notification.Handler != nil {
notification.Handler()
}
}
func create_image(image Image) (C.struct_image, func()) {
var img C.struct_image
if image.Kind == "" || len(image.Bytes) == 0 {
return img, func() {}
}
// Copy the image data into unmanaged memory.
cImageKind := C.CString(string(image.Kind))
cImageData := C.malloc(C.size_t(len(image.Bytes)))
freeImg := func() {
C.free(unsafe.Pointer(cImageKind))
C.free(cImageData)
}
var cImageDataSlice []C.uchar
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&cImageDataSlice))
sliceHeader.Cap = len(image.Bytes)
sliceHeader.Len = len(image.Bytes)
sliceHeader.Data = uintptr(cImageData)
for i, b := range image.Bytes {
cImageDataSlice[i] = C.uchar(b)
}
img.kind = cImageKind
img.bytes = unsafe.Pointer(&cImageDataSlice[0])
img.length = C.int(len(image.Bytes))
return img, freeImg
}