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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2002 Daniel Stenberg
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "dir.h"
#include "file.h"
#include "lcd.h"
#include "button.h"
#include "kernel.h"
#include "usb.h"
#include "tree.h"
#include "main_menu.h"
#include "sprintf.h"
#include "mpeg.h"
#include "playlist.h"
#include "menu.h"
#include "wps.h"
#include "settings.h"
#include "debug.h"
#ifdef HAVE_LCD_BITMAP
#include "icons.h"
#endif
#define MAX_FILES_IN_DIR 200
#define TREE_MAX_FILENAMELEN 128
#define MAX_DIR_LEVELS 10
struct entry {
bool file; /* true if file, false if dir */
char name[TREE_MAX_FILENAMELEN];
};
static struct entry dircache[MAX_FILES_IN_DIR];
static struct entry* dircacheptr[MAX_FILES_IN_DIR];
static int filesindir;
static char lastdir[256] = {0};
void browse_root(void)
{
dirbrowse("/");
}
#ifdef HAVE_LCD_BITMAP
#define TREE_MAX_ON_SCREEN ((LCD_HEIGHT-MARGIN_Y)/LINE_HEIGTH)
#define TREE_MAX_LEN_DISPLAY 16 /* max length that fits on screen */
#define MARGIN_Y 0 /* Y pixel margin */
#define MARGIN_X 12 /* X pixel margin */
#define LINE_Y 0 /* Y position the entry-list starts at */
#define LINE_X 2 /* X position the entry-list starts at */
#define LINE_HEIGTH 8 /* pixels for each text line */
#define CURSOR_Y 0 /* the cursor is not positioned in regard to
the margins, so this is the amount of lines
we add to the cursor Y position to position
it on a line */
extern unsigned char bitmap_icons_6x8[LastIcon][6];
#else /* HAVE_LCD_BITMAP */
#define TREE_MAX_ON_SCREEN 2
#define TREE_MAX_LEN_DISPLAY 11 /* max length that fits on screen */
#define LINE_Y 0 /* Y position the entry-list starts at */
#define LINE_X 1 /* X position the entry-list starts at */
#define CURSOR_Y 0 /* not really used for players */
#endif /* HAVE_LCD_BITMAP */
#ifdef HAVE_RECORDER_KEYPAD
#define TREE_NEXT BUTTON_DOWN
#define TREE_PREV BUTTON_UP
#define TREE_EXIT BUTTON_LEFT
#define TREE_ENTER BUTTON_RIGHT
#define TREE_MENU BUTTON_F1
#else
#define TREE_NEXT BUTTON_RIGHT
#define TREE_PREV BUTTON_LEFT
#define TREE_EXIT BUTTON_STOP
#define TREE_ENTER BUTTON_PLAY
#define TREE_MENU BUTTON_MENU
#endif /* HAVE_RECORDER_KEYPAD */
static int compare(const void* e1, const void* e2)
{
return strncmp((*(struct entry**)e1)->name, (*(struct entry**)e2)->name,
TREE_MAX_FILENAMELEN);
}
static int showdir(char *path, int start)
{
#ifdef HAVE_LCD_BITMAP
int icon_type = 0;
#endif
int i;
/* new dir? cache it */
if (strncmp(path,lastdir,sizeof(lastdir))) {
DIR *dir = opendir(path);
if(!dir)
return -1; /* not a directory */
memset(dircacheptr,0,sizeof(dircacheptr));
for ( i=0; i<MAX_FILES_IN_DIR; i++ ) {
int len;
struct dirent *entry = readdir(dir);
if (!entry)
break;
if(entry->d_name[0] == '.') {
/* skip names starting with a dot */
i--;
continue;
}
dircache[i].file = !(entry->attribute & ATTR_DIRECTORY);
/* show only dir/m3u/mp3 ? */
len = strlen(entry->d_name);
if ( global_settings.mp3filter &&
dircache[i].file &&
(len > 4) &&
(strcasecmp(&entry->d_name[len-4], ".m3u") &&
strcasecmp(&entry->d_name[len-4], ".mp3"))) {
i--;
continue;
}
strncpy(dircache[i].name,entry->d_name,TREE_MAX_FILENAMELEN);
dircache[i].name[TREE_MAX_FILENAMELEN-1]=0;
dircacheptr[i] = &dircache[i];
}
filesindir = i;
closedir(dir);
strncpy(lastdir,path,sizeof(lastdir));
lastdir[sizeof(lastdir)-1] = 0;
qsort(dircacheptr,filesindir,sizeof(struct entry*),compare);
}
lcd_stop_scroll();
#ifdef HAVE_NEW_CHARCELL_LCD
lcd_double_height(false);
#endif
lcd_clear_display();
#ifdef HAVE_LCD_BITMAP
lcd_setmargins(0,MARGIN_Y);
lcd_setfont(0);
#endif
for ( i=start; i < start+TREE_MAX_ON_SCREEN; i++ ) {
int len;
if ( i >= filesindir )
break;
len = strlen(dircacheptr[i]->name);
#ifdef HAVE_LCD_BITMAP
if ( dircacheptr[i]->file ) {
if(!strcasecmp(&dircacheptr[i]->name[len-4], ".m3u"))
icon_type = Playlist;
else
icon_type=File;
} else
icon_type=Folder;
lcd_bitmap(bitmap_icons_6x8[icon_type],
6, MARGIN_Y+(i-start)*LINE_HEIGTH, 6, 8, true);
#endif
lcd_puts(LINE_X, LINE_Y+i-start, dircacheptr[i]->name);
}
return filesindir;
}
static int numentries=0;
static int dircursor=0;
static int start=0;
static int dirpos[MAX_DIR_LEVELS];
static int cursorpos[MAX_DIR_LEVELS];
static int dirlevel=0;
static int play_mode = 0;
static char currdir[255];
/* QUICK HACK! this should be handled by the playlist code later */
char* peek_next_track(int steps)
{
static char buf[256];
switch(play_mode) {
case 1:
/* play-full-dir mode */
/* get next track in dir */
if ( steps == 1 ) {
while (dircursor + start + 1 < numentries ) {
if(dircursor+1 < TREE_MAX_ON_SCREEN)
dircursor++;
else
start++;
if ( dircacheptr[dircursor+start]->file &&
dircacheptr[dircursor+start]->name[strlen(dircacheptr[dircursor+start]->name)-1] == '3') {
snprintf(buf,sizeof buf,"%s/%s",
currdir, dircacheptr[dircursor+start]->name );
return buf;
}
}
}
else {
while (dircursor + start > 0) {
if (dircursor > 0)
dircursor--;
else
start--;
if ( dircacheptr[dircursor+start]->file &&
dircacheptr[dircursor+start]->name[strlen(dircacheptr[dircursor+start]->name)-1] == '3') {
snprintf(buf, sizeof(buf), "%s/%s",
currdir, dircacheptr[dircursor+start]->name);
return buf;
}
}
}
break;
case 2:
/* playlist mode */
return playlist_next(steps);
}
return NULL;
}
bool dirbrowse(char *root)
{
char buf[255];
int i;
int button;
memcpy(currdir,root,sizeof(currdir));
numentries = showdir(root, start);
if (numentries == -1)
return -1; /* root is not a directory */
put_cursorxy(0, CURSOR_Y + dircursor, true);
if ( numentries )
lcd_puts_scroll(LINE_X, LINE_Y+dircursor,
dircacheptr[start+dircursor]->name);
lcd_update();
while(1) {
bool restore = false;
button = button_get(true);
switch(button) {
case TREE_EXIT:
if ( play_mode == 1 )
play_mode = 0;
i=strlen(currdir);
if (i>1) {
while (currdir[i-1]!='/')
i--;
strcpy(buf,&currdir[i]);
if (i==1)
currdir[i]=0;
else
currdir[i-1]=0;
dirlevel--;
if ( dirlevel < MAX_DIR_LEVELS ) {
start = dirpos[dirlevel];
dircursor = cursorpos[dirlevel];
}
else
start = dircursor = 0;
restore = true;
}
else {
mpeg_stop();
play_mode = 0;
}
break;
case TREE_ENTER:
#ifdef HAVE_RECORDER_KEYPAD
case BUTTON_PLAY:
#endif
if ( !numentries )
break;
if ((currdir[0]=='/') && (currdir[1]==0)) {
snprintf(buf,sizeof(buf),"%s%s",currdir,
dircacheptr[dircursor+start]->name);
} else {
snprintf(buf,sizeof(buf),"%s/%s",currdir,
dircacheptr[dircursor+start]->name);
}
if (!dircacheptr[dircursor+start]->file) {
if ( play_mode == 1 )
play_mode = 0;
memcpy(currdir,buf,sizeof(currdir));
if ( dirlevel < MAX_DIR_LEVELS ) {
dirpos[dirlevel] = start;
cursorpos[dirlevel] = dircursor;
}
dirlevel++;
dircursor=0;
start=0;
} else {
int len=strlen(dircacheptr[dircursor+start]->name);
lcd_stop_scroll();
if((len > 4) &&
!strcasecmp(&dircacheptr[dircursor+start]->name[len-4],
".m3u"))
{
play_mode = 2;
play_list(currdir, dircacheptr[dircursor+start]->name);
}
else {
play_mode = 1;
mpeg_play(buf);
}
lcd_stop_scroll();
wps_show();
}
restore = true;
break;
case TREE_PREV:
if(dircursor) {
put_cursorxy(0, CURSOR_Y + LINE_Y+dircursor, false);
dircursor--;
put_cursorxy(0, CURSOR_Y + LINE_Y+dircursor, true);
lcd_update();
}
else {
if (start) {
start--;
numentries = showdir(currdir, start);
put_cursorxy(0, CURSOR_Y + LINE_Y+dircursor, true);
}
}
break;
case TREE_NEXT:
if (dircursor + start + 1 < numentries ) {
if(dircursor+1 < TREE_MAX_ON_SCREEN) {
put_cursorxy(0, CURSOR_Y + LINE_Y+dircursor, false);
dircursor++;
put_cursorxy(0, CURSOR_Y + LINE_Y+dircursor, true);
}
else {
start++;
numentries = showdir(currdir, start);
put_cursorxy(0, CURSOR_Y + LINE_Y+dircursor, true);
}
}
break;
case TREE_MENU: {
bool lastfilter = global_settings.mp3filter;
lcd_stop_scroll();
main_menu();
/* do we need to rescan dir? */
if ( lastfilter != global_settings.mp3filter )
lastdir[0] = 0;
restore = true;
break;
}
case BUTTON_ON:
if ( play_mode ) {
lcd_stop_scroll();
wps_show();
restore = true;
}
break;
case SYS_USB_CONNECTED:
/* Tell the USB thread that we are safe */
DEBUGF("dirbrowse got SYS_USB_CONNECTED\n");
usb_acknowledge(SYS_USB_CONNECTED_ACK);
/* Wait until the USB cable is extracted again */
usb_wait_for_disconnect(&button_queue);
break;
}
if ( restore ) {
/* restore display */
numentries = showdir(currdir, start);
put_cursorxy(0, CURSOR_Y + LINE_Y+dircursor, true);
}
lcd_stop_scroll();
if ( numentries )
lcd_puts_scroll(LINE_X, LINE_Y+dircursor,
dircacheptr[start+dircursor]->name);
lcd_update();
}
return false;
}
|