⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 event.c

📁 在ecos 下mingui 的移植开发
💻 C
字号:
//// event.c: Low level event handling module.//// Copyright (C) 1999, Wei Yongming.//// Current maintainer: Wei Yongming./***  This library is free software; you can redistribute it and/or**  modify it under the terms of the GNU Library General Public**  License as published by the Free Software Foundation; either**  version 2 of the License, or (at your option) any later version.****  This library 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**  Library General Public License for more details.****  You should have received a copy of the GNU Library General Public**  License along with this library; if not, write to the Free**  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,**  MA 02111-1307, USA*/// Create date: 1999.01.11//// Modify records:////  Who             When        Where       For What                Status//-----------------------------------------------------------------------------//  Wei Yongming    1999.7      Tsinghua    Shift Key Status        Finished//// TODO:// #include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#include <semaphore.h>#include "common.h"#include "minigui.h"#include "ial.h"#include "cursor.h"#include "event.h"#ifdef HAVE_LINUX_KEYBOARD_H /* For NR_KEYS */#include <linux/keyboard.h>#endif#ifndef NR_KEYS#define NR_KEYS		128#endif#ifndef lintstatic char fileid[] = "$Id: event.c,v 1.11 2000/08/10 09:39:47 weiym Exp $";#endif#define __USE_TIMEVAL   0// Mouse event parameter.// The variable, dblclicktime, is a shared varible.static int dblclicktime;static int timeoutusec;static int repeatusec;static void GetDblclickTime(void){    char szValue[11];    int ms;#if __USE_TIMEVAL    dblclicktime = DEF_MSEC_DBLCLICK;#else    dblclicktime = DEF_MSEC_DBLCLICK / 10;#endif    if( GetValueFromEtcFile(ETCFILEPATH, MOUSEPARA,                            MOUSEPARA_DBLCLICKTIME, szValue, 10) < 0 )        return;    ms = atoi(szValue);            if (ms > 0 && ms < 1000) {#if __USE_TIMEVAL        dblclicktime = ms;#else        dblclicktime = ms / 10;#endif    }}static void GetTimeout (void){    char szValue [11];    int mytimeoutusec, myrepeatusec;    timeoutusec = DEF_USEC_TIMEOUT;    repeatusec = DEF_REPEAT_TIME;    if (GetValueFromEtcFile (ETCFILEPATH, EVENTPARA,                            EVENTPARA_REPEATUSEC, szValue, 10) < 0)        return;    myrepeatusec = atoi(szValue);            if( GetValueFromEtcFile(ETCFILEPATH, EVENTPARA,                            EVENTPARA_TIMEOUTUSEC, szValue, 10) < 0 )        return;    mytimeoutusec = atoi(szValue);    if (myrepeatusec >= 0 && mytimeoutusec > 0) {        timeoutusec = mytimeoutusec;        repeatusec = myrepeatusec;     }}// Mouse event parameters.static int oldbutton = 0;static struct timeval timeout;#if __USE_TIMEVALstatic struct timeval time1 = {0, 0};static struct timeval time2 = {0, 0};#elsestatic unsigned int time1;static unsigned int time2;extern unsigned int timer_counter;#endif// Key event parameters.static unsigned char oldkeystate[NR_KEYS];static unsigned char olddownkey = 0;static DWORD status;static int alt1 = 0;               /* left alt key state */static int alt2 = 0;               /* right alt key state */static int capslock = 0;           /* caps lock key state */static int esc = 0;                /* escape scan code detected? */static int caps_off = 1;           /* 1 = normal position, 0 = depressed */static int numlock = 0;            /* number lock key state */static int num_off = 1;            /* 1 = normal position, 0 = depressed */static int slock = 0;              /* scroll lock key state */static int slock_off = 1;          /* 1 = normal position, 0 = depressed */static int control1 = 0;           /* left control key state */static int control2 = 0;           /* right control key state */static int shift1 = 0;             /* left shift key state */static int shift2 = 0;             /* left shift key state */static void ResetMouseEvent(void){    IAL_UpdateMouse ();    oldbutton = IAL_GetMouseButton ();#if __USE_TIMEVAL    timerclear(&time1);    timerclear(&time2);#else    time1 = 0;    time2 = 0;#endif}static void ResetKeyEvent(void){    IAL_UpdateKeyboard ();    memcpy (oldkeystate, IAL_GetKeyboardState (), NR_KEYS);    olddownkey  = 0;    status      = 0;    alt1        = 0;    alt2        = 0;    esc         = 0;    control1    = 0;    control2    = 0;    shift1      = 0;    shift2      = 0;    capslock    = 0;    caps_off    = 1;    numlock     = 0;    num_off     = 1;    slock       = 0;    slock_off   = 1;    IAL_SetLeds (slock | (numlock << 1) | (capslock << 2));    timeout.tv_sec = 0;    timeout.tv_usec = timeoutusec;}BOOL GetLWEvent (BOOL wait, PLWEVENT lwe){    static int timeout_count = 0;    static LWEVENT old_lwe = {0, 0};    int event = 0;#if __USE_TIMEVAE    struct timeval temp = {0, 0};    int secinter, usecinter;#else    unsigned int interval;#endif    int button;    PMOUSEEVENT me = &(lwe->data.me);    PKEYEVENT ke = &(lwe->data.ke);    unsigned char* keystate;    int i;    int make;       /* 0 = release, 1 = presse */    if (wait) {        event = IAL_WaitEvent (IAL_MOUSEEVENT | IAL_KEYEVENT, NULL, NULL, NULL, &timeout);    }    else {        if (IAL_UpdateMouse ()) event |= IAL_MOUSEEVENT;        if (IAL_UpdateKeyboard ()) event |= IAL_KEYEVENT;    }        if (event == 0 && wait) {        if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {            // set repeat time            timeout.tv_sec = 0;            timeout.tv_usec = repeatusec;            // repeat last event            if (old_lwe.type == LWETYPE_KEY                     && old_lwe.data.ke.event == KE_KEYDOWN) {               memcpy (lwe, &old_lwe, sizeof (LWEVENT));               lwe->data.ke.status |= KE_REPEATED;               return 1;            }            if (!(old_lwe.type == LWETYPE_MOUSE                    && (old_lwe.data.me.event == ME_LEFTDOWN ||                        old_lwe.data.me.event == ME_RIGHTDOWN ||                        old_lwe.data.me.event == ME_MIDDLEDOWN))) {                // reset delay time                timeout.tv_sec = 0;                timeout.tv_usec = timeoutusec;            }            lwe->type = LWETYPE_TIMEOUT;            lwe->count = ++timeout_count;            return 1;        }        old_lwe.type = 0;        return 0;    }    timeout_count = 0;    // There was a event occurred.    if (event & IAL_MOUSEEVENT) {        lwe->type = LWETYPE_MOUSE;        if (RefreshCursor(&me->x, &me->y, &button)) {            me->event = ME_MOVED;#if __USE_TIMEVAL            timerclear(&time1);            timerclear(&time2);#else            time1 = 0;            time2 = 0;#endif            if (oldbutton == button)                return 1;        }           if ( !(oldbutton & IAL_MOUSE_LEFTBUTTON) &&               (button & IAL_MOUSE_LEFTBUTTON) )        {#if __USE_TIMEVAL            if( timerisset(&time1) )	// check double click            {                gettimeofday(&temp, NULL);                secinter = temp.tv_sec - time1.tv_sec;                usecinter = (temp.tv_usec - time1.tv_usec)/1000;                if(usecinter < 0)                    usecinter += 1000;                if( secinter < 2 && usecinter <= dblclicktime )                    me->event = ME_LEFTDBLCLICK;                else                    me->event = ME_LEFTDOWN;                timerclear(&time1);            }            else            {                gettimeofday(&time1, NULL);                me->event = ME_LEFTDOWN;            }#else            if (time1) {                interval = timer_counter - time1;                if (interval <= dblclicktime)                    me->event = ME_LEFTDBLCLICK;                else                    me->event = ME_LEFTDOWN;                time1 = 0;            }            else {                time1 = timer_counter;                me->event = ME_LEFTDOWN;            }#endif            goto mouseret;        }        if ( (oldbutton & IAL_MOUSE_LEFTBUTTON) &&              !(button & IAL_MOUSE_LEFTBUTTON) )        {            me->event = ME_LEFTUP;            goto mouseret;        }        if ( !(oldbutton & IAL_MOUSE_RIGHTBUTTON) &&               (button & IAL_MOUSE_RIGHTBUTTON) )        {#if __USE_TIMEVAL            if( timerisset(&time2) )	// check double click            {                gettimeofday(&temp, NULL);                secinter = temp.tv_sec - time2.tv_sec;                usecinter = (temp.tv_usec - time2.tv_usec)/1000;                if(usecinter < 0)                    usecinter += 1000;                if( secinter < 2 && usecinter <= dblclicktime )                    me->event = ME_RIGHTDBLCLICK;                else                    me->event = ME_RIGHTDOWN;                timerclear(&time2);            }            else            {                gettimeofday(&time2, NULL);                me->event = ME_RIGHTDOWN;            }#else            if (time2) {                interval = timer_counter - time2;                if (interval <= dblclicktime)                    me->event = ME_RIGHTDBLCLICK;                else                    me->event = ME_RIGHTDOWN;                time2 = 0;            }            else {                time2 = timer_counter;                me->event = ME_RIGHTDOWN;            }#endif            goto mouseret;        }        if ( (oldbutton & IAL_MOUSE_RIGHTBUTTON) &&             !(button & IAL_MOUSE_RIGHTBUTTON) )        {            me->event = ME_RIGHTUP;            goto mouseret;        }    }    if(event & IAL_KEYEVENT) {        lwe->type = LWETYPE_KEY;        keystate = IAL_GetKeyboardState ();        for(i = 0; i < NR_KEYS; i++) {            if(!oldkeystate[i] && keystate[i]) {                 ke->event = KE_KEYDOWN;                 ke->scancode = i;                 olddownkey = i;                 break;            }            if(oldkeystate[i] && !keystate[i]) {                 ke->event = KE_KEYUP;                 ke->scancode = i;                 break;            }        }        if (i == NR_KEYS) {            ke->event = KE_KEYDOWN;            ke->scancode = olddownkey;        }                make = (ke->event == KE_KEYDOWN)?1:0;        if (i != NR_KEYS) {            unsigned leds;            switch (ke->scancode) {                case SCANCODE_CAPSLOCK:                    if (make && caps_off) {                        capslock = 1 - capslock;                        leds = slock | (numlock << 1) | (capslock << 2);                        IAL_SetLeds (leds);                        status = (DWORD)leds << 16;                    }                    caps_off = 1 - make;                break;                                    case SCANCODE_NUMLOCK:                    if (make && num_off) {                        numlock = 1 - numlock;                        leds = slock | (numlock << 1) | (capslock << 2);                        IAL_SetLeds (leds);                        status = (DWORD)leds << 16;                    }                    num_off = 1 - make;                break;                                case SCANCODE_SCROLLLOCK:                    if (make & slock_off) {                        slock = 1 - slock;                        leds = slock | (numlock << 1) | (capslock << 2);                        IAL_SetLeds (leds);                        status = (DWORD)leds << 16;                    }                    slock_off = 1 - make;                    break;                case SCANCODE_LEFTCONTROL:                    control1 = make;                    break;                                    case SCANCODE_RIGHTCONTROL:                    control2 = make;                    break;                                    case SCANCODE_LEFTSHIFT:                    shift1 = make;                    break;                                    case SCANCODE_RIGHTSHIFT:                    shift2 = make;                    break;                                    case SCANCODE_LEFTALT:                    alt1 = make;                    break;                case SCANCODE_RIGHTALT:                    alt2 = make;                    break;                                }            status &= 0xFFFFF0C0;            status |= (DWORD)((capslock << 8) |                             (numlock << 7)   |                             (slock << 6)     |                             (control1 << 5)  |                             (control2 << 4)  |                             (alt1 << 3)      |                             (alt2 << 2)      |                             (shift1 << 1)    |                             (shift2));                                         // Mouse button status            if (oldbutton & IAL_MOUSE_LEFTBUTTON)                status |= 0x00000100;            else if (oldbutton & IAL_MOUSE_RIGHTBUTTON)                status |= 0x00000200;        }        ke->status = status;        memcpy (oldkeystate, keystate, NR_KEYS);        memcpy (&old_lwe, lwe, sizeof (LWEVENT));        timeout.tv_sec = 0;        timeout.tv_usec = timeoutusec;        return 1;    }     old_lwe.type = 0;    return 0;mouseret:    status &= 0xFFFFF0FF;    oldbutton = button;    if (oldbutton & IAL_MOUSE_LEFTBUTTON)        status |= 0x00000100;    if (oldbutton & IAL_MOUSE_RIGHTBUTTON)        status |= 0x00000200;    me->status = status;    memcpy (&old_lwe, lwe, sizeof (LWEVENT));    timeout.tv_sec = 0;    timeout.tv_usec = timeoutusec;    return 1;}BOOL GUIAPI GetKeyStatus (UINT uKey){    if (uKey & 0x0F00)      // this is a mouse key        return oldbutton & (uKey >> 8);    else if (uKey < NR_KEYS)        return oldkeystate [uKey];    return FALSE;}DWORD GUIAPI GetShiftKeyStatus (void){    return status;}BOOL InitLWEvent (void){    GetDblclickTime ();    GetTimeout ();#ifdef _DEBUG    fprintf (stderr, "EVENT: Interval of double click: %d.\n", dblclicktime);    fprintf (stderr, "EVENT: event timeout: %d us.\n", timeoutusec);#endif    if (InitIAL ())        return FALSE;    ResetMouseEvent();    ResetKeyEvent();    return TRUE;}void TerminateLWEvent(void){    ResetMouseEvent ();    ResetKeyEvent ();    TerminateIAL ();}

⌨️ 快捷键说明

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