-
Notifications
You must be signed in to change notification settings - Fork 8
/
renderer.js
481 lines (458 loc) · 16.1 KB
/
renderer.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* globals document, $, setInterval, ctr, alert */
const {
exec,
execFile,
execFileSync,
spawn
} = require('mz/child_process');
const {ipcRenderer} = require('electron');
const {CONFIG} = require('./config.js');
const WINDOW_HEIGHT = 302;
const PADDING_PX = 4;
const TOUCHPAD_LENGTH_X = CONFIG.touchpad_support.touchpad_coords.max.x - CONFIG.touchpad_support.touchpad_coords.min.x;
const TOUCHPAD_LENGTH_Y = CONFIG.touchpad_support.touchpad_coords.max.y - CONFIG.touchpad_support.touchpad_coords.min.y;
const DRAW_AREA_HEIGHT = 193.99;
const SELECT_AREA_HEIGHT = 40.99;
const WINDOW_WIDTH = Math.round((WINDOW_HEIGHT * (TOUCHPAD_LENGTH_X / TOUCHPAD_LENGTH_Y)) * ((DRAW_AREA_HEIGHT + SELECT_AREA_HEIGHT) / WINDOW_HEIGHT)) + PADDING_PX;
const DRAW_AREA_WIDTH = WINDOW_WIDTH;
const AREA_START_X = PADDING_PX;
const AREA_END_X = Math.floor(DRAW_AREA_WIDTH) - PADDING_PX;
const AREA_START_Y = PADDING_PX;
const AREA_END_Y = Math.floor(DRAW_AREA_HEIGHT + SELECT_AREA_HEIGHT) - PADDING_PX;
console.log('Area', AREA_START_X, AREA_END_X, AREA_START_Y, AREA_END_Y);
let helper;
if (CONFIG.use_clipboard) {
helper = spawn('python3', ['gnome-helper.py']);
}
let thisWindowID;
let activeWindowID;
let lastWindowID;
let windowWidth;
let currentTimeout;
let pointer = {style: {}};
let hint = {style: {}};
const States = {
TOUCHPAD_INIT: 1,
TOUCHPAD_READY: 2,
TOUCHPAD_IDLE: 3,
DRAWING_START_TOUCH: 4,
DRAWING_MOVING: 5,
DRAWING_END_TOUCH: 6,
SELECTING_START_TOUCH: 7,
SELECTING_MOVING: 8,
SELECTING_END_TOUCH: 9
};
let state = States.TOUCHPAD_INIT;
const focusLastWindow = async () => {
if (!lastWindowID) {
console.error('Last window is empty');
return false;
}
await exec(`xdotool windowfocus ${lastWindowID}`);
return true;
};
if (CONFIG.touchpad_support.enabled) {
const findTouchpadXInputID = async () => {
let touchpadXInputID;
try {
let [out] = await execFile('xinput', ['list']);
out.split('\n').some((line) => {
if (line.toLowerCase().includes('touchpad') || line.toLowerCase().includes('finger')) {
if (CONFIG.touchpad_support.device_blacklist.some(d => line.toLowerCase().includes(d.toLowerCase()))) return false;
line.split('\t').some((col) => {
if (col.startsWith('id=')) {
[, touchpadXInputID] = col.split('=');
return true;
}
return false;
});
return true;
}
return false;
});
if (touchpadXInputID === null) {
throw new Error(`Please manually edit renderer.js and fill TOUCHPAD_XINPUT_ID as id={number} below.\n\n${out}`);
}
} catch (e) {
console.error(e);
throw new Error(`xinput is required! ${JSON.stringify(e)}`);
}
return touchpadXInputID;
};
(async () => {
let touchpadXInputID = CONFIG.touchpad_support.preferred_device_id.xinput;
if (touchpadXInputID === null) {
try {
touchpadXInputID = await findTouchpadXInputID();
} catch (e) {
alert(e.message);
return;
}
}
console.log(`touchpadXinputID: ${touchpadXInputID}`);
let xinput = spawn('xinput', ['test-xi2', '--root']);
let unclutter;
xinput.stdout.on('data', async (data) => {
let line = data.toString();
if (line.startsWith('EVENT type 14 ')) {
let key = line.split('\n')[2].split(' ')[5];
if (CONFIG.touchpad_support.key_mappings.escape[key]) {
console.log('Escape');
execFileSync('xinput', ['enable', touchpadXInputID]);
if (unclutter) {
unclutter.kill();
unclutter = null;
}
pointer.style.opacity = 0;
hint.style.opacity = 0;
state = States.TOUCHPAD_IDLE;
}
if (CONFIG.touchpad_support.key_mappings.clear_timeout[key]) {
console.log('Clear timer');
if (currentTimeout) {
clearTimeout(currentTimeout);
currentTimeout = null;
}
if (state !== States.TOUCHPAD_IDLE && state !== States.TOUCHPAD_INIT) {
hint.style.opacity = 0.5;
}
ipcRenderer.sendToHost(JSON.stringify({
type: 'keyDown', keyCode: 'Backspace'
}));
ipcRenderer.sendToHost(JSON.stringify({
type: 'keyUp', keyCode: 'Backspace'
}));
}
}
});
let evtest = spawn('evtest');
let touchpadEventID = CONFIG.touchpad_support.preferred_device_id.dev_event;
let availableDevicesMsg = '';
let absX = null;
let absY = null;
evtest.stdout.on('data', (data) => {
if (state === States.TOUCHPAD_INIT || state === States.TOUCHPAD_IDLE) {
return;
}
let oldState = state;
let lines = data.toString();
let touchEventCols;
lines.split('\n').forEach((line) => {
let cols = line.trim().split(' ');
if (cols[8] === '(ABS_X),') {
absX = parseInt(cols[10], 10);
} else if (cols[8] === '(ABS_Y),') {
absY = parseInt(cols[10], 10);
} else if (cols[8] === '(BTN_TOUCH),') {
touchEventCols = cols;
}
});
if (touchEventCols) {
let touchOn = parseInt(touchEventCols[10], 10) === 1;
console.log('TouchOn', touchOn);
if (touchOn) {
pointer.style.opacity = 1;
hint.style.opacity = 0;
let isOptionSelect = absY / CONFIG.touchpad_support.touchpad_coords.max.y > DRAW_AREA_HEIGHT / (DRAW_AREA_HEIGHT + SELECT_AREA_HEIGHT);
let isMoving = state === States.SELECTING_MOVING || state === States.DRAWING_MOVING;
if (isOptionSelect) {
if (!isMoving) {
state = States.SELECTING_START_TOUCH;
}
} else if (!isMoving) {
state = States.DRAWING_START_TOUCH;
}
} else if (state === States.DRAWING_MOVING) {
pointer.style.opacity = 0;
state = States.DRAWING_END_TOUCH;
} else if (state === States.SELECTING_MOVING) {
pointer.style.opacity = 0;
state = States.SELECTING_END_TOUCH;
}
}
let relX = AREA_START_X + Math.floor((AREA_END_X - AREA_START_X) * ((absX - CONFIG.touchpad_support.touchpad_coords.min.x) / TOUCHPAD_LENGTH_X));
let relY = AREA_START_Y + Math.floor((AREA_END_Y - AREA_START_Y) * ((absY - CONFIG.touchpad_support.touchpad_coords.min.y) / TOUCHPAD_LENGTH_Y));
if (currentTimeout) {
clearTimeout(currentTimeout);
currentTimeout = null;
}
if (state === States.SELECTING_START_TOUCH) {
state = States.SELECTING_MOVING;
spawn('xinput', ['disable', touchpadXInputID]);
if (lastWindowID) {
spawn('xdotool', ['windowfocus', lastWindowID]);
}
if (!unclutter) {
unclutter = spawn('unclutter', ['-idle', '0.01']);
}
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseMove', x: relX, y: relY
}));
// spawn('xdotool', ['mousemove', '-w', thisWindowID, relX, relY]);
} else if (state === States.DRAWING_START_TOUCH) {
state = States.DRAWING_MOVING;
spawn('xinput', ['disable', touchpadXInputID]);
if (lastWindowID) {
spawn('xdotool', ['windowfocus', lastWindowID]);
}
if (!unclutter) {
unclutter = spawn('unclutter', ['-idle', '0.01']);
}
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseDown', x: relX, y: relY, button: 'left', clickCount: 1
}));
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseMove', x: relX, y: relY
}));
// spawn('xdotool', ['mousemove', '-w', thisWindowID, relX, relY, 'mousedown', '-w', thisWindowID, '1']);
} else if (state === States.SELECTING_MOVING) {
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseMove', x: relX, y: relY
}));
// spawn('xdotool', ['mousemove', '-w', thisWindowID, relX, relY]);
} else if (state === States.DRAWING_MOVING) {
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseMove', x: relX, y: relY
}));
// spawn('xdotool', ['mousemove', '-w', thisWindowID, relX, relY]);
} else if (state === States.DRAWING_END_TOUCH) {
state = States.TOUCHPAD_READY;
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseUp', x: relX, y: relY
}));
// spawn('xdotool', ['mouseup', '-w', thisWindowID, '1']);
// spawn('xinput', ['enable', touchpadXInputID]);
currentTimeout = setTimeout(() => {
// let out = execFileSync('xdotool', ['getmouselocation']).toString();
// let [x, y] = [out.split(' ')[0].split(':')[1], out.split(' ')[1].split(':')[1]];
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseDown', x: AREA_START_X, y: AREA_END_Y, button: 'left', clickCount: 1
}));
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseUp', x: AREA_START_X, y: AREA_END_Y, button: 'left', clickCount: 1
}));
// spawn('xdotool', ['mousemove', '-w', thisWindowID, AREA_START_X, AREA_END_Y, 'click', '1', 'mousemove', x, y]);
}, CONFIG.touchpad_support.candidate_timeout_ms);
} else if (state === States.SELECTING_END_TOUCH) {
state = States.TOUCHPAD_READY;
// spawn('xdotool', ['click', '1']);
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseDown', x: relX, y: relY, button: 'left', clickCount: 1
}));
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseUp', x: relX, y: relY, button: 'left', clickCount: 1
}));
// spawn('xinput', ['enable', touchpadXInputID]);
}
pointer.style.left = `${relX}px`;
pointer.style.top = `${relY}px`;
console.log('In', oldState, 'Out', state, 'thiswindow', thisWindowID, 'activewindow', activeWindowID);
});
evtest.stderr.on('data', (data) => {
let lines = data.toString();
lines.split('\n').forEach((line) => {
if (touchpadEventID === null && (line.toLowerCase().includes('touchpad') || line.toLowerCase().includes('finger'))
&& !CONFIG.touchpad_support.device_blacklist.some(d => line.toLowerCase().includes(d.toLowerCase()))) {
[touchpadEventID] = line.replace('/dev/input/event', '').split(':');
}
if (line.includes('Select the device event number')) {
if (touchpadEventID === null) {
alert(`Please manually edit renderer.js and fill TOUCHPAD_DEVICE_ID as /dev/input/event{number} below.\n\n${availableDevicesMsg}`);
} else {
console.log(`touchpadEventID: ${touchpadEventID}`);
evtest.stdin.write(`${touchpadEventID}\n`);
state = States.TOUCHPAD_IDLE;
}
} else if (state === States.TOUCHPAD_INIT) {
availableDevicesMsg += line;
}
});
});
evtest.on('error', (err) => {
console.error(err);
alert('evtest is required!', JSON.stringify(err));
});
xinput.on('error', (err) => {
console.error(err);
alert('xinput is required!', JSON.stringify(err));
});
})();
}
const sleep = (ms) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});
};
const initUI = async () => {
$('body').css({
overflow: 'hidden'
});
windowWidth = $('body').width();
let method = 1;
try {
ctr.Ea.b.C.B.C[2].C.view.Ui;
} catch (e) {
console.error(e);
console.log('Using method 2');
method = 2;
}
while (windowWidth) {
try {
$('body > ul > li:nth-child(7)').click(); // Toggle handwrite
if (method === 1) {
ctr.Ea.b.gh();
} else if (method === 2) {
// ctr.Ea.b.gh('zh-tw'); // Spawn it
await sleep(CONFIG.ui_poll_interval_ms);
if (!$('.goog-container.goog-container-vertical.ita-hwt-ime.ita-hwt-ltr.notranslate.ita-hwt-ime-st.ita-hwt-ime-init-opaque').is(':visible')) {
throw new Error();
}
}
break;
} catch (e) {
// Element may not be ready?
console.error(e);
await sleep(CONFIG.ui_poll_interval_ms);
}
}
if (method === 1) {
ctr.Ea.b.C.B.C[2].C.view.Ui(); // Toggle full size
} else if (method === 2) {
let {left, top} = $('.ita-hwt-grip').offset();
let [x, y] = [left + ($('.ita-hwt-grip').width() / 2), top + ($('.ita-hwt-grip').height() / 2)];
ipcRenderer.sendToHost(JSON.stringify({
type: 'mouseMove', x, y
}));
for (let i = 0; i < 2; i++) {
let evt = {
type: ['mouseDown', 'mouseUp'][i % 2], x, y, button: 'left', clickCount: 2
};
console.log(evt);
ipcRenderer.sendToHost(JSON.stringify(evt));
}
await sleep(CONFIG.ui_poll_interval_ms);
}
$('.ita-hwt-grip').remove();
$('.ita-hwt-close').remove();
};
const getNumberOutput = async (cmd) => {
return parseInt((await exec(cmd))[0].trim(), 10);
};
const main = async () => {
await initUI();
thisWindowID = await getNumberOutput('xdotool search "Google Chinese Handwriting IME"');
$('.ita-hwt-backspace').click(async () => {
await focusLastWindow();
if (helper) {
helper.stdin.write('bs!!\n');
} else {
await exec('xdotool key BackSpace');
}
});
$('.ita-hwt-canvas').click(() => {
if (state === States.TOUCHPAD_IDLE) {
state = States.TOUCHPAD_READY;
hint.style.opacity = 0.5;
}
});
setInterval(async () => {
activeWindowID = await getNumberOutput('xdotool getactivewindow');
if (activeWindowID !== thisWindowID) {
lastWindowID = activeWindowID;
}
let val = $('#source').val();
if (val.length > 0) {
$('#source').val('');
if (!CONFIG.touchpad_support.enabled) {
let suc = await focusLastWindow();
if (!suc) {
return;
}
await sleep(CONFIG.ui_poll_interval_ms / 2);
}
if (state === States.TOUCHPAD_READY) {
hint.style.opacity = 0.5;
}
if (activeWindowID !== thisWindowID) {
if (helper) {
console.log('helper', val);
helper.stdin.write(`${val}\n`);
} else {
console.log('xdotool', val);
await execFile('xdotool', ['type', val]);
}
}
if (!CONFIG.touchpad_support.enabled) {
await execFile('xdotool', ['windowfocus', thisWindowID]);
}
}
let newWidth = $('body').width();
if (windowWidth !== newWidth) {
ctr.Ea.b.C.A.C[2].C.view.Ui();
ctr.Ea.b.C.A.C[2].C.view.Ui();
windowWidth = newWidth;
}
}, CONFIG.ui_poll_interval_ms);
$(() => {
$('body').append(`
<style>
.ita-hwt-candidate {
padding: 6px 0px 3px 0px !important;
text-align: center;
flex: 1;
}
.ita-hwt-candidates {
display: flex;
}
#pointer {
transition: opacity 0.2s ease;
background-color: rgba(0, 0, 0, 0.5);
opacity: 0;
border-radius: 1em;
transform: translate(-1em, -1em);
width: 2em;
height: 2em;
left: 0px;
top: 0px;
pointer-events: none;
z-index: 9999999999;
position: absolute;
backdrop-filter: blur(8px);
}
#hint {
transition: opacity 0.2s ease;
opacity: 0;
font-size: 4.5em;
z-index: 9999999999;
position: absolute;
pointer-events: none;
width: 100%;
height: 100%;
left: 0;
top: -20px;
font-weight: 100;
text-align: center;
line-height: 1em;
}
</style>
`);
pointer = document.createElement('div');
pointer.id = 'pointer';
document.body.appendChild(pointer);
hint = document.createElement('h1');
hint.id = 'hint';
hint.appendChild(document.createTextNode('請手寫文字'));
hint.appendChild(document.createElement('br'));
hint.appendChild(document.createTextNode('按Esc鍵離開'));
document.body.appendChild(hint);
});
};
document.addEventListener('DOMContentLoaded', (event) => {
let script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js';
script.onreadystatechange = main;
script.onload = script.onreadystatechange;
document.body.appendChild(script);
});