Tutorials/C Programming Tutorial/How to find out desktop idle time

From ThorstensHome
Revision as of 10:09, 18 October 2008 by WikiSysop (Talk)

Jump to: navigation, search

I wrote this little program because I needed it to test the functionality my program ktimetracker is based on. It shows how long you did not do any user-input, waits 10 seconds, and shows again how long you did not do any user-input. I found it works perfect on SUSE Linux 10.3, and on SUSE Linux 11.1. There was a problem outlined here.

#include <QApplication>
#include <QPushButton>
#include <QX11Info>
#include <X11/extensions/scrnsaver.h>
#include <iostream>

using namespace std;

int main(int argc, char ** argv)
{
  QApplication qa(argc,argv);
  QPushButton* qp=new QPushButton("hello world");
  qp->show();
  int event_base, error_base;
  XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base);
  XScreenSaverInfo* _mit_info = XScreenSaverAllocInfo();
  XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), _mit_info);
  cout << _mit_info->idle << endl;
  int test; 
  sleep(10);
  XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), _mit_info);
  cout << _mit_info->idle << endl;
  return qa.exec();
}

Kompile with

qmake -project 
qmake  
make 
g++ -m64 -o x11test main.o -L/usr/lib64 -lQtGui -L/usr/lib64 -L/usr/X11R6/lib64 -lpng -lSM -lICE -lXi -lXrender -lXrandr -lXfixes -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -lrt -ldl -lpthread -lXss 

run with

./x11test