Mercurial > sdl-ios-xcode
annotate src/events/SDL_mouse.c @ 1670:eef792d31de8 SDL-1.3
Work in progress. :)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 07 Jun 2006 16:10:28 +0000 |
parents | 4da1ee79c9af |
children | 89f7510fe17a |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* General mouse handling code for SDL */ | |
25 | |
26 #include "SDL_events.h" | |
27 #include "SDL_events_c.h" | |
1670 | 28 #include "SDL_mouse_c.h" |
0 | 29 |
30 | |
1670 | 31 static int SDL_num_mice; |
32 static int SDL_current_mouse; | |
33 static SDL_Mouse *SDL_mice; | |
0 | 34 |
35 | |
36 /* Public functions */ | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
37 int |
1668
4da1ee79c9af
more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents:
1662
diff
changeset
|
38 SDL_MouseInit(void) |
0 | 39 { |
1670 | 40 return (0); |
41 } | |
42 | |
43 int | |
44 SDL_AddMouse(SDL_WindowID focus, int x, int y, Uint8 buttonstate) | |
45 { | |
46 SDL_Mouse *new_mice; | |
47 int index; | |
48 SDL_Mouse *mouse; | |
49 | |
50 new_mice = | |
51 (SDL_Mouse *) SDL_realloc(SDL_mice, | |
52 (SDL_num_mice + 1) * sizeof(*new_mice)); | |
53 if (!new_mice) { | |
54 SDL_OutOfMemory(); | |
55 return -1; | |
56 } | |
0 | 57 |
1670 | 58 index = SDL_num_mice++; |
59 mouse = &SDL_mice[index]; | |
60 mouse->focus = focus; | |
61 mouse->x = x; | |
62 mouse->y = y; | |
63 mouse->xdelta = 0; | |
64 mouse->ydelta = 0; | |
65 mouse->buttonstate = buttonstate; | |
66 | |
67 return index; | |
68 } | |
69 | |
70 SDL_Mouse * | |
71 SDL_GetMouse(int index) | |
72 { | |
73 if (index < 0 || index >= SDL_num_mice) { | |
74 return NULL; | |
75 } | |
76 return &SDL_mice[index]; | |
0 | 77 } |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
78 |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
79 void |
1668
4da1ee79c9af
more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents:
1662
diff
changeset
|
80 SDL_MouseQuit(void) |
1123
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
81 { |
1670 | 82 SDL_num_mice = 0; |
83 SDL_current_mouse = 0; | |
84 | |
85 if (SDL_mice) { | |
86 SDL_free(SDL_mice); | |
87 SDL_mice = NULL; | |
88 } | |
89 } | |
90 | |
91 int | |
92 SDL_GetNumMice(void) | |
93 { | |
94 return SDL_num_mice; | |
1123
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
95 } |
0 | 96 |
1670 | 97 int |
98 SDL_SelectMouse(int index) | |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
99 { |
1670 | 100 if (index >= 0 && index < SDL_num_mice) { |
101 SDL_current_mouse = index; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
102 } |
1670 | 103 return SDL_current_mouse; |
104 } | |
105 | |
106 SDL_WindowID | |
107 SDL_GetMouseFocusWindow() | |
108 { | |
109 SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse); | |
110 | |
111 if (!mouse) { | |
112 return 0; | |
113 } | |
114 return mouse->focus; | |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
115 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
116 |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
117 Uint8 |
1668
4da1ee79c9af
more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents:
1662
diff
changeset
|
118 SDL_GetMouseState(int *x, int *y) |
0 | 119 { |
1670 | 120 SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse); |
121 | |
122 if (!mouse) { | |
123 if (x) { | |
124 *x = 0; | |
125 } | |
126 if (y) { | |
127 *y = 0; | |
128 } | |
129 return 0; | |
130 } | |
131 | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
132 if (x) { |
1670 | 133 *x = mouse->x; |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
134 } |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
135 if (y) { |
1670 | 136 *y = mouse->y; |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
137 } |
1670 | 138 return mouse->buttonstate; |
0 | 139 } |
140 | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
141 Uint8 |
1668
4da1ee79c9af
more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents:
1662
diff
changeset
|
142 SDL_GetRelativeMouseState(int *x, int *y) |
0 | 143 { |
1670 | 144 SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse); |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
145 |
1670 | 146 if (!mouse) { |
147 if (x) { | |
148 *x = 0; | |
149 } | |
150 if (y) { | |
151 *y = 0; | |
152 } | |
153 return 0; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
154 } |
0 | 155 |
1670 | 156 if (x) { |
157 *x = mouse->xdelta; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
158 } |
1670 | 159 if (y) { |
160 *y = mouse->ydelta; | |
161 } | |
162 mouse->xdelta = 0; | |
163 mouse->ydelta = 0; | |
164 return mouse->buttonstate; | |
165 } | |
0 | 166 |
1670 | 167 int |
168 SDL_SendMouseMotion(int index, SDL_WindowID windowID, int relative, int x, | |
169 int y) | |
170 { | |
171 SDL_Mouse *mouse = SDL_GetMouse(index); | |
172 int posted; | |
173 int xrel; | |
174 int yrel; | |
175 | |
176 if (!mouse) { | |
177 return 0; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
178 } |
0 | 179 |
1670 | 180 if (windowID) { |
181 mouse->focus = windowID; | |
182 } | |
0 | 183 |
1670 | 184 if (relative) { |
185 /* Push the cursor around */ | |
186 xrel = x; | |
187 yrel = y; | |
188 x = (mouse->x + xrel); | |
189 y = (mouse->y + yrel); | |
190 } else { | |
191 xrel = x - mouse->x; | |
192 yrel = y - mouse->y; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
193 } |
0 | 194 |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
195 /* Drop events that don't change state */ |
1670 | 196 if (!xrel && !yrel) { |
1283
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
197 #if 0 |
1668
4da1ee79c9af
more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents:
1662
diff
changeset
|
198 printf("Mouse event didn't change state - dropped!\n"); |
1283
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
199 #endif |
1670 | 200 return 0; |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
201 } |
1283
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
202 |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
203 /* Update internal mouse state */ |
1670 | 204 mouse->xdelta += xrel; |
205 mouse->ydelta += yrel; | |
0 | 206 |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
207 /* Post the event, if desired */ |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
208 posted = 0; |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
209 if (SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE) { |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
210 SDL_Event event; |
1670 | 211 event.motion.type = SDL_MOUSEMOTION; |
212 event.motion.which = (Uint8) index; | |
213 event.motion.state = mouse->buttonstate; | |
214 event.motion.x = mouse->x; | |
215 event.motion.y = mouse->y; | |
216 event.motion.xrel = xrel; | |
217 event.motion.yrel = yrel; | |
218 event.motion.windowID = mouse->focus; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
219 if ((SDL_EventOK == NULL) || (*SDL_EventOK) (&event)) { |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
220 posted = 1; |
1668
4da1ee79c9af
more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents:
1662
diff
changeset
|
221 SDL_PushEvent(&event); |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
222 } |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
223 } |
1670 | 224 return posted; |
0 | 225 } |
226 | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
227 int |
1670 | 228 SDL_PrivateMouseButton(int index, SDL_WindowID windowID, Uint8 state, |
229 Uint8 button) | |
0 | 230 { |
1670 | 231 SDL_Mouse *mouse = SDL_GetMouse(index); |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
232 int posted; |
1670 | 233 Uint8 type; |
0 | 234 |
1670 | 235 if (!mouse) { |
236 return 0; | |
237 } | |
0 | 238 |
1670 | 239 if (windowID) { |
240 mouse->focus = windowID; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
241 } |
0 | 242 |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
243 /* Figure out which event to perform */ |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
244 switch (state) { |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
245 case SDL_PRESSED: |
1670 | 246 if (mouse->buttonstate & SDL_BUTTON(button)) { |
247 /* Ignore this event, no state change */ | |
248 return 0; | |
249 } | |
250 type = SDL_MOUSEBUTTONDOWN; | |
251 mouse->buttonstate |= SDL_BUTTON(button); | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
252 break; |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
253 case SDL_RELEASED: |
1670 | 254 if (!(mouse->buttonstate & SDL_BUTTON(button))) { |
255 /* Ignore this event, no state change */ | |
256 return 0; | |
257 } | |
258 type = SDL_MOUSEBUTTONUP; | |
259 mouse->buttonstate &= ~SDL_BUTTON(button); | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
260 break; |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
261 default: |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
262 /* Invalid state -- bail */ |
1670 | 263 return 0; |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
264 } |
0 | 265 |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
266 /* Post the event, if desired */ |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
267 posted = 0; |
1670 | 268 if (SDL_ProcessEvents[type] == SDL_ENABLE) { |
269 SDL_Event event; | |
270 event.type = type; | |
271 event.button.which = (Uint8) index; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
272 event.button.state = state; |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
273 event.button.button = button; |
1670 | 274 event.button.x = mouse->x; |
275 event.button.y = mouse->y; | |
276 event.button.windowID = windowID; | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
277 if ((SDL_EventOK == NULL) || (*SDL_EventOK) (&event)) { |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
278 posted = 1; |
1668
4da1ee79c9af
more tweaking indent options
Sam Lantinga <slouken@libsdl.org>
parents:
1662
diff
changeset
|
279 SDL_PushEvent(&event); |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
280 } |
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
281 } |
1670 | 282 return posted; |
0 | 283 } |
284 | |
1662
782fd950bd46
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
Sam Lantinga <slouken@libsdl.org>
parents:
1525
diff
changeset
|
285 /* vi: set ts=4 sw=4 expandtab: */ |