aboutsummaryrefslogtreecommitdiffstats
path: root/vxd_mouse.c
blob: 710f5d276b637a20762feb9a9b6030988e39b82a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*****************************************************************************

Copyright (c) 2024 Jaroslav Hensl <emulator@emulace.cz>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*****************************************************************************/

#include "winhack.h"
#include "vmm.h"
#include "vxd.h"

#include "vxd_lib.h"
#include "3d_accel.h"

#include "cursor.h"

#ifdef SVGA
#include "vxd_svga.h"
#endif

#include "code32.h"

extern FBHDA_t *hda;

static void *mouse_buffer_mem = NULL;
static void *mouse_andmask_data = NULL;
static void *mouse_xormask_data = NULL;
static void *mouse_swap_data = NULL;
static DWORD mouse_mem_size = 0;

static int mouse_x = 0;
static int mouse_y = 0;

static int mouse_w = 0;
static int mouse_h = 0;
static int mouse_pointx = 0;
static int mouse_pointy = 0;
static int mouse_swap_x = 0;
static int mouse_swap_y = 0;
static int mouse_swap_w = 0;
static int mouse_swap_h = 0;
static int mouse_swap_valid = FALSE;
static int mouse_ps = 0;

static BOOL  mouse_valid = FALSE;
static BOOL  mouse_empty = FALSE;
static BOOL  mouse_visible  = FALSE;

#define CUR_MIN_SIZE (32*32*4)

#include "vxd_strings.h"

#include "vxd_mouse_conv.h"

void *mouse_buffer()
{
	if(mouse_buffer_mem == NULL)
	{
		mouse_buffer_mem = (void*)_PageAllocate(RoundToPages(MOUSE_BUFFER_SIZE), PG_SYS, 0, 0, 0x0, 0x100000, NULL, PAGEFIXED);
	}
	
	return mouse_buffer_mem;
}

static void mouse_notify_accel()
{
	if(mouse_valid && mouse_visible && !mouse_empty)
	{
		hda->flags &= ~((DWORD)FB_MOUSE_NO_BLIT);
	}
	else
	{
		hda->flags |= FB_MOUSE_NO_BLIT;
	}
}

BOOL mouse_load()
{
	DWORD ms = 0;
	void *andmask_ptr;
	void *xormask_ptr;
	CURSORSHAPE *cur;
	DWORD cbw;
	
	//dbg_printf(dbg_mouse_load);
	
	if(!mouse_buffer_mem) return FALSE;
		
#ifdef SVGA
	if(SVGA_mouse_hw())
	{
		BOOL r = SVGA_mouse_load();
		mouse_valid = FALSE;
		mouse_notify_accel();
		return r;
	}
#endif

	cur = (CURSORSHAPE*)mouse_buffer_mem;
	
	/* erase cursor if present */
	FBHDA_access_begin(0);
	
	mouse_valid = FALSE;
	
	/* check and alocate/resize buffer */
	ms = (cur->cx * cur->cy * 4);
	if(ms < CUR_MIN_SIZE)
		ms = CUR_MIN_SIZE;
	
	if(ms > mouse_mem_size)
	{
		if(mouse_andmask_data)
			_PageFree(mouse_andmask_data, 0);
		
		if(mouse_xormask_data)
			_PageFree(mouse_xormask_data, 0);
			
		if(mouse_swap_data)
			_PageFree(mouse_swap_data, 0);
			
		mouse_andmask_data = 
			(void*)_PageAllocate(RoundToPages(ms), PG_SYS, 0, 0, 0x0, 0x100000, NULL, PAGEFIXED);
		
		mouse_xormask_data = 
			(void*)_PageAllocate(RoundToPages(ms), PG_SYS, 0, 0, 0x0, 0x100000, NULL, PAGEFIXED);
		
		mouse_swap_data = 
			(void*)_PageAllocate(RoundToPages(ms), PG_SYS, 0, 0, 0x0, 0x100000, NULL, PAGEFIXED);
			
		mouse_mem_size = ms;
	}
	
	/* can't allocate memory */
	if(mouse_andmask_data == NULL ||
		mouse_xormask_data == NULL ||
		mouse_swap_data == NULL)
	{
		dbg_printf(dbg_mouse_no_mem);
		FBHDA_access_end(0);
		return FALSE;
	}
	
	mouse_w = cur->cx;
	mouse_h = cur->cy;
	mouse_pointx = cur->xHotSpot;
	mouse_pointy = cur->yHotSpot;
	
	andmask_ptr = (void*)(cur + 1);
	xormask_ptr = (void*)(((BYTE*)andmask_ptr) + cur->cbWidth*cur->cy);
	
	mouse_ps = (hda->bpp + 7) / 8;
	
	cbw = (mouse_w+7)/8;
	
	/* AND mask (always 1bpp) */
	convmask(cur, cbw, andmask_ptr, mouse_andmask_data);
	
	/* XOR mask (1bpp or screen bpp) */
	if(cur->BitsPixel == 1)
	{
		convmask(cur, cbw, xormask_ptr, mouse_xormask_data);
	}
	else
	{
		memcpy(mouse_xormask_data, xormask_ptr, 
			((cur->BitsPixel + 7)/8) * cur->cx * cur->cy
		);
	}
	
	mouse_valid = TRUE;
	mouse_visible = TRUE;
	mouse_empty = cursor_is_empty();
	
	//dbg_printf(dbg_mouse_status, mouse_valid, mouse_visible, mouse_empty);
	
	//dbg_printf(dbg_cursor_empty, mouse_empty);

	/* blit new cursor */
	FBHDA_access_end(0);
	
	mouse_notify_accel();
	
	return TRUE;
}

