-
Notifications
You must be signed in to change notification settings - Fork 93
/
traymanager.go
394 lines (343 loc) · 9.13 KB
/
traymanager.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package trayicon
import (
"bytes"
"errors"
"sync"
"time"
"github.com/linuxdeepin/go-lib/dbusutil"
x "github.com/linuxdeepin/go-x11-client"
"github.com/linuxdeepin/go-x11-client/ext/composite"
"github.com/linuxdeepin/go-x11-client/ext/damage"
)
const (
OpcodeSystemTrayRequestDock uint32 = iota
OpcodeSystemTrayBeginMessage
OpcodeSystemTrayCancelMessage
)
//go:generate dbusutil-gen -type TrayManager,StatusNotifierWatcher -import github.com/linuxdeepin/go-lib/strv traymanager.go status-notifier-watcher.go
//go:generate dbusutil-gen em -type TrayManager,StatusNotifierWatcher
// TrayManager为系统托盘的管理器。
type TrayManager struct {
service *dbusutil.Service
owner x.Window // the manager selection owner window
visual x.VisualID
icons map[x.Window]*TrayIcon
mutex sync.Mutex
damageNotifyEventHandler DamageNotifyEventHandler
// 目前已有系统托盘窗口的id。
PropsMu sync.RWMutex
// dbusutil-gen: equal=nil
TrayIcons []uint32
// nolint
signals *struct {
// Inited when tray manager is initialized.
Inited struct{}
// Added信号会在系统过盘图标增加时被触发。
// Removed信号会在系统过盘图标被移除时被触发。
// Changed信号会在系统托盘图标改变后被触发。
Added, Removed, Changed struct {
id uint32
}
}
}
type DamageNotifyEventHandler struct {
mu sync.Mutex
queuedWinIds []x.Window
timer *time.Timer
timerStarted bool
manager *TrayManager
}
func (handler *DamageNotifyEventHandler) process(winId x.Window) {
handler.mu.Lock()
var found bool
for _, winId0 := range handler.queuedWinIds {
if winId0 == winId {
found = true
}
}
if !found {
handler.queuedWinIds = append(handler.queuedWinIds, winId)
}
if !handler.timerStarted {
handler.timerStarted = true
handler.timer = time.AfterFunc(60*time.Millisecond, func() {
handler.mu.Lock()
m := handler.manager
for _, winId := range handler.queuedWinIds {
m.handleDamageNotifyEvent(winId)
}
handler.queuedWinIds = nil
handler.timerStarted = false
handler.mu.Unlock()
})
}
handler.mu.Unlock()
}
func NewTrayManager(service *dbusutil.Service) *TrayManager {
visualId := findRGBAVisualID()
m := &TrayManager{
service: service,
visual: visualId,
icons: make(map[x.Window]*TrayIcon),
}
m.damageNotifyEventHandler.manager = m
err := m.init()
if err != nil {
logger.Warning(err)
}
return m
}
func (m *TrayManager) init() error {
win, err := createOwnerWindow(m.visual)
if err != nil {
return err
}
logger.Debug("create owner window", win)
m.owner = win
err = m.acquireSystemTraySelection()
if err != nil {
return err
}
go m.eventHandleLoop()
return nil
}
func (m *TrayManager) checkValid() {
for _, id := range m.TrayIcons {
xid := x.Window(id)
if isValidWindow(xid) {
continue
}
m.removeIcon(xid)
}
}
func (m *TrayManager) handleDamageNotifyEvent(xid x.Window) {
m.mutex.Lock()
icon, ok := m.icons[xid]
m.mutex.Unlock()
if !ok {
return
}
icon.mu.Lock()
if !icon.notify {
// ignore event
icon.mu.Unlock()
return
}
icon.mu.Unlock()
newData, err := icon.getPixmapData()
if err != nil {
logger.Warning(err)
return
}
if !bytes.Equal(icon.data, newData) {
icon.data = newData
err := m.service.Emit(m, "Changed", uint32(xid))
if err != nil {
logger.Warning(err)
}
logger.Debugf("handleDamageNotifyEvent %v changed", xid)
} else {
logger.Debugf("handleDamageNotifyEvent %v no changed", xid)
}
}
func sendClientMessage(win, dest x.Window, msgType x.Atom, pArray *[5]uint32) error {
var data x.ClientMessageData
data.SetData32(pArray)
event := x.ClientMessageEvent{
Format: 32,
Window: win,
Type: msgType,
Data: data,
}
w := x.NewWriter()
x.WriteClientMessageEvent(w, &event)
const evMask = x.EventMaskStructureNotify
return x.SendEventChecked(XConn, false, dest, evMask, w.Bytes()).Check(XConn)
}
// to notify tray icon applications
func (m *TrayManager) sendClientMsgMANAGER() error {
screen := XConn.GetDefaultScreen()
array := [5]uint32{
x.CurrentTime,
uint32(XA_NET_SYSTEM_TRAY_S0),
uint32(m.owner),
}
logger.Debug("send clientMsg MANAGER")
return sendClientMessage(screen.Root, screen.Root, XA_MANAGER, &array)
}
func getSystemTraySelectionOwner() (x.Window, error) {
reply, err := x.GetSelectionOwner(XConn, XA_NET_SYSTEM_TRAY_S0).Reply(XConn)
if err != nil {
return 0, err
}
return reply.Owner, nil
}
func createOwnerWindow(visual x.VisualID) (x.Window, error) {
winId, err := XConn.AllocID()
if err != nil {
return 0, err
}
win := x.Window(winId)
screen := XConn.GetDefaultScreen()
err = x.CreateWindowChecked(XConn,
0,
win, // window
screen.Root, // parent
0, 0, 1, 1, 0,
x.WindowClassInputOnly,
visual,
x.CWEventMask,
[]uint32{x.EventMaskStructureNotify},
).Check(XConn)
if err != nil {
return 0, err
}
return win, nil
}
func (m *TrayManager) acquireSystemTraySelection() error {
currentOwner, err := getSystemTraySelectionOwner()
if err != nil {
return err
}
logger.Debug("currentOwner is ", currentOwner)
if currentOwner != 0 && currentOwner != m.owner {
return errors.New("Another System tray application is running")
}
err = x.SetSelectionOwnerChecked(
XConn,
m.owner,
XA_NET_SYSTEM_TRAY_S0,
x.CurrentTime).Check(XConn)
if err != nil {
return err
}
w := x.NewWriter()
w.Write4b(uint32(m.visual))
x.ChangeProperty(XConn,
x.PropModeReplace,
m.owner, // window
XA_NET_SYSTEM_TRAY_VISUAL, // property
x.AtomVisualID, // type
32, w.Bytes())
w = x.NewWriter()
w.Write4b(0)
x.ChangeProperty(XConn,
x.PropModeReplace,
m.owner, // window
XA_NET_SYSTEM_TRAY_ORIENTAION, // property
x.AtomCardinal, // type
32,
w.Bytes())
logger.Debug("acquire selection successful")
return nil
}
func (m *TrayManager) eventHandleLoop() {
damageExtData := XConn.GetExtensionData(damage.Ext())
damageFirstEvent := damageExtData.FirstEvent
eventChan := make(chan x.GenericEvent, 500)
XConn.AddEventChan(eventChan)
for ev := range eventChan {
switch ev.GetEventCode() {
case x.ClientMessageEventCode:
event, _ := x.NewClientMessageEvent(ev)
if event.Type == XA_NET_SYSTEM_TRAY_OPCODE {
data32 := event.Data.GetData32()
opcode := data32[1]
logger.Debug("system tray opcode", opcode)
if opcode == OpcodeSystemTrayRequestDock {
win := x.Window(data32[2])
logger.Debug("ClientMessageEvent: system tray request dock", win)
// 虚拟键盘注册插件时,任务栏调用XGetWindowProperty函数,wayland下会导致崩溃
// 此处过滤掉虚拟键盘的注册信号
if (NewTrayIcon(win).getName() != "onboard") {
m.addIcon(win)
}
}
}
case damage.NotifyEventCode + damageFirstEvent:
event, _ := damage.NewNotifyEvent(ev)
m.damageNotifyEventHandler.process(x.Window(event.Drawable))
case x.MapNotifyEventCode:
event, _ := x.NewMapNotifyEvent(ev)
logger.Debug("MapNotifyEvent", event.Window)
case x.UnmapNotifyEventCode:
event, _ := x.NewUnmapNotifyEvent(ev)
logger.Debug("UnmapNotifyEvent", event.Window)
case x.DestroyNotifyEventCode:
event, _ := x.NewDestroyNotifyEvent(ev)
logger.Debug("DestroyNotifyEvent", event.Window)
m.removeIcon(event.Window)
default:
logger.Debug(ev)
}
}
}
func (m *TrayManager) addIcon(win x.Window) {
m.checkValid()
m.mutex.Lock()
defer m.mutex.Unlock()
_, ok := m.icons[win]
if ok {
logger.Debugf("addIcon failed: %v existed", win)
return
}
damageId, err := XConn.AllocID()
if err != nil {
logger.Debug("addIcon failed, new damage id failed:", err)
return
}
d := damage.Damage(damageId)
icon := NewTrayIcon(win)
icon.damage = d
err = damage.CreateChecked(XConn, d, x.Drawable(win), damage.ReportLevelRawRectangles).Check(XConn)
if err != nil {
logger.Debug("addIcon failed, damage create failed:", err)
return
}
composite.RedirectWindow(XConn, win, composite.RedirectAutomatic)
const valueMask = x.CWBackPixel | x.CWEventMask
x.ChangeWindowAttributes(XConn, win, valueMask, []uint32{
0, // background pixel
x.EventMaskVisibilityChange | x.EventMaskStructureNotify, // event mask
})
err = m.service.Emit(m, "Added", uint32(win))
if err != nil {
logger.Warning(err)
}
logger.Infof("Add tray icon %v name: %q", win, icon.getName())
m.icons[win] = icon
m.updateTrayIcons()
}
func (m *TrayManager) removeIcon(win x.Window) {
m.mutex.Lock()
defer m.mutex.Unlock()
icon, ok := m.icons[win]
if !ok {
logger.Debugf("removeIcon failed: %v not exist", win)
return
}
// NOTE: no need to destroy the damage, the window of icon is destroyed.
err := XConn.FreeID(uint32(icon.damage))
if err != nil {
logger.Warning(err)
}
delete(m.icons, win)
err = m.service.Emit(m, "Removed", uint32(win))
if err != nil {
logger.Warning(err)
}
logger.Infof("remove tray icon %v", win)
m.updateTrayIcons()
}
func (m *TrayManager) updateTrayIcons() {
var icons []uint32
for _, icon := range m.icons {
icons = append(icons, uint32(icon.win))
}
m.PropsMu.Lock()
m.setPropTrayIcons(icons)
m.PropsMu.Unlock()
}