-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.c
85 lines (80 loc) · 1.92 KB
/
keyboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define KEYBOARDNAME "AT Translated Set 2 keyboard"
void inputcheck(int argc, char* argv[]) {
if (argc != 2) {
printf("Error: Usage -> keyboard [option]\n");
exit(1);
}
if ((strcmp(argv[1], "on") == 0) || (strcmp(argv[1], "ON") == 0)){
strcpy(argv[1], "1");
}
if ((strcmp(argv[1], "off") == 0) || (strcmp(argv[1], "OFF") == 0)){
strcpy(argv[1], "0");
}
if ((strcmp(argv[1], "-help") == 0) || (strcmp(argv[1], "help") == 0)) {
printf("Usage -> keyboard [option]\n");
printf("Options: 0, 1, on, off\n");
system("xinput -list");
printf("Searched for this keyboard name: %s\n",KEYBOARDNAME);
printf("Change this by using -change\n");
exit(0);
}
if (strcmp(argv[1], "-change") == 0) {
char data[2000];
strcpy(data, "./userdevchange ");
strcat(data, KEYBOARDNAME);
system(data);
exit(0);
}
}
void commandexecute(char data[2000], char keyboard_id[4], char* argv[]) {
strcpy(data, "xinput set-int-prop ");
strcat(data, keyboard_id);
strcat(data, " \"Device Enabled\" ");
strcat(data, "8 ");
strcat(data, argv[1]);
system(data);
if (strcmp(argv[1], "0") == 0) {
printf("Keyboard: \e[1;31m[OFF]\e[0m\n");
}
else {
printf("Keyboard: \e[1;32m[ON]\e[0m\n");
}
}
int main(int argc, char* argv[]) {
inputcheck(argc, argv);
char data[2000];
char keyboard_id[4];
FILE *fp;
int i;
int exists;
fp = popen("xinput -list", "r");
while (fgets(data, 2000, fp) != NULL) {
if (strstr(data, KEYBOARDNAME)) {
exists = 1;
for (i = 0; i < strlen(data); i++) {
if (data[i] == '=') {
i++;
int j = 0;
while ((data[i] > 47) && (data[i] < 58)) {
keyboard_id[j] = data[i];
i++;
j++;
}
keyboard_id[j] = '\0';
}
}
}
}
if (exists != 1) {
printf("Keyboard not found!\n");
system("./keyboard -help");
exit(1);
}
pclose(fp);
commandexecute(data, keyboard_id, argv);
return 0;
}