Strncpy elimination.
[wine] / dlls / winedos / int09.c
1 /*
2  * DOS interrupt 09h handler (IRQ1 - KEYBOARD)
3  *
4  * Copyright 1999 Ove Kåven
5  *
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.
10  *
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.
15  *
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
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 "miscemu.h"
30 #include "wine/debug.h"
31 #include "dosexe.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(int);
34
35 #define QUEUELEN 31
36
37 static struct
38 {
39   BYTE queuelen,queue[QUEUELEN],ascii[QUEUELEN];
40 } kbdinfo;
41
42
43 /*
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.
49  */
50 void DOSVM_Int09UpdateKbdStatusFlags(BYTE scan, BOOL extended, BIOSDATA *data, BOOL *modifier)
51 {
52     BYTE realscan = scan & 0x7f; /* remove 0x80 make/break flag */
53     BYTE bit1 = 255, bit2 = 255;
54     INPUT_RECORD msg;
55     DWORD res;
56
57     *modifier = TRUE;
58
59     switch (realscan)
60     {
61       case 0x36: /* r shift */
62               bit1 = 0;
63               break;
64       case 0x2a: /* l shift */
65               bit1 = 1;
66               break;
67       case 0x1d: /* l/r control */
68               bit1 = 2;
69               if (!extended) /* left control only */
70                   bit2 = 0;
71               break;
72       case 0x37: /* SysRq inner parts */
73               /* SysRq scan code sequence: 38, e0, 37, e0, b7, b8 */
74               FIXME("SysRq not handled yet.\n");
75               break;
76       case 0x38: /* l/r menu/alt, SysRq outer parts */
77               bit1 = 3;
78               if (!extended) /* left alt only */
79                   bit2 = 1;
80               break;
81       case 0x46: /* scroll lock */
82               bit1 = 4;
83               if (!extended) /* left ctrl only */
84                   bit2 = 4;
85               break;
86       case 0x45: /* num lock, pause */
87               if (extended) /* distinguish from non-extended Pause key */
88               { /* num lock */
89                   bit1 = 5;
90                   bit2 = 5;
91               }
92               else
93               { /* pause */
94                   if (!(scan & 0x80)) /* "make" code */
95                       bit2 = 3;
96               }
97               break;
98       case 0x3a: /* caps lock */
99               bit1 = 6;
100               bit2 = 6;
101               break;
102       case 0x52: /* insert */
103               bit1 = 7;
104               bit2 = 7;
105               *modifier = FALSE; /* insert is no modifier: thus pass to int16 */
106               break;
107     }
108     /* now that we know which bits to set, update them */
109     if (!(scan & 0x80)) /* "make" code (keypress) */
110     {
111         if (bit2 != 255)
112         {
113             if (bit2 == 3)
114             {
115                 data->KbdFlags2 |= 1 << bit2; /* set "Pause" flag */
116                 TRACE("PAUSE key, sleeping !\n");
117                 /* wait for keypress to unlock pause */
118                 do {
119                     Sleep(55);
120                 } while (!(ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE),&msg,1,&res) && (msg.EventType == KEY_EVENT)));
121                 data->KbdFlags2 &= ~(1 << bit2); /* release "Pause" flag */
122             }
123             else
124                 data->KbdFlags2 |= 1 << bit2;
125         }
126         if (bit1 != 255)
127         {
128             if (bit1 < 4) /* key "pressed" flag */
129                 data->KbdFlags1 |= 1 << bit1;
130             else /* key "active" flag */
131                 data->KbdFlags1 ^= 1 << bit1;
132         }
133     }
134     else /* "break" / release */
135     {
136         if (bit2 != 255)
137             data->KbdFlags2 &= ~(1 << bit2);
138         if (bit1 < 4) /* is it a key "pressed" bit ? */
139             data->KbdFlags1 &= ~(1 << bit1);
140     }
141     TRACE("ext. %d, bits %d/%d, KbdFlags %02x/%02x\n", extended, bit1, bit2, data->KbdFlags1, data->KbdFlags2);
142 }
143
144 /**********************************************************************
145  *          DOSVM_Int09Handler (WINEDOS16.109)
146  *
147  * Handler for int 09h.
148  * See http://www.execpc.com/~geezer/osd/kbd/ for a very good description
149  * 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   BYTE realscan = scan & 0x7f; /* remove 0x80 make/break flag */
156   BOOL modifier = FALSE;
157   static BOOL extended = FALSE; /* indicates start of extended key sequence */
158   BYTE ch[2];
159   int cnt, c2;
160
161   TRACE("scan=%02x, ascii=%02x[%c]\n",scan, ascii, ascii ? ascii : ' ');
162
163   if (scan == 0xe0) /* extended keycode */
164       extended = TRUE;
165   
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);
177
178   if (scan != 0xe0)
179       extended = FALSE; /* reset extended flag now */
180
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))
184   {
185     if (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 */
189       else
190         ch[0] = ascii;
191       /* FIXME: need to handle things such as Shift-F1 etc. */
192       cnt = 1;
193     } else {
194       /* translate */
195       UINT vkey = MapVirtualKeyA(scan&0x7f, 1);
196       BYTE keystate[256];
197       GetKeyboardState(keystate);
198       cnt = ToAscii(vkey, scan, keystate, (LPWORD)ch, 0);
199     }
200     if (cnt>0) {
201       for (c2=0; c2<cnt; c2++)
202         DOSVM_Int16AddChar(ch[c2], scan);
203     } else
204     if (cnt==0) {
205       /* FIXME: need to handle things like shift-F-keys,
206        * 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      * 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);
224     }
225   }
226 }
227
228 void WINAPI DOSVM_Int09SendScan( BYTE scan, BYTE ascii )
229 {
230   if (kbdinfo.queuelen == QUEUELEN) {
231     ERR("keyboard queue overflow\n");
232     return;
233   }
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);
239 }
240
241 /**********************************************************************
242  *          KbdReadScan (WINEDOS.@)
243  */
244 BYTE WINAPI DOSVM_Int09ReadScan( BYTE*ascii )
245 {
246     if (ascii) *ascii = kbdinfo.ascii[0];
247     return kbdinfo.queue[0];
248 }