summaryrefslogtreecommitdiff
path: root/apps/tree.c
blob: 02e5f0a4679b24a1efbceab243312a768f5de55a (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   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 "tree.h"
#include "play.h"
#include "main_menu.h"

#ifdef HAVE_LCD_BITMAP
#include "icons.h"
#endif

#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];
  int namelen;
};

void browse_root(void)
{
  dirbrowse("/");
}


#ifdef HAVE_LCD_BITMAP

#define TREE_MAX_ON_SCREEN   7
#define TREE_MAX_LEN_DISPLAY 16 /* max length that fits on screen */
 
#define MARGIN_Y      8  /* 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 */

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 */

#endif /* HAVE_LCD_BITMAP */

static int showdir(char *path, struct entry *buffer, int start, 
                   int scrollpos, int* at_end)
{
#ifdef HAVE_LCD_BITMAP
    int icon_type = 0;
#endif
    int i;
    int j=0;
    DIR *dir = opendir(path);
    struct dirent *entry;

    if(!dir)
        return -1; /* not a directory */

    i=start;
    *at_end=0; /* Have we displayed the last directory entry? */
    while((entry = readdir(dir))) {
        int len;

        if(entry->d_name[0] == '.')
            /* skip names starting with a dot */
            continue;

        if(j++ < scrollpos) 
            continue ;

        len = strlen(entry->d_name);
        if(len < TREE_MAX_FILENAMELEN)
            /* strncpy() is evil, we memcpy() instead, +1 includes the
               trailing zero */
            memcpy(buffer[i].name, entry->d_name, len+1);
        else
            memcpy(buffer[i].name, "too long", 9);

        buffer[i].file = !(entry->attribute&ATTR_DIRECTORY);

#ifdef HAVE_LCD_BITMAP
        if ( buffer[i].file )
            icon_type=File;
        else
            icon_type=Folder;
        lcd_bitmap(bitmap_icons_6x8[icon_type], 6, MARGIN_Y+i*LINE_HEIGTH, 6, 
                   8, true);
#endif

        if(len < TREE_MAX_LEN_DISPLAY)
            lcd_puts(LINE_X, LINE_Y+i, buffer[i].name);
        else {
            char storage = buffer[i].name[TREE_MAX_LEN_DISPLAY];
            buffer[i].name[TREE_MAX_LEN_DISPLAY]=0;
            lcd_puts(LINE_X, LINE_Y+i, buffer[i].name);
            buffer[i].name[TREE_MAX_LEN_DISPLAY]=storage;
        }

        if(++i >= TREE_MAX_ON_SCREEN)
            break;
    }

    if (entry==0) {
        *at_end=1;
    } else {
        *at_end=(readdir(dir)==0); 
    }
    j = i ;
    while (j++ < TREE_MAX_ON_SCREEN) {
        lcd_puts(LINE_X, LINE_Y+j,"                 ");
    }
    closedir(dir);

    return i;
}

