-
Notifications
You must be signed in to change notification settings - Fork 76
/
wl_introspector.go
123 lines (103 loc) · 2.81 KB
/
wl_introspector.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
package main
import (
"fmt"
wl "github.com/dkolbly/wl"
)
var wlAppId string
func wlGetFocusWindowClass() error {
display, err := wl.Connect("")
if err != nil {
return fmt.Errorf("Connect to Wayland server failed %s", err)
}
appIdChan := make(chan string, 10)
err = registerGlobals(display, appIdChan)
if err != nil {
return err
}
for {
select {
case wlAppId = <-appIdChan:
fmt.Println("wlAppId = ", wlAppId)
case display.Context().Dispatch() <- struct{}{}:
}
}
display.Context().Close()
return nil
}
func registerGlobals(display *wl.Display, appIdChan chan string) error {
registry, err := display.GetRegistry()
if err != nil {
return fmt.Errorf("Display.GetRegistry failed : %s", err)
}
callback, err := display.Sync()
if err != nil {
return fmt.Errorf("Display.Sync failed %s", err)
}
rgeChan := make(chan wl.RegistryGlobalEvent)
rgeHandler := registrar{rgeChan}
registry.AddGlobalHandler(rgeHandler)
cdeChan := make(chan wl.CallbackDoneEvent)
cdeHandler := doner{cdeChan}
callback.AddDoneHandler(cdeHandler)
adHandler := appIdHandler{appIdChan, false}
loop:
for {
select {
case ev := <-rgeChan:
if err := registerInterface(registry, ev, display.Context(), &adHandler); err != nil {
return err
}
case display.Context().Dispatch() <- struct{}{}:
case <-cdeChan:
break loop
}
}
registry.RemoveGlobalHandler(rgeHandler)
callback.RemoveDoneHandler(cdeHandler)
return nil
}
func registerInterface(registry *wl.Registry, ev wl.RegistryGlobalEvent, ctx *wl.Context, adHandler *appIdHandler) error {
switch ev.Interface {
case "zwlr_foreign_toplevel_manager_v1":
manager := NewZwlrForeignToplevelManagerV1(ctx)
manager.AddToplevelHandler(toplevelHandlers{adHandler})
err := registry.Bind(ev.Name, ev.Interface, ev.Version, manager)
if err != nil {
return fmt.Errorf("Unable to bind ZwlrForeignToplevelManagerV1 interface: %s", err)
}
}
return nil
}
type doner struct {
ch chan wl.CallbackDoneEvent
}
func (d doner) HandleCallbackDone(ev wl.CallbackDoneEvent) {
d.ch <- ev
}
type registrar struct {
ch chan wl.RegistryGlobalEvent
}
func (r registrar) HandleRegistryGlobal(ev wl.RegistryGlobalEvent) {
r.ch <- ev
}
type toplevelHandlers struct {
adHandler *appIdHandler
}
func (t toplevelHandlers) HandleZwlrForeignToplevelManagerV1Toplevel(ev ZwlrForeignToplevelManagerV1ToplevelEvent) {
ev.Toplevel.AddAppIdHandler(t.adHandler)
ev.Toplevel.AddDoneHandler(t.adHandler)
}
type appIdHandler struct {
ch chan string
isAppIdUpdated bool
}
func (a *appIdHandler) HandleZwlrForeignToplevelHandleV1AppId(ev ZwlrForeignToplevelHandleV1AppIdEvent) {
a.ch <- ev.AppId
a.isAppIdUpdated = true
}
func (a *appIdHandler) HandleZwlrForeignToplevelHandleV1Done(ev ZwlrForeignToplevelHandleV1DoneEvent) {
if !a.isAppIdUpdated {
a.ch <- ""
}
a.isAppIdUpdated = false
}