gdiplus: Avoid not necessary memory allocation for palette entries.
[wine] / dlls / krnl386.exe16 / int33.c
1 /*
2  * DOS interrupt 33h handler
3  *
4  * Copyright 1999 Ove Kåven
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "dosexe.h"
30 #include "vga.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(int);
34
35 static struct
36 {
37     WORD x, y, but;
38     WORD lbcount, rbcount, rlastx, rlasty, llastx, llasty;
39     FARPROC16 callback;
40     WORD callmask;
41     WORD VMPratio, HMPratio, oldx, oldy;
42     WORD hide_count;
43 } mouse_info;
44
45
46 /**********************************************************************
47  *          INT33_ResetMouse
48  *
49  * Handler for:
50  * - subfunction 0x00 (reset mouse)
51  * - subfunction 0x21 (software reset)
52  */
53 static void INT33_ResetMouse( CONTEXT *context )
54 {
55     memset( &mouse_info, 0, sizeof(mouse_info) );
56     
57     /* Set the default mickey/pixel ratio */
58     mouse_info.HMPratio = 8;
59     mouse_info.VMPratio = 16;
60
61     /* Hide the mouse cursor */
62     mouse_info.hide_count = 1;
63     VGA_ShowMouse( FALSE );    
64
65     if (context)
66     {
67         SET_AX( context, 0xFFFF ); /* driver installed */
68         SET_BX( context, 3 );      /* number of buttons */
69     }
70 }
71
72
73 /**********************************************************************
74  *          DOSVM_Int33Handler
75  *
76  * Handler for int 33h (MS MOUSE).
77  */
78 void WINAPI DOSVM_Int33Handler( CONTEXT *context )
79 {
80     switch (AX_reg(context))
81     {
82     case 0x0000:
83         TRACE("Reset mouse driver and request status\n");
84         INT33_ResetMouse( context );
85         break;
86
87     case 0x0001:
88         TRACE("Show mouse cursor, old hide count: %d\n",
89               mouse_info.hide_count);
90         if (mouse_info.hide_count >= 1)
91             mouse_info.hide_count--;
92         if (!mouse_info.hide_count)
93             VGA_ShowMouse( TRUE );
94         break;
95
96     case 0x0002:
97         TRACE("Hide mouse cursor, old hide count: %d\n",
98               mouse_info.hide_count);
99         if(!mouse_info.hide_count)
100             VGA_ShowMouse( FALSE );            
101         mouse_info.hide_count++;
102         break;
103
104     case 0x0003:
105         TRACE("Return mouse position and button status: (%d,%d) and %d\n",
106               mouse_info.x, mouse_info.y, mouse_info.but);
107         SET_BX( context, mouse_info.but );
108         SET_CX( context, mouse_info.x );
109         SET_DX( context, mouse_info.y );
110         break;
111
112     case 0x0004:
113         FIXME("Position mouse cursor\n");
114         break;
115
116     case 0x0005:
117         TRACE("Return Mouse button press Information for %s mouse button\n",
118               BX_reg(context) ? "right" : "left");
119         if (BX_reg(context)) 
120         {
121             SET_BX( context, mouse_info.rbcount );
122             mouse_info.rbcount = 0;
123             SET_CX( context, mouse_info.rlastx );
124             SET_DX( context, mouse_info.rlasty );
125         } 
126         else 
127         {
128             SET_BX( context, mouse_info.lbcount );
129             mouse_info.lbcount = 0;
130             SET_CX( context, mouse_info.llastx );
131             SET_DX( context, mouse_info.llasty );
132         }
133         SET_AX( context, mouse_info.but );
134         break;
135
136     case 0x0007:
137         FIXME("Define horizontal mouse cursor range %d..%d\n",
138               CX_reg(context), DX_reg(context));
139         break;
140
141     case 0x0008:
142         FIXME("Define vertical mouse cursor range %d..%d\n",
143               CX_reg(context), DX_reg(context));
144         break;
145
146     case 0x0009:
147         FIXME("Define graphics mouse cursor\n");
148         break;
149
150     case 0x000A:
151         FIXME("Define text mouse cursor\n");
152         break;
153
154     case 0x000B:
155         TRACE("Read Mouse motion counters\n");
156         {
157             int dx = ((int)mouse_info.x - (int)mouse_info.oldx)
158                 * (mouse_info.HMPratio / 8);
159             int dy = ((int)mouse_info.y - (int)mouse_info.oldy)
160                 * (mouse_info.VMPratio / 8);
161
162             SET_CX( context, (WORD)dx );
163             SET_DX( context, (WORD)dy );
164
165             mouse_info.oldx = mouse_info.x;
166             mouse_info.oldy = mouse_info.y;
167         }
168         break;
169
170     case 0x000C:
171         TRACE("Define mouse interrupt subroutine\n");
172         mouse_info.callmask = CX_reg(context);
173         mouse_info.callback = (FARPROC16)MAKESEGPTR(context->SegEs, 
174                                                     DX_reg(context));
175         break;
176
177     case 0x000F:
178         TRACE("Set mickey/pixel ratio\n");
179         mouse_info.HMPratio = CX_reg(context);
180         mouse_info.VMPratio = DX_reg(context);
181         break;
182
183     case 0x0010:
184         FIXME("Define screen region for update\n");
185         break;
186
187     case 0x0015:
188         TRACE("Get mouse driver state and memory requirements\n");
189         SET_BX(context, sizeof(mouse_info));
190         break;
191
192     case 0x0021:
193         TRACE("Software reset\n");
194         INT33_ResetMouse( context );
195         break;
196
197     default:
198         INT_BARF(context,0x33);
199     }
200 }
201
202 typedef struct {
203   FARPROC16 proc;
204   WORD mask,but,x,y,mx,my;
205 } MCALLDATA;
206
207 static void MouseRelay(CONTEXT *context,void *mdata)
208 {
209   MCALLDATA *data = mdata;
210   CONTEXT ctx = *context;
211
212   if (!ISV86(&ctx))
213   {
214       ctx.EFlags |= V86_FLAG;
215       ctx.SegSs = 0; /* Allocate new stack. */
216   }
217
218   ctx.Eax   = data->mask;
219   ctx.Ebx   = data->but;
220   ctx.Ecx   = data->x;
221   ctx.Edx   = data->y;
222   ctx.Esi   = data->mx;
223   ctx.Edi   = data->my;
224   ctx.SegCs = SELECTOROF(data->proc);
225   ctx.Eip   = OFFSETOF(data->proc);
226   HeapFree(GetProcessHeap(), 0, data);
227   DPMI_CallRMProc(&ctx, NULL, 0, 0);
228 }
229
230 static void QueueMouseRelay(DWORD mx, DWORD my, WORD mask)
231 {
232   mouse_info.x = mx;
233   mouse_info.y = my;
234
235   /* Left button down */
236   if(mask & 0x02) {
237     mouse_info.but |= 0x01;
238     mouse_info.llastx = mx;
239     mouse_info.llasty = my;
240     mouse_info.lbcount++;
241   }
242
243   /* Left button up */
244   if(mask & 0x04) {
245     mouse_info.but &= ~0x01;
246   }
247
248   /* Right button down */
249   if(mask & 0x08) {
250     mouse_info.but |= 0x02;
251     mouse_info.rlastx = mx;
252     mouse_info.rlasty = my;
253     mouse_info.rbcount++;
254   }
255
256   /* Right button up */
257   if(mask & 0x10) {
258     mouse_info.but &= ~0x02;
259   }
260
261   /* Middle button down */
262   if(mask & 0x20) {
263     mouse_info.but |= 0x04;
264   }
265
266   /* Middle button up */
267   if(mask & 0x40) {
268     mouse_info.but &= ~0x04;
269   }
270
271   if ((mask & mouse_info.callmask) && mouse_info.callback) {
272     MCALLDATA *data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MCALLDATA));
273     data->proc = mouse_info.callback;
274     data->mask = mask & mouse_info.callmask;
275     data->but = mouse_info.but;
276     data->x = mouse_info.x;
277     data->y = mouse_info.y;
278
279     /*
280      * Fake mickeys. 
281      *
282      * FIXME: This is not entirely correct. If mouse if moved to the edge
283      *        of the screen, mouse will stop moving and mickeys won't
284      *        be updated even though they should be.
285      */
286     data->mx = mouse_info.x * (mouse_info.HMPratio / 8);
287     data->my = mouse_info.y * (mouse_info.VMPratio / 8);
288
289     DOSVM_QueueEvent(-1, DOS_PRIORITY_MOUSE, MouseRelay, data);
290   }
291 }
292
293 void DOSVM_Int33Message(UINT message,WPARAM wParam,LPARAM lParam)
294 {
295   WORD mask = 0;
296   unsigned Height, Width, SX=1, SY=1;
297
298   if (!VGA_GetMode(&Height,&Width,NULL)) {
299     /* may need to do some coordinate scaling */
300     if (Width)
301       SX = 640/Width;
302     if (!SX) SX=1;
303   }
304
305   switch (message) {
306   case WM_MOUSEMOVE:
307     mask |= 0x01;
308     break;
309   case WM_LBUTTONDOWN:
310   case WM_LBUTTONDBLCLK:
311     mask |= 0x02;
312     break;
313   case WM_LBUTTONUP:
314     mask |= 0x04;
315     break;
316   case WM_RBUTTONDOWN:
317   case WM_RBUTTONDBLCLK:
318     mask |= 0x08;
319     break;
320   case WM_RBUTTONUP:
321     mask |= 0x10;
322     break;
323   case WM_MBUTTONDOWN:
324   case WM_MBUTTONDBLCLK:
325     mask |= 0x20;
326     break;
327   case WM_MBUTTONUP:
328     mask |= 0x40;
329     break;
330   }
331
332   QueueMouseRelay(LOWORD(lParam) * SX,
333                  HIWORD(lParam) * SY,
334                  mask);
335 }
336
337 void DOSVM_Int33Console(MOUSE_EVENT_RECORD *record)
338 {
339   unsigned Height, Width;
340   WORD mask = 0;
341   BOOL newLeftButton = record->dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED;
342   BOOL oldLeftButton = mouse_info.but & 0x01;
343   BOOL newRightButton = record->dwButtonState & RIGHTMOST_BUTTON_PRESSED;
344   BOOL oldRightButton = mouse_info.but & 0x02;
345   BOOL newMiddleButton = record->dwButtonState & FROM_LEFT_2ND_BUTTON_PRESSED;
346   BOOL oldMiddleButton = mouse_info.but & 0x04;
347
348   if(newLeftButton && !oldLeftButton)
349     mask |= 0x02;
350   else if(!newLeftButton && oldLeftButton)
351     mask |= 0x04;
352
353   if(newRightButton && !oldRightButton)
354     mask |= 0x08;
355   else if(!newRightButton && oldRightButton)
356     mask |= 0x10;
357
358   if(newMiddleButton && !oldMiddleButton)
359     mask |= 0x20;
360   else if(!newMiddleButton && oldMiddleButton)
361     mask |= 0x40;
362   
363   if (VGA_GetAlphaMode(&Width, &Height))
364     QueueMouseRelay( 640 / Width * record->dwMousePosition.X,
365                      200 / Height * record->dwMousePosition.Y,
366                      mask );
367 }