Rev 1392 | Rev 1476 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1392 | Rev 1451 | ||
---|---|---|---|
Line 25... | Line 25... | ||
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 | #include <key_buffer.h> |
29 | #include <key_buffer.h> |
30 | #include <libadt/fifo.h> |
- | |
31 | - | ||
32 | #define KBD_BUFFER_SIZE 128 /**< Size of buffer for pressed keys */ |
- | |
33 | - | ||
34 | FIFO_INITIALIZE_STATIC(buffer, char, KBD_BUFFER_SIZE); /**< Fifo for storing pressed keys */ |
- | |
35 | fifo_count_t buffer_items; /**< Counter of used items for prevent fifo overflow */ |
- | |
36 | 30 | ||
37 | /** Clear key buffer. |
31 | /** Clear key buffer. |
38 | */ |
32 | */ |
39 | void key_buffer_free(void) |
33 | void keybuffer_free(keybuffer_t *keybuffer) |
40 | { |
34 | { |
- | 35 | ||
41 | buffer_items = 0; |
36 | keybuffer->items = 0; |
42 | buffer.head = buffer.tail = 0; |
37 | keybuffer->head = keybuffer->tail = keybuffer->items = 0; |
43 | } |
38 | } |
44 | 39 | ||
45 | /** Key buffer initialization. |
40 | /** Key buffer initialization. |
46 | * |
41 | * |
47 | */ |
42 | */ |
48 | void key_buffer_init(void) |
43 | void keybuffer_init(keybuffer_t *keybuffer) |
49 | { |
44 | { |
50 | key_buffer_free(); |
45 | keybuffer_free(keybuffer); |
51 | } |
46 | } |
52 | 47 | ||
53 | /** Get free space in buffer. |
48 | /** Get free space in buffer. |
54 | * This function is useful for processing some scancodes that are translated |
49 | * This function is useful for processing some scancodes that are translated |
55 | * to more than one character. |
50 | * to more than one character. |
56 | * @return empty buffer space |
51 | * @return empty buffer space |
57 | */ |
52 | */ |
58 | int key_buffer_available(void) |
53 | int keybuffer_available(keybuffer_t *keybuffer) |
59 | { |
54 | { |
60 | return KBD_BUFFER_SIZE - buffer_items; |
55 | return KEYBUFFER_SIZE - keybuffer->items; |
61 | } |
56 | } |
62 | 57 | ||
63 | /** |
58 | /** |
64 | * @return nonzero, if buffer is not empty. |
59 | * @return nonzero, if buffer is not empty. |
65 | */ |
60 | */ |
66 | int key_buffer_empty(void) |
61 | int keybuffer_empty(keybuffer_t *keybuffer) |
67 | { |
62 | { |
68 | return (buffer_items == 0); |
63 | return (keybuffer->items == 0); |
69 | } |
64 | } |
70 | 65 | ||
71 | /** Push key to key buffer. |
66 | /** Push key to key buffer. |
72 | * If buffer is full, character is ignored. |
67 | * If buffer is full, character is ignored. |
73 | * @param key code of stored key |
68 | * @param key code of stored key |
74 | */ |
69 | */ |
75 | void key_buffer_push(char key) |
70 | void keybuffer_push(keybuffer_t *keybuffer, char key) |
76 | { |
71 | { |
77 | if (buffer_items < KBD_BUFFER_SIZE) { |
72 | if (keybuffer->items < KEYBUFFER_SIZE) { |
78 | fifo_push(buffer, key); |
73 | keybuffer->fifo[keybuffer->tail = (keybuffer->tail + 1) < keybuffer->items ? (keybuffer->tail + 1) : 0] = (key); |
79 | buffer_items++; |
74 | keybuffer->items++; |
80 | } |
75 | } |
81 | } |
76 | } |
82 | 77 | ||
83 | /** Pop character from buffer. |
78 | /** Pop character from buffer. |
84 | * @param c pointer to space where to store character from buffer. |
79 | * @param c pointer to space where to store character from buffer. |
85 | * @return zero on empty buffer, nonzero else |
80 | * @return zero on empty buffer, nonzero else |
86 | */ |
81 | */ |
87 | int key_buffer_pop(char *c) |
82 | int keybuffer_pop(keybuffer_t *keybuffer, char *c) |
88 | { |
83 | { |
89 | if (buffer_items > 0) { |
84 | if (keybuffer->items > 0) { |
90 | buffer_items--; |
85 | keybuffer->items--; |
91 | *c = fifo_pop(buffer); |
86 | *c = keybuffer->fifo[keybuffer->head = (keybuffer->head + 1) < keybuffer->items ? (keybuffer->head + 1) : 0]; |
92 | return 1; |
87 | return 1; |
93 | } |
88 | } |
94 | return 0; |
89 | return 0; |
95 | } |
90 | } |
96 | 91 |