summaryrefslogtreecommitdiff
path: root/apps/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins')
-rw-r--r--apps/plugins/lua/rocklib_events.c70
1 files changed, 45 insertions, 25 deletions
diff --git a/apps/plugins/lua/rocklib_events.c b/apps/plugins/lua/rocklib_events.c
index 09f1c2fd8a..41ae8904d0 100644
--- a/apps/plugins/lua/rocklib_events.c
+++ b/apps/plugins/lua/rocklib_events.c
@@ -170,7 +170,7 @@ static void init_event_data(lua_State *L, struct event_data *ev_data)
170 //ev_data->NEWL = NULL; 170 //ev_data->NEWL = NULL;
171 /* rockbox */ 171 /* rockbox */
172 ev_data->thread_id = UINT_MAX; 172 ev_data->thread_id = UINT_MAX;
173 ev_data->thread_state = THREAD_YIELD; 173 ev_data->thread_state = THREAD_YIELD | THREAD_SUSPENDMASK;
174 //ev_data->event_stack = NULL; 174 //ev_data->event_stack = NULL;
175 //ev_data->timer_ticks = 0; 175 //ev_data->timer_ticks = 0;
176 ev_data->freq_input = EV_INPUT; 176 ev_data->freq_input = EV_INPUT;
@@ -316,7 +316,7 @@ static void event_thread(void)
316 lua_interrupt_set(ev_data.L, false); 316 lua_interrupt_set(ev_data.L, false);
317 ev_data.next_event = EV_TICKS; 317 ev_data.next_event = EV_TICKS;
318 rb->yield(); 318 rb->yield();
319 } while(ev_data.thread_state == THREAD_YIELD || is_suspend(THREAD_SUSPENDMASK)); 319 } while(ev_data.thread_state == THREAD_YIELD || is_suspend(THREAD_SUSPENDMASK >> 8));
320 320
321 } 321 }
322 322
@@ -333,26 +333,29 @@ event_error:
333/* timer interrupt callback */ 333/* timer interrupt callback */
334static void rev_timer_isr(void) 334static void rev_timer_isr(void)
335{ 335{
336 ev_data.next_event--; 336 if (ev_data.thread_state != THREAD_QUIT || is_suspend(THREAD_SUSPENDMASK >> 8))
337 ev_data.next_input--;
338
339 if (ev_data.next_input <=0)
340 { 337 {
341 ev_data.thread_state |= ((ev_data.thread_state & THREAD_INPUTMASK) >> 16); 338 ev_data.next_event--;
342 ev_data.next_input = ev_data.freq_input; 339 ev_data.next_input--;
343 }
344 340
345 if (ev_data.cb[TIMEREVENT] != NULL && !is_suspend(TIMEREVENT)) 341 if (ev_data.next_input <=0)
346 {
347 if (TIME_AFTER(*rb->current_tick, ev_data.cb[TIMEREVENT]->id))
348 { 342 {
349 ev_data.thread_state |= thread_ev_states[TIMEREVENT]; 343 ev_data.thread_state |= ((ev_data.thread_state & THREAD_INPUTMASK) >> 16);
350 ev_data.next_event = 0; 344 ev_data.next_input = ev_data.freq_input;
351 } 345 }
352 }
353 346
354 if (ev_data.next_event <= 0) 347 if (ev_data.cb[TIMEREVENT] != NULL && !is_suspend(TIMEREVENT))
355 lua_interrupt_set(ev_data.L, true); 348 {
349 if (TIME_AFTER(*rb->current_tick, ev_data.cb[TIMEREVENT]->id))
350 {
351 ev_data.thread_state |= thread_ev_states[TIMEREVENT];
352 ev_data.next_event = 0;
353 }
354 }
355
356 if (ev_data.next_event <= 0)
357 lua_interrupt_set(ev_data.L, true);
358 }
356} 359}
357 360
358static void create_event_thread_ref(struct event_data *ev_data) 361static void create_event_thread_ref(struct event_data *ev_data)
@@ -387,9 +390,6 @@ static void exit_event_thread(struct event_data *ev_data)
387{ 390{
388 ev_data->thread_state = THREAD_QUIT; 391 ev_data->thread_state = THREAD_QUIT;
389 rb->thread_wait(ev_data->thread_id); /* wait for thread to exit */ 392 rb->thread_wait(ev_data->thread_id); /* wait for thread to exit */
390
391 ev_data->thread_state = THREAD_YIELD;
392 ev_data->thread_id = UINT_MAX;
393} 393}
394 394
395static void init_event_thread(bool init, struct event_data *ev_data) 395static void init_event_thread(bool init, struct event_data *ev_data)
@@ -398,9 +398,13 @@ static void init_event_thread(bool init, struct event_data *ev_data)
398 { 398 {
399 if (!init && ev_data->thread_id != UINT_MAX) 399 if (!init && ev_data->thread_id != UINT_MAX)
400 { 400 {
401 ev_data->thread_state |= THREAD_SUSPENDMASK; /* suspend all events */
402 rb->yield();
401 exit_event_thread(ev_data); 403 exit_event_thread(ev_data);
402 destroy_event_thread_ref(ev_data); 404 destroy_event_thread_ref(ev_data);
403 lua_interrupt_set(ev_data->L, false); 405 lua_interrupt_set(ev_data->L, false);
406 ev_data->thread_state = THREAD_YIELD | THREAD_SUSPENDMASK;
407 ev_data->thread_id = UINT_MAX;
404 } 408 }
405 return; 409 return;
406 } 410 }
@@ -421,15 +425,19 @@ static void init_event_thread(bool init, struct event_data *ev_data)
421 425
422 /* Timer is used to poll waiting events */ 426 /* Timer is used to poll waiting events */
423 rb->timer_register(0, NULL, EV_TIMER_FREQ, rev_timer_isr IF_COP(, CPU)); 427 rb->timer_register(0, NULL, EV_TIMER_FREQ, rev_timer_isr IF_COP(, CPU));
428 ev_data->thread_state &= ~THREAD_SUSPENDMASK;
424} 429}
425 430
426static void playback_event_callback(unsigned short id, void *data) 431static void playback_event_callback(unsigned short id, void *data)
427{ 432{
428 /* playback events are synchronous we need to return ASAP so set a flag */ 433 /* playback events are synchronous we need to return ASAP so set a flag */
429 ev_data.thread_state |= thread_ev_states[PLAYBKEVENT]; 434 if (ev_data.thread_state != THREAD_QUIT && !is_suspend(THREAD_SUSPENDMASK >> 8))
430 ev_data.cb[PLAYBKEVENT]->id = id; 435 {
431 ev_data.cb[PLAYBKEVENT]->data = data; 436 ev_data.thread_state |= thread_ev_states[PLAYBKEVENT];
432 lua_interrupt_set(ev_data.L, true); 437 ev_data.cb[PLAYBKEVENT]->id = id;
438 ev_data.cb[PLAYBKEVENT]->data = data;
439 lua_interrupt_set(ev_data.L, true);
440 }
433} 441}
434 442
435static void register_playbk_events(int flag_events, 443static void register_playbk_events(int flag_events,
@@ -499,11 +507,17 @@ static int rockev_gc(lua_State *L) {
499 void *d = (void *) lua_touserdata (L, 1); 507 void *d = (void *) lua_touserdata (L, 1);
500 508
501 if (d == NULL) 509 if (d == NULL)
510 {
502 return 0; 511 return 0;
512 }
503 else if (d == ev_data.event_stack) /* thread stack is gc'd kill thread */ 513 else if (d == ev_data.event_stack) /* thread stack is gc'd kill thread */
514 {
504 init_event_thread(false, &ev_data); 515 init_event_thread(false, &ev_data);
516 }
505 else if (d == ev_data.cb[PLAYBKEVENT]) 517 else if (d == ev_data.cb[PLAYBKEVENT])
518 {
506 register_playbk_events(0, &playback_event_callback); 519 register_playbk_events(0, &playback_event_callback);
520 }
507 521
508 for( int i= 0; i < EVENT_CT; i++) 522 for( int i= 0; i < EVENT_CT; i++)
509 { 523 {
@@ -526,6 +540,9 @@ static int rockev_gc(lua_State *L) {
526 540
527static int rockev_register(lua_State *L) 541static int rockev_register(lua_State *L)
528{ 542{
543 if (ev_data.thread_state == THREAD_QUIT)
544 return 0;
545
529 int event = luaL_checkoption(L, 1, NULL, ev_map); 546 int event = luaL_checkoption(L, 1, NULL, ev_map);
530 int ev_flag = thread_ev_states[event]; 547 int ev_flag = thread_ev_states[event];
531 int playbk_events; 548 int playbk_events;
@@ -562,6 +579,9 @@ static int rockev_register(lua_State *L)
562 579
563static int rockev_suspend(lua_State *L) 580static int rockev_suspend(lua_State *L)
564{ 581{
582 if (ev_data.thread_state == THREAD_QUIT)
583 return 0;
584
565 int event; /*Arg 1 is event pass nil to suspend all */ 585 int event; /*Arg 1 is event pass nil to suspend all */
566 bool suspend = luaL_optboolean(L, 2, true); 586 bool suspend = luaL_optboolean(L, 2, true);
567 int ev_flag = THREAD_SUSPENDMASK; 587 int ev_flag = THREAD_SUSPENDMASK;
@@ -588,7 +608,7 @@ static int rockev_trigger(lua_State *L)
588 int ev_flag; 608 int ev_flag;
589 609
590 /* protect from invalid events */ 610 /* protect from invalid events */
591 if (ev_data.cb[event] != NULL) 611 if (ev_data.cb[event] != NULL && ev_data.thread_state != THREAD_QUIT)
592 { 612 {
593 ev_flag = thread_ev_states[event]; 613 ev_flag = thread_ev_states[event];
594 /* allow user to pass an id to some of the callback functions */ 614 /* allow user to pass an id to some of the callback functions */