src/events/SDL_mouse.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1283 f214b6fae45a
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* General mouse handling code for SDL */
slouken@0
    24
slouken@0
    25
#include <stdio.h>
slouken@0
    26
#include <stdlib.h>
slouken@0
    27
#include <string.h>
slouken@0
    28
slouken@0
    29
#include "SDL_events.h"
slouken@0
    30
#include "SDL_events_c.h"
slouken@0
    31
#include "SDL_cursor_c.h"
slouken@0
    32
#include "SDL_sysvideo.h"
slouken@0
    33
slouken@0
    34
slouken@0
    35
/* These are static for our mouse handling code */
slouken@943
    36
static Sint16 SDL_MouseX = -1;
slouken@943
    37
static Sint16 SDL_MouseY = -1;
slouken@0
    38
static Sint16 SDL_DeltaX = 0;
slouken@0
    39
static Sint16 SDL_DeltaY = 0;
slouken@0
    40
static Uint8  SDL_ButtonState = 0;
slouken@0
    41
slouken@0
    42
slouken@0
    43
/* Public functions */
slouken@0
    44
int SDL_MouseInit(void)
slouken@0
    45
{
slouken@0
    46
	/* The mouse is at (0,0) */
slouken@943
    47
	SDL_MouseX = -1;
slouken@943
    48
	SDL_MouseY = -1;
slouken@0
    49
	SDL_DeltaX = 0;
slouken@0
    50
	SDL_DeltaY = 0;
slouken@0
    51
	SDL_ButtonState = 0;
slouken@0
    52
slouken@0
    53
	/* That's it! */
slouken@0
    54
	return(0);
slouken@0
    55
}
slouken@1123
    56
void SDL_MouseQuit(void)
slouken@1123
    57
{
slouken@1123
    58
}
slouken@0
    59
slouken@460
    60
/* We lost the mouse, so post button up messages for all pressed buttons */
slouken@460
    61
void SDL_ResetMouse(void)
slouken@460
    62
{
slouken@518
    63
	Uint8 i;
slouken@460
    64
	for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) {
slouken@460
    65
		if ( SDL_ButtonState & SDL_BUTTON(i) ) {
slouken@460
    66
			SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
slouken@460
    67
		}
slouken@460
    68
	}
slouken@460
    69
}
slouken@460
    70
slouken@0
    71
Uint8 SDL_GetMouseState (int *x, int *y)
slouken@0
    72
{
slouken@943
    73
	if ( x ) {
slouken@943
    74
		if ( SDL_MouseX < 0 ) {
slouken@943
    75
			*x = 0;
slouken@943
    76
		} else {
slouken@943
    77
			*x = SDL_MouseX;
slouken@943
    78
		}
slouken@943
    79
	}
slouken@943
    80
	if ( y ) {
slouken@943
    81
		if ( SDL_MouseY < 0 ) {
slouken@943
    82
			*y = 0;
slouken@943
    83
		} else {
slouken@943
    84
			*y = SDL_MouseY;
slouken@943
    85
		}
slouken@943
    86
	}
slouken@0
    87
	return(SDL_ButtonState);
slouken@0
    88
}
slouken@0
    89
slouken@0
    90
Uint8 SDL_GetRelativeMouseState (int *x, int *y)
slouken@0
    91
{
slouken@0
    92
	if ( x )
slouken@0
    93
		*x = SDL_DeltaX;
slouken@0
    94
	if ( y )
slouken@0
    95
		*y = SDL_DeltaY;
slouken@0
    96
	SDL_DeltaX = 0;
slouken@0
    97
	SDL_DeltaY = 0;
slouken@0
    98
	return(SDL_ButtonState);
slouken@0
    99
}
slouken@0
   100
slouken@0
   101
static void ClipOffset(Sint16 *x, Sint16 *y)
slouken@0
   102
{
slouken@0
   103
	/* This clips absolute mouse coordinates when the apparent
slouken@0
   104
	   display surface is smaller than the real display surface.
slouken@0
   105
	 */
slouken@0
   106
	if ( SDL_VideoSurface->offset ) {
slouken@0
   107
		*y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch;
slouken@0
   108
		*x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
slouken@0
   109
				SDL_VideoSurface->format->BytesPerPixel;
slouken@0
   110
	}
slouken@0
   111
}
slouken@0
   112
