-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
222 lines (199 loc) · 6.39 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <errno.h> // for errno
#include <fcntl.h> // for open
#include <linux/input.h> // for input_event
#include <stdbool.h> // for bool
#include <stdio.h> // for fprint
#include <stdlib.h> // for EXIT_FAILURE & EXIT_SUCCESS
#include <string.h> // for strerror
#include <termios.h> // for termios
#include <time.h> // for nanosleep
#include <unistd.h> // for read & STDIN_FILENO
// The state machine for the TV remote.
#include "state_machine/TvRemoteSm.h"
// Key codes for B1 & B2.
#define B1_CODE 17 // w
#define B2_CODE 31 // b
// Key event types.
#define RELEASED_EVENT 0
#define PRESSED_EVENT 1
#define REPEATED_EVENT 2
#define SECONDS_TO_MS 1000
#define MS_TO_MICROSEC 1000
#define SECONDS_TO_NANOSEC 1000000
// https://stackoverflow.com/questions/1157209/is-there-an-alternative-sleep-function-in-c-to-milliseconds
/* msleep(): Sleep for the requested number of milliseconds. */
int msleep(const unsigned long msec)
{
struct timespec ts;
int res;
// Get the non-fractional seconds.
ts.tv_sec = msec / SECONDS_TO_MS;
// Get the fractional value at nanosecond precision.
ts.tv_nsec = (msec % SECONDS_TO_MS) * SECONDS_TO_NANOSEC;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
// Function to get the time in ms.
long long timeInMilliseconds(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (((long long)tv.tv_sec)*SECONDS_TO_MS)+(tv.tv_usec/MS_TO_MICROSEC);
}
// logic comes from https://github.com/MichaelDipperstein/keypress/blob/master/keypress.c
void console_echo(const bool echo_off)
{
unsigned char echo_bit;
if(echo_off)
{
echo_bit = ECHO; // Set to disable echo.
}
else {
echo_bit = 0; // Clear to enable echo.
}
struct termios state;
tcgetattr(STDIN_FILENO, &state);
state.c_lflag &= ~(echo_bit | ICANON);;
tcsetattr(STDIN_FILENO, TCSANOW, &state);
}
const unsigned int LONG_PRESS_TIMEOUT = 800; // ms.
typedef struct KeyState {
unsigned long long press_start_time;
bool pressed;
bool long_press;
int press_event;
int long_press_event;
} KeyState;
// Handle the state transitions between key press & long-press.
void handle_button_press(const int value, KeyState* key, TvRemoteSm* tv_remote)
{
switch (value)
{
case RELEASED_EVENT:
{
// Clear the flags.
key->pressed = false;
key->long_press = false;
key->press_start_time = 0;
break;
}
case PRESSED_EVENT:
{
// Check if the key has already been pressed.
// If not, then register the press.
// This will be cleared on the key release event.
if (!key->pressed)
{
key->pressed = true;
key->press_start_time = timeInMilliseconds();
TvRemoteSm_dispatch_event(tv_remote, key->press_event);
}
break;
}
case REPEATED_EVENT:
{
// Check if the key has already been pressed and a long-press event hasn't been recorded.
// If the long-press event hasn't been recorded, then register the long-press.
// This is necessary because this event will continue to be triggered while the key is depressed.
if (key->pressed && !key->long_press)
{
// Check if it has been long enough to be considered a "long-press";
if ((timeInMilliseconds() - key->press_start_time) > LONG_PRESS_TIMEOUT)
{
key->long_press = true;
TvRemoteSm_dispatch_event(tv_remote, key->long_press_event);
}
}
break;
}
default:
break;
}
return;
}
int main(int argc, char ** argv)
{
// Don't echo key inputs.
const bool ECHO_OFF = true;
console_echo(ECHO_OFF);
// Configure the State Machine for the TV remote.
TvRemoteSm TvRemote;
TvRemoteSm_ctor(&TvRemote);
TvRemoteSm_start(&TvRemote);
// Store the state of the buttons.
KeyState b1 = {
.pressed = false,
.press_start_time = 0,
.long_press = false,
.press_event = TvRemoteSm_EventId_B1_PRESS,
.long_press_event = TvRemoteSm_EventId_B1_LONG_PRESS
};
KeyState b2 = {
.pressed = false,
.press_start_time = 0,
.long_press = false,
.press_event = TvRemoteSm_EventId_B2_PRESS,
.long_press_event = TvRemoteSm_EventId_B2_LONG_PRESS
};
// Open the keyboard input device.
// https://stackoverflow.com/questions/20943322/accessing-keys-from-linux-input-device/20946151#20946151
const char *dev = "/dev/input/by-path/platform-i8042-serio-0-event-kbd";
struct input_event event;
ssize_t n;
const int fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
return EXIT_FAILURE;
}
printf("Starting loop.\n");
while(true)
{
// Read an event from the keyboard.
n = read(fd, &event, sizeof event);
if (n == (ssize_t)-1) {
if (errno == EINTR) {
// Continue processing in the case of an interrupted system call.
continue;
} else {
// Error.
break;
}
} else if (n != sizeof event) {
// Failed to read enough data to constitute an event.
errno = EIO;
break;
}
// Check if this is a key event with an event that we care about:
// - RELEASED_EVENT: 0
// - PRESSED_EVENT: 1
// - REPEATED_EVENT: 2
if (event.type == EV_KEY && event.value >= 0 && event.value <= 2)
{
switch (event.code)
{
case B1_CODE:
{
handle_button_press(event.value, &b1, &TvRemote);
break;
}
case B2_CODE:
{
handle_button_press(event.value, &b2, &TvRemote);
break;
}
default:
// Ignore other keys.
break;
}
}
}
// Flush any remaining output.
fflush(stdout);
fprintf(stderr, "%s.\n", strerror(errno));
// Reset the console.
const bool ECHO_ON = false;
console_echo(ECHO_ON);
// Exit the application.
return EXIT_SUCCESS;
}