forked from muesli/deckmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (161 loc) · 3.67 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"sync"
"time"
"github.com/bendahl/uinput"
"github.com/davecgh/go-spew/spew"
"github.com/godbus/dbus"
"github.com/muesli/streamdeck"
)
var (
dev streamdeck.Device
dbusConn *dbus.Conn
keyboard uinput.Keyboard
x Xorg
deck *Deck
recentWindows []Window
deckFile = flag.String("deck", "deckmaster.deck", "path to deck config file")
brightness = flag.Uint("brightness", 80, "brightness in percent")
)
func handleActiveWindowChanged(_ streamdeck.Device, event ActiveWindowChangedEvent) {
fmt.Printf("Active window changed to %s (%d, %s)\n",
event.Window.Class, event.Window.ID, event.Window.Name)
// remove dupes
i := 0
for _, rw := range recentWindows {
if rw.ID == event.Window.ID {
continue
}
recentWindows[i] = rw
i++
}
recentWindows = recentWindows[:i]
recentWindows = append([]Window{event.Window}, recentWindows...)
if len(recentWindows) > 15 {
recentWindows = recentWindows[0:15]
}
deck.updateWidgets()
}
func handleWindowClosed(dev streamdeck.Device, event WindowClosedEvent) {
i := 0
for _, rw := range recentWindows {
if rw.ID == event.Window.ID {
continue
}
recentWindows[i] = rw
i++
}
recentWindows = recentWindows[:i]
deck.updateWidgets()
}
func main() {
flag.Parse()
var err error
deck, err = LoadDeck(*deckFile)
if err != nil {
log.Fatal(err)
}
dbusConn, err = dbus.SessionBus()
if err != nil {
log.Fatal(err)
}
x = Connect(os.Getenv("DISPLAY"))
defer x.Close()
tch := make(chan interface{})
x.TrackWindows(tch, time.Second)
d, err := streamdeck.Devices()
if err != nil {
log.Fatal(err)
}
if len(d) == 0 {
fmt.Println("No Stream Deck devices found.")
return
}
dev = d[0]
err = dev.Open()
if err != nil {
log.Fatal(err)
}
ver, err := dev.FirmwareVersion()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found device with serial %s (firmware %s)\n",
dev.Serial, ver)
err = dev.Reset()
if err != nil {
log.Fatal(err)
}
if *brightness > 100 {
*brightness = 100
}
err = dev.SetBrightness(uint8(*brightness))
if err != nil {
log.Fatal(err)
}
keyboard, err = uinput.CreateKeyboard("/dev/uinput", []byte("Deckmaster"))
if err != nil {
log.Printf("Could not create virtual input device (/dev/uinput): %s", err)
log.Println("Emulating keyboard events will be disabled!")
} else {
defer keyboard.Close()
}
var keyStates sync.Map
keyTimestamps := make(map[uint8]time.Time)
kch, err := dev.ReadKeys()
if err != nil {
log.Fatal(err)
}
for {
select {
case <-time.After(900 * time.Millisecond):
deck.updateWidgets()
case k, ok := <-kch:
if !ok {
err = dev.Open()
if err != nil {
log.Fatal(err)
}
continue
}
spew.Dump(k)
var state bool
if ks, ok := keyStates.Load(k.Index); ok {
state = ks.(bool)
}
// fmt.Println("Storing state", k.Pressed)
keyStates.Store(k.Index, k.Pressed)
if state && !k.Pressed {
// key was released
if time.Since(keyTimestamps[k.Index]) < 200*time.Millisecond {
fmt.Println("Triggering short action")
deck.triggerAction(k.Index, false)
}
}
if !state && k.Pressed {
// key was pressed
go func() {
// launch timer to observe keystate
time.Sleep(200 * time.Millisecond)
if state, ok := keyStates.Load(k.Index); ok && state.(bool) {
// key still pressed
fmt.Println("Triggering long action")
deck.triggerAction(k.Index, true)
}
}()
}
keyTimestamps[k.Index] = time.Now()
case e := <-tch:
switch event := e.(type) {
case WindowClosedEvent:
handleWindowClosed(dev, event)
case ActiveWindowChangedEvent:
handleActiveWindowChanged(dev, event)
}
}
}
}