platform-x11.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <QDBusConnection>
  20. #include <QDBusMessage>
  21. #include <QDBusReply>
  22. #include <unistd.h>
  23. #include <sstream>
  24. #include <locale.h>
  25. #include "platform.hpp"
  26. #ifdef __linux__
  27. #include <sys/socket.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <stdio.h>
  31. #include <sys/un.h>
  32. #endif
  33. #if defined(__FreeBSD__) || defined(__DragonFly__)
  34. #include <sys/param.h>
  35. #include <fcntl.h>
  36. #include <sys/sysctl.h>
  37. #include <sys/user.h>
  38. #include <libprocstat.h>
  39. #include <pthread_np.h>
  40. #include <condition_variable>
  41. #include <mutex>
  42. #include <thread>
  43. #endif
  44. using std::string;
  45. using std::vector;
  46. using std::ostringstream;
  47. #ifdef __linux__
  48. void CheckIfAlreadyRunning(bool &already_running)
  49. {
  50. int uniq = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  51. if (uniq == -1) {
  52. blog(LOG_ERROR, "Failed to check for running instance, socket: %d", errno);
  53. already_running = 0;
  54. return;
  55. }
  56. struct sockaddr_un bindInfo;
  57. memset(&bindInfo, 0, sizeof(sockaddr_un));
  58. bindInfo.sun_family = AF_LOCAL;
  59. auto bindInfoStrlen = snprintf(bindInfo.sun_path + 1, sizeof(bindInfo.sun_path) - 1, "%s %d %s",
  60. "/com/obsproject", getpid(), App()->GetVersionString().c_str());
  61. int bindErr = bind(uniq, (struct sockaddr *)&bindInfo, sizeof(sa_family_t) + 1 + bindInfoStrlen);
  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 = "/tmp/obs-studio.lock." + std::to_string(geteuid());
  118. int fd = open(tmpfile_name.c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0600);
  119. if (fd == -1) {
  120. already_running = true;
  121. return;
  122. }
  123. already_running = false;
  124. procstat *ps = procstat_open_sysctl();
  125. unsigned int count;
  126. auto procs = procstat_getprocs(ps, KERN_PROC_UID | KERN_PROC_INC_THREAD, geteuid(), &count);
  127. for (unsigned int i = 0; i < count; i++) {
  128. if (!strncmp(procs[i].ki_tdname, RunOnce::thr_name, sizeof(procs[i].ki_tdname))) {
  129. already_running = true;
  130. break;
  131. }
  132. }
  133. procstat_freeprocs(ps, procs);
  134. procstat_close(ps);
  135. RO.thr = std::thread(std::mem_fn(&RunOnce::thr_proc), &RO);
  136. {
  137. std::unique_lock<std::mutex> lk(RO.mtx);
  138. RO.wait_cv.wait(lk, []() { return RO.name_changed; });
  139. }
  140. unlink(tmpfile_name.c_str());
  141. close(fd);
  142. }
  143. #endif
  144. static inline bool check_path(const char *data, const char *path, string &output)
  145. {
  146. ostringstream str;
  147. str << path << data;
  148. output = str.str();
  149. blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
  150. return (access(output.c_str(), R_OK) == 0);
  151. }
  152. #define INSTALL_DATA_PATH OBS_INSTALL_PREFIX "/" OBS_DATA_PATH "/obs-studio/"
  153. bool GetDataFilePath(const char *data, string &output)
  154. {
  155. char *data_path = getenv("OBS_DATA_PATH");
  156. if (data_path != NULL) {
  157. if (check_path(data, data_path, output))
  158. return true;
  159. }
  160. char *relative_data_path = os_get_executable_path_ptr("../" OBS_DATA_PATH "/obs-studio/");
  161. if (relative_data_path) {
  162. bool result = check_path(data, relative_data_path, output);
  163. bfree(relative_data_path);
  164. if (result) {
  165. return true;
  166. }
  167. }
  168. if (check_path(data, OBS_DATA_PATH "/obs-studio/", output))
  169. return true;
  170. if (check_path(data, INSTALL_DATA_PATH, output))
  171. return true;
  172. return false;
  173. }
  174. string GetDefaultVideoSavePath()
  175. {
  176. return string(getenv("HOME"));
  177. }
  178. vector<string> GetPreferredLocales()
  179. {
  180. vector<string> matched;
  181. string messages = setlocale(LC_MESSAGES, NULL);
  182. if (!messages.size() || messages == "C" || messages == "POSIX")
  183. return {};
  184. if (messages.size() > 2)
  185. messages[2] = '-';
  186. for (auto &locale_pair : GetLocaleNames()) {
  187. auto &locale = locale_pair.first;
  188. if (locale == messages.substr(0, locale.size()))
  189. return {locale};
  190. if (locale.substr(0, 2) == messages.substr(0, 2))
  191. matched.push_back(locale);
  192. }
  193. return matched;
  194. }
  195. bool IsAlwaysOnTop(QWidget *window)
  196. {
  197. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  198. }
  199. void SetAlwaysOnTop(QWidget *window, bool enable)
  200. {
  201. Qt::WindowFlags flags = window->windowFlags();
  202. if (enable)
  203. flags |= Qt::WindowStaysOnTopHint;
  204. else
  205. flags &= ~Qt::WindowStaysOnTopHint;
  206. window->setWindowFlags(flags);
  207. window->show();
  208. }
  209. bool SetDisplayAffinitySupported(void)
  210. {
  211. // Not implemented yet
  212. return false;
  213. }
  214. // Not implemented yet
  215. void TaskbarOverlayInit() {}
  216. void TaskbarOverlaySetStatus(TaskbarOverlayStatus) {}
  217. bool HighContrastEnabled()
  218. {
  219. QDBusReply<QVariant> reply;
  220. QDBusMessage msgXdpSettingsVersion = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop",
  221. "/org/freedesktop/portal/desktop",
  222. "org.freedesktop.DBus.Properties", "Get");
  223. msgXdpSettingsVersion << "org.freedesktop.portal.Settings"
  224. << "version";
  225. reply = QDBusConnection::sessionBus().call(msgXdpSettingsVersion);
  226. if (!reply.isValid()) {
  227. blog(LOG_WARNING, "Get on org.freedesktop.portal.Settings returned an invalid reply");
  228. return false;
  229. }
  230. /* NOTE: org.freedesktop.portal.Settings got its contrast settings after
  231. * the ReadOne method. So assumes that if ReadOne is not available, contrast
  232. * isn't available either. */
  233. if (uint32_t version = reply.value().toUInt() < 2) {
  234. blog(LOG_WARNING, "org.freedesktop.portal.Settings version %u does not support ReadOne", version);
  235. return false;
  236. }
  237. /* NOTE: If contrast is not available if will return 0 (false). */
  238. QDBusMessage msgXdpSettingsContrast =
  239. QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop",
  240. "org.freedesktop.portal.Settings", "ReadOne");
  241. msgXdpSettingsContrast << "org.freedesktop.appearance"
  242. << "contrast";
  243. reply = QDBusConnection::sessionBus().call(msgXdpSettingsContrast);
  244. if (!reply.isValid()) {
  245. blog(LOG_WARNING, "ReadOne on org.freedesktop.portal.Settings returned an invalid reply");
  246. return false;
  247. }
  248. return reply.value().toUInt() != 0;
  249. }