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
30 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(int);
39 BYTE queuelen
,queue
[QUEUELEN
],ascii
[QUEUELEN
];
44 * Update the BIOS data segment's keyboard status flags (mem 0x40:0x17/0x18)
45 * if modifier/special keys have been pressed.
46 * FIXME: we merely toggle key status and don't actively set it instead,
47 * so we might be out of sync with the real current system status of these keys.
48 * Probably doesn't matter too much, though.
50 void DOSVM_Int09UpdateKbdStatusFlags(BYTE scan
, BOOL extended
, BIOSDATA
*data
, BOOL
*modifier
)
52 BYTE realscan
= scan
& 0x7f; /* remove 0x80 make/break flag */
53 BYTE bit1
= 255, bit2
= 255;
61 case 0x36: /* r shift */
64 case 0x2a: /* l shift */
67 case 0x1d: /* l/r control */
69 if (!extended
) /* left control only */
72 case 0x37: /* SysRq inner parts */
73 /* SysRq scan code sequence: 38, e0, 37, e0, b7, b8 */
74 FIXME("SysRq not handled yet.\n");
76 case 0x38: /* l/r menu/alt, SysRq outer parts */
78 if (!extended
) /* left alt only */
81 case 0x46: /* scroll lock */
83 if (!extended
) /* left ctrl only */
86 case 0x45: /* num lock, pause */
87 if (extended
) /* distinguish from non-extended Pause key */
94 if (!(scan
& 0x80)) /* "make" code */
98 case 0x3a: /* caps lock */
102 case 0x52: /* insert */
105 *modifier
= FALSE
; /* insert is no modifier: thus pass to int16 */
108 /* now that we know which bits to set, update them */
109 if (!(scan
& 0x80)) /* "make" code (keypress) */
115 data
->KbdFlags2
|= 1 << bit2
; /* set "Pause" flag */
116 TRACE("PAUSE key, sleeping !\n");
117 /* wait for keypress to unlock pause */
120 } while (!(ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE
),&msg
,1,&res
) && (msg
.EventType
== KEY_EVENT
)));
121 data
->KbdFlags2
&= ~(1 << bit2
); /* release "Pause" flag */
124 data
->KbdFlags2
|= 1 << bit2
;
128 if (bit1
< 4) /* key "pressed" flag */
129 data
->KbdFlags1
|= 1 << bit1
;
130 else /* key "active" flag */
131 data
->KbdFlags1
^= 1 << bit1
;
134 else /* "break" / release */
137 data
->KbdFlags2
&= ~(1 << bit2
);
138 if (bit1
< 4) /* is it a key "pressed" bit ? */
139 data
->KbdFlags1
&= ~(1 << bit1
);
141 TRACE("ext. %d, bits %d/%d, KbdFlags %02x/%02x\n", extended
, bit1
, bit2
, data
->KbdFlags1
, data
->KbdFlags2
);
144 /**********************************************************************
145 * DOSVM_Int09Handler (WINEDOS16.109)
147 * Handler for int 09h.
148 * See http://www.execpc.com/~geezer/osd/kbd/ for a very good description
149 * of keyboard mapping modes.
151 void WINAPI
DOSVM_Int09Handler( CONTEXT86
*context
)
153 BIOSDATA
*data
= DOSVM_BiosData();
154 BYTE ascii
, scan
= DOSVM_Int09ReadScan(&ascii
);
155 BYTE realscan
= scan
& 0x7f; /* remove 0x80 make/break flag */
156 BOOL modifier
= FALSE
;
157 static BOOL extended
= FALSE
; /* indicates start of extended key sequence */
161 TRACE("scan=%02x, ascii=%02x[%c]\n",scan
, ascii
, ascii
? ascii
: ' ');
163 if (scan
== 0xe0) /* extended keycode */
166 /* check for keys concerning keyboard status flags */
167 if ((realscan
== 0x52 /* insert */)
168 || (realscan
== 0x3a /* caps lock */)
169 || (realscan
== 0x45 /* num lock (extended) or pause/break */)
170 || (realscan
== 0x46 /* scroll lock */)
171 || (realscan
== 0x2a /* l shift */)
172 || (realscan
== 0x36 /* r shift */)
173 || (realscan
== 0x37 /* SysRq */)
174 || (realscan
== 0x38 /* l/r menu/alt, SysRq */)
175 || (realscan
== 0x1d /* l/r control */))
176 DOSVM_Int09UpdateKbdStatusFlags(scan
, extended
, data
, &modifier
);
179 extended
= FALSE
; /* reset extended flag now */
181 /* only interested in "make" (press) codes, not "break" (release),
182 * and also not in "modifier key only" (w/o ascii) notifications */
183 if (!(scan
& 0x80) && !(modifier
&& !ascii
))
186 /* we already have an ASCII code, no translation necessary */
187 if (data
->KbdFlags1
& 8) /* Alt key ? */
188 ch
[0] = 0; /* ASCII code needs to be 0 if Alt also pressed */
191 /* FIXME: need to handle things such as Shift-F1 etc. */
195 UINT vkey
= MapVirtualKeyA(scan
&0x7f, 1);
197 GetKeyboardState(keystate
);
198 cnt
= ToAscii(vkey
, scan
, keystate
, (LPWORD
)ch
, 0);
201 for (c2
=0; c2
<cnt
; c2
++)
202 DOSVM_Int16AddChar(ch
[c2
], scan
);
205 /* FIXME: need to handle things like shift-F-keys,
206 * 0xE0 extended keys, etc */
207 DOSVM_Int16AddChar(0, scan
);
211 DOSVM_AcknowledgeIRQ( context
);
214 static void KbdRelay( CONTEXT86
*context
, void *data
)
216 if (kbdinfo
.queuelen
) {
217 /* cleanup operation, called from DOSVM_PIC_ioport_out:
218 * we'll remove current scancode from keyboard buffer here,
219 * rather than in ReadScan, because some DOS apps depend on
220 * the scancode being available for reading multiple times... */
221 if (--kbdinfo
.queuelen
) {
222 memmove(kbdinfo
.queue
,kbdinfo
.queue
+1,kbdinfo
.queuelen
);
223 memmove(kbdinfo
.ascii
,kbdinfo
.ascii
+1,kbdinfo
.queuelen
);
228 void WINAPI
DOSVM_Int09SendScan( BYTE scan
, BYTE ascii
)
230 if (kbdinfo
.queuelen
== QUEUELEN
) {
231 ERR("keyboard queue overflow\n");
234 /* add scancode to queue */
235 kbdinfo
.queue
[kbdinfo
.queuelen
] = scan
;
236 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
);
241 /**********************************************************************
242 * KbdReadScan (WINEDOS.@)
244 BYTE WINAPI
DOSVM_Int09ReadScan( BYTE
*ascii
)
246 if (ascii
) *ascii
= kbdinfo
.ascii
[0];
247 return kbdinfo
.queue
[0];