-
Notifications
You must be signed in to change notification settings - Fork 6
/
tiles.js
executable file
·214 lines (180 loc) · 4.77 KB
/
tiles.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
#! /usr/bin/env node
var x11 = require('x11')
var X
var config = require('./config')
var Layout = require('./layout')
var u = require('./utils')
var ease = require('vec2-easing')
require('./xorg')(function (err, client, display) {
if(err) throw err
var rw = client.root, _prevFocus
var mouse = client.mouse
var layouts = [new Layout(rw)]
var l = layouts[0]
mouse.change(function () {
for(var i in l.tiles) {
var v = l.tiles[i]
if(v && v.bounds && v.bounds.contains(mouse)) {
console.log(v === l.focused)
if(v !== l.focused)
v.focus()
// console.log('OVER:', v)
}
}
})
client.client.require('randr', function(Randr) {
Randr.SelectInput(rw.id, Randr.NotifyMask.ScreenChange);
});
rw.on('RRScreenChangeNotify', function(ev) {
rw.bounds.size.set(ev.width, ev.height)
l.layout();
});
function cycleLayout(dir) {
l._delay = Date.now() + l.delay
l.hide()
l = u.relative(layouts, l, dir || 1)
l.show()
}
//create a new window, but don't add it to the tree.
var EV = x11.eventMask.Exposure | x11.eventMask.SubstructureRedirect
| x11.eventMask.MapRequest | x11.eventMask.SubstructureNotify
| x11.eventMask.EnterWindow
rw.set({eventMask: EV}, function(err) {
if (err && err.error == 10) {
console.error('Error: another window manager already running.');
process.exit(1);
}
})
rw.children(function (err, children) {
children.forEach(function (win) {
win.kill()
l.remove(win)
})
l.layout()
})
rw.on('MapRequest', function (ev, win) {
//load the window's properties, and then lay it out.
win.load(function () {
//add to current layout
var b = win.bounds
win.bounds = ease(b, config.easing, config.frameRate)
win.bounds.__proto__ = b
win.bounds.size = ease(b.size, config.easing, config.frameRate)
win.bounds.size.__proto__ = b.size
win.configure({borderWidth: 1})
win.on('focus', function () {
if(_prevFocus)
_prevFocus.set({ borderPixel: 0x0 })
win.set({borderPixel: 0xffff00})
_prevFocus = win
l.focused = win
})
win.map()
l.add(win)
win.focus()
win.raise()
l.layout()
})
// win.set({eventMask: x11.eventMask.EnterWindow})
})
rw.on('DestroyNotify', function (ev, win) {
l.remove(win)
//UGLY HACK AROUND STRANGE ERROR WHERE
//KB SHORTCUTS STOP WORKING WHEN YOU CLOSE ALL THE WINDOWS
// if(!l.tiles.length)
// spawn(process.env.TERM || 'xterm')
})
rw.on('ConfigureRequest', function (ev, win) {
//prevent windows from sizing themselves?
if(win.bounds)
win.bounds.size.set(ev.width, ev.height)
else
win.resize(ev.width, ev.height)
})
var _spawn = require('child_process').spawn
function spawn (cmd) {
var args = cmd.split(/\s+/)
cmd = args.shift()
_spawn(cmd, args)
}
//open terminal
//Command-T/K
rw.onKey(0x40, 45, function (ev) {
if(ev.down)
spawn(process.env.TERM || 'xterm')
})
//Command-C/I
rw.onKey(0x40, 31, function (ev) {
if(ev.down)
spawn(process.env.BROWSER || 'chromium')
})
//Command-Esc
rw.onKey(0x40, 9, function (ev) {
if(ev.down) {
console.log('quiting...')
process.exit(0)
}
})
//Command-Space
rw.onKey(0x40, 65, function (ev) {
if(ev.down) l.toggle()
})
//Command-Left
rw.onKey(0x40, 113, function (ev) {
if(ev.down) l.cycle(-1)
})
rw.onKey(0x40, 114, function (ev) {
if(ev.down) l.cycle(1)
})
//Command-Left
rw.onKey(0x41, 113, function (ev) {
if(ev.down) l.move(-1)
})
rw.onKey(0x41, 114, function (ev) {
if(ev.down) l.move(1)
})
rw.onKey(0x40, 111, function (ev) {
if(ev.down) cycleLayout(1)
})
rw.onKey(0x40, 116 , function (ev) {
if(ev.down) cycleLayout(-1)
})
//Ctrl-N
rw.onKey(0x40, 46 , function (ev) {
if(!ev.down) return
console.log('new layout')
l.hide()
layouts.push(l = new Layout(rw))
l.show()
})
function close (ev) {
if(l.tiles.length === 0) {
if(layouts.length === 1) {
console.log('all windows are closed')
process.exit(0)
}
return closeLayout(ev)
}
if(ev.down && l.focused) {
var _focused = l.focused
//if there are no tiles in this layout,
//close the space.
if(l.tiles.length > 1)
l.cycle(-1)
_focused.close()
}
}
function closeLayout (ev) {
if(!ev.down) return
var _l = u.relative(layouts, l, -1)
if(layouts.length <= 1) return
l.hide()
l.closeAll()
if(layouts.length) u.remove(layouts, l)
l = _l
_l.show()
}
rw.onKey(0x40, 53, close) //command-Q
rw.onKey(0x40, 59, close) //command-W
rw.onKey(0x41, 59, closeLayout) //command-W
})