| 1 | /* | 
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup usbvirtkbd | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 | /** @file | 
|---|
| 33 | * @brief Keyboard keys related structures. | 
|---|
| 34 | */ | 
|---|
| 35 | #ifndef VUK_KEYS_H_ | 
|---|
| 36 | #define VUK_KEYS_H_ | 
|---|
| 37 |  | 
|---|
| 38 | #include <sys/types.h> | 
|---|
| 39 | #include <bool.h> | 
|---|
| 40 |  | 
|---|
| 41 | /** Maximum number of keys that can be pressed simultaneously. */ | 
|---|
| 42 | #define KB_MAX_KEYS_AT_ONCE 6 | 
|---|
| 43 |  | 
|---|
| 44 | /** Key code type. */ | 
|---|
| 45 | typedef uint8_t kb_key_code_t; | 
|---|
| 46 |  | 
|---|
| 47 | #define USB_HIDUT_KBD_KEY(name, usage_id, l, lc, l1, l2) \ | 
|---|
| 48 | KB_KEY_##name = usage_id, | 
|---|
| 49 | /** USB key code. */ | 
|---|
| 50 | typedef enum { | 
|---|
| 51 | #include <usb/classes/hidutkbd.h> | 
|---|
| 52 | } key_code_t; | 
|---|
| 53 |  | 
|---|
| 54 | /** Modifier type. */ | 
|---|
| 55 | typedef uint8_t kb_modifier_t; | 
|---|
| 56 | #define _KB_MOD(shift) (1 << (shift)) | 
|---|
| 57 | #define KB_MOD_LEFT_CTRL _KB_MOD(0) | 
|---|
| 58 | #define KB_MOD_LEFT_SHIFT _KB_MOD(1) | 
|---|
| 59 | #define KB_MOD_LEFT_ALT _KB_MOD(2) | 
|---|
| 60 | #define KB_MOD_LEFT_GUI _KB_MOD(3) | 
|---|
| 61 | #define KB_MOD_RIGHT_CTRL _KB_MOD(4) | 
|---|
| 62 | #define KB_MOD_RIGHT_SHIFT _KB_MOD(5) | 
|---|
| 63 | #define KB_MOD_RIGHT_ALT _KB_MOD(6) | 
|---|
| 64 | #define KB_MOD_RIGHT_GUI _KB_MOD(7) | 
|---|
| 65 |  | 
|---|
| 66 | /** Base key action. */ | 
|---|
| 67 | typedef enum { | 
|---|
| 68 | KB_KEY_DOWN, | 
|---|
| 69 | KB_KEY_UP | 
|---|
| 70 | } kb_key_action_t; | 
|---|
| 71 |  | 
|---|
| 72 | /** Keyboard status. */ | 
|---|
| 73 | typedef struct { | 
|---|
| 74 | /** Bitmap of pressed modifiers. */ | 
|---|
| 75 | kb_modifier_t modifiers; | 
|---|
| 76 | /** Array of currently pressed keys. */ | 
|---|
| 77 | kb_key_code_t pressed_keys[KB_MAX_KEYS_AT_ONCE]; | 
|---|
| 78 | } kb_status_t; | 
|---|
| 79 |  | 
|---|
| 80 | /** Callback type for status change. */ | 
|---|
| 81 | typedef void (*kb_on_status_change)(kb_status_t *); | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | void kb_init(kb_status_t *); | 
|---|
| 85 | void kb_change_modifier(kb_status_t *, kb_key_action_t, kb_modifier_t); | 
|---|
| 86 | void kb_change_key(kb_status_t *, kb_key_action_t, kb_key_code_t); | 
|---|
| 87 |  | 
|---|
| 88 | /** Keyboard event. | 
|---|
| 89 | * Use macros M_DOWN, M_UP, K_DOWN, K_UP, K_PRESS to generate list | 
|---|
| 90 | * of them. | 
|---|
| 91 | */ | 
|---|
| 92 | typedef struct { | 
|---|
| 93 | /** Key action. */ | 
|---|
| 94 | kb_key_action_t action; | 
|---|
| 95 | /** Switch whether action is about modifier or normal key. */ | 
|---|
| 96 | bool normal_key; | 
|---|
| 97 | /** Modifier change. */ | 
|---|
| 98 | kb_modifier_t modifier_change; | 
|---|
| 99 | /** Normal key change. */ | 
|---|
| 100 | kb_key_code_t key_change; | 
|---|
| 101 | } kb_event_t; | 
|---|
| 102 |  | 
|---|
| 103 | #define M_DOWN(mod) \ | 
|---|
| 104 | { KB_KEY_DOWN, false, (mod), 0 } | 
|---|
| 105 | #define M_UP(mod) \ | 
|---|
| 106 | { KB_KEY_UP, false, (mod), 0 } | 
|---|
| 107 | #define K_DOWN(key) \ | 
|---|
| 108 | { KB_KEY_DOWN, true, 0, (key) } | 
|---|
| 109 | #define K_UP(key) \ | 
|---|
| 110 | { KB_KEY_UP, true, 0, (key) } | 
|---|
| 111 | #define K_PRESS(key) \ | 
|---|
| 112 | K_DOWN(key), K_UP(key) | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | void kb_process_events(kb_status_t *, kb_event_t *, size_t, kb_on_status_change); | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | #endif | 
|---|
| 119 | /** | 
|---|
| 120 | * @} | 
|---|
| 121 | */ | 
|---|