src/events/SDL_mouse.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 20 Aug 2002 06:01:20 +0000
changeset 460 a888b3ae31ff
parent 297 f6ffac90895c
child 518 300b46757b7a
permissions -rw-r--r--
Reset mouse state when changing video modes
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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
/* General mouse handling code for SDL */
slouken@0
    29
slouken@0
    30
#include <stdio.h>
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <string.h>
slouken@0
    33
slouken@0
    34
#include "SDL_events.h"
slouken@0
    35
#include "SDL_events_c.h"
slouken@0
    36
#include "SDL_cursor_c.h"
slouken@0
    37
#include "SDL_sysvideo.h"
slouken@0
    38
slouken@0
    39
slouken@0
    40
/* These are static for our mouse handling code */
slouken@0
    41
static Sint16 SDL_MouseX = 0;
slouken@0
    42
static Sint16 SDL_MouseY = 0;
slouken@0
    43
static Sint16 SDL_DeltaX = 0;
slouken@0
    44
static Sint16 SDL_DeltaY = 0;
slouken@0
    45
static Uint8  SDL_ButtonState = 0;
slouken@0
    46
slouken@0
    47
slouken@0
    48
/* Public functions */
slouken@0
    49
int SDL_MouseInit(void)
slouken@0
    50
{
slouken@0
    51
	/* The mouse is at (0,0) */
slouken@0
    52
	SDL_MouseX = 0;
slouken@0
    53
	SDL_MouseY = 0;
slouken@0
    54
	SDL_DeltaX = 0;
slouken@0
    55
	SDL_DeltaY = 0;
slouken@0
    56
	SDL_ButtonState = 0;
slouken@0
    57
slouken@0
    58
	/* That's it! */
slouken@0
    59
	return(0);
slouken@0
    60
}
slouken@0
    61
slouken@460
    62
/* We lost the mouse, so post button up messages for all pressed buttons */
slouken@460
    63
void SDL_ResetMouse(void)
slouken@460
    64
{
slouken@460
    65
	int i;
slouken@460
    66
	for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) {
slouken@460
    67
		if ( SDL_ButtonState & SDL_BUTTON(i) ) {
slouken@460
    68
			SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
slouken@460
    69
		}
slouken@460
    70
	}
slouken@460
    71
}
slouken@460
    72
slouken@0
    73
Uint8 SDL_GetMouseState (int *x, int *y)
slouken@0
    74
{
slouken@0
    75
	if ( x )
slouken@0
    76
		*x = SDL_MouseX;
slouken@0
    77
	if ( y )
slouken@0
    78
		*y = SDL_MouseY;
slouken@0
    79
	return(SDL_ButtonState);
slouken@0
    80
}
slouken@0
    81
slouken@0
    82
Uint8 SDL_GetRelativeMouseState (int *x, int *y)
slouken@0
    83
{
slouken@0
    84
	if ( x )
slouken@0
    85
		*x = SDL_DeltaX;
slouken@0
    86
	if ( y )
slouken@0
    87
		*y = SDL_DeltaY;
slouken@0
    88
	SDL_DeltaX = 0;
slouken@0
    89
	SDL_DeltaY = 0;
slouken@0
    90
	return(SDL_ButtonState);
slouken@0
    91
}
slouken@0
    92
slouken@0
    93
static void ClipOffset(Sint16 *x, Sint16 *y)
slouken@0
    94
{
slouken@0
    95
	/* This clips absolute mouse coordinates when the apparent
slouken@0
    96
	   display surface is smaller than the real display surface.
slouken@0
    97
	 */
slouken@0
    98
	if ( SDL_VideoSurface->offset ) {
slouken@0
    99
		*y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch;
slouken@0
   100
		*x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
slouken@0
   101
				SDL_VideoSurface->format->BytesPerPixel;
slouken@0
   102
	}
slouken@0
   103
}
slouken@0
   104
slouken@0
   105
/* These are global for SDL_eventloop.c */
slouken@0
   106
