http://ftp.x.org/contrib/applications/xscreensaver-2.34.tar.gz
[xscreensaver] / driver / test-passwd.c
1 /* xscreensaver, Copyright (c) 1998 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 /* This is a kludgy test harness for debugging the password dialog box.
13    It's somewhat easier to debug it here than in the xscreensaver executable
14    itself.
15  */
16
17 #define WHICH 0
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <X11/Xlib.h>
26 #include <X11/Xatom.h>
27 #include <X11/Intrinsic.h>
28 #include <X11/StringDefs.h>
29 #include <X11/Shell.h>
30
31 #include "xscreensaver.h"
32 #include "resources.h"
33 #include "version.h"
34
35 char *progname = 0;
36 char *progclass = 0;
37 XrmDatabase db = 0;
38 saver_info *global_si_kludge;
39
40 FILE *real_stderr, *real_stdout;
41
42 void reset_stderr(saver_screen_info *ssi) {}
43 void clear_stderr(saver_screen_info *ssi) {}
44 void reset_watchdog_timer(saver_info *si, Bool on_p) {}
45 void monitor_power_on (saver_info *si) {}
46 void grab_keyboard_and_mouse (saver_info *si, Window w, Cursor c) {}
47 void ungrab_keyboard_and_mouse (saver_info *si) {}
48 Bool select_visual (saver_screen_info *ssi, const char *v) { return False; }
49 void raise_window (saver_info *si, Bool i, Bool b, Bool d) {}
50 void restore_real_vroot (saver_info *si) {}
51 void saver_exit (saver_info *si, int status, const char *core) { exit(status);}
52 const char * signal_name(int signal) { return "???"; }
53 Bool monitor_powered_on_p (saver_info *si) { return True; }
54 void start_notice_events_timer (saver_info *si, Window w) {}
55 void initialize_screensaver_window (saver_info *si) {}
56 void blank_screen (saver_info *si) {}
57 void unblank_screen (saver_info *si) {}
58 Bool handle_clientmessage (saver_info *si, XEvent *e, Bool u) { return False; }
59 int BadWindow_ehandler (Display *dpy, XErrorEvent *error) { exit(1); }
60 int write_init_file (saver_info *si) { return 0;}
61 Bool window_exists_p (Display *dpy, Window window) {return True;}
62
63 const char *blurb(void) { return progname; }
64 Atom XA_SCREENSAVER, XA_DEMO, XA_PREFS;
65
66
67 void
68 idle_timer (XtPointer closure, XtIntervalId *id)
69 {
70   saver_info *si = (saver_info *) closure;
71   XEvent fake_event;
72   fake_event.type = 0;  /* XAnyEvent type, ignored. */
73   fake_event.xany.display = si->dpy;
74   fake_event.xany.window  = 0;
75   XPutBackEvent (si->dpy, &fake_event);
76 }
77
78
79 static char *
80 reformat_hack (const char *hack)
81 {
82   int i;
83   const char *in = hack;
84   int indent = 13;
85   char *h2 = (char *) malloc(strlen(in) + indent + 2);
86   char *out = h2;
87
88   while (isspace(*in)) in++;            /* skip whitespace */
89   while (*in && !isspace(*in) && *in != ':')
90     *out++ = *in++;                     /* snarf first token */
91   while (isspace(*in)) in++;            /* skip whitespace */
92
93   if (*in == ':')
94     *out++ = *in++;                     /* copy colon */
95   else
96     {
97       in = hack;
98       out = h2;                         /* reset to beginning */
99     }
100
101   *out = 0;
102
103   while (isspace(*in)) in++;            /* skip whitespace */
104   for (i = strlen(h2); i < indent; i++) /* indent */
105     *out++ = ' ';
106
107   /* copy the rest of the line. */
108   while (*in)
109     {
110       /* shrink all whitespace to one space, for the benefit of the "demo"
111          mode display.  We only do this when we can easily tell that the
112          whitespace is not significant (no shell metachars).
113        */
114       switch (*in)
115         {
116         case '\'': case '"': case '`': case '\\':
117           {
118             /* Metachars are scary.  Copy the rest of the line unchanged. */
119             while (*in)
120               *out++ = *in++;
121           }
122           break;
123         case ' ': case '\t':
124           {
125             while (*in == ' ' || *in == '\t')
126               in++;
127             *out++ = ' ';
128           }
129           break;
130         default:
131           *out++ = *in++;
132           break;
133         }
134     }
135   *out = 0;
136
137   /* strip trailing whitespace. */
138   out = out-1;
139   while (out > h2 && (*out == ' ' || *out == '\t' || *out == '\n'))
140     *out-- = 0;
141
142   return h2;
143 }
144
145
146 static void
147 get_screenhacks (saver_info *si)
148 {
149   saver_preferences *p = &si->prefs;
150   int i = 0;
151   int start = 0;
152   int end = 0;
153   int size;
154   char *d;
155
156   d = get_string_resource ("monoPrograms", "MonoPrograms");
157   if (d && !*d) { free(d); d = 0; }
158   if (!d)
159     d = get_string_resource ("colorPrograms", "ColorPrograms");
160   if (d && !*d) { free(d); d = 0; }
161
162   if (d)
163     {
164       fprintf (stderr,
165        "%s: the `monoPrograms' and `colorPrograms' resources are obsolete;\n\
166         see the manual for details.\n", blurb());
167       free(d);
168     }
169
170   d = get_string_resource ("programs", "Programs");
171
172   if (p->screenhacks)
173     {
174       for (i = 0; i < p->screenhacks_count; i++)
175         if (p->screenhacks[i])
176           free (p->screenhacks[i]);
177       free(p->screenhacks);
178       p->screenhacks = 0;
179     }
180
181   if (!d || !*d)
182     {
183       p->screenhacks_count = 0;
184       p->screenhacks = 0;
185       return;
186     }
187
188   size = strlen (d);
189
190
191   /* Count up the number of newlines (which will be equal to or larger than
192      the number of hacks.)
193    */
194   i = 0;
195   for (i = 0; d[i]; i++)
196     if (d[i] == '\n')
197       i++;
198   i++;
199
200   p->screenhacks = (char **) calloc (sizeof (char *), i+1);
201
202   /* Iterate over the lines in `d' (the string with newlines)
203      and make new strings to stuff into the `screenhacks' array.
204    */
205   p->screenhacks_count = 0;
206   while (start < size)
207     {
208       /* skip forward over whitespace. */
209       while (d[start] == ' ' || d[start] == '\t' || d[start] == '\n')
210         start++;
211
212       /* skip forward to newline or end of string. */
213       end = start;
214       while (d[end] != 0 && d[end] != '\n')
215         end++;
216
217       /* null terminate. */
218       d[end] = 0;
219
220       p->screenhacks[p->screenhacks_count++] = reformat_hack (d + start);
221       if (p->screenhacks_count >= i)
222         abort();
223
224       start = end+1;
225     }
226
227   if (p->screenhacks_count == 0)
228     {
229       free (p->screenhacks);
230       p->screenhacks = 0;
231     }
232 }
233
234
235
236 static char *fallback[] = {
237 #include "XScreenSaver_ad.h"
238  0
239 };
240
241 int
242 main (int argc, char **argv)
243 {
244   Widget toplevel_shell;
245   saver_screen_info ssip;
246   saver_info sip;
247   saver_info *si = &sip;
248   saver_preferences *p = &si->prefs;
249
250   memset(&sip, 0, sizeof(sip));
251   memset(&ssip, 0, sizeof(ssip));
252
253   si->nscreens = 1;
254   si->screens = si->default_screen = &ssip;
255   ssip.global = si;
256
257   global_si_kludge = si;
258   real_stderr = stderr;
259   real_stdout = stdout;
260
261   si->version = (char *) malloc (5);
262   memcpy (si->version, screensaver_id + 17, 4);
263   progname = argv[0];
264
265 # ifdef SCO
266   set_auth_parameters(argc, argv);
267 # endif /* SCO */
268
269   if (! lock_init (argc, argv)) /* before hack_uid() for proper permissions */
270     {
271       si->locking_disabled_p = True;
272       si->nolock_reason = "error getting password";
273     }
274
275   hack_uid (si);
276
277   progclass = "XScreenSaver";
278
279   toplevel_shell = XtAppInitialize (&si->app, progclass, 0, 0,
280                                     &argc, argv, fallback,
281                                     0, 0);
282
283   si->dpy = XtDisplay (toplevel_shell);
284   si->db = XtDatabase (si->dpy);
285   si->default_screen->toplevel_shell = toplevel_shell;
286   si->default_screen->screen = XtScreen(toplevel_shell);
287   si->default_screen->default_visual =
288     si->default_screen->current_visual =
289       DefaultVisualOfScreen(si->default_screen->screen);
290   si->default_screen->screensaver_window =
291     RootWindowOfScreen(si->default_screen->screen);
292
293   db = si->db;
294   XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
295
296   p->debug_p = True;
297   p->verbose_p = True;
298   p->timestamp_p = True;
299   p->lock_p = True;
300
301   p->fade_p         = get_boolean_resource ("fade", "Boolean");
302   p->unfade_p       = get_boolean_resource ("unfade", "Boolean");
303   p->fade_seconds   = 1000 * get_seconds_resource ("fadeSeconds", "Time");
304   p->fade_ticks     = get_integer_resource ("fadeTicks", "Integer");
305   p->install_cmap_p = get_boolean_resource ("installColormap", "Boolean");
306   p->nice_inferior  = get_integer_resource ("nice", "Nice");
307
308   p->initial_delay   = 1000 * get_seconds_resource ("initialDelay", "Time");
309   p->splash_duration = 1000 * get_seconds_resource ("splashDuration", "Time");
310   p->timeout         = 1000 * get_minutes_resource ("timeout", "Time");
311   p->lock_timeout    = 1000 * get_minutes_resource ("lockTimeout", "Time");
312   p->cycle           = 1000 * get_minutes_resource ("cycle", "Time");
313   p->passwd_timeout  = 1000 * get_seconds_resource ("passwdTimeout", "Time");
314   p->passwd_timeout = 1000 * get_seconds_resource ("passwdTimeout", "Time");
315   p->splash_duration = 1000 * get_seconds_resource ("splashDuration", "Time");
316   p->shell = get_string_resource ("bourneShell", "BourneShell");
317   p->help_url = get_string_resource("helpURL", "URL");
318   p->load_url_command = get_string_resource("loadURL", "LoadURL");
319
320   get_screenhacks(si);
321
322   while (1)
323     {
324 #if WHICH == 0
325       if (unlock_p (si))
326         fprintf (stderr, "%s: password correct\n", progname);
327       else
328         fprintf (stderr, "%s: password INCORRECT!\n", progname);
329
330       XSync(si->dpy, False);
331       sleep (3);
332 #elif WHICH == 1
333       {
334         XEvent event;
335         make_splash_dialog (si);
336         XtAppAddTimeOut (si->app, p->splash_duration + 1000,
337                          idle_timer, (XtPointer) si);
338         while (si->splash_dialog)
339           {
340             XtAppNextEvent (si->app, &event);
341             if (event.xany.window == si->splash_dialog)
342               handle_splash_event (si, &event);
343             XtDispatchEvent (&event);
344           }
345         XSync (si->dpy, False);
346         sleep (1);
347       }
348 #else
349       si->demo_mode_p = True;
350       make_screenhack_dialog (si);
351       XtAppMainLoop(si->app);
352 #endif
353     }
354 }