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 $
19 #include "mpg123ctl.h"
22 #define MAX_TITLE_LEN 9
30 void push_dir(char *s
);
31 void push_dirname(char *s
);
35 char *current_mp3dir();
36 void show_directory_name();
39 void alarmhandler(int sig
);
49 char *dirnames
[MAXDIRS
];
64 int always_scroll
= 0;
68 int *rand_song_num
= NULL
;
79 * patch received from Steven Jorgensen to fix following function
89 int i
, tempnum
, temppos
;
91 directory
= (char *) current_mp3dir();
95 dp
= opendir(directory
);
98 new_directory
= (char *) next_mp3dir();
99 dp
= opendir(new_directory
);
100 while (new_directory
&& (strcmp(new_directory
, directory
) != 0) &&
102 strcpy(directory
, new_directory
);
103 new_directory
= (char *) next_mp3dir();
104 dp
= opendir(new_directory
);
107 strcpy(directory
, new_directory
);
110 show_directory_name();
114 while (entry
!= NULL
) {
115 if (strstr(entry
->d_name
, mp3ext
)) {
116 sprintf(filename
, "%s/%s", directory
, entry
->d_name
);
117 insert_song(num_songs
, entry
->d_name
, filename
);
124 /* Create Pseudo-random permutation of list */
126 rand_song_num
= (int *)malloc(sizeof(int)*num_songs
);
127 if (!rand_song_num
) {
128 /* This shouldn't happen - the list isn't that big */
130 "Error: cannot allocate randomized list\n");
133 for (i
=0; i
<num_songs
; i
++) rand_song_num
[i
] = i
;
134 for (i
=0; i
<num_songs
; i
++) {
135 tempnum
= rand_song_num
[i
];
136 temppos
= rand()%num_songs
;
137 rand_song_num
[i
] = rand_song_num
[temppos
];
138 rand_song_num
[temppos
] = tempnum
;
146 * functions that actually control mpg123
153 waitpid(play_pid
, &status
, 0);
158 void play(char *song_name
)
160 if ((play_pid
= fork()) == 0) {
161 execl(mpg123_cmd
, mpg123_cmd
, "-q",
169 struct hash_elt
*song
;
171 if (next_song
>= num_songs
) {
172 if (repeat_flag
== 0) {
178 song
= get_song(next_song
);
180 song
= get_song(rand_song_num
[next_song
]);
183 strcpy(title
, song
->title
);
189 play(song
->filename
);
191 signal(SIGCHLD
, signal_play
);
192 signal(SIGALRM
, alarmhandler
);
199 struct hash_elt
*song
;
201 if (next_song
<= 1) {
205 next_song
= next_song
- 2;
214 signal(SIGCHLD
, signal_play
);
225 signal(SIGCHLD
, SIG_IGN
);
226 kill(play_pid
, SIGINT
);
227 kill(play_pid
, SIGTERM
);
228 kill(play_pid
, SIGKILL
);
229 waitpid(play_pid
, &status
, 0);
247 * initialization functions
252 signal(SIGINT
, finish
);
253 signal(SIGCHLD
, dostuff
);
257 void set_mpg123(char *s
)
260 strcpy(mpg123_cmd
, s
);
263 void set_mp3ext(char *s
)
268 void set_playlistext(char *s
)
270 strcpy(playlistext
, s
);
273 void set_alwaysscroll(char *s
) {
276 *temp
= tolower(*temp
);
279 if ( !strcmp(s
, "on") || !strcmp(s
, "yes") || !strcmp(s
, "1") ||
280 !strcmp(s
, "true")) {
288 void push_dir(char *s
)
290 dirs
[top
] = (char *) malloc(strlen(s
) + 1);
291 strcpy(dirs
[top
], s
);
296 void push_dirname(char *s
)
298 /* from Steven Jorgensen */
299 if ((strlen(s
) + 1) < 10)
300 dirnames
[ntop
] = (char *) malloc(10);
302 dirnames
[ntop
] = (char *) malloc(strlen(s
) + 1);
303 strcpy(dirnames
[ntop
], s
);
311 return (dirs
[top
--]);
314 void add_mp3dir(char *s
)
319 void add_mp3dirname(char *s
)
325 * directory manipulation
330 if (current_dir
< (max_dirs
- 1)) {
333 return (dirs
[current_dir
]);
338 if (current_dir
> 0) {
341 return (dirs
[current_dir
]);
344 char *current_mp3dir()
346 return (dirs
[current_dir
]);
349 void show_directory_name()
351 if (dirnames
[current_dir
] != NULL
) {
352 while (strlen(dirnames
[current_dir
]) < 9) {
353 strcat(dirnames
[current_dir
], " ");
355 draw_string(dirnames
[current_dir
], 5, 5);
357 draw_string("no dirname", 5, 5);
361 void dir_up(int button_num
)
364 button_down(button_num
);
369 void dir_down(int button_num
)
372 button_down(button_num
);
390 * song title functions
393 void alarmhandler(int sig
)
395 if ((play_pid
> 0) && (do_scroll
== 1)) {
397 signal(SIGALRM
, alarmhandler
);
404 char s
[MAX_TITLE_LEN
+ 1];
408 title_len
= strlen(title
);
410 for (i
= 0; i
< MAX_TITLE_LEN
; i
++) {
411 s
[i
] = title
[(i
+ scroll_pos
) % title_len
];
414 draw_string(s
, 5, 19);
416 if (scroll_pos
> title_len
) {
418 if (!always_scroll
) {
423 draw_string(title
, 5, 19);
428 void turn_on_scroll()
430 if ((!do_scroll
) && (is_playing())) {
433 signal(SIGALRM
, alarmhandler
);
442 void random_toggle(int button_num
)
444 /* button_gray(button_num); */
445 if (random_flag
== 0) {
446 button_down(button_num
);
449 button_up(button_num
);
454 void repeat_toggle(int button_num
)
456 if (repeat_flag
== 0) {
457 button_down(button_num
);
460 button_up(button_num
);