src/video/wincommon/SDL_sysmouse.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 06 Jan 2006 13:20:10 +0000
changeset 1234 73676c1f56ee
parent 527 5c74ac147358
child 1251 86d0d01290ea
permissions -rw-r--r--
For sanity's sake, removed the '&' when passing copy_row array to asm.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>
slouken@0
    29
#include <windows.h>
slouken@0
    30
slouken@0
    31
#include "SDL_error.h"
slouken@0
    32
#include "SDL_mouse.h"
slouken@0
    33
#include "SDL_sysmouse_c.h"
slouken@0
    34
#include "SDL_events_c.h"
slouken@0
    35
#include "SDL_cursor_c.h"
slouken@0
    36
#include "SDL_lowvideo.h"
slouken@0
    37
slouken@0
    38
#ifdef _WIN32_WCE
slouken@0
    39
#define USE_STATIC_CURSOR
slouken@0
    40
#endif
slouken@0
    41
slouken@0
    42
HCURSOR	SDL_hcursor = NULL;		/* Exported for SDL_eventloop.c */
slouken@0
    43
slouken@0
    44
/* The implementation dependent data for the window manager cursor */
slouken@0
    45
/* For some reason when creating a windows cursor, the ands and xors memory
slouken@0
    46
   is not copied, so we need to keep track of it and free it when we are done
slouken@0
    47
   with the cursor.  If we free the memory prematurely, the app crashes. :-}
slouken@0
    48
*/
slouken@0
    49
struct WMcursor {
slouken@0
    50
	HCURSOR curs;
slouken@0
    51
#ifndef USE_STATIC_CURSOR
slouken@0
    52
	Uint8 *ands;
slouken@0
    53
	Uint8 *xors;
slouken@0
    54
#endif
slouken@0
    55
};
slouken@0
    56
slouken@0
    57
/* Convert bits to padded bytes */
slouken@0
    58
#define PAD_BITS(bits)	((bits+7)/8)
slouken@0
    59
slouken@0
    60
#ifdef CURSOR_DEBUG
slouken@0
    61
static void PrintBITMAP(FILE *out, char *bits, int w, int h)
slouken@0
    62
{
slouken@0
    63
	int i;
slouken@0
    64
	unsigned char ch;
slouken@0
    65
slouken@0
    66
	while ( h-- > 0 ) {
slouken@0
    67
		for ( i=0; i<w; ++i ) {
slouken@0
    68
			if ( (i%8) == 0 )
slouken@0
    69
				ch = *bits++;
slouken@0
    70
			if ( ch&0x80 )
slouken@0
    71
				fprintf(out, "X");
slouken@0
    72
			else
slouken@0
    73
				fprintf(out, " ");
slouken@0
    74
			ch <<= 1;
slouken@0
    75
		}
slouken@0
    76
		fprintf(out, "\n");
slouken@0
    77
	}
slouken@0
    78
}
slouken@0
    79
#endif
slouken@0
    80
slouken@0
    81
#ifndef USE_STATIC_CURSOR
slouken@0
    82
/* Local functions to convert the SDL cursor mask into Windows format */
slouken@0
    83
static void memnot(Uint8 *dst, Uint8 *src, int len)
slouken@0
    84
{
slouken@0
    85
	while ( len-- > 0 )
slouken@0
    86
		*dst++ = ~*src++;
slouken@0
    87
}
slouken@0
    88
static void memxor(Uint8 *dst, Uint8 *src1, Uint8 *src2, int len)
slouken@0
    89
{
slouken@0
    90
	while ( len-- > 0 )
slouken@0
    91
		*dst++ = (*src1++)^(*src2++);
slouken@0
    92
}
slouken@0
    93
#endif /* !USE_STATIC_CURSOR */
slouken@0
    94
slouken@0
    95
void WIN_FreeWMCursor(_THIS, WMcursor *cursor)
slouken@0
    96
{
slouken@0
    97
#ifndef USE_STATIC_CURSOR
slouken@506
    98
	if ( cursor->curs == GetCursor() )
slouken@506
    99
		SetCursor(NULL);
slouken@0
   100
	if ( cursor->curs != NULL )
slouken@0
   101
		DestroyCursor(cursor->curs);
slouken@0
   102
	if ( cursor->ands != NULL )
slouken@0
   103
		free(cursor->ands);
slouken@0
   104
	if ( cursor->xors != NULL )
slouken@0
   105
		free(cursor->xors);
slouken@0
   106
#endif /* !USE_STATIC_CURSOR */
slouken@0
   107
	free(cursor);
slouken@0
   108
}
slouken@0
   109
