-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Izurii/permission-file-check
Permission file check
- Loading branch information
Showing
6 changed files
with
112 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
|
||
|
@@ -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', | ||
|
@@ -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(); | ||
|
@@ -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 [email protected]"); | ||
|
||
tray = new Tray(frogIcon); | ||
tray.setToolTip('Lenovo Y720 Keyboard Backlight Controller'); | ||
tray.setToolTip('Lenovo Y720 Keyboard LED Controller'); | ||
|
||
setMenu(); | ||
|
||
|
@@ -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(); | ||
|
@@ -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 [email protected]"); | ||
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(); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> (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": { | ||
|
@@ -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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.