diff options
Diffstat (limited to 'src/newt')
25 files changed, 729 insertions, 683 deletions
diff --git a/src/newt/classes/com/jogamp/newt/Display.java b/src/newt/classes/com/jogamp/newt/Display.java index 193bb26dc..deb4c7abe 100755 --- a/src/newt/classes/com/jogamp/newt/Display.java +++ b/src/newt/classes/com/jogamp/newt/Display.java @@ -182,10 +182,9 @@ public abstract class Display { return true; } public EDTUtil getEDTUtil() { - if( !edtQueried ) { + if( null == edtUtil ) { synchronized (this) { - if( !edtQueried ) { - edtQueried = true; + if( null == edtUtil ) { if(NewtFactory.useEDT()) { final Display f_dpy = this; Thread current = Thread.currentThread(); @@ -203,12 +202,11 @@ public abstract class Display { } return edtUtil; } - volatile boolean edtQueried = false; public void runOnEDTIfAvail(boolean wait, final Runnable task) { - EDTUtil edtUtil = getEDTUtil(); - if(runCreateAndDestroyOnEDT() && null!=edtUtil) { - edtUtil.invoke(wait, task); + EDTUtil _edtUtil = getEDTUtil(); + if(runCreateAndDestroyOnEDT() && null!=_edtUtil) { + _edtUtil.invoke(wait, task); } else { task.run(); } @@ -238,6 +236,7 @@ public abstract class Display { if(null!=edtUtil) { edtUtil.waitUntilStopped(); edtUtil=null; + edt=null; } aDevice = null; } else { @@ -318,11 +317,12 @@ public abstract class Display { private LinkedList/*<NEWTEvent>*/ events = new LinkedList(); protected void dispatchMessages() { - if(0==refCount) return; // we should not exist .. + if(0==refCount) return; // in destruction .. + + LinkedList/*<NEWTEvent>*/ _events = null; if(!events.isEmpty()) { // swap events list to free ASAP - LinkedList/*<NEWTEvent>*/ _events = null; synchronized(eventsLock) { if(!events.isEmpty()) { _events = events; @@ -335,10 +335,13 @@ public abstract class Display { NEWTEventTask eventTask = (NEWTEventTask) iter.next(); NEWTEvent event = eventTask.get(); Object source = event.getSource(); - if(source instanceof Window) { - ((Window)source).sendEvent(event); + if(source instanceof NEWTEventConsumer) { + NEWTEventConsumer consumer = (NEWTEventConsumer) source ; + if(!consumer.consumeEvent(event)) { + enqueueEvent(false, event); + } } else { - throw new RuntimeException("Event source not a NEWT Window: "+source.getClass().getName()+", "+source); + throw new RuntimeException("Event source not a NEWT one: "+source.getClass().getName()+", "+source); } eventTask.notifyIssuer(); } @@ -353,10 +356,6 @@ public abstract class Display { } } - public void enqueueEvent(NEWTEvent e) { - enqueueEvent(false, e); - } - public void enqueueEvent(boolean wait, NEWTEvent e) { Object lock = new Object(); NEWTEventTask eTask = new NEWTEventTask(e, wait?lock:null); diff --git a/src/newt/classes/com/jogamp/newt/Window.java b/src/newt/classes/com/jogamp/newt/Window.java index e9289f5dc..0cd5b31bc 100755 --- a/src/newt/classes/com/jogamp/newt/Window.java +++ b/src/newt/classes/com/jogamp/newt/Window.java @@ -34,11 +34,12 @@ package com.jogamp.newt; import com.jogamp.newt.event.*; +import com.jogamp.newt.util.*; import com.jogamp.newt.impl.Debug; -import com.jogamp.newt.util.EDTUtil; import com.jogamp.common.util.*; import javax.media.nativewindow.*; +import com.jogamp.nativewindow.util.Rectangle; import com.jogamp.nativewindow.impl.RecursiveToolkitLock; import java.util.ArrayList; @@ -46,7 +47,7 @@ import java.util.List; import java.util.Iterator; import java.lang.reflect.Method; -public abstract class Window implements NativeWindow +public abstract class Window implements NativeWindow, NEWTEventConsumer { public static final boolean DEBUG_MOUSE_EVENT = Debug.debug("Window.MouseEvent"); public static final boolean DEBUG_KEY_EVENT = Debug.debug("Window.KeyEvent"); @@ -155,7 +156,7 @@ public abstract class Window implements NativeWindow return 0 != windowHandle ; } if(DEBUG_IMPLEMENTATION) { - System.err.println("Window.createNative() START ("+getThreadName()+", "+this+")"); + System.out.println("Window.createNative() START ("+getThreadName()+", "+this+")"); } if(validateParentWindowHandle()) { Display dpy = getScreen().getDisplay(); @@ -163,7 +164,7 @@ public abstract class Window implements NativeWindow setVisibleImpl(true); } if(DEBUG_IMPLEMENTATION) { - System.err.println("Window.createNative() END ("+getThreadName()+", "+this+")"); + System.out.println("Window.createNative() END ("+getThreadName()+", "+this+")"); } return 0 != windowHandle ; } @@ -190,7 +191,7 @@ public abstract class Window implements NativeWindow } } catch (NativeWindowException nwe) { if(DEBUG_IMPLEMENTATION) { - System.err.println("Window.getNativeWindowHandle: not successful yet: "+nwe); + System.out.println("Window.getNativeWindowHandle: not successful yet: "+nwe); } } finally { if(locked) { @@ -198,7 +199,7 @@ public abstract class Window implements NativeWindow } } if(DEBUG_IMPLEMENTATION) { - System.err.println("Window.getNativeWindowHandle: locked "+locked+", "+nativeWindow); + System.out.println("Window.getNativeWindowHandle: locked "+locked+", "+nativeWindow); } } return handle; @@ -245,7 +246,8 @@ public abstract class Window implements NativeWindow "\n, Visible "+isVisible()+ "\n, Undecorated "+undecorated+ "\n, Fullscreen "+fullscreen+ - "\n, WrappedWindow "+getWrappedWindow()); + "\n, WrappedWindow "+getWrappedWindow()+ + "\n, ChildWindows "+childWindows.size()); sb.append(", SurfaceUpdatedListeners num "+surfaceUpdatedListeners.size()+" ["); for (Iterator iter = surfaceUpdatedListeners.iterator(); iter.hasNext(); ) { @@ -376,41 +378,38 @@ public abstract class Window implements NativeWindow public void run() { windowLock(); try { - if(DEBUG_WINDOW_EVENT) { - System.err.println("Window.destroy(deep: "+deep+") START "+getThreadName()+", "+this); + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.destroy(deep: "+deep+") START "+getThreadName()+", "+Window.this); } // Childs first .. - ArrayList listeners = null; - synchronized(childWindows) { - listeners = childWindows; - } - for(Iterator i = listeners.iterator(); i.hasNext(); ) { + synchronized(childWindowsLock) { + for(Iterator i = childWindows.iterator(); i.hasNext(); ) { NativeWindow nw = (NativeWindow) i.next(); + System.out.println("Window.destroy(deep: "+deep+") CHILD BEGIN"); if(nw instanceof Window) { - ((Window)nw).destroy(deep); + ((Window)nw).sendWindowEvent(WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY); + if(deep) { + ((Window)nw).destroy(deep); + } } else { nw.destroy(); } + System.out.println("Window.destroy(deep: "+deep+") CHILD END"); + } } // Now us .. if(deep) { - synchronized(childWindows) { + synchronized(childWindowsLock) { childWindows = new ArrayList(); } - synchronized(surfaceUpdatedListeners) { + synchronized(surfaceUpdatedListenersLock) { surfaceUpdatedListeners = new ArrayList(); } - synchronized(windowListeners) { - windowListeners = new ArrayList(); - } - synchronized(mouseListeners) { - mouseListeners = new ArrayList(); - } - synchronized(keyListeners) { - keyListeners = new ArrayList(); - } + windowListeners = new ArrayList(); + mouseListeners = new ArrayList(); + keyListeners = new ArrayList(); } Display dpy = null; Screen scr = null; @@ -428,8 +427,8 @@ public abstract class Window implements NativeWindow dpy.destroy(); } } - if(DEBUG_WINDOW_EVENT) { - System.err.println("Window.destroy(deep: "+deep+") END "+getThreadName()+", "+this); + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.destroy(deep: "+deep+") END "+getThreadName()+", "+Window.this); } } finally { windowUnlock(); @@ -466,16 +465,16 @@ public abstract class Window implements NativeWindow try{ if(DEBUG_IMPLEMENTATION || DEBUG_WINDOW_EVENT) { String msg = new String("!!! Window Invalidate(deep: "+deep+") "+getThreadName()); - System.err.println(msg); - //Exception e = new Exception(msg); - //e.printStackTrace(); + //System.out.println(msg); + Exception e = new Exception(msg); + e.printStackTrace(); } windowHandle = 0; - visible=false; - fullscreen=false; + visible = false; + fullscreen = false; if(deep) { - screen = null; + screen = null; parentWindowHandle = 0; parentNativeWindow = null; caps = null; @@ -593,43 +592,6 @@ public abstract class Window implements NativeWindow return false; } - /** - * If set to true, the default value, this NEWT Window implementation will - * handle the destruction (ie {@link #destroy()} call) within {@link #windowDestroyNotify()} implementation.<br> - * If set to false, it's up to the caller/owner to handle destruction within {@link #windowDestroyNotify()}. - */ - public void setHandleDestroyNotify(boolean b) { - handleDestroyNotify = b; - } - - protected void windowDestroyNotify() { - if(DEBUG_WINDOW_EVENT) { - System.err.println("Window.windowDestroyNotify START "+getThreadName()); - } - - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY); - - if(handleDestroyNotify && !isDestroyed()) { - destroy(); - } - - if(DEBUG_WINDOW_EVENT) { - System.err.println("Window.windowDestroyeNotify END "+getThreadName()); - } - } - - protected void windowDestroyed() { - if(DEBUG_WINDOW_EVENT) { - System.err.println("Window.windowDestroyed "+getThreadName()); - } - invalidate(); - } - - protected boolean reparentWindowImpl() { - // default implementation, no native reparenting support - return false; - } - class ReparentAction implements Runnable { NativeWindow newParent; Screen newScreen; @@ -652,7 +614,7 @@ public abstract class Window implements NativeWindow } if(DEBUG_IMPLEMENTATION) { - System.err.println("reparent: START ("+getThreadName()+") windowHandle "+toHexString(windowHandle)+" parentWindowHandle "+toHexString(parentWindowHandle)+" -> "+toHexString(newParentHandle)+", visible "+visible+", parentNativeWindow "+(null!=parentNativeWindow)); + System.out.println("reparent: START ("+getThreadName()+") windowHandle "+toHexString(windowHandle)+" parentWindowHandle "+toHexString(parentWindowHandle)+" -> "+toHexString(newParentHandle)+", visible "+visible+", parentNativeWindow "+(null!=parentNativeWindow)); } if(null!=parentNativeWindow && parentNativeWindow instanceof Window) { @@ -671,8 +633,16 @@ public abstract class Window implements NativeWindow x = 0; y = 0; } + getScreen().getDisplay().dispatchMessages(); // status up2date + boolean wasVisible = isVisible(); + if(wasVisible) { + Window.this.visible = false; + setVisibleImpl(false); + getScreen().getDisplay().dispatchMessages(); // status up2date + } boolean reparentRes = false; reparentRes = reparentWindowImpl(); + getScreen().getDisplay().dispatchMessages(); // status up2date if(!reparentRes) { parentWindowHandle = 0; @@ -680,11 +650,17 @@ public abstract class Window implements NativeWindow if( 0 != windowHandle ) { destroy(false); } + } else { + if(wasVisible) { + Window.this.visible = true; + setVisibleImpl(true); + getScreen().getDisplay().dispatchMessages(); // status up2date + } } } if(DEBUG_IMPLEMENTATION) { - System.err.println("reparentWindow: END ("+getThreadName()+") windowHandle "+toHexString(windowHandle)+", visible: "+visible+", parentWindowHandle "+toHexString(parentWindowHandle)+", parentNativeWindow "+(null!=parentNativeWindow)); + System.out.println("reparentWindow: END ("+getThreadName()+") windowHandle "+toHexString(windowHandle)+", visible: "+visible+", parentWindowHandle "+toHexString(parentWindowHandle)+", parentNativeWindow "+(null!=parentNativeWindow)); } } finally { windowUnlock(); @@ -707,9 +683,10 @@ public abstract class Window implements NativeWindow public void reparentWindow(NativeWindow newParent, Screen newScreen) { if(!isDestroyed()) { runOnEDTIfAvail(true, new ReparentAction(newParent, newScreen)); - // if( isVisible() ) { - enqueueWindowEvent(true, WindowEvent.EVENT_WINDOW_RESIZED); // trigger a resize/relayout to listener - // } + if( isVisible() ) { + sendWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); // trigger a resize/relayout to listener + windowRepaint(0, 0, getWidth(), getHeight()); + } } } @@ -722,17 +699,15 @@ public abstract class Window implements NativeWindow windowLock(); try{ if( !isDestroyed() ) { - ArrayList listeners = null; - synchronized(childWindows) { - listeners = childWindows; - } - if(!visible && listeners.size()>0) { - for(Iterator i = listeners.iterator(); i.hasNext(); ) { + if(!visible && childWindows.size()>0) { + synchronized(childWindowsLock) { + for(Iterator i = childWindows.iterator(); i.hasNext(); ) { NativeWindow nw = (NativeWindow) i.next(); if(nw instanceof Window) { ((Window)nw).setVisible(false); } } + } } if(0==windowHandle && visible) { Window.this.visible = visible; @@ -745,18 +720,20 @@ public abstract class Window implements NativeWindow setVisibleImpl(visible); } } - if(0!=windowHandle && visible && listeners.size()>0) { - for(Iterator i = listeners.iterator(); i.hasNext(); ) { + if(0!=windowHandle && visible && childWindows.size()>0) { + synchronized(childWindowsLock) { + for(Iterator i = childWindows.iterator(); i.hasNext(); ) { NativeWindow nw = (NativeWindow) i.next(); if(nw instanceof Window) { ((Window)nw).setVisible(true); } } + } } } if(DEBUG_IMPLEMENTATION) { - System.err.println("Window setVisible: END ("+getThreadName()+") "+x+"/"+y+" "+width+"x"+height+", fs "+fullscreen+", windowHandle "+toHexString(windowHandle)+", visible: "+Window.this.visible); + System.out.println("Window setVisible: END ("+getThreadName()+") "+x+"/"+y+" "+width+"x"+height+", fs "+fullscreen+", windowHandle "+toHexString(windowHandle)+", visible: "+Window.this.visible); } } finally { windowUnlock(); @@ -792,7 +769,7 @@ public abstract class Window implements NativeWindow public void setVisible(boolean visible) { if(DEBUG_IMPLEMENTATION) { String msg = new String("Window setVisible: START ("+getThreadName()+") "+x+"/"+y+" "+width+"x"+height+", fs "+fullscreen+", windowHandle "+toHexString(windowHandle)+", visible: "+this.visible+" -> "+visible+", parentWindowHandle "+toHexString(parentWindowHandle)+", parentNativeWindow "+(null!=parentNativeWindow)); - //System.err.println(msg); + //System.out.println(msg); Exception ee = new Exception(msg); ee.printStackTrace(); } @@ -829,7 +806,7 @@ public abstract class Window implements NativeWindow try{ if(DEBUG_IMPLEMENTATION) { String msg = new String("Window setSize: START "+this.width+"x"+this.height+" -> "+width+"x"+height+", fs "+fullscreen+", windowHandle "+toHexString(windowHandle)+", visible "+visible); - //System.err.println(msg); + //System.out.println(msg); Exception e = new Exception(msg); e.printStackTrace(); } @@ -846,7 +823,7 @@ public abstract class Window implements NativeWindow this.width = width; this.height = height; } else if ( 0 != windowHandle ) { - // this width/height will be set by windowChanged, called by X11 + // this width/height will be set by windowChanged, called by the native implementation setSizeImpl(width, height); } else { this.width = width; @@ -855,7 +832,7 @@ public abstract class Window implements NativeWindow } } if(DEBUG_IMPLEMENTATION) { - System.err.println("Window setSize: END "+this.width+"x"+this.height+", visibleAction "+visibleAction); + System.out.println("Window setSize: END "+this.width+"x"+this.height+", visibleAction "+visibleAction); } } finally { windowUnlock(); @@ -880,7 +857,7 @@ public abstract class Window implements NativeWindow windowLock(); try{ if(DEBUG_IMPLEMENTATION) { - System.err.println("Window setPosition: "+this.x+"/"+this.y+" -> "+x+"/"+y+", fs "+fullscreen+", windowHandle "+toHexString(windowHandle)); + System.out.println("Window setPosition: "+this.x+"/"+this.y+" -> "+x+"/"+y+", fs "+fullscreen+", windowHandle "+toHexString(windowHandle)); } if ( this.x != x || this.y != y ) { if(!fullscreen) { @@ -917,7 +894,7 @@ public abstract class Window implements NativeWindow h = nfs_height; } if(DEBUG_IMPLEMENTATION || DEBUG_WINDOW_EVENT) { - System.err.println("X11Window fs: "+fullscreen+" "+x+"/"+y+" "+w+"x"+h+", "+isUndecorated()); + System.out.println("X11Window fs: "+fullscreen+" "+x+"/"+y+" "+w+"x"+h+", "+isUndecorated()); } this.fullscreen = setFullscreenImpl(fullscreen, x, y, w, h); } @@ -933,12 +910,11 @@ public abstract class Window implements NativeWindow // private ArrayList childWindows = new ArrayList(); + private Object childWindowsLock = new Object(); protected void removeChild(NativeWindow win) { - synchronized(childWindows) { - ArrayList newChildWindows = (ArrayList) childWindows.clone(); - newChildWindows.remove(win); - childWindows = newChildWindows; + synchronized(childWindowsLock) { + childWindows.remove(win); } } @@ -946,34 +922,72 @@ public abstract class Window implements NativeWindow if (win == null) { return; } - synchronized(childWindows) { - ArrayList newChildWindows = (ArrayList) childWindows.clone(); - newChildWindows.add(win); - childWindows = newChildWindows; + synchronized(childWindowsLock) { + childWindows.add(win); } } // // Generic Event Support // + private void doEvent(boolean enqueue, boolean wait, com.jogamp.newt.event.NEWTEvent event) { + boolean done = false; - public void sendEvent(NEWTEvent e) { + if(!enqueue) { + done = consumeEvent(event); + wait = done; // don't wait if event can't be consumed now + } + + if(!done) { + enqueueEvent(wait, event); + } + } + + public void enqueueEvent(boolean wait, com.jogamp.newt.event.NEWTEvent event) { + if(!getInnerWindow().isDestroyed()) { + getInnerWindow().getScreen().getDisplay().enqueueEvent(wait, event); + } + } + + public boolean consumeEvent(NEWTEvent e) { + switch(e.getEventType()) { + case WindowEvent.EVENT_WINDOW_REPAINT: + if( windowIsLocked() ) { + if(!repaintQueued) { + repaintQueued=true; + return false; + } else { + return true; + } + } else { + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.windowRepaint: "+e); + // Exception ee = new Exception("Window.windowRepaint: "+e); + // ee.printStackTrace(); + } + repaintQueued=false; + } + break; + default: + break; + } if(e instanceof WindowEvent) { - sendWindowEvent((WindowEvent)e); + getInnerWindow().consumeWindowEvent((WindowEvent)e); } else if(e instanceof KeyEvent) { - sendKeyEvent((KeyEvent)e); + getInnerWindow().consumeKeyEvent((KeyEvent)e); } else if(e instanceof MouseEvent) { - sendMouseEvent((MouseEvent)e); - } else if(e instanceof PaintEvent) { - sendPaintEvent((PaintEvent)e); + getInnerWindow().consumeMouseEvent((MouseEvent)e); } + return true; } + protected boolean repaintQueued = false; // // SurfaceUpdatedListener Support // private ArrayList surfaceUpdatedListeners = new ArrayList(); + private Object surfaceUpdatedListenersLock = new Object(); /** * Appends the given {@link com.jogamp.newt.event.SurfaceUpdatedListener} to the end of @@ -999,13 +1013,11 @@ public abstract class Window implements NativeWindow if(l == null) { return; } - synchronized(surfaceUpdatedListeners) { + synchronized(surfaceUpdatedListenersLock) { if(0>index) { index = surfaceUpdatedListeners.size(); } - ArrayList newSurfaceUpdatedListeners = (ArrayList) surfaceUpdatedListeners.clone(); - newSurfaceUpdatedListeners.add(index, l); - surfaceUpdatedListeners = newSurfaceUpdatedListeners; + surfaceUpdatedListeners.add(index, l); } } @@ -1013,21 +1025,19 @@ public abstract class Window implements NativeWindow if (l == null) { return; } - synchronized(surfaceUpdatedListeners) { - ArrayList newSurfaceUpdatedListeners = (ArrayList) surfaceUpdatedListeners.clone(); - newSurfaceUpdatedListeners.remove(l); - surfaceUpdatedListeners = newSurfaceUpdatedListeners; + synchronized(surfaceUpdatedListenersLock) { + surfaceUpdatedListeners.remove(l); } } public void removeAllSurfaceUpdatedListener() { - synchronized(surfaceUpdatedListeners) { + synchronized(surfaceUpdatedListenersLock) { surfaceUpdatedListeners = new ArrayList(); } } public SurfaceUpdatedListener getSurfaceUpdatedListener(int index) { - synchronized(surfaceUpdatedListeners) { + synchronized(surfaceUpdatedListenersLock) { if(0>index) { index = surfaceUpdatedListeners.size()-1; } @@ -1036,19 +1046,17 @@ public abstract class Window implements NativeWindow } public SurfaceUpdatedListener[] getSurfaceUpdatedListeners() { - synchronized(surfaceUpdatedListeners) { + synchronized(surfaceUpdatedListenersLock) { return (SurfaceUpdatedListener[]) surfaceUpdatedListeners.toArray(); } } public void surfaceUpdated(Object updater, NativeWindow window, long when) { - ArrayList listeners = null; - synchronized(surfaceUpdatedListeners) { - listeners = surfaceUpdatedListeners; - } - for(Iterator i = listeners.iterator(); i.hasNext(); ) { + synchronized(surfaceUpdatedListenersLock) { + for(Iterator i = surfaceUpdatedListeners.iterator(); i.hasNext(); ) { SurfaceUpdatedListener l = (SurfaceUpdatedListener) i.next(); l.surfaceUpdated(updater, window, when); + } } } @@ -1061,13 +1069,21 @@ public abstract class Window implements NativeWindow private int lastMouseClickCount = 0; // last mouse button click count public static final int ClickTimeout = 300; - protected void enqueueMouseEvent(int eventType, int modifiers, + public void sendMouseEvent(int eventType, int modifiers, + int x, int y, int button, int rotation) { + doMouseEvent(false, false, eventType, modifiers, x, y, button, rotation); + } + public void enqueueMouseEvent(boolean wait, int eventType, int modifiers, int x, int y, int button, int rotation) { + doMouseEvent(true, wait, eventType, modifiers, x, y, button, rotation); + } + private void doMouseEvent(boolean enqueue, boolean wait, int eventType, int modifiers, + int x, int y, int button, int rotation) { if(x<0||y<0||x>=width||y>=height) { return; // .. invalid .. } if(DEBUG_MOUSE_EVENT) { - System.err.println("enqueueMouseEvent: "+MouseEvent.getEventTypeString(eventType)+ + System.out.println("doMouseEvent: enqueue"+enqueue+", wait "+wait+", "+MouseEvent.getEventTypeString(eventType)+ ", mod "+modifiers+", pos "+x+"/"+y+", button "+button); } if(button<0||button>MouseEvent.BUTTON_NUMBER) { @@ -1111,12 +1127,12 @@ public abstract class Window implements NativeWindow } else { e = new MouseEvent(eventType, this, when, modifiers, x, y, 0, button, 0); } - screen.getDisplay().enqueueEvent(e); + doEvent(enqueue, wait, e); if(null!=eClicked) { if(DEBUG_MOUSE_EVENT) { - System.err.println("enqueueMouseEvent: synthesized MOUSE_CLICKED event"); + System.out.println("doMouseEvent: synthesized MOUSE_CLICKED event"); } - screen.getDisplay().enqueueEvent(eClicked); + doEvent(enqueue, wait, eClicked); } } @@ -1143,52 +1159,41 @@ public abstract class Window implements NativeWindow if(l == null) { return; } - synchronized(mouseListeners) { - if(0>index) { - index = mouseListeners.size(); - } - ArrayList newMouseListeners = (ArrayList) mouseListeners.clone(); - newMouseListeners.add(index, l); - mouseListeners = newMouseListeners; + ArrayList clonedListeners = (ArrayList) mouseListeners.clone(); + if(0>index) { + index = clonedListeners.size(); } + clonedListeners.add(index, l); + mouseListeners = clonedListeners; } public void removeMouseListener(MouseListener l) { if (l == null) { return; } - synchronized(mouseListeners) { - ArrayList newMouseListeners = (ArrayList) mouseListeners.clone(); - newMouseListeners.remove(l); - mouseListeners = newMouseListeners; - } + ArrayList clonedListeners = (ArrayList) mouseListeners.clone(); + clonedListeners.remove(l); + mouseListeners = clonedListeners; } public MouseListener getMouseListener(int index) { - synchronized(mouseListeners) { - if(0>index) { - index = mouseListeners.size()-1; - } - return (MouseListener) mouseListeners.get(index); + ArrayList clonedListeners = (ArrayList) mouseListeners.clone(); + if(0>index) { + index = clonedListeners.size()-1; } + return (MouseListener) clonedListeners.get(index); } public MouseListener[] getMouseListeners() { - synchronized(mouseListeners) { - return (MouseListener[]) mouseListeners.toArray(); - } + return (MouseListener[]) mouseListeners.toArray(); } - protected void sendMouseEvent(MouseEvent e) { + protected void consumeMouseEvent(MouseEvent e) { if(DEBUG_MOUSE_EVENT) { - System.err.println("sendMouseEvent: event: "+e); + System.out.println("consumeMouseEvent: event: "+e); } - ArrayList listeners = null; - synchronized(mouseListeners) { - listeners = mouseListeners; - } - for(Iterator i = listeners.iterator(); i.hasNext(); ) { + for(Iterator i = mouseListeners.iterator(); i.hasNext(); ) { MouseListener l = (MouseListener) i.next(); switch(e.getEventType()) { case MouseEvent.EVENT_MOUSE_CLICKED: @@ -1225,10 +1230,12 @@ public abstract class Window implements NativeWindow // KeyListener/Event Support // - protected void enqueueKeyEvent(int eventType, int modifiers, int keyCode, char keyChar) { - screen.getDisplay().enqueueEvent( - new KeyEvent(eventType, this, System.currentTimeMillis(), - modifiers, keyCode, keyChar) ); + public void sendKeyEvent(int eventType, int modifiers, int keyCode, char keyChar) { + consumeKeyEvent(new KeyEvent(eventType, this, System.currentTimeMillis(), modifiers, keyCode, keyChar) ); + } + + public void enqueueKeyEvent(boolean wait, int eventType, int modifiers, int keyCode, char keyChar) { + enqueueEvent(wait, new KeyEvent(eventType, this, System.currentTimeMillis(), modifiers, keyCode, keyChar) ); } /** @@ -1253,53 +1260,42 @@ public abstract class Window implements NativeWindow if(l == null) { return; } - synchronized(keyListeners) { - if(0>index) { - index = keyListeners.size(); - } - ArrayList newKeyListeners = (ArrayList) keyListeners.clone(); - newKeyListeners.add(index, l); - keyListeners = newKeyListeners; + ArrayList clonedListeners = (ArrayList) keyListeners.clone(); + if(0>index) { + index = clonedListeners.size(); } + clonedListeners.add(index, l); + keyListeners = clonedListeners; } public void removeKeyListener(KeyListener l) { if (l == null) { return; } - synchronized(keyListeners) { - ArrayList newKeyListeners = (ArrayList) keyListeners.clone(); - newKeyListeners.remove(l); - keyListeners = newKeyListeners; - } + ArrayList clonedListeners = (ArrayList) keyListeners.clone(); + clonedListeners.remove(l); + keyListeners = clonedListeners; } public KeyListener getKeyListener(int index) { - synchronized(keyListeners) { - if(0>index) { - index = keyListeners.size()-1; - } - return (KeyListener) keyListeners.get(index); + ArrayList clonedListeners = (ArrayList) keyListeners.clone(); + if(0>index) { + index = clonedListeners.size()-1; } + return (KeyListener) clonedListeners.get(index); } public KeyListener[] getKeyListeners() { - synchronized(keyListeners) { - return (KeyListener[]) keyListeners.toArray(); - } + return (KeyListener[]) keyListeners.toArray(); } private ArrayList keyListeners = new ArrayList(); - protected void sendKeyEvent(KeyEvent e) { + protected void consumeKeyEvent(KeyEvent e) { if(DEBUG_KEY_EVENT) { - System.err.println("sendKeyEvent: "+e); - } - ArrayList listeners = null; - synchronized(keyListeners) { - listeners = keyListeners; + System.out.println("consumeKeyEvent: "+e); } - for(Iterator i = listeners.iterator(); i.hasNext(); ) { + for(Iterator i = keyListeners.iterator(); i.hasNext(); ) { KeyListener l = (KeyListener) i.next(); switch(e.getEventType()) { case KeyEvent.EVENT_KEY_PRESSED: @@ -1320,14 +1316,12 @@ public abstract class Window implements NativeWindow // // WindowListener/Event Support // - protected void enqueueWindowEvent(int eventType) { - enqueueWindowEvent(false, eventType); + public void sendWindowEvent(int eventType) { + consumeWindowEvent( new WindowEvent(eventType, this, System.currentTimeMillis()) ); } - protected void enqueueWindowEvent(boolean wait, int eventType) { - WindowEvent event = new WindowEvent(eventType, this, System.currentTimeMillis()); - screen.getDisplay().enqueueEvent( wait, event ); - // sendWindowEvent ( event ); // FIXME: Think about performance/lag .. ? + public void enqueueWindowEvent(boolean wait, int eventType) { + enqueueEvent( wait, new WindowEvent(eventType, this, System.currentTimeMillis()) ); } private ArrayList windowListeners = new ArrayList(); @@ -1356,51 +1350,40 @@ public abstract class Window implements NativeWindow if(l == null) { return; } - synchronized(windowListeners) { - if(0>index) { - index = windowListeners.size(); - } - ArrayList newWindowListeners = (ArrayList) windowListeners.clone(); - newWindowListeners.add(index, l); - windowListeners = newWindowListeners; + ArrayList clonedListeners = (ArrayList) windowListeners.clone(); + if(0>index) { + index = clonedListeners.size(); } + clonedListeners.add(index, l); + windowListeners = clonedListeners; } public void removeWindowListener(WindowListener l) { if (l == null) { return; } - synchronized(windowListeners) { - ArrayList newWindowListeners = (ArrayList) windowListeners.clone(); - newWindowListeners.remove(l); - windowListeners = newWindowListeners; - } + ArrayList clonedListeners = (ArrayList) windowListeners.clone(); + clonedListeners.remove(l); + windowListeners = clonedListeners; } public WindowListener getWindowListener(int index) { - synchronized(windowListeners) { - if(0>index) { - index = windowListeners.size()-1; - } - return (WindowListener) windowListeners.get(index); + ArrayList clonedListeners = (ArrayList) windowListeners.clone(); + if(0>index) { + index = clonedListeners.size()-1; } + return (WindowListener) clonedListeners.get(index); } public WindowListener[] getWindowListeners() { - synchronized(windowListeners) { - return (WindowListener[]) windowListeners.toArray(); - } + return (WindowListener[]) windowListeners.toArray(); } - protected void sendWindowEvent(WindowEvent e) { + protected void consumeWindowEvent(WindowEvent e) { if(DEBUG_WINDOW_EVENT) { - System.err.println("sendWindowEvent: "+e); - } - ArrayList listeners = null; - synchronized(windowListeners) { - listeners = windowListeners; + System.out.println("consumeWindowEvent: "+e); } - for(Iterator i = listeners.iterator(); i.hasNext(); ) { + for(Iterator i = windowListeners.iterator(); i.hasNext(); ) { WindowListener l = (WindowListener) i.next(); switch(e.getEventType()) { case WindowEvent.EVENT_WINDOW_RESIZED: @@ -1418,6 +1401,9 @@ public abstract class Window implements NativeWindow case WindowEvent.EVENT_WINDOW_LOST_FOCUS: l.windowLostFocus(e); break; + case WindowEvent.EVENT_WINDOW_REPAINT: + l.windowRepaint((WindowUpdateEvent)e); + break; default: throw new NativeWindowException("Unexpected window event type " @@ -1426,78 +1412,113 @@ public abstract class Window implements NativeWindow } } - - // - // PaintListener/Event Support - // - - private ArrayList paintListeners = new ArrayList(); - - /** - * Appends the given {@link com.jogamp.newt.event.PaintListener} to the end of - * the list. + /** + * @param focusGained */ - public void addPaintListener(PaintListener l) { - getInnerWindow().addPaintListener(-1, l); + protected void focusChanged(boolean focusGained) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.focusChanged: "+focusGained); + } + if (focusGained) { + sendWindowEvent(WindowEvent.EVENT_WINDOW_GAINED_FOCUS); + } else { + sendWindowEvent(WindowEvent.EVENT_WINDOW_LOST_FOCUS); + } } - /** - * Inserts the given {@link com.jogamp.newt.event.PaintListener} at the - * specified position in the list.<br> + protected void visibleChanged(boolean visible) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.visibleChanged: "+this.visible+" -> "+visible); + } + this.visible = visible ; + } - * @param index Position where the listener will be inserted. - * Should be within (0 <= index && index <= size()). - * An index value of -1 is interpreted as the end of the list, size(). - * @param l The listener object to be inserted - * @throws IndexOutOfBoundsException If the index is not within (0 <= index && index <= size()), or -1 - */ - public void addPaintListener(int index, PaintListener l) { - if(l == null) { - return; + protected void sizeChanged(int newWidth, int newHeight) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.sizeChanged: "+width+"x"+height+" -> "+newWidth+"x"+newHeight); + } + if(width != newWidth || height != newHeight) { + width = newWidth; + height = newHeight; + if(!fullscreen) { + nfs_width=width; + nfs_height=height; + } + sendWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); } - synchronized(paintListeners) { - if(0>index) { - index = paintListeners.size(); + } + + protected void positionChanged(int newX, int newY) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.positionChanged: "+x+"/"+y+" -> "+newX+"/"+newY); + } + if( 0==parentWindowHandle && ( x != newX || y != newY ) ) { + x = newX; + y = newY; + if(!fullscreen) { + nfs_x=x; + nfs_y=y; } - ArrayList newPaintListeners = (ArrayList) paintListeners.clone(); - newPaintListeners.add(index, l); - paintListeners = newPaintListeners; + sendWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); } } - public void removePaintListener(PaintListener l) { - if (l == null) { - return; + /** + * If set to true, the default value, this NEWT Window implementation will + * handle the destruction (ie {@link #destroy()} call) within {@link #windowDestroyNotify()} implementation.<br> + * If set to false, it's up to the caller/owner to handle destruction within {@link #windowDestroyNotify()}. + */ + public void setHandleDestroyNotify(boolean b) { + handleDestroyNotify = b; + } + + protected void windowDestroyNotify() { + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.windowDestroyNotify START "+getThreadName()); } - synchronized(paintListeners) { - ArrayList newPaintListeners = (ArrayList) paintListeners.clone(); - newPaintListeners.remove(l); - paintListeners = newPaintListeners; + + enqueueWindowEvent(false, WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY); + + if(handleDestroyNotify && !isDestroyed()) { + destroy(); + } + + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.windowDestroyeNotify END "+getThreadName()); } } - public PaintListener getPaintListener(int index) { - synchronized(paintListeners) { - if(0>index) { - index = paintListeners.size()-1; - } - return (PaintListener) paintListeners.get(index); + protected void windowDestroyed() { + if(DEBUG_IMPLEMENTATION) { + System.out.println("Window.windowDestroyed "+getThreadName()); } + invalidate(); } - protected void sendPaintEvent(int eventType, int x, int y, int w, int h) { - sendPaintEvent( new PaintEvent(eventType, this, System.currentTimeMillis(), x, y, w, h) ); + public void setPropagateRepaint(boolean v) { + propagateRepaint = v; } + protected boolean propagateRepaint = true; - protected void sendPaintEvent(PaintEvent e) { - ArrayList listeners = null; - synchronized(paintListeners) { - listeners = paintListeners; + public void windowRepaint(int x, int y, int width, int height) { + if(!propagateRepaint) { + return; + } + if(0>width) { + width=this.width; } - for(Iterator i = listeners.iterator(); i.hasNext(); ) { - PaintListener l = (PaintListener) i.next(); - l.exposed(e); + if(0>height) { + height=this.height; } + + NEWTEvent e = new WindowUpdateEvent(WindowEvent.EVENT_WINDOW_REPAINT, this, System.currentTimeMillis(), + new Rectangle(x, y, width, height)); + doEvent(false, false, e); + } + + protected boolean reparentWindowImpl() { + // default implementation, no native reparenting support + return false; } // diff --git a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java index a84e571e4..b2dd719bc 100644 --- a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java +++ b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java @@ -35,13 +35,20 @@ package com.jogamp.newt.awt; import java.lang.reflect.*; import java.security.*; +import java.awt.Button; +import java.awt.Frame; import java.awt.Canvas; +import java.awt.Graphics; +import java.awt.KeyboardFocusManager; import javax.media.nativewindow.*; -// import javax.media.nativewindow.awt.*; import com.jogamp.newt.event.awt.AWTAdapter; import com.jogamp.newt.event.awt.AWTParentWindowAdapter; +import com.jogamp.newt.event.WindowEvent; +import com.jogamp.newt.event.WindowAdapter; +import com.jogamp.newt.event.MouseEvent; +import com.jogamp.newt.event.MouseAdapter; import com.jogamp.newt.Screen; import com.jogamp.newt.Window; import com.jogamp.newt.impl.Debug; @@ -52,6 +59,7 @@ public class NewtCanvasAWT extends java.awt.Canvas { NativeWindow parent = null; Window newtChild = null; AWTAdapter awtAdapter = null; + boolean hasSwingContainer = false; /** * Instantiates a NewtCanvas without a NEWT child.<br> @@ -67,11 +75,84 @@ public class NewtCanvasAWT extends java.awt.Canvas { super(); setNEWTChild(child); } + + class UnfocusRunnable implements Runnable { + boolean focusTraversed = false; + public void run() { + KeyboardFocusManager focusManager = + KeyboardFocusManager.getCurrentKeyboardFocusManager(); + java.awt.Component comp1 = focusManager.getPermanentFocusOwner(); + java.awt.Component comp2 = focusManager.getFocusOwner(); + if(DEBUG_IMPLEMENTATION) { + System.out.println("AWT Unfocus: traversed "+focusTraversed+" (1)"); + System.out.println("PRE PermenetFocusOwner: "+comp1); + System.out.println("PRE FocusOwner: "+comp2); + } + if(null!=comp1) { + if(!focusTraversed && null==comp2) { + comp1.requestFocus(); + focusTraversed=true; + if(DEBUG_IMPLEMENTATION) { + System.out.println("AWT Unfocus: traversed "+focusTraversed+" (*)"); + } + } else { + focusTraversed=false; + } + + if(DEBUG_IMPLEMENTATION) { + comp1 = focusManager.getPermanentFocusOwner(); + comp2 = focusManager.getFocusOwner(); + System.out.println("MID PermenetFocusOwner: "+comp1); + System.out.println("MID FocusOwner: "+comp2); + } + + focusManager.clearGlobalFocusOwner(); + + if(DEBUG_IMPLEMENTATION) { + comp1 = focusManager.getPermanentFocusOwner(); + comp2 = focusManager.getFocusOwner(); + System.out.println("POST PermenetFocusOwner: "+comp1); + System.out.println("POST FocusOwner: "+comp2); + } + + if(focusTraversed && null!=newtChild) { + newtChild.requestFocus(); + } + } + } + } + UnfocusRunnable unfocusRunnable = new UnfocusRunnable(); + + class FocusListener extends WindowAdapter { + public synchronized void windowGainedFocus(WindowEvent e) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("NewtCanvasAWT focus on: AWT focus "+ NewtCanvasAWT.this.hasFocus()+ + ", focusable "+NewtCanvasAWT.this.isFocusable()+", onEDT "+hasSwingContainer); + } + if(hasSwingContainer) { + java.awt.EventQueue.invokeLater(unfocusRunnable); + } else { + unfocusRunnable.run(); + } + } + public synchronized void windowLostFocus(WindowEvent e) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("NewtCanvasAWT focus off: AWT focus "+ NewtCanvasAWT.this.hasFocus()); + } + } + } + FocusListener focusListener = new FocusListener(); /** sets a new NEWT child, provoking reparenting on the NEWT level. */ public NewtCanvasAWT setNEWTChild(Window child) { if(newtChild!=child) { + if(null!=newtChild) { + newtChild.removeWindowListener(focusListener); + } newtChild = child; + if(null!=newtChild) { + newtChild.addWindowListener(focusListener); + } if(null!=parent) { java.awt.Container cont = getContainer(this); // reparent right away, addNotify has been called already @@ -100,12 +181,22 @@ public class NewtCanvasAWT extends java.awt.Canvas { } } - static java.awt.Container getContainer(java.awt.Component comp) { - while( null != comp && !(comp instanceof java.awt.Container) ) { + static boolean hasSwingContainer(java.awt.Component comp) { + while( null != comp ) { + if( comp instanceof javax.swing.JComponent ) { + return true; + } comp = comp.getParent(); } - if(comp instanceof java.awt.Container) { - return (java.awt.Container) comp; + return false; + } + + static java.awt.Container getContainer(java.awt.Component comp) { + while( null != comp ) { + if( comp instanceof java.awt.Container ) { + return (java.awt.Container) comp; + } + comp = comp.getParent(); } return null; } @@ -113,11 +204,13 @@ public class NewtCanvasAWT extends java.awt.Canvas { public void addNotify() { super.addNotify(); disableBackgroundErase(); + hasSwingContainer = hasSwingContainer(this); java.awt.Container cont = getContainer(this); if(DEBUG_IMPLEMENTATION) { // if ( isShowing() == false ) -> Container was not visible yet. // if ( isShowing() == true ) -> Container is already visible. - System.err.println("NewtCanvasAWT.addNotify: "+newtChild+", "+this+", visible "+isVisible()+", showing "+isShowing()+", displayable "+isDisplayable()+" -> "+cont); + System.err.println("NewtCanvasAWT.addNotify: "+newtChild+", "+this+", visible "+isVisible()+", showing "+isShowing()+ + ", displayable "+isDisplayable()+", swingContainer "+hasSwingContainer+" -> "+cont); } reparentWindow(true, cont); } @@ -154,6 +247,8 @@ public class NewtCanvasAWT extends java.awt.Canvas { newtChild.reparentWindow(parent, screen); newtChild.setVisible(true); setWindowAdapter(true); + newtChild.sendWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); // trigger a resize/relayout to listener + newtChild.windowRepaint(0, 0, newtChild.getWidth(), newtChild.getHeight()); } } else { setWindowAdapter(false); @@ -161,7 +256,18 @@ public class NewtCanvasAWT extends java.awt.Canvas { newtChild.setVisible(false); newtChild.reparentWindow(null, null); } - } + } + + public void paint(Graphics g) { + if(null!=newtChild) { + newtChild.windowRepaint(0, 0, getWidth(), getHeight()); + } + } + public void update(Graphics g) { + if(null!=newtChild) { + newtChild.windowRepaint(0, 0, getWidth(), getHeight()); + } + } // Disables the AWT's erasing of this Canvas's background on Windows // in Java SE 6. This internal API is not available in previous diff --git a/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java b/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java index 2cb2c5177..9afcb840c 100644 --- a/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java +++ b/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java @@ -57,9 +57,6 @@ public class NEWTEvent extends java.util.EventObject { // 2: MouseEvent.java // 3: com.jogamp.newt.Window // 3: com.jogamp.newt.event.awt.AWTNewtEventFactory - // 1: PaintEvent.java - // 2: com.jogamp.newt.Window - // 2: com.jogamp.newt.event.awt.AWTNewtEventFactory // 1: WindowEvent.java // 2: com.jogamp.newt.Window // 2: com.jogamp.newt.event.awt.AWTNewtEventFactory @@ -80,8 +77,7 @@ public class NEWTEvent extends java.util.EventObject { String clazzName = null; - if( (event instanceof com.jogamp.newt.event.WindowEvent) || - (event instanceof com.jogamp.newt.event.PaintEvent) ) { + if( event instanceof com.jogamp.newt.event.WindowEvent ) { if ( stack.length > 2 ) { clazzName = stack[2].getClassName(); } @@ -127,7 +123,7 @@ public class NEWTEvent extends java.util.EventObject { /** * Attach the passed object to this event.<br> - * If an object was previously attached, it will replaced.<br> + * If an object was previously attached, it will be replaced.<br> * Attachments to NEWT events allow users to pass on information * from one custom listener to another, ie custom listener to listener * communication. diff --git a/src/newt/classes/com/jogamp/newt/event/PaintListener.java b/src/newt/classes/com/jogamp/newt/event/NEWTEventConsumer.java index f64ae0069..e37c820a1 100755..100644 --- a/src/newt/classes/com/jogamp/newt/event/PaintListener.java +++ b/src/newt/classes/com/jogamp/newt/event/NEWTEventConsumer.java @@ -1,21 +1,21 @@ /* - * Copyright (c) 2009 Sun Microsystems, Inc. All Rights Reserved. - * + * Copyright (c) 2010 Sven Gothel. All Rights Reserved. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. - * + * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of + * + * Neither the name Sven Gothel or the names of * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A @@ -27,18 +27,17 @@ * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF - * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */ - package com.jogamp.newt.event; -import com.jogamp.newt.*; +public interface NEWTEventConsumer { -/** - * - * @author tdv - */ -public interface PaintListener { - public void exposed(PaintEvent e); + /** + * Consume the event + * + * @return true if the event can be consumed now, + * otherwise propagate it later. + */ + public boolean consumeEvent(NEWTEvent event); } diff --git a/src/newt/classes/com/jogamp/newt/event/TraceWindowAdapter.java b/src/newt/classes/com/jogamp/newt/event/TraceWindowAdapter.java index 5a93bca36..fb9e1877a 100644 --- a/src/newt/classes/com/jogamp/newt/event/TraceWindowAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/TraceWindowAdapter.java @@ -65,4 +65,8 @@ public class TraceWindowAdapter implements WindowListener { System.out.println(e); if(null!=downstream) { downstream.windowLostFocus(e); } } + public void windowRepaint(WindowUpdateEvent e) { + System.out.println(e); + if(null!=downstream) { downstream.windowRepaint(e); } + } } diff --git a/src/newt/classes/com/jogamp/newt/event/WindowAdapter.java b/src/newt/classes/com/jogamp/newt/event/WindowAdapter.java index 51732789e..587622a81 100644 --- a/src/newt/classes/com/jogamp/newt/event/WindowAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/WindowAdapter.java @@ -45,4 +45,6 @@ public abstract class WindowAdapter implements WindowListener } public void windowLostFocus(WindowEvent e) { } + public void windowRepaint(WindowUpdateEvent e) { + } } diff --git a/src/newt/classes/com/jogamp/newt/event/WindowEvent.java b/src/newt/classes/com/jogamp/newt/event/WindowEvent.java index e542c467f..5221b3664 100644 --- a/src/newt/classes/com/jogamp/newt/event/WindowEvent.java +++ b/src/newt/classes/com/jogamp/newt/event/WindowEvent.java @@ -46,7 +46,7 @@ public class WindowEvent extends NEWTEvent { public static final int EVENT_WINDOW_DESTROY_NOTIFY = 102; public static final int EVENT_WINDOW_GAINED_FOCUS = 103; public static final int EVENT_WINDOW_LOST_FOCUS = 104; - // public static final int EVENT_WINDOW_REPAINT = 105; // TODO + public static final int EVENT_WINDOW_REPAINT = 105; public WindowEvent(int eventType, Object source, long when) { super(eventType, source, when); @@ -59,7 +59,7 @@ public class WindowEvent extends NEWTEvent { case EVENT_WINDOW_DESTROY_NOTIFY: return "EVENT_WINDOW_DESTROY_NOTIFY"; case EVENT_WINDOW_GAINED_FOCUS: return "EVENT_WINDOW_GAINED_FOCUS"; case EVENT_WINDOW_LOST_FOCUS: return "EVENT_WINDOW_LOST_FOCUS"; - // case EVENT_WINDOW_REPAINT: return "EVENT_WINDOW_REPAINT"; + case EVENT_WINDOW_REPAINT: return "EVENT_WINDOW_REPAINT"; default: return "unknown (" + type + ")"; } } diff --git a/src/newt/classes/com/jogamp/newt/event/WindowListener.java b/src/newt/classes/com/jogamp/newt/event/WindowListener.java index 871f0fe12..1a10131f7 100644 --- a/src/newt/classes/com/jogamp/newt/event/WindowListener.java +++ b/src/newt/classes/com/jogamp/newt/event/WindowListener.java @@ -41,4 +41,5 @@ public interface WindowListener extends NEWTEventListener { public void windowDestroyNotify(WindowEvent e); public void windowGainedFocus(WindowEvent e); public void windowLostFocus(WindowEvent e); + public void windowRepaint(WindowUpdateEvent e); } diff --git a/src/newt/classes/com/jogamp/newt/event/PaintEvent.java b/src/newt/classes/com/jogamp/newt/event/WindowUpdateEvent.java index e1c667b55..5a11f0f77 100755 --- a/src/newt/classes/com/jogamp/newt/event/PaintEvent.java +++ b/src/newt/classes/com/jogamp/newt/event/WindowUpdateEvent.java @@ -1,21 +1,21 @@ /* - * Copyright (c) 2009 Sun Microsystems, Inc. All Rights Reserved. - * + * Copyright (c) 2010 Sven Gothel. All Rights Reserved. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. - * + * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of + * + * Neither the name Sven Gothel or the names of * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A @@ -27,50 +27,27 @@ * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF - * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */ - package com.jogamp.newt.event; import com.jogamp.newt.*; +import com.jogamp.nativewindow.util.Rectangle; -/** - * - * @author tdv - */ -public class PaintEvent extends NEWTEvent { +public class WindowUpdateEvent extends WindowEvent { + Rectangle bounds; - // bounds of the damage region - private int x, y, width, height; - public PaintEvent(int eventType, Object source, - long when, int x, int y, int w, int h) + public WindowUpdateEvent(int eventType, Object source, long when, Rectangle bounds) { super(eventType, source, when); - this.x = x; - this.y = y; - this.width = w; - this.height = h; + this.bounds = bounds; } - public int getHeight() { - return height; - } - - public int getWidth() { - return width; - } - - public int getX() { - return x; - } - - public int getY() { - return y; + public Rectangle getBounds() { + return bounds; } public String toString() { - return "ExposeEvent[modifiers: x="+x+" y="+y+" w="+width+" h="+height +"]"; + return "WindowUpdateEvent["+super.toString()+", "+bounds+"]"; } - } diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java index e8497a741..b9d1f8678 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java @@ -165,16 +165,8 @@ public abstract class AWTAdapter implements java.util.EventListener /** @see #addTo(java.awt.Component) */ public abstract AWTAdapter removeFrom(java.awt.Component awtComponent); - void enqueueEvent(com.jogamp.newt.event.NEWTEvent event) { - enqueueEvent(false, event); - } - void enqueueEvent(boolean wait, com.jogamp.newt.event.NEWTEvent event) { - try { - newtWindow.getScreen().getDisplay().enqueueEvent(wait, event); - } catch (NullPointerException npe) { - /* that's ok .. window might be down already */ - } + newtWindow.enqueueEvent(wait, event); } } diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java index d2b733f98..7e3e5f1b1 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java @@ -60,7 +60,7 @@ public class AWTKeyAdapter extends AWTAdapter implements java.awt.event.KeyListe if(null!=newtListener) { ((com.jogamp.newt.event.KeyListener)newtListener).keyPressed(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -69,7 +69,7 @@ public class AWTKeyAdapter extends AWTAdapter implements java.awt.event.KeyListe if(null!=newtListener) { ((com.jogamp.newt.event.KeyListener)newtListener).keyReleased(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -78,7 +78,7 @@ public class AWTKeyAdapter extends AWTAdapter implements java.awt.event.KeyListe if(null!=newtListener) { ((com.jogamp.newt.event.KeyListener)newtListener).keyTyped(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } } diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java index 058a5f250..3f774f193 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java @@ -62,7 +62,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseClicked(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -71,7 +71,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseEntered(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -80,7 +80,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseExited(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -89,7 +89,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mousePressed(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -98,7 +98,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseReleased(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -107,7 +107,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseDragged(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -116,7 +116,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseMoved(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } } diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTNewtEventFactory.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTNewtEventFactory.java index e4e97946a..076165c73 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTNewtEventFactory.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTNewtEventFactory.java @@ -48,8 +48,10 @@ class AWTNewtEventFactory { // n/a map.put(java.awt.event.WindowEvent.WINDOW_DEICONIFIED, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_DEICONIFIED); map.put(java.awt.event.WindowEvent.WINDOW_ACTIVATED, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_GAINED_FOCUS); map.put(java.awt.event.WindowEvent.WINDOW_GAINED_FOCUS, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_GAINED_FOCUS); + map.put(java.awt.event.FocusEvent.FOCUS_GAINED, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_GAINED_FOCUS); map.put(java.awt.event.WindowEvent.WINDOW_DEACTIVATED, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_LOST_FOCUS); map.put(java.awt.event.WindowEvent.WINDOW_LOST_FOCUS, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_LOST_FOCUS); + map.put(java.awt.event.FocusEvent.FOCUS_LOST, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_LOST_FOCUS); // n/a map.put(java.awt.event.WindowEvent.WINDOW_STATE_CHANGED, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_STATE_CHANGED); map.put(java.awt.event.ComponentEvent.COMPONENT_MOVED, com.jogamp.newt.event.WindowEvent.EVENT_WINDOW_MOVED); @@ -108,6 +110,14 @@ class AWTNewtEventFactory { return null; // no mapping .. } + static final com.jogamp.newt.event.WindowEvent createWindowEvent(java.awt.event.FocusEvent event, com.jogamp.newt.Window newtSource) { + int type = eventTypeAWT2NEWT.get(event.getID()); + if(-1 < type) { + return new com.jogamp.newt.event.WindowEvent(type, (null==newtSource)?(Object)event.getComponent():(Object)newtSource, System.currentTimeMillis()); + } + return null; // no mapping .. + } + static final com.jogamp.newt.event.MouseEvent createMouseEvent(java.awt.event.MouseEvent event, com.jogamp.newt.Window newtSource) { int type = eventTypeAWT2NEWT.get(event.getID()); if(-1 < type) { diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTParentWindowAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTParentWindowAdapter.java index 7905a728c..43087289a 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTParentWindowAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTParentWindowAdapter.java @@ -53,6 +53,19 @@ public class AWTParentWindowAdapter return super.removeFrom(awtComponent); } + public void focusGained(java.awt.event.FocusEvent e) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("AWT: focusGained: START "+ e.getComponent()); + } + newtWindow.requestFocus(); + } + + public void focusLost(java.awt.event.FocusEvent e) { + if(DEBUG_IMPLEMENTATION) { + System.out.println("AWT: focusLost: "+ e.getComponent()); + } + } + public void componentResized(java.awt.event.ComponentEvent e) { // Need to resize the NEWT child window // the resized event will be send via the native window feedback. diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java index ee7ca97ad..875401544 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java @@ -33,7 +33,7 @@ package com.jogamp.newt.event.awt; public class AWTWindowAdapter extends AWTAdapter - implements java.awt.event.ComponentListener, java.awt.event.WindowListener + implements java.awt.event.ComponentListener, java.awt.event.WindowListener, java.awt.event.FocusListener { WindowClosingListener windowClosingListener; @@ -52,6 +52,7 @@ public class AWTWindowAdapter public AWTAdapter addTo(java.awt.Component awtComponent) { java.awt.Window win = getWindow(awtComponent); awtComponent.addComponentListener(this); + awtComponent.addFocusListener(this); if( null == windowClosingListener ) { windowClosingListener = new WindowClosingListener(); } @@ -65,6 +66,7 @@ public class AWTWindowAdapter } public AWTAdapter removeFrom(java.awt.Component awtComponent) { + awtComponent.removeFocusListener(this); awtComponent.removeComponentListener(this); java.awt.Window win = getWindow(awtComponent); if( null != win && null != windowClosingListener ) { @@ -86,12 +88,30 @@ public class AWTWindowAdapter return null; } + public void focusGained(java.awt.event.FocusEvent e) { + com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); + if(null!=newtListener) { + ((com.jogamp.newt.event.WindowListener)newtListener).windowGainedFocus(event); + } else { + enqueueEvent(false, event); + } + } + + public void focusLost(java.awt.event.FocusEvent e) { + com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); + if(null!=newtListener) { + ((com.jogamp.newt.event.WindowListener)newtListener).windowLostFocus(event); + } else { + enqueueEvent(false, event); + } + } + public void componentResized(java.awt.event.ComponentEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowResized(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -100,7 +120,7 @@ public class AWTWindowAdapter if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowMoved(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -143,7 +163,7 @@ public class AWTWindowAdapter if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowGainedFocus(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } @@ -156,7 +176,7 @@ public class AWTWindowAdapter if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowLostFocus(event); } else { - enqueueEvent(event); + enqueueEvent(false, event); } } 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 a06b7160c..6f936d2c6 100644 --- a/src/newt/classes/com/jogamp/newt/impl/awt/AWTWindow.java +++ b/src/newt/classes/com/jogamp/newt/impl/awt/AWTWindow.java @@ -215,7 +215,7 @@ public class AWTWindow extends Window { } } - public com.jogamp.newt.Insets getInsets() { + public com.jogamp.newt.util.Insets getInsets() { final int insets[] = new int[] { 0, 0, 0, 0 }; runOnEDT(true, new Runnable() { public void run() { @@ -226,7 +226,7 @@ public class AWTWindow extends Window { insets[3] = contInsets.right; } }); - return new com.jogamp.newt. + return new com.jogamp.newt.util. Insets(insets[0],insets[1],insets[2],insets[3]); } @@ -264,19 +264,6 @@ public class AWTWindow extends Window { return canvas; } - protected void enqueueWindowEvent(int eventType) { - super.enqueueWindowEvent(eventType); - } - - protected void enqueueKeyEvent(int eventType, int modifiers, int keyCode, char keyChar) { - super.enqueueKeyEvent(eventType, modifiers, keyCode, keyChar); - } - - protected void enqueueMouseEvent(int eventType, int modifiers, - int x, int y, int button, int rotation) { - super.enqueueMouseEvent(eventType, modifiers, x, y, button, rotation); - } - private void runOnEDT(boolean wait, Runnable r) { EDTUtil edtUtil = screen.getDisplay().getEDTUtil(); if ( ( null != edtUtil && edtUtil.isCurrentThreadEDT() ) || EventQueue.isDispatchThread() ) { diff --git a/src/newt/classes/com/jogamp/newt/impl/macosx/MacWindow.java b/src/newt/classes/com/jogamp/newt/impl/macosx/MacWindow.java index 8a656a5a8..9f47aa49f 100755 --- a/src/newt/classes/com/jogamp/newt/impl/macosx/MacWindow.java +++ b/src/newt/classes/com/jogamp/newt/impl/macosx/MacWindow.java @@ -39,6 +39,7 @@ import com.jogamp.nativewindow.impl.RecursiveToolkitLock; import com.jogamp.newt.*; import com.jogamp.newt.event.*; import com.jogamp.newt.impl.*; +import com.jogamp.newt.util.*; public class MacWindow extends Window { @@ -295,24 +296,6 @@ public class MacWindow extends Window { return fullscreen; } - private void sizeChanged(int newWidth, int newHeight) { - if(width != newWidth || height != newHeight) { - if (DEBUG_IMPLEMENTATION) { - System.out.println(Thread.currentThread().getName()+" Size changed to " + newWidth + ", " + newHeight); - } - width = newWidth; - height = newHeight; - if(!fullscreen) { - nfs_width=width; - nfs_height=height; - } - if (DEBUG_IMPLEMENTATION) { - System.out.println(" Posted WINDOW_RESIZED event"); - } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); - } - } - private void insetsChanged(int left, int top, int right, int bottom) { if (DEBUG_IMPLEMENTATION) { System.out.println(Thread.currentThread().getName()+ @@ -326,32 +309,6 @@ public class MacWindow extends Window { } } - private void positionChanged(int newX, int newY) { - if( 0==parentWindowHandle && ( x != newX || y != newY ) ) { - if (DEBUG_IMPLEMENTATION) { - System.out.println(Thread.currentThread().getName()+" Position changed to " + newX + ", " + newY); - } - x = newX; - y = newY; - if(!fullscreen) { - nfs_x=x; - nfs_y=y; - } - if (DEBUG_IMPLEMENTATION) { - System.out.println(" Posted WINDOW_MOVED event"); - } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); - } - } - - private void focusChanged(boolean focusGained) { - if (focusGained) { - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_GAINED_FOCUS); - } else { - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_LOST_FOCUS); - } - } - private char convertKeyChar(char keyChar) { if (keyChar == '\r') { // Turn these into \n @@ -435,12 +392,12 @@ public class MacWindow extends Window { return keyChar; } - protected void enqueueKeyEvent(int eventType, int modifiers, int keyCode, char keyChar) { + public void enqueueKeyEvent(boolean wait, int eventType, int modifiers, int keyCode, char keyChar) { int key = convertKeyChar(keyChar); if(DEBUG_IMPLEMENTATION) System.out.println("MacWindow.enqueueKeyEvent "+Thread.currentThread().getName()); // Note that we send the key char for the key code on this // platform -- we do not get any useful key codes out of the system - super.enqueueKeyEvent(eventType, modifiers, key, keyChar); + super.enqueueKeyEvent(wait, eventType, modifiers, key, keyChar); } private void createWindow(final boolean recreate, final int x, final int y, final int width, final int height, final boolean fullscreen) { @@ -482,9 +439,9 @@ public class MacWindow extends Window { ie.printStackTrace(); } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_GAINED_FOCUS); + enqueueWindowEvent(false, WindowEvent.EVENT_WINDOW_MOVED); + enqueueWindowEvent(false, WindowEvent.EVENT_WINDOW_RESIZED); + enqueueWindowEvent(false, WindowEvent.EVENT_WINDOW_GAINED_FOCUS); } protected static native boolean initIDs0(); diff --git a/src/newt/classes/com/jogamp/newt/impl/opengl/kd/KDWindow.java b/src/newt/classes/com/jogamp/newt/impl/opengl/kd/KDWindow.java index 0d12a4a0a..0c7254376 100755 --- a/src/newt/classes/com/jogamp/newt/impl/opengl/kd/KDWindow.java +++ b/src/newt/classes/com/jogamp/newt/impl/opengl/kd/KDWindow.java @@ -126,16 +126,11 @@ public class KDWindow extends Window { windowUserData=userData; } - private void sizeChanged(int newWidth, int newHeight) { - width = newWidth; - height = newHeight; - if(!fullscreen) { - nfs_width=width; - nfs_height=height; - } else { + protected void sizeChanged(int newWidth, int newHeight) { + if(fullscreen) { ((KDScreen)screen).setScreenSize(width, height); } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); + super.sizeChanged(newWidth, newHeight); } private long eglWindowHandle; diff --git a/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java b/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java index aedb4ed7d..47f79f46a 100755 --- a/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java +++ b/src/newt/classes/com/jogamp/newt/impl/windows/WindowsWindow.java @@ -36,6 +36,7 @@ package com.jogamp.newt.impl.windows; import javax.media.nativewindow.*; import com.jogamp.newt.*; import com.jogamp.newt.event.*; +import com.jogamp.newt.util.*; public class WindowsWindow extends Window { @@ -216,42 +217,4 @@ public class WindowsWindow extends Window { insets.bottom = bottom; } } - - private void sizeChanged(int newWidth, int newHeight) { - if(width != newWidth || height != newHeight) { - width = newWidth; - height = newHeight; - if(!fullscreen) { - nfs_width=width; - nfs_height=height; - } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); - } - } - - private void positionChanged(int newX, int newY) { - if( 0==parentWindowHandle && ( x != newX || y != newY ) ) { - x = newX; - y = newY; - if(!fullscreen) { - nfs_x=x; - nfs_y=y; - } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); - } - } - - /** - * - * @param focusOwner if focusGained is true, focusOwner is the previous - * focus owner, if focusGained is false, focusOwner is the new focus owner - * @param focusGained - */ - private void focusChanged(long focusOwner, boolean focusGained) { - if (focusGained) { - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_GAINED_FOCUS); - } else { - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_LOST_FOCUS); - } - } } diff --git a/src/newt/classes/com/jogamp/newt/impl/x11/X11Window.java b/src/newt/classes/com/jogamp/newt/impl/x11/X11Window.java index e7fc96019..6890b29fa 100755 --- a/src/newt/classes/com/jogamp/newt/impl/x11/X11Window.java +++ b/src/newt/classes/com/jogamp/newt/impl/x11/X11Window.java @@ -71,7 +71,8 @@ public class X11Window extends Window { if(0!=windowHandleClose && null!=getScreen() ) { X11Display display = (X11Display) getScreen().getDisplay(); try { - CloseWindow0(display.getHandle(), windowHandleClose, display.getJavaObjectAtom()); + CloseWindow0(display.getHandle(), windowHandleClose, + display.getJavaObjectAtom(), display.getWindowDeleteAtom()); } catch (Throwable t) { if(DEBUG_IMPLEMENTATION) { Exception e = new Exception("closeNative failed - "+Thread.currentThread().getName(), t); @@ -134,7 +135,7 @@ public class X11Window extends Window { private native long CreateWindow0(long parentWindowHandle, long display, int screen_index, long visualID, long javaObjectAtom, long windowDeleteAtom, int x, int y, int width, int height, boolean undecorated); - private native void CloseWindow0(long display, long windowHandle, long javaObjectAtom); + private native void CloseWindow0(long display, long windowHandle, long javaObjectAtom, long windowDeleteAtom); private native void setVisible0(long display, long windowHandle, boolean visible); private native void setSize0(long display, long windowHandle, int width, int height); private native void setPosSizeDecor0(long parentWindowHandle, long display, int screen_index, long windowHandle, @@ -145,51 +146,6 @@ public class X11Window extends Window { private native void reparentWindow0(long parentWindowHandle, long display, int screen_index, long windowHandle, int x, int y, boolean undecorated, boolean isVisible); - private void windowChanged(int newX, int newY, int newWidth, int newHeight) { - if(width != newWidth || height != newHeight) { - if(DEBUG_IMPLEMENTATION) { - System.err.println("X11Window windowChanged size: "+this.width+"x"+this.height+" -> "+newWidth+"x"+newHeight); - } - width = newWidth; - height = newHeight; - if(!fullscreen) { - nfs_width=width; - nfs_height=height; - } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); - } - if( 0==parentWindowHandle && ( x != newX || y != newY ) ) { - if(DEBUG_IMPLEMENTATION) { - System.err.println("X11Window windowChanged position: "+this.x+"/"+this.y+" -> "+newX+"x"+newY); - } - x = newX; - y = newY; - if(!fullscreen) { - nfs_x=x; - nfs_y=y; - } - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); - } - } - - /** - * @param focusGained - */ - private void focusChanged(boolean focusGained) { - if (focusGained) { - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_GAINED_FOCUS); - } else { - enqueueWindowEvent(WindowEvent.EVENT_WINDOW_LOST_FOCUS); - } - } - - /** - * @param focusGained - */ - private void visibleChanged(boolean visible) { - // FIXME .. this.visible = visible ; - } - private void windowCreated(long windowHandle) { this.windowHandle = windowHandle; } diff --git a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java index 0e93fc0bd..e66ed1e34 100644 --- a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java +++ b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java @@ -35,6 +35,7 @@ package com.jogamp.newt.opengl; import com.jogamp.newt.*; import com.jogamp.newt.event.*; +import com.jogamp.newt.util.*; import com.jogamp.nativewindow.impl.RecursiveToolkitLock; import javax.media.nativewindow.*; import javax.media.opengl.*; @@ -55,22 +56,34 @@ import java.util.*; */ public class GLWindow extends Window implements GLAutoDrawable { private Window window; - private boolean runPumpMessages; /** * Constructor. Do not call this directly -- use {@link #create()} instead. */ protected GLWindow(Window window) { + this.startTime = System.currentTimeMillis(); this.window = window; this.window.setHandleDestroyNotify(false); - this.runPumpMessages = ( null == getScreen().getDisplay().getEDTUtil() ) ; window.addWindowListener(new WindowAdapter() { + public void windowRepaint(WindowUpdateEvent e) { + if( !windowIsLocked() && null == getAnimator() ) { + display(); + } + } + public void windowResized(WindowEvent e) { sendReshape = true; + if( !windowIsLocked() && null == getAnimator() ) { + display(); + } } public void windowDestroyNotify(WindowEvent e) { - sendDestroy = true; + if( !windowIsLocked() && null == getAnimator() ) { + destroy(); + } else { + sendDestroy = true; + } } }); } @@ -128,31 +141,6 @@ public class GLWindow extends Window implements GLAutoDrawable { return window.getWrappedWindow(); } - /** - * EXPERIMENTAL<br> - * Enable or disables running the {@link Display#pumpMessages} in the {@link #display()} call.<br> - * The default behavior is to run {@link Display#pumpMessages}.<P> - * - * The idea was that in a single threaded environment with one {@link Display} and many {@link Window}'s, - * a performance benefit was expected while disabling the implicit {@link Display#pumpMessages} and - * do it once via {@link GLWindow#runCurrentThreadPumpMessage()} <br> - * This could not have been verified. No measurable difference could have been recognized.<P> - * - * Best performance has been achieved with one GLWindow per thread.<br> - * - * Enabling local pump messages while using the EDT, - * {@link com.jogamp.newt.NewtFactory#setUseEDT(boolean)}, - * will result in an exception. - * - * @deprecated EXPERIMENTAL, semantic is about to be removed after further verification. - */ - public void setRunPumpMessages(boolean onoff) { - if( onoff && null!=getScreen().getDisplay().getEDTUtil() ) { - throw new GLException("GLWindow.setRunPumpMessages(true) - Can't do with EDT on"); - } - runPumpMessages = onoff; - } - protected void createNativeImpl() { shouldNotCallThis(); } @@ -178,7 +166,7 @@ public class GLWindow extends Window implements GLAutoDrawable { // Lock: Have to cover whole workflow (dispose all, context, drawable and window) windowLock(); try { - if(null==window || window.isDestroyed()) { + if( isDestroyed() ) { return; // nop } if(Window.DEBUG_WINDOW_EVENT || window.DEBUG_IMPLEMENTATION) { @@ -186,21 +174,20 @@ public class GLWindow extends Window implements GLAutoDrawable { e1.printStackTrace(); } - if ( null != context && context.isCreated() && null != drawable && drawable.isRealized() ) { - // Catch dispose GLExceptions by GLEventListener, just 'print' them - // so we can continue with the destruction. - try { - helper.invokeGL(drawable, context, disposeAction, null); - } catch (GLException gle) { - gle.printStackTrace(); + if( window.isNativeWindowValid() && null != drawable && drawable.isRealized() ) { + if( null != context && context.isCreated() ) { + // Catch dispose GLExceptions by GLEventListener, just 'print' them + // so we can continue with the destruction. + try { + helper.invokeGL(drawable, context, disposeAction, null); + } catch (GLException gle) { + gle.printStackTrace(); + } + + context.destroy(); + context = null; } - } - if (context != null && null != drawable && drawable.isRealized() ) { - context.destroy(); - context = null; - } - if (drawable != null) { drawable.setRealized(false); drawable = null; } @@ -228,7 +215,7 @@ public class GLWindow extends Window implements GLAutoDrawable { * @see #destroy() */ public void destroy(boolean deep) { - if(!isDestroyed()) { + if( !isDestroyed() ) { runOnEDTIfAvail(true, new DestroyAction(deep)); } } @@ -369,8 +356,11 @@ public class GLWindow extends Window implements GLAutoDrawable { return window.isFullscreen(); } - public void sendEvent(NEWTEvent e) { - window.sendEvent(e); + public void enqueueEvent(boolean wait, com.jogamp.newt.event.NEWTEvent event) { + window.enqueueEvent(wait, event); + } + public boolean consumeEvent(NEWTEvent e) { + return window.consumeEvent(e); } public void addSurfaceUpdatedListener(int index, SurfaceUpdatedListener l) { @@ -418,6 +408,12 @@ public class GLWindow extends Window implements GLAutoDrawable { return window.getKeyListeners(); } + public void sendWindowEvent(int eventType) { + window.sendWindowEvent(eventType); + } + public void enqueueWindowEvent(boolean wait, int eventType) { + window.enqueueWindowEvent(wait, eventType); + } public void addWindowListener(int index, WindowListener l) { window.addWindowListener(index, l); } @@ -430,6 +426,12 @@ public class GLWindow extends Window implements GLAutoDrawable { public WindowListener[] getWindowListeners() { return window.getWindowListeners(); } + public void setPropagateRepaint(boolean v) { + window.setPropagateRepaint(v); + } + public void windowRepaint(int x, int y, int width, int height) { + window.windowRepaint(x, y, width, height); + } public String toString() { return "NEWT-GLWindow[ \n\tHelper: "+helper+", \n\tDrawable: "+drawable + /** ", \n\tWindow: "+window+", \n\tFactory: "+factory+ */ "]"; @@ -487,8 +489,17 @@ public class GLWindow extends Window implements GLAutoDrawable { helper.removeGLEventListener(listener); } + public void setAnimator(Thread animator) { + helper.setAnimator(animator); + window.setPropagateRepaint(null==animator); + } + + public Thread getAnimator() { + return helper.getAnimator(); + } + public void invoke(boolean wait, GLRunnable glRunnable) { - helper.invoke(wait, glRunnable); + helper.invoke(this, wait, glRunnable); } public void display() { @@ -498,28 +509,26 @@ public class GLWindow extends Window implements GLAutoDrawable { public void display(boolean forceReshape) { if( null == window ) { return; } + if(sendDestroy || ( null!=window && window.hasDeviceChanged() && GLAutoDrawable.SCREEN_CHANGE_ACTION_ENABLED ) ) { + sendDestroy=false; + destroy(); + return; + } + if( null == context && window.isVisible() ) { // retry native window and drawable/context creation setVisible(true); } - if( window.isNativeWindowValid() && null != context ) { - if(runPumpMessages) { - window.getScreen().getDisplay().pumpMessages(); + if( window.isVisible() && window.isNativeWindowValid() && null != context ) { + if(forceReshape) { + sendReshape = true; } - if(sendDestroy || window.hasDeviceChanged() && GLAutoDrawable.SCREEN_CHANGE_ACTION_ENABLED) { - destroy(); - sendDestroy=false; - } else if ( window.isVisible() ) { - if(forceReshape) { - sendReshape = true; - } - windowLock(); - try{ - helper.invokeGL(drawable, context, displayAction, initAction); - } finally { - windowUnlock(); - } + windowLock(); + try{ + helper.invokeGL(drawable, context, displayAction, initAction); + } finally { + windowUnlock(); } } } @@ -550,7 +559,7 @@ public class GLWindow extends Window implements GLAutoDrawable { public void run() { // Lock: Locked Surface/Window by MakeCurrent/Release helper.init(GLWindow.this); - startTime = System.currentTimeMillis(); + startTime = System.currentTimeMillis(); // overwrite startTime to real init one curTime = startTime; if(perfLog) { lastCheck = startTime; @@ -564,10 +573,7 @@ public class GLWindow extends Window implements GLAutoDrawable { public void run() { // Lock: Locked Surface/Window by display _and_ MakeCurrent/Release if (sendReshape) { - int width = getWidth(); - int height = getHeight(); - getGL().glViewport(0, 0, width, height); - helper.reshape(GLWindow.this, 0, 0, width, height); + helper.reshape(GLWindow.this, 0, 0, getWidth(), getHeight()); sendReshape = false; } @@ -593,8 +599,8 @@ public class GLWindow extends Window implements GLAutoDrawable { private DisplayAction displayAction = new DisplayAction(); public long getStartTime() { return startTime; } - public long getCurrentTime() { return curTime; } - public long getDuration() { return curTime-startTime; } + public long getCurrentTime() { curTime = System.currentTimeMillis(); return curTime; } + public long getDuration() { return getCurrentTime()-startTime; } public int getTotalFrames() { return totalFrames; } private long startTime = 0; diff --git a/src/newt/classes/com/jogamp/newt/Insets.java b/src/newt/classes/com/jogamp/newt/util/Insets.java index e440892f0..068cc1dfb 100644 --- a/src/newt/classes/com/jogamp/newt/Insets.java +++ b/src/newt/classes/com/jogamp/newt/util/Insets.java @@ -30,7 +30,7 @@ * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * */ -package com.jogamp.newt; +package com.jogamp.newt.util; /** * Simple class representing insets. diff --git a/src/newt/native/WindowsWindow.c b/src/newt/native/WindowsWindow.c index 5e3311d3b..a1dab2678 100755 --- a/src/newt/native/WindowsWindow.c +++ b/src/newt/native/WindowsWindow.c @@ -109,15 +109,16 @@ #define STD_PRINT(...) fprintf(stderr, __VA_ARGS__); fflush(stderr) -static jmethodID sizeChangedID = NULL; static jmethodID insetsChangedID = NULL; +static jmethodID sizeChangedID = NULL; static jmethodID positionChangedID = NULL; static jmethodID focusChangedID = NULL; +static jmethodID visibleChangedID = NULL; static jmethodID windowDestroyNotifyID = NULL; static jmethodID windowDestroyedID = NULL; -static jmethodID enqueueMouseEventID = NULL; -static jmethodID enqueueKeyEventID = NULL; -static jmethodID sendPaintEventID = NULL; +static jmethodID windowRepaintID = NULL; +static jmethodID sendMouseEventID = NULL; +static jmethodID sendKeyEventID = NULL; static RECT* UpdateInsets(JNIEnv *env, HWND hwnd, jobject window); @@ -506,7 +507,7 @@ static int WmChar(JNIEnv *env, jobject window, UINT character, UINT repCnt, if (character == VK_RETURN) { character = J_VK_ENTER; } - (*env)->CallVoidMethod(env, window, enqueueKeyEventID, + (*env)->CallVoidMethod(env, window, sendKeyEventID, (jint) EVENT_KEY_TYPED, GetModifiers(), (jint) -1, @@ -551,7 +552,7 @@ static int WmKeyDown(JNIEnv *env, jobject window, UINT wkey, UINT repCnt, character = WindowsKeyToJavaChar(wkey, modifiers, SAVE); */ - (*env)->CallVoidMethod(env, window, enqueueKeyEventID, + (*env)->CallVoidMethod(env, window, sendKeyEventID, (jint) EVENT_KEY_PRESSED, modifiers, (jint) jkey, @@ -562,7 +563,7 @@ static int WmKeyDown(JNIEnv *env, jobject window, UINT wkey, UINT repCnt, WM_KEYDOWN. */ if (jkey == J_VK_DELETE) { - (*env)->CallVoidMethod(env, window, enqueueKeyEventID, + (*env)->CallVoidMethod(env, window, sendKeyEventID, (jint) EVENT_KEY_TYPED, GetModifiers(), (jint) -1, @@ -586,7 +587,7 @@ static int WmKeyUp(JNIEnv *env, jobject window, UINT wkey, UINT repCnt, character = WindowsKeyToJavaChar(wkey, modifiers, SAVE); */ - (*env)->CallVoidMethod(env, window, enqueueKeyEventID, + (*env)->CallVoidMethod(env, window, sendKeyEventID, (jint) EVENT_KEY_RELEASED, modifiers, (jint) jkey, @@ -706,6 +707,7 @@ static void WmSize(JNIEnv *env, HWND wnd, jobject window, UINT type) static LRESULT CALLBACK wndProc(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) { + LRESULT res = 0; int useDefWindowProc = 0; JNIEnv *env = NULL; jobject window = NULL; @@ -799,7 +801,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_LBUTTONDOWN: NewtWindows_requestFocus ( wnd, FALSE, FALSE ); // request focus on this window, if not already .. - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_PRESSED, GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), @@ -808,7 +810,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, break; case WM_LBUTTONUP: - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_RELEASED, GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), @@ -818,7 +820,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_MBUTTONDOWN: NewtWindows_requestFocus ( wnd, FALSE, FALSE ); // request focus on this window, if not already .. - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_PRESSED, GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), @@ -827,7 +829,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, break; case WM_MBUTTONUP: - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_RELEASED, GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), @@ -837,7 +839,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_RBUTTONDOWN: NewtWindows_requestFocus ( wnd, FALSE, FALSE ); // request focus on this window, if not already .. - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_PRESSED, GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), @@ -846,7 +848,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, break; case WM_RBUTTONUP: - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_RELEASED, GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), @@ -855,7 +857,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, break; case WM_MOUSEMOVE: - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_MOVED, GetModifiers(), (jint) LOWORD(lParam), (jint) HIWORD(lParam), @@ -871,7 +873,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, eventPt.x = x; eventPt.y = y; ScreenToClient(wnd, &eventPt); - (*env)->CallVoidMethod(env, window, enqueueMouseEventID, + (*env)->CallVoidMethod(env, window, sendMouseEventID, (jint) EVENT_MOUSE_WHEEL_MOVED, GetModifiers(), (jint) eventPt.x, (jint) eventPt.y, @@ -881,17 +883,19 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, } case WM_SETFOCUS: - (*env)->CallVoidMethod(env, window, focusChangedID, - (jlong)wParam, JNI_TRUE); + (*env)->CallVoidMethod(env, window, focusChangedID, JNI_TRUE); useDefWindowProc = 1; break; case WM_KILLFOCUS: - (*env)->CallVoidMethod(env, window, focusChangedID, - (jlong)wParam, JNI_FALSE); + (*env)->CallVoidMethod(env, window, focusChangedID, JNI_FALSE); useDefWindowProc = 1; break; + case WM_SHOWWINDOW: + (*env)->CallVoidMethod(env, window, visibleChangedID, wParam==TRUE?JNI_TRUE:JNI_FALSE); + break; + case WM_MOVE: DBG_PRINT("*** WindowsWindow: WM_MOVE window %p, %d/%d\n", wnd, (int)LOWORD(lParam), (int)HIWORD(lParam)); (*env)->CallVoidMethod(env, window, positionChangedID, @@ -901,21 +905,24 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, case WM_PAINT: { RECT r; - if (GetUpdateRect(wnd, &r, FALSE)) { - if ((r.right-r.left) > 0 && (r.bottom-r.top) > 0) { - (*env)->CallVoidMethod(env, window, sendPaintEventID, - 0, r.left, r.top, r.right-r.left, r.bottom-r.top); + useDefWindowProc = 0; + if (GetUpdateRect(wnd, &r, TRUE /* erase background */)) { + /* + jint width = r.right-r.left; + jint height = r.bottom-r.top; + if (width > 0 && height > 0) { + (*env)->CallVoidMethod(env, window, windowRepaintID, r.left, r.top, width, height); } ValidateRect(wnd, &r); - useDefWindowProc = 0; - } else { - useDefWindowProc = 1; + */ } break; } case WM_ERASEBKGND: // ignore erase background + (*env)->CallVoidMethod(env, window, windowRepaintID, 0, 0, -1, -1); useDefWindowProc = 0; + res = 1; break; @@ -926,7 +933,7 @@ static LRESULT CALLBACK wndProc(HWND wnd, UINT message, if (useDefWindowProc) return DefWindowProc(wnd, message, wParam, lParam); - return 0; + return res; } /* @@ -1052,24 +1059,26 @@ JNIEXPORT jint JNICALL Java_com_jogamp_newt_impl_windows_WindowsScreen_getHeight JNIEXPORT jboolean JNICALL Java_com_jogamp_newt_impl_windows_WindowsWindow_initIDs0 (JNIEnv *env, jclass clazz) { - sizeChangedID = (*env)->GetMethodID(env, clazz, "sizeChanged", "(II)V"); insetsChangedID = (*env)->GetMethodID(env, clazz, "insetsChanged", "(IIII)V"); + sizeChangedID = (*env)->GetMethodID(env, clazz, "sizeChanged", "(II)V"); positionChangedID = (*env)->GetMethodID(env, clazz, "positionChanged", "(II)V"); - focusChangedID = (*env)->GetMethodID(env, clazz, "focusChanged", "(JZ)V"); + focusChangedID = (*env)->GetMethodID(env, clazz, "focusChanged", "(Z)V"); + visibleChangedID = (*env)->GetMethodID(env, clazz, "visibleChanged", "(Z)V"); windowDestroyNotifyID = (*env)->GetMethodID(env, clazz, "windowDestroyNotify", "()V"); windowDestroyedID = (*env)->GetMethodID(env, clazz, "windowDestroyed", "()V"); - enqueueMouseEventID = (*env)->GetMethodID(env, clazz, "enqueueMouseEvent", "(IIIIII)V"); - enqueueKeyEventID = (*env)->GetMethodID(env, clazz, "enqueueKeyEvent", "(IIIC)V"); - sendPaintEventID = (*env)->GetMethodID(env, clazz, "sendPaintEvent", "(IIIII)V"); - if (sizeChangedID == NULL || - insetsChangedID == NULL || + windowRepaintID = (*env)->GetMethodID(env, clazz, "windowRepaint", "(IIII)V"); + sendMouseEventID = (*env)->GetMethodID(env, clazz, "sendMouseEvent", "(IIIIII)V"); + sendKeyEventID = (*env)->GetMethodID(env, clazz, "sendKeyEvent", "(IIIC)V"); + if (insetsChangedID == NULL || + sizeChangedID == NULL || positionChangedID == NULL || focusChangedID == NULL || + visibleChangedID == NULL || windowDestroyNotifyID == NULL || windowDestroyedID == NULL || - enqueueMouseEventID == NULL || - sendPaintEventID == NULL || - enqueueKeyEventID == NULL) + windowRepaintID == NULL || + sendMouseEventID == NULL || + sendKeyEventID == NULL) { return JNI_FALSE; } diff --git a/src/newt/native/X11Window.c b/src/newt/native/X11Window.c index a521d2dbd..dbf833633 100755 --- a/src/newt/native/X11Window.c +++ b/src/newt/native/X11Window.c @@ -151,14 +151,16 @@ static const char * const ClazzNameNewtWindow = "com/jogamp/newt/Window"; static jclass newtWindowClz=NULL; -static jmethodID windowChangedID = NULL; +static jmethodID sizeChangedID = NULL; +static jmethodID positionChangedID = NULL; static jmethodID focusChangedID = NULL; static jmethodID visibleChangedID = NULL; static jmethodID windowDestroyNotifyID = NULL; static jmethodID windowDestroyedID = NULL; +static jmethodID windowRepaintID = NULL; static jmethodID windowCreatedID = NULL; -static jmethodID enqueueMouseEventID = NULL; -static jmethodID enqueueKeyEventID = NULL; +static jmethodID sendMouseEventID = NULL; +static jmethodID sendKeyEventID = NULL; static jmethodID displayCompletedID = NULL; @@ -291,8 +293,7 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Display_CompleteDisplay0 * Window */ -// #define WINDOW_EVENT_MASK ( FocusChangeMask | StructureNotifyMask | ExposureMask | VisibilityNotify ) -#define WINDOW_EVENT_MASK ( FocusChangeMask | StructureNotifyMask | VisibilityNotify ) +#define WINDOW_EVENT_MASK ( FocusChangeMask | StructureNotifyMask | ExposureMask ) static int putPtrIn32Long(unsigned long * dst, uintptr_t src) { int i=0; @@ -505,31 +506,37 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Display_DispatchMessages switch(evt.type) { case ButtonPress: NewtWindows_requestFocus1 ( dpy, evt.xany.window ); - (*env)->CallVoidMethod(env, jwindow, enqueueMouseEventID, (jint) EVENT_MOUSE_PRESSED, + (*env)->CallVoidMethod(env, jwindow, sendMouseEventID, + (jint) EVENT_MOUSE_PRESSED, (jint) evt.xbutton.state, (jint) evt.xbutton.x, (jint) evt.xbutton.y, (jint) evt.xbutton.button, 0 /*rotation*/); break; case ButtonRelease: - (*env)->CallVoidMethod(env, jwindow, enqueueMouseEventID, (jint) EVENT_MOUSE_RELEASED, + (*env)->CallVoidMethod(env, jwindow, sendMouseEventID, + (jint) EVENT_MOUSE_RELEASED, (jint) evt.xbutton.state, (jint) evt.xbutton.x, (jint) evt.xbutton.y, (jint) evt.xbutton.button, 0 /*rotation*/); break; case MotionNotify: - (*env)->CallVoidMethod(env, jwindow, enqueueMouseEventID, (jint) EVENT_MOUSE_MOVED, + (*env)->CallVoidMethod(env, jwindow, sendMouseEventID, + (jint) EVENT_MOUSE_MOVED, (jint) evt.xmotion.state, (jint) evt.xmotion.x, (jint) evt.xmotion.y, (jint) 0, 0 /*rotation*/); break; case KeyPress: - (*env)->CallVoidMethod(env, jwindow, enqueueKeyEventID, (jint) EVENT_KEY_PRESSED, + (*env)->CallVoidMethod(env, jwindow, sendKeyEventID, + (jint) EVENT_KEY_PRESSED, (jint) evt.xkey.state, X11KeySym2NewtVKey(keySym), (jchar) keyChar); break; case KeyRelease: - (*env)->CallVoidMethod(env, jwindow, enqueueKeyEventID, (jint) EVENT_KEY_RELEASED, + (*env)->CallVoidMethod(env, jwindow, sendKeyEventID, + (jint) EVENT_KEY_RELEASED, (jint) evt.xkey.state, X11KeySym2NewtVKey(keySym), (jchar) keyChar); - (*env)->CallVoidMethod(env, jwindow, enqueueKeyEventID, (jint) EVENT_KEY_TYPED, + (*env)->CallVoidMethod(env, jwindow, sendKeyEventID, + (jint) EVENT_KEY_TYPED, (jint) evt.xkey.state, (jint) -1, (jchar) keyChar); break; @@ -546,9 +553,10 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Display_DispatchMessages (unsigned int)evt.xconfigure.window, (unsigned int)evt.xconfigure.event, (unsigned int)evt.xconfigure.above, evt.xconfigure.x, evt.xconfigure.y, evt.xconfigure.width, evt.xconfigure.height, evt.xconfigure.override_redirect); - (*env)->CallVoidMethod(env, jwindow, windowChangedID, - (jint) evt.xconfigure.x, (jint) evt.xconfigure.y, + (*env)->CallVoidMethod(env, jwindow, sizeChangedID, (jint) evt.xconfigure.width, (jint) evt.xconfigure.height); + (*env)->CallVoidMethod(env, jwindow, positionChangedID, + (jint) evt.xconfigure.x, (jint) evt.xconfigure.y); break; case ClientMessage: if (evt.xclient.send_event==True && evt.xclient.data.l[0]==(Atom)wmDeleteAtom) { @@ -568,25 +576,28 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Display_DispatchMessages (*env)->CallVoidMethod(env, jwindow, focusChangedID, JNI_FALSE); break; + case Expose: + DBG_PRINT( "X11: event . Expose call 0x%X %d/%d %dx%d\n", (unsigned int)evt.xexpose.window, + evt.xexpose.x, evt.xexpose.y, evt.xexpose.width, evt.xexpose.height); + + if (evt.xexpose.width > 0 && evt.xexpose.height > 0) { + (*env)->CallVoidMethod(env, jwindow, windowRepaintID, + evt.xexpose.x, evt.xexpose.y, evt.xexpose.width, evt.xexpose.height); + } + break; + case MapNotify: DBG_PRINT( "X11: event . MapNotify call 0x%X\n", (unsigned int)evt.xunmap.window); - // FIXME (*env)->CallVoidMethod(env, jwindow, visibleChangedID, JNI_TRUE); + (*env)->CallVoidMethod(env, jwindow, visibleChangedID, JNI_TRUE); break; case UnmapNotify: DBG_PRINT( "X11: event . UnmapNotify call 0x%X\n", (unsigned int)evt.xunmap.window); - // FIXME (*env)->CallVoidMethod(env, jwindow, visibleChangedID, JNI_FALSE); + (*env)->CallVoidMethod(env, jwindow, visibleChangedID, JNI_FALSE); break; // unhandled events .. yet .. - case VisibilityNotify: - DBG_PRINT( "X11: event . VisibilityNotify call 0x%X\n", (unsigned int)evt.xvisibility.window); - break; - case Expose: - DBG_PRINT( "X11: event . Expose call 0x%X\n", (unsigned int)evt.xexpose.window); - /* FIXME: Might want to send a repaint event .. */ - break; default: DBG_PRINT("X11: event . unhandled %d 0x%X call 0x%X\n", evt.type, evt.type, (unsigned int)evt.xunmap.window); } @@ -653,23 +664,27 @@ JNIEXPORT jint JNICALL Java_com_jogamp_newt_impl_x11_X11Screen_getHeight0 JNIEXPORT jboolean JNICALL Java_com_jogamp_newt_impl_x11_X11Window_initIDs0 (JNIEnv *env, jclass clazz) { - windowChangedID = (*env)->GetMethodID(env, clazz, "windowChanged", "(IIII)V"); + sizeChangedID = (*env)->GetMethodID(env, clazz, "sizeChanged", "(II)V"); + positionChangedID = (*env)->GetMethodID(env, clazz, "positionChanged", "(II)V"); focusChangedID = (*env)->GetMethodID(env, clazz, "focusChanged", "(Z)V"); visibleChangedID = (*env)->GetMethodID(env, clazz, "visibleChanged", "(Z)V"); windowDestroyNotifyID = (*env)->GetMethodID(env, clazz, "windowDestroyNotify", "()V"); windowDestroyedID = (*env)->GetMethodID(env, clazz, "windowDestroyed", "()V"); + windowRepaintID = (*env)->GetMethodID(env, clazz, "windowRepaint", "(IIII)V"); windowCreatedID = (*env)->GetMethodID(env, clazz, "windowCreated", "(J)V"); - enqueueMouseEventID = (*env)->GetMethodID(env, clazz, "enqueueMouseEvent", "(IIIIII)V"); - enqueueKeyEventID = (*env)->GetMethodID(env, clazz, "enqueueKeyEvent", "(IIIC)V"); + sendMouseEventID = (*env)->GetMethodID(env, clazz, "sendMouseEvent", "(IIIIII)V"); + sendKeyEventID = (*env)->GetMethodID(env, clazz, "sendKeyEvent", "(IIIC)V"); - if (windowChangedID == NULL || + if (sizeChangedID == NULL || + positionChangedID == NULL || focusChangedID == NULL || visibleChangedID == NULL || windowDestroyNotifyID == NULL || windowDestroyedID == NULL || + windowRepaintID == NULL || windowCreatedID == NULL || - enqueueMouseEventID == NULL || - enqueueKeyEventID == NULL) { + sendMouseEventID == NULL || + sendKeyEventID == NULL) { return JNI_FALSE; } return JNI_TRUE; @@ -749,12 +764,17 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_x11_X11Window_CreateWindow0 pVisualQuery=NULL; } - attrMask = ( CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect ) ; + attrMask = ( CWBackingStore | CWBackingPlanes | CWBackingPixel | CWBackPixel | + CWBorderPixel | CWColormap | CWOverrideRedirect ) ; memset(&xswa, 0, sizeof(xswa)); xswa.override_redirect = ( 0 != parent ) ? True : False ; xswa.border_pixel = 0; xswa.background_pixel = 0; + xswa.backing_store=NotUseful; /* NotUseful, WhenMapped, Always */ + xswa.backing_planes=0; /* planes to be preserved if possible */ + xswa.backing_pixel=0; /* value to use in restoring planes */ + xswa.colormap = XCreateColormap(dpy, windowParent, // XRootWindow(dpy, scrn_idx), visual, @@ -781,6 +801,7 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_x11_X11Window_CreateWindow0 setJavaWindowProperty(env, dpy, window, javaObjectAtom, (*env)->NewGlobalRef(env, obj)); + // XClearWindow(dpy, window); XSync(dpy, False); { @@ -806,7 +827,7 @@ JNIEXPORT jlong JNICALL Java_com_jogamp_newt_impl_x11_X11Window_CreateWindow0 * Signature: (JJ)V */ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Window_CloseWindow0 - (JNIEnv *env, jobject obj, jlong display, jlong window, jlong javaObjectAtom) + (JNIEnv *env, jobject obj, jlong display, jlong window, jlong javaObjectAtom, jlong wmDeleteAtom) { Display * dpy = (Display *) (intptr_t) display; Window w = (Window)window; @@ -827,14 +848,19 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Window_CloseWindow0 _throwNewRuntimeException(dpy, env, "Internal Error .. Window global ref not the same!"); return; } - (*env)->DeleteGlobalRef(env, jwindow); XSync(dpy, False); XSelectInput(dpy, w, 0); XUnmapWindow(dpy, w); + + // Drain all events related to this window .. + JNICALL Java_com_jogamp_newt_impl_x11_X11Display_DispatchMessages0(env, obj, display, javaObjectAtom, wmDeleteAtom); + XDestroyWindow(dpy, w); XSync(dpy, False); + (*env)->DeleteGlobalRef(env, jwindow); + DBG_PRINT( "X11: CloseWindow END\n"); (*env)->CallVoidMethod(env, obj, windowDestroyedID); @@ -922,6 +948,11 @@ static void NewtWindows_reparentWindow DBG_PRINT( "X11: reparentWindow dpy %p, parent %p/%p, win %p, %d/%d undec %d\n", (void*)dpy, (void*) jparent, (void*)parent, (void*)w, x, y, undecorated); + // don't propagate events during reparenting + // long orig_xevent_mask = xwa->your_event_mask ; + /* XSelectInput(dpy, w, orig_xevent_mask & ~ ( StructureNotifyMask ) ); + XSync(dpy, False); */ + if(0 != jparent) { // move into parent .. NewtWindows_setDecorations (dpy, w, False); @@ -955,6 +986,14 @@ static void NewtWindows_reparentWindow XSync(dpy, False); } + /* XSelectInput(dpy, w, orig_xevent_mask); + XSync(dpy, False); */ + + if(JNI_TRUE == isVisible) { + NewtWindows_requestFocus0 ( dpy, w, xwa ); + XSync(dpy, False); + } + DBG_PRINT( "X11: reparentWindow X\n"); } @@ -993,12 +1032,6 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Window_setPosSizeDecor0 xwc.width=width; xwc.height=height; XConfigureWindow(dpy, w, CWX|CWY|CWWidth|CWHeight, &xwc); - - if(JNI_TRUE == isVisible) { - XGetWindowAttributes(dpy, w, &xwa); - NewtWindows_requestFocus0 ( dpy, w, &xwa ); - XSync(dpy, False); - } } /* @@ -1039,7 +1072,7 @@ JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Window_reparentWindow0 JNIEXPORT void JNICALL Java_com_jogamp_newt_impl_x11_X11Window_requestFocus0 (JNIEnv *env, jobject obj, jlong display, jlong window) { - NewtWindows_requestFocus ( (Display *) (intptr_t) display, (Window)window ) ; + NewtWindows_requestFocus1 ( (Display *) (intptr_t) display, (Window)window ) ; } /* |