midpnativeappmanagerpeer.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 864 行 · 第 1/2 页

C
864
字号
/* * * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */#include <jvmconfig.h>#include <kni.h>#include <jvm.h>#include <jvmspi.h>#include <sni.h>#include <midpAMS.h>#include <suitestore_common.h>#include <suitestore_kni_util.h>#include <midpEvents.h>#include <midpServices.h>#include <midpMidletSuiteUtils.h>#include <midpNativeAppManager.h>#include <midpEventUtil.h>#include <pcsl_string.h>#include <midpMalloc.h>#include <midpString.h>#include <midpError.h>#include <midpUtilKni.h>#include <midp_runtime_info.h>#include <heap.h>#include <commandLineUtil_md.h>/** The name of the native application manager peer internal class. */#define APP_MANAGER_PEER "com.sun.midp.main.NativeAppManagerPeer"/** Static buffer where to save the runtime information passed from Java. */static MidletRuntimeInfo g_runtimeInfoBuf;/** * Notifies the registered listeners about the given event. * * @param listenerType type of listeners that should be notified * @param pEventData a structure containing all information about the event *  that caused the call of the listener * * @return error code (ALL_OK if successful) */MIDPErrornams_listeners_notify(NamsListenerType listenerType,                      const NamsEventData* pEventData) {    NamsEventData* pListener =        (NamsEventData*) get_event_listeners_impl((int)listenerType);    while (pListener) {        if (pListener->genericListener.listenerType == (int)listenerType) {            ((MIDP_NAMS_EVENT_LISTENER)                pListener->genericListener.fn_callback)(pEventData);        }        pListener = (NamsEventData*)pListener->genericListener.pNext;    }    return ALL_OK;}/** * Initializes the system. This function must be called before setting * the listeners and calling midp_system_start(). * * @return error code: (ALL_OK if successful) */MIDPError midp_system_initialize(void) {    char *pAppDir, *pConfDir;	int midp_heap_requirement = getHeapRequirement();    JVM_Initialize();    /*     * Set Java heap capacity now so it can been overridden from command     * line.     */    JVM_SetConfig(JVM_CONFIG_HEAP_CAPACITY, midp_heap_requirement);    /* set up appDir before calling midp_system_start */    pAppDir = getApplicationDir(NULL);    if (pAppDir == NULL) {        return GENERAL_ERROR;    }    midpSetAppDir(pAppDir);    /* get midp configuration directory, set it */    pConfDir = getConfigurationDir(NULL);    if (pConfDir == NULL) {        return GENERAL_ERROR;    }    midpSetConfigDir(pConfDir);    if (midpInitialize() != 0) {        return GENERAL_ERROR;    }    return init_listeners_impl();}/** * Clean up the system. Notifies the listeners that the Java system was stopped. * * @param status current Java system status * * @return ALL_OK if it was a normal system shutdown, an error code otherwise */static MIDPError system_cleanup(int status) {    NamsEventData eventData;    MIDPError errCode;    memset((char*)&eventData, 0, sizeof(NamsEventData));    eventData.event = MIDP_NAMS_EVENT_STATE_CHANGED;    eventData.state = MIDP_SYSTEM_STATE_STOPPED;    nams_listeners_notify(SYSTEM_EVENT_LISTENER, &eventData);    switch (status) {        case MIDP_SHUTDOWN_STATUS: {            errCode = ALL_OK;            break;        }        default: {            errCode = GENERAL_ERROR;            break;        }    }    return errCode;}/** * Starts the system. Does not return until the system is stopped. * * @return <tt>ALL_OK</tt> if the system is shutting down or *         <tt>GENERAL_ERROR</tt> if an error */MIDPError midp_system_start(void) {    int vmStatus;    MIDPError errCode;    vmStatus = midpRunMainClass(NULL, APP_MANAGER_PEER, 0, NULL);    if (MIDP_RUNNING_STATUS != vmStatus) {        errCode = system_cleanup(vmStatus);    } else {        errCode = ALL_OK;    }    return errCode;}/** * Stops the system. * * @return error code: ALL_OK if the operation was started successfully */MIDPError midp_system_stop(void) {    MidpEvent evt;    MIDP_EVENT_INITIALIZE(evt);    evt.type = SHUTDOWN_EVENT;    midpStoreEventAndSignalAms(evt);    return ALL_OK;}/** * Adds a listener of the given type. * * @param listener pointer to a callback function that will be called when * an event of the given type happens * @param listenerType defines on which type of events (SYSTEM, MIDLET or * DISPLAY) this listener should be invoked * * @return error code: ALL_OK if successful, *                     OUT_OF_MEMORY if not enough memory, *                     BAD_PARAMS if listener is NULL */MIDPError midp_add_event_listener(MIDP_NAMS_EVENT_LISTENER listener,                                  NamsListenerType listenerType) {    NamsEventData newListener;    newListener.genericListener.dataSize = sizeof(NamsEventData);    newListener.genericListener.fn_callback  = (void*)listener;    newListener.genericListener.listenerType = (int)listenerType;    return add_event_listener_impl((GenericListener*)&newListener);}/** * Removes the given listener of the given type. * * @param listener listener that should be removed * @param listenerType defines for which type of events (SYSTEM, MIDLET or * DISPLAY) this listener was added * * @return error code: ALL_OK if successful, *                     NOT_FOUND if the listener was not found */MIDPErrormidp_remove_event_listener(MIDP_NAMS_EVENT_LISTENER listener,                           NamsListenerType listenerType) {    return remove_event_listener_impl((void*)listener, (int)listenerType);}/** * Removes all listeners of the given type. * * @param listenerType defines for which type of events (SYSTEM, MIDLET or * DISPLAY) the registered listeneres must be removed * * @return error code: ALL_OK if successful, *                     NOT_FOUND if there are no registered listeners *                               of the given type */MIDPErrormidp_remove_all_event_listeners(NamsListenerType listenerType) {    return remove_all_event_listeners_impl((int)listenerType);}/** * Create and start the specified MIDlet. The suiteId is passed to the * midletsuitestorage API to retrieve the class path. * * @param suiteId             The application suite ID * @param className           Fully qualified name of the MIDlet class * @param classNameLen        Length of the MIDlet class name * @param args                An array containning up to 3 arguments for *                            the MIDlet to be run * @param argsLen             An array containing the length of each argument * @param argsNum             Number of arguments * @param appId               The application id used to identify the app * @param pRuntimeInfo Quotas and profile to set for the new application * * @return error code: ALL_OK if the operation was started successfully, *                     BAD_PARAMS if suiteId is invalid or pClassName is null */MIDPError midp_midlet_create_start_with_args(SuiteIdType suiteId,        const jchar *className, jint classNameLen,        const jchar **args, const jint *argsLen,        jint argsNum, jint appId, const MidletRuntimeInfo* pRuntimeInfo) {    MidpEvent evt;    pcsl_string temp;    int i;    /*     * evt.stringParam1 is a midlet class name,     * evt.stringParam2 is a display name,     * evt.stringParam3-5 - the arguments     * evt.stringParam6 - profile name     */    pcsl_string* params[3];    params[0] = &evt.stringParam3;    params[1] = &evt.stringParam4;    params[2] = &evt.stringParam5;    MIDP_EVENT_INITIALIZE(evt);    if (PCSL_STRING_OK ==        pcsl_string_convert_from_utf16(className, classNameLen, &temp)) {        if (pcsl_string_utf16_length(&temp) > 0) {            evt.stringParam1 = temp;        } else {            pcsl_string_free(&temp);            return BAD_PARAMS;        }    }    /* Initialize arguments for the midlet to be run. */    for (i = 0; i < 3; i++) {        if ((i >= argsNum) || (argsLen[i] == 0)) {            *params[i] = PCSL_STRING_NULL;        } else {            if (PCSL_STRING_OK ==                pcsl_string_convert_from_utf16(args[i], argsLen[i], &temp)) {                if (pcsl_string_utf16_length(&temp) > 0) {                    *params[i] = temp;                } else {                    pcsl_string_free(&temp);                    *params[i] = PCSL_STRING_NULL;                }            } else {                *params[i] = PCSL_STRING_NULL;            }        }    }    if (pRuntimeInfo) {        /* Initialize profile's name */        if (pRuntimeInfo->profileNameLen && pRuntimeInfo->profileName != NULL) {            if (PCSL_STRING_OK == pcsl_string_convert_from_utf16(                                      pRuntimeInfo->profileName,                                      pRuntimeInfo->profileNameLen,                                      &temp)) {                if (pcsl_string_utf16_length(&temp) > 0) {                    evt.stringParam6 = temp;                } else {                    pcsl_string_free(&temp);                }            } else {                evt.stringParam6 = PCSL_STRING_NULL;            }        }        evt.intParam3 = pRuntimeInfo->memoryReserved;        evt.intParam4 = pRuntimeInfo->memoryTotal;        evt.intParam5 = pRuntimeInfo->priority;    } else {        evt.stringParam6 = PCSL_STRING_NULL;        evt.intParam3 = evt.intParam4 = evt.intParam5 = -1;    }    evt.type = NATIVE_MIDLET_EXECUTE_REQUEST;    evt.intParam1 = appId;    evt.intParam2 = suiteId;    midpStoreEventAndSignalAms(evt);    return ALL_OK;}/** * Create and start the specified MIDlet. The suiteId is passed to the * midletsuitestorage API to retrieve the class path. * * @param suiteId             The application suite ID * @param className           Fully qualified name of the MIDlet class * @param classNameLen        Length of the MIDlet class name * @param appId               The application id used to identify the app * @param pRuntimeInfo Quotas and profile to set for the new application * * @return error code: ALL_OK if the operation was started successfully, *                     BAD_PARAMS if suiteId is invalid or pClassName is null */MIDPError midp_midlet_create_start(SuiteIdType suiteId,                                   const jchar *className, jint classNameLen,                                   jint appId,                                   const MidletRuntimeInfo* pRuntimeInfo) {    return midp_midlet_create_start_with_args(suiteId,        className, classNameLen, NULL, NULL, 0, appId, pRuntimeInfo);}/** * Resume the specified paused MIDlet. * * @param appId The application id used to identify the app * * @return error code: ALL_OK if the operation was started successfully */MIDPError midp_midlet_resume(jint appId) {    MidpEvent evt;    MIDP_EVENT_INITIALIZE(evt);    evt.type = NATIVE_MIDLET_RESUME_REQUEST;    evt.intParam1 = appId;    midpStoreEventAndSignalAms(evt);    return ALL_OK;}/** * Pause the specified MIDlet. * * @param appId The application id used to identify the app * * @return error code: ALL_OK if the operation was started successfully */MIDPError midp_midlet_pause(jint appId) {    MidpEvent evt;    MIDP_EVENT_INITIALIZE(evt);    evt.type = NATIVE_MIDLET_PAUSE_REQUEST;    evt.intParam1 = appId;    midpStoreEventAndSignalAms(evt);    return ALL_OK;}/** * Stop the specified MIDlet. * * If the midlet is not terminated within the given number of milliseconds, * it will be forcefully terminated. * * @param appId The application id used to identify the app * @param timeout Timeout in milliseconds * * @return error code: ALL_OK if the operation was started successfully */MIDPError midp_midlet_destroy(jint appId, jint timeout) {    MidpEvent evt;    MIDP_EVENT_INITIALIZE(evt);    evt.type = NATIVE_MIDLET_DESTROY_REQUEST;    evt.intParam1 = appId;    evt.intParam2 = timeout;    midpStoreEventAndSignalAms(evt);    return ALL_OK;}/** * Gets ID of the suite containing the specified running MIDlet.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?