/
main.c
166 lines (125 loc) · 3.72 KB
/
main.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* evlag
* Copyright 2018 Filip Aláč
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "arg_parse.h"
#include "fifo.h"
#include "threads.h"
#include <fcntl.h>
#include <linux/rtc.h>
#include <pthread.h>
#include <string.h>
int main(int argc, char **argv)
{
/* Set default values and parse arguments. */
struct arguments args;
args.event_file_name = NULL;
args.delay.tv_sec = 0;
args.delay.tv_usec = 0;
args.buf_size = 0;
args.resize_factor = 2;
args.priority = 20;
args.polling_rate = 2048;
if (parse_args(argc, argv, &args) < 0) {
perror("Failed to parse arguments");
exit(EXIT_FAILURE);
}
/* Set scheduler priority. */
struct sched_param sched;
sched.sched_priority = args.priority;
if (sched_setscheduler(0, SCHED_FIFO, &sched) < 0) {
perror("Failed to set scheduler, check your privilege");
}
/* Open device. */
int fd_event = open(args.event_file_name, O_RDONLY);
if (fd_event < 0) {
perror("Failed to open input device");
exit(EXIT_FAILURE);
}
/* Create libevdev device and grab it. */
struct libevdev *event_dev = NULL;
if (libevdev_new_from_fd(fd_event, &event_dev) < 0) {
perror("Failed to init libevdev");
exit(EXIT_FAILURE);
}
if (libevdev_grab(event_dev, LIBEVDEV_GRAB) < 0) {
perror("Failed to grab device");
exit(EXIT_FAILURE);
}
/* Create uinput clone of device. */
int fd_uinput = open("/dev/uinput", O_WRONLY);
if (fd_uinput < 0) {
perror("Failed to open uinput device");
exit(EXIT_FAILURE);
}
struct libevdev_uinput *uinput_dev = NULL;
if (libevdev_uinput_create_from_device(event_dev,
fd_uinput, &uinput_dev) < 0) {
perror("Failed to create uinput device");
exit(EXIT_FAILURE);
}
/* Create RTC interrupts. */
int fd_rtc = open("/dev/rtc", O_RDONLY);
if (fd_rtc < 0) {
perror("Failed to open RTC timer");
exit(EXIT_FAILURE);
}
if (ioctl(fd_rtc, RTC_IRQP_SET, args.polling_rate) < 0) {
perror("Failed to set RTC interrupts");
exit(EXIT_FAILURE);
}
if (ioctl(fd_rtc, RTC_PIE_ON, 0) < 0) {
perror("Failed to enable RTC interrupts");
exit(EXIT_FAILURE);
}
/* Create fifo buffer. */
struct fifo_header fifo;
if (fifo_create(&fifo, args.buf_size) == NULL) {
perror("Failed to allocate fifo buffer");
exit(EXIT_FAILURE);
}
/* Set data for threads. */
struct thread_data data;
data.fifo = &fifo;
data.args = &args;
data.event_dev = event_dev;
data.uinput_dev = uinput_dev;
data.fd_rtc = fd_rtc;
/* Create threads. */
pthread_t thr[2];
int rc = pthread_create(&thr[0], NULL, get_event, &data);
if (rc != 0) {
printf("Failed to create thread0: (%d) %s\n", rc, strerror(rc));
exit(EXIT_FAILURE);
}
rc = pthread_create(&thr[1], NULL, write_event, &data);
if (rc != 0) {
printf("Failed to create thread1: (%d) %s\n", rc, strerror(rc));
exit(EXIT_FAILURE);
}
/* Join threads. */
rc = pthread_join(thr[0], NULL);
if (rc != 0) {
printf("Failed join thread0: (%d) %s\n", rc, strerror(rc));
exit(EXIT_FAILURE);
}
rc = pthread_join(thr[1], NULL);
if (rc != 0) {
printf("Failed join thread1: (%d) %s\n", rc, strerror(rc));
exit(EXIT_FAILURE);
}
}