ftp://ftp.smr.ru/pub/0/FreeBSD/releases/distfiles/xscreensaver-3.16.tar.gz
[xscreensaver] / driver / test-grab.c
1 /* test-uid.c --- playing with grabs.
2  * xscreensaver, Copyright (c) 1999 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <stdlib.h>
18 #ifdef HAVE_UNISTD_H
19 # include <unistd.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/time.h>
24
25 #include <X11/Xlib.h>
26 #include <X11/Intrinsic.h>
27
28 char *progname = 0;
29 char *progclass = "XScreenSaver";
30
31 #define ALL_POINTER_EVENTS \
32         (ButtonPressMask | ButtonReleaseMask | EnterWindowMask | \
33          LeaveWindowMask | PointerMotionMask | PointerMotionHintMask | \
34          Button1MotionMask | Button2MotionMask | Button3MotionMask | \
35          Button4MotionMask | Button5MotionMask | ButtonMotionMask)
36
37 int
38 main (int argc, char **argv)
39 {
40   XtAppContext app;
41   int kstatus, mstatus;
42   Cursor cursor = 0;
43   int delay = 60 * 15;
44   Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
45                                            &argc, argv, 0, 0, 0);
46   Display *dpy = XtDisplay (toplevel_shell);
47   Window w = RootWindow (dpy, DefaultScreen(dpy));
48   XtGetApplicationNameAndClass (dpy, &progname, &progclass);
49
50   kstatus = XGrabKeyboard (dpy, w, True,
51                            GrabModeSync, GrabModeAsync,
52                            CurrentTime);
53   fprintf (stderr, "%s: grabbing keyboard on 0x%x... %s.\n",
54            progname, (unsigned long) w,
55            (kstatus == GrabSuccess ? "GrabSuccess" :
56             kstatus == AlreadyGrabbed ? "AlreadyGrabbed" :
57             kstatus == GrabInvalidTime ? "GrabInvalidTime" :
58             kstatus == GrabNotViewable ? "GrabNotViewable" :
59             kstatus == GrabFrozen ? "GrabFrozen" :
60             "???"));
61
62   mstatus = XGrabPointer (dpy, w, True, ALL_POINTER_EVENTS,
63                           GrabModeAsync, GrabModeAsync, None,
64                           cursor, CurrentTime);
65   fprintf (stderr, "%s: grabbing mouse on 0x%x... %s.\n",
66            progname, (unsigned long) w,
67            (mstatus == GrabSuccess ? "GrabSuccess" :
68             mstatus == AlreadyGrabbed ? "AlreadyGrabbed" :
69             mstatus == GrabInvalidTime ? "GrabInvalidTime" :
70             mstatus == GrabNotViewable ? "GrabNotViewable" :
71             mstatus == GrabFrozen ? "GrabFrozen" :
72             "???"));
73
74   XSync(dpy, False);
75
76   if (kstatus == GrabSuccess || mstatus == GrabSuccess)
77     {
78       fprintf (stderr, "%s: sleeping for %d:%02d:%02d...\n",
79                progname,
80                delay / (60 * 60),
81                (delay % (60 * 60)) / 60,
82                delay % 60);
83       fflush(stderr);
84       sleep (delay);
85       XSync(dpy, False);
86     }
87
88   exit (0);
89 }