alsa: don't leak memory
[libpicofe.git] / menu.c
diff --git a/menu.c b/menu.c
index eb27ce6..7d2b14e 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -31,10 +31,10 @@ void *g_menuscreen_ptr;
 void *g_menubg_src_ptr;\r
 void *g_menubg_ptr;\r
 \r
-#if !MSCREEN_SIZE_FIXED\r
 int g_menuscreen_w;\r
 int g_menuscreen_h;\r
-#endif\r
+\r
+int g_autostateld_opt;\r
 \r
 static unsigned char *menu_font_data = NULL;\r
 static int menu_text_color = 0xfffe; // default to white\r
@@ -239,7 +239,7 @@ static char tolower_simple(char c)
 \r
 void menu_init_base(void)\r
 {\r
-       int i, c, l;\r
+       int i, c, l, pos;\r
        unsigned char *fd, *fds;\r
        char buff[256];\r
        FILE *f;\r
@@ -252,7 +252,7 @@ void menu_init_base(void)
                return;\r
 \r
        // generate default 8x10 font from fontdata8x8\r
-       for (c = 0, fd = menu_font_data; c < 256; c++)\r
+       for (c = 0, fd = menu_font_data; c < 128; c++)\r
        {\r
                for (l = 0; l < 8; l++)\r
                {\r
@@ -294,17 +294,18 @@ void menu_init_base(void)
        }\r
 \r
        // load custom font and selector (stored as 1st symbol in font table)\r
-       emu_make_path(buff, "skin/font.png", sizeof(buff));\r
+       pos = plat_get_skin_dir(buff, sizeof(buff));\r
+       strcpy(buff + pos, "font.png");\r
        readpng(menu_font_data, buff, READPNG_FONT,\r
                MENU_X2 ? 256 : 128, MENU_X2 ? 320 : 160);\r
        // default selector symbol is '>'\r
        memcpy(menu_font_data, menu_font_data + ((int)'>') * me_mfont_w * me_mfont_h / 2,\r
                me_mfont_w * me_mfont_h / 2);\r
-       emu_make_path(buff, "skin/selector.png", sizeof(buff));\r
+       strcpy(buff + pos, "selector.png");\r
        readpng(menu_font_data, buff, READPNG_SELECTOR, me_mfont_w, me_mfont_h);\r
 \r
        // load custom colors\r
-       emu_make_path(buff, "skin/skin.txt", sizeof(buff));\r
+       strcpy(buff + pos, "skin.txt");\r
        f = fopen(buff, "r");\r
        if (f != NULL)\r
        {\r
@@ -898,10 +899,16 @@ static void draw_dirlist(char *curdir, struct dirent **namelist,
 \r
                snprintf(buff, sizeof(buff), "%s - select, %s - back",\r
                        in_get_key_name(-1, -PBTN_MOK), in_get_key_name(-1, -PBTN_MBACK));\r
-               smalltext_out16(x, g_menuscreen_h - me_sfont_h * 2 - 2, buff, 0xe78c);\r
+               smalltext_out16(x, g_menuscreen_h - me_sfont_h * 3 - 2, buff, 0xe78c);\r
+\r
                snprintf(buff, sizeof(buff), g_menu_filter_off ?\r
                         "%s - hide unknown files" : "%s - show all files",\r
                        in_get_key_name(-1, -PBTN_MA3));\r
+               smalltext_out16(x, g_menuscreen_h - me_sfont_h * 2 - 2, buff, 0xe78c);\r
+\r
+               snprintf(buff, sizeof(buff), g_autostateld_opt ?\r
+                        "%s - autoload save is ON" : "%s - autoload save is OFF",\r
+                       in_get_key_name(-1, -PBTN_MA2));\r
                smalltext_out16(x, g_menuscreen_h - me_sfont_h * 1 - 2, buff, 0xe78c);\r
        }\r
 \r
@@ -933,8 +940,14 @@ static int scandir_filter(const struct dirent *ent)
        if (ent == NULL || ent->d_name == NULL)\r
                return 0;\r
 \r
-       if (ent->d_type == DT_DIR)\r
+       switch (ent->d_type) {\r
+       case DT_DIR:\r
                return 1;\r
+       case DT_LNK:\r
+       case DT_UNKNOWN:\r
+               // could be a dir, deal with it later..\r
+               return 1;\r
+       }\r
 \r
        ext = strrchr(ent->d_name, '.');\r
        if (ext == NULL)\r
@@ -953,9 +966,11 @@ static int dirent_seek_char(struct dirent **namelist, int len, int sel, char c)
        int i;\r
 \r
        sel++;\r
-       for (i = sel + 1; i != sel; i++) {\r
+       for (i = sel + 1; ; i++) {\r
                if (i >= len)\r
                        i = 1;\r
+               if (i == sel)\r
+                       break;\r
 \r
                if (tolower_simple(namelist[i]->d_name[0]) == c)\r
                        break;\r
@@ -969,26 +984,28 @@ static const char *menu_loop_romsel(char *curr_path, int len,
        int (*extra_filter)(struct dirent **namelist, int count,\r
                            const char *basedir))\r
 {\r
-       static char rom_fname_reload[256]; // used for return\r
+       static char rom_fname_reload[256]; // used for scratch and return\r
        char sel_fname[256];\r
        int (*filter)(const struct dirent *);\r
        struct dirent **namelist = NULL;\r
        int n = 0, inp = 0, sel = 0, show_help = 0;\r
        char *curr_path_restore = NULL;\r
        const char *ret = NULL;\r
+       int changed;\r
        char cinp;\r
+       int r, i;\r
 \r
        filter_exts_internal = filter_exts;\r
        sel_fname[0] = 0;\r
 \r
        // is this a dir or a full path?\r
        if (!plat_is_dir(curr_path)) {\r
-               char *p = curr_path + strlen(curr_path) - 1;\r
-               for (; p > curr_path && *p != '/'; p--)\r
-                       ;\r
-               *p = 0;\r
-               curr_path_restore = p;\r
-               snprintf(sel_fname, sizeof(sel_fname), "%s", p + 1);\r
+               char *p = strrchr(curr_path, '/');\r
+               if (p != NULL) {\r
+                       *p = 0;\r
+                       curr_path_restore = p;\r
+                       snprintf(sel_fname, sizeof(sel_fname), "%s", p + 1);\r
+               }\r
 \r
                if (rom_fname_reload[0] == 0)\r
                        show_help = 2;\r
@@ -1023,14 +1040,40 @@ rescan:
                }\r
        }\r
 \r
+       // try to resolve DT_UNKNOWN and symlinks\r
+       changed = 0;\r
+       for (i = 0; i < n; i++) {\r
+               struct stat st;\r
+\r
+               if (namelist[i]->d_type == DT_REG || namelist[i]->d_type == DT_DIR)\r
+                       continue;\r
+\r
+               snprintf(rom_fname_reload, sizeof(rom_fname_reload),\r
+                       "%s/%s", curr_path, namelist[i]->d_name);\r
+               r = stat(rom_fname_reload, &st);\r
+               if (r == 0)\r
+               {\r
+                       if (S_ISREG(st.st_mode)) {\r
+                               namelist[i]->d_type = DT_REG;\r
+                               changed = 1;\r
+                       }\r
+                       else if (S_ISDIR(st.st_mode)) {\r
+                               namelist[i]->d_type = DT_DIR;\r
+                               changed = 1;\r
+                       }\r
+               }\r
+       }\r
+\r
        if (!g_menu_filter_off && extra_filter != NULL)\r
                n = extra_filter(namelist, n, curr_path);\r
 \r
+       if (n > 1 && changed)\r
+               qsort(namelist, n, sizeof(namelist[0]), scandir_cmp);\r
+\r
        // try to find selected file\r
        // note: we don't show '.' so sel is namelist index - 1\r
        sel = 0;\r
        if (sel_fname[0] != 0) {\r
-               int i;\r
                for (i = 1; i < n; i++) {\r
                        char *dname = namelist[i]->d_name;\r
                        if (dname[0] == sel_fname[0] && strcmp(dname, sel_fname) == 0) {\r
@@ -1051,22 +1094,21 @@ rescan:
                inp = in_menu_wait(PBTN_UP|PBTN_DOWN|PBTN_LEFT|PBTN_RIGHT\r
                        | PBTN_L|PBTN_R|PBTN_MA2|PBTN_MA3|PBTN_MOK|PBTN_MBACK\r
                        | PBTN_MENU|PBTN_CHAR, &cinp, 33);\r
-               if (inp & PBTN_UP  )  { sel--;   if (sel < 0)   sel = n-2; }\r
-               if (inp & PBTN_DOWN)  { sel++;   if (sel > n-2) sel = 0; }\r
-               if (inp & PBTN_LEFT)  { sel-=10; if (sel < 0)   sel = 0; }\r
-               if (inp & PBTN_L)     { sel-=24; if (sel < 0)   sel = 0; }\r
-               if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
-               if (inp & PBTN_R)     { sel+=24; if (sel > n-2) sel = n-2; }\r
-               if (inp & PBTN_CHAR)  sel = dirent_seek_char(namelist, n, sel, cinp);\r
                if (inp & PBTN_MA3)   {\r
                        g_menu_filter_off = !g_menu_filter_off;\r
                        snprintf(sel_fname, sizeof(sel_fname), "%s",\r
                                namelist[sel+1]->d_name);\r
                        goto rescan;\r
                }\r
+               if (inp & PBTN_UP  )  { sel--;   if (sel < 0)   sel = n-2; }\r
+               if (inp & PBTN_DOWN)  { sel++;   if (sel > n-2) sel = 0; }\r
+               if (inp & PBTN_LEFT)  { sel-=10; if (sel < 0)   sel = 0; }\r
+               if (inp & PBTN_L)     { sel-=24; if (sel < 0)   sel = 0; }\r
+               if (inp & PBTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
+               if (inp & PBTN_R)     { sel+=24; if (sel > n-2) sel = n-2; }\r
+\r
                if ((inp & PBTN_MOK) || (inp & (PBTN_MENU|PBTN_MA2)) == (PBTN_MENU|PBTN_MA2))\r
                {\r
-                       again:\r
                        if (namelist[sel+1]->d_type == DT_REG)\r
                        {\r
                                snprintf(rom_fname_reload, sizeof(rom_fname_reload),\r
@@ -1106,23 +1148,16 @@ rescan:
                                free(newdir);\r
                                break;\r
                        }\r
-                       else\r
-                       {\r
-                               // unknown file type, happens on NTFS mounts. Try to guess.\r
-                               FILE *tstf; int tmp;\r
-                               snprintf(rom_fname_reload, sizeof(rom_fname_reload),\r
-                                       "%s/%s", curr_path, namelist[sel+1]->d_name);\r
-                               tstf = fopen(rom_fname_reload, "rb");\r
-                               if (tstf != NULL)\r
-                               {\r
-                                       if (fread(&tmp, 1, 1, tstf) > 0 || ferror(tstf) == 0)\r
-                                               namelist[sel+1]->d_type = DT_REG;\r
-                                       else    namelist[sel+1]->d_type = DT_DIR;\r
-                                       fclose(tstf);\r
-                                       goto again;\r
-                               }\r
-                       }\r
                }\r
+               else if (inp & PBTN_MA2) {\r
+                       g_autostateld_opt = !g_autostateld_opt;\r
+                       show_help = 3;\r
+               }\r
+               else if (inp & PBTN_CHAR) {\r
+                       // must be last\r
+                       sel = dirent_seek_char(namelist, n, sel, cinp);\r
+               }\r
+\r
                if (inp & PBTN_MBACK)\r
                        break;\r
 \r