Skip to content

Commit

Permalink
Merge pull request #47 from Izurii/fix/hotkey-addon
Browse files Browse the repository at this point in the history
Fix/hotkey addon
  • Loading branch information
Izurii authored May 21, 2022
2 parents 131ee8f + 61028df commit 88f4811
Show file tree
Hide file tree
Showing 3 changed files with 901 additions and 712 deletions.
38 changes: 28 additions & 10 deletions libs/hotkey/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ ThreadSafeFunction tsfn;

#define SYS_CLASS_INPUT_PATH "/sys/class/input"
#define DEVICE_INPUT_PATH "/dev/input/"
#define KEYBOARD_VENDOR_ID "48d"
#define KEYBOARD_PRODUCT_ID "c100"

const int HOTKEY_FN_SPACE = 786512;
const int HOTKEY_7 = 786515;
Expand All @@ -31,38 +33,54 @@ string _getInputDevice(const Env &env)
throw Error::New(env, "Error opening input directory");
}

bool foundDevice = false;
bool isDeviceProductCorrect, isDeviceVendorCorrect = false;
while ((entry = readdir(directory)) != NULL)
{

if (foundDevice)
if (isDeviceProductCorrect && isDeviceVendorCorrect)
{
break;
}

if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 && strstr(entry->d_name, "event"))
{
string filePath = string(SYS_CLASS_INPUT_PATH) + "/" + entry->d_name + "/device/uevent";
FILE *fp = fopen(filePath.c_str(), "r");
string devicePath = string(SYS_CLASS_INPUT_PATH) + "/" + entry->d_name + "/device/id/";

if (!fp)
string deviceProductPath = devicePath + "product";
string deviceVendorPath = devicePath + "vendor";

FILE *deviceProductFp = fopen(deviceProductPath.c_str(), "r");
FILE *deviceVendorFp = fopen(deviceVendorPath.c_str(), "r");

if (!deviceProductFp || !deviceVendorFp)
{
throw Error::New(env, "Error opening input file");
}

if (fp)
char line[256] = {0};
while (fgets(line, sizeof(line), deviceProductFp))
{
if (strstr(line, KEYBOARD_PRODUCT_ID))
{
isDeviceProductCorrect = true;
break;
}
}
fclose(deviceProductFp);

if (isDeviceProductCorrect)
{
char line[256] = {0};
while (fgets(line, sizeof(line), fp))
while (fgets(line, sizeof(line), deviceVendorFp))
{
if (strstr(line, "KEY=302ff"))
if (strstr(line, KEYBOARD_VENDOR_ID))
{
foundDevice = true;
isDeviceVendorCorrect = true;
inputDevice = entry->d_name;
break;
}
}
fclose(fp);
fclose(deviceVendorFp);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"node": "=16.9.1"
},
"engineStrict": true,
"version": "2.0.1",
"version": "2.0.2",
"description": "Software to control the keyboard backlight rgb leds of Lenovo Legion Y720 notebook.",
"main": "./dist/js/main.js",
"author": "Izurii Hootoh <izuriihootoh@gmail.com> (https://github.com/Izurii)",
Expand Down
Loading

0 comments on commit 88f4811

Please sign in to comment.