Subversion Repositories HelenOS

Rev

Rev 4462 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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