📄 events.c
字号:
/*0001*//*
/*0002./ * Copyright (c) 1998-2001 Sun Microsystems, Inc., 901 San Antonio Road,
/*0003./ * Palo Alto, CA 94303, U.S.A. All Rights Reserved.
/*0004./ *
/*0005./ * Sun Microsystems, Inc. has intellectual property rights relating
/*0006./ * to the technology embodied in this software. In particular, and
/*0007./ * without limitation, these intellectual property rights may include
/*0008./ * one or more U.S. patents, foreign patents, or pending
/*0009./ * applications. Sun, Sun Microsystems, the Sun logo, Java, KJava,
/*0010./ * and all Sun-based and Java-based marks are trademarks or
/*0011./ * registered trademarks of Sun Microsystems, Inc. in the United
/*0012./ * States and other countries.
/*0013./ *
/*0014./ * This software is distributed under licenses restricting its use,
/*0015./ * copying, distribution, and decompilation. No part of this
/*0016./ * software may be reproduced in any form by any means without prior
/*0017./ * written authorization of Sun and its licensors, if any.
/*0018./ *
/*0019./ * FEDERAL ACQUISITIONS: Commercial Software -- Government Users
/*0020./ * Subject to Standard License Terms and Conditions
/*0021./ */
/*0022*/
/*0023*//*=========================================================================
/*0024./ * SYSTEM: KVM
/*0025./ * SUBSYSTEM: Event handling support
/*0026./ * FILE: events.c
/*0027./ * OVERVIEW: This file defines the operations for binding the
/*0028./ * interpreter to the event handling mechanisms of
/*0029./ * the host operating system.
/*0030./ * AUTHOR: Nik Shaylor 4/20/00
/*0031./ * Bill Pittore (debugging support)
/*0032./ * Frank Yellin
/*0033./ *=======================================================================*/
/*0034*/
/*0035*//*
/*0036./ * This file implements the "events:" protocol
/*0037./ *
/*0038./ * @author Nik Shaylor
/*0039./ * @version 1.0 1/15/2000
/*0040./ */
/*0041*/
/*0042*//*=========================================================================
/*0043./ * Include files
/*0044./ *=======================================================================*/
/*0045*/
/*0046*/#include <global.h>
/*0050*/
/*0051*/#ifndef PILOT
/*0052*/#include <stdarg.h>
/*0053*/#endif
/*0054*/
/*0055*//*=========================================================================
/*0056./ * Definitions and declarations
/*0057./ *=======================================================================*/
/*0058*/
/*0059*/#define MAXPARMLENGTH 20
/*0060*/
/*0061*//*=========================================================================
/*0062./ * Local variables
/*0063./ *=======================================================================*/
/*0064*/
/*0065*/static THREAD waitingThread;
/*0066*/static int opened;
/*0067*/
/*0068*/static cell eventBuffer[MAXPARMLENGTH];
/*0069*/static int eventInP;
/*0070*/ int eventCount;
/*0071*/
/*0072*//*=========================================================================
/*0073./ * Event handling functions
/*0074./ *=======================================================================*/
/*0075*/
/*0076*//*=========================================================================
/*0077./ * FUNCTION: InitializeEvents
/*0078./ * TYPE: global function
/*0079./ * OVERVIEW: Initialize the event system.
/*0080./ * We have to explicitly initialize the event system
/*0081./ * so that the KVM can shutdown and restart cleanly.
/*0082./ * INTERFACE:
/*0083./ * parameters: none
/*0084./ * returns: none
/*0085./ *=======================================================================*/
/*0086*/
/*0087*/void InitializeEvents()
/*0088*/{
/*0089*/ waitingThread = 0;
/*0090*/ makeGlobalRoot((cell **)&waitingThread);
/*0091*/ opened = FALSE;
/*0092*/
/*0093*/ eventInP = 0;
/*0094*/ eventCount = 0;
/*0095*/
/*0101*/}
/*0102*/
/*0103*//*=========================================================================
/*0104./ * FUNCTION: StoreKVMEvent
/*0105./ * TYPE: global function
/*0106./ * OVERVIEW: Callback to indicate an event has occurred
/*0107./
/*0108./ * INTERFACE:
/*0109./ * parameters: type: event type
/*0110./ * argCount: number of parameters taken by event
/*0111./ * .....: argCount parameters
/*0112./ * returns: none
/*0113./ *=======================================================================*/
/*0114*/
/*0115*/void
/*0116*/StoreKVMEvent(cell type, int argCount, ...)
/*0117*/{
/*0118*/ int inP;
/*0119*/ int i;
/*0120*/ va_list args;
/*0121*/ if (eventCount == 0) {
/*0122*/ /* Reset the in pointer to 0 if we can */
/*0123*/ eventInP = 0;
/*0124*/ } else if (eventInP > (MAXPARMLENGTH - 1 - argCount)) {
/*0125*/ /* Ignore the event, for now, since we don't have any space to
/*0126./ * store it.
/*0127./ */
/*0128*/ return;
/*0129*/ }
/*0130*/
/*0137*/ if (type == appStopKVMEvent) {
/*0138*/ /* Indicate the end */
/*0139*/ ERROR_THROW(0);
/*0140*/ }
/*0141*/
/*0142*/ inP = eventInP; /* cache this locally */
/*0143*/ eventBuffer[inP++] = type;
/*0144*/
/*0145*/ va_start(args, argCount);
/*0146*/ for (i = 0; i < argCount; i++) {
/*0147*/ eventBuffer[inP++] = va_arg(args, cell);
/*0148*/ }
/*0149*/ va_end(args);
/*0150*/
/*0151*/ /* Update the count and the pointer to where to put the next argument */
/*0152*/ eventCount += (inP - eventInP);
/*0153*/ eventInP = inP;
/*0154*/}
/*0155*/
/*0156*//*=========================================================================
/*0157./ * FUNCTION: getKVMEvent
/*0158./ * TYPE: Local function
/*0159./ * OVERVIEW: Find next event or parameter on the input queue.
/*0160./ * INTERFACE:
/*0161./ * parameters: forever: if TRUE, wait forever, if FALSE wait
/*0162./ * until waitUntil
/*0163./ * waitUntil: wait at most until this time (if !forever).
/*0164./ * result: location to put the next event
/*0165./ * returns: boolean indicating whether an event or parameter
/*0166./ * is available
/*0167./ *=======================================================================*/
/*0168*/
/*0169*/static bool_t
/*0170*/getKVMEvent(bool_t forever, ulong64 waitUntil, cell *result) {
/*0171*/ if (eventCount == 0) {
/*0172*/ /* Try and get the machine's system to give us an event */
/*0173*/ GetAndStoreNextKVMEvent(forever, waitUntil);
/*0174*/ }
/*0175*/ if (eventCount > 0) {
/*0176*/ /* We have an event */
/*0177*/ *result = eventBuffer[eventInP - eventCount];
/*0178*/ eventCount--;
/*0179*/ return TRUE;
/*0180*/ } else {
/*0181*/ /* No event available */
/*0182*/ return FALSE;
/*0183*/ }
/*0184*/}
/*0185*/
/*0186*/ /*=========================================================================
/*0187./ * FUNCTION: InterpreterHandleEvent
/*0188./ * TYPE: Global C function
/*0189./ * OVERVIEW: Wait for a system event
/*0190./ * INTERFACE:
/*0191./ * parameters: time to wait if no events are available immediately;
/*0192./ * zero value indicates that the host platform can go
/*0193./ * to sleep and/or conserve battery until a new native
/*0194./ * event occurs
/*0195./ * returns: nothing
/*0196./ *=======================================================================*/
/*0197*/
/*0198*/void InterpreterHandleEvent(ulong64 wakeupTime) {
/*0199*/ bool_t forever = FALSE; /* The most common value */
/*0200*/
/*0201*/ if (areActiveThreads()) {
/*0202*/ /* Indicate that we don't wait for an event */
/*0203*/ ll_setZero(wakeupTime);
/*0204*/ } else if (ll_zero_ne(wakeupTime)) {
/*0205*/ /* wakeupTime already has the right value. But change it to
/*0206./ * be at most 20ms from now if the debugger is available.
/*0207./ */
/*0208*/#if (ENABLE_JAVA_DEBUGGER || ASYNCHRONOUS_NATIVE_FUNCTIONS)
/*0209*/ ulong64 max = CurrentTime_md();
/*0210*/ ll_inc(max, 20);
/*0211*/ if (ll_compare_ge(wakeupTime, max)) {
/*0212*/ wakeupTime = max;
/*0213*/ }
/*0214*/#endif
/*0215*/ } else {
/*0216*/ /* We wait forever, unless the debugger is available, in which
/*0217./ * case we wait for at most 20ms.
/*0218./ */
/*0219*/#if (ENABLE_JAVA_DEBUGGER || ASYNCHRONOUS_NATIVE_FUNCTIONS)
/*0220*/ wakeupTime = CurrentTime_md();
/*0221*/ ll_inc(wakeupTime, 20);
/*0222*/#else
/*0223*/ forever = TRUE;
/*0224*/ ll_int_to_long(wakeupTime, -1);
/*0225*/#endif
/*0226*/ }
/*0227*/
/*0228*/ if (!opened || waitingThread == NULL) {
/*0229*/ /* Nothing to do but sleep */
/*0230*/ if (ll_zero_eq(wakeupTime)) {
/*0231*/ return;
/*0232*/ } else {
/*0243*/ SLEEP_UNTIL(wakeupTime);
/*0245*/ }
/*0246*/ } else {
/*0247*/ cell result;
/*0248*/ if (getKVMEvent(forever, wakeupTime, &result)) {
/*0249*/ (void)popStackForThread(waitingThread);
/*0250*/ pushStackForThread(waitingThread, result);
/*0251*/ resumeThread(waitingThread);
/*0252*/ waitingThread = NULL;
/*0253*/ }
/*0254*/ }
/*0255*/}
/*0256*/
/*0257*//*=========================================================================
/*0258./ * Protocol implementation methods ("events:" protocol)
/*0259./ *=======================================================================*/
/*0260*/
/*0261*/ /*=========================================================================
/*0262./ * FUNCTION: open()Z (VIRTUAL)
/*0263./ * CLASS: com.sun.cldc.io.j2me.events.Protocol
/*0264./ * TYPE: virtual native function
/*0265./ * OVERVIEW: Read an integer from the event queue
/*0266./ * INTERFACE (operand stack manipulation):
/*0267./ * parameters: this
/*0268./ * returns: the integer value
/*0269./ *=======================================================================*/
/*0270*/
/*0271*/void Java_com_sun_cldc_io_j2me_events_PrivateInputStream_open(void) {
/*0272*/ INSTANCE instance = popStackAsType(INSTANCE);
/*0273*/ (void)instance;
/*0274*/ if (opened) {
/*0275*/ raiseException("java/lang/IllegalAccessException");
/*0276*/ }
/*0277*/ opened = TRUE;
/*0278*/}
/*0279*/
/*0280*/ /*=========================================================================
/*0281./ * FUNCTION: close()Z (VIRTUAL)
/*0282./ * CLASS: com.sun.cldc.io.j2me.events.Protocol
/*0283./ * TYPE: virtual native function
/*0284./ * OVERVIEW: Read an integer from the event queue
/*0285./ * INTERFACE (operand stack manipulation):
/*0286./ * parameters: this
/*0287./ * returns: the integer value
/*0288./ *=======================================================================*/
/*0289*/
/*0290*/void Java_com_sun_cldc_io_j2me_events_PrivateInputStream_close(void) {
/*0291*/ INSTANCE instance = popStackAsType(INSTANCE);
/*0292*/ (void)instance;
/*0293*/ if (!opened) {
/*0294*/ raiseException("java/lang/IllegalAccessException");
/*0295*/ }
/*0296*/ opened = FALSE;
/*0297*/}
/*0298*/
/*0299*/ /*=========================================================================
/*0300./ * FUNCTION: int readInt()
/*0301./ * FUNCTION: byte[] readByteArray()
/*0302./ * FUNCTION String readUTF()
/*0303./ * CLASS: com.sun.cldc.io.j2me.events.Protocol
/*0304./ * TYPE: virtual native function
/*0305./ * OVERVIEW: Read an integer from the event queue
/*0306./ * INTERFACE (operand stack manipulation):
/*0307./ * parameters: this
/*0308./ * returns: the integer value
/*0309./ *=======================================================================*/
/*0310*/
/*0311*/static void readOneEvent() {
/*0312*/ ulong64 wakeupTime;
/*0313*/
/*0314*/ /* The "instance" on the top of the stack is trash */
/*0315*/ ll_setZero(wakeupTime);
/*0316*/
/*0317*/ if (!getKVMEvent(FALSE, wakeupTime, &topStack)) {
/*0318*/ waitingThread = CurrentThread;
/*0319*/ topStack = 0;
/*0320*/ suspendThread();
/*0321*/ }
/*0322*/ }
/*0323*/
/*0324*/void Java_com_sun_cldc_io_j2me_events_PrivateInputStream_readInt(void) {
/*0325*/ readOneEvent();
/*0326*/}
/*0327*/
/*0328*/void Java_com_sun_cldc_io_j2me_events_PrivateInputStream_readByteArray(void) {
/*0329*/ readOneEvent();
/*0330*/}
/*0331*/
/*0332*/void Java_com_sun_cldc_io_j2me_events_PrivateInputStream_readUTF(void) {
/*0333*/ readOneEvent();
/*0334*/}
/*0335*/
/*0336*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -