consolidated log message callbacks
[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 #ifdef DEBUG
74 hw->vm->msg_cb_(MSG_DEBUG, "%s>> down:%u rfb_key:0x%04x", down, key);
75 #endif /* DEBUG */
76
77 if (keysym_rfbtodcpu(key, &dcpu_key)) {
78 /* unhandled key event */
79 return;
80 }
81
82 keyboard->keys_pressed[dcpu_key] = (down ? 1 : 0);
83 if (down) {
84 if ((keyboard->buf_tail + 1) % keyboard->buf_sz == keyboard->buf_head) {
85 hw->vm->msg_cb_(MSG_INFO, "keyboard buffer overflow");
86 return;
87 }
88 keyboard->buf[keyboard->buf_tail] = dcpu_key;
89 keyboard->buf_tail += 1;
90 keyboard->buf_tail %= keyboard->buf_sz;
91 }
92 if (keyboard->interrupt_message) {
93 dcpu16_interrupt(hw->vm, keyboard->interrupt_message);
94 }
95 }
96 #endif /* HAVE_LIBVNCSERVER */
97
98 static
99 void keyboard_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
100 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
101
102 (void)vm;
103
104 keyboard->interrupt_message = 0;
105 keyboard->buf_head = 0;
106 keyboard->buf_tail = 0;
107 memset(keyboard->keys_pressed, 0, sizeof keyboard->keys_pressed);
108 }
109
110 static
111 void keyboard_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
112 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
113
114 (void)vm, (void)keyboard;
115 }
116
117 static
118 void keyboard_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
119 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
120 DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A];
121 DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B];
122
123 switch (reg_a) {
124 case 0: /* clear keyboard buffer */
125 memset(keyboard->buf, 0, keyboard->buf_sz);
126 keyboard->buf_head = 0;
127 keyboard->buf_tail = 0;
128 break;
129
130 case 1: /* get next key from buffer as C */
131 if (keyboard->buf_head == keyboard->buf_tail)
132 break;
133 vm->reg[DCPU16_REG_C] = keyboard->buf[keyboard->buf_head];
134 keyboard->buf_head += 1;
135 keyboard->buf_head %= keyboard->buf_sz;
136 break;
137
138 case 2: /* get currently-pressed-state of key in B as C */
139 vm->reg[DCPU16_REG_C] = keyboard->keys_pressed[reg_b & 0x00ff];
140 break;
141
142 case 3: /* set interrupt state */
143 keyboard->interrupt_message = reg_b;
144 break;
145
146 }
147 }
148
149 static
150 int keyboard_data_init_(struct dcpu16_hw *hw, void *data) {
151 size_t buf_sz = data ? *(size_t *)data : BUF_SZ;
152
153 hw->data = calloc(1, sizeof(struct keyboard_));
154 if (hw->data == NULL) {
155 hw->vm->msg_cb_(MSG_ERROR, "%s():%s", "calloc", strerror(errno));
156 return -1;
157 }
158
159 ((struct keyboard_ *)(hw->data))->buf = malloc(buf_sz * sizeof *((struct keyboard_ *)(hw->data))->buf);
160 if (((struct keyboard_ *)(hw->data))->buf == NULL) {
161 hw->vm->msg_cb_(MSG_ERROR, "%s():%s", "malloc", strerror(errno));
162 free(hw->data);
163 hw->data = NULL;
164 return -1;
165 }
166
167 ((struct keyboard_ *)(hw->data))->buf_sz = buf_sz;
168
169 return 0;
170 }
171
172 static
173 void keyboard_data_free_(struct dcpu16_hw *hw) {
174 if (hw) {
175 if (hw->data) {
176 if (((struct keyboard_ *)(hw->data))->buf) {
177 free(((struct keyboard_ *)(hw->data))->buf);
178 ((struct keyboard_ *)(hw->data))->buf = NULL;
179 }
180 free(hw->data);
181 hw->data = NULL;
182 }
183 }
184 }
185
186 static struct dcpu16_hw_ctl_cmd ctl_[] = {
187 { "buffer_size", "const size_t *", "size_t *", "get or set current buffer size" },
188 { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this keyboard instance with an rfb display" },
189 { NULL, NULL, NULL, NULL }
190 };
191 static
192 int keyboard_data_ctl_(struct dcpu16_hw *hw, const char *cmd, void *data_in, void *data_out) {
193 if (strcmp(cmd, "buffer_size") == 0) {
194 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
195 void *tmp_ptr;
196 const size_t *buf_sz_in = (const size_t *)data_in;
197 size_t *buf_sz_out = (size_t *)data_out;
198
199 if (buf_sz_out) {
200 *buf_sz_out = keyboard->buf_sz;
201 }
202
203 if (buf_sz_in) {
204 #ifdef DEBUG
205 hw->vm->msg_cb_(MSG_DEBUG, "%s>> resizing buffer from %zu to %zu", __func__, keyboard->buf_sz, *buf_sz_in);
206 #endif /* DEBUG */
207
208 tmp_ptr = realloc(keyboard->buf, *buf_sz_in);
209 if (tmp_ptr == NULL) {
210 hw->vm->msg_cb_(MSG_DEBUG, "%s():%s", "realloc", strerror(errno));
211 return -1;
212 }
213 keyboard->buf = tmp_ptr;
214 keyboard->buf_sz = *buf_sz_in;
215 keyboard->buf_head = keyboard->buf_tail = 0;
216 }
217
218 return 0;
219 }
220
221 #ifdef HAVE_LIBVNCSERVER
222 if (strcmp(cmd, "associate_rfbScreen") == 0) {
223 rfbScreenInfoPtr rfbScreen = (rfbScreenInfoPtr)data_in;
224 (void)data_out;
225
226 if (rfbScreen == NULL)
227 return -EFAULT;
228
229 rfbScreen->screenData = hw;
230 rfbScreen->kbdAddEvent = keyboard_rfbevent_;
231
232 return 0;
233 }
234 #endif /* HAVE_LIBVNCSERVER */
235
236 return -EINVAL;
237 }
238
239
240 struct dcpu16_hw_module dcpu16_hw_module_keyboard = {
241 .name_ = "Generic Keyboard (compatible)",
242
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
252 .data_init = keyboard_data_init_,
253 .data_free = keyboard_data_free_,
254 .ctl = keyboard_data_ctl_,
255 .ctl_cmd = ctl_,
256 };