3 /* Copyright (C) 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
13 import gnu
.awt
.LightweightRedirector
;
14 import gnu
.gcj
.xlib
.Display
;
15 import gnu
.gcj
.xlib
.XAnyEvent
;
16 import gnu
.gcj
.xlib
.XExposeEvent
;
17 import gnu
.gcj
.xlib
.XButtonEvent
;
18 import gnu
.gcj
.xlib
.XConfigureEvent
;
19 import java
.awt
.event
.PaintEvent
;
20 import java
.awt
.event
.InputEvent
;
21 import java
.awt
.event
.MouseEvent
;
22 import java
.util
.Vector
;
24 public class XEventLoop
implements Runnable
29 Thread eventLoopThread
;
31 LightweightRedirector lightweightRedirector
= new LightweightRedirector();
33 public XEventLoop(Display display
, EventQueue queue
)
35 this.display
= display
;
38 anyEvent
= new XAnyEvent(display
);
39 eventLoopThread
= new Thread(this, "AWT thread for XEventLoop");
40 eventLoopThread
.start();
51 AWTEvent evt
= getNextEvent();
55 /** get next event. Will block until events become available. */
57 public AWTEvent
getNextEvent()
61 throw new Error("should not be idle");
63 AWTEvent event
= null;
67 event
= createEvent();
70 event
= lightweightRedirector
.redirect(event
);
81 /* The code below will result in an XFlush(). However,
82 while we are waiting for events after calling XFlush(),
83 new X requests issued on other threads will not
84 automatically be flushed. This can lead to a deadlock
85 since XFlush() will not be called before after the
86 processing of the next event, and new events arriving
87 might be dependent on the delivery of the X
90 Code that issues X requests should therefore call
91 flushIfIdle() after they're done, to ensure that the
92 requests are delivered in a timely manner. XFlush is not
93 run if event processing is underway, since we are assured
94 that the event loop execution will return to this point,
95 where requests are flushed again, before waiting for new
98 Alternatively, do the work on the AWT thread, since the
99 XEventQueue knows how to flush the display when it runs out
102 //display.flush(); // implicit?
105 catch (RuntimeException re
)
107 System
.err
.println("Exception thrown on event thread:" + re
);
116 * @returns an AWT event created based on the current XEvent.
117 * Returns null if the current XEvent does not map to any perticular
121 AWTEvent
createEvent()
123 /* avoid attempting to get client data before client data has
128 peer
= anyEvent
.getWindow().getClientData();
131 Component source
= null;
133 // Try to identify source component
135 if (peer
instanceof XCanvasPeer
)
137 source
= ((XCanvasPeer
) peer
).getComponent();
142 String msg
= "unable to locate source for event (" +
144 throw new RuntimeException(msg
);
147 /* if a mapping from anyEvent to AWTEvent is possible, construct a
148 new AWTEvent and return it. */
150 int type
= anyEvent
.getType();
153 case XAnyEvent
.TYPE_EXPOSE
:
154 return createPaintEvent(source
);
155 case XAnyEvent
.TYPE_BUTTON_PRESS
:
156 case XAnyEvent
.TYPE_BUTTON_RELEASE
:
157 return createMouseEvent(type
, source
);
158 case XAnyEvent
.TYPE_UNMAP_NOTIFY
:
159 case XAnyEvent
.TYPE_MAP_NOTIFY
:
160 case XAnyEvent
.TYPE_REPARENT_NOTIFY
:
161 return null; // ignore for now
162 case XAnyEvent
.TYPE_CONFIGURE_NOTIFY
:
163 configureNotify(peer
);
167 String msg
= "Do no know how to handle event (" + anyEvent
+ ")";
168 throw new RuntimeException(msg
);
172 AWTEvent
createPaintEvent(Component src
)
174 XExposeEvent expose
= new XExposeEvent(anyEvent
);
175 PaintEvent pe
= new PaintEvent(src
, PaintEvent
.PAINT
,
180 AWTEvent
createMouseEvent(int type
, Component src
)
182 XButtonEvent buttonEvt
= new XButtonEvent(anyEvent
);
184 int modifiers
= 0; //buttonToModifierMap[buttonEvt.button];
186 /* Warning: this makes assumptions on the contents of
187 X.h... Button1 = 1, Button2 = 2, etc... */
188 switch (buttonEvt
.button
)
191 modifiers
= InputEvent
.BUTTON1_MASK
;
194 modifiers
= InputEvent
.BUTTON2_MASK
;
197 modifiers
= InputEvent
.BUTTON2_MASK
;
201 int state
= buttonEvt
.state
;
203 // remap bits from state to modifiers:
205 if ((state
& XButtonEvent
.MASK_SHIFT
) != 0)
206 modifiers
|= InputEvent
.SHIFT_MASK
;
209 if ((state
& XButtonEvent
.MASK_CONTROL
) != 0)
210 modifiers
|= InputEvent
.CTRL_MASK
;
213 /* FIXME: we need additional X code to properly map MODn states to
216 int clickCount
= 1; // FIXME... Can't get this from X.
217 boolean popupTrigger
= false; // FIXME: look up policy somewhere
222 int id
= (type
== XAnyEvent
.TYPE_BUTTON_PRESS
) ?
223 MouseEvent
.MOUSE_PRESSED
:
224 MouseEvent
.MOUSE_RELEASED
;
226 MouseEvent me
= new MouseEvent(src
,
228 buttonEvt
.time
, modifiers
,
229 buttonEvt
.x
, buttonEvt
.y
,
230 clickCount
, popupTrigger
);
234 void configureNotify(Object peerObj
)
236 XConfigureEvent configEvent
= new XConfigureEvent(anyEvent
);
237 XFramePeer peer
= (XFramePeer
) peerObj
;
239 peer
.configureNotify(configEvent
);
242 public void flushIfIdle()
248 volatile boolean idle
= false;
250 final synchronized void setIdle(boolean idle
)
255 final synchronized boolean isIdle()