midpevents.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 718 行 · 第 1/2 页
C
718 行
/* * * * 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 <string.h>#include <jvmconfig.h>#include <kni.h>#include <jvm.h>#include <jvmspi.h>#include <sni.h>#include <commonKNIMacros.h>#include <ROMStructs.h>#include <midpMalloc.h>#include <midpMidletSuiteUtils.h>#include <midpServices.h>#include <midpEvents.h>#include <midpError.h>#include <midp_constants_data.h>#include <midp_logging.h>#include <midp_thread.h>#include <midpport_eventqueue.h>#include <pcsl_string.h>#include <midpUtilKni.h>#include <midp_properties_port.h>typedef struct Java_com_sun_midp_events_EventQueue _eventQueue;#define getEventQueuePtr(handle) (unhand(_eventQueue,(handle)))#define SET_STRING_EVENT_FIELD(VALUE, STRING, OBJ, ID) \ if (pcsl_string_utf16_length(&VALUE) >= 0) { \ midp_jstring_from_pcsl_string(&VALUE, STRING); \ KNI_SetObjectField(OBJ, ID, STRING); \ }#define GET_STRING_EVENT_FIELD(OBJ, ID, STRING, RESULT) \ KNI_GetObjectField(OBJ, ID, STRING); \ { \ pcsl_string __temp; \ if (PCSL_STRING_OK != midp_jstring_to_pcsl_string(STRING, &__temp)) { \ KNI_ThrowNew(midpOutOfMemoryError, "send native event sp"); \ break; \ } \ RESULT = __temp; \ }/** * @file * Native methods to support the queuing of native methods to be processed * by a Java event thread. Each Isolate has its own queue, in SVM mode * the code will work as if there is only one Isolate. */static int maxIsolates = 1; /* will be reset in MVM mode in initialize *//* * NOTE: MAX_EVENTS is defined in the constants.xml * in the configuration module */typedef struct _EventQueue { /** Queue of pending events */ MidpEvent events[MAX_EVENTS]; /** Number of events in the queue */ int numEvents; /** The queue position of the next event to be processed */ int eventIn; /** The queue position of the next event to be stored */ int eventOut; /** Thread state for each Java native event monitor. */ jboolean isMonitorBlocked;} EventQueue;/** Queues of pending events, one per Isolate or 1 for SVM mode */static EventQueue* pEventQueues = NULL;/* * 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 intParam5FieldID;static jfieldID stringParam1FieldID;static jfieldID stringParam2FieldID;static jfieldID stringParam3FieldID;static jfieldID stringParam4FieldID;static jfieldID stringParam5FieldID;static jfieldID stringParam6FieldID;/** * Gets the event queue associated with an Isolate. * * @param isolateId ID of an Isolate or 0 for SVM mode * * @return an event queue */static EventQueue* getIsolateEventQueue(int isolateId) { /* * Note: Using Isolate IDs as an event queue array index is only done * here for performance reasons and should NOT * be used in other parts of the system. In other parts of * the system something like a matching search should be used. * * In MVM the first isolate has number 1, and we've allocated one * more entry in pEventQueues[], thus the condition bellow is * isolateId > maxIsolates rather than >= . */#if ENABLE_MULTIPLE_ISOLATES if (isolateId < 0 || isolateId > maxIsolates) {#else if (isolateId != 0) {#endif REPORT_CRIT1(LC_CORE, "Assertion failed: Isolate ID (%d) out of bounds", isolateId); /* avoid a SEGV;*/ isolateId = 0; } return &(pEventQueues[isolateId]);}/** * 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(jobject eventObj, jclass classObj) { 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"); intParam5FieldID = KNI_GetFieldID(classObj, "intParam5", "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;}/** * De-allocates the fields of an event. * * @param event The event to be freed */static voidfreeMIDPEventFields(MidpEvent event) { pcsl_string_free(&event.stringParam1); pcsl_string_free(&event.stringParam2); pcsl_string_free(&event.stringParam3); pcsl_string_free(&event.stringParam4); pcsl_string_free(&event.stringParam5); pcsl_string_free(&event.stringParam6);} /** * Gets the next pending event for an isolate. * <p> * <b>NOTE:</b> Any string parameter data must be de-allocated with * <tt>midpFree</tt>. * * @param pResult where to put the pending event * @param isolateId ID of an Isolate or 0 for SVM mode * * @return -1 for no event pending, number of event still pending after this * event */static intgetPendingMIDPEvent(MidpEvent* pResult, int isolateId) { EventQueue* pEventQueue = getIsolateEventQueue(isolateId); if (pEventQueue->numEvents == 0) { return -1; } *pResult = pEventQueue->events[pEventQueue->eventOut]; /* Empty out the events so we do not free it when finalizing. */ MIDP_EVENT_INITIALIZE(pEventQueue->events[pEventQueue->eventOut]); pEventQueue->numEvents--; pEventQueue->eventOut++; if (pEventQueue->eventOut == MAX_EVENTS) { /* This is a circular queue start back a zero. */ pEventQueue->eventOut = 0; } return pEventQueue->numEvents;}/** * Reset an event queue. * * @param handle handle of the event queue. */static void resetEventQueue(int handle) { MidpEvent event; if (NULL == pEventQueues) { return; } pEventQueues[handle].isMonitorBlocked = KNI_FALSE; while (getPendingMIDPEvent(&event, handle) != -1) { freeMIDPEventFields(event); }}/** * Initializes the event system. * <p> * <b>NOTE:</b> The event system must be explicitly initialize so the * VM can shutdown and restart cleanly. * * @return 0 for success, or non-zero if the MIDP implementation is * out of memory */intInitializeEvents(void) { int sizeInBytes; if (NULL != pEventQueues) { /* already done */ return 0; }#if ENABLE_MULTIPLE_ISOLATES maxIsolates = getInternalPropertyInt("MAX_ISOLATES"); if (0 == maxIsolates) { char maxIsolatesStr[5]; REPORT_INFO(LC_AMS, "MAX_ISOLATES property not set"); /* set XML constant value as property value */ maxIsolates = MAX_ISOLATES; sprintf(maxIsolatesStr, "%d", maxIsolates); setInternalProperty("MAX_ISOLATES", maxIsolatesStr); } /* * In MVM the first isolate has number 1, but 0 still can be returned * by midpGetAmsIsolate() if JVM is not running. So in MVM we allocate * one more entry in pEventQueues[] to make indeces from 0 to maxIsolates * inclusively valid. */ sizeInBytes = (maxIsolates + 1) * sizeof (EventQueue);#else sizeInBytes = maxIsolates * sizeof (EventQueue);#endif pEventQueues = midpMalloc(sizeInBytes); if (NULL == pEventQueues) { return -1; } memset(pEventQueues, 0, sizeInBytes); midp_createEventQueueLock(); return 0;}/** * Finalizes the event system. */voidFinalizeEvents(void) { midp_destroyEventQueueLock(); if (pEventQueues != NULL) { midp_resetEvents(); midpFree(pEventQueues); pEventQueues = NULL; }}/** * Resets the all internal event queues, clearing and freeing * any pending events. */voidmidp_resetEvents(void) { int i; /* The Event ID may have changed for each VM startup*/ eventFieldIDsObtained = KNI_FALSE; for (i = 0; i < maxIsolates; i++) { resetEventQueue(i); }}/** * Helper function used by StoreMIDPEventInVmThread * Enqueues an event to be processed by the * Java event thread for a given Isolate */static void StoreMIDPEventInVmThreadImp(MidpEvent event, int isolateId) { EventQueue* pEventQueue; JVMSPI_ThreadID thread; pEventQueue = getIsolateEventQueue(isolateId);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?