int09.c 7.55 KB
Newer Older
1 2
/*
 * DOS interrupt 09h handler (IRQ1 - KEYBOARD)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Copyright 1999 Ove Kven
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 20
 */

21
#include <stdarg.h>
22
#include <stdlib.h>
23
#include <string.h>
24

25
#include "windef.h"
26
#include "winbase.h"
27
#include "wingdi.h"
28 29
#include "winuser.h"
#include "miscemu.h"
30
#include "wine/debug.h"
31 32
#include "dosexe.h"

33
WINE_DEFAULT_DEBUG_CHANNEL(int);
34

Ove Kaaven's avatar
Ove Kaaven committed
35 36
#define QUEUELEN 31

37 38
static struct
{
Ove Kaaven's avatar
Ove Kaaven committed
39
  BYTE queuelen,queue[QUEUELEN],ascii[QUEUELEN];
40 41
} kbdinfo;

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
/*
 * Update the BIOS data segment's keyboard status flags (mem 0x40:0x17/0x18)
 * if modifier/special keys have been pressed.
 * FIXME: we merely toggle key status and don't actively set it instead,
 * so we might be out of sync with the real current system status of these keys.
 * Probably doesn't matter too much, though.
 */
void DOSVM_Int09UpdateKbdStatusFlags(BYTE scan, BOOL extended, BIOSDATA *data, BOOL *modifier)
{
    BYTE realscan = scan & 0x7f; /* remove 0x80 make/break flag */
    BYTE bit1 = 255, bit2 = 255;
    INPUT_RECORD msg;
    DWORD res;

    *modifier = TRUE;

    switch (realscan)
    {
      case 0x36: /* r shift */
	      bit1 = 0;
	      break;
      case 0x2a: /* l shift */
	      bit1 = 1;
	      break;
      case 0x1d: /* l/r control */
	      bit1 = 2;
	      if (!extended) /* left control only */
		  bit2 = 0;
	      break;
      case 0x37: /* SysRq inner parts */
	      /* SysRq scan code sequence: 38, e0, 37, e0, b7, b8 */
	      FIXME("SysRq not handled yet.\n");
	      break;
      case 0x38: /* l/r menu/alt, SysRq outer parts */
	      bit1 = 3;
	      if (!extended) /* left alt only */
	          bit2 = 1;
	      break;
      case 0x46: /* scroll lock */
	      bit1 = 4;
	      if (!extended) /* left ctrl only */
	          bit2 = 4;
	      break;
      case 0x45: /* num lock, pause */
	      if (extended) /* distinguish from non-extended Pause key */
              { /* num lock */
	          bit1 = 5;
	          bit2 = 5;
              }
	      else
              { /* pause */
                  if (!(scan & 0x80)) /* "make" code */
                      bit2 = 3;
              }
	      break;
      case 0x3a: /* caps lock */
	      bit1 = 6;
	      bit2 = 6;
	      break;
      case 0x52: /* insert */
	      bit1 = 7;
	      bit2 = 7;
	      *modifier = FALSE; /* insert is no modifier: thus pass to int16 */
	      break;
    }
    /* now that we know which bits to set, update them */
    if (!(scan & 0x80)) /* "make" code (keypress) */
    {
        if (bit2 != 255)
        {
	    if (bit2 == 3)
	    {
                data->KbdFlags2 |= 1 << bit2; /* set "Pause" flag */
                TRACE("PAUSE key, sleeping !\n");
                /* wait for keypress to unlock pause */
		do {
                    Sleep(55);
		} while (!(ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res) && (msg.EventType == KEY_EVENT)));
                data->KbdFlags2 &= ~(1 << bit2); /* release "Pause" flag */
            }
	    else
                data->KbdFlags2 |= 1 << bit2;
        }
        if (bit1 != 255)
        {
            if (bit1 < 4) /* key "pressed" flag */
	        data->KbdFlags1 |= 1 << bit1;
            else /* key "active" flag */
                data->KbdFlags1 ^= 1 << bit1;
        }
    }
    else /* "break" / release */
    {
        if (bit2 != 255)
            data->KbdFlags2 &= ~(1 << bit2);
        if (bit1 < 4) /* is it a key "pressed" bit ? */
	    data->KbdFlags1 &= ~(1 << bit1);
    }
    TRACE("ext. %d, bits %d/%d, KbdFlags %02x/%02x\n", extended, bit1, bit2, data->KbdFlags1, data->KbdFlags2);
}

144
/**********************************************************************
145
 *	    DOSVM_Int09Handler (WINEDOS16.109)
146 147
 *
 * Handler for int 09h.
148 149
 * See http://www.execpc.com/~geezer/osd/kbd/ for a very good description
 * of keyboard mapping modes.
150
 */
