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