bool dirbrowse(char *root)
{
    struct entry buffer[TREE_MAX_ON_SCREEN];
    int numentries;
    char buf[255];
    char currdir[255];
    int dircursor=0;
    int i;
    int start=0;
    int at_end=0;
    int dirpos[MAX_DIR_LEVELS];
    int dirlevel=0;

    lcd_clear_display();

#ifdef HAVE_LCD_BITMAP
    lcd_putsxy(0,0, "[Browse]",0);
    lcd_setmargins(0,MARGIN_Y);
    lcd_setfont(0);
#endif
    memcpy(currdir,root,sizeof(currdir));

    numentries = showdir(root, buffer, 0, start, &at_end);

    if (numentries == -1) 
        return -1;  /* root is not a directory */

    lcd_puts(0, dircursor, "-");
#ifdef HAVE_LCD_BITMAP
    lcd_update();
#endif

    while(1) {
        int key = button_get();

        if(!key) {
            sleep(1);
            continue;
        }
        switch(key) {
#if defined(SIMULATOR) && defined(HAVE_RECODER_KEYPAD)
            case BUTTON_OFF:
                return false;
#endif

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_LEFT:
#else
            case BUTTON_STOP:
#endif
                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;

                    lcd_clear_display();
#ifdef HAVE_LCD_BITMAP
                    lcd_putsxy(0,0, "[Browse]",0);
#endif
                    dirlevel--;
                    if ( dirlevel < MAX_DIR_LEVELS )
                        start = dirpos[dirlevel];
                    else
                        start = 0;
                    numentries = showdir(currdir, buffer, 0, start, &at_end);
                    dircursor=0;
                    while ( (dircursor < TREE_MAX_ON_SCREEN) && 
                            (strcmp(buffer[dircursor].name,buf)!=0)) 
                        dircursor++;
                    if (dircursor==TREE_MAX_ON_SCREEN)
                        dircursor=0;
                    lcd_puts(0, LINE_Y+dircursor, "-");
                    lcd_update();
                }

                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_RIGHT:
#endif
            case BUTTON_PLAY:
                if ((currdir[0]=='/') && (currdir[1]==0)) {
                    snprintf(buf,sizeof(buf),"%s%s",currdir,buffer[dircursor].name);
                } else {
                    snprintf(buf,sizeof(buf),"%s/%s",currdir,buffer[dircursor].name);
                }

                if (!buffer[dircursor].file) {
                    memcpy(currdir,buf,sizeof(currdir));
                    if ( dirlevel < MAX_DIR_LEVELS )
                        dirpos[dirlevel] = start+dircursor;
                    dirlevel++;
                    dircursor=0;
                    start=0;
                } else {
                    playtune(currdir, buffer[dircursor].name);
#ifdef HAVE_LCD_BITMAP
                    lcd_setmargins(0, MARGIN_Y);
                    lcd_setfont(0);
#endif
                }

                lcd_clear_display();
                numentries = showdir(currdir, buffer, 0, start, &at_end);  
                lcd_puts(0, LINE_Y+dircursor, "-");
#ifdef HAVE_LCD_BITMAP
                lcd_putsxy(0,0, "[Browse]",0);
                lcd_update();
#endif
                break;
                
#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_UP:
#else
            case BUTTON_LEFT:
#endif
                if(dircursor) {
                    lcd_puts(0, LINE_Y+dircursor, " ");
                    dircursor--;
                    lcd_puts(0, LINE_Y+dircursor, "-");
                    lcd_update();
                }
                else {
                    if (start) {   
                        lcd_clear_display();
                        start--;
                        numentries = showdir(currdir, buffer, 0, 
                                             start, &at_end);
                        lcd_puts(0, LINE_Y+dircursor, "-");
#ifdef HAVE_LCD_BITMAP
                        lcd_putsxy(0,0, "[Browse]",0);
                        lcd_update();
#endif
                    }
                }
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_DOWN:
#else
            case BUTTON_RIGHT:
#endif
                if(dircursor+1 < numentries) {
                    lcd_puts(0, LINE_Y+dircursor, " ");
                    dircursor++;
                    lcd_puts(0, LINE_Y+dircursor, "-");
#ifdef HAVE_LCD_BITMAP
                    lcd_update();
#endif
                } else
                {
                    if (!at_end) {
                        lcd_clear_display();
                        start++;
                        numentries = showdir(currdir, buffer, 0, 
                                             start, &at_end);
                        lcd_puts(0, LINE_Y+dircursor, "-");
#ifdef HAVE_LCD_BITMAP
                        lcd_putsxy(0,0, "[Browse]",0);
                        lcd_update();
#endif
                    }
                }
                break;

#ifdef HAVE_RECORDER_KEYPAD
            case BUTTON_F1:
            case BUTTON_F2:
            case BUTTON_F3:
#else
            case BUTTON_MENU:
#endif
                main_menu();

                /* restore display */
                /* TODO: this is just a copy from BUTTON_STOP, fix it */
                lcd_clear_display();
#ifdef HAVE_LCD_BITMAP
                lcd_putsxy(0,0, "[Browse]",0);
                lcd_setmargins(0,MARGIN_Y);
                lcd_setfont(0);
#endif
                numentries = showdir(currdir, buffer, 0, start, &at_end);
                dircursor=0;
                while ( (dircursor < TREE_MAX_ON_SCREEN) && 
                        (strcmp(buffer[dircursor].name,buf)!=0)) 
                    dircursor++;
                if (dircursor==TREE_MAX_ON_SCREEN)
                    dircursor=0;
                lcd_puts(0, LINE_Y+dircursor, "-");
                lcd_update();

                break;
        }
    }

    return false;
}