src/events/SDL_mouse.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Oct 2002 15:19:34 +0000
changeset 518 300b46757b7a
parent 460 a888b3ae31ff
child 769 b8d311d90021
permissions -rw-r--r--
Fixed compiler warning
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* General mouse handling code for SDL */
    29 
    30 #include <stdio.h>
    31 #include <stdlib.h>
    32 #include <string.h>
    33 
    34 #include "SDL_events.h"
    35 #include "SDL_events_c.h"
    36 #include "SDL_cursor_c.h"
    37 #include "SDL_sysvideo.h"
    38 
    39 
    40 /* These are static for our mouse handling code */
    41 static Sint16 SDL_MouseX = 0;
    42 static Sint16 SDL_MouseY = 0;
    43 static Sint16 SDL_DeltaX = 0;
    44 static Sint16 SDL_DeltaY = 0;
    45 static Uint8  SDL_ButtonState = 0;
    46 
    47 
    48 /* Public functions */
    49 int SDL_MouseInit(void)
    50 {
    51 	/* The mouse is at (0,0) */
    52 	SDL_MouseX = 0;
    53 	SDL_MouseY = 0;
    54 	SDL_DeltaX = 0;
    55 	SDL_DeltaY = 0;
    56 	SDL_ButtonState = 0;
    57 
    58 	/* That's it! */
    59 	return(0);
    60 }
    61 
    62 /* We lost the mouse, so post button up messages for all pressed buttons */
    63 void SDL_ResetMouse(void)
    64 {
    65 	Uint8 i;
    66 	for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) {
    67 		if ( SDL_ButtonState & SDL_BUTTON(i) ) {
    68 			SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
    69 		}
    70 	}
    71 }
    72 
    73 Uint8 SDL_GetMouseState (int *x, int *y)
    74 {
    75 	if ( x )
    76 		*x = SDL_MouseX;
    77 	if ( y )
    78 		*y = SDL_MouseY;
    79 	return(SDL_ButtonState);
    80 }
    81 
    82 Uint8 SDL_GetRelativeMouseState (int *x, int *y)
    83 {
    84 	if ( x )
    85 		*x = SDL_DeltaX;
    86 	if ( y )
    87 		*y = SDL_DeltaY;
    88 	SDL_DeltaX = 0;
    89 	SDL_DeltaY = 0;
    90 	return(SDL_ButtonState);
    91 }
    92 
    93 static void ClipOffset(Sint16 *x, Sint16 *y)
    94 {
    95 	/* This clips absolute mouse coordinates when the apparent
    96 	   display surface is smaller than the real display surface.
    97 	 */
    98 	if ( SDL_VideoSurface->offset ) {
    99 		*y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch;
   100 		*x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
   101 				SDL_VideoSurface->format->BytesPerPixel;
   102 	}
   103 }
   104 
   105 /* These are global for SDL_eventloop.c */
   106 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y)
   107 {
   108 	int posted;
   109 	Uint16 X, Y;
   110 	Sint16 Xrel;
   111 	Sint16 Yrel;
   112 
   113 	/* Don't handle mouse motion if there's no cursor surface */
   114 	if ( SDL_VideoSurface == NULL ) {
   115 		return(0);
   116 	}
   117 
   118 	/* Default buttonstate is the current one */
   119 	if ( ! buttonstate ) {
   120 		buttonstate = SDL_ButtonState;
   121 	}
   122 
   123 	Xrel = x;
   124 	Yrel = y;
   125 	if ( relative ) {
   126 		/* Push the cursor around */
   127 		x = (SDL_MouseX+x);
   128 		y = (SDL_MouseY+y);
   129 	} else {
   130 		/* Do we need to clip {x,y} ? */
   131 		ClipOffset(&x, &y);
   132 	}
   133 
   134 	/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
   135 	if ( x < 0 )
   136 		X = 0;
   137 	else
   138 	if ( x >= SDL_VideoSurface->w )
   139 		X = SDL_VideoSurface->w-1;
   140 	else
   141 		X = (Uint16)x;
   142 
   143 	if ( y < 0 )
   144 		Y = 0;
   145 	else
   146 	if ( y >= SDL_VideoSurface->h )
   147 		Y = SDL_VideoSurface->h-1;
   148 	else
   149 		Y = (Uint16)y;
   150 
   151 	/* If not relative mode, generate relative motion from clamped X/Y.
   152 	   This prevents lots of extraneous large delta relative motion when
   153 	   the screen is windowed mode and the mouse is outside the window.
   154 	*/
   155 	if ( ! relative ) {
   156 		Xrel = X-SDL_MouseX;
   157 		Yrel = Y-SDL_MouseY;
   158 	}
   159 
   160 	/* Update internal mouse state */
   161 	SDL_ButtonState = buttonstate;
   162 	SDL_MouseX = X;
   163 	SDL_MouseY = Y;
   164 	SDL_DeltaX += Xrel;
   165 	SDL_DeltaY += Yrel;
   166 	SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
   167 
   168 	/* Post the event, if desired */
   169 	posted = 0;
   170 	if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) {
   171 		SDL_Event event;
   172 		memset(&event, 0, sizeof(event));
   173 		event.type = SDL_MOUSEMOTION;
   174 		event.motion.state = buttonstate;
   175 		event.motion.x = X;
   176 		event.motion.y = Y;
   177 		event.motion.xrel = Xrel;
   178 		event.motion.yrel = Yrel;
   179 		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   180 			posted = 1;
   181 			SDL_PushEvent(&event);
   182 		}
   183 	}
   184 	return(posted);
   185 }
   186 
   187 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y)
   188 {
   189 	SDL_Event event;
   190 	int posted;
   191 	int move_mouse;
   192 	Uint8 buttonstate;
   193 
   194 	memset(&event, 0, sizeof(event));
   195 
   196 	/* Check parameters */
   197 	if ( x || y ) {
   198 		ClipOffset(&x, &y);
   199 		move_mouse = 1;
   200 		/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
   201 		if ( x < 0 )
   202 			x = 0;
   203 		else
   204 		if ( x >= SDL_VideoSurface->w )
   205 			x = SDL_VideoSurface->w-1;
   206 
   207 		if ( y < 0 )
   208 			y = 0;
   209 		else
   210 		if ( y >= SDL_VideoSurface->h )
   211 			y = SDL_VideoSurface->h-1;
   212 	} else {
   213 		move_mouse = 0;
   214 	}
   215 	if ( ! x )
   216 		x = SDL_MouseX;
   217 	if ( ! y )
   218 		y = SDL_MouseY;
   219 
   220 	/* Figure out which event to perform */
   221 	buttonstate = SDL_ButtonState;
   222 	switch ( state ) {
   223 		case SDL_PRESSED:
   224 			event.type = SDL_MOUSEBUTTONDOWN;
   225 			buttonstate |= SDL_BUTTON(button);
   226 			break;
   227 		case SDL_RELEASED:
   228 			event.type = SDL_MOUSEBUTTONUP;
   229 			buttonstate &= ~SDL_BUTTON(button);
   230 			break;
   231 		default:
   232 			/* Invalid state -- bail */
   233 			return(0);
   234 	}
   235 
   236 	/* Update internal mouse state */
   237 	SDL_ButtonState = buttonstate;
   238 	if ( move_mouse ) {
   239 		SDL_MouseX = x;
   240 		SDL_MouseY = y;
   241 		SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
   242 	}
   243 
   244 	/* Post the event, if desired */
   245 	posted = 0;
   246 	if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
   247 		event.button.state = state;
   248 		event.button.button = button;
   249 		event.button.x = x;
   250 		event.button.y = y;
   251 		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   252 			posted = 1;
   253 			SDL_PushEvent(&event);
   254 		}
   255 	}
   256 	return(posted);
   257 }
   258