forked from MediaBrowser/emby-theater-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
970 lines (766 loc) · 31.1 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
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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
(function () {
var electron = require('electron');
var app = electron.app; // Module to control application life.
var BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;
var playerWindow = null;
var hasAppLoaded = false;
var enableDevTools = false;
var enableDevToolsOnStartup = false;
var initialShowEventsComplete = false;
var previousBounds;
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform != 'darwin') {
app.quit();
}
});
function onWindowMoved() {
mainWindow.webContents.executeJavaScript('window.dispatchEvent(new CustomEvent("move", {}));');
var winPosition = mainWindow.getPosition();
playerWindow.setPosition(winPosition[0], winPosition[1]);
}
function onWindowResize() {
var winSize = mainWindow.getSize();
playerWindow.setSize(winSize[0], winSize[1]);
}
var currentWindowState = 'Normal';
var restoreWindowState;
function setWindowState(state) {
restoreWindowState = null;
var previousState = currentWindowState;
if (state == 'Maximized') {
state = 'Fullscreen';
}
if (state == 'Minimized') {
restoreWindowState = previousState;
mainWindow.setAlwaysOnTop(false);
mainWindow.minimize();
} else if (state == 'Maximized') {
if (previousState == "Minimized") {
mainWindow.restore();
}
mainWindow.maximize();
mainWindow.setAlwaysOnTop(false);
} else if (state == 'Fullscreen') {
if (previousState == "Minimized") {
mainWindow.restore();
}
var bounds = mainWindow.getBounds();
previousBounds = bounds;
mainWindow.setFullScreen(true);
mainWindow.setAlwaysOnTop(true);
// So far this is only needed for returning from external player playback
mainWindow.focus();
} else {
var setSize = false;
if (previousState == "Minimized") {
mainWindow.restore();
}
else if (previousState == "Fullscreen") {
setSize = true;
mainWindow.setFullScreen(false);
}
else if (previousState == "Maximized") {
mainWindow.unmaximize();
}
if (setSize) {
var bounds = previousBounds;
if (bounds) {
mainWindow.setBounds(bounds);
} else {
mainWindow.setSize(1280, 720);
mainWindow.center();
}
}
mainWindow.setAlwaysOnTop(false);
}
}
function onWindowStateChanged(state) {
currentWindowState = state;
mainWindow.webContents.executeJavaScript('document.windowState="' + state + '";document.dispatchEvent(new CustomEvent("windowstatechanged", {detail:{windowState:"' + state + '"}}));');
}
function onMinimize() {
playerWindow.minimize();
onWindowStateChanged('Minimized');
}
function onRestore() {
var restoreState = restoreWindowState;
restoreWindowState = null;
if (restoreState && restoreState != 'Normal' && restoreState != 'Minimized') {
setWindowState(restoreState);
} else {
onWindowStateChanged('Normal');
}
playerWindow.restore();
}
function onMaximize() {
onWindowStateChanged('Maximized');
}
function onEnterFullscreen() {
onWindowStateChanged('Fullscreen');
if (initialShowEventsComplete) {
//playerWindow.setFullScreen(true);
mainWindow.setMovable(false);
}
}
function onLeaveFullscreen() {
onWindowStateChanged('Normal');
if (initialShowEventsComplete) {
playerWindow.setFullScreen(false);
mainWindow.setMovable(true);
}
}
function onUnMaximize() {
onWindowStateChanged('Normal');
}
var customFileProtocol = 'electronfile';
function addPathIntercepts() {
var protocol = electron.protocol;
var path = require('path');
protocol.registerFileProtocol(customFileProtocol, function (request, callback) {
// Add 3 to account for ://
var url = request.url.substr(customFileProtocol.length + 3);
url = __dirname + '/' + url;
url = url.split('?')[0];
callback({
path: path.normalize(url)
});
});
//protocol.interceptHttpProtocol('https', function (request, callback) {
// alert(request.url);
// callback({ 'url': request.url, 'referrer': request.referrer, session: null });
//});
}
function sleepSystem() {
var sleepMode = require('sleep-mode');
sleepMode(function (err, stderr, stdout) {
});
}
function restartSystem() {
}
function shutdownSystem() {
var powerOff = require('power-off');
powerOff(function (err, stderr, stdout) {
});
}
function setMainWindowResizable(resizable) {
try {
mainWindow.setResizable(resizable);
} catch (err) {
console.log('Error in setResizable:' + err);
}
}
var isTransparencyRequired = false;
var windowStateOnLoad;
function registerAppHost() {
var protocol = electron.protocol;
var customProtocol = 'electronapphost';
protocol.registerStringProtocol(customProtocol, function (request, callback) {
// Add 3 to account for ://
var url = request.url.substr(customProtocol.length + 3);
var parts = url.split('?');
var command = parts[0];
switch (command) {
case 'windowstate-Normal':
setMainWindowResizable(!isTransparencyRequired);
setWindowState('Normal');
break;
case 'windowstate-Maximized':
setMainWindowResizable(false);
setWindowState('Maximized');
break;
case 'windowstate-Fullscreen':
setMainWindowResizable(false);
setWindowState('Fullscreen');
break;
case 'windowstate-Minimized':
setWindowState('Minimized');
break;
case 'exit':
closeWindow(mainWindow);
break;
case 'sleep':
sleepSystem();
break;
case 'shutdown':
shutdownSystem();
break;
case 'restart':
restartSystem();
break;
case 'openurl':
electron.shell.openExternal(url.substring(url.indexOf('url=') + 4));
break;
case 'shellstart':
var options = require('querystring').parse(parts[1]);
startProcess(options, callback);
return;
case 'shellclose':
closeProcess(require('querystring').parse(parts[1]).id, callback);
return;
case 'video-on':
isTransparencyRequired = true;
setMainWindowResizable(false);
break;
case 'video-off':
isTransparencyRequired = false;
setMainWindowResizable(true);
break;
case 'loaded':
if (windowStateOnLoad) {
setWindowState(windowStateOnLoad);
}
mainWindow.focus();
hasAppLoaded = true;
onLoaded();
break;
}
callback("");
});
}
function onLoaded() {
//var globalShortcut = electron.globalShortcut;
//globalShortcut.register('mediastop', function () {
// sendCommand('stop');
//});
//globalShortcut.register('mediaplaypause', function () {
//});
sendJavascript('window.PlayerWindowId="' + getWindowId(playerWindow) + '";');
}
var processes = {};
function startProcess(options, callback) {
var pid;
var args = (options.arguments || '').split('|||');
try {
var process = require('child_process').execFile(options.path, args, {}, function (error, stdout, stderr) {
if (error) {
console.log('Process closed with error: ' + error);
}
processes[pid] = null;
var script = 'onChildProcessClosed("' + pid + '", ' + (error ? 'true' : 'false') + ');';
sendJavascript(script);
});
pid = process.pid.toString();
processes[pid] = process;
callback(pid);
} catch (err) {
alert('Error launching process: ' + err);
}
}
function closeProcess(id, callback) {
var process = processes[id];
if (process) {
process.kill();
}
callback("");
}
function registerFileSystem() {
var protocol = electron.protocol;
var customProtocol = 'electronfs';
protocol.registerStringProtocol(customProtocol, function (request, callback) {
// Add 3 to account for ://
var url = request.url.substr(customProtocol.length + 3).split('?')[0];
var fs = require('fs');
switch (url) {
case 'fileexists':
case 'directoryexists':
var path = request.url.split('=')[1];
fs.access(path, (err) => {
if (err) {
console.error('fs access result for path: ' + err);
callback('false');
} else {
callback('true');
}
});
break;
default:
callback("");
break;
}
});
}
function registerServerdiscovery() {
var protocol = electron.protocol;
var customProtocol = 'electronserverdiscovery';
var serverdiscovery = require('./serverdiscovery/serverdiscovery-native');
protocol.registerStringProtocol(customProtocol, function (request, callback) {
// Add 3 to account for ://
var url = request.url.substr(customProtocol.length + 3).split('?')[0];
switch (url) {
case 'findservers':
var timeoutMs = request.url.split('=')[1];
serverdiscovery.findServers(timeoutMs, callback);
break;
default:
callback("");
break;
}
});
}
function registerWakeOnLan() {
var protocol = electron.protocol;
var customProtocol = 'electronwakeonlan';
var wakeonlan = require('./wakeonlan/wakeonlan-native');
protocol.registerStringProtocol(customProtocol, function (request, callback) {
// Add 3 to account for ://
var url = request.url.substr(customProtocol.length + 3).split('?')[0];
switch (url) {
case 'wakeserver':
var mac = request.url.split('=')[1].split('&')[0];
var options = {port: request.url.split('=')[2]};
wakeonlan.wake(mac, options, callback);
break;
default:
callback("");
break;
}
});
}
function alert(text) {
electron.dialog.showMessageBox(mainWindow, {
message: text.toString(),
buttons: ['ok']
});
}
function replaceAll(str, find, replace) {
return str.split(find).join(replace);
}
function getAppBaseUrl() {
var url = 'https://tv.emby.media';
//url = 'http://localhost:8088';
return url;
}
function getAppUrl() {
var url = getAppBaseUrl() + '/index.html?autostart=false';
//url += '?v=' + new Date().getTime();
return url;
}
var startInfoJson;
function loadStartInfo() {
return new Promise(function (resolve, reject) {
var os = require("os");
var path = require('path');
var fs = require('fs');
var topDirectory = path.normalize(__dirname);
var pluginDirectory = path.normalize(__dirname + '/plugins');
var scriptsDirectory = path.normalize(__dirname + '/scripts');
fs.readdir(pluginDirectory, function (err, pluginFiles) {
fs.readdir(scriptsDirectory, function (err, scriptFiles) {
pluginFiles = pluginFiles || [];
scriptFiles = scriptFiles || [];
var startInfo = {
paths: {
apphost: customFileProtocol + '://apphost',
shell: customFileProtocol + '://shell',
wakeonlan: customFileProtocol + '://wakeonlan/wakeonlan',
serverdiscovery: customFileProtocol + '://serverdiscovery/serverdiscovery',
fullscreenmanager: 'file://' + replaceAll(path.normalize(topDirectory + '/fullscreenmanager.js'), '\\', '/'),
filesystem: customFileProtocol + '://filesystem'
},
name: app.getName(),
version: app.getVersion(),
deviceName: os.hostname(),
deviceId: os.hostname(),
supportsTransparentWindow: supportsTransparentWindow(),
plugins: pluginFiles.filter(function (f) {
return f.indexOf('.js') != -1;
}).map(function (f) {
return 'file://' + replaceAll(path.normalize(pluginDirectory + '/' + f), '\\', '/');
}),
scripts: scriptFiles.map(function (f) {
return 'file://' + replaceAll(path.normalize(scriptsDirectory + '/' + f), '\\', '/');
})
};
startInfoJson = JSON.stringify(startInfo);
resolve();
});
});
});
}
function setStartInfo() {
var script = 'function startWhenReady(){if (self.Emby && self.Emby.App){self.appStartInfo=' + startInfoJson + ';Emby.App.start(appStartInfo);} else {setTimeout(startWhenReady, 50);}} startWhenReady();';
sendJavascript(script);
//sendJavascript('var appStartInfo=' + startInfoJson + ';');
}
function sendCommand(cmd) {
var script = "require(['inputmanager'], function(inputmanager){inputmanager.trigger('" + cmd + "');});";
sendJavascript(script);
}
function sendJavascript(script) {
// Add some null checks to handle attempts to send JS when the process is closing or has closed
var win = mainWindow;
if (win) {
var web = win.webContents;
if (web) {
web.executeJavaScript(script);
}
}
}
function onAppCommand(e, cmd) {
//switch (command_id) {
// case APPCOMMAND_BROWSER_BACKWARD : return "browser-backward";
// case APPCOMMAND_BROWSER_FORWARD : return "browser-forward";
// case APPCOMMAND_BROWSER_REFRESH : return "browser-refresh";
// case APPCOMMAND_BROWSER_STOP : return "browser-stop";
// case APPCOMMAND_BROWSER_SEARCH : return "browser-search";
// case APPCOMMAND_BROWSER_FAVORITES : return "browser-favorites";
// case APPCOMMAND_BROWSER_HOME : return "browser-home";
// case APPCOMMAND_VOLUME_MUTE : return "volume-mute";
// case APPCOMMAND_VOLUME_DOWN : return "volume-down";
// case APPCOMMAND_VOLUME_UP : return "volume-up";
// case APPCOMMAND_MEDIA_NEXTTRACK : return "media-nexttrack";
// case APPCOMMAND_MEDIA_PREVIOUSTRACK : return "media-previoustrack";
// case APPCOMMAND_MEDIA_STOP : return "media-stop";
// case APPCOMMAND_MEDIA_PLAY_PAUSE : return "media-play_pause";
// case APPCOMMAND_LAUNCH_MAIL : return "launch-mail";
// case APPCOMMAND_LAUNCH_MEDIA_SELECT : return "launch-media-select";
// case APPCOMMAND_LAUNCH_APP1 : return "launch-app1";
// case APPCOMMAND_LAUNCH_APP2 : return "launch-app2";
// case APPCOMMAND_BASS_DOWN : return "bass-down";
// case APPCOMMAND_BASS_BOOST : return "bass-boost";
// case APPCOMMAND_BASS_UP : return "bass-up";
// case APPCOMMAND_TREBLE_DOWN : return "treble-down";
// case APPCOMMAND_TREBLE_UP : return "treble-up";
// case APPCOMMAND_MICROPHONE_VOLUME_MUTE : return "microphone-volume-mute";
// case APPCOMMAND_MICROPHONE_VOLUME_DOWN : return "microphone-volume-down";
// case APPCOMMAND_MICROPHONE_VOLUME_UP : return "microphone-volume-up";
// case APPCOMMAND_HELP : return "help";
// case APPCOMMAND_FIND : return "find";
// case APPCOMMAND_NEW : return "new";
// case APPCOMMAND_OPEN : return "open";
// case APPCOMMAND_CLOSE : return "close";
// case APPCOMMAND_SAVE : return "save";
// case APPCOMMAND_PRINT : return "print";
// case APPCOMMAND_UNDO : return "undo";
// case APPCOMMAND_REDO : return "redo";
// case APPCOMMAND_COPY : return "copy";
// case APPCOMMAND_CUT : return "cut";
// case APPCOMMAND_PASTE : return "paste";
// case APPCOMMAND_REPLY_TO_MAIL : return "reply-to-mail";
// case APPCOMMAND_FORWARD_MAIL : return "forward-mail";
// case APPCOMMAND_SEND_MAIL : return "send-mail";
// case APPCOMMAND_SPELL_CHECK : return "spell-check";
// case APPCOMMAND_MIC_ON_OFF_TOGGLE : return "mic-on-off-toggle";
// case APPCOMMAND_CORRECTION_LIST : return "correction-list";
// case APPCOMMAND_MEDIA_PLAY : return "media-play";
// case APPCOMMAND_MEDIA_PAUSE : return "media-pause";
// case APPCOMMAND_MEDIA_RECORD : return "media-record";
// case APPCOMMAND_MEDIA_FAST_FORWARD : return "media-fast-forward";
// case APPCOMMAND_MEDIA_REWIND : return "media-rewind";
// case APPCOMMAND_MEDIA_CHANNEL_UP : return "media-channel-up";
// case APPCOMMAND_MEDIA_CHANNEL_DOWN : return "media-channel-down";
// case APPCOMMAND_DELETE : return "delete";
// case APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE:
// return "dictate-or-command-control-toggle";
// default:
// return "unknown";
if (cmd != 'Unknown') {
//alert(cmd);
}
switch (cmd) {
case 'browser-backward':
if (mainWindow.webContents.canGoBack()) {
mainWindow.webContents.goBack();
}
break;
case 'browser-forward':
if (mainWindow.webContents.canGoForward()) {
mainWindow.webContents.goForward();
}
break;
case 'browser-stop':
sendCommand("stop");
break;
case 'browser-search':
sendCommand("search");
break;
case 'browser-favorites':
sendCommand("favorites");
break;
case 'browser-home':
sendCommand("home");
break;
case 'browser-refresh':
sendCommand("refresh");
break;
case 'find':
sendCommand("search");
break;
case 'volume-mute':
sendCommand("togglemute");
break;
case 'volume-down':
sendCommand("volumedown");
break;
case 'volume-up':
sendCommand("volumeup");
break;
case 'media-nexttrack':
sendCommand("next");
break;
case 'media-previoustrack':
sendCommand("previous");
break;
case 'media-stop':
sendCommand("stop");
break;
case 'media-play':
sendCommand("play");
break;
case 'media-pause':
sendCommand("pause");
break;
case 'media-record':
sendCommand("record");
break;
case 'media-fast-forward':
sendCommand("fastforward");
break;
case 'media-rewind':
sendCommand("rewind");
break;
case 'media-play_pause':
sendCommand("playpause");
break;
case 'media-channel-up':
sendCommand("channelup");
break;
case 'media-channel-down':
sendCommand("channeldown");
break;
case 'menu':
sendCommand("menu");
break;
case 'info':
sendCommand("info");
break;
}
}
function setCommandLineSwitches() {
var isLinux = require('is-linux');
if (isLinux()) {
app.commandLine.appendSwitch('enable-transparent-visuals');
app.disableHardwareAcceleration();
}
else if (process.platform === 'win32') {
//app.disableHardwareAcceleration();
app.commandLine.appendSwitch('high-dpi-support', 'true');
app.commandLine.appendSwitch('force-device-scale-factor', '1');
}
}
function supportsTransparentWindow() {
return true;
}
function getWindowStateDataPath() {
var path = require("path");
return path.join(app.getPath('userData'), "windowstate.json");
}
function closeWindow(win) {
try {
win.close();
} catch (err) {
console.log('Error closing window. It may have already been closed. ' + err);
}
}
function onWindowClose() {
if (hasAppLoaded) {
var data = mainWindow.getBounds();
data.state = currentWindowState;
var windowStatePath = getWindowStateDataPath();
require("fs").writeFileSync(windowStatePath, JSON.stringify(data));
}
mainWindow.webContents.executeJavaScript('AppCloseHelper.onClosing();');
// Unregister all shortcuts.
electron.globalShortcut.unregisterAll();
closeWindow(playerWindow);
app.quit();
}
function parseCommandLine() {
var isWindows = require('is-windows');
var result = {};
var commandLineArguments = process.argv.slice(2);
var index = 0;
if (isWindows()) {
result.userDataPath = commandLineArguments[index];
index++;
}
result.cecExePath = commandLineArguments[index];
index++;
result.mpvPath = commandLineArguments[index];
index++;
return result;
}
var commandLineOptions = parseCommandLine();
var userDataPath = commandLineOptions.userDataPath;
if (userDataPath) {
app.setPath('userData', userDataPath);
}
function onCecCommand(cmd) {
console.log("Command received: " + cmd);
sendCommand(cmd);
}
/* CEC Module */
function initCec() {
try {
const cec = require('./cec/cec');
var cecExePath = commandLineOptions.cecExePath;
// create the cec event
const EventEmitter = require("events").EventEmitter;
var cecEmitter = new EventEmitter();
var cecOpts = {
cecExePath: cecExePath,
cecEmitter: cecEmitter
};
cec.init(cecOpts);
cecEmitter.on("receive-cmd", onCecCommand);
} catch (err) {
console.log('error initializing cec: ' + err);
}
}
function getWindowId(win) {
var Long = require("long");
var os = require("os");
var handle = win.getNativeWindowHandle();
if (os.endianness() == "LE") {
if (handle.length == 4) {
handle.swap32();
} else if (handle.length == 8) {
handle.swap64();
} else {
console.log("Unknown Native Window Handle Format.");
}
}
var longVal = Long.fromString(handle.toString('hex'), unsigned = true, radix = 16);
return longVal.toString();
}
function initPlaybackHandler(mpvPath) {
var playbackhandler = require('./playbackhandler/playbackhandler');
playbackhandler.initialize(getWindowId(playerWindow), mpvPath);
playbackhandler.registerMediaPlayerProtocol(electron.protocol, mainWindow);
}
setCommandLineSwitches();
var fullscreenOnShow = false;
var windowShowCount = 0;
function onWindowShow() {
windowShowCount++;
if (windowShowCount == 2) {
mainWindow.setFullScreen(true);
if (!fullscreenOnShow) {
// hack alert. in electron 1.4 under windows, the app starts up black. changing window state seems to resolve it.
mainWindow.setFullScreen(false);
}
fullscreenOnShow = false;
mainWindow.center();
mainWindow.focus();
initialShowEventsComplete = true;
}
}
app.on('quit', function () {
closeWindow(mainWindow);
});
function onPlayerWindowRestore() {
mainWindow.focus();
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function () {
var isWindows = require('is-windows');
var windowStatePath = getWindowStateDataPath();
var previousWindowInfo;
try {
previousWindowInfo = JSON.parse(require("fs").readFileSync(windowStatePath, 'utf8'));
}
catch (e) {
previousWindowInfo = {};
}
var windowOptions = {
transparent: false, //supportsTransparency,
frame: false,
resizable: false,
title: 'Emby Theater',
minWidth: 720,
minHeight: 480,
//alwaysOnTop: true,
skipTaskbar: isWindows() ? false : true,
//show: false,
backgroundColor: '#00000000',
center: true,
show: false,
webPreferences: {
webSecurity: false,
webgl: false,
nodeIntegration: false,
plugins: false,
webaudio: true,
java: false,
allowDisplayingInsecureContent: true,
allowRunningInsecureContent: true,
experimentalFeatures: false,
devTools: enableDevTools
},
icon: __dirname + '/icon.ico'
};
windowOptions.width = previousWindowInfo.width || 1280;
windowOptions.height = previousWindowInfo.height || 720;
if (previousWindowInfo.x != null && previousWindowInfo.y != null) {
windowOptions.x = previousWindowInfo.x;
windowOptions.y = previousWindowInfo.y;
}
playerWindow = new BrowserWindow(windowOptions);
windowOptions.backgroundColor = '#00000000';
windowOptions.parent = playerWindow;
windowOptions.transparent = true;
windowOptions.resizable = true;
windowOptions.skipTaskbar = false;
// Create the browser window.
loadStartInfo().then(function () {
mainWindow = new BrowserWindow(windowOptions);
if (enableDevToolsOnStartup) {
mainWindow.openDevTools();
}
mainWindow.webContents.on('dom-ready', setStartInfo);
var url = getAppUrl();
windowStateOnLoad = previousWindowInfo.state;
addPathIntercepts();
registerAppHost();
registerFileSystem();
registerServerdiscovery();
registerWakeOnLan();
// and load the index.html of the app.
mainWindow.loadURL(url);
mainWindow.setMenu(null);
mainWindow.on('move', onWindowMoved);
mainWindow.on('app-command', onAppCommand);
mainWindow.on("close", onWindowClose);
mainWindow.on("minimize", onMinimize);
mainWindow.on("maximize", onMaximize);
mainWindow.on("enter-full-screen", onEnterFullscreen);
mainWindow.on("leave-full-screen", onLeaveFullscreen);
mainWindow.on("restore", onRestore);
mainWindow.on("unmaximize", onUnMaximize);
mainWindow.on("resize", onWindowResize);
playerWindow.on("restore", onPlayerWindowRestore);
playerWindow.on("enter-full-screen", onPlayerWindowRestore);
playerWindow.on("maximize", onPlayerWindowRestore);
playerWindow.on("focus", onPlayerWindowRestore);
playerWindow.on("show", onWindowShow);
mainWindow.on("show", onWindowShow);
// Only the main window should be set to full screen.
// This is done after the window is shown because the
// player window otherwise is shown behind the task bar.
if (previousWindowInfo.state == 'Fullscreen') {
fullscreenOnShow = true;
}
playerWindow.show();
mainWindow.show();
initCec();
initPlaybackHandler(commandLineOptions.mpvPath);
});
});
})();