platform-x11.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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,
  53. "Failed to check for running instance, socket: %d", errno);
  54. already_running = 0;
  55. return;
  56. }
  57. struct sockaddr_un bindInfo;
  58. memset(&bindInfo, 0, sizeof(sockaddr_un));
  59. bindInfo.sun_family = AF_LOCAL;
  60. auto bindInfoStrlen = snprintf(bindInfo.sun_path + 1,
  61. sizeof(bindInfo.sun_path) - 1,
  62. "%s %d %s", "/com/obsproject", getpid(),
  63. App()->GetVersionString().c_str());
  64. int bindErr = bind(uniq, (struct sockaddr *)&bindInfo,
  65. sizeof(sa_family_t) + 1 + bindInfoStrlen);
  66. already_running = bindErr == 0 ? 0 : 1;
  67. if (already_running) {
  68. return;
  69. }
  70. FILE *fp = fopen("/proc/net/unix", "re");
  71. if (fp == NULL) {
  72. return;
  73. }
  74. char *line = NULL;
  75. size_t n = 0;
  76. int obsCnt = 0;
  77. while (getdelim(&line, &n, ' ', fp) != EOF) {
  78. line[strcspn(line, "\n")] = '\0';
  79. if (*line == '@') {
  80. if (strstr(line, "@/com/obsproject") != NULL) {
  81. ++obsCnt;
  82. }
  83. }
  84. }
  85. already_running = obsCnt == 1 ? 0 : 1;
  86. free(line);
  87. fclose(fp);
  88. }
  89. #endif
  90. #if defined(__FreeBSD__) || defined(__DragonFly__)
  91. struct RunOnce {
  92. std::thread thr;
  93. static const char *thr_name;
  94. std::condition_variable cv;
  95. std::condition_variable wait_cv;
  96. std::mutex mtx;
  97. bool exiting = false;
  98. bool name_changed = false;
  99. void thr_proc()
  100. {
  101. std::unique_lock<std::mutex> lk(mtx);
  102. pthread_set_name_np(pthread_self(), thr_name);
  103. name_changed = true;
  104. wait_cv.notify_all();
  105. cv.wait(lk, [this]() { return exiting; });
  106. }
  107. ~RunOnce()
  108. {
  109. if (thr.joinable()) {
  110. std::unique_lock<std::mutex> lk(mtx);
  111. exiting = true;
  112. cv.notify_one();
  113. lk.unlock();
  114. thr.join();
  115. }
  116. }
  117. } RO;
  118. const char *RunOnce::thr_name = "OBS runonce";
  119. void CheckIfAlreadyRunning(bool &already_running)
  120. {
  121. std::string tmpfile_name =
  122. "/tmp/obs-studio.lock." + std::to_string(geteuid());
  123. int fd = open(tmpfile_name.c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0600);
  124. if (fd == -1) {
  125. already_running = true;
  126. return;
  127. }
  128. already_running = false;
  129. procstat *ps = procstat_open_sysctl();
  130. unsigned int count;
  131. auto procs = procstat_getprocs(ps, KERN_PROC_UID | KERN_PROC_INC_THREAD,
  132. geteuid(), &count);
  133. for (unsigned int i = 0; i < count; i++) {
  134. if (!strncmp(procs[i].ki_tdname, RunOnce::thr_name,
  135. sizeof(procs[i].ki_tdname))) {
  136. already_running = true;
  137. break;
  138. }
  139. }
  140. procstat_freeprocs(ps, procs);
  141. procstat_close(ps);
  142. RO.thr = std::thread(std::mem_fn(&RunOnce::thr_proc), &RO);
  143. {
  144. std::unique_lock<std::mutex> lk(RO.mtx);
  145. RO.wait_cv.wait(lk, []() { return RO.name_changed; });
  146. }
  147. unlink(tmpfile_name.c_str());
  148. close(fd);
  149. }
  150. #endif
  151. static inline bool check_path(const char *data, const char *path,
  152. string &output)
  153. {
  154. ostringstream str;
  155. str << path << data;
  156. output = str.str();
  157. blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
  158. return (access(output.c_str(), R_OK) == 0);
  159. }
  160. #define INSTALL_DATA_PATH OBS_INSTALL_PREFIX "/" OBS_DATA_PATH "/obs-studio/"
  161. bool GetDataFilePath(const char *data, string &output)
  162. {
  163. char *data_path = getenv("OBS_DATA_PATH");
  164. if (data_path != NULL) {
  165. if (check_path(data, data_path, output))
  166. return true;
  167. }
  168. char *relative_data_path =
  169. os_get_executable_path_ptr("../" OBS_DATA_PATH "/obs-studio/");
  170. if (relative_data_path) {
  171. bool result = check_path(data, relative_data_path, output);
  172. bfree(relative_data_path);
  173. if (result) {
  174. return true;
  175. }
  176. }
  177. if (check_path(data, OBS_DATA_PATH "/obs-studio/", output))
  178. return true;
  179. if (check_path(data, INSTALL_DATA_PATH, output))
  180. return true;
  181. return false;
  182. }
  183. string GetDefaultVideoSavePath()
  184. {
  185. return string(getenv("HOME"));
  186. }
  187. vector<string> GetPreferredLocales()
  188. {
  189. vector<string> matched;
  190. string messages = setlocale(LC_MESSAGES, NULL);
  191. if (!messages.size() || messages == "C" || messages == "POSIX")
  192. return {};
  193. if (messages.size() > 2)
  194. messages[2] = '-';
  195. for (auto &locale_pair : GetLocaleNames()) {
  196. auto &locale = locale_pair.first;
  197. if (locale == messages.substr(0, locale.size()))
  198. return {locale};
  199. if (locale.substr(0, 2) == messages.substr(0, 2))
  200. matched.push_back(locale);
  201. }
  202. return matched;
  203. }
  204. bool IsAlwaysOnTop(QWidget *window)
  205. {
  206. return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
  207. }
  208. void SetAlwaysOnTop(QWidget *window, bool enable)
  209. {
  210. Qt::WindowFlags flags = window->windowFlags();
  211. if (enable)
  212. flags |= Qt::WindowStaysOnTopHint;
  213. else
  214. flags &= ~Qt::WindowStaysOnTopHint;
  215. window->setWindowFlags(flags);
  216. window->show();
  217. }
  218. bool SetDisplayAffinitySupported(void)
  219. {
  220. // Not implemented yet
  221. return false;
  222. }
  223. // Not implemented yet
  224. void TaskbarOverlayInit() {}
  225. void TaskbarOverlaySetStatus(TaskbarOverlayStatus) {}
  226. bool HighContrastEnabled()
  227. {
  228. QDBusReply<QVariant> reply;
  229. QDBusMessage msgXdpSettingsVersion = QDBusMessage::createMethodCall(
  230. "org.freedesktop.portal.Desktop",
  231. "/org/freedesktop/portal/desktop",
  232. "org.freedesktop.DBus.Properties", "Get");
  233. msgXdpSettingsVersion << "org.freedesktop.portal.Settings"
  234. << "version";
  235. reply = QDBusConnection::sessionBus().call(msgXdpSettingsVersion);
  236. if (!reply.isValid()) {
  237. blog(LOG_WARNING,
  238. "Get on org.freedesktop.portal.Settings returned an invalid reply");
  239. return false;
  240. }
  241. /* NOTE: org.freedesktop.portal.Settings got its contrast settings after
  242. * the ReadOne method. So assumes that if ReadOne is not available, contrast
  243. * isn't available either. */
  244. if (uint32_t version = reply.value().toUInt() < 2) {
  245. blog(LOG_WARNING,
  246. "org.freedesktop.portal.Settings version %u does not support ReadOne",
  247. version);
  248. return false;
  249. }
  250. /* NOTE: If contrast is not available if will return 0 (false). */
  251. QDBusMessage msgXdpSettingsContrast = QDBusMessage::createMethodCall(
  252. "org.freedesktop.portal.Desktop",
  253. "/org/freedesktop/portal/desktop",
  254. "org.freedesktop.portal.Settings", "ReadOne");
  255. msgXdpSettingsContrast << "org.freedesktop.appearance"
  256. << "contrast";
  257. reply = QDBusConnection::sessionBus().call(msgXdpSettingsContrast);
  258. if (!reply.isValid()) {
  259. blog(LOG_WARNING,
  260. "ReadOne on org.freedesktop.portal.Settings returned an invalid reply");
  261. return false;
  262. }
  263. return reply.value().toUInt() != 0;
  264. }