obs-nix.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #ifdef ENABLE_WAYLAND
  20. #include "obs-nix-wayland.h"
  21. #endif
  22. #if defined(__FreeBSD__)
  23. #define _GNU_SOURCE
  24. #endif
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  29. #include <sys/sysctl.h>
  30. #endif
  31. #if !defined(__OpenBSD__)
  32. #include <sys/sysinfo.h>
  33. #endif
  34. #include <sys/utsname.h>
  35. #include <inttypes.h>
  36. const char *get_module_extension(void)
  37. {
  38. return ".so";
  39. }
  40. #ifdef __LP64__
  41. #define BIT_STRING "64bit"
  42. #else
  43. #define BIT_STRING "32bit"
  44. #endif
  45. #define FLATPAK_PLUGIN_PATH "/app/plugins"
  46. static const char *module_bin[] = {
  47. OBS_INSTALL_PREFIX "/" OBS_PLUGIN_DESTINATION,
  48. "../../obs-plugins/" BIT_STRING,
  49. FLATPAK_PLUGIN_PATH "/" OBS_PLUGIN_DESTINATION,
  50. };
  51. static const char *module_data[] = {
  52. OBS_INSTALL_DATA_PATH "/obs-plugins/%module%",
  53. OBS_DATA_PATH "/obs-plugins/%module%",
  54. FLATPAK_PLUGIN_PATH "/share/obs/obs-plugins/%module%"};
  55. static const int module_patterns_size =
  56. sizeof(module_bin) / sizeof(module_bin[0]);
  57. static const struct obs_nix_hotkeys_vtable *hotkeys_vtable = NULL;
  58. void add_default_module_paths(void)
  59. {
  60. for (int i = 0; i < module_patterns_size; i++)
  61. obs_add_module_path(module_bin[i], module_data[i]);
  62. }
  63. /*
  64. * /usr/local/share/libobs
  65. * /usr/share/libobs
  66. */
  67. char *find_libobs_data_file(const char *file)
  68. {
  69. struct dstr output;
  70. dstr_init(&output);
  71. if (check_path(file, OBS_DATA_PATH "/libobs/", &output))
  72. return output.array;
  73. if (OBS_INSTALL_PREFIX[0] != 0) {
  74. if (check_path(file, OBS_INSTALL_DATA_PATH "/libobs/", &output))
  75. return output.array;
  76. }
  77. dstr_free(&output);
  78. return NULL;
  79. }
  80. static void log_processor_cores(void)
  81. {
  82. blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d",
  83. os_get_physical_cores(), os_get_logical_cores());
  84. }
  85. #if defined(__linux__)
  86. static void log_processor_info(void)
  87. {
  88. int physical_id = -1;
  89. int last_physical_id = -1;
  90. char *line = NULL;
  91. size_t linecap = 0;
  92. FILE *fp;
  93. struct dstr proc_name;
  94. struct dstr proc_speed;
  95. fp = fopen("/proc/cpuinfo", "r");
  96. if (!fp)
  97. return;
  98. dstr_init(&proc_name);
  99. dstr_init(&proc_speed);
  100. while (getline(&line, &linecap, fp) != -1) {
  101. if (!strncmp(line, "model name", 10)) {
  102. char *start = strchr(line, ':');
  103. if (!start || *(++start) == '\0')
  104. continue;
  105. dstr_copy(&proc_name, start);
  106. dstr_resize(&proc_name, proc_name.len - 1);
  107. dstr_depad(&proc_name);
  108. }
  109. if (!strncmp(line, "physical id", 11)) {
  110. char *start = strchr(line, ':');
  111. if (!start || *(++start) == '\0')
  112. continue;
  113. physical_id = atoi(start);
  114. }
  115. if (!strncmp(line, "cpu MHz", 7)) {
  116. char *start = strchr(line, ':');
  117. if (!start || *(++start) == '\0')
  118. continue;
  119. dstr_copy(&proc_speed, start);
  120. dstr_resize(&proc_speed, proc_speed.len - 1);
  121. dstr_depad(&proc_speed);
  122. }
  123. if (*line == '\n' && physical_id != last_physical_id) {
  124. last_physical_id = physical_id;
  125. blog(LOG_INFO, "CPU Name: %s", proc_name.array);
  126. blog(LOG_INFO, "CPU Speed: %sMHz", proc_speed.array);
  127. }
  128. }
  129. fclose(fp);
  130. dstr_free(&proc_name);
  131. dstr_free(&proc_speed);
  132. free(line);
  133. }
  134. #elif defined(__FreeBSD__) || defined(__OpenBSD__)
  135. static void log_processor_speed(void)
  136. {
  137. #ifndef __OpenBSD__
  138. char *line = NULL;
  139. size_t linecap = 0;
  140. FILE *fp;
  141. struct dstr proc_speed;
  142. fp = fopen("/var/run/dmesg.boot", "r");
  143. if (!fp) {
  144. blog(LOG_INFO, "CPU: Missing /var/run/dmesg.boot !");
  145. return;
  146. }
  147. dstr_init(&proc_speed);
  148. while (getline(&line, &linecap, fp) != -1) {
  149. if (!strncmp(line, "CPU: ", 5)) {
  150. char *start = strrchr(line, '(');
  151. if (!start || *(++start) == '\0')
  152. continue;
  153. size_t len = strcspn(start, "-");
  154. dstr_ncopy(&proc_speed, start, len);
  155. }
  156. }
  157. blog(LOG_INFO, "CPU Speed: %sMHz", proc_speed.array);
  158. fclose(fp);
  159. dstr_free(&proc_speed);
  160. free(line);
  161. #endif
  162. }
  163. static void log_processor_name(void)
  164. {
  165. int mib[2];
  166. size_t len;
  167. char *proc;
  168. mib[0] = CTL_HW;
  169. mib[1] = HW_MODEL;
  170. sysctl(mib, 2, NULL, &len, NULL, 0);
  171. proc = bmalloc(len);
  172. if (!proc)
  173. return;
  174. sysctl(mib, 2, proc, &len, NULL, 0);
  175. blog(LOG_INFO, "CPU Name: %s", proc);
  176. bfree(proc);
  177. }
  178. static void log_processor_info(void)
  179. {
  180. log_processor_name();
  181. log_processor_speed();
  182. }
  183. #endif
  184. static void log_memory_info(void)
  185. {
  186. #if defined(__OpenBSD__)
  187. int mib[2];
  188. size_t len;
  189. int64_t mem;
  190. mib[0] = CTL_HW;
  191. mib[1] = HW_PHYSMEM64;
  192. len = sizeof(mem);
  193. if (sysctl(mib, 2, &mem, &len, NULL, 0) >= 0)
  194. blog(LOG_INFO, "Physical Memory: %" PRIi64 "MB Total",
  195. mem / 1024 / 1024);
  196. #else
  197. struct sysinfo info;
  198. if (sysinfo(&info) < 0)
  199. return;
  200. blog(LOG_INFO,
  201. "Physical Memory: %" PRIu64 "MB Total, %" PRIu64 "MB Free",
  202. (uint64_t)info.totalram * info.mem_unit / 1024 / 1024,
  203. ((uint64_t)info.freeram + (uint64_t)info.bufferram) *
  204. info.mem_unit / 1024 / 1024);
  205. #endif
  206. }
  207. static void log_kernel_version(void)
  208. {
  209. struct utsname info;
  210. if (uname(&info) < 0)
  211. return;
  212. blog(LOG_INFO, "Kernel Version: %s %s", info.sysname, info.release);
  213. }
  214. #if defined(__linux__) || defined(__FreeBSD__)
  215. static void log_distribution_info(void)
  216. {
  217. FILE *fp;
  218. char *line = NULL;
  219. size_t linecap = 0;
  220. struct dstr distro;
  221. struct dstr version;
  222. fp = fopen("/etc/os-release", "r");
  223. if (!fp) {
  224. blog(LOG_INFO, "Distribution: Missing /etc/os-release !");
  225. return;
  226. }
  227. dstr_init_copy(&distro, "Unknown");
  228. dstr_init_copy(&version, "Unknown");
  229. while (getline(&line, &linecap, fp) != -1) {
  230. if (!strncmp(line, "NAME", 4)) {
  231. char *start = strchr(line, '=');
  232. if (!start || *(++start) == '\0')
  233. continue;
  234. dstr_copy(&distro, start);
  235. dstr_resize(&distro, distro.len - 1);
  236. }
  237. if (!strncmp(line, "VERSION_ID", 10)) {
  238. char *start = strchr(line, '=');
  239. if (!start || *(++start) == '\0')
  240. continue;
  241. dstr_copy(&version, start);
  242. dstr_resize(&version, version.len - 1);
  243. }
  244. }
  245. blog(LOG_INFO, "Distribution: %s %s", distro.array, version.array);
  246. fclose(fp);
  247. dstr_free(&version);
  248. dstr_free(&distro);
  249. free(line);
  250. }
  251. static void log_desktop_session_info(void)
  252. {
  253. char *session_ptr = getenv("XDG_SESSION_TYPE");
  254. if (session_ptr) {
  255. blog(LOG_INFO, "Session Type: %s", session_ptr);
  256. }
  257. }
  258. #endif
  259. void log_system_info(void)
  260. {
  261. #if defined(__linux__) || defined(__FreeBSD__)
  262. log_processor_info();
  263. #endif
  264. log_processor_cores();
  265. log_memory_info();
  266. log_kernel_version();
  267. #if defined(__linux__) || defined(__FreeBSD__)
  268. log_distribution_info();
  269. log_desktop_session_info();
  270. #endif
  271. switch (obs_get_nix_platform()) {
  272. case OBS_NIX_PLATFORM_X11_EGL:
  273. obs_nix_x11_log_info();
  274. break;
  275. #ifdef ENABLE_WAYLAND
  276. case OBS_NIX_PLATFORM_WAYLAND:
  277. break;
  278. #endif
  279. }
  280. }
  281. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys)
  282. {
  283. switch (obs_get_nix_platform()) {
  284. case OBS_NIX_PLATFORM_X11_EGL:
  285. hotkeys_vtable = obs_nix_x11_get_hotkeys_vtable();
  286. break;
  287. #ifdef ENABLE_WAYLAND
  288. case OBS_NIX_PLATFORM_WAYLAND:
  289. hotkeys_vtable = obs_nix_wayland_get_hotkeys_vtable();
  290. break;
  291. #endif
  292. }
  293. return hotkeys_vtable->init(hotkeys);
  294. }
  295. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys)
  296. {
  297. hotkeys_vtable->free(hotkeys);
  298. hotkeys_vtable = NULL;
  299. }
  300. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  301. obs_key_t key)
  302. {
  303. return hotkeys_vtable->is_pressed(context, key);
  304. }
  305. void obs_key_to_str(obs_key_t key, struct dstr *dstr)
  306. {
  307. return hotkeys_vtable->key_to_str(key, dstr);
  308. }
  309. obs_key_t obs_key_from_virtual_key(int sym)
  310. {
  311. return hotkeys_vtable->key_from_virtual_key(sym);
  312. }
  313. int obs_key_to_virtual_key(obs_key_t key)
  314. {
  315. return hotkeys_vtable->key_to_virtual_key(key);
  316. }
  317. static inline void add_combo_key(obs_key_t key, struct dstr *str)
  318. {
  319. struct dstr key_str = {0};
  320. obs_key_to_str(key, &key_str);
  321. if (!dstr_is_empty(&key_str)) {
  322. if (!dstr_is_empty(str)) {
  323. dstr_cat(str, " + ");
  324. }
  325. dstr_cat_dstr(str, &key_str);
  326. }
  327. dstr_free(&key_str);
  328. }
  329. void obs_key_combination_to_str(obs_key_combination_t combination,
  330. struct dstr *str)
  331. {
  332. if ((combination.modifiers & INTERACT_CONTROL_KEY) != 0) {
  333. add_combo_key(OBS_KEY_CONTROL, str);
  334. }
  335. if ((combination.modifiers & INTERACT_COMMAND_KEY) != 0) {
  336. add_combo_key(OBS_KEY_META, str);
  337. }
  338. if ((combination.modifiers & INTERACT_ALT_KEY) != 0) {
  339. add_combo_key(OBS_KEY_ALT, str);
  340. }
  341. if ((combination.modifiers & INTERACT_SHIFT_KEY) != 0) {
  342. add_combo_key(OBS_KEY_SHIFT, str);
  343. }
  344. if (combination.key != OBS_KEY_NONE) {
  345. add_combo_key(combination.key, str);
  346. }
  347. }