Subversion Repositories svn.mios32

Rev

Rev 2494 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2391 Sauraen 1
/*
2
 * MIDIbox Quad Genesis: File browser
3
 *
4
 * ==========================================================================
5
 *
6
 *  Copyright (C) 2016 Sauraen (sauraen@gmail.com)
7
 *  Licensed for personal non-commercial use only.
8
 *  All other rights reserved.
9
 *
10
 * ==========================================================================
11
 */
12
 
13
#include <mios32.h>
14
#include "filebrowser.h"
15
 
16
#include <file.h>
17
#include <string.h>
18
#include <vgm.h>
19
 
20
#include "frontpanel.h"
21
#include "interface.h"
22
#include "nameeditor.h"
23
 
24
static void (*callback_f)(char* filename);
25
static char* curpath;
26
static char* curname;
27
static char* cursubdir;
28
static char extn[4];
29
static u8 waserror;
2492 Sauraen 30
static u8 save_mode;
2391 Sauraen 31
static u8 cursor;
32
static u8 innameeditor;
2492 Sauraen 33
static u8 submode;
34
static u8 yespos;
2391 Sauraen 35
 
36
static void DrawMenu(){
37
    //-----=====-----=====-----=====-----=====
38
    //In /vgm/SEGA/
2472 Sauraen 39
    //Up   >Dir 12345678< >File 12345678.vgm<
2391 Sauraen 40
    //-----=====-----=====-----=====-----=====
41
    if(waserror || !FILE_VolumeAvailable()){
42
        MIOS32_LCD_Clear();
43
        MIOS32_LCD_CursorSet(0,0);
44
        MIOS32_LCD_PrintString("SD card not mounted, or directory error!");
45
        DBG("ERROR curpath = %s", curpath);
46
        DBG("      curname = %s", curname);
47
        DBG("    cursubdir = %s", cursubdir);
48
        return;
49
    }
50
    MIOS32_LCD_Clear();
51
    MIOS32_LCD_CursorSet(0,0);
2492 Sauraen 52
    switch(submode){
53
        case 0:
54
            MIOS32_LCD_PrintFormattedString("In %s%c", curpath, curpath[1] == 0 ? ' ' : '/');
55
            MIOS32_LCD_CursorSet(0,1);
56
            MIOS32_LCD_PrintFormattedString("Up    Dir %s", cursubdir[0] == 0 ? (save_mode ? "[new]" : "[no dir]") : cursubdir);
57
            MIOS32_LCD_CursorSet(21,1);
58
            MIOS32_LCD_PrintFormattedString("File %s", curname[0] == 0 ? (save_mode ? "[new]" : "[none]") : curname);
59
            MIOS32_LCD_CursorSet(cursor ? 20 : 5, 1);
60
            MIOS32_LCD_PrintChar(0x7E);
61
            MIOS32_LCD_CursorSet(cursor ? 38 : 18, 1);
62
            MIOS32_LCD_PrintChar(0x7F);
63
            break;
64
        case 1:
65
            MIOS32_LCD_PrintString("Overwrite file? (Enter=No)");
66
            MIOS32_LCD_CursorSet(1,1);
67
            MIOS32_LCD_PrintString("No   No   No   No   No   No   No   No");
68
            MIOS32_LCD_CursorSet(5*yespos+1, 1);
69
            MIOS32_LCD_PrintString("Yes");
70
            break;
71
    }
2391 Sauraen 72
}
73
 
74
static inline void GotFile(){
2492 Sauraen 75
    if(save_mode == 1){
76
        submode = 1;
77
        yespos = (u8)(VGM_Player_GetVGMTime() & 0x00000007);
78
        DrawMenu();
79
    }else{
80
        sprintf((char*)(curpath[1] == 0 ? curpath : curpath + strlen(curpath)), "/%s", curname);
81
        subscreen = 0;
82
        callback_f(curpath);
83
    }
2391 Sauraen 84
}
85
static inline void GotDir(){
86
    sprintf((char*)(curpath[1] == 0 ? curpath : curpath + strlen(curpath)), "/%s", cursubdir);
2518 Sauraen 87
    MUTEX_SDCARD_TAKE;
2391 Sauraen 88
    if(!FILE_DirExists(curpath)){
89
        if(FILE_MakeDir(curpath) < 0) waserror = 1;
90
    }
91
    cursubdir[0] = 0;
92
    s32 ret = FILE_FindNextFile(curpath, NULL, extn, curname);
93
    if(ret < 0) waserror = 1;
94
    ret = FILE_FindNextDir(curpath, NULL, cursubdir);
2518 Sauraen 95
    MUTEX_SDCARD_GIVE;
2391 Sauraen 96
    if(ret < 0) waserror = 1;
2472 Sauraen 97
    cursor = (cursubdir[0] == 0); //If there's no subdirectories, point cursor to files
2391 Sauraen 98
    DrawMenu();
99
}
100
 
