Debian Codegraph @

addons/native_dialog/textlog.c

raw
1 #define ALLEGRO_INTERNAL_UNSTABLE
2
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include "allegro5/allegro.h"
6 #include "allegro5/allegro_native_dialog.h"
7 #include "allegro5/internal/aintern_native_dialog.h"
8 #include "allegro5/internal/aintern_native_dialog_cfg.h"
9 #include "allegro5/internal/aintern_dtor.h"
10 #include "allegro5/internal/aintern_system.h"
11
12
13 /* The GTK and OSX implementations do not require an extra thread.
14 * The Windows implementation does.
15 */
16 #if defined(ALLEGRO_CFG_NATIVE_DIALOG_WINDOWS)
17 #define TEXT_LOG_EXTRA_THREAD true
18 #else
19 #define TEXT_LOG_EXTRA_THREAD false
20 #endif
21
22
23 /* This will only return when the text window is closed. */
24 static void *text_log_thread_proc(ALLEGRO_THREAD *thread, void *arg)
25 {
26 ALLEGRO_NATIVE_DIALOG *textlog = arg;
27
28 if (!_al_open_native_text_log(textlog)) {
29 al_lock_mutex(textlog->tl_text_mutex);
30 textlog->tl_init_error = true;
31 al_signal_cond(textlog->tl_text_cond);
32 al_unlock_mutex(textlog->tl_text_mutex);
33 }
34
35 return thread;
36 }
37
38
39 /* Function: al_open_native_text_log
40 */
41 ALLEGRO_TEXTLOG *al_open_native_text_log(char const *title, int flags)
42 {
43 ALLEGRO_NATIVE_DIALOG *textlog = NULL;
44
45 /* Avoid warnings when log windows are unimplemented. */
46 (void)title;
47 (void)flags;
48
49 textlog = al_calloc(1, sizeof *textlog);
50 textlog->title = al_ustr_new(title);
51 textlog->flags = flags;
52 if (TEXT_LOG_EXTRA_THREAD) {
53 textlog->tl_thread = al_create_thread(text_log_thread_proc, textlog);
54 }
55 textlog->tl_text_cond = al_create_cond();
56 textlog->tl_text_mutex = al_create_mutex();
57 textlog->tl_pending_text = al_ustr_new("");
58 al_init_user_event_source(&textlog->tl_events);
59
60 textlog->tl_init_error = false;
61 textlog->tl_done = false;
62
63 if (TEXT_LOG_EXTRA_THREAD) {
64 /* Unlike the other dialogs, this one never blocks as the intended
65 * use case is a log window running in the background for debugging
66 * purposes when no console can be used. Therefore we have it run
67 * in a separate thread.
68 */
69 al_start_thread(textlog->tl_thread);
70 al_lock_mutex(textlog->tl_text_mutex);
71 while (!textlog->tl_done && !textlog->tl_init_error) {
72 al_wait_cond(textlog->tl_text_cond, textlog->tl_text_mutex);
73 }
74 al_unlock_mutex(textlog->tl_text_mutex);
75 }
76 else {
77 textlog->tl_init_error = !_al_open_native_text_log(textlog);
78 }
79
80 if (textlog->tl_init_error) {
81 al_close_native_text_log((ALLEGRO_TEXTLOG *)textlog);
82 return NULL;
83 }
84
85 textlog->dtor_item = _al_register_destructor(_al_dtor_list, "textlog", textlog,
86 (void (*)(void *))al_close_native_text_log);
87
88 return (ALLEGRO_TEXTLOG *)textlog;
89 }
90
91
92 /* Function: al_close_native_text_log
93 */
94 void al_close_native_text_log(ALLEGRO_TEXTLOG *textlog)
95 {
96 ALLEGRO_NATIVE_DIALOG *dialog = (ALLEGRO_NATIVE_DIALOG *)textlog;
97
98 if (!dialog)
99 return;
100
101 if (!dialog->tl_init_error) {
102 dialog->tl_done = false;
103
104 if (TEXT_LOG_EXTRA_THREAD) {
105 al_lock_mutex(dialog->tl_text_mutex);
106 _al_close_native_text_log(dialog);
107 while (!dialog->tl_done) {
108 al_wait_cond(dialog->tl_text_cond, dialog->tl_text_mutex);
109 }
110 }
111 else {
112 _al_close_native_text_log(dialog);
113 al_lock_mutex(dialog->tl_text_mutex);
114 }
115
116 _al_unregister_destructor(_al_dtor_list, dialog->dtor_item);
117 }
118
119 al_ustr_free(dialog->title);
120 al_ustr_free(dialog->tl_pending_text);
121
122 al_destroy_user_event_source(&dialog->tl_events);
123
124 al_unlock_mutex(dialog->tl_text_mutex);
125
126 if (TEXT_LOG_EXTRA_THREAD) {
127 al_destroy_thread(dialog->tl_thread);
128 }
129 al_destroy_cond(dialog->tl_text_cond);
130 al_destroy_mutex(dialog->tl_text_mutex);
131 al_free(dialog);
132 }
133
134
135 /* Function: al_append_native_text_log
136 */
137 void al_append_native_text_log(ALLEGRO_TEXTLOG *textlog,
138 char const *format, ...)
139 {
140 ALLEGRO_NATIVE_DIALOG *dialog = (ALLEGRO_NATIVE_DIALOG *)textlog;
141 va_list args;
142
143 /* Fall back to printf if no window. */
144 if (!dialog) {
145 va_start(args, format);
146 vprintf(format, args);
147 va_end(args);
148 return;
149 }
150
151 al_lock_mutex(dialog->tl_text_mutex);
152
153 /* We could optimise the case where format="%s". */
154 va_start(args, format);
155 al_ustr_vappendf(dialog->tl_pending_text, format, args);
156 va_end(args);
157
158 _al_append_native_text_log(dialog);
159
160 al_unlock_mutex(dialog->tl_text_mutex);
161 }
162
163
164 /* Function: al_get_native_text_log_event_source
165 */
166 ALLEGRO_EVENT_SOURCE *al_get_native_text_log_event_source(
167 ALLEGRO_TEXTLOG *textlog)
168 {
169 ALLEGRO_NATIVE_DIALOG *dialog = (ALLEGRO_NATIVE_DIALOG *)textlog;
170 ASSERT(dialog);
171
172 return &dialog->tl_events;
173 }
174
175
176 /* vim: set sts=3 sw=3 et: */