summaryrefslogblamecommitdiffstats
path: root/src/saverwidget.cpp
blob: d7f3a74965bcdfecd3452789d5a0d88d5ab54048 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                        
                         
                   




                    
                






















                                                                                                                                          
                                         



                            



                                       





                                        
                                




                                                                   










                                                                                                               
                    
                                               
             


                            
         
                                                                                       
                                                                                                          




                                                                










                                                         














                                                                 
                                                                                     


                                                                    
                                          






















                                                                              
#include "saverwidget.h"
#include "ui_saver.h"
#include "screensaver.h"
#include "deadlinetype.h"
#include "config.h"

#include <QTimer>
#include <QX11Info>
#include <QDateTime>
#include <QSettings>
#include <QFile>
#include <X11/Xlib.h>

static const QChar C_ZERO('0');

static QString formatTime(qlonglong span)
{
    QString ret;
    qlonglong days = span / 86400;
    if (days > 0) {
        ret = QString::number(days) + "d ";
        span += 86400 * days;
    }
    return ret + QString("%1:%2:%3").arg(span / 3600, 2, 10, C_ZERO).arg((span % 3600) / 60, 2, 10, C_ZERO).arg(span % 60, 2, 10, C_ZERO);
}

SaverWidget::SaverWidget(WId parentWinId, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Saver),
    _parentWinId(parentWinId),
    _lastMouseFake(0),
    _logoutDeadline(0),
    _shutdownDeadline(0),
    _counter(0),
    _deadlineType(DeadlineType::Unknown),
    _standbyDisabled(false),
    _isLocked(false)
{
    ui->setupUi(this);
    QString qss = Config::getMainQss();
    if (!qss.isEmpty()) {
        this->setStyleSheet(qss);
    }
    QTimer *t = new QTimer(this);
    t->setInterval(1000);
    connect(t, &QTimer::timeout, [=]() {
        if (_counter % 60 == 0) {
            this->reloadValues();
        }
        if (_counter % 4 == 0) {
            _isLocked = ScreenSaver::isLocked();
        }
        _counter++;
        qlonglong now = QDateTime::currentMSecsSinceEpoch() / 1000;
        // Determine what's timing out next (session/schedule)
        const DeadlineType *type;
        qlonglong remaining = 0;
        do {
            qlonglong shutdownRemaining = _shutdownDeadline - now;
            qlonglong logoutRemaining = _logoutDeadline - now;
            if (shutdownRemaining > -330 && (shutdownRemaining < logoutRemaining || logoutRemaining <= -330)) {
                type = DeadlineType::Shutdown;
                remaining = shutdownRemaining;
            } else if (logoutRemaining > -330) {
                type = DeadlineType::IdleKill;
                remaining = logoutRemaining;
            } else {
                type = DeadlineType::NoTimeout;
            }
        } while (0);
        if (remaining < 0) {
            remaining = 0;
        }
        ui->lblHeader->setText(type->headerLine(_isLocked).arg(formatTime(remaining)));
        ui->lblClock->setText(QDateTime::currentDateTime().toString(QLocale().dateFormat() + "  HH:mm "));
        if (_deadlineType != type) {
            _deadlineType = type;
            ui->lblText->setText(type->bodyText(_isLocked));
        }
        bool shouldDisable = (remaining > 0 && remaining < 310);
        if (_standbyDisabled != shouldDisable) {
            _standbyDisabled = shouldDisable;
            if (shouldDisable) {
                ScreenSaver::allowSaverAndStandby(false);
                ScreenSaver::forceScreenOn();
            } else {
                ScreenSaver::allowSaverAndStandby(true);
            }
        }
    });
    t->start();
    reloadValues();
}

SaverWidget::~SaverWidget()
{
    delete ui;
}

void SaverWidget::reloadValues()
{
    QString display = QString::fromLocal8Bit(qgetenv("DISPLAY"));
    int i = display.indexOf('.');
    if (i != -1) {
        display = display.left(i);
    }
    QSettings s(QString("/run/openslx/idleaction-") + display, QSettings::IniFormat);
    _logoutDeadline = s.value("lockDeadline", 0).toLongLong();
    _shutdownDeadline = s.value("shutdownDeadline", 0).toLongLong();
    // TODO: Load text strings for everything
    _deadlineType = DeadlineType::Unknown;
}

void SaverWidget::mouseMoveEvent(QMouseEvent *)
{
    /*
    // Doesn't work, method is never called :-(
    qint64 now = QDateTime::currentMSecsSinceEpoch();
    if (now - _lastMouseFake < 1000)
        return;
    ui->lblText->setText(QString::number(int(now / 1000)));
    _lastMouseFake = now;
    XEvent ev;
    memset(&ev, 0, sizeof(ev));
    ev.type = ButtonPress;
    ev.xbutton.button = 1;
    ev.xbutton.same_screen = True;
    ev.xbutton.root = ev.xbutton.window = ev.xbutton.subwindow = _parentWinId;
    XSendEvent(QX11Info::display(), _parentWinId, True, 0xfff, &ev);
    ev.type = ButtonRelease;
    ev.xbutton.state = 0x100;
    XSendEvent(QX11Info::display(), _parentWinId, True, 0xfff, &ev);
    */
}