janitorial: Remove redundant NULL checks before calling HeapFree wrappers.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "dosexe.h"
31 #include "wincon.h"
32 #include "wine/debug.h"
33 #include "windef.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(int);
38
39 /**********************************************************************
40  *          DOSVM_Int16Handler (WINEDOS16.122)
41  *
42  * Handler for int 16h (keyboard)
43  *
44  * NOTE:
45  *
46  *    KEYB.COM (DOS >3.2) adds functions to this interrupt, they are
47  *    not currently listed here.
48  */
49
50 void WINAPI DOSVM_Int16Handler( CONTEXT86 *context )
51 {
52    BIOSDATA *data = NULL;
53    BYTE ascii, scan;
54
55    switch AH_reg(context) {
56
57    case 0x00: /* Get Keystroke */
58       /* Returns: AH = Scan code
59                   AL = ASCII character */
60       TRACE("Get Keystroke\n");
61       DOSVM_Int16ReadChar(&ascii, &scan, context);
62       SET_AL( context, ascii );
63       SET_AH( context, scan );
64       break;
65
66    case 0x01: /* Check for Keystroke */
67       /* Returns: ZF set if no keystroke */
68       /*          AH = Scan code */
69       /*          AL = ASCII character */
70       TRACE("Check for Keystroke\n");
71       if (!DOSVM_Int16ReadChar(&ascii, &scan, NULL))
72       {
73           SET_ZFLAG(context);
74       }
75       else
76       {
77           SET_AL( context, ascii );
78           SET_AH( context, scan );
79           RESET_ZFLAG(context);
80       }
81       /* don't miss the opportunity to break some tight timing loop in DOS
82        * programs causing 100% CPU usage (by doing a Sleep here) */
83       Sleep(5);
84       break;
85
86    case 0x02: /* Get Shift Flags */
87
88       /* read value from BIOS data segment's keyboard status flags field */
89       data = DOSVM_BiosData();
90       SET_AL( context, data->KbdFlags1 );
91
92       TRACE("Get Shift Flags: returning 0x%02x\n", AL_reg(context));
93       break;
94
95    case 0x03: /* Set Typematic Rate and Delay */
96       FIXME("Set Typematic Rate and Delay - Not Supported\n");
97       break;
98       
99    case 0x05:/*simulate  Keystroke*/ 
100       FIXME("Simulating a keystroke is not supported yet\n");
101       break;
102
103    case 0x09: /* Get Keyboard Functionality */
104       FIXME("Get Keyboard Functionality - Not Supported\n");
105       /* As a temporary measure, say that "nothing" is supported... */
106       SET_AL( context, 0 );
107       break;
108
109    case 0x0a: /* Get Keyboard ID */
110       FIXME("Get Keyboard ID - Not Supported\n");
111       break;
112
113    case 0x10: /* Get Enhanced Keystroke */
114       TRACE("Get Enhanced Keystroke - Partially supported\n");
115       /* Returns: AH = Scan code
116                   AL = ASCII character */
117       DOSVM_Int16ReadChar(&ascii, &scan, context);
118       SET_AL( context, ascii );
119       SET_AH( context, scan );
120       break;
121
122
123    case 0x11: /* Check for Enhanced Keystroke */
124       /* Returns: ZF set if no keystroke */
125       /*          AH = Scan code */
126       /*          AL = ASCII character */
127       TRACE("Check for Enhanced Keystroke - Partially supported\n");
128       if (!DOSVM_Int16ReadChar(&ascii, &scan, NULL))
129       {
130           SET_ZFLAG(context);
131       }
132       else
133       {
134           SET_AL( context, ascii );
135           SET_AH( context, scan );
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 /**********************************************************************
152  *          DOSVM_Int16ReadChar
153  *
154  * Either peek into keyboard buffer or wait for next keystroke.
155  *
156  * If waitctx is NULL, return TRUE if buffer had keystrokes and
157  * FALSE if buffer is empty. Returned keystroke will be left into buffer.
158  * 
159  * If waitctx is non-NULL, wait until keystrokes are available.
160  * Return value will always be TRUE and returned keystroke will be
161  * removed from buffer.
162  */
163 int WINAPI DOSVM_Int16ReadChar(BYTE *ascii, BYTE *scan, CONTEXT86 *waitctx)
164 {
165     BIOSDATA *data = DOSVM_BiosData();
166     WORD CurOfs = data->NextKbdCharPtr;
167
168     /* check if there's data in buffer */
169     if (waitctx)
170     {
171         /* wait until input is available... */
172         while (CurOfs == data->FirstKbdCharPtr)
173             DOSVM_Wait( waitctx );
174     }
175     else
176     {
177         if (CurOfs == data->FirstKbdCharPtr)
178             return FALSE;
179     }
180
181     /* read from keyboard queue */
182     TRACE( "(%p,%p,%p) -> %02x %02x\n", ascii, scan, waitctx,
183            ((BYTE*)data)[CurOfs], ((BYTE*)data)[CurOfs+1] );
184
185     if (ascii) *ascii = ((BYTE*)data)[CurOfs];
186     if (scan) *scan = ((BYTE*)data)[CurOfs+1];
187
188     if (waitctx) 
189     {
190         CurOfs += 2;
191         if (CurOfs >= data->KbdBufferEnd) CurOfs = data->KbdBufferStart;
192         data->NextKbdCharPtr = CurOfs;
193     }
194
195     return TRUE;
196 }
197
198 int WINAPI DOSVM_Int16AddChar(BYTE ascii,BYTE scan)
199 {
200   BIOSDATA *data = DOSVM_BiosData();
201   WORD CurOfs = data->FirstKbdCharPtr;
202   WORD NextOfs = CurOfs + 2;
203
204   TRACE("(%02x,%02x)\n",ascii,scan);
205   if (NextOfs >= data->KbdBufferEnd) NextOfs = data->KbdBufferStart;
206   /* check if buffer is full */
207   if (NextOfs == data->NextKbdCharPtr) return 0;
208
209   /* okay, insert character in ring buffer */
210   ((BYTE*)data)[CurOfs] = ascii;
211   ((BYTE*)data)[CurOfs+1] = scan;
212
213   data->FirstKbdCharPtr = NextOfs;
214   return 1;
215 }