summaryrefslogblamecommitdiff
path: root/scrsaver_test.c
blob: 6d65e91a58b0e85578ece85d977b08eb93ba9536 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                         































































                                                                                
                                                                          



























































                                                                                    
/* gcc -g -Wall -L/usr/X11R6/lib/ -lXss -o test test.c */

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>

Display *dpy;

void elock_open_display(int     argc, char    *argv[])
{
    char *display;

    display = getenv("DISPLAY");
    
    if (!(dpy = XOpenDisplay(display)))
    {
       perror("Cannot open display\n");
       exit(1);
    }
    fprintf(stdout,"DISPLAY OPENDED\n");
}

int fatal(char *s)
{
    printf ("fatal: %s\n", s);
    exit (1);
}

void ignoreError (Display *dpy, XErrorEvent *error)
{
    printf ("ignoring error\n");
}

char *stateNames[] = { "Off", "On", "Cycle", "Disable" };
char *kindNames[] = { "Blanked", "Internal", "External" };

int main(int argc, char **argv)
{
    XEvent pe;
    XScreenSaverNotifyEvent *se;
    Window root, saver;
    int	screen;
    int majorVersion, minorVersion;
    int error_base, event_base;
    XScreenSaverInfo	*info;
    XSetWindowAttributes    attr;
    unsigned long	mask;
    GC			gc;

    /*_Xdebug = 1;*/   /* turn on synchronization */

    elock_open_display(argc, argv);

    if (!XScreenSaverQueryExtension (dpy, &event_base, &error_base))
	fatal ("QueryExtension failed");

    printf ("event_base %d, error_base %d\n", event_base, error_base);

    if (!XScreenSaverQueryVersion (dpy, &majorVersion, &minorVersion))
	fatal ("QueryVersion failed");

    printf ("majorVersion: %d, minorVersion: %d\n", majorVersion, minorVersion);
    
    root = DefaultRootWindow (dpy);

    info = XScreenSaverAllocInfo ();
    if (!XScreenSaverQueryInfo (dpy, root, info))
	fatal ("QueryInfo");
  
  
    if ( stateNames[info->state] == "Disable" || info->til_or_since == 0 )
	    printf("YOU YOU YOU YOU ICI");

    printf ("|| window: 0lx%lx\n", info->window);
    printf ("|| state: %s\n", stateNames[info->state]);
    printf ("|| kind: %s\n", kindNames[info->kind]);
    printf ("|| til_or_since: %ld\n", info->til_or_since);
    printf ("|| idle: %ld\n", info->idle);
    printf ("|| eventMask: 0lx%lx\n\n", info->eventMask);
    saver = info->window;
    XFree (info);
    XScreenSaverSelectInput (dpy, root, ScreenSaverNotifyMask|ScreenSaverCycleMask);
    screen = DefaultScreen(dpy);
    attr.background_pixel = BlackPixel (dpy, screen);
    mask = CWBackPixel;

    XScreenSaverSetAttributes (dpy, root, 0, 0,
			       DisplayWidth(dpy, screen),
			       DisplayHeight (dpy, screen),
			       0,
			       CopyFromParent,
			       CopyFromParent,
			       CopyFromParent,
			       mask, &attr);

    gc = DefaultGC (dpy, screen);
    XSetForeground (dpy, gc, WhitePixel(dpy, screen));
    while (1) 
    {
	if ( ! XNextEvent(dpy, &pe));
		fprintf(stdout,"ERROR\n");
    	fprintf(stdout,"DEBUG pe.type:%s\n",pe.type);	

	if (pe.type == event_base)
	{
	    se = (XScreenSaverNotifyEvent *) &pe;
	    printf ("ScreenSaverNotifyEvent\n");
	    printf ("serial: %ld\n", se->serial);
	    printf ("send_event: %d\n", se->send_event);
	    printf ("window: %ld\n", se->window);
	    printf ("root: %ld\n", se->root);
	    printf ("state: %s\n", stateNames[se->state]);
	    printf ("kind: %s\n", kindNames[se->kind]);
	    printf ("forced: %d\n", se->forced);
	    printf ("time: %ld\n", se->time);
	    if (se->state == ScreenSaverOn && 
		se->kind == ScreenSaverExternal)
	    {
/*		XSetErrorHandler (ignoreError); */
		XDrawString (dpy, saver, gc, 100, 100, "Screen Saver Test", 17);
		XSync (dpy, False);
/*		XSetErrorHandler (NULL); */
	    }
	}
	else
	{
	    printf ("unexpected event %d\n", pe.type);
	}
    }
}