Rev 4462 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4462 | Rev 4581 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Josef Cejka |
2 | * Copyright (c) 2006 Josef Cejka |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup kbdgen |
29 | /** @addtogroup kbdgen |
30 | * @brief HelenOS generic uspace keyboard handler. |
30 | * @brief HelenOS generic uspace keyboard handler. |
31 | * @ingroup kbd |
31 | * @ingroup kbd |
32 | * @{ |
32 | * @{ |
33 | */ |
33 | */ |
34 | /** @file |
34 | /** @file |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <keybuffer.h> |
37 | #include <keybuffer.h> |
38 | #include <futex.h> |
38 | #include <futex.h> |
39 | 39 | ||
40 | atomic_t keybuffer_futex = FUTEX_INITIALIZER; |
40 | atomic_t keybuffer_futex = FUTEX_INITIALIZER; |
41 | 41 | ||
42 | /** Clear key buffer. |
42 | /** Clear key buffer. |
43 | */ |
43 | */ |
44 | void keybuffer_free(keybuffer_t *keybuffer) |
44 | void keybuffer_free(keybuffer_t *keybuffer) |
45 | { |
45 | { |
46 | futex_down(&keybuffer_futex); |
46 | futex_down(&keybuffer_futex); |
47 | keybuffer->head = 0; |
47 | keybuffer->head = 0; |
48 | keybuffer->tail = 0; |
48 | keybuffer->tail = 0; |
49 | keybuffer->items = 0; |
49 | keybuffer->items = 0; |
50 | futex_up(&keybuffer_futex); |
50 | futex_up(&keybuffer_futex); |
51 | } |
51 | } |
52 | 52 | ||
53 | /** Key buffer initialization. |
53 | /** Key buffer initialization. |
54 | * |
54 | * |
55 | */ |
55 | */ |
56 | void keybuffer_init(keybuffer_t *keybuffer) |
56 | void keybuffer_init(keybuffer_t *keybuffer) |
57 | { |
57 | { |
58 | keybuffer_free(keybuffer); |
58 | keybuffer_free(keybuffer); |
59 | } |
59 | } |
60 | 60 | ||
61 | /** Get free space in buffer. |
61 | /** Get free space in buffer. |
62 | * |
62 | * |
63 | * This function is useful for processing some scancodes that are translated |
63 | * This function is useful for processing some scancodes that are translated |
64 | * to more than one character. |
64 | * to more than one character. |
65 | * |
65 | * |
66 | * @return empty buffer space |
66 | * @return empty buffer space |
67 | * |
67 | * |
68 | */ |
68 | */ |
69 | size_t keybuffer_available(keybuffer_t *keybuffer) |
69 | size_t keybuffer_available(keybuffer_t *keybuffer) |
70 | { |
70 | { |
71 | return KEYBUFFER_SIZE - keybuffer->items; |
71 | return KEYBUFFER_SIZE - keybuffer->items; |
72 | } |
72 | } |
73 | 73 | ||
74 | /** |
74 | /** |
75 | * |
75 | * |
76 | * @return nonzero, if buffer is not empty. |
76 | * @return nonzero, if buffer is not empty. |
77 | * |
77 | * |
78 | */ |
78 | */ |
79 | bool keybuffer_empty(keybuffer_t *keybuffer) |
79 | bool keybuffer_empty(keybuffer_t *keybuffer) |
80 | { |
80 | { |
81 | return (keybuffer->items == 0); |
81 | return (keybuffer->items == 0); |
82 | } |
82 | } |
83 | 83 | ||
84 | /** Push key event to key buffer. |
84 | /** Push key event to key buffer. |
85 | * |
85 | * |
86 | * If the buffer is full, the event is ignored. |
86 | * If the buffer is full, the event is ignored. |
87 | * |
87 | * |
88 | * @param keybuffer The keybuffer. |
88 | * @param keybuffer The keybuffer. |
89 | * @param ev The event to push. |
89 | * @param ev The event to push. |
90 | * |
90 | * |
91 | */ |
91 | */ |
92 | void keybuffer_push(keybuffer_t *keybuffer, const console_event_t *ev) |
92 | void keybuffer_push(keybuffer_t *keybuffer, const console_event_t *ev) |
93 | { |
93 | { |
94 | futex_down(&keybuffer_futex); |
94 | futex_down(&keybuffer_futex); |
95 | 95 | ||
96 | if (keybuffer->items < KEYBUFFER_SIZE) { |
96 | if (keybuffer->items < KEYBUFFER_SIZE) { |
97 | keybuffer->fifo[keybuffer->tail] = *ev; |
97 | keybuffer->fifo[keybuffer->tail] = *ev; |
98 | keybuffer->tail = (keybuffer->tail + 1) % KEYBUFFER_SIZE; |
98 | keybuffer->tail = (keybuffer->tail + 1) % KEYBUFFER_SIZE; |
99 | keybuffer->items++; |
99 | keybuffer->items++; |
100 | } |
100 | } |
101 | 101 | ||
102 | futex_up(&keybuffer_futex); |
102 | futex_up(&keybuffer_futex); |
103 | } |
103 | } |
104 | 104 | ||
105 | /** Pop event from buffer. |
105 | /** Pop event from buffer. |
106 | * |
106 | * |
107 | * @param edst Pointer to where the event should be saved. |
107 | * @param edst Pointer to where the event should be saved. |
108 | * |
108 | * |
109 | * @return True if an event was popped. |
109 | * @return True if an event was popped. |
110 | * |
110 | * |
111 | */ |
111 | */ |
112 | bool keybuffer_pop(keybuffer_t *keybuffer, console_event_t *edst) |
112 | bool keybuffer_pop(keybuffer_t *keybuffer, console_event_t *edst) |
113 | { |
113 | { |
114 | futex_down(&keybuffer_futex); |
114 | futex_down(&keybuffer_futex); |
115 | 115 | ||
116 | if (keybuffer->items > 0) { |
116 | if (keybuffer->items > 0) { |
117 | keybuffer->items--; |
117 | keybuffer->items--; |
118 | *edst = (keybuffer->fifo[keybuffer->head]); |
118 | *edst = (keybuffer->fifo[keybuffer->head]); |
119 | keybuffer->head = (keybuffer->head + 1) % KEYBUFFER_SIZE; |
119 | keybuffer->head = (keybuffer->head + 1) % KEYBUFFER_SIZE; |
120 | futex_up(&keybuffer_futex); |
120 | futex_up(&keybuffer_futex); |
121 | 121 | ||
122 | return true; |
122 | return true; |
123 | } |
123 | } |
124 | 124 | ||
125 | futex_up(&keybuffer_futex); |
125 | futex_up(&keybuffer_futex); |
126 | 126 | ||
127 | return false; |
127 | return false; |
128 | } |
128 | } |
129 | 129 | ||
130 | /** |
130 | /** |
131 | * @} |
131 | * @} |
132 | */ |
132 | */ |
133 | 133 |