101
static void NameEditorFinished(){
2494 Sauraen 102
    subscreen = SUBSCREEN_FILEBROWSER; //nameeditor reset this
2391 Sauraen 103
    innameeditor = 0;
104
    if(cursor){
105
        //Was editing file name
106
        //Add extension
107
        sprintf(curname + strlen(curname), ".%s", extn);
108
        //Use file
2492 Sauraen 109
        save_mode = 2;
2391 Sauraen 110
        GotFile();
111
        return;
112
    }else{
113
        //Was editing directory name
114
        //Create and use directory
115
        GotDir();
116
        return;
117
    }
118
}
119
 
120
void Filebrowser_Init(){
121
    curpath = vgmh2_malloc(256);
122
    curname = vgmh2_malloc(13);
123
    cursubdir = vgmh2_malloc(9);
124
    curpath[0] = 0;
125
    curname[0] = 0;
126
    cursubdir[0] = 0;
127
}
128
void Filebrowser_Start(const char* initpath, const char* extension, u8 save, void (*callback)(char* filename)){
129
    subscreen = SUBSCREEN_FILEBROWSER;
130
    callback_f = callback;
2492 Sauraen 131
    save_mode = save;
2391 Sauraen 132
    cursor = 0;
133
    waserror = 0;
134
    innameeditor = 0;
2492 Sauraen 135
    submode = 0;
2391 Sauraen 136
    s32 ret = 0;
137
    MUTEX_SDCARD_TAKE;
138
    if(!FILE_VolumeAvailable()){
139
        ret = -69;
140
        goto Error;
141
    }
2472 Sauraen 142
    /*
143
    extn[0] = 0;
144
    DBG("FILE_FileExists(\"\") == %d", FILE_FileExists(extn));
145
    DBG("FILE_DirExists(\"\") == %d", FILE_DirExists(extn));
146
    */
147
    //
2391 Sauraen 148
    strcpy(extn, extension);
2472 Sauraen 149
    if(initpath == NULL){
150
        //Was the last thing in curpath a directory (dialog was quit from)?
151
        if(FILE_DirExists(curpath) == 1 && curpath[0] != 0){
152
            //Leave everything as is
153
            goto Done;
154
        }
155
        //Was the last thing in curpath a file (we selected a file)?
156
        if(FILE_FileExists(curpath) == 1){
157
            //Chop off the filename and put that into curname
158
            u8 i = strlen(curpath)-1;
159
            while(curpath[i] != '/') --i;
160
            if(i != 0) curpath[i] = 0; //Get rid of slash unless it's root
161
            ++i;
162
            u8 j = 0;
163
            while(curpath[i] != 0){
164
                curname[j] = curpath[i];
165
                curpath[i] = 0;
166
                ++j;
167
                ++i;
168
            }
169
            curname[j] = 0;
170
            cursor = 1;
171
            goto Done;
172
        }
173
        //Otherwise, just start over from root
174
        curpath[0] = '/';
175
        curpath[1] = 0;
176
    }else{
177
        strcpy(curpath, initpath);
178
    }
179
    //Initialize new path, get first file and directory
2391 Sauraen 180
    ret = FILE_FindNextFile(curpath, NULL, extn, curname);
181
    if(ret == FILE_ERR_NO_DIR){
182
        ret = FILE_MakeDir(curpath);
183
        if(ret < 0) goto Error;
184
        ret = FILE_FindNextFile(curpath, NULL, extn, curname);
185
    }
186
    if(ret < 0) goto Error;
187
    ret = FILE_FindNextDir(curpath, NULL, cursubdir);
188
    if(ret < 0) goto Error;
2472 Sauraen 189
    cursor = (cursubdir[0] == 0); //If there's no subdirectories, point cursor to files
2391 Sauraen 190
    goto Done;
191
Error:
192
    waserror = 1;
193
    DBG("Filebrowser_Start error %d", ret);
194
Done:
195
    MUTEX_SDCARD_GIVE;
196
    DrawMenu();
197
}
198
 