slouken@0
   110
WMcursor *WIN_CreateWMCursor(_THIS,
slouken@0
   111
		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
slouken@0
   112
{
slouken@0
   113
#ifdef USE_STATIC_CURSOR
slouken@0
   114
	WMcursor *cursor;
slouken@0
   115
slouken@0
   116
	/* Allocate the cursor */
slouken@0
   117
	cursor = (WMcursor *)malloc(sizeof(*cursor));
slouken@0
   118
	if ( cursor ) {
slouken@0
   119
		cursor->curs = LoadCursor(NULL, IDC_ARROW);
slouken@0
   120
	}
slouken@0
   121
	return(cursor);
slouken@0
   122
#else
slouken@0
   123
	WMcursor *cursor;
slouken@0
   124
	int allowed_x;
slouken@0
   125
	int allowed_y;
slouken@0
   126
	int run, pad, i;
slouken@0
   127
	Uint8 *aptr, *xptr;
slouken@0
   128
slouken@0
   129
	/* Check to make sure the cursor size is okay */
slouken@0
   130
	allowed_x = GetSystemMetrics(SM_CXCURSOR);
slouken@0
   131
	allowed_y = GetSystemMetrics(SM_CYCURSOR);
slouken@0
   132
	if ( (w > allowed_x) || (h > allowed_y) ) {
slouken@0
   133
		SDL_SetError("Only cursors of dimension (%dx%d) are allowed",
slouken@0
   134
							allowed_x, allowed_y);
slouken@0
   135
		return(NULL);
slouken@0
   136
	}
slouken@0
   137
slouken@0
   138
	/* Allocate the cursor */
slouken@0
   139
	cursor = (WMcursor *)malloc(sizeof(*cursor));
slouken@0
   140
	if ( cursor == NULL ) {
slouken@0
   141
		SDL_SetError("Out of memory");
slouken@0
   142
		return(NULL);
slouken@0
   143
	}
slouken@0
   144
	cursor->curs = NULL;
slouken@0
   145
	cursor->ands = NULL;
slouken@0
   146
	cursor->xors = NULL;
slouken@0
   147
slouken@0
   148
	/* Pad out to the normal cursor size */
slouken@0
   149
	run = PAD_BITS(w);
slouken@0
   150
	pad = PAD_BITS(allowed_x)-run;
slouken@0
   151
	aptr = cursor->ands = (Uint8 *)malloc((run+pad)*allowed_y);
slouken@0
   152
	xptr = cursor->xors = (Uint8 *)malloc((run+pad)*allowed_y);
slouken@0
   153
	if ( (aptr == NULL) || (xptr == NULL) ) {
slouken@0
   154
		WIN_FreeWMCursor(NULL, cursor);
slouken@0
   155
		SDL_OutOfMemory();
slouken@0
   156
		return(NULL);
slouken@0
   157
	}
slouken@0
   158
	for ( i=0; i<h; ++i ) {
slouken@0
   159
		memxor(xptr, data, mask, run);
slouken@0
   160
		xptr += run;
slouken@0
   161
		data += run;
slouken@0
   162
		memnot(aptr, mask, run);
slouken@0
   163
		mask += run;
slouken@0
   164
		aptr += run;
slouken@0
   165
		memset(xptr,  0, pad);
slouken@0
   166
		xptr += pad;
slouken@0
   167
		memset(aptr, ~0, pad);
slouken@0
   168
		aptr += pad;
slouken@0
   169
	}
slouken@0
   170
	pad += run;
slouken@0
   171
	for ( ; i<allowed_y; ++i ) {
slouken@0
   172
		memset(xptr,  0, pad);
slouken@0
   173
		xptr += pad;
slouken@0
   174
		memset(aptr, ~0, pad);
slouken@0
   175
		aptr += pad;
slouken@0
   176
	}
slouken@0
   177
slouken@0
   178
	/* Create the cursor */
slouken@0
   179
	cursor->curs = CreateCursor(
slouken@0
   180
			(HINSTANCE)GetWindowLong(SDL_Window, GWL_HINSTANCE),
slouken@0
   181
					hot_x, hot_y, allowed_x, allowed_y, 
slouken@0
   182
						cursor->ands, cursor->xors);
slouken@0
   183
	if ( cursor->curs == NULL ) {
slouken@0
   184
		WIN_FreeWMCursor(NULL, cursor);
slouken@0
   185
		SDL_SetError("Windows couldn't create the requested cursor");
slouken@0
   186
		return(NULL);
slouken@0
   187
	}
slouken@0
   188
	return(cursor);
slouken@0
   189
#endif /* USE_STATIC_CURSOR */
slouken@0
   190
}
slouken@0
   191
slouken@0
   192
int WIN_ShowWMCursor(_THIS, WMcursor *cursor)
slouken@0
   193
{
slouken@0
   194
	POINT mouse_pos;
slouken@0
   195
slouken@0
   196
	/* The fullscreen cursor must be done in software with DirectInput */
slouken@13
   197
	if ( !this->screen || DDRAW_FULLSCREEN() ) {
slouken@0
   198
		return(0);
slouken@0
   199
	}
slouken@0
   200
slouken@0
   201
	/* Set the window cursor to our cursor, if applicable */
slouken@0
   202
	if ( cursor != NULL ) {
slouken@0
   203
		SDL_hcursor = cursor->curs;
slouken@0
   204
	} else {
slouken@0
   205
		SDL_hcursor = NULL;
slouken@0
   206
	}
slouken@0
   207
	GetCursorPos(&mouse_pos);
slouken@0
   208
	if ( PtInRect(&SDL_bounds, mouse_pos) ) {
slouken@0
   209
		SetCursor(SDL_hcursor);
slouken@0
   210
	}
slouken@0
   211
	return(1);
slouken@0
   212
}
slouken@0
   213
slouken@0
   214
void WIN_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
slouken@0
   215
{
slouken@13
   216
	if ( DDRAW_FULLSCREEN() ) {
slouken@0
   217
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   218
	} else if ( mouse_relative) {
slouken@0
   219
		/*	RJR: March 28, 2000
slouken@0
   220
			leave physical cursor at center of screen if
slouken@0
   221
			mouse hidden and grabbed */
slouken@0
   222
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   223
	} else {
slouken@527
   224
		POINT pt;
slouken@0
   225
		pt.x = x;
slouken@0
   226
		pt.y = y;
slouken@0
   227
		ClientToScreen(SDL_Window, &pt);
slouken@0
   228
		SetCursorPos(pt.x, pt.y);
slouken@0
   229
	}
slouken@0
   230
}
slouken@0
   231
slouken@0
   232
/* Update the current mouse state and position */
slouken@0
   233
void WIN_UpdateMouse(_THIS)
slouken@0
   234
{
slouken@0
   235
	RECT rect;
slouken@0
   236
	POINT pt;
slouken@0
   237
slouken@13
   238
	if ( ! DDRAW_FULLSCREEN() ) {
slouken@0
   239
		GetClientRect(SDL_Window, &rect);
slouken@0
   240
		GetCursorPos(&pt);
slouken@0
   241
		MapWindowPoints(NULL, SDL_Window, &pt, 1);
slouken@0
   242
		if (PtInRect(&rect, pt) && (WindowFromPoint(pt) == SDL_Window)){
slouken@0
   243
			SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@0
   244
			SDL_PrivateMouseMotion(0,0, (Sint16)pt.x, (Sint16)pt.y);
slouken@0
   245
		} else {
slouken@0
   246
			SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@0
   247
		}
slouken@0
   248
	}
slouken@0
   249
}
slouken@0
   250
slouken@0
   251
/* Check to see if we need to enter or leave mouse relative mode */
slouken@0
   252
void WIN_CheckMouseMode(_THIS)
slouken@0
   253
{
slouken@0
   254
        /* If the mouse is hidden and input is grabbed, we use relative mode */
slouken@0
   255
        if ( !(SDL_cursorstate & CURSOR_VISIBLE) &&
slouken@0
   256
             (this->input_grab != SDL_GRAB_OFF) ) {
slouken@0
   257
                mouse_relative = 1;
slouken@0
   258
        } else {
slouken@0
   259
                mouse_relative = 0;
slouken@0
   260
        }
slouken@0
   261
}