slouken@0
   113
/* These are global for SDL_eventloop.c */
slouken@0
   114
int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y)
slouken@0
   115
{
slouken@0
   116
	int posted;
slouken@0
   117
	Uint16 X, Y;
slouken@0
   118
	Sint16 Xrel;
slouken@0
   119
	Sint16 Yrel;
slouken@0
   120
slouken@0
   121
	/* Don't handle mouse motion if there's no cursor surface */
slouken@0
   122
	if ( SDL_VideoSurface == NULL ) {
slouken@0
   123
		return(0);
slouken@0
   124
	}
slouken@0
   125
slouken@0
   126
	/* Default buttonstate is the current one */
slouken@0
   127
	if ( ! buttonstate ) {
slouken@0
   128
		buttonstate = SDL_ButtonState;
slouken@0
   129
	}
slouken@0
   130
slouken@0
   131
	Xrel = x;
slouken@0
   132
	Yrel = y;
slouken@0
   133
	if ( relative ) {
slouken@0
   134
		/* Push the cursor around */
slouken@0
   135
		x = (SDL_MouseX+x);
slouken@0
   136
		y = (SDL_MouseY+y);
slouken@0
   137
	} else {
slouken@0
   138
		/* Do we need to clip {x,y} ? */
slouken@0
   139
		ClipOffset(&x, &y);
slouken@0
   140
	}
slouken@0
   141
slouken@0
   142
	/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
slouken@0
   143
	if ( x < 0 )
slouken@0
   144
		X = 0;
slouken@0
   145
	else
slouken@0
   146
	if ( x >= SDL_VideoSurface->w )
slouken@0
   147
		X = SDL_VideoSurface->w-1;
slouken@0
   148
	else
slouken@0
   149
		X = (Uint16)x;
slouken@0
   150
slouken@0
   151
	if ( y < 0 )
slouken@0
   152
		Y = 0;
slouken@0
   153
	else
slouken@0
   154
	if ( y >= SDL_VideoSurface->h )
slouken@0
   155
		Y = SDL_VideoSurface->h-1;
slouken@0
   156
	else
slouken@0
   157
		Y = (Uint16)y;
slouken@0
   158
slouken@0
   159
	/* If not relative mode, generate relative motion from clamped X/Y.
slouken@0
   160
	   This prevents lots of extraneous large delta relative motion when
slouken@0
   161
	   the screen is windowed mode and the mouse is outside the window.
slouken@0
   162
	*/
slouken@943
   163
	if ( ! relative && SDL_MouseX >= 0 && SDL_MouseY >= 0 ) {
slouken@0
   164
		Xrel = X-SDL_MouseX;
slouken@0
   165
		Yrel = Y-SDL_MouseY;
slouken@0
   166
	}
slouken@0
   167
slouken@1283
   168
	/* Drop events that don't change state */
slouken@1283
   169
	if ( ! Xrel && ! Yrel ) {
slouken@1283
   170
#if 0
slouken@1283
   171
printf("Mouse event didn't change state - dropped!\n");
slouken@1283
   172
#endif
slouken@1283
   173
		return(0);
slouken@1283
   174
	}
slouken@1283
   175
slouken@0
   176
	/* Update internal mouse state */
slouken@0
   177
	SDL_ButtonState = buttonstate;
slouken@0
   178
	SDL_MouseX = X;
slouken@0
   179
	SDL_MouseY = Y;
slouken@0
   180
	SDL_DeltaX += Xrel;
slouken@0
   181
	SDL_DeltaY += Yrel;
slouken@0
   182
	SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
slouken@0
   183
slouken@0
   184
	/* Post the event, if desired */
slouken@0
   185
	posted = 0;
slouken@0
   186
	if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) {
slouken@0
   187
		SDL_Event event;
slouken@0
   188
		memset(&event, 0, sizeof(event));
slouken@0
   189
		event.type = SDL_MOUSEMOTION;
slouken@0
   190
		event.motion.state = buttonstate;
slouken@0
   191
		event.motion.x = X;
slouken@0
   192
		event.motion.y = Y;
slouken@0
   193
		event.motion.xrel = Xrel;
slouken@0
   194
		event.motion.yrel = Yrel;
slouken@0
   195
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
   196
			posted = 1;
slouken@0
   197
			SDL_PushEvent(&event);
slouken@0
   198
		}
