summaryrefslogtreecommitdiff
path: root/src/client/keyboardBuffer.c
blob: 894060f02d76ea7d4e28e2e6b94f8df61ef15037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * keyboardBuffer.c
 *
 *  Created on: 1. 8. 2008
 *      Author: Karel Podvolecky
 *
 *  Simple front of pressed keys.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <SDL.h>

#include "keyboardBuffer.h"

static keyboardBuffer_t *keyboardBuffer = NULL;

static SDL_keysym emptyKey;

/*
 * Key buffer initializes. The parameter sets the number of keys.
 */
void
initKeyboardBuffer(int size)
{
   assert(size > 0);
   assert(keyboardBuffer == NULL);

   keyboardBuffer = malloc(sizeof(keyboardBuffer_t));
   assert(keyboardBuffer != NULL);
   memset(keyboardBuffer, 0, sizeof(keyboardBuffer_t));

   keyboardBuffer->buff = malloc(size * sizeof(SDL_keysym));
   assert(keyboardBuffer->buff != NULL);
   memset(keyboardBuffer->buff, 0, size * sizeof(SDL_keysym));

   keyboardBuffer->begin = 0;
   keyboardBuffer->end = 0;
   keyboardBuffer->count = 0;
   keyboardBuffer->size = size;

   emptyKey.sym = SDLK_UNKNOWN;
}

/*
 * Empty the buffer
 */
void
clearKeyboardBuffer()
{
   assert(keyboardBuffer != NULL);

   keyboardBuffer->begin = 0;
   keyboardBuffer->end = 0;
   keyboardBuffer->count = 0;
   memset(keyboardBuffer->buff, 0, keyboardBuffer->size * sizeof(SDL_keysym));
}

/*
 * Adds key to the end of the buffer. If the buffer is overrun,
 * an error is printed to stderr and the key is dropped.
 */
bool_t
pushKeyToKeyboardBuffer(SDL_keysym key)
{
   assert(keyboardBuffer != NULL);

   if (keyboardBuffer->count >= keyboardBuffer->size) {
      fprintf(stderr, _("Keyboard Buffer overrun! Dropping key: %02x\n"),
              key.sym);
      return FALSE;
   }

   keyboardBuffer->buff[keyboardBuffer->end] = key;

   keyboardBuffer->end++;
   if (keyboardBuffer->end >= keyboardBuffer->size) {
      keyboardBuffer->end = 0;
   }

   keyboardBuffer->count++;

   return TRUE;
}

/*
 * Takes out first key from the buffer and returns it. If the buffer is empty,
 * an error is printed to stderr and SDLK_UNKNOWN is returned.
 */
SDL_keysym
popKeyFromKeyboardBuffer()
{
   assert(keyboardBuffer != NULL);

   if (keyboardBuffer->count <= 0) {
      fprintf(stderr, _("Keyboard Buffer underrun!\n"));
      return emptyKey;
   }

   SDL_keysym key = keyboardBuffer->buff[keyboardBuffer->begin];
   keyboardBuffer->buff[keyboardBuffer->begin] = emptyKey;

   keyboardBuffer->begin++;
   if (keyboardBuffer->begin >= keyboardBuffer->size) {
      keyboardBuffer->begin = 0;
   }

   keyboardBuffer->count--;

   return key;
}

/*
 * The buffer size
 */
int
getKeyboardBufferSize()
{
   assert(keyboardBuffer != NULL);
   return keyboardBuffer->size;
}

/*
 * Number of keys in the buffer
 */
int
KeyboardBufferCount()
{
   assert(keyboardBuffer != NULL);
   return keyboardBuffer->count;
}


bool_t
isAnyKeyInKeyboardBuffer()
{
   return KeyboardBufferCount() > 0 ? TRUE : FALSE;
}

/*
 * Frees the buffer. If it is not empty, information
 * about filling is prined to stderr first.
 */
void
quitKeyboardBuffer()
{
   assert(keyboardBuffer != NULL);

   if (keyboardBuffer->count > 0) {
      fprintf(stderr, _("Keyboard buffer is not empty!\n"));
   }

   free(keyboardBuffer->buff);
   free(keyboardBuffer);
   keyboardBuffer = NULL;
}