home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / condor40.zip / CONDOR / src / condor_kbdd / idle_serv.c next >
C/C++ Source or Header  |  1989-08-15  |  4KB  |  177 lines

  1. /*
  2.  * Written by Dave Cohrs <dave@cs.wisc.edu>
  3.  */
  4. #include <stdio.h>
  5. #include <utmp.h>
  6. #include <X11/Xlib.h>
  7. #include <sys/time.h>
  8.  
  9. #define MINUTES 60
  10. #define TRUE 1
  11. #define FALSE 0
  12. #define MATCH 0
  13. #define MIN(a,b) ((a)<(b)?(a):(b))
  14.  
  15. int xfd;           /* X file descriptor */
  16. Display *dpy = 0;  /* display */
  17. int screen;        /* default screen */
  18. Window rootwindow; /* default root window */
  19. char *host = 0;    /* DISPLAY name */
  20. XEvent event;      /* For events received. */
  21. time_t    last_event;
  22.  
  23. usage()
  24. {
  25.     fprintf( stderr, "Usage: %s [seconds]\n" );
  26.     exit( 1 );
  27. }
  28.  
  29. main(argc, argv)
  30.     int argc;
  31.     char *argv[];
  32. {
  33.     int        period = 5;
  34.  
  35.     if( argc == 2 ) {
  36.         period = atoi( argv[1] );
  37.  
  38.         if( period <= 0 ) {
  39.             usage( argv[0] );
  40.         }
  41.  
  42.     }
  43.  
  44.     wait_until_X_active( period );
  45.  
  46.         /* open the display and determine well-known some parameters */
  47.     if (!(dpy = XOpenDisplay(host))) {
  48.         fprintf(stderr, "unable to open display '%s'\n",
  49.             XDisplayName(host));
  50.         exit (1);
  51.     }
  52.  
  53.     screen = DefaultScreen(dpy);
  54.     rootwindow = RootWindow(dpy, screen);
  55.     xfd = ConnectionNumber(dpy);
  56.  
  57.     last_event = time( (long *)0 );
  58.     for(;;) {
  59.         if( stays_idle(period) ) {
  60.             printf("Idle time is %d seconds\n", time( (long *)0 ) - last_event);
  61.         } else {
  62.             printf( "Idle time is 0 seconds\n" );
  63.             /* printf( "Sleeping\n" ); */
  64.             sleep( period );
  65.             /* printf( "Waking\n" ); */
  66.             last_event = time( (long *)0 );
  67.         }
  68.     }
  69. }
  70.  
  71. stays_idle( length )
  72. int        length;
  73. {
  74.     fd_set    xmask;              /* mask of file descriptors to check on. */
  75.     int        status;
  76.     struct timeval timeout;    /* holds idle_time timeout */
  77.  
  78.  
  79.         /* Grab key and mouse events */
  80.     XGrabKey(dpy, AnyKey, AnyModifier, rootwindow, False,
  81.          GrabModeSync, GrabModeSync);
  82.     XGrabButton(dpy, AnyButton, AnyModifier, rootwindow, False,
  83.          ButtonPressMask, GrabModeSync, GrabModeSync, None, None );
  84.     XFlush(dpy);
  85.     /* printf( "Grabbed\n" ); */
  86.  
  87.         /* Select to see if we get any events */
  88.     timeout.tv_sec = length;
  89.     timeout.tv_usec = 0;
  90.     FD_ZERO(&xmask);
  91.     FD_SET(xfd, &xmask) ;
  92.     status = select(xfd+1, &xmask, (fd_set *) 0, (fd_set *) 0, &timeout);
  93.  
  94.         /* Eat up any events we got */
  95.     if( status != 0 ) {
  96.         do {
  97.             XNextEvent(dpy, &event);
  98.         } while(XPending(dpy));
  99.     }
  100.  
  101.         /* Ungrab key and mouse, and replay any events we grabbed */
  102.     XAllowEvents(dpy, ReplayKeyboard, CurrentTime);
  103.     XAllowEvents(dpy, ReplayPointer, CurrentTime);
  104.     XUngrabKey(dpy, AnyKey, AnyModifier, rootwindow);
  105.     XUngrabButton(dpy, AnyButton, AnyModifier, rootwindow);
  106.     XFlush(dpy);
  107.     /* printf( "Ungrabbed\n" ); */
  108.  
  109.     return status == 0;
  110. }
  111.  
  112. wait_until_X_active( period )
  113. int        period;
  114. {
  115.     FILE    *fp;
  116.     struct utmp utmp;
  117.     char    XSockName[512];
  118.  
  119.     if( gethostname(XSockName,sizeof XSockName) < 0 ) {
  120.         perror( "gethostname" );
  121.         exit( 1 );
  122.     }
  123.     strcat( XSockName, ":0" );
  124.  
  125.     if( (fp=fopen("/etc/utmp","r")) == NULL ) {
  126.         perror( "fopen of utmp" );
  127.         exit( 1 );
  128.     }
  129.  
  130.     for(;;) {
  131.         while( fread( (char *)&utmp, sizeof utmp, 1, fp ) ) {
  132.             if( utmp.ut_name[0] == '\0' )
  133.                 continue;
  134.  
  135.             if( check_X_active(utmp.ut_host,sizeof(utmp.ut_host),XSockName) ) {
  136.                 (void)fclose( fp );
  137.                 return;
  138.             }
  139.         }
  140.         sleep( period );
  141.     }
  142.  
  143. }
  144.  
  145. check_X_active( ut_host, host_name_len, XSockName )
  146. char    *ut_host;
  147. int        host_name_len;
  148. char    *XSockName;
  149. {
  150.     int     len;
  151.     struct utmp    ut;
  152.  
  153.     if( strcmp(":0",ut_host) == MATCH ) {
  154.         return TRUE;
  155.     }
  156.  
  157.     if( strcmp(":0.0",ut_host) == MATCH ) {
  158.         return TRUE;
  159.     }
  160.  
  161.     if( strcmp("unix:0",ut_host) == MATCH ) {
  162.         return TRUE;
  163.     }
  164.  
  165.     if( strcmp("unix:0.0",ut_host) == MATCH ) {
  166.         return TRUE;
  167.     }
  168.  
  169.         /* allow "<localhostname>:0" or "localhostname:0.0" */
  170.     len = MIN( strlen(XSockName), host_name_len );
  171.     if( strncmp(XSockName,ut_host,len) == MATCH ) {
  172.         return TRUE;
  173.     }
  174.  
  175.     return FALSE;
  176. }
  177.