int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y)
slouken@0
   107
{
slouken@0
   108
	int posted;
slouken@0
   109
	Uint16 X, Y;
slouken@0
   110
	Sint16 Xrel;
slouken@0
   111
	Sint16 Yrel;
slouken@0
   112
slouken@0
   113
	/* Don't handle mouse motion if there's no cursor surface */
slouken@0
   114
	if ( SDL_VideoSurface == NULL ) {
slouken@0
   115
		return(0);
slouken@0
   116
	}
slouken@0
   117
slouken@0
   118
	/* Default buttonstate is the current one */
slouken@0
   119
	if ( ! buttonstate ) {
slouken@0
   120
		buttonstate = SDL_ButtonState;
slouken@0
   121
	}
slouken@0
   122
slouken@0
   123
	Xrel = x;
slouken@0
   124
	Yrel = y;
slouken@0
   125
	if ( relative ) {
slouken@0
   126
		/* Push the cursor around */
slouken@0
   127
		x = (SDL_MouseX+x);
slouken@0
   128
		y = (SDL_MouseY+y);
slouken@0
   129
	} else {
slouken@0
   130
		/* Do we need to clip {x,y} ? */
slouken@0
   131
		ClipOffset(&x, &y);
slouken@0
   132
	}
slouken@0
   133
slouken@0
   134
	/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
slouken@0
   135
	if ( x < 0 )
slouken@0
   136
		X = 0;
slouken@0
   137
	else
slouken@0
   138
	if ( x >= SDL_VideoSurface->w )
slouken@0
   139
		X = SDL_VideoSurface->w-1;
slouken@0
   140
	else
slouken@0
   141
		X = (Uint16)x;
slouken@0
   142
slouken@0
   143
	if ( y < 0 )
slouken@0
   144
		Y = 0;
slouken@0
   145
	else
slouken@0
   146
	if ( y >= SDL_VideoSurface->h )
slouken@0
   147
		Y = SDL_VideoSurface->h-1;
slouken@0
   148
	else
slouken@0
   149
		Y = (Uint16)y;
slouken@0
   150
slouken@0
   151
	/* If not relative mode, generate relative motion from clamped X/Y.
slouken@0
   152
	   This prevents lots of extraneous large delta relative motion when
slouken@0
   153
	   the screen is windowed mode and the mouse is outside the window.
slouken@0
   154
	*/
slouken@0
   155
	if ( ! relative ) {
slouken@0
   156
		Xrel = X-SDL_MouseX;
slouken@0
   157
		Yrel = Y-SDL_MouseY;
slouken@0
   158
	}
slouken@0
   159
slouken@0
   160
	/* Update internal mouse state */
slouken@0
   161
	SDL_ButtonState = buttonstate;
slouken@0
   162
	SDL_MouseX = X;
slouken@0
   163
	SDL_MouseY = Y;
slouken@0
   164
	SDL_DeltaX += Xrel;
slouken@0
   165
	SDL_DeltaY += Yrel;
slouken@0
   166
	SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
slouken@0
   167
slouken@0
   168
	/* Post the event, if desired */
slouken@0
   169
	posted = 0;
slouken@0
   170
	if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) {
slouken@0
   171
		SDL_Event event;
slouken@0
   172
		memset(&event, 0, sizeof(event));
slouken@0
   173
		event.type = SDL_MOUSEMOTION;
slouken@0
   174
		event.motion.state = buttonstate;
slouken@0
   175
		event.motion.x = X;
slouken@0
   176
		event.motion.y = Y;
slouken@0
   177
		event.motion.xrel = Xrel;
slouken@0
   178
		event.motion.yrel = Yrel;
slouken@0
   179
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
   180
			posted = 1;
slouken@0
   181
			SDL_PushEvent(&event);
slouken@0
   182
		}
slouken@0
   183
	}
slouken@0
   184
	return(posted);
slouken@0
   185
}
slouken@0
   186
slouken@0
   187
int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y)
slouken@0
   188
{
slouken@0
   189
	SDL_Event event;
slouken@0
   190
	int posted;
slouken@0
   191
	int move_mouse;
slouken@0
   192
	Uint8 buttonstate;
slouken@0
   193
slouken@0
   194
	memset(&event, 0, sizeof(event));
slouken@0
   195
slouken@0
   196
	/* Check parameters */
slouken@0
   197
	if ( x || y ) {
slouken@0
   198
		ClipOffset(&x, &y);
slouken@0
   199
		move_mouse = 1;
slouken@0
   200
		/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
slouken@0
   201
		if ( x < 0 )
slouken@0
   202
			x = 0;
slouken@0
   203
		else
slouken@0
   204
		if ( x >= SDL_VideoSurface->w )
slouken@0
   205
			x = SDL_VideoSurface->w-1;
slouken@0
   206
slouken@0
   207
		if ( y < 0 )
slouken@0
   208
			y = 0;
slouken@0
   209
		else
slouken@0
   210
		if ( y >= SDL_VideoSurface->h )
slouken@0
   211
			y = SDL_VideoSurface->h-1;
slouken@0
   212
	} else {
slouken@0
   213
		move_mouse = 0;
slouken@0
   214
	}
slouken@0
   215
	if ( ! x )
slouken@0
   216
		x = SDL_MouseX;
slouken@0
   217
	if ( ! y )
slouken@0
   218
		y = SDL_MouseY;
slouken@0
   219
slouken@0
   220
	/* Figure out which event to perform */
slouken@0
   221
	buttonstate = SDL_ButtonState;
slouken@0
   222
	switch ( state ) {
slouken@0
   223
		case SDL_PRESSED:
slouken@0
   224
			event.type = SDL_MOUSEBUTTONDOWN;
slouken@0
   225
			buttonstate |= SDL_BUTTON(button);
slouken@0
   226
			break;
slouken@0
   227
		case SDL_RELEASED:
slouken@0
   228
			event.type = SDL_MOUSEBUTTONUP;
slouken@0
   229
			buttonstate &= ~SDL_BUTTON(button);
slouken@0
   230
			break;
slouken@0
   231
		default:
slouken@0
   232
			/* Invalid state -- bail */
slouken@0
   233
			return(0);
slouken@0
   234
	}
slouken@0
   235
slouken@0
   236
	/* Update internal mouse state */
slouken@0
   237
	SDL_ButtonState = buttonstate;
slouken@0
   238
	if ( move_mouse ) {
slouken@0
   239
		SDL_MouseX = x;
slouken@0
   240
		SDL_MouseY = y;
slouken@0
   241
		SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
slouken@0
   242
	}
slouken@0
   243
slouken@0
   244
	/* Post the event, if desired */
slouken@0
   245
	posted = 0;
slouken@0
   246
	if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
slouken@0
   247
		event.button.state = state;
slouken@0
   248
		event.button.button = button;
slouken@0
   249
		event.button.x = x;
slouken@0
   250
		event.button.y = y;
slouken@0
   251
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
   252
			posted = 1;
slouken@0
   253
			SDL_PushEvent(&event);
slouken@0
   254
		}
slouken@0
   255
	}
slouken@0
   256
	return(posted);
slouken@0
   257
}
slouken@0
   258