comparison src/Input/sid/xmms-sid.c @ 0:13389e613d67 trunk

[svn] - initial import of audacious-plugins tree (lots to do)
author nenolod
date Mon, 18 Sep 2006 01:11:49 -0700
parents
children 6303e3a8a6b8
comparison
equal deleted inserted replaced
-1:000000000000 0:13389e613d67
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
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>
34 #include <audacious/output.h>
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;
90 extern GStaticMutex xs_status_mutex;
91 extern GStaticMutex xs_cfg_mutex;
92 GStaticMutex xs_subctrl_mutex = G_STATIC_MUTEX_INIT;
93
94 static GThread *xs_decode_thread;
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 */
137 g_static_mutex_lock(&xs_status_mutex);
138 if (xs_status.isPlaying) {
139 g_static_mutex_unlock(&xs_status_mutex);
140 xs_stop();
141 } else {
142 g_static_mutex_unlock(&xs_status_mutex);
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
267 if (xs_status.sidPlayer->plrIsOurFile(pcFilename))
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");
290 g_static_mutex_lock(&xs_status_mutex);
291 memcpy(&myStatus, &xs_status, sizeof(t_xs_status));
292 myTune = xs_status.tuneInfo;
293 g_static_mutex_unlock(&xs_status_mutex);
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 */
319 g_static_mutex_lock(&xs_cfg_mutex);
320 g_static_mutex_lock(&xs_status_mutex);
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
353 g_static_mutex_unlock(&xs_status_mutex);
354 g_static_mutex_unlock(&xs_cfg_mutex);
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;
362 g_static_mutex_unlock(&xs_status_mutex);
363 g_static_mutex_unlock(&xs_cfg_mutex);
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
392 g_static_mutex_lock(&xs_status_mutex);
393 xs_status.isError = TRUE;
394 g_static_mutex_unlock(&xs_status_mutex);
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");
428 g_static_mutex_lock(&xs_status_mutex);
429 xs_status.isError = TRUE;
430 g_static_mutex_unlock(&xs_status_mutex);
431 goto xs_err_exit;
432 }
433 } else
434 audioGot = myStatus.sidPlayer->plrFillBuffer(&myStatus, audioBuffer, XS_AUDIOBUF_SIZE);
435
436 /* I <3 visualice/haujobb */
437 produce_audio(xs_plugin_ip.output->written_time(),
438 myStatus.audioFormat, myStatus.audioChannels, audioGot, audioBuffer, NULL);
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 */
493 g_static_mutex_lock(&xs_status_mutex);
494 xs_status.isPlaying = FALSE;
495 g_static_mutex_unlock(&xs_status_mutex);
496
497 /* Exit the playing thread */
498 XSDEBUG("exiting thread, bye.\n");
499 g_thread_exit(NULL);
500 return(NULL);
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! */
535 xs_decode_thread = g_thread_create((GThreadFunc)xs_playthread, NULL, TRUE, NULL);
536 if (xs_decode_thread == NULL) {
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 */
569 g_static_mutex_lock(&xs_status_mutex);
570 if (xs_status.isPlaying) {
571 /* Stop playing */
572 XSDEBUG("stopping...\n");
573 xs_status.isPlaying = FALSE;
574 g_static_mutex_unlock(&xs_status_mutex);
575 g_thread_join(xs_decode_thread);
576 } else {
577 g_static_mutex_unlock(&xs_status_mutex);
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 {
597 g_static_mutex_lock(&xs_status_mutex);
598 /* FIXME FIX ME todo: pause should disable sub-tune controls */
599 g_static_mutex_unlock(&xs_status_mutex);
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
614 g_static_mutex_lock(&xs_status_mutex);
615 g_static_mutex_lock(&xs_subctrl_mutex);
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
623 g_static_mutex_unlock(&xs_subctrl_mutex);
624 g_static_mutex_unlock(&xs_status_mutex);
625 }
626
627
628 void xs_subctrl_prevsong(void)
629 {
630 g_static_mutex_lock(&xs_status_mutex);
631
632 if (xs_status.tuneInfo && xs_status.isPlaying) {
633 if (xs_status.currSong > 1)
634 xs_status.currSong--;
635 }
636
637 g_static_mutex_unlock(&xs_status_mutex);
638
639 xs_subctrl_update();
640 }
641
642
643 void xs_subctrl_nextsong(void)
644 {
645 g_static_mutex_lock(&xs_status_mutex);
646
647 if (xs_status.tuneInfo && xs_status.isPlaying) {
648 if (xs_status.currSong < xs_status.tuneInfo->nsubTunes)
649 xs_status.currSong++;
650 }
651
652 g_static_mutex_unlock(&xs_status_mutex);
653
654 xs_subctrl_update();
655 }
656
657
658 void xs_subctrl_update(void)
659 {
660 GtkAdjustment *tmpAdj;
661
662 g_static_mutex_lock(&xs_status_mutex);
663 g_static_mutex_lock(&xs_subctrl_mutex);
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;
673 g_static_mutex_unlock(&xs_status_mutex);
674 g_static_mutex_unlock(&xs_subctrl_mutex);
675 gtk_adjustment_value_changed(tmpAdj);
676 } else {
677 g_static_mutex_unlock(&xs_status_mutex);
678 g_static_mutex_unlock(&xs_subctrl_mutex);
679 xs_subctrl_close();
680 }
681 } else {
682 g_static_mutex_unlock(&xs_subctrl_mutex);
683 g_static_mutex_unlock(&xs_status_mutex);
684 }
685
686 xs_fileinfo_update();
687 }
688
689
690 void xs_subctrl_close(void)
691 {
692 g_static_mutex_lock(&xs_subctrl_mutex);
693
694 if (xs_subctrl) {
695 gtk_widget_destroy(xs_subctrl);
696 xs_subctrl = NULL;
697 }
698
699 g_static_mutex_unlock(&xs_subctrl_mutex);
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
718 g_static_mutex_lock(&xs_subctrl_mutex);
719 if (!xs_status.tuneInfo || !xs_status.isPlaying || xs_subctrl || (xs_status.tuneInfo->nsubTunes <= 1)) {
720 g_static_mutex_unlock(&xs_subctrl_mutex);
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 g_object_set_data(G_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 g_signal_connect(G_OBJECT(xs_subctrl), "destroy", G_CALLBACK(gtk_widget_destroyed), &xs_subctrl);
736
737 g_signal_connect(G_OBJECT(xs_subctrl), "focus_out_event", G_CALLBACK(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 g_signal_connect(G_OBJECT(xs_subctrl_adj), "value_changed", G_CALLBACK(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_widget_set_usize(subctrl_current, xs_status.tuneInfo->nsubTunes * 10 + 30, -1);
762 gtk_box_pack_start(GTK_BOX(hbox15), subctrl_current, FALSE, TRUE, 0);
763 gtk_scale_set_digits(GTK_SCALE(subctrl_current), 0);
764 gtk_range_set_update_policy(GTK_RANGE(subctrl_current), GTK_UPDATE_DELAYED);
765 gtk_widget_grab_focus(subctrl_current);
766
767 subctrl_next = gtk_button_new_with_label(" > ");
768 gtk_widget_set_name(subctrl_next, "subctrl_next");
769 gtk_box_pack_start(GTK_BOX(hbox15), subctrl_next, FALSE, FALSE, 0);
770
771 g_signal_connect(G_OBJECT(subctrl_prev), "clicked", G_CALLBACK(xs_subctrl_prevsong), NULL);
772
773 g_signal_connect(G_OBJECT(subctrl_next), "clicked", G_CALLBACK(xs_subctrl_nextsong), NULL);
774
775 g_signal_connect(G_OBJECT(xs_subctrl), "key_press_event", G_CALLBACK(xs_subctrl_keypress), NULL);
776
777 gtk_widget_show_all(xs_subctrl);
778
779 g_static_mutex_unlock(&xs_subctrl_mutex);
780 }
781
782
783 /*
784 * Set the time-seek position
785 * The playing thread will do the "seeking", which means sub-tune
786 * changing in XMMS-SID's case. iTime argument is time in seconds,
787 * in contrast to milliseconds used in other occasions.
788 *
789 * This function is called whenever position slider is clicked or
790 * other method of seeking is used (keyboard, etc.)
791 */
792 void xs_seek(gint iTime)
793 {
794 /* Check status */
795 g_static_mutex_lock(&xs_status_mutex);
796 if (!xs_status.tuneInfo || !xs_status.isPlaying) {
797 g_static_mutex_unlock(&xs_status_mutex);
798 return;
799 }
800
801 /* Act according to settings */
802 switch (xs_cfg.subsongControl) {
803 case XS_SSC_SEEK:
804 if (iTime < xs_status.lastTime) {
805 if (xs_status.currSong > 1)
806 xs_status.currSong--;
807 } else if (iTime > xs_status.lastTime) {
808 if (xs_status.currSong < xs_status.tuneInfo->nsubTunes)
809 xs_status.currSong++;
810 }
811 break;
812
813 case XS_SSC_POPUP:
814 xs_subctrl_open();
815 break;
816
817 /* If we have song-position patch, check settings */
818 #ifdef HAVE_SONG_POSITION
819 case XS_SSC_PATCH:
820 if ((iTime > 0) && (iTime <= xs_status.tuneInfo->nsubTunes))
821 xs_status.currSong = iTime;
822 break;
823 #endif
824 }
825
826 g_static_mutex_unlock(&xs_status_mutex);
827 }
828
829
830 /*
831 * Return the playing "position/time"
832 * Determine current position/time in song. Used by XMMS to update
833 * the song clock and position slider and MOST importantly to determine
834 * END OF SONG! Return value of -2 means error, XMMS opens an audio
835 * error dialog. -1 means end of song (if one was playing currently).
836 */
837 gint xs_get_time(void)
838 {
839 /* If errorflag is set, return -2 to signal it to XMMS's idle callback */
840 g_static_mutex_lock(&xs_status_mutex);
841 if (xs_status.isError) {
842 g_static_mutex_unlock(&xs_status_mutex);
843 return -2;
844 }
845
846 /* If there is no tune, return -1 */
847 if (!xs_status.tuneInfo) {
848 g_static_mutex_unlock(&xs_status_mutex);
849 return -1;
850 }
851
852 /* If tune has ended, return -1 */
853 if (!xs_status.isPlaying) {
854 g_static_mutex_unlock(&xs_status_mutex);
855 return -1;
856 }
857
858 /* Let's see what we do */
859 switch (xs_cfg.subsongControl) {
860 case XS_SSC_SEEK:
861 xs_status.lastTime = (xs_plugin_ip.output->output_time() / 1000);
862 break;
863
864 #ifdef HAVE_SONG_POSITION
865 case XS_SSC_PATCH:
866 set_song_position(xs_status.currSong, 1, xs_status.tuneInfo->nsubTunes);
867 break;
868 #endif
869 }
870
871 g_static_mutex_unlock(&xs_status_mutex);
872
873 /* Return output time reported by audio output plugin */
874 return xs_plugin_ip.output->output_time();
875 }
876
877
878 /*
879 * Return song information
880 * This function is called by XMMS when initially loading the playlist.
881 * Subsequent changes to information are made by the player thread,
882 * which uses xs_plugin_ip.set_info();
883 */
884 void xs_get_song_info(gchar * songFilename, gchar ** songTitle, gint * songLength)
885 {
886 t_xs_tuneinfo *pInfo;
887 gint tmpInt;
888
889 /* Get tune information from emulation engine */
890 pInfo = xs_status.sidPlayer->plrGetSIDInfo(songFilename);
891 if (!pInfo)
892 return;
893
894 /* Get sub-tune information, if available */
895 if ((pInfo->startTune > 0) && (pInfo->startTune <= pInfo->nsubTunes)) {
896 (*songTitle) = g_strdup(pInfo->subTunes[pInfo->startTune - 1].tuneTitle);
897
898 tmpInt = pInfo->subTunes[pInfo->startTune - 1].tuneLength;
899 if (tmpInt < 0)
900 (*songLength) = -1;
901 else
902 (*songLength) = (tmpInt * 1000);
903 }
904
905 /* Free tune information */
906 xs_tuneinfo_free(pInfo);
907 }
908
909
910 /* Allocate a new tune information structure
911 */
912 t_xs_tuneinfo *xs_tuneinfo_new(gchar * pcFilename, gint nsubTunes, gint startTune,
913 gchar * sidName, gchar * sidComposer, gchar * sidCopyright,
914 gint loadAddr, gint initAddr, gint playAddr, gint dataFileLen)
915 {
916 t_xs_tuneinfo *pResult;
917
918 /* Allocate structure */
919 pResult = (t_xs_tuneinfo *) g_malloc0(sizeof(t_xs_tuneinfo));
920 if (!pResult) {
921 XSERR("Could not allocate memory for t_xs_tuneinfo ('%s')\n", pcFilename);
922 return NULL;
923 }
924
925 pResult->sidFilename = g_strdup(pcFilename);
926 if (!pResult->sidFilename) {
927 XSERR("Could not allocate sidFilename ('%s')\n", pcFilename);
928 g_free(pResult);
929 return NULL;
930 }
931
932 /* Allocate space for subtune information */
933 if (nsubTunes > 0) {
934 pResult->subTunes = g_malloc0(sizeof(t_xs_subtuneinfo) * nsubTunes);
935 if (!pResult->subTunes) {
936 XSERR("Could not allocate memory for t_xs_subtuneinfo ('%s', %i)\n", pcFilename, nsubTunes);
937
938 g_free(pResult->sidFilename);
939 g_free(pResult);
940 return NULL;
941 }
942 }
943
944 /* The following allocations don't matter if they fail */
945 pResult->sidName = g_strdup(sidName);
946 pResult->sidComposer = g_strdup(sidComposer);
947 pResult->sidCopyright = g_strdup(sidCopyright);
948
949 pResult->nsubTunes = nsubTunes;
950 pResult->startTune = startTune;
951
952 pResult->loadAddr = loadAddr;
953 pResult->initAddr = initAddr;
954 pResult->playAddr = playAddr;
955 pResult->dataFileLen = dataFileLen;
956
957 return pResult;
958 }
959
960
961 /* Free given tune information structure
962 */
963 void xs_tuneinfo_free(t_xs_tuneinfo * pTune)
964 {
965 gint i;
966 if (!pTune)
967 return;
968
969 for (i = 0; i < pTune->nsubTunes; i++) {
970 g_free(pTune->subTunes[i].tuneTitle);
971 pTune->subTunes[i].tuneTitle = NULL;
972 }
973
974 g_free(pTune->subTunes);
975 pTune->nsubTunes = 0;
976 g_free(pTune->sidFilename);
977 pTune->sidFilename = NULL;
978 g_free(pTune->sidName);
979 pTune->sidName = NULL;
980 g_free(pTune->sidComposer);
981 pTune->sidComposer = NULL;
982 g_free(pTune->sidCopyright);
983 pTune->sidCopyright = NULL;
984 g_free(pTune);
985 }