2018-11-07 11:10:38 +00:00
|
|
|
#include "utils.hpp"
|
2020-05-06 12:43:01 +01:00
|
|
|
#include "logger.h"
|
|
|
|
#include "spdlog/logger.h"
|
2020-04-26 01:05:09 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QStringList>
|
2020-04-22 10:56:45 +01:00
|
|
|
#include <memory>
|
2018-12-09 02:35:08 +00:00
|
|
|
|
2020-04-22 10:56:45 +01:00
|
|
|
#if (defined(__linux__) || defined(__FreeBSD__)) && ENABLE_X11
|
2020-05-06 12:43:01 +01:00
|
|
|
#include <QX11Info> // IWYU pragma: keep
|
2020-04-22 10:56:45 +01:00
|
|
|
#include <X11/X.h> // IWYU pragma: keep
|
|
|
|
#include <X11/Xlib.h> // IWYU pragma: keep
|
|
|
|
#include <X11/Xutil.h> // IWYU pragma: keep
|
|
|
|
#include <qx11info_x11.h> // IWYU pragma: keep
|
2020-04-24 11:15:49 +01:00
|
|
|
#undef Bool
|
2018-11-28 11:55:26 +00:00
|
|
|
#endif
|
2018-11-18 13:33:45 +00:00
|
|
|
|
2018-12-09 02:35:08 +00:00
|
|
|
auto utilsLogger = initLogger("utils");
|
|
|
|
|
2018-11-18 13:33:45 +00:00
|
|
|
namespace Utils {
|
2018-11-17 15:45:21 +00:00
|
|
|
QString
|
|
|
|
getPlatformName()
|
2018-11-07 11:10:38 +00:00
|
|
|
{
|
2020-05-06 12:43:01 +01:00
|
|
|
QGuiApplication* qapp = qobject_cast<QGuiApplication*>(QCoreApplication::instance());
|
|
|
|
return qapp->platformName();
|
2018-11-07 11:10:38 +00:00
|
|
|
}
|
2018-12-06 08:13:14 +00:00
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
void launchAboutQt()
|
2018-12-06 08:13:14 +00:00
|
|
|
{
|
2020-05-06 12:43:01 +01:00
|
|
|
QApplication* qapp = qobject_cast<QApplication*>(QCoreApplication::instance());
|
|
|
|
qapp->aboutQt();
|
2018-12-06 08:13:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 15:28:04 +01:00
|
|
|
void updateAppImage()
|
|
|
|
{
|
2020-05-06 12:43:01 +01:00
|
|
|
QString program = QProcessEnvironment::systemEnvironment().value("APPDIR", "") + "/usr/bin/appimageupdatetool";
|
|
|
|
QProcess updater;
|
|
|
|
updater.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
updater.start(program,
|
|
|
|
QStringList() << QProcessEnvironment::systemEnvironment().value(
|
|
|
|
"APPIMAGE", ""));
|
|
|
|
updater.waitForFinished();
|
|
|
|
qApp->exit();
|
2020-05-05 15:28:04 +01:00
|
|
|
}
|
|
|
|
|
2018-11-24 21:00:04 +00:00
|
|
|
// https://www.youtube.com/watch?v=nXaxk27zwlk&feature=youtu.be&t=56m34s
|
2020-05-04 10:24:16 +01:00
|
|
|
inline int
|
2018-11-24 21:00:04 +00:00
|
|
|
fast_mod(const int input, const int ceil)
|
|
|
|
{
|
2020-05-06 12:43:01 +01:00
|
|
|
return input >= ceil ? input % ceil : input;
|
2018-11-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 05:07:22 +00:00
|
|
|
QString
|
2018-12-08 22:21:12 +00:00
|
|
|
createTimestamp(const int seconds)
|
2018-11-19 05:07:22 +00:00
|
|
|
{
|
2018-11-24 21:00:04 +00:00
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
const int s = fast_mod(seconds, 60);
|
|
|
|
const int m = fast_mod(seconds, 3600) / 60;
|
|
|
|
const int h = fast_mod(seconds, 86400) / 3600;
|
2018-11-19 05:07:22 +00:00
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
if (h > 0) {
|
|
|
|
return QString::asprintf("%02d:%02d:%02d", h, m, s);
|
|
|
|
} else {
|
|
|
|
return QString::asprintf("%02d:%02d", m, s);
|
|
|
|
}
|
2018-11-19 05:07:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
void SetScreensaver(WId wid, bool on)
|
2018-12-01 12:10:52 +00:00
|
|
|
{
|
2020-05-06 12:43:01 +01:00
|
|
|
QProcess xdgScreensaver;
|
|
|
|
xdgScreensaver.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
if (on) {
|
|
|
|
utilsLogger->info("Enabling screensaver.");
|
|
|
|
xdgScreensaver.start("xdg-screensaver",
|
|
|
|
QStringList() << "resume" << QString::number(wid));
|
|
|
|
} else {
|
|
|
|
utilsLogger->info("Disabling screensaver.");
|
|
|
|
xdgScreensaver.start("xdg-screensaver",
|
|
|
|
QStringList() << "suspend" << QString::number(wid));
|
|
|
|
}
|
|
|
|
xdgScreensaver.waitForFinished();
|
2018-12-01 12:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
void SetDPMS(bool on)
|
2018-11-07 11:10:38 +00:00
|
|
|
{
|
2020-04-17 12:32:23 +01:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__)
|
2020-05-06 12:43:01 +01:00
|
|
|
if (getPlatformName() != "xcb") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QProcess xsetProcess;
|
|
|
|
xsetProcess.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
if (on) {
|
|
|
|
utilsLogger->info("Enabled DPMS.");
|
|
|
|
xsetProcess.start("xset",
|
|
|
|
QStringList() << "s"
|
|
|
|
<< "on"
|
|
|
|
<< "+dpms");
|
|
|
|
} else {
|
|
|
|
utilsLogger->info("Disabled DPMS.");
|
|
|
|
xsetProcess.start("xset",
|
|
|
|
QStringList() << "s"
|
|
|
|
<< "off"
|
|
|
|
<< "-dpms");
|
|
|
|
}
|
|
|
|
xsetProcess.waitForFinished();
|
2018-12-01 12:10:52 +00:00
|
|
|
#else
|
2020-05-06 12:43:01 +01:00
|
|
|
utilsLogger->error("Can't set DPMS for platform: {}",
|
|
|
|
getPlatformName().toUtf8().constData());
|
2018-11-28 11:55:26 +00:00
|
|
|
#endif
|
2018-11-21 08:03:28 +00:00
|
|
|
}
|
2018-11-07 11:10:38 +00:00
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
void AlwaysOnTop(WId wid, bool on)
|
2018-11-07 11:10:38 +00:00
|
|
|
{
|
2020-04-22 10:56:45 +01:00
|
|
|
#if (defined(__linux__) || defined(__FreeBSD__)) && ENABLE_X11
|
2020-05-06 12:43:01 +01:00
|
|
|
Display* display = QX11Info::display();
|
|
|
|
XEvent event;
|
|
|
|
event.xclient.type = ClientMessage;
|
|
|
|
event.xclient.serial = 0;
|
|
|
|
event.xclient.send_event = true;
|
|
|
|
event.xclient.display = display;
|
|
|
|
event.xclient.window = wid;
|
|
|
|
event.xclient.message_type = XInternAtom(display, "_NET_WM_STATE", False);
|
|
|
|
event.xclient.format = 32;
|
2018-11-07 11:10:38 +00:00
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
event.xclient.data.l[0] = on;
|
|
|
|
event.xclient.data.l[1] = XInternAtom(display, "_NET_WM_STATE_ABOVE", False);
|
|
|
|
event.xclient.data.l[2] = 0;
|
|
|
|
event.xclient.data.l[3] = 0;
|
|
|
|
event.xclient.data.l[4] = 0;
|
2018-11-07 11:10:38 +00:00
|
|
|
|
2020-05-06 12:43:01 +01:00
|
|
|
XSendEvent(display,
|
|
|
|
DefaultRootWindow(display),
|
|
|
|
False,
|
|
|
|
SubstructureRedirectMask | SubstructureNotifyMask,
|
|
|
|
&event);
|
2018-11-07 11:10:38 +00:00
|
|
|
#else
|
2020-05-06 12:43:01 +01:00
|
|
|
utilsLogger->error("Can't set on top for platform: {}",
|
|
|
|
getPlatformName().toUtf8().constData());
|
2018-11-18 13:33:45 +00:00
|
|
|
#endif
|
2018-11-24 21:00:04 +00:00
|
|
|
}
|
2018-12-01 12:10:52 +00:00
|
|
|
}
|