void mouse_move(int x, int y)
{
	//dbg_printf(dbg_mouse_move, x, y);
	
#ifdef SVGA
	if(SVGA_mouse_hw())
	{
		SVGA_mouse_move(x, y);
		return;
	}
#endif
	
	if(mouse_valid && mouse_visible && !mouse_empty)
	{
		FBHDA_access_begin(FBHDA_ACCESS_MOUSE_MOVE);
		mouse_x = x;
		mouse_y = y;
		FBHDA_access_end(FBHDA_ACCESS_MOUSE_MOVE);
	}
	else
	{
		mouse_x = x;
		mouse_y = y;
	}
}

void mouse_show()
{
	//dbg_printf(dbg_mouse_show);
	
#ifdef SVGA
	if(SVGA_mouse_hw())
	{
		SVGA_mouse_show();
		return;
	}
#endif
	FBHDA_access_begin(0);
	mouse_visible = TRUE;
	FBHDA_access_end(0);
	
	mouse_notify_accel();
}

void mouse_hide()
{
	//dbg_printf(dbg_mouse_hide);
	
#ifdef SVGA
	if(SVGA_mouse_hw())
	{
		SVGA_mouse_hide(FALSE);
		return;
	}
#endif
	
	FBHDA_access_begin(0);
	mouse_visible = FALSE;
	FBHDA_access_end(0);
	
	mouse_notify_accel();
}

void mouse_invalidate()
{
	//dbg_printf(dbg_mouse_invalidate);
	
#ifdef SVGA
	if(SVGA_mouse_hw())
	{
		SVGA_mouse_hide(TRUE);
		return;
	}
#endif
	
	mouse_valid = FALSE;
	
	mouse_notify_accel();
}

/* called by FBHDA_access_end */
BOOL mouse_blit()
{
	if(mouse_valid && mouse_visible && !mouse_empty)
	{
		draw_save(mouse_x, mouse_y);
		draw_blit(mouse_x, mouse_y);
		return TRUE;
	}
	
	return FALSE;
}

/* called by FBHDA_access_begin */
void mouse_erase()
{
	if(mouse_valid && mouse_visible && !mouse_empty)
	{
		draw_restore();
	}
}

BOOL mouse_get_rect(DWORD *ptr_left, DWORD *ptr_top,
	DWORD *ptr_right, DWORD *ptr_bottom)
{
	int mx;
	int my;
	int mw;
	int mh;
	
	if(mouse_valid && !mouse_empty)
	{
		mw = mouse_w;
		mx = mouse_x - mouse_pointx;
		
		if(mx < 0)
		{
			mw += mx;
			mx = 0;
		}
		
		if(mx + mw > hda->width)
		{
			mw = hda->width - mx;
		}
		
		mh = mouse_h;
		my = mouse_y - mouse_pointy;
		
		if(my < 0)
		{
			mh += my;
			my = 0;
		}
		
		if(my + mh > hda->height)
		{
			mh = hda->height - my;
		}
		
		if(mw > 0 && mh > 0)
		{
			*ptr_left    = mx;
			*ptr_top     = my;
			*ptr_right   = mx + mw;
			*ptr_bottom  = my + mh;
			
			return TRUE;
		}
		
		return TRUE;
	}
	
	return FALSE;
}