gui: avoid high CPU usage while waiting for input

- add a timeout to ev_get
- set timeout to 1 second when idle
- delay timeout for 15 frames to keep animation objects working
- stop kinetic scrolling immediately at end of list

Change-Id: I77138055c464b65b71e296f9c7ef63ea06809bc1
This commit is contained in:
that
2015-02-09 16:53:21 +01:00
committed by Dees Troy
parent 3f15be40aa
commit de72b6d4d8
4 changed files with 28 additions and 15 deletions
+20 -11
View File
@@ -205,7 +205,7 @@ public:
}
// process input events. returns true if any event was received.
bool processInput();
bool processInput(int timeout_ms);
void handleDrag();
@@ -249,10 +249,10 @@ private:
InputHandler input_handler;
bool InputHandler::processInput()
bool InputHandler::processInput(int timeout_ms)
{
input_event ev;
int ret = ev_get(&ev);
int ret = ev_get(&ev, timeout_ms);
if (ret < 0)
{
@@ -546,7 +546,7 @@ static void ors_command_read()
// This special function will return immediately the first time, but then
// always returns 1/30th of a second (or immediately if called later) from
// the last time it was called
static void loopTimer(void)
static void loopTimer(int input_timeout_ms)
{
static timespec lastCall;
static int initialized = 0;
@@ -560,7 +560,7 @@ static void loopTimer(void)
do
{
bool got_event = input_handler.processInput(); // get inputs but don't send drag notices
bool got_event = input_handler.processInput(input_timeout_ms); // get inputs but don't send drag notices
timespec curTime;
clock_gettime(CLOCK_MONOTONIC, &curTime);
@@ -583,6 +583,7 @@ static void loopTimer(void)
// We need to sleep some period time microseconds
//unsigned int sleepTime = 33333 -(diff.tv_nsec / 1000);
//usleep(sleepTime); // removed so we can scan for input
input_timeout_ms = 0;
} while (1);
}
@@ -615,9 +616,12 @@ static int runPages(const char *page_name, const int stop_on_page_done)
int has_data = 0;
#endif
int input_timeout_ms = 0;
int idle_frames = 0;
for (;;)
{
loopTimer();
loopTimer(input_timeout_ms);
#ifndef TW_OEM_BUILD
if (ors_read_fd > 0) {
FD_ZERO(&fdset);
@@ -637,9 +641,13 @@ static int runPages(const char *page_name, const int stop_on_page_done)
if (!gForceRender.get_value())
{
int ret;
ret = PageManager::Update();
int ret = PageManager::Update();
if (ret == 0)
++idle_frames;
else
idle_frames = 0;
// due to possible animation objects, we need to delay activating the input timeout
input_timeout_ms = idle_frames > 15 ? 1000 : 0;
#ifndef PRINT_RENDER_TIME
if (ret > 1)
@@ -663,7 +671,7 @@ static int runPages(const char *page_name, const int stop_on_page_done)
LOGINFO("Render(): %u ms, flip(): %u ms, total: %u ms\n", render_t, flip_t, render_t+flip_t);
}
else if(ret == 1)
else if (ret > 0)
flip();
#endif
}
@@ -672,6 +680,7 @@ static int runPages(const char *page_name, const int stop_on_page_done)
gForceRender.set_value(0);
PageManager::Render();
flip();
input_timeout_ms = 0;
}
blankTimer.checkForTimeout();
@@ -908,7 +917,7 @@ static void * console_thread(void *cookie)
while (!gGuiConsoleTerminate.get_value())
{
loopTimer();
loopTimer(0);
if (!gForceRender.get_value())
{
+5 -1
View File
@@ -632,8 +632,10 @@ void GUIScrollList::HandleScrolling()
firstDisplayedItem--;
y_offset -= actualItemHeight;
}
if (firstDisplayedItem == 0 && y_offset > 0)
if (firstDisplayedItem == 0 && y_offset > 0) {
y_offset = 0; // user kept dragging downward past the top of the list, so always reset the offset to 0 since we can't scroll any further in this direction
scrollingSpeed = 0; // stop kinetic scrolling
}
// handle dragging upward, scrolling downward
int totalSize = GetItemCount();
@@ -649,9 +651,11 @@ void GUIScrollList::HandleScrolling()
if (bottom_offset != 0 && firstDisplayedItem + lines + 1 >= totalSize && y_offset <= bottom_offset) {
firstDisplayedItem = totalSize - lines - 1;
y_offset = bottom_offset;
scrollingSpeed = 0; // stop kinetic scrolling
} else if (firstDisplayedItem + lines >= totalSize && y_offset < 0) {
firstDisplayedItem = totalSize - lines;
y_offset = 0;
scrollingSpeed = 0; // stop kinetic scrolling
}
}
+2 -2
View File
@@ -719,7 +719,7 @@ static int vk_modify(struct ev *e, struct input_event *ev)
return 0;
}
int ev_get(struct input_event *ev)
int ev_get(struct input_event *ev, int timeout_ms)
{
int r;
unsigned n;
@@ -740,7 +740,7 @@ int ev_get(struct input_event *ev)
lastInputStat = curr;
}
r = poll(ev_fds, ev_count, 0);
r = poll(ev_fds, ev_count, timeout_ms);
if(r > 0) {
for(n = 0; n < ev_count; n++) {
+1 -1
View File
@@ -75,7 +75,7 @@ struct input_event;
int ev_init(void);
void ev_exit(void);
int ev_get(struct input_event *ev);
int ev_get(struct input_event *ev, int timeout_ms);
int ev_has_mouse(void);
// Resources