1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/* GMP3 - A front end for mpg123
* Copyright (C) 1998 Brett Kosinski
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <strings.h>
#include <fcntl.h>
#include "gmp3.h"
#include "list.h"
int randomSelect(int checkFlags)
{
struct timeval time;
struct timezone zone;
int listLength = g_list_length(activePlayList);
int selected;
int songsLeft = FALSE;
GList *selectedNode;
gettimeofday(&time, &zone);
srand(time.tv_usec);
selectedNode = g_list_first(activePlayList)->next;
if (checkFlags)
while (selectedNode)
{
if (! ((songNode *)selectedNode->data)->played)
songsLeft = TRUE;
selectedNode = selectedNode->next;
}
if (songsLeft || ! checkFlags)
{
selected = rand() % (listLength-1) + 1;
selectedNode = g_list_nth(activePlayList, selected);
if (checkFlags)
{
while (((songNode *)selectedNode->data)->played)
{
selected = rand() % (listLength-1) + 1;
selectedNode = g_list_nth(activePlayList, selected);
}
((songNode *)selectedNode->data)->played = TRUE;
}
currentSong = selectedNode;
return FALSE;
}
else
return TRUE;
}
int nextSong(int checkFlags)
{
int randomResults = FALSE;
if (g_list_first(activePlayList)->next == NULL)
return FALSE;
if (loopOne)
return FALSE;
if (shuffle)
{
randomResults = randomSelect(checkFlags);
if (randomResults && loopAll)
{
randomResults = FALSE;
clearPlayedFlags();
}
return randomResults;
}
else if ((currentSong == g_list_last(activePlayList)) && (loopAll))
currentSong = g_list_first(activePlayList);
else if (currentSong == g_list_last(activePlayList))
return TRUE;
else
currentSong = currentSong->next;
return FALSE;
}
int prevSong(int checkFlags)
{
int randomResults;
if (loopOne)
return FALSE;
if (shuffle)
{
randomResults = randomSelect(checkFlags);
return randomResults;
}
else if ((currentSong == g_list_first(activePlayList)->next) ||
(currentSong == activePlayList))
return TRUE;
else
currentSong = currentSong->prev;
return FALSE;
}
int play(GtkWidget *widget, configType *options)
{
char *cmdLine[CMDLINESIZE];
char *filename;
char *url;
int i;
int optionNum = 0;
if (! checkMP3Player())
return TRUE;
if ((! playing) || (doneSong))
{
doneSong = TRUE;
if (g_list_first(activePlayList)->next == NULL)
return TRUE;
playing = TRUE;
if (currentSong == activePlayList)
currentSong = currentSong->next;
for (i = 0; i < CMDLINESIZE; i++)
{
cmdLine[i] = (char *)malloc(1024);
cmdLine[i][0] = '\0';
}
#ifdef __DEBUG__
g_print("Copying name: %s\n", options->mp3player);
#endif
strncpy(cmdLine[0], options->mp3player, 1023);
optionNum++;
#ifdef __DEBUG__
g_print("Copying buffer: %d\n", options->bufferSize);
#endif
if (options->bufferSize != 0)
{
strcpy(cmdLine[optionNum], "-b");
sprintf(cmdLine[optionNum+1], "%d", options->bufferSize);
optionNum += 2;
}
#ifdef __DEBUG__
g_print("Copying Device: %s\n", options->soundDevice);
#endif
if (strcasecmp(options->soundDevice, SOUND_DEV) != 0)
{
strcpy(cmdLine[optionNum], "-a");
sprintf(cmdLine[optionNum+1], "%s", options->soundDevice);
optionNum += 2;
}
#ifdef __DEBUG__
g_print("Copying Proxy: %s\n", options->httpProxy);
#endif
if (strcasecmp(options->httpProxy, "") != 0)
{
strcpy(cmdLine[optionNum], "-p");
sprintf(cmdLine[optionNum+1], "%s", options->httpProxy);
optionNum += 2;
}
#ifdef __DEBUG__
g_print("Copying Auth String: %s\n", options->httpAuthString);
#endif
if (strcasecmp(options->httpAuthString, "") != 0)
{
strcpy(cmdLine[optionNum], "-u");
sprintf(cmdLine[optionNum+1], "%s", options->httpAuthString);
optionNum += 2;
}
#ifdef __DEBUG__
g_print("Copying down-sample: %d\n", options->downSample);
#endif
if (options->downSample != 0)
{
sprintf(cmdLine[optionNum], "-%d", options->downSample);
optionNum++;
}
#ifdef __DEBUG__
g_print("Copying lineout: %d\n", options->downSample);
#endif
if (options->lineOut == TRUE)
{
strcpy(cmdLine[optionNum], "-ol");
optionNum++;
}
#ifdef __DEBUG__
g_print("Copying output: %d\n", options->output);
#endif
if (options->output != STEREO)
{
switch (options->output)
{
case LEFT : { strcpy(cmdLine[optionNum], "-0"); break; }
case RIGHT : { strcpy(cmdLine[optionNum], "-1"); break; }
case MONO : { strcpy(cmdLine[optionNum], "-m"); break; }
}
optionNum++;
}
filename = ((songNode *)(currentSong->data))->fileName;
#ifdef __DEBUG__
g_print("Copying filename: %s\n", filename);
#endif
url = (char *)malloc(8);
strncpy(url, filename, 7);
url[7] = '\0';
if ((access(filename, F_OK) < 0) &&
(strcasecmp(url, "http://") != 0))
{
free(url);
return FALSE;
}
free(url);
strcpy(cmdLine[optionNum], filename);
optionNum++;
for (i = optionNum; i < CMDLINESIZE; i++)
free(cmdLine[i]);
cmdLine[optionNum] = NULL;
mpgPID = fork();
if (mpgPID == 0)
{
#ifndef __DEBUG__
close(2); /* Choke off mpg123's output. */
#endif
execvp(options->mp3player, cmdLine);
}
setpgid(mpgPID, mpgPID);
for (i = 0; i < optionNum; i++)
free(cmdLine[i]);
startSecs = currSecs;
doneSong = FALSE;
printSongName();
resetClock();
return TRUE;
}
return TRUE;
}
void pauseSong()
{
if (playing && ! doneSong)
{
if (! paused)
{
#ifdef __DEBUG__
g_print("Sending SIGSTOP\n");
#endif
kill(-mpgPID, SIGSTOP);
paused = TRUE;
}
else
{
#ifdef __DEBUG__
g_print("Sending SIGCONT\n");
#endif
kill(-mpgPID, SIGCONT);
paused = FALSE;
}
}
}
void stop()
{
int status;
if (playing && ! doneSong)
{
if (paused)
pauseSong();
#ifdef __DEBUG__
g_print("Sending SIGINT\n");
#endif
kill(-mpgPID, SIGKILL);
waitpid(-1, &status, WNOHANG | WUNTRACED);
playing = FALSE;
doneSong = TRUE;
paused = FALSE;
}
}
int forward(GtkWidget *widget, configType *options)
{
if (playing && ! doneSong)
{
stop();
nextSong(FALSE);
return play(NULL, options);
}
else
{
nextSong(FALSE);
printSongName();
resetClock();
}
return TRUE;
}
int back(GtkWidget *widget, configType *options)
{
if (playing && ! doneSong)
{
stop(NULL, NULL);
prevSong(FALSE);
return play(NULL, options);
}
else
{
prevSong(FALSE);
printSongName();
resetClock();
}
return TRUE;
}
|