menu: handle path separator for win32
authorkub <derkub@gmail.com>
Tue, 27 May 2025 21:49:35 +0000 (23:49 +0200)
committerkub <derkub@gmail.com>
Wed, 28 May 2025 16:57:00 +0000 (18:57 +0200)
menu.c

diff --git a/menu.c b/menu.c
index 27c72bf..aa58846 100644 (file)
--- a/menu.c
+++ b/menu.c
 #pragma GCC diagnostic ignored "-Wformat-truncation"\r
 #endif\r
 \r
+#ifndef _WIN32\r
+#define PATH_SEP      "/"\r
+#else\r
+#define PATH_SEP      "\\"\r
+#endif\r
+\r
 static char static_buff[64];\r
 static int  menu_error_time = 0;\r
 char menu_error_msg[64] = { 0, };\r
@@ -888,7 +894,10 @@ static void draw_dirlist(char *curdir, struct dirent **namelist,
                if (pos < 0)  continue;\r
                if (pos >= max_cnt) break;\r
                if (namelist[i]->d_type == DT_DIR) {\r
-                       smalltext_out16(x, pos * me_sfont_h, "/", PXMAKE(0xff, 0xff, 0xb0));\r
+#ifdef _WIN32 // don't prefix drive letters with the directory marker, it looks confusing\r
+                       if (*curdir)\r
+#endif\r
+                               smalltext_out16(x, pos * me_sfont_h, PATH_SEP, PXMAKE(0xff, 0xff, 0xb0));\r
                        smalltext_out16(x + me_sfont_w, pos * me_sfont_h, namelist[i]->d_name, PXMAKE(0xff, 0xff, 0xb0));\r
                } else {\r
                        unsigned short color = fname2color(namelist[i]->d_name);\r
@@ -899,9 +908,9 @@ static void draw_dirlist(char *curdir, struct dirent **namelist,
 \r
        if (show_help) {\r
                darken_ptr = (short *)g_menuscreen_ptr\r
-                       + g_menuscreen_pp * (g_menuscreen_h - me_sfont_h * 5 / 2);\r
+                       + g_menuscreen_pp * (g_menuscreen_h - me_sfont_h * 4);\r
                menu_darken_bg(darken_ptr, darken_ptr,\r
-                       g_menuscreen_pp * (me_sfont_h * 5 / 2), 1);\r
+                       g_menuscreen_pp * (me_sfont_h * 4), 1);\r
 \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
@@ -1011,14 +1020,15 @@ static const char *menu_loop_romsel_d(char *curr_path, int len,
 \r
        // is this a dir or a full path?\r
        if (!plat_is_dir(curr_path)) {\r
-               char *p = strrchr(curr_path, '/');\r
+               char *p = strrchr(curr_path, *PATH_SEP);\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
-       show_help = 2;\r
+       show_help = 2 * (len>=0);\r
+       if (len < 0) len = -len;\r
 \r
 rescan:\r
        if (namelist != NULL) {\r
@@ -1057,7 +1067,7 @@ rescan:
                        continue;\r
 \r
                r = strlen(curr_path);\r
-               slash = (r && curr_path[r-1] == '/') ? "" : "/";\r
+               slash = (r && curr_path[r-1] == *PATH_SEP) ? "" : PATH_SEP;\r
                snprintf(rom_fname_reload, sizeof(rom_fname_reload),\r
                        "%s%s%s", curr_path, slash, namelist[i]->d_name);\r
                r = stat(rom_fname_reload, &st);\r
@@ -1077,7 +1087,7 @@ rescan:
                qsort(namelist, n, sizeof(namelist[0]), scandir_cmp);\r
 \r
        // add ".." if it's somehow not there\r
-       if (n == 0 || strcmp(namelist[0]->d_name, "..")) {\r
+       if ((n == 0 || strcmp(namelist[0]->d_name, "..")) && *curr_path) {\r
                struct dirent *dotdot = malloc(sizeof(*dotdot));\r
                *dotdot = (struct dirent) { .d_name="..", .d_type=DT_DIR };\r
                namelist = realloc(namelist, (n+1)*sizeof(*namelist));\r
@@ -1131,7 +1141,7 @@ rescan:
                        if (namelist[sel]->d_type == DT_REG)\r
                        {\r
                                int l = strlen(curr_path);\r
-                               char *slash = l && curr_path[l-1] == '/' ? "" : "/";\r
+                               char *slash = l && curr_path[l-1] == *PATH_SEP ? "" : PATH_SEP;\r
                                snprintf(rom_fname_reload, sizeof(rom_fname_reload),\r
                                        "%s%s%s", curr_path, slash, namelist[sel]->d_name);\r
                                if (inp & PBTN_MOK) { // return sel\r
@@ -1154,18 +1164,23 @@ rescan:
                                if (strcmp(namelist[sel]->d_name, "..") == 0) {\r
                                        char *start = curr_path;\r
                                        p = start + strlen(start) - 1;\r
-                                       while (*p == '/' && p > start) p--;\r
-                                       while (*p != '/' && p > start) p--;\r
-                                       if (p <= start) plat_get_data_dir(newdir, newlen);\r
-                                       else { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
+                                       while (*p == *PATH_SEP && p > start) p--;\r
+                                       while (*p != *PATH_SEP && p > start) p--;\r
+#ifndef _WIN32 // empty path shows drive letter list\r
+                                       if (p <= start) plat_get_data_dir(newdir, newlen); else\r
+#endif\r
+                                           { strncpy(newdir, start, p-start); newdir[p-start] = 0; }\r
                                } else {\r
                                        strcpy(newdir, curr_path);\r
                                        p = newdir + strlen(newdir) - 1;\r
-                                       while (p >= newdir && *p == '/') *p-- = 0;\r
-                                       strcat(newdir, "/");\r
+                                       while (p >= newdir && *p == *PATH_SEP) *p-- = 0;\r
+#ifdef WIN32 // must not prepend the drive letter\r
+                                       if (p > newdir)\r
+#endif\r
+                                               strcat(newdir, PATH_SEP);\r
                                        strcat(newdir, namelist[sel]->d_name);\r
                                }\r
-                               ret = menu_loop_romsel_d(newdir, newlen, filter_exts, extra_filter, draw_prep);\r
+                               ret = menu_loop_romsel_d(newdir, -newlen, filter_exts, extra_filter, draw_prep);\r
                                free(newdir);\r
                                break;\r
                        }\r
@@ -1194,7 +1209,7 @@ rescan:
 \r
        // restore curr_path\r
        if (curr_path_restore != NULL)\r
-               *curr_path_restore = '/';\r
+               *curr_path_restore = *PATH_SEP;\r
 \r
        return ret;\r
 }\r