-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
243 lines (222 loc) · 6.9 KB
/
index.js
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
const {dirname} = require('path')
const {exec} = require('child_process')
const events = require('events')
const x11 = require('x11')
const EWMH = require('ewmh')
const {stringToKeys} = require('./util')
const keys = require('./lib/keys')
const Workspace = require('./workspace')
const Window = require('./window')
const listen = require('./srv')
const commandQueue = new events.EventEmitter
const name = 'wm'
const configuration = require('rc')(name)
// todo: fix this completely
const keybindings = (() => {
const keybindings = []
for (const binding in configuration.keybindings) {
keybindings.push(Object.assign({}, {
cmd: configuration.keybindings[binding]
}, stringToKeys(binding)))
}
return keybindings
})()
const ASYNC = 1
const NOPE = 0
process.env.PATH = `${dirname(process.argv[1])}/bin:${process.env.PATH}`
let attributes
let child
let start
let X
let workspaces
let workspace = null
let screen
let root
let ewmh
function grabKeys(X, bindings) {
bindings.forEach(binding => {
X.GrabKey(root, true,
binding.buttons,
binding.keycode,
ASYNC,
ASYNC
)
})
}
// todo: make this key configurable
function grabButtons(X) {
X.GrabButton(
root, true, x11.eventMask.ButtonPress | x11.eventMask.ButtonRelease | x11.eventMask.PointerMotion,
ASYNC, ASYNC, NOPE, NOPE, 1, keys.buttons.M
)
X.GrabButton(
root, true, x11.eventMask.ButtonPress | x11.eventMask.ButtonRelease | x11.eventMask.PointerMotion,
ASYNC, ASYNC, NOPE, NOPE, 3, keys.buttons.M
)
}
// todo: make this size configurable
function makeWorkspaces(size) {
const workspaces = []
for (let id = 0; id < size; id++) {
workspaces.push(Workspace.create(id))
}
return workspaces
}
function createClient() {
x11.createClient((error, display) => {
screen = display.screen[0]
root = screen.root
X = global.X = display.client
ewmh = new EWMH(X, root)
ewmh.on('Desktop', desktop => exec(`notify-send "workspace switch ${desktop}"`))
grabKeys(X, keybindings)
grabButtons(X)
workspaces = makeWorkspaces(5)
workspace = workspaces[0]
ewmh.set_number_of_desktops(5, error => {
if (error) exec(`notify-send "${error}"`)
ewmh.set_current_desktop(0)
})
}).on('event', event => {
// todo: put all these event handlers in functions
// todo: it will fix the duplicate decls and be neater
// todo: perhaps emit each of these events from another emitter
switch(event.name) {
case 'KeyPress':
keybindings.forEach(binding => {
if (event.buttons == binding.buttons && event.keycode == binding.keycode) {
exec(binding.cmd, console.log)
}
})
break
case 'ButtonPress':
child = event.child
X.RaiseWindow(child)
workspace.currentWindow = Window.create(child)
Window.focus(workspace.currentWindow)
if (!Workspace.contains(workspace, workspace.currentWindow)) {
Workspace.addWindow(workspace, workspace.currentWindow)
}
X.GetGeometry(child, (error, attr) => {
start = event
attributes = attr
})
break
case 'MotionNotify':
if (!start) return
const xdiff = event.rootx - start.rootx
const ydiff = event.rooty - start.rooty
start.keycode == 1 && start.buttons == keys.buttons.M && X.MoveWindow(
start.child,
attributes.xPos + (start.keycode == 1 ? xdiff : 0),
attributes.yPos + (start.keycode == 1 ? ydiff : 0)
)
start.keycode == 3 && X.ResizeWindow(
start.child,
Math.max(1, attributes.width + (start.keycode == 3 ? xdiff : 0)),
Math.max(1, attributes.height + (start.keycode == 3 ? ydiff : 0))
)
break
case 'ButtonRelease':
start = null
break
case 'MapRequest':
X.GetWindowAttributes(event.wid, (error, attributes) => {
if (error) return console.error(error)
if (attributes[8]) {
return X.MapWindow(event.wid)
}
})
X.ChangeWindowAttributes(
event.wid,
{eventMask: x11.eventMask.EnterWindow}
)
let window = Window.create(event.wid)
Workspace.addWindow(workspace, window)
break
case 'FocusIn':
case 'EnterNotify':
child = event.wid
workspace.currentWindow = Window.create(child)
Window.focus(workspace.currentWindow)
break
case 'ConfigureRequest':
child = event.wid
X.ResizeWindow(event.wid, event.width, event.height)
break
}
X.ChangeWindowAttributes(root, {
eventMask: x11.eventMask.SubstructureNotify
| x11.eventMask.SubstructureRedirect
| x11.eventMask.ResizeRedirect
| x11.eventMask.Exposure
| x11.eventMask.MapRequest
| x11.eventMask.EnterWindow
| x11.eventMask.FocusChange
}, console.error)
}).on('error', console.error)
}
// commands
commandQueue.on('cmd', cmd => {
let match = cmd.match(/workspace\s+(\w+)\s+(\d+)/)
if (match) {
switch (match[1]) {
case 'switch':
// todo: make this start showing the new windows before hiding the old ones
workspaces.forEach(workspace => Workspace.hide(workspace))
workspace = workspaces[match[2] - 1]
Workspace.show(workspace, root)
ewmh.set_current_desktop(match[2] - 1)
break
}
}
match = cmd.match(/^window\s+(\w+)\s+([a-z0-9]+)/)
if (match) {
switch (match[1]) {
case 'destroy':
// workspace.currentWindow?
break
case 'move':
// todo: remove only from current workspace?
if (!workspace.currentWindow) break
workspaces.forEach(workspace => Workspace.removeWindow(workspace, workspace.currentWindow))
Workspace.addWindow(workspaces[match[2] - 1], workspace.currentWindow)
Window.hide(workspace.currentWindow)
workspace.currentWindow = null
Workspace.show(workspace)
break
case 'tile':
switch (match[2]) {
case 'left':
X.ResizeWindow(workspace.currentWindow.id, screen.pixel_width / 2, screen.pixel_height)
X.MoveWindow(workspace.currentWindow.id, 0, 0)
break
case 'right':
X.ResizeWindow(workspace.currentWindow.id, screen.pixel_width / 2, screen.pixel_height)
X.MoveWindow(workspace.currentWindow.id, screen.pixel_width / 2, 0)
break
case 'full':
X.ResizeWindow(workspace.currentWindow.id, screen.pixel_width, screen.pixel_height)
X.MoveWindow(workspace.currentWindow.id, 0, 0)
}
}
}
// todo: make this just reload instead of killing the client
match = cmd.match(/^reload$/)
if (match) {
workspaces.forEach(workspace => Workspace.hide(workspace))
const home = workspace = workspaces[0]
Workspace.show(home)
ewmh.set_current_desktop(0)
workspaces.forEach(workspace => {
workspace.windows.forEach(window => {
Workspace.removeWindow(workspace, window)
Workspace.addWindow(home,window)
})
})
X.KillClient()
createClient()
}
})
listen(commandQueue)
createClient()