ole32/tests: Relax the reference counting tests a bit. We only care whether reference...
[wine/hramrach.git] / dlls / krnl386.exe16 / int09.c
blob91b7b5ea34b1558deee029d734e88b9e127468f3
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "wine/debug.h"
30 #include "dosexe.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(int);
34 #define QUEUELEN 31
36 static struct
38 BYTE queuelen,queue[QUEUELEN],ascii[QUEUELEN];
39 } kbdinfo;
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 static 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;
53 INPUT_RECORD msg;
54 DWORD res;
56 *modifier = TRUE;
58 switch (realscan)
60 case 0x36: /* r shift */
61 bit1 = 0;
62 break;
63 case 0x2a: /* l shift */
64 bit1 = 1;
65 break;
66 case 0x1d: /* l/r control */
67 bit1 = 2;
68 if (!extended) /* left control only */
69 bit2 = 0;
70 break;
71 case 0x37: /* SysRq inner parts */
72 /* SysRq scan code sequence: 38, e0, 37, e0, b7, b8 */
73 FIXME("SysRq not handled yet.\n");
74 break;
75 case 0x38: /* l/r menu/alt, SysRq outer parts */
76 bit1 = 3;
77 if (!extended) /* left alt only */
78 bit2 = 1;
79 break;
80 case 0x46: /* scroll lock */
81 bit1 = 4;
82 if (!extended) /* left ctrl only */
83 bit2 = 4;
84 break;
85 case 0x45: /* num lock, pause */
86 if (extended) /* distinguish from non-extended Pause key */
87 { /* num lock */
88 bit1 = 5;
89 bit2 = 5;
91 else
92 { /* pause */
93 if (!(scan & 0x80)) /* "make" code */
94 bit2 = 3;
96 break;
97 case 0x3a: /* caps lock */
98 bit1 = 6;
99 bit2 = 6;
100 break;
101 case 0x52: /* insert */
102 bit1 = 7;
103 bit2 = 7;
104 *modifier = FALSE; /* insert is no modifier: thus pass to int16 */
105 break;
107 /* now that we know which bits to set, update them */
108 if (!(scan & 0x80)) /* "make" code (keypress) */
110 if (bit2 != 255)
112 if (bit2 == 3)
114 data->KbdFlags2 |= 1 << bit2; /* set "Pause" flag */
115 TRACE("PAUSE key, sleeping !\n");
116 /* wait for keypress to unlock pause */
117 do {
118 Sleep(55);
119 } while (!(ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res) && (msg.EventType == KEY_EVENT)));
120 data->KbdFlags2 &= ~(1 << bit2); /* release "Pause" flag */
122 else
123 data->KbdFlags2 |= 1 << bit2;
125 if (bit1 != 255)
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 */
135 if (bit2 != 255)
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 /**********************************************************************
144 * DOSVM_Int09Handler
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 = DOSVM_BiosData();
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 */
157 BYTE ch[2];
158 int cnt, c2;
160 TRACE("scan=%02x, ascii=%02x[%c]\n",scan, ascii, ascii ? ascii : ' ');
162 if (scan == 0xe0) /* extended keycode */
163 extended = TRUE;
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);
177 if (scan != 0xe0)
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))
184 if (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 */
188 else
189 ch[0] = ascii;
190 /* FIXME: need to handle things such as Shift-F1 etc. */
191 cnt = 1;
192 } else {
193 /* translate */
194 UINT vkey = MapVirtualKeyA(scan&0x7f, 1);
195 BYTE keystate[256];
196 GetKeyboardState(keystate);
197 cnt = ToAscii(vkey, scan, keystate, (LPWORD)ch, 0);
199 if (cnt>0) {
200 for (c2=0; c2<cnt; c2++)
201 DOSVM_Int16AddChar(ch[c2], scan);
202 } else
203 if (cnt==0) {
204 /* FIXME: need to handle things like shift-F-keys,
205 * 0xE0 extended keys, etc */
206 DOSVM_Int16AddChar(0, scan);
210 DOSVM_AcknowledgeIRQ( context );
213 static void KbdRelay( CONTEXT86 *context, void *data )
215 if (kbdinfo.queuelen) {
216 /* cleanup operation, called from DOSVM_PIC_ioport_out:
217 * we'll remove current scancode from keyboard buffer here,
218 * rather than in ReadScan, because some DOS apps depend on
219 * the scancode being available for reading multiple times... */
220 if (--kbdinfo.queuelen) {
221 memmove(kbdinfo.queue,kbdinfo.queue+1,kbdinfo.queuelen);
222 memmove(kbdinfo.ascii,kbdinfo.ascii+1,kbdinfo.queuelen);
227 void DOSVM_Int09SendScan( BYTE scan, BYTE ascii )
229 if (kbdinfo.queuelen == QUEUELEN) {
230 ERR("keyboard queue overflow\n");
231 return;
233 /* add scancode to queue */
234 kbdinfo.queue[kbdinfo.queuelen] = scan;
235 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);
240 BYTE DOSVM_Int09ReadScan( BYTE*ascii )
242 if (ascii) *ascii = kbdinfo.ascii[0];
243 return kbdinfo.queue[0];