📄 event.c
字号:
*dx = *dy = 0; drv->packet_len=4; getextra=0; } else { /* Got 3/4, as expected */ /* Motion is independent of packetlen... */ *dx = (char)(((data[0] & 0x03) << 6) | (data[1] & 0x3F)); *dy = (char)(((data[0] & 0x0C) << 4) | (data[2] & 0x3F)); prev = ((data[0] & 0x20) >> 3) | ((data[0] & 0x10) >> 4); if (drv->packet_len==4) b = data[3]>>4; } if (drv->packet_len == 4) { if (b == 0) { drv->packet_len = 3; getextra = 1; } else { if (b & 0x2) prev |= 2; } } *buttons = prev; /* This "chord-middle" behaviour was reported by David A. van Leeuwen */ if (((prev ^ *buttons) & 5) == 5) *buttons = *buttons ? 2 : 0; prev = *buttons; break; } case EVT_noMouse: return false; break; } return true;}/****************************************************************************REMARKS:Map a keypress via the key mapping table****************************************************************************/static int getKeyMapping( keymap *tab, int nb, int key){ int i; for(i = 0; i < nb; i++) { if (tab[i].scan == key) return tab[i].map; } return key;}#ifdef USE_OS_JOYSTICKstatic char js0_axes = 0, js0_buttons = 0;static char js1_axes = 0, js1_buttons = 0;static char joystick0_dev[20] = "/dev/js0";static char joystick1_dev[20] = "/dev/js1";/****************************************************************************REMARKS:Create a joystick event from the joystick data****************************************************************************/static void makeJoyEvent( event_t *evt){ evt->message = 0; if (buts0 && axis0) { if (buts0[0]) evt->message |= EVT_JOY1_BUTTONA; if (buts0[1]) evt->message |= EVT_JOY1_BUTTONB; evt->where_x = axis0[0]; evt->where_y = axis0[1]; } else evt->where_x = evt->where_y = 0; if (buts1 && axis1) { if (buts1[0]) evt->message |= EVT_JOY2_BUTTONA; if (buts1[1]) evt->message |= EVT_JOY2_BUTTONB; evt->where_x = axis1[0]; evt->where_y = axis1[1]; } else evt->where_x = evt->where_y = 0;}/****************************************************************************REMARKS:Read the joystick axis data****************************************************************************/int EVTAPI _EVT_readJoyAxis( int jmask, int *axis){ int mask = 0; if ((js_version & ~0xffff) == 0) { /* Old 0.x driver */ struct JS_DATA_TYPE js; if (joystick0_fd && read(joystick0_fd, &js, JS_RETURN) == JS_RETURN) { if (jmask & EVT_JOY_AXIS_X1) axis[0] = js.x; if (jmask & EVT_JOY_AXIS_Y1) axis[1] = js.y; mask |= EVT_JOY_AXIS_X1|EVT_JOY_AXIS_Y1; } if (joystick1_fd && read(joystick1_fd, &js, JS_RETURN) == JS_RETURN) { if (jmask & EVT_JOY_AXIS_X2) axis[2] = js.x; if (jmask & EVT_JOY_AXIS_Y2) axis[3] = js.y; mask |= EVT_JOY_AXIS_X2|EVT_JOY_AXIS_Y2; } } else { if (axis0) { if (jmask & EVT_JOY_AXIS_X1) axis[0] = axis0[0]; if (jmask & EVT_JOY_AXIS_Y1) axis[1] = axis0[1]; mask |= EVT_JOY_AXIS_X1 | EVT_JOY_AXIS_Y1; } if (axis1) { if (jmask & EVT_JOY_AXIS_X2) axis[2] = axis1[0]; if (jmask & EVT_JOY_AXIS_Y2) axis[3] = axis1[1]; mask |= EVT_JOY_AXIS_X2 | EVT_JOY_AXIS_Y2; } } return mask;}/****************************************************************************REMARKS:Read the joystick button data****************************************************************************/int EVTAPI _EVT_readJoyButtons(void){ int buts = 0; if ((js_version & ~0xffff) == 0) { /* Old 0.x driver */ struct JS_DATA_TYPE js; if (joystick0_fd && read(joystick0_fd, &js, JS_RETURN) == JS_RETURN) buts = js.buttons; if (joystick1_fd && read(joystick1_fd, &js, JS_RETURN) == JS_RETURN) buts |= js.buttons << 2; } else { if (buts0) buts |= EVT_JOY1_BUTTONA*buts0[0] + EVT_JOY1_BUTTONB*buts0[1]; if (buts1) buts |= EVT_JOY2_BUTTONA*buts1[0] + EVT_JOY2_BUTTONB*buts1[1]; } return buts;}/****************************************************************************DESCRIPTION:Returns the mask indicating what joystick axes are attached.HEADER:event.hREMARKS:This function is used to detect the attached joysticks, and determinewhat axes are present and functioning. This function will re-detect anyattached joysticks when it is called, so if the user forgot to attachthe joystick when the application started, you can call this function tore-detect any newly attached joysticks.SEE ALSO:EVT_joySetLowerRight, EVT_joySetCenter, EVT_joyIsPresent****************************************************************************/int EVTAPI EVT_joyIsPresent(void){ static int mask = 0; int i; char *tmp, name0[128], name1[128]; static ibool inited = false; if (inited) return mask; memset(EVT.joyMin,0,sizeof(EVT.joyMin)); memset(EVT.joyCenter,0,sizeof(EVT.joyCenter)); memset(EVT.joyMax,0,sizeof(EVT.joyMax)); memset(EVT.joyPrev,0,sizeof(EVT.joyPrev)); EVT.joyButState = 0; if ((tmp = getenv(ENV_JOYDEV0)) != NULL) strcpy(joystick0_dev,tmp); if ((tmp = getenv(ENV_JOYDEV1)) != NULL) strcpy(joystick1_dev,tmp); if ((joystick0_fd = open(joystick0_dev, O_RDONLY)) < 0) joystick0_fd = 0; if ((joystick1_fd = open(joystick1_dev, O_RDONLY)) < 0) joystick1_fd = 0; if (!joystick0_fd && !joystick1_fd) // No joysticks detected return 0; inited = true; if (ioctl(joystick0_fd ? joystick0_fd : joystick1_fd, JSIOCGVERSION, &js_version) < 0) return 0; /* Initialise joystick 0 */ if (joystick0_fd) { ioctl(joystick0_fd, JSIOCGNAME(sizeof(name0)), name0); if (js_version & ~0xffff) { struct js_event js; ioctl(joystick0_fd, JSIOCGAXES, &js0_axes); ioctl(joystick0_fd, JSIOCGBUTTONS, &js0_buttons); axis0 = PM_calloc((int)js0_axes, sizeof(short)); buts0 = PM_malloc((int)js0_buttons); /* Read the initial events */ while(dataReady(joystick0_fd) && read(joystick0_fd, &js, sizeof(struct js_event)) == sizeof(struct js_event) && (js.type & JS_EVENT_INIT) ) { if (js.type & JS_EVENT_BUTTON) buts0[js.number] = js.value; else if (js.type & JS_EVENT_AXIS) axis0[js.number] = scaleJoyAxis(js.value,js.number); } } else { js0_axes = 2; js0_buttons = 2; axis0 = PM_calloc((int)js0_axes, sizeof(short)); buts0 = PM_malloc((int)js0_buttons); } } /* Initialise joystick 1 */ if (joystick1_fd) { ioctl(joystick1_fd, JSIOCGNAME(sizeof(name1)), name1); if (js_version & ~0xffff) { struct js_event js; ioctl(joystick1_fd, JSIOCGAXES, &js1_axes); ioctl(joystick1_fd, JSIOCGBUTTONS, &js1_buttons); axis1 = PM_calloc((int)js1_axes, sizeof(short)); buts1 = PM_malloc((int)js1_buttons); /* Read the initial events */ while(dataReady(joystick1_fd) && read(joystick1_fd, &js, sizeof(struct js_event))==sizeof(struct js_event) && (js.type & JS_EVENT_INIT) ) { if (js.type & JS_EVENT_BUTTON) buts1[js.number] = js.value; else if (js.type & JS_EVENT_AXIS) axis1[js.number] = scaleJoyAxis(js.value,js.number<<2); } } else { js1_axes = 2; js1_buttons = 2; axis1 = PM_calloc((int)js1_axes, sizeof(short)); buts1 = PM_malloc((int)js1_buttons); } }#ifdef CHECKED fprintf(stderr,"Using joystick driver version %d.%d.%d\n", js_version >> 16, (js_version >> 8) & 0xff, js_version & 0xff); if (joystick0_fd) fprintf(stderr,"Joystick 1 (%s): %s\n", joystick0_dev, name0); if (joystick1_fd) fprintf(stderr,"Joystick 2 (%s): %s\n", joystick1_dev, name1);#endif mask = _EVT_readJoyAxis(EVT_JOY_AXIS_ALL,EVT.joyCenter); if (mask) { for (i = 0; i < JOY_NUM_AXES; i++) EVT.joyMax[i] = EVT.joyCenter[i]*2; } return mask;}/****************************************************************************DESCRIPTION:Polls the joystick for position and button information.HEADER:event.hREMARKS:This routine is used to poll analogue joysticks for button and positioninformation. It should be called once for each main loop of the userapplication, just before processing all pending events via EVT_getNext.All information polled from the joystick will be posted to the eventqueue for later retrieval.Note: Most analogue joysticks will provide readings that change even though the joystick has not moved. Hence if you call this routine you will likely get an EVT_JOYMOVE event every time through your event loop.SEE ALSO:EVT_getNext, EVT_peekNext, EVT_joySetUpperLeft, EVT_joySetLowerRight,EVT_joySetCenter, EVT_joyIsPresent****************************************************************************/void EVTAPI EVT_pollJoystick(void){ event_t evt; int i,axis[JOY_NUM_AXES],newButState,mask,moved,ps; if ((js_version & ~0xFFFF) == 0 && EVT.joyMask) { /* Read joystick axes and post movement events if they have * changed since the last time we polled. Until the events are * actually flushed, we keep modifying the same joystick movement * event, so you won't get multiple movement event */ mask = _EVT_readJoyAxis(EVT.joyMask,axis); newButState = _EVT_readJoyButtons(); moved = false; for (i = 0; i < JOY_NUM_AXES; i++) { if (mask & (EVT_JOY_AXIS_X1 << i)) axis[i] = scaleJoyAxis(axis[i],i); else axis[i] = EVT.joyPrev[i]; if (axis[i] != EVT.joyPrev[i]) moved = true; } if (moved) { memcpy(EVT.joyPrev,axis,sizeof(EVT.joyPrev)); ps = _EVT_disableInt(); if (EVT.oldJoyMove != -1) { /* Modify the existing joystick movement event */ EVT.evtq[EVT.oldJoyMove].message = newButState; EVT.evtq[EVT.oldJoyMove].where_x = EVT.joyPrev[0]; EVT.evtq[EVT.oldJoyMove].where_y = EVT.joyPrev[1]; EVT.evtq[EVT.oldJoyMove].relative_x = EVT.joyPrev[2]; EVT.evtq[EVT.oldJoyMove].relative_y = EVT.joyPrev[3]; } else if (EVT.count < EVENTQSIZE) { /* Add a new joystick movement event */ EVT.oldJoyMove = EVT.freeHead; memset(&evt,0,sizeof(evt)); evt.what = EVT_JOYMOVE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -