2 * DOS interrupt 09h handler (IRQ1 - KEYBOARD)
4 * Copyright 1999 Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(int);
38 BYTE queuelen,queue[QUEUELEN],ascii[QUEUELEN];
43 * Update the BIOS data segment's keyboard status flags (mem 0x40:0x17/0x18)
44 * if modifier/special keys have been pressed.
45 * FIXME: we merely toggle key status and don't actively set it instead,
46 * so we might be out of sync with the real current system status of these keys.
47 * Probably doesn't matter too much, though.
49 void DOSVM_Int09UpdateKbdStatusFlags(BYTE scan, BOOL extended, BIOSDATA *data, BOOL *modifier)
51 BYTE realscan = scan & 0x7f; /* remove 0x80 make/break flag */
52 BYTE bit1 = 255, bit2 = 255;
60 case 0x36: /* r shift */
63 case 0x2a: /* l shift */
66 case 0x1d: /* l/r control */
68 if (!extended) /* left control only */
71 case 0x37: /* SysRq inner parts */
72 /* SysRq scan code sequence: 38, e0, 37, e0, b7, b8 */
73 FIXME("SysRq not handled yet.\n");
75 case 0x38: /* l/r menu/alt, SysRq outer parts */
77 if (!extended) /* left alt only */
80 case 0x46: /* scroll lock */
82 if (!extended) /* left ctrl only */
85 case 0x45: /* num lock, pause */
86 if (extended) /* distinguish from non-extended Pause key */
93 if (!(scan & 0x80)) /* "make" code */
97 case 0x3a: /* caps lock */
101 case 0x52: /* insert */
104 *modifier = FALSE; /* insert is no modifier: thus pass to int16 */
107 /* now that we know which bits to set, update them */
108 if (!(scan & 0x80)) /* "make" code (keypress) */
114 data->KbdFlags2 |= 1 << bit2; /* set "Pause" flag */
115 TRACE("PAUSE key, sleeping !\n");
116 /* wait for keypress to unlock pause */
119 } while (!(ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res) && (msg.EventType == KEY_EVENT)));
120 data->KbdFlags2 &= ~(1 << bit2); /* release "Pause" flag */
123 data->KbdFlags2 |= 1 << bit2;
127 if (bit1 < 4) /* key "pressed" flag */
128 data->KbdFlags1 |= 1 << bit1;
129 else /* key "active" flag */
130 data->KbdFlags1 ^= 1 << bit1;
133 else /* "break" / release */
136 data->KbdFlags2 &= ~(1 << bit2);
137 if (bit1 < 4) /* is it a key "pressed" bit ? */
138 data->KbdFlags1 &= ~(1 << bit1);
140 TRACE("ext. %d, bits %d/%d, KbdFlags %02x/%02x\n", extended, bit1, bit2, data->KbdFlags1, data->KbdFlags2);
143 /**********************************************************************
146 * Handler for int 09h.
147 * See http://www.execpc.com/~geezer/osd/kbd/ for a very good description
148 * of keyboard mapping modes.
150 void WINAPI DOSVM_Int09Handler( CONTEXT86 *context )
152 BIOSDATA *data = BIOS_DATA;
153 BYTE ascii, scan = DOSVM_Int09ReadScan(&ascii);
154 BYTE realscan = scan & 0x7f; /* remove 0x80 make/break flag */
155 BOOL modifier = FALSE;
156 static BOOL extended = FALSE; /* indicates start of extended key sequence */
160 TRACE("scan=%02x, ascii=%02x[%c]\n",scan, ascii, ascii ? ascii : ' ');
162 if (scan == 0xe0) /* extended keycode */
165 /* check for keys concerning keyboard status flags */
166 if ((realscan == 0x52 /* insert */)
167 || (realscan == 0x3a /* caps lock */)
168 || (realscan == 0x45 /* num lock (extended) or pause/break */)
169 || (realscan == 0x46 /* scroll lock */)
170 || (realscan == 0x2a /* l shift */)
171 || (realscan == 0x36 /* r shift */)
172 || (realscan == 0x37 /* SysRq */)
173 || (realscan == 0x38 /* l/r menu/alt, SysRq */)
174 || (realscan == 0x1d /* l/r control */))
175 DOSVM_Int09UpdateKbdStatusFlags(scan, extended, data, &modifier);
178 extended = FALSE; /* reset extended flag now */
180 /* only interested in "make" (press) codes, not "break" (release),
181 * and also not in "modifier key only" (w/o ascii) notifications */
182 if (!(scan & 0x80) && !(modifier && !ascii))
185 /* we already have an ASCII code, no translation necessary */
186 if (data->KbdFlags1 & 8) /* Alt key ? */
187 ch[0] = 0; /* ASCII code needs to be 0 if Alt also pressed */
190 /* FIXME: need to handle things such as Shift-F1 etc. */
194 UINT vkey = MapVirtualKeyA(scan&0x7f, 1);
196 GetKeyboardState(keystate);
197 cnt = ToAscii(vkey, scan, keystate, (LPWORD)ch, 0);
200 for (c2=0; c2<cnt; c2++)
201 DOSVM_Int16AddChar(ch[c2], scan);
204 /* FIXME: need to handle things like shift-F-keys,
205 * 0xE0 extended keys, etc */
206 DOSVM_Int16AddChar(0, scan);
209 DOSVM_PIC_ioport_out( 0x20, 0x20 ); /* send EOI */
212 static void KbdRelay( CONTEXT86 *context, void *data )
214 if (kbdinfo.queuelen) {
215 /* cleanup operation, called from DOSVM_PIC_ioport_out:
216 * we'll remove current scancode from keyboard buffer here,
217 * rather than in ReadScan, because some DOS apps depend on
218 * the scancode being available for reading multiple times... */
219 if (--kbdinfo.queuelen) {
220 memmove(kbdinfo.queue,kbdinfo.queue+1,kbdinfo.queuelen);
221 memmove(kbdinfo.ascii,kbdinfo.ascii+1,kbdinfo.queuelen);
226 void WINAPI DOSVM_Int09SendScan( BYTE scan, BYTE ascii )
228 if (kbdinfo.queuelen == QUEUELEN) {
229 ERR("keyboard queue overflow\n");
232 /* add scancode to queue */
233 kbdinfo.queue[kbdinfo.queuelen] = scan;
234 kbdinfo.ascii[kbdinfo.queuelen++] = ascii;
235 /* tell app to read it by triggering IRQ 1 (int 09) */
236 DOSVM_QueueEvent(1,DOS_PRIORITY_KEYBOARD,KbdRelay,NULL);
239 /**********************************************************************
240 * KbdReadScan (WINEDOS.@)
242 BYTE WINAPI DOSVM_Int09ReadScan( BYTE*ascii )
244 if (ascii) *ascii = kbdinfo.ascii[0];
245 return kbdinfo.queue[0];