UGL之Input Service

2022-03-31 10:45:50 浏览数 (1)

欢迎关注VxWorks567

如转发 请标明出处!

图形系统里,常用的Input Device应该就是KeyboardMouse

KB一般是PS2接口或者USB接口,当然也可以自定义,例如使用串口的Keypad;而KeyMap,常用的是美式键盘,WindML还支持一些其它布局

Mouse也是类似的,PS2、USB、串口,都可以。系统在处理Mouse数据时,通常在屏幕上绘制一个Pointer;而提供Pointer消息的,还可能是其它Device,例如Touch、Tablet

WindML在响应这些Input Device时,会将它们的数据封装为Input Message,然后route到Application,这个机制叫做Input Service

今天主要看看Application如何使用这些Input Message,至于Input Service内部是如何工作的,以后有机会再说

首先通过uglRegistryFind()找到Input Service

然后通过uglInputMsgGet()获取Input Message

参数timeout比较简单,单位是毫秒,另外可用UGL_NO_WAIT和UGL_WAIT_FOREVER

pMsg就复杂一些了

type一般是MSG_KEYBOARD或MSG_POINTER;objectId在callback里使用(以后再说);data是union类型

data的详细定义如下

代码语言:javascript复制
/* raw keyboard messages are posted by keyboard drivers */
typedef struct ugl_raw_kbd_data
    {
    struct ugl_input_dev *deviceId;       /* input device id */
    UGL_BOOL              isKeyDown;      /* down transition */
    UGL_BOOL              isExtended;     /* extended key */
    UGL_BOOL              isKeyCode;      /* down transition */
    union
        {
        UGL_UINT32        scanCode;       /* key scancode */
        UGL_UINT32        keyCode;        /* mapped key value   modifiers */
        }value;      
    } UGL_RAW_KBD_DATA;

/* raw pointer messages are posted by pointer drivers */
typedef struct ugl_raw_ptr_data
    {
    struct ugl_input_dev *deviceId;       /* input device id */
    UGL_UINT16            buttonState;    /* pointer button states */
    UGL_BOOL              isAbsolute;     /* UGL_TRUE == absolute, 
                                             UGL_FALSE == relative */
    union
        {
        UGL_POINT         absolute;       /* absolute coordinates */
        UGL_VECTOR        relative;       /* relative coordinates */
        }pos;
    } UGL_RAW_PTR_DATA;

/* 
 * modifiers
 * Keyboard and pointer messages are tagged with the the current state of 
 * both the keyboard modifiers keys (<alt>, <ctrl>, etc.)
 * and pointer button states (left button, right button, etc.).
 * If a bit is set then the corresponding key or
 * pointer button is "active" or is pressed down.
 */
#define UGL_PTR_BUTTON1         0x00000001
#define UGL_PTR_BUTTON2         0x00000002
#define UGL_PTR_BUTTON3         0x00000004
#define UGL_PTR_BUTTON4         0x00000008
#define UGL_PTR_BUTTON5         0x00000010
#define UGL_PTR_BUTTON_MASK     0x000000ff
#define UGL_PTR_MOD_MASK        0x000000ff

#define UGL_KBD_KEYDOWN         0x00010000
#define UGL_KBD_LEFT_ALT        0x00020000
#define UGL_KBD_RIGHT_ALT       0x00040000
#define UGL_KBD_ALT             0x00060000
#define UGL_KBD_LEFT_SHIFT      0x00080000
#define UGL_KBD_RIGHT_SHIFT     0x00100000
#define UGL_KBD_SHIFT           0x00180000
#define UGL_KBD_LEFT_CTRL       0x00200000
#define UGL_KBD_RIGHT_CTRL      0x00400000
#define UGL_KBD_CTRL            0x00600000
#define UGL_KBD_NUM_LOCK        0x00800000
#define UGL_KBD_SCROLL_LOCK     0x01000000
#define UGL_KBD_CAPS_LOCK       0x02000000
#define UGL_KBD_EXTENDED_KEY    0x04000000
#define UGL_KBD_NUM_PAD         0x08000000
#define UGL_KBD_MOD_MASK        0xffff0000  /* gets modifiers from keyCode */
#define UGL_KBD_KEY_MASK        0x0000ffff  /* gets key value from keyCode */

