annotate util.c @ 883:9f149f0e98b7

It is now possible to switch back and forth between the menu ROM and the game
author Michael Pavone <pavone@retrodev.com>
date Fri, 13 Nov 2015 19:15:37 -0800
parents a77670cd178d
children 252dfd29831d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
495
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <string.h>
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <stdlib.h>
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <stdio.h>
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <ctype.h>
768
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
5 #include <stdint.h>
805
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 799
diff changeset
6 #include <stdarg.h>
495
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
8 #include <sys/types.h>
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
9 #include <sys/stat.h>
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
10 #include <unistd.h>
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
11
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
12 #ifdef __ANDROID__
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
13 #include <android/log.h>
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
14 #define info_puts(msg) __android_log_write(ANDROID_LOG_INFO, "BlastEm", msg)
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
15 #define warning_puts(msg) __android_log_write(ANDROID_LOG_WARN, "BlastEm", msg)
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
16 #define fatal_puts(msg) __android_log_write(ANDROID_LOG_FATAL, "BlastEm", msg)
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
17
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
18 #define info_printf(msg, args) __android_log_vprint(ANDROID_LOG_INFO, "BlastEm", msg, args)
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
19 #define warning_printf(msg, args) __android_log_vprint(ANDROID_LOG_WARN, "BlastEm", msg, args)
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
20 #define fatal_printf(msg, args) __android_log_vprint(ANDROID_LOG_FATAL, "BlastEm", msg, args)
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
21 #else
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
22 #define info_puts(msg) fputs(stdout, msg);
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
23 #define warning_puts(msg) fputs(stderr, msg);
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
24 #define fatal_puts(msg) fputs(stderr, msg);
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
25
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 879
diff changeset
26 #define info_printf(msg, args) vprintf(msg, args)
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 879
diff changeset
27 #define warning_printf(msg, args) vfprintf(stderr, msg, args)
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 879
diff changeset
28 #define fatal_printf(msg, args) vfprintf(stderr, msg, args)
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
29 #endif
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
30
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
31 #include "blastem.h" //for headless global
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
32 #include "render.h" //for render_errorbox
866
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
33 #include "util.h"
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
34
876
540cc4a7d626 Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents: 875
diff changeset
35 char * alloc_concat(char const * first, char const * second)
495
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 int flen = strlen(first);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38 int slen = strlen(second);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 char * ret = malloc(flen + slen + 1);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 memcpy(ret, first, flen);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 memcpy(ret+flen, second, slen+1);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 return ret;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44
876
540cc4a7d626 Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents: 875
diff changeset
45 char * alloc_concat_m(int num_parts, char const ** parts)
495
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 int total = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 for (int i = 0; i < num_parts; i++) {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 total += strlen(parts[i]);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 char * ret = malloc(total + 1);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 *ret = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 for (int i = 0; i < num_parts; i++) {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 strcat(ret, parts[i]);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 return ret;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 long file_size(FILE * f)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 fseek(f, 0, SEEK_END);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 long fsize = ftell(f);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 fseek(f, 0, SEEK_SET);
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 return fsize;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 char * strip_ws(char * text)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 while (*text && (!isprint(*text) || isblank(*text)))
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 text++;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 char * ret = text;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 text = ret + strlen(ret) - 1;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 while (text > ret && (!isprint(*text) || isblank(*text)))
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 *text = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 text--;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 return ret;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 char * split_keyval(char * text)
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 while (*text && !isblank(*text))
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 text++;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 if (!*text) {
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 return text;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91 }
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 *text = 0;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 return text+1;
39cad98d2789 Allow OpenGL support to be disabled at compile time. Move generic utility functions out of config.c
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 }
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
95
768
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
96 uint32_t nearest_pow2(uint32_t val)
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
97 {
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
98 uint32_t ret = 1;
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
99 while (ret < val)
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
100 {
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
101 ret = ret << 1;
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
102 }
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
103 return ret;
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
104 }
2f48a3c187c6 Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
105
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
106 static char * exe_str;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
107
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
108 void set_exe_str(char * str)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
109 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
110 exe_str = str;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
111 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
112
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
113 void fatal_error(char *format, ...)
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
114 {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
115 va_list args;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
116 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
117 if (!headless) {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
118 //take a guess at the final size
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
119 size_t size = strlen(format) * 2;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
120 char *buf = malloc(size);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
121 size_t actual = vsnprintf(buf, size, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
122 if (actual >= size) {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
123 actual++;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
124 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
125 buf = malloc(actual);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
126 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
127 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
128 vsnprintf(buf, actual, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
129 }
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
130 fatal_puts(buf);
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
131 render_errorbox("Fatal Error", buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
132 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
133 } else {
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
134 fatal_printf(format, args);
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
135 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
136 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
137 exit(1);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
138 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
139
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
140 void warning(char *format, ...)
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
141 {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
142 va_list args;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
143 va_start(args, format);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
144 #ifndef _WIN32
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
145 if (headless || (isatty(STDERR_FILENO) && isatty(STDIN_FILENO))) {
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
146 warning_printf(format, args);
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
147 } else {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
148 #endif
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
149 size_t size = strlen(format) * 2;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
150 char *buf = malloc(size);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
151 size_t actual = vsnprintf(buf, size, format, args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
152 if (actual >= size) {
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
153 actual++;
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
154 free(buf);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
155 buf = malloc(actual);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
156 va_end(args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
157 va_start(args, format);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
158 vsnprintf(buf, actual, format, args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
159 }
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
160 warning_puts(buf);
794
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
161 render_infobox("BlastEm Info", buf);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
162 free(buf);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
163 #ifndef _WIN32
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
164 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
165 #endif
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
166 va_end(args);
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
167 }
792be135d3af Spawn a terminal for the debugger when needed if we are not already attached to one
Michael Pavone <pavone@retrodev.com>
parents: 792
diff changeset
168
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
169 void info_message(char *format, ...)
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
170 {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
171 va_list args;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
172 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
173 #ifndef _WIN32
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
174 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) {
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
175 info_printf(format, args);
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
176 } else {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
177 #endif
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
178 size_t size = strlen(format) * 2;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
179 char *buf = malloc(size);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
180 size_t actual = vsnprintf(buf, size, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
181 if (actual >= size) {
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
182 actual++;
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
183 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
184 buf = malloc(actual);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
185 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
186 va_start(args, format);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
187 vsnprintf(buf, actual, format, args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
188 }
879
a77670cd178d Send info/warning/fatal messages to logcat on Android
Michael Pavone <pavone@retrodev.com>
parents: 876
diff changeset
189 info_puts(buf);
792
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
190 render_infobox("BlastEm Info", buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
191 free(buf);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
192 #ifndef _WIN32
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
193 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
194 #endif
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
195 va_end(args);
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
196 }
724bbec47f86 Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents: 773
diff changeset
197
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
198 #ifdef _WIN32
795
bce97fc0bb8a Fix mingw-w64 build and cross-compilation
=?UTF-8?q?Higor=20Eur=C3=ADpedes?= <heuripedes@gmail.com>
parents: 773
diff changeset
199 #include <windows.h>
bce97fc0bb8a Fix mingw-w64 build and cross-compilation
=?UTF-8?q?Higor=20Eur=C3=ADpedes?= <heuripedes@gmail.com>
parents: 773
diff changeset
200 #include <shlobj.h>
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
201
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
202 char * get_home_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
203 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
204 static char path[MAX_PATH];
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
205 SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
206 return path;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
207 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
208
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
209 char * get_exe_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
210 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
211 static char path[MAX_PATH];
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
212 HMODULE module = GetModuleHandleA(NULL);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
213 GetModuleFileNameA(module, path, MAX_PATH);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
214
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
215 int pathsize = strlen(path);
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
216 for(char * cur = path + pathsize - 1; cur != path; cur--)
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
217 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
218 if (*cur == '\\') {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
219 *cur = 0;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
220 break;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
221 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
222 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
223 return path;
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
224 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
225
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
226 #else
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
227
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
228 char * get_home_dir()
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
229 {
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
230 return getenv("HOME");
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
231 }
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
232
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
233 char * readlink_alloc(char * path)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
234 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
235 char * linktext = NULL;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
236 ssize_t linksize = 512;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
237 ssize_t cursize = 0;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
238 do {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
239 if (linksize > cursize) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
240 cursize = linksize;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
241 if (linktext) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
242 free(linktext);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
243 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
244 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
245 linktext = malloc(cursize);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
246 linksize = readlink(path, linktext, cursize-1);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
247 if (linksize == -1) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
248 perror("readlink");
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
249 free(linktext);
559
6b248602ab84 blastem builds and almost works on OS X now
Mike Pavone <pavone@retrodev.com>
parents: 549
diff changeset
250 return NULL;
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
251 }
549
32da1e0d5e55 Properly null terminate string returned by readlink in util.c
Michael Pavone <pavone@retrodev.com>
parents: 496
diff changeset
252 } while ((linksize+1) > cursize);
32da1e0d5e55 Properly null terminate string returned by readlink in util.c
Michael Pavone <pavone@retrodev.com>
parents: 496
diff changeset
253 linktext[linksize] = 0;
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
254 return linktext;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
255 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
256
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
257 char * get_exe_dir()
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
258 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
259 static char * exe_dir;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
260 if (!exe_dir) {
762
206c449eaa81 Get "portable" builds working on Linux and add a build time check for whether /proc exists
Michael Pavone <pavone@retrodev.com>
parents: 744
diff changeset
261 char * cur;
763
2a25ea5d94f7 Fix sense of HAS_PROC check
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
262 #ifdef HAS_PROC
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
263 char * linktext = readlink_alloc("/proc/self/exe");
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
264 if (!linktext) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
265 goto fallback;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
266 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
267 int linksize = strlen(linktext);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
268 for(cur = linktext + linksize - 1; cur != linktext; cur--)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
269 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
270 if (*cur == '/') {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
271 *cur = 0;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
272 break;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
273 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
274 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
275 if (cur == linktext) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
276 free(linktext);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
277 fallback:
762
206c449eaa81 Get "portable" builds working on Linux and add a build time check for whether /proc exists
Michael Pavone <pavone@retrodev.com>
parents: 744
diff changeset
278 #endif
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
279 if (!exe_str) {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
280 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
281 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
282 int pathsize = strlen(exe_str);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
283 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--)
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
284 {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
285 if (*cur == '/') {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
286 exe_dir = malloc(cur-exe_str+1);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
287 memcpy(exe_dir, exe_str, cur-exe_str);
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
288 exe_dir[cur-exe_str] = 0;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
289 break;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
290 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
291 }
763
2a25ea5d94f7 Fix sense of HAS_PROC check
Michael Pavone <pavone@retrodev.com>
parents: 762
diff changeset
292 #ifdef HAS_PROC
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
293 } else {
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
294 exe_dir = linktext;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
295 }
762
206c449eaa81 Get "portable" builds working on Linux and add a build time check for whether /proc exists
Michael Pavone <pavone@retrodev.com>
parents: 744
diff changeset
296 #endif
496
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
297 }
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
298 return exe_dir;
6fc71114d145 Extract function to determine executable directory from load_config so it can be used elsewhere
Mike Pavone <pavone@retrodev.com>
parents: 495
diff changeset
299 }
866
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
300 #include <dirent.h>
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
301
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
302 dir_entry *get_dir_list(char *path, size_t *numret)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
303 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
304 DIR *d = opendir(path);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
305 if (!d) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
306 if (numret) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
307 *numret = 0;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
308 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
309 return NULL;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
310 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
311 size_t storage = 64;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
312 dir_entry *ret = malloc(sizeof(dir_entry) * storage);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
313 size_t pos = 0;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
314 struct dirent* entry;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
315 while (entry = readdir(d))
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
316 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
317 if (entry->d_type != DT_REG && entry->d_type != DT_LNK && entry->d_type != DT_DIR) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
318 continue;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
319 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
320 if (pos == storage) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
321 storage = storage * 2;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
322 ret = realloc(ret, sizeof(dir_entry) * storage);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
323 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
324 ret[pos].name = strdup(entry->d_name);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
325 ret[pos++].is_dir = entry->d_type == DT_DIR;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
326 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
327 if (numret) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
328 *numret = pos;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
329 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
330 return ret;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
331 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
332
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
333 void free_dir_list(dir_entry *list, size_t numentries)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
334 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
335 for (size_t i = 0; i < numentries; i++)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
336 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
337 free(list[i].name);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
338 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
339 free(list);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
340 }
742
2e1b3b258523 Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents: 741
diff changeset
341
741
80a67be1770b Initial work on Windows port
Michael Pavone <pavone@retrodev.com>
parents: 549
diff changeset
342 #endif
875
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
343
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
344 #ifdef __ANDROID__
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
345
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
346 #include <SDL.h>
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
347 char *read_bundled_file(char *name, long *sizeret)
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
348 {
876
540cc4a7d626 Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents: 875
diff changeset
349 SDL_RWops *rw = SDL_RWFromFile(name, "rb");
875
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
350 if (!rw) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
351 if (sizeret) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
352 *sizeret = -1;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
353 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
354 return NULL;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
355 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
356
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
357 long fsize = rw->size(rw);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
358 if (sizeret) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
359 *sizeret = fsize;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
360 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
361 char *ret;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
362 if (fsize) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
363 ret = malloc(fsize);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
364 if (SDL_RWread(rw, ret, 1, fsize) != fsize) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
365 free(ret);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
366 ret = NULL;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
367 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
368 } else {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
369 ret = NULL;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
370 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
371 SDL_RWclose(rw);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
372 return ret;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
373 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
374
876
540cc4a7d626 Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents: 875
diff changeset
375 char const *get_config_dir()
875
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
376 {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
377 return SDL_AndroidGetInternalStoragePath();
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
378 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
379
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
380 #else
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
381
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
382 char *read_bundled_file(char *name, long *sizeret)
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
383 {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
384 char *exe_dir = get_exe_dir();
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
385 if (!exe_dir) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
386 if (sizeret) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
387 *sizeret = -1;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
388 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
389 return NULL;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
390 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
391 char *pieces[] = {exe_dir, "/", name};
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
392 char *path = alloc_concat_m(3, pieces);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
393 FILE *f = fopen(path, "rb");
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
394 free(path);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
395 if (!f) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
396 if (sizeret) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
397 *sizeret = -1;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
398 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
399 return NULL;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
400 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
401
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
402 long fsize = file_size(f);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
403 if (sizeret) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
404 *sizeret = fsize;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
405 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
406 char *ret;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
407 if (fsize) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
408 //reserve an extra byte in case caller wants
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
409 //to null terminate the data
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
410 ret = malloc(fsize+1);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
411 if (fread(ret, 1, fsize, f) != fsize) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
412 free(ret);
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
413 ret = NULL;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
414 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
415 } else {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
416 ret = NULL;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
417 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
418 return ret;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
419 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
420
876
540cc4a7d626 Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents: 875
diff changeset
421 char const *get_config_dir()
875
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
422 {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
423 static char* confdir;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
424 if (!confdir) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
425 char *homedir = get_home_dir();
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
426 if (homedir) {
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
427 confdir = alloc_concat(homedir, "/.config/blastem");
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
428 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
429 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
430 return confdir;
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
431 }
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
432
54ffba3768d6 Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
433 #endif