-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathGlassApplication.cpp
569 lines (491 loc) · 16.7 KB
/
GlassApplication.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cstdlib>
#include <com_sun_glass_ui_gtk_GtkApplication.h>
#include <com_sun_glass_events_WindowEvent.h>
#include <com_sun_glass_events_MouseEvent.h>
#include <com_sun_glass_events_ViewEvent.h>
#include <com_sun_glass_events_KeyEvent.h>
#include <jni.h>
#include "glass_general.h"
#include "glass_evloop.h"
#include "glass_dnd.h"
#include "glass_window.h"
#include "glass_screen.h"
#include "PlatformSupport.h"
GdkEventFunc process_events_prev;
static void process_events(GdkEvent*, gpointer);
JNIEnv* mainEnv; // Use only with main loop thread!!!
PlatformSupport* platformSupport = NULL;
extern gboolean disableGrab;
static gboolean call_runnable (gpointer data)
{
RunnableContext* context = reinterpret_cast<RunnableContext*>(data);
JNIEnv *env;
int envStatus = javaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
if (envStatus == JNI_EDETACHED) {
javaVM->AttachCurrentThread((void **)&env, NULL);
}
env->CallVoidMethod(context->runnable, jRunnableRun, NULL);
LOG_EXCEPTION(env);
env->DeleteGlobalRef(context->runnable);
free(context);
if (envStatus == JNI_EDETACHED) {
javaVM->DetachCurrentThread();
}
return FALSE;
}
extern "C" {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
static void init_threads() {
gboolean is_g_thread_get_initialized = FALSE;
if (glib_check_version(2, 32, 0)) { // < 2.32
if (!glib_check_version(2, 20, 0)) {
is_g_thread_get_initialized = g_thread_get_initialized();
}
if (!is_g_thread_get_initialized) {
g_thread_init(NULL);
}
}
gdk_threads_init();
}
#pragma GCC diagnostic pop
jboolean gtk_verbose = JNI_FALSE;
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _initGTK
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1initGTK
(JNIEnv *env, jclass clazz, jint version, jboolean verbose, jfloat uiScale)
{
(void) clazz;
(void) version;
OverrideUIScale = uiScale;
gtk_verbose = verbose;
env->ExceptionClear();
init_threads();
gdk_threads_enter();
gtk_init(NULL, NULL);
// Major version is checked before loading
if (version == 3
&& gtk_check_version(version, GTK_3_MIN_MINOR_VERSION, GTK_3_MIN_MICRO_VERSION)) {
char message[100];
snprintf(message, sizeof(message),
"Minimum GTK version required is %d.%d.%d. System has %d.%d.%d.",
version, GTK_3_MIN_MINOR_VERSION, GTK_3_MIN_MICRO_VERSION,
gtk_major_version, gtk_minor_version, gtk_micro_version);
jclass uoe = env->FindClass("java/lang/UnsupportedOperationException");
env->ThrowNew(uoe, message);
return;
}
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _queryLibrary
* Signature: Signature: (IZ)I
*/
#ifndef STATIC_BUILD
JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1queryLibrary
(JNIEnv *env, jclass clazz, jint suggestedVersion, jboolean verbose)
{
// If we are being called, then the launcher is
// not in use, and we are in the proper glass library already.
// This can be done by renaming the gtk versioned native
// libraries to be libglass.so
// Note: we will make no effort to complain if the suggestedVersion
// is out of phase.
(void)env;
(void)clazz;
(void)suggestedVersion;
(void)verbose;
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
return com_sun_glass_ui_gtk_GtkApplication_QUERY_NO_DISPLAY;
}
XCloseDisplay(display);
return com_sun_glass_ui_gtk_GtkApplication_QUERY_USE_CURRENT;
}
#endif
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1init
(JNIEnv * env, jobject obj, jlong handler, jboolean _disableGrab)
{
(void)obj;
mainEnv = env;
process_events_prev = (GdkEventFunc) handler;
disableGrab = (gboolean) _disableGrab;
glass_gdk_x11_display_set_window_scale(gdk_display_get_default(), 1);
gdk_event_handler_set(process_events, NULL, NULL);
GdkScreen *default_gdk_screen = gdk_screen_get_default();
if (default_gdk_screen != NULL) {
g_signal_connect(G_OBJECT(default_gdk_screen), "monitors-changed",
G_CALLBACK(screen_settings_changed), NULL);
g_signal_connect(G_OBJECT(default_gdk_screen), "size-changed",
G_CALLBACK(screen_settings_changed), NULL);
}
GdkWindow *root = gdk_screen_get_root_window(default_gdk_screen);
gdk_window_set_events(root, static_cast<GdkEventMask>(gdk_window_get_events(root) | GDK_PROPERTY_CHANGE_MASK));
platformSupport = new PlatformSupport(env, obj);
// Set ibus to sync mode
setenv("IBUS_ENABLE_SYNC_MODE", "1", 1);
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _runLoop
* Signature: (Ljava/lang/Runnable;Z)V
*/
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1runLoop
(JNIEnv * env, jobject obj, jobject launchable, jboolean noErrorTrap)
{
(void)obj;
(void)noErrorTrap;
env->CallVoidMethod(launchable, jRunnableRun);
CHECK_JNI_EXCEPTION(env);
// GTK installs its own X error handler that conflicts with AWT.
// During drag and drop, AWT hides errors so we need to hide them
// to avoid exit()'ing. It's not clear that we don't want to hide
// X error all the time, otherwise FX will exit().
//
// A better solution would be to coordinate with AWT and save and
// restore the X handler.
// Disable X error handling
#ifndef VERBOSE
if (!noErrorTrap) {
gdk_error_trap_push();
}
#endif
gtk_main();
// When the last JFrame closes and DISPOSE_ON_CLOSE is specified,
// Java exits with an X error. X error are hidden during the FX
// event loop and should be restored when the event loop exits. Unfortunately,
// this is too early. The fix is to never restore X errors.
//
// See JDK-8126059 & JDK-8118745
// Restore X error handling
// #ifndef VERBOSE
// if (!noErrorTrap) {
// gdk_error_trap_pop();
// }
// #endif
gdk_threads_leave();
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _terminateLoop
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1terminateLoop
(JNIEnv * env, jobject obj)
{
(void)env;
(void)obj;
gtk_main_quit();
if (platformSupport) {
delete platformSupport;
platformSupport = NULL;
}
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _submitForLaterInvocation
* Signature: (Ljava/lang/Runnable;)V
*/
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1submitForLaterInvocation
(JNIEnv * env, jobject obj, jobject runnable)
{
(void)obj;
RunnableContext* context = (RunnableContext*)malloc(sizeof(RunnableContext));
if (context != NULL) {
context->runnable = env->NewGlobalRef(runnable);
gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE + 30, call_runnable, context, NULL);
// we release this context in call_runnable
} else {
fprintf(stderr, "malloc failed in GtkApplication__1submitForLaterInvocation\n");
}
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: enterNestedEventLoopImpl
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_enterNestedEventLoopImpl
(JNIEnv * env, jobject obj)
{
(void)env;
(void)obj;
gtk_main();
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: leaveNestedEventLoopImpl
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_leaveNestedEventLoopImpl
(JNIEnv * env, jobject obj)
{
(void)env;
(void)obj;
gtk_main_quit();
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: staticScreen_getScreens
* Signature: ()[Lcom/sun/glass/ui/Screen;
*/
JNIEXPORT jobjectArray JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticScreen_1getScreens
(JNIEnv * env, jobject obj)
{
(void)obj;
try {
return rebuild_screens(env);
} catch (jni_exception&) {
return NULL;
}
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: staticTimer_getMinPeriod
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticTimer_1getMinPeriod
(JNIEnv * env, jobject obj)
{
(void)env;
(void)obj;
return 0; // There are no restrictions on period in g_threads
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: staticTimer_getMaxPeriod
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticTimer_1getMaxPeriod
(JNIEnv * env, jobject obj)
{
(void)env;
(void)obj;
return 10000; // There are no restrictions on period in g_threads
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: staticView_getMultiClickTime
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickTime
(JNIEnv * env, jobject obj)
{
(void)env;
(void)obj;
static gint multi_click_time = -1;
if (multi_click_time == -1) {
g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &multi_click_time, NULL);
}
return (jlong)multi_click_time;
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: staticView_getMultiClickMaxX
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickMaxX
(JNIEnv * env, jobject obj)
{
(void)env;
(void)obj;
static gint multi_click_dist = -1;
if (multi_click_dist == -1) {
g_object_get(gtk_settings_get_default(), "gtk-double-click-distance", &multi_click_dist, NULL);
}
return multi_click_dist;
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: staticView_getMultiClickMaxY
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickMaxY
(JNIEnv * env, jobject obj)
{
return Java_com_sun_glass_ui_gtk_GtkApplication_staticView_1getMultiClickMaxX(env, obj);
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: _supportsTransparentWindows
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1supportsTransparentWindows
(JNIEnv * env, jobject obj) {
(void)env;
(void)obj;
return gdk_display_supports_composite(gdk_display_get_default())
&& gdk_screen_is_composited(gdk_screen_get_default());
}
/*
* Class: com_sun_glass_ui_gtk_GtkApplication
* Method: getPlatformPreferences
* Signature: ()Ljava/util/Map;
*/
JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_gtk_GtkApplication_getPlatformPreferences
(JNIEnv *env, jobject self)
{
return platformSupport ? platformSupport->collectPreferences() : NULL;
}
} // extern "C"
bool is_window_enabled_for_event(GdkWindow * window, WindowContext *ctx, gint event_type) {
if (gdk_window_is_destroyed(window)) {
return FALSE;
}
/*
* GDK_DELETE can be blocked for disabled window e.q. parent window
* which prevents from closing it
*/
switch (event_type) {
case GDK_CONFIGURE:
case GDK_DESTROY:
case GDK_EXPOSE:
case GDK_DAMAGE:
case GDK_WINDOW_STATE:
case GDK_FOCUS_CHANGE:
return TRUE;
break;
}//switch
if (ctx != NULL ) {
return ctx->isEnabled();
}
return TRUE;
}
static void process_events(GdkEvent* event, gpointer data)
{
GdkWindow* window = event->any.window;
WindowContext *ctx = window != NULL ? (WindowContext*)
g_object_get_data(G_OBJECT(window), GDK_WINDOW_DATA_CONTEXT) : NULL;
if ((window != NULL)
&& !is_window_enabled_for_event(window, ctx, event->type)) {
return;
}
EventsCounterHelper helper(ctx);
if ((event->type == GDK_KEY_PRESS || event->type == GDK_KEY_RELEASE) && ctx != NULL && ctx->filterIME(event)) {
return;
}
glass_evloop_call_hooks(event);
if (ctx != NULL) {
try {
switch (event->type) {
case GDK_PROPERTY_NOTIFY:
// let gtk handle it first to prevent a glitch
gtk_main_do_event(event);
ctx->process_property_notify(&event->property);
break;
case GDK_CONFIGURE:
ctx->process_configure(&event->configure);
gtk_main_do_event(event);
break;
case GDK_FOCUS_CHANGE:
ctx->process_focus(&event->focus_change);
gtk_main_do_event(event);
break;
case GDK_DESTROY:
destroy_and_delete_ctx(ctx);
gtk_main_do_event(event);
break;
case GDK_DELETE:
ctx->process_delete();
break;
case GDK_EXPOSE:
case GDK_DAMAGE:
ctx->process_expose(&event->expose);
break;
case GDK_WINDOW_STATE:
ctx->process_state(&event->window_state);
gtk_main_do_event(event);
break;
case GDK_BUTTON_PRESS:
case GDK_BUTTON_RELEASE:
ctx->process_mouse_button(&event->button);
break;
case GDK_MOTION_NOTIFY:
ctx->process_mouse_motion(&event->motion);
gdk_event_request_motions(&event->motion);
break;
case GDK_SCROLL:
ctx->process_mouse_scroll(&event->scroll);
break;
case GDK_ENTER_NOTIFY:
case GDK_LEAVE_NOTIFY:
ctx->process_mouse_cross(&event->crossing);
break;
case GDK_KEY_PRESS:
case GDK_KEY_RELEASE:
ctx->process_key(&event->key);
break;
case GDK_DROP_START:
case GDK_DRAG_ENTER:
case GDK_DRAG_LEAVE:
case GDK_DRAG_MOTION:
process_dnd_target(ctx, &event->dnd);
break;
case GDK_MAP:
// fall-through
case GDK_UNMAP:
case GDK_CLIENT_EVENT:
case GDK_VISIBILITY_NOTIFY:
case GDK_SETTING:
case GDK_OWNER_CHANGE:
gtk_main_do_event(event);
break;
default:
break;
}
} catch (jni_exception&) {
}
} else {
if (window == gdk_screen_get_root_window(gdk_screen_get_default())) {
if (event->any.type == GDK_PROPERTY_NOTIFY) {
if (event->property.atom == gdk_atom_intern_static_string("_NET_WORKAREA")
|| event->property.atom == gdk_atom_intern_static_string("_NET_CURRENT_DESKTOP")) {
screen_settings_changed(gdk_screen_get_default(), NULL);
}
}
}
//process only for non-FX windows
if (process_events_prev != NULL) {
(*process_events_prev)(event, data);
} else {
gtk_main_do_event(event);
}
}
}