-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.js
237 lines (216 loc) · 6.67 KB
/
client.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
var x11 = require('x11');
var jot = require('net');
var keysym = require('keysym');
var spawn = require('child_process').spawn;
let ip = process.argv[2];
let screen_with = 1920
let screen_height= 1080;
console.log("connecting to: tcp://"+ip+':13334')
var client = jot.createConnection({ host: ip, port: 13334 }, function () {
console.log('connected to server!');
// sendCommand({a:'aaaa',b:'bbbb',c:1,d:true});
// sendCommand({a:'aaaa',b:'bbbb',c:2,d:true});
// sendCommand({a:'aaaa',b:'bbbb',c:3,d:true});
// sendCommand({a:'aaaa',b:'bbbb',c:4,d:true});
});
client.on('end', () => {
console.log("socket disconnected");
process.exit(-1);
});
client.on('error', (err) => {
console.log('socket error:', err);
process.exit(-1);
});
client.on('data', function (data) {
console.log("socket receive: " + data.toString());
});
function sendCommand(data) {
client.write(JSON.stringify(data)+'\n');
}
var ks = x11.keySyms;
var ks2Name = {};
for (var key in ks)
ks2Name[ks[key].code] = key;
var kk2Name = {};
var Modifiers = {
17: 'shift',
1: 'shift',
20: 'control',
4: 'control',
24: 'alt',
8: 'alt',
21: ['shift', 'control'],
5: ['shift', 'control'],
25: ['shift', 'alt'],
9: ['shift', 'alt'],
28: ['control', 'alt'],
12: ['control', 'alt'],
};
var KeyMaps = {
return: 'enter',
prior: 'pageup',
next: 'pagedown',
num_lock: '',
caps_lock: '',
grave: '`',
comma: ',',
less: ',',
period: '.',
greater: '.',
slash: '/',
question: '/',
semicolon: ';',
colon: ';',
apostrophe: '\'',
quotedbl: '\'',
backslash: '\\',
bar: '\\',
bracketleft: '[',
braceleft: '[',
bracketright: ']',
braceright: ']',
minus: '-',
underscore: '-',
equal: '=',
plus: '=',
};
function crateWindow() {
x11.createClient(function (err, display) {
var X = display.client;
var root = display.screen[0].root;
var wid = X.AllocID();
var w = 192 * 3;
var h = 108 * 3;
X.CreateWindow(
wid, root, // new window id, parent
0, 0, w, h, // x, y, w, h
0, 0, 0, 0, // border, depth, class, visual
{
eventMask:
// x11.eventMask.Exposure |
x11.eventMask.StructureNotify |
x11.eventMask.PointerMotion |
x11.eventMask.ButtonPress |
x11.eventMask.ButtonRelease |
x11.eventMask.KeyPress |
x11.eventMask.KeyRelease
} // other parameters
);
X.MapWindow(wid);
console.log('wid: ', wid);
var args = [
'--wid=' + wid,
'--profile=low-latency',
'--video-latency-hacks=yes',
'--no-config',
// '--no-input-default-bindings',
'--input-vo-keyboard=no',
// '--input-meida-keys=no',
'--no-input-cursor',
'--idle=yes',
'--osc=no', // On Screen Control
// '--no-osd-bar', // On Screen Display
'--no-cache',
'--untimed',
'--no-correct-pts',
// '--fps=30',
'--load-stats-overlay=yes',
//'--vo=gpu',
'--cursor-autohide=no',
'--autofit-larger=' + 192 * 7, // 当画面卡顿时将该值改小
'--no-keepaspect', // 禁用会导致渲染性能损失
'--no-keepaspect-window',
'tcp://'+ip+':13333'];
console.log('mpv args:', args);
// mpv --show-profile=libmpv
// mpv --show-profile=low-latency
// mpv --wid=106954753 --no-cache --untimed --no-demuxer-thread --vd-lavc-threads=1 tcp://192.168.78.132:13333 --no-input-cursor --no-input-default-bindings --no-config --input-vo-keyboard=no
var player = spawn('mpv', args, { stdio: 'inherit' });
player.on('exit', (code) => {
process.exit('mpv exit: ' + code);
});
var X = display.client;
var min = display.min_keycode;
var max = display.max_keycode;
X.GetKeyboardMapping(min, max - min, function (err, list) {
for (var i = 0; i < list.length; ++i) {
var name = kk2Name[i + min] = [];
var sublist = list[i];
for (var j = 0; j < sublist.length; ++j)
name.push([ks2Name[sublist[j]], sublist[j]]);
}
});
var index = 0;
var mpid;
X.on('event', function (ev) {
// if(ev.name !='MotionNotify' && ev.name != 'ConfigureNotify')
// console.log(index++ + " event: ", ev);
try {
if (ev.name == 'CreateNotify')
mpid = ev.wid;
if (ev.name == 'ConfigureNotify' && ev.wid == wid) {
w = ev.width;
h = ev.height;
// X.ResizeWindow(mpid, ev.width, ev.height);
}
if (ev.name == 'MotionNotify' && ev.wid == wid) {
let data = { type: 'move', x: ev.x * screen_with / w, y: ev.y * screen_height / h };
// console.log(JSON.stringify(data));
sendCommand(data);
}
if ((ev.name == 'ButtonPress' || ev.name == 'ButtonRelease') && (ev.keycode >= 1 && ev.keycode <= 3)) {
let data = {
type: 'button',
button: ev.keycode == 1 ? 'left' : ev.keycode == 2 ? 'middle' : 'right',
state: ev.name == 'ButtonPress' ? 'down' : 'up',
x: Math.round(ev.x * 1920 / w),
y: Math.round(ev.y * 1080 / h)
};
console.log(JSON.stringify(data));
sendCommand(data);
}
if ((ev.name == 'ButtonPress') && (ev.keycode >= 4 && ev.keycode <= 5)) {
let data = {
type: 'scroll',
// button: ev.keycode == 4 ? 'left' : ev.keycode == 2 ? 'middle' : 'right',
// state: ev.name == 'ButtonPress' ? 'down' : 'up',
x: 0,
y: ev.keycode == 4 ? '5' : '-5'
};
console.log(JSON.stringify(data));
sendCommand(data);
}
if (ev.name == 'KeyPress' || ev.name == 'KeyRelease') {
var keySyms = kk2Name[ev.keycode];
// console.log(keySyms);
var shift = ev.buttons == 17;// || ev.buttons == 18; // capslock
var control = ev.buttons == 20;
var alt = ev.buttons == 24;
let name = '';
if (keySyms) {
var code = keysym.fromKeysym(keySyms[shift ? 1 : 0][1]);
if (code === undefined) {
code = keysym.fromKeysym(keySyms[0][1]);
}
if (code) {
name = code.names[0].toLowerCase().replace(/_l$|_r$/, '');
}
if (KeyMaps[name] != undefined) name = KeyMaps[name];
}
if (name.length <= 0) return;
let data = {
type: 'keyboard',
key: name,
state: ev.name == 'KeyPress' ? 'down' : 'up',
modifier: Modifiers[ev.buttons]
};
console.log(JSON.stringify(data));
sendCommand(data);
}
} catch (err) {
console.log("error: ", err);
}
});
});
}
crateWindow();