[libbluray-devel] Per-xlet event queues

hpi1 git at videolan.org
Sat Mar 30 01:39:09 CET 2013


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Sat Mar 30 01:53:54 2013 +0200| [a8a50855a08d21032ddd9418593d216f29cdcb08] | committer: hpi1

Per-xlet event queues

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=a8a50855a08d21032ddd9418593d216f29cdcb08
---

 .../bdj/java-j2me/java/awt/BDToolkit.java          |   38 ++++++++++++++++++++
 .../bdj/java-j2se/java/awt/BDToolkit.java          |   38 ++++++++++++++++++++
 src/libbluray/bdj/java/java/awt/BDRootWindow.java  |    6 +++-
 src/libbluray/bdj/java/org/havi/ui/HComponent.java |    3 ++
 src/libbluray/bdj/java/org/havi/ui/HScene.java     |    3 ++
 .../bdj/java/org/videolan/BDJAppProxy.java         |   18 ++++++++++
 .../bdj/java/org/videolan/BDJXletContext.java      |   10 ++++++
 7 files changed, 115 insertions(+), 1 deletion(-)

diff --git a/src/libbluray/bdj/java-j2me/java/awt/BDToolkit.java b/src/libbluray/bdj/java-j2me/java/awt/BDToolkit.java
index c439fad..7798506 100644
--- a/src/libbluray/bdj/java-j2me/java/awt/BDToolkit.java
+++ b/src/libbluray/bdj/java-j2me/java/awt/BDToolkit.java
@@ -23,7 +23,9 @@ import java.awt.image.ColorModel;
 import java.awt.image.ImageObserver;
 import java.awt.image.ImageProducer;
 import java.net.URL;
+import java.util.Collections;
 import java.util.Hashtable;
+import java.util.WeakHashMap;
 import java.util.Map;
 import java.util.Iterator;
 
@@ -193,7 +195,43 @@ public class BDToolkit extends Toolkit {
 
     public void beep() { }
 
+    // mapping of Components to AppContexts, WeakHashMap<Component,AppContext>
+    private static final Map contextMap =
+        Collections.synchronizedMap(new WeakHashMap());
+
+    public static void addComponent(Component component) {
+
+        BDJXletContext context = BDJXletContext.getCurrentContext();
+        if (context == null) {
+            logger.warning("addComponent() outside of app context");
+            return;
+        }
+        contextMap.put(component, context);
+    }
+
+    public static EventQueue getEventQueue(Component component) {
+        if (component != null) {
+            BDJXletContext ctx = (BDJXletContext)contextMap.get(component);
+            if (ctx != null) {
+                EventQueue eq = ctx.getEventQueue();
+                if (eq == null) {
+                    logger.warning("getEventQueue() failed: no context event queue");
+                }
+                return eq;
+            }
+            logger.warning("getEventQueue() failed: no context");
+        }
+        return null;
+    }
+
     protected EventQueue getSystemEventQueueImpl() {
+        BDJXletContext ctx = BDJXletContext.getCurrentContext();
+        if (ctx != null) {
+            EventQueue eq = ctx.getEventQueue();
+            if (eq != null) {
+                return eq;
+            }
+        }
         return eventQueue;
     }
 }
diff --git a/src/libbluray/bdj/java-j2se/java/awt/BDToolkit.java b/src/libbluray/bdj/java-j2se/java/awt/BDToolkit.java
index fecec43..c35b1e1 100644
--- a/src/libbluray/bdj/java-j2se/java/awt/BDToolkit.java
+++ b/src/libbluray/bdj/java-j2se/java/awt/BDToolkit.java
@@ -25,7 +25,9 @@ import java.awt.image.ImageObserver;
 import java.awt.image.ImageProducer;
 import java.awt.peer.KeyboardFocusManagerPeer;
 import java.net.URL;
+import java.util.Collections;
 import java.util.Hashtable;
+import java.util.WeakHashMap;
 import java.util.Map;
 import java.util.Iterator;
 
@@ -206,7 +208,43 @@ public class BDToolkit extends Toolkit implements KeyboardFocusManagerPeerProvid
     public void beep() {
     }
 
+    // mapping of Components to AppContexts, WeakHashMap<Component,AppContext>
+    private static final Map contextMap =
+        Collections.synchronizedMap(new WeakHashMap());
+
+    public static void addComponent(Component component) {
+
+        BDJXletContext context = BDJXletContext.getCurrentContext();
+        if (context == null) {
+            logger.warning("addComponent() outside of app context");
+            return;
+        }
+        contextMap.put(component, context);
+    }
+
+    public static EventQueue getEventQueue(Component component) {
+        if (component != null) {
+            BDJXletContext ctx = (BDJXletContext)contextMap.get(component);
+            if (ctx != null) {
+                EventQueue eq = ctx.getEventQueue();
+                if (eq == null) {
+                    logger.warning("getEventQueue() failed: no context event queue");
+                }
+                return eq;
+            }
+            logger.warning("getEventQueue() failed: no context");
+        }
+        return null;
+    }
+
     protected EventQueue getSystemEventQueueImpl() {
+        BDJXletContext ctx = BDJXletContext.getCurrentContext();
+        if (ctx != null) {
+            EventQueue eq = ctx.getEventQueue();
+            if (eq != null) {
+                return eq;
+            }
+        }
         return eventQueue;
     }
 
diff --git a/src/libbluray/bdj/java/java/awt/BDRootWindow.java b/src/libbluray/bdj/java/java/awt/BDRootWindow.java
index 1fa8274..d0a35d9 100644
--- a/src/libbluray/bdj/java/java/awt/BDRootWindow.java
+++ b/src/libbluray/bdj/java/java/awt/BDRootWindow.java
@@ -66,6 +66,10 @@ public class BDRootWindow extends Frame {
         return null;
     }
 
