3 * Copyright (c)1999 Patrick Crosby <xb@dotfiles.com>.
4 * This software covered by the GPL. See COPYING file for details.
8 * This file contains all the functions for controlling the
9 * mpg123 backend processes.
11 * Random play functionality courtesy of:
12 * Matthew D. Campbell <matt@campbellhome.dhs.org>
14 * $Id: mpg123ctl.c,v 1.15 1999/10/12 04:41:20 pcrosby Exp $
17 #include "mpg123ctl.h"
20 #define MAX_TITLE_LEN 9
28 void push_dir(char *s
);
29 void push_dirname(char *s
);
33 char *current_mp3dir();
34 void show_directory_name();
37 void alarmhandler(int sig
);
47 char *dirnames
[MAXDIRS
];
62 int always_scroll
= 0;
66 int *rand_song_num
= NULL
;
77 * patch received from Steven Jorgensen to fix following function
87 int i
, tempnum
, temppos
;
89 directory
= (char *) current_mp3dir();
93 dp
= opendir(directory
);
96 new_directory
= (char *) next_mp3dir();
97 dp
= opendir(new_directory
);
98 while (new_directory
&& (strcmp(new_directory
, directory
) != 0) &&
100 strcpy(directory
, new_directory
);
101 new_directory
= (char *) next_mp3dir();
102 dp
= opendir(new_directory
);
105 strcpy(directory
, new_directory
);
108 show_directory_name();
112 while (entry
!= NULL
) {
113 if (strstr(entry
->d_name
, mp3ext
)) {
114 sprintf(filename
, "%s/%s", directory
, entry
->d_name
);
115 insert_song(num_songs
, entry
->d_name
, filename
);
122 /* Create Pseudo-random permutation of list */
124 rand_song_num
= (int *)malloc(sizeof(int)*num_songs
);
125 if (!rand_song_num
) {
126 /* This shouldn't happen - the list isn't that big */
128 "Error: cannot allocate randomized list\n");
131 for (i
=0; i
<num_songs
; i
++) rand_song_num
[i
] = i
;
132 for (i
=0; i
<num_songs
; i
++) {
133 tempnum
= rand_song_num
[i
];
134 temppos
= rand()%num_songs
;
135 rand_song_num
[i
] = rand_song_num
[temppos
];
136 rand_song_num
[temppos
] = tempnum
;
144 * functions that actually control mpg123
151 waitpid(play_pid
, &status
, 0);
156 void play(char *song_name
)
158 if ((play_pid
= fork()) == 0) {
159 execl(mpg123_cmd
, mpg123_cmd
, "-q",
167 struct hash_elt
*song
;
169 if (next_song
>= num_songs
) {
170 if (repeat_flag
== 0) {
176 song
= get_song(next_song
);
178 song
= get_song(rand_song_num
[next_song
]);
181 strcpy(title
, song
->title
);
187 play(song
->filename
);
189 signal(SIGCHLD
, signal_play
);
190 signal(SIGALRM
, alarmhandler
);
197 struct hash_elt
*song
;
199 if (next_song
<= 1) {
203 next_song
= next_song
- 2;
212 signal(SIGCHLD
, signal_play
);
223 signal(SIGCHLD
, SIG_IGN
);
224 kill(play_pid
, SIGINT
);
225 kill(play_pid
, SIGTERM
);
226 kill(play_pid
, SIGKILL
);
227 waitpid(play_pid
, &status
, 0);
245 * initialization functions
250 signal(SIGINT
, finish
);
251 signal(SIGCHLD
, dostuff
);
255 void set_mpg123(char *s
)
258 strcpy(mpg123_cmd
, s
);
261 void set_mp3ext(char *s
)
266 void set_playlistext(char *s
)
268 strcpy(playlistext
, s
);
271 void set_alwaysscroll(char *s
) {
274 *temp
= tolower(*temp
);
277 if ( !strcmp(s
, "on") || !strcmp(s
, "yes") || !strcmp(s
, "1") ||
278 !strcmp(s
, "true")) {
286 void push_dir(char *s
)
288 dirs
[top
] = (char *) malloc(strlen(s
) + 1);
289 strcpy(dirs
[top
], s
);
294 void push_dirname(char *s
)
296 /* from Steven Jorgensen */
297 if ((strlen(s
) + 1) < 10)
298 dirnames
[ntop
] = (char *) malloc(10);
300 dirnames
[ntop
] = (char *) malloc(strlen(s
) + 1);
301 strcpy(dirnames
[ntop
], s
);
309 return (dirs
[top
--]);
312 void add_mp3dir(char *s
)
317 void add_mp3dirname(char *s
)
323 * directory manipulation
328 if (current_dir
< (max_dirs
- 1)) {
331 return (dirs
[current_dir
]);
336 if (current_dir
> 0) {
339 return (dirs
[current_dir
]);
342 char *current_mp3dir()
344 return (dirs
[current_dir
]);
347 void show_directory_name()
349 if (dirnames
[current_dir
] != NULL
) {
350 while (strlen(dirnames
[current_dir
]) < 9) {
351 strcat(dirnames
[current_dir
], " ");
353 draw_string(dirnames
[current_dir
], 5, 5);
355 draw_string("no dirname", 5, 5);
359 void dir_up(int button_num
)
362 button_down(button_num
);
367 void dir_down(int button_num
)
370 button_down(button_num
);
388 * song title functions
391 void alarmhandler(int sig
)
393 if ((play_pid
> 0) && (do_scroll
== 1)) {
395 signal(SIGALRM
, alarmhandler
);
402 char s
[MAX_TITLE_LEN
+ 1];
406 title_len
= strlen(title
);
408 for (i
= 0; i
< MAX_TITLE_LEN
; i
++) {
409 s
[i
] = title
[(i
+ scroll_pos
) % title_len
];
412 draw_string(s
, 5, 19);
414 if (scroll_pos
> title_len
) {
416 if (!always_scroll
) {
421 draw_string(title
, 5, 19);
426 void turn_on_scroll()
428 if ((!do_scroll
) && (is_playing())) {
431 signal(SIGALRM
, alarmhandler
);
440 void random_toggle(int button_num
)
442 /* button_gray(button_num); */
443 if (random_flag
== 0) {
444 button_down(button_num
);
447 button_up(button_num
);
452 void repeat_toggle(int button_num
)
454 if (repeat_flag
== 0) {
455 button_down(button_num
);
458 button_up(button_num
);