| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include <errno.h>
- #include <fcntl.h>
- #include <glib-unix.h>
- #include <linux/input.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include "platform_linux.h"
- static const struct {
- unsigned short code;
- enum radio_key key;
- } keymap[] = {
- { KEY_ESC, RK_ESC }, { KEY_NEXTSONG, RK_NEXT }, { KEY_STOPCD, RK_STOP },
- { KEY_MENU, RK_MENU }, { KEY_LIST, RK_LIST }, { KEY_UP, RK_UP },
- { KEY_PREVIOUSSONG, RK_PREV }, { KEY_RIGHT, RK_RIGHT },
- { KEY_DOWN, RK_DOWN }, { KEY_PAUSECD, RK_PAUSE }, { KEY_OK, RK_OK },
- { KEY_LEFT, RK_LEFT }, { KEY_PLAYCD, RK_PLAY },
- };
- static gboolean on_input(gint fd, GIOCondition cond, gpointer data)
- {
- struct app *app = data;
- struct input_event ev;
- unsigned int i;
- (void)cond;
- while (read(fd, &ev, sizeof(ev)) == (ssize_t)sizeof(ev)) {
- /* 1 press, 2 autorepeat: app_key() applies the lockout */
- if (ev.type != EV_KEY || ev.value == 0)
- continue;
- for (i = 0; i < G_N_ELEMENTS(keymap); i++)
- if (keymap[i].code == ev.code)
- app_key(app, keymap[i].key);
- }
- return G_SOURCE_CONTINUE;
- }
- static int find_keys_device(void)
- {
- char path[32], name[64];
- int i, fd;
- for (i = 0; i < 32; i++) {
- snprintf(path, sizeof(path), "/dev/input/event%d", i);
- fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
- if (fd < 0)
- continue;
- if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) > 0 &&
- !strcmp(name, DEFAULT_KEYS_NAME))
- return fd;
- close(fd);
- }
- return -1;
- }
- bool platform_keys_start(struct app *app, const char *path)
- {
- int fd = path ? open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC) : find_keys_device();
- if (fd < 0)
- return false;
- g_unix_fd_add(fd, G_IO_IN, on_input, app);
- return true;
- }
- void platform_backlight_set(const char *path, bool on)
- {
- FILE *f;
- if (!path)
- return;
- f = fopen(path, "w");
- if (!f)
- return;
- fputs(on ? "1\n" : "0\n", f);
- fclose(f);
- }
- static gboolean on_signal(gpointer data)
- {
- g_main_loop_quit(data);
- return G_SOURCE_REMOVE;
- }
- void platform_run(void)
- {
- GMainLoop *loop = g_main_loop_new(NULL, FALSE);
- g_unix_signal_add(SIGINT, on_signal, loop);
- g_unix_signal_add(SIGTERM, on_signal, loop);
- g_main_loop_run(loop);
- g_main_loop_unref(loop);
- }
|