blob: 10cf652d77e9f24b4e1cafd72ccd0f989ecda719 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
52extern 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
56static int32_t num_buttons = 3;
57
58static 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
92static 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
133static 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#ifdef XAWT
208#define FUNC_NAME(name) Java_sun_awt_X11_XRobotPeer_ ## name
209#else
210#define FUNC_NAME(name) Java_sun_awt_motif_MRobotPeer_ ## name
211#endif
212
213JNIEXPORT void JNICALL
214FUNC_NAME(setup) (JNIEnv * env, jclass cls) {
215 int32_t xtestAvailable;
216
217 DTRACE_PRINTLN("RobotPeer: setup()");
218
219 AWT_LOCK();
220
221 xtestAvailable = isXTestAvailable();
222 DTRACE_PRINTLN1("RobotPeer: XTest available = %d", xtestAvailable);
223 if (!xtestAvailable) {
224 JNU_ThrowByName(env, "java/awt/AWTException", "java.awt.Robot requires your X server support the XTEST extension version 2.2");
225 AWT_UNLOCK();
226 return;
227 }
228
229 getNumButtons();
230
231 AWT_UNLOCK();
232}
233
234JNIEXPORT void JNICALL
235FUNC_NAME(getRGBPixelsImpl)( JNIEnv *env,
236 jclass cls,
237 jobject xgc,
238 jint x,
239 jint y,
240 jint width,
241 jint height,
242 jintArray pixelArray) {
243
244 XImage *image;
245 jint *ary; /* Array of jints for sending pixel values back
246 * to parent process.
247 */
248 Window rootWindow;
249 AwtGraphicsConfigDataPtr adata;
250
251 DTRACE_PRINTLN6("RobotPeer: getRGBPixelsImpl(%lx, %d, %d, %d, %d, %x)", xgc, x, y, width, height, pixelArray);
252
253 AWT_LOCK();
254
255 /* avoid a lot of work for empty rectangles */
256 if ((width * height) == 0) {
257 AWT_UNLOCK();
258 return;
259 }
260 DASSERT(width * height > 0); /* only allow positive size */
261
262 adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
263 DASSERT(adata != NULL);
264
265 rootWindow = XRootWindow(awt_display, adata->awt_visInfo.screen);
266 image = getWindowImage(awt_display, rootWindow, x, y, width, height);
267
268 /* Array to use to crunch around the pixel values */
269 ary = (jint *) malloc(width * height * sizeof (jint));
270 if (ary == NULL) {
271 JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
272 XDestroyImage(image);
273 AWT_UNLOCK();
274 return;
275 }
276 /* convert to Java ARGB pixels */
277 for (y = 0; y < height; y++) {
278 for (x = 0; x < width; x++) {
279 jint pixel = (jint) XGetPixel(image, x, y); /* Note ignore upper
280 * 32-bits on 64-bit
281 * OSes.
282 */
283
284 pixel |= 0xff000000; /* alpha - full opacity */
285
286 ary[(y * width) + x] = pixel;
287 }
288 }
289 (*env)->SetIntArrayRegion(env, pixelArray, 0, height * width, ary);
290 free(ary);
291
292 XDestroyImage(image);
293
294 AWT_UNLOCK();
295}
296
297JNIEXPORT void JNICALL
298FUNC_NAME(keyPressImpl) (JNIEnv *env,
299 jclass cls,
300 jint keycode) {
301
302 AWT_LOCK();
303
304 DTRACE_PRINTLN1("RobotPeer: keyPressImpl(%i)", keycode);
305
306 XTestFakeKeyEvent(awt_display,
307 XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
308 True,
309 CurrentTime);
310
311 XSync(awt_display, False);
312
313 AWT_UNLOCK();
314
315}
316
317JNIEXPORT void JNICALL
318FUNC_NAME(keyReleaseImpl) (JNIEnv *env,
319 jclass cls,
320 jint keycode) {
321 AWT_LOCK();
322
323 DTRACE_PRINTLN1("RobotPeer: keyReleaseImpl(%i)", keycode);
324
325 XTestFakeKeyEvent(awt_display,
326 XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
327 False,
328 CurrentTime);
329
330 XSync(awt_display, False);
331
332 AWT_UNLOCK();
333}
334
335JNIEXPORT void JNICALL
336FUNC_NAME(mouseMoveImpl) (JNIEnv *env,
337 jclass cls,
338 jobject xgc,
339 jint root_x,
340 jint root_y) {
341
342 AwtGraphicsConfigDataPtr adata;
343
344 AWT_LOCK();
345
346 DTRACE_PRINTLN3("RobotPeer: mouseMoveImpl(%lx, %i, %i)", xgc, root_x, root_y);
347
348 adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
349 DASSERT(adata != NULL);
350
351 XWarpPointer(awt_display, None, XRootWindow(awt_display, adata->awt_visInfo.screen), 0, 0, 0, 0, root_x, root_y);
352 XSync(awt_display, False);
353
354 AWT_UNLOCK();
355}
356
357JNIEXPORT void JNICALL
358FUNC_NAME(mousePressImpl) (JNIEnv *env,
359 jclass cls,
360 jint buttonMask) {
361 AWT_LOCK();
362
363 DTRACE_PRINTLN1("RobotPeer: mousePressImpl(%i)", buttonMask);
364
365 if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK) {
366 XTestFakeButtonEvent(awt_display, 1, True, CurrentTime);
367 }
368 if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK) &&
369 (num_buttons >= 2)) {
370 XTestFakeButtonEvent(awt_display, 2, True, CurrentTime);
371 }
372 if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK) &&
373 (num_buttons >= 3)) {
374 XTestFakeButtonEvent(awt_display, 3, True, CurrentTime);
375 }
376 XSync(awt_display, False);
377
378 AWT_UNLOCK();
379}
380
381JNIEXPORT void JNICALL
382FUNC_NAME(mouseReleaseImpl) (JNIEnv *env,
383 jclass cls,
384 jint buttonMask) {
385 AWT_LOCK();
386
387 DTRACE_PRINTLN1("RobotPeer: mouseReleaseImpl(%i)", buttonMask);
388
389 if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK) {
390 XTestFakeButtonEvent(awt_display, 1, False, CurrentTime);
391 }
392 if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK) &&
393 (num_buttons >= 2)) {
394 XTestFakeButtonEvent(awt_display, 2, False, CurrentTime);
395 }
396 if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK) &&
397 (num_buttons >= 3)) {
398 XTestFakeButtonEvent(awt_display, 3, False, CurrentTime);
399 }
400 XSync(awt_display, False);
401
402 AWT_UNLOCK();
403}
404
405JNIEXPORT void JNICALL
406FUNC_NAME(mouseWheelImpl) (JNIEnv *env,
407 jclass cls,
408 jint wheelAmt) {
409/* Mouse wheel is implemented as a button press of button 4 and 5, so it */
410/* probably could have been hacked into robot_mouseButtonEvent, but it's */
411/* cleaner to give it its own command type, in case the implementation */
412/* needs to be changed later. -bchristi, 6/20/01 */
413
414 int32_t repeat = abs(wheelAmt);
415 int32_t button = wheelAmt < 0 ? 4 : 5; /* wheel up: button 4 */
416 /* wheel down: button 5 */
417 int32_t loopIdx;
418
419 AWT_LOCK();
420
421 DTRACE_PRINTLN1("RobotPeer: mouseWheelImpl(%i)", wheelAmt);
422
423 for (loopIdx = 0; loopIdx < repeat; loopIdx++) { /* do nothing for */
424 /* wheelAmt == 0 */
425 XTestFakeButtonEvent(awt_display, button, True, CurrentTime);
426 XTestFakeButtonEvent(awt_display, button, False, CurrentTime);
427 }
428 XSync(awt_display, False);
429
430 AWT_UNLOCK();
431}