Mercurial > repos > blastem
annotate util.h @ 2195:40290a923886
Implement Game Gear start button and region register
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 21 Aug 2022 22:56:32 -0700 |
parents | bdd83b47d78a |
children | 794ba17f0716 |
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 #ifndef UTIL_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 #define UTIL_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 |
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 <stdio.h> |
957 | 5 #include <time.h> |
1295
96ad1b9bbb3a
Make save directory configurable. Satisfies ticket:4
Michael Pavone <pavone@retrodev.com>
parents:
1292
diff
changeset
|
6 #include "tern.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 |
866
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
8 typedef struct { |
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
9 char *name; |
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
10 uint8_t is_dir; |
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
11 } dir_entry; |
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
12 |
1008
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
13 #ifdef _WIN32 |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
14 #define PATH_SEP "\\" |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
15 #else |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
16 #define PATH_SEP "/" |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
17 #endif |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
18 |
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
|
19 //Utility functions |
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
|
20 |
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
|
21 //Allocates a new string containing the concatenation of first and second |
876
540cc4a7d626
Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents:
875
diff
changeset
|
22 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
|
23 //Allocates a new string containing the concatenation of the strings pointed to by parts |
876
540cc4a7d626
Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents:
875
diff
changeset
|
24 char * alloc_concat_m(int num_parts, char const ** parts); |
2158
bdd83b47d78a
Implement config file migrations and add iso and cue to extension list
Michael Pavone <pavone@retrodev.com>
parents:
2156
diff
changeset
|
25 //Allocates a new string containing the concatenation of the strings pointed to by parts separated by sep |
bdd83b47d78a
Implement config file migrations and add iso and cue to extension list
Michael Pavone <pavone@retrodev.com>
parents:
2156
diff
changeset
|
26 char * alloc_join(int num_parts, char const **parts, char sep); |
1292
5905593d6828
Allow initial_path to contain variable references which allows the default value to be actually specified in the default config file
Michael Pavone <pavone@retrodev.com>
parents:
1140
diff
changeset
|
27 //Returns a newly allocated string in which all variables in based are replaced with values from vars or the environment |
5905593d6828
Allow initial_path to contain variable references which allows the default value to be actually specified in the default config file
Michael Pavone <pavone@retrodev.com>
parents:
1140
diff
changeset
|
28 char *replace_vars(char *base, tern_node *vars, uint8_t allow_env); |
1103
22e87b739ad6
WIP split of ROM loading/argument parsing from Genesis emulation code. Compiles and doesn't crash, but nothing works. Still a few too many globals as well.
Michael Pavone <pavone@retrodev.com>
parents:
1008
diff
changeset
|
29 //Byteswaps a ROM image in memory |
22e87b739ad6
WIP split of ROM loading/argument parsing from Genesis emulation code. Compiles and doesn't crash, but nothing works. Still a few too many globals as well.
Michael Pavone <pavone@retrodev.com>
parents:
1008
diff
changeset
|
30 void byteswap_rom(int filesize, uint16_t *cart); |
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
|
31 //Returns the size of a file using fseek and ftell |
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
|
32 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
|
33 //Strips whitespace and non-printable characters from the beginning and end of a string |
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
|
34 char * strip_ws(char * text); |
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
|
35 //Inserts a null after the first word, returns a pointer to the second word |
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 char * split_keyval(char * text); |
1783 | 37 //Checks if haystack starts with prefix |
38 uint8_t startswith(const char *haystack, const char *prefix); | |
1305
5ceb316c479a
Allow games to be specified in ROM DB via sha1 instead of product ID. Added a new ROM DB memory map device type fixed for emulating simple fixed value copy protection registers. Used those two features to support Ya Se Chuan Shuo via a ROM DB entry.
Michael Pavone <pavone@retrodev.com>
parents:
1295
diff
changeset
|
39 //Takes a binary byte buffer and produces a lowercase hex string |
5ceb316c479a
Allow games to be specified in ROM DB via sha1 instead of product ID. Added a new ROM DB memory map device type fixed for emulating simple fixed value copy protection registers. Used those two features to support Ya Se Chuan Shuo via a ROM DB entry.
Michael Pavone <pavone@retrodev.com>
parents:
1295
diff
changeset
|
40 void bin_to_hex(uint8_t *output, uint8_t *input, uint64_t size); |
1527
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1523
diff
changeset
|
41 //Takes an (optionally) null-terminated UTF16-BE string and converts a maximum of max_size code-units to UTF-8 |
4f6e8acd7b6a
Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
1523
diff
changeset
|
42 char *utf16be_to_utf8(uint8_t *buf, uint32_t max_size); |
1572
5efeca06d942
Scale UI font size based on window size and start basing widget sizes based on font size
Michael Pavone <pavone@retrodev.com>
parents:
1540
diff
changeset
|
43 //Returns the next Unicode codepoint from a utf-8 string |
5efeca06d942
Scale UI font size based on window size and start basing widget sizes based on font size
Michael Pavone <pavone@retrodev.com>
parents:
1540
diff
changeset
|
44 int utf8_codepoint(const char **text); |
1008
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
45 //Determines whether a character is a valid path separator for the current platform |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
46 char is_path_sep(char c); |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
47 //Determines whether a path is considered an absolute path on the current platform |
51885857c019
Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
Michael Pavone <pavone@retrodev.com>
parents:
957
diff
changeset
|
48 char is_absolute_path(char *path); |
955
229c23b3ab73
Switch to storing SRAM/EEPROM and save states in a per-game directory rather than next to the ROM (for SRAM/EEPROM) or in the current working directory (for save states)
Michael Pavone <pavone@retrodev.com>
parents:
876
diff
changeset
|
49 //Returns the basename of a path with th extension (if any) stripped |
1581
7121daaa48c2
Fix drag and drop when using Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1572
diff
changeset
|
50 char * basename_no_extension(const char *path); |
1140
4490c9c12272
Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents:
1103
diff
changeset
|
51 //Returns the extension from a path or NULL if there is no extension |
1523
c416ace65ff1
Fix const correctness for path_extension
Michael Pavone <pavone@retrodev.com>
parents:
1485
diff
changeset
|
52 char *path_extension(char const *path); |
1485
369da70ee2c2
Filter file list in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1484
diff
changeset
|
53 //Returns true if the given path matches one of the extensions in the list |
2156
237068a25523
Added UI for setting firmware paths
Michael Pavone <pavone@retrodev.com>
parents:
2032
diff
changeset
|
54 uint8_t path_matches_extensions(char *path, const char **ext_list, uint32_t num_exts); |
1438
e2bd03ed3190
Allow reloading current ROM with a hotkey (default F5) and allow locking on a cartridge via menu
Michael Pavone <pavone@retrodev.com>
parents:
1305
diff
changeset
|
55 //Returns the directory portion of a path or NULL if there is no directory part |
1581
7121daaa48c2
Fix drag and drop when using Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1572
diff
changeset
|
56 char *path_dirname(const char *path); |
768
2f48a3c187c6
Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents:
742
diff
changeset
|
57 //Gets the smallest power of two that is >= a certain value, won't work for values > 0x80000000 |
2f48a3c187c6
Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents:
742
diff
changeset
|
58 uint32_t nearest_pow2(uint32_t val); |
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
|
59 //Should be called by main with the value of argv[0] for use by 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
|
60 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
|
61 //Returns the directory the executable is in |
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
|
62 char * get_exe_dir(); |
742
2e1b3b258523
Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents:
496
diff
changeset
|
63 //Returns the user's home directory |
2e1b3b258523
Make Windows port a little less half-assed
Michael Pavone <pavone@retrodev.com>
parents:
496
diff
changeset
|
64 char * get_home_dir(); |
875
54ffba3768d6
Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents:
866
diff
changeset
|
65 //Returns an appropriate path for storing config files |
876
540cc4a7d626
Fix Android build breakage
Michael Pavone <pavone@retrodev.com>
parents:
875
diff
changeset
|
66 char const *get_config_dir(); |
955
229c23b3ab73
Switch to storing SRAM/EEPROM and save states in a per-game directory rather than next to the ROM (for SRAM/EEPROM) or in the current working directory (for save states)
Michael Pavone <pavone@retrodev.com>
parents:
876
diff
changeset
|
67 //Returns an appropriate path for saving non-config data like savestates |
1295
96ad1b9bbb3a
Make save directory configurable. Satisfies ticket:4
Michael Pavone <pavone@retrodev.com>
parents:
1292
diff
changeset
|
68 char const *get_userdata_dir(); |
875
54ffba3768d6
Make menu stuff work on Android (theoretically)
Michael Pavone <pavone@retrodev.com>
parents:
866
diff
changeset
|
69 //Reads a file bundled with the executable |
1140
4490c9c12272
Detect system type from filename if header based methods fail. Allow overriding system type from command line.
Michael Pavone <pavone@retrodev.com>
parents:
1103
diff
changeset
|
70 char *read_bundled_file(char *name, uint32_t *sizeret); |
866
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
71 //Retunrs an array of normal files and directories residing in a directory |
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
72 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:
794
diff
changeset
|
73 //Frees a dir list returned by get_dir_list |
69a6ec208111
Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
794
diff
changeset
|
74 void free_dir_list(dir_entry *list, size_t numentries); |
1484
d82af64c94d2
Sort directory listing in Nuklear UI file browser
Michael Pavone <pavone@retrodev.com>
parents:
1438
diff
changeset
|
75 //Performs a case-insensitive sort by file name on a dir list |
d82af64c94d2
Sort directory listing in Nuklear UI file browser
Michael Pavone <pavone@retrodev.com>
parents:
1438
diff
changeset
|
76 void sort_dir_list(dir_entry *list, size_t num_entries); |
957 | 77 //Gets the modification time of a file |
78 time_t get_modification_time(char *path); | |
955
229c23b3ab73
Switch to storing SRAM/EEPROM and save states in a per-game directory rather than next to the ROM (for SRAM/EEPROM) or in the current working directory (for save states)
Michael Pavone <pavone@retrodev.com>
parents:
876
diff
changeset
|
79 //Recusrively creates a directory if it does not exist |
1540
e94cff9cb625
Make sure config directory exists before trying to save config file
Michael Pavone <pavone@retrodev.com>
parents:
1527
diff
changeset
|
80 int ensure_dir_exists(const char *path); |
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
|
81 //Returns the contents of a symlink in a newly allocated string |
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
|
82 char * readlink_alloc(char * path); |
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:
768
diff
changeset
|
83 //Prints an error message to stderr and to a message box if not in headless mode and then exits |
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:
768
diff
changeset
|
84 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:
768
diff
changeset
|
85 //Prints an information message to stdout and to a message box if not in headless mode and not attached to a console |
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:
768
diff
changeset
|
86 void info_message(char *format, ...); |
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
|
87 //Prints an information message to stderr and to a message box if not in headless mode and not attached to a console |
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
|
88 void warning(char *format, ...); |
1792
52a47611a273
Avoid printing a bunch of junk to stdout when GDB remote debugging is enabled as this can confuse GDB
Michael Pavone <pavone@retrodev.com>
parents:
1783
diff
changeset
|
89 //Prints a debug message to stdout |
52a47611a273
Avoid printing a bunch of junk to stdout when GDB remote debugging is enabled as this can confuse GDB
Michael Pavone <pavone@retrodev.com>
parents:
1783
diff
changeset
|
90 void debug_message(char *format, ...); |
52a47611a273
Avoid printing a bunch of junk to stdout when GDB remote debugging is enabled as this can confuse GDB
Michael Pavone <pavone@retrodev.com>
parents:
1783
diff
changeset
|
91 //Disables output of info and debug messages to stdout |
52a47611a273
Avoid printing a bunch of junk to stdout when GDB remote debugging is enabled as this can confuse GDB
Michael Pavone <pavone@retrodev.com>
parents:
1783
diff
changeset
|
92 void disable_stdout_messages(void); |
2032
441d5d6cea2f
Make KDEBUG functionality play nice with gdb remote debugging
Michael Pavone <pavone@retrodev.com>
parents:
1949
diff
changeset
|
93 //Returns stdout disable status |
441d5d6cea2f
Make KDEBUG functionality play nice with gdb remote debugging
Michael Pavone <pavone@retrodev.com>
parents:
1949
diff
changeset
|
94 uint8_t is_stdout_enabled(void); |
1852
a4cae960fd08
Allow config file to be saved with executable for "portable" setups
Michael Pavone <pavone@retrodev.com>
parents:
1792
diff
changeset
|
95 //Deletes a file, returns true on success, false on failure |
a4cae960fd08
Allow config file to be saved with executable for "portable" setups
Michael Pavone <pavone@retrodev.com>
parents:
1792
diff
changeset
|
96 uint8_t delete_file(char *path); |
1949
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
97 //Initializes the socket library on platforms that need it |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
98 void socket_init(void); |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
99 //Sets a sockt to blocking or non-blocking mode |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
100 int socket_blocking(int sock, int should_block); |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
101 //Close a socket |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
102 void socket_close(int sock); |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
103 //Return the last error on a socket operation |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
104 int socket_last_error(void); |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
105 //Returns if the last socket error was EAGAIN/EWOULDBLOCK |
5a76a7373823
Get WIP net play code compiling on Windows and cleanup some unistd.h includes
Michael Pavone <pavone@retrodev.com>
parents:
1852
diff
changeset
|
106 int socket_error_is_wouldblock(void); |
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
|
107 |
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
|
108 #endif //UTIL_H_ |