Skip to content

Commit 145e18a

Browse files
mk-fFrenzie
authored andcommitted
Implement event-injection for PocketBook to control auto-suspension (koreader#576)
* implement event-injection into InkviewMain
1 parent b462060 commit 145e18a

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

input/input-pocketbook.h

+80-2
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,88 @@ static inline void debug_mtinfo(iv_mtinfo *mti) {
7979
printf("\n");
8080
}
8181

82+
/*
83+
* The PocketBook has an auto-suspend-feature, which puts the reader to sleep
84+
* after approximately two seconds "inactivity in the current eventhandler".
85+
*
86+
* The handler (pb_event_handle) runs via InkViewMain in a forked-off process
87+
* and just relays incoming events like keypress / touch via a pipe to the
88+
* main-koreader-process when they occur. In consequence, the forked process
89+
* quickly becomes idle w/o external events, leading to suspension of the
90+
* whole device.
91+
*
92+
* This breaks the initial loading of modules and books.
93+
*
94+
* There are multiple functions which can affect auto-suspension: Beside
95+
* iv_sleepmode() which controls if auto-suspension is enabled at all, the
96+
* function SetHardTimer() makes it possible to execute a callback after a
97+
* given amount of time. SetHardTimer() will wake the Reader if it is
98+
* suspended and suspension will not occur while the callback is executed.
99+
*
100+
* However, both functions will not work properly if not called _from within
101+
* the current eventhandler_.
102+
*
103+
* SendEventTo() can be used to send an event to the current eventhandler of a
104+
* specific (system) task. GetCurrentTask() returns the caller's currently
105+
* active task.
106+
*/
107+
108+
/*
109+
* define a fake-event which can be send via SendEventTo into
110+
* pb_event_handle()
111+
*/
112+
#define PB_SPECIAL_SUSPEND 333
113+
114+
/* callback to disable suspension */
115+
void disable_suspend(void) {
116+
iv_sleepmode(0);
117+
}
118+
119+
/* callback to enable suspension */
120+
void enable_suspend(void) {
121+
iv_sleepmode(1);
122+
}
123+
124+
static int external_suspend_control = 0;
125+
126+
void fallback_enable_suspend(void) {
127+
if (external_suspend_control == 0)
128+
enable_suspend();
129+
}
130+
131+
static int send_to_event_handler(int type, int par1, int par2) {
132+
SendEventTo(GetCurrentTask(), type, par1, par2);
133+
}
134+
135+
136+
static int setSuspendState(lua_State *L) {
137+
send_to_event_handler(
138+
PB_SPECIAL_SUSPEND,
139+
luaL_checkint(L, 1),
140+
luaL_checkint(L,2)
141+
);
142+
}
143+
82144
int touch_pointers = 0;
83145
static int pb_event_handler(int type, int par1, int par2) {
84-
//printf("ev:%d %d %d\n", type, par1, par2);
85-
//fflush(stdout);
146+
// printf("ev:%d %d %d\n", type, par1, par2);
147+
// fflush(stdout);
86148
int i;
87149
iv_mtinfo *mti;
150+
88151
// general settings in only possible in forked process
89152
if (type == EVT_INIT) {
90153
SetPanelType(PANEL_DISABLED);
91154
get_gti_pointer();
155+
/* disable suspend to make uninterrupted loading possible. */
156+
disable_suspend();
157+
/*
158+
* re-enable suspending after a minute. This is normally handled by a
159+
* plugin on onReaderReady(). However, if loading of the plugin fails
160+
* for some reason, suspension would stay inactive consuming a lot of
161+
* power
162+
*/
163+
SetHardTimer("fallback_enable_suspend", fallback_enable_suspend, 1000 * 60);
92164
}
93165

94166
if (type == EVT_POINTERDOWN) {
@@ -132,6 +204,12 @@ static int pb_event_handler(int type, int par1, int par2) {
132204
genEmuEvent(inputfds[0], EV_ABS, ABS_MT_TRACKING_ID, -1);
133205
}
134206
touch_pointers = 0;
207+
} else if (type == PB_SPECIAL_SUSPEND) {
208+
external_suspend_control = 1;
209+
if (par1 == 0)
210+
SetHardTimer("disable_suspend", disable_suspend, par2);
211+
else
212+
SetHardTimer("enable_suspend", enable_suspend, par2);
135213
} else {
136214
genEmuEvent(inputfds[0], type, par1, par2);
137215
}

input/input.c

+3
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ static const struct luaL_Reg input_func[] = {
261261
{"closeAll", closeInputDevices},
262262
{"waitForEvent", waitForInput},
263263
{"fakeTapInput", fakeTapInput},
264+
#ifdef POCKETBOOK
265+
{"setSuspendState", setSuspendState},
266+
#endif
264267
{NULL, NULL}
265268
};
266269

0 commit comments

Comments
 (0)