-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3wm.js
311 lines (246 loc) · 5.85 KB
/
i3wm.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const os = require('os')
const childProc = require('child_process')
const net = require('net')
const EventEmitter = require('events')
/*******************************
* PROTOCOL
*******************************/
/**
* Message/reply format
* "i3-ipc" <message length> <message type> <payload>
*
* Length and types are u32.
*/
const MAGIC = 'i3-ipc'
// Byes
const B_M = MAGIC.length
const B_N = 4 // long
// Offsets
const O_M = 0
const O_L = B_M // length
const O_T = B_M + B_N // message type
const O_P = B_M + B_N + B_N // message payload
// See: https://i3wm.org/docs/ipc.html#_sending_messages_to_i3
const MESSAGES = {
RUN_COMMAND: 0,
GET_WORKSPACES: 1,
SUBSCRIBE: 2,
GET_OUTPUTS: 3,
GET_TREE: 4,
GET_MARKS: 5,
GET_BAR_CONFIG: 6,
GET_VERSION: 7,
GET_BINDING_MODES: 8,
GET_CONFIG: 9,
SEND_TICK: 10,
SYNC: 11,
}
// See: https://i3wm.org/docs/ipc.html#_available_events
const EVENTS_MAP = {
0: 'workspace',
1: 'output',
2: 'mode',
3: 'window',
4: 'barconfig_update',
5: 'binding',
6: 'shutdown',
7: 'tick',
}
// See: https://i3wm.org/docs/ipc.html#_reply_format
const REPLIES = {
COMMAND: 0,
WORKSPACES: 1,
SUBSCRIBE: 2,
OUTPUTS: 3,
TREE: 4,
MARKS: 5,
BAR_CONFIG: 6,
VERSION: 7,
BINDING_MODES: 8,
GET_CONFIG: 9,
TICK: 10,
SYNC: 11,
}
/**
* Used in message object, holding metadata.
*/
const Meta = Symbol('i3wm Meta')
const getSocketPath = async (bin = 'i3') => {
return new Promise((resolve, reject) => {
const cmd = [bin, '--get-socketpath']
childProc.exec(cmd.join(' '), (err, stdout) => {
if (err) {
return reject(err)
}
resolve(stdout.toString().trim())
})
})
}
const encodePayload = (data) => {
return typeof data === 'object'
? JSON.stringify(data)
: String(data)
}
const encodeMessage = (type, payload) => {
const payloadData = encodePayload(payload)
const length = Buffer.byteLength(payloadData, 'ascii')
const b = Buffer.alloc(
B_M +
B_N + // length
B_N + // type
length
)
b.write(MAGIC, O_M, 'ascii')
b.writeUInt32LE(length, O_L)
b.writeUInt32LE(type, O_T)
b.write(payloadData, O_P, 'ascii')
return b
}
const encodeCommand = (cmd, ...args) => {
const _args = args.map(encodePayload)
const payload = _args.length > 0
? [cmd, ..._args].join(' ')
: cmd
return encodeMessage(MESSAGES.RUN_COMMAND, payload)
}
/**
* Reads u32 used in protocol.
*
* Integers are not converted by i3 so endiance must be checked.
*/
const readInt = (() => {
const BUFFER_READ_INT_FN = 'readUInt32' + os.endianness()
return (buffer, offset = 0) => {
return buffer[BUFFER_READ_INT_FN](offset)
}
})()
const decodeMessage = (data) => {
const length = readInt(data, O_L)
const rawType = readInt(data, O_T)
const isEvent = rawType >>> 31 === 1 // highest-bit = 1 -> event
const type = isEvent
? rawType ^ (1 << 31) // toggle highest-bit
: rawType
const payload = data.slice(O_P, O_P + length).toString()
const decoded = JSON.parse(payload)
decoded[Meta] = {
isEvent,
type,
}
return decoded
}
/*******************************
* CLIENT
*******************************/
class ReplyTimeoutError extends Error { }
const REPLY_TIMEOUT = 200
class Client extends EventEmitter {
static async connect({
bin = 'i3'
} = {}) {
const sock = await getSocketPath(bin)
const conn = net.createConnection(sock)
const client = new Client
client._conn = conn;
conn.on('data', (data) => {
const msg = decodeMessage(data)
client.emit('_message', msg)
})
client.on('_write', (data) => {
conn.write(data)
})
return client
}
static disconnect(client) {
client._conn.destroy()
}
constructor() {
super()
this.on('_message', (msg) => {
const { type, isEvent } = msg[Meta]
if (isEvent) {
this._handleEvent(msg, type)
} else {
this._handleReply(msg, type)
}
})
}
message(type, payload) {
if (typeof type === 'string') {
const foundType = MESSAGES[type.toUpperCase()]
if (foundType === undefined) {
throw new Error(`Message type '${type}' is incorrect`)
}
type = foundType;
}
const data = encodeMessage(type, payload)
this._write(data)
return this._promiseImmidiateReplay()
}
/**
* Sends single command.
*/
async command(command, ...payload) {
const data = encodeCommand(command, ...payload)
this._write(data)
const [r1] = await this._promiseImmidiateReplay()
return pipeSuccessReply(r1)
}
subscribe(...events) {
return this.message(MESSAGES.SUBSCRIBE, events)
.then(pipeSuccessReply)
}
sync() {
return this.message(MESSAGES.SYNC)
.then(pipeSuccessReply)
}
tick(payload) {
return this.message(MESSAGES.SEND_TICK, payload)
.then(pipeSuccessReply)
}
_handleReply(message) {
this.emit('_reply', message)
}
_handleEvent(message, type) {
const eventName = EVENTS_MAP[type]
this.emit(eventName, message)
}
_write(data) {
this.emit('_write', data)
}
_promiseImmidiateReplay() {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new ReplyTimeoutError('Reply timeout'))
}, REPLY_TIMEOUT)
// Crypting name to make it easy to identify handlers
// added by this block.
const _i3wm_handler = (message) => {
resolve(message)
this.off('_reply', _i3wm_handler)
clearTimeout(timer)
}
this.on('_reply', _i3wm_handler)
})
}
}
/**
* Some replies may fail. Throws if they do.
*/
const pipeSuccessReply = (reply) => {
if (!reply.success) {
throw new Error('Unsuccessful replied')
}
return reply
}
module.exports = {
Client,
ReplyTimeoutError,
MESSAGES,
REPLIES,
Meta,
getSocketPath,
encodePayload,
encodeMessage,
encodeCommand,
}