revive PC build, support Linux
[gpsp.git] / gui.c
diff --git a/gui.c b/gui.c
index 21e1023..a08bb63 100644 (file)
--- a/gui.c
+++ b/gui.c
 
 #endif
 
 
 #endif
 
-#ifndef GP2X_BUILD
-#include "gp2x/cpuctrl.h"
-#endif
-
 #include "common.h"
 #include "font.h"
 
 #include "common.h"
 #include "font.h"
 
@@ -38,7 +34,7 @@
 
 #ifdef GP2X_BUILD
 
 
 #ifdef GP2X_BUILD
 
-#define FILE_LIST_ROWS ((int)((SDL_SCREEN_HEIGHT - 40) / FONT_HEIGHT))
+#define FILE_LIST_ROWS 20
 #define FILE_LIST_POSITION 5
 #define DIR_LIST_POSITION 260
 
 #define FILE_LIST_POSITION 5
 #define DIR_LIST_POSITION 260
 
 
 #ifdef PSP_BUILD
 
 
 #ifdef PSP_BUILD
 
+#define COLOR_BG            color16(2, 8, 10)
+
 #define color16(red, green, blue)                                             \
   (blue << 11) | (green << 5) | red                                           \
 
 #else
 
 #define color16(red, green, blue)                                             \
   (blue << 11) | (green << 5) | red                                           \
 
 #else
 
-#define color16(red, green, blue)                                             \
-  (red << 11) | (green << 5) | blue                                           \
-
-#endif
-
-#ifdef GP2X_BUILD
-
 #define COLOR_BG            color16(0, 0, 0)
 
 #define COLOR_BG            color16(0, 0, 0)
 
-#else
-
-#define COLOR_BG            color16(2, 8, 10)
+#define color16(red, green, blue)                                             \
+  (red << 11) | (green << 5) | blue                                           \
 
 #endif
 
 
 #endif
 
 #define COLOR_FRAMESKIP_BAR color16(15, 31, 31)
 #define COLOR_HELP_TEXT     color16(16, 40, 24)
 
 #define COLOR_FRAMESKIP_BAR color16(15, 31, 31)
 #define COLOR_HELP_TEXT     color16(16, 40, 24)
 
+#ifdef PSP_BUILD
+  u8 *clock_speed_options[] =
+  {
+    "33MHz", "66MHz", "100MHz", "133MHz", "166MHz", "200MHz", "233MHz",
+    "266MHz", "300MHz", "333MHz"
+  };
+  #define menu_get_clock_speed() \
+    clock_speed = (clock_speed_number + 1) * 33
+  #define get_clock_speed_number() \
+    clock_speed_number = (clock_speed / 33) - 1
+#elif defined(WIZ_BUILD)
+  u8 *clock_speed_options[] =
+  {
+    "300MHz", "333MHz", "366MHz", "400MHz", "433MHz",
+    "466MHz", "500MHz", "533MHz", "566MHz", "600MHz",
+    "633MHz", "666MHz", "700MHz", "733MHz", "766MHz",
+    "800MHz", "833MHz", "866MHz", "900MHz"
+  };
+  #define menu_get_clock_speed() \
+    clock_speed = 300 + (clock_speed_number * 3333) / 100
+  #define get_clock_speed_number() \
+    clock_speed_number = (clock_speed - 300) / 33
+#elif defined(GP2X_BUILD)
+  u8 *clock_speed_options[] =
+  {
+    "150MHz", "160MHz", "170MHz", "180MHz", "190MHz",
+    "200MHz", "210MHz", "220MHz", "230MHz", "240MHz",
+    "250MHz", "260MHz", "270MHz", "280MHz", "290MHz"
+  };
+  #define menu_get_clock_speed() \
+    clock_speed = 150 + clock_speed_number * 10
+  #define get_clock_speed_number() \
+    clock_speed_number = (clock_speed - 150) / 10
+#else
+  u8 *clock_speed_options[] =
+  {
+    "0"
+  };
+  #define menu_get_clock_speed() 0
+  #define get_clock_speed_number() 0
+#endif
+
+
 int sort_function(const void *dest_str_ptr, const void *src_str_ptr)
 {
   char *dest_str = *((char **)dest_str_ptr);
 int sort_function(const void *dest_str_ptr, const void *src_str_ptr)
 {
   char *dest_str = *((char **)dest_str_ptr);
@@ -112,11 +145,11 @@ s32 load_file(u8 **wildcards, u8 *result)
   u32 chosen_file, chosen_dir;
   u32 dialog_result = 1;
   s32 return_value = 1;
   u32 chosen_file, chosen_dir;
   u32 dialog_result = 1;
   s32 return_value = 1;
-  u32 current_file_selection;
-  u32 current_file_scroll_value;
+  s32 current_file_selection;
+  s32 current_file_scroll_value;
   u32 current_dir_selection;
   u32 current_dir_scroll_value;
   u32 current_dir_selection;
   u32 current_dir_scroll_value;
-  u32 current_file_in_scroll;
+  s32 current_file_in_scroll;
   u32 current_dir_in_scroll;
   u32 current_file_number, current_dir_number;
   u32 current_column = 0;
   u32 current_dir_in_scroll;
   u32 current_file_number, current_dir_number;
   u32 current_column = 0;
@@ -336,6 +369,13 @@ s32 load_file(u8 **wildcards, u8 *result)
                 current_file_in_scroll++;
               }
             }
                 current_file_in_scroll++;
               }
             }
