Skip to content

Commit

Permalink
[FIX] moved context-aware modules to main process
Browse files Browse the repository at this point in the history
  • Loading branch information
ykhwong committed Nov 16, 2019
1 parent 1a7244c commit 84dd630
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 152 deletions.
132 changes: 128 additions & 4 deletions backend.js
Expand Up @@ -12,6 +12,9 @@ app.on('ready', function() {
let startAsTray = false;
let isMainWinShown = false;
let isMainWin2shown = false;
let remoteLib = {};
let remoteVar = {};

const winData = {
"home" : {
"width" : 600,
Expand Down Expand Up @@ -201,8 +204,7 @@ app.on('ready', function() {
backgroundColor: '#060621',
webPreferences: {
webSecurity: false,
nodeIntegration: true,
nodeIntegrationInWorker: true
nodeIntegration: true
}
});
if (!maximizable) {
Expand Down Expand Up @@ -254,8 +256,9 @@ app.on('ready', function() {

function loadIpc() {
const ipc = require('electron').ipcMain;

ipc.on('remote', (event, data) => {
switch (data) {
switch (data.name) {
case "exit":
if (mainWindow2 != null) {
mainWindow2.destroy();
Expand Down Expand Up @@ -295,12 +298,133 @@ app.on('ready', function() {
mainWindow2.webContents.send('remote', { msg: 'reload' });
}
break;
case "passConfigData":
remoteVar.configData = data.details;
break;
case "passIgnoreIoHookVal":
remoteVar.ignoreIoHook = data.details;
break;
default:
console.log("Unhandled function - loadIpc()");
console.log("Unhandled function - loadIpc(): " + data);
mainWindow.destroy();
break;
}
});

ipc.on('require', (event, data) => {
/*
data.lib : string
data.func : string
data.on : string
data.args : array
*/
let ret = -1;
switch (data.lib) {
case "ffi":
if (data.func === null && data.args === null) {
remoteLib.ffi = require("ffi");
try {
//const { remote } = require('electron');
let { RTLD_NOW, RTLD_GLOBAL } = remoteLib.ffi.DynamicLibrary.FLAGS;
remoteLib.ffi.DynamicLibrary(
app.getAppPath().replace(/(\\|\/)resources(\\|\/)app\.asar/, "") + '/Processing.NDI.Lib.x64.dll',
RTLD_NOW | RTLD_GLOBAL
);
remoteVar.lib = remoteLib.ffi.Library(app.getAppPath().replace(/(\\|\/)resources(\\|\/)app\.asar/, "") + '/PPTNDI.dll', {
'init': [ 'int', [] ],
'destroy': [ 'int', [] ],
'send': [ 'int', [ "string", "bool" ] ]
});
ret = remoteVar.lib;
} catch(e) {
console.log("remoteLib failed: " + e);
}
}
if (data.func === "init") {
let ret = -1;
if (typeof remoteVar.lib !== 'undefined') {
ret = remoteVar.lib.init();
}
} else if (data.func === "destroy") {
let ret = -1;
if (typeof remoteVar.lib !== 'undefined') {
ret = remoteVar.lib.destroy();
}
} else if (data.func === "send") {
let ret = -1;
if (typeof remoteVar.lib !== 'undefined') {
ret = remoteVar.lib.send( ...data.args );
}
}
break;
case "iohook":
if (data.on === null && data.args === null) {
remoteLib.ioHook = require('iohook');
ret = remoteLib.ioHook;
} else if (data.on === "start") {
ret = remoteLib.ioHook.start();
} else if (data.on === "keyup") {
remoteLib.ioHook.on('keyup', event => {
if (typeof mainWindow2 === 'undefined' || mainWindow2 === null) return;
if (event.shiftKey && event.ctrlKey) {
let chr = String.fromCharCode( event.rawcode );
if (chr === "") return;
switch (chr) {
case remoteVar.configData.hotKeys.prev:
mainWindow2.webContents.send('remote', { msg: 'gotoPrev' });
break;
case remoteVar.configData.hotKeys.next:
mainWindow2.webContents.send('remote', { msg: 'gotoNext' });
break;
case remoteVar.configData.hotKeys.transparent:
mainWindow2.webContents.send('remote', { msg: 'update_trn' });
break;
case remoteVar.configData.hotKeys.black:
mainWindow2.webContents.send('remote', { msg: 'update_black' });
break;
case remoteVar.configData.hotKeys.white:
mainWindow2.webContents.send('remote', { msg: 'update_white' });
break;
default:
break;
}
}
});
ret = 1;
} else if (data.on === "keydown") {
remoteLib.ioHook.on('keydown', event => {
if (typeof mainWindow2 === 'undefined' || mainWindow2 === null || remoteVar.ignoreIoHook) return;
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
)) {
mainWindow2.webContents.send('remote', { msg: 'stdin_write_newline' });
}
});
ret = 1;
} else if (data.on === "mouseup") {
remoteLib.ioHook.on('mouseup', event => {
if (typeof mainWindow2 === 'undefined' || mainWindow2 === null || remoteVar.ignoreIoHook) return;
mainWindow2.webContents.send('remote', { msg: 'stdin_write_newline' });
});
ret = 1;
} else if (data.on === "mousewheel") {
remoteLib.ioHook.on('mousewheel', event => {
if (typeof mainWindow2 === 'undefined' || mainWindow2 === null || remoteVar.ignoreIoHook) return;
mainWindow2.webContents.send('remote', { msg: 'stdin_write_newline' });
});
ret = 1;
}
break;
default:
break;
}
event.returnValue = ret;
});

}

init();
Expand Down
156 changes: 91 additions & 65 deletions frontend/client.js
Expand Up @@ -135,9 +135,8 @@ $(document).ready(function() {
const { remote } = require('electron');
const ipc = require('electron').ipcRenderer;
const fs = require("fs-extra");
const ffi = require('ffi');
let ffi;
let lib;
let ioHook;
let maxSlideNum = 0;
let prevSlide = 1;
let currentSlide = 1;
Expand Down Expand Up @@ -168,24 +167,16 @@ $(document).ready(function() {
process.chdir(remote.app.getAppPath().replace(/(\\|\/)resources(\\|\/)app\.asar/, ""));
} catch(e) {
}
try {
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");
ffi = ipc.sendSync("require", { lib: "ffi", func: null, args: null });
if ( ffi === -1 ) {
alertMsg("DLL init failed");
ipc.send('remote', { name: "exit" });
}
if (lib.init() === 1) {

lib = ipc.sendSync("require", { lib: "ffi", func: "init", args: null });
if (lib === 1) {
alertMsg('Failed to create a listening server!');
ipc.send('remote', "exit");
ipc.send('remote', { name: "exit" });
return;
}

Expand Down Expand Up @@ -310,12 +301,26 @@ $(document).ready(function() {
return;
}
slideTranTimers[10] = setTimeout(function() {
lib.send(tmpDir + "/Slide" + currentSlide.toString() + ".png", false);
ipc.sendSync("require", {
lib: "ffi",
func: "send",
args: [
tmpDir + "/Slide" + currentSlide.toString() + ".png",
false
]
});
updateCurNext(curSli, nextSli);
}, 10 * parseFloat(duration) * 50);
}
slideTranTimers[i] = setTimeout(function() {
lib.send(tmpDir + "/t" + i.toString() + ".png", true);
ipc.sendSync("require", {
lib: "ffi",
func: "send",
args: [
tmpDir + "/t" + i.toString() + ".png",
true
]
});
if ( i % 2 === 0 ) {
let now = new Date().getTime();
$("img.image_picker_image:first").attr("src", tmpDir + "/t" + i.toString() + ".png?" + now);
Expand Down Expand Up @@ -361,7 +366,14 @@ $(document).ready(function() {
} else {
stopSlideTransition();
updateCurNext(curSli, nextSli);
lib.send(curSli, false);
ipc.sendSync("require", {
lib: "ffi",
func: "send",
args: [
curSli,
false
]
});
}
$("#slide_cnt").html("SLIDE " + currentSlide + " / " + maxSlideNum);
blkBool = false;
Expand Down Expand Up @@ -792,7 +804,14 @@ $(document).ready(function() {
}
$("img.image_picker_image:first").attr('src', dirTo);

lib.send(dirTo, false);
ipc.sendSync("require", {
lib: "ffi",
func: "send",
args: [
dirTo,
false
]
});
}

$('#blk').click(function() {
Expand Down Expand Up @@ -934,27 +953,15 @@ $(document).ready(function() {
}

function cleanupForExit() {
lib.destroy();
ipc.sendSync("require", { lib: "ffi", func: "destroy", args: null });
cleanupForTemp(false);
ipc.send('remote', "exit");
ipc.send('remote', { name: "exit" });
}

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: gotoPrev(); break;
case configData.hotKeys.next: gotoNext(); break;
case configData.hotKeys.transparent: updateBlkWhtTrn("trn"); break;
case configData.hotKeys.black: updateBlkWhtTrn("black"); break;
case configData.hotKeys.white: updateBlkWhtTrn("white"); break;
}
}
});
ioHook.start();
let ioHook = ipc.sendSync("require", { lib: "iohook", on: null, args: null });
ipc.sendSync("require", { lib: "iohook", on: "keyup", args: null });
ipc.sendSync("require", { lib: "iohook", on: "start", args: null });
}

function reflectConfig() {
Expand All @@ -970,41 +977,60 @@ $(document).ready(function() {
$.getJSON(configPath, function(json) {
configData.hotKeys = json.hotKeys;
configData.startWithTheFirstSlideSelected = json.startWithTheFirstSlideSelected;
ipc.send('remote', { name: "passConfigData", details: configData });
});
} else {
// Do nothing
}
}

ipc.on('remote' , function(event, data){
if (data.msg == "exit") {
cleanupForExit();
return;
}
if (data.msg == "reload") {
reflectConfig();
return;
}
if (data.msg == "focused") {
let stats;
let tmpPptTimestamp = 0;
if (pptTimestamp === 0) {
return;
}
try {
stats = fs.statSync(pptPath);
tmpPptTimestamp = stats.mtimeMs;
if (pptTimestamp === tmpPptTimestamp) {
} else {
pptTimestamp = tmpPptTimestamp;
askReloadFile("", "This file has been modified. Do you want to reload it?", "");
switch (data.msg) {
case "exit":
cleanupForExit();
break;
case "reload":
reflectConfig();
break;
case "focused":
let stats;
let tmpPptTimestamp = 0;
if (pptTimestamp === 0) {
return;
}
} catch(e) {
}
}
if (data.msg == "blurred") {
// we don't care here
try {
stats = fs.statSync(pptPath);
tmpPptTimestamp = stats.mtimeMs;
if (pptTimestamp === tmpPptTimestamp) {
} else {
pptTimestamp = tmpPptTimestamp;
askReloadFile("", "This file has been modified. Do you want to reload it?", "");
}
} catch(e) {
}
break;
case "blurred":
// we don't care here
break;
case "gotoPrev":
gotoPrev();
break;
case "gotoNext":
gotoNext();
break;
case "update_trn":
updateBlkWhtTrn("trn");
break;
case "update_black":
updateBlkWhtTrn("black");
break;
case "update_white":
updateBlkWhtTrn("white");
break;
default:
break;
}
return;
});

$('#minimize').click(function() {
Expand Down

0 comments on commit 84dd630

Please sign in to comment.