Mercurial > sdl-ios-xcode
annotate src/thread/os2/SDL_syscond.c @ 1629:ef4a796e7f24
Fixed bug #55
From Christian Walther:
When writing my patch for #12, I ended up doing all sorts of changes to the way
application/window activating/deactivating is handled in the Quartz backend,
resulting in the attached patch. It does make the code a bit cleaner IMHO, but
as it might be regarded as a case of "if it ain't broken, don't fix it" I'd
like to hear other people's opinion about it. Please shout if some change
strikes you as unnecessary or wrong, and I'll explain the reasons behind it. As
far as I tested it, it does not introduce any new bugs, but I may well have
missed some.
- The most fundamental change (that triggered most of the others) is irrelevant
for the usual single-window SDL applications, it only affects the people who
are crazy enough to display other Cocoa windows alongside the SDL window (I'm
actually doing this currently, although the additional window only displays
debugging info and won't be present in the final product): Before, some things
were done on the application becoming active, some on the window becoming key,
and some on the window becoming main. Conceptually, all these actions belong to
the window becoming key, so that's what I implemented. However, since in a
single-window application these three events always happen together, the
previous implementation "ain't broken".
- This slightly changed the meaning of the SDL_APPMOUSEFOCUS flag from
SDL_GetAppState(): Before, it meant "window is main and mouse is inside window
(or mode is fullscreen)". Now, it means "window is key and mouse is inside
window (or mode is fullscreen)". It makes more sense to me that way. (See
http://developer.apple.com/documentation/Cocoa/Conceptual/WinPanel/Concepts/ChangingMainKeyWindow.html
for a discussion of what key and main windows are.) The other two flags are
unchanged: SDL_APPACTIVE = application is not hidden and window is not
minimized, SDL_APPINPUTFOCUS = window is key (or mode is fullscreen).
- As a side effect, the reorganization fixes the following two issues (and
maybe others) (but they could also be fixed in less invasive ways):
* A regression that was introduced in revision 1.42 of SDL_QuartzVideo.m
(http://libsdl.org/cgi/cvsweb.cgi/SDL12/src/video/quartz/SDL_QuartzVideo.m.diff?r1=1.41&r2=1.42)
(from half-desirable to undesirable behavior):
Situation: While in windowed mode, hide the cursor using
SDL_ShowCursor(SDL_DISABLE), move the mouse outside of the window so that the
cursor becomes visible again, and SDL_SetVideoMode() to a fullscreen mode.
What happened before revision 1.42: The cursor is visible, but becomes
invisible as soon as the mouse is moved (half-desirable).
What happens in revision 1.42 and after (including current CVS): The cursor is
visible and stays visible (undesirable).
What happens after my patch: The cursor is invisible from the beginning
(desirable).
* When the cursor is hidden and grabbed, switch away from the application using
cmd-tab (which ungrabs and makes the cursor visible), move the cursor outside
of the SDL window, then cmd-tab back to the application. In 1.2.8 and in the
current CVS, the cursor is re-grabbed, but it stays visible (immovable in the
middle of the window). With my patch, the cursor is correctly re-grabbed and
hidden. (For some reason, it still doesn't work correctly if you switch back to
the application using the dock instead of cmd-tab. I haven't been able to
figure out why. I can step over [NSCursor hide] being called in the debugger,
but it seems to have no effect.)
- The patch includes my patch for #12 (it was easier to obtain using cvs diff
that way). If you apply both of them, you will end up with 6 duplicate lines in
SDL_QuartzEvents.m.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 13 Apr 2006 14:17:48 +0000 |
parents | d910939febfa |
children | 782fd950bd46 c121d94672cb a1b03ba2fcd0 |
rev | line source |
---|---|
1190 | 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:
1190
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
1190 | 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:
1190
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
1190 | 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:
1190
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
1190 | 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:
1190
diff
changeset
|
13 Lesser General Public License for more details. |
1190 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
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:
1190
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:
1190
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
1190 | 18 |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
22 #include "SDL_config.h" |
1190 | 23 |
24 /* An implementation of condition variables using semaphores and mutexes */ | |
25 /* | |
26 This implementation borrows heavily from the BeOS condition variable | |
27 implementation, written by Christopher Tate and Owen Smith. Thanks! | |
28 */ | |
29 | |
30 #include "SDL_thread.h" | |
31 | |
32 struct SDL_cond | |
33 { | |
34 SDL_mutex *lock; | |
35 int waiting; | |
36 int signals; | |
37 SDL_sem *wait_sem; | |
38 SDL_sem *wait_done; | |
39 }; | |
40 | |
41 /* Create a condition variable */ | |
42 DECLSPEC SDL_cond * SDLCALL SDL_CreateCond(void) | |
43 { | |
44 SDL_cond *cond; | |
45 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
46 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond)); |
1190 | 47 if ( cond ) { |
48 cond->lock = SDL_CreateMutex(); | |
49 cond->wait_sem = SDL_CreateSemaphore(0); | |
50 cond->wait_done = SDL_CreateSemaphore(0); | |
51 cond->waiting = cond->signals = 0; | |
52 if ( ! cond->lock || ! cond->wait_sem || ! cond->wait_done ) { | |
53 SDL_DestroyCond(cond); | |
54 cond = NULL; | |
55 } | |
56 } else { | |
57 SDL_OutOfMemory(); | |
58 } | |
59 return(cond); | |
60 } | |
61 | |
62 /* Destroy a condition variable */ | |
63 DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond) | |
64 { | |
65 if ( cond ) { | |
66 if ( cond->wait_sem ) { | |
67 SDL_DestroySemaphore(cond->wait_sem); | |
68 } | |
69 if ( cond->wait_done ) { | |
70 SDL_DestroySemaphore(cond->wait_done); | |
71 } | |
72 if ( cond->lock ) { | |
73 SDL_DestroyMutex(cond->lock); | |
74 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
75 SDL_free(cond); |
1190 | 76 } |
77 } | |
78 | |
79 /* Restart one of the threads that are waiting on the condition variable */ | |
80 DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond) | |
81 { | |
82 if ( ! cond ) { | |
83 SDL_SetError("Passed a NULL condition variable"); | |
84 return -1; | |
85 } | |
86 | |
87 /* If there are waiting threads not already signalled, then | |
88 signal the condition and wait for the thread to respond. | |
89 */ | |
90 SDL_LockMutex(cond->lock); | |
91 if ( cond->waiting > cond->signals ) { | |
92 ++cond->signals; | |
93 SDL_SemPost(cond->wait_sem); | |
94 SDL_UnlockMutex(cond->lock); | |
95 SDL_SemWait(cond->wait_done); | |
96 } else { | |
97 SDL_UnlockMutex(cond->lock); | |
98 } | |
99 | |
100 return 0; | |
101 } | |
102 | |
103 /* Restart all threads that are waiting on the condition variable */ | |
104 DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond *cond) | |
105 { | |
106 if ( ! cond ) { | |
107 SDL_SetError("Passed a NULL condition variable"); | |
108 return -1; | |
109 } | |
110 | |
111 /* If there are waiting threads not already signalled, then | |
112 signal the condition and wait for the thread to respond. | |
113 */ | |
114 SDL_LockMutex(cond->lock); | |
115 if ( cond->waiting > cond->signals ) { | |
116 int i, num_waiting; | |
117 | |
118 num_waiting = (cond->waiting - cond->signals); | |
119 cond->signals = cond->waiting; | |
120 for ( i=0; i<num_waiting; ++i ) { | |
121 SDL_SemPost(cond->wait_sem); | |
122 } | |
123 /* Now all released threads are blocked here, waiting for us. | |
124 Collect them all (and win fabulous prizes!) :-) | |
125 */ | |
126 SDL_UnlockMutex(cond->lock); | |
127 for ( i=0; i<num_waiting; ++i ) { | |
128 SDL_SemWait(cond->wait_done); | |
129 } | |
130 } else { | |
131 SDL_UnlockMutex(cond->lock); | |
132 } | |
133 | |
134 return 0; | |
135 } | |
136 | |
137 /* Wait on the condition variable for at most 'ms' milliseconds. | |
138 The mutex must be locked before entering this function! | |
139 The mutex is unlocked during the wait, and locked again after the wait. | |
140 | |
141 Typical use: | |
142 | |
143 Thread A: | |
144 SDL_LockMutex(lock); | |
145 while ( ! condition ) { | |
146 SDL_CondWait(cond); | |
147 } | |
148 SDL_UnlockMutex(lock); | |
149 | |
150 Thread B: | |
151 SDL_LockMutex(lock); | |
152 ... | |
153 condition = true; | |
154 ... | |
155 SDL_UnlockMutex(lock); | |
156 */ | |
157 DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms) | |
158 { | |
159 int retval; | |
160 | |
161 if ( ! cond ) { | |
162 SDL_SetError("Passed a NULL condition variable"); | |
163 return -1; | |
164 } | |
165 | |
166 /* Obtain the protection mutex, and increment the number of waiters. | |
167 This allows the signal mechanism to only perform a signal if there | |
168 are waiting threads. | |
169 */ | |
170 SDL_LockMutex(cond->lock); | |
171 ++cond->waiting; | |
172 SDL_UnlockMutex(cond->lock); | |
173 | |
174 /* Unlock the mutex, as is required by condition variable semantics */ | |
175 SDL_UnlockMutex(mutex); | |
176 | |
177 /* Wait for a signal */ | |
178 if ( ms == SDL_MUTEX_MAXWAIT ) { | |
179 retval = SDL_SemWait(cond->wait_sem); | |
180 } else { | |
181 retval = SDL_SemWaitTimeout(cond->wait_sem, ms); | |
182 } | |
183 | |
184 /* Let the signaler know we have completed the wait, otherwise | |
185 the signaler can race ahead and get the condition semaphore | |
186 if we are stopped between the mutex unlock and semaphore wait, | |
187 giving a deadlock. See the following URL for details: | |
188 http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html | |
189 */ | |
190 SDL_LockMutex(cond->lock); | |
191 if ( cond->signals > 0 ) { | |
192 /* If we timed out, we need to eat a condition signal */ | |
193 if ( retval > 0 ) { | |
194 SDL_SemWait(cond->wait_sem); | |
195 } | |
196 /* We always notify the signal thread that we are done */ | |
197 SDL_SemPost(cond->wait_done); | |
198 | |
199 /* Signal handshake complete */ | |
200 --cond->signals; | |
201 } | |
202 --cond->waiting; | |
203 SDL_UnlockMutex(cond->lock); | |
204 | |
205 /* Lock the mutex, as is required by condition variable semantics */ | |
206 SDL_LockMutex(mutex); | |
207 | |
208 return retval; | |
209 } | |
210 | |
211 /* Wait on the condition variable forever */ | |
212 DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex) | |
213 { | |
214 return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT); | |
215 } |