Removed #include of wingdi.h and windef.h from winuser.h (and resolved
[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 "console.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       CONSOLE_GetKeystroke(&AH_reg(context), &AL_reg(context));
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 (!CONSOLE_CheckForKeystroke(&AH_reg(context), &AL_reg(context)))
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 (GetAsyncKeyState(VK_RSHIFT))
62           AL_reg(context) |= 0x01;
63       if (GetAsyncKeyState(VK_LSHIFT))
64           AL_reg(context) |= 0x02;
65       if (GetAsyncKeyState(VK_LCONTROL) || GetAsyncKeyState(VK_RCONTROL))
66           AL_reg(context) |= 0x04;
67       if (GetAsyncKeyState(VK_LMENU) || GetAsyncKeyState(VK_RMENU))
68           AL_reg(context) |= 0x08;
69       if (GetAsyncKeyState(VK_SCROLL))
70           AL_reg(context) |= 0x10;
71       if (GetAsyncKeyState(VK_NUMLOCK))
72           AL_reg(context) |= 0x20;
73       if (GetAsyncKeyState(VK_CAPITAL))
74           AL_reg(context) |= 0x40;
75       if (GetAsyncKeyState(VK_INSERT))
76           AL_reg(context) |= 0x80;
77       TRACE("Get Shift Flags: returning 0x%02x\n", AL_reg(context));
78       break;
79
80    case 0x03: /* Set Typematic Rate and Delay */
81       FIXME("Set Typematic Rate and Delay - Not Supported\n");
82       break;
83
84    case 0x09: /* Get Keyboard Functionality */
85       FIXME("Get Keyboard Functionality - Not Supported\n");
86       /* As a temporary measure, say that "nothing" is supported... */
87       AL_reg(context) = 0;
88       break;
89
90    case 0x0a: /* Get Keyboard ID */
91       FIXME("Get Keyboard ID - Not Supported\n");
92       break;
93
94    case 0x10: /* Get Enhanced Keystroke */
95       TRACE("Get Enhanced Keystroke - Partially supported\n");
96       /* Returns: AH = Scan code
97                   AL = ASCII character */   
98       CONSOLE_GetKeystroke(&AH_reg(context), &AL_reg(context));
99       break;
100   
101
102    case 0x11: /* Check for Enhanced Keystroke */
103       /* Returns: ZF set if no keystroke */
104       /*          AH = Scan code */
105       /*          AL = ASCII character */
106       TRACE("Check for Enhanced Keystroke - Partially supported\n");
107       if (!CONSOLE_CheckForKeystroke(&AH_reg(context), &AL_reg(context)))
108       {
109           SET_ZFLAG(context);
110       }
111       else
112       {
113           RESET_ZFLAG(context);
114       }
115       break;
116
117    case 0x12: /* Get Extended Shift States */
118       FIXME("Get Extended Shift States - Not Supported\n");
119       break;
120  
121    default:
122       FIXME("Unknown INT 16 function - 0x%x\n", AH_reg(context));   
123       break;
124
125    }
126 }
127
128 int WINAPI INT_Int16ReadChar(BYTE*ascii,BYTE*scan,BOOL peek)
129 {
130   BIOSDATA *data = DOSMEM_BiosData();
131   WORD CurOfs = data->NextKbdCharPtr;
132
133   /* check if there's data in buffer */
134   if (peek) {
135     if (CurOfs == data->FirstKbdCharPtr)
136       return 0;
137   } else {
138     while (CurOfs == data->FirstKbdCharPtr) {
139       /* no input available yet, so wait... */
140       DOSVM_Wait( -1, 0 );
141     }
142   }
143   /* read from keyboard queue */
144   TRACE("(%p,%p,%d) -> %02x %02x\n",ascii,scan,peek,((BYTE*)data)[CurOfs],((BYTE*)data)[CurOfs+1]);
145   if (ascii) *ascii = ((BYTE*)data)[CurOfs];
146   if (scan) *scan = ((BYTE*)data)[CurOfs+1];
147   if (!peek) {
148     CurOfs += 2;
149     if (CurOfs >= data->KbdBufferEnd) CurOfs = data->KbdBufferStart;
150     data->NextKbdCharPtr = CurOfs;
151   }
152   return 1;
153 }
154
155 int WINAPI INT_Int16AddChar(BYTE ascii,BYTE scan)
156 {
157   BIOSDATA *data = DOSMEM_BiosData();
158   WORD CurOfs = data->FirstKbdCharPtr;
159   WORD NextOfs = CurOfs + 2;
160
161   TRACE("(%02x,%02x)\n",ascii,scan);
162   if (NextOfs >= data->KbdBufferEnd) NextOfs = data->KbdBufferStart;
163   /* check if buffer is full */
164   if (NextOfs == data->NextKbdCharPtr) return 0;
165
166   /* okay, insert character in ring buffer */
167   ((BYTE*)data)[CurOfs] = ascii;
168   ((BYTE*)data)[CurOfs+1] = scan;
169
170   data->FirstKbdCharPtr = NextOfs;
171   return 1;
172 }