Skip to content

Instantly share code, notes, and snippets.

@EXL
Last active May 11, 2022 02:04
Show Gist options
  • Save EXL/4085d01ecc0b61022f5a8e2c1c3e0ef6 to your computer and use it in GitHub Desktop.
Save EXL/4085d01ecc0b61022f5a8e2c1c3e0ef6 to your computer and use it in GitHub Desktop.
Shutdowner - simple countdown timer with monitoring user activity and shutdown computer after time up
[Time]
Minutes=0
Seconds=10
#include "Dialog.h"
#include "ui_Dialog.h"
#include "LCDNumber.h"
#include <QSettings>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QSettings settings("config.ini", QSettings::IniFormat);
settings.beginGroup("Time");
int minutes = settings.value("Minutes").toInt();
int seconds = settings.value("Seconds").toInt();
settings.endGroup();
if (minutes == 0 && seconds == 0) {
seconds = 15;
}
ui->lcdNumber->set_Time(minutes, seconds);
ui->lcdNumber->start_Timer(1000);
setWindowTitle("Shutdowner for J()KER by EXL, 2016");
// setWindowFlags(Qt::FramelessWindowHint);
}
Dialog::~Dialog()
{
delete ui;
}
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>504</width>
<height>223</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="LCDNumber" name="lcdNumber"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>LCDNumber</class>
<extends>QLCDNumber</extends>
<header>LCDNumber.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
#include "LCDNumber.h"
#include <QDebug>
#include <QApplication>
#if defined(Q_OS_UNIX)
#include <QLibrary>
#include <QtX11Extras/QX11Info>
#include <X11/Xutil.h>
#elif defined(Q_OS_WIN)
#include <windows.h>
#endif // Q_OS_MACRO
LCDNumber::LCDNumber(QWidget *parent)
: QLCDNumber(parent)
{
timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(setDisplay()));
}
LCDNumber::~LCDNumber()
{
// Empty destructor
}
void LCDNumber::setDisplay()
{
if ((idleTime() < 1000) && !firstTick) {
qApp->exit();
}
if (timeEnd == 0) {
shutDownComputer();
qApp->exit();
}
// qDebug() << timeEnd << idleTime();
this->timeValue->setHMS(0,this->timeValue->addSecs(-1).minute(),this->timeValue->addSecs(-1).second());
this->display(this->timeValue->toString());
timeEnd -= 1;
firstTick = false;
}
void LCDNumber::set_Time(int minutes, int seconds)
{
timeValue = new QTime(0, minutes, seconds);
this->display(timeValue->toString());
timeEnd = minutes * 60 + seconds;
firstTick = true;
}
void LCDNumber::start_Timer(int sec)
{
timer->start(sec);
}
#if defined(Q_OS_UNIX)
bool LCDNumber::shutDownComputer()
{
system("systemctl poweroff");
return true;
}
typedef struct {
Window window; /* screen saver window - may not exist */
int state; /* ScreenSaverOff, ScreenSaverOn, ScreenSaverDisabled*/
int kind; /* ScreenSaverBlanked, ...Internal, ...External */
unsigned long til_or_since; /* time til or since screen saver */
unsigned long idle; /* total time since last user input */
unsigned long eventMask; /* currently selected events for this client */
} XScreenSaverInfo;
typedef XScreenSaverInfo* (*XScreenSaverAllocInfo)();
typedef Status (*XScreenSaverQueryInfo)(Display* display, Drawable* drawable, XScreenSaverInfo* info);
static XScreenSaverAllocInfo _xScreenSaverAllocInfo = 0;
static XScreenSaverQueryInfo _xScreenSaverQueryInfo = 0;
uint LCDNumber::idleTime()
{
static bool xssResolved = false;
if (!xssResolved) {
QLibrary xssLib(QLatin1String("Xss"), 1);
if (xssLib.load()) {
_xScreenSaverAllocInfo = (XScreenSaverAllocInfo) xssLib.resolve("XScreenSaverAllocInfo");
_xScreenSaverQueryInfo = (XScreenSaverQueryInfo) xssLib.resolve("XScreenSaverQueryInfo");
xssResolved = true;
}
}
uint idle = 0;
if (xssResolved)
{
XScreenSaverInfo* info = _xScreenSaverAllocInfo();
const int screen = QX11Info::appScreen();
Qt::HANDLE rootWindow = (void *) QX11Info::appRootWindow(screen);
_xScreenSaverQueryInfo(QX11Info::display(), (Drawable*) rootWindow, info);
idle = info->idle;
if (info)
XFree(info);
}
return idle;
}
#elif defined(Q_OS_WIN)
bool LCDNumber::shutDownComputer()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return( FALSE );
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE |
SHTDN_REASON_FLAG_PLANNED))
return FALSE;
//shutdown was successful
return TRUE;
}
uint LCDNumber::idleTime()
{
uint idle = -1;
LASTINPUTINFO info;
info.cbSize = sizeof(LASTINPUTINFO);
if (::GetLastInputInfo(&info))
idle = ::GetTickCount() - info.dwTime;
return idle;
}
#endif // Q_OS_MACRO
#ifndef LCDNUMBER_H
#define LCDNUMBER_H
#include <QLCDNumber>
#include <QTime>
#include <QTimer>
class LCDNumber : public QLCDNumber
{
Q_OBJECT
public:
explicit LCDNumber(QWidget *parent = 0);
~LCDNumber();
public slots:
void setDisplay();
public:
void start_Timer(int);
void set_Time(int, int);
private:
uint idleTime();
bool shutDownComputer();
private:
int timeEnd;
bool firstTick;
private:
QTimer* timer;
QTime* timeValue;
};
#endif // LCDNUMBER_H
#include "Dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
#-------------------------------------------------
#
# Project created by QtCreator 2016-07-29T22:35:05
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
D_IN = $$PWD/config.ini
D_OUT = $$OUT_PWD
win32: {
D_IN ~= s,/,\\,g
D_OUT ~= s,/,\\,g
}
deploy.commands = $$QMAKE_COPY $$D_IN $$D_OUT
first.depends = $(first) deploy
export(first.depends)
export(deploy.commands)
QMAKE_EXTRA_TARGETS += first deploy
unix: {
QT += x11extras
}
TARGET = Shutdowner
TEMPLATE = app
SOURCES += main.cpp\
Dialog.cpp \
LCDNumber.cpp
HEADERS += Dialog.h \
LCDNumber.h
FORMS += Dialog.ui
unix: {
LIBS += -lX11
}
win: {
QMAKE_LFLAGS += -static -static-libgcc -static-libstdc++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment