HERMITs: Detecting user activity
AGENTS
HERMITs: Detecting user activity
2010-12-01
By
picoFlamingo

In our first demonstration introducing our Pervasive Widgets (the HERMITs), we just used the events on our SDL application (Wendy) to detect the user activity. However that is not very convenient if we want to use proper widgets in our devices, so we need to look for a more transparent way to detect user activity.

As we are running an X-server the XScreensaver extensions are a good option for that. What we want is to be able to detect when there is some user activity, but we do not want to get events every time user press a key or touch the screen... we want them only if that didn't happen for a while.

This is a simple working application using this approach.

/* gcc -lX11 -lXss -o idle idle.c */
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>

Display *display = NULL;
#define IDLE_ALLOWED 5
int state = 0;

int
getIdleTime()
{
  static XScreenSaverInfo *mit_info = NULL;
  int idle_time, event_base, error_base;

  if (XScreenSaverQueryExtension(display, &event_base, &error_base))
    {
      if (mit_info == NULL) mit_info = XScreenSaverAllocInfo();
      XScreenSaverQueryInfo (display, RootWindow(display, 0), mit_info);      
    }
  idle_time = (mit_info->idle) / 1000;
  state = mit_info->state;

  return idle_time;
}

int
main (int argc, char *argv[])
{
  int   t, t1, trigger;
  int   st0, st1;

  display = XOpenDisplay (":0");
  st0 = st1 = t1 =0;
  while (1)
    {
      t = getIdleTime ();
      st1 = state;
      if (t > IDLE_ALLOWED)  trigger = 1;
      if ((t == 0 && trigger) || (st1 == 0 && st0 != 0))
      {
          printf ("[%d] Welcome back (%d) !!!\n", time(NULL), t);
          fflush (NULL);
          trigger = 0;
       }
      st0 = st1;
      sleep (1);
    }
}

To compile this simple application you need to install libxss-dev, then use the following command:

gcc -lX11 -lXss -o idle idle.c 

It compiles like a charm on any Linux PC as well as in the BeagleBoard and the OMAP Zoom2. Actually all the components of the current Hermits demonstration are working in the OMAP Zoom2 pretty well.

Wendy running on OMAP ZOOM 2