1 /* demo-Xm.c --- implements the interactive demo-mode and options dialogs.
2 * xscreensaver, Copyright (c) 1993-2001 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_MOTIF /* whole file */
26 # include <pwd.h> /* for getpwuid() */
32 # include <sys/utsname.h> /* for uname() */
33 #endif /* HAVE_UNAME */
37 #include <X11/Xproto.h> /* for CARD32 */
38 #include <X11/Xatom.h> /* for XA_INTEGER */
39 #include <X11/Intrinsic.h>
40 #include <X11/StringDefs.h>
42 /* We don't actually use any widget internals, but these are included
43 so that gdb will have debug info for the widgets... */
44 #include <X11/IntrinsicP.h>
45 #include <X11/ShellP.h>
53 # include <X11/Xmu/Error.h>
55 # include <Xmu/Error.h>
66 #include <Xm/LabelG.h>
67 #include <Xm/RowColumn.h>
68 #include <Xm/MessageB.h>
70 #ifdef HAVE_XMCOMBOBOX /* a Motif 2.0 widget */
71 # include <Xm/ComboBox.h>
72 # ifndef XmNtextField /* Lesstif 0.89.4 bug */
73 # undef HAVE_XMCOMBOBOX
75 # if (XmVersion < 2001) /* Lesstif has two personalities these days */
76 # undef HAVE_XMCOMBOBOX
78 #endif /* HAVE_XMCOMBOBOX */
82 #include "resources.h" /* for parse_time() */
83 #include "visual.h" /* for has_writable_cells() */
84 #include "remote.h" /* for xscreensaver_command() */
92 #define countof(x) (sizeof((x))/sizeof((*x)))
96 char *progclass = "XScreenSaver";
100 saver_preferences *a, *b;
103 static void *global_prefs_pair; /* I hate C so much... */
105 char *blurb (void) { return progname; }
107 extern Widget create_xscreensaver_demo (Widget parent);
108 extern const char *visual_menu[];
111 static char *short_version = 0;
114 Atom XA_SCREENSAVER, XA_SCREENSAVER_RESPONSE, XA_SCREENSAVER_VERSION;
115 Atom XA_SCREENSAVER_ID, XA_SCREENSAVER_STATUS, XA_SELECT, XA_DEMO;
116 Atom XA_ACTIVATE, XA_BLANK, XA_LOCK, XA_RESTART, XA_EXIT;
119 static void populate_demo_window (Widget toplevel,
120 int which, prefs_pair *pair);
121 static void populate_prefs_page (Widget top, prefs_pair *pair);
122 static int apply_changes_and_save (Widget widget);
123 static int maybe_reload_init_file (Widget widget, prefs_pair *pair);
124 static void await_xscreensaver (Widget widget);
127 /* Some random utility functions
131 name_to_widget (Widget widget, const char *name)
136 strcpy (name2+1, name);
138 while ((parent = XtParent (widget)))
140 return XtNameToWidget (widget, name2);
145 /* Why this behavior isn't automatic in *either* toolkit, I'll never know.
146 Takes a scroller, viewport, or list as an argument.
149 ensure_selected_item_visible (Widget list)
153 if (XmListGetSelectedPos (list, &pos_list, &pos_count) && pos_count > 0)
158 XmNtopItemPosition, &top,
159 XmNvisibleItemCount, &visible,
161 if (pos_list[0] >= top + visible)
163 int pos = pos_list[0] - visible + 1;
164 if (pos < 0) pos = 0;
165 XmListSetPos (list, pos);
167 else if (pos_list[0] < top)
169 XmListSetPos (list, pos_list[0]);
173 XtFree ((char *) pos_list);
178 warning_dialog_dismiss_cb (Widget button, XtPointer client_data,
181 Widget shell = (Widget) client_data;
182 XtDestroyWidget (shell);
187 warning_dialog (Widget parent, const char *message, int center)
189 char *msg = strdup (message);
204 dialog = XmCreateWarningDialog (parent, "warning", av, ac);
206 w = XmMessageBoxGetChild (dialog, XmDIALOG_MESSAGE_LABEL);
207 if (w) XtUnmanageChild (w);
208 w = XmMessageBoxGetChild (dialog, XmDIALOG_CANCEL_BUTTON);
209 if (w) XtUnmanageChild (w);
210 w = XmMessageBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
211 if (w) XtUnmanageChild (w);
213 ok = XmMessageBoxGetChild (dialog, XmDIALOG_OK_BUTTON);
216 XtSetArg (av[ac], XmNnumColumns, 1); ac++;
217 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ac++;
218 XtSetArg (av[ac], XmNpacking, XmPACK_COLUMN); ac++;
219 XtSetArg (av[ac], XmNrowColumnType, XmWORK_AREA); ac++;
220 XtSetArg (av[ac], XmNspacing, 0); ac++;
221 container = XmCreateRowColumn (dialog, "container", av, ac);
227 char *s = strchr (head, '\n');
230 sprintf (name, "label%d", i++);
232 xmstr = XmStringCreate (head, XmSTRING_DEFAULT_CHARSET);
234 XtSetArg (av[ac], XmNlabelString, xmstr); ac++;
235 XtSetArg (av[ac], XmNmarginHeight, 0); ac++;
236 label = XmCreateLabelGadget (container, name, av, ac);
237 XtManageChild (label);
238 XmStringFree (xmstr);
248 XtManageChild (container);
249 XtRealizeWidget (dialog);
250 XtManageChild (dialog);
252 XtAddCallback (ok, XmNactivateCallback, warning_dialog_dismiss_cb, dialog);
259 run_cmd (Widget widget, Atom command, int arg)
264 apply_changes_and_save (widget);
265 status = xscreensaver_command (XtDisplay (widget),
266 command, arg, False, &err);
271 sprintf (buf, "Error:\n\n%s", err);
273 strcpy (buf, "Unknown error!");
274 warning_dialog (widget, buf, 100);
281 run_hack (Widget widget, int which, Bool report_errors_p)
283 if (which < 0) return;
284 apply_changes_and_save (widget);
286 run_cmd (widget, XA_DEMO, which + 1);
290 xscreensaver_command (XtDisplay (widget), XA_DEMO, which + 1, False, &s);
301 exit_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
303 apply_changes_and_save (XtParent (button));
309 wm_close_cb (Widget widget, GdkEvent *event, XtPointer data)
311 apply_changes_and_save (XtParent (button));
317 cut_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
320 warning_dialog (XtParent (button),
322 "cut unimplemented\n", 1);
327 copy_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
330 warning_dialog (XtParent (button),
332 "copy unimplemented\n", 1);
337 paste_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
340 warning_dialog (XtParent (button),
342 "paste unimplemented\n", 1);
347 about_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
350 char *s = strdup (screensaver_id + 4);
353 s2 = strchr (s, ',');
357 sprintf (buf, "%s\n%s\n\n"
358 "For updates, check http://www.jwz.org/xscreensaver/",
362 warning_dialog (XtParent (button), buf, 100);
367 doc_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
369 prefs_pair *pair = (prefs_pair *) client_data;
371 saver_preferences *p = pair->a;
374 if (!p->help_url || !*p->help_url)
376 warning_dialog (XtParent (button),
378 "No Help URL has been specified.\n", 100);
382 help_command = (char *) malloc (strlen (p->load_url_command) +
383 (strlen (p->help_url) * 2) + 20);
384 strcpy (help_command, "( ");
385 sprintf (help_command + strlen(help_command),
386 p->load_url_command, p->help_url, p->help_url);
387 strcat (help_command, " ) &");
388 system (help_command);
394 activate_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
396 run_cmd (XtParent (button), XA_ACTIVATE, 0);
401 lock_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
403 run_cmd (XtParent (button), XA_LOCK, 0);
408 kill_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
410 run_cmd (XtParent (button), XA_EXIT, 0);
415 restart_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
418 run_cmd (XtParent (button), XA_RESTART, 0);
420 button = XtParent (button);
421 apply_changes_and_save (button);
422 xscreensaver_command (XtDisplay (button), XA_EXIT, 0, False, NULL);
424 system ("xscreensaver -nosplash &");
427 await_xscreensaver (button);
431 await_xscreensaver (Widget widget)
435 Display *dpy = XtDisplay (widget);
438 while (!rversion && (--countdown > 0))
440 /* Check for the version of the running xscreensaver... */
441 server_xscreensaver_version (dpy, &rversion, 0, 0);
443 /* If it's not there yet, wait a second... */
454 /* Timed out, no screensaver running. */
457 Bool root_p = (geteuid () == 0);
461 "The xscreensaver daemon did not start up properly.\n"
466 "You are running as root. This usually means that xscreensaver\n"
467 "was unable to contact your X server because access control is\n"
468 "turned on. Try running this command:\n"
470 " xhost +localhost\n"
472 "and then selecting `File / Restart Daemon'.\n"
474 "Note that turning off access control will allow anyone logged\n"
475 "on to this machine to access your screen, which might be\n"
476 "considered a security problem. Please read the xscreensaver\n"
477 "manual and FAQ for more information.\n"
479 "You shouldn't run X as root. Instead, you should log in as a\n"
480 "normal user, and `su' as necessary.");
482 strcat (buf, "Please check your $PATH and permissions.");
484 warning_dialog (XtParent (widget), buf, 1);
489 static int _selected_hack_number = -1;
492 selected_hack_number (Widget toplevel)
494 return _selected_hack_number;
499 demo_write_init_file (Widget widget, saver_preferences *p)
501 if (!write_init_file (p, short_version, False))
505 const char *f = init_file_name();
507 warning_dialog (widget,
508 "Error:\n\nCouldn't determine init file name!\n",
512 char *b = (char *) malloc (strlen(f) + 1024);
513 sprintf (b, "Error:\n\nCouldn't write %s\n", f);
514 warning_dialog (widget, b, 100);
523 apply_changes_and_save (Widget widget)
525 prefs_pair *pair = global_prefs_pair;
526 saver_preferences *p = pair->a;
527 Widget list_widget = name_to_widget (widget, "list");
528 int which = selected_hack_number (widget);
530 Widget cmd = name_to_widget (widget, "cmdText");
531 Widget enabled = name_to_widget (widget, "enabled");
533 Widget vis = name_to_widget (widget, "combo");
534 # ifdef HAVE_XMCOMBOBOX
536 # else /* !HAVE_XMCOMBOBOX */
537 Widget menu = 0, *kids = 0, selected_item = 0;
540 # endif /* !HAVE_XMCOMBOBOX */
542 Bool enabled_p = False;
543 const char *visual = 0;
544 const char *command = 0;
549 if (which < 0) return -1;
551 # ifdef HAVE_XMCOMBOBOX
552 XtVaGetValues (vis, XmNtextField, &text, 0);
554 /* If we can't get at the text field of this combo box, we're screwed. */
556 XtVaGetValues (text, XmNvalue, &visual, 0);
558 # else /* !HAVE_XMCOMBOBOX */
559 XtVaGetValues (vis, XmNsubMenuId, &menu, 0);
560 XtVaGetValues (menu, XmNnumChildren, &nkids, XmNchildren, &kids, 0);
561 XtVaGetValues (menu, XmNmenuHistory, &selected_item, 0);
563 for (i = 0; i < nkids; i++)
564 if (kids[i] == selected_item)
567 visual = visual_menu[i];
568 # endif /* !HAVE_XMCOMBOBOX */
570 XtVaGetValues (enabled, XmNset, &enabled_p, 0);
571 XtVaGetValues (cmd, XtNvalue, &command, 0);
573 if (maybe_reload_init_file (widget, pair) != 0)
576 /* Sanity-check and canonicalize whatever the user typed into the combo box.
578 if (!strcasecmp (visual, "")) visual = "";
579 else if (!strcasecmp (visual, "any")) visual = "";
580 else if (!strcasecmp (visual, "default")) visual = "Default";
581 else if (!strcasecmp (visual, "default-n")) visual = "Default-N";
582 else if (!strcasecmp (visual, "default-i")) visual = "Default-I";
583 else if (!strcasecmp (visual, "best")) visual = "Best";
584 else if (!strcasecmp (visual, "mono")) visual = "Mono";
585 else if (!strcasecmp (visual, "monochrome")) visual = "Mono";
586 else if (!strcasecmp (visual, "gray")) visual = "Gray";
587 else if (!strcasecmp (visual, "grey")) visual = "Gray";
588 else if (!strcasecmp (visual, "color")) visual = "Color";
589 else if (!strcasecmp (visual, "gl")) visual = "GL";
590 else if (!strcasecmp (visual, "staticgray")) visual = "StaticGray";
591 else if (!strcasecmp (visual, "staticcolor")) visual = "StaticColor";
592 else if (!strcasecmp (visual, "truecolor")) visual = "TrueColor";
593 else if (!strcasecmp (visual, "grayscale")) visual = "GrayScale";
594 else if (!strcasecmp (visual, "greyscale")) visual = "GrayScale";
595 else if (!strcasecmp (visual, "pseudocolor")) visual = "PseudoColor";
596 else if (!strcasecmp (visual, "directcolor")) visual = "DirectColor";
597 else if (1 == sscanf (visual, " %ld %c", &id, &c)) ;
598 else if (1 == sscanf (visual, " 0x%lx %c", &id, &c)) ;
601 XBell (XtDisplay (widget), 0); /* unparsable */
603 /* #### gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (vis)->entry), "Any");*/
606 ensure_selected_item_visible (list_widget);
608 if (!p->screenhacks[which]->visual)
609 p->screenhacks[which]->visual = strdup ("");
610 if (!p->screenhacks[which]->command)
611 p->screenhacks[which]->command = strdup ("");
613 if (p->screenhacks[which]->enabled_p != enabled_p ||
614 !!strcasecmp (p->screenhacks[which]->visual, visual) ||
615 !!strcasecmp (p->screenhacks[which]->command, command))
617 /* Something was changed -- store results into the struct,
620 free (p->screenhacks[which]->visual);
621 free (p->screenhacks[which]->command);
622 p->screenhacks[which]->visual = strdup (visual);
623 p->screenhacks[which]->command = strdup (command);
624 p->screenhacks[which]->enabled_p = enabled_p;
626 return demo_write_init_file (widget, p);
629 /* No changes made */
634 run_this_cb (Widget button, XtPointer client_data, XtPointer ignored)
636 int which = selected_hack_number (XtParent (button));
637 if (which < 0) return;
638 if (0 == apply_changes_and_save (XtParent (button)))
639 run_hack (XtParent (button), which, True);
644 manual_cb (Widget button, XtPointer client_data, XtPointer ignored)
646 prefs_pair *pair = (prefs_pair *) client_data;
647 saver_preferences *p = pair->a;
648 Widget list_widget = name_to_widget (button, "list");
649 int which = selected_hack_number (button);
650 char *name, *name2, *cmd, *s;
651 if (which < 0) return;
652 apply_changes_and_save (button);
653 ensure_selected_item_visible (list_widget);
655 name = strdup (p->screenhacks[which]->command);
657 while (isspace (*name2)) name2++;
659 while (*s && !isspace (*s)) s++;
661 s = strrchr (name2, '/');
664 cmd = get_string_resource ("manualCommand", "ManualCommand");
667 char *cmd2 = (char *) malloc (strlen (cmd) + strlen (name2) + 100);
669 sprintf (cmd2 + strlen (cmd2),
671 name2, name2, name2, name2);
672 strcat (cmd2, " ) &");
678 warning_dialog (XtParent (button),
679 "Error:\n\nno `manualCommand' resource set.",
688 run_next_cb (Widget button, XtPointer client_data, XtPointer ignored)
690 prefs_pair *pair = (prefs_pair *) client_data;
691 saver_preferences *p = pair->a;
693 Widget list_widget = name_to_widget (button, "list");
694 int which = selected_hack_number (button);
696 button = XtParent (button);
703 if (which >= p->screenhacks_count)
706 apply_changes_and_save (button);
708 XmListDeselectAllItems (list_widget); /* LessTif lossage */
709 XmListSelectPos (list_widget, which+1, True);
711 ensure_selected_item_visible (list_widget);
712 populate_demo_window (button, which, pair);
713 run_hack (button, which, False);
718 run_prev_cb (Widget button, XtPointer client_data, XtPointer ignored)
720 prefs_pair *pair = (prefs_pair *) client_data;
721 saver_preferences *p = pair->a;
723 Widget list_widget = name_to_widget (button, "list");
724 int which = selected_hack_number (button);
726 button = XtParent (button);
729 which = p->screenhacks_count - 1;
734 which = p->screenhacks_count - 1;
736 apply_changes_and_save (button);
738 XmListDeselectAllItems (list_widget); /* LessTif lossage */
739 XmListSelectPos (list_widget, which+1, True);
741 ensure_selected_item_visible (list_widget);
742 populate_demo_window (button, which, pair);
743 run_hack (button, which, False);
747 /* Helper for the text fields that contain time specifications:
748 this parses the text, and does error checking.
751 hack_time_text (Widget button, const char *line, Time *store, Bool sec_p)
756 value = parse_time ((char *) line, sec_p, True);
757 value *= 1000; /* Time measures in microseconds */
763 "Unparsable time format: \"%s\"\n",
765 warning_dialog (XtParent (button), b, 100);
774 prefs_ok_cb (Widget button, XtPointer client_data, XtPointer ignored)
776 prefs_pair *pair = (prefs_pair *) client_data;
778 saver_preferences *p = pair->a;
779 saver_preferences *p2 = pair->b;
780 Bool changed = False;
783 button = XtParent (button);
785 # define SECONDS(field, name) \
787 XtVaGetValues (name_to_widget (button, (name)), XtNvalue, &v, 0); \
788 hack_time_text (button, v, (field), True)
790 # define MINUTES(field, name) \
792 XtVaGetValues (name_to_widget (button, (name)), XtNvalue, &v, 0); \
793 hack_time_text (button, v, (field), False)
795 # define INTEGER(field, name) do { \
796 unsigned int value; \
798 XtVaGetValues (name_to_widget (button, (name)), XtNvalue, &v, 0); \
801 else if (sscanf (v, "%u%c", &value, &c) != 1) \
804 sprintf (b, "Error:\n\n" "Not an integer: \"%s\"\n", v); \
805 warning_dialog (XtParent (button), b, 100); \
811 # define CHECKBOX(field, name) \
812 XtVaGetValues (name_to_widget (button, (name)), XmNset, &field, 0)
814 MINUTES (&p2->timeout, "timeoutText");
815 MINUTES (&p2->cycle, "cycleText");
816 SECONDS (&p2->fade_seconds, "fadeSecondsText");
817 INTEGER (&p2->fade_ticks, "fadeTicksText");
818 MINUTES (&p2->lock_timeout, "lockText");
819 SECONDS (&p2->passwd_timeout, "passwdText");
820 CHECKBOX (p2->verbose_p, "verboseToggle");
821 CHECKBOX (p2->install_cmap_p, "cmapToggle");
822 CHECKBOX (p2->fade_p, "fadeToggle");
823 CHECKBOX (p2->unfade_p, "unfadeToggle");
824 CHECKBOX (p2->lock_p, "lockToggle");
831 # define COPY(field) \
832 if (p->field != p2->field) changed = True; \
838 COPY(passwd_timeout);
842 COPY(install_cmap_p);
848 populate_prefs_page (button, pair);
851 demo_write_init_file (button, p);
856 prefs_cancel_cb (Widget button, XtPointer client_data, XtPointer ignored)
858 prefs_pair *pair = (prefs_pair *) client_data;
861 populate_prefs_page (XtParent (button), pair);
866 list_select_cb (Widget list, XtPointer client_data, XtPointer call_data)
868 prefs_pair *pair = (prefs_pair *) client_data;
870 XmListCallbackStruct *lcb = (XmListCallbackStruct *) call_data;
871 int which = lcb->item_position - 1;
873 apply_changes_and_save (list);
874 populate_demo_window (list, which, pair);
876 if (lcb->reason == XmCR_DEFAULT_ACTION && which >= 0)
877 run_hack (list, which, True);
881 /* Populating the various widgets
885 /* Formats a `Time' into "H:MM:SS". (Time is microseconds.)
888 format_time (char *buf, Time time)
891 unsigned int h = 0, m = 0;
902 sprintf (buf, "%u:%02u:%02u", h, m, s);
906 /* Finds the number of the last hack to run, and makes that item be
910 scroll_to_current_hack (Widget toplevel, prefs_pair *pair)
914 unsigned long nitems, bytesafter;
916 Display *dpy = XtDisplay (toplevel);
920 if (XGetWindowProperty (dpy, RootWindow (dpy, 0), /* always screen #0 */
921 XA_SCREENSAVER_STATUS,
922 0, 3, False, XA_INTEGER,
923 &type, &format, &nitems, &bytesafter,
924 (unsigned char **) &data)
926 && type == XA_INTEGER
929 which = (int) data[2] - 1;
931 if (data) free (data);
936 list = name_to_widget (toplevel, "list");
937 apply_changes_and_save (toplevel);
939 XmListDeselectAllItems (list); /* LessTif lossage */
940 XmListSelectPos (list, which+1, True);
942 ensure_selected_item_visible (list);
943 populate_demo_window (toplevel, which, pair);
949 populate_hack_list (Widget toplevel, prefs_pair *pair)
951 saver_preferences *p = pair->a;
952 Widget list = name_to_widget (toplevel, "list");
953 screenhack **hacks = p->screenhacks;
956 for (h = hacks; *h; h++)
958 char *pretty_name = (h[0]->name
959 ? strdup (h[0]->name)
960 : make_hack_name (h[0]->command));
962 XmString xmstr = XmStringCreate (pretty_name, XmSTRING_DEFAULT_CHARSET);
963 XmListAddItem (list, xmstr, 0);
964 XmStringFree (xmstr);
967 XtAddCallback (list, XmNbrowseSelectionCallback, list_select_cb, pair);
968 XtAddCallback (list, XmNdefaultActionCallback, list_select_cb, pair);
973 populate_prefs_page (Widget top, prefs_pair *pair)
975 saver_preferences *p = pair->a;
978 format_time (s, p->timeout);
979 XtVaSetValues (name_to_widget (top, "timeoutText"), XmNvalue, s, 0);
980 format_time (s, p->cycle);
981 XtVaSetValues (name_to_widget (top, "cycleText"), XmNvalue, s, 0);
982 format_time (s, p->lock_timeout);
983 XtVaSetValues (name_to_widget (top, "lockText"), XmNvalue, s, 0);
984 format_time (s, p->passwd_timeout);
985 XtVaSetValues (name_to_widget (top, "passwdText"), XmNvalue, s, 0);
986 format_time (s, p->fade_seconds);
987 XtVaSetValues (name_to_widget (top, "fadeSecondsText"), XmNvalue, s, 0);
988 sprintf (s, "%u", p->fade_ticks);
989 XtVaSetValues (name_to_widget (top, "fadeTicksText"), XmNvalue, s, 0);
991 XtVaSetValues (name_to_widget (top, "verboseToggle"),
992 XmNset, p->verbose_p, 0);
993 XtVaSetValues (name_to_widget (top, "cmapToggle"),
994 XmNset, p->install_cmap_p, 0);
995 XtVaSetValues (name_to_widget (top, "fadeToggle"),
996 XmNset, p->fade_p, 0);
997 XtVaSetValues (name_to_widget (top, "unfadeToggle"),
998 XmNset, p->unfade_p, 0);
999 XtVaSetValues (name_to_widget (top, "lockToggle"),
1000 XmNset, p->lock_p, 0);
1004 Bool found_any_writable_cells = False;
1005 Display *dpy = XtDisplay (top);
1006 int nscreens = ScreenCount(dpy);
1008 for (i = 0; i < nscreens; i++)
1010 Screen *s = ScreenOfDisplay (dpy, i);
1011 if (has_writable_cells (s, DefaultVisualOfScreen (s)))
1013 found_any_writable_cells = True;
1018 #ifdef HAVE_XF86VMODE_GAMMA
1019 found_any_writable_cells = True; /* if we can gamma fade, go for it */
1022 XtVaSetValues (name_to_widget (top, "fadeSecondsLabel"), XtNsensitive,
1023 found_any_writable_cells, 0);
1024 XtVaSetValues (name_to_widget (top, "fadeTicksLabel"), XtNsensitive,
1025 found_any_writable_cells, 0);
1026 XtVaSetValues (name_to_widget (top, "fadeSecondsText"), XtNsensitive,
1027 found_any_writable_cells, 0);
1028 XtVaSetValues (name_to_widget (top, "fadeTicksText"), XtNsensitive,
1029 found_any_writable_cells, 0);
1030 XtVaSetValues (name_to_widget (top, "cmapToggle"), XtNsensitive,
1031 found_any_writable_cells, 0);
1032 XtVaSetValues (name_to_widget (top, "fadeToggle"), XtNsensitive,
1033 found_any_writable_cells, 0);
1034 XtVaSetValues (name_to_widget (top, "unfadeToggle"), XtNsensitive,
1035 found_any_writable_cells, 0);
1041 sensitize_demo_widgets (Widget toplevel, Bool sensitive_p)
1043 const char *names[] = { "cmdLabel", "cmdText", "enabled",
1044 "visLabel", "combo", "demo", "man" };
1046 for (i = 0; i < sizeof(names)/countof(*names); i++)
1048 Widget w = name_to_widget (toplevel, names[i]);
1049 XtVaSetValues (w, XtNsensitive, sensitive_p, 0);
1052 /* I don't know how to handle these yet... */
1054 const char *names2[] = { "cut", "copy", "paste" };
1055 for (i = 0; i < sizeof(names2)/countof(*names2); i++)
1057 Widget w = name_to_widget (toplevel, names2[i]);
1058 XtVaSetValues (w, XtNsensitive, FALSE, 0);
1065 /* Pixmaps for the up and down arrow buttons (yeah, this is sleazy...)
1070 static char *up_arrow_xpm[] = {
1072 " c None s background",
1094 static char *down_arrow_xpm[] = {
1096 " c None s background",
1118 #endif /* HAVE_XPM */
1122 pixmapify_buttons (Widget toplevel)
1126 Display *dpy = XtDisplay (toplevel);
1127 Window window = XtWindow (toplevel);
1128 XWindowAttributes xgwa;
1129 XpmAttributes xpmattrs;
1130 Pixmap up_pixmap = 0, down_pixmap = 0;
1132 Widget up = name_to_widget (toplevel, "up");
1133 Widget dn = name_to_widget (toplevel, "down");
1134 # ifdef XpmColorSymbols
1136 XpmColorSymbol symbols[2];
1140 XGetWindowAttributes (dpy, window, &xgwa);
1142 xpmattrs.valuemask = 0;
1144 # ifdef XpmColorSymbols
1145 symbols[0].name = "background";
1146 symbols[0].pixel = 0;
1147 symbols[1].name = 0;
1148 XtVaGetValues (up, XmNbackground, &xc, 0);
1149 XQueryColor (dpy, xgwa.colormap, &xc);
1150 sprintf (color, "#%04X%04X%04X", xc.red, xc.green, xc.blue);
1151 symbols[0].value = color;
1152 symbols[0].pixel = xc.pixel;
1154 xpmattrs.valuemask |= XpmColorSymbols;
1155 xpmattrs.colorsymbols = symbols;
1156 xpmattrs.numsymbols = 1;
1159 # ifdef XpmCloseness
1160 xpmattrs.valuemask |= XpmCloseness;
1161 xpmattrs.closeness = 40000;
1164 xpmattrs.valuemask |= XpmVisual;
1165 xpmattrs.visual = xgwa.visual;
1168 xpmattrs.valuemask |= XpmDepth;
1169 xpmattrs.depth = xgwa.depth;
1172 xpmattrs.valuemask |= XpmColormap;
1173 xpmattrs.colormap = xgwa.colormap;
1176 result = XpmCreatePixmapFromData(dpy, window, up_arrow_xpm,
1177 &up_pixmap, 0 /* mask */, &xpmattrs);
1178 if (!up_pixmap || (result != XpmSuccess && result != XpmColorError))
1180 fprintf (stderr, "%s: Can't load pixmaps\n", progname);
1184 result = XpmCreatePixmapFromData(dpy, window, down_arrow_xpm,
1185 &down_pixmap, 0 /* mask */, &xpmattrs);
1186 if (!down_pixmap || (result != XpmSuccess && result != XpmColorError))
1188 fprintf (stderr, "%s: Can't load pixmaps\n", progname);
1192 XtVaSetValues (up, XmNlabelType, XmPIXMAP, XmNlabelPixmap, up_pixmap, 0);
1193 XtVaSetValues (dn, XmNlabelType, XmPIXMAP, XmNlabelPixmap, down_pixmap, 0);
1195 #endif /* HAVE_XPM */
1201 get_hack_blurb (screenhack *hack)
1204 char *prog_name = strdup (hack->command);
1205 char *pretty_name = (hack->name
1206 ? strdup (hack->name)
1207 : make_hack_name (hack->command));
1208 char doc_name[255], doc_class[255];
1211 for (s = prog_name; *s && !isspace(*s); s++)
1214 s = strrchr (prog_name, '/');
1215 if (s) strcpy (prog_name, s+1);
1217 sprintf (doc_name, "hacks.%s.documentation", pretty_name);
1218 sprintf (doc_class, "hacks.%s.documentation", prog_name);
1222 doc_string = get_string_resource (doc_name, doc_class);
1225 for (s = doc_string; *s; s++)
1229 /* skip over whitespace at beginning of line */
1231 while (*s && (*s == ' ' || *s == '\t'))
1234 else if (*s == ' ' || *s == '\t')
1236 /* compress all other horizontal whitespace. */
1239 for (s2 = s; *s2 && (*s2 == ' ' || *s2 == '\t'); s2++)
1241 if (s2 > s) strcpy (s, s2);
1246 while (*s && isspace (*s)) /* Strip trailing whitespace */
1249 /* Delete whitespace at end of each line. */
1250 for (; s > doc_string; s--)
1251 if (*s == '\n' && (s[-1] == ' ' || s[-1] == '\t'))
1254 s2 > doc_string && (*s2 == ' ' || *s2 == '\t');
1258 if (s2 < s) strcpy (s2, s);
1262 /* Delete leading blank lines. */
1263 for (s = doc_string; *s == '\n'; s++)
1265 if (s > doc_string) strcpy (doc_string, s);
1269 static int doc_installed = 0;
1270 if (doc_installed == 0)
1272 if (get_boolean_resource ("hacks.documentation.isInstalled",
1273 "hacks.documentation.isInstalled"))
1279 if (doc_installed < 0)
1281 strdup ("Error:\n\n"
1282 "The documentation strings do not appear to be "
1283 "installed. This is probably because there is "
1284 "an \"XScreenSaver\" app-defaults file installed "
1285 "that is from an older version of the program. "
1286 "To fix this problem, delete that file, or "
1287 "install a current version (either will work.)");
1289 doc_string = strdup ("");
1297 populate_demo_window (Widget toplevel, int which, prefs_pair *pair)
1299 saver_preferences *p = pair->a;
1300 screenhack *hack = (which >= 0 ? p->screenhacks[which] : 0);
1301 Widget frameL = name_to_widget (toplevel, "frameLabel");
1302 Widget doc = name_to_widget (toplevel, "doc");
1303 Widget cmd = name_to_widget (toplevel, "cmdText");
1304 Widget enabled = name_to_widget (toplevel, "enabled");
1305 Widget vis = name_to_widget (toplevel, "combo");
1308 char *pretty_name = (hack
1310 ? strdup (hack->name)
1311 : make_hack_name (hack->command))
1313 char *doc_string = hack ? get_hack_blurb (hack) : 0;
1317 xmstr = XmStringCreate (pretty_name, XmSTRING_DEFAULT_CHARSET);
1318 XtVaSetValues (frameL, XmNlabelString, xmstr, 0);
1319 XmStringFree (xmstr);
1321 XtVaSetValues (doc, XmNvalue, doc_string, 0);
1322 XtVaSetValues (cmd, XmNvalue, (hack ? hack->command : ""), 0);
1324 XtVaSetValues (enabled, XmNset, (hack ? hack->enabled_p : False), 0);
1327 if (hack && hack->visual && *hack->visual)
1328 for (i = 0; visual_menu[i]; i++)
1329 if (!strcasecmp (hack->visual, visual_menu[i]))
1331 if (!visual_menu[i]) i = -1;
1334 # ifdef HAVE_XMCOMBOBOX
1336 XtVaGetValues (vis, XmNtextField, &text, 0);
1337 XtVaSetValues (vis, XmNselectedPosition, i, 0);
1339 XtVaSetValues (text, XmNvalue, hack->visual, 0);
1340 # else /* !HAVE_XMCOMBOBOX */
1345 XtVaGetValues (vis, XmNsubMenuId, &menu, 0);
1346 if (!menu) abort ();
1347 XtVaGetValues (menu, XmNnumChildren, &nkids, XmNchildren, &kids, 0);
1350 XtVaSetValues (vis, XmNmenuHistory, kids[i], 0);
1351 # endif /* !HAVE_XMCOMBOBOX */
1354 sensitize_demo_widgets (toplevel, (hack ? True : False));
1356 if (pretty_name) free (pretty_name);
1357 if (doc_string) free (doc_string);
1359 _selected_hack_number = which;
1365 maybe_reload_init_file (Widget widget, prefs_pair *pair)
1368 saver_preferences *p = pair->a;
1370 static Bool reentrant_lock = False;
1371 if (reentrant_lock) return 0;
1372 reentrant_lock = True;
1374 if (init_file_changed_p (p))
1376 const char *f = init_file_name();
1381 if (!f || !*f) return 0;
1382 b = (char *) malloc (strlen(f) + 1024);
1385 "file \"%s\" has changed, reloading.\n",
1387 warning_dialog (widget, b, 100);
1392 which = selected_hack_number (widget);
1393 list = name_to_widget (widget, "list");
1395 XtVaSetValues (list, XmNitemCount, 0, 0);
1397 populate_hack_list (widget, pair);
1399 XmListDeselectAllItems (list); /* LessTif lossage */
1400 XmListSelectPos (list, which+1, True);
1402 populate_prefs_page (widget, pair);
1403 populate_demo_window (widget, which, pair);
1404 ensure_selected_item_visible (list);
1409 reentrant_lock = False;
1415 /* Attach all callback functions to widgets
1419 add_callbacks (Widget toplevel, prefs_pair *pair)
1423 # define CB(NAME,FN) \
1424 w = name_to_widget (toplevel, (NAME)); \
1425 XtAddCallback (w, XmNactivateCallback, (FN), pair)
1427 CB ("blank", activate_menu_cb);
1428 CB ("lock", lock_menu_cb);
1429 CB ("kill", kill_menu_cb);
1430 CB ("restart", restart_menu_cb);
1431 CB ("exit", exit_menu_cb);
1433 CB ("cut", cut_menu_cb);
1434 CB ("copy", copy_menu_cb);
1435 CB ("paste", paste_menu_cb);
1437 CB ("about", about_menu_cb);
1438 CB ("docMenu", doc_menu_cb);
1440 CB ("down", run_next_cb);
1441 CB ("up", run_prev_cb);
1442 CB ("demo", run_this_cb);
1443 CB ("man", manual_cb);
1445 CB ("preferencesForm.Cancel", prefs_cancel_cb);
1446 CB ("preferencesForm.OK", prefs_ok_cb);
1453 sanity_check_resources (Widget toplevel)
1455 const char *names[] = { "demoTab", "optionsTab", "cmdLabel", "visLabel",
1456 "enabled", "demo", "man", "timeoutLabel",
1457 "cycleLabel", "fadeSecondsLabel", "fadeTicksLabel",
1458 "lockLabel", "passwdLabel" };
1460 for (i = 0; i < sizeof(names)/countof(*names); i++)
1462 Widget w = name_to_widget (toplevel, names[i]);
1463 const char *name = XtName(w);
1466 XtVaGetValues (w, XmNlabelString, &xm, 0);
1467 if (xm) XmStringGetLtoR (xm, XmSTRING_DEFAULT_CHARSET, &label);
1468 if (w && (!label || !strcmp (name, label)))
1470 xm = XmStringCreate ("ERROR", XmSTRING_DEFAULT_CHARSET);
1471 XtVaSetValues (w, XmNlabelString, xm, 0);
1476 /* Set certain buttons to be the same size (the max of the set.)
1479 hack_button_sizes (Widget toplevel)
1481 Widget demo = name_to_widget (toplevel, "demo");
1482 Widget man = name_to_widget (toplevel, "man");
1483 Widget ok = name_to_widget (toplevel, "OK");
1484 Widget can = name_to_widget (toplevel, "Cancel");
1485 Widget up = name_to_widget (toplevel, "up");
1486 Widget down = name_to_widget (toplevel, "down");
1489 XtVaGetValues (demo, XmNwidth, &w1, 0);
1490 XtVaGetValues (man, XmNwidth, &w2, 0);
1491 XtVaSetValues ((w1 > w2 ? man : demo), XmNwidth, (w1 > w2 ? w1 : w2), 0);
1493 XtVaGetValues (ok, XmNwidth, &w1, 0);
1494 XtVaGetValues (can, XmNwidth, &w2, 0);
1495 XtVaSetValues ((w1 > w2 ? can : ok), XmNwidth, (w1 > w2 ? w1 : w2), 0);
1497 XtVaGetValues (up, XmNwidth, &w1, 0);
1498 XtVaGetValues (down, XmNwidth, &w2, 0);
1499 XtVaSetValues ((w1 > w2 ? down : up), XmNwidth, (w1 > w2 ? w1 : w2), 0);
1505 /* The main demo-mode command loop.
1510 mapper (XrmDatabase *db, XrmBindingList bindings, XrmQuarkList quarks,
1511 XrmRepresentation *type, XrmValue *value, XPointer closure)
1514 for (i = 0; quarks[i]; i++)
1516 if (bindings[i] == XrmBindTightly)
1517 fprintf (stderr, (i == 0 ? "" : "."));
1518 else if (bindings[i] == XrmBindLoosely)
1519 fprintf (stderr, "*");
1521 fprintf (stderr, " ??? ");
1522 fprintf(stderr, "%s", XrmQuarkToString (quarks[i]));
1525 fprintf (stderr, ": %s\n", (char *) value->addr);
1533 the_network_is_not_the_computer (Widget parent)
1535 Display *dpy = XtDisplay (parent);
1536 char *rversion, *ruser, *rhost;
1537 char *luser, *lhost;
1539 struct passwd *p = getpwuid (getuid ());
1540 const char *d = DisplayString (dpy);
1542 # if defined(HAVE_UNAME)
1544 if (uname (&uts) < 0)
1545 lhost = "<UNKNOWN>";
1547 lhost = uts.nodename;
1549 strcpy (lhost, getenv("SYS$NODE"));
1550 # else /* !HAVE_UNAME && !VMS */
1551 strcat (lhost, "<UNKNOWN>");
1552 # endif /* !HAVE_UNAME && !VMS */
1554 if (p && p->pw_name)
1559 server_xscreensaver_version (dpy, &rversion, &ruser, &rhost);
1561 /* Make a buffer that's big enough for a number of copies of all the
1562 strings, plus some. */
1563 msg = (char *) malloc (10 * ((rversion ? strlen(rversion) : 0) +
1564 (ruser ? strlen(ruser) : 0) +
1565 (rhost ? strlen(rhost) : 0) +
1572 if (!rversion || !*rversion)
1576 "The XScreenSaver daemon doesn't seem to be running\n"
1577 "on display \"%s\". You can launch it by selecting\n"
1578 "`Restart Daemon' from the File menu, or by typing\n"
1579 "\"xscreensaver &\" in a shell.",
1582 else if (p && ruser && *ruser && !!strcmp (ruser, p->pw_name))
1584 /* Warn that the two processes are running as different users.
1588 "%s is running as user \"%s\" on host \"%s\".\n"
1589 "But the xscreensaver managing display \"%s\"\n"
1590 "is running as user \"%s\" on host \"%s\".\n"
1592 "Since they are different users, they won't be reading/writing\n"
1593 "the same ~/.xscreensaver file, so %s isn't\n"
1594 "going to work right.\n"
1596 "Either re-run %s as \"%s\", or re-run\n"
1597 "xscreensaver as \"%s\" (which you can do by\n"
1598 "selecting `Restart Daemon' from the File menu.)\n",
1599 progname, luser, lhost,
1601 (ruser ? ruser : "???"), (rhost ? rhost : "???"),
1603 progname, (ruser ? ruser : "???"),
1606 else if (rhost && *rhost && !!strcmp (rhost, lhost))
1608 /* Warn that the two processes are running on different hosts.
1612 "%s is running as user \"%s\" on host \"%s\".\n"
1613 "But the xscreensaver managing display \"%s\"\n"
1614 "is running as user \"%s\" on host \"%s\".\n"
1616 "If those two machines don't share a file system (that is,\n"
1617 "if they don't see the same ~%s/.xscreensaver file) then\n"
1618 "%s won't work right.\n"
1620 "You can restart the daemon on \"%s\" as \"%s\" by\n"
1621 "selecting `Restart Daemon' from the File menu.)",
1622 progname, luser, lhost,
1624 (ruser ? ruser : "???"), (rhost ? rhost : "???"),
1629 else if (!!strcmp (rversion, short_version))
1631 /* Warn that the version numbers don't match.
1635 "This is %s version %s.\n"
1636 "But the xscreensaver managing display \"%s\"\n"
1637 "is version %s. This could cause problems.",
1638 progname, short_version,
1645 warning_dialog (parent, msg, 1);
1651 /* We use this error handler so that X errors are preceeded by the name
1652 of the program that generated them.
1655 demo_ehandler (Display *dpy, XErrorEvent *error)
1657 fprintf (stderr, "\nX error in %s:\n", progname);
1658 XmuPrintDefaultErrorMessage (dpy, error, stderr);
1665 static char *defaults[] = {
1666 #include "XScreenSaver_ad.h"
1672 main (int argc, char **argv)
1675 prefs_pair Pair, *pair;
1676 saver_preferences P, P2, *p, *p2;
1680 Widget toplevel_shell, dialog;
1681 char *real_progname = argv[0];
1684 s = strrchr (real_progname, '/');
1685 if (s) real_progname = s+1;
1692 memset (p, 0, sizeof (*p));
1693 memset (p2, 0, sizeof (*p2));
1695 global_prefs_pair = pair;
1697 progname = real_progname;
1699 /* We must read exactly the same resources as xscreensaver.
1700 That means we must have both the same progclass *and* progname,
1701 at least as far as the resource database is concerned. So,
1702 put "xscreensaver" in argv[0] while initializing Xt.
1704 argv[0] = "xscreensaver";
1708 toplevel_shell = XtAppInitialize (&app, progclass, 0, 0, &argc, argv,
1711 dpy = XtDisplay (toplevel_shell);
1712 db = XtDatabase (dpy);
1713 XtGetApplicationNameAndClass (dpy, &progname, &progclass);
1714 XSetErrorHandler (demo_ehandler);
1716 /* Complain about unrecognized command-line arguments.
1718 for (i = 1; i < argc; i++)
1721 if (s[0] == '-' && s[1] == '-')
1723 if (!strcmp (s, "-prefs"))
1727 fprintf (stderr, "usage: %s [ -display dpy-string ] [ -prefs ]\n",
1733 short_version = (char *) malloc (5);
1734 memcpy (short_version, screensaver_id + 17, 4);
1735 short_version [4] = 0;
1737 /* Load the init file, which may end up consulting the X resource database
1738 and the site-wide app-defaults file. Note that at this point, it's
1739 important that `progname' be "xscreensaver", rather than whatever
1746 /* Now that Xt has been initialized, and the resources have been read,
1747 we can set our `progname' variable to something more in line with
1750 progname = real_progname;
1755 XrmName name = { 0 };
1756 XrmClass class = { 0 };
1758 XrmEnumerateDatabase (db, &name, &class, XrmEnumAllLevels, mapper,
1764 /* Intern the atoms that xscreensaver_command() needs.
1766 XA_VROOT = XInternAtom (dpy, "__SWM_VROOT", False);
1767 XA_SCREENSAVER = XInternAtom (dpy, "SCREENSAVER", False);
1768 XA_SCREENSAVER_VERSION = XInternAtom (dpy, "_SCREENSAVER_VERSION",False);
1769 XA_SCREENSAVER_STATUS = XInternAtom (dpy, "_SCREENSAVER_STATUS", False);
1770 XA_SCREENSAVER_ID = XInternAtom (dpy, "_SCREENSAVER_ID", False);
1771 XA_SCREENSAVER_RESPONSE = XInternAtom (dpy, "_SCREENSAVER_RESPONSE", False);
1772 XA_SELECT = XInternAtom (dpy, "SELECT", False);
1773 XA_DEMO = XInternAtom (dpy, "DEMO", False);
1774 XA_ACTIVATE = XInternAtom (dpy, "ACTIVATE", False);
1775 XA_BLANK = XInternAtom (dpy, "BLANK", False);
1776 XA_LOCK = XInternAtom (dpy, "LOCK", False);
1777 XA_EXIT = XInternAtom (dpy, "EXIT", False);
1778 XA_RESTART = XInternAtom (dpy, "RESTART", False);
1780 /* Create the window and all its widgets.
1782 dialog = create_xscreensaver_demo (toplevel_shell);
1784 /* Set the window's title. */
1787 char *v = (char *) strdup(strchr(screensaver_id, ' '));
1788 char *s1, *s2, *s3, *s4;
1789 s1 = (char *) strchr(v, ' '); s1++;
1790 s2 = (char *) strchr(s1, ' ');
1791 s3 = (char *) strchr(v, '('); s3++;
1792 s4 = (char *) strchr(s3, ')');
1795 sprintf (title, "%.50s %.50s, %.50s", progclass, s1, s3);
1796 XtVaSetValues (toplevel_shell, XtNtitle, title, 0);
1800 sanity_check_resources (toplevel_shell);
1801 add_callbacks (toplevel_shell, pair);
1802 populate_hack_list (toplevel_shell, pair);
1803 populate_prefs_page (toplevel_shell, pair);
1804 sensitize_demo_widgets (toplevel_shell, False);
1805 scroll_to_current_hack (toplevel_shell, pair);
1807 XtManageChild (dialog);
1808 XtRealizeWidget(toplevel_shell);
1810 /* The next few calls must come after XtRealizeWidget(). */
1811 pixmapify_buttons (toplevel_shell);
1812 hack_button_sizes (toplevel_shell);
1813 ensure_selected_item_visible (name_to_widget (toplevel_shell, "list"));
1816 XtVaSetValues (toplevel_shell, XmNallowShellResize, False, 0);
1819 /* Handle the -prefs command-line argument. */
1822 Widget tabber = name_to_widget (toplevel_shell, "folder");
1823 Widget this_tab = name_to_widget (toplevel_shell, "optionsTab");
1824 Widget this_page = name_to_widget (toplevel_shell, "preferencesForm");
1827 if (!tabber) abort();
1829 XtVaGetValues (tabber, XmNnumChildren, &nkids, XmNchildren, &kids, 0);
1832 XtUnmanageChildren (kids, nkids);
1834 XtManageChild (this_page);
1836 XmProcessTraversal (this_tab, XmTRAVERSE_CURRENT);
1839 /* Issue any warnings about the running xscreensaver daemon. */
1840 the_network_is_not_the_computer (toplevel_shell);
1843 XtAppMainLoop (app);
1847 #endif /* HAVE_MOTIF -- whole file */