Rev 11 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | /* |
| 2 | * src/system.c |
||
| 3 | * |
||
| 4 | * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved. |
||
| 5 | * |
||
| 6 | * The use and distribution terms for this software are contained in the file |
||
| 7 | * named README, which can be found in the root of this distribution. By |
||
| 8 | * using this software in any fashion, you are agreeing to be bound by the |
||
| 9 | * terms of this license. |
||
| 10 | * |
||
| 11 | * You must not remove this notice, or any other, from this software. |
||
| 12 | */ |
||
| 13 | |||
| 11 | pmbaty | 14 | #include <SDL2/SDL.h> |
| 1 | pmbaty | 15 | |
| 16 | #include <signal.h> |
||
| 17 | |||
| 18 | #include "system.h" |
||
| 11 | pmbaty | 19 | #if defined(_WIN32) |
| 1 | pmbaty | 20 | #include "windows.h" |
| 2 | pmbaty | 21 | char *sys_getbasepath (void) |
| 22 | { |
||
| 23 | static char app_path[1024] = ""; |
||
| 24 | if (app_path[0] == 0) |
||
| 25 | { |
||
| 26 | GetModuleFileName (NULL, app_path, 1024); |
||
| 27 | if (strrchr (app_path, '\\') != NULL) |
||
| 28 | *strrchr (app_path, '\\') = 0; |
||
| 29 | } |
||
| 30 | return (app_path); |
||
| 31 | } |
||
| 13 | pmbaty | 32 | #define sys_getdatapath() sys_getbasepath () |
| 11 | pmbaty | 33 | #elif defined(__APPLE__) |
| 2 | pmbaty | 34 | #include <CoreFoundation/CoreFoundation.h> |
| 13 | pmbaty | 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
||
| 37 | #include <unistd.h> |
||
| 2 | pmbaty | 38 | #include <dlfcn.h> |
| 13 | pmbaty | 39 | #include <pwd.h> |
| 2 | pmbaty | 40 | void MessageBox (void *handle, char *msg, char *title, int buttons) |
| 41 | { |
||
| 42 | const void *keys[2]; |
||
| 43 | const void *values[2]; |
||
| 44 | SInt32 result; |
||
| 1 | pmbaty | 45 | |
| 2 | pmbaty | 46 | if (title == NULL) |
| 47 | title = ""; |
||
| 1 | pmbaty | 48 | |
| 2 | pmbaty | 49 | keys[0] = kCFUserNotificationAlertHeaderKey; |
| 50 | values[0] = CFStringCreateWithCString (kCFAllocatorDefault, title, kCFStringEncodingUTF8);; |
||
| 51 | |||
| 52 | keys[1] = kCFUserNotificationAlertMessageKey; |
||
| 53 | values[1] = CFStringCreateWithCString (kCFAllocatorDefault, msg, kCFStringEncodingUTF8); |
||
| 54 | |||
| 55 | result = 0; |
||
| 56 | CFUserNotificationCreate |
||
| 57 | ( |
||
| 58 | kCFAllocatorDefault, |
||
| 59 | 0, |
||
| 60 | kCFUserNotificationPlainAlertLevel, |
||
| 61 | &result, |
||
| 62 | CFDictionaryCreate |
||
| 63 | ( |
||
| 64 | 0, |
||
| 65 | keys, |
||
| 66 | values, |
||
| 67 | sizeof (keys) / sizeof (*keys), |
||
| 68 | &kCFTypeDictionaryKeyCallBacks, |
||
| 69 | &kCFTypeDictionaryValueCallBacks |
||
| 70 | ) |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | #define MB_OK 0 |
||
| 74 | char *sys_getbasepath (void) |
||
| 75 | { |
||
| 76 | static char app_path[1024] = ""; |
||
| 77 | if (app_path[0] == 0) |
||
| 78 | { |
||
| 79 | Dl_info addr_info; |
||
| 7 | pmbaty | 80 | if (dladdr (sys_getbasepath, &addr_info) == 0) |
| 2 | pmbaty | 81 | return ("."); |
| 82 | strcpy_s (app_path, 1024, addr_info.dli_fname); |
||
| 83 | if (strrchr (app_path, '/') != NULL) |
||
| 84 | *strrchr (app_path, '/') = 0; |
||
| 4 | pmbaty | 85 | if (strrchr (app_path, '/') != NULL) |
| 86 | *strrchr (app_path, '/') = 0; |
||
| 87 | strcat_s (app_path, 1024, "/Resources"); |
||
| 2 | pmbaty | 88 | } |
| 89 | return (app_path); |
||
| 90 | } |
||
| 13 | pmbaty | 91 | char *sys_getdatapath (void) |
| 92 | { |
||
| 93 | static char data_path[1024] = ""; |
||
| 94 | if (data_path[0] == 0) |
||
| 95 | { |
||
| 96 | // Pierre-Marie Baty -- macOS .app fix |
||
| 97 | // peek in ~/Library/Application Support/<DOTTED BUNDLE ID> |
||
| 98 | // if the directory doesn't exist, create it |
||
| 99 | char bundle_id[256]; |
||
| 100 | char *homedir = getenv ("HOME"); |
||
| 101 | if (homedir == NULL) |
||
| 102 | homedir = getpwuid (getuid ())->pw_dir; |
||
| 103 | CFStringGetCString (CFBundleGetIdentifier (CFBundleGetMainBundle ()), bundle_id, sizeof (bundle_id), kCFStringEncodingUTF8); |
||
| 104 | sprintf (data_path, "%s/Library/Application Support/%s", homedir, bundle_id); |
||
| 105 | if (access (data_path, 0) != 0) |
||
| 106 | mkdir (data_path, 0755); |
||
| 107 | } |
||
| 108 | return (data_path); |
||
| 109 | } |
||
| 11 | pmbaty | 110 | #else // !_WIN32 && !__APPLE__ |
| 2 | pmbaty | 111 | #define MessageBox(handle,msg,title,btns) fprintf (stderr, msg) |
| 112 | #define MB_OK 0 |
||
| 113 | char *sys_getbasepath (void) |
||
| 114 | { |
||
| 115 | return ("."); |
||
| 116 | } |
||
| 13 | pmbaty | 117 | #define sys_getdatapath() sys_getbasepath () |
| 11 | pmbaty | 118 | #endif // _WIN32 || __APPLE__ |
| 2 | pmbaty | 119 | |
| 120 | |||
| 1 | pmbaty | 121 | /* |
| 122 | * Panic |
||
| 123 | */ |
||
| 124 | void sys_panic (char *err, ...) |
||
| 125 | { |
||
| 126 | va_list argptr; |
||
| 127 | char s[1024]; |
||
| 128 | |||
| 129 | /* prepare message */ |
||
| 130 | va_start (argptr, err); |
||
| 131 | vsprintf_s (s, sizeof (s), err, argptr); |
||
| 132 | va_end (argptr); |
||
| 133 | |||
| 134 | /* print message and die */ |
||
| 135 | MessageBox (NULL, s, NULL, MB_OK); |
||
| 136 | exit (1); |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | /* |
||
| 141 | * Print a message |
||
| 142 | */ |
||
| 143 | void sys_printf (char *msg, ...) |
||
| 144 | { |
||
| 145 | va_list argptr; |
||
| 146 | char s[2048]; |
||
| 147 | |||
| 148 | /* prepare message */ |
||
| 149 | va_start (argptr, msg); |
||
| 150 | vsprintf_s (s, sizeof (s), msg, argptr); |
||
| 151 | va_end (argptr); |
||
| 152 | |||
| 153 | MessageBox (NULL, s, NULL, MB_OK); |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /* |
||
| 158 | * Return number of microseconds elapsed since first call |
||
| 159 | */ |
||
| 160 | U32 sys_gettime (void) |
||
| 161 | { |
||
| 162 | static U32 ticks_base = 0; |
||
| 163 | U32 ticks; |
||
| 164 | |||
| 165 | ticks = SDL_GetTicks (); |
||
| 166 | |||
| 167 | if (!ticks_base) |
||
| 168 | ticks_base = ticks; |
||
| 169 | |||
| 170 | return ticks - ticks_base; |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /* |
||
| 175 | * Sleep a number of microseconds |
||
| 176 | */ |
||
| 177 | void sys_sleep (int s) |
||
| 178 | { |
||
| 179 | SDL_Delay (s); |
||
| 180 | } |
||
| 181 | |||
| 182 | |||
| 183 | /* |
||
| 184 | * Initialize system |
||
| 185 | */ |
||
| 186 | void sys_init (int argc, char **argv) |
||
| 187 | { |
||
| 188 | sysarg_init (argc, argv); |
||
| 189 | sysvid_init (); |
||
| 190 | sysjoy_init (); |
||
| 191 | |||
| 192 | if (sysarg_args_nosound == 0) |
||
| 193 | syssnd_init (); |
||
| 194 | |||
| 195 | atexit (sys_shutdown); |
||
| 196 | signal (SIGINT, exit); |
||
| 197 | signal (SIGTERM, exit); |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | /* |
||
| 202 | * Shutdown system |
||
| 203 | */ |
||
| 204 | void sys_shutdown (void) |
||
| 205 | { |
||
| 206 | syssnd_shutdown (); |
||
| 207 | sysjoy_shutdown (); |
||
| 208 | sysvid_shutdown (); |
||
| 209 | } |