Skip to content

Commit

Permalink
Merge pull request #20 from Izurii/permission-file-check
Browse files Browse the repository at this point in the history
Permission file check
  • Loading branch information
Izurii authored May 3, 2021
2 parents 31ba9f0 + 0e34c96 commit 78c000c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 33 deletions.
2 changes: 2 additions & 0 deletions driver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,6 @@ const setKeyboardOptions = async (backLightMode, segmentsOptions, lockfilePath)
}
};

exports.getHidrawDevices = getHidrawDevices;
exports.findCorrectDevice = findCorrectDevice;
exports.setKeyboardOptions = setKeyboardOptions;
103 changes: 89 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const { app, ipcMain, BrowserWindow, Tray, Menu, Notification, dialog } = require('electron');
const { setKeyboardOptions } = require('./driver/index');
const { app, ipcMain, BrowserWindow, Tray, Menu, Notification, dialog, clipboard, nativeImage } = require('electron');
const { getHidrawDevices, findCorrectDevice, setKeyboardOptions } = require('./driver/index');
const { exec, spawn } = require('child_process');

const AutoLaunch = require('auto-launch');
const Store = require('electron-store');
const path = require('path');
const firstRun = require('electron-first-run');

const fs = require('fs');
const isFirstRun = firstRun();
const constants = require('constants');
const sudo = require('sudo-prompt');

const LedController = new AutoLaunch({
name: 'Lenovo Y720 Led Controller',
name: 'Lenovo Y720 Keyboard LED Controller',
isHidden: true
});

Expand All @@ -21,9 +23,9 @@ try {
let mainWindow;
let tray = null;

var frogIcon = path.join(__dirname, '/src/assets/icon.png');
var frogIcon = nativeImage.createFromPath(path.join(__dirname, '/resources/icon.png'));

var trayQuit = false;
var usualQuit = false;
var menu = [
{
label: 'Open/Show',
Expand All @@ -43,7 +45,7 @@ var menu = [
}
}},
{ label: 'Separator', type: 'separator'},
{ label: 'Exit', type: 'normal', click: () => { trayQuit = true; app.quit(); }}
{ label: 'Exit', type: 'normal', click: () => { usualQuit = true; app.quit(); }}
];

const store = new Store();
Expand Down Expand Up @@ -139,11 +141,8 @@ if(!app.requestSingleInstanceLock()) {
body: "I'm on the background, open me again using the tray menu"
});

let res = await setKeyboardOptions(selectedProfile.backlightMode, selectedProfile.profileOptions, app.getPath('userData'));
res!==true && dialog.showErrorBox('Error', res+"\n\nContact the dev for more information izuriihootoh@gmail.com");

tray = new Tray(frogIcon);
tray.setToolTip('Lenovo Y720 Keyboard Backlight Controller');
tray.setToolTip('Lenovo Y720 Keyboard LED Controller');

setMenu();

Expand All @@ -162,13 +161,18 @@ if(!app.requestSingleInstanceLock()) {
show: isFirstRun ? true : false
});

await checkPermission();

app.allowRendererProcessReuse = false;
mainWindow.loadFile(path.join(__dirname, '/src/index.html'));


let res = await setKeyboardOptions(selectedProfile.backlightMode, selectedProfile.profileOptions, app.getPath('userData'));
res!==true && genericError(res);

listenerForHotKey();

mainWindow.on('close', (event) => {
if(!trayQuit) {
if(!usualQuit) {
event.preventDefault();
mainWindow.hide();
if (isFirstRun) backgroundNotification.show();
Expand Down Expand Up @@ -231,4 +235,75 @@ const listenerForHotKey = async () => {

});

};
};

const testPermissionDeviceFile = async (deviceName) => {
return await new Promise((resolve, reject) => { fs.access(`/dev/${deviceName}`, constants.W_OK, (err => resolve(err))); });
};

const checkPermission = async () => {

let correctDevice = await findCorrectDevice(await getHidrawDevices());
let permissionDevice = await testPermissionDeviceFile(correctDevice);
let shellCommand = `chown $USER:$USER /dev/${correctDevice}`;

if(permissionDevice && permissionDevice.code=="EACCES") {

await new Promise((resolve, reject) => {

dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Permissions to write in the device file needed',
message: 'You need to copy and paste, or click the "Copy to clipboard" button, '+
`this into a terminal\n\nsudo ${shellCommand}`+
'\n\nAlternatively you can use the "Do it for me!" button.',
buttons: ['Cancel', 'Copy to clipboard', 'Do it for me!']
}).then(async (result) => {
if(!result.response) {
usualQuit = true; app.quit();
} else {
if (result.response==1) {
clipboard.writeText(shellCommand);
await checkPermissionDialog(correctDevice, resolve);
} else if (result.response==2) {
sudo.exec(shellCommand, {name: 'Lenovo Y720 Keyboard LED Controller'}, async (err, stdout, stderr) => {
if (err) genericError(err.message);
await checkPermissionDialog(correctDevice, resolve);
});
}
}
});
});
}
}

