src/video/wincommon/SDL_sysmouse.c
author Sam Lantinga
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 13 e30a8ce27c22
permissions -rw-r--r--
Initial revision
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@0
    20
    slouken@devolution.com
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@0
    98
	if ( cursor->curs != NULL )
slouken@0
    99
		DestroyCursor(cursor->curs);
slouken@0
   100
	if ( cursor->ands != NULL )
slouken@0
   101
		free(cursor->ands);
slouken@0
   102
	if ( cursor->xors != NULL )
slouken@0
   103
		free(cursor->xors);
slouken@0
   104
#endif /* !USE_STATIC_CURSOR */
slouken@0
   105
	free(cursor);
slouken@0
   106
}
slouken@0
   107
slouken@0
   108
WMcursor *WIN_CreateWMCursor(_THIS,
slouken@0
   109
		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
slouken@0
   110
{
slouken@0
   111
#ifdef USE_STATIC_CURSOR
slouken@0
   112
	WMcursor *cursor;
slouken@0
   113
slouken@0
   114
	/* Allocate the cursor */
slouken@0
   115
	cursor = (WMcursor *)malloc(sizeof(*cursor));
slouken@0
   116
	if ( cursor ) {
slouken@0
   117
		cursor->curs = LoadCursor(NULL, IDC_ARROW);
slouken@0
   118
	}
slouken@0
   119
	return(cursor);
slouken@0
   120
#else
slouken@0
   121
	WMcursor *cursor;
slouken@0
   122
	int allowed_x;
slouken@0
   123
	int allowed_y;
slouken@0
   124
	int run, pad, i;
slouken@0
   125
	Uint8 *aptr, *xptr;
slouken@0
   126
slouken@0
   127
	/* Check to make sure the cursor size is okay */
slouken@0
   128
	allowed_x = GetSystemMetrics(SM_CXCURSOR);
slouken@0
   129
	allowed_y = GetSystemMetrics(SM_CYCURSOR);
slouken@0
   130
	if ( (w > allowed_x) || (h > allowed_y) ) {
slouken@0
   131
		SDL_SetError("Only cursors of dimension (%dx%d) are allowed",
slouken@0
   132
							allowed_x, allowed_y);
slouken@0
   133
		return(NULL);
slouken@0
   134
	}
slouken@0
   135
slouken@0
   136
	/* Allocate the cursor */
slouken@0
   137
	cursor = (WMcursor *)malloc(sizeof(*cursor));
slouken@0
   138
	if ( cursor == NULL ) {
slouken@0
   139
		SDL_SetError("Out of memory");
slouken@0
   140
		return(NULL);
slouken@0
   141
	}
slouken@0
   142
	cursor->curs = NULL;
slouken@0
   143
	cursor->ands = NULL;
slouken@0
   144
	cursor->xors = NULL;
slouken@0
   145
slouken@0
   146
	/* Pad out to the normal cursor size */
slouken@0
   147
	run = PAD_BITS(w);
slouken@0
   148
	pad = PAD_BITS(allowed_x)-run;
slouken@0
   149
	aptr = cursor->ands = (Uint8 *)malloc((run+pad)*allowed_y);
slouken@0
   150
	xptr = cursor->xors = (Uint8 *)malloc((run+pad)*allowed_y);
slouken@0
   151
	if ( (aptr == NULL) || (xptr == NULL) ) {
slouken@0
   152
		WIN_FreeWMCursor(NULL, cursor);
slouken@0
   153
		SDL_OutOfMemory();
slouken@0
   154
		return(NULL);
slouken@0
   155
	}
slouken@0
   156
	for ( i=0; i<h; ++i ) {
slouken@0
   157
		memxor(xptr, data, mask, run);
slouken@0
   158
		xptr += run;
slouken@0
   159
		data += run;
slouken@0
   160
		memnot(aptr, mask, run);
slouken@0
   161
		mask += run;
slouken@0
   162
		aptr += run;
slouken@0
   163
		memset(xptr,  0, pad);
slouken@0
   164
		xptr += pad;
slouken@0
   165
		memset(aptr, ~0, pad);
slouken@0
   166
		aptr += pad;
slouken@0
   167
	}
slouken@0
   168
	pad += run;
slouken@0
   169
	for ( ; i<allowed_y; ++i ) {
slouken@0
   170
		memset(xptr,  0, pad);
slouken@0
   171
		xptr += pad;
slouken@0
   172
		memset(aptr, ~0, pad);
slouken@0
   173
		aptr += pad;
slouken@0
   174
	}
slouken@0
   175
slouken@0
   176
	/* Create the cursor */
slouken@0
   177
	cursor->curs = CreateCursor(
slouken@0
   178
			(HINSTANCE)GetWindowLong(SDL_Window, GWL_HINSTANCE),
slouken@0
   179
					hot_x, hot_y, allowed_x, allowed_y, 
slouken@0
   180
						cursor->ands, cursor->xors);
slouken@0
   181
	if ( cursor->curs == NULL ) {
slouken@0
   182
		WIN_FreeWMCursor(NULL, cursor);
slouken@0
   183
		SDL_SetError("Windows couldn't create the requested cursor");
slouken@0
   184
		return(NULL);
slouken@0
   185
	}
slouken@0
   186
	return(cursor);
slouken@0
   187
#endif /* USE_STATIC_CURSOR */
slouken@0
   188
}
slouken@0
   189
slouken@0
   190
int WIN_ShowWMCursor(_THIS, WMcursor *cursor)
slouken@0
   191
{
slouken@0
   192
	POINT mouse_pos;
slouken@0
   193
slouken@0
   194
	/* The fullscreen cursor must be done in software with DirectInput */
slouken@0
   195
	if ( !this->screen || DIRECTX_FULLSCREEN() ) {
slouken@0
   196
		return(0);
slouken@0
   197
	}
slouken@0
   198
slouken@0
   199
	/* Set the window cursor to our cursor, if applicable */
slouken@0
   200
	if ( cursor != NULL ) {
slouken@0
   201
		SDL_hcursor = cursor->curs;
slouken@0
   202
	} else {
slouken@0
   203
		SDL_hcursor = NULL;
slouken@0
   204
	}
slouken@0
   205
	GetCursorPos(&mouse_pos);
slouken@0
   206
	if ( PtInRect(&SDL_bounds, mouse_pos) ) {
slouken@0
   207
		SetCursor(SDL_hcursor);
slouken@0
   208
	}
slouken@0
   209
	return(1);
slouken@0
   210
}
slouken@0
   211
slouken@0
   212
void WIN_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
slouken@0
   213
{
slouken@0
   214
	POINT pt;
slouken@0
   215
slouken@0
   216
	if ( DIRECTX_FULLSCREEN() ) {
slouken@0
   217
		x += (this->screen->offset % this->screen->pitch) /
slouken@0
   218
		      this->screen->format->BytesPerPixel;
slouken@0
   219
		y += (this->screen->offset / this->screen->pitch);
slouken@0
   220
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   221
	} else if ( mouse_relative) {
slouken@0
   222
		/*	RJR: March 28, 2000
slouken@0
   223
			leave physical cursor at center of screen if
slouken@0
   224
			mouse hidden and grabbed */
slouken@0
   225
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   226
	} else {
slouken@0
   227
		pt.x = x;
slouken@0
   228
		pt.y = y;
slouken@0
   229
		ClientToScreen(SDL_Window, &pt);
slouken@0
   230
		SetCursorPos(pt.x, pt.y);
slouken@0
   231
	}
slouken@0
   232
}
slouken@0
   233
slouken@0
   234
/* Update the current mouse state and position */
slouken@0
   235
void WIN_UpdateMouse(_THIS)
slouken@0
   236
{
slouken@0
   237
	RECT rect;
slouken@0
   238
	POINT pt;
slouken@0
   239
slouken@0
   240
	if ( ! DIRECTX_FULLSCREEN() ) {
slouken@0
   241
		GetClientRect(SDL_Window, &rect);
slouken@0
   242
		GetCursorPos(&pt);
slouken@0
   243
		MapWindowPoints(NULL, SDL_Window, &pt, 1);
slouken@0
   244
		if (PtInRect(&rect, pt) && (WindowFromPoint(pt) == SDL_Window)){
slouken@0
   245
			SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@0
   246
			SDL_PrivateMouseMotion(0,0, (Sint16)pt.x, (Sint16)pt.y);
slouken@0
   247
		} else {
slouken@0
   248
			SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@0
   249
		}
slouken@0
   250
	}
slouken@0
   251
}
slouken@0
   252
slouken@0
   253
/* Check to see if we need to enter or leave mouse relative mode */
slouken@0
   254
void WIN_CheckMouseMode(_THIS)
slouken@0
   255
{
slouken@0
   256
        /* If the mouse is hidden and input is grabbed, we use relative mode */
slouken@0
   257
        if ( !(SDL_cursorstate & CURSOR_VISIBLE) &&
slouken@0
   258
             (this->input_grab != SDL_GRAB_OFF) ) {
slouken@0
   259
                mouse_relative = 1;
slouken@0
   260
        } else {
slouken@0
   261
                mouse_relative = 0;
slouken@0
   262
        }
slouken@0
   263
}