2f11bc0d2cdb29ce0f1337dff519b0c285bcce50
[xscreensaver] / driver / test-vp.c
1 /* test-xinerama.c --- playing with XF86VidModeGetViewPort
2  * xscreensaver, Copyright (c) 2004 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 <time.h>
24 #include <sys/time.h>
25
26 #include <X11/Xlib.h>
27 #include <X11/Xatom.h>
28 #include <X11/Intrinsic.h>
29
30 #include <X11/Xproto.h>
31 #include <X11/extensions/xf86vmode.h>
32 #include <X11/extensions/Xinerama.h>
33
34 char *progname = 0;
35 char *progclass = "XScreenSaver";
36
37 static const char *
38 blurb (void)
39 {
40   static char buf[255];
41   time_t now = time ((time_t *) 0);
42   char *ct = (char *) ctime (&now);
43   int n = strlen(progname);
44   if (n > 100) n = 99;
45   strncpy(buf, progname, n);
46   buf[n++] = ':';
47   buf[n++] = ' ';
48   strncpy(buf+n, ct+11, 8);
49   strcpy(buf+n+9, ": ");
50   return buf;
51 }
52
53
54 static int
55 screen_count (Display *dpy)
56 {
57   int n = ScreenCount(dpy);
58   int xn = 0;
59   int event_number, error_number;
60
61   if (!XineramaQueryExtension (dpy, &event_number, &error_number))
62     {
63       fprintf(stderr, "%s: XineramaQueryExtension(dpy, ...)    ==> False\n",
64               blurb());
65       goto DONE;
66     }
67   else
68     fprintf(stderr,   "%s: XineramaQueryExtension(dpy, ...)    ==> %d, %d\n",
69             blurb(), event_number, error_number);
70
71   if (!XineramaIsActive(dpy))
72     {
73       fprintf(stderr, "%s: XineramaIsActive(dpy)               ==> False\n",
74               blurb());
75       goto DONE;
76     }
77   else
78     {
79       int major, minor;
80       XineramaScreenInfo *xsi;
81       fprintf(stderr, "%s: XineramaIsActive(dpy)               ==> True\n",
82               blurb());
83       if (!XineramaQueryVersion(dpy, &major, &minor))
84         {
85           fprintf(stderr,
86                   "%s: XineramaQueryVersion(dpy, ...)      ==> False\n",
87                   blurb());
88           goto DONE;
89         }
90       else
91         fprintf(stderr,
92                 "%s: XineramaQueryVersion(dpy, ...)      ==> %d, %d\n",
93                 blurb(), major, minor);
94
95       xsi = XineramaQueryScreens (dpy, &xn);
96       if (xsi) XFree (xsi);
97     }
98
99  DONE:
100   fprintf (stderr, "\n");
101   fprintf (stderr, "%s: X client screens: %d\n", blurb(), n);
102   fprintf (stderr, "%s: Xinerama screens: %d\n", blurb(), xn);
103   fprintf (stderr, "\n");
104
105   if (xn > n) return xn;
106   else return n;
107 }
108
109
110 int
111 main (int argc, char **argv)
112 {
113   int event_number, error_number;
114   int major, minor;
115   int nscreens = 0;
116   int i;
117
118   XtAppContext app;
119   Widget toplevel_shell = XtAppInitialize (&app, progclass, 0, 0,
120                                            &argc, argv, 0, 0, 0);
121   Display *dpy = XtDisplay (toplevel_shell);
122   XtGetApplicationNameAndClass (dpy, &progname, &progclass);
123
124   if (!XF86VidModeQueryExtension(dpy, &event_number, &error_number))
125     {
126       fprintf(stderr, "%s: XF86VidModeQueryExtension(dpy, ...) ==> False\n",
127               blurb());
128       fprintf(stderr,
129               "%s: server does not support the XF86VidMode extension.\n",
130               blurb());
131       exit(1);
132     }
133   else
134     fprintf(stderr, "%s: XF86VidModeQueryExtension(dpy, ...) ==> %d, %d\n",
135             blurb(), event_number, error_number);
136
137   if (!XF86VidModeQueryVersion(dpy, &major, &minor))
138     {
139       fprintf(stderr, "%s: XF86VidModeQueryVersion(dpy, ...) ==> False\n",
140               blurb());
141       fprintf(stderr,
142               "%s: server didn't report XF86VidMode version numbers?\n",
143               blurb());
144     }
145   else
146     fprintf(stderr, "%s: XF86VidModeQueryVersion(dpy, ...)   ==> %d, %d\n",
147             blurb(), major, minor);
148
149   nscreens = screen_count (dpy);
150
151   for (i = 0; i < nscreens; i++)
152     {
153       int result;
154       int x = 0, y = 0, dot = 0;
155       XF86VidModeModeLine ml = { 0, };
156
157       result = XF86VidModeGetViewPort (dpy, i, &x, &y);
158       if (! result)
159         fprintf(stderr, "%s: XF86VidModeGetViewPort(dpy, %d, ...) ==> %d\n",
160                 blurb(), i, result);
161
162       result = XF86VidModeGetModeLine (dpy, i, &dot, &ml);
163       if (! result)
164         fprintf(stderr, "%s: XF86VidModeGetModeLine(dpy, %d, ...) ==> %d\n",
165                 blurb(), i, result);
166
167       fprintf (stderr, "%s:   screen %d: %dx%d; viewport: %dx%d+%d+%d\n",
168                blurb(), i,
169                DisplayWidth (dpy, i), DisplayHeight (dpy, i),
170                ml.hdisplay, ml.vdisplay, x, y
171                );
172
173       fprintf (stderr,
174                "%s:       hsync start %d; end %d; total %d; skew  %d;\n",
175                blurb(),
176                ml.hsyncstart, ml.hsyncend, ml.htotal, ml.hskew);
177       fprintf (stderr,
178                "%s:       vsync start %d; end %d; total %d; flags 0x%04x;\n",
179                blurb(),
180                ml.vsyncstart, ml.vsyncend, ml.vtotal, ml.flags);
181       fprintf (stderr, "\n");
182     }
183   XSync (dpy, False);
184   exit (0);
185 }