obs-nix.c 9.1 KB

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