runnams.c

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

C
812
字号
/* * * * 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 <string.h>#include <stdio.h>#include <ctype.h>#include <jvm.h>#include <kni.h>#include <findMidlet.h>#include <midpAMS.h>#include <midp_run_vm.h>#include <midpInit.h>#include <midpMalloc.h>#include <midpStorage.h>#include <midpServices.h>#include <midpNativeThread.h>#include <midpNativeAppManager.h>#include <midpUtilKni.h>#include <suitestore_task_manager.h>#include <commandLineUtil_md.h>/** * @file * * Example of how the public MIDP API can be used to run an installed * MIDlet Suite. */#define MIDLET_DESTROY_DEFAULT_TIMEOUT 5000#define NAMS_TEST_SERVICE_CLASS_NAME "com.sun.midp.main.NamsTestServiceMidlet"#if ENABLE_I3_TESTextern void initNams(void);extern int findNextEmptyMIDlet(int appId);extern midp_ThreadRoutine midlet_starter_routine;static void initNamsCommands(int argn, char* args[]);#endif/** Usage text for the runNams executable. */static const char* const runUsageText ="\n""Usage: runNams [<VM args>] (-namsTestService |\n""           -runMainClass <mainClass> [<args>] |\n""           (-runMidlet | -jamsTestMode (<suite number> | <suite ID>)\n""              [<classname of MIDlet to run> [<arg0> [<arg1> [<arg2>]]]]))\n""\n""Options are:\n""    -namsTestService - runs NAMS Test Service;\n""    -runMainClass - runs an alternate main class;\n""    -runMidlet    - runs a MIDlet using NAMS API;\n""    -jamsTestMode - runs a MIDlet in the AMS isolate using the JAMS mode API\n""\n""  where <suite number> is the number of a suite as displayed by the\n""  listMidlets command, and <suite ID> is the unique ID a suite is \n""  referenced by\n\n";static jchar discoverClassName[] =    {'c', 'o', 'm', '.', 's', 'u', 'n', '.', 'm', 'i', 'd', 'p',    '.', 'i', 'n', 's', 't', 'a', 'l', 'l', 'e', 'r', '.',    'D', 'i', 's', 'c', 'o', 'v', 'e', 'r', 'y', 'A', 'p', 'p'};static jchar selectorClassName[] =    {'c', 'o', 'm', '.', 's', 'u', 'n', '.', 'm', 'i', 'd', 'p',    '.', 'a', 'p', 'p', 'm', 'a', 'n', 'a', 'g', 'e', 'r',    '.', 'M', 'I', 'D', 'l', 'e', 't', 'S', 'e', 'l', 'e', 'c', 't', 'o', 'r'};#if ENABLE_I3_TESTstatic jchar namsManagerClassName[] =    {'c', 'o', 'm', '.', 's', 'u', 'n', '.', 'm', 'i', 'd', 'p',    '.', 'm', 'a', 'i', 'n',    '.', 'N', 'a', 'm', 's', 'M', 'a', 'n', 'a', 'g', 'e', 'r'};static jchar i3frameworkClassName[] =    {'c', 'o', 'm', '.', 's', 'u', 'n', '.', 'm', 'i', 'd', 'p',    '.', 'i', '3', 't', 'e', 's', 't',    '.', 'N', 'a', 'm', 's', 'F', 'r', 'a', 'm', 'e', 'w', 'o', 'r', 'k'};#endifstatic pcsl_string argsForMidlet[3] = {    PCSL_STRING_NULL_INITIALIZER,    PCSL_STRING_NULL_INITIALIZER,    PCSL_STRING_NULL_INITIALIZER};static SuiteIdType suiteIDToRun = UNUSED_SUITE_ID;static pcsl_string classNameToRun = PCSL_STRING_NULL_INITIALIZER;static pcsl_string* const aclassNameToRun = &classNameToRun;static SuiteIdType* pSuiteIds = NULL;static int numberOfSuiteIds = 0;static jint *pSuiteRunState = NULL;static jint foregroundAppId = 0;static SuiteIdType getSuiteId(int index) {    if (index >= 0 && index < numberOfSuiteIds) {        return pSuiteIds[index];    }    return UNUSED_SUITE_ID;}static MIDPError loadSuiteIds() {    int i;    MIDPError status;    /*     * This is a public API which can be called without the VM running     * so we need automatically init anything needed, to make the     * caller's code less complex.     *     * Initialization is performed in steps so that we do use any     * extra resources such as the VM for the operation being performed.     */    if (midpInit(LIST_LEVEL) != 0) {        return OUT_OF_MEMORY;    }    status = midp_get_suite_ids(&pSuiteIds, &numberOfSuiteIds);    if (status != ALL_OK) {        REPORT_ERROR(LC_AMS, "Can't load suite IDs.");        fprintf(stderr, "Can't load suite IDs: error %d.\n", status);        return status;    }    pSuiteRunState = (jint*)midpMalloc(numberOfSuiteIds*sizeof(jint));    if (pSuiteRunState == NULL) {        REPORT_ERROR(LC_AMS, "Out of Memory");        fprintf(stderr, "Out Of Memory\n");        return OUT_OF_MEMORY;    }    for (i = 0; i < numberOfSuiteIds; i++) {        pSuiteRunState[i] = MIDP_MIDLET_STATE_DESTROYED;    }    return ALL_OK;}static void unloadSuiteIds() {    if (pSuiteIds != NULL) {        midp_free_suite_ids(pSuiteIds, numberOfSuiteIds);        pSuiteIds = NULL;        numberOfSuiteIds = 0;    }    if (pSuiteRunState != NULL) {        midpFree(pSuiteRunState);        pSuiteRunState = NULL;    }}void nams_process_command(int command, int param) {    jint classNameLen = -1;    jchar* pClassName = NULL;    printf("* Received nams command (%d, %d)\n", command, param);    switch (command) {    case -1:        midp_system_stop();        break;    case 1: {        /* Run by number */        SuiteIdType suiteId = getSuiteId(param - 1);	if (suiteId == UNUSED_SUITE_ID) {            printf("invalid suite index [%d]\n", param);            break;        }        midp_midlet_create_start(/* midlet suite id */                                 suiteId,                                 /* midlet class name */                                 NULL_MIDP_STRING.data,                                 NULL_MIDP_STRING.len,                                 param,                                 NULL                                );        break;    }    case 2:        midp_midlet_pause(param);        break;    case 3:        midp_midlet_resume(param);        break;    case 4:        midp_midlet_destroy(param, MIDLET_DESTROY_DEFAULT_TIMEOUT);        break;    case 5:        midp_midlet_set_foreground(param);        break;    case 6: {        switch (param) {#if ENABLE_I3_TEST        case 0:            pClassName = namsManagerClassName;            classNameLen = sizeof (namsManagerClassName) / sizeof (jchar);            break;        case 1:            pClassName = i3frameworkClassName;            classNameLen = sizeof (i3frameworkClassName) / sizeof (jchar);            break;#endif        case 2:            pClassName = discoverClassName;            classNameLen = sizeof (discoverClassName) / sizeof (jchar);            break;        default:            pClassName = selectorClassName;            classNameLen = sizeof (selectorClassName) / sizeof (jchar);            break;        }        midp_midlet_create_start(INTERNAL_SUITE_ID,                                 pClassName, classNameLen,#if ENABLE_I3_TEST                                 ((param != 0)                                     ? findNextEmptyMIDlet(0) : 0)#else                                 -param#endif                                 , NULL                                );        break;    }    default:        printf("* Received WM_TEST(%d, %d)\n", command, param);        break;    }}/** * The function that will be called when Java system state * changes. * * @param pEventData */void system_state_listener(const NamsEventData* pEventData) {    printf("--- system_state_listener(event = %d, state = %d)\n",        pEventData->event, pEventData->state);    if (pEventData->event == MIDP_NAMS_EVENT_STATE_CHANGED &&            pEventData->state == MIDP_SYSTEM_STATE_ACTIVE) {        int i;        const jchar *jchArgsForMidlet[3];        jint  argsLen[3];        /* Currently we support up to 3 arguments. */        for (i = 0; i < 3; i++) {            jchArgsForMidlet[i] = pcsl_string_get_utf16_data(&argsForMidlet[i]);            argsLen[i] = pcsl_string_utf16_length(&argsForMidlet[i]);        }        GET_PCSL_STRING_DATA_AND_LENGTH(aclassNameToRun)        (void) midp_midlet_create_start_with_args(suiteIDToRun,                                           (const jchar*)aclassNameToRun_data,                                           aclassNameToRun_len,                                           (const jchar**)jchArgsForMidlet,                                           argsLen,                                           3,#if ENABLE_I3_TEST                                           findNextEmptyMIDlet(0),#else                                           /*                                            * There is only one application                                            * excepting namsTestService mode                                            */                                           1,#endif                                           NULL);        RELEASE_PCSL_STRING_DATA_AND_LENGTH        for (i = 0; i < 3; i++) {            pcsl_string_release_utf16_data(jchArgsForMidlet[i],                                           &argsForMidlet[i]);        }    }}/** * The typedef of the background listener that is notified * when the background system changes. * * @param pEventData */void background_listener(const NamsEventData* pEventData) {    int i = 0;    if (pEventData == NULL) {        printf("--- background_listener(): NULL event data!\n");        return;    }    if (pEventData->state != MIDP_DISPLAY_STATE_BACKGROUND &&        pEventData->state != MIDP_DISPLAY_STATE_BACKGROUND_REQUEST) {        /* probably foreground request - don't handle */        return;    }    printf("--- background_listener(appId = %d, reason = %d)\n",           pEventData->appId, pEventData->reason);    for (i = 0; i < numberOfSuiteIds; i++) {        if (pSuiteRunState[i] == MIDP_MIDLET_STATE_ACTIVE &&            i+1 != foregroundAppId) {            printf("midp_midlet_set_foreground(suiteId = %d)\n", i+1);            midp_midlet_set_foreground(i+1);            break;        }    }}/** * The typedef of the foreground listener that is notified * when the foreground midlet changes. * * @param pEventData */void foreground_listener(const NamsEventData* pEventData) {    if (pEventData == NULL) {        printf("--- foreground_listener(): NULL event data!\n");        return;    }    if (pEventData->state != MIDP_DISPLAY_STATE_FOREGROUND &&        pEventData->state != MIDP_DISPLAY_STATE_FOREGROUND_REQUEST) {        /* probably background request - don't handle */        return;    }    printf("--- foreground_listener(appId = %d, reason = %d)\n",           pEventData->appId, pEventData->reason);    foregroundAppId = pEventData->appId;    if (pEventData->appId > 0 && pEventData->appId <= numberOfSuiteIds) {        printf("[%d] ", pEventData->appId);        printf("Suite ID = %ld", (long)pSuiteIds[pEventData->appId-1]);        printf(" has the foreground   reason = %d\n", pEventData->reason);    }}/** * The typedef of the midlet state listener that is notified * with the midlet state changes. * * @param pEventData */void state_change_listener(const NamsEventData* pEventData) {    if (!pEventData || !pEventData->pSuiteData) {        printf("--- state_change_listener(): invalid pEventData!\n");        return;    }    printf("--- state_change_listener(appId = %d, state = %d, reason = %d)\n",           pEventData->appId, pEventData->state, pEventData->reason);    if (pEventData->event != MIDP_NAMS_EVENT_STATE_CHANGED) {        printf("Dropping event: %d\n", pEventData->event);        return;    }    if (pEventData->appId > 0) {        SuiteIdType suiteId = pEventData->pSuiteData->suiteId;        printf("[%d] ", pEventData->appId);        printf("Suite ID = %ld", (long)suiteId);        if (pEventData->appId <= numberOfSuiteIds) {            pSuiteRunState[pEventData->appId-1] = pEventData->state;        }

⌨️ 快捷键说明

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