Files
UsbMakroBoard/main.c
2024-05-06 20:35:43 +02:00

138 lines
4.2 KiB
C

// https://stackoverflow.com/questions/20943322/accessing-keys-from-linux-input-device
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <string.h>
#include <stdio.h>
static const char *const evval[3] = {
"RELEASED",
"PRESSED ",
"REPEATED"
};
/**
* +--+--+--+--+
* |30|48|46|32|
* +--+--+--+--+
* |18|33|34|35|
* +--+--+--+--+
* |23|36|37|38|
* +--+--+--+--+
* |50|49|24|25|
* +--+--+--+--+
* |16|19|31|20|
* +--+--+--+--+
* */
enum p_key {
p11 = 30,
p12 = 48,
p13 = 46,
p14 = 32,
p21 = 18,
p22 = 33,
p23 = 34,
p24 = 35,
p31 = 23,
p32 = 36,
p33 = 37,
p34 = 38,
p41 = 50,
p42 = 49,
p43 = 24,
p44 = 25,
p51 = 16,
p52 = 19,
p53 = 31,
p54 = 20
};
int main(void)
{
const char* dev = "/dev/input/by-id/usb-MAX_Falcon_20_RGB-if02-event-kbd"; //at.event26
struct input_event ev;
ssize_t n;
int fd;
fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
return EXIT_FAILURE;
}
// This grabs the device, so the we do not just listen to events but we actually consume them! This way the keypresses are not handled by any other handler!
// https://stackoverflow.com/questions/29942421/read-barcodes-from-input-event-linux-c/29956584#29956584
if (ioctl(fd, EVIOCGRAB, 1)) {
const int saved_errno = errno;
close(fd);
return errno = (saved_errno) ? errno : EACCES;
}
// Main-Loop
while (1) {
n = read(fd, &ev, sizeof ev);
//n = ioctl(fd, EVIOCGRAB, 1);
if (n == (ssize_t)-1) {
if (errno == EINTR)
continue;
else
break;
} else
if (n != sizeof ev) {
errno = EIO;
break;
}
if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2) {
//printf("%s 0x%04x (%d)\n", evval[ev.value], (int)ev.code, (int)ev.code);
if (evval[ev.value] == "RELEASED") {
switch((int)ev.code) {
case p11:
system("notify-send 'Screenlayout: default'; /home/tbehrendt/.screenlayout/default.sh");
break;
case p12:
system("notify-send 'Screenlayout: main low res'; /home/tbehrendt/.screenlayout/main_low_res.sh");
break;
case p13:
system("notify-send 'Screenlayout: tv mirror'; /home/tbehrendt/.screenlayout/tv_mirror.sh");
break;
case p14:
system("notify-send 'Screenlayout: tv standalone'; /home/tbehrendt/.screenlayout/tv_standalone.sh");
break;
case p21: printf("p21\n"); break;
case p22: printf("p22\n"); break;
case p23: printf("p23\n"); break;
case p24: printf("p24\n"); break;
case p31: printf("p31\n"); break;
case p32: printf("p32\n"); break;
case p33: printf("p33\n"); break;
case p34: printf("p34\n"); break;
case p41: printf("p41\n"); break;
case p42: printf("p42\n"); break;
case p43: printf("p43\n"); break;
case p44: printf("p44\n"); break;
case p51:
system("notify-send mute toggled;pactl set-sink-mute @DEFAULT_SINK@ toggle");
break;
case p52:
system("notify-send 'VOL +5%'; pactl set-sink-volume @DEFAULT_SINK@ +5%");
break;
case p53:
system("notify-send 'VOL -5%'; pactl set-sink-volume @DEFAULT_SINK@ -5%");
break;
case p54:
system("/home/tbehrendt/.scripts/makroboard/p51.sh &");
break;
}
}
}
}
fflush(stdout);
fprintf(stderr, "%s.\n", strerror(errno));
return EXIT_FAILURE;
}