platform-x11.cpp 5.7 KB

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