git @ Cat's Eye Technologies linapple / master src / Keyboard.cpp
master

Tree @master (Download .tar.gz)

Keyboard.cpp @masterraw · history · blame

  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
/*
AppleWin : An Apple //e emulator for Windows

Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2007, Tom Charlesworth, Michael Pohoreski

AppleWin 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.

AppleWin 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 AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Description: Keyboard emulation
 *
 * Author: Various
 */

/* Adaptation for SDL and POSIX (l) by beom beotiger, Nov-Dec 2007 */


#include "stdafx.h"
#include <iostream>
//#pragma  hdrstop

//static bool g_bKeybBufferEnable = false;

#define KEY_OLD

/*static BYTE asciicode[2][10] = {
  {0x08,0x0D,0x15,0x2F,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x08,0x0B,0x15,0x0A,0x00,0x00,0x00,0x00,0x00,0x7F}
};  // Convert PC arrow keys to Apple keycodes*/

/*static*/ bool  g_bShiftKey = false;
/*static*/ bool  g_bCtrlKey  = false;
/*static*/ bool  g_bAltKey   = false;
static bool  g_bCapsLock = true;
static int   lastvirtkey     = 0;  // Current PC keycode
static BYTE  keycode         = 0;  // Current Apple keycode
static DWORD keyboardqueries = 0;

#ifdef KEY_OLD
// Original
static BOOL  keywaiting      = 0;
#else
// Buffered key input:
// - Needed on faster PCs where aliasing occurs during short/fast bursts of 6502 code.
// - Keyboard only sampled during 6502 execution, so if it's run too fast then key presses will be missed.
const int KEY_BUFFER_MIN_SIZE = 1;
const int KEY_BUFFER_MAX_SIZE = 2;
static int g_nKeyBufferSize = KEY_BUFFER_MAX_SIZE;  // Circ key buffer size
static int g_nNextInIdx = 0;
static int g_nNextOutIdx = 0;
static int g_nKeyBufferCnt = 0;

static struct
{
  int nVirtKey;
  BYTE nAppleKey;
} g_nKeyBuffer[KEY_BUFFER_MAX_SIZE];
#endif

static BYTE g_nLastKey = 0x00;

//
// ----- ALL GLOBALLY ACCESSIBLE FUNCTIONS ARE BELOW THIS LINE -----
//

//===========================================================================

void KeybReset()
{
#ifdef KEY_OLD
  keywaiting = 0;
#else
  g_nNextInIdx = 0;
  g_nNextOutIdx = 0;
  g_nKeyBufferCnt = 0;
  g_nLastKey = 0x00;

  g_nKeyBufferSize = g_bKeybBufferEnable ? KEY_BUFFER_MAX_SIZE : KEY_BUFFER_MIN_SIZE;
#endif
}

//===========================================================================

//void KeybSetBufferMode(bool bNewKeybBufferEnable)
//{
//  if(g_bKeybBufferEnable == bNewKeybBufferEnable)
//    return;
//
//  g_bKeybBufferEnable = bNewKeybBufferEnable;
//  KeybReset();
//}
//
//bool KeybGetBufferMode()
//{
//  return g_bKeybBufferEnable;
//}

//===========================================================================
bool KeybGetAltStatus ()
{
  return g_bAltKey;
}

//===========================================================================
bool KeybGetCapsStatus ()
{
  return g_bCapsLock;
}

//===========================================================================
bool KeybGetCtrlStatus ()
{
  return g_bCtrlKey;
}

//===========================================================================
bool KeybGetShiftStatus ()
{
  return g_bShiftKey;
}

//===========================================================================
void KeybUpdateCtrlShiftStatus()
{
//  g_bShiftKey = (GetKeyState( VK_SHIFT  ) & KF_UP) ? true : false; // 0x8000 KF_UP
//  g_bCtrlKey  = (GetKeyState( VK_CONTROL) & KF_UP) ? true : false;
//  g_bAltKey   = (GetKeyState( VK_MENU   ) & KF_UP) ? true : false;
  Uint8 *keys;
  keys = SDL_GetKeyState(NULL);

  g_bShiftKey = (keys[SDLK_LSHIFT] | keys[SDLK_RSHIFT]); // 0x8000 KF_UP   SHIFT
  g_bCtrlKey  = (keys[SDLK_LCTRL]  | keys[SDLK_RCTRL]);  // CTRL
  g_bAltKey   = (keys[SDLK_LALT]   | keys[SDLK_RALT]);  // ALT
}

//===========================================================================
BYTE KeybGetKeycode()    // Used by MemCheckPaging() & VideoCheckMode()
{
  return keycode;
}

//===========================================================================
DWORD KeybGetNumQueries ()  // Used in determining 'idleness' of Apple system
{
  DWORD result = keyboardqueries;
  keyboardqueries = 0;
  return result;
}

