platform-x11.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 "platform.hpp"
  16. #include <OBSApp.hpp>
  17. #include <obs-config.h>
  18. #include <util/bmem.h>
  19. #include <util/platform.h>
  20. #include <QDBusConnection>
  21. #include <QDBusReply>
  22. #if defined(__FreeBSD__) || defined(__DragonFly__)
  23. #include <condition_variable>
  24. #include <mutex>
  25. #endif
  26. #include <sstream>
  27. #include <string>
  28. #if defined(__FreeBSD__) || defined(__DragonFly__)
  29. #include <thread>
  30. #endif
  31. #include <vector>
  32. #if defined(__FreeBSD__) || defined(__DragonFly__)
  33. #include <fcntl.h>
  34. #include <libprocstat.h>
  35. #include <pthread_np.h>
  36. #endif
  37. #if defined(__FreeBSD__) || defined(__DragonFly__)
  38. #include <sys/param.h>
  39. #endif
  40. #ifdef __linux__
  41. #include <sys/socket.h>
  42. #endif
  43. #if defined(__FreeBSD__) || defined(__DragonFly__)
  44. #include <sys/sysctl.h>
  45. #endif
  46. #ifdef __linux__
  47. #include <sys/un.h>
  48. #endif
  49. #if defined(__FreeBSD__) || defined(__DragonFly__)
  50. #include <sys/user.h>
  51. #endif
  52. #include <unistd.h>
  53. using std::ostringstream;
  54. using std::string;
  55. using std::vector;
  56. #ifdef __linux__
  57. void CheckIfAlreadyRunning(bool &already_running)
  58. {
  59. int uniq = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  60. if (uniq == -1) {
  61. blog(LOG_ERROR, "Failed to check for running instance, socket: %d", errno);
  62. already_running = 0;
  63. return;
  64. }
  65. struct sockaddr_un bindInfo;
  66. memset(&bindInfo, 0, sizeof(sockaddr_un));
  67. bindInfo.sun_family = AF_LOCAL;
  68. auto bindInfoStrlen = snprintf(bindInfo.sun_path + 1, sizeof(bindInfo.sun_path) - 1, "%s %d %s",
  69. "/com/obsproject", getpid(), App()->GetVersionString().c_str());
  70. int bindErr = bind(uniq, (struct sockaddr *)&bindInfo, sizeof(sa_family_t) + 1 + bindInfoStrlen);
  71. already_running = bindErr == 0 ? 0 : 1;
  72. if (already_running) {
  73. return;
  74. }
  75. FILE *fp = fopen("/proc/net/unix", "re");
  76. if (fp == NULL) {
  77. return;
  78. }
  79. char *line = NULL;
  80. size_t n = 0;
  81. int obsCnt = 0;
  82. while (getdelim(&line, &n, ' ', fp) != EOF) {
  83. line[strcspn(line, "\n")] = '\0';
  84. if (*line == '@') {
  85. if (strstr(line, "@/com/obsproject") != NULL) {
  86. ++obsCnt;
  87. }
  88. }
  89. }
  90. already_running = obsCnt == 1 ? 0 : 1;
  91. free(line);
  92. fclose(fp);
  93. }
  94. #endif
  95. #if defined(__FreeBSD__) || defined(__DragonFly__)
  96. struct RunOnce {
  97. std::thread thr;
  98. static const char *thr_name;
  99. std::condition_variable cv;
  100. std::condition_variable wait_cv;
  101. std::mutex mtx;
  102. bool exiting = false;
  103. bool name_changed = false;
  104. void thr_proc()
  105. {
  106. std::unique_lock<std::mutex> lk(mtx);
  107. pthread_set_name_np(pthread_self(), thr_name);
  108. name_changed = true;
  109. wait_cv.notify_all();
  110. cv.wait(lk, [this]() { return exiting; });
  111. }
  112. ~RunOnce()
  113. {
  114. if (thr.joinable()) {
  115. std::unique_lock<std::mutex> lk(mtx);
  116. exiting = true;
  117. cv.notify_one();
  118. lk.unlock();
  119. thr.join();
  120. }
  121. }
  122. } RO;
  123. const char *RunOnce::thr_name = "OBS runonce";
  124. void CheckIfAlreadyRunning(bool &already_running)
  125. {
  126. std::string tmpfile_name = "/tmp/obs-studio.lock." + std::to_string(geteuid());
  127. int fd = open(tmpfile_name.c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0600);
  128. if (fd == -1) {
  129. already_running = true;
  130. return;
  131. }
  132. already_running = false;
  133. procstat *ps = procstat_open_sysctl();
  134. unsigned int count;
  135. auto procs = procstat_getprocs(ps, KERN_PROC_UID | KERN_PROC_INC_THREAD, geteuid(), &count);
  136. for (unsigned int i = 0; i < count; i++) {
  137. if (!strncmp(procs[i].ki_tdname, RunOnce::thr_name, sizeof(procs[i].ki_tdname))) {
  138. already_running = true;
  139. break;
  140. }
  141. }
  142. procstat_freeprocs(ps, procs);
  143. procstat_close(ps);
  144. RO.thr = std::thread(std::mem_fn(&RunOnce::thr_proc), &RO);
  145. {
  146. std::unique_lock<std::mutex> lk(RO.mtx);
  147. RO.wait_cv.wait(lk, []() { return RO.name_changed; });
  148. }
  149. unlink(tmpfile_name.c_str());
  150. close(fd);
  151. }
  152. #endif
  153. static inline bool check_path(const char *data, const char *path, string &output)
  154. {
  155. ostringstream str;
  156. str << path << data;
  157. output = str.str();
  158. blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
  159. return (access(output.c_str(), R_OK) == 0);
  160. }
  161. #define INSTALL_DATA_PATH OBS_INSTALL_PREFIX "/" OBS_DATA_PATH "/obs-studio/"
  162. bool GetDataFilePath(const char *data, string &output)
  163. {
  164. char *data_path = getenv("OBS_DATA_PATH");
  165. if (data_path != NULL) {
  166. if (check_path(data, data_path, output))
  167. return true;
  168. }
  169. char *relative_data_path = 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("org.freedesktop.portal.Desktop",
  230. "/org/freedesktop/portal/desktop",
  231. "org.freedesktop.DBus.Properties", "Get");
  232. msgXdpSettingsVersion << "org.freedesktop.portal.Settings"
  233. << "version";
  234. reply = QDBusConnection::sessionBus().call(msgXdpSettingsVersion);
  235. if (!reply.isValid()) {
  236. blog(LOG_WARNING, "Get on org.freedesktop.portal.Settings returned an invalid reply");
  237. return false;
  238. }
  239. /* NOTE: org.freedesktop.portal.Settings got its contrast settings after
  240. * the ReadOne method. So assumes that if ReadOne is not available, contrast
  241. * isn't available either. */
  242. if (uint32_t version = reply.value().toUInt() < 2) {
  243. blog(LOG_WARNING, "org.freedesktop.portal.Settings version %u does not support ReadOne", version);
  244. return false;
  245. }
  246. /* NOTE: If contrast is not available if will return 0 (false). */
  247. QDBusMessage msgXdpSettingsContrast =
  248. QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop",
  249. "org.freedesktop.portal.Settings", "ReadOne");
  250. msgXdpSettingsContrast << "org.freedesktop.appearance"
  251. << "contrast";
  252. reply = QDBusConnection::sessionBus().call(msgXdpSettingsContrast);
  253. if (!reply.isValid()) {
  254. blog(LOG_WARNING, "ReadOne on org.freedesktop.portal.Settings returned an invalid reply");
  255. return false;
  256. }
  257. return reply.value().toUInt() != 0;
  258. }