15 July, 2006

Detect idleness, and log-out of Gnome.

/* xidle.c
* Grab XScreenSaver Extension Idle information
* Idea ripped from GAIM
* by Spirilis
* + output modifications by f0rked
* 2005-04-26
*
* I ripped over the original rip. A quick dirty hack
* + logout after 30 mins.
* by Ritesh Khadgaray <khadgaray@gmail.com>
* Fri Jul 14 22:38:55 2006
*
* Distributed under the terms of the GNU General Public License version 2:
* (original GAIM copyright notice follows)
* Gaim is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

*/

/* NOTE
* Original source : http://labs.f0rked.com/xidle.c
*
* This program will disable logout prompt
* logout after 30mins, and kill an apps which refuses to logout.
* this application is unsupported. Use at your own risk.
*
* Build requirement : For RHEL4
* GConf2-devel - for disabling logout prompt
* gtk2-devel - for g_* functions
* xorg-x11-devel - for Xss extension
* psmisc - for killall
*
* Compile using: g++ -o xidle xidle.c -L/usr/X11/lib/ -lXss * `pkg-config gconf-2.0 gtk+-x11-2.0 --libs --cflags`
*
*/


#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>

#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <gconf/gconf-client.h>

#include <stdlib.h>


/* in seconds */

const
guint32 xidle_action_timeout=1800;
/* logout sequence for gnome */

const
gchar xidle_action_execute[]=
"/usr/bin/gnome-session-save --kill"
";sleep 5"
";killall metacity-dialog"
;

static
XScreenSaverInfo *mit_info = NULL;

void

gnome_logout (void)
{

/* Set logout prompt to false, to enable automagic logout */

GConfClient *client = gconf_client_get_default ();
const
gchar key[] = "/apps/gnome-session/options/logout_prompt";

gconf_client_set_bool (client, key, FALSE, NULL);
g_object_unref (client);

/* Logout */

gtk_main_quit ();
system (xidle_action_execute);
}


gboolean check_idle(gpointer *data)
{

/* Check how long system has been idle */

XScreenSaverQueryInfo (GDK_DISPLAY (), GDK_ROOT_WINDOW (),

mit_info);

float
seconds = (float) mit_info->idle / 1000;

g_message ("Idle for: %f\n", seconds);
if
(seconds > xidle_action_timeout){

gnome_logout();
return
FALSE;
}


return
TRUE;
}



int

main
(int argc, char **argv)
{

gtk_init (&argc, &argv);
gconf_init (argc, argv, NULL);

int
event_base, error_base;
if
(!XScreenSaverQueryExtension(GDK_DISPLAY (), &event_base, &error_base))
{

g_error ("Error: XScreenSaver Extension not present\n");

return
-1;
}


mit_info = XScreenSaverAllocInfo ();
g_timeout_add(500,check_idle,NULL);

gtk_main ();
return
0;
}