Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/xawt/XToolkit.c
32287 views
1
/*
2
* Copyright (c) 2002, 2020, 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
26
#include <X11/Xlib.h>
27
#include <X11/Xutil.h>
28
#include <X11/Xos.h>
29
#include <X11/Xatom.h>
30
#ifdef __linux__
31
#include <execinfo.h>
32
#endif
33
34
#include <jvm.h>
35
#include <jni.h>
36
#include <jlong.h>
37
#include <jni_util.h>
38
39
#include "awt_p.h"
40
#include "awt_Component.h"
41
#include "awt_MenuComponent.h"
42
#include "awt_Font.h"
43
#include "awt_util.h"
44
45
#include "sun_awt_X11_XToolkit.h"
46
#include "java_awt_SystemColor.h"
47
#include "java_awt_TrayIcon.h"
48
#include <X11/extensions/XTest.h>
49
50
#include <unistd.h>
51
52
uint32_t awt_NumLockMask = 0;
53
Boolean awt_ModLockIsShiftLock = False;
54
55
static int32_t num_buttons = 0;
56
int32_t getNumButtons();
57
58
extern JavaVM *jvm;
59
60
// Tracing level
61
static int tracing = 0;
62
#ifdef PRINT
63
#undef PRINT
64
#endif
65
#ifdef PRINT2
66
#undef PRINT2
67
#endif
68
69
#define PRINT if (tracing) printf
70
#define PRINT2 if (tracing > 1) printf
71
72
73
struct ComponentIDs componentIDs;
74
75
struct MenuComponentIDs menuComponentIDs;
76
77
#ifndef HEADLESS
78
79
extern Display* awt_init_Display(JNIEnv *env, jobject this);
80
extern void freeNativeStringArray(char **array, jsize length);
81
extern char** stringArrayToNative(JNIEnv *env, jobjectArray array, jsize * ret_length);
82
83
struct XFontPeerIDs xFontPeerIDs;
84
85
JNIEXPORT void JNICALL
86
Java_sun_awt_X11_XFontPeer_initIDs
87
(JNIEnv *env, jclass cls)
88
{
89
xFontPeerIDs.xfsname =
90
(*env)->GetFieldID(env, cls, "xfsname", "Ljava/lang/String;");
91
}
92
#endif /* !HEADLESS */
93
94
/* This function gets called from the static initializer for FileDialog.java
95
to initialize the fieldIDs for fields that may be accessed from C */
96
97
JNIEXPORT void JNICALL
98
Java_java_awt_FileDialog_initIDs
99
(JNIEnv *env, jclass cls)
100
{
101
102
}
103
104
JNIEXPORT void JNICALL
105
Java_sun_awt_X11_XToolkit_initIDs
106
(JNIEnv *env, jclass clazz)
107
{
108
jfieldID fid = (*env)->GetStaticFieldID(env, clazz, "numLockMask", "I");
109
CHECK_NULL(fid);
110
awt_NumLockMask = (*env)->GetStaticIntField(env, clazz, fid);
111
DTRACE_PRINTLN1("awt_NumLockMask = %u", awt_NumLockMask);
112
fid = (*env)->GetStaticFieldID(env, clazz, "modLockIsShiftLock", "I");
113
CHECK_NULL(fid);
114
awt_ModLockIsShiftLock = (*env)->GetStaticIntField(env, clazz, fid) != 0 ? True : False;
115
}
116
117
/*
118
* Class: sun_awt_X11_XToolkit
119
* Method: getTrayIconDisplayTimeout
120
* Signature: ()J
121
*/
122
JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getTrayIconDisplayTimeout
123
(JNIEnv *env, jclass clazz)
124
{
125
#ifndef JAVASE_EMBEDDED
126
return (jlong) 2000;
127
#else
128
return (jlong) 10000;
129
#endif
130
}
131
132
/*
133
* Class: sun_awt_X11_XToolkit
134
* Method: getDefaultXColormap
135
* Signature: ()J
136
*/
137
JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultXColormap
138
(JNIEnv *env, jclass clazz)
139
{
140
AwtGraphicsConfigDataPtr defaultConfig =
141
getDefaultConfig(DefaultScreen(awt_display));
142
143
return (jlong) defaultConfig->awt_cmap;
144
}
145
146
JNIEXPORT jint JNICALL
147
JNI_OnLoad(JavaVM *vm, void *reserved)
148
{
149
jvm = vm;
150
151
//Set the gtk backend to x11 on all the systems
152
putenv("GDK_BACKEND=x11");
153
154
return JNI_VERSION_1_2;
155
}
156
157
/*
158
* Class: sun_awt_X11_XToolkit
159
* Method: nativeLoadSystemColors
160
* Signature: ([I)V
161
*/
162
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_nativeLoadSystemColors
163
(JNIEnv *env, jobject this, jintArray systemColors)
164
{
165
AwtGraphicsConfigDataPtr defaultConfig =
166
getDefaultConfig(DefaultScreen(awt_display));
167
awtJNI_CreateColorData(env, defaultConfig, 1);
168
}
169
170
JNIEXPORT void JNICALL
171
Java_java_awt_Component_initIDs
172
(JNIEnv *env, jclass cls)
173
{
174
jclass keyclass = NULL;
175
176
177
componentIDs.x = (*env)->GetFieldID(env, cls, "x", "I");
178
CHECK_NULL(componentIDs.x);
179
componentIDs.y = (*env)->GetFieldID(env, cls, "y", "I");
180
CHECK_NULL(componentIDs.y);
181
componentIDs.width = (*env)->GetFieldID(env, cls, "width", "I");
182
CHECK_NULL(componentIDs.width);
183
componentIDs.height = (*env)->GetFieldID(env, cls, "height", "I");
184
CHECK_NULL(componentIDs.height);
185
componentIDs.isPacked = (*env)->GetFieldID(env, cls, "isPacked", "Z");
186
CHECK_NULL(componentIDs.isPacked);
187
componentIDs.peer =
188
(*env)->GetFieldID(env, cls, "peer", "Ljava/awt/peer/ComponentPeer;");
189
CHECK_NULL(componentIDs.peer);
190
componentIDs.background =
191
(*env)->GetFieldID(env, cls, "background", "Ljava/awt/Color;");
192
CHECK_NULL(componentIDs.background);
193
componentIDs.foreground =
194
(*env)->GetFieldID(env, cls, "foreground", "Ljava/awt/Color;");
195
CHECK_NULL(componentIDs.foreground);
196
componentIDs.graphicsConfig =
197
(*env)->GetFieldID(env, cls, "graphicsConfig",
198
"Ljava/awt/GraphicsConfiguration;");
199
CHECK_NULL(componentIDs.graphicsConfig);
200
componentIDs.name =
201
(*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");
202
CHECK_NULL(componentIDs.name);
203
204
/* Use _NoClientCode() methods for trusted methods, so that we
205
* know that we are not invoking client code on trusted threads
206
*/
207
componentIDs.getParent =
208
(*env)->GetMethodID(env, cls, "getParent_NoClientCode",
209
"()Ljava/awt/Container;");
210
CHECK_NULL(componentIDs.getParent);
211
212
componentIDs.getLocationOnScreen =
213
(*env)->GetMethodID(env, cls, "getLocationOnScreen_NoTreeLock",
214
"()Ljava/awt/Point;");
215
CHECK_NULL(componentIDs.getLocationOnScreen);
216
217
keyclass = (*env)->FindClass(env, "java/awt/event/KeyEvent");
218
CHECK_NULL(keyclass);
219
220
componentIDs.isProxyActive =
221
(*env)->GetFieldID(env, keyclass, "isProxyActive",
222
"Z");
223
CHECK_NULL(componentIDs.isProxyActive);
224
225
componentIDs.appContext =
226
(*env)->GetFieldID(env, cls, "appContext",
227
"Lsun/awt/AppContext;");
228
229
(*env)->DeleteLocalRef(env, keyclass);
230
}
231
232
233
JNIEXPORT void JNICALL
234
Java_java_awt_Container_initIDs
235
(JNIEnv *env, jclass cls)
236
{
237
238
}
239
240
241
JNIEXPORT void JNICALL
242
Java_java_awt_Button_initIDs
243
(JNIEnv *env, jclass cls)
244
{
245
246
}
247
248
JNIEXPORT void JNICALL
249
Java_java_awt_Scrollbar_initIDs
250
(JNIEnv *env, jclass cls)
251
{
252
253
}
254
255
256
JNIEXPORT void JNICALL
257
Java_java_awt_Window_initIDs
258
(JNIEnv *env, jclass cls)
259
{
260
261
}
262
263
JNIEXPORT void JNICALL
264
Java_java_awt_Frame_initIDs
265
(JNIEnv *env, jclass cls)
266
{
267
268
}
269
270
271
JNIEXPORT void JNICALL
272
Java_java_awt_MenuComponent_initIDs(JNIEnv *env, jclass cls)
273
{
274
menuComponentIDs.appContext =
275
(*env)->GetFieldID(env, cls, "appContext", "Lsun/awt/AppContext;");
276
}
277
278
JNIEXPORT void JNICALL
279
Java_java_awt_Cursor_initIDs(JNIEnv *env, jclass cls)
280
{
281
}
282
283
284
JNIEXPORT void JNICALL Java_java_awt_MenuItem_initIDs
285
(JNIEnv *env, jclass cls)
286
{
287
}
288
289
290
JNIEXPORT void JNICALL Java_java_awt_Menu_initIDs
291
(JNIEnv *env, jclass cls)
292
{
293
}
294
295
JNIEXPORT void JNICALL
296
Java_java_awt_TextArea_initIDs
297
(JNIEnv *env, jclass cls)
298
{
299
}
300
301
302
JNIEXPORT void JNICALL
303
Java_java_awt_Checkbox_initIDs
304
(JNIEnv *env, jclass cls)
305
{
306
}
307
308
309
JNIEXPORT void JNICALL Java_java_awt_ScrollPane_initIDs
310
(JNIEnv *env, jclass cls)
311
{
312
}
313
314
JNIEXPORT void JNICALL
315
Java_java_awt_TextField_initIDs
316
(JNIEnv *env, jclass cls)
317
{
318
}
319
320
JNIEXPORT jboolean JNICALL AWTIsHeadless() {
321
#ifdef HEADLESS
322
return JNI_TRUE;
323
#else
324
return JNI_FALSE;
325
#endif
326
}
327
328
JNIEXPORT void JNICALL Java_java_awt_Dialog_initIDs (JNIEnv *env, jclass cls)
329
{
330
}
331
332
333
/* ========================== Begin poll section ================================ */
334
335
// Includes
336
337
#include <sys/time.h>
338
#include <limits.h>
339
#include <locale.h>
340
#include <pthread.h>
341
342
#include <dlfcn.h>
343
#include <fcntl.h>
344
345
#include <poll.h>
346
#ifndef POLLRDNORM
347
#define POLLRDNORM POLLIN
348
#endif
349
350
// Prototypes
351
352
static void waitForEvents(JNIEnv *, jlong);
353
static void awt_pipe_init();
354
static void processOneEvent(XtInputMask iMask);
355
static Boolean performPoll(JNIEnv *, jlong);
356
static void wakeUp();
357
static void update_poll_timeout(int timeout_control);
358
static uint32_t get_poll_timeout(jlong nextTaskTime);
359
360
// Defines
361
362
#ifndef bzero
363
#define bzero(a,b) memset(a, 0, b)
364
#endif
365
366
#define AWT_POLL_BUFSIZE 100 /* bytes */
367
#define AWT_READPIPE (awt_pipe_fds[0])
368
#define AWT_WRITEPIPE (awt_pipe_fds[1])
369
370
#ifdef JAVASE_EMBEDDED
371
#define DEF_AWT_MAX_POLL_TIMEOUT ((uint32_t)4000000000) /* milliseconds */
372
#else
373
#define DEF_AWT_MAX_POLL_TIMEOUT ((uint32_t)500) /* milliseconds */
374
#endif
375
376
#define DEF_AWT_FLUSH_TIMEOUT ((uint32_t)100) /* milliseconds */
377
#define AWT_MIN_POLL_TIMEOUT ((uint32_t)0) /* milliseconds */
378
379
#define TIMEOUT_TIMEDOUT 0
380
#define TIMEOUT_EVENTS 1
381
382
/* awt_poll_alg - AWT Poll Events Aging Algorithms */
383
#define AWT_POLL_FALSE 1
384
#define AWT_POLL_AGING_SLOW 2
385
#define AWT_POLL_AGING_FAST 3
386
387
#define AWT_POLL_THRESHOLD 1000 // msec, Block if delay is larger
388
#define AWT_POLL_BLOCK -1 // cause poll() block
389
390
// Static fields
391
392
#ifdef JAVASE_EMBEDDED
393
static int awt_poll_alg = AWT_POLL_AGING_FAST;
394
#else
395
static int awt_poll_alg = AWT_POLL_AGING_SLOW;
396
#endif
397
398
static uint32_t AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT; /* milliseconds */
399
static uint32_t AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT; /* milliseconds */
400
static pthread_t awt_MainThread = 0;
401
static int32_t awt_pipe_fds[2]; /* fds for wkaeup pipe */
402
static Boolean awt_pipe_inited = False; /* make sure pipe is initialized before write */
403
static jlong awt_next_flush_time = 0LL; /* 0 == no scheduled flush */
404
static jlong awt_last_flush_time = 0LL; /* 0 == no scheduled flush */
405
static uint32_t curPollTimeout;
406
static struct pollfd pollFds[2];
407
static jlong poll_sleep_time = 0LL; // Used for tracing
408
static jlong poll_wakeup_time = 0LL; // Used for tracing
409
410
// AWT static poll timeout. Zero means "not set", aging algorithm is
411
// used. Static poll timeout values higher than 50 cause application
412
// look "slow" - they don't respond to user request fast
413
// enough. Static poll timeout value less than 10 are usually
414
// considered by schedulers as zero, so this might cause unnecessary
415
// CPU consumption by Java. The values between 10 - 50 are suggested
416
// for single client desktop configurations. For SunRay servers, it
417
// is highly recomended to use aging algorithm (set static poll timeout
418
// to 0).
419
static int32_t static_poll_timeout = 0;
420
421
static Bool isMainThread() {
422
return awt_MainThread == pthread_self();
423
}
424
425
/*
426
* Creates the AWT utility pipe. This pipe exists solely so that
427
* we can cause the main event thread to wake up from a poll() or
428
* select() by writing to this pipe.
429
*/
430
static void
431
awt_pipe_init() {
432
433
if (awt_pipe_inited) {
434
return;
435
}
436
437
if ( pipe ( awt_pipe_fds ) == 0 )
438
{
439
/*
440
** the write wakes us up from the infinite sleep, which
441
** then we cause a delay of AWT_FLUSHTIME and then we
442
** flush.
443
*/
444
int32_t flags = 0;
445
/* set the pipe to be non-blocking */
446
flags = fcntl ( AWT_READPIPE, F_GETFL, 0 );
447
fcntl( AWT_READPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );
448
flags = fcntl ( AWT_WRITEPIPE, F_GETFL, 0 );
449
fcntl( AWT_WRITEPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );
450
awt_pipe_inited = True;
451
}
452
else
453
{
454
AWT_READPIPE = -1;
455
AWT_WRITEPIPE = -1;
456
}
457
458
459
} /* awt_pipe_init() */
460
461
/**
462
* Reads environment variables to initialize timeout fields.
463
*/
464
static void readEnv() {
465
char * value;
466
int tmp_poll_alg;
467
static Boolean env_read = False;
468
if (env_read) return;
469
470
env_read = True;
471
472
value = getenv("_AWT_MAX_POLL_TIMEOUT");
473
if (value != NULL) {
474
AWT_MAX_POLL_TIMEOUT = atoi(value);
475
if (AWT_MAX_POLL_TIMEOUT == 0) {
476
AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT;
477
}
478
}
479
curPollTimeout = AWT_MAX_POLL_TIMEOUT/2;
480
481
value = getenv("_AWT_FLUSH_TIMEOUT");
482
if (value != NULL) {
483
AWT_FLUSH_TIMEOUT = atoi(value);
484
if (AWT_FLUSH_TIMEOUT == 0) {
485
AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT;
486
}
487
}
488
489
value = getenv("_AWT_POLL_TRACING");
490
if (value != NULL) {
491
tracing = atoi(value);
492
}
493
494
value = getenv("_AWT_STATIC_POLL_TIMEOUT");
495
if (value != NULL) {
496
static_poll_timeout = atoi(value);
497
}
498
if (static_poll_timeout != 0) {
499
curPollTimeout = static_poll_timeout;
500
}
501
502
// non-blocking poll()
503
value = getenv("_AWT_POLL_ALG");
504
if (value != NULL) {
505
tmp_poll_alg = atoi(value);
506
switch(tmp_poll_alg) {
507
case AWT_POLL_FALSE:
508
case AWT_POLL_AGING_SLOW:
509
case AWT_POLL_AGING_FAST:
510
awt_poll_alg = tmp_poll_alg;
511
break;
512
default:
513
PRINT("Unknown value of _AWT_POLL_ALG, assuming Slow Aging Algorithm by default");
514
awt_poll_alg = AWT_POLL_AGING_SLOW;
515
break;
516
}
517
}
518
}
519
520
/**
521
* Returns the amount of milliseconds similar to System.currentTimeMillis()
522
*/
523
static jlong
524
awtJNI_TimeMillis(void)
525
{
526
struct timeval t;
527
528
gettimeofday(&t, 0);
529
530
return jlong_add(jlong_mul(jint_to_jlong(t.tv_sec), jint_to_jlong(1000)),
531
jint_to_jlong(t.tv_usec / 1000));
532
}
533
534
/**
535
* Updates curPollTimeout according to the aging algorithm.
536
* @param timeout_control Either TIMEOUT_TIMEDOUT or TIMEOUT_EVENTS
537
*/
538
static void update_poll_timeout(int timeout_control) {
539
PRINT2("tout: %d\n", timeout_control);
540
541
// If static_poll_timeout is set, curPollTimeout has the fixed value
542
if (static_poll_timeout != 0) return;
543
544
// Update it otherwise
545
546
switch(awt_poll_alg) {
547
case AWT_POLL_AGING_SLOW:
548
if (timeout_control == TIMEOUT_TIMEDOUT) {
549
/* add 1/4 (plus 1, in case the division truncates to 0) */
550
curPollTimeout += ((curPollTimeout>>2) + 1);
551
curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout);
552
} else if (timeout_control == TIMEOUT_EVENTS) {
553
/* subtract 1/4 (plus 1, in case the division truncates to 0) */
554
curPollTimeout -= ((curPollTimeout>>2) + 1);
555
curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, curPollTimeout);
556
}
557
break;
558
case AWT_POLL_AGING_FAST:
559
if (timeout_control == TIMEOUT_TIMEDOUT) {
560
curPollTimeout += ((curPollTimeout>>2) + 1);
561
curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout);
562
if((int)curPollTimeout > AWT_POLL_THRESHOLD || (int)curPollTimeout == AWT_POLL_BLOCK)
563
curPollTimeout = AWT_POLL_BLOCK;
564
} else if (timeout_control == TIMEOUT_EVENTS) {
565
curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, 1);
566
}
567
break;
568
}
569
}
570
571
/*
572
* Gets the best timeout for the next call to poll().
573
*
574
* @param nextTaskTime -1, if there are no tasks; next time when
575
* timeout task needs to be run, in millis(of currentTimeMillis)
576
*/
577
static uint32_t get_poll_timeout(jlong nextTaskTime)
578
{
579
uint32_t ret_timeout = 0;
580
uint32_t timeout;
581
uint32_t taskTimeout;
582
uint32_t flushTimeout;
583
584
jlong curTime = awtJNI_TimeMillis();
585
timeout = curPollTimeout;
586
switch(awt_poll_alg) {
587
case AWT_POLL_AGING_SLOW:
588
case AWT_POLL_AGING_FAST:
589
taskTimeout = (nextTaskTime == -1) ? AWT_MAX_POLL_TIMEOUT : (uint32_t)max(0, (int32_t)(nextTaskTime - curTime));
590
flushTimeout = (awt_next_flush_time > 0) ? (uint32_t)max(0, (int32_t)(awt_next_flush_time - curTime)) : AWT_MAX_POLL_TIMEOUT;
591
592
PRINT2("to: %d, ft: %d, to: %d, tt: %d, mil: %d\n", taskTimeout, flushTimeout, timeout, (int)nextTaskTime, (int)curTime);
593
594
// Adjust timeout to flush_time and task_time
595
ret_timeout = min(flushTimeout, min(taskTimeout, timeout));
596
if((int)curPollTimeout == AWT_POLL_BLOCK)
597
ret_timeout = AWT_POLL_BLOCK;
598
break;
599
600
case AWT_POLL_FALSE:
601
ret_timeout = (nextTaskTime > curTime) ?
602
(nextTaskTime - curTime) :
603
((nextTaskTime == -1) ? -1 : 0);
604
break;
605
}
606
607
return ret_timeout;
608
609
} /* get_poll_timeout() */
610
611
/*
612
* Waits for X/Xt events to appear on the pipe. Returns only when
613
* it is likely (but not definite) that there are events waiting to
614
* be processed.
615
*
616
* This routine also flushes the outgoing X queue, when the
617
* awt_next_flush_time has been reached.
618
*
619
* If fdAWTPipe is greater or equal than zero the routine also
620
* checks if there are events pending on the putback queue.
621
*/
622
void
623
waitForEvents(JNIEnv *env, jlong nextTaskTime) {
624
if (performPoll(env, nextTaskTime)
625
&& (awt_next_flush_time > 0)
626
&& (awtJNI_TimeMillis() >= awt_next_flush_time)) {
627
628
XFlush(awt_display);
629
awt_last_flush_time = awt_next_flush_time;
630
awt_next_flush_time = 0LL;
631
}
632
} /* waitForEvents() */
633
634
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_waitForEvents (JNIEnv *env, jclass class, jlong nextTaskTime) {
635
waitForEvents(env, nextTaskTime);
636
}
637
638
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1toolkit_1init (JNIEnv *env, jclass class) {
639
awt_MainThread = pthread_self();
640
641
awt_pipe_init();
642
readEnv();
643
}
644
645
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1output_1flush (JNIEnv *env, jclass class) {
646
awt_output_flush();
647
}
648
649
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_wakeup_1poll (JNIEnv *env, jclass class) {
650
wakeUp();
651
}
652
653
/*
654
* Polls both the X pipe and our AWT utility pipe. Returns
655
* when there is data on one of the pipes, or the operation times
656
* out.
657
*
658
* Not all Xt events come across the X pipe (e.g., timers
659
* and alternate inputs), so we must time out every now and
660
* then to check the Xt event queue.
661
*
662
* The fdAWTPipe will be empty when this returns.
663
*/
664
static Boolean
665
performPoll(JNIEnv *env, jlong nextTaskTime) {
666
static Bool pollFdsInited = False;
667
static char read_buf[AWT_POLL_BUFSIZE + 1]; /* dummy buf to empty pipe */
668
669
uint32_t timeout = get_poll_timeout(nextTaskTime);
670
int32_t result;
671
672
if (!pollFdsInited) {
673
pollFds[0].fd = ConnectionNumber(awt_display);
674
pollFds[0].events = POLLRDNORM;
675
pollFds[0].revents = 0;
676
677
pollFds[1].fd = AWT_READPIPE;
678
pollFds[1].events = POLLRDNORM;
679
pollFds[1].revents = 0;
680
pollFdsInited = True;
681
} else {
682
pollFds[0].revents = 0;
683
pollFds[1].revents = 0;
684
}
685
686
AWT_NOFLUSH_UNLOCK();
687
688
/* ACTUALLY DO THE POLL() */
689
if (timeout == 0) {
690
// be sure other threads get a chance
691
if (!awtJNI_ThreadYield(env)) {
692
return FALSE;
693
}
694
}
695
696
if (tracing) poll_sleep_time = awtJNI_TimeMillis();
697
result = poll( pollFds, 2, (int32_t) timeout );
698
if (tracing) poll_wakeup_time = awtJNI_TimeMillis();
699
PRINT("%d of %d, res: %d\n", (int)(poll_wakeup_time-poll_sleep_time), (int)timeout, result);
700
701
AWT_LOCK();
702
if (result == 0) {
703
/* poll() timed out -- update timeout value */
704
update_poll_timeout(TIMEOUT_TIMEDOUT);
705
PRINT2("performPoll(): TIMEOUT_TIMEDOUT curPollTimeout = %d \n", curPollTimeout);
706
}
707
if (pollFds[1].revents) {
708
int count;
709
PRINT("Woke up\n");
710
/* There is data on the AWT pipe - empty it */
711
do {
712
count = read(AWT_READPIPE, read_buf, AWT_POLL_BUFSIZE );
713
} while (count == AWT_POLL_BUFSIZE );
714
PRINT2("performPoll(): data on the AWT pipe: curPollTimeout = %d \n", curPollTimeout);
715
}
716
if (pollFds[0].revents) {
717
// Events in X pipe
718
update_poll_timeout(TIMEOUT_EVENTS);
719
PRINT2("performPoll(): TIMEOUT_EVENTS curPollTimeout = %ld \n", curPollTimeout);
720
}
721
return TRUE;
722
723
} /* performPoll() */
724
725
/**
726
* Schedules next auto-flush event or performs forced flush depending
727
* on the time of the previous flush.
728
*/
729
void awt_output_flush() {
730
if (awt_next_flush_time == 0) {
731
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
732
733
jlong curTime = awtJNI_TimeMillis(); // current time
734
jlong l_awt_last_flush_time = awt_last_flush_time; // last time we flushed queue
735
jlong next_flush_time = l_awt_last_flush_time + AWT_FLUSH_TIMEOUT;
736
737
if (curTime >= next_flush_time) {
738
// Enough time passed from last flush
739
PRINT("f1\n");
740
AWT_LOCK();
741
XFlush(awt_display);
742
awt_last_flush_time = curTime;
743
AWT_NOFLUSH_UNLOCK();
744
} else {
745
awt_next_flush_time = next_flush_time;
746
PRINT("f2\n");
747
wakeUp();
748
}
749
}
750
}
751
752
753
/**
754
* Wakes-up poll() in performPoll
755
*/
756
static void wakeUp() {
757
static char wakeUp_char = 'p';
758
if (!isMainThread() && awt_pipe_inited) {
759
write ( AWT_WRITEPIPE, &wakeUp_char, 1 );
760
}
761
}
762
763
764
/* ========================== End poll section ================================= */
765
766
/*
767
* Class: java_awt_KeyboardFocusManager
768
* Method: initIDs
769
* Signature: ()V
770
*/
771
JNIEXPORT void JNICALL
772
Java_java_awt_KeyboardFocusManager_initIDs
773
(JNIEnv *env, jclass cls)
774
{
775
}
776
777
/*
778
* Class: sun_awt_X11_XToolkit
779
* Method: getEnv
780
* Signature: (Ljava/lang/String;)Ljava/lang/String;
781
*/
782
JNIEXPORT jstring JNICALL Java_sun_awt_X11_XToolkit_getEnv
783
(JNIEnv *env , jclass clazz, jstring key) {
784
char *ptr = NULL;
785
const char *keystr = NULL;
786
jstring ret = NULL;
787
788
keystr = JNU_GetStringPlatformChars(env, key, NULL);
789
if (keystr) {
790
ptr = getenv(keystr);
791
if (ptr) {
792
ret = JNU_NewStringPlatform(env, (const char *) ptr);
793
}
794
JNU_ReleaseStringPlatformChars(env, key, (const char*)keystr);
795
}
796
return ret;
797
}
798
799
#ifdef __linux__
800
void print_stack(void)
801
{
802
void *array[10];
803
size_t size;
804
char **strings;
805
size_t i;
806
807
size = backtrace (array, 10);
808
strings = backtrace_symbols (array, size);
809
810
fprintf (stderr, "Obtained %zd stack frames.\n", size);
811
812
for (i = 0; i < size; i++)
813
fprintf (stderr, "%s\n", strings[i]);
814
815
free (strings);
816
}
817
#endif
818
819
Window get_xawt_root_shell(JNIEnv *env) {
820
static jclass classXRootWindow = NULL;
821
static jmethodID methodGetXRootWindow = NULL;
822
static Window xawt_root_shell = None;
823
824
if (xawt_root_shell == None){
825
if (classXRootWindow == NULL){
826
jclass cls_tmp = (*env)->FindClass(env, "sun/awt/X11/XRootWindow");
827
if (!JNU_IsNull(env, cls_tmp)) {
828
classXRootWindow = (jclass)(*env)->NewGlobalRef(env, cls_tmp);
829
(*env)->DeleteLocalRef(env, cls_tmp);
830
}
831
}
832
if( classXRootWindow != NULL) {
833
methodGetXRootWindow = (*env)->GetStaticMethodID(env, classXRootWindow, "getXRootWindow", "()J");
834
}
835
if( classXRootWindow != NULL && methodGetXRootWindow !=NULL ) {
836
xawt_root_shell = (Window) (*env)->CallStaticLongMethod(env, classXRootWindow, methodGetXRootWindow);
837
}
838
if ((*env)->ExceptionCheck(env)) {
839
(*env)->ExceptionDescribe(env);
840
(*env)->ExceptionClear(env);
841
}
842
}
843
return xawt_root_shell;
844
}
845
846
/*
847
* Old, compatibility, backdoor for DT. This is a different
848
* implementation. It keeps the signature, but acts on
849
* awt_root_shell, not the frame passed as an argument. Note, that
850
* the code that uses the old backdoor doesn't work correctly with
851
* gnome session proxy that checks for WM_COMMAND when the window is
852
* firts mapped, because DT code calls this old backdoor *after* the
853
* frame is shown or it would get NPE with old AWT (previous
854
* implementation of this backdoor) otherwise. Old style session
855
* managers (e.g. CDE) that check WM_COMMAND only during session
856
* checkpoint should work fine, though.
857
*
858
* NB: The function name looks deceptively like a JNI native method
859
* name. It's not! It's just a plain function.
860
*/
861
862
JNIEXPORT void JNICALL
863
Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this,
864
jobject frame, jstring jcommand)
865
{
866
const char *command;
867
XTextProperty text_prop;
868
char *c[1];
869
int32_t status;
870
Window xawt_root_window;
871
872
AWT_LOCK();
873
xawt_root_window = get_xawt_root_shell(env);
874
875
if ( xawt_root_window == None ) {
876
AWT_UNLOCK();
877
JNU_ThrowNullPointerException(env, "AWT root shell is unrealized");
878
return;
879
}
880
881
command = (char *) JNU_GetStringPlatformChars(env, jcommand, NULL);
882
if (command != NULL) {
883
c[0] = (char *)command;
884
status = XmbTextListToTextProperty(awt_display, c, 1,
885
XStdICCTextStyle, &text_prop);
886
887
if (status == Success || status > 0) {
888
XSetTextProperty(awt_display, xawt_root_window,
889
&text_prop, XA_WM_COMMAND);
890
if (text_prop.value != NULL)
891
XFree(text_prop.value);
892
}
893
JNU_ReleaseStringPlatformChars(env, jcommand, command);
894
}
895
AWT_UNLOCK();
896
}
897
898
899
/*
900
* New DT backdoor to set WM_COMMAND. New code should use this
901
* backdoor and call it *before* the first frame is shown so that
902
* gnome session proxy can correctly handle it.
903
*
904
* NB: The function name looks deceptively like a JNI native method
905
* name. It's not! It's just a plain function.
906
*/
907
JNIEXPORT void JNICALL
908
Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jarray)
909
{
910
jsize length;
911
char ** array;
912
XTextProperty text_prop;
913
int status;
914
Window xawt_root_window;
915
916
AWT_LOCK();
917
xawt_root_window = get_xawt_root_shell(env);
918
919
if (xawt_root_window == None) {
920
AWT_UNLOCK();
921
JNU_ThrowNullPointerException(env, "AWT root shell is unrealized");
922
return;
923
}
924
925
array = stringArrayToNative(env, jarray, &length);
926
927
if (array != NULL) {
928
status = XmbTextListToTextProperty(awt_display, array, length,
929
XStdICCTextStyle, &text_prop);
930
if (status < 0) {
931
switch (status) {
932
case XNoMemory:
933
JNU_ThrowOutOfMemoryError(env,
934
"XmbTextListToTextProperty: XNoMemory");
935
break;
936
case XLocaleNotSupported:
937
JNU_ThrowInternalError(env,
938
"XmbTextListToTextProperty: XLocaleNotSupported");
939
break;
940
case XConverterNotFound:
941
JNU_ThrowNullPointerException(env,
942
"XmbTextListToTextProperty: XConverterNotFound");
943
break;
944
default:
945
JNU_ThrowInternalError(env,
946
"XmbTextListToTextProperty: unknown error");
947
}
948
} else {
949
XSetTextProperty(awt_display, xawt_root_window,
950
&text_prop, XA_WM_COMMAND);
951
}
952
953
if (text_prop.value != NULL)
954
XFree(text_prop.value);
955
956
freeNativeStringArray(array, length);
957
}
958
AWT_UNLOCK();
959
}
960
961
/*
962
* Class: java_awt_TrayIcon
963
* Method: initIDs
964
* Signature: ()V
965
*/
966
JNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs(JNIEnv *env , jclass clazz)
967
{
968
}
969
970
971
/*
972
* Class: java_awt_Cursor
973
* Method: finalizeImpl
974
* Signature: ()V
975
*/
976
JNIEXPORT void JNICALL
977
Java_java_awt_Cursor_finalizeImpl(JNIEnv *env, jclass clazz, jlong pData)
978
{
979
Cursor xcursor;
980
981
xcursor = (Cursor)pData;
982
if (xcursor != None) {
983
AWT_LOCK();
984
XFreeCursor(awt_display, xcursor);
985
AWT_UNLOCK();
986
}
987
}
988
989
990
/*
991
* Class: sun_awt_X11_XToolkit
992
* Method: getNumberOfButtonsImpl
993
* Signature: ()I
994
*/
995
JNIEXPORT jint JNICALL Java_sun_awt_X11_XToolkit_getNumberOfButtonsImpl
996
(JNIEnv * env, jobject cls){
997
if (num_buttons == 0) {
998
num_buttons = getNumButtons();
999
}
1000
return num_buttons;
1001
}
1002
1003
int32_t getNumButtons() {
1004
int32_t major_opcode, first_event, first_error;
1005
int32_t xinputAvailable;
1006
int32_t numDevices, devIdx, clsIdx;
1007
XDeviceInfo* devices;
1008
XDeviceInfo* aDevice;
1009
XButtonInfo* bInfo;
1010
int32_t local_num_buttons = 0;
1011
1012
/* 4700242:
1013
* If XTest is asked to press a non-existant mouse button
1014
* (i.e. press Button3 on a system configured with a 2-button mouse),
1015
* then a crash may happen. To avoid this, we use the XInput
1016
* extension to query for the number of buttons on the XPointer, and check
1017
* before calling XTestFakeButtonEvent().
1018
*/
1019
xinputAvailable = XQueryExtension(awt_display, INAME, &major_opcode, &first_event, &first_error);
1020
if (xinputAvailable) {
1021
DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XINPUT) returns major_opcode = %d, first_event = %d, first_error = %d",
1022
major_opcode, first_event, first_error);
1023
devices = XListInputDevices(awt_display, &numDevices);
1024
for (devIdx = 0; devIdx < numDevices; devIdx++) {
1025
aDevice = &(devices[devIdx]);
1026
#ifdef IsXExtensionPointer
1027
if (aDevice->use == IsXExtensionPointer) {
1028
for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) {
1029
if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) {
1030
bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx]));
1031
local_num_buttons = bInfo->num_buttons;
1032
DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons);
1033
break;
1034
}
1035
}
1036
break;
1037
}
1038
#endif
1039
if (local_num_buttons <= 0 ) {
1040
if (aDevice->use == IsXPointer) {
1041
for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) {
1042
if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) {
1043
bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx]));
1044
local_num_buttons = bInfo->num_buttons;
1045
DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons);
1046
break;
1047
}
1048
}
1049
break;
1050
}
1051
}
1052
}
1053
1054
XFreeDeviceList(devices);
1055
}
1056
else {
1057
DTRACE_PRINTLN1("RobotPeer: XINPUT extension is unavailable, assuming %d mouse buttons", num_buttons);
1058
}
1059
if (local_num_buttons == 0 ) {
1060
local_num_buttons = 3;
1061
}
1062
1063
return local_num_buttons;
1064
}
1065
1066
/*
1067
* Class: sun_awt_X11_XWindowPeer
1068
* Method: getJvmPID
1069
* Signature: ()I
1070
*/
1071
JNIEXPORT jint JNICALL Java_sun_awt_X11_XWindowPeer_getJvmPID
1072
(JNIEnv *env, jclass cls)
1073
{
1074
/* Return the JVM's PID. */
1075
return getpid();
1076
}
1077
1078
#ifndef HOST_NAME_MAX
1079
#define HOST_NAME_MAX 1024 /* Overestimated */
1080
#endif
1081
1082
/*
1083
* Class: sun_awt_X11_XWindowPeer
1084
* Method: getLocalHostname
1085
* Signature: ()Ljava/lang/String;
1086
*/
1087
JNIEXPORT jstring JNICALL Java_sun_awt_X11_XWindowPeer_getLocalHostname
1088
(JNIEnv *env, jclass cls)
1089
{
1090
/* Return the machine's FQDN. */
1091
char hostname[HOST_NAME_MAX + 1];
1092
if (gethostname(hostname, HOST_NAME_MAX + 1) == 0) {
1093
hostname[HOST_NAME_MAX] = '\0';
1094
jstring res = (*env)->NewStringUTF(env, hostname);
1095
return res;
1096
}
1097
1098
return (jstring)NULL;
1099
}
1100
1101