//===========================================================================
void KeybQueueKeypress (int key, BOOL bASCII)
{
//  static bool bFreshReset; - do not use

  if (bASCII == ASCII)
  {
/*    if (bFreshReset && key == 0x03)
    {
      bFreshReset = 0;
      return; // Swallow spurious CTRL-C caused by CTRL-BREAK
    }
    bFreshReset = 0;*/
    if (key > 0x7F) return;
// Conver SHIFTed keys to their secondary values
// may be this is straitfoward method, but it seems to be working. What else we need?? --bb
    KeybUpdateCtrlShiftStatus();
   if(g_bShiftKey)     // SHIFT is pressed
   {
      // GPH fixed shift bug
      if(isalpha(key)) {
        key &= (char) (~0x20);
      }
      switch(key) {
        case '1': key = '!'; break;
        case '2': key = '@'; break;
        case '3': key = '#'; break;
        case '4': key = '$'; break;
        case '5': key = '%'; break;
        case '6': key = '^'; break;
        case '7': key = '&'; break;
        case '8': key = '*'; break;
        case '9': key = '('; break;
        case '0': key = ')'; break;
        case '`': key = '~'; break;
        case '-': key = '_'; break;
        case '=': key = '+'; break;
        case '\\': key = '|'; break;
        case '[': key = '{'; break;
        case ']': key = '}'; break;
        case ';': key = ':'; break;
        case '\'': key = '"'; break;
        case ',': key = '<'; break;
        case '.': key = '>'; break;
        case '/': key = '?'; break;
        default:        break;
      }
    } else if (g_bCtrlKey) {
      if(key >= SDLK_a && key <= SDLK_z) key = key - SDLK_a + 1;
      else switch(key) {
        case '\\': key = 28; break;
        case '[' : key = 27; break;
        case ']' : key = 29; break;
        case SDLK_RETURN: key = 10; break;

        default: break;
      }
    }


    if (!IS_APPLE2)
    {
      if (g_bCapsLock && (key >= 'a') && (key <='z'))
        keycode = key - 32;
      else
        keycode = key;
    }
    else
    {
      if (key >= '`')
        keycode = key - 32;
      else
        keycode = key;
    }
    lastvirtkey = key;
  }
  else
  {
/*    if ((key == VK_CANCEL) && (GetKeyState(VK_CONTROL) < 0)) - implement in Frame.cpp
    {
      // Ctrl+Reset
      if (!IS_APPLE2)
        MemResetPaging();

      DiskReset();
      KeybReset();
      if (!IS_APPLE2)
        VideoResetState();  // Switch Alternate char set off
      MB_Reset();

#ifndef KEY_OLD
      g_nNextInIdx = g_nNextOutIdx = g_nKeyBufferCnt = 0;
#endif

      CpuReset();
      bFreshReset = 1;
      return;
    }
*/
/*   No pasting??? Ye-e-e-e-et! */
//     if ((key == VK_INSERT) && (GetKeyState(VK_SHIFT) < 0))
//     {
//       // Shift+Insert
//       ClipboardInitiatePaste();
//       return;
//     }

//     if (!((key >= VK_LEFT) && (key <= VK_DELETE) && asciicode[IS_APPLE2 ? 0 : 1][key - VK_LEFT]))
//       return;
//     keycode = asciicode[IS_APPLE2 ? 0 : 1][key - VK_LEFT];    // Convert to Apple arrow keycode
//     lastvirtkey = key;
//     {0x08,0x0D,0x15,0x2F,0x00,0x00,0x00,0x00,0x00,0x00}, - good old APPLE2
//     {0x08,0x0B,0x15,0x0A,0x00,0x00,0x00,0x00,0x00,0x7F}

    if(IS_APPLE2)
      switch(key) {
        case SDLK_LEFT: keycode = 0x08; break;
        case SDLK_UP:  keycode = 0x0D; break;
        case SDLK_RIGHT:keycode = 0x15; break;
        case SDLK_DOWN: keycode = 0x2F; break;
        case SDLK_DELETE:keycode = 0x00;break;
        default: return;
      }
    else
      switch(key) {
        case SDLK_LEFT: keycode = 0x08; break;
        case SDLK_UP:  keycode = 0x0B; break;
        case SDLK_RIGHT:keycode = 0x15; break;
        case SDLK_DOWN: keycode = 0x0A; break;
        case SDLK_DELETE:keycode = 0x7F;break;
        default: return;
      }
    lastvirtkey = key;
  }


#ifdef KEY_OLD
  keywaiting = 1;
#else
  bool bOverflow = false;

  if(g_nKeyBufferCnt < g_nKeyBufferSize)
    g_nKeyBufferCnt++;
  else
    bOverflow = true;

  g_nKeyBuffer[g_nNextInIdx].nVirtKey = lastvirtkey;
  g_nKeyBuffer[g_nNextInIdx].nAppleKey = keycode;
  g_nNextInIdx = (g_nNextInIdx + 1) % g_nKeyBufferSize;

  if(bOverflow)
    g_nNextOutIdx = (g_nNextOutIdx + 1) % g_nKeyBufferSize;
#endif
}

