2
|
1 |
/*
|
|
2 |
* Copyright 2002-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any 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_KeyboardFocusManager.h"
|
|
43 |
#include "awt_Font.h"
|
|
44 |
|
|
45 |
#include "sun_awt_X11_XToolkit.h"
|
|
46 |
#include "java_awt_SystemColor.h"
|
|
47 |
#include "java_awt_TrayIcon.h"
|
|
48 |
|
|
49 |
uint32_t awt_NumLockMask = 0;
|
|
50 |
Boolean awt_ModLockIsShiftLock = False;
|
|
51 |
|
|
52 |
extern JavaVM *jvm;
|
|
53 |
|
|
54 |
// Tracing level
|
|
55 |
static int tracing = 0;
|
|
56 |
#ifdef PRINT
|
|
57 |
#undef PRINT
|
|
58 |
#endif
|
|
59 |
#ifdef PRINT2
|
|
60 |
#undef PRINT2
|
|
61 |
#endif
|
|
62 |
|
|
63 |
#define PRINT if (tracing) printf
|
|
64 |
#define PRINT2 if (tracing > 1) printf
|
|
65 |
|
|
66 |
|
|
67 |
struct ComponentIDs componentIDs;
|
|
68 |
|
|
69 |
struct MenuComponentIDs menuComponentIDs;
|
|
70 |
|
|
71 |
struct KeyboardFocusManagerIDs keyboardFocusManagerIDs;
|
|
72 |
|
|
73 |
#ifndef HEADLESS
|
|
74 |
|
|
75 |
extern Display* awt_init_Display(JNIEnv *env, jobject this);
|
|
76 |
|
|
77 |
extern struct MFontPeerIDs mFontPeerIDs;
|
|
78 |
|
|
79 |
JNIEXPORT void JNICALL
|
|
80 |
Java_sun_awt_X11_XFontPeer_initIDs
|
|
81 |
(JNIEnv *env, jclass cls)
|
|
82 |
{
|
|
83 |
mFontPeerIDs.xfsname =
|
|
84 |
(*env)->GetFieldID(env, cls, "xfsname", "Ljava/lang/String;");
|
|
85 |
}
|
|
86 |
#endif /* !HEADLESS */
|
|
87 |
|
|
88 |
/* This function gets called from the static initializer for FileDialog.java
|
|
89 |
to initialize the fieldIDs for fields that may be accessed from C */
|
|
90 |
|
|
91 |
JNIEXPORT void JNICALL
|
|
92 |
Java_java_awt_FileDialog_initIDs
|
|
93 |
(JNIEnv *env, jclass cls)
|
|
94 |
{
|
|
95 |
|
|
96 |
}
|
|
97 |
|
|
98 |
JNIEXPORT void JNICALL
|
|
99 |
Java_sun_awt_X11_XToolkit_initIDs
|
|
100 |
(JNIEnv *env, jclass clazz)
|
|
101 |
{
|
|
102 |
jfieldID fid = (*env)->GetStaticFieldID(env, clazz, "numLockMask", "I");
|
|
103 |
awt_NumLockMask = (*env)->GetStaticIntField(env, clazz, fid);
|
|
104 |
DTRACE_PRINTLN1("awt_NumLockMask = %u", awt_NumLockMask);
|
|
105 |
fid = (*env)->GetStaticFieldID(env, clazz, "modLockIsShiftLock", "I");
|
|
106 |
awt_ModLockIsShiftLock = (*env)->GetStaticIntField(env, clazz, fid) != 0 ? True : False;
|
|
107 |
}
|
|
108 |
|
|
109 |
/*
|
|
110 |
* Class: sun_awt_X11_XToolkit
|
|
111 |
* Method: getDefaultXColormap
|
|
112 |
* Signature: ()J
|
|
113 |
*/
|
|
114 |
JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultXColormap
|
|
115 |
(JNIEnv *env, jclass clazz)
|
|
116 |
{
|
|
117 |
AwtGraphicsConfigDataPtr defaultConfig =
|
|
118 |
getDefaultConfig(DefaultScreen(awt_display));
|
|
119 |
|
|
120 |
return (jlong) defaultConfig->awt_cmap;
|
|
121 |
}
|
|
122 |
|
|
123 |
JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultScreenData
|
|
124 |
(JNIEnv *env, jclass clazz)
|
|
125 |
{
|
|
126 |
return ptr_to_jlong(getDefaultConfig(DefaultScreen(awt_display)));
|
|
127 |
}
|
|
128 |
|
|
129 |
|
|
130 |
JNIEXPORT jint JNICALL
|
|
131 |
JNI_OnLoad(JavaVM *vm, void *reserved)
|
|
132 |
{
|
|
133 |
jvm = vm;
|
|
134 |
return JNI_VERSION_1_2;
|
|
135 |
}
|
|
136 |
|
|
137 |
/*
|
|
138 |
* Class: sun_awt_X11_XToolkit
|
|
139 |
* Method: nativeLoadSystemColors
|
|
140 |
* Signature: ([I)V
|
|
141 |
*/
|
|
142 |
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_nativeLoadSystemColors
|
|
143 |
(JNIEnv *env, jobject this, jintArray systemColors)
|
|
144 |
{
|
|
145 |
AwtGraphicsConfigDataPtr defaultConfig =
|
|
146 |
getDefaultConfig(DefaultScreen(awt_display));
|
|
147 |
awtJNI_CreateColorData(env, defaultConfig, 1);
|
|
148 |
}
|
|
149 |
|
|
150 |
JNIEXPORT void JNICALL
|
|
151 |
Java_java_awt_Component_initIDs
|
|
152 |
(JNIEnv *env, jclass cls)
|
|
153 |
{
|
|
154 |
jclass keyclass = NULL;
|
|
155 |
|
|
156 |
|
|
157 |
componentIDs.x = (*env)->GetFieldID(env, cls, "x", "I");
|
|
158 |
componentIDs.y = (*env)->GetFieldID(env, cls, "y", "I");
|
|
159 |
componentIDs.width = (*env)->GetFieldID(env, cls, "width", "I");
|
|
160 |
componentIDs.height = (*env)->GetFieldID(env, cls, "height", "I");
|
|
161 |
componentIDs.isPacked = (*env)->GetFieldID(env, cls, "isPacked", "Z");
|
|
162 |
componentIDs.peer =
|
|
163 |
(*env)->GetFieldID(env, cls, "peer", "Ljava/awt/peer/ComponentPeer;");
|
|
164 |
componentIDs.background =
|
|
165 |
(*env)->GetFieldID(env, cls, "background", "Ljava/awt/Color;");
|
|
166 |
componentIDs.foreground =
|
|
167 |
(*env)->GetFieldID(env, cls, "foreground", "Ljava/awt/Color;");
|
|
168 |
componentIDs.graphicsConfig =
|
|
169 |
(*env)->GetFieldID(env, cls, "graphicsConfig",
|
|
170 |
"Ljava/awt/GraphicsConfiguration;");
|
|
171 |
componentIDs.name =
|
|
172 |
(*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");
|
|
173 |
|
|
174 |
/* Use _NoClientCode() methods for trusted methods, so that we
|
|
175 |
* know that we are not invoking client code on trusted threads
|
|
176 |
*/
|
|
177 |
componentIDs.getParent =
|
|
178 |
(*env)->GetMethodID(env, cls, "getParent_NoClientCode",
|
|
179 |
"()Ljava/awt/Container;");
|
|
180 |
|
|
181 |
componentIDs.getLocationOnScreen =
|
|
182 |
(*env)->GetMethodID(env, cls, "getLocationOnScreen_NoTreeLock",
|
|
183 |
"()Ljava/awt/Point;");
|
|
184 |
|
|
185 |
keyclass = (*env)->FindClass(env, "java/awt/event/KeyEvent");
|
|
186 |
DASSERT (keyclass != NULL);
|
|
187 |
|
|
188 |
componentIDs.isProxyActive =
|
|
189 |
(*env)->GetFieldID(env, keyclass, "isProxyActive",
|
|
190 |
"Z");
|
|
191 |
|
|
192 |
componentIDs.appContext =
|
|
193 |
(*env)->GetFieldID(env, cls, "appContext",
|
|
194 |
"Lsun/awt/AppContext;");
|
|
195 |
|
|
196 |
(*env)->DeleteLocalRef(env, keyclass);
|
|
197 |
}
|
|
198 |
|
|
199 |
|
|
200 |
JNIEXPORT void JNICALL
|
|
201 |
Java_java_awt_Container_initIDs
|
|
202 |
(JNIEnv *env, jclass cls)
|
|
203 |
{
|
|
204 |
|
|
205 |
}
|
|
206 |
|
|
207 |
|
|
208 |
JNIEXPORT void JNICALL
|
|
209 |
Java_java_awt_Button_initIDs
|
|
210 |
(JNIEnv *env, jclass cls)
|
|
211 |
{
|
|
212 |
|
|
213 |
}
|
|
214 |
|
|
215 |
JNIEXPORT void JNICALL
|
|
216 |
Java_java_awt_Scrollbar_initIDs
|
|
217 |
(JNIEnv *env, jclass cls)
|
|
218 |
{
|
|
219 |
|
|
220 |
}
|
|
221 |
|
|
222 |
|
|
223 |
JNIEXPORT void JNICALL
|
|
224 |
Java_java_awt_Window_initIDs
|
|
225 |
(JNIEnv *env, jclass cls)
|
|
226 |
{
|
|
227 |
|
|
228 |
}
|
|
229 |
|
|
230 |
JNIEXPORT void JNICALL
|
|
231 |
Java_java_awt_Frame_initIDs
|
|
232 |
(JNIEnv *env, jclass cls)
|
|
233 |
{
|
|
234 |
|
|
235 |
}
|
|
236 |
|
|
237 |
|
|
238 |
JNIEXPORT void JNICALL
|
|
239 |
Java_java_awt_MenuComponent_initIDs(JNIEnv *env, jclass cls)
|
|
240 |
{
|
|
241 |
menuComponentIDs.appContext =
|
|
242 |
(*env)->GetFieldID(env, cls, "appContext", "Lsun/awt/AppContext;");
|
|
243 |
}
|
|
244 |
|
|
245 |
JNIEXPORT void JNICALL
|
|
246 |
Java_java_awt_Cursor_initIDs(JNIEnv *env, jclass cls)
|
|
247 |
{
|
|
248 |
}
|
|
249 |
|
|
250 |
|
|
251 |
JNIEXPORT void JNICALL Java_java_awt_MenuItem_initIDs
|
|
252 |
(JNIEnv *env, jclass cls)
|
|
253 |
{
|
|
254 |
}
|
|
255 |
|
|
256 |
|
|
257 |
JNIEXPORT void JNICALL Java_java_awt_Menu_initIDs
|
|
258 |
(JNIEnv *env, jclass cls)
|
|
259 |
{
|
|
260 |
}
|
|
261 |
|
|
262 |
JNIEXPORT void JNICALL
|
|
263 |
Java_java_awt_TextArea_initIDs
|
|
264 |
(JNIEnv *env, jclass cls)
|
|
265 |
{
|
|
266 |
}
|
|
267 |
|
|
268 |
|
|
269 |
JNIEXPORT void JNICALL
|
|
270 |
Java_java_awt_Checkbox_initIDs
|
|
271 |
(JNIEnv *env, jclass cls)
|
|
272 |
{
|
|
273 |
}
|
|
274 |
|
|
275 |
|
|
276 |
JNIEXPORT void JNICALL Java_java_awt_ScrollPane_initIDs
|
|
277 |
(JNIEnv *env, jclass cls)
|
|
278 |
{
|
|
279 |
}
|
|
280 |
|
|
281 |
JNIEXPORT void JNICALL
|
|
282 |
Java_java_awt_TextField_initIDs
|
|
283 |
(JNIEnv *env, jclass cls)
|
|
284 |
{
|
|
285 |
}
|
|
286 |
|
|
287 |
JNIEXPORT jboolean JNICALL AWTIsHeadless() {
|
|
288 |
#ifdef HEADLESS
|
|
289 |
return JNI_TRUE;
|
|
290 |
#else
|
|
291 |
return JNI_FALSE;
|
|
292 |
#endif
|
|
293 |
}
|
|
294 |
|
|
295 |
JNIEXPORT void JNICALL Java_java_awt_Dialog_initIDs (JNIEnv *env, jclass cls)
|
|
296 |
{
|
|
297 |
}
|
|
298 |
|
|
299 |
|
|
300 |
/* ========================== Begin poll section ================================ */
|
|
301 |
|
|
302 |
// Includes
|
|
303 |
|
|
304 |
#include <sys/time.h>
|
|
305 |
#include <limits.h>
|
|
306 |
#include <locale.h>
|
|
307 |
#include <pthread.h>
|
|
308 |
|
|
309 |
#include <dlfcn.h>
|
|
310 |
#include <fcntl.h>
|
|
311 |
|
|
312 |
#include <poll.h>
|
|
313 |
#ifndef POLLRDNORM
|
|
314 |
#define POLLRDNORM POLLIN
|
|
315 |
#endif
|
|
316 |
|
|
317 |
// Prototypes
|
|
318 |
|
|
319 |
static void waitForEvents(JNIEnv *, jlong);
|
|
320 |
static void awt_pipe_init();
|
|
321 |
static void processOneEvent(XtInputMask iMask);
|
|
322 |
static void performPoll(JNIEnv *, jlong);
|
|
323 |
static void wakeUp();
|
|
324 |
static void update_poll_timeout(int timeout_control);
|
|
325 |
static uint32_t get_poll_timeout(jlong nextTaskTime);
|
|
326 |
|
|
327 |
// Defines
|
|
328 |
|
|
329 |
#ifndef bzero
|
|
330 |
#define bzero(a,b) memset(a, 0, b)
|
|
331 |
#endif
|
|
332 |
|
|
333 |
#define AWT_POLL_BUFSIZE 100 /* bytes */
|
|
334 |
#define AWT_READPIPE (awt_pipe_fds[0])
|
|
335 |
#define AWT_WRITEPIPE (awt_pipe_fds[1])
|
|
336 |
|
|
337 |
#define DEF_AWT_MAX_POLL_TIMEOUT ((uint32_t)500) /* milliseconds */
|
|
338 |
#define DEF_AWT_FLUSH_TIMEOUT ((uint32_t)100) /* milliseconds */
|
|
339 |
#define AWT_MIN_POLL_TIMEOUT ((uint32_t)0) /* milliseconds */
|
|
340 |
|
|
341 |
#define TIMEOUT_TIMEDOUT 0
|
|
342 |
#define TIMEOUT_EVENTS 1
|
|
343 |
|
|
344 |
// Static fields
|
|
345 |
|
|
346 |
static uint32_t AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT; /* milliseconds */
|
|
347 |
static uint32_t AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT; /* milliseconds */
|
|
348 |
static pthread_t awt_MainThread = 0;
|
|
349 |
static int32_t awt_pipe_fds[2]; /* fds for wkaeup pipe */
|
|
350 |
static Boolean awt_pipe_inited = False; /* make sure pipe is initialized before write */
|
|
351 |
static jlong awt_next_flush_time = 0LL; /* 0 == no scheduled flush */
|
|
352 |
static jlong awt_last_flush_time = 0LL; /* 0 == no scheduled flush */
|
|
353 |
static uint32_t curPollTimeout;
|
|
354 |
static struct pollfd pollFds[2];
|
|
355 |
static jlong poll_sleep_time = 0LL; // Used for tracing
|
|
356 |
static jlong poll_wakeup_time = 0LL; // Used for tracing
|
|
357 |
|
|
358 |
// AWT static poll timeout. Zero means "not set", aging algorithm is
|
|
359 |
// used. Static poll timeout values higher than 50 cause application
|
|
360 |
// look "slow" - they don't respond to user request fast
|
|
361 |
// enough. Static poll timeout value less than 10 are usually
|
|
362 |
// considered by schedulers as zero, so this might cause unnecessary
|
|
363 |
// CPU consumption by Java. The values between 10 - 50 are suggested
|
|
364 |
// for single client desktop configurations. For SunRay servers, it
|
|
365 |
// is highly recomended to use aging algorithm (set static poll timeout
|
|
366 |
// to 0).
|
|
367 |
static int32_t static_poll_timeout = 0;
|
|
368 |
|
|
369 |
static Bool isMainThread() {
|
|
370 |
return awt_MainThread == pthread_self();
|
|
371 |
}
|
|
372 |
|
|
373 |
/*
|
|
374 |
* Creates the AWT utility pipe. This pipe exists solely so that
|
|
375 |
* we can cause the main event thread to wake up from a poll() or
|
|
376 |
* select() by writing to this pipe.
|
|
377 |
*/
|
|
378 |
static void
|
|
379 |
awt_pipe_init() {
|
|
380 |
|
|
381 |
if (awt_pipe_inited) {
|
|
382 |
return;
|
|
383 |
}
|
|
384 |
|
|
385 |
if ( pipe ( awt_pipe_fds ) == 0 )
|
|
386 |
{
|
|
387 |
/*
|
|
388 |
** the write wakes us up from the infinite sleep, which
|
|
389 |
** then we cause a delay of AWT_FLUSHTIME and then we
|
|
390 |
** flush.
|
|
391 |
*/
|
|
392 |
int32_t flags = 0;
|
|
393 |
/* set the pipe to be non-blocking */
|
|
394 |
flags = fcntl ( AWT_READPIPE, F_GETFL, 0 );
|
|
395 |
fcntl( AWT_READPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );
|
|
396 |
flags = fcntl ( AWT_WRITEPIPE, F_GETFL, 0 );
|
|
397 |
fcntl( AWT_WRITEPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );
|
|
398 |
awt_pipe_inited = True;
|
|
399 |
}
|
|
400 |
else
|
|
401 |
{
|
|
402 |
AWT_READPIPE = -1;
|
|
403 |
AWT_WRITEPIPE = -1;
|
|
404 |
}
|
|
405 |
|
|
406 |
|
|
407 |
} /* awt_pipe_init() */
|
|
408 |
|
|
409 |
/**
|
|
410 |
* Reads environment variables to initialize timeout fields.
|
|
411 |
*/
|
|
412 |
static void readEnv() {
|
|
413 |
char * value;
|
|
414 |
static Boolean env_read = False;
|
|
415 |
if (env_read) return;
|
|
416 |
|
|
417 |
env_read = True;
|
|
418 |
|
|
419 |
value = getenv("_AWT_MAX_POLL_TIMEOUT");
|
|
420 |
if (value != NULL) {
|
|
421 |
AWT_MAX_POLL_TIMEOUT = atoi(value);
|
|
422 |
if (AWT_MAX_POLL_TIMEOUT == 0) {
|
|
423 |
AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT;
|
|
424 |
}
|
|
425 |
}
|
|
426 |
curPollTimeout = AWT_MAX_POLL_TIMEOUT/2;
|
|
427 |
|
|
428 |
value = getenv("_AWT_FLUSH_TIMEOUT");
|
|
429 |
if (value != NULL) {
|
|
430 |
AWT_FLUSH_TIMEOUT = atoi(value);
|
|
431 |
if (AWT_FLUSH_TIMEOUT == 0) {
|
|
432 |
AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT;
|
|
433 |
}
|
|
434 |
}
|
|
435 |
|
|
436 |
value = getenv("_AWT_POLL_TRACING");
|
|
437 |
if (value != NULL) {
|
|
438 |
tracing = atoi(value);
|
|
439 |
}
|
|
440 |
|
|
441 |
value = getenv("_AWT_STATIC_POLL_TIMEOUT");
|
|
442 |
if (value != NULL) {
|
|
443 |
static_poll_timeout = atoi(value);
|
|
444 |
}
|
|
445 |
if (static_poll_timeout != 0) {
|
|
446 |
curPollTimeout = static_poll_timeout;
|
|
447 |
}
|
|
448 |
}
|
|
449 |
|
|
450 |
/**
|
|
451 |
* Returns the amount of milliseconds similar to System.currentTimeMillis()
|
|
452 |
*/
|
|
453 |
static jlong
|
|
454 |
awtJNI_TimeMillis(void)
|
|
455 |
{
|
|
456 |
struct timeval t;
|
|
457 |
|
|
458 |
gettimeofday(&t, 0);
|
|
459 |
|
|
460 |
return jlong_add(jlong_mul(jint_to_jlong(t.tv_sec), jint_to_jlong(1000)),
|
|
461 |
jint_to_jlong(t.tv_usec / 1000));
|
|
462 |
}
|
|
463 |
|
|
464 |
/**
|
|
465 |
* Updates curPollTimeout according to the aging algorithm.
|
|
466 |
* @param timeout_control Either TIMEOUT_TIMEDOUT or TIMEOUT_EVENTS
|
|
467 |
*/
|
|
468 |
static void update_poll_timeout(int timeout_control) {
|
|
469 |
PRINT2("tout: %d\n", timeout_control);
|
|
470 |
|
|
471 |
// If static_poll_timeout is set, curPollTimeout has the fixed value
|
|
472 |
if (static_poll_timeout != 0) return;
|
|
473 |
|
|
474 |
// Update it otherwise
|
|
475 |
if (timeout_control == TIMEOUT_TIMEDOUT) {
|
|
476 |
/* add 1/4 (plus 1, in case the division truncates to 0) */
|
|
477 |
curPollTimeout += ((curPollTimeout>>2) + 1);
|
|
478 |
curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout);
|
|
479 |
} else if (timeout_control == TIMEOUT_EVENTS) {
|
|
480 |
/* subtract 1/4 (plus 1, in case the division truncates to 0) */
|
|
481 |
curPollTimeout -= ((curPollTimeout>>2) + 1);
|
|
482 |
curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, curPollTimeout);
|
|
483 |
}
|
|
484 |
}
|
|
485 |
|
|
486 |
/*
|
|
487 |
* Gets the best timeout for the next call to poll().
|
|
488 |
*
|
|
489 |
* @param nextTaskTime -1, if there are no tasks; next time when
|
|
490 |
* timeout task needs to be run, in millis(of currentTimeMillis)
|
|
491 |
*/
|
|
492 |
static uint32_t get_poll_timeout(jlong nextTaskTime)
|
|
493 |
{
|
|
494 |
jlong curTime = awtJNI_TimeMillis();
|
|
495 |
uint32_t timeout = curPollTimeout;
|
|
496 |
uint32_t taskTimeout = (nextTaskTime == -1) ? AWT_MAX_POLL_TIMEOUT : (uint32_t)max(0, (int32_t)(nextTaskTime - curTime));
|
|
497 |
uint32_t flushTimeout = (awt_next_flush_time > 0) ? (uint32_t)max(0, (int32_t)(awt_next_flush_time - curTime)) : AWT_MAX_POLL_TIMEOUT;
|
|
498 |
|
|
499 |
PRINT2("to: %d, ft: %d, to: %d, tt: %d, mil: %d\n", taskTimeout, flushTimeout, timeout, (int)nextTaskTime, (int)curTime);
|
|
500 |
|
|
501 |
// Adjust timeout to flush_time and task_time
|
|
502 |
return min(flushTimeout, min(taskTimeout, timeout));
|
|
503 |
} /* awt_get_poll_timeout() */
|
|
504 |
|
|
505 |
/*
|
|
506 |
* Waits for X/Xt events to appear on the pipe. Returns only when
|
|
507 |
* it is likely (but not definite) that there are events waiting to
|
|
508 |
* be processed.
|
|
509 |
*
|
|
510 |
* This routine also flushes the outgoing X queue, when the
|
|
511 |
* awt_next_flush_time has been reached.
|
|
512 |
*
|
|
513 |
* If fdAWTPipe is greater or equal than zero the routine also
|
|
514 |
* checks if there are events pending on the putback queue.
|
|
515 |
*/
|
|
516 |
void
|
|
517 |
waitForEvents(JNIEnv *env, jlong nextTaskTime) {
|
|
518 |
performPoll(env, nextTaskTime);
|
|
519 |
if ((awt_next_flush_time > 0) && (awtJNI_TimeMillis() >= awt_next_flush_time)) {
|
|
520 |
XFlush(awt_display);
|
|
521 |
awt_last_flush_time = awt_next_flush_time;
|
|
522 |
awt_next_flush_time = 0LL;
|
|
523 |
}
|
|
524 |
} /* waitForEvents() */
|
|
525 |
|
|
526 |
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_waitForEvents (JNIEnv *env, jclass class, jlong nextTaskTime) {
|
|
527 |
waitForEvents(env, nextTaskTime);
|
|
528 |
}
|
|
529 |
|
|
530 |
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1toolkit_1init (JNIEnv *env, jclass class) {
|
|
531 |
awt_MainThread = pthread_self();
|
|
532 |
|
|
533 |
awt_pipe_init();
|
|
534 |
readEnv();
|
|
535 |
}
|
|
536 |
|
|
537 |
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1output_1flush (JNIEnv *env, jclass class) {
|
|
538 |
awt_output_flush();
|
|
539 |
}
|
|
540 |
|
|
541 |
JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_wakeup_1poll (JNIEnv *env, jclass class) {
|
|
542 |
wakeUp();
|
|
543 |
}
|
|
544 |
|
|
545 |
/*
|
|
546 |
* Polls both the X pipe and our AWT utility pipe. Returns
|
|
547 |
* when there is data on one of the pipes, or the operation times
|
|
548 |
* out.
|
|
549 |
*
|
|
550 |
* Not all Xt events come across the X pipe (e.g., timers
|
|
551 |
* and alternate inputs), so we must time out every now and
|
|
552 |
* then to check the Xt event queue.
|
|
553 |
*
|
|
554 |
* The fdAWTPipe will be empty when this returns.
|
|
555 |
*/
|
|
556 |
static void
|
|
557 |
performPoll(JNIEnv *env, jlong nextTaskTime) {
|
|
558 |
static Bool pollFdsInited = False;
|
|
559 |
static char read_buf[AWT_POLL_BUFSIZE + 1]; /* dummy buf to empty pipe */
|
|
560 |
|
|
561 |
uint32_t timeout = get_poll_timeout(nextTaskTime);
|
|
562 |
int32_t result;
|
|
563 |
|
|
564 |
if (!pollFdsInited) {
|
|
565 |
pollFds[0].fd = ConnectionNumber(awt_display);
|
|
566 |
pollFds[0].events = POLLRDNORM;
|
|
567 |
pollFds[0].revents = 0;
|
|
568 |
|
|
569 |
pollFds[1].fd = AWT_READPIPE;
|
|
570 |
pollFds[1].events = POLLRDNORM;
|
|
571 |
pollFds[1].revents = 0;
|
|
572 |
pollFdsInited = True;
|
|
573 |
} else {
|
|
574 |
pollFds[0].revents = 0;
|
|
575 |
pollFds[1].revents = 0;
|
|
576 |
}
|
|
577 |
|
|
578 |
AWT_NOFLUSH_UNLOCK();
|
|
579 |
|
|
580 |
/* ACTUALLY DO THE POLL() */
|
|
581 |
if (timeout == 0) {
|
|
582 |
// be sure other threads get a chance
|
|
583 |
awtJNI_ThreadYield(env);
|
|
584 |
}
|
|
585 |
|
|
586 |
if (tracing) poll_sleep_time = awtJNI_TimeMillis();
|
|
587 |
result = poll( pollFds, 2, (int32_t) timeout );
|
|
588 |
if (tracing) poll_wakeup_time = awtJNI_TimeMillis();
|
|
589 |
PRINT("%d of %d, res: %d\n", (int)(poll_wakeup_time-poll_sleep_time), (int)timeout, result);
|
|
590 |
|
|
591 |
AWT_LOCK();
|
|
592 |
if (result == 0) {
|
|
593 |
/* poll() timed out -- update timeout value */
|
|
594 |
update_poll_timeout(TIMEOUT_TIMEDOUT);
|
|
595 |
}
|
|
596 |
if (pollFds[1].revents) {
|
|
597 |
int count;
|
|
598 |
PRINT("Woke up\n");
|
|
599 |
/* There is data on the AWT pipe - empty it */
|
|
600 |
do {
|
|
601 |
count = read(AWT_READPIPE, read_buf, AWT_POLL_BUFSIZE );
|
|
602 |
} while (count == AWT_POLL_BUFSIZE );
|
|
603 |
}
|
|
604 |
if (pollFds[0].revents) {
|
|
605 |
// Events in X pipe
|
|
606 |
update_poll_timeout(TIMEOUT_EVENTS);
|
|
607 |
}
|
|
608 |
return;
|
|
609 |
|
|
610 |
} /* performPoll() */
|
|
611 |
|
|
612 |
/**
|
|
613 |
* Schedules next auto-flush event or performs forced flush depending
|
|
614 |
* on the time of the previous flush.
|
|
615 |
*/
|
|
616 |
void awt_output_flush() {
|
|
617 |
if (awt_next_flush_time == 0) {
|
|
618 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
|
|
619 |
|
|
620 |
jlong curTime = awtJNI_TimeMillis(); // current time
|
|
621 |
jlong l_awt_last_flush_time = awt_last_flush_time; // last time we flushed queue
|
|
622 |
jlong next_flush_time = l_awt_last_flush_time + AWT_FLUSH_TIMEOUT;
|
|
623 |
|
|
624 |
if (curTime >= next_flush_time) {
|
|
625 |
// Enough time passed from last flush
|
|
626 |
PRINT("f1\n");
|
|
627 |
AWT_LOCK();
|
|
628 |
XFlush(awt_display);
|
|
629 |
awt_last_flush_time = curTime;
|
|
630 |
AWT_NOFLUSH_UNLOCK();
|
|
631 |
} else {
|
|
632 |
awt_next_flush_time = next_flush_time;
|
|
633 |
PRINT("f2\n");
|
|
634 |
wakeUp();
|
|
635 |
}
|
|
636 |
}
|
|
637 |
}
|
|
638 |
|
|
639 |
|
|
640 |
/**
|
|
641 |
* Wakes-up poll() in performPoll
|
|
642 |
*/
|
|
643 |
static void wakeUp() {
|
|
644 |
static char wakeUp_char = 'p';
|
|
645 |
if (!isMainThread() && awt_pipe_inited) {
|
|
646 |
write ( AWT_WRITEPIPE, &wakeUp_char, 1 );
|
|
647 |
}
|
|
648 |
}
|
|
649 |
|
|
650 |
|
|
651 |
/* ========================== End poll section ================================= */
|
|
652 |
|
|
653 |
/*
|
|
654 |
* Class: java_awt_KeyboardFocusManager
|
|
655 |
* Method: initIDs
|
|
656 |
* Signature: ()V
|
|
657 |
*/
|
|
658 |
JNIEXPORT void JNICALL
|
|
659 |
Java_java_awt_KeyboardFocusManager_initIDs
|
|
660 |
(JNIEnv *env, jclass cls)
|
|
661 |
{
|
|
662 |
}
|
|
663 |
|
|
664 |
/*
|
|
665 |
* Class: sun_awt_X11_XToolkit
|
|
666 |
* Method: getEnv
|
|
667 |
* Signature: (Ljava/lang/String;)Ljava/lang/String;
|
|
668 |
*/
|
|
669 |
JNIEXPORT jstring JNICALL Java_sun_awt_X11_XToolkit_getEnv
|
|
670 |
(JNIEnv *env , jclass clazz, jstring key) {
|
|
671 |
char *ptr = NULL;
|
|
672 |
const char *keystr = NULL;
|
|
673 |
jstring ret = NULL;
|
|
674 |
|
|
675 |
keystr = JNU_GetStringPlatformChars(env, key, NULL);
|
|
676 |
if (key) {
|
|
677 |
ptr = getenv(keystr);
|
|
678 |
if (ptr) {
|
|
679 |
ret = JNU_NewStringPlatform(env, (const char *) ptr);
|
|
680 |
}
|
|
681 |
JNU_ReleaseStringPlatformChars(env, key, (const char*)keystr);
|
|
682 |
}
|
|
683 |
return ret;
|
|
684 |
}
|
|
685 |
|
|
686 |
#ifdef __linux__
|
|
687 |
void print_stack(void)
|
|
688 |
{
|
|
689 |
void *array[10];
|
|
690 |
size_t size;
|
|
691 |
char **strings;
|
|
692 |
size_t i;
|
|
693 |
|
|
694 |
size = backtrace (array, 10);
|
|
695 |
strings = backtrace_symbols (array, size);
|
|
696 |
|
|
697 |
fprintf (stderr, "Obtained %zd stack frames.\n", size);
|
|
698 |
|
|
699 |
for (i = 0; i < size; i++)
|
|
700 |
fprintf (stderr, "%s\n", strings[i]);
|
|
701 |
|
|
702 |
free (strings);
|
|
703 |
}
|
|
704 |
#endif
|
|
705 |
|
|
706 |
Window get_xawt_root_shell(JNIEnv *env) {
|
|
707 |
static jclass classXRootWindow = NULL;
|
|
708 |
static jmethodID methodGetXRootWindow = NULL;
|
|
709 |
static Window xawt_root_shell = None;
|
|
710 |
|
|
711 |
if (xawt_root_shell == None){
|
|
712 |
if (classXRootWindow == NULL){
|
|
713 |
jclass cls_tmp = (*env)->FindClass(env, "sun/awt/X11/XRootWindow");
|
|
714 |
classXRootWindow = (jclass)(*env)->NewGlobalRef(env, cls_tmp);
|
|
715 |
(*env)->DeleteLocalRef(env, cls_tmp);
|
|
716 |
}
|
|
717 |
if( classXRootWindow != NULL) {
|
|
718 |
methodGetXRootWindow = (*env)->GetStaticMethodID(env, classXRootWindow, "getXRootWindow", "()J");
|
|
719 |
}
|
|
720 |
if( classXRootWindow != NULL && methodGetXRootWindow !=NULL ) {
|
|
721 |
xawt_root_shell = (Window) (*env)->CallStaticLongMethod(env, classXRootWindow, methodGetXRootWindow);
|
|
722 |
}
|
|
723 |
if ((*env)->ExceptionCheck(env)) {
|
|
724 |
(*env)->ExceptionDescribe(env);
|
|
725 |
(*env)->ExceptionClear(env);
|
|
726 |
}
|
|
727 |
}
|
|
728 |
return xawt_root_shell;
|
|
729 |
}
|
|
730 |
|
|
731 |
/*
|
|
732 |
* Old, compatibility, backdoor for DT. This is a different
|
|
733 |
* implementation. It keeps the signature, but acts on
|
|
734 |
* awt_root_shell, not the frame passed as an argument. Note, that
|
|
735 |
* the code that uses the old backdoor doesn't work correctly with
|
|
736 |
* gnome session proxy that checks for WM_COMMAND when the window is
|
|
737 |
* firts mapped, because DT code calls this old backdoor *after* the
|
|
738 |
* frame is shown or it would get NPE with old AWT (previous
|
|
739 |
* implementation of this backdoor) otherwise. Old style session
|
|
740 |
* managers (e.g. CDE) that check WM_COMMAND only during session
|
|
741 |
* checkpoint should work fine, though.
|
|
742 |
*
|
|
743 |
* NB: The function name looks deceptively like a JNI native method
|
|
744 |
* name. It's not! It's just a plain function.
|
|
745 |
*/
|
|
746 |
|
|
747 |
JNIEXPORT void JNICALL
|
|
748 |
Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this,
|
|
749 |
jobject frame, jstring jcommand)
|
|
750 |
{
|
|
751 |
const char *command;
|
|
752 |
XTextProperty text_prop;
|
|
753 |
char *c[1];
|
|
754 |
int32_t status;
|
|
755 |
Window xawt_root_window;
|
|
756 |
|
|
757 |
AWT_LOCK();
|
|
758 |
xawt_root_window = get_xawt_root_shell(env);
|
|
759 |
|
|
760 |
if ( xawt_root_window == None ) {
|
|
761 |
JNU_ThrowNullPointerException(env, "AWT root shell is unrealized");
|
|
762 |
AWT_UNLOCK();
|
|
763 |
return;
|
|
764 |
}
|
|
765 |
|
|
766 |
command = (char *) JNU_GetStringPlatformChars(env, jcommand, NULL);
|
|
767 |
c[0] = (char *)command;
|
|
768 |
status = XmbTextListToTextProperty(awt_display, c, 1,
|
|
769 |
XStdICCTextStyle, &text_prop);
|
|
770 |
|
|
771 |
if (status == Success || status > 0) {
|
|
772 |
XSetTextProperty(awt_display, xawt_root_window,
|
|
773 |
&text_prop, XA_WM_COMMAND);
|
|
774 |
if (text_prop.value != NULL)
|
|
775 |
XFree(text_prop.value);
|
|
776 |
}
|
|
777 |
JNU_ReleaseStringPlatformChars(env, jcommand, command);
|
|
778 |
AWT_UNLOCK();
|
|
779 |
}
|
|
780 |
|
|
781 |
|
|
782 |
/*
|
|
783 |
* New DT backdoor to set WM_COMMAND. New code should use this
|
|
784 |
* backdoor and call it *before* the first frame is shown so that
|
|
785 |
* gnome session proxy can correctly handle it.
|
|
786 |
*
|
|
787 |
* NB: The function name looks deceptively like a JNI native method
|
|
788 |
* name. It's not! It's just a plain function.
|
|
789 |
*/
|
|
790 |
JNIEXPORT void JNICALL
|
|
791 |
Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv)
|
|
792 |
{
|
|
793 |
static const char empty[] = "";
|
|
794 |
|
|
795 |
int argc;
|
|
796 |
const char **cargv;
|
|
797 |
XTextProperty text_prop;
|
|
798 |
int status;
|
|
799 |
int i;
|
|
800 |
Window xawt_root_window;
|
|
801 |
|
|
802 |
AWT_LOCK();
|
|
803 |
xawt_root_window = get_xawt_root_shell(env);
|
|
804 |
|
|
805 |
if (xawt_root_window == None) {
|
|
806 |
JNU_ThrowNullPointerException(env, "AWT root shell is unrealized");
|
|
807 |
AWT_UNLOCK();
|
|
808 |
return;
|
|
809 |
}
|
|
810 |
|
|
811 |
argc = (int)(*env)->GetArrayLength(env, jargv);
|
|
812 |
if (argc == 0) {
|
|
813 |
AWT_UNLOCK();
|
|
814 |
return;
|
|
815 |
}
|
|
816 |
|
|
817 |
/* array of C strings */
|
|
818 |
cargv = (const char **)calloc(argc, sizeof(char *));
|
|
819 |
if (cargv == NULL) {
|
|
820 |
JNU_ThrowOutOfMemoryError(env, "Unable to allocate cargv");
|
|
821 |
AWT_UNLOCK();
|
|
822 |
return;
|
|
823 |
}
|
|
824 |
|
|
825 |
/* fill C array with platform chars of java strings */
|
|
826 |
for (i = 0; i < argc; ++i) {
|
|
827 |
jstring js;
|
|
828 |
const char *cs;
|
|
829 |
|
|
830 |
cs = NULL;
|
|
831 |
js = (*env)->GetObjectArrayElement(env, jargv, i);
|
|
832 |
if (js != NULL) {
|
|
833 |
cs = JNU_GetStringPlatformChars(env, js, NULL);
|
|
834 |
}
|
|
835 |
if (cs == NULL) {
|
|
836 |
cs = empty;
|
|
837 |
}
|
|
838 |
cargv[i] = cs;
|
|
839 |
(*env)->DeleteLocalRef(env, js);
|
|
840 |
}
|
|
841 |
|
|
842 |
/* grr, X prototype doesn't declare cargv as const, thought it really is */
|
|
843 |
status = XmbTextListToTextProperty(awt_display, (char **)cargv, argc,
|
|
844 |
XStdICCTextStyle, &text_prop);
|
|
845 |
if (status < 0) {
|
|
846 |
switch (status) {
|
|
847 |
case XNoMemory:
|
|
848 |
JNU_ThrowOutOfMemoryError(env,
|
|
849 |
"XmbTextListToTextProperty: XNoMemory");
|
|
850 |
break;
|
|
851 |
case XLocaleNotSupported:
|
|
852 |
JNU_ThrowInternalError(env,
|
|
853 |
"XmbTextListToTextProperty: XLocaleNotSupported");
|
|
854 |
break;
|
|
855 |
case XConverterNotFound:
|
|
856 |
JNU_ThrowNullPointerException(env,
|
|
857 |
"XmbTextListToTextProperty: XConverterNotFound");
|
|
858 |
break;
|
|
859 |
default:
|
|
860 |
JNU_ThrowInternalError(env,
|
|
861 |
"XmbTextListToTextProperty: unknown error");
|
|
862 |
}
|
|
863 |
} else {
|
|
864 |
|
|
865 |
XSetTextProperty(awt_display, xawt_root_window,
|
|
866 |
&text_prop, XA_WM_COMMAND);
|
|
867 |
}
|
|
868 |
|
|
869 |
for (i = 0; i < argc; ++i) {
|
|
870 |
jstring js;
|
|
871 |
|
|
872 |
if (cargv[i] == empty)
|
|
873 |
continue;
|
|
874 |
|
|
875 |
js = (*env)->GetObjectArrayElement(env, jargv, i);
|
|
876 |
JNU_ReleaseStringPlatformChars(env, js, cargv[i]);
|
|
877 |
(*env)->DeleteLocalRef(env, js);
|
|
878 |
}
|
|
879 |
if (text_prop.value != NULL)
|
|
880 |
XFree(text_prop.value);
|
|
881 |
AWT_UNLOCK();
|
|
882 |
}
|
|
883 |
|
|
884 |
/*
|
|
885 |
* Class: java_awt_TrayIcon
|
|
886 |
* Method: initIDs
|
|
887 |
* Signature: ()V
|
|
888 |
*/
|
|
889 |
JNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs(JNIEnv *env , jclass clazz)
|
|
890 |
{
|
|
891 |
}
|
|
892 |
|
|
893 |
|
|
894 |
/*
|
|
895 |
* Class: java_awt_Cursor
|
|
896 |
* Method: finalizeImpl
|
|
897 |
* Signature: ()V
|
|
898 |
*/
|
|
899 |
JNIEXPORT void JNICALL
|
|
900 |
Java_java_awt_Cursor_finalizeImpl(JNIEnv *env, jclass clazz, jlong pData)
|
|
901 |
{
|
|
902 |
Cursor xcursor;
|
|
903 |
|
|
904 |
xcursor = (Cursor)pData;
|
|
905 |
if (xcursor != None) {
|
|
906 |
AWT_LOCK();
|
|
907 |
XFreeCursor(awt_display, xcursor);
|
|
908 |
AWT_UNLOCK();
|
|
909 |
}
|
|
910 |
}
|