-
Notifications
You must be signed in to change notification settings - Fork 271
/
keyboard.cpp
78 lines (70 loc) · 2.03 KB
/
keyboard.cpp
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
#include <linux/input.h> // input_event
#include <fcntl.h> // O_RDONLY, O_NONBLOCK
#include <stdio.h> // printf
#include <stdint.h> // uint64_t
#include "config.h"
#include "keyboard.h"
#include "util.h"
#include "tick.h"
#if defined(BACKLIGHT_CONTROL_FROM_KEYBOARD) && defined(TURN_DISPLAY_OFF_AFTER_USECS_OF_INACTIVITY)
#define READ_KEYBOARD_ENABLED
#endif
int key_fd = -1;
void OpenKeyboard()
{
#ifdef READ_KEYBOARD_ENABLED
key_fd = open(KEYBOARD_INPUT_FILE, O_RDONLY|O_NONBLOCK);
if (key_fd < 0) printf("Warning: cannot open keyboard input file " KEYBOARD_INPUT_FILE "! Try double checking that it exists, or reconfigure it in keyboard.cpp, or remove line '#define BACKLIGHT_CONTROL_FROM_KEYBOARD' in config.h if you do not want keyboard activity to factor into backlight control.\n");
#endif
}
int ReadKeyboard()
{
#ifdef READ_KEYBOARD_ENABLED
if (key_fd < 0) return 0;
struct input_event ev;
ssize_t bytesRead = -1;
int numRead = 0;
do
{
bytesRead = read(key_fd, &ev, sizeof(struct input_event));
if (bytesRead >= sizeof(struct input_event))
{
if (ev.type == 1 && ev.code != 0) // key up or down
{
// printf("time: %d %d type: %d, code: %d, value: %d\n", ev.time.tv_sec, ev.time.tv_usec, ev.type, ev.code, ev.value);
++numRead;
}
}
} while(bytesRead > 0);
return numRead;
#else
return 0;
#endif
}
void CloseKeyboard()
{
#ifdef READ_KEYBOARD_ENABLED
if (key_fd >= 0)
{
close(key_fd);
key_fd = -1;
}
#endif
}
static uint64_t lastKeyboardPressTime = 0;
static uint64_t lastKeyboardPressCheckTime = 0;
uint64_t TimeSinceLastKeyboardPress(void)
{
#ifdef READ_KEYBOARD_ENABLED
uint64_t now = tick();
if (now - lastKeyboardPressCheckTime >= 250000) // ReadKeyboard() takes about 8 usecs on Pi 3B, so 250msecs poll interval should be fine
{
lastKeyboardPressCheckTime = now;
if (ReadKeyboard())
lastKeyboardPressTime = now;
}
return now - lastKeyboardPressTime;
#else
return 0;
#endif
}