1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/******************************************************************************
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
Copyright (C) 2014 by Zachary Lund <admin@computerquip.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <obs-config.h>
#include "obs-app.hpp"
#include <QGuiApplication>
#include <QScreen>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusReply>
#include <unistd.h>
#include <sstream>
#include <locale.h>
#include "platform.hpp"
#ifdef __linux__
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/un.h>
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/param.h>
#include <fcntl.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <libprocstat.h>
#include <pthread_np.h>
#include <condition_variable>
#include <mutex>
#include <thread>
#endif
using std::string;
using std::vector;
using std::ostringstream;
#ifdef __linux__
void CheckIfAlreadyRunning(bool &already_running)
{
int uniq = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (uniq == -1) {
blog(LOG_ERROR,
"Failed to check for running instance, socket: %d", errno);
already_running = 0;
return;
}
struct sockaddr_un bindInfo;
memset(&bindInfo, 0, sizeof(sockaddr_un));
bindInfo.sun_family = AF_LOCAL;
auto bindInfoStrlen = snprintf(bindInfo.sun_path + 1,
sizeof(bindInfo.sun_path) - 1,
"%s %d %s", "/com/obsproject", getpid(),
App()->GetVersionString().c_str());
int bindErr = bind(uniq, (struct sockaddr *)&bindInfo,
sizeof(sa_family_t) + 1 + bindInfoStrlen);
already_running = bindErr == 0 ? 0 : 1;
if (already_running) {
return;
}
FILE *fp = fopen("/proc/net/unix", "re");
if (fp == NULL) {
return;
}
char *line = NULL;
size_t n = 0;
int obsCnt = 0;
while (getdelim(&line, &n, ' ', fp) != EOF) {
line[strcspn(line, "\n")] = '\0';
if (*line == '@') {
if (strstr(line, "@/com/obsproject") != NULL) {
++obsCnt;
}
}
}
already_running = obsCnt == 1 ? 0 : 1;
free(line);
fclose(fp);
}
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
struct RunOnce {
std::thread thr;
static const char *thr_name;
std::condition_variable cv;
std::condition_variable wait_cv;
std::mutex mtx;
bool exiting = false;
bool name_changed = false;
void thr_proc()
{
std::unique_lock<std::mutex> lk(mtx);
pthread_set_name_np(pthread_self(), thr_name);
name_changed = true;
wait_cv.notify_all();
cv.wait(lk, [this]() { return exiting; });
}
~RunOnce()
{
if (thr.joinable()) {
std::unique_lock<std::mutex> lk(mtx);
exiting = true;
cv.notify_one();
lk.unlock();
thr.join();
}
}
} RO;
const char *RunOnce::thr_name = "OBS runonce";
void CheckIfAlreadyRunning(bool &already_running)
{
std::string tmpfile_name =
"/tmp/obs-studio.lock." + std::to_string(geteuid());
int fd = open(tmpfile_name.c_str(), O_RDWR | O_CREAT | O_EXLOCK, 0600);
if (fd == -1) {
already_running = true;
return;
}
already_running = false;
procstat *ps = procstat_open_sysctl();
unsigned int count;
auto procs = procstat_getprocs(ps, KERN_PROC_UID | KERN_PROC_INC_THREAD,
geteuid(), &count);
for (unsigned int i = 0; i < count; i++) {
if (!strncmp(procs[i].ki_tdname, RunOnce::thr_name,
sizeof(procs[i].ki_tdname))) {
already_running = true;
break;
}
}
procstat_freeprocs(ps, procs);
procstat_close(ps);
RO.thr = std::thread(std::mem_fn(&RunOnce::thr_proc), &RO);
{
std::unique_lock<std::mutex> lk(RO.mtx);
RO.wait_cv.wait(lk, []() { return RO.name_changed; });
}
unlink(tmpfile_name.c_str());
close(fd);
}
#endif
static inline bool check_path(const char *data, const char *path,
string &output)
{
ostringstream str;
str << path << data;
output = str.str();
blog(LOG_DEBUG, "Attempted path: %s", output.c_str());
return (access(output.c_str(), R_OK) == 0);
}
#define INSTALL_DATA_PATH OBS_INSTALL_PREFIX "/" OBS_DATA_PATH "/obs-studio/"
bool GetDataFilePath(const char *data, string &output)
{
char *data_path = getenv("OBS_DATA_PATH");
if (data_path != NULL) {
if (check_path(data, data_path, output))
return true;
}
char *relative_data_path =
os_get_executable_path_ptr("../" OBS_DATA_PATH "/obs-studio/");
if (relative_data_path) {
bool result = check_path(data, relative_data_path, output);
bfree(relative_data_path);
if (result) {
return true;
}
}
if (check_path(data, OBS_DATA_PATH "/obs-studio/", output))
return true;
if (check_path(data, INSTALL_DATA_PATH, output))
return true;
return false;
}
string GetDefaultVideoSavePath()
{
return string(getenv("HOME"));
}
vector<string> GetPreferredLocales()
{
vector<string> matched;
string messages = setlocale(LC_MESSAGES, NULL);
if (!messages.size() || messages == "C" || messages == "POSIX")
return {};
if (messages.size() > 2)
messages[2] = '-';
for (auto &locale_pair : GetLocaleNames()) {
auto &locale = locale_pair.first;
if (locale == messages.substr(0, locale.size()))
return {locale};
if (locale.substr(0, 2) == messages.substr(0, 2))
matched.push_back(locale);
}
return matched;
}
bool IsAlwaysOnTop(QWidget *window)
{
return (window->windowFlags() & Qt::WindowStaysOnTopHint) != 0;
}
void SetAlwaysOnTop(QWidget *window, bool enable)
{
Qt::WindowFlags flags = window->windowFlags();
if (enable)
flags |= Qt::WindowStaysOnTopHint;
else
flags &= ~Qt::WindowStaysOnTopHint;
window->setWindowFlags(flags);
window->show();
}
bool SetDisplayAffinitySupported(void)
{
// Not implemented yet
return false;
}
// Not implemented yet
void TaskbarOverlayInit() {}
void TaskbarOverlaySetStatus(TaskbarOverlayStatus) {}
bool HighContrastEnabled()
{
QDBusReply<QVariant> reply;
QDBusMessage msgXdpSettingsVersion = QDBusMessage::createMethodCall(
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.DBus.Properties", "Get");
msgXdpSettingsVersion << "org.freedesktop.portal.Settings"
<< "version";
reply = QDBusConnection::sessionBus().call(msgXdpSettingsVersion);
if (!reply.isValid()) {
blog(LOG_WARNING,
"Get on org.freedesktop.portal.Settings returned an invalid reply");
return false;
}
/* NOTE: org.freedesktop.portal.Settings got its contrast settings after
* the ReadOne method. So assumes that if ReadOne is not available, contrast
* isn't available either. */
if (uint32_t version = reply.value().toUInt() < 2) {
blog(LOG_WARNING,
"org.freedesktop.portal.Settings version %u does not support ReadOne",
version);
return false;
}
/* NOTE: If contrast is not available if will return 0 (false). */
QDBusMessage msgXdpSettingsContrast = QDBusMessage::createMethodCall(
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings", "ReadOne");
msgXdpSettingsContrast << "org.freedesktop.appearance"
<< "contrast";
reply = QDBusConnection::sessionBus().call(msgXdpSettingsContrast);
if (!reply.isValid()) {
blog(LOG_WARNING,
"ReadOne on org.freedesktop.portal.Settings returned an invalid reply");
return false;
}
return reply.value().toUInt() != 0;
}
|