Skip to content

Commit

Permalink
Jitsi Push-to-Talk
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Nov 15, 2018
1 parent 0aeef31 commit 70e7c63
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 71 deletions.
20 changes: 20 additions & 0 deletions electron_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,27 @@
"dependencies": {
"auto-launch": "^5.0.1",
"electron-window-state": "^4.1.0",
"iohook": "^0.2.4",
"minimist": "^1.2.0",
"png-to-ico": "^1.0.2"
},
"iohook": {
"targets": [
"node-64",
"electron-64"
],
"platforms": [
"win32",
"darwin",
"linux"
],
"arches": [
"x64",
"ia32"
]
},
"cmake-js": {
"runtime": "electron",
"runtimeVersion": "3.0.5"
}
}
94 changes: 94 additions & 0 deletions electron_app/src/electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ if (checkSquirrelHooks()) return;
const argv = require('minimist')(process.argv);
const {app, ipcMain, powerSaveBlocker, BrowserWindow, Menu} = require('electron');
const AutoLaunch = require('auto-launch');
const ioHook = require('iohook');

const tray = require('./tray');
const vectorMenu = require('./vectormenu');
Expand Down Expand Up @@ -238,6 +239,13 @@ app.on('ready', () => {
return false;
}
});
mainWindow.on('blur', () => {
// Stop recording keypresses if Riot loses focus
// Used for Push-To-Talk, keypress recording only triggered when setting
// a global shortcut in Settings
mainWindow.webContents.send('window-blurred');
stopListeningKeys();
});

if (process.platform === 'win32') {
// Handle forward/backward mouse buttons in Windows
Expand Down Expand Up @@ -268,6 +276,92 @@ app.on('before-quit', () => {
}
});

// Counter for keybindings we have registered
let ioHookTasks = 0;

// Limit for amount of keybindings that can be
// registered at once.
const keybindingRegistrationLimit = 1;

// Fires when a global keybinding is being registered
ipcMain.on('register-keybinding', function(ev, keybinding) {
// Prevent registering more than the defined limit
if (ioHookTasks >= keybindingRegistrationLimit) {
ioHookTasks = keybindingRegistrationLimit;
return;
}

// Start listening for global keyboard shortcuts
if (ioHookTasks <= 0) {
ioHookTasks = 0;
ioHook.start();
}
ioHookTasks++;

ioHook.registerShortcut(keybinding.code, () => {
ev.sender.send('keybinding-pressed', keybinding.name);
}, () => {
ev.sender.send('keybinding-released', keybinding.name);
});
});

// Fires when a global keybinding is being unregistered
ipcMain.on('unregister-keybinding', function(ev, keybindingCode) {
// Stop listening for global keyboard shortcuts if we're
// unregistering the last one
if (ioHookTasks <= 1) {
ioHook.stop();
}
ioHookTasks--;

ioHook.unregisterShortcutByKeys(keybindingCode);
});

// Tell renderer process what key was pressed
// iohook has its own encoding for keys, so we can't just use a
// listener in the renderer process to register iohook shortcuts
let renderProcessID = null;
const reportKeyEvent = function(keyEvent) {
// "this" is the renderer process because we call this method with .bind()
renderProcessID.sender.send('keypress', {
keydown: keyEvent.type == 'keydown',
keycode: keyEvent.keycode,
});
};

// Fires when listening on all keys
// !!Security note: Ensure iohook is only allowed to listen to keybindings
// when the browser window is in focus, else an XSS could lead to keylogging
// Currently, this is achieved by leveraging browserWindow to act on focus loss
ipcMain.on('start-listening-keys', function(ev, keybindingCode) {
// Start recording keypresses
if (ioHookTasks <= 0) {
ioHookTasks = 0;
ioHook.start();
}
ioHookTasks++;

renderProcessID = ev;
ioHook.on('keydown', reportKeyEvent);
ioHook.on('keyup', reportKeyEvent);
});

const stopListeningKeys = () => {
// Stop recording keypresses
ioHook.off('keydown', reportKeyEvent);
ioHook.off('keyup', reportKeyEvent);
};

ipcMain.on('stop-listening-keys', () => {
if (ioHookTasks <= 1) {
ioHookTasks = 1;
ioHook.stop();
}
ioHookTasks--;

stopListeningKeys();
});

// Set the App User Model ID to match what the squirrel
// installer uses for the shortcut icon.
// This makes notifications work on windows 8.1 (and is
Expand Down
Loading

0 comments on commit 70e7c63

Please sign in to comment.