1 /* demo.c --- implements the interactive demo-mode and options dialogs.
2 * xscreensaver, Copyright (c) 1993-1998 Jamie Zawinski <jwz@jwz.org>
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
17 #ifdef HAVE_ATHENA_KLUDGE /* don't ask */
19 # define HAVE_ATHENA 1
29 # include <pwd.h> /* for getpwuid() */
35 # include <sys/utsname.h> /* for uname() */
36 #endif /* HAVE_UNAME */
40 #include <X11/Intrinsic.h>
41 #include <X11/StringDefs.h>
43 /* We don't actually use any widget internals, but these are included
44 so that gdb will have debug info for the widgets... */
45 #include <X11/IntrinsicP.h>
46 #include <X11/ShellP.h>
52 # include <Xm/ToggleB.h>
53 # include <Xm/MessageB.h>
54 # include <Xm/LabelG.h>
55 # include <Xm/RowColumn.h>
57 #else /* HAVE_ATHENA */
58 /* Athena demo code contributed by Jon A. Christopher <jac8782@tamu.edu> */
59 /* Copyright 1997, with the same permissions as above. */
60 # include <X11/Shell.h>
61 # include <X11/Xaw/Form.h>
62 # include <X11/Xaw/Box.h>
63 # include <X11/Xaw/List.h>
64 # include <X11/Xaw/Command.h>
65 # include <X11/Xaw/Toggle.h>
66 # include <X11/Xaw/Viewport.h>
67 # include <X11/Xaw/Dialog.h>
68 # include <X11/Xaw/Scrollbar.h>
69 # include <X11/Xaw/Text.h>
70 #endif /* HAVE_ATHENA */
74 #include "resources.h" /* for parse_time() */
75 #include "visual.h" /* for has_writable_cells() */
76 #include "remote.h" /* for xscreensaver_command() */
84 char *progclass = "XScreenSaver";
88 saver_preferences *a, *b;
92 char *blurb (void) { return progname; }
94 static void run_hack (Display *dpy, int n);
97 static saver_preferences *global_prefs_kludge = 0; /* I hate C so much... */
98 #endif /* HAVE_ATHENA */
100 static char *short_version = 0;
103 Atom XA_SCREENSAVER, XA_SCREENSAVER_RESPONSE, XA_SCREENSAVER_VERSION;
104 Atom XA_SCREENSAVER_TIME, XA_SCREENSAVER_ID, XA_SELECT, XA_DEMO, XA_RESTART;
106 extern void create_demo_dialog (Widget, Visual *, Colormap);
107 extern void create_preferences_dialog (Widget, Visual *, Colormap);
109 extern Widget demo_dialog;
110 extern Widget label1;
111 extern Widget text_line;
112 extern Widget demo_form;
113 extern Widget demo_list;
114 extern Widget next, prev, done, restart, edit;
116 extern Widget preferences_dialog;
117 extern Widget preferences_form;
118 extern Widget prefs_done, prefs_cancel;
119 extern Widget timeout_text, cycle_text, fade_text, fade_ticks_text;
120 extern Widget lock_timeout_text, passwd_timeout_text;
121 extern Widget verbose_toggle, install_cmap_toggle, fade_toggle, unfade_toggle,
127 # define set_toggle_button_state(toggle,state) \
128 XmToggleButtonSetState ((toggle), (state), True)
129 # define set_text_string(text_widget,string) \
130 XmTextSetString ((text_widget), (string))
131 # define add_button_callback(button,cb,arg) \
132 XtAddCallback ((button), XmNactivateCallback, (cb), (arg))
133 # define add_toggle_callback(button,cb,arg) \
134 XtAddCallback ((button), XmNvalueChangedCallback, (cb), (arg))
135 # define add_text_callback add_toggle_callback
137 #else /* HAVE_ATHENA */
139 # define set_toggle_button_state(toggle,state) \
140 XtVaSetValues((toggle), XtNstate, (state), 0)
141 # define set_text_string(text_widget,string) \
142 XtVaSetValues ((text_widget), XtNvalue, (string), 0)
143 # define add_button_callback(button,cb,arg) \
144 XtAddCallback ((button), XtNcallback, (cb), (arg))
145 # define add_toggle_callback add_button_callback
146 # define add_text_callback(b,c,a) ERROR!
148 #endif /* HAVE_ATHENA */
151 #define disable_widget(widget) \
152 XtVaSetValues((widget), XtNsensitive, False, 0)
156 get_text_string (Widget text_widget)
159 return XmTextGetString (text_widget);
160 #else /* HAVE_ATHENA */
162 if (XtIsSubclass(text_widget, textWidgetClass))
163 XtVaGetValues (text_widget, XtNstring, &string, 0);
164 else if (XtIsSubclass(text_widget, dialogWidgetClass))
165 XtVaGetValues (text_widget, XtNvalue, &string, 0);
170 #endif /* HAVE_ATHENA */
174 get_label_string (Widget label_widget)
178 XmString xm_label = 0;
179 XtVaGetValues (label_widget, XmNlabelString, &xm_label, 0);
182 XmStringGetLtoR (xm_label, XmSTRING_DEFAULT_CHARSET, &label);
184 #else /* HAVE_ATHENA */
186 XtVaGetValues (label_widget, XtNlabel, &label, 0);
187 return (label ? strdup(label) : 0);
188 #endif /* HAVE_ATHENA */
193 set_label_string (Widget label_widget, char *string)
196 XmString xm_string = XmStringCreate (string, XmSTRING_DEFAULT_CHARSET);
197 XtVaSetValues (label_widget, XmNlabelString, xm_string, 0);
198 XmStringFree (xm_string);
199 #else /* HAVE_ATHENA */
200 XtVaSetValues (label_widget, XtNlabel, string, 0);
201 #endif /* HAVE_ATHENA */
206 format_into_label (Widget label, const char *arg)
208 char *text = get_label_string (label);
209 char *buf = (char *) malloc ((text ? strlen(text) : 0) + strlen(arg) + 100);
211 if (!text || !strcmp (text, XtName (label)))
212 strcpy (buf, "ERROR: RESOURCES ARE NOT INSTALLED CORRECTLY");
214 sprintf (buf, text, arg);
216 set_label_string (label, buf);
222 /* Why this behavior isn't automatic in *either* toolkit, I'll never know.
225 ensure_selected_item_visible (Widget list)
230 if (XmListGetSelectedPos (list, &pos_list, &pos_count) && pos_count > 0)
235 XmNtopItemPosition, &top,
236 XmNvisibleItemCount, &visible,
238 if (pos_list[0] >= top + visible)
240 int pos = pos_list[0] - visible + 1;
241 if (pos < 0) pos = 0;
242 XmListSetPos (list, pos);
244 else if (pos_list[0] < top)
246 XmListSetPos (list, pos_list[0]);
250 XtFree ((char *) pos_list);
252 #else /* HAVE_ATHENA */
253 # ifdef HAVE_XawViewportSetCoordinates
255 int margin = 16; /* should be line height or something. */
258 Dimension list_h = 0, vp_h = 0;
259 Dimension top_margin = 4; /* I don't know where this value comes from */
260 Position vp_x = 0, vp_y = 0, current_y;
262 Widget viewport = XtParent(demo_list);
263 Widget sb = (viewport ? XtNameToWidget(viewport, "*vertical") : 0);
264 float sb_top = 0, sb_size = 0;
265 XawListReturnStruct *current = XawListShowCurrent(demo_list);
266 if (!current || !sb) return;
268 XtVaGetValues(demo_list,
269 XtNnumberStrings, &count,
272 if (count < 2 || list_h < 10) return;
274 XtVaGetValues(viewport, XtNheight, &vp_h, XtNx, &vp_x, XtNy, &vp_y, 0);
275 if (vp_h < 10) return;
277 XtVaGetValues(sb, XtNtopOfThumb, &sb_top, XtNshown, &sb_size, 0);
278 if (sb_size <= 0) return;
280 pos = current->list_index;
281 cratio = ((double) pos) / ((double) count);
282 current_y = (cratio * list_h);
284 if (cratio < sb_top ||
285 cratio > sb_top + sb_size)
288 current_y -= (vp_h - margin - margin);
292 if ((long)current_y >= (long) list_h)
293 current_y = (Position) ((long)list_h - (long)vp_h);
295 if ((long)current_y < (long)top_margin)
296 current_y = (Position)top_margin;
298 XawViewportSetCoordinates (viewport, vp_x, current_y);
300 # endif /* HAVE_XawViewportSetCoordinates */
301 #endif /* HAVE_ATHENA */
305 /* Callback for the text area:
306 - note the text the user has entered;
307 - change the corresponding element in `screenhacks';
308 - write the .xscreensaver file;
309 - tell the xscreensaver daemon to run that hack.
312 text_cb (Widget text_widget, XtPointer client_data, XtPointer call_data)
314 Display *dpy = XtDisplay (text_widget);
315 saver_preferences *p = (saver_preferences *) client_data;
316 char *new_text = get_text_string (text_widget);
318 int hack_number = -1; /* 0-based */
321 XawListReturnStruct *current = XawListShowCurrent(demo_list);
322 hack_number = current->list_index;
323 #else /* HAVE_MOTIF */
326 if (XmListGetSelectedPos (demo_list, &pos_list, &pos_count))
327 hack_number = pos_list[0] - 1;
329 XtFree ((char *) pos_list);
330 #endif /* HAVE_MOTIF */
332 ensure_selected_item_visible (demo_list);
334 if (hack_number < 0 || hack_number >= p->screenhacks_count)
336 set_text_string (text_widget, "");
341 fprintf(stderr, "%d:\nold: %s\nnew: %s\n",
342 hack_number, p->screenhacks [hack_number], new_text);
344 if (p->screenhacks [hack_number])
345 free (p->screenhacks [hack_number]);
346 p->screenhacks [hack_number] = strdup (new_text);
350 XmListDeselectAllItems (demo_list);
352 XmString xmstr = XmStringCreate (new_text, XmSTRING_DEFAULT_CHARSET);
353 XmListReplaceItemsPos (demo_list, &xmstr, 1, hack_number+1);
354 XmStringFree (xmstr);
356 XmListSelectPos (demo_list, hack_number+1, True);
358 #else /* HAVE_ATHENA */
361 Widget vp = XtParent(demo_list);
362 Widget sb = (vp ? XtNameToWidget(vp, "*vertical") : 0);
363 Dimension list_h = 0;
364 Position vp_x = 0, vp_y = 0;
367 XawListUnhighlight (demo_list);
369 XtVaGetValues (vp, XtNx, &vp_x, 0);
370 XtVaGetValues (sb, XtNtopOfThumb, &sb_top, 0);
371 XtVaGetValues (demo_list, XtNheight, &list_h, 0);
372 vp_y = (sb_top * list_h);
373 XtVaSetValues (demo_list,
374 XtNlist, p->screenhacks,
375 XtNnumberStrings, p->screenhacks_count,
377 XawViewportSetCoordinates (vp, vp_x, vp_y);
378 XawListHighlight (demo_list, hack_number);
381 #endif /* HAVE_ATHENA */
383 write_init_file (p, short_version);
385 usleep (500000); /* give the disk time to settle down */
387 run_hack (dpy, hack_number+1);
393 /* Bend over backwards to make hitting Return in the text field do the
396 static void text_enter (Widget w, XEvent *event, String *av, Cardinal *ac)
398 text_cb (w, global_prefs_kludge, 0); /* I hate C so much... */
401 static XtActionsRec actions[] = {{"done", text_enter}
403 static char translations[] = ("<Key>Return: done()\n"
404 "<Key>Linefeed: done()\n"
405 "Ctrl<Key>M: done()\n"
406 "Ctrl<Key>J: done()\n");
407 #endif /* HAVE_ATHENA */
410 /* Callback for the Run Next button.
413 next_cb (Widget button, XtPointer client_data, XtPointer call_data)
416 XawListReturnStruct *current = XawListShowCurrent(demo_list);
418 XtVaGetValues (demo_list, XtNnumberStrings, &cnt, 0);
419 if (current->list_index == XAW_LIST_NONE ||
420 current->list_index + 1 >= cnt)
421 current->list_index = 0;
423 current->list_index++;
424 XawListHighlight(demo_list, current->list_index);
426 ensure_selected_item_visible (demo_list);
427 current = XawListShowCurrent(demo_list);
428 XtVaSetValues(text_line, XtNstring, current->string, 0);
430 run_hack (XtDisplay (button), current->list_index + 1);
432 #else /* HAVE_MOTIF */
434 saver_preferences *p = (saver_preferences *) client_data;
438 if (! XmListGetSelectedPos (demo_list, &pos_list, &pos_count))
441 XmListDeselectAllItems (demo_list); /* LessTif lossage */
442 XmListSelectPos (demo_list, pos, True);
446 pos = pos_list[0] + 1;
447 if (pos > p->screenhacks_count)
449 XmListDeselectAllItems (demo_list); /* LessTif lossage */
450 XmListSelectPos (demo_list, pos, True);
453 ensure_selected_item_visible (demo_list);
454 run_hack (XtDisplay (button), pos);
456 XtFree ((char *) pos_list);
458 #endif /* HAVE_MOTIF */
462 /* Callback for the Run Previous button.
465 prev_cb (Widget button, XtPointer client_data, XtPointer call_data)
468 XawListReturnStruct *current = XawListShowCurrent(demo_list);
470 XtVaGetValues (demo_list, XtNnumberStrings, &cnt, 0);
471 if (current->list_index == XAW_LIST_NONE ||
472 current->list_index <= 0)
473 current->list_index = cnt-1;
475 current->list_index--;
476 XawListHighlight(demo_list, current->list_index);
478 ensure_selected_item_visible (demo_list);
479 current = XawListShowCurrent(demo_list);
480 XtVaSetValues(text_line, XtNstring, current->string, 0);
482 run_hack (XtDisplay (button), current->list_index + 1);
484 #else /* HAVE_MOTIF */
486 saver_preferences *p = (saver_preferences *) client_data;
490 if (! XmListGetSelectedPos (demo_list, &pos_list, &pos_count))
492 pos = p->screenhacks_count;
493 XmListDeselectAllItems (demo_list); /* LessTif lossage */
494 XmListSelectPos (demo_list, pos, True);
498 pos = pos_list[0] - 1;
500 pos = p->screenhacks_count;
501 XmListDeselectAllItems (demo_list); /* LessTif lossage */
502 XmListSelectPos (demo_list, pos, True);
505 ensure_selected_item_visible (demo_list);
506 run_hack (XtDisplay (button), pos);
508 XtFree ((char *) pos_list);
510 #endif /* HAVE_MOTIF */
514 /* Callback run when a list element is double-clicked.
517 select_cb (Widget button, XtPointer client_data, XtPointer call_data)
519 /* saver_preferences *p = (saver_preferences *) client_data; */
522 XawListReturnStruct *item = (XawListReturnStruct*)call_data;
523 XtVaSetValues(text_line, XtNstring, item->string, 0);
524 run_hack (XtDisplay (button), item->list_index + 1);
526 #else /* HAVE_MOTIF */
527 XmListCallbackStruct *lcb = (XmListCallbackStruct *) call_data;
530 XmStringGetLtoR (lcb->item, XmSTRING_DEFAULT_CHARSET, &string);
531 set_text_string (text_line, (string ? string : ""));
533 if (lcb->reason == XmCR_DEFAULT_ACTION && string)
534 run_hack (XtDisplay (button), lcb->item_position);
538 #endif /* HAVE_MOTIF */
543 static void pop_preferences_dialog (prefs_pair *pair);
544 static void make_preferences_dialog (prefs_pair *pair, Widget parent);
546 /* Callback for the Preferences button.
549 preferences_cb (Widget button, XtPointer client_data, XtPointer call_data)
551 prefs_pair *pair = (prefs_pair *) client_data;
552 Widget parent = button;
555 parent = XtParent(parent);
556 } while (XtParent(parent));
558 if (! preferences_dialog)
559 make_preferences_dialog (pair, parent);
560 pop_preferences_dialog (pair);
563 /* Callback for the Quit button.
566 quit_cb (Widget button, XtPointer client_data, XtPointer call_data)
568 /* Save here? Right now we don't need to, because we save every time
569 the text field is edited, or the Preferences OK button is pressed.
575 /* Callback for the (now unused) Restart button.
578 restart_cb (Widget button, XtPointer client_data, XtPointer call_data)
580 xscreensaver_command (XtDisplay (button), XA_RESTART, 0, False);
585 pop_up_dialog_box (Widget dialog, Widget form)
588 XtRealizeWidget (dialog);
589 XtPopup (dialog, XtGrabNone);
590 #else /* HAVE_MOTIF */
591 XtRealizeWidget (form);
592 XtManageChild (form);
593 #endif /* HAVE_MOTIF */
594 XMapRaised (XtDisplay (dialog), XtWindow (dialog));
599 make_demo_dialog (Widget toplevel_shell, prefs_pair *pair)
601 saver_preferences *p = pair->a;
602 /* saver_preferences *p2 = pair->b; */
604 Widget parent = toplevel_shell;
605 char **hacks = p->screenhacks;
607 create_demo_dialog (parent,
608 DefaultVisualOfScreen (XtScreen (parent)),
609 DefaultColormapOfScreen (XtScreen (parent)));
610 format_into_label (label1, short_version);
612 add_button_callback (next, next_cb, (XtPointer) p);
613 add_button_callback (prev, prev_cb, (XtPointer) p);
614 add_button_callback (done, quit_cb, (XtPointer) p);
616 add_button_callback(restart,restart_cb, (XtPointer) p);
617 add_button_callback (edit, preferences_cb, (XtPointer) pair);
620 XtAddCallback (demo_list, XmNbrowseSelectionCallback,
621 select_cb, (XtPointer) p);
622 XtAddCallback (demo_list, XmNdefaultActionCallback,
623 select_cb, (XtPointer) p);
624 XtAddCallback (text_line, XmNactivateCallback, text_cb, (XtPointer) p);
627 for (; *hacks; hacks++)
629 XmString xmstr = XmStringCreate (*hacks, XmSTRING_DEFAULT_CHARSET);
630 XmListAddItem (demo_list, xmstr, 0);
631 XmStringFree (xmstr);
634 #else /* HAVE_ATHENA */
636 /* Hook up the text line. */
638 XtAppAddActions(XtWidgetToApplicationContext(text_line),
639 actions, XtNumber(actions));
640 XtOverrideTranslations(text_line, XtParseTranslationTable(translations));
643 /* Must realize the widget before populating the list, or the dialog
644 will be as wide as the longest string.
646 XtRealizeWidget (demo_dialog);
648 XtVaSetValues (demo_list,
650 XtNnumberStrings, p->screenhacks_count,
652 XtAddCallback (demo_list, XtNcallback, select_cb, p);
654 /* Now that we've populated the list, make sure that the list is as
655 wide as the dialog itself.
658 Widget viewport = XtParent(demo_list);
659 Widget subform = XtParent(viewport);
660 Widget box = XtNameToWidget(demo_dialog, "*box");
661 Widget label1 = XtNameToWidget(demo_dialog, "*label1");
662 Widget label2 = XtNameToWidget(demo_dialog, "*label2");
663 Dimension x=0, y=0, w=0, h=0, bw=0, w2=0;
664 XtVaGetValues(subform,
665 XtNwidth, &w, XtNheight, &h, XtNborderWidth, &bw, 0);
666 XtVaGetValues(box, XtNwidth, &w2, 0);
668 XtResizeWidget(subform, w2, h, bw);
670 /* Why isn't the viewport getting centered? */
671 XtVaGetValues(viewport,
672 XtNx, &x, XtNy, &y, XtNheight, &h, XtNborderWidth, &bw, 0);
673 XtConfigureWidget(viewport, x, y, w2-x-x, h, bw);
675 /* And the text line, too. */
676 XtVaGetValues(text_line,
677 XtNwidth, &w, XtNheight, &h, XtNborderWidth, &bw, 0);
678 XtVaGetValues(viewport, XtNwidth, &w2, 0);
680 XtResizeWidget(text_line, w2, h, bw);
682 /* And the labels too. */
683 XtVaGetValues(label1,
684 XtNwidth, &w, XtNheight, &h, XtNborderWidth, &bw, 0);
686 XtResizeWidget(label1, w2, h, bw);
688 XtVaGetValues(label2,
689 XtNwidth, &w, XtNheight, &h, XtNborderWidth, &bw, 0);
691 XtResizeWidget(label2, w2, h, bw);
695 #endif /* HAVE_ATHENA */
697 pop_up_dialog_box(demo_dialog, demo_form);
700 /* For Athena, have to do this after the dialog is managed. */
701 ensure_selected_item_visible (demo_list);
702 #endif /* HAVE_ATHENA */
706 /* the Preferences dialog
709 /* Helper for the text fields that contain time specifications:
710 this parses the text, and does error checking.
713 hack_time_text (Display *dpy, char *line, Time *store, Bool sec_p)
718 value = parse_time (line, sec_p, True);
719 value *= 1000; /* Time measures in microseconds */
728 /* Callback for text fields that hold a time that default to seconds,
729 when not fully spelled out. client_data is an Time* where the value goes.
732 prefs_sec_cb (Widget button, XtPointer client_data, XtPointer call_data)
734 hack_time_text (XtDisplay (button), get_text_string (button),
735 (Time *) client_data, True);
739 /* Callback for text fields that hold a time that default to minutes,
740 when not fully spelled out. client_data is an Time* where the value goes.
743 prefs_min_cb (Widget button, XtPointer client_data, XtPointer call_data)
745 hack_time_text (XtDisplay (button), get_text_string (button),
746 (Time *) client_data, False);
750 /* Callback for text fields that hold an integer value.
751 client_data is an int* where the value goes.
754 prefs_int_cb (Widget button, XtPointer client_data, XtPointer call_data)
756 char *line = get_text_string (button);
757 int *store = (int *) client_data;
762 else if (sscanf (line, "%u%c", &value, &c) != 1)
763 XBell (XtDisplay (button), 0);
768 /* Callback for toggle buttons. client_data is an Bool* where the value goes.
771 prefs_bool_cb (Widget button, XtPointer client_data, XtPointer call_data)
773 Bool *store = (Bool *) client_data;
775 *store = ((XmToggleButtonCallbackStruct *) call_data)->set;
776 #else /* HAVE_ATHENA */
777 Boolean state = FALSE;
778 XtVaGetValues (button, XtNstate, &state, 0);
780 #endif /* HAVE_ATHENA */
784 /* Callback for the Cancel button on the Preferences dialog.
787 prefs_cancel_cb (Widget button, XtPointer client_data, XtPointer call_data)
789 XtDestroyWidget (preferences_dialog);
790 preferences_dialog = 0;
791 XMapRaised (XtDisplay (demo_dialog), XtWindow (demo_dialog));
795 /* Callback for the OK button on the Preferences dialog.
798 prefs_ok_cb (Widget button, XtPointer client_data, XtPointer call_data)
800 prefs_pair *pair = (prefs_pair *) client_data;
801 saver_preferences *p = pair->a;
802 saver_preferences *p2 = pair->b;
804 prefs_cancel_cb (button, 0, call_data);
807 /* Athena doesn't let us put callbacks on these widgets, so run
808 all the callbacks by hand when OK is pressed. */
809 prefs_min_cb (timeout_text, (XtPointer) &p2->timeout, 0);
810 prefs_min_cb (cycle_text, (XtPointer) &p2->cycle, 0);
811 prefs_sec_cb (fade_text, (XtPointer) &p2->fade_seconds, 0);
812 prefs_int_cb (fade_ticks_text, (XtPointer) &p2->fade_ticks, 0);
813 prefs_min_cb (lock_timeout_text, (XtPointer) &p2->lock_timeout, 0);
814 prefs_sec_cb (passwd_timeout_text, (XtPointer) &p2->passwd_timeout, 0);
815 #endif /* HAVE_ATHENA */
817 p->timeout = p2->timeout;
818 p->cycle = p2->cycle;
819 p->lock_timeout = p2->lock_timeout;
820 p->passwd_timeout = p2->passwd_timeout;
821 p->fade_seconds = p2->fade_seconds;
822 p->fade_ticks = p2->fade_ticks;
823 p->verbose_p = p2->verbose_p;
824 p->install_cmap_p = p2->install_cmap_p;
825 p->fade_p = p2->fade_p;
826 p->unfade_p = p2->unfade_p;
827 p->lock_p = p2->lock_p;
829 write_init_file (p, short_version);
834 make_preferences_dialog (prefs_pair *pair, Widget parent)
836 saver_preferences *p = pair->a;
837 saver_preferences *p2 = pair->b;
839 Screen *screen = XtScreen (parent);
840 Display *dpy = XtDisplay (parent);
842 *p2 = *p; /* copy all slots of p into p2. */
844 create_preferences_dialog (parent,
845 DefaultVisualOfScreen (screen),
846 DefaultColormapOfScreen (screen));
848 add_button_callback (prefs_done, prefs_ok_cb, (XtPointer) pair);
849 add_button_callback (prefs_cancel, prefs_cancel_cb, 0);
851 #define CB(widget,type,slot) \
852 add_text_callback ((widget), (type), (XtPointer) (slot))
853 #define CBT(widget,type,slot) \
854 add_toggle_callback ((widget), (type), (XtPointer) (slot))
857 /* When using Athena widgets, we can't set callbacks for these,
858 so in that case, we run them by hand when "OK" is pressed. */
859 CB (timeout_text, prefs_min_cb, &p2->timeout);
860 CB (cycle_text, prefs_min_cb, &p2->cycle);
861 CB (fade_text, prefs_sec_cb, &p2->fade_seconds);
862 CB (fade_ticks_text, prefs_int_cb, &p2->fade_ticks);
863 CB (lock_timeout_text, prefs_min_cb, &p2->lock_timeout);
864 CB (passwd_timeout_text, prefs_sec_cb, &p2->passwd_timeout);
865 #endif /* HAVE_MOTIF */
867 CBT (verbose_toggle, prefs_bool_cb, &p2->verbose_p);
868 CBT (install_cmap_toggle, prefs_bool_cb, &p2->install_cmap_p);
869 CBT (fade_toggle, prefs_bool_cb, &p2->fade_p);
870 CBT (unfade_toggle, prefs_bool_cb, &p2->unfade_p);
871 CBT (lock_toggle, prefs_bool_cb, &p2->lock_p);
876 Bool found_any_writable_cells = False;
877 int nscreens = ScreenCount(dpy);
879 for (i = 0; i < nscreens; i++)
881 Screen *s = ScreenOfDisplay (dpy, i);
882 if (has_writable_cells (s, DefaultVisualOfScreen (s)))
884 found_any_writable_cells = True;
889 if (! found_any_writable_cells) /* fading isn't possible */
891 disable_widget (fade_text);
892 disable_widget (fade_ticks_text);
893 disable_widget (install_cmap_toggle);
894 disable_widget (fade_toggle);
895 disable_widget (unfade_toggle);
901 /* Formats a `Time' into "H:MM:SS". (Time is microseconds.)
904 format_time (char *buf, Time time)
907 unsigned int h = 0, m = 0;
918 sprintf (buf, "%u:%02u:%02u", h, m, s);
923 pop_preferences_dialog (prefs_pair *pair)
925 /* saver_preferences *p = pair->a; */
926 saver_preferences *p2 = pair->b;
929 format_time (s, p2->timeout); set_text_string(timeout_text, s);
930 format_time (s, p2->cycle); set_text_string(cycle_text, s);
931 format_time (s, p2->lock_timeout); set_text_string(lock_timeout_text, s);
932 format_time (s, p2->passwd_timeout); set_text_string(passwd_timeout_text, s);
933 format_time (s, p2->fade_seconds); set_text_string(fade_text, s);
934 sprintf (s, "%u", p2->fade_ticks); set_text_string(fade_ticks_text, s);
936 set_toggle_button_state (verbose_toggle, p2->verbose_p);
937 set_toggle_button_state (install_cmap_toggle, p2->install_cmap_p);
938 set_toggle_button_state (fade_toggle, p2->fade_p);
939 set_toggle_button_state (unfade_toggle, p2->unfade_p);
940 set_toggle_button_state (lock_toggle, p2->lock_p);
942 pop_up_dialog_box (preferences_dialog, preferences_form);
947 run_hack (Display *dpy, int n)
950 xscreensaver_command (dpy, XA_DEMO, n, False);
955 warning_dialog_dismiss_cb (Widget button, XtPointer client_data,
958 Widget shell = (Widget) client_data;
959 XtDestroyWidget (shell);
963 warning_dialog (Widget parent, const char *message)
965 char *msg = strdup (message);
982 dialog = XmCreateWarningDialog (parent, "versionWarning", av, ac);
984 w = XmMessageBoxGetChild (dialog, XmDIALOG_MESSAGE_LABEL);
985 if (w) XtUnmanageChild (w);
986 w = XmMessageBoxGetChild (dialog, XmDIALOG_CANCEL_BUTTON);
987 if (w) XtUnmanageChild (w);
988 w = XmMessageBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
989 if (w) XtUnmanageChild (w);
991 ok = XmMessageBoxGetChild (dialog, XmDIALOG_OK_BUTTON);
994 XtSetArg (av[ac], XmNnumColumns, 1); ac++;
995 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ac++;
996 XtSetArg (av[ac], XmNpacking, XmPACK_COLUMN); ac++;
997 XtSetArg (av[ac], XmNrowColumnType, XmWORK_AREA); ac++;
998 XtSetArg (av[ac], XmNspacing, 0); ac++;
999 container = XmCreateRowColumn (dialog, "container", av, ac);
1001 #else /* HAVE_ATHENA */
1004 dialog = XtVaCreatePopupShell("warning_dialog", transientShellWidgetClass,
1006 form = XtVaCreateManagedWidget("warning_form", formWidgetClass, dialog, 0);
1008 #endif /* HAVE_ATHENA */
1014 char *s = strchr (head, '\n');
1017 sprintf (name, "label%d", i++);
1020 xmstr = XmStringCreate (head, XmSTRING_DEFAULT_CHARSET);
1022 XtSetArg (av[ac], XmNlabelString, xmstr); ac++;
1023 label = XmCreateLabelGadget (container, name, av, ac);
1024 XtManageChild (label);
1025 XmStringFree (xmstr);
1026 #else /* HAVE_ATHENA */
1028 label = XtVaCreateManagedWidget (name, labelWidgetClass,
1030 XtNleft, XtChainLeft,
1031 XtNright, XtChainRight,
1033 (label ? XtNfromVert : XtNtop),
1034 (label ? label : XtChainTop),
1037 #endif /* HAVE_ATHENA */
1047 XtManageChild (container);
1048 XtRealizeWidget (dialog);
1049 XtManageChild (dialog);
1051 #else /* HAVE_ATHENA */
1053 ok = XtVaCreateManagedWidget ("ok", commandWidgetClass, form,
1054 XtNleft, XtChainLeft,
1055 XtNbottom, XtChainBottom,
1059 XtRealizeWidget (dialog);
1060 XtPopup (dialog, XtGrabNone);
1062 #endif /* HAVE_ATHENA */
1064 add_button_callback (ok, warning_dialog_dismiss_cb, dialog);
1071 /* The main demo-mode command loop.
1076 mapper (XrmDatabase *db, XrmBindingList bindings, XrmQuarkList quarks,
1077 XrmRepresentation *type, XrmValue *value, XPointer closure)
1080 for (i = 0; quarks[i]; i++)
1082 if (bindings[i] == XrmBindTightly)
1083 fprintf (stderr, (i == 0 ? "" : "."));
1084 else if (bindings[i] == XrmBindLoosely)
1085 fprintf (stderr, "*");
1087 fprintf (stderr, " ??? ");
1088 fprintf(stderr, "%s", XrmQuarkToString (quarks[i]));
1091 fprintf (stderr, ": %s\n", (char *) value->addr);
1098 the_network_is_not_the_computer (Widget parent)
1100 Display *dpy = XtDisplay (parent);
1101 char *rversion, *ruser, *rhost;
1102 char *luser, *lhost;
1104 struct passwd *p = getpwuid (getuid ());
1105 const char *d = DisplayString (dpy);
1107 # if defined(HAVE_UNAME)
1109 if (uname (&uts) < 0)
1110 lhost = "<UNKNOWN>";
1112 lhost = uts.nodename;
1114 strcpy (lhost, getenv("SYS$NODE"));
1115 # else /* !HAVE_UNAME && !VMS */
1116 strcat (lhost, "<UNKNOWN>");
1117 # endif /* !HAVE_UNAME && !VMS */
1119 if (p && p->pw_name)
1124 server_xscreensaver_version (dpy, &rversion, &ruser, &rhost);
1126 /* Make a buffer that's big enough for a number of copies of all the
1127 strings, plus some. */
1128 msg = (char *) malloc (10 * ((rversion ? strlen(rversion) : 0) +
1129 (ruser ? strlen(ruser) : 0) +
1130 (rhost ? strlen(rhost) : 0) +
1137 if (!rversion || !*rversion)
1141 "xscreensaver doesn't seem to be running on display \"%s\".",
1144 else if (p && ruser && *ruser && !!strcmp (ruser, p->pw_name))
1146 /* Warn that the two processes are running as different users.
1150 "%s is running as user \"%s\" on host \"%s\".\n"
1151 "But the xscreensaver managing display \"%s\"\n"
1152 "is running as user \"%s\" on host \"%s\".\n"
1154 "Since they are different users, they won't be reading/writing\n"
1155 "the same ~/.xscreensaver file, so %s isn't\n"
1156 "going to work right.\n"
1158 "Either re-run %s as \"%s\", or re-run\n"
1159 "xscreensaver as \"%s\".\n",
1160 progname, luser, lhost,
1162 (ruser ? ruser : "???"), (rhost ? rhost : "???"),
1164 progname, (ruser ? ruser : "???"),
1167 else if (rhost && *rhost && !!strcmp (rhost, lhost))
1169 /* Warn that the two processes are running on different hosts.
1173 "%s is running as user \"%s\" on host \"%s\".\n"
1174 "But the xscreensaver managing display \"%s\"\n"
1175 "is running as user \"%s\" on host \"%s\".\n"
1177 "If those two machines don't share a file system (that is,\n"
1178 "if they don't see the same ~%s/.xscreensaver file) then\n"
1179 "%s won't work right.",
1180 progname, luser, lhost,
1182 (ruser ? ruser : "???"), (rhost ? rhost : "???"),
1186 else if (!!strcmp (rversion, short_version))
1188 /* Warn that the version numbers don't match.
1192 "This is %s version %s.\n"
1193 "But the xscreensaver managing display \"%s\"\n"
1194 "is version %s. This could cause problems.",
1195 progname, short_version,
1202 warning_dialog (parent, msg);
1208 static char *defaults[] = {
1209 #include "XScreenSaver_ad.h"
1214 main (int argc, char **argv)
1217 prefs_pair Pair, *pair;
1218 saver_preferences P, P2, *p, *p2;
1222 Widget toplevel_shell;
1223 char *real_progname = argv[0];
1226 s = strrchr (real_progname, '/');
1227 if (s) real_progname = s+1;
1234 memset (p, 0, sizeof (*p));
1235 memset (p2, 0, sizeof (*p2));
1237 /* We must read exactly the same resources as xscreensaver.
1238 That means we must have both the same progclass *and* progname,
1239 at least as far as the resource database is concerned. So,
1240 put "xscreensaver" in argv[0] while initializing Xt.
1242 argv[0] = "xscreensaver";
1244 toplevel_shell = XtAppInitialize (&app, progclass, 0, 0, &argc, argv,
1246 dpy = XtDisplay (toplevel_shell);
1247 db = XtDatabase (dpy);
1248 XtGetApplicationNameAndClass (dpy, &progname, &progclass);
1250 for (i = 1; i < argc; i++)
1253 if (s[0] == '-' && s[1] == '-')
1255 if (!strcmp (s, "-prefs"))
1259 fprintf (stderr, "usage: %s [ -display dpy-string ] [ -prefs ]\n",
1265 short_version = (char *) malloc (5);
1266 memcpy (short_version, screensaver_id + 17, 4);
1267 short_version [4] = 0;
1270 /* Now that Xt has been initialized, we can set our `progname' variable
1271 to something that makes more sense (like our "real" argv[0].)
1273 progname = real_progname;
1277 p->fading_possible_p = True;
1282 global_prefs_kludge = p; /* I hate C so much... */
1283 #endif /* HAVE_ATHENA */
1287 XrmName name = { 0 };
1288 XrmClass class = { 0 };
1290 XrmEnumerateDatabase (db, &name, &class, XrmEnumAllLevels, mapper,
1291 (XtPointer) &count);
1296 XA_VROOT = XInternAtom (dpy, "__SWM_VROOT", False);
1297 XA_SCREENSAVER = XInternAtom (dpy, "SCREENSAVER", False);
1298 XA_SCREENSAVER_VERSION = XInternAtom (dpy, "_SCREENSAVER_VERSION",False);
1299 XA_SCREENSAVER_TIME = XInternAtom (dpy, "_SCREENSAVER_TIME", False);
1300 XA_SCREENSAVER_ID = XInternAtom (dpy, "_SCREENSAVER_ID", False);
1301 XA_SCREENSAVER_RESPONSE = XInternAtom (dpy, "_SCREENSAVER_RESPONSE", False);
1302 XA_SELECT = XInternAtom (dpy, "SELECT", False);
1303 XA_DEMO = XInternAtom (dpy, "DEMO", False);
1304 XA_RESTART = XInternAtom (dpy, "RESTART", False);
1306 make_demo_dialog (toplevel_shell, pair);
1310 make_preferences_dialog (pair, toplevel_shell);
1311 pop_preferences_dialog (pair);
1314 the_network_is_not_the_computer (preferences_dialog
1315 ? preferences_dialog