summaryrefslogtreecommitdiff
path: root/scrsaver_test.c
blob: 6d65e91a58b0e85578ece85d977b08eb93ba9536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 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);
	}
    }
}