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