Added a first-cut version of MapVirtualKeyExW() that has the same
[wine] / msdos / int16.c
1 /*
2  * DOS interrupt 16h handler
3  */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #include "config.h"
10 #include "module.h"
11 #include "dosexe.h"
12 #include "wincon.h"
13 #include "debugtools.h"
14 #include "windef.h"
15 #include "wingdi.h"
16 #include "winuser.h"
17 #include "miscemu.h"
18
19 DEFAULT_DEBUG_CHANNEL(int16);
20
21 /**********************************************************************
22  *          INT_Int16Handler
23  *
24  * Handler for int 16h (keyboard)
25  *
26  * NOTE:
27  * 
28  *    KEYB.COM (DOS >3.2) adds functions to this interrupt, they are
29  *    not currently listed here.
30  */
31
32 void WINAPI INT_Int16Handler( CONTEXT86 *context )
33 {
34    switch AH_reg(context) {
35
36    case 0x00: /* Get Keystroke */
37       /* Returns: AH = Scan code
38                   AL = ASCII character */   
39       TRACE("Get Keystroke\n");
40       INT_Int16ReadChar(&AL_reg(context), &AH_reg(context), FALSE);
41       break;
42
43    case 0x01: /* Check for Keystroke */
44       /* Returns: ZF set if no keystroke */
45       /*          AH = Scan code */
46       /*          AL = ASCII character */
47       TRACE("Check for Keystroke\n");
48       if (!INT_Int16ReadChar(&AL_reg(context), &AH_reg(context), TRUE))
49       {
50           SET_ZFLAG(context);
51       }
52       else
53       {
54           RESET_ZFLAG(context);
55       }
56       break;
57
58    case 0x02: /* Get Shift Flags */      
59       AL_reg(context) = 0;
60
61 #if 0  /* FIXME: cannot call USER functions here */
62       if (GetAsyncKeyState(VK_RSHIFT))
63           AL_reg(context) |= 0x01;
64       if (GetAsyncKeyState(VK_LSHIFT))
65           AL_reg(context) |= 0x02;
66       if (GetAsyncKeyState(VK_LCONTROL) || GetAsyncKeyState(VK_RCONTROL))
67           AL_reg(context) |= 0x04;
68       if (GetAsyncKeyState(VK_LMENU) || GetAsyncKeyState(VK_RMENU))
69           AL_reg(context) |= 0x08;
70       if (GetAsyncKeyState(VK_SCROLL))
71           AL_reg(context) |= 0x10;
72       if (GetAsyncKeyState(VK_NUMLOCK))
73           AL_reg(context) |= 0x20;
74       if (GetAsyncKeyState(VK_CAPITAL))
75           AL_reg(context) |= 0x40;
76       if (GetAsyncKeyState(VK_INSERT))
77           AL_reg(context) |= 0x80;
78 #endif
79       TRACE("Get Shift Flags: returning 0x%02x\n", AL_reg(context));
80       break;
81
82    case 0x03: /* Set Typematic Rate and Delay */
83       FIXME("Set Typematic Rate and Delay - Not Supported\n");
84       break;
85
86    case 0x09: /* Get Keyboard Functionality */
87       FIXME("Get Keyboard Functionality - Not Supported\n");
88       /* As a temporary measure, say that "nothing" is supported... */
89       AL_reg(context) = 0;
90       break;
91
92    case 0x0a: /* Get Keyboard ID */
93       FIXME("Get Keyboard ID - Not Supported\n");
94       break;
95
96    case 0x10: /* Get Enhanced Keystroke */
97       TRACE("Get Enhanced Keystroke - Partially supported\n");
98       /* Returns: AH = Scan code
99                   AL = ASCII character */   
100       INT_Int16ReadChar(&AL_reg(context), &AH_reg(context), FALSE);
101       break;
102   
103
104    case 0x11: /* Check for Enhanced Keystroke */
105       /* Returns: ZF set if no keystroke */
106       /*          AH = Scan code */
107       /*          AL = ASCII character */
108       TRACE("Check for Enhanced Keystroke - Partially supported\n");
109       if (!INT_Int16ReadChar(&AL_reg(context), &AH_reg(context), TRUE))
110       {
111           SET_ZFLAG(context);
112       }
113       else
114       {
115           RESET_ZFLAG(context);
116       }
117       break;
118
119    case 0x12: /* Get Extended Shift States */
120       FIXME("Get Extended Shift States - Not Supported\n");
121       break;
122  
123    default:
124       FIXME("Unknown INT 16 function - 0x%x\n", AH_reg(context));   
125       break;
126
127    }
128 }
129
130 int WINAPI INT_Int16ReadChar(BYTE*ascii,BYTE*scan,BOOL peek)
131 {
132   BIOSDATA *data = DOSMEM_BiosData();
133   WORD CurOfs = data->NextKbdCharPtr;
134
135   /* check if there's data in buffer */
136   if (peek) {
137     if (CurOfs == data->FirstKbdCharPtr)
138       return 0;
139   } else {
140     while (CurOfs == data->FirstKbdCharPtr) {
141       /* no input available yet, so wait... */
142       DOSVM_Wait( -1, 0 );
143     }
144   }
145   /* read from keyboard queue */
146   TRACE("(%p,%p,%d) -> %02x %02x\n",ascii,scan,peek,((BYTE*)data)[CurOfs],((BYTE*)data)[CurOfs+1]);
147   if (ascii) *ascii = ((BYTE*)data)[CurOfs];
148   if (scan) *scan = ((BYTE*)data)[CurOfs+1];
149   if (!peek) {
150     CurOfs += 2;
151     if (CurOfs >= data->KbdBufferEnd) CurOfs = data->KbdBufferStart;
152     data->NextKbdCharPtr = CurOfs;
153   }
154   return 1;
155 }
156
157 int WINAPI INT_Int16AddChar(BYTE ascii,BYTE scan)
158 {
159   BIOSDATA *data = DOSMEM_BiosData();
160   WORD CurOfs = data->FirstKbdCharPtr;
161   WORD NextOfs = CurOfs + 2;
162
163   TRACE("(%02x,%02x)\n",ascii,scan);
164   if (NextOfs >= data->KbdBufferEnd) NextOfs = data->KbdBufferStart;
165   /* check if buffer is full */
166   if (NextOfs == data->NextKbdCharPtr) return 0;
167
168   /* okay, insert character in ring buffer */
169   ((BYTE*)data)[CurOfs] = ascii;
170   ((BYTE*)data)[CurOfs+1] = scan;
171
172   data->FirstKbdCharPtr = NextOfs;
173   return 1;
174 }