| 1 |
#include <gtk/gtk.h> |
| 2 |
|
| 3 |
#include "allegro5/allegro.h" |
| 4 |
#include "allegro5/allegro_native_dialog.h" |
| 5 |
#include "allegro5/internal/aintern_native_dialog.h" |
| 6 |
#include "allegro5/internal/aintern_native_dialog_cfg.h" |
| 7 |
#include "gtk_dialog.h" |
| 8 |
|
| 9 |
#include "allegro5/internal/aintern_vector.h" |
| 10 |
|
| 11 |
ALLEGRO_DEBUG_CHANNEL("gtk") |
| 12 |
|
| 13 |
|
| 14 |
/* GTK is not thread safe. We launch a single thread which runs the GTK main |
| 15 |
* loop, and it is the only thread which calls into GTK. (g_timeout_add may be |
| 16 |
* called from other threads without locking.) |
| 17 |
* |
| 18 |
* We used to attempt to use gdk_threads_enter/gdk_threads_leave but hit |
| 19 |
* some problems with deadlocks so switched to this. |
| 20 |
*/ |
| 21 |
|
| 22 |
// G_STATIC_MUTEX_INIT causes a warning about a missing initializer, so if we |
| 23 |
// have version 2.32 or newer don't use it to avoid the warning. |
| 24 |
#if GLIB_CHECK_VERSION(2, 32, 0) |
| 25 |
#define NEWER_GLIB 1 |
| 26 |
#else |
| 27 |
#define NEWER_GLIB 0 |
| 28 |
#endif |
| 29 |
#if NEWER_GLIB |
| 30 |
static GMutex nd_gtk_mutex; |
| 31 |
static void nd_gtk_lock(void) { g_mutex_lock(&nd_gtk_mutex); } |
| 32 |
static void nd_gtk_unlock(void) { g_mutex_unlock(&nd_gtk_mutex); } |
| 33 |
#else |
| 34 |
static GStaticMutex nd_gtk_mutex = G_STATIC_MUTEX_INIT; |
| 35 |
static void nd_gtk_lock(void) { g_static_mutex_lock(&nd_gtk_mutex); } |
| 36 |
static void nd_gtk_unlock(void) { g_static_mutex_unlock(&nd_gtk_mutex); } |
| 37 |
#endif |
| 38 |
static GThread *nd_gtk_thread = NULL; |
| 39 |
|
| 40 |
|
| 41 |
static void *nd_gtk_thread_func(void *data) |
| 42 |
{ |
| 43 |
GAsyncQueue *queue = data; |
| 44 |
|
| 45 |
ALLEGRO_DEBUG("GLIB %d.%d.%d\n", |
| 46 |
GLIB_MAJOR_VERSION, |
| 47 |
GLIB_MINOR_VERSION, |
| 48 |
GLIB_MICRO_VERSION); |
| 49 |
|
| 50 |
g_async_queue_push(queue, ACK_OK); |
| 51 |
|
| 52 |
gtk_main(); |
| 53 |
|
| 54 |
ALLEGRO_INFO("GTK stopped.\n"); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
bool _al_gtk_ensure_thread(void) |
| 60 |
{ |
| 61 |
bool ok = true; |
| 62 |
|
| 63 |
#if !NEWER_GLIB |
| 64 |
if (!g_thread_supported()) |
| 65 |
g_thread_init(NULL); |
| 66 |
#endif |
| 67 |
|
| 68 |
/* al_init_native_dialog_addon() didn't always exist so GTK might not have |
| 69 |
* been initialised. gtk_init_check knows if it's been initialised already |
| 70 |
* so we can just call it again. |
| 71 |
*/ |
| 72 |
{ |
| 73 |
int argc = 0; |
| 74 |
char **argv = NULL; |
| 75 |
if (!gtk_init_check(&argc, &argv)) { |
| 76 |
ALLEGRO_ERROR("gtk_init_check failed\n"); |
| 77 |
return false; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
nd_gtk_lock(); |
| 82 |
|
| 83 |
if (!nd_gtk_thread) { |
| 84 |
GAsyncQueue *queue = g_async_queue_new(); |
| 85 |
#if NEWER_GLIB |
| 86 |
nd_gtk_thread = g_thread_new("gtk thread", nd_gtk_thread_func, queue); |
| 87 |
#else |
| 88 |
bool joinable = FALSE; |
| 89 |
nd_gtk_thread = g_thread_create(nd_gtk_thread_func, queue, joinable, NULL); |
| 90 |
#endif |
| 91 |
if (!nd_gtk_thread) { |
| 92 |
ok = false; |
| 93 |
} |
| 94 |
else { |
| 95 |
ok = (g_async_queue_pop(queue) == ACK_OK); |
| 96 |
} |
| 97 |
g_async_queue_unref(queue); |
| 98 |
} |
| 99 |
|
| 100 |
nd_gtk_unlock(); |
| 101 |
|
| 102 |
return ok; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/* [user thread] */ |
| 107 |
bool _al_gtk_init_args(void *ptr, size_t size) |
| 108 |
{ |
| 109 |
ARGS_BASE *args = (ARGS_BASE *)ptr; |
| 110 |
memset(args, 0, size); |
| 111 |
args->mutex = al_create_mutex(); |
| 112 |
if (!args->mutex) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
args->cond = al_create_cond(); |
| 116 |
if (!args->cond) { |
| 117 |
al_destroy_mutex(args->mutex); |
| 118 |
return false; |
| 119 |
} |
| 120 |
args->done = false; |
| 121 |
args->response = true; |
| 122 |
return args; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/* [user thread] */ |
| 127 |
bool _al_gtk_wait_for_args(GSourceFunc func, void *data) |
| 128 |
{ |
| 129 |
ARGS_BASE *args = (ARGS_BASE *) data; |
| 130 |
bool response; |
| 131 |
|
| 132 |
al_lock_mutex(args->mutex); |
| 133 |
g_timeout_add(0, func, data); |
| 134 |
while (args->done == false) { |
| 135 |
al_wait_cond(args->cond, args->mutex); |
| 136 |
} |
| 137 |
al_unlock_mutex(args->mutex); |
| 138 |
|
| 139 |
response = args->response; |
| 140 |
|
| 141 |
al_destroy_mutex(args->mutex); |
| 142 |
al_destroy_cond(args->cond); |
| 143 |
|
| 144 |
return response; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/* [gtk thread] */ |
| 149 |
void *_al_gtk_lock_args(gpointer data) |
| 150 |
{ |
| 151 |
ARGS_BASE *args = (ARGS_BASE *) data; |
| 152 |
al_lock_mutex(args->mutex); |
| 153 |
return args; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/* [gtk thread] */ |
| 158 |
gboolean _al_gtk_release_args(gpointer data) |
| 159 |
{ |
| 160 |
ARGS_BASE *args = (ARGS_BASE *) data; |
| 161 |
|
| 162 |
args->done = true; |
| 163 |
al_signal_cond(args->cond); |
| 164 |
al_unlock_mutex(args->mutex); |
| 165 |
|
| 166 |
return FALSE; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/* vim: set sts=3 sw=3 et: */ |