Mercurial > hg > forks > pwpunix
annotate pwplib/pwplib-unix.c @ 50:e2b6a35bcb0c
Remove usage of malloc.h; Other misc cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 24 May 2010 08:48:18 +0300 |
parents | d83239a98d1e |
children | 53676367d46f |
rev | line source |
---|---|
0 | 1 #define __PWPLIB_C |
2 | |
3 #include "config.h" | |
4 | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 | |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
8 #ifdef DRIVE_SDL |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
9 #include <SDL.h> |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
10 #endif |
0 | 11 #include <signal.h> |
12 #include <time.h> | |
13 #include <sys/time.h> | |
14 | |
15 #include "pwplib.h" | |
16 #include "tty.h" | |
17 | |
18 | |
19 /******************** random stuff *************************/ | |
20 | |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
21 static int sdl_init = 0; |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
22 |
0 | 23 extern void pwplib_dummy(); |
24 #define pwp_dummy pwplib_dummy | |
25 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
26 void pwpwrite(const char *fmt, ...) |
0 | 27 { |
19 | 28 if (!pwplib.setup[SETUP_SHUTUP]) { |
29 va_list ap; | |
30 va_start(ap, fmt); | |
31 vfprintf(stderr, fmt, ap); | |
32 va_end(ap); | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
33 |
19 | 34 usleep(1000 * pwplib.set.infodelay); |
35 } | |
0 | 36 } |
37 | |
38 /********************* timer ******************************/ | |
39 | |
40 #define TIMERHZ 72 | |
41 int pwp_unix_tod() | |
42 { | |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
43 #ifdef DRIVE_SDL |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
44 if (sdl_init) { |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
45 int quit = 0; |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
46 SDL_Event event; |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
47 if (SDL_PollEvent(&event) >= 0) { |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
48 switch (event.type) { |
19 | 49 case SDL_KEYDOWN: |
50 if (event.key.keysym.sym == SDLK_ESCAPE) | |
51 quit = 1; | |
52 break; | |
53 case SDL_QUIT: | |
54 quit = 1; | |
55 break; | |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
56 } |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
57 } |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
58 if (quit) { |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
59 SDL_Quit(); |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
60 pwplib_shutdown(); |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
61 exit(1); |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
62 } |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
63 return (SDL_GetTicks() * TIMERHZ) / 1000; |
19 | 64 } |
65 else | |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
66 #endif |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
67 { |
0 | 68 struct timeval tod; |
19 | 69 gettimeofday(&tod, NULL); |
70 return (tod.tv_sec * TIMERHZ) + (tod.tv_usec * TIMERHZ / 1000000); | |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
71 } |
0 | 72 } |
73 | |
74 /********************* destructors, signals etc **********/ | |
75 | |
76 void pwp_fatalsignal(int n) | |
77 { | |
19 | 78 pwpwrite("* pwplib shutting down\n"); |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
79 |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
80 #ifdef DRIVE_SDL |
19 | 81 if (sdl_init) { |
82 sdl_init = 0; | |
83 SDL_Quit(); | |
84 } | |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
85 #endif |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
86 |
19 | 87 pwplib_shutdown(); |
0 | 88 |
19 | 89 pwpwrite("* died to signal %d\n", n); |
90 | |
91 exit(1); | |
0 | 92 } |
93 | |
94 /******************** initialization *********************/ | |
95 | |
96 int pwplib_initcore() | |
97 { | |
19 | 98 int sdl_flags = 0; |
99 pwplib_init_common(); | |
0 | 100 |
19 | 101 /*** setup signals, etc ***/ |
0 | 102 |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
103 #ifdef DRIVE_SDL |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
104 #ifdef DRIVE_VIDEO |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
105 sdl_flags |= SDL_INIT_VIDEO; |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
106 #endif |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
107 #ifdef DRIVE_AUDIO |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
108 sdl_flags |= SDL_INIT_AUDIO; |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
109 #endif |
19 | 110 /* With SDL, we let it handle the signals */ |
111 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_EVENTTHREAD | sdl_flags) != 0) { | |
28 | 112 pwpwrite("* SDL could not be initialized (%s).\n", SDL_GetError()); |
15
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
113 sdl_init = 0; |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
114 } else |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
115 sdl_init = 1; |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
116 #endif |
fa87f8897f21
Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
117 |
19 | 118 if (!sdl_init) { |
119 signal(SIGTERM, pwp_fatalsignal); | |
120 signal(SIGINT, pwp_fatalsignal); | |
121 signal(SIGQUIT, pwp_fatalsignal); | |
122 signal(SIGKILL, pwp_fatalsignal); | |
123 signal(SIGSEGV, pwp_fatalsignal); | |
124 } | |
0 | 125 |
126 /*** video ***/ | |
127 | |
128 # ifdef DRIVE_VIDEO | |
129 | |
130 # ifdef DRIVE_SDL | |
19 | 131 if (sdl_init && pwp_SDL_init()) |
132 { | |
133 } | |
134 else | |
0 | 135 # endif |
136 # ifdef DRIVE_PVP | |
19 | 137 if (pwplib.setup[SETUP_PVP]) |
138 pvp_init(); | |
139 else | |
0 | 140 # endif |
19 | 141 if (!pwplib.setup[SETUP_NOVIDEO]) |
142 { | |
143 tty_init(); | |
144 } | |
0 | 145 # endif |
146 | |
147 /*** audio ***/ | |
148 # ifdef DRIVE_AUDIO | |
19 | 149 if (!pwplib.setup[SETUP_NOSOUND]) |
150 { | |
151 int snd = 0; | |
0 | 152 #ifdef DRIVE_SDL |
19 | 153 if (!snd) |
154 { | |
155 if (sdl_init && pwp_sdlaudio_init()) | |
156 snd++; | |
157 } | |
0 | 158 #endif |
159 | |
160 #ifdef DRIVE_OSS | |
19 | 161 if (!snd) |
162 { | |
163 if (oss_init()) | |
164 snd++; | |
165 } | |
0 | 166 #endif |
167 | |
168 #ifdef DRIVE_HPUX | |
19 | 169 if (!snd) |
170 { | |
171 if (hpuxsnd_init()) | |
172 snd++; | |
173 } | |
0 | 174 #endif |
175 | |
19 | 176 if (!snd) |
177 pwpwrite("* couldn't init sound. going silent.\n"); | |
0 | 178 |
19 | 179 } |
180 else | |
181 pwpwrite("* no sound, as requested\n"); | |
0 | 182 # endif |
183 | |
184 /*** fallbacks - remove? ***/ | |
185 | |
19 | 186 if (pwplib.dump_rast == pwplib_dummy && pwplib.dump_attr != pwplib_dummy) |
187 pwplib.dump_rast = pwplib_dump_rast_plain; | |
0 | 188 |
189 /*** timer ***/ | |
190 | |
19 | 191 if (pwplib.setup[SETUP_BPS] | pwplib.setup[SETUP_FPS]) |
192 { | |
193 pwpwrite("non-realtime output (bps=%d fps=%d)\n", pwplib.setup[SETUP_BPS], pwplib.setup[SETUP_FPS]); | |
194 pwplib.timerfunc = pwp_timer_nrt; | |
195 pwplib.timer_counter = 0; | |
196 } | |
0 | 197 else |
19 | 198 pwplib.timerfunc = pwp_unix_tod; |
0 | 199 |
200 /*** hmmm ***/ | |
201 | |
29
d83239a98d1e
Take SDL into account when halving height of rendering buffer, and remove the corresponding "hack" from sdl.c. Also misc. cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
202 if (!sdl_init && pwplib.setup[SETUP_HALVE]) |
19 | 203 pwplib.videobuf.height <<= 1; |
0 | 204 |
19 | 205 /* only allow if smaller than original? */ |
0 | 206 |
19 | 207 if (pwplib.setup[SETUP_USERHEIGHT]) |
208 pwplib.videobuf.height = pwplib.setup[SETUP_USERHEIGHT]; | |
209 if (pwplib.setup[SETUP_USERWIDTH]) | |
210 pwplib.videobuf.width = pwplib.setup[SETUP_USERWIDTH]; | |
0 | 211 |
212 /*** done ***/ | |
19 | 213 |
0 | 214 # ifdef DRIVE_VIDEO |
19 | 215 if (!pwplib.setup[SETUP_NOVIDEO]) |
216 { | |
217 pwpwrite("* pwplib now controls a %d x %d framebuffer\n", pwplib.videobuf.width, pwplib.videobuf.height); | |
218 } | |
0 | 219 # endif |
220 | |
19 | 221 return 1; |
0 | 222 } |
223 | |
224 /************************************/ | |
225 | |
19 | 226 const char *pwplogo = |
227 "\033[2J\033[H\n" | |
228 " \033[36;46mMMmmmmmmMM\033[0m\n" | |
229 " \033[36;46mMM\033[0m\n" | |
230 " \033[31;41mxxxxxxxxxxxx\033[0m\n" | |
231 " \033[31;41mxxxxxxxxxxxxxxxxxxx\033[0m\n" | |
232 " \033[31;41mxxxxxxxxx\033[0m\n" | |
233 " \033[33;43mZZZZZ\033[34;44m....\033[0m %%%%%%%%% %%% %%% %%%%%%%%%\n" | |
234 " \033[37;47m@@@@\033[34;44m......\033[0m %%%%%%%%%. %%%....... %%%. %%%%%%%%%....\n" | |
235 " \033[37;47m@@@@\033[34;44m....\033[37mo\033[34m.\033[0m .....%%% %%% %%% %%% %%% %%% %%%\n" | |
236 " \033[37;47m@@@@@@@\033[34;44m....\033[0m %%% %%% %%% %%% %%% %%% %%%\n" | |
237 " \033[37;47m@@@@@@@@@@@\033[0m %%%% %%% %%% %%% %%% %%%% %%%\n" | |
238 " \033[37;47m@@@@@@@@@@@@@\033[0m %%%% %%% %%% %%% %%% %%%% %%%\n" | |
239 " \033[37;47m@@@@@@@@@@@@@@@\033[0m %%%%%%%%% %%% %%% %%% %%%%%%%%%\n" | |
240 " \033[37;47m@@@@\033[0m %%%%%%%%% %%% %%% %%% %%%%%%%%%\n" | |
241 " \033[37;47m@@@@@@\033[0m %%% %%%%% %%% %%% %%%\n" | |
242 " \033[37;47m@@@@@@@@@@\033[0m \033[31;41mxx\033[0m %%% %%%%% %%% %%% %%%\n" | |
243 " \033[37;47m@@@@@@@@@@@@@@\033[0m %%% %%%%% %%%% %%% %%%\n" | |
244 " \033[37;47m@@@@@@@@@@@\033[0m %%% %%%%% %%%% %%% %%%\n" | |
245 " \033[37;47m@@@@@@@@@\033[0m .....%%%....... %%%%%%%%%%%%%%. %%%..........\n" | |
246 " \033[37;47m@@@@@\033[0m %%% %%%%%%%%%%%%%% %%%\n" | |
247 " \033[37;47m@@@@\033[0m\n\n"; | |
0 | 248 |
249 | |
250 void pwplib_startup() | |
251 { | |
19 | 252 if (!pwplib.setup[SETUP_SHUTUP]) |
253 { | |
254 pwpwrite("starting up.... 2sec delay for ^C"); | |
255 sleep(2); | |
256 } | |
0 | 257 |
19 | 258 /* send initstuff? */ |
0 | 259 } |
260 | |
261 void pwplib_end() | |
262 { | |
19 | 263 if (pwplib.setup[SETUP_SHUTUP]) |
264 return; | |
0 | 265 |
19 | 266 pwplib_shutdown(); |
267 sleep(2); | |
0 | 268 |
19 | 269 fputs(pwplogo, stderr); |
270 pwpwrite("shutting down multimedia subsystems\n" "operation finished - support pwp\n\n\n"); | |
0 | 271 } |
272 | |
273 /***************************************************************/ | |
274 | |
19 | 275 int pwplib_init(int argc, char **argv) |
0 | 276 { |
19 | 277 pwplib.argc = argc; |
278 pwplib.argv = argv; | |
0 | 279 |
19 | 280 pwplib_getopts(); |
0 | 281 |
19 | 282 if (pwplib.setup[SETUP_WANTHELP]) |
283 { | |
50
e2b6a35bcb0c
Remove usage of malloc.h; Other misc cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
284 pwplib_printhelp(); |
19 | 285 exit(0); |
286 } | |
0 | 287 |
19 | 288 if (!pwplib.setup[SETUP_SHUTUP]) |
289 { | |
290 fputs(pwplogo, stderr); | |
291 pwpwrite("pwplib " PWPLIB_VERSION " initializing..\n\nrun with --help for some options\n\n"); | |
292 } | |
293 | |
294 pwpwrite("* using locale '%s'\n", pwplib.set.lang); | |
0 | 295 |
19 | 296 if (!pwplib_initcore()) |
297 { | |
298 pwpwrite("initialization failed!\n"); | |
299 return 0; | |
300 } | |
0 | 301 |
19 | 302 if (pwplib.setup[SETUP_WANTHELP]) |
303 return 0; | |
0 | 304 |
19 | 305 return 1; |
0 | 306 } |