Mercurial > sdl-ios-xcode
comparison src/events/SDL_mouse.c @ 1662:782fd950bd46 SDL-1.3
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.
The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce
The headers are being converted to automatically generate doxygen documentation.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 28 May 2006 13:04:16 +0000 |
parents | 23a347cfbed8 |
children | 4da1ee79c9af |
comparison
equal
deleted
inserted
replaced
1661:281d3f4870e5 | 1662:782fd950bd46 |
---|---|
32 /* These are static for our mouse handling code */ | 32 /* These are static for our mouse handling code */ |
33 static Sint16 SDL_MouseX = 0; | 33 static Sint16 SDL_MouseX = 0; |
34 static Sint16 SDL_MouseY = 0; | 34 static Sint16 SDL_MouseY = 0; |
35 static Sint16 SDL_DeltaX = 0; | 35 static Sint16 SDL_DeltaX = 0; |
36 static Sint16 SDL_DeltaY = 0; | 36 static Sint16 SDL_DeltaY = 0; |
37 static Uint8 SDL_ButtonState = 0; | 37 static Uint8 SDL_ButtonState = 0; |
38 | 38 |
39 | 39 |
40 /* Public functions */ | 40 /* Public functions */ |
41 int SDL_MouseInit(void) | 41 int |
42 { | 42 SDL_MouseInit (void) |
43 /* The mouse is at (0,0) */ | 43 { |
44 SDL_MouseX = 0; | 44 /* The mouse is at (0,0) */ |
45 SDL_MouseY = 0; | 45 SDL_MouseX = 0; |
46 SDL_DeltaX = 0; | 46 SDL_MouseY = 0; |
47 SDL_DeltaY = 0; | 47 SDL_DeltaX = 0; |
48 SDL_ButtonState = 0; | 48 SDL_DeltaY = 0; |
49 | 49 SDL_ButtonState = 0; |
50 /* That's it! */ | 50 |
51 return(0); | 51 /* That's it! */ |
52 } | 52 return (0); |
53 void SDL_MouseQuit(void) | 53 } |
54 | |
55 void | |
56 SDL_MouseQuit (void) | |
54 { | 57 { |
55 } | 58 } |
56 | 59 |
57 /* We lost the mouse, so post button up messages for all pressed buttons */ | 60 /* We lost the mouse, so post button up messages for all pressed buttons */ |
58 void SDL_ResetMouse(void) | 61 void |
59 { | 62 SDL_ResetMouse (void) |
60 Uint8 i; | 63 { |
61 for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) { | 64 Uint8 i; |
62 if ( SDL_ButtonState & SDL_BUTTON(i) ) { | 65 for (i = 0; i < sizeof (SDL_ButtonState) * 8; ++i) { |
63 SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0); | 66 if (SDL_ButtonState & SDL_BUTTON (i)) { |
64 } | 67 SDL_PrivateMouseButton (SDL_RELEASED, i, 0, 0); |
65 } | 68 } |
66 } | 69 } |
67 | 70 } |
68 Uint8 SDL_GetMouseState (int *x, int *y) | 71 |
69 { | 72 Uint8 |
70 if ( x ) { | 73 SDL_GetMouseState (int *x, int *y) |
71 *x = SDL_MouseX; | 74 { |
72 } | 75 if (x) { |
73 if ( y ) { | 76 *x = SDL_MouseX; |
74 *y = SDL_MouseY; | 77 } |
75 } | 78 if (y) { |
76 return(SDL_ButtonState); | 79 *y = SDL_MouseY; |
77 } | 80 } |
78 | 81 return (SDL_ButtonState); |
79 Uint8 SDL_GetRelativeMouseState (int *x, int *y) | 82 } |
80 { | 83 |
81 if ( x ) | 84 Uint8 |
82 *x = SDL_DeltaX; | 85 SDL_GetRelativeMouseState (int *x, int *y) |
83 if ( y ) | 86 { |
84 *y = SDL_DeltaY; | 87 if (x) |
85 SDL_DeltaX = 0; | 88 *x = SDL_DeltaX; |
86 SDL_DeltaY = 0; | 89 if (y) |
87 return(SDL_ButtonState); | 90 *y = SDL_DeltaY; |
88 } | 91 SDL_DeltaX = 0; |
89 | 92 SDL_DeltaY = 0; |
90 static void ClipOffset(Sint16 *x, Sint16 *y) | 93 return (SDL_ButtonState); |
91 { | 94 } |
92 /* This clips absolute mouse coordinates when the apparent | 95 |
93 display surface is smaller than the real display surface. | 96 static void |
94 */ | 97 ClipOffset (Sint16 * x, Sint16 * y) |
95 if ( SDL_VideoSurface->offset ) { | 98 { |
96 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch; | 99 SDL_VideoDevice *_this = SDL_GetVideoDevice (); |
97 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/ | 100 |
98 SDL_VideoSurface->format->BytesPerPixel; | 101 /* This clips absolute mouse coordinates when the apparent |
99 } | 102 display surface is smaller than the real display surface. |
103 */ | |
104 if (SDL_VideoSurface->offset) { | |
105 *y -= SDL_VideoSurface->offset / SDL_VideoSurface->pitch; | |
106 *x -= (SDL_VideoSurface->offset % SDL_VideoSurface->pitch) / | |
107 SDL_VideoSurface->format->BytesPerPixel; | |
108 } | |
100 } | 109 } |
101 | 110 |
102 /* These are global for SDL_eventloop.c */ | 111 /* These are global for SDL_eventloop.c */ |
103 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y) | 112 int |
104 { | 113 SDL_PrivateMouseMotion (Uint8 buttonstate, int relative, Sint16 x, Sint16 y) |
105 int posted; | 114 { |
106 Uint16 X, Y; | 115 SDL_VideoDevice *_this = SDL_GetVideoDevice (); |
107 Sint16 Xrel; | 116 int posted; |
108 Sint16 Yrel; | 117 Uint16 X, Y; |
109 | 118 Sint16 Xrel; |
110 /* Don't handle mouse motion if there's no cursor surface */ | 119 Sint16 Yrel; |
111 if ( SDL_VideoSurface == NULL ) { | 120 |
112 return(0); | 121 /* Don't handle mouse motion if there's no cursor surface */ |
113 } | 122 if (SDL_VideoSurface == NULL) { |
114 | 123 return (0); |
115 /* Default buttonstate is the current one */ | 124 } |
116 if ( ! buttonstate ) { | 125 |
117 buttonstate = SDL_ButtonState; | 126 /* Default buttonstate is the current one */ |
118 } | 127 if (!buttonstate) { |
119 | 128 buttonstate = SDL_ButtonState; |
120 Xrel = x; | 129 } |
121 Yrel = y; | 130 |
122 if ( relative ) { | 131 Xrel = x; |
123 /* Push the cursor around */ | 132 Yrel = y; |
124 x = (SDL_MouseX+x); | 133 if (relative) { |
125 y = (SDL_MouseY+y); | 134 /* Push the cursor around */ |
126 } else { | 135 x = (SDL_MouseX + x); |
127 /* Do we need to clip {x,y} ? */ | 136 y = (SDL_MouseY + y); |
128 ClipOffset(&x, &y); | 137 } else { |
129 } | 138 /* Do we need to clip {x,y} ? */ |
130 | 139 ClipOffset (&x, &y); |
131 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | 140 } |
132 if ( x < 0 ) | 141 |
133 X = 0; | 142 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ |
134 else | 143 if (x < 0) |
135 if ( x >= SDL_VideoSurface->w ) | 144 X = 0; |
136 X = SDL_VideoSurface->w-1; | 145 else if (x >= SDL_VideoSurface->w) |
137 else | 146 X = SDL_VideoSurface->w - 1; |
138 X = (Uint16)x; | 147 else |
139 | 148 X = (Uint16) x; |
140 if ( y < 0 ) | 149 |
141 Y = 0; | 150 if (y < 0) |
142 else | 151 Y = 0; |
143 if ( y >= SDL_VideoSurface->h ) | 152 else if (y >= SDL_VideoSurface->h) |
144 Y = SDL_VideoSurface->h-1; | 153 Y = SDL_VideoSurface->h - 1; |
145 else | 154 else |
146 Y = (Uint16)y; | 155 Y = (Uint16) y; |
147 | 156 |
148 /* If not relative mode, generate relative motion from clamped X/Y. | 157 /* If not relative mode, generate relative motion from clamped X/Y. |
149 This prevents lots of extraneous large delta relative motion when | 158 This prevents lots of extraneous large delta relative motion when |
150 the screen is windowed mode and the mouse is outside the window. | 159 the screen is windowed mode and the mouse is outside the window. |
151 */ | 160 */ |
152 if ( ! relative ) { | 161 if (!relative) { |
153 Xrel = X-SDL_MouseX; | 162 Xrel = X - SDL_MouseX; |
154 Yrel = Y-SDL_MouseY; | 163 Yrel = Y - SDL_MouseY; |
155 } | 164 } |
156 | 165 |
157 /* Drop events that don't change state */ | 166 /* Drop events that don't change state */ |
158 if ( ! Xrel && ! Yrel ) { | 167 if (!Xrel && !Yrel) { |
159 #if 0 | 168 #if 0 |
160 printf("Mouse event didn't change state - dropped!\n"); | 169 printf ("Mouse event didn't change state - dropped!\n"); |
161 #endif | 170 #endif |
162 return(0); | 171 return (0); |
163 } | 172 } |
164 | 173 |
165 /* Update internal mouse state */ | 174 /* Update internal mouse state */ |
166 SDL_ButtonState = buttonstate; | 175 SDL_ButtonState = buttonstate; |
167 SDL_MouseX = X; | 176 SDL_MouseX = X; |
168 SDL_MouseY = Y; | 177 SDL_MouseY = Y; |
169 SDL_DeltaX += Xrel; | 178 SDL_DeltaX += Xrel; |
170 SDL_DeltaY += Yrel; | 179 SDL_DeltaY += Yrel; |
171 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | 180 SDL_MoveCursor (SDL_MouseX, SDL_MouseY); |
172 | 181 |
173 /* Post the event, if desired */ | 182 /* Post the event, if desired */ |
174 posted = 0; | 183 posted = 0; |
175 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) { | 184 if (SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE) { |
176 SDL_Event event; | 185 SDL_Event event; |
177 SDL_memset(&event, 0, sizeof(event)); | 186 SDL_memset (&event, 0, sizeof (event)); |
178 event.type = SDL_MOUSEMOTION; | 187 event.type = SDL_MOUSEMOTION; |
179 event.motion.state = buttonstate; | 188 event.motion.state = buttonstate; |
180 event.motion.x = X; | 189 event.motion.x = X; |
181 event.motion.y = Y; | 190 event.motion.y = Y; |
182 event.motion.xrel = Xrel; | 191 event.motion.xrel = Xrel; |
183 event.motion.yrel = Yrel; | 192 event.motion.yrel = Yrel; |
184 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | 193 if ((SDL_EventOK == NULL) || (*SDL_EventOK) (&event)) { |
185 posted = 1; | 194 posted = 1; |
186 SDL_PushEvent(&event); | 195 SDL_PushEvent (&event); |
187 } | 196 } |
188 } | 197 } |
189 return(posted); | 198 return (posted); |
190 } | 199 } |
191 | 200 |
192 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y) | 201 int |
193 { | 202 SDL_PrivateMouseButton (Uint8 state, Uint8 button, Sint16 x, Sint16 y) |
194 SDL_Event event; | 203 { |
195 int posted; | 204 SDL_VideoDevice *_this = SDL_GetVideoDevice (); |
196 int move_mouse; | 205 SDL_Event event; |
197 Uint8 buttonstate; | 206 int posted; |
198 | 207 int move_mouse; |
199 SDL_memset(&event, 0, sizeof(event)); | 208 Uint8 buttonstate; |
200 | 209 |
201 /* Check parameters */ | 210 SDL_memset (&event, 0, sizeof (event)); |
202 if ( x || y ) { | 211 |
203 ClipOffset(&x, &y); | 212 /* Check parameters */ |
204 move_mouse = 1; | 213 if (x || y) { |
205 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | 214 ClipOffset (&x, &y); |
206 if ( x < 0 ) | 215 move_mouse = 1; |
207 x = 0; | 216 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ |
208 else | 217 if (x < 0) |
209 if ( x >= SDL_VideoSurface->w ) | 218 x = 0; |
210 x = SDL_VideoSurface->w-1; | 219 else if (x >= SDL_VideoSurface->w) |
211 | 220 x = SDL_VideoSurface->w - 1; |
212 if ( y < 0 ) | 221 |
213 y = 0; | 222 if (y < 0) |
214 else | 223 y = 0; |
215 if ( y >= SDL_VideoSurface->h ) | 224 else if (y >= SDL_VideoSurface->h) |
216 y = SDL_VideoSurface->h-1; | 225 y = SDL_VideoSurface->h - 1; |
217 } else { | 226 } else { |
218 move_mouse = 0; | 227 move_mouse = 0; |
219 } | 228 } |
220 if ( ! x ) | 229 if (!x) |
221 x = SDL_MouseX; | 230 x = SDL_MouseX; |
222 if ( ! y ) | 231 if (!y) |
223 y = SDL_MouseY; | 232 y = SDL_MouseY; |
224 | 233 |
225 /* Figure out which event to perform */ | 234 /* Figure out which event to perform */ |
226 buttonstate = SDL_ButtonState; | 235 buttonstate = SDL_ButtonState; |
227 switch ( state ) { | 236 switch (state) { |
228 case SDL_PRESSED: | 237 case SDL_PRESSED: |
229 event.type = SDL_MOUSEBUTTONDOWN; | 238 event.type = SDL_MOUSEBUTTONDOWN; |
230 buttonstate |= SDL_BUTTON(button); | 239 buttonstate |= SDL_BUTTON (button); |
231 break; | 240 break; |
232 case SDL_RELEASED: | 241 case SDL_RELEASED: |
233 event.type = SDL_MOUSEBUTTONUP; | 242 event.type = SDL_MOUSEBUTTONUP; |
234 buttonstate &= ~SDL_BUTTON(button); | 243 buttonstate &= ~SDL_BUTTON (button); |
235 break; | 244 break; |
236 default: | 245 default: |
237 /* Invalid state -- bail */ | 246 /* Invalid state -- bail */ |
238 return(0); | 247 return (0); |
239 } | 248 } |
240 | 249 |
241 /* Update internal mouse state */ | 250 /* Update internal mouse state */ |
242 SDL_ButtonState = buttonstate; | 251 SDL_ButtonState = buttonstate; |
243 if ( move_mouse ) { | 252 if (move_mouse) { |
244 SDL_MouseX = x; | 253 SDL_MouseX = x; |
245 SDL_MouseY = y; | 254 SDL_MouseY = y; |
246 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | 255 SDL_MoveCursor (SDL_MouseX, SDL_MouseY); |
247 } | 256 } |
248 | 257 |
249 /* Post the event, if desired */ | 258 /* Post the event, if desired */ |
250 posted = 0; | 259 posted = 0; |
251 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { | 260 if (SDL_ProcessEvents[event.type] == SDL_ENABLE) { |
252 event.button.state = state; | 261 event.button.state = state; |
253 event.button.button = button; | 262 event.button.button = button; |
254 event.button.x = x; | 263 event.button.x = x; |
255 event.button.y = y; | 264 event.button.y = y; |
256 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | 265 if ((SDL_EventOK == NULL) || (*SDL_EventOK) (&event)) { |
257 posted = 1; | 266 posted = 1; |
258 SDL_PushEvent(&event); | 267 SDL_PushEvent (&event); |
259 } | 268 } |
260 } | 269 } |
261 return(posted); | 270 return (posted); |
262 } | 271 } |
263 | 272 |
273 /* vi: set ts=4 sw=4 expandtab: */ |