-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.c
409 lines (326 loc) · 12.6 KB
/
keyboard.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* keyboard.c: Routines for dealing with the Spectrum's keyboard
Copyright (c) 1999-2016 Philip Kendall
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#ifdef HAVE_LIB_GLIB
#include <glib.h>
#endif /* #ifdef HAVE_LIB_GLIB */
#include <libspectrum.h>
#include "infrastructure/startup_manager.h"
#include "keyboard.h"
#include "ui/ui.h"
/* Bit masks for each of the eight keyboard half-rows; `AND' the selected
ones of these to get the value to return
*/
libspectrum_byte keyboard_return_values[8];
/* The hash used for storing the UI -> Fuse input layer key mappings */
static GHashTable *keysyms_hash;
struct spectrum_keys_wrapper {
input_key input;
keyboard_spectrum_keys_t spectrum;
};
/* Which Spectrum keys should be pressed for each key passed from the
Fuse input layer */
static struct spectrum_keys_wrapper spectrum_keys_table[] = {
{ INPUT_KEY_Escape, { KEYBOARD_1, KEYBOARD_Caps } },
{ INPUT_KEY_1, { KEYBOARD_1, KEYBOARD_NONE } },
{ INPUT_KEY_2, { KEYBOARD_2, KEYBOARD_NONE } },
{ INPUT_KEY_3, { KEYBOARD_3, KEYBOARD_NONE } },
{ INPUT_KEY_4, { KEYBOARD_4, KEYBOARD_NONE } },
{ INPUT_KEY_5, { KEYBOARD_5, KEYBOARD_NONE } },
{ INPUT_KEY_6, { KEYBOARD_6, KEYBOARD_NONE } },
{ INPUT_KEY_7, { KEYBOARD_7, KEYBOARD_NONE } },
{ INPUT_KEY_8, { KEYBOARD_8, KEYBOARD_NONE } },
{ INPUT_KEY_9, { KEYBOARD_9, KEYBOARD_NONE } },
{ INPUT_KEY_0, { KEYBOARD_0, KEYBOARD_NONE } },
{ INPUT_KEY_minus, { KEYBOARD_j, KEYBOARD_Symbol } },
{ INPUT_KEY_equal, { KEYBOARD_l, KEYBOARD_Symbol } },
{ INPUT_KEY_BackSpace, { KEYBOARD_0, KEYBOARD_Caps } },
{ INPUT_KEY_Tab, { KEYBOARD_Caps, KEYBOARD_Symbol } },
{ INPUT_KEY_q, { KEYBOARD_q, KEYBOARD_NONE } },
{ INPUT_KEY_w, { KEYBOARD_w, KEYBOARD_NONE } },
{ INPUT_KEY_e, { KEYBOARD_e, KEYBOARD_NONE } },
{ INPUT_KEY_r, { KEYBOARD_r, KEYBOARD_NONE } },
{ INPUT_KEY_t, { KEYBOARD_t, KEYBOARD_NONE } },
{ INPUT_KEY_y, { KEYBOARD_y, KEYBOARD_NONE } },
{ INPUT_KEY_u, { KEYBOARD_u, KEYBOARD_NONE } },
{ INPUT_KEY_i, { KEYBOARD_i, KEYBOARD_NONE } },
{ INPUT_KEY_o, { KEYBOARD_o, KEYBOARD_NONE } },
{ INPUT_KEY_p, { KEYBOARD_p, KEYBOARD_NONE } },
{ INPUT_KEY_Caps_Lock, { KEYBOARD_2, KEYBOARD_Caps } },
{ INPUT_KEY_a, { KEYBOARD_a, KEYBOARD_NONE } },
{ INPUT_KEY_s, { KEYBOARD_s, KEYBOARD_NONE } },
{ INPUT_KEY_d, { KEYBOARD_d, KEYBOARD_NONE } },
{ INPUT_KEY_f, { KEYBOARD_f, KEYBOARD_NONE } },
{ INPUT_KEY_g, { KEYBOARD_g, KEYBOARD_NONE } },
{ INPUT_KEY_h, { KEYBOARD_h, KEYBOARD_NONE } },
{ INPUT_KEY_j, { KEYBOARD_j, KEYBOARD_NONE } },
{ INPUT_KEY_k, { KEYBOARD_k, KEYBOARD_NONE } },
{ INPUT_KEY_l, { KEYBOARD_l, KEYBOARD_NONE } },
{ INPUT_KEY_semicolon, { KEYBOARD_o, KEYBOARD_Symbol } },
{ INPUT_KEY_apostrophe, { KEYBOARD_7, KEYBOARD_Symbol } },
{ INPUT_KEY_numbersign, { KEYBOARD_3, KEYBOARD_Symbol } },
{ INPUT_KEY_Return, { KEYBOARD_Enter, KEYBOARD_NONE } },
{ INPUT_KEY_Shift_L, { KEYBOARD_NONE, KEYBOARD_Caps } },
{ INPUT_KEY_z, { KEYBOARD_z, KEYBOARD_NONE } },
{ INPUT_KEY_x, { KEYBOARD_x, KEYBOARD_NONE } },
{ INPUT_KEY_c, { KEYBOARD_c, KEYBOARD_NONE } },
{ INPUT_KEY_v, { KEYBOARD_v, KEYBOARD_NONE } },
{ INPUT_KEY_b, { KEYBOARD_b, KEYBOARD_NONE } },
{ INPUT_KEY_n, { KEYBOARD_n, KEYBOARD_NONE } },
{ INPUT_KEY_m, { KEYBOARD_m, KEYBOARD_NONE } },
{ INPUT_KEY_comma, { KEYBOARD_n, KEYBOARD_Symbol } },
{ INPUT_KEY_period, { KEYBOARD_m, KEYBOARD_Symbol } },
{ INPUT_KEY_slash, { KEYBOARD_v, KEYBOARD_Symbol } },
{ INPUT_KEY_Shift_R, { KEYBOARD_NONE, KEYBOARD_Caps } },
{ INPUT_KEY_asterisk, { KEYBOARD_b, KEYBOARD_Symbol } },
{ INPUT_KEY_dollar, { KEYBOARD_4, KEYBOARD_Symbol } },
{ INPUT_KEY_exclam, { KEYBOARD_1, KEYBOARD_Symbol } },
{ INPUT_KEY_less, { KEYBOARD_r, KEYBOARD_Symbol } },
{ INPUT_KEY_parenright, { KEYBOARD_9, KEYBOARD_Symbol } },
{ INPUT_KEY_colon, { KEYBOARD_z, KEYBOARD_Symbol } },
{ INPUT_KEY_plus, { KEYBOARD_k, KEYBOARD_Symbol } },
{ INPUT_KEY_Control_L, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Alt_L, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Meta_L, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Super_L, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Hyper_L, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_space, { KEYBOARD_space, KEYBOARD_NONE } },
{ INPUT_KEY_Hyper_R, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Super_R, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Meta_R, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Alt_R, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Control_R, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Mode_switch, { KEYBOARD_NONE, KEYBOARD_Symbol } },
{ INPUT_KEY_Left, { KEYBOARD_5, KEYBOARD_NONE } },
{ INPUT_KEY_Down, { KEYBOARD_6, KEYBOARD_NONE } },
{ INPUT_KEY_Up, { KEYBOARD_7, KEYBOARD_NONE } },
{ INPUT_KEY_Right, { KEYBOARD_8, KEYBOARD_NONE } },
{ INPUT_KEY_KP_Enter, { KEYBOARD_Enter, KEYBOARD_NONE } },
{ INPUT_KEY_NONE, { KEYBOARD_NONE, KEYBOARD_NONE } } /* End marker */
};
static GHashTable *spectrum_keys;
/* When each Spectrum key is pressed, twiddle this {port,bit} pair
in `keyboard_return_values'.
*/
struct key_bit {
int port;
libspectrum_byte bit;
};
struct key_info {
keyboard_key_name key;
struct key_bit bit;
};
static struct key_info keyboard_data_table[] = {
{ KEYBOARD_1, { 3, 0x01 } },
{ KEYBOARD_2, { 3, 0x02 } },
{ KEYBOARD_3, { 3, 0x04 } },
{ KEYBOARD_4, { 3, 0x08 } },
{ KEYBOARD_5, { 3, 0x10 } },
{ KEYBOARD_6, { 4, 0x10 } },
{ KEYBOARD_7, { 4, 0x08 } },
{ KEYBOARD_8, { 4, 0x04 } },
{ KEYBOARD_9, { 4, 0x02 } },
{ KEYBOARD_0, { 4, 0x01 } },
{ KEYBOARD_q, { 2, 0x01 } },
{ KEYBOARD_w, { 2, 0x02 } },
{ KEYBOARD_e, { 2, 0x04 } },
{ KEYBOARD_r, { 2, 0x08 } },
{ KEYBOARD_t, { 2, 0x10 } },
{ KEYBOARD_y, { 5, 0x10 } },
{ KEYBOARD_u, { 5, 0x08 } },
{ KEYBOARD_i, { 5, 0x04 } },
{ KEYBOARD_o, { 5, 0x02 } },
{ KEYBOARD_p, { 5, 0x01 } },
{ KEYBOARD_a, { 1, 0x01 } },
{ KEYBOARD_s, { 1, 0x02 } },
{ KEYBOARD_d, { 1, 0x04 } },
{ KEYBOARD_f, { 1, 0x08 } },
{ KEYBOARD_g, { 1, 0x10 } },
{ KEYBOARD_h, { 6, 0x10 } },
{ KEYBOARD_j, { 6, 0x08 } },
{ KEYBOARD_k, { 6, 0x04 } },
{ KEYBOARD_l, { 6, 0x02 } },
{ KEYBOARD_Enter, { 6, 0x01 } },
{ KEYBOARD_Caps, { 0, 0x01 } },
{ KEYBOARD_z, { 0, 0x02 } },
{ KEYBOARD_x, { 0, 0x04 } },
{ KEYBOARD_c, { 0, 0x08 } },
{ KEYBOARD_v, { 0, 0x10 } },
{ KEYBOARD_b, { 7, 0x10 } },
{ KEYBOARD_n, { 7, 0x08 } },
{ KEYBOARD_m, { 7, 0x04 } },
{ KEYBOARD_Symbol, { 7, 0x02 } },
{ KEYBOARD_space, { 7, 0x01 } },
{ KEYBOARD_NONE, { 0, 0x00 } } /* End marker */
};
/* The hash used for storing keyboard_data_table */
static GHashTable *keyboard_data;
/* A textual name for each key */
struct key_text_t {
keyboard_key_name key;
const char *text;
};
struct key_text_t key_text_table[] = {
{ KEYBOARD_NONE, "Nothing" },
{ KEYBOARD_space, "Space" },
{ KEYBOARD_0, "0" },
{ KEYBOARD_1, "1" },
{ KEYBOARD_2, "2" },
{ KEYBOARD_3, "3" },
{ KEYBOARD_4, "4" },
{ KEYBOARD_5, "5" },
{ KEYBOARD_6, "6" },
{ KEYBOARD_7, "7" },
{ KEYBOARD_8, "8" },
{ KEYBOARD_9, "9" },
{ KEYBOARD_a, "A" },
{ KEYBOARD_b, "B" },
{ KEYBOARD_c, "C" },
{ KEYBOARD_d, "D" },
{ KEYBOARD_e, "E" },
{ KEYBOARD_f, "F" },
{ KEYBOARD_g, "G" },
{ KEYBOARD_h, "H" },
{ KEYBOARD_i, "I" },
{ KEYBOARD_j, "J" },
{ KEYBOARD_k, "K" },
{ KEYBOARD_l, "L" },
{ KEYBOARD_m, "M" },
{ KEYBOARD_n, "N" },
{ KEYBOARD_o, "O" },
{ KEYBOARD_p, "P" },
{ KEYBOARD_q, "Q" },
{ KEYBOARD_r, "R" },
{ KEYBOARD_s, "S" },
{ KEYBOARD_t, "T" },
{ KEYBOARD_u, "U" },
{ KEYBOARD_v, "V" },
{ KEYBOARD_w, "W" },
{ KEYBOARD_x, "X" },
{ KEYBOARD_y, "Y" },
{ KEYBOARD_z, "Z" },
{ KEYBOARD_Enter, "Enter" },
{ KEYBOARD_Caps, "Caps Shift" },
{ KEYBOARD_Symbol, "Symbol Shift" },
{ KEYBOARD_JOYSTICK_FIRE, "Joystick Fire" },
{ KEYBOARD_NONE, NULL }, /* End marker */
};
static GHashTable *key_text;
static int
keyboard_init( void *context )
{
struct key_info *ptr;
struct spectrum_keys_wrapper *ptr2;
keysyms_map_t *ptr3;
struct key_text_t *ptr4;
keyboard_release_all();
keyboard_data = g_hash_table_new( g_int_hash, g_int_equal );
for( ptr = keyboard_data_table; ptr->key != KEYBOARD_NONE; ptr++ )
g_hash_table_insert( keyboard_data, &( ptr->key ), &( ptr->bit ) );
spectrum_keys = g_hash_table_new( g_int_hash, g_int_equal );
for( ptr2 = spectrum_keys_table; ptr2->input != INPUT_KEY_NONE; ptr2++ )
g_hash_table_insert( spectrum_keys,
&( ptr2->input ), &( ptr2->spectrum ) );
keysyms_hash = g_hash_table_new( g_int_hash, g_int_equal );
for( ptr3 = keysyms_map; ptr3->ui; ptr3++ )
g_hash_table_insert( keysyms_hash, &( ptr3->ui ), &( ptr3->fuse ) );
key_text = g_hash_table_new( g_int_hash, g_int_equal );
for( ptr4 = key_text_table; ptr4->text != NULL; ptr4++ )
g_hash_table_insert( key_text, &( ptr4->key ), &( ptr4->text ) );
return 0;
}
static void
keyboard_end( void )
{
g_hash_table_destroy( keyboard_data );
g_hash_table_destroy( spectrum_keys );
g_hash_table_destroy( keysyms_hash );
g_hash_table_destroy( key_text );
}
void
keyboard_register_startup( void )
{
startup_manager_module dependencies[] = {
STARTUP_MANAGER_MODULE_LIBSPECTRUM,
STARTUP_MANAGER_MODULE_SETUID
};
startup_manager_register( STARTUP_MANAGER_MODULE_KEYBOARD, dependencies,
ARRAY_SIZE( dependencies ), keyboard_init,
keyboard_end, NULL );
}
libspectrum_byte
keyboard_read( libspectrum_byte porth )
{
libspectrum_byte data = 0xff; int i;
for( i=0; i<8; i++,porth>>=1 ) {
if(! (porth&0x01) ) data &= keyboard_return_values[i];
}
return data;
}
void
keyboard_press( keyboard_key_name key )
{
struct key_bit *ptr;
ptr = g_hash_table_lookup( keyboard_data, &key );
if( ptr ) keyboard_return_values[ ptr->port ] &= ~( ptr->bit );
}
void
keyboard_release( keyboard_key_name key )
{
struct key_bit *ptr;
ptr = g_hash_table_lookup( keyboard_data, &key );
if( ptr ) keyboard_return_values[ ptr->port ] |= ptr->bit;
}
int keyboard_release_all( void )
{
int i;
for( i=0; i<8; i++ ) keyboard_return_values[i] = 0xff;
return 0;
}
const keyboard_spectrum_keys_t*
keyboard_get_spectrum_keys( input_key keysym )
{
return g_hash_table_lookup( spectrum_keys, &keysym );
}
input_key
keysyms_remap( libspectrum_dword ui_keysym )
{
const input_key *ptr;
ptr = g_hash_table_lookup( keysyms_hash, &ui_keysym );
return ptr ? *ptr : INPUT_KEY_NONE;
}
const char*
keyboard_key_text( keyboard_key_name key )
{
const char **ptr;
ptr = g_hash_table_lookup( key_text, &key );
return ptr ? *ptr : "[Unknown key]";
}
libspectrum_byte
keyboard_simulate_keypress( libspectrum_byte porth, keyboard_key_name key )
{
libspectrum_byte r = 0xff;
struct key_bit *data;
data = g_hash_table_lookup( keyboard_data, &key );
if( data ) {
libspectrum_byte mask = (1 << data->port);
if( !(porth & mask) ) {
r &= ~data->bit;
}
}
return r;
}