author | yan |
Thu, 19 Jun 2008 11:26:54 +0400 | |
changeset 1175 | 026b52c440fe |
parent 2 | 90ce3da70b43 |
child 1962 | 6c293d33645b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1999-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 |
#ifdef HEADLESS |
|
27 |
#error This file should not be included in headless library |
|
28 |
#endif |
|
29 |
||
30 |
#include "awt_p.h" |
|
31 |
#include "awt_Component.h" |
|
32 |
#include "awt_GraphicsEnv.h" |
|
33 |
#define XK_MISCELLANY |
|
34 |
#include <X11/keysymdef.h> |
|
35 |
#include <X11/Intrinsic.h> |
|
36 |
#include <X11/Xutil.h> |
|
37 |
#include <X11/Xmd.h> |
|
38 |
#include <X11/extensions/xtestext1.h> |
|
39 |
#include <X11/extensions/XTest.h> |
|
40 |
#include <X11/extensions/XInput.h> |
|
41 |
#include <X11/extensions/XI.h> |
|
42 |
#include <jni.h> |
|
43 |
#include "robot_common.h" |
|
44 |
#include "canvas.h" |
|
45 |
#include "wsutils.h" |
|
46 |
#include "list.h" |
|
47 |
#include "multiVis.h" |
|
48 |
#ifdef __linux__ |
|
49 |
#include <sys/socket.h> |
|
50 |
#endif |
|
51 |
||
52 |
extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs; |
|
53 |
||
54 |
// 2 would be more correct, however that's how Robot originally worked |
|
55 |
// and tests start to fail if this value is changed |
|
56 |
static int32_t num_buttons = 3; |
|
57 |
||
58 |
static int32_t isXTestAvailable() { |
|
59 |
int32_t major_opcode, first_event, first_error; |
|
60 |
int32_t event_basep, error_basep, majorp, minorp; |
|
61 |
int32_t isXTestAvailable; |
|
62 |
||
63 |
/* check if XTest is available */ |
|
64 |
isXTestAvailable = XQueryExtension(awt_display, XTestExtensionName, &major_opcode, &first_event, &first_error); |
|
65 |
DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XTEST) returns major_opcode = %d, first_event = %d, first_error = %d", |
|
66 |
major_opcode, first_event, first_error); |
|
67 |
if (isXTestAvailable) { |
|
68 |
/* check if XTest version is OK */ |
|
69 |
XTestQueryExtension(awt_display, &event_basep, &error_basep, &majorp, &minorp); |
|
70 |
DTRACE_PRINTLN4("RobotPeer: XTestQueryExtension returns event_basep = %d, error_basep = %d, majorp = %d, minorp = %d", |
|
71 |
event_basep, error_basep, majorp, minorp); |
|
72 |
if (majorp < 2 || (majorp == 2 && minorp < 2)) { |
|
73 |
/* bad version*/ |
|
74 |
DTRACE_PRINTLN2("XRobotPeer: XTEST version is %d.%d \n", majorp, minorp); |
|
75 |
if (majorp == 2 && minorp == 1) { |
|
76 |
DTRACE_PRINTLN("XRobotPeer: XTEST is 2.1 - no grab is available\n"); |
|
77 |
} else { |
|
78 |
isXTestAvailable = False; |
|
79 |
} |
|
80 |
} else { |
|
81 |
/* allow XTest calls even if someone else has the grab; e.g. during |
|
82 |
* a window resize operation. Works only with XTEST2.2*/ |
|
83 |
XTestGrabControl(awt_display, True); |
|
84 |
} |
|
85 |
} else { |
|
86 |
DTRACE_PRINTLN("RobotPeer: XTEST extension is unavailable"); |
|
87 |
} |
|
88 |
||
89 |
return isXTestAvailable; |
|
90 |
} |
|
91 |
||
92 |
static void getNumButtons() { |
|
93 |
int32_t major_opcode, first_event, first_error; |
|
94 |
int32_t xinputAvailable; |
|
95 |
int32_t numDevices, devIdx, clsIdx; |
|
96 |
XDeviceInfo* devices; |
|
97 |
XDeviceInfo* aDevice; |
|
98 |
XButtonInfo* bInfo; |
|
99 |
||
100 |
/* 4700242: |
|
101 |
* If XTest is asked to press a non-existant mouse button |
|
102 |
* (i.e. press Button3 on a system configured with a 2-button mouse), |
|
103 |
* then a crash may happen. To avoid this, we use the XInput |
|
104 |
* extension to query for the number of buttons on the XPointer, and check |
|
105 |
* before calling XTestFakeButtonEvent(). |
|
106 |
*/ |
|
107 |
xinputAvailable = XQueryExtension(awt_display, INAME, &major_opcode, &first_event, &first_error); |
|
108 |
DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XINPUT) returns major_opcode = %d, first_event = %d, first_error = %d", |
|
109 |
major_opcode, first_event, first_error); |
|
110 |
if (xinputAvailable) { |
|
111 |
devices = XListInputDevices(awt_display, &numDevices); |
|
112 |
for (devIdx = 0; devIdx < numDevices; devIdx++) { |
|
113 |
aDevice = &(devices[devIdx]); |
|
114 |
if (aDevice->use == IsXPointer) { |
|
115 |
for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) { |
|
116 |
if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) { |
|
117 |
bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx])); |
|
118 |
num_buttons = bInfo->num_buttons; |
|
119 |
DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons); |
|
120 |
break; |
|
121 |
} |
|
122 |
} |
|
123 |
break; |
|
124 |
} |
|
125 |
} |
|
126 |
XFreeDeviceList(devices); |
|
127 |
} |
|
128 |
else { |
|
129 |
DTRACE_PRINTLN1("RobotPeer: XINPUT extension is unavailable, assuming %d mouse buttons", num_buttons); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
static XImage *getWindowImage(Display * display, Window window, |
|
134 |
int32_t x, int32_t y, |
|
135 |
int32_t w, int32_t h) { |
|
136 |
XImage *image; |
|
137 |
int32_t transparentOverlays; |
|
138 |
int32_t numVisuals; |
|
139 |
XVisualInfo *pVisuals; |
|
140 |
int32_t numOverlayVisuals; |
|
141 |
OverlayInfo *pOverlayVisuals; |
|
142 |
int32_t numImageVisuals; |
|
143 |
XVisualInfo **pImageVisuals; |
|
144 |
list_ptr vis_regions; /* list of regions to read from */ |
|
145 |
list_ptr vis_image_regions ; |
|
146 |
int32_t allImage = 0 ; |
|
147 |
int32_t format = ZPixmap; |
|
148 |
||
149 |
/* prevent user from moving stuff around during the capture */ |
|
150 |
XGrabServer(display); |
|
151 |
||
152 |
/* |
|
153 |
* The following two functions live in multiVis.c-- they are pretty |
|
154 |
* much verbatim taken from the source to the xwd utility from the |
|
155 |
* X11 source. This version of the xwd source was somewhat better written |
|
156 |
* for reuse compared to Sun's version. |
|
157 |
* |
|
158 |
* ftp.x.org/pub/R6.3/xc/programs/xwd |
|
159 |
* |
|
160 |
* We use these functions since they do the very tough job of capturing |
|
161 |
* the screen correctly when it contains multiple visuals. They take into |
|
162 |
* account the depth/colormap of each visual and produce a capture as a |
|
163 |
* 24-bit RGB image so we don't have to fool around with colormaps etc. |
|
164 |
*/ |
|
165 |
||
166 |
GetMultiVisualRegions( |
|
167 |
display, |
|
168 |
window, |
|
169 |
x, y, w, h, |
|
170 |
&transparentOverlays, |
|
171 |
&numVisuals, |
|
172 |
&pVisuals, |
|
173 |
&numOverlayVisuals, |
|
174 |
&pOverlayVisuals, |
|
175 |
&numImageVisuals, |
|
176 |
&pImageVisuals, |
|
177 |
&vis_regions, |
|
178 |
&vis_image_regions, |
|
179 |
&allImage ); |
|
180 |
||
181 |
image = ReadAreaToImage( |
|
182 |
display, |
|
183 |
window, |
|
184 |
x, y, w, h, |
|
185 |
numVisuals, |
|
186 |
pVisuals, |
|
187 |
numOverlayVisuals, |
|
188 |
pOverlayVisuals, |
|
189 |
numImageVisuals, |
|
190 |
pImageVisuals, |
|
191 |
vis_regions, |
|
192 |
vis_image_regions, |
|
193 |
format, |
|
194 |
allImage ); |
|
195 |
||
196 |
/* allow user to do stuff again */ |
|
197 |
XUngrabServer(display); |
|
198 |
||
199 |
/* make sure the grab/ungrab is flushed */ |
|
200 |
XSync(display, False); |
|
201 |
||
202 |
return image; |
|
203 |
} |
|
204 |
||
205 |
/*********************************************************************************************/ |
|
206 |
||
207 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
208 |
Java_sun_awt_X11_XRobotPeer_setup (JNIEnv * env, jclass cls) { |
2 | 209 |
int32_t xtestAvailable; |
210 |
||
211 |
DTRACE_PRINTLN("RobotPeer: setup()"); |
|
212 |
||
213 |
AWT_LOCK(); |
|
214 |
||
215 |
xtestAvailable = isXTestAvailable(); |
|
216 |
DTRACE_PRINTLN1("RobotPeer: XTest available = %d", xtestAvailable); |
|
217 |
if (!xtestAvailable) { |
|
218 |
JNU_ThrowByName(env, "java/awt/AWTException", "java.awt.Robot requires your X server support the XTEST extension version 2.2"); |
|
219 |
AWT_UNLOCK(); |
|
220 |
return; |
|
221 |
} |
|
222 |
||
223 |
getNumButtons(); |
|
224 |
||
225 |
AWT_UNLOCK(); |
|
226 |
} |
|
227 |
||
228 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
229 |
Java_sun_awt_X11_XRobotPeer_getRGBPixelsImpl( JNIEnv *env, |
2 | 230 |
jclass cls, |
231 |
jobject xgc, |
|
232 |
jint x, |
|
233 |
jint y, |
|
234 |
jint width, |
|
235 |
jint height, |
|
236 |
jintArray pixelArray) { |
|
237 |
||
238 |
XImage *image; |
|
239 |
jint *ary; /* Array of jints for sending pixel values back |
|
240 |
* to parent process. |
|
241 |
*/ |
|
242 |
Window rootWindow; |
|
243 |
AwtGraphicsConfigDataPtr adata; |
|
244 |
||
245 |
DTRACE_PRINTLN6("RobotPeer: getRGBPixelsImpl(%lx, %d, %d, %d, %d, %x)", xgc, x, y, width, height, pixelArray); |
|
246 |
||
247 |
AWT_LOCK(); |
|
248 |
||
249 |
/* avoid a lot of work for empty rectangles */ |
|
250 |
if ((width * height) == 0) { |
|
251 |
AWT_UNLOCK(); |
|
252 |
return; |
|
253 |
} |
|
254 |
DASSERT(width * height > 0); /* only allow positive size */ |
|
255 |
||
256 |
adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData); |
|
257 |
DASSERT(adata != NULL); |
|
258 |
||
259 |
rootWindow = XRootWindow(awt_display, adata->awt_visInfo.screen); |
|
260 |
image = getWindowImage(awt_display, rootWindow, x, y, width, height); |
|
261 |
||
262 |
/* Array to use to crunch around the pixel values */ |
|
263 |
ary = (jint *) malloc(width * height * sizeof (jint)); |
|
264 |
if (ary == NULL) { |
|
265 |
JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError"); |
|
266 |
XDestroyImage(image); |
|
267 |
AWT_UNLOCK(); |
|
268 |
return; |
|
269 |
} |
|
270 |
/* convert to Java ARGB pixels */ |
|
271 |
for (y = 0; y < height; y++) { |
|
272 |
for (x = 0; x < width; x++) { |
|
273 |
jint pixel = (jint) XGetPixel(image, x, y); /* Note ignore upper |
|
274 |
* 32-bits on 64-bit |
|
275 |
* OSes. |
|
276 |
*/ |
|
277 |
||
278 |
pixel |= 0xff000000; /* alpha - full opacity */ |
|
279 |
||
280 |
ary[(y * width) + x] = pixel; |
|
281 |
} |
|
282 |
} |
|
283 |
(*env)->SetIntArrayRegion(env, pixelArray, 0, height * width, ary); |
|
284 |
free(ary); |
|
285 |
||
286 |
XDestroyImage(image); |
|
287 |
||
288 |
AWT_UNLOCK(); |
|
289 |
} |
|
290 |
||
291 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
292 |
Java_sun_awt_X11_XRobotPeer_keyPressImpl (JNIEnv *env, |
2 | 293 |
jclass cls, |
294 |
jint keycode) { |
|
295 |
||
296 |
AWT_LOCK(); |
|
297 |
||
298 |
DTRACE_PRINTLN1("RobotPeer: keyPressImpl(%i)", keycode); |
|
299 |
||
300 |
XTestFakeKeyEvent(awt_display, |
|
301 |
XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)), |
|
302 |
True, |
|
303 |
CurrentTime); |
|
304 |
||
305 |
XSync(awt_display, False); |
|
306 |
||
307 |
AWT_UNLOCK(); |
|
308 |
||
309 |
} |
|
310 |
||
311 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
312 |
Java_sun_awt_X11_XRobotPeer_keyReleaseImpl (JNIEnv *env, |
2 | 313 |
jclass cls, |
314 |
jint keycode) { |
|
315 |
AWT_LOCK(); |
|
316 |
||
317 |
DTRACE_PRINTLN1("RobotPeer: keyReleaseImpl(%i)", keycode); |
|
318 |
||
319 |
XTestFakeKeyEvent(awt_display, |
|
320 |
XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)), |
|
321 |
False, |
|
322 |
CurrentTime); |
|
323 |
||
324 |
XSync(awt_display, False); |
|
325 |
||
326 |
AWT_UNLOCK(); |
|
327 |
} |
|
328 |
||
329 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
330 |
Java_sun_awt_X11_XRobotPeer_mouseMoveImpl (JNIEnv *env, |
2 | 331 |
jclass cls, |
332 |
jobject xgc, |
|
333 |
jint root_x, |
|
334 |
jint root_y) { |
|
335 |
||
336 |
AwtGraphicsConfigDataPtr adata; |
|
337 |
||
338 |
AWT_LOCK(); |
|
339 |
||
340 |
DTRACE_PRINTLN3("RobotPeer: mouseMoveImpl(%lx, %i, %i)", xgc, root_x, root_y); |
|
341 |
||
342 |
adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData); |
|
343 |
DASSERT(adata != NULL); |
|
344 |
||
345 |
XWarpPointer(awt_display, None, XRootWindow(awt_display, adata->awt_visInfo.screen), 0, 0, 0, 0, root_x, root_y); |
|
346 |
XSync(awt_display, False); |
|
347 |
||
348 |
AWT_UNLOCK(); |
|
349 |
} |
|
350 |
||
351 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
352 |
Java_sun_awt_X11_XRobotPeer_mousePressImpl (JNIEnv *env, |
2 | 353 |
jclass cls, |
354 |
jint buttonMask) { |
|
355 |
AWT_LOCK(); |
|
356 |
||
357 |
DTRACE_PRINTLN1("RobotPeer: mousePressImpl(%i)", buttonMask); |
|
358 |
||
359 |
if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK) { |
|
360 |
XTestFakeButtonEvent(awt_display, 1, True, CurrentTime); |
|
361 |
} |
|
362 |
if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK) && |
|
363 |
(num_buttons >= 2)) { |
|
364 |
XTestFakeButtonEvent(awt_display, 2, True, CurrentTime); |
|
365 |
} |
|
366 |
if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK) && |
|
367 |
(num_buttons >= 3)) { |
|
368 |
XTestFakeButtonEvent(awt_display, 3, True, CurrentTime); |
|
369 |
} |
|
370 |
XSync(awt_display, False); |
|
371 |
||
372 |
AWT_UNLOCK(); |
|
373 |
} |
|
374 |
||
375 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
376 |
Java_sun_awt_X11_XRobotPeer_mouseReleaseImpl (JNIEnv *env, |
2 | 377 |
jclass cls, |
378 |
jint buttonMask) { |
|
379 |
AWT_LOCK(); |
|
380 |
||
381 |
DTRACE_PRINTLN1("RobotPeer: mouseReleaseImpl(%i)", buttonMask); |
|
382 |
||
383 |
if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK) { |
|
384 |
XTestFakeButtonEvent(awt_display, 1, False, CurrentTime); |
|
385 |
} |
|
386 |
if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK) && |
|
387 |
(num_buttons >= 2)) { |
|
388 |
XTestFakeButtonEvent(awt_display, 2, False, CurrentTime); |
|
389 |
} |
|
390 |
if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK) && |
|
391 |
(num_buttons >= 3)) { |
|
392 |
XTestFakeButtonEvent(awt_display, 3, False, CurrentTime); |
|
393 |
} |
|
394 |
XSync(awt_display, False); |
|
395 |
||
396 |
AWT_UNLOCK(); |
|
397 |
} |
|
398 |
||
399 |
JNIEXPORT void JNICALL |
|
1175
026b52c440fe
6706121: makefile: unnecessary Motif classes compilation in JDK7
yan
parents:
2
diff
changeset
|
400 |
Java_sun_awt_X11_XRobotPeer_mouseWheelImpl (JNIEnv *env, |
2 | 401 |
jclass cls, |
402 |
jint wheelAmt) { |
|
403 |
/* Mouse wheel is implemented as a button press of button 4 and 5, so it */ |
|
404 |
/* probably could have been hacked into robot_mouseButtonEvent, but it's */ |
|
405 |
/* cleaner to give it its own command type, in case the implementation */ |
|
406 |
/* needs to be changed later. -bchristi, 6/20/01 */ |
|
407 |
||
408 |
int32_t repeat = abs(wheelAmt); |
|
409 |
int32_t button = wheelAmt < 0 ? 4 : 5; /* wheel up: button 4 */ |
|
410 |
/* wheel down: button 5 */ |
|
411 |
int32_t loopIdx; |
|
412 |
||
413 |
AWT_LOCK(); |
|
414 |
||
415 |
DTRACE_PRINTLN1("RobotPeer: mouseWheelImpl(%i)", wheelAmt); |
|
416 |
||
417 |
for (loopIdx = 0; loopIdx < repeat; loopIdx++) { /* do nothing for */ |
|
418 |
/* wheelAmt == 0 */ |
|
419 |
XTestFakeButtonEvent(awt_display, button, True, CurrentTime); |
|
420 |
XTestFakeButtonEvent(awt_display, button, False, CurrentTime); |
|
421 |
} |
|
422 |
XSync(awt_display, False); |
|
423 |
||
424 |
AWT_UNLOCK(); |
|
425 |
} |