Skip to content

Commit c7bcbc0

Browse files
committed
feat: add macOS compatibility
1 parent 5e5a43c commit c7bcbc0

7 files changed

Lines changed: 199 additions & 14 deletions

File tree

package/garlicui/src/clock.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <sys/time.h>
22
#include <sys/ioctl.h>
3+
#ifdef __linux__
34
#include <linux/rtc.h>
5+
#endif
46
#include <fcntl.h>
57

68
#include "clock.h"
@@ -66,6 +68,7 @@ void clock_set_current_time(time_t timestamp, int utc_offset)
6668
// Open the device for read/write access
6769
int dev_fd = open(dev_path, O_RDWR);
6870

71+
#ifdef __linux__
6972
// We managed to open the device for read/write access
7073
if (dev_fd >= 0)
7174
{
@@ -87,6 +90,7 @@ void clock_set_current_time(time_t timestamp, int utc_offset)
8790
// Close the device
8891
close(dev_fd);
8992
}
93+
#endif
9094

9195
// Free the device path buffer
9296
free(dev_path);

package/garlicui/src/gui.c

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ struct gui_context * gui_create_context(int argc, char * argv[])
518518

519519
// Render the east face button icon
520520
context->surfaces.bars.bottom.actions.select.surface = gui_render_select_legend_surface(context);
521-
521+
522522
// We failed to render the east face button icon
523523
if (context->surfaces.bars.bottom.actions.select.surface == NULL)
524524
{
@@ -586,6 +586,7 @@ struct gui_context * gui_create_context(int argc, char * argv[])
586586
context->surfaces.overlays.notch.position.w = context->surfaces.overlays.notch.surface->w;
587587
context->surfaces.overlays.notch.position.h = context->surfaces.overlays.notch.surface->h;
588588

589+
#ifndef __MACOSX__
589590
// We couldn't enumerate the joysticks
590591
if (SDL_NumJoysticks() <= 0)
591592
{
@@ -602,7 +603,7 @@ struct gui_context * gui_create_context(int argc, char * argv[])
602603
// No use going further if we have no way of controlling the UI
603604
goto free_menu_action_surface;
604605
}
605-
606+
#endif
606607
// Allocate memory for the additional main menu data
607608
main_menu_node_data = calloc(1, sizeof(struct gui_menu_node_data));
608609

@@ -630,7 +631,9 @@ struct gui_context * gui_create_context(int argc, char * argv[])
630631
context->menu.root->activate(context->menu.root);
631632

632633
// Enable joystick input
634+
#ifndef __MACOSX__
633635
SDL_JoystickEventState(SDL_ENABLE);
636+
#endif
634637

635638
// Restore the previous UI state
636639
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[])
684687
free(main_menu_node_data);
685688

686689
close_internal_joystick:
690+
#ifndef __MACOSX__
687691
// Close the internal joystick
688692
SDL_JoystickClose(context->inputs.internal.joystick);
693+
#endif
689694

690695
free_notch_overlay_surface:
691696
// Free the notch overlay
@@ -1026,6 +1031,150 @@ void gui_update(struct gui_context * context)
10261031
// Differentiate input event types
10271032
switch (event.type)
10281033
{
1034+
// Keyboard button events
1035+
case SDL_KEYUP:
1036+
case SDL_KEYDOWN:
1037+
{
1038+
// Determine the pressed state
1039+
int pressed = event.type == SDL_KEYDOWN;
1040+
1041+
// Differentiate between buttons
1042+
switch (event.key.keysym.sym)
1043+
{
1044+
// The up button
1045+
case SDLK_UP:
1046+
{
1047+
// Update
1048+
context->inputs.internal.current.dpad_y = pressed ? 1 : 0;
1049+
1050+
// Break
1051+
break;
1052+
}
1053+
1054+
// The right button
1055+
case SDLK_RIGHT:
1056+
{
1057+
// Update
1058+
context->inputs.internal.current.dpad_x = pressed ? 1 : 0;
1059+
1060+
// Break
1061+
break;
1062+
}
1063+
1064+
// The down button
1065+
case SDLK_DOWN:
1066+
{
1067+
// Update
1068+
context->inputs.internal.current.dpad_y = pressed ? -1 : 0;
1069+
1070+
// Break
1071+
break;
1072+
}
1073+
1074+
// The left button
1075+
case SDLK_LEFT:
1076+
{
1077+
// Update
1078+
context->inputs.internal.current.dpad_x = pressed ? -1 : 0;
1079+
1080+
// Break
1081+
break;
1082+
}
1083+
1084+
// The south-facing face button (A on XBOX, B on Nintendo, Cross on PlayStation)
1085+
case SDLK_z:
1086+
{
1087+
// Update
1088+
context->inputs.internal.current.south = pressed;
1089+
1090+
// Break
1091+
break;
1092+
}
1093+
1094+
// The east-facing face button (B on XBOX, A on Nintendo, Circle on PlayStation)
1095+
case SDLK_x:
1096+
{
1097+
// Update
1098+
context->inputs.internal.current.east = pressed;
1099+
1100+
// Break
1101+
break;
1102+
}
1103+
1104+
// The north-facing face button (Y on XBOX, X on Nintendo, Triangle on PlayStation)
1105+
case SDLK_c:
1106+
{
1107+
// Update
1108+
context->inputs.internal.current.north = pressed;
1109+
1110+
// Break
1111+
break;
1112+
}
1113+
1114+
// The west-facing face button (X on XBOX, Y on Nintendo, Square on PlayStation)
1115+
case SDLK_v:
1116+
{
1117+
// Update
1118+
context->inputs.internal.current.west = pressed;
1119+
1120+
// Break
1121+
break;
1122+
}
1123+
1124+
// The enter button
1125+
case SDLK_RETURN:
1126+
{
1127+
// Update
1128+
context->inputs.internal.current.start = pressed;
1129+
1130+
// Break
1131+
break;
1132+
}
1133+
1134+
// The backspace button
1135+
case SDLK_BACKSPACE:
1136+
{
1137+
// Update
1138+
context->inputs.internal.current.select = pressed;
1139+
1140+
// Break
1141+
break;
1142+
}
1143+
1144+
// The space button
1145+
case SDLK_SPACE:
1146+
{
1147+
// Update
1148+
context->inputs.internal.current.mode = pressed;
1149+
1150+
// Break
1151+
break;
1152+
}
1153+
1154+
// The escape button
1155+
case SDLK_ESCAPE:
1156+
{
1157+
// Update
1158+
context->inputs.internal.current.power = pressed;
1159+
1160+
// Break
1161+
break;
1162+
}
1163+
1164+
// Other buttons
1165+
default:
1166+
{
1167+
// Break
1168+
break;
1169+
}
1170+
}
1171+
1172+
// Log the event
1173+
// printf("button %d state %d\n", event.jbutton.button, pressed);
1174+
1175+
// Break
1176+
break;
1177+
}
10291178
// Regular button events
10301179
case SDL_JOYBUTTONUP:
10311180
case SDL_JOYBUTTONDOWN:
@@ -1888,4 +2037,4 @@ void gui_write_configuration(struct gui_context * context)
18882037
// Cleanup
18892038
xmlFreeDoc(document);
18902039
}
1891-
}
2040+
}

