2
* Copyright (c) 2006 Josef Cejka
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
29
/** @addtogroup kbdgen
30
* @brief HelenOS generic uspace keyboard handler.
37
#include <keybuffer.h>
40
atomic_t keybuffer_futex = FUTEX_INITIALIZER;
44
void keybuffer_free(keybuffer_t *keybuffer)
46
futex_down(&keybuffer_futex);
50
futex_up(&keybuffer_futex);
53
/** Key buffer initialization.
56
void keybuffer_init(keybuffer_t *keybuffer)
58
keybuffer_free(keybuffer);
61
/** Get free space in buffer.
63
* This function is useful for processing some scancodes that are translated
64
* to more than one character.
66
* @return empty buffer space
69
size_t keybuffer_available(keybuffer_t *keybuffer)
71
return KEYBUFFER_SIZE - keybuffer->items;
76
* @return nonzero, if buffer is not empty.
79
bool keybuffer_empty(keybuffer_t *keybuffer)
81
return (keybuffer->items == 0);
84
/** Push key event to key buffer.
86
* If the buffer is full, the event is ignored.
88
* @param keybuffer The keybuffer.
89
* @param ev The event to push.
92
void keybuffer_push(keybuffer_t *keybuffer, const console_event_t *ev)
94
futex_down(&keybuffer_futex);
96
if (keybuffer->items < KEYBUFFER_SIZE) {
97
keybuffer->fifo[keybuffer->tail] = *ev;
98
keybuffer->tail = (keybuffer->tail + 1) % KEYBUFFER_SIZE;
102
futex_up(&keybuffer_futex);
105
/** Pop event from buffer.
107
* @param edst Pointer to where the event should be saved.
109
* @return True if an event was popped.
112
bool keybuffer_pop(keybuffer_t *keybuffer, console_event_t *edst)
114
futex_down(&keybuffer_futex);
116
if (keybuffer->items > 0) {
118
*edst = (keybuffer->fifo[keybuffer->head]);
119
keybuffer->head = (keybuffer->head + 1) % KEYBUFFER_SIZE;
120
futex_up(&keybuffer_futex);
125
futex_up(&keybuffer_futex);