Added a first-cut version of MapVirtualKeyExW() that has the same
[wine] / msdos / int09.c
1 /*
2  * DOS interrupt 09h handler (IRQ1 - KEYBOARD)
3  */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include "windef.h"
8 #include "wingdi.h"
9 #include "winuser.h"
10 #include "miscemu.h"
11 #include "debugtools.h"
12 #include "dosexe.h"
13
14 DEFAULT_DEBUG_CHANNEL(int);
15
16 #define QUEUELEN 31
17
18 static struct
19 {
20   BYTE queuelen,queue[QUEUELEN],ascii[QUEUELEN];
21 } kbdinfo;
22
23
24 /**********************************************************************
25  *          INT_Int09Handler
26  *
27  * Handler for int 09h.
28  */
29 void WINAPI INT_Int09Handler( CONTEXT86 *context )
30 {
31   BYTE ascii, scan = INT_Int09ReadScan(&ascii);
32   BYTE ch[2];
33   int cnt, c2;
34
35   TRACE("scan=%02x\n",scan);
36   if (!(scan & 0x80)) {
37     if (ascii) {
38       /* we already have an ASCII code, no translation necessary */
39       ch[0] = ascii;
40       cnt = 1;
41     } else {
42 #if 0  /* FIXME: cannot call USER functions here */
43       UINT vkey = MapVirtualKeyA(scan&0x7f, 1);
44       /* as in TranslateMessage, windows/input.c */
45       cnt = ToAscii(vkey, scan, QueueKeyStateTable, (LPWORD)ch, 0);
46 #else
47       cnt = 0;
48 #endif
49     }
50     if (cnt>0) {
51       for (c2=0; c2<cnt; c2++)
52         INT_Int16AddChar(ch[c2], scan);
53     } else
54     if (cnt==0) {
55       /* FIXME: need to handle things like shift-F-keys,
56        * 0xE0 extended keys, etc */
57       INT_Int16AddChar(0, scan);
58     }
59   }
60   DOSVM_PIC_ioport_out(0x20, 0x20); /* send EOI */
61 }
62
63 static void KbdRelay( CONTEXT86 *context, void *data )
64 {
65   if (kbdinfo.queuelen) {
66     /* cleanup operation, called from DOSVM_PIC_ioport_out:
67      * we'll remove current scancode from keyboard buffer here,
68      * rather than in ReadScan, because some DOS apps depend on
69      * the scancode being available for reading multiple times... */
70     if (--kbdinfo.queuelen) {
71       memmove(kbdinfo.queue,kbdinfo.queue+1,kbdinfo.queuelen);
72       memmove(kbdinfo.ascii,kbdinfo.ascii+1,kbdinfo.queuelen);
73     }
74   }
75 }
76
77 void WINAPI INT_Int09SendScan( BYTE scan, BYTE ascii )
78 {
79   if (kbdinfo.queuelen == QUEUELEN) {
80     ERR("keyboard queue overflow\n");
81     return;
82   }
83   /* add scancode to queue */
84   kbdinfo.queue[kbdinfo.queuelen] = scan;
85   kbdinfo.ascii[kbdinfo.queuelen++] = ascii;
86   /* tell app to read it by triggering IRQ 1 (int 09) */
87   DOSVM_QueueEvent(1,DOS_PRIORITY_KEYBOARD,KbdRelay,NULL);
88 }
89
90 BYTE WINAPI INT_Int09ReadScan( BYTE*ascii )
91 {
92     if (ascii) *ascii = kbdinfo.ascii[0];
93     return kbdinfo.queue[0];
94 }