Mercurial > audlegacy
annotate Plugins/Input/sid/xmms-sid.c @ 574:80a301d11c77 trunk
[svn] Add -I../.. here to satisfy OSS header dependency (O_o)
author | nenolod |
---|---|
date | Thu, 02 Feb 2006 15:42:11 -0800 |
parents | 8faef4a1461f |
children | f7e6ffc5ccde |
rev | line source |
---|---|
269 | 1 /* |
2 XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS) | |
3 | |
4 Main source file | |
5 | |
6 Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org> | |
7 (C) Copyright 1999-2005 Tecnic Software productions (TNSP) | |
8 | |
9 This program is free software; you can redistribute it and/or modify | |
10 it under the terms of the GNU General Public License as published by | |
11 the Free Software Foundation; either version 2 of the License, or | |
12 (at your option) any later version. | |
13 | |
14 This program is distributed in the hope that it will be useful, | |
15 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 GNU General Public License for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
20 along with this program; if not, write to the Free Software | |
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
23 |
269 | 24 #include "xmms-sid.h" |
25 #include "xs_support.h" | |
26 | |
27 #ifdef HAVE_STDLIB_H | |
28 #include <stdlib.h> | |
29 #endif | |
30 | |
31 #include <stdarg.h> | |
32 | |
33 #include <audacious/plugin.h> | |
352 | 34 #include <audacious/output.h> |
269 | 35 #include <libaudacious/util.h> |
36 | |
37 #include <gdk/gdkkeysyms.h> | |
38 #include <gtk/gtk.h> | |
39 | |
40 #include "xs_config.h" | |
41 #include "xs_length.h" | |
42 #include "xs_stil.h" | |
43 #include "xs_filter.h" | |
44 #include "xs_fileinfo.h" | |
45 #include "xs_interface.h" | |
46 #include "xs_glade.h" | |
47 | |
48 /* | |
49 * Include player engines | |
50 */ | |
51 #ifdef HAVE_SIDPLAY1 | |
52 #include "xs_sidplay1.h" | |
53 #endif | |
54 #ifdef HAVE_SIDPLAY2 | |
55 #include "xs_sidplay2.h" | |
56 #endif | |
57 | |
58 | |
59 /* | |
60 * List of players and links to their functions | |
61 */ | |
62 t_xs_player xs_playerlist[] = { | |
63 #ifdef HAVE_SIDPLAY1 | |
64 {XS_ENG_SIDPLAY1, | |
65 xs_sidplay1_isourfile, | |
66 xs_sidplay1_init, xs_sidplay1_close, | |
67 xs_sidplay1_initsong, xs_sidplay1_fillbuffer, | |
68 xs_sidplay1_loadsid, xs_sidplay1_deletesid, | |
69 xs_sidplay1_getsidinfo | |
70 }, | |
71 #endif | |
72 #ifdef HAVE_SIDPLAY2 | |
73 {XS_ENG_SIDPLAY2, | |
74 xs_sidplay2_isourfile, | |
75 xs_sidplay2_init, xs_sidplay2_close, | |
76 xs_sidplay2_initsong, xs_sidplay2_fillbuffer, | |
77 xs_sidplay2_loadsid, xs_sidplay2_deletesid, | |
78 xs_sidplay2_getsidinfo | |
79 }, | |
80 #endif | |
81 }; | |
82 | |
83 const gint xs_nplayerlist = (sizeof(xs_playerlist) / sizeof(t_xs_player)); | |
84 | |
85 | |
86 /* | |
87 * Global variables | |
88 */ | |
89 t_xs_status xs_status; | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
90 extern GStaticMutex xs_status_mutex; |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
91 extern GStaticMutex xs_cfg_mutex; |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
92 GStaticMutex xs_subctrl_mutex = G_STATIC_MUTEX_INIT; |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
93 |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
94 static GThread *xs_decode_thread; |
269 | 95 |
96 static GtkWidget *xs_subctrl = NULL; | |
97 static GtkObject *xs_subctrl_adj = NULL; | |
98 | |
99 void xs_subctrl_close(void); | |
100 void xs_subctrl_update(void); | |
101 | |
102 | |
103 /* | |
104 * Error messages | |
105 */ | |
106 void XSERR(const char *fmt, ...) | |
107 { | |
108 va_list ap; | |
109 fprintf(stderr, "XMMS-SID: "); | |
110 va_start(ap, fmt); | |
111 vfprintf(stderr, fmt, ap); | |
112 va_end(ap); | |
113 } | |
114 | |
115 #ifndef DEBUG_NP | |
116 void XSDEBUG(const char *fmt, ...) | |
117 { | |
118 #ifdef DEBUG | |
119 va_list ap; | |
120 fprintf(stderr, "XSDEBUG: "); | |
121 va_start(ap, fmt); | |
122 vfprintf(stderr, fmt, ap); | |
123 va_end(ap); | |
124 #endif | |
125 } | |
126 #endif | |
127 | |
128 /* | |
129 * Initialization functions | |
130 */ | |
131 void xs_reinit(void) | |
132 { | |
133 gint iPlayer; | |
134 gboolean isInitialized; | |
135 | |
136 /* Stop playing, if we are */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
137 g_static_mutex_lock(&xs_status_mutex); |
269 | 138 if (xs_status.isPlaying) { |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
139 g_static_mutex_unlock(&xs_status_mutex); |
269 | 140 xs_stop(); |
141 } else { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
142 g_static_mutex_unlock(&xs_status_mutex); |
269 | 143 } |
144 | |
145 /* Initialize status and sanitize configuration */ | |
146 xs_memset(&xs_status, 0, sizeof(xs_status)); | |
147 | |
148 if (xs_cfg.audioFrequency < 8000) | |
149 xs_cfg.audioFrequency = 8000; | |
150 | |
151 if (xs_cfg.oversampleFactor < XS_MIN_OVERSAMPLE) | |
152 xs_cfg.oversampleFactor = XS_MIN_OVERSAMPLE; | |
153 else if (xs_cfg.oversampleFactor > XS_MAX_OVERSAMPLE) | |
154 xs_cfg.oversampleFactor = XS_MAX_OVERSAMPLE; | |
155 | |
156 if (xs_cfg.audioChannels != XS_CHN_MONO) | |
157 xs_cfg.oversampleEnable = FALSE; | |
158 | |
159 xs_status.audioFrequency = xs_cfg.audioFrequency; | |
160 xs_status.audioBitsPerSample = xs_cfg.audioBitsPerSample; | |
161 xs_status.audioChannels = xs_cfg.audioChannels; | |
162 xs_status.audioFormat = -1; | |
163 xs_status.oversampleEnable = xs_cfg.oversampleEnable; | |
164 xs_status.oversampleFactor = xs_cfg.oversampleFactor; | |
165 | |
166 /* Try to initialize emulator engine */ | |
167 XSDEBUG("initializing emulator engine #%i...\n", xs_cfg.playerEngine); | |
168 | |
169 iPlayer = 0; | |
170 isInitialized = FALSE; | |
171 while ((iPlayer < xs_nplayerlist) && !isInitialized) { | |
172 if (xs_playerlist[iPlayer].plrIdent == xs_cfg.playerEngine) { | |
173 if (xs_playerlist[iPlayer].plrInit(&xs_status)) { | |
174 isInitialized = TRUE; | |
175 xs_status.sidPlayer = (t_xs_player *) & xs_playerlist[iPlayer]; | |
176 } | |
177 } | |
178 iPlayer++; | |
179 } | |
180 | |
181 XSDEBUG("init#1: %s, %i\n", (isInitialized) ? "OK" : "FAILED", iPlayer); | |
182 | |
183 iPlayer = 0; | |
184 while ((iPlayer < xs_nplayerlist) && !isInitialized) { | |
185 if (xs_playerlist[iPlayer].plrInit(&xs_status)) { | |
186 isInitialized = TRUE; | |
187 xs_status.sidPlayer = (t_xs_player *) & xs_playerlist[iPlayer]; | |
188 xs_cfg.playerEngine = xs_playerlist[iPlayer].plrIdent; | |
189 } else | |
190 iPlayer++; | |
191 } | |
192 | |
193 XSDEBUG("init#2: %s, %i\n", (isInitialized) ? "OK" : "FAILED", iPlayer); | |
194 | |
195 /* Get settings back, in case the chosen emulator backend changed them */ | |
196 xs_cfg.audioFrequency = xs_status.audioFrequency; | |
197 xs_cfg.audioBitsPerSample = xs_status.audioBitsPerSample; | |
198 xs_cfg.audioChannels = xs_status.audioChannels; | |
199 xs_cfg.oversampleEnable = xs_status.oversampleEnable; | |
200 | |
201 /* Initialize song-length database */ | |
202 xs_songlen_close(); | |
203 if (xs_cfg.songlenDBEnable && (xs_songlen_init() != 0)) { | |
204 XSERR("Error initializing song-length database!\n"); | |
205 } | |
206 | |
207 /* Initialize STIL database */ | |
208 xs_stil_close(); | |
209 if (xs_cfg.stilDBEnable && (xs_stil_init() != 0)) { | |
210 XSERR("Error initializing STIL database!\n"); | |
211 } | |
212 } | |
213 | |
214 | |
215 /* | |
216 * Initialize XMMS-SID | |
217 */ | |
218 void xs_init(void) | |
219 { | |
220 XSDEBUG("xs_init()\n"); | |
221 | |
222 /* Initialize and get configuration */ | |
223 xs_memset(&xs_cfg, 0, sizeof(xs_cfg)); | |
224 xs_init_configuration(); | |
225 xs_read_configuration(); | |
226 | |
227 /* Initialize subsystems */ | |
228 xs_reinit(); | |
229 | |
230 XSDEBUG("OK\n"); | |
231 } | |
232 | |
233 | |
234 /* | |
235 * Shut down XMMS-SID | |
236 */ | |
237 void xs_close(void) | |
238 { | |
239 XSDEBUG("xs_close(): shutting down...\n"); | |
240 | |
241 /* Stop playing, free structures */ | |
242 xs_stop(); | |
243 | |
244 xs_tuneinfo_free(xs_status.tuneInfo); | |
245 xs_status.tuneInfo = NULL; | |
246 xs_status.sidPlayer->plrDeleteSID(&xs_status); | |
247 xs_status.sidPlayer->plrClose(&xs_status); | |
248 | |
249 xs_songlen_close(); | |
250 xs_stil_close(); | |
251 | |
252 XSDEBUG("shutdown finished.\n"); | |
253 } | |
254 | |
255 | |
256 /* | |
257 * Check whether the given file is handled by this plugin | |
258 */ | |
259 gint xs_is_our_file(gchar * pcFilename) | |
260 { | |
261 assert(xs_status.sidPlayer); | |
262 | |
263 /* Check the filename */ | |
264 if (pcFilename == NULL) | |
265 return FALSE; | |
266 | |
370 | 267 if (xs_status.sidPlayer->plrIsOurFile(pcFilename)) |
269 | 268 return TRUE; |
269 | |
270 return FALSE; | |
271 } | |
272 | |
273 | |
274 /* | |
275 * Main playing thread loop | |
276 */ | |
277 void *xs_playthread(void *argPointer) | |
278 { | |
279 t_xs_status myStatus; | |
280 t_xs_tuneinfo *myTune; | |
281 gboolean audioOpen = FALSE, doPlay = FALSE, isFound = FALSE; | |
282 gboolean playedTune[XS_STIL_MAXENTRY + 1]; | |
283 gint audioGot, songLength, i; | |
284 gchar *audioBuffer = NULL, *oversampleBuffer = NULL; | |
285 | |
286 (void) argPointer; | |
287 | |
288 /* Initialize */ | |
289 XSDEBUG("entering player thread\n"); | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
290 g_static_mutex_lock(&xs_status_mutex); |
269 | 291 memcpy(&myStatus, &xs_status, sizeof(t_xs_status)); |
292 myTune = xs_status.tuneInfo; | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
293 g_static_mutex_unlock(&xs_status_mutex); |
269 | 294 |
295 xs_memset(&playedTune, 0, sizeof(playedTune)); | |
296 | |
297 /* Allocate audio buffer */ | |
298 audioBuffer = (gchar *) g_malloc(XS_AUDIOBUF_SIZE); | |
299 if (audioBuffer == NULL) { | |
300 XSERR("Couldn't allocate memory for audio data buffer!\n"); | |
301 goto xs_err_exit; | |
302 } | |
303 | |
304 if (myStatus.oversampleEnable) { | |
305 oversampleBuffer = (gchar *) g_malloc(XS_AUDIOBUF_SIZE * myStatus.oversampleFactor); | |
306 if (oversampleBuffer == NULL) { | |
307 XSERR("Couldn't allocate memory for audio oversampling buffer!\n"); | |
308 goto xs_err_exit; | |
309 } | |
310 } | |
311 | |
312 /* | |
313 * Main player loop: while not stopped, loop here - play subtunes | |
314 */ | |
315 audioOpen = FALSE; | |
316 doPlay = TRUE; | |
317 while (xs_status.isPlaying && doPlay) { | |
318 /* Automatic sub-tune change logic */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
319 g_static_mutex_lock(&xs_cfg_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
320 g_static_mutex_lock(&xs_status_mutex); |
269 | 321 assert(xs_status.currSong >= 1); |
322 assert(xs_status.currSong <= XS_STIL_MAXENTRY); | |
323 myStatus.isPlaying = TRUE; | |
324 | |
325 if (xs_cfg.subAutoEnable && (myStatus.currSong == xs_status.currSong)) { | |
326 /* Check if currently selected sub-tune has been played already */ | |
327 if (playedTune[myStatus.currSong]) { | |
328 /* Find a tune that has not been played */ | |
329 XSDEBUG("tune #%i already played, finding next match ...\n", myStatus.currSong); | |
330 isFound = FALSE; | |
331 i = 0; | |
332 while (!isFound && (++i <= myTune->nsubTunes)) { | |
333 if (xs_cfg.subAutoMinOnly) { | |
334 /* A tune with minimum length must be found */ | |
335 if (!playedTune[i] | |
336 && myTune->subTunes[i].tuneLength >= xs_cfg.subAutoMinTime) | |
337 isFound = TRUE; | |
338 } else { | |
339 /* Any unplayed tune is okay */ | |
340 if (!playedTune[i]) | |
341 isFound = TRUE; | |
342 } | |
343 } | |
344 | |
345 if (isFound) { | |
346 /* Set the new sub-tune */ | |
347 XSDEBUG("found #%i\n", i); | |
348 xs_status.currSong = i; | |
349 } else | |
350 /* This is the end */ | |
351 doPlay = FALSE; | |
352 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
353 g_static_mutex_unlock(&xs_status_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
354 g_static_mutex_unlock(&xs_cfg_mutex); |
269 | 355 continue; /* This is ugly, but ... */ |
356 } | |
357 } | |
358 | |
359 /* Tell that we are initializing, update sub-tune controls */ | |
360 myStatus.currSong = xs_status.currSong; | |
361 playedTune[myStatus.currSong] = TRUE; | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
362 g_static_mutex_unlock(&xs_status_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
363 g_static_mutex_unlock(&xs_cfg_mutex); |
269 | 364 |
365 XSDEBUG("subtune #%i selected, initializing...\n", myStatus.currSong); | |
366 | |
367 GDK_THREADS_ENTER(); | |
368 xs_subctrl_update(); | |
369 GDK_THREADS_LEAVE(); | |
370 | |
371 /* Check minimum playtime */ | |
372 songLength = myTune->subTunes[myStatus.currSong - 1].tuneLength; | |
373 if (xs_cfg.playMinTimeEnable && (songLength >= 0)) { | |
374 if (songLength < xs_cfg.playMinTime) | |
375 songLength = xs_cfg.playMinTime; | |
376 } | |
377 | |
378 /* Initialize song */ | |
379 if (!myStatus.sidPlayer->plrInitSong(&myStatus)) { | |
380 XSERR("Couldn't initialize SID-tune '%s' (sub-tune #%i)!\n", | |
381 myTune->sidFilename, myStatus.currSong); | |
382 goto xs_err_exit; | |
383 } | |
384 | |
385 | |
386 /* Open the audio output */ | |
387 if (!xs_plugin_ip.output-> | |
388 open_audio(myStatus.audioFormat, myStatus.audioFrequency, myStatus.audioChannels)) { | |
389 XSERR("Couldn't open XMMS audio output (fmt=%x, freq=%i, nchan=%i)!\n", myStatus.audioFormat, | |
390 myStatus.audioFrequency, myStatus.audioChannels); | |
391 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
392 g_static_mutex_lock(&xs_status_mutex); |
269 | 393 xs_status.isError = TRUE; |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
394 g_static_mutex_unlock(&xs_status_mutex); |
269 | 395 goto xs_err_exit; |
396 } | |
397 | |
398 audioOpen = TRUE; | |
399 | |
400 /* Set song information for current subtune */ | |
401 xs_plugin_ip.set_info(myTune->subTunes[myStatus.currSong - 1].tuneTitle, | |
402 (songLength > 0) ? (songLength * 1000) : -1, | |
403 (myTune->subTunes[myStatus.currSong - 1].tuneSpeed > | |
404 0) ? (myTune->subTunes[myStatus.currSong - 1].tuneSpeed * 1000) : -1, | |
405 myStatus.audioFrequency, myStatus.audioChannels); | |
406 | |
407 | |
408 XSDEBUG("playing\n"); | |
409 | |
410 /* | |
411 * Play the subtune | |
412 */ | |
413 while (xs_status.isPlaying && myStatus.isPlaying && (xs_status.currSong == myStatus.currSong)) { | |
414 /* Render audio data */ | |
415 if (myStatus.oversampleEnable) { | |
416 /* Perform oversampled rendering */ | |
417 audioGot = myStatus.sidPlayer->plrFillBuffer(&myStatus, | |
418 oversampleBuffer, | |
419 (XS_AUDIOBUF_SIZE * | |
420 myStatus.oversampleFactor)); | |
421 | |
422 audioGot /= myStatus.oversampleFactor; | |
423 | |
424 /* Execute rate-conversion with filtering */ | |
425 if (xs_filter_rateconv(audioBuffer, oversampleBuffer, | |
426 myStatus.audioFormat, myStatus.oversampleFactor, audioGot) < 0) { | |
427 XSERR("Oversampling rate-conversion pass failed.\n"); | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
428 g_static_mutex_lock(&xs_status_mutex); |
269 | 429 xs_status.isError = TRUE; |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
430 g_static_mutex_unlock(&xs_status_mutex); |
269 | 431 goto xs_err_exit; |
432 } | |
433 } else | |
434 audioGot = myStatus.sidPlayer->plrFillBuffer(&myStatus, audioBuffer, XS_AUDIOBUF_SIZE); | |
435 | |
436 /* I <3 visualice/haujobb */ | |
350 | 437 produce_audio(xs_plugin_ip.output->written_time(), |
438 myStatus.audioFormat, myStatus.audioChannels, audioGot, audioBuffer, NULL); | |
269 | 439 |
440 /* Wait a little */ | |
441 while (xs_status.isPlaying && | |
442 (xs_status.currSong == myStatus.currSong) && | |
443 (xs_plugin_ip.output->buffer_free() < audioGot)) | |
444 xmms_usleep(500); | |
445 | |
446 /* Check if we have played enough */ | |
447 if (xs_cfg.playMaxTimeEnable) { | |
448 if (xs_cfg.playMaxTimeUnknown) { | |
449 if ((songLength < 0) && | |
450 (xs_plugin_ip.output->output_time() >= (xs_cfg.playMaxTime * 1000))) | |
451 myStatus.isPlaying = FALSE; | |
452 } else { | |
453 if (xs_plugin_ip.output->output_time() >= (xs_cfg.playMaxTime * 1000)) | |
454 myStatus.isPlaying = FALSE; | |
455 } | |
456 } | |
457 | |
458 if (songLength >= 0) { | |
459 if (xs_plugin_ip.output->output_time() >= (songLength * 1000)) | |
460 myStatus.isPlaying = FALSE; | |
461 } | |
462 } | |
463 | |
464 XSDEBUG("subtune ended/stopped\n"); | |
465 | |
466 /* Close audio output plugin */ | |
467 if (audioOpen) { | |
468 XSDEBUG("close audio #1\n"); | |
469 xs_plugin_ip.output->close_audio(); | |
470 audioOpen = FALSE; | |
471 } | |
472 | |
473 /* Now determine if we continue by selecting other subtune or something */ | |
474 if (!myStatus.isPlaying && !xs_cfg.subAutoEnable) | |
475 doPlay = FALSE; | |
476 } | |
477 | |
478 xs_err_exit: | |
479 /* Close audio output plugin */ | |
480 if (audioOpen) { | |
481 XSDEBUG("close audio #2\n"); | |
482 xs_plugin_ip.output->close_audio(); | |
483 } | |
484 | |
485 g_free(audioBuffer); | |
486 g_free(oversampleBuffer); | |
487 | |
488 /* Set playing status to false (stopped), thus when | |
489 * XMMS next calls xs_get_time(), it can return appropriate | |
490 * value "not playing" status and XMMS knows to move to | |
491 * next entry in the playlist .. or whatever it wishes. | |
492 */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
493 g_static_mutex_lock(&xs_status_mutex); |
269 | 494 xs_status.isPlaying = FALSE; |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
495 g_static_mutex_unlock(&xs_status_mutex); |
269 | 496 |
497 /* Exit the playing thread */ | |
498 XSDEBUG("exiting thread, bye.\n"); | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
499 g_thread_exit(NULL); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
500 return(NULL); |
269 | 501 } |
502 | |
503 | |
504 /* | |
505 * Start playing the given file | |
506 * Here we load the tune and initialize the playing thread. | |
507 * Usually you would also initialize the output-plugin, but | |
508 * this is XMMS-SID and we do it on the player thread instead. | |
509 */ | |
510 void xs_play_file(gchar * pcFilename) | |
511 { | |
512 assert(xs_status.sidPlayer); | |
513 | |
514 XSDEBUG("play '%s'\n", pcFilename); | |
515 | |
516 /* Get tune information */ | |
517 if ((xs_status.tuneInfo = xs_status.sidPlayer->plrGetSIDInfo(pcFilename)) == NULL) | |
518 return; | |
519 | |
520 /* Initialize the tune */ | |
521 if (!xs_status.sidPlayer->plrLoadSID(&xs_status, pcFilename)) { | |
522 xs_tuneinfo_free(xs_status.tuneInfo); | |
523 xs_status.tuneInfo = NULL; | |
524 return; | |
525 } | |
526 | |
527 XSDEBUG("load ok\n"); | |
528 | |
529 /* Set general status information */ | |
530 xs_status.isPlaying = TRUE; | |
531 xs_status.isError = FALSE; | |
532 xs_status.currSong = xs_status.tuneInfo->startTune; | |
533 | |
534 /* Start the playing thread! */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
535 xs_decode_thread = g_thread_create((GThreadFunc)xs_playthread, NULL, TRUE, NULL); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
536 if (xs_decode_thread == NULL) { |
269 | 537 XSERR("Couldn't start playing thread!\n"); |
538 xs_tuneinfo_free(xs_status.tuneInfo); | |
539 xs_status.tuneInfo = NULL; | |
540 xs_status.sidPlayer->plrDeleteSID(&xs_status); | |
541 } | |
542 | |
543 /* Okay, here the playing thread has started up and we | |
544 * return from here to XMMS. Rest is up to XMMS's GUI | |
545 * and playing thread. | |
546 */ | |
547 XSDEBUG("systems should be up?\n"); | |
548 } | |
549 | |
550 | |
551 /* | |
552 * Stop playing | |
553 * Here we set the playing status to stop and wait for playing | |
554 * thread to shut down. In any "correctly" done plugin, this is | |
555 * also the function where you close the output-plugin, but since | |
556 * XMMS-SID has special behaviour (audio opened/closed in the | |
557 * playing thread), we don't do that here. | |
558 * | |
559 * Finally tune and other memory allocations are free'd. | |
560 */ | |
561 void xs_stop(void) | |
562 { | |
563 XSDEBUG("STOP_REQ\n"); | |
564 | |
565 /* Close the sub-tune control window, if any */ | |
566 xs_subctrl_close(); | |
567 | |
568 /* Lock xs_status and stop playing thread */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
569 g_static_mutex_lock(&xs_status_mutex); |
269 | 570 if (xs_status.isPlaying) { |
571 /* Stop playing */ | |
572 XSDEBUG("stopping...\n"); | |
573 xs_status.isPlaying = FALSE; | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
574 g_static_mutex_unlock(&xs_status_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
575 g_thread_join(xs_decode_thread); |
269 | 576 } else { |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
577 g_static_mutex_unlock(&xs_status_mutex); |
269 | 578 } |
579 | |
580 /* Status is now stopped, update the sub-tune | |
581 * controller in fileinfo window (if open) | |
582 */ | |
583 xs_fileinfo_update(); | |
584 | |
585 /* Free tune information */ | |
586 xs_status.sidPlayer->plrDeleteSID(&xs_status); | |
587 xs_tuneinfo_free(xs_status.tuneInfo); | |
588 xs_status.tuneInfo = NULL; | |
589 } | |
590 | |
591 | |
592 /* | |
593 * Pause/unpause the playing | |
594 */ | |
595 void xs_pause(short pauseState) | |
596 { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
597 g_static_mutex_lock(&xs_status_mutex); |
269 | 598 /* FIXME FIX ME todo: pause should disable sub-tune controls */ |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
599 g_static_mutex_unlock(&xs_status_mutex); |
269 | 600 |
601 xs_subctrl_close(); | |
602 xs_fileinfo_update(); | |
603 xs_plugin_ip.output->pause(pauseState); | |
604 } | |
605 | |
606 | |
607 /* | |
608 * Pop-up subtune selector | |
609 */ | |
610 void xs_subctrl_setsong(void) | |
611 { | |
612 gint n; | |
613 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
614 g_static_mutex_lock(&xs_status_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
615 g_static_mutex_lock(&xs_subctrl_mutex); |
269 | 616 |
617 if (xs_status.tuneInfo && xs_status.isPlaying) { | |
618 n = (gint) GTK_ADJUSTMENT(xs_subctrl_adj)->value; | |
619 if ((n >= 1) && (n <= xs_status.tuneInfo->nsubTunes)) | |
620 xs_status.currSong = n; | |
621 } | |
622 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
623 g_static_mutex_unlock(&xs_subctrl_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
624 g_static_mutex_unlock(&xs_status_mutex); |
269 | 625 } |
626 | |
627 | |
628 void xs_subctrl_prevsong(void) | |
629 { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
630 g_static_mutex_lock(&xs_status_mutex); |
269 | 631 |
632 if (xs_status.tuneInfo && xs_status.isPlaying) { | |
633 if (xs_status.currSong > 1) | |
634 xs_status.currSong--; | |
635 } | |
636 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
637 g_static_mutex_unlock(&xs_status_mutex); |
269 | 638 |
639 xs_subctrl_update(); | |
640 } | |
641 | |
642 | |
643 void xs_subctrl_nextsong(void) | |
644 { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
645 g_static_mutex_lock(&xs_status_mutex); |
269 | 646 |
647 if (xs_status.tuneInfo && xs_status.isPlaying) { | |
648 if (xs_status.currSong < xs_status.tuneInfo->nsubTunes) | |
649 xs_status.currSong++; | |
650 } | |
651 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
652 g_static_mutex_unlock(&xs_status_mutex); |
269 | 653 |
654 xs_subctrl_update(); | |
655 } | |
656 | |
657 | |
658 void xs_subctrl_update(void) | |
659 { | |
660 GtkAdjustment *tmpAdj; | |
661 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
662 g_static_mutex_lock(&xs_status_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
663 g_static_mutex_lock(&xs_subctrl_mutex); |
269 | 664 |
665 /* Check if control window exists, we are currently playing and have a tune */ | |
666 if (xs_subctrl) { | |
667 if (xs_status.tuneInfo && xs_status.isPlaying) { | |
668 tmpAdj = GTK_ADJUSTMENT(xs_subctrl_adj); | |
669 | |
670 tmpAdj->value = xs_status.currSong; | |
671 tmpAdj->lower = 1; | |
672 tmpAdj->upper = xs_status.tuneInfo->nsubTunes; | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
673 g_static_mutex_unlock(&xs_status_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
674 g_static_mutex_unlock(&xs_subctrl_mutex); |
269 | 675 gtk_adjustment_value_changed(tmpAdj); |
676 } else { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
677 g_static_mutex_unlock(&xs_status_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
678 g_static_mutex_unlock(&xs_subctrl_mutex); |
269 | 679 xs_subctrl_close(); |
680 } | |
681 } else { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
682 g_static_mutex_unlock(&xs_subctrl_mutex); |
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
683 g_static_mutex_unlock(&xs_status_mutex); |
269 | 684 } |
685 | |
686 xs_fileinfo_update(); | |
687 } | |
688 | |
689 | |
690 void xs_subctrl_close(void) | |
691 { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
692 g_static_mutex_lock(&xs_subctrl_mutex); |
269 | 693 |
694 if (xs_subctrl) { | |
695 gtk_widget_destroy(xs_subctrl); | |
696 xs_subctrl = NULL; | |
697 } | |
698 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
699 g_static_mutex_unlock(&xs_subctrl_mutex); |
269 | 700 } |
701 | |
702 | |
703 gboolean xs_subctrl_keypress(GtkWidget * win, GdkEventKey * ev) | |
704 { | |
705 (void) win; | |
706 | |
707 if (ev->keyval == GDK_Escape) | |
708 xs_subctrl_close(); | |
709 | |
710 return FALSE; | |
711 } | |
712 | |
713 | |
714 void xs_subctrl_open(void) | |
715 { | |
716 GtkWidget *frame25, *hbox15, *subctrl_prev, *subctrl_current, *subctrl_next; | |
717 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
718 g_static_mutex_lock(&xs_subctrl_mutex); |
269 | 719 if (!xs_status.tuneInfo || !xs_status.isPlaying || xs_subctrl || (xs_status.tuneInfo->nsubTunes <= 1)) { |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
720 g_static_mutex_unlock(&xs_subctrl_mutex); |
269 | 721 return; |
722 } | |
723 | |
724 /* Create the pop-up window */ | |
725 xs_subctrl = gtk_window_new (GTK_WINDOW_TOPLEVEL); | |
726 gtk_window_set_type_hint (GTK_WINDOW(xs_subctrl), GDK_WINDOW_TYPE_HINT_DIALOG); | |
727 gtk_widget_set_name(xs_subctrl, "xs_subctrl"); | |
728 gtk_object_set_data(GTK_OBJECT(xs_subctrl), "xs_subctrl", xs_subctrl); | |
729 | |
730 gtk_window_set_title(GTK_WINDOW(xs_subctrl), "Subtune Control"); | |
731 gtk_window_set_position(GTK_WINDOW(xs_subctrl), GTK_WIN_POS_MOUSE); | |
732 gtk_container_set_border_width(GTK_CONTAINER(xs_subctrl), 0); | |
733 gtk_window_set_policy(GTK_WINDOW(xs_subctrl), FALSE, FALSE, FALSE); | |
734 | |
735 gtk_signal_connect(GTK_OBJECT(xs_subctrl), "destroy", GTK_SIGNAL_FUNC(gtk_widget_destroyed), &xs_subctrl); | |
736 | |
737 gtk_signal_connect(GTK_OBJECT(xs_subctrl), "focus_out_event", GTK_SIGNAL_FUNC(xs_subctrl_close), NULL); | |
738 | |
739 gtk_widget_realize(xs_subctrl); | |
740 gdk_window_set_decorations(xs_subctrl->window, (GdkWMDecoration) 0); | |
741 | |
742 | |
743 /* Create the control widgets */ | |
744 frame25 = gtk_frame_new(NULL); | |
745 gtk_container_add(GTK_CONTAINER(xs_subctrl), frame25); | |
746 gtk_container_set_border_width(GTK_CONTAINER(frame25), 2); | |
747 gtk_frame_set_shadow_type(GTK_FRAME(frame25), GTK_SHADOW_OUT); | |
748 | |
749 hbox15 = gtk_hbox_new(FALSE, 4); | |
750 gtk_container_add(GTK_CONTAINER(frame25), hbox15); | |
751 | |
752 subctrl_prev = gtk_button_new_with_label(" < "); | |
753 gtk_widget_set_name(subctrl_prev, "subctrl_prev"); | |
754 gtk_box_pack_start(GTK_BOX(hbox15), subctrl_prev, FALSE, FALSE, 0); | |
755 | |
756 xs_subctrl_adj = gtk_adjustment_new(xs_status.currSong, 1, xs_status.tuneInfo->nsubTunes, 1, 1, 0); | |
757 gtk_signal_connect(GTK_OBJECT(xs_subctrl_adj), "value_changed", GTK_SIGNAL_FUNC(xs_subctrl_setsong), NULL); | |
758 | |
759 subctrl_current = gtk_hscale_new(GTK_ADJUSTMENT(xs_subctrl_adj)); | |
760 gtk_widget_set_name(subctrl_current, "subctrl_current"); | |
761 gtk_box_pack_start(GTK_BOX(hbox15), subctrl_current, FALSE, TRUE, 0); | |
762 gtk_scale_set_digits(GTK_SCALE(subctrl_current), 0); | |
763 gtk_range_set_update_policy(GTK_RANGE(subctrl_current), GTK_UPDATE_DELAYED); | |
764 gtk_widget_grab_focus(subctrl_current); | |
765 | |
766 subctrl_next = gtk_button_new_with_label(" > "); | |
767 gtk_widget_set_name(subctrl_next, "subctrl_next"); | |
768 gtk_box_pack_start(GTK_BOX(hbox15), subctrl_next, FALSE, FALSE, 0); | |
769 | |
770 gtk_signal_connect(GTK_OBJECT(subctrl_prev), "clicked", GTK_SIGNAL_FUNC(xs_subctrl_prevsong), NULL); | |
771 | |
772 gtk_signal_connect(GTK_OBJECT(subctrl_next), "clicked", GTK_SIGNAL_FUNC(xs_subctrl_nextsong), NULL); | |
773 | |
774 gtk_signal_connect(GTK_OBJECT(xs_subctrl), "key_press_event", GTK_SIGNAL_FUNC(xs_subctrl_keypress), NULL); | |
775 | |
776 gtk_widget_show_all(xs_subctrl); | |
777 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
778 g_static_mutex_unlock(&xs_subctrl_mutex); |
269 | 779 } |
780 | |
781 | |
782 /* | |
783 * Set the time-seek position | |
784 * The playing thread will do the "seeking", which means sub-tune | |
785 * changing in XMMS-SID's case. iTime argument is time in seconds, | |
786 * in contrast to milliseconds used in other occasions. | |
787 * | |
788 * This function is called whenever position slider is clicked or | |
789 * other method of seeking is used (keyboard, etc.) | |
790 */ | |
791 void xs_seek(gint iTime) | |
792 { | |
793 /* Check status */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
794 g_static_mutex_lock(&xs_status_mutex); |
269 | 795 if (!xs_status.tuneInfo || !xs_status.isPlaying) { |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
796 g_static_mutex_unlock(&xs_status_mutex); |
269 | 797 return; |
798 } | |
799 | |
800 /* Act according to settings */ | |
801 switch (xs_cfg.subsongControl) { | |
802 case XS_SSC_SEEK: | |
803 if (iTime < xs_status.lastTime) { | |
804 if (xs_status.currSong > 1) | |
805 xs_status.currSong--; | |
806 } else if (iTime > xs_status.lastTime) { | |
807 if (xs_status.currSong < xs_status.tuneInfo->nsubTunes) | |
808 xs_status.currSong++; | |
809 } | |
810 break; | |
811 | |
812 case XS_SSC_POPUP: | |
813 xs_subctrl_open(); | |
814 break; | |
815 | |
816 /* If we have song-position patch, check settings */ | |
817 #ifdef HAVE_SONG_POSITION | |
818 case XS_SSC_PATCH: | |
819 if ((iTime > 0) && (iTime <= xs_status.tuneInfo->nsubTunes)) | |
820 xs_status.currSong = iTime; | |
821 break; | |
822 #endif | |
823 } | |
824 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
825 g_static_mutex_unlock(&xs_status_mutex); |
269 | 826 } |
827 | |
828 | |
829 /* | |
830 * Return the playing "position/time" | |
831 * Determine current position/time in song. Used by XMMS to update | |
832 * the song clock and position slider and MOST importantly to determine | |
833 * END OF SONG! Return value of -2 means error, XMMS opens an audio | |
834 * error dialog. -1 means end of song (if one was playing currently). | |
835 */ | |
836 gint xs_get_time(void) | |
837 { | |
838 /* If errorflag is set, return -2 to signal it to XMMS's idle callback */ | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
839 g_static_mutex_lock(&xs_status_mutex); |
269 | 840 if (xs_status.isError) { |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
841 g_static_mutex_unlock(&xs_status_mutex); |
269 | 842 return -2; |
843 } | |
844 | |
845 /* If there is no tune, return -1 */ | |
846 if (!xs_status.tuneInfo) { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
847 g_static_mutex_unlock(&xs_status_mutex); |
269 | 848 return -1; |
849 } | |
850 | |
851 /* If tune has ended, return -1 */ | |
852 if (!xs_status.isPlaying) { | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
853 g_static_mutex_unlock(&xs_status_mutex); |
269 | 854 return -1; |
855 } | |
856 | |
857 /* Let's see what we do */ | |
858 switch (xs_cfg.subsongControl) { | |
859 case XS_SSC_SEEK: | |
860 xs_status.lastTime = (xs_plugin_ip.output->output_time() / 1000); | |
861 break; | |
862 | |
863 #ifdef HAVE_SONG_POSITION | |
864 case XS_SSC_PATCH: | |
865 set_song_position(xs_status.currSong, 1, xs_status.tuneInfo->nsubTunes); | |
866 break; | |
867 #endif | |
868 } | |
869 | |
304
d0e9693d2115
[svn] Convert to configdb. GThreadify and peel off layer of threading macro madness.
chainsaw
parents:
269
diff
changeset
|
870 g_static_mutex_unlock(&xs_status_mutex); |
269 | 871 |
872 /* Return output time reported by audio output plugin */ | |
873 return xs_plugin_ip.output->output_time(); | |
874 } | |
875 | |
876 | |
877 /* | |
878 * Return song information | |
879 * This function is called by XMMS when initially loading the playlist. | |
880 * Subsequent changes to information are made by the player thread, | |
881 * which uses xs_plugin_ip.set_info(); | |
882 */ | |
883 void xs_get_song_info(gchar * songFilename, gchar ** songTitle, gint * songLength) | |
884 { | |
885 t_xs_tuneinfo *pInfo; | |
886 gint tmpInt; | |
887 | |
888 /* Get tune information from emulation engine */ | |
889 pInfo = xs_status.sidPlayer->plrGetSIDInfo(songFilename); | |
890 if (!pInfo) | |
891 return; | |
892 | |
893 /* Get sub-tune information, if available */ | |
894 if ((pInfo->startTune > 0) && (pInfo->startTune <= pInfo->nsubTunes)) { | |
895 (*songTitle) = g_strdup(pInfo->subTunes[pInfo->startTune - 1].tuneTitle); | |
896 | |
897 tmpInt = pInfo->subTunes[pInfo->startTune - 1].tuneLength; | |
898 if (tmpInt < 0) | |
899 (*songLength) = -1; | |
900 else | |
901 (*songLength) = (tmpInt * 1000); | |
902 } | |
903 | |
904 /* Free tune information */ | |
905 xs_tuneinfo_free(pInfo); | |
906 } | |
907 | |
908 | |
909 /* Allocate a new tune information structure | |
910 */ | |
911 t_xs_tuneinfo *xs_tuneinfo_new(gchar * pcFilename, gint nsubTunes, gint startTune, | |
912 gchar * sidName, gchar * sidComposer, gchar * sidCopyright, | |
913 gint loadAddr, gint initAddr, gint playAddr, gint dataFileLen) | |
914 { | |
915 t_xs_tuneinfo *pResult; | |
916 | |
917 /* Allocate structure */ | |
918 pResult = (t_xs_tuneinfo *) g_malloc0(sizeof(t_xs_tuneinfo)); | |
919 if (!pResult) { | |
920 XSERR("Could not allocate memory for t_xs_tuneinfo ('%s')\n", pcFilename); | |
921 return NULL; | |
922 } | |
923 | |
924 pResult->sidFilename = g_strdup(pcFilename); | |
925 if (!pResult->sidFilename) { | |
926 XSERR("Could not allocate sidFilename ('%s')\n", pcFilename); | |
927 g_free(pResult); | |
928 return NULL; | |
929 } | |
930 | |
931 /* Allocate space for subtune information */ | |
932 if (nsubTunes > 0) { | |
933 pResult->subTunes = g_malloc0(sizeof(t_xs_subtuneinfo) * nsubTunes); | |
934 if (!pResult->subTunes) { | |
935 XSERR("Could not allocate memory for t_xs_subtuneinfo ('%s', %i)\n", pcFilename, nsubTunes); | |
936 | |
937 g_free(pResult->sidFilename); | |
938 g_free(pResult); | |
939 return NULL; | |
940 } | |
941 } | |
942 | |
943 /* The following allocations don't matter if they fail */ | |
944 pResult->sidName = g_strdup(sidName); | |
945 pResult->sidComposer = g_strdup(sidComposer); | |
946 pResult->sidCopyright = g_strdup(sidCopyright); | |
947 | |
948 pResult->nsubTunes = nsubTunes; | |
949 pResult->startTune = startTune; | |
950 | |
951 pResult->loadAddr = loadAddr; | |
952 pResult->initAddr = initAddr; | |
953 pResult->playAddr = playAddr; | |
954 pResult->dataFileLen = dataFileLen; | |
955 | |
956 return pResult; | |
957 } | |
958 | |
959 | |
960 /* Free given tune information structure | |
961 */ | |
962 void xs_tuneinfo_free(t_xs_tuneinfo * pTune) | |
963 { | |
964 gint i; | |
965 if (!pTune) | |
966 return; | |
967 | |
968 for (i = 0; i < pTune->nsubTunes; i++) { | |
969 g_free(pTune->subTunes[i].tuneTitle); | |
970 pTune->subTunes[i].tuneTitle = NULL; | |
971 } | |
972 | |
973 g_free(pTune->subTunes); | |
974 pTune->nsubTunes = 0; | |
975 g_free(pTune->sidFilename); | |
976 pTune->sidFilename = NULL; | |
977 g_free(pTune->sidName); | |
978 pTune->sidName = NULL; | |
979 g_free(pTune->sidComposer); | |
980 pTune->sidComposer = NULL; | |
981 g_free(pTune->sidCopyright); | |
982 pTune->sidCopyright = NULL; | |
983 g_free(pTune); | |
984 } |