sxss

simple x screensaver
git clone git://git.ckyln.com/~cem/sxss
Log | Files | Refs | README | LICENSE

sxss.c (1061B)


      1 /*
      2  * simple X screensaver
      3  */
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <unistd.h>
      7 #include <X11/extensions/scrnsaver.h>
      8 #include <xcb/xproto.h>
      9 
     10 #include "config.h"
     11 
     12 #define VERSION "0.1.0"
     13 
     14 static void
     15 spawn(char *const argv[])
     16 {
     17 	switch (fork()) {
     18 	case 0:
     19 		execvp(argv[0], argv);
     20 		perror("execvp");
     21 		_exit(1);
     22 	case -1:
     23 		perror("fork");
     24 	}
     25 }
     26 
     27 static void
     28 die(const char *str)
     29 {
     30 	fprintf(stderr, "%s\n", str);
     31 	exit(1);
     32 }
     33 
     34 
     35 int
     36 main(int argc, char *argv[])
     37 {
     38 
     39 	if (argc > 1) die("sxss-"VERSION);
     40 
     41 	XScreenSaverInfo *info;
     42 	Display *dpy;
     43 	int base, errbase;
     44 
     45 	if (!(dpy = XOpenDisplay(0))) die("Cannot open display.");
     46 
     47 	if (!XScreenSaverQueryExtension(dpy, &base, &errbase))
     48 		die("Screensaver extension not activated.");
     49 
     50 	while(1) {
     51 
     52 		info = XScreenSaverAllocInfo();
     53 		XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
     54 
     55 		switch(info->state) {
     56 		case ScreenSaverDisabled:
     57 			sleep(10);
     58 			break;
     59 		case ScreenSaverOn:
     60 			break;
     61 		case ScreenSaverOff:
     62 			if (info->til_or_since < 1000) spawn(screensaver);
     63 			break;
     64 		}
     65 		sleep(1);
     66 	}
     67 }