platform-x11.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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-config.h>
  16. #include "obs-app.hpp"
  17. #include <QGuiApplication>
  18. #include <QScreen>
  19. #include <unistd.h>
  20. #include <sstream>
  21. #include <locale.h>
  22. #include "platform.hpp"
  23. #ifdef __linux__
  24. #include <sys/socket.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <stdio.h>
  28. #include <sys/un.h>
  29. #endif
  30. #if defined(__FreeBSD__) || defined(__DragonFly__)
  31. #include <sys/param.h>
  32. #include <fcntl.h>
  33. #include <sys/sysctl.h>
  34. #include <sys/user.h>
  35. #include <libprocstat.h>
  36. #include <pthread_np.h>
  37. #include <condition_variable>
  38. #include <mutex>
  39. #include <thread>
  40. #endif
  41. using std::string;
  42. using std::vector;
  43. using std::ostringstream;
  44. #ifdef __linux__
  45. void CheckIfAlreadyRunning(bool &already_running)
  46. {
  47. int uniq = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  48. if (uniq == -1) {
  49. blog(LOG_ERROR,
  50. "Failed to check for running instance, socket: %d", errno);
  51. already_running = 0;
  52. return;
  53. }
  54. struct sockaddr_un bindInfo;
  55. memset(&bindInfo, 0, sizeof(sockaddr_un));
  56. bindInfo.sun_family = AF_LOCAL;
  57. auto bindInfoStrlen = snprintf(bindInfo.sun_path + 1,
  58. sizeof(bindInfo.sun_path) - 1,
  59. "%s %d %s", "/com/obsproject", getpid(),
  60. App()->GetVersionString().c_str());
  61. int bindErr = bind(uniq, (struct sockaddr *)&bindInfo,
  62. sizeof(sa_family_t) + 1 + bindInfoStrlen);
  63. already_running = bindErr == 0 ? 0 : 1;
  64. if (already_running) {
  65. return;
  66. }
  67. FILE *fp = fopen("/proc/net/unix", "re");
  68. if (fp == NULL) {
  69. return;
  70. }
  71. char *line = NULL;
  72. size_t n = 0;
  73. int obsCnt = 0;
  74. while (getdelim(&line, &n, ' ', fp) != EOF) {
  75. line[strcspn(line, "\n")] = '\0';
  76. if (*line == '@') {
  77. if (strstr(line, "@/com/obsproject") != NULL) {
  78. ++obsCnt;
  79. }
  80. }
  81. }
  82. already_running = obsCnt == 1 ? 0 : 1;
  83. free(line);
  84. fclose(fp);
  85. }
  86. #endif
  87. #if defined(__FreeBSD__) || defined(__DragonFly__)
  88. struct RunOnce {
  89. std::thread thr;
  90. static const char *thr_name;
  91. std::condition_variable cv;
  92. std::condition_variable wait_cv;
  93. std::mutex mtx;
  94. bool exiting = false;
  95. bool name_changed = false;
  96. void thr_proc()
  97. {
  98. std::unique_lock<std::mutex> lk(mtx);
  99. pthread_set_name_np(pthread_self(), thr_name);
  100. name_changed = true;
  101. wait_cv.notify_all();
  102. cv.wait(lk, [this]() { return exiting; });
  103. }
  104. ~RunOnce()
  105. {
  106. if (thr.joinable()) {
  107. std::unique_lock<std::mutex> lk(mtx);
  108. exiting = true;
  109. cv.notify_one();
  110. lk.unlock();
  111. thr.join();
  112. }
  113. }
  114. } RO;
  115. const char *RunOnce::thr_name = "OBS runonce";
  116. void CheckIfAlreadyRunning(bool &already_running)
  117. {
  118. std::string tmpfile_name =
  119. "/tmp/obs-studio.lock." + std::to_string(geteuid());
  120. int fd = open(tmpfile_name.c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0600);
  121. if (fd == -1) {
  122. already_running = true;
  123. return;
  124. }
  125. already_running = false;
  126. procstat *ps = procstat_open_sysctl();
  127. unsigned int count;
  128. auto procs = procstat_getprocs(ps, KERN_PROC_UID | KERN_PROC_INC_THREAD,
  129. geteuid(), &count);
  130. for (unsigned int i = 0; i < count; i++) {
  131. if (!strncmp(procs[i].ki_tdname, RunOnce::thr_name,
  132. sizeof(procs[i].ki_tdname))) {
  133. already_running = true;
  134. break;
  135. }
  136. }
  137. procstat_freeprocs(ps, procs);
  138. procstat_close(ps);
  139. RO.thr = std::thread(std::mem_fn(&RunOnce::thr_proc), &RO);
  140. {
  141. std::unique_lock<std::mutex> lk(RO.mtx);
  142. RO.wait_cv.wait(lk, []() { return RO.name_changed; });
  143. }
  144. unlink(tmpfile_name.c_str());
  145. close(fd);
  146. }
  147. #endif
  148. static inline bool check_path(const char *data, const char *path,
  149. string &output)
  150. {
  151. ostringstream str;
  152. str << path << data;
  153. output = str.str();
  154. blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
  155. return (access(output.c_str(), R_OK) == 0);
  156. }
  157. #define INSTALL_DATA_PATH OBS_INSTALL_PREFIX "/" OBS_DATA_PATH "/obs-studio/"
  158. bool GetDataFilePath(const char *data, string &output)
  159. {
  160. char *data_path = getenv("OBS_DATA_PATH");
  161. if (data_path != NULL) {
  162. if (check_path(data, data_path, output))
  163. return true;
  164. }
  165. char *relative_data_path =
  166. os_get_executable_path_ptr("../" OBS_DATA_PATH "/obs-studio/");
  167. if (relative_data_path) {
  168. bool result = check_path(data, relative_data_path, output);
  169. bfree(relative_data_path);
  170. if (result) {
  171. return true;
  172. }
  173. }
  174. if (check_path(data, OBS_DATA_PATH "/obs-studio/", output))
  175. return true;
  176. if (check_path(data, INSTALL_DATA_PATH, output))
  177. return true;
  178. return false;
  179. }
  180. string GetDefaultVideoSavePath()
  181. {
  182. return string(getenv("HOME"));
  183. }
  184. vector<string> GetPreferredLocales()
  185. {
  186. vector<string> matched;
  187. string messages = setlocale(LC_MESSAGES, NULL);
  188. if (!messages.size() || messages == "C" || messages == "POSIX")
  189. return {};
  190. if (messages.size() > 2)
  191. messages[2] = '-';
  192. for (auto &locale_pair : GetLocaleNames()) {
  193. auto &locale = locale_pair.first;
  194. if (locale == messages.substr(0, locale.size()))
  195. return {locale};
  196. if (locale.substr(0, 2) == messages.substr(0, 2))
  197. matched.push_back(locale);
  198. }
  199. return matched;
  200. }
  201. bool IsAlwaysOnTop(QWidget *window)
  202. {
  203. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  204. }
  205. void SetAlwaysOnTop(QWidget *window, bool enable)
  206. {
  207. Qt::WindowFlags flags = window->windowFlags();
  208. if (enable)
  209. flags |= Qt::WindowStaysOnTopHint;
  210. else
  211. flags &= ~Qt::WindowStaysOnTopHint;
  212. window->setWindowFlags(flags);
  213. window->show();
  214. }
  215. bool SetDisplayAffinitySupported(void)
  216. {
  217. // Not implemented yet
  218. return false;
  219. }
  220. // Not implemented yet
  221. void TaskbarOverlayInit() {}
  222. void TaskbarOverlaySetStatus(TaskbarOverlayStatus) {}