1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/* XEventPump.java -- Pumps events from X to AWT
Copyright (C) 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.awt.peer.x;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.PaintEvent;
import java.util.HashMap;
import gnu.x11.Display;
import gnu.x11.event.ButtonPress;
import gnu.x11.event.ButtonRelease;
import gnu.x11.event.ConfigureNotify;
import gnu.x11.event.Event;
import gnu.x11.event.Expose;
import gnu.x11.event.Input;
import gnu.x11.event.KeyPress;
import gnu.x11.event.KeyRelease;
import gnu.x11.event.MotionNotify;
/**
* Fetches events from X, translates them to AWT events and pumps them up
* into the AWT event queue.
*
* @author Roman Kennke (kennke@aicas.com)
*/
public class XEventPump
implements Runnable
{
/**
* The X Display from which we fetch and pump up events.
*/
private Display display;
/**
* Maps X Windows to AWT Windows to be able to correctly determine the
* event targets.
*/
private HashMap windows;
/**
* Indicates if we are currently inside a drag operation. This is
* set to the button ID when a button is pressed and to -1 (indicating
* that no drag is active) when the mouse is released.
*/
private int drag;
/**
* Creates a new XEventPump for the specified X Display.
*
* @param d the X Display
*/
XEventPump(Display d)
{
display = d;
windows = new HashMap();
drag = -1;
Thread thread = new Thread(this, "X Event Pump");
thread.setDaemon(true);
thread.start();
}
/**
* The main event pump loop. This basically fetches events from the
* X Display and pumps them into the system event queue.
*/
public void run()
{
while (display.connected)
{
try
{
Event xEvent = display.next_event();
handleEvent(xEvent);
}
catch (ThreadDeath death)
{
// If someone wants to kill us, let them.
return;
}
catch (Throwable x)
{
System.err.println("Exception during event dispatch:");
x.printStackTrace(System.err);
}
}
}
/**
* Adds an X Window to AWT Window mapping. This is required so that the
* event pump can correctly determine the event targets.
*
* @param xWindow the X Window
* @param awtWindow the AWT Window
*/
void registerWindow(gnu.x11.Window xWindow, Window awtWindow)
{
if (XToolkit.DEBUG)
System.err.println("registering window id: " + xWindow.id);
windows.put(new Integer(xWindow.id), awtWindow);
}
void unregisterWindow(gnu.x11.Window xWindow)
{
windows.remove(new Integer(xWindow.id));
}
private void handleEvent(Event xEvent)
{
Integer key = null;
Window awtWindow = null;
if (XToolkit.DEBUG)
System.err.println("fetched event: " + xEvent);
switch (xEvent.code())
{
case ButtonPress.CODE:
ButtonPress bp = (ButtonPress) xEvent;
key= new Integer(bp.event_window_id);
awtWindow = (Window) windows.get(key);
// Create and post the mouse event.
int button = bp.detail();
// AWT cannot handle more than 3 buttons and expects 0 instead.
if (button >= gnu.x11.Input.BUTTON3)
button = 0;
drag = button;
MouseEvent mp = new MouseEvent(awtWindow, MouseEvent.MOUSE_PRESSED,
System.currentTimeMillis(),
KeyboardMapping.mapModifiers(bp.state()) | buttonToModifier(button),
bp.event_x(), bp.event_y(),
1, false, button);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(mp);
break;
case ButtonRelease.CODE:
ButtonRelease br = (ButtonRelease) xEvent;
key= new Integer(br.event_window_id);
awtWindow = (Window) windows.get(key);
button = br.detail();
// AWT cannot handle more than 3 buttons and expects 0 instead.
if (button >= gnu.x11.Input.BUTTON3)
button = 0;
drag = -1;
MouseEvent mr = new MouseEvent(awtWindow, MouseEvent.MOUSE_RELEASED,
System.currentTimeMillis(),
KeyboardMapping.mapModifiers(br.state()) | buttonToModifier(button),
br.event_x(), br.event_y(),
1, false, button);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(mr);
break;
case MotionNotify.CODE:
MotionNotify mn = (MotionNotify) xEvent;
key= new Integer(mn.event_window_id);
awtWindow = (Window) windows.get(key);
MouseEvent mm;
if (drag == -1)
{
mm = new MouseEvent(awtWindow, MouseEvent.MOUSE_MOVED,
System.currentTimeMillis(), 0,
mn.event_x(), mn.event_y(),
1, false);
}
else
{
mm = new MouseEvent(awtWindow, MouseEvent.MOUSE_DRAGGED,
System.currentTimeMillis(), 0,
mn.event_x(), mn.event_y(),
1, false);
}
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(mm);
break;
case ConfigureNotify.CODE:
key= new Integer(((ConfigureNotify) xEvent).event_window_id);
awtWindow = (Window) windows.get(key);
ConfigureNotify c = (ConfigureNotify) xEvent;
if (XToolkit.DEBUG)
System.err.println("resize request for window id: " + key);
// Detect and report size changes.
XWindowPeer xwindow = (XWindowPeer) awtWindow.getPeer();
Insets i = xwindow.insets();
if (c.width() != awtWindow.getWidth() - i.left - i.right
|| c.height() != awtWindow.getHeight() - i.top - i.bottom)
{
if (XToolkit.DEBUG)
System.err.println("Setting size on AWT window: " + c.width()
+ ", " + c.height() + ", " + awtWindow.getWidth()
+ ", " + awtWindow.getHeight());
xwindow.callback = true;
xwindow.xwindow.width = c.width();
xwindow.xwindow.height = c.height();
awtWindow.setSize(c.width() + i.left + i.right,
c.height() + i.top + i.bottom);
xwindow.callback = false;
}
break;
case Expose.CODE:
key= new Integer(((Expose) xEvent).window_id);
awtWindow = (Window) windows.get(key);
Expose exp = (Expose) xEvent;
if (XToolkit.DEBUG)
System.err.println("expose request for window id: " + key);
Rectangle r = new Rectangle(exp.x(), exp.y(), exp.width(),
exp.height());
//System.err.println("expose paint: " + r);
// We need to clear the background of the exposed rectangle.
assert awtWindow != null : "awtWindow == null for window ID: " + key;
Graphics g = awtWindow.getGraphics();
g.clearRect(r.x, r.y, r.width, r.height);
g.dispose();
PaintEvent pev = new PaintEvent(awtWindow, PaintEvent.PAINT, r);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(pev);
break;
case KeyPress.CODE:
case KeyRelease.CODE:
key = new Integer(((Input) xEvent).event_window_id);
awtWindow = (Window) windows.get(key);
handleKeyEvent(xEvent, awtWindow);
break;
default:
if (XToolkit.DEBUG)
System.err.println("Unhandled X event: " + xEvent);
}
}
/**
* Handles key events from X.
*
* @param xEvent the X event
* @param awtWindow the AWT window to which the event gets posted
*/
private void handleKeyEvent(Event xEvent, Window awtWindow)
{
Input keyEvent = (Input) xEvent;
int xKeyCode = keyEvent.detail();
int xMods = keyEvent.state();
int keyCode = KeyboardMapping.mapToKeyCode(xEvent.display.input, xKeyCode,
xMods);
char keyChar = KeyboardMapping.mapToKeyChar(xEvent.display.input, xKeyCode,
xMods);
if (XToolkit.DEBUG)
System.err.println("XEventPump.handleKeyEvent: " + xKeyCode + ", "
+ xMods + ": " + ((int) keyChar) + ", " + keyCode);
int awtMods = KeyboardMapping.mapModifiers(xMods);
long when = System.currentTimeMillis();
KeyEvent ke;
if (keyEvent.code() == KeyPress.CODE)
{
ke = new KeyEvent(awtWindow, KeyEvent.KEY_PRESSED, when,
awtMods, keyCode,
KeyEvent.CHAR_UNDEFINED);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ke);
if (keyChar != KeyEvent.CHAR_UNDEFINED)
{
ke = new KeyEvent(awtWindow, KeyEvent.KEY_TYPED, when,
awtMods, KeyEvent.VK_UNDEFINED,
keyChar);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ke);
}
}
else
{
ke = new KeyEvent(awtWindow, KeyEvent.KEY_RELEASED, when,
awtMods, keyCode,
KeyEvent.CHAR_UNDEFINED);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(ke);
}
}
/** Translates an X button identifier to the AWT's MouseEvent modifier
* mask. As the AWT cannot handle more than 3 buttons those return
* <code>0</code>.
*/
static int buttonToModifier(int button)
{
switch (button)
{
case gnu.x11.Input.BUTTON1:
return MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON1_MASK;
case gnu.x11.Input.BUTTON2:
return MouseEvent.BUTTON2_DOWN_MASK | MouseEvent.BUTTON2_MASK;
case gnu.x11.Input.BUTTON3:
return MouseEvent.BUTTON3_DOWN_MASK | MouseEvent.BUTTON3_MASK;
}
return 0;
}
}
|