Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
907 lines (765 loc) Β· 26.1 KB
/
Copy pathmain.js
File metadata and controls
907 lines (765 loc) Β· 26.1 KB
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
const { app, BrowserWindow, WebContentsView, ipcMain, Menu, session } = require('electron');
const path = require('path');
const fs = require('fs');
const os = require('os');
const { pathToFileURL } = require('url');
const { exec } = require('child_process');
const log = require('electron-log');
const { Moon } = require('lunarphase-js');
const { getFactsForDay } = require('./facts');
const wifi = require('./wifi');
// Configure electron-log
log.transports.file.level = 'info';
log.transports.console.level = process.env.NODE_ENV === 'development' ? 'debug' : 'error';
log.transports.file.maxSize = 5 * 1024 * 1024; // 5MB
log.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}';
process.on('warning', (warning) => {
log.warn('Process warning:', warning);
});
const ERR_ABORTED = -3; // Chromium's code for a navigation that was replaced or cancelled
const TRANSPARENT = '#00000000';
const RETRY_BASE_MS = 5000;
const RETRY_MAX_MS = 60000;
// Space around a slide for the on-screen controls; tuned for the round display.
const SLIDE_INSET = { left: 190, top: 120, horizontal: 380, vertical: 220 };
const BUILT_IN_CONFIG = {
urls: [
'https://timechaincalendar.com/en',
'https://bitfeed.live/'
],
autoRotate: false,
autoRotateInterval: 60000, // 1 minute
fullscreen: true,
enableDevTools: false
};
let mainWindow;
let config = {};
let views = []; // { view, url, backgroundColor, error, loadFailed, retryTimer, retryCount, closed }
let currentViewIndex = 0;
let attachedView = null;
let screensaverView = null;
let isScreensaverActive = false;
let isModalOpen = false;
function sendToRenderer(channel, ...args) {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send(channel, ...args);
}
}
// Get user data directory for config storage
function getConfigPath() {
const userDataPath = app.getPath('userData');
return path.join(userDataPath, 'config.json');
}
// Get default config path (shipped with app)
function getDefaultConfigPath() {
return path.join(__dirname, 'config.json');
}
// Resolve screensaver URL, handling @Quotes special path
function resolveScreensaverUrl(url) {
if (url && url.startsWith('@Quotes/')) {
const relativePath = url.substring('@Quotes/'.length);
return pathToFileURL(path.join(__dirname, 'Quotes', relativePath)).href;
}
return url;
}
function isUsableConfig(value) {
return Boolean(value) && Array.isArray(value.urls) && value.urls.length > 0;
}
// Write to a temp file and rename, so a power cut can't leave a truncated file.
function writeFileAtomic(filePath, data) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
const tempPath = `${filePath}.tmp`;
const fd = fs.openSync(tempPath, 'w');
try {
fs.writeSync(fd, data);
fs.fsyncSync(fd);
} finally {
fs.closeSync(fd);
}
fs.renameSync(tempPath, filePath);
}
function readShippedConfig() {
try {
const shipped = JSON.parse(fs.readFileSync(getDefaultConfigPath(), 'utf8'));
if (isUsableConfig(shipped)) return shipped;
log.error('Shipped config has no URLs, using built-in defaults');
} catch (error) {
log.error('Error loading shipped config:', error);
}
return { ...BUILT_IN_CONFIG };
}
function loadConfig() {
const configPath = getConfigPath();
let raw;
try {
raw = fs.readFileSync(configPath, 'utf8');
} catch (error) {
config = readShippedConfig();
if (error.code === 'ENOENT') {
log.info('No user config yet, creating one from defaults');
try {
writeFileAtomic(configPath, JSON.stringify(config, null, 2));
} catch (writeError) {
log.warn('Could not write default config:', writeError);
}
} else {
log.error('Could not read user config, running on defaults:', error);
}
return;
}
try {
const parsed = JSON.parse(raw);
if (!isUsableConfig(parsed)) throw new Error('config has no URLs');
config = parsed;
log.info('Config loaded from user data directory:', configPath);
} catch (error) {
// Leave the file in place so a typo can be fixed; only run on defaults for now.
log.error(`User config is invalid (${error.message}), running on defaults. A copy is at ${configPath}.broken`);
try {
fs.writeFileSync(`${configPath}.broken`, raw, 'utf8');
} catch (copyError) {
log.warn('Could not back up invalid config:', copyError);
}
config = readShippedConfig();
}
}
function normalizeUrlEntry(urlData) {
if (typeof urlData === 'string') return { url: urlData, backgroundColor: '#000000' };
return { url: urlData.url, backgroundColor: urlData.backgroundColor || '#000000' };
}
// Decides what is on screen. Every change of state calls this instead of
// attaching views directly: screensaver first, then the current slide unless a
// modal is open or the slide failed to load (so the error overlay can be seen).
function syncAttachedView() {
if (!mainWindow || mainWindow.isDestroyed()) return;
const { width, height } = mainWindow.getBounds();
const entry = views[currentViewIndex];
let target = null;
let bounds = null;
if (isScreensaverActive) {
target = screensaverView;
bounds = { x: 0, y: 0, width, height };
} else if (!isModalOpen && entry && !entry.error) {
target = entry.view;
bounds = {
x: SLIDE_INSET.left,
y: SLIDE_INSET.top,
width: width - SLIDE_INSET.horizontal,
height: height - SLIDE_INSET.vertical
};
}
if (attachedView !== target) {
if (attachedView) mainWindow.contentView.removeChildView(attachedView);
if (target) mainWindow.contentView.addChildView(target);
attachedView = target;
}
if (target) target.setBounds(bounds);
}
function publishCurrentView() {
const entry = views[currentViewIndex];
if (!entry || !mainWindow || mainWindow.isDestroyed()) return;
mainWindow.setBackgroundColor(entry.backgroundColor);
sendToRenderer('view-changed', {
index: currentViewIndex,
url: entry.url,
backgroundColor: entry.backgroundColor,
error: entry.error
});
}
function loadView(entry) {
entry.loadFailed = false;
// Failures are reported through did-fail-load.
entry.view.webContents.loadURL(entry.url).catch(() => {});
}
function scheduleRetry(entry) {
if (entry.closed) return;
clearTimeout(entry.retryTimer);
const delay = Math.min(RETRY_BASE_MS * 2 ** entry.retryCount, RETRY_MAX_MS);
entry.retryCount++;
entry.retryTimer = setTimeout(() => loadView(entry), delay);
}
function markViewFailed(entry, message) {
entry.loadFailed = true;
entry.error = message;
const index = views.indexOf(entry);
if (index === currentViewIndex) {
syncAttachedView();
sendToRenderer('webview-error', { index, url: entry.url, error: message });
}
scheduleRetry(entry);
}
// Slides and the screensaver sit on top of the main window's own page. Like the
// old BrowserView they start transparent, so while a page loads the slide's
// background colour (painted by the renderer underneath) shows instead of white.
function createPageView() {
const view = new WebContentsView({
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webSecurity: true
}
});
view.setBackgroundColor(TRANSPARENT);
return view;
}
function createView(url, backgroundColor) {
const view = createPageView();
const entry = { view, url, backgroundColor, error: null, loadFailed: false, retryTimer: null, retryCount: 0, closed: false };
view.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL, isMainFrame) => {
if (!isMainFrame || errorCode === ERR_ABORTED) return;
log.error(`Failed to load ${validatedURL}:`, errorDescription);
markViewFailed(entry, errorDescription);
});
view.webContents.on('did-finish-load', () => {
// Chromium also reports "finished" for the error page of a failed load.
if (entry.loadFailed) return;
log.info('Loaded:', url);
entry.error = null;
entry.retryCount = 0;
const index = views.indexOf(entry);
if (index === currentViewIndex) {
syncAttachedView();
sendToRenderer('webview-loaded', { index, url });
}
});
view.webContents.on('render-process-gone', (event, details) => {
if (details.reason === 'clean-exit') return;
log.error(`Page process for ${url} is gone:`, details.reason);
markViewFailed(entry, `Page stopped unexpectedly (${details.reason})`);
});
loadView(entry);
return entry;
}
function closeWebContents(view) {
try {
view.webContents.close();
} catch (error) {
log.warn('Error closing view:', error);
}
}
function closeView(entry) {
entry.closed = true;
clearTimeout(entry.retryTimer);
if (attachedView === entry.view) {
if (mainWindow && !mainWindow.isDestroyed()) mainWindow.contentView.removeChildView(entry.view);
attachedView = null;
}
closeWebContents(entry.view);
}
// Match the views to config.urls, keeping any page whose URL is still wanted.
function syncSlideViews() {
const current = views[currentViewIndex];
const reusable = [...views];
views = config.urls.map((urlData) => {
const { url, backgroundColor } = normalizeUrlEntry(urlData);
const match = reusable.findIndex((entry) => entry.url === url);
if (match === -1) return createView(url, backgroundColor);
const [entry] = reusable.splice(match, 1);
entry.backgroundColor = backgroundColor;
return entry;
});
reusable.forEach(closeView);
currentViewIndex = Math.max(views.indexOf(current), 0);
syncAttachedView();
publishCurrentView();
}
function showSlide(index) {
if (!Number.isInteger(index) || index < 0 || index >= views.length) return;
if (isScreensaverActive) return;
currentViewIndex = index;
syncAttachedView();
publishCurrentView();
}
function retryView(entry) {
clearTimeout(entry.retryTimer);
entry.retryCount = 0;
loadView(entry);
}
function createScreensaverView() {
const view = createPageView();
const contents = view.webContents;
contents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL, isMainFrame) => {
if (!isMainFrame || errorCode === ERR_ABORTED) return;
log.error(`Screensaver failed to load ${validatedURL}:`, errorDescription);
sendToRenderer('screensaver-error', { url: validatedURL, error: errorDescription });
});
contents.on('did-finish-load', () => {
log.info('Screensaver loaded successfully');
sendToRenderer('screensaver-loaded');
// Report clicks and touches through a console message
contents.executeJavaScript(`
(function() {
const sendHideMessage = () => {
console.log('SCREENSAVER_HIDE_INTERACTION');
};
document.addEventListener('click', sendHideMessage, true);
document.addEventListener('touchstart', sendHideMessage, true);
document.addEventListener('mousedown', sendHideMessage, true);
})();
`).catch((error) => log.warn('Could not add screensaver input listeners:', error));
});
contents.on('console-message', (event) => {
if (event.message === 'SCREENSAVER_HIDE_INTERACTION') hideScreensaver();
});
contents.on('before-input-event', () => hideScreensaver());
contents.on('context-menu', () => hideScreensaver());
contents.on('will-navigate', (event) => {
event.preventDefault();
hideScreensaver();
});
return view;
}
function showScreensaver() {
if (isScreensaverActive) return;
screensaverView = createScreensaverView();
isScreensaverActive = true;
syncAttachedView();
const configUrl = config.screensaverUrl || 'https://lodev09.github.io/web-screensavers/jellyfish/';
const screensaverUrl = resolveScreensaverUrl(configUrl);
log.info('Loading screensaver URL:', screensaverUrl);
screensaverView.webContents.loadURL(screensaverUrl).catch(() => {});
sendToRenderer('screensaver-shown');
}
function hideScreensaver() {
if (!isScreensaverActive) return;
isScreensaverActive = false;
syncAttachedView();
publishCurrentView();
sendToRenderer('screensaver-hidden');
// The screensaver's memory is only freed by closing it. This often runs inside
// one of its own event handlers, where it can't be closed straight away.
const view = screensaverView;
screensaverView = null;
setImmediate(() => closeWebContents(view));
}
// Handle keyboard shortcuts when window is focused
function handleKeyPress(event, input) {
if (!mainWindow || mainWindow.isDestroyed()) return;
if (input.type !== 'keyDown') return;
// Only handle shortcuts when main window is focused
if (!mainWindow.isFocused()) return;
// Any key dismisses the screensaver
if (isScreensaverActive) {
event.preventDefault();
hideScreensaver();
return;
}
// An open modal gets all keys, including Escape to close itself
if (isModalOpen) return;
switch (input.key) {
case 'ArrowLeft':
if (!input.meta && !input.control && !input.alt) {
event.preventDefault();
const prevIndex = (currentViewIndex - 1 + views.length) % views.length;
showSlide(prevIndex);
}
break;
case 'ArrowRight':
if (!input.meta && !input.control && !input.alt) {
event.preventDefault();
const nextIndex = (currentViewIndex + 1) % views.length;
showSlide(nextIndex);
}
break;
case 'F11':
event.preventDefault();
const isFullscreen = mainWindow.isFullScreen();
mainWindow.setFullScreen(!isFullscreen);
break;
case 'Escape':
event.preventDefault();
app.quit();
break;
}
}
function createWindow() {
// Prevent creating multiple windows
if (mainWindow && !mainWindow.isDestroyed()) {
log.warn('Window already exists, not creating new one');
return;
}
loadConfig();
mainWindow = new BrowserWindow({
width: 1080,
height: 1080,
fullscreen: config.fullscreen,
icon: path.join(__dirname, 'icons/icon.png'),
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webSecurity: true,
spellcheck: false,
preload: path.join(__dirname, 'preload.js')
},
show: true, // Show window immediately
titleBarStyle: 'hidden',
frame: false,
// Electron 43+ rounds frameless windows on Linux; the kiosk wants square edges
roundedCorners: false
});
mainWindow.loadFile('index.html');
// Send config to renderer once content is loaded
mainWindow.webContents.once('did-finish-load', () => {
mainWindow.webContents.send('config-loaded', config);
// Create the slide views after main window is ready
syncSlideViews();
});
mainWindow.on('resize', syncAttachedView);
mainWindow.on('closed', () => {
views.forEach(closeView);
views = [];
if (screensaverView) {
closeWebContents(screensaverView);
screensaverView = null;
isScreensaverActive = false;
}
attachedView = null;
mainWindow = null;
});
// Create menu for development
if (config.enableDevTools || process.argv.includes('--dev')) {
const menu = Menu.buildFromTemplate([
{
label: 'Developer',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'quit' }
]
}
]);
Menu.setApplicationMenu(menu);
} else {
Menu.setApplicationMenu(null);
}
// Register keyboard event handler for window-focused shortcuts
mainWindow.webContents.on('before-input-event', handleKeyPress);
// Listen for clicks on main window to hide screensaver
mainWindow.on('focus', () => {
if (isScreensaverActive) {
log.info('Window focus detected while screensaver active, hiding...');
hideScreensaver();
}
});
// Add context menu support for text inputs
mainWindow.webContents.on('context-menu', (event, params) => {
// Only show context menu for editable content (text inputs, textareas)
if (params.isEditable) {
const contextMenu = Menu.buildFromTemplate([
{ label: 'Cut', role: 'cut', enabled: params.editFlags.canCut },
{ label: 'Copy', role: 'copy', enabled: params.editFlags.canCopy },
{ label: 'Paste', role: 'paste', enabled: params.editFlags.canPaste },
{ type: 'separator' },
{ label: 'Select All', role: 'selectAll', enabled: params.editFlags.canSelectAll }
]);
contextMenu.popup({ window: mainWindow });
}
});
}
// App event handlers
if (app.requestSingleInstanceLock()) {
app.on('second-instance', () => {
if (mainWindow && !mainWindow.isDestroyed()) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
app.whenReady().then(() => {
// Displayed web pages get no camera, microphone, location, notifications, etc.
session.defaultSession.setPermissionRequestHandler((webContents, permission, callback) => callback(false));
session.defaultSession.setPermissionCheckHandler(() => false);
createWindow();
});
} else {
app.quit();
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (app.isReady() && BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// IPC handlers
ipcMain.handle('get-config', () => {
return config;
});
// Navigation handlers
ipcMain.handle('go-to-slide', (event, index) => {
showSlide(index);
});
ipcMain.handle('next-slide', () => {
const nextIndex = (currentViewIndex + 1) % views.length;
showSlide(nextIndex);
});
ipcMain.handle('previous-slide', () => {
const prevIndex = (currentViewIndex - 1 + views.length) % views.length;
showSlide(prevIndex);
});
ipcMain.handle('reload-current-slide', () => {
if (views[currentViewIndex]) retryView(views[currentViewIndex]);
});
// Save config to file
ipcMain.handle('save-config', async (event, newConfig) => {
try {
const configPath = getConfigPath();
writeFileAtomic(configPath, JSON.stringify(newConfig, null, 2));
// Update current config
config = newConfig;
log.info('Config saved successfully to:', configPath);
return { success: true, path: configPath };
} catch (error) {
log.error('Failed to save config:', error);
return {
success: false,
error: error.message,
details: 'Settings could not be saved. This may happen in read-only environments like AppImage.'
};
}
});
// Apply a config that save-config has already stored
ipcMain.handle('reload-with-new-config', () => {
log.info('Applying new config');
syncSlideViews();
return { success: true };
});
// Exit app handler
ipcMain.handle('exit-app', () => {
app.quit();
});
// Screensaver handlers
ipcMain.handle('show-screensaver', () => {
showScreensaver();
});
ipcMain.handle('hide-screensaver', () => {
hideScreensaver();
});
ipcMain.handle('is-screensaver-active', () => {
return isScreensaverActive;
});
// Moon phase handler
ipcMain.handle('get-moon-phase', () => {
try {
const currentPhase = Moon.lunarPhase();
const moonEmoji = getMoonPhaseEmoji(currentPhase);
return { phase: currentPhase, emoji: moonEmoji };
} catch (error) {
log.error('Moon phase calculation failed:', error);
return { phase: 'Unknown', emoji: 'π' };
}
});
// Bitcoin fact handler
ipcMain.handle('get-todays-bitcoin-fact', () => {
const today = new Date();
try {
return getFactsForDay(today.getMonth() + 1, today.getDate());
} catch (error) {
log.error('Could not read Bitcoin facts:', error);
return [];
}
});
// Logging handlers for renderer process
ipcMain.handle('log-info', (event, { message, args }) => {
log.info(message, ...args);
});
ipcMain.handle('log-error', (event, { message, args }) => {
log.error(message, ...args);
});
ipcMain.handle('log-warn', (event, { message, args }) => {
log.warn(message, ...args);
});
// Modal state handlers
ipcMain.handle('set-modal-state', (event, modalOpen) => {
isModalOpen = modalOpen;
syncAttachedView();
});
// Autostart functionality (Linux only)
ipcMain.handle('get-autostart-status', async () => {
const isLinux = os.platform() === 'linux';
if (!isLinux) {
return { isLinux: false, enabled: false };
}
try {
const homeDir = os.homedir();
const serviceFile = path.join(homeDir, '.config', 'systemd', 'user', 'circle.service');
// Check if service file exists
const serviceExists = fs.existsSync(serviceFile);
if (!serviceExists) {
return { isLinux: true, enabled: false };
}
// Check if service is enabled
const { stdout } = await execPromise('systemctl --user is-enabled circle.service');
const isEnabled = stdout.trim() === 'enabled';
return { isLinux: true, enabled: isEnabled };
} catch (error) {
log.error('Error checking autostart status:', error);
return { isLinux: true, enabled: false };
}
});
ipcMain.handle('set-autostart', async (event, enable) => {
const isLinux = os.platform() === 'linux';
if (!isLinux) {
throw new Error('Autostart is only supported on Linux');
}
try {
if (enable) {
await enableAutostart();
} else {
await disableAutostart();
}
log.info(`Autostart ${enable ? 'enabled' : 'disabled'} successfully`);
return { success: true };
} catch (error) {
log.error(`Error ${enable ? 'enabling' : 'disabling'} autostart:`, error);
throw error;
}
});
// Helper function to promisify exec
function execPromise(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve({ stdout, stderr });
}
});
});
}
// Enable autostart by creating systemd service file and enabling it
async function enableAutostart() {
const homeDir = os.homedir();
const configDir = path.join(homeDir, '.config', 'systemd', 'user');
const serviceFile = path.join(configDir, 'circle.service');
// Get the current executable path
// For AppImage, use APPIMAGE environment variable, otherwise fallback to execPath
const execPath = process.env.APPIMAGE || process.execPath;
// Create .config/systemd/user directory if it doesn't exist
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
// systemd unit-file quoting, so paths with spaces or special characters work
const escapedExecPath = execPath
.replace(/(["\\])/g, '\\$1')
.replace(/%/g, '%%')
.replace(/\$/g, '$$$$');
// Create service file content
const serviceContent = `[Unit]
Description=Circle
After=graphical.target
After=network.target
Wants=graphical.target
[Service]
Type=simple
ExecStart="${escapedExecPath}"
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=default.target
`;
// Write service file
fs.writeFileSync(serviceFile, serviceContent, 'utf8');
log.info('Service file created at:', serviceFile);
// Reload systemd daemon
await execPromise('systemctl --user daemon-reload');
// Enable the service
await execPromise('systemctl --user enable circle.service');
// Start the service (optional - just enable for now)
// await execPromise('systemctl --user start circle.service');
}
// Disable autostart by disabling and removing systemd service
async function disableAutostart() {
// Only disable, never stop: the app may itself be running under this service.
try {
await execPromise('systemctl --user disable circle.service');
} catch (error) {
log.warn('Could not disable service (it may not exist):', error.message);
}
// Remove service file if it exists
const homeDir = os.homedir();
const serviceFile = path.join(homeDir, '.config', 'systemd', 'user', 'circle.service');
if (fs.existsSync(serviceFile)) {
fs.unlinkSync(serviceFile);
log.info('Service file removed:', serviceFile);
}
// Reload systemd daemon
try {
await execPromise('systemctl --user daemon-reload');
} catch (error) {
log.warn('Error reloading systemd daemon:', error.message);
}
}
// WiFi functionality (Linux only)
const isLinux = os.platform() === 'linux';
ipcMain.handle('get-wifi-status', async () => {
if (!isLinux) return { connected: false, networkName: null };
try {
const networkName = await wifi.getActiveWifiName();
return { connected: networkName !== null, networkName };
} catch (error) {
log.error('Error getting WiFi status:', error.message);
return { connected: false, networkName: null };
}
});
ipcMain.handle('scan-wifi', async () => {
if (!isLinux) throw new Error('WiFi scanning is only supported on Linux');
try {
return await wifi.scanNetworks();
} catch (error) {
log.error('Error scanning WiFi networks:', error.message);
throw new Error(`Failed to scan WiFi networks: ${error.message}`);
}
});
ipcMain.handle('connect-wifi', async (event, { ssid, password }) => {
if (!isLinux) throw new Error('WiFi connection is only supported on Linux');
try {
await wifi.connect(ssid, password);
log.info(`Connected to WiFi: ${ssid}`);
return { success: true, message: `Connected to ${ssid}` };
} catch (error) {
log.error('Error connecting to WiFi:', error.message);
throw error;
}
});
ipcMain.handle('disconnect-wifi', async () => {
if (!isLinux) throw new Error('WiFi disconnection is only supported on Linux');
try {
const name = await wifi.disconnectActive();
log.info(`Disconnected from WiFi: ${name}`);
return { success: true, message: `Disconnected from ${name}` };
} catch (error) {
log.error('Error disconnecting from WiFi:', error.message);
throw new Error(`Failed to disconnect: ${error.message}`);
}
});
// Helper function for moon phase emojis
function getMoonPhaseEmoji(phase) {
const phaseNames = {
'New': 'π',
'Waxing Crescent': 'π',
'First Quarter': 'π',
'Waxing Gibbous': 'π',
'Full': 'π',
'Waning Gibbous': 'π',
'Last Quarter': 'π',
'Waning Crescent': 'π'
};
return phaseNames[phase] || 'π';
}
app.on('web-contents-created', (event, contents) => {
// New windows would cover the kiosk with no way to close them on a touch screen
contents.setWindowOpenHandler(() => ({ action: 'deny' }));
// The main window never navigates away from the app UI
contents.on('will-navigate', (navigationEvent) => {
if (mainWindow && !mainWindow.isDestroyed() && contents === mainWindow.webContents) {
navigationEvent.preventDefault();
}
});
});