obs-nix.c 12 KB

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