package/garlicui/src/gui_main_menu.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "gui_context_menu.h"
88
#include "gui_retroarch_menu.h"
99

10+
#ifndef VERSION
11+
#define VERSION "dev"
12+
#endif
13+
1014
/**
1115
* @brief Activates the main menu.
1216
*/
@@ -143,4 +147,4 @@ static void gui_invalidate_main_menu(struct gui_node * this)
143147
}
144148
}
145149

146-
#endif
150+
#endif

package/garlicui/src/io.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ int io_get_content_items(const char * path, char *** items, int include_files, i
476476
// Open the given directory
477477
DIR * directory = opendir(path);
478478

479+
// We failed to open the directory
480+
if (directory == NULL)
481+
{
482+
return 0;
483+
}
484+
479485
// The collected items
480486
char ** collected_items = NULL;
481487

@@ -929,6 +935,13 @@ void io_shutdown()
929935
// Close the SysRq trigger file
930936
fclose(sysrq);
931937
}
938+
#ifdef __MACOSX__
939+
else
940+
{
941+
// Exit the application
942+
exit(0);
943+
}
944+
#endif
932945
}
933946

934947
/**

package/garlicui/src/io.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
*/
1212
typedef char * (*textformatter)(const char *);
1313

14+
/**
15+
* @brief The directory in which all boot related files are stored.
16+
*
17+
* This includes the boot script, rootfs, global configuration file, etc.
18+
*/
19+
#ifdef __MACOSX__
20+
#define FOLDER_CONFIGURATION_BOOT_FOLDER ""
21+
#else
22+
#define FOLDER_CONFIGURATION_BOOT_FOLDER "/media/boot/"
23+
#endif
24+
1425
/**
1526
* @brief The folder that contains the game library template.
1627
*/
@@ -29,14 +40,7 @@ typedef char * (*textformatter)(const char *);
2940
/**
3041
* @brief The folder that contains the icon overrides.
3142
*/
32-
#define ICON_FOLDER_PATH "/media/boot/icons"
33-
34-
/**
35-
* @brief The directory in which all boot related files are stored.
36-
*
37-
* This includes the boot script, rootfs, global configuration file, etc.
38-
*/
39-
#define FOLDER_CONFIGURATION_BOOT_FOLDER "/media/boot"
43+
#define ICON_FOLDER_PATH FOLDER_CONFIGURATION_BOOT_FOLDER "icons"
4044

4145
/**
4246
* @brief The folder icon file name.
@@ -189,4 +193,4 @@ void io_copy_directory(const char * src_dir, const char * dest_dir);
189193
*/
190194
void io_unpack_resources();
191195

192-
#endif
196+
#endif

package/garlicui/src/localization.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <unicode/ustring.h>
2+
13
#include "icon.h"
24
#include "localization.h"
35

@@ -32,7 +34,11 @@ const char * localization_font_file_path(const char * locale)
3234
if (strcmp(localized_font_file_path, font_file_path_msgid) == 0)
3335
{
3436
// Fall back to a semi-safe latin default font (better than nothing)
37+
#ifdef __MACOSX__
38+
localized_font_file_path = "/System/Library/Fonts/Monaco.ttf";
39+
#else
3540
localized_font_file_path = "/usr/share/fonts/oswald/Oswald-Regular.ttf";
41+
#endif
3642
}
3743

3844
// Copy the localized font file path into the static buffer
@@ -204,4 +210,4 @@ int localization_get_supported_locales(char *** locales)
204210

205211
// Return the number of supported locales
206212
return num_locales;
207-
}
213+
}

package/garlicui/src/sdl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#include <SDL/SDL_gfxPrimitives.h>
99
#include <librsvg/rsvg.h>
1010

11+
// https://github.com/OpenDingux/SDL/blob/dd7260f1d7f79a58aba95a03fd6532729181eadb/include/SDL_main.h#L54
12+
#if defined(main)
13+
#undef main
14+
#endif
15+
1116
/**
1217
* @brief Fills the given SDL surface with a solid color.
1318
*

0 commit comments

Comments
 (0)