PDA

View Full Version : Timer to find out if app id idle not working as expected



Cupidvogel
29th December 2015, 15:43
I need to find out when my app is idle for say 20 seconds, and then do something. The logic I thought of is this: run a timer constantly with timeout set to 20 seconds, on any user action, cancel the timer and start it again. Then I found this code here (http://blog.surgeons.org.uk/2010/08/detecting-application-idle-in-qt-4.html) do the same thing. This is the code:



#include "IdleTimer.hpp"

// Static initialisers
IdleTimer *IdleTimer::m_Instance = 0;
QTimer *IdleTimer::m_timer = new QTimer();
int IdleTimer::m_timeout = 0;
QMutex IdleTimer::m_timeoutMutex;

/*!
* Class constructor
*/
IdleTimer::IdleTimer(QObject *parent, int seconds) :
QObject(parent)
{
m_timeoutMutex.lock();

if (seconds)
m_timeout = seconds;

Q_ASSERT(parent);
Q_ASSERT_X(m_timeout, "IdleTimer Constructor", "The timeout must be specified in the first call to instance.");

parent->installEventFilter(this);
m_timer->singleShot(m_timeout*1000, this, SLOT(idleTimeout()));

m_timeoutMutex.unlock();
}

/*!
* Either reset the timeout to a different value to restart the timer
*/
void IdleTimer::start(int seconds/*=0*/) {
m_timeoutMutex.lock();

if(seconds)
m_timeout = seconds;

Q_ASSERT_X(m_timeout, "IdleTimer reset", "A timeout must be specified either in this call or in the first call to instance.");

m_timer->start(m_timeout*1000);

m_timeoutMutex.unlock();
}

void IdleTimer::stop() {
m_timeoutMutex.lock();
m_timer->stop();
m_timeoutMutex.unlock();
}

/*!
* The vent filter
*/
bool IdleTimer::eventFilter(QObject *obj, QEvent *ev)
{
qDebug() << "Ah, caught!" << endl;
if(ev->type() == QEvent::KeyPress ||
ev->type() == QEvent::MouseMove)
// now reset your timer, for example
IdleTimer::m_timer->start();

// Must return to allow further processing
return QObject::eventFilter(obj, ev);
}

// Must return to allow further processing
return QObject::eventFilter(obj, ev);
}

/*slot*/
void IdleTimer::idleTimeout() {
qDebug("Application has been idle, emitting idle signal ...");
emit idle();
}



Header:



#ifndef IDLETIMER_H
#define IDLETIMER_H

#include <qobject>
#include <qmutex>
#include <qevent>
#include <qtimer>

class IdleTimer : public QObject
{
Q_OBJECT
public:
// Singleton class stuff
static IdleTimer* instance(QObject *parent=0, int seconds=0) {
static QMutex mutex;
if (!m_Instance) {
mutex.lock();
m_Instance = new IdleTimer(parent, seconds);
mutex.unlock();
}
return m_Instance;
}

static void drop() {
qDebug("IdleTimer dropped ...");
static QMutex mutex;
mutex.lock();

m_timer->stop();
if(m_timer) delete m_timer;

if (m_Instance)
delete m_Instance;
m_Instance = 0;
mutex.unlock();
}

void start(int seconds = 0);
void stop();

private:
explicit IdleTimer(QObject *parent, int seconds);
explicit IdleTimer() {}
~IdleTimer() {}

IdleTimer(const IdleTimer &); // hide copy constructor
IdleTimer& operator=(const IdleTimer &); // hide assign op

static IdleTimer *m_Instance;
static QTimer *m_timer;

static QMutex m_timeoutMutex;
static int m_timeout;

signals:
void idle();

private slots:
void idleTimeout();

protected:
bool eventFilter(QObject *obj, QEvent *ev);
};

#endif // IDLETIMER_H



This is how I am using this (MyApplication extends QApplication)



MyApplication:: MyApplication()
{
IdleTimer* timer = IdleTimer::instance(this,20);
}


The problem is on app launch, whatever happens, whether I am active or idle, the Application has been idle, emitting idle signal ... gets printed after the first 20 seconds. Moving the mouse, clicking somewhere does not cancel the timer and restart it. But the qDebug within event-filter works correctly on moving/clicking, which means the event filter is working fine, code is coming upto that point, but somehow I am not able to restart the timer as expected.

How do I fix this?

anda_skoa
29th December 2015, 16:10
You are not passing a parent to getInstance() so you are not installing an event filter.

This all looks really complicated, why all the mutexes?

Just create the IdleTimer object in main() and install it as an event filter on the Qt application object.

Btw, there is also http://inqlude.org/libraries/kidletime.html if you want a tested solution.

Cheers,
_

