Skip to content

Commit 9ea6e49

Browse files
author
Shaun Inman
committed
rg35xxsp: more robust lid sleep handling
1 parent ee50f87 commit 9ea6e49

File tree

3 files changed

+27
-35
lines changed

3 files changed

+27
-35
lines changed

workspace/all/common/api.c

-7
Original file line numberDiff line numberDiff line change
@@ -1440,13 +1440,6 @@ int PWR_ignoreSettingInput(int btn, int show_setting) {
14401440
return show_setting && (btn==BTN_MOD_PLUS || btn==BTN_MOD_MINUS);
14411441
}
14421442

1443-
void PWR_requestSleep(void) {
1444-
pwr.requested_sleep = 1;
1445-
}
1446-
void PWR_requestWake(void) {
1447-
pwr.requested_wake = 1;
1448-
}
1449-
14501443
void PWR_update(int* _dirty, int* _show_setting, PWR_callback_t before_sleep, PWR_callback_t after_sleep) {
14511444
int dirty = _dirty ? *_dirty : 0;
14521445
int show_setting = _show_setting ? *_show_setting : 0;

workspace/all/common/api.h

-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ void PWR_powerOff(void);
257257
int PWR_isPoweringOff(void);
258258

259259
void PWR_fauxSleep(void);
260-
void PWR_requestSleep(void);
261-
void PWR_requestWake(void);
262260

263261
void PWR_disableSleep(void);
264262
void PWR_enableSleep(void);

workspace/rg35xxplus/platform/platform.c

+27-26
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,6 @@
2121

2222
///////////////////////////////
2323

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-
4124
#define RAW_UP 103
4225
#define RAW_DOWN 108
4326
#define RAW_LEFT 105
@@ -71,21 +54,30 @@ static void* PLAT_lidThread(void *arg) {
7154
#define INPUT_COUNT 2
7255
static int inputs[INPUT_COUNT];
7356

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+
7472
void PLAT_initInput(void) {
7573
inputs[0] = open("/dev/input/event0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
7674
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);
7976
}
8077
void PLAT_quitInput(void) {
8178
for (int i=0; i<INPUT_COUNT; i++) {
8279
close(inputs[i]);
8380
}
84-
85-
if (exists(LID_PATH)) {
86-
pthread_cancel(lid_pt);
87-
pthread_join(lid_pt,NULL);
88-
}
8981
}
9082

9183
// from <linux/input.h> which has BTN_ constants that conflict with platform.h
@@ -134,7 +126,7 @@ void PLAT_pollInput(void) {
134126

135127
pressed = value;
136128
// 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; }
138130
else if (code==RAW_DOWN) { btn = BTN_DOWN; id = BTN_ID_DOWN; }
139131
else if (code==RAW_LEFT) { btn = BTN_LEFT; id = BTN_ID_LEFT; }
140132
else if (code==RAW_RIGHT) { btn = BTN_RIGHT; id = BTN_ID_RIGHT; }
@@ -212,15 +204,24 @@ void PLAT_pollInput(void) {
212204
}
213205
}
214206
}
207+
208+
if (check_lid && PLAT_lidChanged(NULL)) pad.just_released |= BTN_SLEEP;
215209
}
216210

217211
int PLAT_shouldWake(void) {
212+
int lid = 1; // assume open by default
213+
if (check_lid && PLAT_lidChanged(&lid) && lid) return 1;
214+
218215
int input;
219216
static struct input_event event;
220217
for (int i=0; i<INPUT_COUNT; i++) {
221218
input = inputs[i];
222219
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+
}
224225
}
225226
}
226227
return 0;

0 commit comments

Comments
 (0)