-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
127 lines (114 loc) · 3.93 KB
/
main.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
const {app, ipcMain, BrowserWindow, Menu, powerSaveBlocker} = require('electron');
const path = require('path');
let powerSaver = null;
function stopPowerSaver() {
if (powerSaver != null && powerSaveBlocker.isStarted(powerSaver)) {
powerSaveBlocker.stop(powerSaver);
powerSaver = null;
}
}
function startPowerSaver() {
if (powerSaver == null) {
powerSaver = powerSaveBlocker.start('prevent-app-suspension')
}
}
const getUpnpApi = () => require('./api/index');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
Menu.setApplicationMenu(Menu.buildFromTemplate(require('./menu')));
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: `${__dirname}/preload.js`,
backgroundThrottling: false
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'src', 'index.html'));
// Open the DevTools.
if (process.env.NODE_ENV === 'dev') {
mainWindow.webContents.openDevTools();
}
ipcMain.handle('ssdpSearch', async () => {
const upnp = getUpnpApi();
upnp.eventEmitter.on('device', () => mainWindow.webContents.send('device'))
return upnp.ssdpStart()
.then(() => upnp.ssdpSearch())
});
ipcMain.handle('browse', async (event, args) => {
return getUpnpApi().browse(args);
});
ipcMain.handle('play', async (event, args) => {
return getUpnpApi()
.play(args)
.then(() => startPowerSaver())
});
ipcMain.handle('resume', async () => {
return getUpnpApi().resume();
});
ipcMain.handle('pause', async () => {
return getUpnpApi().pause();
});
ipcMain.handle('stop', async () => {
return getUpnpApi().stop().then(() => {
stopPowerSaver();
})
});
ipcMain.handle('search', async (event, args) => {
return getUpnpApi().search(args);
});
ipcMain.handle('seek', async (event, args) => {
return getUpnpApi().seek(args);
});
ipcMain.handle('getRenderers', async () => {
return getUpnpApi().getRenderers();
});
ipcMain.handle('getSearchCapabilities', async () => {
return getUpnpApi().getSearchCapabilities();
});
ipcMain.handle('getServers', async () => {
return getUpnpApi().getServers();
});
ipcMain.handle('getPositionInfo', async () => {
return getUpnpApi().getPositionInfo();
});
ipcMain.handle('getTransportInfo', async () => {
return getUpnpApi().getTransportInfo();
});
ipcMain.handle('selectServer', async (event, args) => {
return getUpnpApi().selectServer(args)
});
ipcMain.handle('selectRenderer', async (event, args) => {
return getUpnpApi().selectRenderer(args)
});
ipcMain.handle('getVolumeDBRange', async (event, args) => {
return getUpnpApi().getVolumeDBRange(args)
});
ipcMain.handle('getVolumeDB', async (event, args) => {
return getUpnpApi().getVolumeDB(args)
});
ipcMain.handle('getVolume', async (event, args) => {
return getUpnpApi().getVolume(args)
});
ipcMain.handle('setVolume', async (event, args) => {
return getUpnpApi().setVolume(args)
});
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed, including on macOS
app.on('window-all-closed', () => {
const upnp = getUpnpApi();
upnp.ssdpStop(); //TODO only stop what we actually started...
upnp.stop().catch(() => null).finally(() => {
stopPowerSaver();
app.quit();
})
});