From 779354b1965e5b8bf12925e504f5eedcade89f85 Mon Sep 17 00:00:00 2001 From: qkdreyer Date: Sun, 26 Nov 2023 18:28:15 +0100 Subject: [PATCH] feat: add macOS compatibility --- package/garlicui/src/clock.c | 4 + package/garlicui/src/gui.c | 155 ++++++++++++++++++++++++++- package/garlicui/src/gui_main_menu.h | 6 +- package/garlicui/src/io.c | 13 +++ package/garlicui/src/io.h | 22 ++-- package/garlicui/src/localization.c | 8 +- package/garlicui/src/sdl.h | 5 + 7 files changed, 199 insertions(+), 14 deletions(-) diff --git a/package/garlicui/src/clock.c b/package/garlicui/src/clock.c index 2df9ffd7d5..3f06b75753 100644 --- a/package/garlicui/src/clock.c +++ b/package/garlicui/src/clock.c @@ -1,6 +1,8 @@ #include #include +#ifdef __linux__ #include +#endif #include #include "clock.h" @@ -66,6 +68,7 @@ void clock_set_current_time(time_t timestamp, int utc_offset) // Open the device for read/write access int dev_fd = open(dev_path, O_RDWR); +#ifdef __linux__ // We managed to open the device for read/write access if (dev_fd >= 0) { @@ -87,6 +90,7 @@ void clock_set_current_time(time_t timestamp, int utc_offset) // Close the device close(dev_fd); } +#endif // Free the device path buffer free(dev_path); diff --git a/package/garlicui/src/gui.c b/package/garlicui/src/gui.c index 2cd690ebc1..ab2588c3ca 100644 --- a/package/garlicui/src/gui.c +++ b/package/garlicui/src/gui.c @@ -518,7 +518,7 @@ struct gui_context * gui_create_context(int argc, char * argv[]) // Render the east face button icon context->surfaces.bars.bottom.actions.select.surface = gui_render_select_legend_surface(context); - + // We failed to render the east face button icon if (context->surfaces.bars.bottom.actions.select.surface == NULL) { @@ -586,6 +586,7 @@ struct gui_context * gui_create_context(int argc, char * argv[]) context->surfaces.overlays.notch.position.w = context->surfaces.overlays.notch.surface->w; context->surfaces.overlays.notch.position.h = context->surfaces.overlays.notch.surface->h; +#ifndef __MACOSX__ // We couldn't enumerate the joysticks if (SDL_NumJoysticks() <= 0) { @@ -602,7 +603,7 @@ struct gui_context * gui_create_context(int argc, char * argv[]) // No use going further if we have no way of controlling the UI goto free_menu_action_surface; } - +#endif // Allocate memory for the additional main menu data main_menu_node_data = calloc(1, sizeof(struct gui_menu_node_data)); @@ -630,7 +631,9 @@ struct gui_context * gui_create_context(int argc, char * argv[]) context->menu.root->activate(context->menu.root); // Enable joystick input +#ifndef __MACOSX__ SDL_JoystickEventState(SDL_ENABLE); +#endif // Restore the previous UI state gui_restore_ui_state(context, (const char *)context->settings.ui_state); @@ -684,8 +687,10 @@ struct gui_context * gui_create_context(int argc, char * argv[]) free(main_menu_node_data); close_internal_joystick: +#ifndef __MACOSX__ // Close the internal joystick SDL_JoystickClose(context->inputs.internal.joystick); +#endif free_notch_overlay_surface: // Free the notch overlay @@ -1026,6 +1031,150 @@ void gui_update(struct gui_context * context) // Differentiate input event types switch (event.type) { + // Keyboard button events + case SDL_KEYUP: + case SDL_KEYDOWN: + { + // Determine the pressed state + int pressed = event.type == SDL_KEYDOWN; + + // Differentiate between buttons + switch (event.key.keysym.sym) + { + // The up button + case SDLK_UP: + { + // Update + context->inputs.internal.current.dpad_y = pressed ? -1 : 0; + + // Break + break; + } + + // The right button + case SDLK_RIGHT: + { + // Update + context->inputs.internal.current.dpad_x = pressed ? 1 : 0; + + // Break + break; + } + + // The down button + case SDLK_DOWN: + { + // Update + context->inputs.internal.current.dpad_y = pressed ? 1 : 0; + + // Break + break; + } + + // The left button + case SDLK_LEFT: + { + // Update + context->inputs.internal.current.dpad_x = pressed ? -1 : 0; + + // Break + break; + } + + // The south-facing face button (A on XBOX, B on Nintendo, Cross on PlayStation) + case SDLK_z: + { + // Update + context->inputs.internal.current.south = pressed; + + // Break + break; + } + + // The east-facing face button (B on XBOX, A on Nintendo, Circle on PlayStation) + case SDLK_x: + { + // Update + context->inputs.internal.current.east = pressed; + + // Break + break; + } + + // The north-facing face button (Y on XBOX, X on Nintendo, Triangle on PlayStation) + case SDLK_c: + { + // Update + context->inputs.internal.current.north = pressed; + + // Break + break; + } + + // The west-facing face button (X on XBOX, Y on Nintendo, Square on PlayStation) + case SDLK_v: + { + // Update + context->inputs.internal.current.west = pressed; + + // Break + break; + } + + // The enter button + case SDLK_RETURN: + { + // Update + context->inputs.internal.current.start = pressed; + + // Break + break; + } + + // The backspace button + case SDLK_BACKSPACE: + { + // Update + context->inputs.internal.current.select = pressed; + + // Break + break; + } + + // The space button + case SDLK_SPACE: + { + // Update + context->inputs.internal.current.mode = pressed; + + // Break + break; + } + + // The escape button + case SDLK_ESCAPE: + { + // Update + context->inputs.internal.current.power = pressed; + + // Break + break; + } + + // Other buttons + default: + { + // Break + break; + } + } + + // Log the event + // printf("button %d state %d\n", event.jbutton.button, pressed); + + // Break + break; + } // Regular button events case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: @@ -1888,4 +2037,4 @@ void gui_write_configuration(struct gui_context * context) // Cleanup xmlFreeDoc(document); } -} \ No newline at end of file +} diff --git a/package/garlicui/src/gui_main_menu.h b/package/garlicui/src/gui_main_menu.h index 68e2d83c55..b1b8fc585c 100644 --- a/package/garlicui/src/gui_main_menu.h +++ b/package/garlicui/src/gui_main_menu.h @@ -7,6 +7,10 @@ #include "gui_context_menu.h" #include "gui_retroarch_menu.h" +#ifndef VERSION +#define VERSION "dev" +#endif + /** * @brief Activates the main menu. */ @@ -143,4 +147,4 @@ static void gui_invalidate_main_menu(struct gui_node * this) } } -#endif \ No newline at end of file +#endif diff --git a/package/garlicui/src/io.c b/package/garlicui/src/io.c index 1aa55602af..6cb22abec8 100644 --- a/package/garlicui/src/io.c +++ b/package/garlicui/src/io.c @@ -476,6 +476,12 @@ int io_get_content_items(const char * path, char *** items, int include_files, i // Open the given directory DIR * directory = opendir(path); + // We failed to open the directory + if (directory == NULL) + { + return 0; + } + // The collected items char ** collected_items = NULL; @@ -929,6 +935,13 @@ void io_shutdown() // Close the SysRq trigger file fclose(sysrq); } +#ifdef __MACOSX__ + else + { + // Exit the application + exit(0); + } +#endif } /** diff --git a/package/garlicui/src/io.h b/package/garlicui/src/io.h index 63d2eca69d..c2e4dd2345 100644 --- a/package/garlicui/src/io.h +++ b/package/garlicui/src/io.h @@ -11,6 +11,17 @@ */ typedef char * (*textformatter)(const char *); +/** + * @brief The directory in which all boot related files are stored. + * + * This includes the boot script, rootfs, global configuration file, etc. + */ +#ifdef __MACOSX__ +#define FOLDER_CONFIGURATION_BOOT_FOLDER "" +#else +#define FOLDER_CONFIGURATION_BOOT_FOLDER "/media/boot/" +#endif + /** * @brief The folder that contains the game library template. */ @@ -29,14 +40,7 @@ typedef char * (*textformatter)(const char *); /** * @brief The folder that contains the icon overrides. */ -#define ICON_FOLDER_PATH "/media/boot/icons" - -/** - * @brief The directory in which all boot related files are stored. - * - * This includes the boot script, rootfs, global configuration file, etc. - */ -#define FOLDER_CONFIGURATION_BOOT_FOLDER "/media/boot" +#define ICON_FOLDER_PATH FOLDER_CONFIGURATION_BOOT_FOLDER "icons" /** * @brief The folder icon file name. @@ -189,4 +193,4 @@ void io_copy_directory(const char * src_dir, const char * dest_dir); */ void io_unpack_resources(); -#endif \ No newline at end of file +#endif diff --git a/package/garlicui/src/localization.c b/package/garlicui/src/localization.c index 378a00f33f..105125a972 100644 --- a/package/garlicui/src/localization.c +++ b/package/garlicui/src/localization.c @@ -1,3 +1,5 @@ +#include + #include "icon.h" #include "localization.h" @@ -32,7 +34,11 @@ const char * localization_font_file_path(const char * locale) if (strcmp(localized_font_file_path, font_file_path_msgid) == 0) { // Fall back to a semi-safe latin default font (better than nothing) +#ifdef __MACOSX__ + localized_font_file_path = "/System/Library/Fonts/Monaco.ttf"; +#else localized_font_file_path = "/usr/share/fonts/oswald/Oswald-Regular.ttf"; +#endif } // Copy the localized font file path into the static buffer @@ -204,4 +210,4 @@ int localization_get_supported_locales(char *** locales) // Return the number of supported locales return num_locales; -} \ No newline at end of file +} diff --git a/package/garlicui/src/sdl.h b/package/garlicui/src/sdl.h index f83ae65082..b9e7d67824 100644 --- a/package/garlicui/src/sdl.h +++ b/package/garlicui/src/sdl.h @@ -8,6 +8,11 @@ #include #include +// https://github.com/OpenDingux/SDL/blob/dd7260f1d7f79a58aba95a03fd6532729181eadb/include/SDL_main.h#L54 +#if defined(main) +#undef main +#endif + /** * @brief Fills the given SDL surface with a solid color. *