slouken@0
   199
	}
slouken@0
   200
	return(posted);
slouken@0
   201
}
slouken@0
   202
slouken@0
   203
int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y)
slouken@0
   204
{
slouken@0
   205
	SDL_Event event;
slouken@0
   206
	int posted;
slouken@0
   207
	int move_mouse;
slouken@0
   208
	Uint8 buttonstate;
slouken@0
   209
slouken@0
   210
	memset(&event, 0, sizeof(event));
slouken@0
   211
slouken@0
   212
	/* Check parameters */
slouken@0
   213
	if ( x || y ) {
slouken@0
   214
		ClipOffset(&x, &y);
slouken@0
   215
		move_mouse = 1;
slouken@0
   216
		/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
slouken@0
   217
		if ( x < 0 )
slouken@0
   218
			x = 0;
slouken@0
   219
		else
slouken@0
   220
		if ( x >= SDL_VideoSurface->w )
slouken@0
   221
			x = SDL_VideoSurface->w-1;
slouken@0
   222
slouken@0
   223
		if ( y < 0 )
slouken@0
   224
			y = 0;
slouken@0
   225
		else
slouken@0
   226
		if ( y >= SDL_VideoSurface->h )
slouken@0
   227
			y = SDL_VideoSurface->h-1;
slouken@0
   228
	} else {
slouken@0
   229
		move_mouse = 0;
slouken@0
   230
	}
slouken@0
   231
	if ( ! x )
slouken@0
   232
		x = SDL_MouseX;
slouken@0
   233
	if ( ! y )
slouken@0
   234
		y = SDL_MouseY;
slouken@0
   235
slouken@0
   236
	/* Figure out which event to perform */
slouken@0
   237
	buttonstate = SDL_ButtonState;
slouken@0
   238
	switch ( state ) {
slouken@0
   239
		case SDL_PRESSED:
slouken@0
   240
			event.type = SDL_MOUSEBUTTONDOWN;
slouken@0
   241
			buttonstate |= SDL_BUTTON(button);
slouken@0
   242
			break;
slouken@0
   243
		case SDL_RELEASED:
slouken@0
   244
			event.type = SDL_MOUSEBUTTONUP;
slouken@0
   245
			buttonstate &= ~SDL_BUTTON(button);
slouken@0
   246
			break;
slouken@0
   247
		default:
slouken@0
   248
			/* Invalid state -- bail */
slouken@0
   249
			return(0);
slouken@0
   250
	}
slouken@0
   251
slouken@0
   252
	/* Update internal mouse state */
slouken@0
   253
	SDL_ButtonState = buttonstate;
slouken@0
   254
	if ( move_mouse ) {
slouken@0
   255
		SDL_MouseX = x;
slouken@0
   256
		SDL_MouseY = y;
slouken@0
   257
		SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
slouken@0
   258
	}
slouken@0
   259
slouken@0
   260
	/* Post the event, if desired */
slouken@0
   261
	posted = 0;
slouken@0
   262
	if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
slouken@0
   263
		event.button.state = state;
slouken@0
   264
		event.button.button = button;
slouken@0
   265
		event.button.x = x;
slouken@0
   266
		event.button.y = y;
slouken@0
   267
		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
slouken@0
   268
			posted = 1;
slouken@0
   269
			SDL_PushEvent(&event);
slouken@0
   270
		}
slouken@0
   271
	}
slouken@0
   272
	return(posted);
slouken@0
   273
}
slouken@0
   274