http://www.jwz.org/xscreensaver/xscreensaver-5.07.tar.gz
[xscreensaver] / driver / test-screens.c
1 /* test-screens.c --- some test cases for the "monitor sanity" checks.
2  * xscreensaver, Copyright (c) 2008 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 <X11/Xlib.h>
18
19 /* This file doesn't need the Xt headers, so stub these types out... */
20 #undef XtPointer
21 #define XtAppContext void*
22 #define XrmDatabase  void*
23 #define XtIntervalId void*
24 #define XtPointer    void*
25 #define Widget       void*
26
27 #include "xscreensaver.h"
28 #include "visual.h"
29
30 #undef WidthOfScreen
31 #undef HeightOfScreen
32 #define WidthOfScreen(s) 10240
33 #define HeightOfScreen(s) 10240
34
35 #undef screen_number
36 #define screen_number(s) ((int) s)
37
38 #include "screens.c"   /* to get at static void check_monitor_sanity() */
39
40 char *progname = 0;
41 char *progclass = "XScreenSaver";
42
43 const char *blurb(void) { return progname; }
44
45 Bool safe_XF86VidModeGetViewPort(Display *d, int i, int *x, int *y) { abort(); }
46 void initialize_screen_root_widget(saver_screen_info *ssi) { abort(); }
47 Visual *get_best_gl_visual (saver_info *si, Screen *sc) { abort(); }
48
49
50 static const char *
51 failstr (monitor_sanity san)
52 {
53   switch (san) {
54   case S_SANE:      return "OK";
55   case S_ENCLOSED:  return "ENC";
56   case S_DUPLICATE: return "DUP";
57   case S_OVERLAP:   return "OVR";
58   case S_OFFSCREEN: return "OFF";
59   case S_DISABLED:  return "DIS";
60   default: abort(); break;
61   }
62 }
63
64
65 static void
66 test (int testnum, const char *screens, const char *desired)
67 {
68   monitor *monitors[100];
69   char result[2048];
70   char *out = result;
71   int i, nscreens = 0;
72   char *token = strtok (strdup(screens), ",");
73   while (token)
74     {
75       monitor *m = calloc (1, sizeof (monitor));
76       char c;
77       m->id = (testnum * 1000) + nscreens;
78       if (5 == sscanf (token, "%dx%d+%d+%d@%d%c", 
79                        &m->width, &m->height, &m->x, &m->y, 
80                        (int *) &m->screen, &c))
81         ;
82       else if (4 != sscanf (token, "%dx%d+%d+%d%c", 
83                             &m->width, &m->height, &m->x, &m->y, &c))
84         {
85           fprintf (stderr, "%s: unparsable geometry: %s\n", blurb(), token);
86           exit (1);
87         }
88       monitors[nscreens] = m;
89       nscreens++;
90       token = strtok (0, ",");
91     }
92   monitors[nscreens] = 0;
93
94   check_monitor_sanity (monitors);
95
96   *out = 0;
97   for (i = 0; i < nscreens; i++)
98     {
99       monitor *m = monitors[i];
100       if (out != result) *out++ = ',';
101       if (m->sanity == S_SANE)
102         {
103           sprintf (out, "%dx%d+%d+%d", m->width, m->height, m->x, m->y);
104           if (m->screen)
105             sprintf (out + strlen(out), "@%d", (int) m->screen);
106         }
107       else
108         strcpy (out, failstr (m->sanity));
109       out += strlen(out);
110     }
111   *out = 0;
112
113   if (!strcmp (result, desired))
114     fprintf (stderr, "%s: test %2d OK\n", blurb(), testnum);
115   else
116     fprintf (stderr, "%s: test %2d FAILED:\n"
117              "%s:   given: %s\n"
118              "%s:  wanted: %s\n"
119              "%s:     got: %s\n",
120              blurb(), testnum,
121              blurb(), screens, 
122              blurb(), desired, 
123              blurb(), result);
124
125 # if 0
126   {
127     saver_info SI;
128     SI.monitor_layout = monitors;
129     describe_monitor_layout (&SI);
130   }
131 # endif
132
133 }
134
135 static void
136 run_tests(void)
137 {
138   int i = 1;
139 # define A(a)   test (i++, a, a);
140 # define B(a,b) test (i++, a, b)
141
142   A("");
143   A("1024x768+0+0");
144   A("1024x768+0+0,1024x768+1024+0");
145   A("1024x768+0+0,1024x768+0+768");
146   A("1024x768+0+0,1024x768+0+768,1024x768+1024+0");
147   A("800x600+0+0,800x600+0+0@1,800x600+10+0@2");
148
149   B("1024x768+999999+0",
150     "OFF");
151   B("1024x768+-999999+-999999",
152     "OFF");
153   B("1024x768+0+0,1024x768+0+0",
154     "1024x768+0+0,DUP");
155   B("1024x768+0+0,1024x768+0+0,1024x768+0+0",
156     "1024x768+0+0,DUP,DUP");
157   B("1024x768+0+0,1024x768+1024+0,1024x768+0+0",
158     "1024x768+0+0,1024x768+1024+0,DUP");
159   B("1280x1024+0+0,1024x768+0+64,800x600+0+0,640x480+0+0,720x400+0+0",
160     "1280x1024+0+0,ENC,ENC,ENC,ENC");
161   B("1024x768+0+64,1280x1024+0+0,800x600+0+0,640x480+0+0,800x600+0+0,720x400+0+0",
162     "ENC,1280x1024+0+0,ENC,ENC,ENC,ENC");
163   B("1024x768+0+64,1280x1024+0+0,800x600+0+0,640x480+0+0,1280x1024+0+0,720x400+0+0",
164     "ENC,1280x1024+0+0,ENC,ENC,DUP,ENC");
165   B("720x400+0+0,640x480+0+0,800x600+0+0,1024x768+0+64,1280x1024+0+0",
166     "ENC,ENC,ENC,ENC,1280x1024+0+0");
167   B("1280x1024+0+0,800x600+1280+0,800x600+1300+0",
168     "1280x1024+0+0,800x600+1280+0,OVR");
169   B("1280x1024+0+0,800x600+1280+0,800x600+1300+0,1280x1024+0+0,800x600+1280+0",
170     "1280x1024+0+0,800x600+1280+0,OVR,DUP,DUP");
171
172   /*  +-------------+----+  +------+---+   1: 1440x900,  widescreen display
173       |             :    |  | 3+4  :   |   2: 1280x1024, conventional display
174       |     1+2     : 1  |  +......+   |   3: 1024x768,  laptop
175       |             :    |  |  3       |   4: 800x600,   external projector
176       +.............+----+  +----------+
177       |      2      |
178       |             |
179       +-------------+
180    */
181   B("1440x900+0+0,1280x1024+0+0,1024x768+1440+0,800x600+1440+0",
182     "1440x900+0+0,OVR,1024x768+1440+0,ENC");
183   B("800x600+0+0,800x600+0+0,800x600+800+0",
184     "800x600+0+0,DUP,800x600+800+0");
185   B("1600x1200+0+0,1360x768+0+0",
186     "1600x1200+0+0,ENC");
187   B("1600x1200+0+0,1360x768+0+0,1600x1200+0+0@1,1360x768+0+0@1",
188     "1600x1200+0+0,ENC,1600x1200+0+0@1,ENC");
189 }
190
191
192 int
193 main (int argc, char **argv)
194 {
195   char *s;
196   progname = argv[0];
197   s = strrchr(progname, '/');
198   if (s) progname = s+1;
199   if (argc != 1)
200     {
201       fprintf (stderr, "usage: %s\n", argv[0]);
202       exit (1);
203     }
204
205   run_tests();
206
207   exit (0);
208 }