forked from cratonica/trayhost
-
Notifications
You must be signed in to change notification settings - Fork 8
/
trayhost.go
175 lines (152 loc) · 3.04 KB
/
trayhost.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
/*
*/
package trayhost
import (
"fmt"
"io/ioutil"
"log"
"os"
"sort"
)
/*
#cgo linux pkg-config: gtk+-2.0
#cgo linux CFLAGS: -DLINUX -I/usr/include/libappindicator-0.1/
#cgo linux LDFLAGS: -ldl
#cgo windows CFLAGS: -DWIN32 -DUNICODE
#cgo darwin CFLAGS: -DDARWIN -x objective-c
#cgo darwin LDFLAGS: -framework Cocoa
#include <stdlib.h>
#include "platform/platform.h"
*/
import "C"
const (
ICON_PRIMARY = iota
ICON_ALTERNATIVE = iota
ICON_ATTENTION = iota
)
const (
WINDOWS = iota
OSX = iota
GNOME = iota
UNITY = iota
LINUX_GENERIC = iota
)
type MenuItem struct {
Title string
Disabled bool
Handler func()
}
type MenuItemUpdate struct {
ItemId int
Item MenuItem
}
type MenuItems map[int]MenuItem
var isExiting bool = false
var menuItems MenuItems
var UpdateCh = make(chan MenuItemUpdate, 99)
var icons = map[int]string{}
var tmpFiles []string = make([]string, 0, 3)
var clickHandler func()
var Debug bool = false
var curIconId int = -1
var tmpDir string
// Run the host system's event loop
func Initialize(title string, imageData []byte, items MenuItems, tmpDirectory string) (err error) {
if !Debug {
log.SetOutput(ioutil.Discard)
}
tmpDir = tmpDirectory
err = SetIconImage(ICON_PRIMARY, imageData)
if err != nil {
return
}
initialize(title)
err = SetIcon(ICON_PRIMARY)
if err != nil {
return
}
setMenu(items)
return
}
func SetIconImage(iconId int, imageData []byte) (err error) {
iconPth, err := createTempFile(imageData)
if err != nil {
return
}
icons[iconId] = iconPth
return
}
func EnterLoop() {
go menuUpdateLoop()
C.native_loop()
isExiting = true
}
func Exit() {
C.exit_loop()
cleanup()
}
func SetIcon(iconId int) (err error) {
if iconId != curIconId {
iconPth, ok := icons[iconId]
if !ok {
err = fmt.Errorf("No icon with icon id %d", iconId)
return
}
log.Printf("Setting icon %s (id: %d)", iconPth, iconId)
setIcon(iconPth)
curIconId = iconId
}
return
}
func SetClickHandler(handler func()) {
clickHandler = handler
}
func menuUpdateLoop() {
for update := range UpdateCh {
updateMenuItem(update.ItemId, update.Item)
}
}
func updateMenuItem(id int, item MenuItem) {
menuItems[id] = item
setMenuItem(id, item)
}
func setMenu(menu MenuItems) {
menuItems = menu
menuItemOrder := make([]int, 0, len(menuItems))
for key, _ := range menuItems {
menuItemOrder = append(menuItemOrder, key)
}
sort.Ints(menuItemOrder)
for id := range menuItemOrder {
item := menuItems[id]
setMenuItem(id, item)
}
}
func createTempFile(iconData []byte) (filename string, err error) {
file, err := ioutil.TempFile(tmpDir, "trayhosticon")
if err != nil {
return
}
defer file.Close()
_, err = file.Write(iconData)
filename = file.Name()
tmpFiles = append(tmpFiles, filename)
return
}
func cleanup() {
for _, file := range tmpFiles {
err := os.Remove(file)
if err != nil {
log.Printf("Failed to remove tmp file %s: %v\n", file, err)
} else {
log.Printf("Tmp file %s removed\n", file)
}
}
}
func cbool(b bool) C.int {
if b {
return 1
} else {
return 0
}
}