Listing 5: X monitor program (chkact)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/X.h>
#include <stdio.h>
#include <sys/file.h>
#include <signal.h>
/* SCCS footprint */
static char _sid[]="@(#)chkact.c 1.3 95/04/20 10:53:30";
/* chkact <#seconds>
Author: Doug Morris
Purpose: Monitor for activation of screen screen saver triggered
by <#seconds> of display inactivity
Return codes: 0 - screen saver triggered
1 - chkact terminated by signal
Messages:
Usage chkact <#seconds>
Signal received : <sig> <code>
Screen saver activated
*/
Display *dpy; /* X server we're talking to */
/* Catch signals: return non-zero return code and reset screen saver */
void hbrk(sig,code,scp,addr)
int sig,code;
struct sigcontext *scp;
char *addr;
{
printf("Signal received : %d %d\n",sig,code);
XSetScreenSaver(dpy,-1,1,DontPreferBlanking,AllowExposures);
XForceScreenSaver(dpy,ScreenSaverReset);
XCloseDisplay(dpy);
exit(1);
}
main(argc, argv)
int argc;
char **argv;
{
int wait;
XEvent evnt;
Visual *vis;
if(argc == 1){ printf("Usage chkact <seconds>\n"); exit(1);};
if(argc > 1) wait=atoi(argv[1]);
/* Connect to the server */
if ((dpy = XOpenDisplay(NULL)) == (Display *) 0) {
fprintf(stderr, "can't open display\n");
exit(1);
};
/* Select Create events on root window */
XSelectInput(dpy,DefaultRootWindow(dpy),SubstructureNotifyMask|StructureNotifyMask);
/* Catch INT and TERM signals */
signal(SIGINT,hbrk);
signal(SIGTERM,hbrk);
/* Enable screen saver and set time-out to <wait> seconds */
XSetScreenSaver(dpy,wait,1,DontPreferBlanking,AllowExposures);
/* Monitor Create events and wait for screen saver to be activated
When screen saver is activated a special window is created with
id < root
*/
while(1)
{
XNextEvent(dpy,&evnt);
switch(evnt.type) {
case CreateNotify:
if(evnt.xcreatewindow.window < DefaultRootWindow(dpy))
{
printf("Screen saver activated \n");
/* Reset screen saver and exit */
XSetScreenSaver(dpy,-1,1,DontPreferBlanking,AllowExposures);
XForceScreenSaver(dpy,ScreenSaverReset);
XCloseDisplay(dpy);
exit(0);
}
break;
};
};
}
# End of File
|