obs-nix.c 11 KB

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