const genericError = (error) => {
dialog.showErrorBox('Error', error+"\n\nContact the dev for more information izuriihootoh@gmail.com");
usualQuit = true; app.quit();
};

const checkPermissionDialog = async (deviceName, resolve) => {
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Checking permission to write device file',
message: 'Use the buttons to check if the permission was granted correctly.',
buttons: ['Cancel', 'Check permission']
}).then(async (result) => {
if(!result.response) {
usualQuit = true; app.quit();
} else if(result.response==1) {
let permissionDevice = await testPermissionDeviceFile(deviceName);
if(permissionDevice && permissionDevice.code=="EACCES") {
dialog.showErrorBox('Error', 'Permission not detected!');
await checkPermissionDialog(deviceName, resolve);
} else {
dialog.showMessageBox(mainWindow, {
type: 'info',
title: 'Success!',
message: 'Permission granted you can now proceed to use the software.'
});
resolve();
}
}
});
}
40 changes: 21 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
{
"productName": "Lenovo Y720 Keyboard LED Controller",
"name": "y720-kb-led-controller",
"engines": {
"node": "=14.16.0"
},
"engineStrict": true,
"version": "v1.7.25-genesis",
"description": "",
"version": "v1.8.13-apocalypse",
"description": "Software to control the keyboard backlight rgb leds of Lenovo Legion Y720 notebook.",
"main": "main.js",
"author": "Izurii Hootoh <izuriihootoh@gmail.com> (https://github.com/Izurii)",
"scripts": {
"postinstall": "electron-builder install-app-deps",
"start": "electron-forge start",
"rebuild": "./node_modules/.bin/electron-rebuild",
"test": "echo \"Error: no test specified\" && exit 1",
"package": "electron-forge package",
"make": "electron-forge make",
"dist": "electron-builder"
},
"build": {
"appId": "y720-kb-led-controller",
"icon": "src/assets/icon.png",
"icon": "resources/icon.png",
"linux": {
"target": [
"AppImage",
"deb"
"deb",
"AppImage"
],
"executableName": "y720-kb-led-controller",
"category": "Utility",
"icon": "./resources/icons",
"executableName": "LenovoY720KeyboardController",
"desktop": {
"Name": "Lenovo Y720 Keyboard Led Controller",
"Icon": "src/assets/icon.png",
"Categories": "Utility"
}
}
},
"files": [
"resources/*",
"driver/*",
"main.js",
"src/**/*"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/Izurii/Lenovo-Y720-KB-LED-Driver.git"
"url": "git+https://github.com/Izurii/Lenovo-Y720-KB-Led-Controller.git"
},
"keywords": [],
"license": "ISC",
"bugs": {
"url": "https://github.com/Izurii/Lenovo-Y720-KB-LED-Driver/issues"
"url": "https://github.com/Izurii/Lenovo-Y720-KB-Led-Controller/issues"
},
"homepage": "https://github.com/Izurii/Lenovo-Y720-KB-LED-Driver#readme",
"homepage": "https://github.com/Izurii/Lenovo-Y720-KB-Led-Controller#readme",
"devDependencies": {
"@electron-forge/cli": "*",
"electron": "^12.0.2",
"electron-builder": "^22.10.5",
"electron-rebuild": "^2.3.5",
"electron-reloader": "^1.2.0"
},
"dependencies": {
Expand All @@ -60,6 +61,7 @@
"electron-first-run": "^3.0.0",
"electron-store": "^7.0.2",
"ioctl": "^2.0.2",
"proper-lockfile": "^4.1.2"
"proper-lockfile": "^4.1.2",
"sudo-prompt": "^9.2.1"
}
}
File renamed without changes
Binary file added resources/icons/128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/assets/icon.ico
Binary file not shown.

0 comments on commit 78c000c

Please sign in to comment.