1 /*
2  * Copyright (c) 2003, 2011, 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 package sun.awt.X11;
26 
27 import java.awt.*;
28 import java.awt.event.InputEvent;
29 import java.awt.peer.*;
30 
31 import sun.awt.AWTAccessor;
32 import sun.awt.SunToolkit;
33 import sun.awt.X11GraphicsConfig;
34 
35 class XRobotPeer implements RobotPeer {
36 
37     static {
loadNativeLibraries()38         loadNativeLibraries();
39     }
40 
41     private X11GraphicsConfig   xgc = null;
42     /*
43      * native implementation uses some static shared data (pipes, processes)
44      * so use a class lock to synchronize native method calls
45      */
46     static Object robotLock = new Object();
47 
XRobotPeer(GraphicsConfiguration gc)48     XRobotPeer(GraphicsConfiguration gc) {
49         this.xgc = (X11GraphicsConfig)gc;
50         SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
51         setup(tk.getNumberOfButtons(), AWTAccessor.getInputEventAccessor().getButtonDownMasks());
52     }
53 
dispose()54     public void dispose() {
55         // does nothing
56     }
57 
mouseMove(int x, int y)58     public void mouseMove(int x, int y) {
59         mouseMoveImpl(xgc, x, y);
60     }
61 
mousePress(int buttons)62     public void mousePress(int buttons) {
63         mousePressImpl(buttons);
64     }
65 
mouseRelease(int buttons)66     public void mouseRelease(int buttons) {
67         mouseReleaseImpl(buttons);
68     }
69 
mouseWheel(int wheelAmt)70     public void mouseWheel(int wheelAmt) {
71     mouseWheelImpl(wheelAmt);
72     }
73 
keyPress(int keycode)74     public void keyPress(int keycode) {
75         keyPressImpl(keycode);
76     }
77 
keyRelease(int keycode)78     public void keyRelease(int keycode) {
79         keyReleaseImpl(keycode);
80     }
81 
getRGBPixel(int x, int y)82     public int getRGBPixel(int x, int y) {
83         int pixelArray[] = new int[1];
84         getRGBPixelsImpl(xgc, x, y, 1, 1, pixelArray);
85         return pixelArray[0];
86     }
87 
getRGBPixels(Rectangle bounds)88     public int [] getRGBPixels(Rectangle bounds) {
89         int pixelArray[] = new int[bounds.width*bounds.height];
90         getRGBPixelsImpl(xgc, bounds.x, bounds.y, bounds.width, bounds.height, pixelArray);
91         return pixelArray;
92     }
93 
setup(int numberOfButtons, int[] buttonDownMasks)94     private static native synchronized void setup(int numberOfButtons, int[] buttonDownMasks);
95 
mouseMoveImpl(X11GraphicsConfig xgc, int x, int y)96     private static native synchronized void mouseMoveImpl(X11GraphicsConfig xgc, int x, int y);
mousePressImpl(int buttons)97     private static native synchronized void mousePressImpl(int buttons);
mouseReleaseImpl(int buttons)98     private static native synchronized void mouseReleaseImpl(int buttons);
mouseWheelImpl(int wheelAmt)99     private static native synchronized void mouseWheelImpl(int wheelAmt);
100 
keyPressImpl(int keycode)101     private static native synchronized void keyPressImpl(int keycode);
keyReleaseImpl(int keycode)102     private static native synchronized void keyReleaseImpl(int keycode);
103 
getRGBPixelsImpl(X11GraphicsConfig xgc, int x, int y, int width, int height, int pixelArray[])104     private static native synchronized void getRGBPixelsImpl(X11GraphicsConfig xgc, int x, int y, int width, int height, int pixelArray[]);
loadNativeLibraries()105     private static native void loadNativeLibraries();
106 }
107