|
21 | 21 |
|
22 | 22 | ///////////////////////////////
|
23 | 23 |
|
24 |
| -#define LID_PATH "/sys/class/power_supply/axp2202-battery/hallkey" |
25 |
| -static pthread_t lid_pt; |
26 |
| -static void* PLAT_lidThread(void *arg) { |
27 |
| - static int lid_open = 1; |
28 |
| - while (1) { |
29 |
| - int lid = getInt(LID_PATH); |
30 |
| - if (lid!=lid_open) { |
31 |
| - lid_open = lid; |
32 |
| - if (lid) PWR_requestWake(); |
33 |
| - else PWR_requestSleep(); |
34 |
| - } |
35 |
| - SDL_Delay(500); |
36 |
| - } |
37 |
| -} |
38 |
| - |
39 |
| -/////////////////////////////// |
40 |
| - |
41 | 24 | #define RAW_UP 103
|
42 | 25 | #define RAW_DOWN 108
|
43 | 26 | #define RAW_LEFT 105
|
@@ -71,21 +54,30 @@ static void* PLAT_lidThread(void *arg) {
|
71 | 54 | #define INPUT_COUNT 2
|
72 | 55 | static int inputs[INPUT_COUNT];
|
73 | 56 |
|
| 57 | +#define LID_PATH "/sys/class/power_supply/axp2202-battery/hallkey" |
| 58 | +static int check_lid = 0; |
| 59 | +static int lid_open = 1; |
| 60 | +int PLAT_lidChanged(int* state) { |
| 61 | + if (check_lid) { |
| 62 | + int lid = getInt(LID_PATH); |
| 63 | + if (lid!=lid_open) { |
| 64 | + lid_open = lid; |
| 65 | + if (state) *state = lid; |
| 66 | + return 1; |
| 67 | + } |
| 68 | + } |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
74 | 72 | void PLAT_initInput(void) {
|
75 | 73 | inputs[0] = open("/dev/input/event0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
76 | 74 | inputs[1] = open("/dev/input/event1", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
77 |
| - |
78 |
| - if (exists(LID_PATH)) pthread_create(&lid_pt, NULL, &PLAT_lidThread, NULL); |
| 75 | + check_lid = exists(LID_PATH); |
79 | 76 | }
|
80 | 77 | void PLAT_quitInput(void) {
|
81 | 78 | for (int i=0; i<INPUT_COUNT; i++) {
|
82 | 79 | close(inputs[i]);
|
83 | 80 | }
|
84 |
| - |
85 |
| - if (exists(LID_PATH)) { |
86 |
| - pthread_cancel(lid_pt); |
87 |
| - pthread_join(lid_pt,NULL); |
88 |
| - } |
89 | 81 | }
|
90 | 82 |
|
91 | 83 | // from <linux/input.h> which has BTN_ constants that conflict with platform.h
|
@@ -134,7 +126,7 @@ void PLAT_pollInput(void) {
|
134 | 126 |
|
135 | 127 | pressed = value;
|
136 | 128 | // LOG_info("key event: %i (%i)\n", code,pressed);
|
137 |
| - if (code==RAW_UP) { btn = BTN_UP; id = BTN_ID_UP; } |
| 129 | + if (code==RAW_UP) { btn = BTN_UP; id = BTN_ID_UP; } |
138 | 130 | else if (code==RAW_DOWN) { btn = BTN_DOWN; id = BTN_ID_DOWN; }
|
139 | 131 | else if (code==RAW_LEFT) { btn = BTN_LEFT; id = BTN_ID_LEFT; }
|
140 | 132 | else if (code==RAW_RIGHT) { btn = BTN_RIGHT; id = BTN_ID_RIGHT; }
|
@@ -212,15 +204,24 @@ void PLAT_pollInput(void) {
|
212 | 204 | }
|
213 | 205 | }
|
214 | 206 | }
|
| 207 | + |
| 208 | + if (check_lid && PLAT_lidChanged(NULL)) pad.just_released |= BTN_SLEEP; |
215 | 209 | }
|
216 | 210 |
|
217 | 211 | int PLAT_shouldWake(void) {
|
| 212 | + int lid = 1; // assume open by default |
| 213 | + if (check_lid && PLAT_lidChanged(&lid) && lid) return 1; |
| 214 | + |
218 | 215 | int input;
|
219 | 216 | static struct input_event event;
|
220 | 217 | for (int i=0; i<INPUT_COUNT; i++) {
|
221 | 218 | input = inputs[i];
|
222 | 219 | while (read(input, &event, sizeof(event))==sizeof(event)) {
|
223 |
| - if (event.type==EV_KEY && event.code==RAW_POWER && event.value==0) return 1; |
| 220 | + if (event.type==EV_KEY && event.code==RAW_POWER && event.value==0) { |
| 221 | + // ignore input while lid is closed |
| 222 | + if (check_lid && !lid_open) return 0; // do it here so we eat the input |
| 223 | + return 1; |
| 224 | + } |
224 | 225 | }
|
225 | 226 | }
|
226 | 227 | return 0;
|
|
0 commit comments