-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.c
640 lines (552 loc) · 15.1 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*****************************************************************************/
/*
Copyright 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) 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 3 of the License, or
(at your option) any later version.
VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*
Keyboard handling / translation - system -> emulator
TODO: any key(s) that results in a multi-key (eg. SHIFT/ALT/CTRL)
combination on the CoCo side is only sending one interrupt
signal for both keys. This seems to work in virtually all cases,
but is it an accurate emulation?
*/
/*****************************************************************************/
#define DIRECTINPUT_VERSION 0x0800
// this must be before dinput.h as it contains Windows types and not Windows.h include
#include <windows.h>
#include <dinput.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "defines.h"
#include "keyboard.h"
#include "mc6821.h"
#include "tcc1014registers.h"
#include "Vcc.h"
#include "joystickinput.h"
#include "keyboardLayout.h"
#include "config.h"
#include <queue>
#include "xDebug.h"
/*****************************************************************************/
/*
Forward declarations
*/
unsigned char SetMouseStatus(unsigned char, unsigned char);
bool pasting = false; //Are the keyboard functions in the middle of a paste operation?
bool GetNextScanInPasteQueue(unsigned char col);
bool IsShiftKeyDown();
/*****************************************************************************/
// Global variables
/*****************************************************************************/
// key translation table maximum size, (arbitrary) most of the layouts are < 80 entries
#define KBTABLE_ENTRY_COUNT 100
#define KEY_DOWN 1
#define KEY_UP 0
// Simulated Keypresses for Clipboard pasting.
std::queue<unsigned char> PasteInputQueue;
LARGE_INTEGER Frequency;
LARGE_INTEGER LastAdvance;
double PasteDelay = 0.008; // This is the interkey delay in seconds - it sets the time between KEYDOWN and KEYUP
int CurrentThrottle = 0;
bool IsShift = false;
enum PasteState
{
Idle,
KeyDown,
KeyUp,
CheckDone,
};
PasteState CurrentPasteState;
bool ShiftPaste = false;
bool CtrlPaste = false;
/* track all keyboard scan codes state (up/down) */
static int ScanTable[256];
/* run-time 'rollover' table to pass to the MC6821 when a key is pressed */
static unsigned char RolloverTable[8]; // CoCo 'keys' for emulator
// run-time keyboard layout table (key(s) to keys(s) translation)
static keytranslationentry_t KeyTransTable[KBTABLE_ENTRY_COUNT];
/*****************************************************************************/
// Get CoCo 'scan' code called from MC6821.c to read the keyboard/joystick state
/*****************************************************************************/
unsigned char
vccKeyboardGetScan(unsigned char Col)
{
unsigned char temp;
unsigned char x;
unsigned char mask;
unsigned char ret_val;
ret_val = 0;
GetNextScanInPasteQueue(Col);
temp = ~Col; //Get colums
mask = 1;
for (x=0; x<8; x++)
{
if ( (temp & mask) ) // Found an active column scan
{
ret_val |= RolloverTable[x];
}
mask = (mask<<1);
}
ret_val = 127 - ret_val;
// Add joystick button and analog compare bits.
ret_val = vccJoystickGetScan(ret_val);
#if 0 // no noticible change when this is disabled
// TODO: move to MC6821/GIME
{
/** another keyboard IRQ flag - this should really be in the GIME code*/
static unsigned char IrqFlag = 0;
if ((ret_val & 0x7F) != 0x7F)
{
if ((IrqFlag == 0) & GimeGetKeyboardInteruptState())
{
GimeAssertKeyboardInterupt();
IrqFlag = 1;
}
}
else
{
IrqFlag = 0;
}
}
#endif
return (ret_val);
}
/*****************************************************************************/
void _vccKeyboardUpdateRolloverTable()
{
int Index;
unsigned char LockOut = 0;
// clear the rollover table
for (Index = 0; Index < 8; Index++)
{
RolloverTable[Index] = 0;
}
// set rollover table based on ScanTable key status
for (Index = 0; Index<KBTABLE_ENTRY_COUNT; Index++)
{
// stop at last entry
if ( (KeyTransTable[Index].ScanCode1 == 0)
& (KeyTransTable[Index].ScanCode2 == 0)
)
{
break;
}
if ( LockOut != KeyTransTable[Index].ScanCode1 )
{
// Single input key
if ( (KeyTransTable[Index].ScanCode1 != 0)
&& (KeyTransTable[Index].ScanCode2 == 0)
)
{
// check if key pressed
if (ScanTable[KeyTransTable[Index].ScanCode1] == KEY_DOWN)
{
int col;
col = KeyTransTable[Index].Col1;
assert(col >=0 && col <8);
RolloverTable[col] |= KeyTransTable[Index].Row1;
col = KeyTransTable[Index].Col2;
assert(col >= 0 && col <8);
RolloverTable[col] |= KeyTransTable[Index].Row2;
}
}
// Double Input Key
if ( (KeyTransTable[Index].ScanCode1 != 0)
&& (KeyTransTable[Index].ScanCode2 != 0)
)
{
// check if both keys pressed
if ( (ScanTable[KeyTransTable[Index].ScanCode1] == KEY_DOWN)
&& (ScanTable[KeyTransTable[Index].ScanCode2] == KEY_DOWN)
)
{
int col;
col = KeyTransTable[Index].Col1;
assert(col >=0 && col <8);
RolloverTable[col] |= KeyTransTable[Index].Row1;
col = KeyTransTable[Index].Col2;
assert(col >= 0 && col <8);
RolloverTable[col] |= KeyTransTable[Index].Row2;
// always SHIFT
LockOut = KeyTransTable[Index].ScanCode1;
break;
}
}
}
}
}
/*****************************************************************************/
// Dispatch keyboard event to the emulator.
//
// Called from system. eg. WndProc : WM_KEYDOWN/WM_SYSKEYDOWN/WM_SYSKEYUP/WM_KEYUP
//
// @param key Windows virtual key code (VK_XXXX - not used)
// @param ScanCode keyboard scan code (DIK_XXXX - DirectInput)
// @param Status Key status - kEventKeyDown/kEventKeyUp
/*****************************************************************************/
void vccKeyboardHandleKey(unsigned char key, unsigned char ScanCode, keyevent_e keyState)
{
//XTRACE("Key : %c (%3d / 0x%02X) Scan : %d / 0x%02X\n",key,key,key, ScanCode, ScanCode);
//If requested, abort pasting operation.
if (ScanCode == 0x01 || ScanCode == 0x43 || ScanCode == 0x3F) { pasting = false; }
// check for shift key
// Left and right shift generate different scan codes
if (ScanCode == DIK_RSHIFT)
{
ScanCode = DIK_LSHIFT;
}
#if 0 // TODO: CTRL and/or ALT?
// CTRL key - right -> left
if (ScanCode == DIK_RCONTROL)
{
ScanCode = DIK_LCONTROL;
}
// ALT key - right -> left
if (ScanCode == DIK_RMENU)
{
ScanCode = DIK_LMENU;
}
#endif
switch (keyState)
{
default:
// internal error
break;
// Key Down
case kEventKeyDown:
ScanCode = SetMouseStatus(ScanCode, 1); // In case keyboard joystick
// track key is down
ScanTable[ScanCode] = KEY_DOWN;
if (ScanCode == DIK_LSHIFT) {
IsShift = true;
}
_vccKeyboardUpdateRolloverTable();
if ( GimeGetKeyboardInteruptState() ) {
GimeAssertKeyboardInterupt();
}
break;
// Key Up
case kEventKeyUp:
ScanCode = SetMouseStatus(ScanCode, 0);
// reset key (released)
ScanTable[ScanCode] = KEY_UP;
// TODO: verify this is accurate emulation
// Clean out rollover table on shift release
if ( ScanCode == DIK_LSHIFT ) {
IsShift = false;
for (int Index = 0; Index < KBTABLE_ENTRY_COUNT; Index++) {
ScanTable[Index] = KEY_UP;
}
}
_vccKeyboardUpdateRolloverTable();
break;
}
}
/*****************************************************************************/
/*
* Key translation table compare function for sorting (with qsort)
*/
int keyTransCompare(const void * e1, const void * e2)
{
keytranslationentry_t * entry1 = (keytranslationentry_t *)e1;
keytranslationentry_t * entry2 = (keytranslationentry_t *)e2;
int result = 0;
// ascending
// elem1 - elem2
// empty listing push to end
if ( entry1->ScanCode1 == 0
&& entry1->ScanCode2 == 0
&& entry2->ScanCode1 != 0
)
{
return 1;
}
else
if ( entry2->ScanCode1 == 0
&& entry2->ScanCode2 == 0
&& entry1->ScanCode1 != 0
)
{
return -1;
}
else
// push shift/alt/control by themselves to the end
if ( entry1->ScanCode2 == 0
&& ( entry1->ScanCode1 == DIK_LSHIFT
|| entry1->ScanCode1 == DIK_LMENU
|| entry1->ScanCode1 == DIK_LCONTROL
)
)
{
result = 1;
}
else
// push shift/alt/control by themselves to the end
if ( entry2->ScanCode2 == 0
&& ( entry2->ScanCode1 == DIK_LSHIFT
|| entry2->ScanCode1 == DIK_LMENU
|| entry2->ScanCode1 == DIK_LCONTROL
)
)
{
result = -1;
}
else
// move double key combos in front of single ones
if ( entry1->ScanCode2 == 0
&& entry2->ScanCode2 != 0
)
{
result = 1;
}
else
// move double key combos in front of single ones
if ( entry2->ScanCode2 == 0
&& entry1->ScanCode2 != 0
)
{
result = -1;
}
else
{
result = entry1->ScanCode1 - entry2->ScanCode1;
if (result == 0)
{
result = entry1->Row1 - entry2->Row1;
}
if (result == 0)
{
result = entry1->Col1 - entry2->Col1;
}
if (result == 0)
{
result = entry1->Row2 - entry2->Row2;
}
if (result == 0)
{
result = entry1->Col2 - entry2->Col2;
}
}
return result;
}
/*****************************************************************************/
/*
* Rebuilds the run-time keyboard translation lookup table based on the
* current keyboard layout.
*
* The entries are sorted. Any SHIFT + [char] entries need to be placed first
*/
void vccKeyboardBuildRuntimeTable(keyboardlayout_e keyBoardLayout)
{
int Index1 = 0;
int Index2 = 0;
keytranslationentry_t * keyLayoutTable = NULL;
keytranslationentry_t keyTransEntry;
assert(keyBoardLayout >= 0 && keyBoardLayout < kKBLayoutCount);
switch (keyBoardLayout)
{
case kKBLayoutCoCo:
keyLayoutTable = keyTranslationsCoCo;
break;
case kKBLayoutNatural:
keyLayoutTable = keyTranslationsNatural;
break;
case kKBLayoutCompact:
keyLayoutTable = keyTranslationsCompact;
break;
case kKBLayoutCustom:
keyLayoutTable = keyTranslationsCustom;
break;
default:
assert(0 && "unknown keyboard layout");
break;
}
XTRACE("Building run-time key table for layout # : %d - %s\n", keyBoardLayout, k_keyboardLayoutNames[keyBoardLayout]);
// copy the selected keyboard layout to the run-time table
memset(KeyTransTable, 0, sizeof(KeyTransTable));
Index2 = 0;
for (Index1 = 0; ; Index1++)
{
memcpy(&keyTransEntry, &keyLayoutTable[Index1], sizeof(keytranslationentry_t));
//
// Change entries to what the code expects
//
// Make sure ScanCode1 is never 0
// If the key combo uses SHIFT, put it in ScanCode1
// Completely clear unused entries (ScanCode1+2 are 0)
//
//
// swaps ScanCode1 with ScanCode2 if ScanCode2 == DIK_LSHIFT
//
if ( keyTransEntry.ScanCode2 == DIK_LSHIFT )
{
keyTransEntry.ScanCode2 = keyTransEntry.ScanCode1;
keyTransEntry.ScanCode1 = DIK_LSHIFT;
}
//
// swaps ScanCode1 with ScanCode2 if ScanCode1 is zero
//
if ( (keyTransEntry.ScanCode1 == 0)
& (keyTransEntry.ScanCode2 != 0)
)
{
keyTransEntry.ScanCode1 = keyTransEntry.ScanCode2;
keyTransEntry.ScanCode2 = 0;
}
// check for terminating entry
if ( keyTransEntry.ScanCode1 == 0
&& keyTransEntry.ScanCode2 == 0
)
{
break;
}
memcpy(&KeyTransTable[Index2++], &keyTransEntry,sizeof(keytranslationentry_t));
assert(Index2 <= KBTABLE_ENTRY_COUNT && "keyboard layout table is longer than we can handle");
}
//
// Sort the key translation table
//
// Since the table is searched from beginning to end each
// time a key is pressed, we want them to be in the correct
// order.
//
qsort(KeyTransTable, KBTABLE_ENTRY_COUNT, sizeof(keytranslationentry_t), keyTransCompare);
#ifdef _DEBUG
//
// Debug dump the table
//
for (Index1 = 0; Index1 < KBTABLE_ENTRY_COUNT; Index1++)
{
// check for null entry
if ( KeyTransTable[Index1].ScanCode1 == 0
&& KeyTransTable[Index1].ScanCode2 == 0
)
{
// done
break;
}
XTRACE("Key: %3d - 0x%02X (%3d) 0x%02X (%3d) - %2d %2d %2d %2d\n",
Index1,
KeyTransTable[Index1].ScanCode1,
KeyTransTable[Index1].ScanCode1,
KeyTransTable[Index1].ScanCode2,
KeyTransTable[Index1].ScanCode2,
KeyTransTable[Index1].Row1,
KeyTransTable[Index1].Col1,
KeyTransTable[Index1].Row2,
KeyTransTable[Index1].Col2
);
}
#endif
}
bool GetPaste() {
return pasting;
}
void SetPaste(bool tmp) {
pasting = tmp;
}
void PasteIntoQueue(std::string txt)
{
vccKeyboardBuildRuntimeTable((keyboardlayout_e)1);
for (auto& c : txt)
{
PasteInputQueue.push(c);
}
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&LastAdvance);
CurrentPasteState = PasteState::KeyDown;
CurrentThrottle = SetSpeedThrottle(QUERY);
SetSpeedThrottle(0);
SetPaste(true);
}
#include <sstream>
bool GetNextScanInPasteQueue(unsigned char col)
{
if (CurrentPasteState == PasteState::Idle)
{
return false;
}
// Get the Current Tick
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
// Time to advance state?
float elapsed = (now.QuadPart - LastAdvance.QuadPart) / (float)Frequency.QuadPart;
if (elapsed < PasteDelay)
{
return false;
}
LastAdvance = now;
switch (CurrentPasteState)
{
case PasteState::KeyDown:
{
// Peek at next character.
unsigned char next = PasteInputQueue.front();
// Shift Key?
if (next == 0x36)
{
// Lower Shift key and get the next.
PasteInputQueue.pop();
vccKeyboardHandleKey(0x36, 0x36, kEventKeyDown);
next = PasteInputQueue.front();
ShiftPaste = true;
}
// Control Key?
else if (next == 0x1D)
{
// Lower Control key and get the next.
PasteInputQueue.pop();
vccKeyboardHandleKey(0x1D, 0x1D, kEventKeyDown);
next = PasteInputQueue.front();
CtrlPaste = true;
}
vccKeyboardHandleKey(next, next, kEventKeyDown);
CurrentPasteState = KeyUp;
break;
}
case PasteState::KeyUp:
{
// Raise key that was down.
unsigned char last = PasteInputQueue.front();
vccKeyboardHandleKey(last, last, kEventKeyUp);
// Raise shift or control if either were down
if (ShiftPaste) vccKeyboardHandleKey(0x36, 0x36, kEventKeyUp);
if (CtrlPaste) vccKeyboardHandleKey(0x1D, 0x1D, kEventKeyUp);
ShiftPaste = CtrlPaste = false;
PasteInputQueue.pop();
CurrentPasteState = CheckDone;
break;
}
case PasteState::CheckDone:
{
if (PasteInputQueue.size() == 0)
{
SetPaste(false);
vccKeyboardBuildRuntimeTable((keyboardlayout_e)GetKeyboardLayout());
SetSpeedThrottle(CurrentThrottle);
CurrentPasteState = PasteState::Idle;
}
else
{
CurrentPasteState = PasteState::KeyDown;
}
break;
}
}
return true;
}
bool IsShiftKeyDown() {
return IsShift;
}