javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/Platform.java
changeset 80 d6dafc5d983f
child 87 1627c337e51e
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 /**
       
    18  * \file
       
    19  * \brief Target platform dependent Java module for Symbian.
       
    20  *
       
    21  */
       
    22 
       
    23 package com.nokia.microedition.m2g;
       
    24 import javax.microedition.lcdui.Graphics;
       
    25 import javax.microedition.lcdui.Image;
       
    26 import org.eclipse.swt.widgets.Display;
       
    27 
       
    28 import com.nokia.mj.impl.nokialcdui.LCDUIInvoker;
       
    29 import org.eclipse.swt.internal.extension.DisplayExtension;
       
    30 import org.eclipse.swt.internal.extension.Toolkit;
       
    31 
       
    32 /**
       
    33  * Implements platform-dependent functionality. At the moment, this
       
    34  * includes native finalization and some helper methods for
       
    35  * synchronizing 2D and 3D rendering.
       
    36  */
       
    37 class Platform
       
    38 {
       
    39     /**
       
    40      * eSWT display for ui thread access
       
    41      */
       
    42     private static Display display = null;
       
    43     private static boolean libraryLoaded = false;
       
    44 
       
    45     //------------------------------------------------------------------
       
    46     // Package private methods
       
    47     //------------------------------------------------------------------
       
    48 
       
    49     /**
       
    50      * Executes given runnable in UI thread if caller thread is not UI thread
       
    51      */
       
    52     static void executeInUIThread(M2GRunnableQt obj)
       
    53     {
       
    54         if (display == null)
       
    55         {
       
    56             if (!uiThreadAvailable())
       
    57             {
       
    58                 throw new Error("Ui thread not available");
       
    59             }
       
    60         }
       
    61         if (display.isDisposed())
       
    62         {
       
    63             throw new Error("Display already disposed");
       
    64         }
       
    65 
       
    66         if (obj != null)
       
    67         {
       
    68             // If we are not in UI thread use display
       
    69             if (Thread.currentThread() != display.getThread())
       
    70             {
       
    71                 display.syncExec(obj);
       
    72             }
       
    73             else
       
    74             {
       
    75                 // In this case we are in UI thread so just execute directly
       
    76                 obj.run();
       
    77             }
       
    78             // Check if any exceptions occured in execution
       
    79             // and throw forward in caller thread
       
    80             obj.checkAndThrow();
       
    81         }
       
    82     }
       
    83 
       
    84     /**
       
    85      * Check the UI thread / toolkit init status and store display if it is available
       
    86      * @return true if either lcdui or eswt toolkit is initialized and ui thread is accessible
       
    87      *              otherwise false
       
    88      */
       
    89     static boolean uiThreadAvailable()
       
    90     {
       
    91         if (display != null)
       
    92         {
       
    93             return true;
       
    94         }
       
    95         else
       
    96         {
       
    97             //display = DisplayExtension.getDisplayInstance();
       
    98         	//As now the thread is always available we are taking it from Toolkit.
       
    99         	display  = Toolkit. getInternalDisplay();
       
   100         	
       
   101 			
       
   102 			if (display == null)
       
   103             {
       
   104 				
       
   105                 return false;
       
   106             }
       
   107             else
       
   108             {
       
   109             		
       
   110                 // UI thread is available, so load native library if not already loaded
       
   111                 if (!libraryLoaded)
       
   112                 {
       
   113                 		
       
   114                     com.nokia.mj.impl.rt.support.Jvm.loadSystemLibrary("javam2g");
       
   115                     libraryLoaded = true;
       
   116                 }
       
   117                 return true;
       
   118             }
       
   119         }
       
   120     }
       
   121     
       
   122     /**
       
   123      * Load Library if not already loaded
       
   124      * 
       
   125      
       
   126     static void loadLibarary()
       
   127     {
       
   128     	if (!libraryLoaded)
       
   129         {
       
   130     		
       
   131             com.nokia.mj.impl.rt.support.Jvm.loadSystemLibrary("javam2g");
       
   132             libraryLoaded = true;
       
   133         }
       
   134     }*/
       
   135 
       
   136     /**
       
   137      * Registers an Object3D in the global handle-to-object map. The
       
   138      * handle of the object must already be set at this point!
       
   139      */
       
   140 //    static final void registerFinalizer(Object3D obj)
       
   141 //    {
       
   142 //        //heuristicGC();
       
   143 //    }
       
   144 //
       
   145 //    /**
       
   146 //     * Registers a Graphics3D object (not derived from Object3D) for
       
   147 //     * finalization.
       
   148 //     */
       
   149 //    static final void registerFinalizer(Graphics3D g3d)
       
   150 //    {
       
   151 //        //heuristicGC();
       
   152 //    }
       
   153 //
       
   154 //    /**
       
   155 //     * Registers an Interface object for finalization
       
   156 //     */
       
   157 //    static final void registerFinalizer(Interface m2G)
       
   158 //    {
       
   159 //    }
       
   160 //
       
   161 //    /**
       
   162 //     * Registers a Loader object for finalization
       
   163 //     */
       
   164 //    static final void registerFinalizer(Loader loader)
       
   165 //    {
       
   166 //    }
       
   167 //
       
   168 //    /**
       
   169 //     * Flushes all pending rendering to a Graphics context and blocks
       
   170 //     * until finished
       
   171 //     */
       
   172 //    static final void sync(Graphics g)
       
   173 //    {
       
   174 //        //ToolkitInvoker invoker = ToolkitInvoker.getToolkitInvoker();
       
   175 //        //invoker.toolkitSync(invoker.getToolkit());
       
   176 //    }
       
   177 //
       
   178 //    /**
       
   179 //     * Flushes all pending rendering to an Image object
       
   180 //     */
       
   181 //    static final void sync(Image img)
       
   182 //    {
       
   183 //        //ToolkitInvoker invoker = ToolkitInvoker.getToolkitInvoker();
       
   184 //        //invoker.toolkitSync(invoker.getToolkit());
       
   185 //    }
       
   186 //
       
   187 //    /**
       
   188 //     * Finalizes the native peer of an interface
       
   189 //     */
       
   190 //    static final native void finalizeInterface(int handle);
       
   191 //
       
   192 //    /**
       
   193 //     * Finalizes the native peer of an object
       
   194 //     * JCF: added this wrapper method so we could pass the toolkit handle to the native method.
       
   195 //     */
       
   196     static final void finalizeObject(int handle)
       
   197     {
       
   198         try
       
   199         {
       
   200             final int finalHandle = handle;
       
   201             executeInUIThread(
       
   202                 new M2GRunnableQt()
       
   203             {
       
   204                 void doRun()
       
   205                 {
       
   206                     _finalizeObject(finalHandle);
       
   207                 }
       
   208             });
       
   209         }
       
   210         catch (Exception e)
       
   211         {
       
   212             // do nothing
       
   213         }
       
   214     }
       
   215 
       
   216     /**
       
   217      * Finalizes the native peer of an object associated with
       
   218      * given Interface instance
       
   219      */
       
   220 //    static final void finalizeObject(int handle, Interface aInterface)
       
   221 //    {
       
   222 //        try
       
   223 //        {
       
   224 //            final int finalHandle = handle;
       
   225 //            executeInUIThread(
       
   226 //                new M2GRunnableQt()
       
   227 //            {
       
   228 //                public void doRun()
       
   229 //                {
       
   230 //                    _finalizeObject(finalHandle);
       
   231 //                }
       
   232 //            });
       
   233 //        }
       
   234 //        catch (Exception e)
       
   235 //        {
       
   236 //            // do nothing
       
   237 //        }
       
   238 //    }
       
   239 
       
   240 
       
   241     //------------------------------------------------------------------
       
   242     // Private methods
       
   243     //------------------------------------------------------------------
       
   244 
       
   245     /**
       
   246      * Trigger GC if minimum free memory limit has been exceeded in the native side
       
   247      */
       
   248     static final void heuristicGC()
       
   249     {
       
   250     }
       
   251     private static final native void _finalizeObject(int handle);
       
   252 }
       
   253