deal with some gcc7+ warnings
[libpicofe.git] / menu.c
diff --git a/menu.c b/menu.c
index 04c0410..f32c3ce 100644 (file)
--- a/menu.c
+++ b/menu.c
 #include "plat.h"\r
 #include "posix.h"\r
 \r
+#if defined(__GNUC__) && __GNUC__ >= 7\r
+#pragma GCC diagnostic ignored "-Wformat-truncation"\r
+#endif\r
+\r
 static char static_buff[64];\r
 static int  menu_error_time = 0;\r
 char menu_error_msg[64] = { 0, };\r
@@ -33,6 +37,9 @@ void *g_menubg_ptr;
 \r
 int g_menuscreen_w;\r
 int g_menuscreen_h;\r
+int g_menuscreen_pp;\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
@@ -51,11 +58,17 @@ static int g_menu_filter_off;
 static int g_border_style;\r
 static int border_left, border_right, border_top, border_bottom;\r
 \r
+void menuscreen_memset_lines(unsigned short *dst, int c, int l)\r
+{\r
+       for (; l > 0; l--, dst += g_menuscreen_pp)\r
+               memset(dst, c, g_menuscreen_w * 2);\r
+}\r
+\r
 // draws text to current bbp16 screen\r
 static void text_out16_(int x, int y, const char *text, int color)\r
 {\r
        int i, lh, tr, tg, tb, len;\r
-       unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r
+       unsigned short *dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp;\r
        tr = (color & 0xf800) >> 8;\r
        tg = (color & 0x07e0) >> 3;\r
        tb = (color & 0x001f) << 3;\r
@@ -84,7 +97,7 @@ static void text_out16_(int x, int y, const char *text, int color)
                unsigned short *dst = dest;\r
                int u, l;\r
 \r
-               for (l = 0; l < lh; l++, dst += g_menuscreen_w - me_mfont_w)\r
+               for (l = 0; l < lh; l++, dst += g_menuscreen_pp - me_mfont_w)\r
                {\r
                        for (u = me_mfont_w / 2; u > 0; u--, src++)\r
                        {\r
@@ -157,7 +170,7 @@ static void smalltext_out16_(int x, int y, const char *texto, int color)
                        break;\r
 \r
                src = fontdata6x8[c];\r
-               dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w;\r
+               dst = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp;\r
 \r
                while (h--)\r
                {\r
@@ -172,7 +185,7 @@ static void smalltext_out16_(int x, int y, const char *texto, int color)
                                                dst += multiplier;\r
                                }\r
 \r
-                               dst += g_menuscreen_w - me_sfont_w;\r
+                               dst += g_menuscreen_pp - me_sfont_w;\r
                        }\r
                        src++;\r
                }\r
@@ -205,13 +218,13 @@ static void menu_draw_selection(int x, int y, int w)
        if (menu_sel_color < 0) return; // no selection hilight\r
 \r
        if (y > 0) y--;\r
-       dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_w + me_mfont_w * 2 - 2;\r
+       dest = (unsigned short *)g_menuscreen_ptr + x + y * g_menuscreen_pp + me_mfont_w * 2 - 2;\r
        for (h = me_mfont_h + 1; h > 0; h--)\r
        {\r
                dst = dest;\r
                for (i = w - (me_mfont_w * 2 - 2); i > 0; i--)\r
                        *dst++ = menu_sel_color;\r
-               dest += g_menuscreen_w;\r
+               dest += g_menuscreen_pp;\r
        }\r
 }\r
 \r
@@ -237,7 +250,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
@@ -250,19 +263,19 @@ 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
                        unsigned char fd8x8 = fontdata8x8[c*8+l];\r
-                       if (fd8x8&0x80) *fd  = 0xf0;\r
-                       if (fd8x8&0x40) *fd |= 0x0f; fd++;\r
-                       if (fd8x8&0x20) *fd  = 0xf0;\r
-                       if (fd8x8&0x10) *fd |= 0x0f; fd++;\r
-                       if (fd8x8&0x08) *fd  = 0xf0;\r
-                       if (fd8x8&0x04) *fd |= 0x0f; fd++;\r
-                       if (fd8x8&0x02) *fd  = 0xf0;\r
-                       if (fd8x8&0x01) *fd |= 0x0f; fd++;\r
+                       if (fd8x8&0x80) { *fd  = 0xf0; }\r
+                       if (fd8x8&0x40) { *fd |= 0x0f; }; fd++;\r
+                       if (fd8x8&0x20) { *fd  = 0xf0; }\r
+                       if (fd8x8&0x10) { *fd |= 0x0f; }; fd++;\r
+                       if (fd8x8&0x08) { *fd  = 0xf0; }\r
+                       if (fd8x8&0x04) { *fd |= 0x0f; }; fd++;\r
+                       if (fd8x8&0x02) { *fd  = 0xf0; }\r
+                       if (fd8x8&0x01) { *fd |= 0x0f; }; fd++;\r
                }\r
                fd += 8*2/2; // 2 empty lines\r
        }\r
@@ -292,17 +305,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
@@ -378,10 +392,10 @@ static void menu_darken_text_bg(void)
                ymax = g_menuscreen_h - 1;\r
 \r
        for (x = xmin; x <= xmax; x++)\r
