]> Dogcows Code - chaz/yoink/blob - src/moof/fastevents.c
bugfix: win32 packaging script temp directories
[chaz/yoink] / src / moof / fastevents.c
1 /*
2 NET2 is a threaded, event based, network IO library for SDL.
3 Copyright (C) 2002 Bob Pendleton
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 2.1
8 of the License, or (at your option) any later version.
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
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA
19
20 If you do not wish to comply with the terms of the LGPL please
21 contact the author as other terms are available for a fee.
22
23 Bob Pendleton
24 Bob@Pendleton.com
25 */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "SDL.h"
31 #include "SDL_thread.h"
32
33 #include "fastevents.h"
34
35 //----------------------------------------
36 //
37 // error handling code
38 //
39
40 static char* error = NULL;
41
42 static __inline__ void setError(char *err)
43 {
44 error = err;
45 }
46
47 char *FE_GetError()
48 {
49 return error;
50 }
51
52 //----------------------------------------
53 //
54 // Threads, mutexs, thread utils, and
55 // thread safe wrappers
56 //
57
58 static SDL_mutex *eventLock = NULL;
59 static SDL_cond *eventWait = NULL;
60 static SDL_TimerID eventTimer = 0;
61
62 //----------------------------------------
63 //
64 //
65 //
66
67 int FE_PushEvent(SDL_Event *ev)
68 {
69 SDL_LockMutex(eventLock);
70 while (-1 == SDL_PushEvent(ev))
71 {
72 SDL_CondWait(eventWait, eventLock);
73 }
74 SDL_UnlockMutex(eventLock);
75 SDL_CondSignal(eventWait);
76
77 return 1;
78 }
79
80 //----------------------------------------
81 //
82 //
83 //
84
85 void FE_PumpEvents()
86 {
87 SDL_LockMutex(eventLock);
88 SDL_PumpEvents();
89 SDL_UnlockMutex(eventLock);
90 }
91
92 //----------------------------------------
93 //
94 //
95 //
96
97 int FE_PollEvent(SDL_Event *event)
98 {
99 int val = 0;
100
101 SDL_LockMutex(eventLock);
102 val = SDL_PollEvent(event);
103 SDL_UnlockMutex(eventLock);
104
105 if (0 < val)
106 {
107 SDL_CondSignal(eventWait);
108 }
109
110 return val;
111 }
112
113 //----------------------------------------
114 //
115 // Replacement for SDL_WaitEvent();
116 //
117
118 int FE_WaitEvent(SDL_Event *event)
119 {
120 int val = 0;
121
122 SDL_LockMutex(eventLock);
123 while (0 >= (val = SDL_PollEvent(event)))
124 {
125 SDL_CondWait(eventWait, eventLock);
126 }
127 SDL_UnlockMutex(eventLock);
128 SDL_CondSignal(eventWait);
129
130 return val;
131 }
132
133 //----------------------------------------
134 //
135 //
136 //
137
138 static Uint32 timerCallback(Uint32 interval, void *param)
139 {
140 SDL_CondBroadcast(eventWait);
141
142 return interval;
143 }
144
145 //----------------------------------------
146 //
147 //
148 //
149
150 int FE_Init()
151 {
152 if (0 == (SDL_INIT_TIMER & SDL_WasInit(SDL_INIT_TIMER)))
153 {
154 SDL_InitSubSystem(SDL_INIT_TIMER);
155 }
156
157 eventLock = SDL_CreateMutex();
158 if (NULL == eventLock)
159 {
160 setError("FE: can't create a mutex");
161 return -1;
162 }
163
164 eventWait = SDL_CreateCond();
165 if (NULL == eventWait)
166 {
167 setError("FE: can't create a condition variable");
168 return -1;
169 }
170
171 eventTimer = SDL_AddTimer(10, timerCallback, NULL);
172 if (NULL == eventTimer)
173 {
174 setError("FE: can't add a timer");
175 return -1;
176 }
177
178 return 0;
179 }
180
181 //----------------------------------------
182 //
183 //
184 //
185
186 void FE_Quit()
187 {
188 SDL_DestroyMutex(eventLock);
189 eventLock = NULL;
190
191 SDL_DestroyCond(eventWait);
192 eventWait = NULL;
193
194 SDL_RemoveTimer(eventTimer);
195 }
This page took 0.041878 seconds and 4 git commands to generate.