Create separate input module
This commit is contained in:
45
main.c
45
main.c
@@ -1,46 +1,47 @@
|
||||
#include "input.h"
|
||||
#include "renderer.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <linux/input-event-codes.h>
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
||||
|
||||
static bool quit = false;
|
||||
|
||||
static void key_press_callback(int key)
|
||||
{
|
||||
switch (key) {
|
||||
case KEY_Q:
|
||||
quit = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int err;
|
||||
|
||||
const int input_fd = open("/dev/input/event0", O_RDONLY);
|
||||
assert(input_fd != -1);
|
||||
const int input_fd = input_init();
|
||||
input_on_press(key_press_callback);
|
||||
|
||||
const renderer_params_t renderer_params = renderer_init();
|
||||
const int drm_fd = renderer_params.drm_fd;
|
||||
|
||||
renderer_clear();
|
||||
renderer_swap();
|
||||
|
||||
const int max_fd = MAX(input_fd, drm_fd);
|
||||
fd_set set;
|
||||
bool running = true;
|
||||
while (running) {
|
||||
|
||||
while (!quit) {
|
||||
FD_ZERO(&set);
|
||||
FD_SET(input_fd, &set);
|
||||
FD_SET(drm_fd, &set);
|
||||
|
||||
select(max_fd + 1, &set, nullptr, nullptr, nullptr);
|
||||
|
||||
if (FD_ISSET(input_fd, &set)) {
|
||||
struct input_event ev;
|
||||
const int len = read(input_fd, &ev, sizeof(ev));
|
||||
assert(len == sizeof(ev));
|
||||
|
||||
if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2) {
|
||||
if (ev.code == KEY_Q && ev.value == 1)
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
if (FD_ISSET(input_fd, &set))
|
||||
input_handle();
|
||||
|
||||
if (FD_ISSET(drm_fd, &set)) {
|
||||
renderer_handle();
|
||||
@@ -50,7 +51,7 @@ int main()
|
||||
}
|
||||
|
||||
renderer_cleanup();
|
||||
close(input_fd);
|
||||
input_cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user