-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghetto.go
249 lines (195 loc) · 5.38 KB
/
ghetto.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
package ghettowm
import (
"log"
"os"
"syscall"
"unsafe"
"bits.chrsm.org/x/windows/virtd"
"github.com/chrsm/winapi"
"github.com/chrsm/winapi/kernel"
"github.com/chrsm/winapi/user"
)
const (
windowNext = 1
windowPrev = 2
)
type WindowManager struct {
desktopCount int
desktopCurrent int
// per-desktop windows
windows map[int]*desktop
keybinds *keybinds
hwnd winapi.HWND
shmsg uint
rwnd winapi.HWND
runch chan func()
}
func New(runch chan func()) *WindowManager {
wm := &WindowManager{
desktopCount: virtd.GetDesktopCount(),
desktopCurrent: virtd.GetCurrentDesktopNumber(),
keybinds: newKeybinds(),
windows: make(map[int]*desktop),
}
for i := 0; i < wm.desktopCount; i++ {
wm.windows[i] = &desktop{}
}
winkeyState.Store(0)
wm.runch = runch
return wm
}
func (gwm *WindowManager) RegisterHotkey(modifiers user.ModKey, vkey user.VirtualKey, cb func(int)) bool {
gwm.keybinds.set(modifiers, vkey, cb)
return true
}
func (gwm *WindowManager) switchWindow(direction int) {
curdeskn := virtd.GetCurrentDesktopNumber()
curdesk, ok := gwm.windows[curdeskn]
if !ok {
log.Printf("Tried to switch windows on a desktop that doesn't exist internally (%d)", curdeskn)
}
cur := curdesk.find(user.GetForegroundWindow())
if cur == nil {
log.Printf("could not find active window(%X)", int(user.GetForegroundWindow()))
return
}
var dst *window
switch direction {
case windowNext:
dst = cur.next
if dst == nil {
// we've reached the end, so we go back to the first window.
dst = curdesk.head
}
case windowPrev:
dst = cur.prev
if dst == nil {
// we've reached the beginning, so we go back to the last window.
dst = curdesk.tail
}
default:
panic("unknown direction")
}
if dst == nil {
log.Printf("no window to switch to, unfortunately..")
return
}
user.SetForegroundWindow(dst.hwnd)
}
func (gwm *WindowManager) NextWindow() {
gwm.switchWindow(windowNext)
}
func (gwm *WindowManager) PrevWindow() {
gwm.switchWindow(windowPrev)
}
func (gwm *WindowManager) SwitchDesktop(dst int) bool {
if dst == gwm.desktopCurrent {
return false
}
virtd.GoToDesktopNumber(dst)
gwm.desktopCurrent = dst
return true
}
func (gwm *WindowManager) SwitchDesktopPrev() bool {
return gwm.SwitchDesktop(bound(gwm.desktopCurrent-1, gwm.desktopCount))
}
func (gwm *WindowManager) SwitchDesktopNext() bool {
return gwm.SwitchDesktop(bound(gwm.desktopCurrent+1, gwm.desktopCount))
}
func (gwm *WindowManager) Quit() {
/*for k := range gwm.keybinds.set {
kb := gwm.keybinds.set[k]
user.UnregisterHotkey(gwm.hwnd, kb.id)
}*/
log.Println("Quitting!")
os.Exit(0)
}
func (gwm *WindowManager) Run() {
const HWND_MESSAGE = winapi.HWND(^uintptr(2))
cname := "ghettowm"
gwm.rwnd = user.GetDesktopWindow()
log.Printf("desktop window(%X, %d); owner(%X)", gwm.rwnd, gwm.rwnd, user.GetWindow(gwm.rwnd, user.GwOwner))
self := kernel.GetModuleHandle("")
wclass := user.WindowClass{
WndProc: syscall.NewCallback(gwm.wndproc),
HInstance: winapi.HINSTANCE(self),
ClassName: syscall.StringToUTF16Ptr(cname),
}
wclass.CbSize = winapi.DWORD(unsafe.Sizeof(wclass))
user.RegisterClass(&wclass)
gwm.hwnd = user.CreateWindow(
cname,
cname,
0,
0,
0,
0,
0,
HWND_MESSAGE,
0,
winapi.HMODULE(self),
nil,
)
// I'm not sure if this is always 0xC028..
gwm.shmsg = user.RegisterWindowMessage("SHELLHOOK")
user.RegisterShellHookWindow(gwm.hwnd)
// register global keyboard hook for WINKEY
user.SetWindowsHookEx(user.WindowsHookKeyboardLowLevel, gwm.keyboardHook, 0, 0)
vm := newVM(gwm)
defer vm.Close()
// Run user configuration through lua, because I don't feel that
// writing a conf language for this makes sense, and opens up more
// customization options in the future.
if err := vm.DoFile("ghetto.lua"); err != nil {
panic(err)
}
log.Println("configuration succeeded")
user.EnumWindows(gwm.enumproc, 0)
msg := &user.Message{}
for {
log.Printf("waiting for message..")
ok := user.GetMessage(msg, 0, 0, 0)
if ok {
user.TranslateMessage(msg)
user.DispatchMessage(msg)
}
log.Printf("dispatched: %#v", msg)
}
}
func (gwm *WindowManager) wndproc(hwnd winapi.HWND, msg uint32, wparam uintptr, lparam uintptr) uintptr {
log.Printf("hwnd(%X); msg(%X, %d); wp(%X, %d); lp(%X, %d)", hwnd, msg, msg, wparam, wparam, lparam, lparam)
switch msg {
case user.WmHotkey:
log.Printf("WM_HOTKEY: %v", wparam)
kb := gwm.keybinds.getByID(int(wparam))
if kb != nil {
gwm.runch <- func() {
kb.cb(int(wparam))
}
} else {
return user.DefWindowProc(hwnd, msg, winapi.WPARAM(wparam), winapi.LPARAM(lparam))
}
default:
if msg == uint32(gwm.shmsg) && isUsefulHShellMsg(wparam) {
// handle it
return 0
}
return user.DefWindowProc(hwnd, msg, winapi.WPARAM(wparam), winapi.LPARAM(lparam))
}
return 0
}
func (gwm *WindowManager) enumproc(hwnd winapi.HWND, _ winapi.LPARAM) uintptr {
// check if the window is something we want to be responsible for
buf := make([]uint16, 32)
buf2 := make([]uint16, 32)
// do something with it..
title := user.GetWindowText(hwnd, buf)
_, pid := user.GetWindowThreadProcessId(hwnd)
cname := user.GetClassName(hwnd, buf2)
// log.Printf("hwnd(%X,%d); cname(%s); desktopn(%d); title(%s); pid(%d)", hwnd, hwnd, cname, virtd.GetWindowDesktopNumber(hwnd), title, pid)
_, _, _ = title, pid, cname
if usableWindow(hwnd) {
gwm.windows[virtd.GetWindowDesktopNumber(hwnd)].push(hwnd)
}
return 1
}