int09.c 7.53 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
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
#include "winuser.h"
29
#include "wine/debug.h"
30 31
#include "dosexe.h"

32
WINE_DEFAULT_DEBUG_CHANNEL(int);
33

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

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

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
/*
 * 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);
}

143
/**********************************************************************
144
 *	    DOSVM_Int09Handler (WINEDOS16.109)
145 146
 *
 * Handler for int 09h.
147 148
 * See http://www.execpc.com/~geezer/osd/kbd/ for a very good description
 * of keyboard mapping modes.
149
 */
150
void WINAPI DOSVM_Int09Handler( CONTEXT86 *context )
151
{
152
  BIOSDATA *data = DOSVM_BiosData();
153
  BYTE ascii, scan = DOSVM_Int09ReadScan(&ascii);
154 155 156
  BYTE realscan = scan & 0x7f; /* remove 0x80 make/break flag */
  BOOL modifier = FALSE;
  static BOOL extended = FALSE; /* indicates start of extended key sequence */
157
  BYTE ch[2];
158
  int cnt, c2;
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
  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))
  {
184 185
    if (ascii) {
      /* we already have an ASCII code, no translation necessary */
186 187 188 189 190
      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. */
191 192
      cnt = 1;
    } else {
193
      /* translate */
194
      UINT vkey = MapVirtualKeyA(scan&0x7f, 1);
195 196 197
      BYTE keystate[256];
      GetKeyboardState(keystate);
      cnt = ToAscii(vkey, scan, keystate, (LPWORD)ch, 0);
198
    }
199 200
    if (cnt>0) {
      for (c2=0; c2<cnt; c2++)
201
        DOSVM_Int16AddChar(ch[c2], scan);
202 203
    } else
    if (cnt==0) {
204 205
      /* FIXME: need to handle things like shift-F-keys,
       * 0xE0 extended keys, etc */
206
      DOSVM_Int16AddChar(0, scan);
207 208
    }
  }
209 210

  DOSVM_AcknowledgeIRQ( context );
211 212
}

213
static void KbdRelay( CONTEXT86 *context, void *data )
214
{
215
  if (kbdinfo.queuelen) {
216
    /* cleanup operation, called from DOSVM_PIC_ioport_out:
217 218 219
     * 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... */
220 221 222
    if (--kbdinfo.queuelen) {
      memmove(kbdinfo.queue,kbdinfo.queue+1,kbdinfo.queuelen);
      memmove(kbdinfo.ascii,kbdinfo.ascii+1,kbdinfo.queuelen);
223
    }
224 225 226
  }
}

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

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