-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
113 lines (95 loc) · 3.14 KB
/
test.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
'use strict';
const constants = {
MAGIC_STRING: "i3-ipc",
TYPES: {
COMMAND: 0,
GET_WORKSPACES: 1,
SUBSCRIBE: 2,
GET_OUTPUTS: 3,
GET_TREE: 4,
GET_MARKS: 5,
GET_BAR_CONFIG: 6
},
EVENTS: {
WORKSPACE: 0,
OUTPUT: 1
}
}
const utils = {
packInt: function packInt() {
console.log(arguments)
var args = Array.prototype.slice.call(arguments);
console.log(args)
var result = '';
args.forEach(function (e) {
console.log(String.fromCharCode(e & 0xFF));
console.log(String.fromCharCode(e >> 8 & 0xFF));
console.log(String.fromCharCode(e >> 16 & 0xFF));
console.log(String.fromCharCode(e >> 24 & 0xFF));
result += String.fromCharCode(e & 0xFF);
result += String.fromCharCode(e >> 8 & 0xFF);
result += String.fromCharCode(e >> 16 & 0xFF);
result += String.fromCharCode(e >> 24 & 0xFF);
});
return result;
},
unpackInt: function unpackInt(str) {
var b1 = str.charCodeAt(0) & 0xFF;
var b2 = str.charCodeAt(1) & 0xFF;
var b3 = str.charCodeAt(2) & 0xFF;
var b4 = str.charCodeAt(3) & 0xFF;
return (((((b4 << 8) + b3) << 8) + b2) << 8) + b1;
}
}
function format(type, payload) {
const size = payload ? payload.length : 0;
let msg = constants.MAGIC_STRING;
msg += utils.packInt(size, type);
if (payload)
msg += payload;
return msg;
}
function parse(data) {
if (data.slice(0, constants.MAGIC_STRING.length).toString() != constants.MAGIC_STRING) {
return { error: 'wrong magic code' };
}
var size = utils.unpackInt(data.slice(constants.MAGIC_STRING.length, constants.MAGIC_STRING.length + 4).toString());
var type = utils.unpackInt(data.slice(constants.MAGIC_STRING.length + 4, constants.MAGIC_STRING.length + 8).toString());
/**
* Everything after `size` is discarded.
* Maybe re-enqueue the left-over data;
* this way the whole communication could work
* without queueing the messages internally.
**/
var answer = data.slice(constants.MAGIC_STRING.length + 8, data.length);
var json = {};
json.data = JSON.parse(answer.toString());
json.size = size;
/**
* got an event? high bit is set!
* the implemantation of unpackInt
* seems a little bit wrong;
* should return 1 here, right?
**/
if ((type >> 31) == -1) {
json.event = true;
type = type & 0x7f;
}
json.type = type;
return json;
}
const net = require('net');
const unixClient = net.createConnection(process.env.I3SOCK);
console.log('format' + format(constants.TYPES.GET_WORKSPACES))
unixClient.write(format(constants.TYPES.GET_WORKSPACES))
unixClient.on('connection', (s) => {
unixClient.write(format(constants.TYPES.GET_WORKSPACES))
console.log('got connection!', s);
// s.write('i3-ipc' + Buffer.from('1').toString());
// s.end();
});
unixClient.on('data', (s) => {
console.log('got data!', parse(s));
// s.write('i3-ipc' + Buffer.from('1').toString());
// s.end();
});