اندی پیش برادرم یک مونوپاد خرید کرد که به دلیل استفاده نادرست، مونوپاد خراب شد اما ریموت بلوتوثی مونوپاد سالم ماند و به دست من افتاد. بنده هم این فکر به ذهنم خطور کرد که از این ریموت ۳ کلیده به عنوان ریموت بلوتوثی لپتاپ استفاده کنم. بعد از روشن کردن ریموت و اتصال لپتاپ بهش متوجه شدم که ریموت به عنوان یک کیبورد عمل میکند. با نوشتن یک برنامه ساده به زبان سی و خواندن داده از /dev/eventX(فراموش کردم کدام eventX بود) متوجه شدم که هر کلید چه کدی دارد:
-
کلید + با کد ۱۱۵
-
کلید - با کد ۱۱۴
-
کلید شات یا دوربین به عنوان دو کلید عمل میکند. یعنی زمانی که آنرا فشار میدهید دو کد میفرستد که به ترتیب ۲۸ و ۱۱۵ هستند.
سپس برنامهای دیگر به زبان سی نوشتم که این کدها را بصورت رشته ذخیره و زمانی که کلید دوربین فشار داده شد، دستور مورد نظر را در صورت درست بودن اجرا کند.
/*
Copyright (c) 2020, Farooq Karimi Zadeh <fkz@riseup.net>
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <string.h>
#include <stdio.h>
int end_of_command(const char *s) {
unsigned len = strlen(s);
if (s[len - 1] == '+' && len - 1 && s[len - 2] == ';') {
return 0;
} else {
return 1;
}
}
int main(int argc, char *argv[]) {
const unsigned commands_len = 3;
char *commands_remote[10] = {
"+;+",
"-;+",
";+"
};
char *commands[10] = {
"Some command",
"Some more",
"su -c \"xdotool key super+l\" farooqkz"
};
struct input_event ev;
ssize_t n;
int fd;
char command[12];
unsigned error_num;
if (argc == 2 && !strcmp("-d", argv[1])) {
chdir("/");
if (fork() > 0) {
exit(EXIT_SUCCESS);
}
}
near_beginning:
error_num = 0;
strcpy(command, "");
while (1) {
fd = open("/dev/input/event18", O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open it... Retrying after 1sec\n");
sleep(1);
} else {
break;
}
}
while (1) {
n = read(fd, &ev, sizeof(ev));
if (n != sizeof(ev)) {
printf("Error...\n");
error_num++;
if (error_num == 8) {
goto near_beginning; // I really wanted to use
} else { // goto just for fun...
continue;
}
}
if (ev.value == 1) { // if event was key pressed...
switch (ev.code) {
case 114:
strcat(command, "-");
break;
case 115:
strcat(command, "+");
break;
case 28:
strcat(command, ";");
break;
}
printf("%s\n", command);
if (!end_of_command(command)) {
for (unsigned i = 0; i < commands_len; i++) {
if (!strcmp(command, commands_remote[i])) {
system(commands[i]);
}
}
strcpy(command, "");
}
if (strlen(command) == 11) {
strcpy(command, "");
}
}
}
}