desky-agent 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -3
- package/bin/cli.js +216 -206
- package/electron/icons.js +79 -79
- package/electron/main.js +244 -219
- package/firmware/firmware.bin +0 -0
- package/firmware/manifest.json +2 -2
- package/package.json +41 -40
- package/src/autostart.js +106 -106
- package/src/board.js +69 -69
- package/src/core.js +193 -172
- package/src/daemon.js +522 -446
- package/src/firmware.js +207 -207
- package/src/flasher.js +113 -113
- package/src/hook.js +92 -86
- package/src/install.js +418 -375
- package/src/link.js +207 -207
- package/src/paths.js +44 -44
- package/src/release.js +131 -0
- package/src/selfupdate.js +58 -0
- package/src/updater.js +138 -138
- package/ui/app.html +1519 -1470
- package/ui/clawd3d.js +377 -375
- package/ui/vendor/OrbitControls.js +1532 -1532
- package/ui/vendor/RoomEnvironment.js +144 -144
- package/ui/vendor/STLLoader.js +410 -410
- package/ui/vendor/three.LICENSE +21 -21
- package/ui/vendor/three.module.min.js +6 -6
package/electron/icons.js
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
// Tray icons: the pixel Desky from the app's favicon, drawn here as PNGs so there are no image files to keep in
|
|
3
|
-
// step. One pose per thing worth seeing at a glance in the tray.
|
|
4
|
-
|
|
5
|
-
const zlib = require('zlib');
|
|
6
|
-
|
|
7
|
-
const ORANGE = [0xd9, 0x77, 0x4b, 255];
|
|
8
|
-
// the body in the tray for each colour Desky is printed in (src/core.js COLORS); graphite a shade lighter, to be seen
|
|
9
|
-
// on a dark taskbar
|
|
10
|
-
const BODY = { orange: ORANGE, white: [0xec, 0xe8, 0xe1, 255], mint: [0x7c, 0xc4, 0xa4, 255], blue: [0x6f, 0xa3, 0xd8, 255],
|
|
11
|
-
graphite: [0x6a, 0x67, 0x63, 255] };
|
|
12
|
-
const GREY = [0x8a, 0x85, 0x80, 255];
|
|
13
|
-
const EYE = [0x22, 0x22, 0x22, 255];
|
|
14
|
-
|
|
15
|
-
/** 16x16 grid of the pose: [x, y, w, h, colour] rectangles. */
|
|
16
|
-
function pose(kind, body) {
|
|
17
|
-
const r = [[2, 3, 12, 9, body], [3, 12, 2, 3, body], [11, 12, 2, 3, body]];
|
|
18
|
-
if (kind === 'ask') r.push([0, 0, 2, 6, body], [14, 0, 2, 6, body]); // arms up: a call for you
|
|
19
|
-
else r.push([0, 6, 2, 3, body], [14, 6, 2, 3, body]);
|
|
20
|
-
if (kind === 'sleep') r.push([5, 7, 2, 1, EYE], [9, 7, 2, 1, EYE]); // eyes shut
|
|
21
|
-
else if (kind === 'read') r.push([5, 7, 2, 3, EYE], [9, 7, 2, 3, EYE]); // looking down at the screen
|
|
22
|
-
else r.push([5, 5, 2, 3, EYE], [9, 5, 2, 3, EYE]);
|
|
23
|
-
return r;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const CRC = new Int32Array(256).map((_, n) => {
|
|
27
|
-
let c = n;
|
|
28
|
-
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
29
|
-
return c;
|
|
30
|
-
});
|
|
31
|
-
function crc32(buf) {
|
|
32
|
-
let c = -1;
|
|
33
|
-
for (const b of buf) c = CRC[(c ^ b) & 0xff] ^ (c >>> 8);
|
|
34
|
-
return (c ^ -1) >>> 0;
|
|
35
|
-
}
|
|
36
|
-
function chunk(type, data) {
|
|
37
|
-
const len = Buffer.alloc(4);
|
|
38
|
-
len.writeUInt32BE(data.length);
|
|
39
|
-
const td = Buffer.concat([Buffer.from(type, 'ascii'), data]);
|
|
40
|
-
const crc = Buffer.alloc(4);
|
|
41
|
-
crc.writeUInt32BE(crc32(td));
|
|
42
|
-
return Buffer.concat([len, td, crc]);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function png(rects, scale) {
|
|
46
|
-
const size = 16 * scale;
|
|
47
|
-
const rgba = Buffer.alloc(size * size * 4);
|
|
48
|
-
for (const [x, y, w, h, c] of rects) {
|
|
49
|
-
for (let yy = y * scale; yy < (y + h) * scale; yy++) {
|
|
50
|
-
for (let xx = x * scale; xx < (x + w) * scale; xx++) Buffer.from(c).copy(rgba, (yy * size + xx) * 4);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
const raw = Buffer.alloc(size * (size * 4 + 1));
|
|
54
|
-
for (let y = 0; y < size; y++) rgba.copy(raw, y * (size * 4 + 1) + 1, y * size * 4, (y + 1) * size * 4);
|
|
55
|
-
const ihdr = Buffer.alloc(13);
|
|
56
|
-
ihdr.writeUInt32BE(size, 0);
|
|
57
|
-
ihdr.writeUInt32BE(size, 4);
|
|
58
|
-
ihdr[8] = 8; // 8 bits per channel
|
|
59
|
-
ihdr[9] = 6; // RGBA
|
|
60
|
-
return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr),
|
|
61
|
-
chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** What the tray shows: no board (grey), asleep, reading, a call for you, or plain Desky. */
|
|
65
|
-
function trayKind(st) {
|
|
66
|
-
if (!st || !st.connected) return 'off';
|
|
67
|
-
if (st.state === 'sleep') return 'sleep';
|
|
68
|
-
if (st.state === 'read') return 'read';
|
|
69
|
-
if (st.state === 'ask') return 'ask';
|
|
70
|
-
return 'on';
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** PNG buffers at 1x, 2x and 3x for a tray kind, in Desky's colour. */
|
|
74
|
-
function trayPngs(kind, color) {
|
|
75
|
-
const rects = pose(kind === 'off' ? 'on' : kind, kind === 'off' ? GREY : BODY[color] || ORANGE);
|
|
76
|
-
return [1, 2, 3].map(scale => ({ scale, buffer: png(rects, scale) }));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
module.exports = { trayPngs, trayKind, png, pose };
|
|
1
|
+
'use strict';
|
|
2
|
+
// Tray icons: the pixel Desky from the app's favicon, drawn here as PNGs so there are no image files to keep in
|
|
3
|
+
// step. One pose per thing worth seeing at a glance in the tray.
|
|
4
|
+
|
|
5
|
+
const zlib = require('zlib');
|
|
6
|
+
|
|
7
|
+
const ORANGE = [0xd9, 0x77, 0x4b, 255];
|
|
8
|
+
// the body in the tray for each colour Desky is printed in (src/core.js COLORS); graphite a shade lighter, to be seen
|
|
9
|
+
// on a dark taskbar
|
|
10
|
+
const BODY = { orange: ORANGE, white: [0xec, 0xe8, 0xe1, 255], mint: [0x7c, 0xc4, 0xa4, 255], blue: [0x6f, 0xa3, 0xd8, 255],
|
|
11
|
+
graphite: [0x6a, 0x67, 0x63, 255] };
|
|
12
|
+
const GREY = [0x8a, 0x85, 0x80, 255];
|
|
13
|
+
const EYE = [0x22, 0x22, 0x22, 255];
|
|
14
|
+
|
|
15
|
+
/** 16x16 grid of the pose: [x, y, w, h, colour] rectangles. */
|
|
16
|
+
function pose(kind, body) {
|
|
17
|
+
const r = [[2, 3, 12, 9, body], [3, 12, 2, 3, body], [11, 12, 2, 3, body]];
|
|
18
|
+
if (kind === 'ask') r.push([0, 0, 2, 6, body], [14, 0, 2, 6, body]); // arms up: a call for you
|
|
19
|
+
else r.push([0, 6, 2, 3, body], [14, 6, 2, 3, body]);
|
|
20
|
+
if (kind === 'sleep') r.push([5, 7, 2, 1, EYE], [9, 7, 2, 1, EYE]); // eyes shut
|
|
21
|
+
else if (kind === 'read') r.push([5, 7, 2, 3, EYE], [9, 7, 2, 3, EYE]); // looking down at the screen
|
|
22
|
+
else r.push([5, 5, 2, 3, EYE], [9, 5, 2, 3, EYE]);
|
|
23
|
+
return r;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const CRC = new Int32Array(256).map((_, n) => {
|
|
27
|
+
let c = n;
|
|
28
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
29
|
+
return c;
|
|
30
|
+
});
|
|
31
|
+
function crc32(buf) {
|
|
32
|
+
let c = -1;
|
|
33
|
+
for (const b of buf) c = CRC[(c ^ b) & 0xff] ^ (c >>> 8);
|
|
34
|
+
return (c ^ -1) >>> 0;
|
|
35
|
+
}
|
|
36
|
+
function chunk(type, data) {
|
|
37
|
+
const len = Buffer.alloc(4);
|
|
38
|
+
len.writeUInt32BE(data.length);
|
|
39
|
+
const td = Buffer.concat([Buffer.from(type, 'ascii'), data]);
|
|
40
|
+
const crc = Buffer.alloc(4);
|
|
41
|
+
crc.writeUInt32BE(crc32(td));
|
|
42
|
+
return Buffer.concat([len, td, crc]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function png(rects, scale) {
|
|
46
|
+
const size = 16 * scale;
|
|
47
|
+
const rgba = Buffer.alloc(size * size * 4);
|
|
48
|
+
for (const [x, y, w, h, c] of rects) {
|
|
49
|
+
for (let yy = y * scale; yy < (y + h) * scale; yy++) {
|
|
50
|
+
for (let xx = x * scale; xx < (x + w) * scale; xx++) Buffer.from(c).copy(rgba, (yy * size + xx) * 4);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const raw = Buffer.alloc(size * (size * 4 + 1));
|
|
54
|
+
for (let y = 0; y < size; y++) rgba.copy(raw, y * (size * 4 + 1) + 1, y * size * 4, (y + 1) * size * 4);
|
|
55
|
+
const ihdr = Buffer.alloc(13);
|
|
56
|
+
ihdr.writeUInt32BE(size, 0);
|
|
57
|
+
ihdr.writeUInt32BE(size, 4);
|
|
58
|
+
ihdr[8] = 8; // 8 bits per channel
|
|
59
|
+
ihdr[9] = 6; // RGBA
|
|
60
|
+
return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr),
|
|
61
|
+
chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What the tray shows: no board (grey), asleep, reading, a call for you, or plain Desky. */
|
|
65
|
+
function trayKind(st) {
|
|
66
|
+
if (!st || !st.connected) return 'off';
|
|
67
|
+
if (st.state === 'sleep') return 'sleep';
|
|
68
|
+
if (st.state === 'read') return 'read';
|
|
69
|
+
if (st.state === 'ask') return 'ask';
|
|
70
|
+
return 'on';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** PNG buffers at 1x, 2x and 3x for a tray kind, in Desky's colour. */
|
|
74
|
+
function trayPngs(kind, color) {
|
|
75
|
+
const rects = pose(kind === 'off' ? 'on' : kind, kind === 'off' ? GREY : BODY[color] || ORANGE);
|
|
76
|
+
return [1, 2, 3].map(scale => ({ scale, buffer: png(rects, scale) }));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { trayPngs, trayKind, png, pose };
|
package/electron/main.js
CHANGED
|
@@ -1,219 +1,244 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
// Desky in the tray: the daemon (port, hooks, the control app's HTTP) lives in this process, the tray icon shows
|
|
3
|
-
// what Desky shows, and the control app opens in a window of its own.
|
|
4
|
-
//
|
|
5
|
-
// Desky open the window (a shortcut, the Applications folder); wakes Desky after "Выйти"
|
|
6
|
-
// Desky --background tray only: a hook started it, or "desky-agent start"
|
|
7
|
-
// Desky --login tray only, at login; a new login undoes an old "Выйти"
|
|
8
|
-
//
|
|
9
|
-
// One instance: a second launch hands its wish (open the window) to the first and quits.
|
|
10
|
-
|
|
11
|
-
const fs = require('fs');
|
|
12
|
-
const http = require('http');
|
|
13
|
-
const path = require('path');
|
|
14
|
-
const { app, BrowserWindow, Menu, Tray, Notification, nativeImage, shell, dialog } = require('electron');
|
|
15
|
-
const paths = require('../src/paths');
|
|
16
|
-
const { Daemon, log } = require('../src/daemon');
|
|
17
|
-
const autostart = require('../src/autostart');
|
|
18
|
-
const { trayPngs, trayKind } = require('./icons');
|
|
19
|
-
|
|
20
|
-
const NAMES = { wake: 'прокинувся', listen: 'слухає', work: 'працює', read: 'читає', ask: 'чекає на тебе',
|
|
21
|
-
error: 'помилка', done: 'закінчив', idle: 'нічого не робить', sleep: 'спить' };
|
|
22
|
-
const MAC = process.platform === 'darwin';
|
|
23
|
-
const quietLaunch = argv => argv.includes('--background') || argv.includes('--login');
|
|
24
|
-
|
|
25
|
-
app.setName('Desky');
|
|
26
|
-
if (process.platform === 'win32') app.setAppUserModelId('desk.clawd.app');
|
|
27
|
-
|
|
28
|
-
let daemon = null;
|
|
29
|
-
let tray = null;
|
|
30
|
-
let win = null;
|
|
31
|
-
let shownKey = '';
|
|
32
|
-
let fwShown = '';
|
|
33
|
-
let
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
app.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
await
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
tray
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
res
|
|
85
|
-
res.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
req.on('
|
|
89
|
-
req.
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
const
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
{
|
|
129
|
-
{ label:
|
|
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
|
-
if (
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (
|
|
218
|
-
|
|
219
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
// Desky in the tray: the daemon (port, hooks, the control app's HTTP) lives in this process, the tray icon shows
|
|
3
|
+
// what Desky shows, and the control app opens in a window of its own.
|
|
4
|
+
//
|
|
5
|
+
// Desky open the window (a shortcut, the Applications folder); wakes Desky after "Выйти"
|
|
6
|
+
// Desky --background tray only: a hook started it, or "desky-agent start"
|
|
7
|
+
// Desky --login tray only, at login; a new login undoes an old "Выйти"
|
|
8
|
+
//
|
|
9
|
+
// One instance: a second launch hands its wish (open the window) to the first and quits.
|
|
10
|
+
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const http = require('http');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const { app, BrowserWindow, Menu, Tray, Notification, nativeImage, shell, dialog } = require('electron');
|
|
15
|
+
const paths = require('../src/paths');
|
|
16
|
+
const { Daemon, log } = require('../src/daemon');
|
|
17
|
+
const autostart = require('../src/autostart');
|
|
18
|
+
const { trayPngs, trayKind } = require('./icons');
|
|
19
|
+
|
|
20
|
+
const NAMES = { wake: 'прокинувся', listen: 'слухає', work: 'працює', read: 'читає', ask: 'чекає на тебе',
|
|
21
|
+
error: 'помилка', done: 'закінчив', idle: 'нічого не робить', sleep: 'спить' };
|
|
22
|
+
const MAC = process.platform === 'darwin';
|
|
23
|
+
const quietLaunch = argv => argv.includes('--background') || argv.includes('--login');
|
|
24
|
+
|
|
25
|
+
app.setName('Desky');
|
|
26
|
+
if (process.platform === 'win32') app.setAppUserModelId('desk.clawd.app');
|
|
27
|
+
|
|
28
|
+
let daemon = null;
|
|
29
|
+
let tray = null;
|
|
30
|
+
let win = null;
|
|
31
|
+
let shownKey = '';
|
|
32
|
+
let fwShown = '';
|
|
33
|
+
let appShown = '';
|
|
34
|
+
let quitting = false;
|
|
35
|
+
|
|
36
|
+
if (!app.requestSingleInstanceLock()) {
|
|
37
|
+
app.exit(0);
|
|
38
|
+
} else {
|
|
39
|
+
app.on('second-instance', (_e, argv) => {
|
|
40
|
+
if (quietLaunch(argv)) return;
|
|
41
|
+
paths.markStopped(false);
|
|
42
|
+
openWindow();
|
|
43
|
+
});
|
|
44
|
+
app.on('window-all-closed', () => { /* stay in the tray */ });
|
|
45
|
+
app.whenReady().then(boot).catch(e => {
|
|
46
|
+
log('не запустився: ' + (e.stack || e));
|
|
47
|
+
dialog.showErrorBox('Desky Agent', 'Desky не запустився: ' + e.message + '\n\nЛог: ' + paths.LOG);
|
|
48
|
+
app.exit(1);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function boot() {
|
|
53
|
+
if (MAC && app.dock) app.dock.hide();
|
|
54
|
+
if (!quietLaunch(process.argv) || process.argv.includes('--login')) paths.markStopped(false);
|
|
55
|
+
menu();
|
|
56
|
+
|
|
57
|
+
daemon = new Daemon({ onStop: () => quit() });
|
|
58
|
+
try {
|
|
59
|
+
await daemon.listen();
|
|
60
|
+
} catch (e) {
|
|
61
|
+
if (e.code !== 'EADDRINUSE') throw e;
|
|
62
|
+
// the old Python daemon (agent/) or a stray instance: ask it to go, then take the port
|
|
63
|
+
await post('/stop');
|
|
64
|
+
await new Promise(r => setTimeout(r, 2500));
|
|
65
|
+
daemon.server.removeAllListeners('error');
|
|
66
|
+
await daemon.listen().catch(() => {
|
|
67
|
+
throw new Error('порт 127.0.0.1:' + paths.port() + ' зайнятий іншою програмою');
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
daemon.start();
|
|
71
|
+
|
|
72
|
+
tray = new Tray(image('off'));
|
|
73
|
+
tray.setToolTip('Desky');
|
|
74
|
+
if (!MAC) tray.on('click', () => openWindow()); // Windows, Linux: a click opens; the menu is on right click
|
|
75
|
+
refresh();
|
|
76
|
+
setInterval(refresh, 1000);
|
|
77
|
+
|
|
78
|
+
if (!quietLaunch(process.argv)) openWindow();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function post(p) {
|
|
82
|
+
return new Promise(resolve => {
|
|
83
|
+
const req = http.request({ host: '127.0.0.1', port: paths.port(), path: p, method: 'POST', timeout: 1500,
|
|
84
|
+
headers: { 'Content-Type': 'application/json', 'Content-Length': 2 } }, res => {
|
|
85
|
+
res.resume();
|
|
86
|
+
res.on('end', resolve);
|
|
87
|
+
});
|
|
88
|
+
req.on('error', resolve);
|
|
89
|
+
req.on('timeout', () => req.destroy());
|
|
90
|
+
req.end('{}');
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function image(kind) {
|
|
95
|
+
const img = nativeImage.createEmpty();
|
|
96
|
+
const color = daemon && daemon.cfg ? daemon.cfg.color : 'orange';
|
|
97
|
+
for (const { scale, buffer } of trayPngs(kind, color)) img.addRepresentation({ scaleFactor: scale, buffer });
|
|
98
|
+
return img;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ---------------------------------------------------------------- the tray
|
|
102
|
+
function refresh() {
|
|
103
|
+
const st = daemon.status();
|
|
104
|
+
const kind = trayKind(st);
|
|
105
|
+
const name = NAMES[st.state] || st.state;
|
|
106
|
+
const fw = st.firmware_update;
|
|
107
|
+
const updating = Boolean(fw && fw.state === 'updating');
|
|
108
|
+
const line = updating ? 'Оновлюю прошивку…' : st.connected ? `Desky ${name}` : 'Плату не під\'єднано';
|
|
109
|
+
if (fw && fw.text !== fwShown) { // one line when the update starts and one when it ends
|
|
110
|
+
fwShown = fw.text;
|
|
111
|
+
notify(fw.text);
|
|
112
|
+
}
|
|
113
|
+
const au = st.app_update || {};
|
|
114
|
+
const appNews = au.text || (au.available ? `Вийшла нова версія Desky: ${au.latest}. Оновити можна в пульті` : '');
|
|
115
|
+
if (appNews && appNews !== appShown) { // a new version once, then the update's start and end
|
|
116
|
+
appShown = appNews;
|
|
117
|
+
notify(appNews, () => openWindow());
|
|
118
|
+
}
|
|
119
|
+
const offer = au.available && au.can && au.state !== 'updating';
|
|
120
|
+
const key = [kind, line, st.manual, st.connected, autostart.isEnabledCached(), daemon.cfg.color,
|
|
121
|
+
offer && au.latest].join('|');
|
|
122
|
+
if (key === shownKey) return;
|
|
123
|
+
shownKey = key;
|
|
124
|
+
tray.setImage(image(kind));
|
|
125
|
+
tray.setToolTip(updating ? 'Desky: ' + fw.text
|
|
126
|
+
: st.connected ? `Desky: ${name} · ${st.port}` : 'Desky: плату не під\'єднано');
|
|
127
|
+
tray.setContextMenu(Menu.buildFromTemplate([
|
|
128
|
+
{ label: line, enabled: false },
|
|
129
|
+
{ label: updating ? 'Не від\'єднуй плату' : st.connected ? `Порт ${st.port}` : 'Під\'єднай кабелем, Desky знайдеться сам',
|
|
130
|
+
enabled: false },
|
|
131
|
+
{ type: 'separator' },
|
|
132
|
+
{ label: 'Відкрити пульт', click: () => openWindow() },
|
|
133
|
+
{ label: 'Повернути агенту', visible: st.manual, click: () => daemon.setManual(false) },
|
|
134
|
+
{ label: 'Перезапустити плату', enabled: st.connected, click: () => daemon.link.reset() },
|
|
135
|
+
{ label: `Оновити Desky до ${au.latest}`, visible: Boolean(offer), click: () => {
|
|
136
|
+
try {
|
|
137
|
+
daemon.startAppUpdate();
|
|
138
|
+
} catch (e) {
|
|
139
|
+
dialog.showErrorBox('Desky Agent', 'Не вдалося: ' + e.message);
|
|
140
|
+
}
|
|
141
|
+
shownKey = '';
|
|
142
|
+
refresh();
|
|
143
|
+
} },
|
|
144
|
+
{ type: 'separator' },
|
|
145
|
+
{ label: 'Запускати під час входу', type: 'checkbox', checked: autostart.isEnabledCached(),
|
|
146
|
+
click: item => {
|
|
147
|
+
try {
|
|
148
|
+
if (item.checked) autostart.enable();
|
|
149
|
+
else autostart.disable();
|
|
150
|
+
} catch (e) {
|
|
151
|
+
dialog.showErrorBox('Desky Agent', 'Не вдалося: ' + e.message);
|
|
152
|
+
}
|
|
153
|
+
autostart.forget();
|
|
154
|
+
shownKey = '';
|
|
155
|
+
refresh();
|
|
156
|
+
} },
|
|
157
|
+
{ label: 'Тека з логом', click: () => shell.openPath(paths.STATE) },
|
|
158
|
+
{ type: 'separator' },
|
|
159
|
+
{ label: 'Вийти', click: () => quit() },
|
|
160
|
+
]));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function notify(body, onClick) {
|
|
164
|
+
try {
|
|
165
|
+
if (!Notification.isSupported()) return;
|
|
166
|
+
const n = new Notification({ title: 'Desky Agent', body, silent: true });
|
|
167
|
+
if (onClick) n.on('click', onClick);
|
|
168
|
+
n.show();
|
|
169
|
+
} catch (e) { /* the tray, the window and the log still say it */ }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ---------------------------------------------------------------- the window
|
|
173
|
+
function bounds() {
|
|
174
|
+
try {
|
|
175
|
+
const b = JSON.parse(fs.readFileSync(paths.WINDOW, 'utf8'));
|
|
176
|
+
if (b.width > 400 && b.height > 300) return b;
|
|
177
|
+
} catch (e) { /* first time */ }
|
|
178
|
+
return { width: 1300, height: 960 };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function openWindow() {
|
|
182
|
+
if (!daemon) return;
|
|
183
|
+
if (win) {
|
|
184
|
+
if (win.isMinimized()) win.restore();
|
|
185
|
+
win.show();
|
|
186
|
+
win.focus();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
win = new BrowserWindow({
|
|
190
|
+
...bounds(), minWidth: 900, minHeight: 600, title: 'Desky Agent', show: false, backgroundColor: '#161514',
|
|
191
|
+
autoHideMenuBar: true, icon: image('on'),
|
|
192
|
+
webPreferences: { contextIsolation: true, sandbox: true, nodeIntegration: false },
|
|
193
|
+
});
|
|
194
|
+
win.loadURL(`http://127.0.0.1:${daemon.httpPort}/`);
|
|
195
|
+
win.once('ready-to-show', () => win.show());
|
|
196
|
+
win.webContents.setWindowOpenHandler(({ url }) => {
|
|
197
|
+
if (/^https?:/.test(url)) shell.openExternal(url);
|
|
198
|
+
return { action: 'deny' };
|
|
199
|
+
});
|
|
200
|
+
win.webContents.on('will-navigate', (e, url) => {
|
|
201
|
+
if (!url.startsWith(`http://127.0.0.1:${daemon.httpPort}/`)) {
|
|
202
|
+
e.preventDefault();
|
|
203
|
+
shell.openExternal(url);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
win.on('close', () => {
|
|
207
|
+
try {
|
|
208
|
+
fs.mkdirSync(paths.STATE, { recursive: true });
|
|
209
|
+
const b = win.getNormalBounds();
|
|
210
|
+
fs.writeFileSync(paths.WINDOW, JSON.stringify(b));
|
|
211
|
+
} catch (e) { /* not worth failing for */ }
|
|
212
|
+
});
|
|
213
|
+
win.on('closed', () => {
|
|
214
|
+
win = null;
|
|
215
|
+
if (MAC && app.dock) app.dock.hide();
|
|
216
|
+
});
|
|
217
|
+
if (MAC && app.dock) app.dock.show();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function menu() {
|
|
221
|
+
if (!MAC) {
|
|
222
|
+
Menu.setApplicationMenu(null);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
// macOS: Cmd+C / Cmd+V in the app's text fields need an Edit menu
|
|
226
|
+
Menu.setApplicationMenu(Menu.buildFromTemplate([
|
|
227
|
+
{ label: 'Desky Agent', submenu: [{ role: 'about' }, { type: 'separator' }, { role: 'hide' }, { type: 'separator' },
|
|
228
|
+
{ label: 'Закрити вікно', accelerator: 'Cmd+W', click: () => win && win.close() },
|
|
229
|
+
{ label: 'Вийти з Desky Agent', accelerator: 'Cmd+Q', click: () => quit() }] },
|
|
230
|
+
{ role: 'editMenu' },
|
|
231
|
+
{ role: 'windowMenu' },
|
|
232
|
+
]));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Leave the tray and free the port. Hooks do not start Desky again until it is opened or the next login. */
|
|
236
|
+
async function quit() {
|
|
237
|
+
if (quitting) return;
|
|
238
|
+
quitting = true;
|
|
239
|
+
paths.markStopped(true);
|
|
240
|
+
if (win) win.destroy();
|
|
241
|
+
if (tray) tray.destroy();
|
|
242
|
+
if (daemon) await daemon.close();
|
|
243
|
+
app.exit(0);
|
|
244
|
+
}
|
package/firmware/firmware.bin
CHANGED
|
Binary file
|