199
void Filebrowser_BtnSoftkey(u8 softkey, u8 state){
200
    if(innameeditor){
201
        NameEditor_BtnSoftkey(softkey, state);
202
        return;
203
    }
204
    if(!state) return;
205
    if(waserror || !FILE_VolumeAvailable()){
206
        waserror = 1;
207
        DrawMenu();
208
        return;
209
    }
2492 Sauraen 210
    if(submode == 1){
211
        if(softkey == yespos){
212
            save_mode = 2;
213
            GotFile();
214
        }else{
215
            submode = 0;
216
            DrawMenu();
217
        }
218
        return;
219
    }
2391 Sauraen 220
    if(softkey == 0){
2472 Sauraen 221
        if(curpath[1] == 0) return; //We're at root, don't go up
222
        //Chop off the filename and put that into cursubdir
2391 Sauraen 223
        u8 i = strlen(curpath)-1;
2472 Sauraen 224
        while(curpath[i] != '/') --i;
225
        if(i != 0) curpath[i] = 0; //Get rid of slash unless it's root
226
        ++i;
227
        u8 j = 0;
228
        while(curpath[i] != 0){
229
            cursubdir[j] = curpath[i];
2391 Sauraen 230
            curpath[i] = 0;
2472 Sauraen 231
            ++j;
232
            ++i;
2391 Sauraen 233
        }
2472 Sauraen 234
        cursubdir[j] = 0;
2518 Sauraen 235
        MUTEX_SDCARD_TAKE;
2391 Sauraen 236
        s32 ret = FILE_FindNextFile(curpath, NULL, extn, curname);
2518 Sauraen 237
        MUTEX_SDCARD_GIVE;
2391 Sauraen 238
        if(ret < 0) waserror = 1;
2472 Sauraen 239
        cursor = 0;
2391 Sauraen 240
        DrawMenu();
241
    }else if(softkey <= 3){
242
        cursor = 0;
243
        DrawMenu();
244
    }else{
245
        cursor = 1;
246
        DrawMenu();
247
    }
248
}
249
void Filebrowser_BtnSystem(u8 button, u8 state){
250
    if(innameeditor){
251
        NameEditor_BtnSystem(button, state);
252
        return;
253
    }
254
    if(!state) return;
2492 Sauraen 255
    if(submode == 1){
256
        submode = 0;
257
        DrawMenu();
258
        return;
259
    }
2391 Sauraen 260
    if(button == FP_B_MENU){
261
        subscreen = 0;
262
        callback_f(NULL);
263
        return;
264
    }
265
    if(waserror || !FILE_VolumeAvailable()){
266
        waserror = 1;
267
        DrawMenu();
268
        return;
269
    }
270
    if(button == FP_B_ENTER){
271
        if(cursor){
272
            //Press Enter on file
273
            if(curname[0] == 0){
2492 Sauraen 274
                if(save_mode){
2391 Sauraen 275
                    //Prompt for new file
276
                    innameeditor = 1;
277
                    curname[0] = 'A';
278
                    curname[1] = 0;
279
                    NameEditor_Start(curname, 8, "New file", &NameEditorFinished);
280
                }else{
281
                    return;
282
                }
283
            }else{
284
                //Return file name
285
                GotFile();
286
                return;
287
            }
288
        }else{
289
            //Press Enter on directory
290
            if(cursubdir[0] == 0){
2492 Sauraen 291
                if(save_mode){
2391 Sauraen 292
                    //Prompt for new subdirectory
293
                    innameeditor = 1;
294
                    cursubdir[0] = 'A';
295
                    cursubdir[1] = 0;
296
                    NameEditor_Start(cursubdir, 8, "New dir", &NameEditorFinished);
297
                }else{
298
                    return;
299
                }
300
            }else{
301
                //Enter subdirectory
302
                GotDir();
303
                return;
304
            }
305
        }
306
    }
307
}
308
void Filebrowser_EncDatawheel(s32 incrementer){
309
    if(innameeditor){
310
        NameEditor_EncDatawheel(incrementer);
311
        return;
312
    }
313
    if(waserror || !FILE_VolumeAvailable()){
314
        waserror = 1;
315
        DrawMenu();
316
        return;
317
    }
318
    if(cursor){
319
        if(incrementer > 0){
320
            MUTEX_SDCARD_TAKE;
321
            char* newname = vgmh2_malloc(13);
322
            if(FILE_FindNextFile(curpath, curname[0] == 0 ? NULL : curname, extn, newname) < 0) waserror = 1;
323
            strcpy(curname, newname);
324
            vgmh2_free(newname);
325
            MUTEX_SDCARD_GIVE;
326
            DrawMenu();
327
            return;
328
        }else if(incrementer < 0){
329
            MUTEX_SDCARD_TAKE;
330
            char* newname = vgmh2_malloc(13);
331
            if(FILE_FindPreviousFile(curpath, curname[0] == 0 ? NULL : curname, extn, newname) < 0) waserror = 1;
332
            strcpy(curname, newname);
333
            vgmh2_free(newname);
334
            MUTEX_SDCARD_GIVE;
335
            DrawMenu();
336
            return;
337
        }
338
    }else{
339
        if(incrementer > 0){
340
            MUTEX_SDCARD_TAKE;
341
            char* newdir = vgmh2_malloc(9);
342
            if(FILE_FindNextDir(curpath, cursubdir[0] == 0 ? NULL : cursubdir, newdir) < 0) waserror = 1;
343
            strcpy(cursubdir, newdir);
344
            vgmh2_free(newdir);
345
            MUTEX_SDCARD_GIVE;
346
            DrawMenu();
347
            return;
348
        }else if(incrementer < 0){
349
            MUTEX_SDCARD_TAKE;
350
            char* newdir = vgmh2_malloc(9);
351
            if(FILE_FindPreviousDir(curpath, cursubdir[0] == 0 ? NULL : cursubdir, newdir) < 0) waserror = 1;
352
            strcpy(cursubdir, newdir);
353
            vgmh2_free(newdir);
354
            MUTEX_SDCARD_GIVE;
355
            DrawMenu();
356
            return;
357
        }
358
    }
359
}