XEventLoop.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package gnu.awt.xlib;
  2. /* Copyright (C) 2000 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. import java.awt.*;
  8. import gnu.awt.LightweightRedirector;
  9. import gnu.gcj.xlib.Display;
  10. import gnu.gcj.xlib.XAnyEvent;
  11. import gnu.gcj.xlib.XExposeEvent;
  12. import gnu.gcj.xlib.XButtonEvent;
  13. import gnu.gcj.xlib.XConfigureEvent;
  14. import java.awt.event.PaintEvent;
  15. import java.awt.event.InputEvent;
  16. import java.awt.event.MouseEvent;
  17. import java.util.Vector;
  18. public class XEventLoop implements Runnable
  19. {
  20. Display display;
  21. EventQueue queue;
  22. XAnyEvent anyEvent;
  23. private Thread eventLoopThread;
  24. LightweightRedirector lightweightRedirector = new LightweightRedirector();
  25. public XEventLoop(Display display, EventQueue queue)
  26. {
  27. this.display = display;
  28. this.queue = queue;
  29. anyEvent = new XAnyEvent(display);
  30. eventLoopThread = new Thread(this, "AWT thread for XEventLoop");
  31. eventLoopThread.start();
  32. }
  33. public void run ()
  34. {
  35. // FIXME: do we need an interrupt mechanism for window shutdown?
  36. while (true)
  37. postNextEvent (true);
  38. }
  39. /** If there's an event available, post it.
  40. * @return true if an event was posted
  41. */
  42. boolean postNextEvent(boolean block)
  43. {
  44. AWTEvent evt = getNextEvent(block);
  45. if (evt != null)
  46. queue.postEvent(evt);
  47. return evt != null;
  48. }
  49. /** Get the next event.
  50. * @param block If true, block until an event becomes available
  51. */
  52. public AWTEvent getNextEvent(boolean block)
  53. {
  54. // ASSERT:
  55. if (isIdle())
  56. throw new Error("should not be idle");
  57. AWTEvent event = null;
  58. if (loadNextEvent(block))
  59. {
  60. event = createEvent();
  61. event = lightweightRedirector.redirect(event);
  62. }
  63. return event;
  64. }
  65. boolean loadNextEvent(boolean block)
  66. {
  67. boolean gotEvent = false;
  68. try
  69. {
  70. setIdle(true);
  71. /* The code below will result in an XFlush(). However,
  72. while we are waiting for events after calling XFlush(),
  73. new X requests issued on other threads will not
  74. automatically be flushed. This can lead to a deadlock
  75. since XFlush() will not be called before after the
  76. processing of the next event, and new events arriving
  77. might be dependent on the delivery of the X
  78. requests.
  79. Code that issues X requests should therefore call
  80. flushIfIdle() after they're done, to ensure that the
  81. requests are delivered in a timely manner. XFlush is not
  82. run if event processing is underway, since we are assured
  83. that the event loop execution will return to this point,
  84. where requests are flushed again, before waiting for new
  85. events.
  86. Alternatively, do the work on the AWT thread, since the
  87. XEventQueue knows how to flush the display when it runs out
  88. of events. */
  89. //display.flush(); // implicit?
  90. gotEvent = anyEvent.loadNext(block);
  91. }
  92. catch (RuntimeException re)
  93. {
  94. System.err.println("Exception thrown on event thread:" + re);
  95. }
  96. finally
  97. {
  98. setIdle(false);
  99. }
  100. return gotEvent;
  101. }
  102. /**
  103. * @returns an AWT event created based on the current XEvent.
  104. * Returns null if the current XEvent does not map to any perticular
  105. * AWT event.
  106. */
  107. AWTEvent createEvent ()
  108. {
  109. int type = anyEvent.getType ();
  110. // Ignore some events without further processing
  111. switch (type)
  112. {
  113. // ignore "no expose" events, which are generated whenever a pixmap
  114. // is copied to copied to a window which is entirely unobscured
  115. case XAnyEvent.TYPE_NO_EXPOSE:
  116. case XAnyEvent.TYPE_UNMAP_NOTIFY: // ignore for now
  117. case XAnyEvent.TYPE_MAP_NOTIFY: // ignore for now
  118. case XAnyEvent.TYPE_REPARENT_NOTIFY: // ignore for now
  119. return null;
  120. default:
  121. break; // continue processing events not in ignore list
  122. }
  123. /* avoid attempting to get client data before client data has
  124. been set. */
  125. Object peer;
  126. synchronized (this)
  127. {
  128. peer = anyEvent.getWindow ().getClientData ();
  129. }
  130. Component source = null;
  131. // Try to identify source component
  132. if (peer instanceof XCanvasPeer)
  133. {
  134. source = ((XCanvasPeer) peer).getComponent ();
  135. }
  136. if (source == null)
  137. {
  138. String msg = "unable to locate source for event (" +
  139. anyEvent + "): peer=" + peer;
  140. throw new RuntimeException (msg);
  141. }
  142. /* if a mapping from anyEvent to AWTEvent is possible, construct a
  143. new AWTEvent and return it. */
  144. switch (type)
  145. {
  146. case XAnyEvent.TYPE_EXPOSE:
  147. return createPaintEvent (source);
  148. case XAnyEvent.TYPE_BUTTON_PRESS:
  149. case XAnyEvent.TYPE_BUTTON_RELEASE:
  150. return createMouseEvent (type, source);
  151. case XAnyEvent.TYPE_CONFIGURE_NOTIFY:
  152. configureNotify (peer);
  153. return null;
  154. default:
  155. String msg = "Do not know how to handle event (" + anyEvent + ")";
  156. throw new RuntimeException (msg);
  157. }
  158. }
  159. AWTEvent createPaintEvent(Component src)
  160. {
  161. XExposeEvent expose = new XExposeEvent(anyEvent);
  162. PaintEvent pe = new PaintEvent(src, PaintEvent.PAINT,
  163. expose.getBounds());
  164. return pe;
  165. }
  166. AWTEvent createMouseEvent(int type, Component src)
  167. {
  168. XButtonEvent buttonEvt = new XButtonEvent(anyEvent);
  169. int modifiers = 0; //buttonToModifierMap[buttonEvt.button];
  170. /* Warning: this makes assumptions on the contents of
  171. X.h... Button1 = 1, Button2 = 2, etc... */
  172. switch (buttonEvt.button)
  173. {
  174. case 1:
  175. modifiers = InputEvent.BUTTON1_DOWN_MASK;
  176. break;
  177. case 2:
  178. modifiers = InputEvent.BUTTON2_DOWN_MASK;
  179. break;
  180. case 3:
  181. modifiers = InputEvent.BUTTON2_DOWN_MASK;
  182. break;
  183. }
  184. int state = buttonEvt.state;
  185. // remap bits from state to modifiers:
  186. if ((state & XButtonEvent.MASK_SHIFT) != 0)
  187. modifiers |= InputEvent.SHIFT_MASK;
  188. if ((state & XButtonEvent.MASK_CONTROL) != 0)
  189. modifiers |= InputEvent.CTRL_MASK;
  190. /* FIXME: we need additional X code to properly map MODn states to
  191. input modifiers */
  192. int clickCount = 1; // FIXME... Can't get this from X.
  193. boolean popupTrigger = false; // FIXME: look up policy somewhere
  194. int x = buttonEvt.x;
  195. int y = buttonEvt.y;
  196. int id = (type == XAnyEvent.TYPE_BUTTON_PRESS) ?
  197. MouseEvent.MOUSE_PRESSED :
  198. MouseEvent.MOUSE_RELEASED;
  199. MouseEvent me = new MouseEvent(src,
  200. id,
  201. buttonEvt.time, modifiers,
  202. buttonEvt.x, buttonEvt.y,
  203. clickCount, popupTrigger);
  204. return me;
  205. }
  206. void configureNotify(Object peerObj)
  207. {
  208. XConfigureEvent configEvent = new XConfigureEvent(anyEvent);
  209. XFramePeer peer = (XFramePeer) peerObj;
  210. peer.configureNotify(configEvent);
  211. }
  212. public void flushIfIdle()
  213. {
  214. if (isIdle())
  215. display.flush();
  216. }
  217. volatile boolean idle = false;
  218. final synchronized void setIdle(boolean idle)
  219. {
  220. this.idle = idle;
  221. }
  222. final synchronized boolean isIdle()
  223. {
  224. return idle;
  225. }
  226. }