+            else
+            {
+              clear_screen(COLOR_BG);
+              current_file_selection = 0;
+              current_file_scroll_value = 0;
+              current_file_in_scroll = 0;
+            }
           }
           else
           {
           }
           else
           {
@@ -356,6 +396,25 @@ s32 load_file(u8 **wildcards, u8 *result)
 
           break;
 
 
           break;
 
+        case CURSOR_R:
+          if (current_column != 0)
+            break;
+          clear_screen(COLOR_BG);
+         current_file_selection += FILE_LIST_ROWS;
+          if (current_file_selection > num_files - 1)
+            current_file_selection = num_files - 1;
+          current_file_scroll_value = current_file_selection - FILE_LIST_ROWS / 2;
+          if (current_file_scroll_value < 0)
+          {
+            current_file_scroll_value = 0;
+            current_file_in_scroll = current_file_selection;
+          }
+          else
+          {
+            current_file_in_scroll = FILE_LIST_ROWS / 2;
+          }
+          break;
+
         case CURSOR_UP:
           if(current_column == 0)
           {
         case CURSOR_UP:
           if(current_column == 0)
           {
@@ -372,6 +431,17 @@ s32 load_file(u8 **wildcards, u8 *result)
                 current_file_in_scroll--;
               }
             }
                 current_file_in_scroll--;
               }
             }
+            else
+            {
+              clear_screen(COLOR_BG);
+              current_file_selection = num_files - 1;
+              current_file_in_scroll = FILE_LIST_ROWS - 1;
+              if (current_file_in_scroll > num_files - 1)
+                current_file_in_scroll = num_files - 1;
+              current_file_scroll_value = num_files - FILE_LIST_ROWS;
+              if (current_file_scroll_value < 0)
+                current_file_scroll_value = 0;
+            }
           }
           else
           {
           }
           else
           {
@@ -391,7 +461,26 @@ s32 load_file(u8 **wildcards, u8 *result)
           }
           break;
 
           }
           break;
 
-        case CURSOR_RIGHT:
+        case CURSOR_L:
+          if (current_column != 0)
+            break;
+          clear_screen(COLOR_BG);
+         current_file_selection -= FILE_LIST_ROWS;
+          if (current_file_selection < 0)
+            current_file_selection = 0;
+          current_file_scroll_value = current_file_selection - FILE_LIST_ROWS / 2;
+          if (current_file_scroll_value < 0)
+          {
+            current_file_scroll_value = 0;
+            current_file_in_scroll = current_file_selection;
+          }
+          else
+          {
+            current_file_in_scroll = FILE_LIST_ROWS / 2;
+          }
+          break;
+
+         case CURSOR_RIGHT:
           if(current_column == 0)
           {
             if(num_dirs != 0)
           if(current_column == 0)
           {
             if(num_dirs != 0)
@@ -651,6 +740,18 @@ u32 gamepad_config_line_to_button[] =
 
 #endif
 
 
 #endif
 
+u8 *scale_options[] =
+{
+#ifdef WIZ_BUILD
+  "unscaled 3:2", "scaled 3:2 (slower)",
+  "unscaled 3:2 (anti-tear)", "scaled 3:2 (anti-tear)"
+#else
+  "unscaled 3:2", "scaled 3:2", "fullscreen", "scaled 3:2 (software)"
+#ifdef PSP_BUILD
+  " 16:9"
+#endif
+#endif
+};
 
 s32 load_game_config_file()
 {
 
 s32 load_game_config_file()
 {
@@ -676,7 +777,10 @@ s32 load_game_config_file()
       random_skip = file_options[2] % 2;
       clock_speed = file_options[3];
 
       random_skip = file_options[2] % 2;
       clock_speed = file_options[3];
 
-#ifdef GP2X_BUILD
+#ifdef WIZ_BUILD
+      if(clock_speed > 900)
+        clock_speed = 533;
+#elif defined(GP2X_BUILD)
       if(clock_speed >= 300)
         clock_speed = 200;
 #else
       if(clock_speed >= 300)
         clock_speed = 200;
 #else
@@ -709,12 +813,11 @@ s32 load_game_config_file()
 
   current_frameskip_type = auto_frameskip;
   frameskip_value = 4;
 
   current_frameskip_type = auto_frameskip;
   frameskip_value = 4;
-  random_skip = 0;
-#ifdef GP2X_BUILD
-  clock_speed = 200;
-#else
-  clock_speed = 333;
+#ifdef WIZ_BUILD
+  frameskip_value = 1;
 #endif
 #endif
+  random_skip = 0;
+  clock_speed = default_clock_speed;
 
   for(i = 0; i < 10; i++)
   {
 
   for(i = 0; i < 10; i++)
   {
@@ -729,10 +832,10 @@ s32 load_config_file()
 {
   u8 config_path[512];
 
 {
   u8 config_path[512];
 
-  #if (defined(PSP_BUILD) || defined(ARM_ARCH)) && !defined(_WIN32_WCE)
-    sprintf(config_path, "%s/%s", main_path, GPSP_CONFIG_FILENAME);
-  #else
+  #if defined(_WIN32) || defined(_WIN32_WCE)
     sprintf(config_path, "%s\\%s", main_path, GPSP_CONFIG_FILENAME);
     sprintf(config_path, "%s\\%s", main_path, GPSP_CONFIG_FILENAME);
+  #else
+    sprintf(config_path, "%s/%s", main_path, GPSP_CONFIG_FILENAME);
   #endif
 
   file_open(config_file, config_path, read);
   #endif
 
   file_open(config_file, config_path, read);
@@ -749,7 +852,8 @@ s32 load_config_file()
       s32 menu_button = -1;
       file_read_array(config_file, file_options);
 
       s32 menu_button = -1;
       file_read_array(config_file, file_options);
 
-      screen_scale = file_options[0] % 3;
+      screen_scale = file_options[0] %
+        (sizeof(scale_options) / sizeof(scale_options[0]));
       screen_filter = file_options[1] % 2;
       global_enable_audio = file_options[2] % 2;
 
       screen_filter = file_options[1] % 2;
       global_enable_audio = file_options[2] % 2;
 
@@ -921,7 +1025,7 @@ void get_savestate_snapshot(u8 *savestate_filename)
   {
     memset(snapshot_buffer, 0, 240 * 160 * 2);
     print_string_ext("No savestate exists for this slot.",
   {
     memset(snapshot_buffer, 0, 240 * 160 * 2);
     print_string_ext("No savestate exists for this slot.",
-     0xFFFF, 0x0000, 15, 75, snapshot_buffer, 240, 0);
+     0xFFFF, 0x0000, 15, 75, snapshot_buffer, 240, 0, 0, FONT_HEIGHT);
     print_string("---------- --/--/---- --:--:--          ", COLOR_HELP_TEXT,
      COLOR_BG, 10, 40);
   }
     print_string("---------- --/--/---- --:--:--          ", COLOR_HELP_TEXT,
      COLOR_BG, 10, 40);
   }
@@ -958,11 +1062,8 @@ void get_savestate_filename_noshot(u32 slot, u8 *name_buffer)
 
 u32 menu(u16 *original_screen)
 {
 
 u32 menu(u16 *original_screen)
 {
-  u32 clock_speed_number;
-#ifdef GP2X_BUILD
-  static u32 clock_speed_old = 200;
-#endif
   u8 print_buffer[81];
   u8 print_buffer[81];
+  u32 clock_speed_number;
   u32 _current_option = 0;
   gui_action_type gui_action;
   menu_enum _current_menu = MAIN_MENU;
   u32 _current_option = 0;
   gui_action_type gui_action;
   menu_enum _current_menu = MAIN_MENU;
@@ -1009,6 +1110,17 @@ u32 menu(u16 *original_screen)
     "Does nothing."
   };
 
     "Does nothing."
   };
 
+  void menu_update_clock()
+  {
+    get_clock_speed_number();
+    if (clock_speed_number < 0 || clock_speed_number >=
+     sizeof(clock_speed_options) / sizeof(clock_speed_options[0]))
+    {
+      clock_speed = default_clock_speed;
+      get_clock_speed_number();
+    }
+  }
+
   void menu_exit()
   {
     if(!first_load)
   void menu_exit()
   {
     if(!first_load)
@@ -1017,11 +1129,7 @@ u32 menu(u16 *original_screen)
 
   void menu_quit()
   {
 
   void menu_quit()
   {
-  #ifdef PSP_BUILD
-    clock_speed = (clock_speed_number + 1) * 33;
-  #elif defined(GP2X_BUILD)
-    clock_speed = 150 + clock_speed_number * 10;
-  #endif
+    menu_get_clock_speed();
     save_config_file();
     quit();
   }
     save_config_file();
     quit();
   }
@@ -1041,6 +1149,7 @@ u32 menu(u16 *original_screen)
        return_value = 1;
        repeat = 0;
        reg[CHANGED_PC_STATUS] = 1;
        return_value = 1;
        repeat = 0;
        reg[CHANGED_PC_STATUS] = 1;
+       menu_update_clock();
     }
     else
     {
     }
     else
     {
@@ -1152,14 +1261,6 @@ u32 menu(u16 *original_screen)
   u8 *yes_no_options[] = { "no", "yes" };
   u8 *enable_disable_options[] = { "disabled", "enabled" };
 
   u8 *yes_no_options[] = { "no", "yes" };
   u8 *enable_disable_options[] = { "disabled", "enabled" };
 
-  u8 *scale_options[] =
-  {
-    "unscaled 3:2", "scaled 3:2", "fullscreen"
-#ifdef PSP_BUILD
-    " 16:9"
-#endif
-  };
-
   u8 *frameskip_options[] = { "automatic", "manual", "off" };
   u8 *frameskip_variation_options[] = { "uniform", "random" };
 
   u8 *frameskip_options[] = { "automatic", "manual", "off" };
   u8 *frameskip_variation_options[] = { "uniform", "random" };
 
@@ -1181,21 +1282,6 @@ u32 menu(u16 *original_screen)
 
   u8 *update_backup_options[] = { "Exit only", "Automatic" };
 
 
   u8 *update_backup_options[] = { "Exit only", "Automatic" };
 
-#ifdef GP2X_BUILD
-  u8 *clock_speed_options[] =
-  {
-    "150MHz", "160MHz", "170MHz", "180MHz", "190MHz",
-    "200MHz", "210MHz", "220MHz", "230MHz", "240MHz",
-    "250MHz", "260MHz", "270MHz", "280MHz", "290MHz"
-  };
-#else
-  u8 *clock_speed_options[] =
-  {
-    "33MHz", "66MHz", "100MHz", "133MHz", "166MHz", "200MHz", "233MHz",
-    "266MHz", "300MHz", "333MHz"
-  };
-#endif
-
   u8 *gamepad_config_buttons[] =
   {
     "UP",
   u8 *gamepad_config_buttons[] =
   {
     "UP",
@@ -1226,7 +1312,8 @@ u32 menu(u16 *original_screen)
   menu_option_type graphics_sound_options[] =
   {
     string_selection_option(NULL, "Display scaling", scale_options,
   menu_option_type graphics_sound_options[] =
   {
     string_selection_option(NULL, "Display scaling", scale_options,
-     (u32 *)(&screen_scale), 3,
+     (u32 *)(&screen_scale),
+     sizeof(scale_options) / sizeof(scale_options[0]),
 #ifndef GP2X_BUILD
      "Determines how the GBA screen is resized in relation to the entire\n"
      "screen. Select unscaled 3:2 for GBA resolution, scaled 3:2 for GBA\n"
 #ifndef GP2X_BUILD
      "Determines how the GBA screen is resized in relation to the entire\n"
      "screen. Select unscaled 3:2 for GBA resolution, scaled 3:2 for GBA\n"
@@ -1244,14 +1331,14 @@ u32 menu(u16 *original_screen)
 #endif
     string_selection_option(NULL, "Frameskip type", frameskip_options,
      (u32 *)(&current_frameskip_type), 3,
 #endif
     string_selection_option(NULL, "Frameskip type", frameskip_options,
      (u32 *)(&current_frameskip_type), 3,
-     "Determines what kind of frameskipping to use.\n"
 #ifndef GP2X_BUILD
 #ifndef GP2X_BUILD
+     "Determines what kind of frameskipping to use.\n"
      "Frameskipping may improve emulation speed of many games.\n"
      "Frameskipping may improve emulation speed of many games.\n"
+#endif
      "Off: Do not skip any frames.\n"
      "Off: Do not skip any frames.\n"
-     "Auto: Skip up to N frames (see next option) as needed.\n"
+     "Auto: Skip up to N frames (see next opt) as needed.\n"
      "Manual: Always render only 1 out of N + 1 frames."
      "Manual: Always render only 1 out of N + 1 frames."
-#endif
-     "", 5),
+     , 5),
     numeric_selection_option(NULL, "Frameskip value", &frameskip_value, 100,
 #ifndef GP2X_BUILD
      "For auto frameskip, determines the maximum number of frames that\n"
     numeric_selection_option(NULL, "Frameskip value", &frameskip_value, 100,
 #ifndef GP2X_BUILD
      "For auto frameskip, determines the maximum number of frames that\n"
@@ -1281,10 +1368,15 @@ u32 menu(u16 *original_screen)
              &audio_buffer_size_number, 10,
 #endif
 
              &audio_buffer_size_number, 10,
 #endif
 
+#ifdef PSP_BUILD
      "Set the size (in bytes) of the audio buffer. Larger values may result\n"
      "in slightly better performance at the cost of latency; the lowest\n"
      "value will give the most responsive audio.\n"
      "This option requires gpSP to be restarted before it will take effect.",
      "Set the size (in bytes) of the audio buffer. Larger values may result\n"
      "in slightly better performance at the cost of latency; the lowest\n"
      "value will give the most responsive audio.\n"
      "This option requires gpSP to be restarted before it will take effect.",
+#else
+     "Set the size (in bytes) of the audio buffer.\n"
+     "This option requires gpSP restart to take effect.",
+#endif
      10),
     submenu_option(NULL, "Back", "Return to the main menu.", 12)
   };
      10),
     submenu_option(NULL, "Back", "Return to the main menu.", 12)
   };
@@ -1305,11 +1397,7 @@ u32 menu(u16 *original_screen)
     cheat_option(9),
     string_selection_option(NULL, "Clock speed",
      clock_speed_options, &clock_speed_number,
     cheat_option(9),
     string_selection_option(NULL, "Clock speed",
      clock_speed_options, &clock_speed_number,
-#ifdef GP2X_BUILD
-     15,
-#else
-     10,
-#endif
+     sizeof(clock_speed_options) / sizeof(clock_speed_options[0]),
      "Change the clock speed of the device. Higher clock\n"
      "speed will yield better performance, but will drain\n"
      "battery life further.", 11),
      "Change the clock speed of the device. Higher clock\n"
      "speed will yield better performance, but will drain\n"
      "battery life further.", 11),
@@ -1409,9 +1497,15 @@ u32 menu(u16 *original_screen)
     gamepad_config_option("Y            ", 7),
     gamepad_config_option("Left Trigger ", 8),
     gamepad_config_option("Right Trigger", 9),
     gamepad_config_option("Y            ", 7),
     gamepad_config_option("Left Trigger ", 8),
     gamepad_config_option("Right Trigger", 9),
+#ifdef WIZ_BUILD
+    gamepad_config_option("Menu         ", 10),
+#else
     gamepad_config_option("Start        ", 10),
     gamepad_config_option("Start        ", 10),
+#endif
     gamepad_config_option("Select       ", 11),
     gamepad_config_option("Select       ", 11),
+#ifndef WIZ_BUILD
     gamepad_config_option("Stick Push   ", 12),
     gamepad_config_option("Stick Push   ", 12),
+#endif
     submenu_option(NULL, "Back", "Return to the main menu.", 14)
   };
 
     submenu_option(NULL, "Back", "Return to the main menu.", 14)
   };
 
@@ -1509,12 +1603,7 @@ u32 menu(u16 *original_screen)
     }
   }
 
     }
   }
 
-#ifdef PSP_BUILD
-  clock_speed_number = (clock_speed / 33) - 1;
-#elif defined(GP2X_BUILD)
-  clock_speed_number = (clock_speed - 150) / 10;
-#endif
-
+  menu_update_clock();
   video_resolution_large();
 
 #ifndef GP2X_BUILD
   video_resolution_large();
 
 #ifndef GP2X_BUILD
@@ -1531,7 +1620,7 @@ u32 menu(u16 *original_screen)
     first_load = 1;
     memset(original_screen, 0x00, 240 * 160 * 2);
     print_string_ext("No game loaded yet.", 0xFFFF, 0x0000,
     first_load = 1;
     memset(original_screen, 0x00, 240 * 160 * 2);
     print_string_ext("No game loaded yet.", 0xFFFF, 0x0000,
-     60, 75,original_screen, 240, 0);
+     60, 75,original_screen, 240, 0, 0, FONT_HEIGHT);
   }
 
   choose_menu(&main_menu);
   }
 
   choose_menu(&main_menu);
@@ -1577,12 +1666,12 @@ u32 menu(u16 *original_screen)
       if(display_option == current_option)
       {
         print_string_pad(line_buffer, COLOR_ACTIVE_ITEM, COLOR_BG, 10,
       if(display_option == current_option)
       {
         print_string_pad(line_buffer, COLOR_ACTIVE_ITEM, COLOR_BG, 10,
-         (display_option->line_number * 10) + 40, 36);
+         (display_option->line_number * 10) + 40, 41);
       }
       else
       {
         print_string_pad(line_buffer, COLOR_INACTIVE_ITEM, COLOR_BG, 10,
       }
       else
       {
         print_string_pad(line_buffer, COLOR_INACTIVE_ITEM, COLOR_BG, 10,
-         (display_option->line_number * 10) + 40, 36);
+         (display_option->line_number * 10) + 40, 41);
       }
     }
 
       }
     }
 
@@ -1663,19 +1752,8 @@ u32 menu(u16 *original_screen)
 
   set_gba_resolution(screen_scale);
   video_resolution_small();
 
   set_gba_resolution(screen_scale);
   video_resolution_small();
-
-  #ifdef PSP_BUILD
-    clock_speed = (clock_speed_number + 1) * 33;
-    scePowerSetClockFrequency(clock_speed, clock_speed, clock_speed / 2);
-  #elif defined(GP2X_BUILD)
-    clock_speed = 150 + clock_speed_number * 10;
-    if (clock_speed != clock_speed_old)
-    {
-      printf("about to set CPU clock to %iMHz\n", clock_speed);
-      set_FCLK(clock_speed);
-      clock_speed_old = clock_speed;
-    }
-  #endif
+  menu_get_clock_speed();
+  set_clock_speed();
 
   SDL_PauseAudio(0);
 
 
   SDL_PauseAudio(0);