1 | /*
|
---|
2 | * Copyright (c) 2006 Josef Cejka
|
---|
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 kbdgen
|
---|
30 | * @brief HelenOS generic uspace keyboard handler.
|
---|
31 | * @ingroup kbd
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <futex.h>
|
---|
38 | #include "keybuffer.h"
|
---|
39 |
|
---|
40 | atomic_t keybuffer_futex = FUTEX_INITIALIZER;
|
---|
41 |
|
---|
42 | /** Clear key buffer.
|
---|
43 | */
|
---|
44 | void keybuffer_free(keybuffer_t *keybuffer)
|
---|
45 | {
|
---|
46 | futex_down(&keybuffer_futex);
|
---|
47 | keybuffer->head = 0;
|
---|
48 | keybuffer->tail = 0;
|
---|
49 | keybuffer->items = 0;
|
---|
50 | futex_up(&keybuffer_futex);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** Key buffer initialization.
|
---|
54 | *
|
---|
55 | */
|
---|
56 | void keybuffer_init(keybuffer_t *keybuffer)
|
---|
57 | {
|
---|
58 | keybuffer_free(keybuffer);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Get free space in buffer.
|
---|
62 | *
|
---|
63 | * This function is useful for processing some scancodes that are translated
|
---|
64 | * to more than one character.
|
---|
65 | *
|
---|
66 | * @return empty buffer space
|
---|
67 | *
|
---|
68 | */
|
---|
69 | size_t keybuffer_available(keybuffer_t *keybuffer)
|
---|
70 | {
|
---|
71 | return KEYBUFFER_SIZE - keybuffer->items;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | *
|
---|
76 | * @return nonzero, if buffer is not empty.
|
---|
77 | *
|
---|
78 | */
|
---|
79 | bool keybuffer_empty(keybuffer_t *keybuffer)
|
---|
80 | {
|
---|
81 | return (keybuffer->items == 0);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Push key event to key buffer.
|
---|
85 | *
|
---|
86 | * If the buffer is full, the event is ignored.
|
---|
87 | *
|
---|
88 | * @param keybuffer The keybuffer.
|
---|
89 | * @param ev The event to push.
|
---|
90 | *
|
---|
91 | */
|
---|
92 | void keybuffer_push(keybuffer_t *keybuffer, const console_event_t *ev)
|
---|
93 | {
|
---|
94 | futex_down(&keybuffer_futex);
|
---|
95 |
|
---|
96 | if (keybuffer->items < KEYBUFFER_SIZE) {
|
---|
97 | keybuffer->fifo[keybuffer->tail] = *ev;
|
---|
98 | keybuffer->tail = (keybuffer->tail + 1) % KEYBUFFER_SIZE;
|
---|
99 | keybuffer->items++;
|
---|
100 | }
|
---|
101 |
|
---|
102 | futex_up(&keybuffer_futex);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /** Pop event from buffer.
|
---|
106 | *
|
---|
107 | * @param edst Pointer to where the event should be saved.
|
---|
108 | *
|
---|
109 | * @return True if an event was popped.
|
---|
110 | *
|
---|
111 | */
|
---|
112 | bool keybuffer_pop(keybuffer_t *keybuffer, console_event_t *edst)
|
---|
113 | {
|
---|
114 | futex_down(&keybuffer_futex);
|
---|
115 |
|
---|
116 | if (keybuffer->items > 0) {
|
---|
117 | keybuffer->items--;
|
---|
118 | *edst = (keybuffer->fifo[keybuffer->head]);
|
---|
119 | keybuffer->head = (keybuffer->head + 1) % KEYBUFFER_SIZE;
|
---|
120 | futex_up(&keybuffer_futex);
|
---|
121 |
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | futex_up(&keybuffer_futex);
|
---|
126 |
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * @}
|
---|
132 | */
|
---|