1
0

obs-nix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. char *abs_module_install_path = os_get_abs_path_ptr(OBS_INSTALL_PREFIX "/" OBS_PLUGIN_DESTINATION);
  61. if (abs_module_bin_path &&
  62. (!abs_module_install_path || strcmp(abs_module_bin_path, abs_module_install_path) != 0)) {
  63. obs_add_module_path(module_bin_path, module_data_path);
  64. }
  65. bfree(abs_module_install_path);
  66. bfree(abs_module_bin_path);
  67. }
  68. bfree(module_bin_path);
  69. bfree(module_data_path);
  70. for (int i = 0; i < module_patterns_size; i++) {
  71. obs_add_module_path(module_bin[i], module_data[i]);
  72. }
  73. }
  74. /*
  75. * /usr/local/share/libobs
  76. * /usr/share/libobs
  77. */
  78. char *find_libobs_data_file(const char *file)
  79. {
  80. struct dstr output;
  81. dstr_init(&output);
  82. if (check_path(file, OBS_DATA_PATH "/libobs/", &output))
  83. return output.array;
  84. char *relative_data_path = os_get_executable_path_ptr("../" OBS_DATA_PATH "/libobs/");
  85. if (relative_data_path) {
  86. bool found = check_path(file, relative_data_path, &output);
  87. bfree(relative_data_path);
  88. if (found) {
  89. return output.array;
  90. }
  91. }
  92. if (OBS_INSTALL_PREFIX[0] != 0) {
  93. if (check_path(file, OBS_INSTALL_DATA_PATH "/libobs/", &output))
  94. return output.array;
  95. }
  96. dstr_free(&output);
  97. return NULL;
  98. }
  99. static void log_processor_cores(void)
  100. {
  101. blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d", os_get_physical_cores(), os_get_logical_cores());
  102. }
  103. #if defined(__linux__)
  104. static void log_processor_info(void)
  105. {
  106. int physical_id = -1;
  107. int last_physical_id = -1;
  108. char *line = NULL;
  109. size_t linecap = 0;
  110. FILE *fp;
  111. struct dstr proc_name;
  112. struct dstr proc_speed;
  113. fp = fopen("/proc/cpuinfo", "r");
  114. if (!fp)
  115. return;
  116. dstr_init(&proc_name);
  117. dstr_init(&proc_speed);
  118. while (getline(&line, &linecap, fp) != -1) {
  119. if (!strncmp(line, "model name", 10)) {
  120. char *start = strchr(line, ':');
  121. if (!start || *(++start) == '\0')
  122. continue;
  123. dstr_copy(&proc_name, start);
  124. dstr_resize(&proc_name, proc_name.len - 1);
  125. dstr_depad(&proc_name);
  126. }
  127. if (!strncmp(line, "physical id", 11)) {
  128. char *start = strchr(line, ':');
  129. if (!start || *(++start) == '\0')
  130. continue;
  131. physical_id = atoi(start);
  132. }
  133. if (!strncmp(line, "cpu MHz", 7)) {
  134. char *start = strchr(line, ':');
  135. if (!start || *(++start) == '\0')
  136. continue;
  137. dstr_copy(&proc_speed, start);
  138. dstr_resize(&proc_speed, proc_speed.len - 1);
  139. dstr_depad(&proc_speed);
  140. }
  141. if (*line == '\n' && physical_id != last_physical_id) {
  142. last_physical_id = physical_id;
  143. blog(LOG_INFO, "CPU Name: %s", proc_name.array);
  144. blog(LOG_INFO, "CPU Speed: %sMHz", proc_speed.array);
  145. }
  146. }
  147. fclose(fp);
  148. dstr_free(&proc_name);
  149. dstr_free(&proc_speed);
  150. free(line);
  151. }
  152. #elif defined(__FreeBSD__) || defined(__OpenBSD__)
  153. static void log_processor_speed(void)
  154. {
  155. #ifndef __OpenBSD__
  156. char *line = NULL;
  157. size_t linecap = 0;
  158. FILE *fp;
  159. struct dstr proc_speed;
  160. fp = fopen("/var/run/dmesg.boot", "r");
  161. if (!fp) {
  162. blog(LOG_INFO, "CPU: Missing /var/run/dmesg.boot !");
  163. return;
  164. }
  165. dstr_init(&proc_speed);
  166. while (getline(&line, &linecap, fp) != -1) {
  167. if (!strncmp(line, "CPU: ", 5)) {
  168. char *start = strrchr(line, '(');
  169. if (!start || *(++start) == '\0')
  170. continue;
  171. size_t len = strcspn(start, "-");
  172. dstr_ncopy(&proc_speed, start, len);
  173. }
  174. }
  175. blog(LOG_INFO, "CPU Speed: %sMHz", proc_speed.array);
  176. fclose(fp);
  177. dstr_free(&proc_speed);
  178. free(line);
  179. #endif
  180. }
  181. static void log_processor_name(void)
  182. {
  183. int mib[2];
  184. size_t len;
  185. char *proc;
  186. mib[0] = CTL_HW;
  187. mib[1] = HW_MODEL;
  188. sysctl(mib, 2, NULL, &len, NULL, 0);
  189. proc = bmalloc(len);
  190. if (!proc)
  191. return;
  192. sysctl(mib, 2, proc, &len, NULL, 0);
  193. blog(LOG_INFO, "CPU Name: %s", proc);
  194. bfree(proc);
  195. }
  196. static void log_processor_info(void)
  197. {
  198. log_processor_name();
  199. log_processor_speed();
  200. }
  201. #endif
  202. static void log_memory_info(void)
  203. {
  204. #if defined(__OpenBSD__)
  205. int mib[2];
  206. size_t len;
  207. int64_t mem;
  208. mib[0] = CTL_HW;
  209. mib[1] = HW_PHYSMEM64;
  210. len = sizeof(mem);
  211. if (sysctl(mib, 2, &mem, &len, NULL, 0) >= 0)
  212. blog(LOG_INFO, "Physical Memory: %" PRIi64 "MB Total", mem / 1024 / 1024);
  213. #else
  214. struct sysinfo info;
  215. if (sysinfo(&info) < 0)
  216. return;
  217. blog(LOG_INFO, "Physical Memory: %" PRIu64 "MB Total, %" PRIu64 "MB Free",
  218. (uint64_t)info.totalram * info.mem_unit / 1024 / 1024,
  219. ((uint64_t)info.freeram + (uint64_t)info.bufferram) * info.mem_unit / 1024 / 1024);
  220. #endif
  221. }
  222. static void log_kernel_version(void)
  223. {
  224. struct utsname info;
  225. if (uname(&info) < 0)
  226. return;
  227. blog(LOG_INFO, "Kernel Version: %s %s", info.sysname, info.release);
  228. }
  229. #if defined(__linux__) || defined(__FreeBSD__)
  230. static void log_distribution_info(void)
  231. {
  232. FILE *fp;
  233. char *line = NULL;
  234. size_t linecap = 0;
  235. struct dstr distro;
  236. struct dstr version;
  237. fp = fopen("/etc/os-release", "r");
  238. if (!fp) {
  239. blog(LOG_INFO, "Distribution: Missing /etc/os-release !");
  240. return;
  241. }
  242. dstr_init_copy(&distro, "Unknown");
  243. dstr_init_copy(&version, "Unknown");
  244. while (getline(&line, &linecap, fp) != -1) {
  245. if (!strncmp(line, "NAME", 4)) {
  246. char *start = strchr(line, '=');
  247. if (!start || *(++start) == '\0')
  248. continue;
  249. dstr_copy(&distro, start);
  250. dstr_resize(&distro, distro.len - 1);
  251. }
  252. if (!strncmp(line, "VERSION_ID", 10)) {
  253. char *start = strchr(line, '=');
  254. if (!start || *(++start) == '\0')
  255. continue;
  256. dstr_copy(&version, start);
  257. dstr_resize(&version, version.len - 1);
  258. }
  259. }
  260. blog(LOG_INFO, "Distribution: %s %s", distro.array, version.array);
  261. fclose(fp);
  262. dstr_free(&version);
  263. dstr_free(&distro);
  264. free(line);
  265. }
  266. static void log_flatpak_extensions(const char *extensions)
  267. {
  268. if (!extensions)
  269. return;
  270. char **exts_list = strlist_split(extensions, ';', false);
  271. for (char **ext = exts_list; *ext != NULL; ext++) {
  272. // Log the extension name without its commit hash
  273. char **name = strlist_split(*ext, '=', false);
  274. blog(LOG_INFO, " - %s", *name);
  275. strlist_free(name);
  276. }
  277. strlist_free(exts_list);
  278. }
  279. static void log_flatpak_info(void)
  280. {
  281. config_t *fp_info = NULL;
  282. if (config_open(&fp_info, "/.flatpak-info", CONFIG_OPEN_EXISTING) != CONFIG_SUCCESS) {
  283. blog(LOG_ERROR, "Unable to open .flatpak-info file");
  284. return;
  285. }
  286. const char *branch = config_get_string(fp_info, "Instance", "branch");
  287. const char *arch = config_get_string(fp_info, "Instance", "arch");
  288. const char *commit = config_get_string(fp_info, "Instance", "app-commit");
  289. const char *runtime = config_get_string(fp_info, "Application", "runtime");
  290. const char *app_exts = config_get_string(fp_info, "Instance", "app-extensions");
  291. const char *runtime_exts = config_get_string(fp_info, "Instance", "runtime-extensions");
  292. const char *fp_version = config_get_string(fp_info, "Instance", "flatpak-version");
  293. blog(LOG_INFO, "Flatpak Branch: %s", branch ? branch : "none");
  294. blog(LOG_INFO, "Flatpak Arch: %s", arch ? arch : "unknown");
  295. blog(LOG_INFO, "Flatpak Commit: %s", commit ? commit : "unknown");
  296. blog(LOG_INFO, "Flatpak Runtime: %s", runtime ? runtime : "none");
  297. if (app_exts) {
  298. blog(LOG_INFO, "App Extensions:");
  299. log_flatpak_extensions(app_exts);
  300. }
  301. if (runtime_exts) {
  302. blog(LOG_INFO, "Runtime Extensions:");
  303. log_flatpak_extensions(runtime_exts);
  304. }
  305. blog(LOG_INFO, "Flatpak Framework Version: %s", fp_version ? fp_version : "unknown");
  306. config_close(fp_info);
  307. }
  308. static void log_desktop_session_info(void)
  309. {
  310. char *current_desktop = getenv("XDG_CURRENT_DESKTOP");
  311. char *session_desktop = getenv("XDG_SESSION_DESKTOP");
  312. char *session_type = getenv("XDG_SESSION_TYPE");
  313. if (current_desktop && session_desktop)
  314. blog(LOG_INFO, "Desktop Environment: %s (%s)", current_desktop, session_desktop);
  315. else if (current_desktop || session_desktop)
  316. blog(LOG_INFO, "Desktop Environment: %s", current_desktop ? current_desktop : session_desktop);
  317. if (session_type)
  318. blog(LOG_INFO, "Session Type: %s", session_type);
  319. }
  320. #endif
  321. void log_system_info(void)
  322. {
  323. #if defined(__linux__) || defined(__FreeBSD__)
  324. log_processor_info();
  325. #endif
  326. log_processor_cores();
  327. log_memory_info();
  328. log_kernel_version();
  329. #if defined(__linux__) || defined(__FreeBSD__)
  330. if (access("/.flatpak-info", F_OK) == 0)
  331. log_flatpak_info();
  332. else
  333. log_distribution_info();
  334. log_desktop_session_info();
  335. #endif
  336. if (obs_get_nix_platform() == OBS_NIX_PLATFORM_X11_EGL)
  337. obs_nix_x11_log_info();
  338. }
  339. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys)
  340. {
  341. switch (obs_get_nix_platform()) {
  342. case OBS_NIX_PLATFORM_X11_EGL:
  343. hotkeys_vtable = obs_nix_x11_get_hotkeys_vtable();
  344. break;
  345. #ifdef ENABLE_WAYLAND
  346. case OBS_NIX_PLATFORM_WAYLAND:
  347. hotkeys_vtable = obs_nix_wayland_get_hotkeys_vtable();
  348. break;
  349. #endif
  350. default:
  351. break;
  352. }
  353. return hotkeys_vtable->init(hotkeys);
  354. }
  355. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys)
  356. {
  357. hotkeys_vtable->free(hotkeys);
  358. hotkeys_vtable = NULL;
  359. }
  360. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context, obs_key_t key)
  361. {
  362. return hotkeys_vtable->is_pressed(context, key);
  363. }
  364. void obs_key_to_str(obs_key_t key, struct dstr *dstr)
  365. {
  366. return hotkeys_vtable->key_to_str(key, dstr);
  367. }
  368. obs_key_t obs_key_from_virtual_key(int sym)
  369. {
  370. return hotkeys_vtable->key_from_virtual_key(sym);
  371. }
  372. int obs_key_to_virtual_key(obs_key_t key)
  373. {
  374. return hotkeys_vtable->key_to_virtual_key(key);
  375. }
  376. static inline void add_combo_key(obs_key_t key, struct dstr *str)
  377. {
  378. struct dstr key_str = {0};
  379. obs_key_to_str(key, &key_str);
  380. if (!dstr_is_empty(&key_str)) {
  381. if (!dstr_is_empty(str)) {
  382. dstr_cat(str, " + ");
  383. }
  384. dstr_cat_dstr(str, &key_str);
  385. }
  386. dstr_free(&key_str);
  387. }
  388. void obs_key_combination_to_str(obs_key_combination_t combination, struct dstr *str)
  389. {
  390. if ((combination.modifiers & INTERACT_CONTROL_KEY) != 0) {
  391. add_combo_key(OBS_KEY_CONTROL, str);
  392. }
  393. if ((combination.modifiers & INTERACT_COMMAND_KEY) != 0) {
  394. add_combo_key(OBS_KEY_META, str);
  395. }
  396. if ((combination.modifiers & INTERACT_ALT_KEY) != 0) {
  397. add_combo_key(OBS_KEY_ALT, str);
  398. }
  399. if ((combination.modifiers & INTERACT_SHIFT_KEY) != 0) {
  400. add_combo_key(OBS_KEY_SHIFT, str);
  401. }
  402. if (combination.key != OBS_KEY_NONE) {
  403. add_combo_key(combination.key, str);
  404. }
  405. }