reference-libobs-util-platform.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. Platform Helpers
  2. ================
  3. These functions/structures/types are used to perform actions that
  4. typically don't have shared functions across different operating systems
  5. and platforms.
  6. .. code:: cpp
  7. #include <util/platform.h>
  8. File Functions
  9. --------------
  10. .. function:: FILE *os_wfopen(const wchar_t *path, const char *mode)
  11. Opens a file with a wide string path.
  12. ----------------------
  13. .. function:: FILE *os_fopen(const char *path, const char *mode)
  14. Opens a file with a UTF8 string path.
  15. ----------------------
  16. .. function:: int64_t os_fgetsize(FILE *file)
  17. Returns a file's size.
  18. ----------------------
  19. .. function:: int os_stat(const char *file, struct stat *st)
  20. Equivalent to the posix *stat* function.
  21. ----------------------
  22. .. function:: int os_fseeki64(FILE *file, int64_t offset, int origin)
  23. Equivalent to fseek.
  24. ----------------------
  25. .. function:: int64_t os_ftelli64(FILE *file)
  26. Gets the current file position.
  27. ----------------------
  28. .. function:: size_t os_fread_utf8(FILE *file, char **pstr)
  29. Reads a UTF8 encoded file and allocates a pointer to the UTF8 string.
  30. ----------------------
  31. .. function:: char *os_quick_read_utf8_file(const char *path)
  32. Reads a UTF8 encoded file and returns an allocated pointer to the
  33. string.
  34. ----------------------
  35. .. function:: bool os_quick_write_utf8_file(const char *path, const char *str, size_t len, bool marker)
  36. Writes a UTF8 encoded file.
  37. ----------------------
  38. .. function:: bool os_quick_write_utf8_file_safe(const char *path, const char *str, size_t len, bool marker, const char *temp_ext, const char *backup_ext)
  39. Writes a UTF8 encoded file with overwrite corruption prevention.
  40. ----------------------
  41. .. function:: int64_t os_get_file_size(const char *path)
  42. Gets a file's size.
  43. ----------------------
  44. .. function:: int64_t os_get_free_space(const char *path)
  45. Gets free space of a specific file path.
  46. ---------------------
  47. String Conversion Functions
  48. ---------------------------
  49. .. function:: size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
  50. Converts a UTF8 string to a wide string.
  51. ----------------------
  52. .. function:: size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst, size_t dst_size)
  53. Converts a wide string to a UTF8 string.
  54. ----------------------
  55. .. function:: size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  56. Gets an bmalloc-allocated wide string converted from a UTF8 string.
  57. ----------------------
  58. .. function:: size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  59. Gets an bmalloc-allocated UTF8 string converted from a wide string.
  60. ---------------------
  61. Number/String Conversion Functions
  62. ----------------------------------
  63. .. function:: double os_strtod(const char *str)
  64. Converts a string to a double.
  65. ----------------------
  66. .. function:: int os_dtostr(double value, char *dst, size_t size)
  67. Converts a double to a string.
  68. ---------------------
  69. Dynamic Link Library Functions
  70. ------------------------------
  71. These functions are roughly equivalent to dlopen/dlsym/dlclose.
  72. .. function:: void *os_dlopen(const char *path)
  73. Opens a dynamic library.
  74. ----------------------
  75. .. function:: void *os_dlsym(void *module, const char *func)
  76. Returns a symbol from a dynamic library.
  77. ----------------------
  78. .. function:: void os_dlclose(void *module)
  79. Closes a dynamic library.
  80. ---------------------
  81. .. function:: bool os_is_obs_plugin(const char *path)
  82. Returns true if the path is a dynamic library that looks like an OBS plugin.
  83. Currently only needed on Windows for performance reasons.
  84. ---------------------
  85. CPU Usage Functions
  86. -------------------
  87. .. function:: os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  88. Creates a CPU usage information object.
  89. ----------------------
  90. .. function:: double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  91. Queries the current CPU usage.
  92. ----------------------
  93. .. function:: void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  94. Destroys a CPU usage information object.
  95. ---------------------
  96. Sleep/Time Functions
  97. --------------------
  98. .. function:: bool os_sleepto_ns(uint64_t time_target)
  99. Sleeps to a specific time with high precision, in nanoseconds.
  100. ---------------------
  101. .. function:: void os_sleep_ms(uint32_t duration)
  102. Sleeps for a specific number of milliseconds.
  103. ---------------------
  104. .. function:: uint64_t os_gettime_ns(void)
  105. Gets the current high-precision system time, in nanoseconds.
  106. ---------------------
  107. Other Path/File Functions
  108. -------------------------
  109. .. function:: int os_get_config_path(char *dst, size_t size, const char *name)
  110. char *os_get_config_path_ptr(const char *name)
  111. Gets the user-specific application configuration data path.
  112. ---------------------
  113. .. function:: int os_get_program_data_path(char *dst, size_t size, const char *name)
  114. char *os_get_program_data_path_ptr(const char *name)
  115. Gets the application configuration data path.
  116. ---------------------
  117. .. function:: bool os_file_exists(const char *path)
  118. Returns true if a file/directory exists, false otherwise.
  119. ---------------------
  120. .. function:: size_t os_get_abs_path(const char *path, char *abspath, size_t size)
  121. char *os_get_abs_path_ptr(const char *path)
  122. Converts a relative path to an absolute path.
  123. ---------------------
  124. .. function:: const char *os_get_path_extension(const char *path)
  125. Returns the extension portion of a path string.
  126. ---------------------
  127. .. type:: typedef struct os_dir os_dir_t
  128. A directory object.
  129. .. type:: struct os_dirent
  130. A directory entry record.
  131. .. member:: char os_dirent.d_name[256]
  132. The directory entry name.
  133. .. member:: bool os_dirent.directory
  134. *true* if the entry is a directory.
  135. ---------------------
  136. .. function:: os_dir_t *os_opendir(const char *path)
  137. Opens a directory object to enumerate files within the directory.
  138. ---------------------
  139. .. function:: struct os_dirent *os_readdir(os_dir_t *dir)
  140. Returns the linked list of directory entries.
  141. ---------------------
  142. .. function:: void os_closedir(os_dir_t *dir)
  143. Closes a directory object.
  144. ---------------------
  145. .. type:: struct os_globent
  146. A glob entry.
  147. .. member:: char *os_globent.path
  148. The full path to the glob entry.
  149. .. member:: bool os_globent.directory
  150. *true* if the glob entry is a directory, *false* otherwise.
  151. .. type:: struct os_glob_info
  152. A glob object.
  153. .. member:: size_t os_glob_info.gl_pathc
  154. Number of glob entries.
  155. .. member:: struct os_globent *os_glob_info.gl_pathv
  156. Array of glob entries.
  157. .. type:: typedef struct os_glob_info os_glob_t
  158. ---------------------
  159. .. function:: int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  160. Enumerates files based upon a glob string.
  161. ---------------------
  162. .. function:: void os_globfree(os_glob_t *pglob)
  163. Frees a glob object.
  164. ---------------------
  165. .. function:: int os_unlink(const char *path)
  166. Deletes a file.
  167. ---------------------
  168. .. function:: int os_rmdir(const char *path)
  169. Deletes a directory.
  170. ---------------------
  171. .. function:: char *os_getcwd(char *path, size_t size)
  172. Returns a new bmalloc-allocated path to the current working
  173. directory.
  174. ---------------------
  175. .. function:: int os_chdir(const char *path)
  176. Changes the current working directory.
  177. ---------------------
  178. .. function:: int os_mkdir(const char *path)
  179. Creates a directory.
  180. ---------------------
  181. .. function:: int os_mkdirs(const char *path)
  182. Creates a full directory path if it doesn't exist.
  183. ---------------------
  184. .. function:: int os_rename(const char *old_path, const char *new_path)
  185. Renames a file.
  186. ---------------------
  187. .. function:: int os_copyfile(const char *file_in, const char *file_out)
  188. Copies a file.
  189. ---------------------
  190. .. function:: int os_safe_replace(const char *target_path, const char *from_path, const char *backup_path)
  191. Safely replaces a file.
  192. ---------------------
  193. .. function:: char *os_generate_formatted_filename(const char *extension, bool space, const char *format)
  194. Returns a new bmalloc-allocated filename generated from specific
  195. formatting.
  196. ---------------------
  197. Sleep-Inhibition Functions
  198. --------------------------
  199. These functions/types are used to inhibit the computer from going to
  200. sleep.
  201. .. type:: struct os_inhibit_info
  202. .. type:: typedef struct os_inhibit_info os_inhibit_t
  203. ---------------------
  204. .. function:: os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  205. Creates a sleep inhibition object.
  206. ---------------------
  207. .. function:: bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  208. Activates/deactivates a sleep inhibition object.
  209. ---------------------
  210. .. function:: void os_inhibit_sleep_destroy(os_inhibit_t *info)
  211. Destroys a sleep inhibition object. If the sleep inhibition object
  212. was active, it will be deactivated.
  213. ---------------------
  214. Other Functions
  215. ---------------
  216. .. function:: void os_breakpoint(void)
  217. Triggers a debugger breakpoint (or crashes the program if no debugger
  218. present).
  219. ---------------------
  220. .. function:: int os_get_physical_cores(void)
  221. Returns the number of physical cores available.
  222. ---------------------
  223. .. function:: int os_get_logical_cores(void)
  224. Returns the number of logical cores available.
  225. ---------------------
  226. .. function:: uint64_t os_get_sys_free_size(void)
  227. Returns the amount of memory available.
  228. ---------------------
  229. .. type:: struct os_proc_memory_usage
  230. Memory usage structure.
  231. .. member:: uint64_t os_proc_memory_usage.resident_size
  232. Resident size.
  233. .. member:: uint64_t os_proc_memory_usage.virtual_size
  234. Virtual size.
  235. .. type:: typedef struct os_proc_memory_usage os_proc_memory_usage_t
  236. ---------------------
  237. .. function:: bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
  238. Gets memory usage of the current process.
  239. ---------------------
  240. .. function:: uint64_t os_get_proc_resident_size(void)
  241. Returns the resident memory size of the current process.
  242. ---------------------
  243. .. function:: uint64_t os_get_proc_virtual_size(void)
  244. Returns the virtual memory size of the current process.