cdc_natives.c

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

C
751
字号
/* * * * Copyright  1990-2007 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 <stdio.h>#include <sys/time.h>#include <jvmconfig.h>#include <kni.h>#include <midp_logging.h>#include <midpMalloc.h>#include <midpAMS.h>#include <midpInit.h>#include <midp_mastermode_port.h>#include <midp_foreground_id.h>#include <keymap_input.h>#include <lcdlf_export.h>#include <fbapp_export.h>#ifdef DIRECTFB#include <directfbapp_export.h>#endif/* IMPL_NOTE - CDC declarations */CVMInt64 CVMtimeMillis(void);static int controlPipe[2]; /* [0] for read, [1] for write */static void initCDCEvents();static MidpReentryData newSignal;static MidpEvent newMidpEvent;/* in mastermode_handle_signal.c */void handleKey(MidpReentryData* pNewSignal, MidpEvent* pNewMidpEvent);KNIEXPORT KNI_RETURNTYPE_LONGJVM_JavaMilliSeconds() {    return CVMtimeMillis();}/** * Get the current Isolate ID. * * @return ID of the current Isolate */KNIEXPORT KNI_RETURNTYPE_INTKNIDECL(com_sun_midp_main_MIDletSuiteLoader_getIsolateId) {    (void)_p_mb;    KNI_ReturnInt(0);}/** * Get the Isolate ID of the AMS Isolate. * * @return Isolate ID of AMS Isolate */KNIEXPORT KNI_RETURNTYPE_INTKNIDECL(com_sun_midp_main_MIDletSuiteLoader_getAmsIsolateId) {    (void) _p_mb;    KNI_ReturnInt(0);}/** * Register the Isolate ID of the AMS Isolate by making a native * method call that will call JVM_CurrentIsolateId and set * it in the proper native variable. */KNIEXPORT KNI_RETURNTYPE_VOIDKNIDECL(com_sun_midp_main_MIDletSuiteLoader_registerAmsIsolateId) {    (void) _arguments;    (void) _p_mb;    KNI_ReturnVoid();}/** * Send hint to VM about the begin of MIDlet startup phase * to allow the VM to fine tune its internal parameters to * achieve optimal peformance * * @param midletIsolateId ID of the started MIDlet isolate */KNIEXPORT KNI_RETURNTYPE_VOIDKNIDECL(com_sun_midp_main_MIDletSuiteLoader_vmBeginStartUp) {    (void) _arguments;    (void) _p_mb;    KNI_ReturnVoid();}/** * Send hint to VM about the end of MIDlet startup phase * to allow the VM to restore its internal parameters * changed on startup time for better performance * * @param midletIsolateId ID of the started MIDlet isolate */KNIEXPORT KNI_RETURNTYPE_VOIDKNIDECL(com_sun_midp_main_MIDletSuiteLoader_vmEndStartUp) {    (void) _arguments;    (void) _p_mb;    KNI_ReturnVoid();}/** * Initializes the UI. * * @return <tt>0</tt> upon successful initialization, otherwise *         <tt>-1</tt> */static intmidpInitializeUI(void) {    /*    if (InitializeEvents() != 0) {        return -1;    }    */    /*     * Porting consideration:     * Here is a good place to put I18N init.     * function. e.g. initLocaleMethod();     */#if ENABLE_JAVA_DEBUGGER    {        char* argv[2];        /* Get the VM debugger port property. */        argv[1] = (char *)getInternalProperty("VmDebuggerPort");        if (argv[1] != NULL) {            argv[0] = "-port";            (void)JVM_ParseOneArg(2, argv);        }    }#endif    /*         IMPL_NOTE if (pushopen() != 0) {            return -1;        }    */    lcdlf_ui_init();    return 0;}/** * Finalizes the UI. */static voidmidpFinalizeUI(void) {    lcdlf_ui_finalize();    /*       IMPL_NOTE: pushclose();       FinalizeEvents();       Porting consideration:       Here is a good place to put I18N finalization       function. e.g. finalizeLocaleMethod();    */    /*     * Note: the AMS isolate will have been registered by a native method     * call, so there is no corresponding midpRegisterAmsIsolateId in the     * midpInitializeUI() function.     */    /* midpUnregisterAmsIsolateId(); */}KNIEXPORT KNI_RETURNTYPE_VOIDKNIDECL(com_sun_midp_main_CDCInit_initMidpNativeStates) {    jchar jbuff[1024];    static char conf_buff[1024], store_buff[1024];    int max = sizeof(conf_buff) - 1;    int len, i;    KNI_StartHandles(2);        KNI_DeclareHandle(config);    KNI_DeclareHandle(storage);        KNI_GetParameterAsObject(1, config);    KNI_GetParameterAsObject(2, storage);    initCDCEvents();    len = KNI_GetStringLength(config);    if (len > max) {        len = max;    }    if (len >= 0) {        /* config != null */        KNI_GetStringRegion(config, 0, len, jbuff);        for (i = 0; i<len; i++) {            conf_buff[i] = (char)jbuff[i];        }        conf_buff[len] = 0;        midpSetConfigDir(conf_buff);    }    len = KNI_GetStringLength(storage);    if (len > max) {        len = max;    }    if (len >= 0) {        /* sotarage != null */        KNI_GetStringRegion(storage, 0, len, jbuff);        for (i = 0; i<len; i++) {            store_buff[i] = (char)jbuff[i];        }        store_buff[len] = 0;        midpSetAppDir(store_buff);    }    if (midpInitialize() != 0) {        printf("midpInitialize() failed\n");    }    if (midpInitCallback(VM_LEVEL, midpInitializeUI, midpFinalizeUI) != 0) {        printf("midpInitCallback(VM_LEVEL, ...) failed\n");    }    KNI_EndHandles();    KNI_ReturnVoid();}/* * Event handling */static void initCDCEvents() {    if (pipe(controlPipe) != 0) {        perror("pipe(controlPipe) failed");    }}/* * Looking up field IDs takes some time, but does not change during a VM * session so the field IDs can be cached to save time. These IDs are only * access by native methods running in the VM thread. */static int eventFieldIDsObtained = 0;static jfieldID typeFieldID;static jfieldID intParam1FieldID;static jfieldID intParam2FieldID;static jfieldID intParam3FieldID;static jfieldID intParam4FieldID;static jfieldID stringParam1FieldID;static jfieldID stringParam2FieldID;static jfieldID stringParam3FieldID;static jfieldID stringParam4FieldID;static jfieldID stringParam5FieldID;static jfieldID stringParam6FieldID;/** * Gets the field ids of a Java event object and cache them * in local static storage. * * @param eventObj handle to an NativeEvent Java object * @param classObj handle to the NativeEvent class */static voidcacheEventFieldIDs(KNIDECLARGS jobject eventObj, jclass classObj) {    (void) _arguments;    (void) _p_mb;    if (eventFieldIDsObtained) {        return;    }    KNI_GetObjectClass(eventObj, classObj);    typeFieldID = KNI_GetFieldID(classObj, "type", "I");    intParam1FieldID = KNI_GetFieldID(classObj, "intParam1", "I");    intParam2FieldID = KNI_GetFieldID(classObj, "intParam2", "I");    intParam3FieldID = KNI_GetFieldID(classObj, "intParam3", "I");    intParam4FieldID = KNI_GetFieldID(classObj, "intParam4", "I");    stringParam1FieldID = KNI_GetFieldID(classObj, "stringParam1",                                         "Ljava/lang/String;");    stringParam2FieldID = KNI_GetFieldID(classObj, "stringParam2",                                         "Ljava/lang/String;");    stringParam3FieldID = KNI_GetFieldID(classObj, "stringParam3",                                         "Ljava/lang/String;");    stringParam4FieldID = KNI_GetFieldID(classObj, "stringParam4",                                         "Ljava/lang/String;");    stringParam5FieldID = KNI_GetFieldID(classObj, "stringParam5",                                         "Ljava/lang/String;");    stringParam6FieldID = KNI_GetFieldID(classObj, "stringParam6",                                         "Ljava/lang/String;");    eventFieldIDsObtained = 1;}static void readControlIntField(jobject objectHandle, jfieldID fieldID) {    int n;    read(controlPipe[0], &n, sizeof(int));    KNI_SetIntField(objectHandle, fieldID, n);}static void sendControlIntField(jobject objectHandle, jfieldID fieldID) {    int n = KNI_GetIntField(objectHandle, fieldID);    write(controlPipe[1], &n, sizeof(int));#ifdef CVM_DEBUG    printf("%d ", n);#endif}static void readControlStringField(KNIDECLARGS jobject objectHandle,                                   jfieldID fieldID) {    int len, i;    jchar *data;    (void) _arguments;    (void) _p_mb;    read(controlPipe[0], &len, sizeof(int));    data = (jchar*)midpMalloc(len * sizeof(jchar));    if (data != NULL) {        read(controlPipe[0], data, len * sizeof(jchar));    } else {        for (i=0; i<len; i++) {            jchar dummy;            read(controlPipe[0], &dummy, sizeof(jchar));        }        len = 0; /* IMPL_NOTE: throw out of memory */    }    KNI_StartHandles(1);    KNI_DeclareHandle(stringObj);    KNI_NewString(data, len, stringObj);    KNI_SetObjectField(objectHandle, fieldID, stringObj);    KNI_EndHandles();}static void sendControlStringField(KNIDECLARGS jobject objectHandle,                                   jobject stringObj, jfieldID fieldID) {    int len;    jchar *data = NULL;    (void) _arguments;    (void) _p_mb;    (void) _ee;    KNI_GetObjectField(objectHandle, fieldID, stringObj);    len = KNI_GetStringLength(stringObj);    if (len > 0) {        data = (jchar*)midpMalloc(len * sizeof(jchar));        if (data == NULL) {

⌨️ 快捷键说明

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