platform-x11.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. snprintf(bindInfo.sun_path + 1, sizeof(bindInfo.sun_path) - 1,
  58. "%s %d %s", "/com/obsproject", getpid(),
  59. App()->GetVersionString().c_str());
  60. int bindErr = bind(uniq, (struct sockaddr *)&bindInfo,
  61. sizeof(struct sockaddr_un));
  62. already_running = bindErr == 0 ? 0 : 1;
  63. if (already_running) {
  64. return;
  65. }
  66. FILE *fp = fopen("/proc/net/unix", "re");
  67. if (fp == NULL) {
  68. return;
  69. }
  70. char *line = NULL;
  71. size_t n = 0;
  72. int obsCnt = 0;
  73. while (getdelim(&line, &n, ' ', fp) != EOF) {
  74. line[strcspn(line, "\n")] = '\0';
  75. if (*line == '@') {
  76. if (strstr(line, "@/com/obsproject") != NULL) {
  77. ++obsCnt;
  78. }
  79. }
  80. }
  81. already_running = obsCnt == 1 ? 0 : 1;
  82. free(line);
  83. fclose(fp);
  84. }
  85. #endif
  86. #if defined(__FreeBSD__) || defined(__DragonFly__)
  87. struct RunOnce {
  88. std::thread thr;
  89. static const char *thr_name;
  90. std::condition_variable cv;
  91. std::condition_variable wait_cv;
  92. std::mutex mtx;
  93. bool exiting = false;
  94. bool name_changed = false;
  95. void thr_proc()
  96. {
  97. std::unique_lock<std::mutex> lk(mtx);
  98. pthread_set_name_np(pthread_self(), thr_name);
  99. name_changed = true;
  100. wait_cv.notify_all();
  101. cv.wait(lk, [this]() { return exiting; });
  102. }
  103. ~RunOnce()
  104. {
  105. if (thr.joinable()) {
  106. std::unique_lock<std::mutex> lk(mtx);
  107. exiting = true;
  108. cv.notify_one();
  109. lk.unlock();
  110. thr.join();
  111. }
  112. }
  113. } RO;
  114. const char *RunOnce::thr_name = "OBS runonce";
  115. void CheckIfAlreadyRunning(bool &already_running)
  116. {
  117. std::string tmpfile_name =
  118. "/tmp/obs-studio.lock." + std::to_string(geteuid());
  119. int fd = open(tmpfile_name.c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0600);
  120. if (fd == -1) {
  121. already_running = true;
  122. return;
  123. }
  124. already_running = false;
  125. procstat *ps = procstat_open_sysctl();
  126. unsigned int count;
  127. auto procs = procstat_getprocs(ps, KERN_PROC_UID | KERN_PROC_INC_THREAD,
  128. geteuid(), &count);
  129. for (unsigned int i = 0; i < count; i++) {
  130. if (!strncmp(procs[i].ki_tdname, RunOnce::thr_name,
  131. sizeof(procs[i].ki_tdname))) {
  132. already_running = true;
  133. break;
  134. }
  135. }
  136. procstat_freeprocs(ps, procs);
  137. procstat_close(ps);
  138. RO.thr = std::thread(std::mem_fn(&RunOnce::thr_proc), &RO);
  139. {
  140. std::unique_lock<std::mutex> lk(RO.mtx);
  141. RO.wait_cv.wait(lk, []() { return RO.name_changed; });
  142. }
  143. unlink(tmpfile_name.c_str());
  144. close(fd);
  145. }
  146. #endif
  147. static inline bool check_path(const char *data, const char *path,
  148. string &output)
  149. {
  150. ostringstream str;
  151. str << path << data;
  152. output = str.str();
  153. blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
  154. return (access(output.c_str(), R_OK) == 0);
  155. }
  156. #define INSTALL_DATA_PATH OBS_INSTALL_PREFIX OBS_DATA_PATH "/obs-studio/"
  157. bool GetDataFilePath(const char *data, string &output)
  158. {
  159. char *data_path = getenv("OBS_DATA_PATH");
  160. if (data_path != NULL) {
  161. if (check_path(data, data_path, output))
  162. return true;
  163. }
  164. if (check_path(data, OBS_DATA_PATH "/obs-studio/", output))
  165. return true;
  166. if (check_path(data, INSTALL_DATA_PATH, output))
  167. return true;
  168. return false;
  169. }
  170. string GetDefaultVideoSavePath()
  171. {
  172. return string(getenv("HOME"));
  173. }
  174. vector<string> GetPreferredLocales()
  175. {
  176. vector<string> matched;
  177. string messages = setlocale(LC_MESSAGES, NULL);
  178. if (!messages.size() || messages == "C" || messages == "POSIX")
  179. return {};
  180. if (messages.size() > 2)
  181. messages[2] = '-';
  182. for (auto &locale_pair : GetLocaleNames()) {
  183. auto &locale = locale_pair.first;
  184. if (locale == messages.substr(0, locale.size()))
  185. return {locale};
  186. if (locale.substr(0, 2) == messages.substr(0, 2))
  187. matched.push_back(locale);
  188. }
  189. return matched;
  190. }
  191. bool IsAlwaysOnTop(QWidget *window)
  192. {
  193. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  194. }
  195. void SetAlwaysOnTop(QWidget *window, bool enable)
  196. {
  197. Qt::WindowFlags flags = window->windowFlags();
  198. if (enable)
  199. flags |= Qt::WindowStaysOnTopHint;
  200. else
  201. flags &= ~Qt::WindowStaysOnTopHint;
  202. window->setWindowFlags(flags);
  203. window->show();
  204. }
  205. bool SetDisplayAffinitySupported(void)
  206. {
  207. // Not implemented yet
  208. return false;
  209. }
  210. // Not implemented yet
  211. void TaskbarOverlayInit() {}
  212. void TaskbarOverlaySetStatus(TaskbarOverlayStatus) {}