source: mainline/uspace/srv/hid/console/keybuffer.c@ 6ef2c11

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6ef2c11 was b3d513f, checked in by Martin Decky <martin@…>, 15 years ago

restructure servers into a more well-arranged hierarchy

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