From d809adb540fe7e797e59c0eac687423220667685 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Fri, 22 Jun 2018 14:38:40 +0200 Subject: [PATCH] Process Win32 commandline --- src/main.c | 225 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 151 insertions(+), 74 deletions(-) diff --git a/src/main.c b/src/main.c index fa5ac0d7..8425b929 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,10 @@ #include #ifndef _MSC_VER #include +#else +#if _MSC_VER < 1900 +#define snprintf _snprintf +#endif #endif #if defined(USE_SDL) || defined(USE_SDL2) #include @@ -104,35 +108,45 @@ static int setsize_height; //--- Affichage de la syntaxe, et de la liste des modes vidéos disponibles --- void Display_syntax(void) { - int mode_index; - printf("Syntax: grafx2 [] [] []\n\n"); - printf(" can be:]\n"); - printf("\t-? -h -H -help for this help screen\n"); - printf("\t-wide to emulate a video mode with wide pixels (2x1)\n"); - printf("\t-tall to emulate a video mode with tall pixels (1x2)\n"); - printf("\t-double to emulate a video mode with double pixels (2x2)\n"); - printf("\t-wide2 to emulate a video mode with double wide pixels (4x2)\n"); - printf("\t-tall2 to emulate a video mode with double tall pixels (2x4)\n"); - printf("\t-triple to emulate a video mode with triple pixels (3x3)\n"); - printf("\t-quadruple to emulate a video mode with quadruple pixels (4x4)\n"); - printf("\t-rgb n to reduce RGB precision (2 to 256, 256=max precision)\n"); - printf("\t-gamma n to adjust Gamma correction (1 to 30, 10=no correction)\n"); - printf("\t-skin to use an alternate file with the menu graphics\n"); - printf("\t-mode to set a video mode\n"); - printf("\t-size to set the image size\n"); - printf("Arguments can be prefixed either by / - or --\n"); - printf("They can also be abbreviated.\n\n"); - printf("Available video modes:\n\n"); + int mode_index, i; + char modes[1024*2]; + const char * syntax = + "Syntax: grafx2 [] [] []\n\n" + " can be:\n" + "\t-? -h -H -help for this help screen\n" + "\t-wide to emulate a video mode with wide pixels (2x1)\n" + "\t-tall to emulate a video mode with tall pixels (1x2)\n" + "\t-double to emulate a video mode with double pixels (2x2)\n" + "\t-wide2 to emulate a video mode with double wide pixels (4x2)\n" + "\t-tall2 to emulate a video mode with double tall pixels (2x4)\n" + "\t-triple to emulate a video mode with triple pixels (3x3)\n" + "\t-quadruple to emulate a video mode with quadruple pixels (4x4)\n" + "\t-rgb n to reduce RGB precision (2 to 256, 256=max precision)\n" + "\t-gamma n to adjust Gamma correction (1 to 30, 10=no correction)\n" + "\t-skin to use an alternate file with the menu graphics\n" + "\t-mode to set a video mode\n" + "\t-size to set the image size\n" + "Arguments can be prefixed either by / - or --\n" + "They can also be abbreviated.\n\n"; + fputs(syntax, stdout); + + i = snprintf(modes, sizeof(modes), "Available video modes:\n\n"); for (mode_index = 0; mode_index < Nb_video_modes; mode_index += 12) { int k; for (k = 0; k < 6; k++) { if (mode_index + k >= Nb_video_modes) break; - printf("%12s",Mode_label(mode_index + k)); + i += snprintf(modes + i, sizeof(modes) - i, "%12s", Mode_label(mode_index + k)); } - puts(""); + i += snprintf(modes + i, sizeof(modes) - i, "\n"); } + fputs(modes, stdout); + +#if defined(WIN32) + MessageBoxA(NULL, syntax, "GrafX2", MB_OK); + MessageBoxA(NULL, modes, "GrafX2", MB_OK); +#endif } // ---------------------------- Sortie impromptue ---------------------------- @@ -267,9 +281,9 @@ int Analyze_command_line(int argc, char * argv[], char *main_filename, char *mai file_in_command_line = 0; Resolution_in_command_line = 0; - + Current_resolution = Config.Default_resolution; - + for (index = 1; indexDefault_palette[MC_Dark] =Config.Fav_menu_colors[1]; // Gfx->Default_palette[MC_Light]=Config.Fav_menu_colors[2]; // Gfx->Default_palette[MC_White]=Config.Fav_menu_colors[3]; - + // Even when using the skin's palette, if RGB range is small // the colors will be unusable. Compute_optimal_menu_colors(Gfx->Default_palette); - + // Infos sur les trames (Sieve) Sieve_mode=0; Copy_preset_sieve(0); @@ -852,20 +866,20 @@ int Init_program(int argc,char * argv[]) // Open a console for debugging... //ActivateConsole(); #endif - + Main.image_width=Screen_width/Pixel_width; Main.image_height=Screen_height/Pixel_height; Spare.image_width=Screen_width/Pixel_width; Spare.image_height=Screen_height/Pixel_height; - - starting_image_mode = Config.Default_mode_layers ? + + starting_image_mode = Config.Default_mode_layers ? IMAGE_MODE_LAYERED : IMAGE_MODE_ANIMATION; // Allocation de mémoire pour les différents écrans virtuels (et brosse) if (Init_all_backup_lists(starting_image_mode , Screen_width, Screen_height)==0) Error(ERROR_MEMORY); // Update toolbars' visibility, now that the current image has a mode Check_menu_mode(); - + // Nettoyage de l'écran virtuel (les autres recevront celui-ci par copie) memset(Main_screen,0,Main.image_width*Main.image_height); @@ -930,13 +944,13 @@ int Init_program(int argc,char * argv[]) { strcpy(Main.selector.Directory, main_directory); } - + // Test de recuperation de fichiers sauvés switch (Check_recovery()) { T_IO_Context context; - default: + default: // Some files were loaded from last crash-exit. // Do not load files from command-line, nor show splash screen. Compute_optimal_menu_colors(Main.palette); @@ -955,9 +969,9 @@ int Init_program(int argc,char * argv[]) "Some backups can be present in\n" "the spare page too.\n"); break; - + case -1: // Unable to write lock file - Verbose_message("Warning", + Verbose_message("Warning", "Safety backups (every minute) are\n" "disabled because Grafx2 is running\n" "from a read-only device, or other\n" @@ -965,14 +979,14 @@ int Init_program(int argc,char * argv[]) break; case 0: - + switch (file_in_command_line) { case 0: if (Config.Opening_message) Button_Message_initial(); break; - + case 2: // Load this file Init_context_layered_image(&context, spare_filename, spare_directory); @@ -980,7 +994,7 @@ int Init_program(int argc,char * argv[]) Destroy_context(&context); Redraw_layered_image(); End_of_modification(); - + Button_Page(BUTTON_PAGE); // no break ! proceed with the other file now case 1: @@ -1011,7 +1025,7 @@ int Init_program(int argc,char * argv[]) // If only one image was loaded, assume the spare has same image type if (file_in_command_line==1) Spare.backups->Pages->Image_mode = Main.backups->Pages->Image_mode; - + Hide_cursor(); Compute_optimal_menu_colors(Main.palette); Back_color=Main.backups->Pages->Background_transparent ? @@ -1019,14 +1033,14 @@ int Init_program(int argc,char * argv[]) Best_color_range(0,0,0,Config.Palette_cells_X*Config.Palette_cells_Y); Fore_color=Main.palette[Back_color].R+Main.palette[Back_color].G+Main.palette[Back_color].B < 3*127 ? Best_color_range(255,255,255,Config.Palette_cells_X*Config.Palette_cells_Y) : - Best_color_range(0,0,0,Config.Palette_cells_X*Config.Palette_cells_Y); + Best_color_range(0,0,0,Config.Palette_cells_X*Config.Palette_cells_Y); Check_menu_mode(); Display_all_screen(); Display_menu(); Display_cursor(); Resolution_in_command_line = 0; break; - + default: break; } @@ -1053,7 +1067,7 @@ void Program_shutdown(void) { RECT r; static SDL_SysWMinfo pInfo; - + SDL_GetWMInfo(&pInfo); GetWindowRect(pInfo.window, &r); @@ -1151,12 +1165,12 @@ void Program_shutdown(void) #if defined(USE_SDL) || defined(USE_SDL2) SDL_Quit(); #endif - + #if defined(__GP2X__) || defined(__WIZ__) || defined(__CAANOO__) chdir("/usr/gp2x"); execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL); #endif - + } @@ -1169,28 +1183,91 @@ int main(int argc,char * argv[]) { #if defined(WIN32) && !defined(USE_SDL) && !defined(USE_SDL2) TCHAR ModuleFileName[MAX_PATH]; - TCHAR ModuleShortFileName[MAX_PATH]; - int i; + TCHAR TmpArg[MAX_PATH]; + TCHAR ShortFileName[MAX_PATH]; + int i, j, k; + int inquote = 0; int argc = 0; char arg_buffer[4096]; - char * argv[16] = {NULL}; + char * argv[64] = {NULL}; Init_Win32(hInstance, hPrevInstance); - GetModuleFileName(NULL, ModuleFileName, MAX_PATH); - GetShortPathName(ModuleFileName, ModuleShortFileName, MAX_PATH); - argv[argc++] = arg_buffer; - for (i = 0; i < (int)sizeof(arg_buffer); i++) { - arg_buffer[i] = (char)ModuleShortFileName[i]; - if (arg_buffer[i] == 0) break; + if (GetModuleFileName(NULL, ModuleFileName, MAX_PATH) == 0) + { + MessageBoxA(NULL, "Error initializing program", NULL, MB_OK | MB_ICONERROR); + return 1; } - // TODO : parse command line + GetShortPathName(ModuleFileName, ShortFileName, MAX_PATH); + argv[argc++] = arg_buffer; + for (i = 0; i < (int)sizeof(arg_buffer); ) + { + arg_buffer[i] = (char)ShortFileName[i]; + if (arg_buffer[i++] == 0) break; + } + k = 0; + for (j = 0; pCmdLine[j] != 0 && k < MAX_PATH - 1; j++) + { + if (inquote) + { + if (pCmdLine[j] == '"') + { + inquote = 0; + continue; + } + } + else + { + if (pCmdLine[j] == '"') + { + inquote = 1; + continue; + } + if (pCmdLine[j] == ' ' || pCmdLine[j] == '\t') + { // next argument + TmpArg[k++] = '\0'; + argv[argc++] = arg_buffer + i; + if (GetShortPathName(TmpArg, ShortFileName, MAX_PATH) > 0) + { + for (k = 0; ShortFileName[k] != 0; k++) + arg_buffer[i++] = ShortFileName[k]; + } + else + { + for (k = 0; TmpArg[k] != 0; k++) + arg_buffer[i++] = TmpArg[k]; + } + arg_buffer[i++] = 0; + k = 0; + continue; + } + } + TmpArg[k++] = pCmdLine[j]; + } + TmpArg[k] = '\0'; + if (k > 0) + { + argv[argc++] = arg_buffer + i; + if (GetShortPathName(TmpArg, ShortFileName, MAX_PATH) > 0) + { + for (k = 0; ShortFileName[k] != 0; k++) + arg_buffer[i++] = ShortFileName[k]; + } + else + { + for (k = 0; TmpArg[k] != 0; k++) + arg_buffer[i++] = TmpArg[k]; + } + arg_buffer[i++] = 0; + } + + // TODO : nCmdShow indicates if the window must be maximized, etc. #endif if(!Init_program(argc,argv)) { Program_shutdown(); return 0; } - + Main_handler(); Program_shutdown();