-               screen[y * g_menuscreen_w + x] = 0xa514;\r
+               screen[y * g_menuscreen_pp + x] = 0xa514;\r
        for (y++; y < ymax; y++)\r
        {\r
-               ls = y * g_menuscreen_w;\r
+               ls = y * g_menuscreen_pp;\r
                screen[ls + xmin] = 0xffff;\r
                for (x = xmin + 1; x < xmax; x++)\r
                {\r
@@ -391,7 +405,7 @@ static void menu_darken_text_bg(void)
                }\r
                screen[ls + xmax] = 0xffff;\r
        }\r
-       ls = y * g_menuscreen_w;\r
+       ls = y * g_menuscreen_pp;\r
        for (x = xmin; x <= xmax; x++)\r
                screen[ls + x] = 0xffff;\r
 }\r
@@ -408,6 +422,8 @@ static void menu_reset_borders(void)
 \r
 static void menu_draw_begin(int need_bg, int no_borders)\r
 {\r
+       int y;\r
+\r
        plat_video_menu_begin();\r
 \r
        menu_reset_borders();\r
@@ -415,12 +431,14 @@ static void menu_draw_begin(int need_bg, int no_borders)
 \r
        if (need_bg) {\r
                if (g_border_style && no_borders) {\r
-                       menu_darken_bg(g_menuscreen_ptr, g_menubg_ptr,\r
-                               g_menuscreen_w * g_menuscreen_h, 1);\r
+                       for (y = 0; y < g_menuscreen_h; y++)\r
+                               menu_darken_bg((short *)g_menuscreen_ptr + g_menuscreen_pp * y,\r
+                                       (short *)g_menubg_ptr + g_menuscreen_w * y, g_menuscreen_w, 1);\r
                }\r
                else {\r
-                       memcpy(g_menuscreen_ptr, g_menubg_ptr,\r
-                               g_menuscreen_w * g_menuscreen_h * 2);\r
+                       for (y = 0; y < g_menuscreen_h; y++)\r
+                               memcpy((short *)g_menuscreen_ptr + g_menuscreen_pp * y,\r
+                                       (short *)g_menubg_ptr + g_menuscreen_w * y, g_menuscreen_w * 2);\r
                }\r
        }\r
 }\r
@@ -868,8 +886,8 @@ static void draw_dirlist(char *curdir, struct dirent **namelist,
 //     if (!rom_loaded)\r
 //             menu_darken_bg(gp2x_screen, 320*240, 0);\r
 \r
-       darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_w * max_cnt/2 * me_sfont_h;\r
-       menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_w * me_sfont_h * 8 / 10, 0);\r
+       darken_ptr = (short *)g_menuscreen_ptr + g_menuscreen_pp * max_cnt/2 * me_sfont_h;\r
+       menu_darken_bg(darken_ptr, darken_ptr, g_menuscreen_pp * me_sfont_h * 8 / 10, 0);\r
 \r
        x = 5 + me_mfont_w + 1;\r
        if (start - 2 >= 0)\r
@@ -890,16 +908,22 @@ static void draw_dirlist(char *curdir, struct dirent **namelist,
 \r
        if (show_help) {\r
                darken_ptr = (short *)g_menuscreen_ptr\r
-                       + g_menuscreen_w * (g_menuscreen_h - me_sfont_h * 5 / 2);\r
+                       + g_menuscreen_pp * (g_menuscreen_h - me_sfont_h * 5 / 2);\r
                menu_darken_bg(darken_ptr, darken_ptr,\r
-                       g_menuscreen_w * (me_sfont_h * 5 / 2), 1);\r
+                       g_menuscreen_pp * (me_sfont_h * 5 / 2), 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
-               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
@@ -928,11 +952,17 @@ static int scandir_filter(const struct dirent *ent)
        const char *ext;\r
        int i;\r
 \r
-       if (ent == NULL || ent->d_name == NULL)\r
+       if (ent == 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
@@ -951,9 +981,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
@@ -967,14 +999,16 @@ 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
@@ -1021,14 +1055,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
@@ -1049,22 +1109,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
@@ -1104,23 +1163,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
@@ -1344,7 +1396,8 @@ static void draw_key_config(const me_bind_action *opts, int opt_cnt, int player_
        y = (g_menuscreen_h - 4 * me_mfont_h) / 2 - (2 + opt_cnt) * me_mfont_h / 2;\r
        if (x < me_mfont_w * 2)\r
                x = me_mfont_w * 2;\r
-\r
+       if (y < 0)\r
+               y = 0;\r
        menu_draw_begin(1, 0);\r
        if (player_idx >= 0)\r
                text_out16(x, y, "Player %i controls", player_idx + 1);\r
@@ -1470,6 +1523,10 @@ static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_
                        in_unbind_all(bind_dev_id, opts[sel].mask << mask_shift, bindtype);\r
 \r
                in_bind_key(bind_dev_id, kc, opts[sel].mask << mask_shift, bindtype, 0);\r
+\r
+               // make sure bind change is displayed\r
+               if (dev_id != -1)\r
+                       dev_id = bind_dev_id;\r
        }\r
 }\r
 \r