Debian Codegraph @

src/sdl/sdl_event_hack.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * SDL event hack (see below).
12 *
13 * See LICENSE.txt for copyright information.
14 */
15
16 #include "allegro5/allegro.h"
17 #include "allegro5/internal/aintern_system.h"
18 #include "allegro5/internal/aintern_timer.h"
19 #include "allegro5/internal/aintern_exitfunc.h"
20 #include "allegro5/platform/allegro_internal_sdl.h"
21
22 /* This is a thread which wakes up event queues from time to time (with fake
23 * timer events) to prevent a deadlock in an unbound al_wait_for_event.
24 */
25
26 static ALLEGRO_THREAD *thread;
27
28 static void wakeup_with_fake_timer_event(void)
29 {
30 ALLEGRO_EVENT_SOURCE *es = al_get_keyboard_event_source();
31 _al_event_source_lock(es);
32 ALLEGRO_EVENT event;
33 event.timer.type = ALLEGRO_EVENT_TIMER;
34 event.timer.timestamp = al_get_time();
35 event.timer.count = 0;
36 event.timer.error = 0;
37 _al_event_source_emit_event(es, &event);
38 _al_event_source_unlock(es);
39 }
40
41 static void *wakeup_thread(ALLEGRO_THREAD *thread, void *user)
42 {
43 al_rest(1);
44 while (!al_get_thread_should_stop(thread)) {
45 /* If the program uses timers, this hack is not required usually. */
46 if (_al_get_active_timers_count())
47 break;
48 if (!al_is_keyboard_installed())
49 break;
50 wakeup_with_fake_timer_event();
51 al_rest(0.01);
52 }
53 return user;
54 }
55
56 static void _uninstall_sdl_event_hack(void)
57 {
58 if (thread) {
59 al_set_thread_should_stop(thread);
60 al_join_thread(thread, NULL);
61 al_destroy_thread(thread);
62 }
63 }
64
65 void _al_sdl_event_hack(void)
66 {
67 if (thread)
68 return;
69 _al_add_exit_func(_uninstall_sdl_event_hack, "uninstall_sdl_event_hack");
70 thread = al_create_thread(wakeup_thread, NULL);
71 al_start_thread(thread);
72 }