From 3dfdc779f1efdcdfaf85fa742a3d6b7cadbaaadd Mon Sep 17 00:00:00 2001
From: Peter Wang <novalazy@gmail.com>
Date: Wed, 24 Nov 2010 08:04:41 +1100
Subject: [PATCH] Add extra logging to GTK native dialogs implementation.

---
 addons/native_dialog/gtk_dialog.c |   39 ++++++++++++++++++++++++++++++++++--
 1 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/addons/native_dialog/gtk_dialog.c b/addons/native_dialog/gtk_dialog.c
index fd2e15c..5a0e495 100644
--- a/addons/native_dialog/gtk_dialog.c
+++ b/addons/native_dialog/gtk_dialog.c
@@ -21,6 +21,10 @@
 
 ALLEGRO_DEBUG_CHANNEL("gtk")
 
+/*---------------------------------------------------------------------------*/
+/* GTK background thread                                                     */
+/*---------------------------------------------------------------------------*/
+
 static int global_counter = 0;
 static GStaticMutex gtk_lock = G_STATIC_MUTEX_INIT;
 static GCond *gtk_cond = NULL;
@@ -56,19 +60,27 @@ static bool gtk_start_and_lock(ALLEGRO_NATIVE_DIALOG *fd)
    int argc = 0;
    char **argv = NULL;
 
-   if (!g_thread_supported())
+   if (!g_thread_supported()) {
+      ALLEGRO_DEBUG("Calling g_thread_init.\n");
       g_thread_init(NULL);
+   }
+   else {
+      ALLEGRO_DEBUG("g_thread_init already called.\n");
+   }
 
    g_static_mutex_lock(&gtk_lock);
 
    global_counter++;
+   ALLEGRO_DEBUG("++global_counter = %d\n", global_counter);
+
    if (global_counter == 1) {
+      ALLEGRO_DEBUG("Calling gdk_threads_init\n");
       gdk_threads_init();
 
       if (!gtk_init_check(&argc, &argv)) {
+         ALLEGRO_ERROR("GTK init failed.\n");
          global_counter--;
          g_static_mutex_unlock(&gtk_lock);
-         ALLEGRO_WARN("GTK init failed.\n");
          return false;
       }
 
@@ -79,19 +91,27 @@ static bool gtk_start_and_lock(ALLEGRO_NATIVE_DIALOG *fd)
 
    gdk_threads_enter();
    fd->is_active = true;
+   ALLEGRO_DEBUG("Leaving gtk_start_and_lock\n");
    return true;
 }
 
 static void gtk_unlock_and_wait(ALLEGRO_NATIVE_DIALOG *nd)
 {
+   ALLEGRO_DEBUG("Entering gtk_unlock_and_wait\n");
    gdk_threads_leave();
 
-   while (nd->is_active)
+   while (nd->is_active) {
+      ALLEGRO_DEBUG("nd->is_active = %d\n", nd->is_active);
       g_cond_wait(gtk_cond, g_static_mutex_get_mutex(&gtk_lock));
+   }
 
    global_counter--;
+   ALLEGRO_DEBUG("--global_counter = %d\n", global_counter);
+
    if (global_counter == 0) {
+      ALLEGRO_DEBUG("Calling gtk_main_quit\n");
       gtk_main_quit();
+      ALLEGRO_DEBUG("Joining gtk_thread\n");
       g_thread_join(gtk_thread);
       gtk_thread = NULL;
       g_cond_free(gtk_cond);
@@ -99,8 +119,13 @@ static void gtk_unlock_and_wait(ALLEGRO_NATIVE_DIALOG *nd)
    }
 
    g_static_mutex_unlock(&gtk_lock);
+   ALLEGRO_DEBUG("Leaving gtk_unlock_and_wait\n");
 }
 
+/*---------------------------------------------------------------------------*/
+/* File dialog                                                               */
+/*---------------------------------------------------------------------------*/
+
 static void set_dialog_inactive(ALLEGRO_NATIVE_DIALOG *nd)
 {
    g_static_mutex_lock(&gtk_lock);
@@ -229,6 +254,10 @@ bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
    return true;
 }
 
+/*---------------------------------------------------------------------------*/
+/* Message box                                                               */
+/*---------------------------------------------------------------------------*/
+
 int _al_show_native_message_box(ALLEGRO_DISPLAY *display,
    ALLEGRO_NATIVE_DIALOG *fd)
 {
@@ -287,6 +316,10 @@ int _al_show_native_message_box(ALLEGRO_DISPLAY *display,
    return fd->mb_pressed_button;
 }
 
+/*---------------------------------------------------------------------------*/
+/* Text log                                                                  */
+/*---------------------------------------------------------------------------*/
+
 static void emit_close_event(ALLEGRO_NATIVE_DIALOG *textlog, bool keypress)
 {
    ALLEGRO_EVENT event;
-- 
1.7.1

