Mercurial > sdl-ios-xcode
annotate src/video/wincommon/SDL_sysmouse.c @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | 450721ad5436 |
children | 604d73db6802 |
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:
1251
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:
1251
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:
1251
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:
1251
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:
1251
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:
1251
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:
1251
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:
13
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
23 #include "SDL_windows.h" |
0 | 24 |
25 #include "SDL_error.h" | |
26 #include "SDL_mouse.h" | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
27 #include "SDL_stdlib.h" |
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
28 #include "SDL_string.h" |
0 | 29 #include "SDL_sysmouse_c.h" |
30 #include "SDL_events_c.h" | |
31 #include "SDL_cursor_c.h" | |
32 #include "SDL_lowvideo.h" | |
33 | |
34 #ifdef _WIN32_WCE | |
35 #define USE_STATIC_CURSOR | |
36 #endif | |
37 | |
38 HCURSOR SDL_hcursor = NULL; /* Exported for SDL_eventloop.c */ | |
39 | |
40 /* The implementation dependent data for the window manager cursor */ | |
41 /* For some reason when creating a windows cursor, the ands and xors memory | |
42 is not copied, so we need to keep track of it and free it when we are done | |
43 with the cursor. If we free the memory prematurely, the app crashes. :-} | |
44 */ | |
45 struct WMcursor { | |
46 HCURSOR curs; | |
47 #ifndef USE_STATIC_CURSOR | |
48 Uint8 *ands; | |
49 Uint8 *xors; | |
50 #endif | |
51 }; | |
52 | |
53 /* Convert bits to padded bytes */ | |
54 #define PAD_BITS(bits) ((bits+7)/8) | |
55 | |
56 #ifdef CURSOR_DEBUG | |
57 static void PrintBITMAP(FILE *out, char *bits, int w, int h) | |
58 { | |
59 int i; | |
60 unsigned char ch; | |
61 | |
62 while ( h-- > 0 ) { | |
63 for ( i=0; i<w; ++i ) { | |
64 if ( (i%8) == 0 ) | |
65 ch = *bits++; | |
66 if ( ch&0x80 ) | |
67 fprintf(out, "X"); | |
68 else | |
69 fprintf(out, " "); | |
70 ch <<= 1; | |
71 } | |
72 fprintf(out, "\n"); | |
73 } | |
74 } | |
75 #endif | |
76 | |
77 #ifndef USE_STATIC_CURSOR | |
78 /* Local functions to convert the SDL cursor mask into Windows format */ | |
79 static void memnot(Uint8 *dst, Uint8 *src, int len) | |
80 { | |
81 while ( len-- > 0 ) | |
82 *dst++ = ~*src++; | |
83 } | |
84 static void memxor(Uint8 *dst, Uint8 *src1, Uint8 *src2, int len) | |
85 { | |
86 while ( len-- > 0 ) | |
87 *dst++ = (*src1++)^(*src2++); | |
88 } | |
89 #endif /* !USE_STATIC_CURSOR */ | |
90 | |
91 void WIN_FreeWMCursor(_THIS, WMcursor *cursor) | |
92 { | |
93 #ifndef USE_STATIC_CURSOR | |
506
f097dba83975
Fixed cursor resource leak in Windows (thanks Huib-Jan Imbens!)
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
94 if ( cursor->curs == GetCursor() ) |
f097dba83975
Fixed cursor resource leak in Windows (thanks Huib-Jan Imbens!)
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
95 SetCursor(NULL); |
0 | 96 if ( cursor->curs != NULL ) |
97 DestroyCursor(cursor->curs); | |
98 if ( cursor->ands != NULL ) | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
99 SDL_free(cursor->ands); |
0 | 100 if ( cursor->xors != NULL ) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
101 SDL_free(cursor->xors); |
0 | 102 #endif /* !USE_STATIC_CURSOR */ |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
103 SDL_free(cursor); |
0 | 104 } |
105 | |
106 WMcursor *WIN_CreateWMCursor(_THIS, | |
107 Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y) | |
108 { | |
109 #ifdef USE_STATIC_CURSOR | |
110 WMcursor *cursor; | |
111 | |
112 /* Allocate the cursor */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
113 cursor = (WMcursor *)SDL_malloc(sizeof(*cursor)); |
0 | 114 if ( cursor ) { |
115 cursor->curs = LoadCursor(NULL, IDC_ARROW); | |
116 } | |
117 return(cursor); | |
118 #else | |
119 WMcursor *cursor; | |
120 int allowed_x; | |
121 int allowed_y; | |
122 int run, pad, i; | |
123 Uint8 *aptr, *xptr; | |
124 | |
125 /* Check to make sure the cursor size is okay */ | |
126 allowed_x = GetSystemMetrics(SM_CXCURSOR); | |
127 allowed_y = GetSystemMetrics(SM_CYCURSOR); | |
128 if ( (w > allowed_x) || (h > allowed_y) ) { | |
129 SDL_SetError("Only cursors of dimension (%dx%d) are allowed", | |
130 allowed_x, allowed_y); | |
131 return(NULL); | |
132 } | |
133 | |
134 /* Allocate the cursor */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
135 cursor = (WMcursor *)SDL_malloc(sizeof(*cursor)); |
0 | 136 if ( cursor == NULL ) { |
137 SDL_SetError("Out of memory"); | |
138 return(NULL); | |
139 } | |
140 cursor->curs = NULL; | |
141 cursor->ands = NULL; | |
142 cursor->xors = NULL; | |
143 | |
144 /* Pad out to the normal cursor size */ | |
145 run = PAD_BITS(w); | |
146 pad = PAD_BITS(allowed_x)-run; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
147 aptr = cursor->ands = (Uint8 *)SDL_malloc((run+pad)*allowed_y); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
148 xptr = cursor->xors = (Uint8 *)SDL_malloc((run+pad)*allowed_y); |
0 | 149 if ( (aptr == NULL) || (xptr == NULL) ) { |
150 WIN_FreeWMCursor(NULL, cursor); | |
151 SDL_OutOfMemory(); | |
152 return(NULL); | |
153 } | |
154 for ( i=0; i<h; ++i ) { | |
155 memxor(xptr, data, mask, run); | |
156 xptr += run; | |
157 data += run; | |
158 memnot(aptr, mask, run); | |
159 mask += run; | |
160 aptr += run; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
161 SDL_memset(xptr, 0, pad); |
0 | 162 xptr += pad; |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
163 SDL_memset(aptr, ~0, pad); |
0 | 164 aptr += pad; |
165 } | |
166 pad += run; | |
167 for ( ; i<allowed_y; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
168 SDL_memset(xptr, 0, pad); |
0 | 169 xptr += pad; |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
170 SDL_memset(aptr, ~0, pad); |
0 | 171 aptr += pad; |
172 } | |
173 | |
174 /* Create the cursor */ | |
175 cursor->curs = CreateCursor( | |
176 (HINSTANCE)GetWindowLong(SDL_Window, GWL_HINSTANCE), | |
177 hot_x, hot_y, allowed_x, allowed_y, | |
178 cursor->ands, cursor->xors); | |
179 if ( cursor->curs == NULL ) { | |
180 WIN_FreeWMCursor(NULL, cursor); | |
181 SDL_SetError("Windows couldn't create the requested cursor"); | |
182 return(NULL); | |
183 } | |
184 return(cursor); | |
185 #endif /* USE_STATIC_CURSOR */ | |
186 } | |
187 | |
188 int WIN_ShowWMCursor(_THIS, WMcursor *cursor) | |
189 { | |
190 POINT mouse_pos; | |
191 | |
192 /* The fullscreen cursor must be done in software with DirectInput */ | |
13
e30a8ce27c22
Fixed double-mouse event bug on Windows using OpenGL
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
193 if ( !this->screen || DDRAW_FULLSCREEN() ) { |
0 | 194 return(0); |
195 } | |
196 | |
197 /* Set the window cursor to our cursor, if applicable */ | |
198 if ( cursor != NULL ) { | |
199 SDL_hcursor = cursor->curs; | |
200 } else { | |
201 SDL_hcursor = NULL; | |
202 } | |
203 GetCursorPos(&mouse_pos); | |
204 if ( PtInRect(&SDL_bounds, mouse_pos) ) { | |
205 SetCursor(SDL_hcursor); | |
206 } | |
207 return(1); | |
208 } | |
209 | |
210 void WIN_WarpWMCursor(_THIS, Uint16 x, Uint16 y) | |
211 { | |
13
e30a8ce27c22
Fixed double-mouse event bug on Windows using OpenGL
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
212 if ( DDRAW_FULLSCREEN() ) { |
0 | 213 SDL_PrivateMouseMotion(0, 0, x, y); |
214 } else if ( mouse_relative) { | |
215 /* RJR: March 28, 2000 | |
216 leave physical cursor at center of screen if | |
217 mouse hidden and grabbed */ | |
218 SDL_PrivateMouseMotion(0, 0, x, y); | |
219 } else { | |
527
5c74ac147358
Fixed mouse warp position bug with offset video modes
Sam Lantinga <slouken@libsdl.org>
parents:
506
diff
changeset
|
220 POINT pt; |
0 | 221 pt.x = x; |
222 pt.y = y; | |
223 ClientToScreen(SDL_Window, &pt); | |
224 SetCursorPos(pt.x, pt.y); | |
225 } | |
226 } | |
227 | |
228 /* Update the current mouse state and position */ | |
229 void WIN_UpdateMouse(_THIS) | |
230 { | |
231 RECT rect; | |
232 POINT pt; | |
233 | |
13
e30a8ce27c22
Fixed double-mouse event bug on Windows using OpenGL
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
234 if ( ! DDRAW_FULLSCREEN() ) { |
0 | 235 GetClientRect(SDL_Window, &rect); |
236 GetCursorPos(&pt); | |
237 MapWindowPoints(NULL, SDL_Window, &pt, 1); | |
238 if (PtInRect(&rect, pt) && (WindowFromPoint(pt) == SDL_Window)){ | |
239 SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS); | |
240 SDL_PrivateMouseMotion(0,0, (Sint16)pt.x, (Sint16)pt.y); | |
241 } else { | |
242 SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS); | |
243 } | |
244 } | |
245 } | |
246 | |
247 /* Check to see if we need to enter or leave mouse relative mode */ | |
248 void WIN_CheckMouseMode(_THIS) | |
249 { | |
1251
86d0d01290ea
Updated Windows CE/PocketPC support...adds GAPI driver, landscape mode,
Ryan C. Gordon <icculus@icculus.org>
parents:
527
diff
changeset
|
250 #ifndef _WIN32_WCE |
0 | 251 /* If the mouse is hidden and input is grabbed, we use relative mode */ |
252 if ( !(SDL_cursorstate & CURSOR_VISIBLE) && | |
253 (this->input_grab != SDL_GRAB_OFF) ) { | |
254 mouse_relative = 1; | |
255 } else { | |
256 mouse_relative = 0; | |
257 } | |
1251
86d0d01290ea
Updated Windows CE/PocketPC support...adds GAPI driver, landscape mode,
Ryan C. Gordon <icculus@icculus.org>
parents:
527
diff
changeset
|
258 #else |
86d0d01290ea
Updated Windows CE/PocketPC support...adds GAPI driver, landscape mode,
Ryan C. Gordon <icculus@icculus.org>
parents:
527
diff
changeset
|
259 mouse_relative = 0; |
86d0d01290ea
Updated Windows CE/PocketPC support...adds GAPI driver, landscape mode,
Ryan C. Gordon <icculus@icculus.org>
parents:
527
diff
changeset
|
260 #endif |
0 | 261 } |