Fixed some dll separation issues.
[wine] / dlls / winedos / int16.c
1 /*
2  * DOS interrupt 16h handler
3  *
4  * Copyright 1998 Joseph Pranevich
5  * Copyright 1999 Ove Kåven
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29
30 #include "module.h"
31 #include "dosexe.h"
32 #include "wincon.h"
33 #include "wine/debug.h"
34 #include "windef.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "miscemu.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(int);
40
41 /**********************************************************************
42  *          DOSVM_Int16Handler
43  *
44  * Handler for int 16h (keyboard)
45  *
46  * NOTE:
47  *
48  *    KEYB.COM (DOS >3.2) adds functions to this interrupt, they are
49  *    not currently listed here.
50  */
51
52 void WINAPI DOSVM_Int16Handler( CONTEXT86 *context )
53 {
54    switch AH_reg(context) {
55
56    case 0x00: /* Get Keystroke */
57       /* Returns: AH = Scan code
58                   AL = ASCII character */
59       TRACE("Get Keystroke\n");
60       DOSVM_Int16ReadChar(&AL_reg(context), &AH_reg(context), FALSE);
61       break;
62
63    case 0x01: /* Check for Keystroke */
64       /* Returns: ZF set if no keystroke */
65       /*          AH = Scan code */
66       /*          AL = ASCII character */
67       TRACE("Check for Keystroke\n");
68       if (!DOSVM_Int16ReadChar(&AL_reg(context), &AH_reg(context), TRUE))
69       {
70           SET_ZFLAG(context);
71       }
72       else
73       {
74           RESET_ZFLAG(context);
75       }
76       /* don't miss the opportunity to break some tight timing loop in DOS
77        * programs causing 100% CPU usage (by doing a Sleep here) */
78       Sleep(5);
79       break;
80
81    case 0x02: /* Get Shift Flags */
82       AL_reg(context) = 0;
83
84       if (GetAsyncKeyState(VK_RSHIFT))
85           AL_reg(context) |= 0x01;
86       if (GetAsyncKeyState(VK_LSHIFT))
87           AL_reg(context) |= 0x02;
88       if (GetAsyncKeyState(VK_LCONTROL) || GetAsyncKeyState(VK_RCONTROL))
89           AL_reg(context) |= 0x04;
90       if (GetAsyncKeyState(VK_LMENU) || GetAsyncKeyState(VK_RMENU))
91           AL_reg(context) |= 0x08;
92       if (GetAsyncKeyState(VK_SCROLL))
93           AL_reg(context) |= 0x10;
94       if (GetAsyncKeyState(VK_NUMLOCK))
95           AL_reg(context) |= 0x20;
96       if (GetAsyncKeyState(VK_CAPITAL))
97           AL_reg(context) |= 0x40;
98       if (GetAsyncKeyState(VK_INSERT))
99           AL_reg(context) |= 0x80;
100       TRACE("Get Shift Flags: returning 0x%02x\n", AL_reg(context));
101       break;
102
103    case 0x03: /* Set Typematic Rate and Delay */
104       FIXME("Set Typematic Rate and Delay - Not Supported\n");
105       break;
106
107    case 0x09: /* Get Keyboard Functionality */
108       FIXME("Get Keyboard Functionality - Not Supported\n");
109       /* As a temporary measure, say that "nothing" is supported... */
110       AL_reg(context) = 0;
111       break;
112
113    case 0x0a: /* Get Keyboard ID */
114       FIXME("Get Keyboard ID - Not Supported\n");
115       break;
116
117    case 0x10: /* Get Enhanced Keystroke */
118       TRACE("Get Enhanced Keystroke - Partially supported\n");
119       /* Returns: AH = Scan code
120                   AL = ASCII character */
121       DOSVM_Int16ReadChar(&AL_reg(context), &AH_reg(context), FALSE);
122       break;
123
124
125    case 0x11: /* Check for Enhanced Keystroke */
126       /* Returns: ZF set if no keystroke */
127       /*          AH = Scan code */
128       /*          AL = ASCII character */
129       TRACE("Check for Enhanced Keystroke - Partially supported\n");
130       if (!DOSVM_Int16ReadChar(&AL_reg(context), &AH_reg(context), TRUE))
131       {
132           SET_ZFLAG(context);
133       }
134       else
135       {
136           RESET_ZFLAG(context);
137       }
138       break;
139
140    case 0x12: /* Get Extended Shift States */
141       FIXME("Get Extended Shift States - Not Supported\n");
142       break;
143
144    default:
145       FIXME("Unknown INT 16 function - 0x%x\n", AH_reg(context));
146       break;
147
148    }
149 }
150
151 int WINAPI DOSVM_Int16ReadChar(BYTE*ascii,BYTE*scan,BOOL peek)
152 {
153   BIOSDATA *data = BIOS_DATA;
154   WORD CurOfs = data->NextKbdCharPtr;
155
156   /* check if there's data in buffer */
157   if (peek) {
158     if (CurOfs == data->FirstKbdCharPtr)
159       return 0;
160   } else {
161     while (CurOfs == data->FirstKbdCharPtr) {
162       /* no input available yet, so wait... */
163       DOSVM_Wait( -1, 0 );
164     }
165   }
166   /* read from keyboard queue */
167   TRACE("(%p,%p,%d) -> %02x %02x\n",ascii,scan,peek,((BYTE*)data)[CurOfs],((BYTE*)data)[CurOfs+1]);
168   if (ascii) *ascii = ((BYTE*)data)[CurOfs];
169   if (scan) *scan = ((BYTE*)data)[CurOfs+1];
170   if (!peek) {
171     CurOfs += 2;
172     if (CurOfs >= data->KbdBufferEnd) CurOfs = data->KbdBufferStart;
173     data->NextKbdCharPtr = CurOfs;
174   }
175   return 1;
176 }
177
178 int WINAPI DOSVM_Int16AddChar(BYTE ascii,BYTE scan)
179 {
180   BIOSDATA *data = BIOS_DATA;
181   WORD CurOfs = data->FirstKbdCharPtr;
182   WORD NextOfs = CurOfs + 2;
183
184   TRACE("(%02x,%02x)\n",ascii,scan);
185   if (NextOfs >= data->KbdBufferEnd) NextOfs = data->KbdBufferStart;
186   /* check if buffer is full */
187   if (NextOfs == data->NextKbdCharPtr) return 0;
188
189   /* okay, insert character in ring buffer */
190   ((BYTE*)data)[CurOfs] = ascii;
191   ((BYTE*)data)[CurOfs+1] = scan;
192
193   data->FirstKbdCharPtr = NextOfs;
194   return 1;
195 }