1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 #include <X11/Xlib.h>
  26 #include <X11/Xatom.h>
  27 #include <gdk/gdk.h>
  28 #include <gdk/gdkx.h>
  29 #include <gtk/gtk.h>
  30 #include <glib.h>
  31 
  32 #include <cstdlib>
  33 #include <com_sun_glass_ui_Application.h>
  34 #include <com_sun_glass_ui_gtk_GtkApplication.h>
  35 #include <com_sun_glass_events_WindowEvent.h>
  36 #include <com_sun_glass_events_MouseEvent.h>
  37 #include <com_sun_glass_events_ViewEvent.h>
  38 #include <com_sun_glass_events_KeyEvent.h>
  39 #include <jni.h>
  40 
  41 #include "glass_general.h"
  42 #include "glass_evloop.h"
  43 #include "glass_dnd.h"
  44 #include "glass_window.h"
  45 #include "glass_screen.h"
  46 
  47 GdkEventFunc process_events_prev;
  48 static void process_events(GdkEvent*, gpointer);
  49 
  50 JNIEnv* mainEnv; // Use only with main loop thread!!!
  51 
  52 extern gboolean disableGrab;
  53 
  54 static gboolean call_runnable (gpointer data)
  55 {
  56     RunnableContext* context = reinterpret_cast<RunnableContext*>(data);
  57 
  58     mainEnv->CallVoidMethod(context->runnable, jRunnableRun, NULL);
  59     LOG_EXCEPTION(mainEnv);
  60     mainEnv->DeleteGlobalRef(context->runnable);
  61     free(context);
  62     return FALSE;
  63 }
  64 
  65 extern "C" {
  66 
  67 
  68 #pragma GCC diagnostic push
  69 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  70 static void init_threads() {
  71     gboolean is_g_thread_get_initialized = FALSE;
  72     if (glib_check_version(2, 32, 0)) { // < 2.32
  73         if (!glib_check_version(2, 20, 0)) {
  74             is_g_thread_get_initialized = g_thread_get_initialized();
  75         }
  76         if (!is_g_thread_get_initialized) {
  77             g_thread_init(NULL);
  78         }
  79     }
  80     gdk_threads_init();
  81 }
  82 #pragma GCC diagnostic pop
  83 
  84 jboolean gtk_verbose = JNI_FALSE;
  85 
  86 /*
  87  * Class:     com_sun_glass_ui_gtk_GtkApplication
  88  * Method:    _initGTK
  89  * Signature: (IZ)I
  90  */
  91 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
  92   (JNIEnv *env, jclass clazz, jint version, jboolean verbose)
  93 {
  94     (void) clazz;
  95     (void) version;
  96 
  97     gtk_verbose = verbose;
  98 
  99     env->ExceptionClear();
 100     init_threads();
 101 
 102     gdk_threads_enter();
 103     gtk_init(NULL, NULL);
 104 
 105     return JNI_TRUE;
 106 }
 107 
 108 /*
 109  * Class:     com_sun_glass_ui_gtk_GtkApplication
 110  * Method:    _queryLibrary
 111  * Signature: Signature: (IZ)I
 112  */
 113 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1queryLibrary
 114   (JNIEnv *env, jclass clazz, jint suggestedVersion, jboolean verbose)
 115 {
 116     // If we are being called, then the launcher is
 117     // not in use, and we are in the proper glass library already.
 118     // This can be done by renaming the gtk versioned native
 119     // libraries to be libglass.so
 120     // Note: we will make no effort to complain if the suggestedVersion
 121     // is out of phase.
 122 
 123     (void)env;
 124     (void)clazz;
 125     (void)suggestedVersion;
 126     (void)verbose;
 127 
 128     Display *display = XOpenDisplay(NULL);
 129     if (display == NULL) {
 130         return com_sun_glass_ui_gtk_GtkApplication_QUERY_NO_DISPLAY;
 131     }
 132     XCloseDisplay(display);
 133 
 134     return com_sun_glass_ui_gtk_GtkApplication_QUERY_USE_CURRENT;
 135 }
 136 
 137 /*
 138  * Class:     com_sun_glass_ui_gtk_GtkApplication
 139  * Method:    _init
 140  * Signature: ()V
 141  */
 142 JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1init
 143   (JNIEnv * env, jobject obj, jlong handler, jboolean _disableGrab)
 144 {
 145     (void)obj;
 146 
 147     mainEnv = env;
 148     process_events_prev = (GdkEventFunc) handler;
 149     disableGrab = (gboolean) _disableGrab;
 150 
 151     glass_gdk_x11_display_set_window_scale(gdk_display_get_default(), 1);
 152     gdk_event_handler_set(process_events, NULL, NULL);
 153 
 154     GdkScreen *default_gdk_screen = gdk_screen_get_default();
 155     if (default_gdk_screen != NULL) {
 156         g_signal_connect(G_OBJECT(default_gdk_screen), "monitors-changed",
 157                          G_CALLBACK(screen_settings_changed), NULL);
 158         g_signal_connect(G_OBJECT(default_gdk_screen), "size-changed",
 159                          G_CALLBACK(screen_settings_changed), NULL);
 160     }
 161 
 162     GdkWindow *root = gdk_screen_get_root_window(default_gdk_screen);
 163     gdk_window_set_events(root, static_cast<GdkEventMask>(gdk_window_get_events(root) | GDK_PROPERTY_CHANGE_MASK));
 164 }
 165 
 166 /*
 167  * Class:     com_sun_glass_ui_gtk_GtkApplication
 168  * Method:    _runLoop
 169  * Signature: (Ljava/lang/Runnable;Z)V
 170  */
 171 JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1runLoop
 172   (JNIEnv * env, jobject obj, jobject launchable, jboolean noErrorTrap)
 173 {
 174     (void)obj;
 175     (void)noErrorTrap;
 176 
 177     env->CallVoidMethod(launchable, jRunnableRun);
 178     CHECK_JNI_EXCEPTION(env);
 179 
 180     // GTK installs its own X error handler that conflicts with AWT.
 181     // During drag and drop, AWT hides errors so we need to hide them
 182     // to avoid exit()'ing.  It's not clear that we don't want to hide
 183     // X error all the time, otherwise FX will exit().
 184     //
 185     // A better solution would be to coordinate with AWT and save and
 186     // restore the X handler.
 187 
 188     // Disable X error handling
 189 #ifndef VERBOSE
 190     if (!noErrorTrap) {
 191         gdk_error_trap_push();
 192     }
 193 #endif
 194 
 195     gtk_main();
 196 
 197     // When the last JFrame closes and DISPOSE_ON_CLOSE is specified,
 198     // Java exits with an X error. X error are hidden during the FX
 199     // event loop and should be restored when the event loop exits. Unfortunately,
 200     // this is too early. The fix is to never restore X errors.
 201     //
 202     // See RT-21408 & RT-20756
 203 
 204     // Restore X error handling
 205     // #ifndef VERBOSE
 206     //     if (!noErrorTrap) {
 207     //         gdk_error_trap_pop();
 208     //     }
 209     // #endif
 210 
 211     gdk_threads_leave();
 212 
 213 }
 214 
 215 /*
 216  * Class:     com_sun_glass_ui_gtk_GtkApplication
 217  * Method:    _terminateLoop
 218  * Signature: ()V
 219  */
 220 JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1terminateLoop
 221   (JNIEnv * env, jobject obj)
 222 {
 223     (void)env;
 224     (void)obj;
 225 
 226     gtk_main_quit();
 227 }
 228 
 229 /*
 230  * Class:     com_sun_glass_ui_gtk_GtkApplication
 231  * Method:    _submitForLaterInvocation
 232  * Signature: (Ljava/lang/Runnable;)V
 233  */
 234 JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1submitForLaterInvocation
 235   (JNIEnv * env, jobject obj, jobject runnable)
 236 {
 237     (void)obj;
 238 
 239     RunnableContext* context = (RunnableContext*)malloc(sizeof(RunnableContext));
 240     context->runnable = env->NewGlobalRef(runnable);
 241     gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE + 30, call_runnable, context, NULL);
 242 }
 243 
 244 /*
 245  * Class:     com_sun_glass_ui_gtk_GtkApplication
 246  * Method:    enterNestedEventLoopImpl
 247  * Signature: ()V
 248  */
 249 JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_enterNestedEventLoopImpl
 250   (JNIEnv * env, jobject obj)
 251 {
 252     (void)env;
 253     (void)obj;
 254 
 255     gtk_main();
 256 }
 257 
 258 /*
 259  * Class:     com_sun_glass_ui_gtk_GtkApplication
 260  * Method:    leaveNestedEventLoopImpl
 261  * Signature: ()V
 262  */
 263 JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_leaveNestedEventLoopImpl
 264   (JNIEnv * env, jobject obj)
 265 {
 266     (void)env;
 267     (void)obj;
 268 
 269     gtk_main_quit();
 270 }
 271 
 272 /*
 273  * Class:     com_sun_glass_ui_gtk_GtkApplication
 274  * Method:    staticScreen_getScreens
 275  * Signature: ()[Lcom/sun/glass/ui/Screen;
 276  */
 277 JNIEXPORT jobjectArray JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticScreen_1getScreens
 278   (JNIEnv * env, jobject obj)
 279 {
 280     (void)obj;
 281 
 282     try {
 283         return rebuild_screens(env);
 284     } catch (jni_exception&) {
 285         return NULL;
 286     }
 287 }
 288 
 289 /*
 290  * Class:     com_sun_glass_ui_gtk_GtkApplication
 291  * Method:    staticTimer_getMinPeriod
 292  * Signature: ()I
 293  */
 294 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticTimer_1getMinPeriod
 295   (JNIEnv * env, jobject obj)
 296 {
 297     (void)env;
 298     (void)obj;
 299 
 300     return 0; // There are no restrictions on period in g_threads
 301 }
 302 
 303 /*
 304  * Class:     com_sun_glass_ui_gtk_GtkApplication
 305  * Method:    staticTimer_getMaxPeriod
 306  * Signature: ()I
 307  */
 308 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticTimer_1getMaxPeriod
 309   (JNIEnv * env, jobject obj)
 310 {
 311     (void)env;
 312     (void)obj;
 313 
 314     return 10000; // There are no restrictions on period in g_threads
 315 }
 316 
 317 /*
 318  * Class:     com_sun_glass_ui_gtk_GtkApplication
 319  * Method:    staticView_getMultiClickTime
 320  * Signature: ()J
 321  */
 322 JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickTime
 323   (JNIEnv * env, jobject obj)
 324 {
 325     (void)env;
 326     (void)obj;
 327 
 328     static gint multi_click_time = -1;
 329     if (multi_click_time == -1) {
 330         g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &multi_click_time, NULL);
 331     }
 332     return (jlong)multi_click_time;
 333 }
 334 
 335 /*
 336  * Class:     com_sun_glass_ui_gtk_GtkApplication
 337  * Method:    staticView_getMultiClickMaxX
 338  * Signature: ()I
 339  */
 340 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickMaxX
 341   (JNIEnv * env, jobject obj)
 342 {
 343     (void)env;
 344     (void)obj;
 345 
 346     static gint multi_click_dist = -1;
 347 
 348     if (multi_click_dist == -1) {
 349         g_object_get(gtk_settings_get_default(), "gtk-double-click-distance", &multi_click_dist, NULL);
 350     }
 351     return multi_click_dist;
 352 }
 353 
 354 /*
 355  * Class:     com_sun_glass_ui_gtk_GtkApplication
 356  * Method:    staticView_getMultiClickMaxY
 357  * Signature: ()I
 358  */
 359 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickMaxY
 360   (JNIEnv * env, jobject obj)
 361 {
 362     return Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickMaxX(env, obj);
 363 }
 364 
 365 /*
 366  * Class:     com_sun_glass_ui_gtk_GtkApplication
 367  * Method:    _supportsTransparentWindows
 368  * Signature: ()Z
 369  */
 370 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1supportsTransparentWindows
 371   (JNIEnv * env, jobject obj) {
 372     (void)env;
 373     (void)obj;
 374 
 375     return gdk_display_supports_composite(gdk_display_get_default())
 376             && gdk_screen_is_composited(gdk_screen_get_default());
 377 }
 378 
 379 } // extern "C"
 380 
 381 bool is_window_enabled_for_event(GdkWindow * window, WindowContext *ctx, gint event_type) {
 382 
 383 
 384     if (gdk_window_is_destroyed(window)) {
 385         return FALSE;
 386     }
 387 
 388     /*
 389      * GDK_DELETE can be blocked for disabled window e.q. parent window
 390      * which prevents from closing it
 391      */
 392     switch (event_type) {
 393         case GDK_CONFIGURE:
 394         case GDK_DESTROY:
 395         case GDK_EXPOSE:
 396         case GDK_DAMAGE:
 397         case GDK_WINDOW_STATE:
 398         case GDK_FOCUS_CHANGE:
 399             return TRUE;
 400             break;
 401     }//switch
 402 
 403     if (ctx != NULL ) {
 404         return ctx->isEnabled();
 405     }
 406     return TRUE;
 407 }
 408 
 409 static void process_events(GdkEvent* event, gpointer data)
 410 {
 411     GdkWindow* window = event->any.window;
 412     WindowContext *ctx = window != NULL ? (WindowContext*)
 413         g_object_get_data(G_OBJECT(window), GDK_WINDOW_DATA_CONTEXT) : NULL;
 414 
 415     if ((window != NULL)
 416             && !is_window_enabled_for_event(window, ctx, event->type)) {
 417         return;
 418     }
 419 
 420     if (ctx != NULL && ctx->hasIME() && ctx->filterIME(event)) {
 421         return;
 422     }
 423 
 424     glass_evloop_call_hooks(event);
 425 
 426     if (ctx != NULL && dynamic_cast<WindowContextPlug*>(ctx) && ctx->get_gtk_window()) {
 427         WindowContextPlug* ctx_plug = dynamic_cast<WindowContextPlug*>(ctx);
 428         if (!ctx_plug->embedded_children.empty()) {
 429             // forward to child
 430             ctx = (WindowContext*) ctx_plug->embedded_children.back();
 431             window = ctx->get_gdk_window();
 432         }
 433     }
 434 
 435     if (is_in_drag()) {
 436         process_dnd_source(window, event);
 437     }
 438 
 439     if (ctx != NULL) {
 440         EventsCounterHelper helper(ctx);
 441         try {
 442             switch (event->type) {
 443                 case GDK_PROPERTY_NOTIFY:
 444                     ctx->process_property_notify(&event->property);
 445                     gtk_main_do_event(event);
 446                     break;
 447                 case GDK_CONFIGURE:
 448                     ctx->process_configure(&event->configure);
 449                     gtk_main_do_event(event);
 450                     break;
 451                 case GDK_FOCUS_CHANGE:
 452                     ctx->process_focus(&event->focus_change);
 453                     gtk_main_do_event(event);
 454                     break;
 455                 case GDK_DESTROY:
 456                     destroy_and_delete_ctx(ctx);
 457                     gtk_main_do_event(event);
 458                     break;
 459                 case GDK_DELETE:
 460                     ctx->process_delete();
 461                     break;
 462                 case GDK_EXPOSE:
 463                 case GDK_DAMAGE:
 464                     ctx->process_expose(&event->expose);
 465                     break;
 466                 case GDK_WINDOW_STATE:
 467                     ctx->process_state(&event->window_state);
 468                     gtk_main_do_event(event);
 469                     break;
 470                 case GDK_BUTTON_PRESS:
 471                 case GDK_BUTTON_RELEASE:
 472                     ctx->process_mouse_button(&event->button);
 473                     break;
 474                 case GDK_MOTION_NOTIFY:
 475                     ctx->process_mouse_motion(&event->motion);
 476                     gdk_event_request_motions(&event->motion);
 477                     break;
 478                 case GDK_SCROLL:
 479                     ctx->process_mouse_scroll(&event->scroll);
 480                     break;
 481                 case GDK_ENTER_NOTIFY:
 482                 case GDK_LEAVE_NOTIFY:
 483                     ctx->process_mouse_cross(&event->crossing);
 484                     break;
 485                 case GDK_KEY_PRESS:
 486                 case GDK_KEY_RELEASE:
 487                     ctx->process_key(&event->key);
 488                     break;
 489                 case GDK_DROP_START:
 490                 case GDK_DRAG_ENTER:
 491                 case GDK_DRAG_LEAVE:
 492                 case GDK_DRAG_MOTION:
 493                     process_dnd_target(ctx, &event->dnd);
 494                     break;
 495                 case GDK_MAP:
 496                     ctx->process_map();
 497                     // fall-through
 498                 case GDK_UNMAP:
 499                 case GDK_CLIENT_EVENT:
 500                 case GDK_VISIBILITY_NOTIFY:
 501                 case GDK_SETTING:
 502                 case GDK_OWNER_CHANGE:
 503                     gtk_main_do_event(event);
 504                     break;
 505                 default:
 506                     break;
 507             }
 508         } catch (jni_exception&) {
 509         }
 510     } else {
 511 
 512         if (window == gdk_screen_get_root_window(gdk_screen_get_default())) {
 513             if (event->any.type == GDK_PROPERTY_NOTIFY) {
 514                 if (event->property.atom == gdk_atom_intern_static_string("_NET_WORKAREA")
 515                         || event->property.atom == gdk_atom_intern_static_string("_NET_CURRENT_DESKTOP")) {
 516                     screen_settings_changed(gdk_screen_get_default(), NULL);
 517                 }
 518             }
 519         }
 520 
 521         //process only for non-FX windows
 522         if (process_events_prev != NULL) {
 523             (*process_events_prev)(event, data);
 524         } else {
 525             gtk_main_do_event(event);
 526         }
 527     }
 528 }