| 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 usb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Keyboard keys related structures.
|
|---|
| 35 | */
|
|---|
| 36 | #include "keys.h"
|
|---|
| 37 |
|
|---|
| 38 | /** Initializes keyboard status. */
|
|---|
| 39 | void kb_init(kb_status_t *status)
|
|---|
| 40 | {
|
|---|
| 41 | status->modifiers = 0;
|
|---|
| 42 | size_t i;
|
|---|
| 43 | for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
|
|---|
| 44 | status->pressed_keys[i] = 0;
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /** Change pressed modifiers. */
|
|---|
| 49 | void kb_change_modifier(kb_status_t *status, kb_key_action_t action,
|
|---|
| 50 | kb_modifier_t modifier)
|
|---|
| 51 | {
|
|---|
| 52 | if (action == KB_KEY_DOWN) {
|
|---|
| 53 | status->modifiers = status->modifiers | modifier;
|
|---|
| 54 | } else {
|
|---|
| 55 | status->modifiers = status->modifiers & (~modifier);
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /** Find index of given key in key code array.
|
|---|
| 60 | * @retval (size_t)-1 Key not found.
|
|---|
| 61 | */
|
|---|
| 62 | static size_t find_key_index(kb_key_code_t *keys, size_t size, kb_key_code_t key)
|
|---|
| 63 | {
|
|---|
| 64 | size_t i;
|
|---|
| 65 | for (i = 0; i < size; i++) {
|
|---|
| 66 | if (keys[i] == key) {
|
|---|
| 67 | return i;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | return (size_t)-1;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Change pressed key. */
|
|---|
| 74 | void kb_change_key(kb_status_t *status, kb_key_action_t action,
|
|---|
| 75 | kb_key_code_t key_code)
|
|---|
| 76 | {
|
|---|
| 77 | size_t pos = find_key_index(status->pressed_keys, KB_MAX_KEYS_AT_ONCE,
|
|---|
| 78 | key_code);
|
|---|
| 79 | if (action == KB_KEY_DOWN) {
|
|---|
| 80 | if (pos != (size_t)-1) {
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 | /*
|
|---|
| 84 | * Find first free item in the array.
|
|---|
| 85 | */
|
|---|
| 86 | size_t i;
|
|---|
| 87 | for (i = 0; i < KB_MAX_KEYS_AT_ONCE; i++) {
|
|---|
| 88 | if (status->pressed_keys[i] == 0) {
|
|---|
| 89 | status->pressed_keys[i] = key_code;
|
|---|
| 90 | return;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | // TODO - handle buffer overflow
|
|---|
| 94 | } else {
|
|---|
| 95 | if (pos == (size_t)-1) {
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 | status->pressed_keys[pos] = 0;
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Process list of events. */
|
|---|
| 103 | void kb_process_events(kb_status_t *status,
|
|---|
| 104 | kb_event_t *events, size_t count,
|
|---|
| 105 | kb_on_status_change on_change)
|
|---|
| 106 | {
|
|---|
| 107 | while (count-- > 0) {
|
|---|
| 108 | if (events->normal_key) {
|
|---|
| 109 | kb_change_key(status, events->action,
|
|---|
| 110 | events->key_change);
|
|---|
| 111 | } else {
|
|---|
| 112 | kb_change_modifier(status, events->action,
|
|---|
| 113 | events->modifier_change);
|
|---|
| 114 | }
|
|---|
| 115 | if (on_change) {
|
|---|
| 116 | on_change(status);
|
|---|
| 117 | }
|
|---|
| 118 | events++;
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /** @}
|
|---|
| 123 | */
|
|---|