[java-atk-wrapper: 1/2] Use a separate context for delivering events



commit 37a9a39986ac3ecb8717448980e99ccf4344c81c
Author: Samuel Thibault <samuel thibault ens-lyon org>
Date:   Mon Apr 1 22:26:42 2019 +0200

    Use a separate context for delivering events
    
    When the java application is using a gtk look&feel, it will be registering
    stuff in the default main_context, which would thus unexpectedly run within
    the thread started by java-atk-wrapper! Making java-atk-wrapper use its
    own glib main_context looks much safer, so that its operations get really
    isolated in its own thread without interfering with the rest of the
    application.
    
    Fixes https://bugzilla.gnome.org/show_bug.cgi?id=760058

 jni/src/AtkWrapper.c | 49 +++++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 16 deletions(-)
---
diff --git a/jni/src/AtkWrapper.c b/jni/src/AtkWrapper.c
index 1322d45..a5e8e64 100644
--- a/jni/src/AtkWrapper.c
+++ b/jni/src/AtkWrapper.c
@@ -53,6 +53,7 @@ gboolean jaw_debug = FALSE;
 
 static gint key_dispatch_result;
 static GMainLoop* jni_main_loop;
+static GMainContext *jni_main_context;
 
 static gboolean jaw_initialized = FALSE;
 
@@ -108,6 +109,20 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_initNativeLibrary()
   return JNI_TRUE;
 }
 
+static guint
+jni_main_idle_add(GSourceFunc    function, gpointer       data)
+{
+  GSource *source;
+  guint id;
+
+  source = g_idle_source_new();
+  g_source_set_callback(source, function, data, NULL);
+  id = g_source_attach(source, jni_main_context);
+  g_source_unref(source);
+
+  return id;
+}
+
 JNIEXPORT void
 JNICALL Java_org_GNOME_Accessibility_AtkWrapper_loadAtkBridge()
 {
@@ -124,7 +139,9 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_loadAtkBridge()
   if (jaw_debug)
     printf("Jaw Initialization STATUS in loadAtkBridge: %d\n", jaw_initialized);
 
-  jni_main_loop = g_main_loop_new (NULL, FALSE); /*main loop NOT running*/
+  jni_main_context = g_main_context_new();
+  jni_main_loop = g_main_loop_new (jni_main_context, FALSE); /*main loop NOT running*/
+  atk_bridge_set_event_context(jni_main_context);
   thread = g_thread_new(message, jni_loop_callback, (void *) jni_main_loop);
   if(thread == NULL)
   {
@@ -256,7 +273,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_focusNotify(JNIEnv *jniEnv,
 {
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(focus_notify_handler, para);
+  jni_main_idle_add(focus_notify_handler, para);
 }
 
 static gboolean
@@ -334,7 +351,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowOpen(JNIEnv *jniEnv,
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
   para->is_toplevel = (jIsToplevel == JNI_TRUE) ? TRUE : FALSE;
-  g_idle_add(window_open_handler, para);
+  jni_main_idle_add(window_open_handler, para);
 }
 
 static gboolean
@@ -411,7 +428,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowClose(JNIEnv *jniEnv,
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
   para->is_toplevel = (jIsToplevel == JNI_TRUE) ? TRUE : FALSE;
-  g_idle_add(window_close_handler, para);
+  jni_main_idle_add(window_close_handler, para);
 }
 
 static gboolean
@@ -460,7 +477,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowMinimize(JNIEnv *jniEnv,
 {
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(window_minimize_handler, para);
+  jni_main_idle_add(window_minimize_handler, para);
 }
 
 static gboolean
@@ -509,7 +526,7 @@ JNIEXPORT void JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowMaximize(JN
 {
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac );
-  g_idle_add(window_maximize_handler, para);
+  jni_main_idle_add(window_maximize_handler, para);
 }
 
 static gboolean
@@ -559,7 +576,7 @@ JNIEXPORT void JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowRestore(JNI
 
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(window_restore_handler, para);
+  jni_main_idle_add(window_restore_handler, para);
 }
 
 static gboolean
@@ -607,7 +624,7 @@ JNIEXPORT void JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowActivate(JN
 
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(window_activate_handler, para);
+  jni_main_idle_add(window_activate_handler, para);
 }
 
 static gboolean
@@ -657,7 +674,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowDeactivate(JNIEnv *jniEnv,
 
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(window_deactivate_handler, para);
+  jni_main_idle_add(window_deactivate_handler, para);
 }
 
 static gboolean
@@ -708,7 +725,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_windowStateChange(JNIEnv *jniEnv
 
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(window_state_change_handler, para);
+  jni_main_idle_add(window_state_change_handler, para);
 }
 
 static gint
@@ -1014,7 +1031,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_emitSignal(JNIEnv *jniEnv,
   CallbackPara *para = alloc_callback_para(global_ac);
   para->signal_id = (gint)id;
   para->args = global_args;
-  g_idle_add(signal_emit_handler, para);
+  jni_main_idle_add(signal_emit_handler, para);
 }
 
 static gboolean
@@ -1073,7 +1090,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_objectStateChange(JNIEnv *jniEnv
   } else {
     para->state_value = FALSE;
   }
-  g_idle_add(object_state_change_handler, para);
+  jni_main_idle_add(object_state_change_handler, para);
 }
 
 static gboolean
@@ -1130,7 +1147,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_componentAdded(JNIEnv *jniEnv,
 {
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(component_added_handler, para);
+  jni_main_idle_add(component_added_handler, para);
 }
 
 static gboolean
@@ -1189,7 +1206,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_componentRemoved(JNIEnv *jniEnv,
 {
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(component_removed_handler, para);
+  jni_main_idle_add(component_removed_handler, para);
 }
 
 /**
@@ -1250,7 +1267,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_boundsChanged(JNIEnv *jniEnv,
 {
   jobject global_ac = (*jniEnv)->NewGlobalRef(jniEnv, jAccContext);
   CallbackPara *para = alloc_callback_para(global_ac);
-  g_idle_add(bounds_changed_handler, para);
+  jni_main_idle_add(bounds_changed_handler, para);
 }
 
 static gboolean
@@ -1368,7 +1385,7 @@ JNICALL Java_org_GNOME_Accessibility_AtkWrapper_dispatchKeyEvent(JNIEnv *jniEnv,
 {
   jboolean key_consumed;
   jobject global_key_event = (*jniEnv)->NewGlobalRef(jniEnv, jAtkKeyEvent);
-  g_idle_add(key_dispatch_handler, (gpointer)global_key_event);
+  jni_main_idle_add(key_dispatch_handler, (gpointer)global_key_event);
 
   if(jaw_debug)
     printf("key_dispatch_result saved = %d\n ", key_dispatch_result);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]