1 /* demo-Xm.c --- implements the interactive demo-mode and options dialogs.
2 * xscreensaver, Copyright (c) 1993-2003 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"
359 "This is the Motif version of \"xscreensaver-demo\". The Motif\n"
360 "version is no longer maintained. Please use the GTK version\n"
361 "instead, which has many more features.\n"
363 "For xscreensaver updates, check http://www.jwz.org/xscreensaver/",
367 warning_dialog (XtParent (button), buf, 100);
372 doc_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
374 prefs_pair *pair = (prefs_pair *) client_data;
376 saver_preferences *p = pair->a;
379 if (!p->help_url || !*p->help_url)
381 warning_dialog (XtParent (button),
383 "No Help URL has been specified.\n", 100);
387 help_command = (char *) malloc (strlen (p->load_url_command) +
388 (strlen (p->help_url) * 2) + 20);
389 strcpy (help_command, "( ");
390 sprintf (help_command + strlen(help_command),
391 p->load_url_command, p->help_url, p->help_url);
392 strcat (help_command, " ) &");
393 system (help_command);
399 activate_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
401 run_cmd (XtParent (button), XA_ACTIVATE, 0);
406 lock_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
408 run_cmd (XtParent (button), XA_LOCK, 0);
413 kill_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
415 run_cmd (XtParent (button), XA_EXIT, 0);
420 restart_menu_cb (Widget button, XtPointer client_data, XtPointer ignored)
423 run_cmd (XtParent (button), XA_RESTART, 0);
425 button = XtParent (button);
426 apply_changes_and_save (button);
427 xscreensaver_command (XtDisplay (button), XA_EXIT, 0, False, NULL);
429 system ("xscreensaver -nosplash &");
432 await_xscreensaver (button);
436 await_xscreensaver (Widget widget)
440 Display *dpy = XtDisplay (widget);
443 while (!rversion && (--countdown > 0))
445 /* Check for the version of the running xscreensaver... */
446 server_xscreensaver_version (dpy, &rversion, 0, 0);
448 /* If it's not there yet, wait a second... */
459 /* Timed out, no screensaver running. */
462 Bool root_p = (geteuid () == 0);
466 "The xscreensaver daemon did not start up properly.\n"
471 "You are running as root. This usually means that xscreensaver\n"
472 "was unable to contact your X server because access control is\n"
473 "turned on. Try running this command:\n"
475 " xhost +localhost\n"
477 "and then selecting `File / Restart Daemon'.\n"
479 "Note that turning off access control will allow anyone logged\n"
480 "on to this machine to access your screen, which might be\n"
481 "considered a security problem. Please read the xscreensaver\n"
482 "manual and FAQ for more information.\n"
484 "You shouldn't run X as root. Instead, you should log in as a\n"
485 "normal user, and `su' as necessary.");
487 strcat (buf, "Please check your $PATH and permissions.");
489 warning_dialog (XtParent (widget), buf, 1);
494 static int _selected_hack_number = -1;
497 selected_hack_number (Widget toplevel)
499 return _selected_hack_number;
504 demo_write_init_file (Widget widget, saver_preferences *p)
506 if (!write_init_file (p, short_version, False))
510 const char *f = init_file_name();
512 warning_dialog (widget,
513 "Error:\n\nCouldn't determine init file name!\n",
517 char *b = (char *) malloc (strlen(f) + 1024);
518 sprintf (b, "Error:\n\nCouldn't write %s\n", f);
519 warning_dialog (widget, b, 100);
528 apply_changes_and_save (Widget widget)
530 prefs_pair *pair = global_prefs_pair;
531 saver_preferences *p = pair->a;
532 Widget list_widget = name_to_widget (widget, "list");
533 int which = selected_hack_number (widget);
535 Widget cmd = name_to_widget (widget, "cmdText");
536 Widget enabled = name_to_widget (widget, "enabled");
538 Widget vis = name_to_widget (widget, "combo");
539 # ifdef HAVE_XMCOMBOBOX
541 # else /* !HAVE_XMCOMBOBOX */
542 Widget menu = 0, *kids = 0, selected_item = 0;
545 # endif /* !HAVE_XMCOMBOBOX */
547 Bool enabled_p = False;
548 const char *visual = 0;
549 const char *command = 0;
554 if (which < 0) return -1;
556 # ifdef HAVE_XMCOMBOBOX
557 XtVaGetValues (vis, XmNtextField, &text, NULL);
559 /* If we can't get at the text field of this combo box, we're screwed. */
561 XtVaGetValues (text, XmNvalue, &visual, NULL);
563 # else /* !HAVE_XMCOMBOBOX */
564 XtVaGetValues (vis, XmNsubMenuId, &menu, NULL);
565 XtVaGetValues (menu, XmNnumChildren, &nkids, XmNchildren, &kids, NULL);
566 XtVaGetValues (menu, XmNmenuHistory, &selected_item, NULL);
568 for (i = 0; i < nkids; i++)
569 if (kids[i] == selected_item)
572 visual = visual_menu[i];
573 # endif /* !HAVE_XMCOMBOBOX */
575 XtVaGetValues (enabled, XmNset, &enabled_p, NULL);
576 XtVaGetValues (cmd, XtNvalue, &command, NULL);
578 if (maybe_reload_init_file (widget, pair) != 0)
581 /* Sanity-check and canonicalize whatever the user typed into the combo box.
583 if (!strcasecmp (visual, "")) visual = "";
584 else if (!strcasecmp (visual, "any")) visual = "";
585 else if (!strcasecmp (visual, "default")) visual = "Default";
586 else if (!strcasecmp (visual, "default-n")) visual = "Default-N";
587 else if (!strcasecmp (visual, "default-i")) visual = "Default-I";
588 else if (!strcasecmp (visual, "best")) visual = "Best";
589 else if (!strcasecmp (visual, "mono")) visual = "Mono";
590 else if (!strcasecmp (visual, "monochrome")) visual = "Mono";
591 else if (!strcasecmp (visual, "gray")) visual = "Gray";
592 else if (!strcasecmp (visual, "grey")) visual = "Gray";
593 else if (!strcasecmp (visual, "color")) visual = "Color";
594 else if (!strcasecmp (visual, "gl")) visual = "GL";
595 else if (!strcasecmp (visual, "staticgray")) visual = "StaticGray";
596 else if (!strcasecmp (visual, "staticcolor")) visual = "StaticColor";
597 else if (!strcasecmp (visual, "truecolor")) visual = "TrueColor";
598 else if (!strcasecmp (visual, "grayscale")) visual = "GrayScale";
599 else if (!strcasecmp (visual, "greyscale")) visual = "GrayScale";
600 else if (!strcasecmp (visual, "pseudocolor")) visual = "PseudoColor";
601 else if (!strcasecmp (visual, "directcolor")) visual = "DirectColor";
602 else if (1 == sscanf (visual, " %ld %c", &id, &c)) ;
603 else if (1 == sscanf (visual, " 0x%lx %c", &id, &c)) ;
606 XBell (XtDisplay (widget), 0); /* unparsable */
608 /* #### gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (vis)->entry), "Any");*/
611 ensure_selected_item_visible (list_widget);
613 if (!p->screenhacks[which]->visual)
614 p->screenhacks[which]->visual = strdup ("");
615 if (!p->screenhacks[which]->command)
616 p->screenhacks[which]->command = strdup ("");
618 if (p->screenhacks[which]->enabled_p != enabled_p ||
619 !!strcasecmp (p->screenhacks[which]->visual, visual) ||
620 !!strcasecmp (p->screenhacks[which]->command, command))
622 /* Something was changed -- store results into the struct,
625 free (p->screenhacks[which]->visual);
626 free (p->screenhacks[which]->command);
627 p->screenhacks[which]->visual = strdup (visual);
628 p->screenhacks[which]->command = strdup (command);
629 p->screenhacks[which]->enabled_p = enabled_p;
631 return demo_write_init_file (widget, p);
634 /* No changes made */
639 run_this_cb (Widget button, XtPointer client_data, XtPointer ignored)
641 int which = selected_hack_number (XtParent (button));
642 if (which < 0) return;
643 if (0 == apply_changes_and_save (XtParent (button)))
644 run_hack (XtParent (button), which, True);
649 manual_cb (Widget button, XtPointer client_data, XtPointer ignored)
651 prefs_pair *pair = (prefs_pair *) client_data;
652 saver_preferences *p = pair->a;
653 Widget list_widget = name_to_widget (button, "list");
654 int which = selected_hack_number (button);
655 char *name, *name2, *cmd, *s;
656 if (which < 0) return;
657 apply_changes_and_save (button);
658 ensure_selected_item_visible (list_widget);
660 name = strdup (p->screenhacks[which]->command);
662 while (isspace (*name2)) name2++;
664 while (*s && !isspace (*s)) s++;
666 s = strrchr (name2, '/');
669 cmd = get_string_resource ("manualCommand", "ManualCommand");
672 char *cmd2 = (char *) malloc (strlen (cmd) + strlen (name2) + 100);
674 sprintf (cmd2 + strlen (cmd2),
676 name2, name2, name2, name2);
677 strcat (cmd2, " ) &");
683 warning_dialog (XtParent (button),
684 "Error:\n\nno `manualCommand' resource set.",
693 run_next_cb (Widget button, XtPointer client_data, XtPointer ignored)
695 prefs_pair *pair = (prefs_pair *) client_data;
696 saver_preferences *p = pair->a;
698 Widget list_widget = name_to_widget (button, "list");
699 int which = selected_hack_number (button);
701 button = XtParent (button);
708 if (which >= p->screenhacks_count)
711 apply_changes_and_save (button);
713 XmListDeselectAllItems (list_widget); /* LessTif lossage */
714 XmListSelectPos (list_widget, which+1, True);
716 ensure_selected_item_visible (list_widget);
717 populate_demo_window (button, which, pair);
718 run_hack (button, which, False);
723 run_prev_cb (Widget button, XtPointer client_data, XtPointer ignored)
725 prefs_pair *pair = (prefs_pair *) client_data;
726 saver_preferences *p = pair->a;
728 Widget list_widget = name_to_widget (button, "list");
729 int which = selected_hack_number (button);
731 button = XtParent (button);
734 which = p->screenhacks_count - 1;
739 which = p->screenhacks_count - 1;
741 apply_changes_and_save (button);
743 XmListDeselectAllItems (list_widget); /* LessTif lossage */
744 XmListSelectPos (list_widget, which+1, True);
746 ensure_selected_item_visible (list_widget);
747 populate_demo_window (button, which, pair);
748 run_hack (button, which, False);
752 /* Helper for the text fields that contain time specifications:
753 this parses the text, and does error checking.
756 hack_time_text (Widget button, const char *line, Time *store, Bool sec_p)
761 value = parse_time ((char *) line, sec_p, True);
762 value *= 1000; /* Time measures in microseconds */
768 "Unparsable time format: \"%s\"\n",
770 warning_dialog (XtParent (button), b, 100);
779 prefs_ok_cb (Widget button, XtPointer client_data, XtPointer ignored)
781 prefs_pair *pair = (prefs_pair *) client_data;
783 saver_preferences *p = pair->a;
784 saver_preferences *p2 = pair->b;
785 Bool changed = False;
788 button = XtParent (button);
790 # define SECONDS(field, name) \
792 XtVaGetValues (name_to_widget (button, (name)), XtNvalue, &v, NULL); \
793 hack_time_text (button, v, (field), True)
795 # define MINUTES(field, name) \
797 XtVaGetValues (name_to_widget (button, (name)), XtNvalue, &v, NULL); \
798 hack_time_text (button, v, (field), False)
800 # define INTEGER(field, name) do { \
801 unsigned int value; \
803 XtVaGetValues (name_to_widget (button, (name)), XtNvalue, &v, NULL); \
806 else if (sscanf (v, "%u%c", &value, &c) != 1) \
809 sprintf (b, "Error:\n\n" "Not an integer: \"%s\"\n", v); \
810 warning_dialog (XtParent (button), b, 100); \
816 # define CHECKBOX(field, name) \
817 XtVaGetValues (name_to_widget (button, (name)), XmNset, &field, NULL)
819 MINUTES (&p2->timeout, "timeoutText");
820 MINUTES (&p2->cycle, "cycleText");
821 SECONDS (&p2->fade_seconds, "fadeSecondsText");
822 INTEGER (&p2->fade_ticks, "fadeTicksText");
823 MINUTES (&p2->lock_timeout, "lockText");
824 SECONDS (&p2->passwd_timeout, "passwdText");
825 CHECKBOX (p2->verbose_p, "verboseToggle");
826 CHECKBOX (p2->install_cmap_p, "cmapToggle");
827 CHECKBOX (p2->fade_p, "fadeToggle");
828 CHECKBOX (p2->unfade_p, "unfadeToggle");
829 CHECKBOX (p2->lock_p, "lockToggle");
836 # define COPY(field) \
837 if (p->field != p2->field) changed = True; \
843 COPY(passwd_timeout);
847 COPY(install_cmap_p);
853 populate_prefs_page (button, pair);
856 demo_write_init_file (button, p);
861 prefs_cancel_cb (Widget button, XtPointer client_data, XtPointer ignored)
863 prefs_pair *pair = (prefs_pair *) client_data;
866 populate_prefs_page (XtParent (button), pair);
871 list_select_cb (Widget list, XtPointer client_data, XtPointer call_data)
873 prefs_pair *pair = (prefs_pair *) client_data;
875 XmListCallbackStruct *lcb = (XmListCallbackStruct *) call_data;
876 int which = lcb->item_position - 1;
878 apply_changes_and_save (list);
879 populate_demo_window (list, which, pair);
881 if (lcb->reason == XmCR_DEFAULT_ACTION && which >= 0)
882 run_hack (list, which, True);
886 /* Populating the various widgets
890 /* Formats a `Time' into "H:MM:SS". (Time is microseconds.)
893 format_time (char *buf, Time time)
896 unsigned int h = 0, m = 0;
907 sprintf (buf, "%u:%02u:%02u", h, m, s);
911 /* Finds the number of the last hack to run, and makes that item be
915 scroll_to_current_hack (Widget toplevel, prefs_pair *pair)
919 unsigned long nitems, bytesafter;
921 Display *dpy = XtDisplay (toplevel);
925 if (XGetWindowProperty (dpy, RootWindow (dpy, 0), /* always screen #0 */
926 XA_SCREENSAVER_STATUS,
927 0, 3, False, XA_INTEGER,
928 &type, &format, &nitems, &bytesafter,
929 (unsigned char **) &data)
931 && type == XA_INTEGER
934 which = (int) data[2] - 1;
936 if (data) free (data);
941 list = name_to_widget (toplevel, "list");
942 apply_changes_and_save (toplevel);
944 XmListDeselectAllItems (list); /* LessTif lossage */
945 XmListSelectPos (list, which+1, True);
947 ensure_selected_item_visible (list);
948 populate_demo_window (toplevel, which, pair);
954 populate_hack_list (Widget toplevel, prefs_pair *pair)
956 saver_preferences *p = pair->a;
957 Widget list = name_to_widget (toplevel, "list");
958 screenhack **hacks = p->screenhacks;
961 for (h = hacks; *h; h++)
963 char *pretty_name = (h[0]->name
964 ? strdup (h[0]->name)
965 : make_hack_name (h[0]->command));
967 XmString xmstr = XmStringCreate (pretty_name, XmSTRING_DEFAULT_CHARSET);
968 XmListAddItem (list, xmstr, 0);
969 XmStringFree (xmstr);
972 XtAddCallback (list, XmNbrowseSelectionCallback, list_select_cb, pair);
973 XtAddCallback (list, XmNdefaultActionCallback, list_select_cb, pair);
978 populate_prefs_page (Widget top, prefs_pair *pair)
980 saver_preferences *p = pair->a;
983 format_time (s, p->timeout);
984 XtVaSetValues (name_to_widget (top, "timeoutText"), XmNvalue, s, NULL);
985 format_time (s, p->cycle);
986 XtVaSetValues (name_to_widget (top, "cycleText"), XmNvalue, s, NULL);
987 format_time (s, p->lock_timeout);
988 XtVaSetValues (name_to_widget (top, "lockText"), XmNvalue, s, NULL);
989 format_time (s, p->passwd_timeout);
990 XtVaSetValues (name_to_widget (top, "passwdText"), XmNvalue, s, NULL);
991 format_time (s, p->fade_seconds);
992 XtVaSetValues (name_to_widget (top, "fadeSecondsText"), XmNvalue, s, NULL);
993 sprintf (s, "%u", p->fade_ticks);
994 XtVaSetValues (name_to_widget (top, "fadeTicksText"), XmNvalue, s, NULL);
996 XtVaSetValues (name_to_widget (top, "verboseToggle"),
997 XmNset, p->verbose_p, NULL);
998 XtVaSetValues (name_to_widget (top, "cmapToggle"),
999 XmNset, p->install_cmap_p, NULL);
1000 XtVaSetValues (name_to_widget (top, "fadeToggle"),
1001 XmNset, p->fade_p, NULL);
1002 XtVaSetValues (name_to_widget (top, "unfadeToggle"),
1003 XmNset, p->unfade_p, NULL);
1004 XtVaSetValues (name_to_widget (top, "lockToggle"),
1005 XmNset, p->lock_p, NULL);
1009 Bool found_any_writable_cells = False;
1010 Display *dpy = XtDisplay (top);
1011 int nscreens = ScreenCount(dpy);
1013 for (i = 0; i < nscreens; i++)
1015 Screen *s = ScreenOfDisplay (dpy, i);
1016 if (has_writable_cells (s, DefaultVisualOfScreen (s)))
1018 found_any_writable_cells = True;
1023 #ifdef HAVE_XF86VMODE_GAMMA
1024 found_any_writable_cells = True; /* if we can gamma fade, go for it */
1027 XtVaSetValues (name_to_widget (top, "fadeSecondsLabel"), XtNsensitive,
1028 found_any_writable_cells, NULL);
1029 XtVaSetValues (name_to_widget (top, "fadeTicksLabel"), XtNsensitive,
1030 found_any_writable_cells, NULL);
1031 XtVaSetValues (name_to_widget (top, "fadeSecondsText"), XtNsensitive,
1032 found_any_writable_cells, NULL);
1033 XtVaSetValues (name_to_widget (top, "fadeTicksText"), XtNsensitive,
1034 found_any_writable_cells, NULL);
1035 XtVaSetValues (name_to_widget (top, "cmapToggle"), XtNsensitive,
1036 found_any_writable_cells, NULL);
1037 XtVaSetValues (name_to_widget (top, "fadeToggle"), XtNsensitive,
1038 found_any_writable_cells, NULL);
1039 XtVaSetValues (name_to_widget (top, "unfadeToggle"), XtNsensitive,
1040 found_any_writable_cells, NULL);
1046 sensitize_demo_widgets (Widget toplevel, Bool sensitive_p)
1048 const char *names[] = { "cmdLabel", "cmdText", "enabled",
1049 "visLabel", "combo", "demo", "man" };
1051 for (i = 0; i < sizeof(names)/countof(*names); i++)
1053 Widget w = name_to_widget (toplevel, names[i]);
1054 XtVaSetValues (w, XtNsensitive, sensitive_p, NULL);
1057 /* I don't know how to handle these yet... */
1059 const char *names2[] = { "cut", "copy", "paste" };
1060 for (i = 0; i < sizeof(names2)/countof(*names2); i++)
1062 Widget w = name_to_widget (toplevel, names2[i]);
1063 XtVaSetValues (w, XtNsensitive, FALSE, NULL);
1070 /* Pixmaps for the up and down arrow buttons (yeah, this is sleazy...)
1075 static char *up_arrow_xpm[] = {
1077 " c None s background",
1099 static char *down_arrow_xpm[] = {
1101 " c None s background",
1123 #endif /* HAVE_XPM */
1127 pixmapify_buttons (Widget toplevel)
1131 Display *dpy = XtDisplay (toplevel);
1132 Window window = XtWindow (toplevel);
1133 XWindowAttributes xgwa;
1134 XpmAttributes xpmattrs;
1135 Pixmap up_pixmap = 0, down_pixmap = 0;
1137 Widget up = name_to_widget (toplevel, "up");
1138 Widget dn = name_to_widget (toplevel, "down");
1139 # ifdef XpmColorSymbols
1141 XpmColorSymbol symbols[2];
1145 XGetWindowAttributes (dpy, window, &xgwa);
1147 xpmattrs.valuemask = 0;
1149 # ifdef XpmColorSymbols
1150 symbols[0].name = "background";
1151 symbols[0].pixel = 0;
1152 symbols[1].name = 0;
1153 XtVaGetValues (up, XmNbackground, &xc, NULL);
1154 XQueryColor (dpy, xgwa.colormap, &xc);
1155 sprintf (color, "#%04X%04X%04X", xc.red, xc.green, xc.blue);
1156 symbols[0].value = color;
1157 symbols[0].pixel = xc.pixel;
1159 xpmattrs.valuemask |= XpmColorSymbols;
1160 xpmattrs.colorsymbols = symbols;
1161 xpmattrs.numsymbols = 1;
1164 # ifdef XpmCloseness
1165 xpmattrs.valuemask |= XpmCloseness;
1166 xpmattrs.closeness = 40000;
1169 xpmattrs.valuemask |= XpmVisual;
1170 xpmattrs.visual = xgwa.visual;
1173 xpmattrs.valuemask |= XpmDepth;
1174 xpmattrs.depth = xgwa.depth;
1177 xpmattrs.valuemask |= XpmColormap;
1178 xpmattrs.colormap = xgwa.colormap;
1181 result = XpmCreatePixmapFromData(dpy, window, up_arrow_xpm,
1182 &up_pixmap, 0 /* mask */, &xpmattrs);
1183 if (!up_pixmap || (result != XpmSuccess && result != XpmColorError))
1185 fprintf (stderr, "%s: Can't load pixmaps\n", progname);
1189 result = XpmCreatePixmapFromData(dpy, window, down_arrow_xpm,
1190 &down_pixmap, 0 /* mask */, &xpmattrs);
1191 if (!down_pixmap || (result != XpmSuccess && result != XpmColorError))
1193 fprintf (stderr, "%s: Can't load pixmaps\n", progname);
1197 XtVaSetValues (up, XmNlabelType, XmPIXMAP, XmNlabelPixmap, up_pixmap, NULL);
1198 XtVaSetValues (dn, XmNlabelType, XmPIXMAP, XmNlabelPixmap, down_pixmap,NULL);
1200 #endif /* HAVE_XPM */
1206 get_hack_blurb (screenhack *hack)
1209 char *prog_name = strdup (hack->command);
1210 char *pretty_name = (hack->name
1211 ? strdup (hack->name)
1212 : make_hack_name (hack->command));
1213 char doc_name[255], doc_class[255];
1216 for (s = prog_name; *s && !isspace(*s); s++)
1219 s = strrchr (prog_name, '/');
1220 if (s) strcpy (prog_name, s+1);
1222 sprintf (doc_name, "hacks.%s.documentation", pretty_name);
1223 sprintf (doc_class, "hacks.%s.documentation", prog_name);
1227 doc_string = get_string_resource (doc_name, doc_class);
1230 for (s = doc_string; *s; s++)
1234 /* skip over whitespace at beginning of line */
1236 while (*s && (*s == ' ' || *s == '\t'))
1239 else if (*s == ' ' || *s == '\t')
1241 /* compress all other horizontal whitespace. */
1244 for (s2 = s; *s2 && (*s2 == ' ' || *s2 == '\t'); s2++)
1246 if (s2 > s) strcpy (s, s2);
1251 while (*s && isspace (*s)) /* Strip trailing whitespace */
1254 /* Delete whitespace at end of each line. */
1255 for (; s > doc_string; s--)
1256 if (*s == '\n' && (s[-1] == ' ' || s[-1] == '\t'))
1259 s2 > doc_string && (*s2 == ' ' || *s2 == '\t');
1263 if (s2 < s) strcpy (s2, s);
1267 /* Delete leading blank lines. */
1268 for (s = doc_string; *s == '\n'; s++)
1270 if (s > doc_string) strcpy (doc_string, s);
1275 static int doc_installed = 0;
1276 if (doc_installed == 0)
1278 if (get_boolean_resource ("hacks.documentation.isInstalled",
1279 "hacks.documentation.isInstalled"))
1285 if (doc_installed < 0)
1287 strdup ("Error:\n\n"
1288 "The documentation strings do not appear to be "
1289 "installed. This is probably because there is "
1290 "an \"XScreenSaver\" app-defaults file installed "
1291 "that is from an older version of the program. "
1292 "To fix this problem, delete that file, or "
1293 "install a current version (either will work.)");
1296 doc_string = strdup (
1298 "This is the Motif version of \"xscreensaver-demo\". The Motif "
1299 "version is no longer maintained. Please use the GTK version "
1300 "instead, which has many more features."
1302 "If you were running the GTK version, there would be a preview "
1303 "of this screen saver mode displayed here, along with graphical "
1304 "configuration options.");
1312 populate_demo_window (Widget toplevel, int which, prefs_pair *pair)
1314 saver_preferences *p = pair->a;
1315 screenhack *hack = (which >= 0 ? p->screenhacks[which] : 0);
1316 Widget frameL = name_to_widget (toplevel, "frameLabel");
1317 Widget doc = name_to_widget (toplevel, "doc");
1318 Widget cmd = name_to_widget (toplevel, "cmdText");
1319 Widget enabled = name_to_widget (toplevel, "enabled");
1320 Widget vis = name_to_widget (toplevel, "combo");
1323 char *pretty_name = (hack
1325 ? strdup (hack->name)
1326 : make_hack_name (hack->command))
1328 char *doc_string = hack ? get_hack_blurb (hack) : 0;
1332 xmstr = XmStringCreate (pretty_name, XmSTRING_DEFAULT_CHARSET);
1333 XtVaSetValues (frameL, XmNlabelString, xmstr, NULL);
1334 XmStringFree (xmstr);
1336 XtVaSetValues (doc, XmNvalue, doc_string, NULL);
1337 XtVaSetValues (cmd, XmNvalue, (hack ? hack->command : ""), NULL);
1339 XtVaSetValues (enabled, XmNset, (hack ? hack->enabled_p : False), NULL);
1342 if (hack && hack->visual && *hack->visual)
1343 for (i = 0; visual_menu[i]; i++)
1344 if (!strcasecmp (hack->visual, visual_menu[i]))
1346 if (!visual_menu[i]) i = -1;
1349 # ifdef HAVE_XMCOMBOBOX
1351 XtVaGetValues (vis, XmNtextField, &text, NULL);
1352 XtVaSetValues (vis, XmNselectedPosition, i, NULL);
1354 XtVaSetValues (text, XmNvalue, hack->visual, NULL);
1355 # else /* !HAVE_XMCOMBOBOX */
1360 XtVaGetValues (vis, XmNsubMenuId, &menu, NULL);
1361 if (!menu) abort ();
1362 XtVaGetValues (menu, XmNnumChildren, &nkids, XmNchildren, &kids, NULL);
1365 XtVaSetValues (vis, XmNmenuHistory, kids[i], NULL);
1366 # endif /* !HAVE_XMCOMBOBOX */
1369 sensitize_demo_widgets (toplevel, (hack ? True : False));
1371 if (pretty_name) free (pretty_name);
1372 if (doc_string) free (doc_string);
1374 _selected_hack_number = which;
1380 maybe_reload_init_file (Widget widget, prefs_pair *pair)
1383 saver_preferences *p = pair->a;
1385 static Bool reentrant_lock = False;
1386 if (reentrant_lock) return 0;
1387 reentrant_lock = True;
1389 if (init_file_changed_p (p))
1391 const char *f = init_file_name();
1396 if (!f || !*f) return 0;
1397 b = (char *) malloc (strlen(f) + 1024);
1400 "file \"%s\" has changed, reloading.\n",
1402 warning_dialog (widget, b, 100);
1407 which = selected_hack_number (widget);
1408 list = name_to_widget (widget, "list");
1410 XtVaSetValues (list, XmNitemCount, 0, NULL);
1412 populate_hack_list (widget, pair);
1414 XmListDeselectAllItems (list); /* LessTif lossage */
1415 XmListSelectPos (list, which+1, True);
1417 populate_prefs_page (widget, pair);
1418 populate_demo_window (widget, which, pair);
1419 ensure_selected_item_visible (list);
1424 reentrant_lock = False;
1430 /* Attach all callback functions to widgets
1434 add_callbacks (Widget toplevel, prefs_pair *pair)
1438 # define CB(NAME,FN) \
1439 w = name_to_widget (toplevel, (NAME)); \
1440 XtAddCallback (w, XmNactivateCallback, (FN), pair)
1442 CB ("blank", activate_menu_cb);
1443 CB ("lock", lock_menu_cb);
1444 CB ("kill", kill_menu_cb);
1445 CB ("restart", restart_menu_cb);
1446 CB ("exit", exit_menu_cb);
1448 CB ("cut", cut_menu_cb);
1449 CB ("copy", copy_menu_cb);
1450 CB ("paste", paste_menu_cb);
1452 CB ("about", about_menu_cb);
1453 CB ("docMenu", doc_menu_cb);
1455 CB ("down", run_next_cb);
1456 CB ("up", run_prev_cb);
1457 CB ("demo", run_this_cb);
1458 CB ("man", manual_cb);
1460 CB ("preferencesForm.Cancel", prefs_cancel_cb);
1461 CB ("preferencesForm.OK", prefs_ok_cb);
1468 sanity_check_resources (Widget toplevel)
1470 const char *names[] = { "demoTab", "optionsTab", "cmdLabel", "visLabel",
1471 "enabled", "demo", "man", "timeoutLabel",
1472 "cycleLabel", "fadeSecondsLabel", "fadeTicksLabel",
1473 "lockLabel", "passwdLabel" };
1475 for (i = 0; i < sizeof(names)/countof(*names); i++)
1477 Widget w = name_to_widget (toplevel, names[i]);
1478 const char *name = XtName(w);
1481 XtVaGetValues (w, XmNlabelString, &xm, NULL);
1482 if (xm) XmStringGetLtoR (xm, XmSTRING_DEFAULT_CHARSET, &label);
1483 if (w && (!label || !strcmp (name, label)))
1485 xm = XmStringCreate ("ERROR", XmSTRING_DEFAULT_CHARSET);
1486 XtVaSetValues (w, XmNlabelString, xm, NULL);
1491 /* Set certain buttons to be the same size (the max of the set.)
1494 hack_button_sizes (Widget toplevel)
1496 Widget demo = name_to_widget (toplevel, "demo");
1497 Widget man = name_to_widget (toplevel, "man");
1498 Widget ok = name_to_widget (toplevel, "OK");
1499 Widget can = name_to_widget (toplevel, "Cancel");
1500 Widget up = name_to_widget (toplevel, "up");
1501 Widget down = name_to_widget (toplevel, "down");
1504 XtVaGetValues (demo, XmNwidth, &w1, NULL);
1505 XtVaGetValues (man, XmNwidth, &w2, NULL);
1506 XtVaSetValues ((w1 > w2 ? man : demo), XmNwidth, (w1 > w2 ? w1 : w2), NULL);
1508 XtVaGetValues (ok, XmNwidth, &w1, NULL);
1509 XtVaGetValues (can, XmNwidth, &w2, NULL);
1510 XtVaSetValues ((w1 > w2 ? can : ok), XmNwidth, (w1 > w2 ? w1 : w2), NULL);
1512 XtVaGetValues (up, XmNwidth, &w1, NULL);
1513 XtVaGetValues (down, XmNwidth, &w2, NULL);
1514 XtVaSetValues ((w1 > w2 ? down : up), XmNwidth, (w1 > w2 ? w1 : w2), NULL);
1520 /* The main demo-mode command loop.
1525 mapper (XrmDatabase *db, XrmBindingList bindings, XrmQuarkList quarks,
1526 XrmRepresentation *type, XrmValue *value, XPointer closure)
1529 for (i = 0; quarks[i]; i++)
1531 if (bindings[i] == XrmBindTightly)
1532 fprintf (stderr, (i == 0 ? "" : "."));
1533 else if (bindings[i] == XrmBindLoosely)
1534 fprintf (stderr, "*");
1536 fprintf (stderr, " ??? ");
1537 fprintf(stderr, "%s", XrmQuarkToString (quarks[i]));
1540 fprintf (stderr, ": %s\n", (char *) value->addr);
1548 the_network_is_not_the_computer (Widget parent)
1550 Display *dpy = XtDisplay (parent);
1551 char *rversion, *ruser, *rhost;
1552 char *luser, *lhost;
1554 struct passwd *p = getpwuid (getuid ());
1555 const char *d = DisplayString (dpy);
1557 # if defined(HAVE_UNAME)
1559 if (uname (&uts) < 0)
1560 lhost = "<UNKNOWN>";
1562 lhost = uts.nodename;
1564 strcpy (lhost, getenv("SYS$NODE"));
1565 # else /* !HAVE_UNAME && !VMS */
1566 strcat (lhost, "<UNKNOWN>");
1567 # endif /* !HAVE_UNAME && !VMS */
1569 if (p && p->pw_name)
1574 server_xscreensaver_version (dpy, &rversion, &ruser, &rhost);
1576 /* Make a buffer that's big enough for a number of copies of all the
1577 strings, plus some. */
1578 msg = (char *) malloc (10 * ((rversion ? strlen(rversion) : 0) +
1579 (ruser ? strlen(ruser) : 0) +
1580 (rhost ? strlen(rhost) : 0) +
1587 if (!rversion || !*rversion)
1591 "The XScreenSaver daemon doesn't seem to be running\n"
1592 "on display \"%s\". You can launch it by selecting\n"
1593 "`Restart Daemon' from the File menu, or by typing\n"
1594 "\"xscreensaver &\" in a shell.",
1597 else if (p && ruser && *ruser && !!strcmp (ruser, p->pw_name))
1599 /* Warn that the two processes are running as different users.
1603 "%s is running as user \"%s\" on host \"%s\".\n"
1604 "But the xscreensaver managing display \"%s\"\n"
1605 "is running as user \"%s\" on host \"%s\".\n"
1607 "Since they are different users, they won't be reading/writing\n"
1608 "the same ~/.xscreensaver file, so %s isn't\n"
1609 "going to work right.\n"
1611 "Either re-run %s as \"%s\", or re-run\n"
1612 "xscreensaver as \"%s\" (which you can do by\n"
1613 "selecting `Restart Daemon' from the File menu.)\n",
1614 progname, luser, lhost,
1616 (ruser ? ruser : "???"), (rhost ? rhost : "???"),
1618 progname, (ruser ? ruser : "???"),
1621 else if (rhost && *rhost && !!strcmp (rhost, lhost))
1623 /* Warn that the two processes are running on different hosts.
1627 "%s is running as user \"%s\" on host \"%s\".\n"
1628 "But the xscreensaver managing display \"%s\"\n"
1629 "is running as user \"%s\" on host \"%s\".\n"
1631 "If those two machines don't share a file system (that is,\n"
1632 "if they don't see the same ~%s/.xscreensaver file) then\n"
1633 "%s won't work right.\n"
1635 "You can restart the daemon on \"%s\" as \"%s\" by\n"
1636 "selecting `Restart Daemon' from the File menu.)",
1637 progname, luser, lhost,
1639 (ruser ? ruser : "???"), (rhost ? rhost : "???"),
1644 else if (!!strcmp (rversion, short_version))
1646 /* Warn that the version numbers don't match.
1650 "This is %s version %s.\n"
1651 "But the xscreensaver managing display \"%s\"\n"
1652 "is version %s. This could cause problems.",
1653 progname, short_version,
1660 warning_dialog (parent, msg, 1);
1666 /* We use this error handler so that X errors are preceeded by the name
1667 of the program that generated them.
1670 demo_ehandler (Display *dpy, XErrorEvent *error)
1672 fprintf (stderr, "\nX error in %s:\n", progname);
1673 XmuPrintDefaultErrorMessage (dpy, error, stderr);
1680 static char *defaults[] = {
1681 #include "XScreenSaver_ad.h"
1687 main (int argc, char **argv)
1690 prefs_pair Pair, *pair;
1691 saver_preferences P, P2, *p, *p2;
1695 Widget toplevel_shell, dialog;
1696 char *real_progname = argv[0];
1699 s = strrchr (real_progname, '/');
1700 if (s) real_progname = s+1;
1707 memset (p, 0, sizeof (*p));
1708 memset (p2, 0, sizeof (*p2));
1710 global_prefs_pair = pair;
1712 progname = real_progname;
1714 /* We must read exactly the same resources as xscreensaver.
1715 That means we must have both the same progclass *and* progname,
1716 at least as far as the resource database is concerned. So,
1717 put "xscreensaver" in argv[0] while initializing Xt.
1719 argv[0] = "xscreensaver";
1723 toplevel_shell = XtAppInitialize (&app, progclass, 0, 0, &argc, argv,
1726 dpy = XtDisplay (toplevel_shell);
1727 db = XtDatabase (dpy);
1728 XtGetApplicationNameAndClass (dpy, &progname, &progclass);
1729 XSetErrorHandler (demo_ehandler);
1731 /* Complain about unrecognized command-line arguments.
1733 for (i = 1; i < argc; i++)
1736 if (s[0] == '-' && s[1] == '-')
1738 if (!strcmp (s, "-prefs"))
1742 fprintf (stderr, "usage: %s [ -display dpy-string ] [ -prefs ]\n",
1748 short_version = (char *) malloc (5);
1749 memcpy (short_version, screensaver_id + 17, 4);
1750 short_version [4] = 0;
1752 /* Load the init file, which may end up consulting the X resource database
1753 and the site-wide app-defaults file. Note that at this point, it's
1754 important that `progname' be "xscreensaver", rather than whatever
1761 /* Now that Xt has been initialized, and the resources have been read,
1762 we can set our `progname' variable to something more in line with
1765 progname = real_progname;
1770 XrmName name = { 0 };
1771 XrmClass class = { 0 };
1773 XrmEnumerateDatabase (db, &name, &class, XrmEnumAllLevels, mapper,
1779 /* Intern the atoms that xscreensaver_command() needs.
1781 XA_VROOT = XInternAtom (dpy, "__SWM_VROOT", False);
1782 XA_SCREENSAVER = XInternAtom (dpy, "SCREENSAVER", False);
1783 XA_SCREENSAVER_VERSION = XInternAtom (dpy, "_SCREENSAVER_VERSION",False);
1784 XA_SCREENSAVER_STATUS = XInternAtom (dpy, "_SCREENSAVER_STATUS", False);
1785 XA_SCREENSAVER_ID = XInternAtom (dpy, "_SCREENSAVER_ID", False);
1786 XA_SCREENSAVER_RESPONSE = XInternAtom (dpy, "_SCREENSAVER_RESPONSE", False);
1787 XA_SELECT = XInternAtom (dpy, "SELECT", False);
1788 XA_DEMO = XInternAtom (dpy, "DEMO", False);
1789 XA_ACTIVATE = XInternAtom (dpy, "ACTIVATE", False);
1790 XA_BLANK = XInternAtom (dpy, "BLANK", False);
1791 XA_LOCK = XInternAtom (dpy, "LOCK", False);
1792 XA_EXIT = XInternAtom (dpy, "EXIT", False);
1793 XA_RESTART = XInternAtom (dpy, "RESTART", False);
1795 /* Create the window and all its widgets.
1797 dialog = create_xscreensaver_demo (toplevel_shell);
1799 /* Set the window's title. */
1802 char *v = (char *) strdup(strchr(screensaver_id, ' '));
1803 char *s1, *s2, *s3, *s4;
1804 s1 = (char *) strchr(v, ' '); s1++;
1805 s2 = (char *) strchr(s1, ' ');
1806 s3 = (char *) strchr(v, '('); s3++;
1807 s4 = (char *) strchr(s3, ')');
1810 sprintf (title, "%.50s %.50s, %.50s", progclass, s1, s3);
1811 XtVaSetValues (toplevel_shell, XtNtitle, title, NULL);
1815 sanity_check_resources (toplevel_shell);
1816 add_callbacks (toplevel_shell, pair);
1817 populate_hack_list (toplevel_shell, pair);
1818 populate_prefs_page (toplevel_shell, pair);
1819 sensitize_demo_widgets (toplevel_shell, False);
1820 scroll_to_current_hack (toplevel_shell, pair);
1822 XtManageChild (dialog);
1823 XtRealizeWidget(toplevel_shell);
1825 /* The next few calls must come after XtRealizeWidget(). */
1826 pixmapify_buttons (toplevel_shell);
1827 hack_button_sizes (toplevel_shell);
1828 ensure_selected_item_visible (name_to_widget (toplevel_shell, "list"));
1831 XtVaSetValues (toplevel_shell, XmNallowShellResize, False, NULL);
1834 /* Handle the -prefs command-line argument. */
1837 Widget tabber = name_to_widget (toplevel_shell, "folder");
1838 Widget this_tab = name_to_widget (toplevel_shell, "optionsTab");
1839 Widget this_page = name_to_widget (toplevel_shell, "preferencesForm");
1842 if (!tabber) abort();
1844 XtVaGetValues (tabber, XmNnumChildren, &nkids, XmNchildren, &kids, NULL);
1847 XtUnmanageChildren (kids, nkids);
1849 XtManageChild (this_page);
1851 XmProcessTraversal (this_tab, XmTRAVERSE_CURRENT);
1854 /* Issue any warnings about the running xscreensaver daemon. */
1855 the_network_is_not_the_computer (toplevel_shell);
1858 XtAppMainLoop (app);
1862 #endif /* HAVE_MOTIF -- whole file */