patch for broken SWT

Found a bug? Post here.
Post Reply
User avatar
arsiamons
Posts: 6
Joined: Tue 12. Jan 2010, 03:36

patch for broken SWT

Post by arsiamons » Fri 15. Jan 2010, 21:28

Hi!
As i have written, i use jReality within an EclipseRCP application under native SWT. To get this working i have done several changes in the SWT classes already existing within the SVN repository.
One change that is necessary beyond the SWT implementation is the method within the JOGLRenderer class to set the rendering size:

Code: Select all

Index: src/de/jreality/jogl/JOGLRenderer.java
===================================================================
--- src/de/jreality/jogl/JOGLRenderer.java	(revision 212)
+++ src/de/jreality/jogl/JOGLRenderer.java	(working copy)
@@ -614,4 +614,9 @@
 		throw new IllegalArgumentException("Picking has been removed from JOGL renderer");
 	}
 
+	public void setSize(int width, int height) {
+		this.width = width;
+		this.height = height;
+	}
+
 }
The SWT classes seem to be very rough implemented... What can I do to improve them? E.g. it should be possible (with this code?) to be more generic and support different rendering backends? What else may be helpful to do?

Code: Select all

Index: src/de/jreality/jogl/SwtQueue.java
===================================================================
--- src/de/jreality/jogl/SwtQueue.java	(revision 212)
+++ src/de/jreality/jogl/SwtQueue.java	(working copy)
@@ -63,16 +63,26 @@
   }
   
   private SwtQueue() {
-    swtThread = new Thread(this);
-    swtThread.start();
-    synchronized(initLock) {
-      try {
-        while (!inited) initLock.wait();
-      } catch (InterruptedException e) {
-        // TODO Auto-generated catch block
-        e.printStackTrace();
-      }
-    }
+	  // check if we have already a display
+	  Display disp = Display.getDefault();
+	  if (disp != null) {
+		  // display already existing
+	      inited = false;
+	      display = disp;
+	      swtThread = disp.getThread();
+	  }
+	  else {
+		  swtThread = new Thread(this);
+		  swtThread.start();
+		  synchronized(initLock) {
+			  try {
+				  while (!inited) initLock.wait();
+			  } catch (InterruptedException e) {
+				  // TODO Auto-generated catch block
+				  e.printStackTrace();
+			  }
+		  }
+	  }
   }
   
   public Display getDisplay() {
@@ -90,7 +100,8 @@
         shell[0].addDisposeListener(new DisposeListener() {
           public void widgetDisposed(DisposeEvent arg0) {
             shellCnt--;
-            if (shellCnt==0) display.dispose();
+            // inited == false if the display was not constructed by this class
+            if (shellCnt==0 && inited) display.dispose();
           }
         });
       };
Index: src/de/jreality/jogl/SwtViewer.java
===================================================================
--- src/de/jreality/jogl/SwtViewer.java	(revision 212)
+++ src/de/jreality/jogl/SwtViewer.java	(working copy)
@@ -228,8 +228,10 @@
   int rot = 0;
 
   public void init() {
+	canvas.setCurrent();
+	
+	// track the size of the window
     canvas.addListener(SWT.Resize, new Listener() {
-
       public void handleEvent(Event event) {
         Rectangle bounds = canvas.getBounds();
         myDimension.width=bounds.width;
@@ -237,6 +239,15 @@
         renderer.setSize(myDimension.width, myDimension.height);
       }
     });
+    
+    // track update events
+    canvas.addListener(SWT.Paint, new Listener() {
+		@Override
+		public void handleEvent(Event event) {
+			renderAsync();
+		}
+    });
+    
     context = GLDrawableFactory.getFactory().createExternalGLContext();
     Rectangle bounds = canvas.getBounds();
     myDimension.width=bounds.width;
Index: src/de/jreality/toolsystem/raw/DeviceMouseSWT.java
===================================================================
--- src/de/jreality/toolsystem/raw/DeviceMouseSWT.java	(revision 212)
+++ src/de/jreality/toolsystem/raw/DeviceMouseSWT.java	(working copy)
@@ -42,6 +42,7 @@
 
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.MouseEvent;
@@ -156,7 +157,7 @@
     component.getDisplay().syncExec(r);
   }
 
-  public void initialize(Viewer viewer) {
+  public void initialize(Viewer viewer, Map<String,Object> config) {
     if (!(viewer instanceof SwtViewer)) throw new RuntimeException("only for SWT viewer!");
     setComponent(((SwtViewer)viewer).getGLCanvas());
   }
Index: src/de/jreality/toolsystem/raw/DeviceKeyboardSWT.java
===================================================================
--- src/de/jreality/toolsystem/raw/DeviceKeyboardSWT.java	(revision 212)
+++ src/de/jreality/toolsystem/raw/DeviceKeyboardSWT.java	(working copy)
@@ -42,6 +42,7 @@
 
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 
 import org.eclipse.swt.events.KeyEvent;
 import org.eclipse.swt.events.KeyListener;
@@ -85,7 +86,7 @@
     // maps InputDevices to Timers performing "keyReleased"
     private HashMap pendingReleases=new HashMap();
 
-    public void initialize(Viewer viewer) {
+    public void initialize(Viewer viewer, Map<String,Object> config) {
       if (!(viewer instanceof SwtViewer)) throw new RuntimeException("only for SWT viewer!");
       setComponent(((SwtViewer)viewer).getGLCanvas());
     }

STRESS
Posts: 141
Joined: Mon 19. Jan 2009, 12:10

Re: patch for broken SWT

Post by STRESS » Mon 18. Jan 2010, 11:18

Hi

I also did some changes to get the native SWT backend working again. I think there are some posts somewhere in Advanced Development section of this forum. Maybe we can synchronize our changes. Basically I made it so it can more or less work with the existing backend selecting structure, but I haven't synced it with the changes done by the development team in the meantime. I know there have been some changes made.

It would be nice to port the software backend to SWT as well but that looks like a lot of more work to me and I would suggest fixing some of its problems first.

Post Reply