-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlist.c
467 lines (404 loc) · 8.52 KB
/
playlist.c
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Copyright (C) 2005 Preshant V
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <itunesdb.h>
#ifdef __linux__
#include <linux/limits.h>
#else
#include <limits.h>
#endif
#include "ipod.h"
#include "itunes_db.h"
struct tracknode {
struct track *cur;
struct track origcur;
struct tracknode *prevnode;
struct tracknode *nextnode;
};
void play_next_track();
void play_prev_track();
static struct tracknode *get_tracknode(int nodenum);
static void shuffle_songs(int preserve);
static void set_shuffle_mode(int newmode);
static void set_repeat_mode(int newmode);
static void swap_tracknodes(struct tracknode *one, struct tracknode *two);
static int get_rand_numb(int themax);
static int shuffle = 0, repeat = 0;
static struct tracknode *songhead = NULL;
static struct tracknode *cursong = NULL;
/*
* global so other parts know the position, mainly [aac|mp3].c
*/
int playlistpos = 0, playlistlength = 0;
extern void new_mp3_window(char *filename, char *album, char *artist, char *title, int len);
extern void new_aac_window(char *filename, char *album, char *artist, char *title, int len);
extern int is_mp3_type(char *extension);
extern int is_aac_type(char *extension);
extern void new_message_window(char *message);
static void
play_track(struct tracknode *tracknode)
{
struct itdb_track *track = NULL;
char mpath[PATH_MAX];
char *fileext;
if (tracknode == NULL) {
printf("play_track got NULL!\n");
return;
}
track = db_read_details(tracknode->cur);
if (!track) {
printf("error with track!");
return;
}
snprintf(mpath, PATH_MAX-1, "/%s", track->path);
/* only decode mp3 files */
fileext = strrchr(mpath, '.');
if (fileext)
{
printf("playing %d of %d (%s)\n", playlistpos, playlistlength, mpath);
if (is_mp3_type(fileext)) {
#ifdef IPOD
new_mp3_window(mpath, track->album, track->artist,
track->title, track->length);
#endif
} else if (is_aac_type(fileext)) {
#ifdef USE_HELIXAACDEC
new_aac_window(mpath, track->album, track->artist,
track->title, track->length);
#endif
}
}
else
{
new_message_window("Invalid format");
}
}
void
start_play_queue()
{
shuffle = ipod_get_setting(SHUFFLE);
repeat = ipod_get_setting(REPEAT);
if (shuffle < 0)
shuffle = 0;
if (shuffle > 1)
shuffle = 1;
if (repeat < 0)
repeat = 0;
if (repeat > 2)
repeat = 2;
/*
* the playlist has been copied, the length autoset by the
* add_track_to_queue and the playlistpos has been set to
* current point
*/
cursong = get_tracknode(playlistpos);
if (shuffle == 0)
{
// no moving necessary
}
else if (shuffle == 1)
{
swap_tracknodes(songhead, cursong);
cursong = get_tracknode(1);
playlistpos = 1;
shuffle_songs(1);
}
play_track(cursong);
/*
* shuffle albums not yet supported
*/
}
void
checkshufrpt()
{
if (ipod_get_setting(SHUFFLE) != shuffle) {
set_shuffle_mode(ipod_get_setting(SHUFFLE));
}
if (ipod_get_setting(REPEAT) != repeat) {
set_repeat_mode(ipod_get_setting(REPEAT));
}
}
static void
swap_tracknodes(struct tracknode *one, struct tracknode *two)
{
/* swaps two tracknodes */
struct track *temp = two->cur;
two->cur = one->cur;
one->cur = temp;
}
static int
get_rand_numb(int max)
{
static int seedinit = 0;
if (!seedinit)
{
int fd, data;
fd = open("/dev/random", O_RDONLY);
if (fd == -1) {
data = (int)time(NULL);
}
else {
read(fd, &data, sizeof(data));
close(fd);
}
srand(data);
seedinit = 1;
}
return 1 + (int)((float)max * rand() / (RAND_MAX + 1.0));
}
static struct tracknode *
get_tracknode(int nodenum)
{
struct tracknode *tempnode;
int counter = 1;
tempnode = songhead;
while (counter < nodenum)
{
if (tempnode == NULL) {
printf("get_tracknode error, couldn't get %d!\n", nodenum);
return songhead;
}
tempnode = tempnode->nextnode;
counter++;
}
return tempnode;
}
static void
shuffle_songs(int preserve)
{
struct tracknode *cur;
if (cursong == NULL) {
return;
}
/* does a suffle of songs only, only the head stays at pos */
if (preserve == 1) {
cur = cursong;
} else {
cur = songhead;
}
if (preserve == 1) {
cur = cur->nextnode;
if (cur == NULL) {
return;
}
}
while (cur!=NULL)
{
int swapnode = 1;
while (swapnode == 1)
{
swapnode = get_rand_numb(playlistlength);
}
swap_tracknodes(cur, get_tracknode(swapnode));
cur = cur->nextnode;
}
}
static void
shuffle_albums()
{
/*
* this changes position in playlist as well
*/
}
static void
shuffle_off()
{
char found = 0;
struct track *curtrack;
struct tracknode *cur;
curtrack = cursong->cur;
cur = songhead;
playlistpos = 1;
while (cur != NULL)
{
cur->cur = &(cur->origcur);
if (&(cur->origcur) == curtrack)
{
/*
* find the new position in this playlist
*/
cursong = cur;
found = 1;
}
if (found == 0) {
playlistpos++;
}
cur = cur->nextnode;
}
}
void
add_track_to_queue(struct track *song)
{
struct tracknode *newsong;
struct tracknode *nextnode;
struct track *asong;
asong = (struct track *)malloc(sizeof(struct track));
*asong = *song;
newsong = (struct tracknode *)malloc(sizeof(struct tracknode));
nextnode = songhead;
while (nextnode != NULL && nextnode->nextnode != NULL)
{
nextnode = nextnode->nextnode;
}
if (nextnode != NULL && nextnode->nextnode == NULL)
{
nextnode->nextnode = newsong;
newsong->prevnode = nextnode;
}
else
{
songhead = newsong;
newsong->prevnode = NULL;
}
newsong->cur = asong;
memcpy(&(newsong->origcur),asong,sizeof(struct track));
newsong->nextnode = NULL;
playlistlength++;
}
/*
* delete functionality a little buggy void deletenode(struct tracknode *
* todel) { struct tracknode * prev; struct tracknode * next;
* prev=todel->prevnode; next=todel->nextnode;
* if(prev!=NULL){prev->nextnode=next;}else{songhead=next;}
* if(next!=NULL){next->prevnode=prev;} cout<<"deleting
* :"<<todel->cur->title<<endl; delete todel; }
*
*
* void deletesong(struct itdb_track * todel) { struct tracknode *
* cur=songhead; while(cur!=NULL) { if(cur->cur = todel) {
* deletenode(cur); playlistlength--; return; } cur=cur->nextnode; } } so
* has been disabled.
*/
void
clear_play_queue()
{
struct tracknode *curnode;
struct tracknode *nextnode;
curnode = songhead;
while (curnode != NULL)
{
nextnode = curnode->nextnode;
free(curnode);
curnode = nextnode;
}
songhead = NULL;
playlistlength = 0;
playlistpos = 0;
}
void
play_next_track()
{
checkshufrpt();
if (cursong == NULL) {
return;
}
if (repeat == 1) {
play_track(cursong);
return;
}
if (repeat == 0 || cursong->nextnode != NULL) {
playlistpos++;
cursong = cursong->nextnode;
play_track(cursong);
return;
}
else if (cursong->nextnode == NULL && repeat == 2) {
shuffle_songs(0);
cursong = songhead;
playlistpos = 1;
play_track(cursong);
}
}
void
play_prev_track()
{
struct tracknode *lasttrack;
checkshufrpt();
if (cursong == NULL) {
return;
}
if (repeat == 1) {
play_track(cursong);
return;
}
if (cursong->prevnode != NULL || repeat == 0) {
cursong = cursong->prevnode;
playlistpos--;
play_track(cursong);
return;
}
if (cursong->prevnode == NULL && repeat == 2) {
lasttrack = songhead;
playlistpos = 1;
while (lasttrack->nextnode != NULL) {
lasttrack = lasttrack->nextnode;
playlistpos++;
}
shuffle_songs(0);
cursong = lasttrack;
play_track(cursong);
}
}
static void
set_shuffle_mode(int newmode)
{
if (newmode == -1) {
shuffle++;
}
else {
shuffle = newmode;
}
if (shuffle > 1) {
shuffle = 0;
}
if (shuffle == 1)
{
/*
* mode 1- shuffle songs
*/
shuffle_songs(1);
}
else if (shuffle == 2)
{
shuffle_albums();
}
else if (shuffle == 0)
{
shuffle_off();
}
}
static void
set_repeat_mode(int newmode)
{
if (newmode == -1) {
repeat++;
}
else {
repeat = newmode;
}
repeat++;
if (repeat > 2) {
repeat = 0;
}
}