further reorg of module abstraction and control interface
[dcpu16] / hw_keyboard.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4
5 #ifdef HAVE_LIBVNCSERVER
6 #include <rfb/rfb.h>
7 #include <rfb/keysym.h>
8 #endif /* HAVE_LIBVNCSERVER */
9
10 #include "dcpu16.h"
11 #include "hw_keyboard.h"
12
13 #define BUF_SZ 32
14
15 #ifdef WANT_VARIADIC_VOIDP_CAST
16 #define VOIDP(__x__) ((void *)(__x__))
17 #else
18 #define VOIDP(__x__) (__x__)
19 #endif
20
21 struct keyboard_ {
22 char *buf;
23 size_t buf_sz;
24 size_t buf_head;
25 size_t buf_tail;
26 DCPU16_WORD interrupt_message;
27 unsigned char keys_pressed[256];
28 };
29
30 #ifdef HAVE_LIBVNCSERVER
31
32 static const struct keysym_map_ {
33 unsigned int rfb_start;
34 unsigned int rfb_end;
35 DCPU16_WORD dcpu_start;
36 } keymap_[] = {
37 { XK_space, XK_asciitilde, 0x20 }, /* ASCII range */
38 { XK_Delete, XK_Delete, 0x7f }, /* ASCII del */
39 { XK_BackSpace, XK_BackSpace, 0x10 }, /* bs */
40 { XK_Return, XK_Return, 0x11 }, /* ret */
41 { XK_Insert, XK_Insert, 0x12 }, /* ins */
42 { XK_Delete, XK_Delete, 0x13 }, /* del */
43 { XK_Up, XK_Up, 0x80 }, /* arrow up */
44 { XK_Down, XK_Down, 0x81 }, /* arrow down */
45 { XK_Left, XK_Left, 0x82 }, /* arrow left */
46 { XK_Right, XK_Right, 0x83 }, /* arrow right */
47 { XK_Shift_L, XK_Shift_R, 0x90 }, /* shift range */
48 { XK_Control_L, XK_Control_R, 0x91 }, /* control range */
49 { 0, 0, 0x0 }
50 };
51
52 static int keysym_rfbtodcpu(unsigned int rfb, DCPU16_WORD *dcpu) {
53 const struct keysym_map_ *map;
54
55 for (map = keymap_; map->rfb_start; map++) {
56 if (rfb >= map->rfb_start
57 && rfb <= map->rfb_end) {
58 *dcpu = (map->dcpu_start + (rfb - map->rfb_start));
59 return 0;
60 }
61 }
62
63 *dcpu = 0x00;
64 return -1;
65 }
66
67 static
68 void keyboard_rfbevent_(rfbBool down, rfbKeySym key, rfbClientPtr cl) {
69 DCPU16_WORD dcpu_key;
70 struct dcpu16_hw *hw = (struct dcpu16_hw *)cl->screen->screenData;
71 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
72
73 hw->vm->trace_cb_("%s>> down:%u rfb_key:0x%04x", down, key);
74
75 if (keysym_rfbtodcpu(key, &dcpu_key)) {
76 /* unhandled key event */
77 return;
78 }
79
80 keyboard->keys_pressed[dcpu_key] = (down ? 1 : 0);
81 if (down) {
82 if ((keyboard->buf_tail + 1) % keyboard->buf_sz == keyboard->buf_head) {
83 hw->vm->warn_cb_("keyboard buffer overflow");
84 return;
85 }
86 keyboard->buf[keyboard->buf_tail] = dcpu_key;
87 keyboard->buf_tail += 1;
88 keyboard->buf_tail %= keyboard->buf_sz;
89 }
90 if (keyboard->interrupt_message) {
91 dcpu16_interrupt(hw->vm, keyboard->interrupt_message);
92 }
93 }
94
95 void keyboard_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr rfbScreen) {
96 rfbScreen->screenData = hw;
97 rfbScreen->kbdAddEvent = keyboard_rfbevent_;
98 }
99 #endif /* HAVE_LIBVNCSERVER */
100
101 static
102 void keyboard_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
103 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
104
105 (void)vm;
106
107 keyboard->interrupt_message = 0;
108 keyboard->buf_head = 0;
109 keyboard->buf_tail = 0;
110 memset(keyboard->keys_pressed, 0, sizeof keyboard->keys_pressed);
111 }
112
113 static
114 void keyboard_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
115 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
116
117 (void)vm, (void)keyboard;
118 }
119
120 static
121 void keyboard_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
122 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
123 DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A];
124 DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B];
125
126 switch (reg_a) {
127 case 0: /* clear keyboard buffer */
128 memset(keyboard->buf, 0, keyboard->buf_sz);
129 keyboard->buf_head = 0;
130 keyboard->buf_tail = 0;
131 break;
132
133 case 1: /* get next key from buffer as C */
134 if (keyboard->buf_head == keyboard->buf_tail)
135 break;
136 vm->reg[DCPU16_REG_C] = keyboard->buf[keyboard->buf_head];
137 keyboard->buf_head += 1;
138 keyboard->buf_head %= keyboard->buf_sz;
139 break;
140
141 case 2: /* get currently-pressed-state of key in B as C */
142 vm->reg[DCPU16_REG_C] = keyboard->keys_pressed[reg_b & 0x00ff];
143 break;
144
145 case 3: /* set interrupt state */
146 keyboard->interrupt_message = reg_b;
147 break;
148
149 }
150 }
151
152 static
153 int keyboard_data_init_(struct dcpu16_hw *hw, void *data) {
154 size_t buf_sz = data ? *(size_t *)data : BUF_SZ;
155
156 hw->data = calloc(1, sizeof(struct keyboard_));
157 if (hw->data == NULL) {
158 hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno));
159 return -1;
160 }
161
162 ((struct keyboard_ *)(hw->data))->buf = malloc(buf_sz * sizeof *((struct keyboard_ *)(hw->data))->buf);
163 if (((struct keyboard_ *)(hw->data))->buf == NULL) {
164 hw->vm->warn_cb_("%s():%s", "malloc", strerror(errno));
165 free(hw->data);
166 hw->data = NULL;
167 return -1;
168 }
169
170 ((struct keyboard_ *)(hw->data))->buf_sz = buf_sz;
171
172 return 0;
173 }
174
175 static
176 void keyboard_data_free_(struct dcpu16_hw *hw) {
177 if (hw) {
178 if (hw->data) {
179 if (((struct keyboard_ *)(hw->data))->buf) {
180 free(((struct keyboard_ *)(hw->data))->buf);
181 ((struct keyboard_ *)(hw->data))->buf = NULL;
182 }
183 free(hw->data);
184 hw->data = NULL;
185 }
186 }
187 }
188
189 static struct dcpu16_hw_ctl_cmd ctl_[] = {
190 { "buffer_size", "const size_t *", "size_t *", "get or set current buffer size" },
191 { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this keyboard instance with an rfb display" },
192 { NULL, NULL, NULL, NULL }
193 };
194 static
195 int keyboard_data_ctl_(struct dcpu16_hw *hw, const char *cmd, void *data_in, void *data_out) {
196 if (strcmp(cmd, "buffer_size") == 0) {
197 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
198 void *tmp_ptr;
199 const size_t *buf_sz_in = (const size_t *)data_in;
200 size_t *buf_sz_out = (size_t *)data_out;
201
202 if (buf_sz_out) {
203 *buf_sz_out = keyboard->buf_sz;
204 }
205
206 if (buf_sz_in) {
207 hw->vm->trace_cb_("%s>> resizing buffer from %zu to %zu", __func__, keyboard->buf_sz, *buf_sz_in);
208
209 tmp_ptr = realloc(keyboard->buf, *buf_sz_in);
210 if (tmp_ptr == NULL) {
211 hw->vm->warn_cb_("%s():%s", "realloc", strerror(errno));
212 return -1;
213 }
214 keyboard->buf = tmp_ptr;
215 keyboard->buf_sz = *buf_sz_in;
216 keyboard->buf_head = keyboard->buf_tail = 0;
217 }
218
219 return 0;
220 }
221
222 #ifdef HAVE_LIBVNCSERVER
223 if (strcmp(cmd, "associate_rfbScreen") == 0) {
224 rfbScreenInfoPtr rfbScreen = (rfbScreenInfoPtr)data_in;
225 (void)data_out;
226
227 if (rfbScreen == NULL)
228 return -EFAULT;
229
230 rfbScreen->screenData = hw;
231 rfbScreen->kbdAddEvent = keyboard_rfbevent_;
232
233 return 0;
234 }
235 #endif /* HAVE_LIBVNCSERVER */
236
237 return -EINVAL;
238 }
239
240 static struct dcpu16_hw hw_ = {
241 .vm = NULL,
242 .name_ = "Generic Keyboard (compatible)",
243 .id_l = 0x7406,
244 .id_h = 0x30cf,
245 .ver = 0x0001,
246 .mfg_l = 0x0000,
247 .mfg_h = 0x0000,
248 .hwi = keyboard_hwi_,
249 .cycle = keyboard_cycle_,
250 .reset = keyboard_reset_,
251 .data = (struct keyboard_ *)NULL
252 };
253
254 struct dcpu16_hw_module dcpu16_hw_module_keyboard = {
255 .template = &hw_,
256 .data_init = keyboard_data_init_,
257 .data_free = keyboard_data_free_,
258 .ctl = keyboard_data_ctl_,
259 .ctl_cmd = ctl_,
260 };