2018-11-07 11:10:38 +00:00
|
|
|
#include "utils.hpp"
|
2020-04-22 10:56:45 +01:00
|
|
|
#include <qapplication.h>
|
|
|
|
#include <qcoreapplication.h>
|
|
|
|
#include <qguiapplication.h>
|
|
|
|
#include <qprocess.h>
|
|
|
|
#include <qstringlist.h>
|
|
|
|
#include <memory>
|
2018-12-09 02:35:08 +00:00
|
|
|
#include "logger.h"
|
2020-04-22 10:56:45 +01:00
|
|
|
#include "spdlog/logger.h"
|
2018-12-09 02:35:08 +00:00
|
|
|
|
2020-04-22 10:56:45 +01:00
|
|
|
#if (defined(__linux__) || defined(__FreeBSD__)) && ENABLE_X11
|
|
|
|
#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
|
|
|
|
#include <QX11Info> // IWYU pragma: keep
|
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
|
|
|
{
|
2018-11-17 15:45:21 +00:00
|
|
|
QGuiApplication* qapp =
|
|
|
|
qobject_cast<QGuiApplication*>(QCoreApplication::instance());
|
2018-11-07 11:10:38 +00:00
|
|
|
return qapp->platformName();
|
|
|
|
}
|
2018-12-06 08:13:14 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
launchAboutQt()
|
|
|
|
{
|
|
|
|
QApplication* qapp =
|
|
|
|
qobject_cast<QApplication*>(QCoreApplication::instance());
|
|
|
|
qapp->aboutQt();
|
|
|
|
}
|
|
|
|
|
2018-11-24 21:00:04 +00:00
|
|
|
// https://www.youtube.com/watch?v=nXaxk27zwlk&feature=youtu.be&t=56m34s
|
2018-12-08 22:21:12 +00:00
|
|
|
inline const int
|
2018-11-24 21:00:04 +00:00
|
|
|
fast_mod(const int input, const int ceil)
|
|
|
|
{
|
|
|
|
return input >= ceil ? input % ceil : input;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-12-08 22:21:12 +00: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
|
|
|
|
|
|
|
if (h > 0) {
|
|
|
|
return QString::asprintf("%02d:%02d:%02d", h, m, s);
|
|
|
|
} else {
|
|
|
|
return QString::asprintf("%02d:%02d", m, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 12:10:52 +00:00
|
|
|
void
|
|
|
|
SetScreensaver(WId wid, bool on)
|
|
|
|
{
|
|
|
|
QProcess xdgScreensaver;
|
|
|
|
xdgScreensaver.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
if (on) {
|
2018-12-09 02:35:08 +00:00
|
|
|
utilsLogger->info("Enabling screensaver.");
|
2018-12-01 12:10:52 +00:00
|
|
|
xdgScreensaver.start("xdg-screensaver",
|
|
|
|
QStringList() << "resume" << QString::number(wid));
|
|
|
|
} else {
|
2018-12-09 02:35:08 +00:00
|
|
|
utilsLogger->info("Disabling screensaver.");
|
2018-12-01 12:10:52 +00:00
|
|
|
xdgScreensaver.start("xdg-screensaver",
|
|
|
|
QStringList() << "suspend" << QString::number(wid));
|
|
|
|
}
|
|
|
|
xdgScreensaver.waitForFinished();
|
|
|
|
}
|
|
|
|
|
2018-11-17 15:45:21 +00: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__)
|
2018-11-07 17:22:18 +00:00
|
|
|
if (getPlatformName() != "xcb") {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-01 12:10:52 +00:00
|
|
|
QProcess xsetProcess;
|
|
|
|
xsetProcess.setProcessChannelMode(QProcess::ForwardedChannels);
|
2018-11-07 11:10:38 +00:00
|
|
|
if (on) {
|
2018-12-09 02:35:08 +00:00
|
|
|
utilsLogger->info("Enabled DPMS.");
|
2018-12-01 12:10:52 +00:00
|
|
|
xsetProcess.start("xset",
|
|
|
|
QStringList() << "s"
|
|
|
|
<< "on"
|
|
|
|
<< "+dpms");
|
2018-11-07 11:10:38 +00:00
|
|
|
} else {
|
2018-12-09 02:35:08 +00:00
|
|
|
utilsLogger->info("Disabled DPMS.");
|
2018-12-01 12:10:52 +00:00
|
|
|
xsetProcess.start("xset",
|
|
|
|
QStringList() << "s"
|
|
|
|
<< "off"
|
|
|
|
<< "-dpms");
|
2018-11-07 11:10:38 +00:00
|
|
|
}
|
2018-12-01 12:10:52 +00:00
|
|
|
xsetProcess.waitForFinished();
|
|
|
|
#else
|
2018-12-09 02:35:08 +00: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
|
|
|
|
2018-11-17 15:45:21 +00: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
|
2018-11-07 11:10:38 +00:00
|
|
|
Display* display = QX11Info::display();
|
|
|
|
XEvent event;
|
|
|
|
event.xclient.type = ClientMessage;
|
|
|
|
event.xclient.serial = 0;
|
2020-04-17 12:32:23 +01:00
|
|
|
event.xclient.send_event = true;
|
2018-11-07 11:10:38 +00:00
|
|
|
event.xclient.display = display;
|
|
|
|
event.xclient.window = wid;
|
|
|
|
event.xclient.message_type = XInternAtom(display, "_NET_WM_STATE", False);
|
|
|
|
event.xclient.format = 32;
|
|
|
|
|
|
|
|
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-17 15:45:21 +00:00
|
|
|
XSendEvent(display,
|
|
|
|
DefaultRootWindow(display),
|
|
|
|
False,
|
|
|
|
SubstructureRedirectMask | SubstructureNotifyMask,
|
|
|
|
&event);
|
2018-11-07 11:10:38 +00:00
|
|
|
#else
|
2018-12-09 02:35:08 +00: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
|
|
|
}
|