summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-26 07:24:45 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-26 07:24:45 +0300
commit9de147d199dc1f4f9fb715de5c544b164f4ae5ea (patch)
tree19f9d13c9220f5fb430a1155f67cc9fc1c9ca0ae /src
parenta78d7cb111096964d466fe045ada7f67e347c1eb (diff)
Cleanup: Running tickers
Diffstat (limited to 'src')
-rw-r--r--src/app.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/app.c b/src/app.c
index 1b8147d0..fe450c51 100644
--- a/src/app.c
+++ b/src/app.c
@@ -318,12 +318,17 @@ const iString *dataDir_App(void) {
318 return collect_String(cleanedCStr_Path(dataDir_App_)); 318 return collect_String(cleanedCStr_Path(dataDir_App_));
319} 319}
320 320
321iLocalDef iBool isWaitingAllowed_App_(const iApp *d) {
322 return !d->pendingRefresh && isEmpty_SortedArray(&d->tickers);
323}
324
321void processEvents_App(enum iAppEventMode eventMode) { 325void processEvents_App(enum iAppEventMode eventMode) {
322 iApp *d = &app_; 326 iApp *d = &app_;
323 SDL_Event ev; 327 SDL_Event ev;
324 while ( 328 while ((isWaitingAllowed_App_(d) && eventMode == waitForNewEvents_AppEventMode &&
325 (!d->pendingRefresh && eventMode == waitForNewEvents_AppEventMode && SDL_WaitEvent(&ev)) || 329 SDL_WaitEvent(&ev)) ||
326 ((d->pendingRefresh || eventMode == postedEventsOnly_AppEventMode) && SDL_PollEvent(&ev))) { 330 ((!isWaitingAllowed_App_(d) || eventMode == postedEventsOnly_AppEventMode) &&
331 SDL_PollEvent(&ev))) {
327 switch (ev.type) { 332 switch (ev.type) {
328 case SDL_QUIT: 333 case SDL_QUIT:
329 d->running = iFalse; 334 d->running = iFalse;
@@ -358,12 +363,14 @@ static void runTickers_App_(iApp *d) {
358 const uint32_t now = SDL_GetTicks(); 363 const uint32_t now = SDL_GetTicks();
359 d->elapsedSinceLastTicker = (d->lastTickerTime ? now - d->lastTickerTime : 0); 364 d->elapsedSinceLastTicker = (d->lastTickerTime ? now - d->lastTickerTime : 0);
360 d->lastTickerTime = now; 365 d->lastTickerTime = now;
366 if (isEmpty_SortedArray(&d->tickers)) {
367 d->lastTickerTime = 0;
368 return;
369 }
361 /* Tickers may add themselves again, so we'll run off a copy. */ 370 /* Tickers may add themselves again, so we'll run off a copy. */
362 iSortedArray *pending = copy_SortedArray(&d->tickers); 371 iSortedArray *pending = copy_SortedArray(&d->tickers);
363 clear_SortedArray(&d->tickers); 372 clear_SortedArray(&d->tickers);
364 if (!isEmpty_SortedArray(pending)) { 373 postRefresh_App();
365 postRefresh_App();
366 }
367 iConstForEach(Array, i, &pending->values) { 374 iConstForEach(Array, i, &pending->values) {
368 const iTicker *ticker = i.value; 375 const iTicker *ticker = i.value;
369 if (ticker->callback) { 376 if (ticker->callback) {
@@ -371,6 +378,9 @@ static void runTickers_App_(iApp *d) {
371 } 378 }
372 } 379 }
373 delete_SortedArray(pending); 380 delete_SortedArray(pending);
381 if (isEmpty_SortedArray(&d->tickers)) {
382 d->lastTickerTime = 0;
383 }
374} 384}
375 385
376static int run_App_(iApp *d) { 386static int run_App_(iApp *d) {
@@ -378,8 +388,8 @@ static int run_App_(iApp *d) {
378 d->running = iTrue; 388 d->running = iTrue;
379 SDL_EventState(SDL_DROPFILE, SDL_ENABLE); /* open files via drag'n'drop */ 389 SDL_EventState(SDL_DROPFILE, SDL_ENABLE); /* open files via drag'n'drop */
380 while (d->running) { 390 while (d->running) {
381 runTickers_App_(d);
382 processEvents_App(waitForNewEvents_AppEventMode); 391 processEvents_App(waitForNewEvents_AppEventMode);
392 runTickers_App_(d);
383 refresh_App(); 393 refresh_App();
384 recycle_Garbage(); 394 recycle_Garbage();
385 } 395 }
@@ -419,9 +429,6 @@ int run_App(int argc, char **argv) {
419void postRefresh_App(void) { 429void postRefresh_App(void) {
420 iApp *d = &app_; 430 iApp *d = &app_;
421 if (!d->pendingRefresh) { 431 if (!d->pendingRefresh) {
422 if (!isEmpty_SortedArray(&d->tickers)) {
423 d->lastTickerTime = SDL_GetTicks(); /* tickers had been paused */
424 }
425 d->pendingRefresh = iTrue; 432 d->pendingRefresh = iTrue;
426 SDL_Event ev; 433 SDL_Event ev;
427 ev.user.type = SDL_USEREVENT; 434 ev.user.type = SDL_USEREVENT;