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