From 340b1ceb07907be113e33c54d084e53ddc93e368 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 3 Sep 2010 00:06:03 +0300 Subject: NEWT: Focus Fix + Cleanup Issueing 'requestFocus' via the native EDT dispatch loop may cause a deadlock, due to a possible implicite AWT requestFocus call (NewtCanvasAWT). Approach: RequestFocus issued directly, by Window.requestFocus() and the native EDT dispatch loop, is queued for later execution by EDT. This shall decouple a possible native windowing TK resource collision. - X11Windows.c: Add missing 'reparented' param for requestFocus to force requestFocus after reparenting. - AWTWindow.java: Add requestFocusImpl() +++ NEWT: Cleanup - Remove Event Type Bits in: - EventListener.h - NEWTEventListener.java - Remove InputEvent 'consume' status - --- src/newt/classes/com/jogamp/newt/Display.java | 6 ++--- src/newt/classes/com/jogamp/newt/Window.java | 28 +++++++++++++------- .../classes/com/jogamp/newt/awt/NewtCanvasAWT.java | 30 ++++++++++------------ .../classes/com/jogamp/newt/event/InputEvent.java | 9 ------- .../classes/com/jogamp/newt/event/NEWTEvent.java | 9 +++++++ .../com/jogamp/newt/event/NEWTEventConsumer.java | 4 +-- .../com/jogamp/newt/event/NEWTEventListener.java | 5 ---- .../com/jogamp/newt/impl/awt/AWTDisplay.java | 2 +- .../com/jogamp/newt/impl/awt/AWTWindow.java | 8 ++++++ 9 files changed, 56 insertions(+), 45 deletions(-) (limited to 'src/newt/classes') diff --git a/src/newt/classes/com/jogamp/newt/Display.java b/src/newt/classes/com/jogamp/newt/Display.java index 00abd8406..b9ef93572 100644 --- a/src/newt/classes/com/jogamp/newt/Display.java +++ b/src/newt/classes/com/jogamp/newt/Display.java @@ -181,7 +181,7 @@ public abstract class Display { } } - public boolean runCreateAndDestroyOnEDT() { + protected boolean getShallRunOnEDT() { return true; } @@ -232,7 +232,7 @@ public abstract class Display { public void runOnEDTIfAvail(boolean wait, final Runnable task) { EDTUtil _edtUtil = getEDTUtil(); - if(runCreateAndDestroyOnEDT() && null!=_edtUtil) { + if(getShallRunOnEDT() && null!=_edtUtil) { _edtUtil.invoke(wait, task); } else { task.run(); @@ -363,7 +363,7 @@ public abstract class Display { enqueueEvent(false, event); } } else { - throw new RuntimeException("Event source not a NEWT one: "+source.getClass().getName()+", "+source); + throw new RuntimeException("Event source not NEWT: "+source.getClass().getName()+", "+source); } eventTask.notifyIssuer(); } diff --git a/src/newt/classes/com/jogamp/newt/Window.java b/src/newt/classes/com/jogamp/newt/Window.java index 0ec8a109b..d002791ca 100644 --- a/src/newt/classes/com/jogamp/newt/Window.java +++ b/src/newt/classes/com/jogamp/newt/Window.java @@ -297,12 +297,21 @@ public abstract class Window implements NativeWindow, NEWTEventConsumer } public void requestFocus() { - if(0 != windowHandle) { - requestFocusImpl(); - } + enqueueRequestFocus(false); // FIXME: or shall we wait ? } protected void requestFocusImpl() {} + class RequestFocusAction implements Runnable { + public void run() { + Window.this.requestFocusImpl(); + } + } + RequestFocusAction requestFocusAction = new RequestFocusAction(); + + public void enqueueRequestFocus(boolean wait) { + runOnEDTIfAvail(wait, requestFocusAction); + } + /** * May set to a {@link FocusRunnable}, {@link FocusRunnable#run()} before Newt requests the native focus. * This allows notifying a covered window toolkit like AWT that the focus is requested, @@ -985,20 +994,19 @@ public abstract class Window implements NativeWindow, NEWTEventConsumer switch(e.getEventType()) { case WindowEvent.EVENT_WINDOW_REPAINT: if( windowIsLocked() ) { + // make sure only one repaint event is queued if(!repaintQueued) { repaintQueued=true; return false; - } else { - return true; } - } else { - if(DEBUG_IMPLEMENTATION) { + return true; + } + if(DEBUG_IMPLEMENTATION) { System.out.println("Window.windowRepaint: "+e); // Exception ee = new Exception("Window.windowRepaint: "+e); // ee.printStackTrace(); - } - repaintQueued=false; } + repaintQueued=false; // no repaint event queued break; default: break; @@ -1009,6 +1017,8 @@ public abstract class Window implements NativeWindow, NEWTEventConsumer getInnerWindow().consumeKeyEvent((KeyEvent)e); } else if(e instanceof MouseEvent) { getInnerWindow().consumeMouseEvent((MouseEvent)e); + } else { + throw new NativeWindowException("Unexpected NEWTEvent type " + e); } return true; } diff --git a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java index 6fd924e66..57647df5c 100644 --- a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java +++ b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java @@ -96,7 +96,7 @@ public class NewtCanvasAWT extends java.awt.Canvas { if(DEBUG_IMPLEMENTATION) { System.out.println("FocusActionImpl.run() "+Window.getThreadName()); } - NewtCanvasAWT.this.requestFocusAWT(); + NewtCanvasAWT.this.requestFocusAWTParent(); } } FocusActionImpl focusActionImpl = new FocusActionImpl(); @@ -213,12 +213,11 @@ public class NewtCanvasAWT extends java.awt.Canvas { } } - void requestFocusAWT() { + final void requestFocusAWTParent() { super.requestFocus(); } - public void requestFocus() { - super.requestFocus(); + final void requestFocusNEWTChild() { if(null!=newtChild) { newtChild.setFocusAction(null); newtChild.requestFocus(); @@ -226,32 +225,31 @@ public class NewtCanvasAWT extends java.awt.Canvas { } } + public void requestFocus() { + requestFocusAWTParent(); + requestFocusNEWTChild(); + } + public boolean requestFocus(boolean temporary) { boolean res = super.requestFocus(temporary); - if(res && null!=newtChild) { - newtChild.setFocusAction(null); - newtChild.requestFocus(); - newtChild.setFocusAction(focusAction); + if(res) { + requestFocusNEWTChild(); } return res; } public boolean requestFocusInWindow() { boolean res = super.requestFocusInWindow(); - if(res && null!=newtChild) { - newtChild.setFocusAction(null); - newtChild.requestFocus(); - newtChild.setFocusAction(focusAction); + if(res) { + requestFocusNEWTChild(); } return res; } public boolean requestFocusInWindow(boolean temporary) { boolean res = super.requestFocusInWindow(temporary); - if(res && null!=newtChild) { - newtChild.setFocusAction(null); - newtChild.requestFocus(); - newtChild.setFocusAction(focusAction); + if(res) { + requestFocusNEWTChild(); } return res; } diff --git a/src/newt/classes/com/jogamp/newt/event/InputEvent.java b/src/newt/classes/com/jogamp/newt/event/InputEvent.java index 02fbe59ef..d014de7f3 100644 --- a/src/newt/classes/com/jogamp/newt/event/InputEvent.java +++ b/src/newt/classes/com/jogamp/newt/event/InputEvent.java @@ -48,17 +48,9 @@ public abstract class InputEvent extends NEWTEvent protected InputEvent(int eventType, Object source, long when, int modifiers) { super(eventType, source, when); - this.consumed=false; this.modifiers=modifiers; } - public void consume() { - consumed=true; - } - - public boolean isConsumed() { - return consumed; - } public int getModifiers() { return modifiers; } @@ -94,6 +86,5 @@ public abstract class InputEvent extends NEWTEvent return "InputEvent[modifiers:"+modifiers+", "+super.toString()+"]"; } - private boolean consumed; private int modifiers; } diff --git a/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java b/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java index 9afcb840c..c85fc5f64 100644 --- a/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java +++ b/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java @@ -40,6 +40,13 @@ import java.util.*; * NEWT events are provided for notification purposes ONLY;
* The NEWT will automatically handle the event semantics internally, regardless of whether a program is receiving these events or not.
* The actual event semantic is processed before the event is send.
+ * + * Event type registry:
+ *
*/ public class NEWTEvent extends java.util.EventObject { private boolean isSystemEvent; @@ -61,6 +68,8 @@ public class NEWTEvent extends java.util.EventObject { // 2: com.jogamp.newt.Window // 2: com.jogamp.newt.event.awt.AWTNewtEventFactory // + // FIXME: verify the isSystemEvent evaluation + // static final String WindowClazzName = "com.jogamp.newt.Window" ; static final String AWTNewtEventFactoryClazzName = "com.jogamp.newt.event.awt.AWTNewtEventFactory" ; diff --git a/src/newt/classes/com/jogamp/newt/event/NEWTEventConsumer.java b/src/newt/classes/com/jogamp/newt/event/NEWTEventConsumer.java index e37c820a1..d3d897f3b 100644 --- a/src/newt/classes/com/jogamp/newt/event/NEWTEventConsumer.java +++ b/src/newt/classes/com/jogamp/newt/event/NEWTEventConsumer.java @@ -36,8 +36,8 @@ public interface NEWTEventConsumer { /** * Consume the event * - * @return true if the event can be consumed now, - * otherwise propagate it later. + * @return true if the event has been consumed, + * otherwise it returns false for later propagation. */ public boolean consumeEvent(NEWTEvent event); } diff --git a/src/newt/classes/com/jogamp/newt/event/NEWTEventListener.java b/src/newt/classes/com/jogamp/newt/event/NEWTEventListener.java index 2a187f8c4..90d00383d 100644 --- a/src/newt/classes/com/jogamp/newt/event/NEWTEventListener.java +++ b/src/newt/classes/com/jogamp/newt/event/NEWTEventListener.java @@ -37,10 +37,5 @@ import com.jogamp.newt.*; public interface NEWTEventListener extends java.util.EventListener { - public static final int WINDOW = 1 << 0; - public static final int MOUSE = 1 << 1; - public static final int KEY = 1 << 2; - public static final int SURFACE = 1 << 3; - } diff --git a/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java b/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java index e705d364a..92c4e53eb 100644 --- a/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java +++ b/src/newt/classes/com/jogamp/newt/impl/awt/AWTDisplay.java @@ -54,7 +54,7 @@ public class AWTDisplay extends Display { protected void closeNative() { } - public boolean runCreateAndDestroyOnEDT() { + protected boolean getShallRunOnEDT() { return false; } protected void dispatchMessagesNative() { /* nop */ } diff --git a/src/newt/classes/com/jogamp/newt/impl/awt/AWTWindow.java b/src/newt/classes/com/jogamp/newt/impl/awt/AWTWindow.java index 56e5a4240..db55aee6f 100644 --- a/src/newt/classes/com/jogamp/newt/impl/awt/AWTWindow.java +++ b/src/newt/classes/com/jogamp/newt/impl/awt/AWTWindow.java @@ -84,6 +84,14 @@ public class AWTWindow extends Window { // non fullscreen dimensions .. private int nfs_width, nfs_height, nfs_x, nfs_y; + protected void requestFocusImpl() { + runOnEDT(true, new Runnable() { + public void run() { + container.requestFocus(); + } + }); + } + protected void setTitleImpl(final String title) { runOnEDT(true, new Runnable() { public void run() { -- cgit v1.2.3