428971d5a6a6ecfcb51c0e19e88d3ed024c23ab6
[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 keysym_rfbtodcpu(key, &dcpu_key);
74
75 fprintf(stderr, "%s: down:%u key:0x%04x dcpu_key:0x%04x\n",
76 __func__,
77 down,
78 key,
79 dcpu_key);
80
81 fprintf(stderr, "%s: hw:%p name:%s vm:%p\n",
82 __func__,
83 VOIDP(hw),
84 hw->name_,
85 VOIDP(hw->vm));
86
87 keyboard->keys_pressed[dcpu_key] = (down ? 1 : 0);
88 if (down) {
89 if ((keyboard->buf_tail + 1) % keyboard->buf_sz == keyboard->buf_head) {
90 hw->vm->warn_cb_("keyboard buffer overflow");
91 return;
92 }
93 keyboard->buf[keyboard->buf_tail] = dcpu_key;
94 keyboard->buf_tail += 1;
95 keyboard->buf_tail %= keyboard->buf_sz;
96 }
97 if (keyboard->interrupt_message) {
98 dcpu16_interrupt(hw->vm, keyboard->interrupt_message);
99 }
100 }
101
102 void keyboard_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr rfbScreen) {
103 rfbScreen->screenData = hw;
104 rfbScreen->kbdAddEvent = keyboard_rfbevent_;
105 }
106 #endif /* HAVE_LIBVNCSERVER */
107
108 static
109 void keyboard_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
110 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
111
112 (void)vm;
113
114 keyboard->interrupt_message = 0;
115 memset(keyboard->buf, 0, keyboard->buf_sz);
116 }
117
118 static
119 void keyboard_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
120 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
121
122 (void)vm, (void)keyboard;
123 }
124
125 static
126 void keyboard_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
127 struct keyboard_ *keyboard = (struct keyboard_ *)hw->data;
128 DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A];
129 DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B];
130
131 switch (reg_a) {
132 case 0: /* clear keyboard buffer */
133 memset(keyboard->buf, 0, keyboard->buf_sz);
134 keyboard->buf_head = 0;
135 keyboard->buf_tail = 0;
136 break;
137
138 case 1: /* get next key from buffer as C */
139 if (keyboard->buf_head == keyboard->buf_tail)
140 break;
141 vm->reg[DCPU16_REG_C] = keyboard->buf[keyboard->buf_head];
142 keyboard->buf_head += 1;
143 keyboard->buf_head %= keyboard->buf_sz;
144 break;
145
146 case 2: /* get currently-pressed-state of key in B as C */
147 vm->reg[DCPU16_REG_C] = keyboard->keys_pressed[reg_b & 0x00ff];
148 break;
149
150 case 3: /* set interrupt state */
151 keyboard->interrupt_message = reg_b;
152 break;
153
154 }
155 }
156
157 static
158 int keyboard_data_init_(struct dcpu16_hw *hw, void *data) {
159 size_t buf_sz = data ? *(size_t *)data : BUF_SZ;
160
161 hw->data = calloc(1, sizeof(struct keyboard_));
162 if (hw->data == NULL) {
163 hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno));
164 return -1;
165 }
166
167 ((struct keyboard_ *)(hw->data))->buf = malloc(buf_sz * sizeof *((struct keyboard_ *)(hw->data))->buf);
168 if (((struct keyboard_ *)(hw->data))->buf == NULL) {
169 hw->vm->warn_cb_("%s():%s", "malloc", strerror(errno));
170 free(hw->data);
171 hw->data = NULL;
172 return -1;
173 }
174
175 ((struct keyboard_ *)(hw->data))->buf_sz = buf_sz;
176
177 return 0;
178 }
179
180 static
181 void keyboard_data_free_(struct dcpu16_hw *hw) {
182 if (hw) {
183 if (hw->data) {
184 if (((struct keyboard_ *)(hw->data))->buf) {
185 free(((struct keyboard_ *)(hw->data))->buf);
186 ((struct keyboard_ *)(hw->data))->buf = NULL;
187 }
188 free(hw->data);
189 hw->data = NULL;
190 }
191 }
192 }
193
194 static struct dcpu16_hw hw_ = {
195 .vm = NULL,
196 .name_ = "Generic Keyboard (compatible)",
197 .id_l = 0x7406,
198 .id_h = 0x30cf,
199 .ver = 0x0001,
200 .mfg_l = 0x0000,
201 .mfg_h = 0x0000,
202 .hwi = keyboard_hwi_,
203 .cycle = keyboard_cycle_,
204 .reset = keyboard_reset_,
205 .data = (struct keyboard_ *)NULL
206 };
207
208 struct dcpu16_hw_module dcpu16_hw_module_keyboard = {
209 .template = &hw_,
210 .data_init = keyboard_data_init_,
211 .data_free = keyboard_data_free_,
212 };