platform-x11.cpp 8.0 KB

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