+    public static void stopEventQueue(EventQueue eq) {
+        eq.getDispatchThread().stopDispatching();
+    }
+
     public void postKeyEvent(int id, int modifiers, int keyCode) {
         Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getGlobalFocusOwner();
         if (focusOwner != null) {
@@ -76,7 +80,7 @@ public class BDRootWindow extends Frame {
                     event = new KeyEvent(focusOwner, id, when, modifiers, KeyEvent.VK_UNDEFINED, (char)keyCode);
                 else
                     event = new KeyEvent(focusOwner, id, when, modifiers, keyCode, KeyEvent.CHAR_UNDEFINED);
-                Toolkit.getEventQueue().postEvent(event);
+                BDToolkit.getEventQueue(focusOwner).postEvent(event);
                 return;
             } catch (Throwable e) {
                 System.err.println(" *** " + e + "");
diff --git a/src/libbluray/bdj/java/org/havi/ui/HComponent.java b/src/libbluray/bdj/java/org/havi/ui/HComponent.java
index 5459d58..c853952 100644
--- a/src/libbluray/bdj/java/org/havi/ui/HComponent.java
+++ b/src/libbluray/bdj/java/org/havi/ui/HComponent.java
@@ -24,11 +24,14 @@ import java.awt.AWTEvent;
 import java.awt.Component;
 import org.dvb.ui.TestOpacity;
 
+import java.awt.BDToolkit;
+
 public abstract class HComponent extends Component implements HMatteLayer,
         TestOpacity {
     public HComponent()
     {
         this(0, 0, 0, 0);
+        BDToolkit.addComponent(this);
     }
 
     public HComponent(int x, int y, int width, int height)
diff --git a/src/libbluray/bdj/java/org/havi/ui/HScene.java b/src/libbluray/bdj/java/org/havi/ui/HScene.java
index e614a04..336b91a 100644
--- a/src/libbluray/bdj/java/org/havi/ui/HScene.java
+++ b/src/libbluray/bdj/java/org/havi/ui/HScene.java
@@ -36,10 +36,13 @@ import java.util.Map;
 import org.havi.ui.event.HEventGroup;
 import org.videolan.BDJXletContext;
 import org.videolan.GUIManager;
+import java.awt.BDToolkit;
+
 
 public class HScene extends Container implements HComponentOrdering {
     protected HScene() {
         context = BDJXletContext.getCurrentContext();
+        BDToolkit.addComponent(this);
     }
 
     public BDJXletContext getXletContext() {
diff --git a/src/libbluray/bdj/java/org/videolan/BDJAppProxy.java b/src/libbluray/bdj/java/org/videolan/BDJAppProxy.java
index 1b84004..824c171 100644
--- a/src/libbluray/bdj/java/org/videolan/BDJAppProxy.java
+++ b/src/libbluray/bdj/java/org/videolan/BDJAppProxy.java
@@ -23,6 +23,8 @@ import org.dvb.application.AppStateChangeEvent;
 import org.dvb.application.AppStateChangeEventListener;
 import org.dvb.application.DVBJProxy;
 
+import java.awt.EventQueue;
+
 import java.io.File;
 import java.util.LinkedList;
 import javax.tv.xlet.Xlet;
@@ -37,6 +39,14 @@ public class BDJAppProxy implements DVBJProxy, Runnable {
         thread = new Thread(threadGroup, this);
         thread.setDaemon(true);
         thread.start();
+
+        /* wait until thread has been started and event queue is initialized.
+         * We want event dispatcher thread to be inside xlet thread group
+         * -> event queue must be created from thread running inside applet thread group.
+         */
+        while (context.getEventQueue() == null) {
+            Thread.yield();
+        }
     }
 
     public int getState() {
@@ -136,6 +146,12 @@ public class BDJAppProxy implements DVBJProxy, Runnable {
         } catch (InterruptedException e) {
 
         }
+
+        EventQueue eq = context.getEventQueue();
+        context.setEventQueue(null);
+        if (eq != null) {
+            GUIManager.stopEventQueue(eq);
+        }
     }
 
     public void addAppStateChangeEventListener(AppStateChangeEventListener listener) {
@@ -275,6 +291,8 @@ public class BDJAppProxy implements DVBJProxy, Runnable {
     }
 
     public void run() {
+        context.setEventQueue(new EventQueue());
+
         for (;;) {
             AppCommand cmd;
             synchronized(cmds) {
diff --git a/src/libbluray/bdj/java/org/videolan/BDJXletContext.java b/src/libbluray/bdj/java/org/videolan/BDJXletContext.java
index 81c9a61..5112cbe 100644
--- a/src/libbluray/bdj/java/org/videolan/BDJXletContext.java
+++ b/src/libbluray/bdj/java/org/videolan/BDJXletContext.java
@@ -19,6 +19,7 @@
 package org.videolan;
 
 import java.awt.Container;
+import java.awt.EventQueue;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
@@ -81,6 +82,14 @@ public class BDJXletContext implements javax.tv.xlet.XletContext, javax.microedi
         return loader;
     }
 
+    protected void setEventQueue(EventQueue eq) {
+        eventQueue = eq;
+    }
+
+    public EventQueue getEventQueue() {
+        return eventQueue;
+    }
+
     public static BDJXletContext getCurrentContext() {
         Object obj = AccessController.doPrivileged(
                 new PrivilegedAction() {
@@ -114,4 +123,5 @@ public class BDJXletContext implements javax.tv.xlet.XletContext, javax.microedi
     private AppID appid;
     private BDJClassLoader loader;
     private Container container;
+    private EventQueue eventQueue = null;
 }



More information about the libbluray-devel mailing list