obs-nix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Copyright (C) 2014 by Zachary Lund <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "obs-internal.h"
  16. #include "obs-nix.h"
  17. #include "obs-nix-platform.h"
  18. #include "obs-nix-x11.h"
  19. #include "util/config-file.h"
  20. #ifdef ENABLE_WAYLAND
  21. #include "obs-nix-wayland.h"
  22. #endif
  23. #if defined(__FreeBSD__)
  24. #define _GNU_SOURCE
  25. #endif
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  30. #include <sys/sysctl.h>
  31. #endif
  32. #if !defined(__OpenBSD__)
  33. #include <sys/sysinfo.h>
  34. #endif
  35. #include <sys/utsname.h>
  36. #include <inttypes.h>
  37. const char *get_module_extension(void)
  38. {
  39. return ".so";
  40. }
  41. #define FLATPAK_PLUGIN_PATH "/app/plugins"
  42. static const char *module_bin[] = {
  43. "../../obs-plugins/64bit",
  44. OBS_INSTALL_PREFIX "/" OBS_PLUGIN_DESTINATION,
  45. FLATPAK_PLUGIN_PATH "/" OBS_PLUGIN_DESTINATION,
  46. };
  47. static const char *module_data[] = {
  48. OBS_DATA_PATH "/obs-plugins/%module%",
  49. OBS_INSTALL_DATA_PATH "/obs-plugins/%module%",
  50. FLATPAK_PLUGIN_PATH "/share/obs/obs-plugins/%module%",
  51. };
  52. static const int module_patterns_size = sizeof(module_bin) / sizeof(module_bin[0]);
  53. static const struct obs_nix_hotkeys_vtable *hotkeys_vtable = NULL;
  54. void add_default_module_paths(void)
  55. {
  56. char *module_bin_path = os_get_executable_path_ptr("../" OBS_PLUGIN_PATH);
  57. char *module_data_path = os_get_executable_path_ptr("../" OBS_DATA_PATH "/obs-plugins/%module%");
  58. if (module_bin_path && module_data_path) {
  59. char *abs_module_bin_path = os_get_abs_path_ptr(module_bin_path);
  60. if (abs_module_bin_path &&
  61. strcmp(abs_module_bin_path, OBS_INSTALL_PREFIX "/" OBS_PLUGIN_DESTINATION) != 0) {
  62. obs_add_module_path(module_bin_path, module_data_path);
  63. }
  64. bfree(abs_module_bin_path);
  65. }
  66. bfree(module_bin_path);
  67. bfree(module_data_path);
  68. for (int i = 0; i < module_patterns_size; i++) {
  69. obs_add_module_path(module_bin[i], module_data[i]);
  70. }
  71. }
  72. /*
  73. * /usr/local/share/libobs
  74. * /usr/share/libobs
  75. */
  76. char *find_libobs_data_file(const char *file)
  77. {
  78. struct dstr output;
  79. dstr_init(&output);
  80. if (check_path(file, OBS_DATA_PATH "/libobs/", &output))
  81. return output.array;
  82. char *relative_data_path = os_get_executable_path_ptr("../" OBS_DATA_PATH "/libobs/");
  83. if (relative_data_path) {
  84. bool found = check_path(file, relative_data_path, &output);
  85. bfree(relative_data_path);
  86. if (found) {
  87. return output.array;
  88. }
  89. }
  90. if (OBS_INSTALL_PREFIX[0] != 0) {
  91. if (check_path(file, OBS_INSTALL_DATA_PATH "/libobs/", &output))
  92. return output.array;
  93. }
  94. dstr_free(&output);
  95. return NULL;
  96. }
  97. static void log_processor_cores(void)
  98. {
  99. blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d", os_get_physical_cores(), os_get_logical_cores());
  100. }
  101. #if defined(__linux__)
  102. static void log_processor_info(void)
  103. {
  104. int physical_id = -1;
  105. int last_physical_id = -1;
  106. char *line = NULL;
  107. size_t linecap = 0;
  108. FILE *fp;
  109. struct dstr proc_name;
  110. struct dstr proc_speed;
  111. fp = fopen("/proc/cpuinfo", "r");
  112. if (!fp)
  113. return;
  114. dstr_init(&proc_name);
  115. dstr_init(&proc_speed);
  116. while (getline(&line, &linecap, fp) != -1) {
  117. if (!strncmp(line, "model name", 10)) {
  118. char *start = strchr(line, ':');
  119. if (!start || *(++start) == '\0')
  120. continue;
  121. dstr_copy(&proc_name, start);
  122. dstr_resize(&proc_name, proc_name.len - 1);
  123. dstr_depad(&proc_name);
  124. }
  125. if (!strncmp(line, "physical id", 11)) {
  126. char *start = strchr(line, ':');
  127. if (!start || *(++start) == '\0')
  128. continue;
  129. physical_id = atoi(start);
  130. }
  131. if (!strncmp(line, "cpu MHz", 7)) {
  132. char *start = strchr(line, ':');
  133. if (!start || *(++start) == '\0')
  134. continue;
  135. dstr_copy(&proc_speed, start);
  136. dstr_resize(&proc_speed, proc_speed.len - 1);
  137. dstr_depad(&proc_speed);
  138. }
  139. if (*line == '\n' && physical_id != last_physical_id) {
  140. last_physical_id = physical_id;
  141. blog(LOG_INFO, "CPU Name: %s", proc_name.array);
  142. blog(LOG_INFO, "CPU Speed: %sMHz", proc_speed.array);
  143. }
  144. }
  145. fclose(fp);
  146. dstr_free(&proc_name);
  147. dstr_free(&proc_speed);
  148. free(line);
  149. }
  150. #elif defined(__FreeBSD__) || defined(__OpenBSD__)
  151. static void log_processor_speed(void)
  152. {
  153. #ifndef __OpenBSD__
  154. char *line = NULL;
  155. size_t linecap = 0;
  156. FILE *fp;
  157. struct dstr proc_speed;
  158. fp = fopen("/var/run/dmesg.boot", "r");
  159. if (!fp) {
  160. blog(LOG_INFO, "CPU: Missing /var/run/dmesg.boot !");
  161. return;
  162. }
  163. dstr_init(&proc_speed);
  164. while (getline(&line, &linecap, fp) != -1) {
  165. if (!strncmp(line, "CPU: ", 5)) {
  166. char *start = strrchr(line, '(');
  167. if (!start || *(++start) == '\0')
  168. continue;
  169. size_t len = strcspn(start, "-");
  170. dstr_ncopy(&proc_speed, start, len);
  171. }
  172. }
  173. blog(LOG_INFO, "CPU Speed: %sMHz", proc_speed.array);
  174. fclose(fp);
  175. dstr_free(&proc_speed);
  176. free(line);
  177. #endif
  178. }
  179. static void log_processor_name(void)
  180. {
  181. int mib[2];
  182. size_t len;
  183. char *proc;
  184. mib[0] = CTL_HW;
  185. mib[1] = HW_MODEL;
  186. sysctl(mib, 2, NULL, &len, NULL, 0);
  187. proc = bmalloc(len);
  188. if (!proc)
  189. return;
  190. sysctl(mib, 2, proc, &len, NULL, 0);
  191. blog(LOG_INFO, "CPU Name: %s", proc);
  192. bfree(proc);
  193. }
  194. static void log_processor_info(void)
  195. {
  196. log_processor_name();
  197. log_processor_speed();
  198. }
  199. #endif
  200. static void log_memory_info(void)
  201. {
  202. #if defined(__OpenBSD__)
  203. int mib[2];
  204. size_t len;
  205. int64_t mem;
  206. mib[0] = CTL_HW;
  207. mib[1] = HW_PHYSMEM64;
  208. len = sizeof(mem);
  209. if (sysctl(mib, 2, &mem, &len, NULL, 0) >= 0)
  210. blog(LOG_INFO, "Physical Memory: %" PRIi64 "MB Total", mem / 1024 / 1024);
  211. #else
  212. struct sysinfo info;
  213. if (sysinfo(&info) < 0)
  214. return;
  215. blog(LOG_INFO, "Physical Memory: %" PRIu64 "MB Total, %" PRIu64 "MB Free",
  216. (uint64_t)info.totalram * info.mem_unit / 1024 / 1024,
  217. ((uint64_t)info.freeram + (uint64_t)info.bufferram) * info.mem_unit / 1024 / 1024);
  218. #endif
  219. }
  220. static void log_kernel_version(void)
  221. {
  222. struct utsname info;
  223. if (uname(&info) < 0)
  224. return;
  225. blog(LOG_INFO, "Kernel Version: %s %s", info.sysname, info.release);
  226. }
  227. #if defined(__linux__) || defined(__FreeBSD__)
  228. static void log_distribution_info(void)
  229. {
  230. FILE *fp;
  231. char *line = NULL;
  232. size_t linecap = 0;
  233. struct dstr distro;
  234. struct dstr version;
  235. fp = fopen("/etc/os-release", "r");
  236. if (!fp) {
  237. blog(LOG_INFO, "Distribution: Missing /etc/os-release !");
  238. return;
  239. }
  240. dstr_init_copy(&distro, "Unknown");
  241. dstr_init_copy(&version, "Unknown");
  242. while (getline(&line, &linecap, fp) != -1) {
  243. if (!strncmp(line, "NAME", 4)) {
  244. char *start = strchr(line, '=');
  245. if (!start || *(++start) == '\0')
  246. continue;
  247. dstr_copy(&distro, start);
  248. dstr_resize(&distro, distro.len - 1);
  249. }
  250. if (!strncmp(line, "VERSION_ID", 10)) {
  251. char *start = strchr(line, '=');
  252. if (!start || *(++start) == '\0')
  253. continue;
  254. dstr_copy(&version, start);
  255. dstr_resize(&version, version.len - 1);
  256. }
  257. }
  258. blog(LOG_INFO, "Distribution: %s %s", distro.array, version.array);
  259. fclose(fp);
  260. dstr_free(&version);
  261. dstr_free(&distro);
  262. free(line);
  263. }
  264. static void log_flatpak_extensions(const char *extensions)
  265. {
  266. if (!extensions)
  267. return;
  268. char **exts_list = strlist_split(extensions, ';', false);
  269. for (char **ext = exts_list; *ext != NULL; ext++) {
  270. // Log the extension name without its commit hash
  271. char **name = strlist_split(*ext, '=', false);
  272. blog(LOG_INFO, " - %s", *name);
  273. strlist_free(name);
  274. }
  275. strlist_free(exts_list);
  276. }
  277. static void log_flatpak_info(void)
  278. {
  279. config_t *fp_info = NULL;
  280. if (config_open(&fp_info, "/.flatpak-info", CONFIG_OPEN_EXISTING) != CONFIG_SUCCESS) {
  281. blog(LOG_ERROR, "Unable to open .flatpak-info file");
  282. return;
  283. }
  284. const char *branch = config_get_string(fp_info, "Instance", "branch");
  285. const char *arch = config_get_string(fp_info, "Instance", "arch");
  286. const char *runtime = config_get_string(fp_info, "Application", "runtime");
  287. const char *app_exts = config_get_string(fp_info, "Instance", "app-extensions");
  288. const char *runtime_exts = config_get_string(fp_info, "Instance", "runtime-extensions");
  289. const char *fp_version = config_get_string(fp_info, "Instance", "flatpak-version");
  290. blog(LOG_INFO, "Flatpak Branch: %s", branch ? branch : "none");
  291. blog(LOG_INFO, "Flatpak Arch: %s", arch ? arch : "unknown");
  292. blog(LOG_INFO, "Flatpak Runtime: %s", runtime ? runtime : "none");
  293. if (app_exts) {
  294. blog(LOG_INFO, "App Extensions:");
  295. log_flatpak_extensions(app_exts);
  296. }
  297. if (runtime_exts) {
  298. blog(LOG_INFO, "Runtime Extensions:");
  299. log_flatpak_extensions(runtime_exts);
  300. }
  301. blog(LOG_INFO, "Flatpak Framework Version: %s", fp_version ? fp_version : "unknown");
  302. config_close(fp_info);
  303. }
  304. static void log_desktop_session_info(void)
  305. {
  306. char *current_desktop = getenv("XDG_CURRENT_DESKTOP");
  307. char *session_desktop = getenv("XDG_SESSION_DESKTOP");
  308. char *session_type = getenv("XDG_SESSION_TYPE");
  309. if (current_desktop && session_desktop)
  310. blog(LOG_INFO, "Desktop Environment: %s (%s)", current_desktop, session_desktop);
  311. else if (current_desktop || session_desktop)
  312. blog(LOG_INFO, "Desktop Environment: %s", current_desktop ? current_desktop : session_desktop);
  313. if (session_type)
  314. blog(LOG_INFO, "Session Type: %s", session_type);
  315. }
  316. #endif
  317. void log_system_info(void)
  318. {
  319. #if defined(__linux__) || defined(__FreeBSD__)
  320. log_processor_info();
  321. #endif
  322. log_processor_cores();
  323. log_memory_info();
  324. log_kernel_version();
  325. #if defined(__linux__) || defined(__FreeBSD__)
  326. if (access("/.flatpak-info", F_OK) == 0)
  327. log_flatpak_info();
  328. else
  329. log_distribution_info();
  330. log_desktop_session_info();
  331. #endif
  332. if (obs_get_nix_platform() == OBS_NIX_PLATFORM_X11_EGL)
  333. obs_nix_x11_log_info();
  334. }
  335. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys)
  336. {
  337. switch (obs_get_nix_platform()) {
  338. case OBS_NIX_PLATFORM_X11_EGL:
  339. hotkeys_vtable = obs_nix_x11_get_hotkeys_vtable();
  340. break;
  341. #ifdef ENABLE_WAYLAND
  342. case OBS_NIX_PLATFORM_WAYLAND:
  343. hotkeys_vtable = obs_nix_wayland_get_hotkeys_vtable();
  344. break;
  345. #endif
  346. default:
  347. break;
  348. }
  349. return hotkeys_vtable->init(hotkeys);
  350. }
  351. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys)
  352. {
  353. hotkeys_vtable->free(hotkeys);
  354. hotkeys_vtable = NULL;
  355. }
  356. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context, obs_key_t key)
  357. {
  358. return hotkeys_vtable->is_pressed(context, key);
  359. }
  360. void obs_key_to_str(obs_key_t key, struct dstr *dstr)
  361. {
  362. return hotkeys_vtable->key_to_str(key, dstr);
  363. }
  364. obs_key_t obs_key_from_virtual_key(int sym)
  365. {
  366. return hotkeys_vtable->key_from_virtual_key(sym);
  367. }
  368. int obs_key_to_virtual_key(obs_key_t key)
  369. {
  370. return hotkeys_vtable->key_to_virtual_key(key);
  371. }
  372. static inline void add_combo_key(obs_key_t key, struct dstr *str)
  373. {
  374. struct dstr key_str = {0};
  375. obs_key_to_str(key, &key_str);
  376. if (!dstr_is_empty(&key_str)) {
  377. if (!dstr_is_empty(str)) {
  378. dstr_cat(str, " + ");
  379. }
  380. dstr_cat_dstr(str, &key_str);
  381. }
  382. dstr_free(&key_str);
  383. }
  384. void obs_key_combination_to_str(obs_key_combination_t combination, struct dstr *str)
  385. {
  386. if ((combination.modifiers & INTERACT_CONTROL_KEY) != 0) {
  387. add_combo_key(OBS_KEY_CONTROL, str);
  388. }
  389. if ((combination.modifiers & INTERACT_COMMAND_KEY) != 0) {
  390. add_combo_key(OBS_KEY_META, str);
  391. }
  392. if ((combination.modifiers & INTERACT_ALT_KEY) != 0) {
  393. add_combo_key(OBS_KEY_ALT, str);
  394. }
  395. if ((combination.modifiers & INTERACT_SHIFT_KEY) != 0) {
  396. add_combo_key(OBS_KEY_SHIFT, str);
  397. }
  398. if (combination.key != OBS_KEY_NONE) {
  399. add_combo_key(combination.key, str);
  400. }
  401. }