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