Created
August 16, 2023 11:06
-
-
Save schneidersoft/2405b5cfa54ae364c9d87aacfb63aebb to your computer and use it in GitHub Desktop.
set LEDS on your keyboard
This file contains hidden or 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
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <linux/input.h> | |
#include <sys/ioctl.h> | |
struct leds { | |
const char *name; | |
int value; | |
} ks[] = { | |
{ .name="LED_NUML", .value=LED_NUML }, | |
{ .name="LED_CAPSL", .value=LED_CAPSL }, | |
{ .name="LED_SCROLLL", .value=LED_SCROLLL }, | |
{ .name="LED_COMPOSE", .value=LED_COMPOSE }, | |
{ .name="LED_KANA", .value=LED_KANA }, | |
{ .name="LED_SLEEP", .value=LED_SLEEP }, | |
{ .name="LED_SUSPEND", .value=LED_SUSPEND }, | |
{ .name="LED_MUTE", .value=LED_MUTE }, | |
{ .name="LED_MISC", .value=LED_MISC }, | |
{ .name="LED_MAIL", .value=LED_MAIL }, | |
{ .name="LED_CHARGING", .value=LED_CHARGING }, | |
{NULL, 0} | |
}; | |
int get_led_value(const char *val) { | |
if (strcmp(val, "true") == 0) { | |
return 1; | |
} else if (strcmp(val, "false") == 0) { | |
return 0; | |
} | |
return -1; | |
} | |
int get_led_code(const char *led) { | |
for (struct leds *k = ks; k->name; k+=1) { | |
if (strcmp(k->name, led) == 0) { | |
return k->value; | |
} | |
} | |
return -1; | |
} | |
void usage(char **argv) { | |
printf("USAGE: %s DEVICE LED true\n", argv[0]); | |
printf("USAGE: %s DEVICE LED false\n", argv[0]); | |
printf("supported LEDs are:\n"); | |
for (struct leds *k = ks; k->name; k+=1) { | |
printf("0x%02X %s\n", k->value, k->name); | |
} | |
} | |
int main(int argc, char **argv) { | |
if (argc != 4) { | |
usage(argv); | |
return -1; | |
} | |
const char *dev = argv[1]; | |
const int val = get_led_value(argv[3]); | |
const int lednum = get_led_code(argv[2]); | |
if (val == -1) { | |
usage(argv); | |
return -1; | |
} | |
if (lednum == -1) { | |
usage(argv); | |
return -1; | |
} | |
int kb = open(dev, O_RDWR ); | |
if (kb < 0) { | |
printf("unable to open device file"); | |
return -1; | |
} | |
//char kbname [256] = "key-x123"; | |
char LedStatus = 0; | |
// ioctl (kb, EVIOCGBIT (EV_LED,sizeof (LedStatus)), &LedStatus); | |
// printf("EVENT BITS: 0x%08X\n", LedStatus); | |
ioctl (kb, EVIOCGLED (sizeof (LedStatus)), &LedStatus); | |
printf("LED STATUS: 0x%08X\n", LedStatus); | |
struct input_event event; | |
event.type = EV_LED; | |
event.code = lednum; | |
event.value = val; | |
for (int x = 0; x < sizeof(event); x++) { | |
printf("\\x%02x", ((unsigned char *)&event)[x]); | |
} | |
printf("\n"); | |
if (write(kb, &event, sizeof (struct input_event)) < 0) { | |
printf("some error\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment