|
1 | | -#include "menu.h" |
2 | | -#include <iostream> |
3 | | -#include <SDL/SDL.h> |
| 1 | +/* |
| 2 | + FK - FunKey retro gaming console library |
| 3 | + Copyright (C) 2020-2021 Vincent Buso |
| 4 | + Copyright (C) 2020-2021 Michel Stempin |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | +
|
| 20 | + Vincent Buso |
| 21 | + |
| 22 | + Michel Stempin |
| 23 | + |
| 24 | +*/ |
| 25 | + |
| 26 | +/** |
| 27 | + * @file FK_menu.c |
| 28 | + * This is the menu API for the FunKey retro gaming console library |
| 29 | + */ |
| 30 | + |
| 31 | +#include "fk_menu.h" |
4 | 32 |
|
5 | 33 | /// -------------- DEFINES -------------- |
6 | 34 |
|
|
57 | 85 |
|
58 | 86 |
|
59 | 87 | /// -------------- STATIC VARIABLES -------------- |
60 | | -SDL_Surface * Menu::background_screen = NULL; |
61 | | -int Menu::backup_key_repeat_delay=0; |
62 | | -int Menu::backup_key_repeat_interval=0; |
63 | | -TTF_Font *Menu::menu_title_font = NULL; |
64 | | -TTF_Font *Menu::menu_info_font = NULL; |
65 | | -TTF_Font *Menu::menu_small_info_font = NULL; |
66 | | -SDL_Surface *img_arrow_top = NULL; |
67 | | -SDL_Surface *img_arrow_bottom = NULL; |
68 | | -SDL_Surface ** Menu::menu_zone_surfaces = NULL; |
69 | | -int *Menu::idx_menus = NULL; |
70 | | -int Menu::nb_menu_zones = 0; |
71 | | -int Menu::menuItem=0; |
72 | | -int Menu::stop_menu_loop = 0; |
73 | | - |
74 | | -SDL_Color Menu::text_color = {GRAY_MAIN_R, GRAY_MAIN_G, GRAY_MAIN_B}; |
75 | | -int Menu::padding_y_from_center_menu_zone = 18; |
76 | | -uint16_t Menu::width_progress_bar = 100; |
77 | | -uint16_t Menu::height_progress_bar = 20; |
| 88 | +static SDL_Surface * background_screen = NULL; |
| 89 | +static int backup_key_repeat_delay=0; |
| 90 | +static int backup_key_repeat_interval=0; |
| 91 | +static TTF_Font *menu_title_font = NULL; |
| 92 | +static TTF_Font *menu_info_font = NULL; |
| 93 | +static TTF_Font *menu_small_info_font = NULL; |
| 94 | +static SDL_Surface *img_arrow_top = NULL; |
| 95 | +static SDL_Surface *img_arrow_bottom = NULL; |
| 96 | +static SDL_Surface ** menu_zone_surfaces = NULL; |
| 97 | +static int *idx_menus = NULL; |
| 98 | +static int nb_menu_zones = 0; |
| 99 | +static int menuItem=0; |
| 100 | +static int stop_menu_loop = 0; |
| 101 | + |
| 102 | +static SDL_Color text_color = {GRAY_MAIN_R, GRAY_MAIN_G, GRAY_MAIN_B}; |
| 103 | +static int padding_y_from_center_menu_zone = 18; |
| 104 | +static uint16_t width_progress_bar = 100; |
| 105 | +static uint16_t height_progress_bar = 20; |
78 | 106 |
|
79 | 107 | #ifdef HAS_MENU_VOLUME |
80 | | -uint16_t Menu::x_volume_bar = 0; |
81 | | -uint16_t Menu::y_volume_bar = 0; |
82 | | -int Menu::volume_percentage = 0; |
| 108 | +static uint16_t x_volume_bar = 0; |
| 109 | +static uint16_t y_volume_bar = 0; |
| 110 | +static int volume_percentage = 0; |
83 | 111 | #endif |
84 | 112 | #ifdef HAS_MENU_BRIGHTNESS |
85 | | -uint16_t Menu::x_brightness_bar = 0; |
86 | | -uint16_t Menu::y_brightness_bar = 0; |
87 | | -int Menu::brightness_percentage = 0; |
| 113 | +static uint16_t x_brightness_bar = 0; |
| 114 | +static uint16_t y_brightness_bar = 0; |
| 115 | +static int brightness_percentage = 0; |
88 | 116 | #endif |
89 | 117 |
|
90 | 118 | #ifdef HAS_MENU_ASPECT_RATIO |
91 | 119 | #undef X |
92 | 120 | #define X(a, b) b, |
93 | | -const char *Menu::aspect_ratio_name[] = {ASPECT_RATIOS}; |
94 | | -int Menu::aspect_ratio = ASPECT_RATIOS_TYPE_STRECHED; |
95 | | -int Menu::aspect_ratio_factor_percent = 50; |
96 | | -int Menu::aspect_ratio_factor_step = 10; |
| 121 | +static const char *aspect_ratio_name[] = {ASPECT_RATIOS}; |
| 122 | +static int aspect_ratio = ASPECT_RATIOS_TYPE_STRECHED; |
| 123 | +static int aspect_ratio_factor_percent = 50; |
| 124 | +static int aspect_ratio_factor_step = 10; |
97 | 125 | #endif |
98 | 126 |
|
99 | 127 | #ifdef HAS_MENU_THEME |
100 | | -Configuration *Menu::config = NULL; |
101 | | -int Menu::indexChooseLayout = 0; |
| 128 | +static Configuration *config = NULL; |
| 129 | +static int indexChooseLayout = 0; |
102 | 130 | #endif |
103 | 131 |
|
104 | 132 | #if defined(HAS_MENU_SAVE) || defined (HAS_MENU_LOAD) |
105 | | -int Menu::savestate_slot = 0; |
| 133 | +static int savestate_slot = 0; |
106 | 134 | #endif |
107 | 135 |
|
108 | 136 | #ifdef HAS_MENU_USB |
109 | 137 | /// USB stuff |
110 | | -int usb_data_connected = 0; |
111 | | -int usb_sharing = 0; |
| 138 | +static int usb_data_connected = 0; |
| 139 | +static int usb_sharing = 0; |
112 | 140 | #endif |
113 | 141 |
|
114 | 142 | #ifdef HAS_MENU_RO_RW |
115 | | -int read_write = 0; |
| 143 | +static int read_write = 0; |
116 | 144 | #endif |
117 | 145 |
|
118 | 146 | /// -------------- FUNCTIONS IMPLEMENTATION -------------- |
119 | | -#ifdef HAS_MENU_THEME |
120 | | -void Menu::init(Configuration &c) |
121 | | - #else |
122 | | -void Menu::init(void) |
123 | | -#endif |
124 | | -{ |
125 | | - MENU_DEBUG_PRINTF("Init Menu\n"); |
126 | | - /// ----- Loading the fonts ----- |
127 | | - menu_title_font = TTF_OpenFont(MENU_FONT_NAME_TITLE, MENU_FONT_SIZE_TITLE); |
128 | | - if(!menu_title_font){ |
129 | | - MENU_ERROR_PRINTF("ERROR in init_menu_SDL: Could not open menu font %s, %s\n", MENU_FONT_NAME_TITLE, SDL_GetError()); |
130 | | - } |
131 | | - menu_info_font = TTF_OpenFont(MENU_FONT_NAME_INFO, MENU_FONT_SIZE_INFO); |
132 | | - if(!menu_info_font){ |
133 | | - MENU_ERROR_PRINTF("ERROR in init_menu_SDL: Could not open menu font %s, %s\n", MENU_FONT_NAME_INFO, SDL_GetError()); |
134 | | - } |
135 | | - menu_small_info_font = TTF_OpenFont(MENU_FONT_NAME_SMALL_INFO, MENU_FONT_SIZE_SMALL_INFO); |
136 | | - if(!menu_small_info_font){ |
137 | | - MENU_ERROR_PRINTF("ERROR in init_menu_SDL: Could not open menu font %s, %s\n", MENU_FONT_NAME_SMALL_INFO, SDL_GetError()); |
138 | | - } |
139 | | - |
140 | | - /// ------ Load arrows imgs ------- |
141 | | - img_arrow_top = IMG_Load(MENU_PNG_ARROW_TOP_PATH); |
142 | | - if(!img_arrow_top) { |
143 | | - MENU_ERROR_PRINTF("ERROR IMG_Load: %s\n", IMG_GetError()); |
144 | | - } |
145 | | - img_arrow_bottom = IMG_Load(MENU_PNG_ARROW_BOTTOM_PATH); |
146 | | - if(!img_arrow_bottom) { |
147 | | - MENU_ERROR_PRINTF("ERROR IMG_Load: %s\n", IMG_GetError()); |
148 | | - } |
149 | | - |
150 | | -#ifdef HAS_MENU_THEME |
151 | | - /// ------ Save config pointer ------ |
152 | | - config = &c; |
153 | | -#endif |
154 | | -#ifdef HAS_MENU_RO_RW |
155 | | - /// ----- Shell cmd ---- |
156 | | - FILE *fp = popen(SHELL_CMD_RO, "r"); |
157 | | - if (fp == NULL) { |
158 | | - MENU_ERROR_PRINTF("Failed to run command %s\n", SHELL_CMD_RO); |
159 | | - } |
160 | | -#endif |
161 | | - |
162 | | - /// ------ Init menu zones ------ |
163 | | - init_menu_zones(); |
164 | | - |
165 | | - return; |
166 | | -} |
167 | | - |
168 | | - |
169 | | -void Menu::end(void) |
170 | | -{ |
171 | | - MENU_DEBUG_PRINTF("End Menu \n"); |
172 | | - /// ------ Close font ------- |
173 | | - TTF_CloseFont(menu_title_font); |
174 | | - TTF_CloseFont(menu_info_font); |
175 | | - TTF_CloseFont(menu_small_info_font); |
176 | | - |
177 | | - /// ------ Free Surfaces ------- |
178 | | - for(int i=0; i < nb_menu_zones; i++){ |
179 | | - if(menu_zone_surfaces[i] != NULL){ |
180 | | - SDL_FreeSurface(menu_zone_surfaces[i]); |
181 | | - } |
182 | | - } |
183 | | - SDL_FreeSurface(img_arrow_top); |
184 | | - SDL_FreeSurface(img_arrow_bottom); |
185 | | - |
186 | | - /// ------ Free Menu memory and reset vars ----- |
187 | | - if(idx_menus){ |
188 | | - free(idx_menus); |
189 | | - } |
190 | | - idx_menus=NULL; |
191 | | - nb_menu_zones = 0; |
192 | | - |
193 | | -#ifdef HAS_MENU_RO_RW |
194 | | - /// ----- Shell cmd ---- |
195 | | - FILE *fp = popen(SHELL_CMD_RO, "r"); |
196 | | - if (fp == NULL) { |
197 | | - MENU_ERROR_PRINTF("Failed to run command %s\n", SHELL_CMD_RO); |
198 | | - } |
199 | | -#endif |
200 | | - |
201 | | - return; |
202 | | -} |
203 | | - |
204 | | -void Menu::stop(void) |
205 | | -{ |
206 | | - stop_menu_loop = 1; |
207 | | -} |
208 | | - |
209 | 147 |
|
210 | 148 | #if defined(HAS_MENU_VOLUME) || defined(HAS_MENU_BRIGHTNESS) |
211 | | -void Menu::draw_progress_bar(SDL_Surface * surface, uint16_t x, uint16_t y, uint16_t width, |
| 149 | +static void draw_progress_bar(SDL_Surface * surface, uint16_t x, uint16_t y, uint16_t width, |
212 | 150 | uint16_t height, uint8_t percentage, uint16_t nb_bars) |
213 | 151 | { |
214 | 152 | /// ------ Init Variables ------ |
@@ -258,7 +196,7 @@ void Menu::draw_progress_bar(SDL_Surface * surface, uint16_t x, uint16_t y, uint |
258 | 196 | } |
259 | 197 | #endif |
260 | 198 |
|
261 | | -void Menu::add_menu_zone(ENUM_MENU_TYPE menu_type) |
| 199 | +static void add_menu_zone(ENUM_MENU_TYPE menu_type) |
262 | 200 | { |
263 | 201 | /// ------ Increase nb of menu zones ------- |
264 | 202 | nb_menu_zones++; |
@@ -412,7 +350,7 @@ void Menu::add_menu_zone(ENUM_MENU_TYPE menu_type) |
412 | 350 | SDL_FreeSurface(text_surface); |
413 | 351 | } |
414 | 352 |
|
415 | | -void Menu::init_menu_zones(void) |
| 353 | +static void init_menu_zones(void) |
416 | 354 | { |
417 | 355 | #ifdef HAS_MENU_VOLUME |
418 | 356 | add_menu_zone(MENU_TYPE_VOLUME); |
@@ -450,7 +388,98 @@ void Menu::init_menu_zones(void) |
450 | 388 | } |
451 | 389 |
|
452 | 390 |
|
453 | | -void Menu::init_menu_system_values(void) |
| 391 | +#ifdef HAS_MENU_THEME |
| 392 | +void FK_InitMenu(Configuration &c) |
| 393 | + #else |
| 394 | +void FK_InitMenu(void) |
| 395 | +#endif |
| 396 | +{ |
| 397 | + MENU_DEBUG_PRINTF("Init Menu\n"); |
| 398 | + /// ----- Loading the fonts ----- |
| 399 | + menu_title_font = TTF_OpenFont(MENU_FONT_NAME_TITLE, MENU_FONT_SIZE_TITLE); |
| 400 | + if(!menu_title_font){ |
| 401 | + MENU_ERROR_PRINTF("ERROR in init_menu_SDL: Could not open menu font %s, %s\n", MENU_FONT_NAME_TITLE, SDL_GetError()); |
| 402 | + } |
| 403 | + menu_info_font = TTF_OpenFont(MENU_FONT_NAME_INFO, MENU_FONT_SIZE_INFO); |
| 404 | + if(!menu_info_font){ |
| 405 | + MENU_ERROR_PRINTF("ERROR in init_menu_SDL: Could not open menu font %s, %s\n", MENU_FONT_NAME_INFO, SDL_GetError()); |
| 406 | + } |
| 407 | + menu_small_info_font = TTF_OpenFont(MENU_FONT_NAME_SMALL_INFO, MENU_FONT_SIZE_SMALL_INFO); |
| 408 | + if(!menu_small_info_font){ |
| 409 | + MENU_ERROR_PRINTF("ERROR in init_menu_SDL: Could not open menu font %s, %s\n", MENU_FONT_NAME_SMALL_INFO, SDL_GetError()); |
| 410 | + } |
| 411 | + |
| 412 | + /// ------ Load arrows imgs ------- |
| 413 | + img_arrow_top = IMG_Load(MENU_PNG_ARROW_TOP_PATH); |
| 414 | + if(!img_arrow_top) { |
| 415 | + MENU_ERROR_PRINTF("ERROR IMG_Load: %s\n", IMG_GetError()); |
| 416 | + } |
| 417 | + img_arrow_bottom = IMG_Load(MENU_PNG_ARROW_BOTTOM_PATH); |
| 418 | + if(!img_arrow_bottom) { |
| 419 | + MENU_ERROR_PRINTF("ERROR IMG_Load: %s\n", IMG_GetError()); |
| 420 | + } |
| 421 | + |
| 422 | +#ifdef HAS_MENU_THEME |
| 423 | + /// ------ Save config pointer ------ |
| 424 | + config = &c; |
| 425 | +#endif |
| 426 | +#ifdef HAS_MENU_RO_RW |
| 427 | + /// ----- Shell cmd ---- |
| 428 | + FILE *fp = popen(SHELL_CMD_RO, "r"); |
| 429 | + if (fp == NULL) { |
| 430 | + MENU_ERROR_PRINTF("Failed to run command %s\n", SHELL_CMD_RO); |
| 431 | + } |
| 432 | +#endif |
| 433 | + |
| 434 | + /// ------ Init menu zones ------ |
| 435 | + init_menu_zones(); |
| 436 | + |
| 437 | + return; |
| 438 | +} |
| 439 | + |
| 440 | + |
| 441 | +void FK_EndMenu(void) |
| 442 | +{ |
| 443 | + MENU_DEBUG_PRINTF("End Menu \n"); |
| 444 | + /// ------ Close font ------- |
| 445 | + TTF_CloseFont(menu_title_font); |
| 446 | + TTF_CloseFont(menu_info_font); |
| 447 | + TTF_CloseFont(menu_small_info_font); |
| 448 | + |
| 449 | + /// ------ Free Surfaces ------- |
| 450 | + for(int i=0; i < nb_menu_zones; i++){ |
| 451 | + if(menu_zone_surfaces[i] != NULL){ |
| 452 | + SDL_FreeSurface(menu_zone_surfaces[i]); |
| 453 | + } |
| 454 | + } |
| 455 | + SDL_FreeSurface(img_arrow_top); |
| 456 | + SDL_FreeSurface(img_arrow_bottom); |
| 457 | + |
| 458 | + /// ------ Free Menu memory and reset vars ----- |
| 459 | + if(idx_menus){ |
| 460 | + free(idx_menus); |
| 461 | + } |
| 462 | + idx_menus=NULL; |
| 463 | + nb_menu_zones = 0; |
| 464 | + |
| 465 | +#ifdef HAS_MENU_RO_RW |
| 466 | + /// ----- Shell cmd ---- |
| 467 | + FILE *fp = popen(SHELL_CMD_RO, "r"); |
| 468 | + if (fp == NULL) { |
| 469 | + MENU_ERROR_PRINTF("Failed to run command %s\n", SHELL_CMD_RO); |
| 470 | + } |
| 471 | +#endif |
| 472 | + |
| 473 | + return; |
| 474 | +} |
| 475 | + |
| 476 | +void FK_StopMenu(void) |
| 477 | +{ |
| 478 | + stop_menu_loop = 1; |
| 479 | +} |
| 480 | + |
| 481 | + |
| 482 | +static void init_menu_system_values(void) |
454 | 483 | { |
455 | 484 | #if defined(HAS_MENU_VOLUME) || defined(HAS_MENU_BRIGHTNESS) || defined(HAS_MENU_USB) || defined(HAS_MENU_RO_RW) |
456 | 485 | FILE *fp; |
@@ -528,7 +557,7 @@ void Menu::init_menu_system_values(void) |
528 | 557 | #endif |
529 | 558 | } |
530 | 559 |
|
531 | | -void Menu::menu_screen_refresh(SDL_Surface *screen, int menuItem, int prevItem, int scroll, uint8_t menu_confirmation, uint8_t menu_action) |
| 560 | +static void menu_screen_refresh(SDL_Surface *screen, int menuItem, int prevItem, int scroll, uint8_t menu_confirmation, uint8_t menu_action) |
532 | 561 | { |
533 | 562 | /// --------- Vars --------- |
534 | 563 | #ifdef HAS_MENU_USB |
@@ -817,7 +846,7 @@ void Menu::menu_screen_refresh(SDL_Surface *screen, int menuItem, int prevItem, |
817 | 846 | } |
818 | 847 |
|
819 | 848 |
|
820 | | -int Menu::run(SDL_Surface *screen) |
| 849 | +int FK_RunMenu(SDL_Surface *screen) |
821 | 850 | { |
822 | 851 | MENU_DEBUG_PRINTF("Run Menu\n"); |
823 | 852 |
|
|
0 commit comments