#include <stdio.h>\r
#include <string.h>\r
#include <stdlib.h>\r
+#include <wchar.h>\r
#include <unistd.h> // for getcwd cegcc implementation\r
+#include <dirent.h> // for opendir\r
#include <windows.h>\r
\r
#include "giz.h"\r
};\r
\r
static unsigned char bg_buffer[321*240*2];\r
+unsigned char menu_screen[321*240*2]; /* draw here and blit later, to avoid flicker */\r
char menuErrorMsg[40] = {0, };\r
\r
static void menu_darken_bg(void *dst, const void *src, int pixels, int darker);\r
\r
static void menu_draw_begin(int use_bgbuff)\r
{\r
- if (giz_screen == NULL)\r
- {\r
- Framework2D_WaitVSync();\r
- giz_screen = Framework2D_LockBuffer();\r
- }\r
- else\r
- {\r
- lprintf("%s: screen was not NULL\n", __FUNCTION__);\r
- }\r
- memcpy32(giz_screen, (int *)bg_buffer, 321*240*2/4);\r
+ if (use_bgbuff)\r
+ memcpy32((int *)menu_screen, (int *)bg_buffer, 321*240*2/4);\r
}\r
\r
\r
static void menu_draw_end(void)\r
{\r
+ Framework2D_WaitVSync();\r
+ giz_screen = Framework2D_LockBuffer();\r
+ if (giz_screen == NULL)\r
+ {\r
+ lprintf_al("%s: Framework2D_LockBuffer() returned NULL\n", __FUNCTION__);\r
+ return;\r
+ }\r
+ memcpy32(giz_screen, (int *)menu_screen, 321*240*2/4);\r
Framework2D_UnlockBuffer();\r
giz_screen = NULL;\r
}\r
unsigned short *dst;\r
\r
menu_draw_begin(0);\r
- dst = (unsigned short *)giz_screen + 321*20;\r
+ dst = (unsigned short *)menu_screen + 321*20;\r
\r
if (len > 320) len = 320;\r
for (ln = 10; ln > 0; ln--, dst += 320)\r
while (p > rom_name && *p != '/') p--;\r
\r
if (rom_data == NULL)\r
- memset(giz_screen, 0, 321*240*2);\r
+ memset(menu_screen, 0, 321*240*2);\r
menu_draw_begin(1);\r
\r
smalltext_out16(1, 1, "Loading", 0xffff);\r
\r
// -------------- ROM selector --------------\r
\r
+#define DT_DIR 1\r
+#define DT_REG 2\r
+\r
+struct my_dirent\r
+{\r
+ unsigned char d_type;\r
+ char d_name[0];\r
+};\r
+\r
// rrrr rggg gggb bbbb\r
-#if 0\r
+#if 1\r
static unsigned short file2color(const char *fname)\r
{\r
const char *ext = fname + strlen(fname) - 3;\r
return 0xffff;\r
}\r
\r
-static void draw_dirlist(char *curdir, struct dirent **namelist, int n, int sel)\r
+static void draw_dirlist(char *curdir, struct my_dirent **namelist, int n, int sel)\r
{\r
int start, i, pos;\r
\r
menu_draw_begin(1);\r
\r
if (rom_data == NULL) {\r
- menu_darken_bg(giz_screen, giz_screen, 321*240, 0);\r
+ menu_darken_bg(menu_screen, menu_screen, 321*240, 0);\r
}\r
\r
- menu_darken_bg((char *)giz_screen + 321*120*2, (char *)giz_screen + 321*120*2, 321*8, 0);\r
+ menu_darken_bg((char *)menu_screen + 321*120*2, (char *)menu_screen + 321*120*2, 321*8, 0);\r
\r
if(start - 2 >= 0)\r
smalltext_out16_lim(14, (start - 2)*10, curdir, 0xffff, 53-2);\r
\r
static int scandir_cmp(const void *p1, const void *p2)\r
{\r
- struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2;\r
- if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2);\r
+ struct my_dirent **d1 = (struct my_dirent **)p1, **d2 = (struct my_dirent **)p2;\r
+ if ((*d1)->d_type == (*d2)->d_type) return strcasecmp((*d1)->d_name, (*d2)->d_name);\r
if ((*d1)->d_type == DT_DIR) return -1; // put before\r
if ((*d2)->d_type == DT_DIR) return 1;\r
- return alphasort(d1, d2);\r
+ return strcasecmp((*d1)->d_name, (*d2)->d_name);\r
}\r
\r
static char *filter_exts[] = {\r
- ".mp3", ".MP3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",\r
- ".jpg", ".gpe", ".cue"\r
+ ".mp3", ".srm", ".brm", "s.gz", ".mds", "bcfg", ".txt", ".htm", "html",\r
+ ".jpg", ".cue", ".exe", ".dll"\r
};\r
\r
-static int scandir_filter(const struct dirent *ent)\r
+static int scandir_filter(const struct my_dirent *ent)\r
{\r
const char *p;\r
int i;\r
\r
for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++)\r
{\r
- if (strcmp(p, filter_exts[i]) == 0) return 0;\r
+ if (strcasecmp(p, filter_exts[i]) == 0) return 0;\r
}\r
\r
return 1;\r
}\r
\r
+static int cstr2wstr(wchar_t *dst, const char *src)\r
+{\r
+ int i, len;\r
+ for (i = len = strlen(src); i >= 0; i--)\r
+ dst[i] = src[i];\r
+ return len;\r
+}\r
+\r
+static void wstr2cstr(char *dst, const wchar_t *src)\r
+{\r
+ int i;\r
+ for (i = wcslen(src); i >= 0; i--)\r
+ dst[i] = src[i];\r
+}\r
+\r
+static int my_scandir(const char *dir, struct my_dirent ***namelist_out,\r
+ int(*filter)(const struct my_dirent *),\r
+ int(*compar)(const void *, const void *))\r
+{\r
+ wchar_t *wdir;\r
+ int i, len, ret = -1, name_alloc = 4, name_count = 0;\r
+ struct my_dirent **namelist = NULL, *ent;\r
+ WIN32_FIND_DATA finddata;\r
+ HANDLE fh = INVALID_HANDLE_VALUE;\r
+ BOOL bRet;\r
+\r
+ wdir = malloc(sizeof(wdir[0]) * MAX_PATH);\r
+ if (wdir == NULL) { lprintf_al("%s:%s: OOM\n", __FILE__, __LINE__); goto fail; }\r
+\r
+ namelist = malloc(sizeof(*namelist) * name_alloc);\r
+ if (namelist == NULL) { lprintf_al("%s:%s: OOM\n", __FILE__, __LINE__); goto fail; }\r
+\r
+ // try to read first..\r
+ len = cstr2wstr(wdir, dir);\r
+ for (i = 0; i < len; i++)\r
+ if (wdir[i] == '/') wdir[i] = '\\';\r
+ if (wdir[len-1] != '\\') wdir[len++] = '\\';\r
+ wdir[len++] = '*';\r
+ wdir[len++] = 0;\r
+ /*if (wdir[len-1] == '\\') wdir[len-1] = 0;*/\r
+\r
+ bRet = 1;\r
+ fh = FindFirstFile(wdir, &finddata);\r
+ if (fh == INVALID_HANDLE_VALUE)\r
+ {\r
+ if (GetLastError() != ERROR_NO_MORE_FILES)\r
+ {\r
+ lprintf("FindFirstFile(\"%s\") failed with %lu\n", dir, GetLastError());\r
+ goto fail;\r
+ }\r
+ bRet = 0;\r
+ }\r
+\r
+ // add "." and ".."\r
+ ent = malloc(sizeof(*ent) + 3);\r
+ ent->d_type = DT_DIR; strcpy(ent->d_name, ".");\r
+ namelist[name_count++] = ent;\r
+ ent = malloc(sizeof(*ent) + 3);\r
+ ent->d_type = DT_DIR; strcpy(ent->d_name, "..");\r
+ namelist[name_count++] = ent;\r
+\r
+ while (bRet)\r
+ {\r
+ ent = malloc(sizeof(*ent) + wcslen(finddata.cFileName) + 1);\r
+ ent->d_type = (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;\r
+ wstr2cstr(ent->d_name, finddata.cFileName);\r
+ if (filter == NULL || filter(ent))\r
+ namelist[name_count++] = ent;\r
+ else free(ent);\r
+\r
+ if (name_count >= name_alloc)\r
+ {\r
+ void *tmp;\r
+ name_alloc *= 2;\r
+ tmp = realloc(namelist, sizeof(*namelist) * name_alloc);\r
+ if (tmp == NULL) { lprintf_al("%s:%s: OOM\n", __FILE__, __LINE__); goto fail; }\r
+ namelist = tmp;\r
+ }\r
+\r
+ bRet = FindNextFile(fh, &finddata);\r
+ }\r
+\r
+ // sort\r
+ if (compar != NULL && name_count > 3) qsort(&namelist[2], name_count - 2, sizeof(namelist[0]), compar);\r
+\r
+ // all done.\r
+ ret = name_count;\r
+ *namelist_out = namelist;\r
+ goto end;\r
+\r
+fail:\r
+ if (namelist != NULL)\r
+ {\r
+ while (name_count--)\r
+ free(namelist[name_count]);\r
+ free(namelist);\r
+ }\r
+end:\r
+ if (fh != INVALID_HANDLE_VALUE) FindClose(fh);\r
+ if (wdir != NULL) free(wdir);\r
+ return ret;\r
+}\r
+\r
+\r
static char *romsel_loop(char *curr_path)\r
{\r
- struct dirent **namelist;\r
+ struct my_dirent **namelist;\r
DIR *dir;\r
int n, sel = 0;\r
unsigned long inp = 0;\r
fname = p+1;\r
}\r
\r
- n = scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
+ n = my_scandir(curr_path, &namelist, scandir_filter, scandir_cmp);\r
if (n < 0) {\r
- // try root\r
- n = scandir("/", &namelist, scandir_filter, scandir_cmp);\r
+ // try root..\r
+ n = my_scandir("/", &namelist, scandir_filter, scandir_cmp);\r
if (n < 0) {\r
// oops, we failed\r
- lprintf("dir: "); lprintf(curr_path); lprintf("\n");\r
- perror("scandir");\r
+ lprintf("scandir failed, dir: "); lprintf(curr_path); lprintf("\n");\r
return NULL;\r
}\r
}\r
if(inp & BTN_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }\r
if(inp & BTN_R) { sel+=24; if (sel > n-2) sel = n-2; }\r
if(inp & BTN_PLAY) { // enter dir/select\r
- again:\r
if (namelist[sel+1]->d_type == DT_REG) {\r
strcpy(romFileName, curr_path);\r
strcat(romFileName, "/");\r
ret = romsel_loop(newdir);\r
free(newdir);\r
break;\r
- } else {\r
- // unknown file type, happens on NTFS mounts. Try to guess.\r
- FILE *tstf; int tmp;\r
- strcpy(romFileName, curr_path);\r
- strcat(romFileName, "/");\r
- strcat(romFileName, namelist[sel+1]->d_name);\r
- tstf = fopen(romFileName, "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
if(inp & BTN_STOP) break; // cancel\r
\r
return ret;\r
}\r
-#endif\r
+#else\r
static char *romsel_loop(char *curr_path)\r
{\r
return NULL;\r
}\r
+#endif\r
\r
// ------------ patch/gg menu ------------\r
\r
currentConfig.KeyBinds[i] &= ~action;\r
}\r
\r
-static int count_bound_keys(int action)\r
+static int count_bound_keys(int action, int pl_idx)\r
{\r
int i, keys = 0;\r
\r
for (i = 0; i < 32; i++)\r
+ {\r
+ if (pl_idx >= 0 && (currentConfig.KeyBinds[i]&0x30000) != (pl_idx<<16)) continue;\r
if (currentConfig.KeyBinds[i] & action) keys++;\r
+ }\r
\r
return keys;\r
}\r
menu_draw_begin(1);\r
if (player_idx >= 0) {\r
text_out16(80, 20, "Player %i controls", player_idx + 1);\r
- x = 100;\r
+ x = 80;\r
} else {\r
text_out16(80, 20, "Emulator controls");\r
x = 40;\r
inp &= ~BTN_HOME;\r
for (i = 0; i < 32; i++)\r
if (inp & (1 << i)) {\r
- if (count_bound_keys(opts[sel].mask) >= 2)\r
- currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only\r
+ if (count_bound_keys(opts[sel].mask, player_idx) >= 2)\r
+ currentConfig.KeyBinds[i] &= ~opts[sel].mask; // allow to unbind only\r
else currentConfig.KeyBinds[i] ^= opts[sel].mask;\r
- if (player_idx >= 0) {\r
+ if (player_idx >= 0 && (currentConfig.KeyBinds[i] & opts[sel].mask)) {\r
currentConfig.KeyBinds[i] &= ~(3 << 16);\r
currentConfig.KeyBinds[i] |= player_idx << 16;\r
}\r
\r
// error\r
if (menuErrorMsg[0]) {\r
- memset((char *)giz_screen + 321*224*2, 0, 321*16*2);\r
+ memset((char *)menu_screen + 321*224*2, 0, 321*16*2);\r
text_out16(5, 226, menuErrorMsg);\r
}\r
menu_draw_end();\r
\r
/* make sure action buttons are not pressed on entering menu */\r
draw_menu_root(menu_sel);\r
+\r
while (Framework_PollGetButtons(1) & (BTN_PLAY|BTN_STOP|BTN_HOME)) Sleep(50);\r
\r
for (;;)\r
if (use_game_bg)\r
{\r
// darken the active framebuffer\r
+ // TODO: take from somewhere else, not giz_screen\r
memset(bg_buffer, 0, 321*8*2);\r
menu_darken_bg(bg_buffer + 321*8*2, (char *)giz_screen + 321*8*2, 321*224, 1);\r
memset(bg_buffer + 321*232*2, 0, 321*8*2);\r
}\r
else\r
{\r
+ int i;\r
// should really only happen once, on startup..\r
readpng(bg_buffer, "skin/background.png", READPNG_BG);\r
+ // adjust 320 width to 321\r
+ for (i = 239; i > 0; i--)\r
+ memmove(bg_buffer + 321*2*i, bg_buffer + 320*2*i, 320*2);\r
}\r
}\r
\r