1
0

platform.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include <stdio.h>
  18. #include <wchar.h>
  19. #include <sys/types.h>
  20. #include "c99defs.h"
  21. /*
  22. * Platform-independent functions for Accessing files, encoding, DLLs,
  23. * sleep, timer, and timing.
  24. */
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. EXPORT FILE *os_wfopen(const wchar_t *path, const char *mode);
  29. EXPORT FILE *os_fopen(const char *path, const char *mode);
  30. EXPORT int64_t os_fgetsize(FILE *file);
  31. #ifdef _WIN32
  32. EXPORT int os_stat(const char *file, struct stat *st);
  33. #else
  34. #define os_stat stat
  35. #endif
  36. EXPORT int os_fseeki64(FILE *file, int64_t offset, int origin);
  37. EXPORT int64_t os_ftelli64(FILE *file);
  38. EXPORT size_t os_fread_mbs(FILE *file, char **pstr);
  39. EXPORT size_t os_fread_utf8(FILE *file, char **pstr);
  40. /* functions purely for convenience */
  41. EXPORT char *os_quick_read_utf8_file(const char *path);
  42. EXPORT bool os_quick_write_utf8_file(const char *path, const char *str, size_t len, bool marker);
  43. EXPORT bool os_quick_write_utf8_file_safe(const char *path, const char *str, size_t len, bool marker,
  44. const char *temp_ext, const char *backup_ext);
  45. EXPORT char *os_quick_read_mbs_file(const char *path);
  46. EXPORT bool os_quick_write_mbs_file(const char *path, const char *str, size_t len);
  47. EXPORT int64_t os_get_file_size(const char *path);
  48. EXPORT int64_t os_get_free_space(const char *path);
  49. EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst, size_t dst_size);
  50. EXPORT size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size);
  51. EXPORT size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size);
  52. EXPORT size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst, size_t dst_size);
  53. EXPORT size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
  54. EXPORT size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
  55. EXPORT size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr);
  56. EXPORT size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr);
  57. EXPORT size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr);
  58. EXPORT size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr);
  59. EXPORT double os_strtod(const char *str);
  60. EXPORT int os_dtostr(double value, char *dst, size_t size);
  61. EXPORT void *os_dlopen(const char *path);
  62. EXPORT void *os_dlsym(void *module, const char *func);
  63. EXPORT void os_dlclose(void *module);
  64. EXPORT bool os_is_obs_plugin(const char *path);
  65. struct os_cpu_usage_info;
  66. typedef struct os_cpu_usage_info os_cpu_usage_info_t;
  67. EXPORT os_cpu_usage_info_t *os_cpu_usage_info_start(void);
  68. EXPORT double os_cpu_usage_info_query(os_cpu_usage_info_t *info);
  69. EXPORT void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info);
  70. typedef const void os_performance_token_t;
  71. EXPORT os_performance_token_t *os_request_high_performance(const char *reason);
  72. EXPORT void os_end_high_performance(os_performance_token_t *);
  73. /**
  74. * Sleeps to a specific time (in nanoseconds). Doesn't have to be super
  75. * accurate in terms of actual slept time because the target time is ensured.
  76. * Returns false if already at or past target time.
  77. */
  78. EXPORT bool os_sleepto_ns(uint64_t time_target);
  79. EXPORT bool os_sleepto_ns_fast(uint64_t time_target);
  80. EXPORT void os_sleep_ms(uint32_t duration);
  81. EXPORT uint64_t os_gettime_ns(void);
  82. EXPORT int os_get_config_path(char *dst, size_t size, const char *name);
  83. EXPORT char *os_get_config_path_ptr(const char *name);
  84. EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name);
  85. EXPORT char *os_get_program_data_path_ptr(const char *name);
  86. EXPORT char *os_get_executable_path_ptr(const char *name);
  87. EXPORT bool os_file_exists(const char *path);
  88. EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);
  89. EXPORT char *os_get_abs_path_ptr(const char *path);
  90. EXPORT const char *os_get_path_extension(const char *path);
  91. EXPORT bool os_get_emulation_status(void);
  92. struct os_dir;
  93. typedef struct os_dir os_dir_t;
  94. struct os_dirent {
  95. char d_name[256];
  96. bool directory;
  97. };
  98. EXPORT os_dir_t *os_opendir(const char *path);
  99. EXPORT struct os_dirent *os_readdir(os_dir_t *dir);
  100. EXPORT void os_closedir(os_dir_t *dir);
  101. struct os_globent {
  102. char *path;
  103. bool directory;
  104. };
  105. struct os_glob_info {
  106. size_t gl_pathc;
  107. struct os_globent *gl_pathv;
  108. };
  109. typedef struct os_glob_info os_glob_t;
  110. /* currently no flags available */
  111. EXPORT int os_glob(const char *pattern, int flags, os_glob_t **pglob);
  112. EXPORT void os_globfree(os_glob_t *pglob);
  113. EXPORT int os_unlink(const char *path);
  114. EXPORT int os_rmdir(const char *path);
  115. EXPORT char *os_getcwd(char *path, size_t size);
  116. EXPORT int os_chdir(const char *path);
  117. EXPORT uint64_t os_get_free_disk_space(const char *dir);
  118. #define MKDIR_EXISTS 1
  119. #define MKDIR_SUCCESS 0
  120. #define MKDIR_ERROR -1
  121. EXPORT int os_mkdir(const char *path);
  122. EXPORT int os_mkdirs(const char *path);
  123. EXPORT int os_rename(const char *old_path, const char *new_path);
  124. EXPORT int os_copyfile(const char *file_in, const char *file_out);
  125. EXPORT int os_safe_replace(const char *target_path, const char *from_path, const char *backup_path);
  126. EXPORT char *os_generate_formatted_filename(const char *extension, bool space, const char *format);
  127. struct os_inhibit_info;
  128. typedef struct os_inhibit_info os_inhibit_t;
  129. EXPORT os_inhibit_t *os_inhibit_sleep_create(const char *reason);
  130. EXPORT bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active);
  131. EXPORT void os_inhibit_sleep_destroy(os_inhibit_t *info);
  132. EXPORT void os_breakpoint(void);
  133. EXPORT void os_oom(void);
  134. EXPORT int os_get_physical_cores(void);
  135. EXPORT int os_get_logical_cores(void);
  136. EXPORT uint64_t os_get_sys_free_size(void);
  137. EXPORT uint64_t os_get_sys_total_size(void);
  138. struct os_proc_memory_usage {
  139. uint64_t resident_size;
  140. uint64_t virtual_size;
  141. };
  142. typedef struct os_proc_memory_usage os_proc_memory_usage_t;
  143. EXPORT bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage);
  144. EXPORT uint64_t os_get_proc_resident_size(void);
  145. EXPORT uint64_t os_get_proc_virtual_size(void);
  146. #define UUID_STR_LENGTH 36
  147. EXPORT char *os_generate_uuid(void);
  148. EXPORT
  149. struct timespec *os_nstime_to_timespec(uint64_t timestamp, struct timespec *storage);
  150. /* clang-format off */
  151. #ifdef __APPLE__
  152. # define ARCH_BITS 64
  153. #else
  154. # ifdef _WIN32
  155. # ifdef _WIN64
  156. # define ARCH_BITS 64
  157. # else
  158. # define ARCH_BITS 32
  159. # endif
  160. # else
  161. # ifdef __LP64__
  162. # define ARCH_BITS 64
  163. # else
  164. # define ARCH_BITS 32
  165. # endif
  166. # endif
  167. #endif
  168. /* clang-format on */
  169. #ifdef __cplusplus
  170. }
  171. #endif