//===========================================================================

/*static HGLOBAL hglb = NULL;
static LPTSTR lptstr = NULL;
static bool g_bPasteFromClipboard = false;
static bool g_bClipboardActive = false;*/
/*
void ClipboardInitiatePaste()
{
  if (g_bClipboardActive)
    return;

  g_bPasteFromClipboard = true;
}

static void ClipboardDone()
{
  if (g_bClipboardActive)
  {
    g_bClipboardActive = false;
    GlobalUnlock(hglb);
    CloseClipboard();
  }
}

static void ClipboardInit()
{
  ClipboardDone();

  if (!IsClipboardFormatAvailable(CF_TEXT))
    return;

  if (!OpenClipboard(g_hFrameWindow))
    return;

  hglb = GetClipboardData(CF_TEXT);
  if (hglb == NULL)
  {
    CloseClipboard();
    return;
  }

  lptstr = (char*) GlobalLock(hglb);
  if (lptstr == NULL)
  {
    CloseClipboard();
    return;
  }

  g_bPasteFromClipboard = false;
  g_bClipboardActive = true;
}

static char ClipboardCurrChar(bool bIncPtr)
{
  char nKey;
  int nInc = 1;

  if((lptstr[0] == 0x0D) && (lptstr[1] == 0x0A))
  {
    nKey = 0x0D;
    nInc = 2;
  }
  else
  {
    nKey = lptstr[0];
  }

  if(bIncPtr)
    lptstr += nInc;

  return nKey;
}*/

//===========================================================================

BYTE /*__stdcall */KeybReadData (WORD, WORD, BYTE, BYTE, ULONG)
{
  keyboardqueries++;

//   if(g_bPasteFromClipboard)
//     ClipboardInit();
//
//   if(g_bClipboardActive)
//   {
//     if(*lptstr == 0)
//       ClipboardDone();
//     else
//       return 0x80 | ClipboardCurrChar(false);
//   }

  //

#ifdef KEY_OLD
  return keycode | (keywaiting ? 0x80 : 0);
#else
  BYTE nKey = g_nKeyBufferCnt ? 0x80 : 0;
  if(g_nKeyBufferCnt)
  {
    nKey |= g_nKeyBuffer[g_nNextOutIdx].nAppleKey;
    g_nLastKey = g_nKeyBuffer[g_nNextOutIdx].nAppleKey;
  }
  else
  {
    nKey |= g_nLastKey;
  }
  return nKey;
#endif
}

//===========================================================================

BYTE /*__stdcall */KeybReadFlag (WORD, WORD, BYTE, BYTE, ULONG)
{
  keyboardqueries++;

  //

//   if(g_bPasteFromClipboard)
//     ClipboardInit();
//
//   if(g_bClipboardActive)
//   {
//     if(*lptstr == 0)
//       ClipboardDone();
//     else
//       return 0x80 | ClipboardCurrChar(true);
//   }

  //

  Uint8 *keys;
  keys = SDL_GetKeyState(NULL); // get current key state - thanx to SDL developers! ^_^ beom beotiger
#ifdef KEY_OLD
  keywaiting = 0;
  return keycode | (keys[lastvirtkey] ? 0x80 : 0);
#else
  BYTE nKey = (keys[g_nKeyBuffer[g_nNextOutIdx].nVirtKey]) ? 0x80 : 0;
  nKey |= g_nKeyBuffer[g_nNextOutIdx].nAppleKey;
  if(g_nKeyBufferCnt)
  {
    g_nKeyBufferCnt--;
    g_nNextOutIdx = (g_nNextOutIdx + 1) % g_nKeyBufferSize;
  }
  return nKey;
#endif
}

//===========================================================================
void KeybToggleCapsLock ()
{
  if (!IS_APPLE2)
  {
    g_bCapsLock = !g_bCapsLock;// never mind real CapsLock status, heh???(GetKeyState(VK_CAPITAL) & 1);
//    printf("g_bCapsLock=%d\n", g_bCapsLock);
    FrameRefreshStatus(DRAW_LEDS);
  }
}

//===========================================================================

DWORD KeybGetSnapshot(SS_IO_Keyboard* pSS)
{
  pSS->keyboardqueries  = keyboardqueries;
  pSS->nLastKey      = g_nLastKey;
  return 0;
}

DWORD KeybSetSnapshot(SS_IO_Keyboard* pSS)
{
  keyboardqueries  = pSS->keyboardqueries;
  g_nLastKey    = pSS->nLastKey;
  return 0;
}