void IdlenessWatcher::idleTimeout()
{
    uint msSinceUserInput = getIdleTimeMs();
    if (msSinceUserInput >= getMaxIdleTimeoutMs())
    {
        mTimer.stop();
        doAction(mPSettings.getIdlenessAction());
    }
    else
        mTimer.start(getMaxIdleTimeoutMs() - msSinceUserInput);
}
void IdlenessWatcher::idleTimeout()
{
    uint msSinceUserInput = getIdleTimeMs();
    qDebug() << "    ms since user input:" << msSinceUserInput;
    if (msSinceUserInput >= getMaxIdleTimeoutMs())
    {
        doAction(mPSettings.getIdlenessAction());        
    }
    else
    {
        qDebug() << "--- Locking screen in" << (getMaxIdleTimeoutMs() - msSinceUserInput) << "(maybe).";
        mTimer.start(getMaxIdleTimeoutMs() - msSinceUserInput);
    }
}
IdlenessWatcher::IdlenessWatcher(QObject* parent):
    Watcher(parent),
    mPSettings(),
    mErrorNotification(tr("LxQt Idleness watcher failed to start")),
    mDBusWatcher(this),
    mInhibitorCookie(0),
    mIsLocked(false)
{
    qDebug() << "Starting idlenesswatcher";
    mConn = X11Helper::connection();
    xcb_prefetch_extension_data(mConn, &xcb_screensaver_id);
    xcb_prefetch_extension_data(mConn, &xcb_dpms_id);
    xcb_screensaver_query_version_cookie_t verCookie = xcb_screensaver_query_version_unchecked(mConn, XCB_SCREENSAVER_MAJOR_VERSION, XCB_SCREENSAVER_MINOR_VERSION);
    xcb_dpms_get_version_cookie_t dpmsVerCookie = xcb_dpms_get_version_unchecked(mConn, XCB_DPMS_MAJOR_VERSION, XCB_DPMS_MINOR_VERSION);
    // Note that XCB is asynchronous, so we want to make requests ASAP and get the responses as late as possible.

    mScreen = screenOfDisplay(mConn, 0);
    mErrorNotification.setUrgencyHint(LxQt::Notification::UrgencyCritical);
    mErrorNotification.setIcon("object-unlocked");
    mErrorNotification.setTimeout(0);

    new ScreenSaverAdaptor(this);
    QDBusConnection sessionBus = QDBusConnection::sessionBus();
    if (!sessionBus.registerService("org.freedesktop.ScreenSaver")
        || !sessionBus.registerObject("/ScreenSaver", this))
    {
        mErrorNotification.setBody(tr("D-Bus interface org.freedesktop.ScreenSaver is already registered"));
        mErrorNotification.update();
        qWarning() << "ERROR: D-Bus interface org.freedesktop.ScreenSaver is already registered";
    }

    mDBusWatcher.setConnection(QDBusConnection::sessionBus());
    mDBusWatcher.setWatchMode(QDBusServiceWatcher::WatchForUnregistration);

    connect(&mTimer, SIGNAL(timeout()), SLOT(idleTimeout()));
    connect(&mPSettings, SIGNAL(settingsChanged()), SLOT(restartTimer()));
    connect(&mDBusWatcher, SIGNAL(serviceUnregistered(QString)), SLOT(serviceUnregistered(QString)));
    connect(&mLockProcess, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(screenUnlocked(int,QProcess::ExitStatus)));
    connect(&mErrorNotification, SIGNAL(actionActivated(int)), SLOT(notificationAction(int)));

    // Get XCB responses ...
    const xcb_query_extension_reply_t* extReply = xcb_get_extension_data(mConn, &xcb_screensaver_id);
    const xcb_query_extension_reply_t* dpmsExtReply = xcb_get_extension_data(mConn, &xcb_dpms_id);
    xcb_screensaver_query_version_reply_t* verReply = xcb_screensaver_query_version_reply(mConn, verCookie, NULL);
    xcb_dpms_get_version_reply_t* dpmsVerReply = xcb_dpms_get_version_reply(mConn, dpmsVerCookie, NULL);

    if (mScreen && extReply && extReply->present && dpmsExtReply && dpmsExtReply->present
        && verReply && dpmsVerReply
        && verReply->server_major_version == XCB_SCREENSAVER_MAJOR_VERSION
        && verReply->server_minor_version >= XCB_SCREENSAVER_MINOR_VERSION
      //&& dpmsVerReply->server_major_version == XCB_DPMS_MAJOR_VERSION
      //&& dpmsVerReply->server_minor_version >= XCB_DPMS_MINOR_VERSION
            )
    {
        free(verReply);
        free(dpmsVerReply);
    }
    else
    {
        mErrorNotification.setBody(tr("The X11 Screensaver extension is not usable"));
        mErrorNotification.update();
        if (verReply)
            free(verReply);
        qCritical() << "ERROR: Can't use the X11 Screensaver Extension!";
    }

    mErrorNotification.setActions(QStringList(tr("Configure...")));

    qDebug() << "LxQt Screenlocker started.";
    qDebug() << "timeout:" << getMaxIdleTimeoutMs() << "ms, lock command:" << mLockCommand;
    restartTimer();
}
void IdlenessWatcher::restartTimer()
{
    qDebug() << ">>> Timer Restarted, waiting: " << getMaxIdleTimeoutMs() << "msecs";
    mTimer.start(getMaxIdleTimeoutMs());
}