-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.c
More file actions
40 lines (32 loc) · 845 Bytes
/
controller.c
File metadata and controls
40 lines (32 loc) · 845 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include "controller.h"
static int fd = -1;
void stop_controller_server(pthread_t thread_id)
{
pthread_cancel(thread_id);
if (fd != -1 && close(fd) < 0)
fprintf(stderr, "%s", "Close failed.\n");
}
static void *start_controller_server(void *args)
{
struct js_event event;
void (*function_pointer)() = (void*)args;
fd = open(DEVICE_PATH, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "%s", "Can't open the input device.\n");
return NULL;
}
while (read(fd, &event, sizeof(struct js_event)))
function_pointer(event);
}
pthread_t new_controller_server(void *callback_function)
{
pthread_t thread_id;
int retval;
retval = pthread_create(&thread_id, NULL, start_controller_server, (void*)callback_function);
return thread_id;
}