/* keyboard message data -- generated from raw keyboard messages */
typedef struct ugl_keyboard_data
    {
    UGL_RAW_KBD_DATA rawKbd;         /* keyboard data provided by driver */
    UGL_TIMESTAMP    timeStamp;      /* time message was queued */
    UGL_WCHAR        key;            /* mapped key value */
    UGL_POINT        ptrPos;         /* position of pointer */
    UGL_UINT32       modifiers;      /* kbd modifier keys & ptr btn states */
    } UGL_KEYBOARD_DATA;

/* pointer message data -- generated from raw pointer messages */
typedef struct ugl_pointer_data
    {
    UGL_RAW_PTR_DATA rawPtr;         /* pointer data provided by driver */
    UGL_TIMESTAMP    timeStamp;      /* time message was queued */
    UGL_POINT        position;       /* position of pointer */
    UGL_VECTOR       delta;          /* change in position of pointer */
    UGL_UINT32       buttonState;    /* ptr btn states & kbd modifiers*/
    UGL_UINT32       buttonChange;   /* change in pointer button states */
    } UGL_POINTER_DATA;

写个例子,用标准的PS2键鼠试试

代码语言:javascript复制
/*
 * 版权所有  公众号  VxWorks567
 */
#include <stdio.h>        /* printf() */
#include <ugl/ugl.h>
#include <ugl/uglinput.h> /* UGL_INPUT_SERVICE_ID */

void uglTest()
{
    UGL_REG_DATA *pReg;
    UGL_MSG       msg;
    UGL_INPUT_SERVICE_ID srvId;

    /* 初始化WindML */
    uglDisplayOpen(NULL, NULL, NULL);

    /* 获取输入服务 */
    pReg  = uglRegistryFind(UGL_INPUT_SERVICE_TYPE, NULL, NULL, NULL);
    srvId = (UGL_INPUT_SERVICE_ID)pReg->id;

    while(1)
        {
        /* 获取键鼠消息 */
        uglInputMsgGet(srvId, &msg, UGL_WAIT_FOREVER);

        if(msg.type == MSG_POINTER)
            {
            switch(msg.data.pointer.buttonChange)
                {
                case 0:              /* 移动鼠标 */
                    printf("x = %d, y = %dn", msg.data.pointer.position.x, msg.data.pointer.position.y);
                    break;
                case UGL_PTR_BUTTON1:
                    printf("Button Left %sn", (msg.data.pointer.buttonState & UGL_PTR_BUTTON1)?"Down":"Up");
                    break;
                case UGL_PTR_BUTTON2:
                    printf("Button Right %sn", (msg.data.pointer.buttonState & UGL_PTR_BUTTON2)?"Down":"Up");
                    break;
                case UGL_PTR_BUTTON3:
                    printf("Button Middle %sn", (msg.data.pointer.buttonState & UGL_PTR_BUTTON3)?"Down":"Up");
                    break;
                default:
                    printf("Other Buttonn");
                    break;
                }
            }
        else if(msg.type == MSG_KEYBOARD)
            {
            if(msg.data.keyboard.modifiers & UGL_KBD_KEYDOWN) 
                {
                printf("Key %c Downn", msg.data.keyboard.key);
                if (msg.data.keyboard.key == 'q' ||
                    msg.data.keyboard.key == 'Q')
                    break;
                }
            }
        else
            {
            printf("other typen");
            }
        }

    uglDisplayClose(NULL);
    return;
    }

跑一跑,效果如下

偷个懒,只打印了ASCII里的可打印字符,其它key,例如Ctrl、home等等,自己去调整吧

0 人点赞