forked from zserge/tray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tray.go
208 lines (181 loc) · 3.97 KB
/
tray.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package tray
/*
#cgo darwin CFLAGS: -DDARWIN -x objective-c -fobjc-arc
#cgo darwin LDFLAGS: -framework Cocoa
#cgo linux pkg-config: gtk+-3.0 appindicator3-0.1
#include <stdlib.h>
#include "tray.h"
static struct tray_menu menu[128];
static struct tray tray = {
.menu = menu
};
static struct tray* tray_get() {
return &tray;
}
static struct tray_menu* tray_menu_get(size_t i) {
return &menu[i];
}
static void tray_menu_reset() {
for (int i = 0; i < 128; ++i) {
menu[i].text = NULL;
menu[i].cb = NULL;
menu[i].context = NULL;
}
}
extern void tray_menu_cb(struct tray_menu *);
static void tray_menu_set_cb(struct tray_menu *s) {
s->cb = &tray_menu_cb;
}
*/
import "C"
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"unsafe"
"github.com/mattn/go-pointer"
)
var Seperator = &Menu{Text: "-"}
//export tray_menu_cb
func tray_menu_cb(cm *C.struct_tray_menu) {
if cm == nil || cm.context == nil {
return
}
m := pointer.Restore(cm.context).(*Menu)
if m != nil && m.Callback != nil {
m.Callback(m.tray, m)
}
}
type alloc struct {
strings []*C.char
ptrs []unsafe.Pointer
}
func (a *alloc) String(str string) *C.char {
cs := C.CString(str)
a.strings = append(a.strings, cs)
return cs
}
func (a *alloc) Pointer(o interface{}) unsafe.Pointer {
ptr := pointer.Save(o)
a.ptrs = append(a.ptrs, ptr)
return ptr
}
func (a *alloc) Free() {
for _, cs := range a.strings {
C.free(unsafe.Pointer(cs))
}
for _, ptr := range a.ptrs {
pointer.Unref(ptr)
}
}
type Menu struct {
tray *Tray
Text string
Checked bool
Disabled bool
Callback func(*Tray, *Menu)
SubMenu []*Menu
}
type Tray struct {
Icon []byte
Menu []*Menu
allocs []*alloc
}
func (t *Tray) syncC() {
C.tray_menu_reset()
a := &alloc{
strings: []*C.char{},
ptrs: []unsafe.Pointer{},
}
t.allocs = append(t.allocs, a)
ct := C.tray_get()
if runtime.GOOS == "darwin" {
ct.icon.data = (*C.char)(unsafe.Pointer(&t.Icon[0]))
ct.icon.length = (C.int)(len(t.Icon))
} else {
fmt.Println(C.GoString(ct.icon.data))
// On linux instead we store the content of the icon the temp file,
// which is created in the .Run method and cleaned up via .Quit.
err := ioutil.WriteFile(C.GoString(ct.icon.data), t.Icon, 0644)
if err != nil {
panic(err)
}
}
for i, m := range t.Menu {
m.tray = t
trayMenuSet(a, i, m)
}
offset := len(t.Menu) + 1
for i, m := range t.Menu {
if m.SubMenu == nil || len(m.SubMenu) == 0 {
continue
}
cm := C.tray_menu_get(C.size_t(i))
cm.submenu = C.tray_menu_get(C.size_t(offset))
for _, s := range m.SubMenu {
s.tray = t
trayMenuSet(a, offset, s)
offset += 1
}
offset += 1
}
if len(t.allocs) > 2 {
a, t.allocs = t.allocs[0], t.allocs[1:]
a.Free()
}
}
func (t *Tray) Run() error {
runtime.LockOSThread()
if runtime.GOOS != "darwin" {
// On linux we'll have to store the icon in a tempfile, so let's
// create the file. This file will be written to in .syncC and it will
// be cleaned up by .Quit.
ct := C.tray_get()
tmp, err := ioutil.TempFile("", "icon")
if err != nil {
panic(err)
}
// So append it on linux as well as it has no impact
ct.icon.data = C.CString(tmp.Name() + ".ico")
}
t.syncC()
if C.tray_init(C.tray_get()) < 0 {
return fmt.Errorf("tray init failed")
}
for C.tray_loop(1) == 0 {
}
return nil
}
func (t *Tray) Update() {
t.syncC()
C.tray_update(C.tray_get())
}
func (t *Tray) Quit() {
if runtime.GOOS != "darwin" {
// Let's cleanup or temporary file.
ct := C.tray_get()
os.Remove(C.GoString(ct.icon.data))
}
C.tray_exit()
}
func Insert(a []*Menu, i int, m *Menu) []*Menu {
return append(a[:i], append([]*Menu{m}, a[i:]...)...)
}
func Remove(a []*Menu, i int) []*Menu {
return append(a[:i-1], a[i:]...)
}
func trayMenuSet(a *alloc, i int, m *Menu) {
cm := C.tray_menu_get(C.size_t(i))
cm.text = a.String(m.Text)
cm.checked = boolToInt(m.Checked)
cm.disabled = boolToInt(m.Disabled)
cm.context = a.Pointer(m)
C.tray_menu_set_cb(cm)
}
func boolToInt(b bool) C.int {
if b {
return 1
}
return 0
}