Cupidvogel
29th December 2015, 16:18
Ah. I am sorry. That was posted by mistake. Yes I am passing this and 20 as parameters (otherwise the code won't compile!). And it ensures that the debug statement is printed out 20 seconds after the app launches no matter what!

Yes, the mutex makes it complicated. I removed them and kept it minimal to see if that is causing any problem. Same result. :(

d_stranz
29th December 2015, 16:20
The IdleTimer constructor requires a non-NULL QObject parent, on which it installs the event filter to watch for mouse move and key press events. You don't provide one.


MyApplication:: MyApplication()
{
IdleTimer* timer = IdleTimer::getInstance();
}


And this isn't your real code. IdleTimer has no getInstance() method. Even if you meant to write instance() instead, this code would crash and burn because the IdleTimer constructor requires a non-NULL parent pointer in order for this code to execute:


parent->installEventFilter(this);

I see I type more slowly than you and anda_skoa...

This IdleTimer class is a little messed up anyway - as a singleton, you can have only one of them. If you call instance() a second time, with a different parent, nothing actually happens; because the singleton already exists, it doesn't create a new instance, which means an event filter is never installed on the new parent. The only way it really works is if you install it on the application object where it can filter all events for everything in the app.

Cupidvogel
29th December 2015, 16:23
Yep. That I posted by mistake (I was experimenting with different constructors and copy pasted disjoint versions of the files). :) I mentioned it above as well..

Cupidvogel
30th December 2015, 01:21
I tried this in main.cpp of my app:



Application app(argc, argv);

IdleTimer *timer = IdleTimer::instance(&app,5);
app.installEventFilter(timer);



Still same result. The debug message always comes 20 seconds after the app launches, no mater what I do, like clicking or moving the mouse.

anda_skoa
30th December 2015, 09:17
Does the eventFilter method get called?

Cheers,
_

Cupidvogel
30th December 2015, 17:17
Yes, that's the problem, it does! If I put a simple qDebug in the event filter if conditional, it prints out all the time when I move my mouse or click anything! I cannot understand how it just fails to clear the timer!

anda_skoa
30th December 2015, 17:31
Weird.
Have you tried stop/start?

Cheers,
_

Cupidvogel
30th December 2015, 17:46
Wait, I will try to post the exact code I am currently using. Gimme 5 minutes.

Added after 11 minutes:

IdleTimer.cpp:



#include "IdleTimer.hpp"

// Static initialisers
IdleTimer *IdleTimer::m_Instance = 0;
QTimer *IdleTimer::m_timer = new QTimer();
int IdleTimer::m_timeout = 0;

/*!
* Class constructor
*/
IdleTimer::IdleTimer(QObject *parent, int seconds) :
QObject(parent)
{
if (seconds)
m_timeout = seconds;

Q_ASSERT(parent);
Q_ASSERT_X(m_timeout, "IdleTimer Constructor", "The timeout must be specified in the first call to instance.");

parent->installEventFilter(this);
m_timer->singleShot(m_timeout*1000, this, SLOT(idleTimeout()));

}

/*!
* Either reset the timeout to a different value to restart the timer
*/
void IdleTimer::start(int seconds/*=0*/) {

if(seconds)
m_timeout = seconds;

Q_ASSERT_X(m_timeout, "IdleTimer reset", "A timeout must be specified either in this call or in the first call to instance.");

m_timer->start(m_timeout*1000);

}

void IdleTimer::stop() {
m_timer->stop();
}

/*!
* The vent filter
*/
bool IdleTimer::eventFilter(QObject *obj, QEvent *ev)
{
if(ev->type() == QEvent::KeyPress ||
ev->type() == QEvent::MouseMove)
{
IdleTimer::m_timer->stop();
return QObject::eventFilter(obj, ev);
}

// Must return to allow further processing
return QObject::eventFilter(obj, ev);
}

/*slot*/
void IdleTimer::idleTimeout() {
qDebug("Application has been idle, emitting idle signal ...");
emit idle();
}



IdleTimer.hpp:



#ifndef IDLETIMER_H
#define IDLETIMER_H

#include <qobject>
#include <qevent>
#include <qtimer>

class IdleTimer : public QObject
{
Q_OBJECT
public:
// Singleton class stuff
static IdleTimer* instance(QObject *parent=0, int seconds=0) {
if (!m_Instance) {
m_Instance = new IdleTimer(parent, seconds);
}
return m_Instance;
}

static void drop() {
qDebug("IdleTimer dropped ...");

m_timer->stop();
if(m_timer) delete m_timer;

if (m_Instance)
delete m_Instance;
m_Instance = 0;
}

void start(int seconds = 0);
void stop();

private:
explicit IdleTimer(QObject *parent, int seconds);
explicit IdleTimer() {}
~IdleTimer() {}

IdleTimer(const IdleTimer &); // hide copy constructor
IdleTimer& operator=(const IdleTimer &); // hide assign op

static IdleTimer *m_Instance;
static QTimer *m_timer;

static int m_timeout;

signals:
void idle();

private slots:
void idleTimeout();

protected:
bool eventFilter(QObject *obj, QEvent *ev);
};

#endif // IDLETIMER_H



main.cpp:



int main(int argc, char *argv[])
{
Application app(argc, argv);

IdleTimer *timer = IdleTimer::instance(&app,5);
app.installEventFilter(timer);
//other stuff
}


I think the explicit installEventFilter call in main is not required, since it does it anyway in the IdleTimer constructor. But either way, it doesn't work. Right 5 seconds after app launches, it prints out the message Application has been idle, emitting idle signal ..., even though I am constantly moving the mouse, clicking around..

d_stranz
30th December 2015, 17:54
Just a hunch - when you create the timer, your event loop isn't running yet. What if you create a one-shot zero-timeout timer, and in its timeout slot create your idle timer and start it running? By doing that, you ensure that the event loop is already running when your idle timer gets instantiated.

I agree that your manual call to installEventFilter() in main() is superfluous.

Cupidvogel
30th December 2015, 18:04
That's interesting. But doesn't work. :( I tried this:



#include "IdleTimer.hpp"

// Static initialisers
IdleTimer *IdleTimer::m_Instance = 0;
QTimer *IdleTimer::m_timer = new QTimer();
QTimer *IdleTimer::test_timer = new QTimer();
int IdleTimer::m_timeout = 0;

/*!
* Class constructor
*/
IdleTimer::IdleTimer(QObject *parent, int seconds) :
QObject(parent)
{
fParent = parent;
if (seconds)
m_timeout = seconds;

Q_ASSERT(parent);
Q_ASSERT_X(m_timeout, "IdleTimer Constructor", "The timeout must be specified in the first call to instance.");


test_timer->singleShot(10*1000, this, SLOT(createTimer()));


}

void IdleTimer::createTimer()
{
qDebug() << "Creating my timer" << endl;
fParent->installEventFilter(this);
m_timer->singleShot(m_timeout*1000, this, SLOT(idleTimeout()));
}

/*!
* Either reset the timeout to a different value to restart the timer
*/
void IdleTimer::start(int seconds/*=0*/) {

if(seconds)
m_timeout = seconds;

Q_ASSERT_X(m_timeout, "IdleTimer reset", "A timeout must be specified either in this call or in the first call to instance.");

m_timer->start(m_timeout*1000);

}

void IdleTimer::stop() {
m_timer->stop();
}

/*!
* The vent filter
*/
bool IdleTimer::eventFilter(QObject *obj, QEvent *ev)
{
if(ev->type() == QEvent::KeyPress ||
ev->type() == QEvent::MouseMove)
{
IdleTimer::m_timer->stop();
return QObject::eventFilter(obj, ev);
}

// Must return to allow further processing
return QObject::eventFilter(obj, ev);
}

/*slot*/
void IdleTimer::idleTimeout() {
qDebug("Application has been idle, emitting idle signal ...");
emit idle();
}





#ifndef IDLETIMER_H
#define IDLETIMER_H

#include <qobject>
#include <qevent>
#include <qtimer>

class IdleTimer : public QObject
{
Q_OBJECT
public:
// Singleton class stuff
static IdleTimer* instance(QObject *parent=0, int seconds=0) {
if (!m_Instance) {
m_Instance = new IdleTimer(parent, seconds);
}
return m_Instance;
}

static void drop() {
qDebug("IdleTimer dropped ...");

m_timer->stop();
if(m_timer) delete m_timer;

if (m_Instance)
delete m_Instance;
m_Instance = 0;
}

void start(int seconds = 0);
void stop();

private:
explicit IdleTimer(QObject *parent, int seconds);
explicit IdleTimer() {}
~IdleTimer() {}

IdleTimer(const IdleTimer &); // hide copy constructor
IdleTimer& operator=(const IdleTimer &); // hide assign op

static IdleTimer *m_Instance;
static QTimer *m_timer;
static QTimer *test_timer;

static int m_timeout;
QObject* fParent;

signals:
void idle();

private slots:
void idleTimeout();
void createTimer();

protected:
bool eventFilter(QObject *obj, QEvent *ev);
};

#endif // IDLETIMER_H





int main(int argc, char *argv[])
{
Application app(argc, argv);

IdleTimer *timer = IdleTimer::instance(&app,5);
}


It prints out the creating timer statement after 10 seconds, but then the timeout message 5 seconds after that, even with clicks and mousemove..

d_stranz
30th December 2015, 18:46
Umm, that wasn't exactly what I was thinking. More like this:



int main( int argc, char * argv[] )
{
QApplication app( argc, argv );
MyMainWindow mainWindow;

mainWindow.show();
app.exec();
}

MyMainWindow::MyMainWindow( QWidget * parent ) : QMainWindow( parent )
{
// setupUi, etc.

QTimer::singleShot( 0, this, SLOT( onSSTimeout() ) );
}

void MyMainWindow::onSSTimeout()
{
IdleTimer::instance( qApp, 20 );
}

Cupidvogel
30th December 2015, 19:08
Oh okay. I thought that as well, but then went on with the other way. Lemme try it out..

Added after 19 minutes:

Nope. Still doesn't work. I suspected as much. If it were the event loop problem, then the timer won't even start. Something else is wrong here.. :(

d_stranz
30th December 2015, 20:01
I would strip it down to the bare minimum. Forget the IdleTimer code - you don't need a singleton, you just need a timer that resets itself on a mouse or keyboard event. Derive from QTimer, add the event filter to it, add the timeout slot, and create it in main().

Cupidvogel
30th December 2015, 20:06
Yep. Doing that.. :)