-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
342 lines (316 loc) · 13.3 KB
/
service.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
const path = require('path');
const isDev = require('electron-is-dev');
const fs = require('fs');
const Jimp = require('jimp');
const promisify = require('util.promisify');
const AsyncLock = require('async-lock');
const {tmpdir} = require('os');
const Registry = require('winreg');
const {simpleClone, shuffle} = require('./utils.js');
const ad = require('./winapi/ActiveDesktop.js');
const winapi = require('./winapi/API.js');
const DataStore = require('./datastore.js');
const logger = require('./logger.js');
const monitorinfo = require('./monitorinfo.js');
var exports = module.exports = {};
exports.startService = function(argv) {
const BG_FN = 'wallpaper-switcher-background.png';
const BG_PATH = path.join(tmpdir(), BG_FN);
var status, profiles, monitors;
var profiles_store = new DataStore('config.json');
var status_store = new DataStore('status.json');
var timer = null;
var lock = new AsyncLock();
var iad = ad.create(null, true);
const defaultProfile = {
background: 'picture',
shuffle: false,
fit: 'fill',
picture_path: '',
slideshow_path: '',
interval: 30
};
const regex_image = /.+\.(jpg|jpeg|gif|png|bmp)$/;
Promise.all([
promisify(profiles_store.load.bind(profiles_store))(),
promisify(status_store.load.bind(status_store))(),
monitorinfo.load()
]).then((loaded_val) => {
profiles = simpleClone(loaded_val[0]);
status = simpleClone(loaded_val[1]);
monitors = simpleClone(loaded_val[2]);
status.current = status.current || {};
status.future = status.future || {};
return boot();
}).catch((err) => {
logger.error(err.message);
process.exit(-1);
});
function boot() {
updateState()
.then((profile_updated) => {
if(profile_updated) {
return saveProfile();
}
return true;
})
.then(commitBackground)
.then(saveStatus)
.then(setTimer)
.then(() => {
monitorinfo.on('change', onUpdateMonitor);
profiles_store.on('change', onUpdateProfile);
logger.info('daemon started');
})
;
}
function updateState() {
status.tick = Date.now();
return Promise.all(monitors.monitors.map((monitor) => {
var id = monitor.id;
let profile_updated = false;
if(!profiles[id]) {
profiles[id] = simpleClone(defaultProfile);
profile_updated = true;
}
var profile = profiles[id];
Object.keys(defaultProfile).forEach((k) => {
if(!profile.hasOwnProperty(k)) {
profile[k] = defaultProfile[k];
profile_updated = true;
}
});
var interval_ms = 1000 * 60 * profiles[id].interval;
if(profile.background === 'slideshow') {
if(!status.future[id]) {
return createFuture(id).then(() => profile_updated);
}
else {
var nextTick = status.future[id].nextTick || (status.tick + interval_ms);
if(nextTick < status.tick + 60 * 1000) {
//Advance tick
status.future[id].nextTick += Math.ceil((status.tick + 60 * 1000 - nextTick) / (interval_ms)) * interval_ms;
//Try shift
var shift_success = (fn) => {
status.current[id] = {active: fn};
return profile_updated;
};
var shift_fail = () => {
if(status.future[id].queue.length === 0) {
return createFuture(id).then(() => profile_updated);
}
return checkFile(profile.slideshow_path, status.future[id].queue.shift())
.then(shift_success, shift_fail);
}
return checkFile(profile.slideshow_path, status.future[id].queue.shift())
.then(shift_success, shift_fail);
}
}
}
return profile_updated;
})).then((val) => val.reduce((updated, v) => updated || v, false));
}
function commitBackground() {
return Promise.all(monitors.monitors.map((monitor) => {
var id = monitor.id;
var rect = monitor.rect;
var profile = profiles[id];
if(profile.background === 'picture' || profile.background === 'slideshow') {
try {
var img_path = profile.background === 'picture'
? profile.picture_path
: path.join(profile.slideshow_path, status.current[id].active);
}
catch(err) {
return new Jimp(rect.Right - rect.Left, rect.Bottom - rect.Top);
}
return Jimp.read(img_path).then((img) => {
var width = rect.Right - rect.Left, height = rect.Bottom - rect.Top;
var canvas = new Jimp(width, height);
switch(profile.fit) {
case 'fill':
return img.cover(width, height);
case 'fit':
return img.contain(width, height);
case 'stretch':
return img.resize(width, height);
case 'tile':
for(let x = 0; x < width; x += img.bitmap.width) {
for(let y = 0; y < height; y += img.bitmap.height) {
canvas = canvas.blit(img, x, y);
}
}
return canvas;
case 'center':
var sw = Math.min(width, img.bitmap.width),
sh = Math.min(height, img.bitmap.height);
var sx = (img.bitmap.width - sw) / 2,
sy = (img.bitmap.height - sh) / 2,
tx = (width - sw) / 2,
ty = (height - sh) / 2;
return canvas.blit(img, tx, ty, sx, sy, sw, sh);
}
}).catch((err) => {
//Fallback to pure black background
return new Jimp(rect.Right - rect.Left, rect.Bottom - rect.Top);
});
}
else {
return new Jimp(rect.Right - rect.Left, rect.Bottom - rect.Top);
}
})).then((images) =>
monitors.monitors
.reduce((canvas, monitor, idx) => canvas.blit(images[idx], monitor.rect.Left, monitor.rect.Top),
new Jimp(monitors.width, monitors.height))
).then((background) => promisify(background.write.bind(background))(BG_PATH)
).then(() =>
promisify(winapi.FindWindow)({lpClassName: 'Progman', lpWindowName: null})
.then((hwnd) => promisify(winapi.SendMessageTimeout)({
hwnd: hwnd,
Msg: 0x52c,
wParam: 0,
lParam: 0,
fuFlags: 0,
uTimeout: 500
}))
.then(() => promisify(iad.SetWallpaper)({pwszWallpaper: BG_PATH}))
).then(() => promisify(iad.SetWallpaperOptions)({pwpo: {dwStyle: ad.WallpaperStyle.Span}})
).then(() => promisify(iad.ApplyChanges)({dwFlags: ad.Apply.All}));
}
function setTimer() {
var nextTick = monitors.monitors.filter((monitor) =>
profiles[monitor.id]
&& profiles[monitor.id].background === 'slideshow'
&& status.future[monitor.id]
&& status.future[monitor.id].nextTick)
.reduce((tick, monitor) => Math.min(tick, status.future[monitor.id].nextTick), Number.MAX_SAFE_INTEGER);
timer = setTimeout(onUpdateSlideshow, Math.min(2147483647, Math.max(0, nextTick - Date.now())));
}
function onUpdateSlideshow() {
lock.acquire('everything', () => {
clearTimeout(timer);
return endUpdate();
});
}
function onUpdateProfile(_profiles) {
lock.acquire('everything', () => {
let old_profiles = simpleClone(profiles);
profiles = _profiles;
let status_updated = false;
let need_render = monitors.monitors.map((monitor) => {
var id = monitor.id;
let old_profile = old_profiles[id] || defaultProfile;
let profile = profiles[id] || defaultProfile;
if(status.future[id]
&& (old_profile.slideshow_path !== profile.slideshow_path
|| old_profile.shuffle !== profile.shuffle)) {
status_updated = true;
delete status.future[id];
}
if(status.future[id] && old_profile.interval !== profile.interval) {
status_updated = true;
status.future[id].nextTick = status.tick + profile.interval * 1000 * 60;
}
return (old_profile.background !== profile.background)
|| (old_profile.fit !== profile.fit)
|| (profile.background === 'picture'
&& (old_profile.picture_path !== profile.picture_path))
|| (profile.background === 'slideshow'
&& (old_profile.slideshow_path !== profile.slideshow_path
|| old_profile.shuffle !== profile.shuffle));
}).reduce((rerender, v) => rerender || v, false);
if(need_render) {
clearTimeout(timer);
return endUpdate();
}
else if(status_updated) {
clearTimeout(timer);
return saveStatus()
.then(setTimer);
}
});
}
function onUpdateMonitor(_monitors) {
lock.acquire('everything', () => {
clearTimeout(timer);
monitors = _monitors;
return endUpdate();
});
}
function endUpdate() {
return updateState()
.then((profile_updated) => {
profiles_store.removeListener('change', onUpdateProfile);
if(profile_updated) {
return saveProfile();
}
return true;
})
.then(commitBackground)
.then(saveStatus)
.then(() => profiles_store.on('change', onUpdateProfile))
.then(setTimer);
}
function listCandidates(id) {
return promisify(fs.readdir)(profiles[id].slideshow_path)
.then((files) => files.filter(fn => regex_image.test(fn)))
.catch(() => []);
}
function createFuture(id) {
return listCandidates(id).then((files) => {
let interval_ms = 1000 * 60 * profiles[id].interval;
if(profiles[id].shuffle) {
files = shuffle(files);
}
status.current[id] = {active: files.shift()};
status.future[id] = {
nextTick: status.tick + interval_ms,
queue: files
};
});
}
function checkFile(dir, fn) {
try {
var filepath = path.join(dir, fn);
}
catch(err) {
return Promise.reject();
}
return promisify(fs.access)(filepath, fs.constants.R_OK).then(() => fn);
}
function saveProfile() {
return promisify(profiles_store.save.bind(profiles_store))(profiles);
}
function saveStatus() {
return promisify(status_store.save.bind(status_store))(status);
}
};
exports.installService = function() {
const key = new Registry({
hive: Registry.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run'
});
const val = isDev
? `"${process.execPath}" "${process.cwd()}" service`
: `"${process.execPath}" service`;
key.set("Wallpaper Switcher Daemon", Registry.REG_SZ, val, (err) => {
if(err) {
console.error("Install failed: %s", err.message);
process.exit(-1);
}
process.exit(0);
});
};
exports.removeService = function() {
const key = new Registry({
hive: Registry.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run'
});
key.remove("Wallpaper Switcher Daemon", (err) => {
if(err) {
console.error("Remove failed: %s", err.message);
process.exit(-1);
}
process.exit(0);
});
};