-
Notifications
You must be signed in to change notification settings - Fork 16
/
client_control.js
885 lines (831 loc) · 22 KB
/
client_control.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
/* vbsBg: Handles the slides with background included */
const vbsBg =`
Dim objPPT
Dim preState
Dim ap
Dim curPos
Dim newWidth
Dim newHeight
On Error Resume Next
Sub Proc()
Dim sl
Dim shGroup
Dim isSaved
Set objSlideShow = ap.SlideShowWindow.View
If ap.Saved Then
isSaved = True
Else
isSaved = False
End If
With ap.Slides(objSlideShow.CurrentShowPosition)
If newWidth = 0 Then
.Export Wscript.Arguments.Item(0) & "/Slide.png", "PNG"
Else
.Export Wscript.Arguments.Item(0) & "/Slide.png", "PNG", newWidth, newHeight
End If
End With
If isSaved = True Then
ap.Saved = True
End If
Dim entryEffect
Dim duration
entryEffect = ap.Slides(curPos).SlideShowTransition.EntryEffect
duration = ap.Slides(curPos).SlideShowTransition.Duration
Wscript.Echo "PPTNDI: Sent " & duration & " " & entryEffect & " " & objSlideShow.CurrentShowPosition
End Sub
sub Main()
newWidth = 0
newHeight = 0
If Wscript.Arguments.Item(1) = 0 Then
Else
newWidth = Wscript.Arguments.Item(1)
newHeight = Wscript.Arguments.Item(2)
End If
Do While True
On Error Resume Next
Err.Clear
Set objPPT = CreateObject("PowerPoint.Application")
If Err.Number = 0 Then
Err.Clear
Set ap = objPPT.ActivePresentation
curPos = 0
If Err.Number = 0 Then
objPPT.DisplayAlerts = False
Err.Clear
curPos = ap.SlideShowWindow.View.CurrentShowPosition
If Err.Number = 0 Then
If ap.SlideShowWindow.View.State = -1 Then
ElseIf ap.SlideShowWindow.View.State = 1 Then
Proc()
ElseIf ap.SlideShowWindow.View.State = 2 Then
'Wscript.Echo "PPTNDI: Paused" -- breaks hotkeys
Proc()
ElseIf ap.SlideShowWindow.View.State = 3 Then
Wscript.Echo "PPTNDI: Black"
ElseIf ap.SlideShowWindow.View.State = 4 Then
Wscript.Echo "PPTNDI: White"
ElseIf ap.SlideShowWindow.View.State = 5 Then
Wscript.Echo "PPTNDI: Done"
End If
Else
Wscript.Echo "PPTNDI: Ready"
curPos = 0
End If
End If
Else
Wscript.Echo "PPTNDI: NoPPT"
End If
cmd = Wscript.StdIn.ReadLine()
If left(cmd, 6) = "setRes" Then
Dim p1
Dim res
p1 = Replace(cmd, "setRes ", "")
res = Split(p1, "x")
newWidth = res(0)
newHeight = res(1)
End If
Loop
End Sub
Main
`;
/* vbsNoBg: Handles the slides with no background included */
const vbsNoBg =`
Dim objPPT
Dim preState
Dim ap
Dim curPos
Dim newWidth
Dim newHeight
On Error Resume Next
Sub Proc()
Dim sl
Dim shGroup
Dim sngWidth
Dim sngHeight
Dim origGrpCnt
oriGrpCnt = 0
With ap.PageSetup
sngWidth = .SlideWidth
sngHeight = .SlideHeight
End With
Set objSlideShow = ap.SlideShowWindow.View
With ap.Slides(objSlideShow.CurrentShowPosition)
Dim isSaved
If ap.Saved Then
isSaved = True
Else
isSaved = False
End If
origGrpCnt = ap.Slides(objSlideShow.CurrentShowPosition).Shapes.Range().Count
With .Shapes.AddTextBox( 1, 0, 0, sngWidth, sngHeight)
Set shpGroup = ap.Slides(objSlideShow.CurrentShowPosition).Shapes.Range()
If shpGroup.Count = origGrpCnt Then
.Delete
origGrpCnt = 0
If isSaved = True Then
ap.Saved = True
End If
Exit Sub
End If
If newWidth = 0 Then
shpGroup.Export Wscript.Arguments.Item(0) & "/Slide.png", 2, , , 1
Else
shpGroup.Export Wscript.Arguments.Item(0) & "/Slide.png", 2, Round(newWidth / 1.33333333, 0), Round(newHeight / 1.33333333, 0), 1
End If
.Delete
End With
If isSaved = True Then
ap.Saved = True
End If
Dim entryEffect
Dim duration
entryEffect = ap.Slides(curPos).SlideShowTransition.EntryEffect
duration = ap.Slides(curPos).SlideShowTransition.Duration
Wscript.Echo "PPTNDI: Sent " & duration & " " & entryEffect & " " & objSlideShow.CurrentShowPosition
End With
End Sub
sub Main()
newWidth = 0
newHeight = 0
If Wscript.Arguments.Item(1) = 0 Then
Else
newWidth = Wscript.Arguments.Item(1)
newHeight = Wscript.Arguments.Item(2)
End If
Do While True
On Error Resume Next
Err.Clear
Set objPPT = CreateObject("PowerPoint.Application")
If Err.Number = 0 Then
Err.Clear
Set ap = objPPT.ActivePresentation
curPos = 0
If Err.Number = 0 Then
objPPT.DisplayAlerts = False
Err.Clear
curPos = ap.SlideShowWindow.View.CurrentShowPosition
If Err.Number = 0 Then
If ap.SlideShowWindow.View.State = -1 Then
ElseIf ap.SlideShowWindow.View.State = 1 Then
Proc()
ElseIf ap.SlideShowWindow.View.State = 2 Then
'Wscript.Echo "PPTNDI: Paused" -- breaks hotkeys
Proc()
ElseIf ap.SlideShowWindow.View.State = 3 Then
Wscript.Echo "PPTNDI: Black"
ElseIf ap.SlideShowWindow.View.State = 4 Then
Wscript.Echo "PPTNDI: White"
ElseIf ap.SlideShowWindow.View.State = 5 Then
Wscript.Echo "PPTNDI: Done"
End If
Else
Wscript.Echo "PPTNDI: Ready"
curPos = 0
End If
End If
Else
Wscript.Echo "PPTNDI: NoPPT"
End If
cmd = Wscript.StdIn.ReadLine()
If left(cmd, 6) = "setRes" Then
Dim p1
Dim res
p1 = Replace(cmd, "setRes ", "")
res = Split(p1, "x")
newWidth = res(0)
newHeight = res(1)
End If
Loop
End Sub
Main
`;
/* vbsCheckSlide: Checks the status of current slide in real time */
const vbsCheckSlide =`
Dim objPPT
Dim preSlideIdx
Dim curPos
preSlideIdx = 0
sub Main()
Wscript.Echo "Status: 0"
Do While True
On Error Resume Next
Err.Clear
Set objPPT = CreateObject("PowerPoint.Application")
If Err.Number = 0 Then
Err.Clear
Set ap = objPPT.ActivePresentation
If Err.Number = 0 Then
Err.Clear
curPos = ap.SlideShowWindow.View.CurrentShowPosition
If preSlideIdx = curPos Then
Else
Wscript.Echo "Status: " & curPos
preSlideIdx = curPos
End If
Else
Wscript.Echo "Status: OFF"
End If
Else
preSlideIdx = 0
Wscript.Echo "Status: 0"
End If
Wscript.Sleep(500)
Loop
End sub
Main
`;
/* vbsDiretCmd: Handles hotkey */
const vbsDirectCmd = `
Dim objPPT
Dim cmd
sub Main()
Do While True
On Error Resume Next
cmd = Wscript.StdIn.ReadLine()
Set objPPT = CreateObject("PowerPoint.Application")
If Err.Number = 0 Then
Err.Clear
Set ap = objPPT.ActivePresentation
If Err.Number = 0 Then
Err Clear
Set objSlideShow = ap.SlideShowWindow.View
If cmd = "prev" Then
objSlideShow.GotoSlide objSlideShow.CurrentShowPosition - 1
End If
If cmd = "next" Then
objSlideShow.GotoSlide objSlideShow.CurrentShowPosition + 1
End If
If cmd = "black" Then
ap.SlideShowWindow.View.State = 3
End If
If cmd = "white" Then
ap.SlideShowWindow.View.State = 4
End If
If cmd = "pause" Then
ap.SlideShowWindow.View.State = 5
End If
End If
End If
Loop
End Sub
Main
`;
$(document).ready(function() {
const spawn = require( 'child_process' ).spawn;
const ipc = require('electron').ipcRenderer;
const fs = require("fs-extra");
const ffi = require('ffi');
let lib;
let ignoreIoHook = false;
let ioHook = null;
let tmpDir = null;
let preFile = "";
let slideWidth = 0;
let slideHeight = 0;
let customSlideX = 0;
let customSlideY = 0;
let lastSignalTime = 0;
let lastSlideStat = "0";
let configData = {};
let pin = true;
let mustStop = false;
let res; // vbsBg & vbsNoBg
let res2; // vbsDirectCmd
let res3; // vbsCheckSlide
let duration = "";
let effect = "";
let slideIdx = "";
let slideTranTimers = [];
let curStatus = "";
function alertMsg(myMsg) {
const { remote } = require('electron');
const {dialog} = require('electron').remote;
let currentWindow = remote.getCurrentWindow();
let options;
options = {
type: 'info',
message: myMsg,
buttons: ["OK"]
};
dialog.showMessageBoxSync(currentWindow, options);
}
function runLib() {
try {
const { remote } = require('electron');
let { RTLD_NOW, RTLD_GLOBAL } = ffi.DynamicLibrary.FLAGS;
ffi.DynamicLibrary(
remote.app.getAppPath().replace(/(\\|\/)resources(\\|\/)app\.asar/, "") + '/Processing.NDI.Lib.x64.dll',
RTLD_NOW | RTLD_GLOBAL
);
lib = ffi.Library(remote.app.getAppPath().replace(/(\\|\/)resources(\\|\/)app\.asar/, "") + '/PPTNDI.dll', {
'init': [ 'int', [] ],
'destroy': [ 'int', [] ],
'send': [ 'int', [ "string", "bool" ] ]
});
} catch(e) {
alertMsg(e);
ipc.send('remote', "exit");
}
if (lib.init() === 1) {
alertMsg('Failed to create a listening server!');
ipc.send('remote', "exit");
return;
}
}
function stopSlideTransition() {
for (var pp=2; pp<=9; pp++) {
clearTimeout(slideTranTimers[pp]);
}
mustStop = true;
}
function sendColorNDI(color) {
const now = new Date().getTime();
const sharp = require('sharp');
let file;
let colorInfo = {};
let mWidth = (slideWidth === 0 ? 1920 : slideWidth);
let mHeight = (slideHeight === 0 ? 1080 : slideHeight);
switch (color) {
case "black":
file = tmpDir + "/SlideBlack.png";
colorInfo = { r: 0, g: 0, b: 0, alpha: 1 }
break;
case "white":
file = tmpDir + "/SlideWhite.png";
colorInfo = { r: 255, g: 255, b: 255, alpha: 1 };
break;
case "tran":
file = tmpDir + "/SlideTran.png";
colorInfo = { r: 255, g: 255, b: 255, alpha: 0 };
break;
}
sharp({
create: {
width: mWidth,
height: mHeight,
channels: 4,
background: colorInfo
}
}).toFile(file).then( data => {
$("#slidePreview").attr("src", file + "?" + now);
lib.send(file, false);
});
}
function updateStat(cmd, details) {
let msg = "Status: ";
curStatus=cmd;
msg = msg + cmd;
if (/\S/.test(details)) {
msg = msg + "<br />" + details;
}
$("#tip").html(msg);
}
function sendNDI(file, data) {
const now = new Date().getTime();
const cmd = data.toString();
let newSlideIdx;
preFile = tmpDir + "/SlidePre.png";
stopSlideTransition();
if (/^PPTNDI: Sent /.test(cmd)) {
let tmpCmd = cmd.replace(/^PPTNDI: Sent /, "");
duration = tmpCmd.split(" ")[0].trim();
effect = tmpCmd.split(" ")[1].trim();
newSlideIdx = tmpCmd.split(" ")[2].trim();
} else if(/^PPTNDI: White/.test(cmd)) {
file = tmpDir + "/SlideWhite.png";
newSlideIdx = "white";
} else if(/^PPTNDI: Black/.test(cmd)) {
file = tmpDir + "/SlideBlack.png"
newSlideIdx = "black";
} else if(/^PPTNDI: Done/.test(cmd)) {
updateStat("END OF SLIDE SHOW", "");
return;
//} else if(/^PPTNDI: Paused/.test(cmd)) {
// updateStat("PAUSED", "");
// return;
} else if(/^PPTNDI: Ready/.test(cmd)) {
updateStat("READY", "In PowerPoint, start the Slide Show.");
return;
} else if(/^PPTNDI: NoPPT/.test(cmd)) {
updateStat("ERROR", "Microsoft PowerPoint not found.");
return;
} else {
console.log(cmd);
return;
}
updateStat("OK", "The request has been completed.");
if (/^PPTNDI: Sent /.test(cmd)) {
let fd;
try {
fd = fs.openSync(file, 'r+');
} catch (err) {
if (err && err.code === 'EBUSY'){
if (fd !== undefined) {
fs.closeSync(fd);
}
sendNDI(file, data);
return;
}
}
if (fd !== undefined) {
fs.closeSync(fd);
}
function getMeta(url, callback) {
var img = new Image();
img.src = url;
img.onload = function() { callback(this.width, this.height); }
}
getMeta(
file + "?" + now,
function(width, height) {
slideWidth = width;
slideHeight = height;
$("#slideRes").html("( " + slideWidth + " x " + slideHeight + " )");
}
);
$("#slidePreview").attr("src", file + "?" + now);
}
if (slideIdx === newSlideIdx) {
if (lastSignalTime >= (Date.now() - 500)) {
return;
}
}
console.log(cmd);
slideIdx = newSlideIdx;
lastSignalTime = Date.now();
if (newSlideIdx === "black" || newSlideIdx === "white") {
sendColorNDI(newSlideIdx);
} else {
if (preFile !== "") {
let buf1;
let buf2;
if (fs.existsSync(preFile) && fs.existsSync(file)) {
buf1 = fs.readFileSync(preFile);
buf2 = fs.readFileSync(file);
if (buf1.equals(buf2)) {
return;
}
}
}
if ($("#slide_tran").is(":checked")) {
if(!/^\s*0\s*$/.test(effect)) {
if (fs.existsSync(preFile)) {
mustStop = false;
procTransition(file, data);
return;
}
}
}
try {
fs.copySync(file, preFile);
} catch(e) {
console.log("file could not be generated: "+ preFile);
}
lib.send(file, false);
}
}
function handleHook(cmd) {
switch (cmd) {
case "prev":
case "next":
res2.stdin.write(cmd + "\n");
res.stdin.write("\n");
break;
case "tran":
setTimeout(function() {
ignoreIoHook = true;
sendColorNDI("tran");
ignoreIoHook = false;
}, 500);
break;
case "black":
case "white":
if (slideWidth === 0 || curStatus === "READY") {
sendColorNDI(cmd);
} else {
res2.stdin.write(cmd + "\n");
res.stdin.write("\n");
}
break;
default:
break;
}
}
function registerIoHook() {
ioHook = require('iohook');
ioHook.on('keyup', event => {
if (event.shiftKey && event.ctrlKey) {
let chr = String.fromCharCode( event.rawcode );
if (chr === "") return;
switch (chr) {
case configData.hotKeys.prev: handleHook("prev"); break;
case configData.hotKeys.next: handleHook("next"); break;
case configData.hotKeys.transparent: handleHook("tran"); break;
case configData.hotKeys.black: handleHook("black"); break;
case configData.hotKeys.white: handleHook("white"); break;
default: break;
}
}
});
ioHook.on('keydown', event => {
console.log(event.keycode);
if (!ignoreIoHook) {
if (((event.altKey || event.shiftKey || event.ctrlKey || event.metaKey) && event.keycode === 63) ||
!(event.altKey || event.shiftKey || event.ctrlKey || event.metaKey) && (
event.keycode === 63 || event.keycode === 3655 || event.keycode === 3663 ||
event.keycode === 28 || event.keycode === 57 || event.keycode === 48 || event.keycode === 17 ||
event.keycode === 49 || event.keycode === 25 || event.keycode === 60999 || event.keycode === 61007 ||
event.keycode === 61003 || event.keycode === 61000 || event.keycode === 61005 || event.keycode === 61008
)) {
res.stdin.write("\n");
}
}
});
ioHook.on('mouseup', event => {
if (!ignoreIoHook) {
res.stdin.write("\n");
}
});
ioHook.on('mousewheel', event => {
if (!ignoreIoHook) {
res.stdin.write("\n");
}
});
ioHook.start();
}
function procTransition(file, data) {
const transLvl=9;
preFile = tmpDir + "/SlidePre.png";
try {
for (var i=2; i<=transLvl; i++) {
fs.unlinkSync(tmpDir + "/t" + i.toString() + ".png");
}
} catch(e) {
}
function sendSlides(i) {
console.log(i);
if (mustStop) {
return;
}
function setLast() {
if (mustStop) {
return;
}
slideTranTimers[10] = setTimeout(function() {
lib.send(tmpDir + "/Slide.png", false);
if (fs.existsSync(file)) {
try {
fs.copySync(file, preFile);
} catch(e) {
console.log("file could not be generated: "+ preFile);
}
}
}, 10 * parseFloat(duration) * 50);
}
slideTranTimers[i] = setTimeout(function() {
lib.send(tmpDir + "/t" + i.toString() + ".png", true);
}, i * parseFloat(duration) * 50);
if (i === transLvl) {
const now = new Date().getTime();
setLast();
$("#slidePreview").attr("src", file + "?" + now);
}
}
function doTrans() {
const mergeImages = require('merge-images');
stopSlideTransition();
mustStop = false;
for (let i=2; i<=transLvl; i++) {
let now = new Date().getTime();
mergeImages([
{ src: preFile + "?" + now, opacity: 1 - (0.1 * i) },
{ src: file + "?" + now, opacity: 0.1 * i }
])
.then(b64 => {
let b64data = b64.replace(/^data:image\/png;base64,/, "");
try {
fs.writeFileSync(tmpDir + "/t" + i.toString() + ".png", b64data, 'base64');
if (i === 8) {
for (var i2=2; i2<=transLvl; i2++) {
sendSlides(i2);
}
}
} catch(e) {
}
});
};
}
doTrans();
}
function init() {
const { remote } = require('electron');
let file;
let vbsDir;
let vbsDir2;
let newVbsContent;
let now = new Date().getTime();
try {
process.chdir(remote.app.getAppPath().replace(/(\\|\/)resources(\\|\/)app\.asar/, ""));
} catch(e) {
}
runLib();
tmpDir = process.env.TEMP + '/ppt_ndi';
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
}
tmpDir += '/' + now;
fs.mkdirSync(tmpDir);
vbsDir = tmpDir + '/wb.vbs';
vbsDir2 = tmpDir + '/wb2.vbs';
vbsDir3 = tmpDir + '/wb3.vbs';
file = tmpDir + "/Slide.png";
newVbsContent = vbsNoBg;
try {
fs.writeFileSync(vbsDir, newVbsContent, 'utf-8');
} catch(e) {
alertMsg('Failed to access the temporary directory!');
return;
}
try {
fs.writeFileSync(vbsDir2, vbsDirectCmd, 'utf-8');
} catch(e) {
}
try {
fs.writeFileSync(vbsDir3, vbsCheckSlide, 'utf-8');
} catch(e) {
alertMsg('Failed to access the temporary directory!');
return;
}
if (fs.existsSync(vbsDir)) {
let resX = 0;
let resY = 0;
if (customSlideX == 0 || customSlideY == 0 || !/\S/.test(customSlideX) || !/\S/.test(customSlideY)) {
resX = 0;
resY = 0;
} else {
resX = customSlideX;
resY = customSlideY;
}
res = spawn( 'cscript.exe', [ vbsDir, tmpDir, resX, resY, "//NOLOGO", '' ] );
res.stdout.on('data', function(data) {
sendNDI(file, data);
});
} else {
alertMsg('Failed to parse the presentation!');
return;
}
if (fs.existsSync(vbsDir2)) {
res2 = spawn( 'cscript.exe', [ vbsDir2, "//NOLOGO", '' ] );
}
if (fs.existsSync(vbsDir3)) {
res3 = spawn( 'cscript.exe', [ vbsDir3, "//NOLOGO", '' ] );
} else {
alertMsg('Failed to parse the presentation!');
return;
}
res3.stdout.on('data', function(data) {
let curSlideStat = data.toString().replace(/^Status: /, "");
if (/^\s*OFF\s*$/.test(curSlideStat)) {
// Ready
updateStat("READY", "In PowerPoint, open a presentation file.");
} else if (/^\s*0\s*$/.test(curSlideStat)) {
// Not found
// updateStat("-", "");
} else {
// ON
}
if (/^\s*0\s*$/.test(lastSlideStat)) {
if (!/^\s*0\s*$/.test(curSlideStat)) {
if (slideIdx != curSlideStat) {
res.stdin.write("\n");
}
}
}
lastSlideStat = curSlideStat;
});
// Enable Always On Top by default
ipc.send('remote', "onTop");
$("#pin").attr("src", "pin_green.png");
pin = true;
// Enable Slide Checkerboard by default
$("#slidePreview").css('background-image', "url('trans_slide.png')");
registerIoHook();
reflectConfig();
$("#resWidth").val("0");
$("#resHeight").val("0");
$("#resWidth, #resHeight").click(function() {
$(this).val("");
});
$("#setRes").click(function() {
let resX = $("#resWidth").val();
let resY = $("#resHeight").val();
if (/^\d+$/.test(resX) && /^\d+$/.test(resY)) {
res.stdin.write("setRes " + resX + "x" + resY + "\n");
customSlideX = parseInt(resX, 10);
customSlideY = parseInt(resY, 10);
}
});
}
function cleanupForTemp() {
if (fs.existsSync(tmpDir)) {
fs.removeSync(tmpDir);
}
}
function reflectConfig() {
const configFile = 'config.js';
let configPath = "";
const { remote } = require('electron');
configPath = remote.app.getAppPath().replace(/(\\|\/)resources(\\|\/)app\.asar/, "") + "/" + configFile;
if (!fs.existsSync(configPath)) {
const appDataPath = process.env.APPDATA + "/PPT-NDI";
configPath = appDataPath + "/" + configFile;
}
if (fs.existsSync(configPath)) {
$.getJSON(configPath, function(json) {
configData.hotKeys = json.hotKeys;
});
} else {
// Do nothing
}
}
function cleanupForExit() {
lib.destroy();
cleanupForTemp();
ipc.send('remote', "exit");
}
ipc.on('remote' , function(event, data){
if (data.msg == "exit") {
cleanupForExit();
return;
}
if (data.msg == "reload") {
reflectConfig();
return;
}
});
$('#closeImg').click(function() {
cleanupForExit();
});
$('#bk').click(function() {
let newVbsContent;
let vbsDir = tmpDir + '/wb.vbs';
let file = tmpDir + "/Slide.png";
if ($("#bk").is(":checked")) {
newVbsContent = vbsBg;
try {
fs.writeFileSync(vbsDir, newVbsContent, 'utf-8');
} catch(e) {
alertMsg('Failed to access the temporary directory!');
return;
}
} else {
newVbsContent = vbsNoBg;
try {
fs.writeFileSync(vbsDir, newVbsContent, 'utf-8');
} catch(e) {
alertMsg('Failed to access the temporary directory!');
return;
}
}
res.stdin.pause();
res.kill();
res = null;
if (fs.existsSync(vbsDir)) {
let resX = 0;
let resY = 0;
if (customSlideX == 0 || customSlideY == 0 || !/\S/.test(customSlideX) || !/\S/.test(customSlideY)) {
resX = 0;
resY = 0;
} else {
resX = customSlideX;
resY = customSlideY;
}
res = spawn( 'cscript.exe', [ vbsDir, tmpDir, resX, resY, "//NOLOGO", '' ] );
res.stdout.on('data', function(data) {
sendNDI(file, data);
});
} else {
alertMsg('Failed to parse the presentation!');
return;
}
});
$('#trans_checker').click(function() {
if ($("#trans_checker").is(":checked")) {
$("#slidePreview").css('background-image', "url('trans_slide.png')");
} else {
$("#slidePreview").css('background-image', "url('null_slide.png')");
}
});
$('#pin').click(function() {
if (pin) {
ipc.send('remote', "onTopOff");
$("#pin").attr("src", "pin_grey.png");
pin = false;
} else {
ipc.send('remote', "onTop");
$("#pin").attr("src", "pin_green.png");
pin = true;
}
});
init();
});