151
void WINAPI DOSVM_Int09Handler( CONTEXT86 *context )
152
{
153
  BIOSDATA *data = DOSVM_BiosData();
154
  BYTE ascii, scan = DOSVM_Int09ReadScan(&ascii);
155 156 157
  BYTE realscan = scan & 0x7f; /* remove 0x80 make/break flag */
  BOOL modifier = FALSE;
  static BOOL extended = FALSE; /* indicates start of extended key sequence */
158
  BYTE ch[2];
159
  int cnt, c2;
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
  TRACE("scan=%02x, ascii=%02x[%c]\n",scan, ascii, ascii ? ascii : ' ');

  if (scan == 0xe0) /* extended keycode */
      extended = TRUE;
  
  /* check for keys concerning keyboard status flags */
  if ((realscan == 0x52 /* insert */)
  ||  (realscan == 0x3a /* caps lock */)
  ||  (realscan == 0x45 /* num lock (extended) or pause/break */)
  ||  (realscan == 0x46 /* scroll lock */)
  ||  (realscan == 0x2a /* l shift */)
  ||  (realscan == 0x36 /* r shift */)
  ||  (realscan == 0x37 /* SysRq */)
  ||  (realscan == 0x38 /* l/r menu/alt, SysRq */)
  ||  (realscan == 0x1d /* l/r control */))
      DOSVM_Int09UpdateKbdStatusFlags(scan, extended, data, &modifier);

  if (scan != 0xe0)
      extended = FALSE; /* reset extended flag now */

  /* only interested in "make" (press) codes, not "break" (release),
   * and also not in "modifier key only" (w/o ascii) notifications */
  if (!(scan & 0x80) && !(modifier && !ascii))
  {
185 186
    if (ascii) {
      /* we already have an ASCII code, no translation necessary */
187 188 189 190 191
      if (data->KbdFlags1 & 8) /* Alt key ? */
	ch[0] = 0; /* ASCII code needs to be 0 if Alt also pressed */
      else
        ch[0] = ascii;
      /* FIXME: need to handle things such as Shift-F1 etc. */
192 193
      cnt = 1;
    } else {
194
      /* translate */
195
      UINT vkey = MapVirtualKeyA(scan&0x7f, 1);
196 197 198
      BYTE keystate[256];
      GetKeyboardState(keystate);
      cnt = ToAscii(vkey, scan, keystate, (LPWORD)ch, 0);
199
    }
200 201
    if (cnt>0) {
      for (c2=0; c2<cnt; c2++)
202
        DOSVM_Int16AddChar(ch[c2], scan);
203 204
    } else
    if (cnt==0) {
205 206
      /* FIXME: need to handle things like shift-F-keys,
       * 0xE0 extended keys, etc */
207
      DOSVM_Int16AddChar(0, scan);
208 209
    }
  }
210 211

  DOSVM_AcknowledgeIRQ( context );
212 213
}

214
static void KbdRelay( CONTEXT86 *context, void *data )
215
{
216
  if (kbdinfo.queuelen) {
217
    /* cleanup operation, called from DOSVM_PIC_ioport_out:
218 219 220
     * we'll remove current scancode from keyboard buffer here,
     * rather than in ReadScan, because some DOS apps depend on
     * the scancode being available for reading multiple times... */
221 222 223
    if (--kbdinfo.queuelen) {
      memmove(kbdinfo.queue,kbdinfo.queue+1,kbdinfo.queuelen);
      memmove(kbdinfo.ascii,kbdinfo.ascii+1,kbdinfo.queuelen);
224
    }
225 226 227
  }
}

228
void WINAPI DOSVM_Int09SendScan( BYTE scan, BYTE ascii )
229
{
230
  if (kbdinfo.queuelen == QUEUELEN) {
Ove Kaaven's avatar
Ove Kaaven committed
231 232 233
    ERR("keyboard queue overflow\n");
    return;
  }
234
  /* add scancode to queue */
235 236
  kbdinfo.queue[kbdinfo.queuelen] = scan;
  kbdinfo.ascii[kbdinfo.queuelen++] = ascii;
237
  /* tell app to read it by triggering IRQ 1 (int 09) */
238
  DOSVM_QueueEvent(1,DOS_PRIORITY_KEYBOARD,KbdRelay,NULL);
239 240
}

Patrik Stridvall's avatar
Patrik Stridvall committed
241 242 243
/**********************************************************************
 *	    KbdReadScan (WINEDOS.@)
 */
244
BYTE WINAPI DOSVM_Int09ReadScan( BYTE*ascii )
245
{
246 247
    if (ascii) *ascii = kbdinfo.ascii[0];
    return kbdinfo.queue[0];
248
}