obs-nix.c 12 KB

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