1 /* xscreensaver, Copyright (c) 1992-2010 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * And remember: X Windows is to graphics hacking as roman numerals are to
12 * the square root of pi.
15 /* This file contains simple code to open a window or draw on the root.
16 The idea being that, when writing a graphics hack, you can just link
17 with this .o to get all of the uninteresting junk out of the way.
19 Create a few static global procedures and variables:
21 static void *YOURNAME_init (Display *, Window);
23 Return an opaque structure representing your drawing state.
25 static unsigned long YOURNAME_draw (Display *, Window, void *closure);
28 The `closure' arg is your drawing state, that you created in `init'.
29 Return the number of microseconds to wait until the next frame.
31 This should return in some small fraction of a second.
32 Do not call `usleep' or loop excessively. For long loops, use a
35 static void YOURNAME_reshape (Display *, Window, void *closure,
36 unsigned int width, unsigned int height);
38 Called when the size of the window changes with the new size.
40 static Bool YOURNAME_event (Display *, Window, void *closure,
43 Called when a keyboard or mouse event arrives.
44 Return True if you handle it in some way, False otherwise.
46 static void YOURNAME_free (Display *, Window, void *closure);
48 Called when you are done: free everything you've allocated,
49 including your private `state' structure.
51 NOTE: this is called in windowed-mode when the user typed
52 'q' or clicks on the window's close box; but when
53 xscreensaver terminates this screenhack, it does so by
54 killing the process with SIGSTOP. So this callback is
57 static char YOURNAME_defaults [] = { "...", "...", ... , 0 };
59 This variable is an array of strings, your default resources.
60 Null-terminate the list.
62 static XrmOptionDescRec YOURNAME_options[] = { { ... }, ... { 0,0,0,0 } }
64 This variable describes your command-line options.
65 Null-terminate the list.
67 Finally , invoke the XSCREENSAVER_MODULE() macro to tie it all together.
71 - Make sure that all functions in your module are static (check this
72 by running "nm -g" on the .o file).
74 - Do not use global variables: all such info must be stored in the
75 private `state' structure.
77 - Do not use static function-local variables, either. Put it in `state'.
79 Assume that there are N independent runs of this code going in the
80 same address space at the same time: they must not affect each other.
82 - Don't forget to write an XML file to describe the user interface
83 of your screen saver module. See .../hacks/config/README for details.
89 #include <X11/Intrinsic.h>
90 #include <X11/IntrinsicP.h>
91 #include <X11/CoreP.h>
92 #include <X11/Shell.h>
93 #include <X11/StringDefs.h>
94 #include <X11/keysym.h>
95 #include <dbus/dbus.h>
98 # include <X11/SGIScheme.h> /* for SgiUseSchemes() */
103 # include <X11/Xmu/Error.h>
105 # include <Xmu/Error.h>
111 #include "screenhackI.h"
116 #ifndef _XSCREENSAVER_VROOT_H_
117 # error Error! You have an old version of vroot.h! Check -I args.
118 #endif /* _XSCREENSAVER_VROOT_H_ */
121 # define isupper(c) ((c) >= 'A' && (c) <= 'Z')
124 # define _tolower(c) ((c) - 'A' + 'a')
128 /* This is defined by the SCREENHACK_MAIN() macro via screenhack.h.
130 extern struct xscreensaver_function_table *xscreensaver_function_table;
133 const char *progname; /* used by hacks in error messages */
134 const char *progclass; /* used by ../utils/resources.c */
135 Bool mono_p; /* used by hacks */
138 static XrmOptionDescRec default_options [] = {
139 { "-root", ".root", XrmoptionNoArg, "True" },
140 { "-window", ".root", XrmoptionNoArg, "False" },
141 { "-mono", ".mono", XrmoptionNoArg, "True" },
142 { "-install", ".installColormap", XrmoptionNoArg, "True" },
143 { "-noinstall",".installColormap", XrmoptionNoArg, "False" },
144 { "-visual", ".visualID", XrmoptionSepArg, 0 },
145 { "-window-id", ".windowID", XrmoptionSepArg, 0 },
146 { "-fps", ".doFPS", XrmoptionNoArg, "True" },
147 { "-no-fps", ".doFPS", XrmoptionNoArg, "False" },
148 { "-view", ".view", XrmoptionSepArg, 1 },
151 { "-pair", ".pair", XrmoptionNoArg, "True" },
152 # endif /* DEBUG_PAIR */
156 static char *default_defaults[] = {
158 "*geometry: 600x480", /* this should be .geometry, but nooooo... */
160 "*installColormap: false",
162 "*visualID: default",
164 "*desktopGrabber: xscreensaver-getimage %s",
168 static XrmOptionDescRec *merged_options;
169 static int merged_options_size;
170 static char **merged_defaults;
176 struct xscreensaver_function_table *ft = xscreensaver_function_table;
178 const XrmOptionDescRec *options = ft->options;
179 const char * const *defaults = ft->defaults;
180 const char *progclass = ft->progclass;
182 int def_opts_size, opts_size;
183 int def_defaults_size, defaults_size;
185 for (def_opts_size = 0; default_options[def_opts_size].option;
188 for (opts_size = 0; options[opts_size].option; opts_size++)
191 merged_options_size = def_opts_size + opts_size;
192 merged_options = (XrmOptionDescRec *)
193 malloc ((merged_options_size + 1) * sizeof(*default_options));
194 memcpy (merged_options, default_options,
195 (def_opts_size * sizeof(*default_options)));
196 memcpy (merged_options + def_opts_size, options,
197 ((opts_size + 1) * sizeof(*default_options)));
199 for (def_defaults_size = 0; default_defaults[def_defaults_size];
202 for (defaults_size = 0; defaults[defaults_size]; defaults_size++)
204 merged_defaults = (char **)
205 malloc ((def_defaults_size + defaults_size + 1) * sizeof (*defaults));;
206 memcpy (merged_defaults, default_defaults,
207 def_defaults_size * sizeof(*defaults));
208 memcpy (merged_defaults + def_defaults_size, defaults,
209 (defaults_size + 1) * sizeof(*defaults));
211 /* This totally sucks. Xt should behave like this by default.
212 If the string in `defaults' looks like ".foo", change that
217 for (s = merged_defaults; *s; s++)
220 const char *oldr = *s;
221 char *newr = (char *) malloc(strlen(oldr) + strlen(progclass) + 3);
222 strcpy (newr, progclass);
232 /* Make the X errors print out the name of this program, so we have some
233 clue which one has a bug when they die under the screensaver.
237 screenhack_ehandler (Display *dpy, XErrorEvent *error)
239 fprintf (stderr, "\nX error in %s:\n", progname);
240 if (XmuPrintDefaultErrorMessage (dpy, error, stderr))
243 fprintf (stderr, " (nonfatal.)\n");
248 MapNotify_event_p (Display *dpy, XEvent *event, XPointer window)
250 return (event->xany.type == MapNotify &&
251 event->xvisibility.window == (Window) window);
255 static Atom XA_WM_PROTOCOLS, XA_WM_DELETE_WINDOW;
257 /* Dead-trivial event handling: exits if "q" or "ESC" are typed.
258 Exit if the WM_PROTOCOLS WM_DELETE_WINDOW ClientMessage is received.
259 Returns False if the screen saver should now terminate.
262 screenhack_handle_event_1 (Display *dpy, XEvent *event)
264 switch (event->xany.type)
270 XLookupString (&event->xkey, &c, 1, &keysym, 0);
275 return False; /* exit */
276 else if (! (keysym >= XK_Shift_L && keysym <= XK_Hyper_R))
277 XBell (dpy, 0); /* beep for non-chord keys */
285 if (event->xclient.message_type != XA_WM_PROTOCOLS)
287 char *s = XGetAtomName(dpy, event->xclient.message_type);
288 if (!s) s = "(null)";
289 fprintf (stderr, "%s: unknown ClientMessage %s received!\n",
292 else if (event->xclient.data.l[0] != XA_WM_DELETE_WINDOW)
294 char *s1 = XGetAtomName(dpy, event->xclient.message_type);
295 char *s2 = XGetAtomName(dpy, event->xclient.data.l[0]);
296 if (!s1) s1 = "(null)";
297 if (!s2) s2 = "(null)";
298 fprintf (stderr, "%s: unknown ClientMessage %s[%s] received!\n",
303 return False; /* exit */
313 pick_visual (Screen *screen)
315 struct xscreensaver_function_table *ft = xscreensaver_function_table;
317 if (ft->pick_visual_hook)
319 Visual *v = ft->pick_visual_hook (screen);
323 return get_visual_resource (screen, "visualID", "VisualID", False);
327 /* Notice when the user has requested a different visual or colormap
328 on a pre-existing window (e.g., "-root -visual truecolor" or
329 "-window-id 0x2c00001 -install") and complain, since when drawing
330 on an existing window, we have no choice about these things.
333 visual_warning (Screen *screen, Window window, Visual *visual, Colormap cmap,
336 struct xscreensaver_function_table *ft = xscreensaver_function_table;
338 char *visual_string = get_string_resource (DisplayOfScreen (screen),
339 "visualID", "VisualID");
340 Visual *desired_visual = pick_visual (screen);
344 if (window == RootWindowOfScreen (screen))
345 strcpy (win, "root window");
347 sprintf (win, "window 0x%lx", (unsigned long) window);
350 sprintf (why, "-window-id 0x%lx", (unsigned long) window);
352 strcpy (why, "-root");
354 if (visual_string && *visual_string)
357 for (s = visual_string; *s; s++)
358 if (isupper (*s)) *s = _tolower (*s);
360 if (!strcmp (visual_string, "default") ||
361 !strcmp (visual_string, "default") ||
362 !strcmp (visual_string, "best"))
363 /* don't warn about these, just silently DWIM. */
365 else if (visual != desired_visual)
367 fprintf (stderr, "%s: ignoring `-visual %s' because of `%s'.\n",
368 progname, visual_string, why);
369 fprintf (stderr, "%s: using %s's visual 0x%lx.\n",
370 progname, win, XVisualIDFromVisual (visual));
372 free (visual_string);
375 if (visual == DefaultVisualOfScreen (screen) &&
376 has_writable_cells (screen, visual) &&
377 get_boolean_resource (DisplayOfScreen (screen),
378 "installColormap", "InstallColormap"))
380 fprintf (stderr, "%s: ignoring `-install' because of `%s'.\n",
382 fprintf (stderr, "%s: using %s's colormap 0x%lx.\n",
383 progname, win, (unsigned long) cmap);
386 if (ft->validate_visual_hook)
388 if (! ft->validate_visual_hook (screen, win, visual))
397 /* Bad Things Happen if stdin, stdout, and stderr have been closed
398 (as by the `sh incantation "attraction >&- 2>&-"). When you do
399 that, the X connection gets allocated to one of these fds, and
400 then some random library writes to stderr, and random bits get
401 stuffed down the X pipe, causing "Xlib: sequence lost" errors.
402 So, we cause the first three file descriptors to be open to
403 /dev/null if they aren't open to something else already. This
404 must be done before any other files are opened (or the closing
405 of that other file will again free up one of the "magic" first
408 We do this by opening /dev/null three times, and then closing
409 those fds, *unless* any of them got allocated as #0, #1, or #2,
410 in which case we leave them open. Gag.
412 Really, this crap is technically required of *every* X program,
413 if you want it to be robust in the face of "2>&-".
415 int fd0 = open ("/dev/null", O_RDWR);
416 int fd1 = open ("/dev/null", O_RDWR);
417 int fd2 = open ("/dev/null", O_RDWR);
418 if (fd0 > 2) close (fd0);
419 if (fd1 > 2) close (fd1);
420 if (fd2 > 2) close (fd2);
425 screenhack_table_handle_events (Display *dpy,
426 const struct xscreensaver_function_table *ft,
427 Window window, void *closure
429 , Window window2, void *closure2
433 XtAppContext app = XtDisplayToApplicationContext (dpy);
435 if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
436 XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
438 while (XPending (dpy))
441 XNextEvent (dpy, &event);
443 if (event.xany.type == ConfigureNotify)
445 if (event.xany.window == window)
446 ft->reshape_cb (dpy, window, closure,
447 event.xconfigure.width, event.xconfigure.height);
449 if (event.xany.window == window2)
450 ft->reshape_cb (dpy, window2, closure2,
451 event.xconfigure.width, event.xconfigure.height);
454 else if (event.xany.type == ClientMessage ||
455 (! (event.xany.window == window
456 ? ft->event_cb (dpy, window, closure, &event)
458 : event.xany.window == window2
459 ? ft->event_cb (dpy, window2, closure2, &event)
462 if (! screenhack_handle_event_1 (dpy, &event))
465 if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
466 XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
473 usleep_and_process_events (Display *dpy,
474 const struct xscreensaver_function_table *ft,
475 Window window, fps_state *fpst, void *closure,
478 , Window window2, fps_state *fpst2, void *closure2,
484 unsigned long quantum = 100000; /* 1/10th second */
493 if (fpst) fps_slept (fpst, quantum);
495 if (fpst2) fps_slept (fpst2, quantum);
499 if (! screenhack_table_handle_events (dpy, ft, window, closure
512 screenhack_do_fps (Display *dpy, Window w, fps_state *fpst, void *closure)
514 fps_compute (fpst, 0);
520 run_screenhack_table (Display *dpy,
525 const struct xscreensaver_function_table *ft,
528 #define LIVEWP_SIGNAL_INTERFACE "org.maemo.livewp"
529 #define LIVEWP_PAUSE_LIVEBG_ON_VIEW1 "pause_livebg_on_view1"
530 #define LIVEWP_PAUSE_LIVEBG_ON_VIEW2 "pause_livebg_on_view2"
531 #define LIVEWP_PAUSE_LIVEBG_ON_VIEW3 "pause_livebg_on_view3"
532 #define LIVEWP_PAUSE_LIVEBG_ON_VIEW4 "pause_livebg_on_view4"
533 #define LIVEWP_PAUSE_LIVEBG_ON_VIEW "pause_livebg_on_view"
534 #define LIVEWP_PLAY_LIVEBG_ON_VIEW1 "play_livebg_on_view1"
535 #define LIVEWP_PLAY_LIVEBG_ON_VIEW2 "play_livebg_on_view2"
536 #define LIVEWP_PLAY_LIVEBG_ON_VIEW3 "play_livebg_on_view3"
537 #define LIVEWP_PLAY_LIVEBG_ON_VIEW4 "play_livebg_on_view4"
538 #define LIVEWP_PLAY_LIVEBG_ON_VIEW "play_livebg_on_view"
540 DBusConnection* conn;
542 DBusMessageIter args;
546 int start_frame = 200;
548 /* Kludge: even though the init_cb functions are declared to take 2 args,
549 actually call them with 3, for the benefit of xlockmore_init() and
552 void *(*init_cb) (Display *, Window, void *) =
553 (void *(*) (Display *, Window, void *)) ft->init_cb;
555 void (*fps_cb) (Display *, Window, fps_state *, void *) = ft->fps_cb;
557 void *closure = init_cb (dpy, window, ft->setup_arg);
558 fps_state *fpst = fps_init (dpy, window);
562 fps_state *fpst2 = 0;
563 if (window2) closure2 = init_cb (dpy, window2, ft->setup_arg);
564 if (window2) fpst2 = fps_init (dpy, window2);
567 if (! closure) /* if it returns nothing, it can't possibly be re-entrant. */
570 if (! fps_cb) fps_cb = screenhack_do_fps;
572 /* initialise the error */
573 dbus_error_init(&err);
574 conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
575 if (dbus_error_is_set(&err)) {
576 fprintf(stderr, "Connection Error (%s)\n", err.message);
577 dbus_error_free(&err);
580 fprintf(stderr, "Connection Null\n");
584 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view1'", NULL);
585 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view1'", NULL);
588 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view2'", NULL);
589 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view2'", NULL);
592 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view3'", NULL);
593 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view3'", NULL);
596 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view4'", NULL);
597 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view4'", NULL);
600 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view5'", NULL);
601 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view5'", NULL);
604 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view6'", NULL);
605 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view6'", NULL);
608 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view7'", NULL);
609 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view7'", NULL);
612 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view8'", NULL);
613 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view8'", NULL);
616 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='pause_livebg_on_view9'", NULL);
617 dbus_bus_add_match (conn, "type='signal', interface='org.maemo.livewp', member='play_livebg_on_view9'", NULL);
620 dbus_connection_flush(conn);
625 unsigned long delay = ft->draw_cb (dpy, window, closure);
627 unsigned long delay2 = 0;
628 if (window2) delay2 = ft->draw_cb (dpy, window2, closure2);
632 if (fpst) fps_cb (dpy, window, fpst, closure);
634 if (fpst2) fps_cb (dpy, window, fpst2, closure);
636 if (! usleep_and_process_events (dpy, ft,
637 window, fpst, closure, delay
639 , window2, fpst2, closure2, delay2
645 if (start_frame > 0) {
651 /* non blocking read of the next available message */
652 dbus_connection_read_write(conn, 0);
654 /* blocking read of the next available message */
655 dbus_connection_read_write(conn, 20000);
657 msg = dbus_connection_pop_message(conn);
662 /* check this is a method call for the right interface & method */
663 if ((view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW1))||
664 (view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW2))||
665 (view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW3))||
666 (view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW4))||
667 (view == 5 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW5))||
668 (view == 6 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW6))||
669 (view == 7 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW7))||
670 (view == 8 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW8))||
671 (view == 9 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW9))){
672 /* fprintf(stderr, "Pause scene visible %i\n", view); */
674 dbus_message_unref (msg);
677 if ((view == 1 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW1))||
678 (view == 2 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW2))||
679 (view == 3 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW3))||
680 (view == 4 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW4))||
681 (view == 5 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW5))||
682 (view == 6 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW6))||
683 (view == 7 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW7))||
684 (view == 8 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW8))||
685 (view == 9 && dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW9))){
686 /* fprintf(stderr, "Play scene visible %i\n", view); */
688 dbus_message_unref (msg);
692 /* dbus_connection_steal_borrowed_message(conn, msg); */
693 /* fprintf (stderr, "APPLICATION PATH11111111111111111111 %s %s %s\n", dbus_message_get_path(msg), dbus_message_get_interface (msg), dbus_message_get_member (msg));*/
694 /*dbus_connection_return_message(conn, msg);*/
695 if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW) ||
696 dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW)){
697 if (!dbus_message_iter_init(msg, &args))
698 fprintf(stderr, "dbus message has no param\n");
699 else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args))
700 fprintf(stderr, "dbus message param is not int \n");
702 dbus_message_iter_get_basic(&args, ¶m);
703 fprintf(stderr, "dbus param = %i\n", param);
706 if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PAUSE_LIVEBG_ON_VIEW)){
707 fprintf(stderr, "dbus Pause scene visible %i\n", param);
710 if (dbus_message_is_signal(msg, LIVEWP_SIGNAL_INTERFACE, LIVEWP_PLAY_LIVEBG_ON_VIEW)){
711 fprintf(stderr, "dbus Play scene visible %i\n", param);
714 /*msg = dbus_connection_pop_message(conn);*/
715 fprintf(stderr, "dbus steal message serial = %i\n", dbus_message_get_serial(msg));
716 /*dbus_connection_steal_borrowed_message(conn, msg);*/
718 fprintf(stderr, "dbus return message\n");
719 /*dbus_connection_return_message(conn, msg);*/
723 /* dbus_message_unref (msg); */
726 ft->free_cb (dpy, window, closure);
727 if (fpst) fps_free (fpst);
730 if (window2) ft->free_cb (dpy, window2, closure2);
731 if (window2) fps_free (fpst2);
733 /* close the connection */
734 dbus_connection_close(conn);
739 make_shell (Screen *screen, Widget toplevel, int width, int height)
741 Display *dpy = DisplayOfScreen (screen);
742 Visual *visual = pick_visual (screen);
743 Boolean def_visual_p = (toplevel &&
744 visual == DefaultVisualOfScreen (screen));
746 if (width <= 0) width = 600;
747 if (height <= 0) height = 480;
752 XtVaSetValues (toplevel,
753 XtNmappedWhenManaged, False,
756 XtNinput, True, /* for WM_HINTS */
758 XtRealizeWidget (toplevel);
759 window = XtWindow (toplevel);
761 if (get_boolean_resource (dpy, "installColormap", "InstallColormap"))
764 XCreateColormap (dpy, window, DefaultVisualOfScreen (screen),
766 XSetWindowColormap (dpy, window, cmap);
773 Colormap cmap = XCreateColormap (dpy, VirtualRootWindowOfScreen(screen),
775 bg = get_pixel_resource (dpy, cmap, "background", "Background");
776 bd = get_pixel_resource (dpy, cmap, "borderColor", "Foreground");
778 new = XtVaAppCreateShell (progname, progclass,
779 topLevelShellWidgetClass, dpy,
780 XtNmappedWhenManaged, False,
782 XtNdepth, visual_depth (screen, visual),
786 XtNbackground, (Pixel) bg,
787 XtNborderColor, (Pixel) bd,
788 XtNinput, True, /* for WM_HINTS */
791 if (!toplevel) /* kludge for the second window in -pair mode... */
792 XtVaSetValues (new, XtNx, 0, XtNy, 550, NULL);
794 XtRealizeWidget (new);
802 init_window (Display *dpy, Widget toplevel, const char *title)
805 XWindowAttributes xgwa;
806 XtPopup (toplevel, XtGrabNone);
807 XtVaSetValues (toplevel, XtNtitle, title, NULL);
809 /* Select KeyPress, and announce that we accept WM_DELETE_WINDOW.
811 window = XtWindow (toplevel);
812 XGetWindowAttributes (dpy, window, &xgwa);
813 XSelectInput (dpy, window,
814 (xgwa.your_event_mask | KeyPressMask | KeyReleaseMask |
815 ButtonPressMask | ButtonReleaseMask));
816 XChangeProperty (dpy, window, XA_WM_PROTOCOLS, XA_ATOM, 32,
818 (unsigned char *) &XA_WM_DELETE_WINDOW, 1);
823 main (int argc, char **argv)
825 struct xscreensaver_function_table *ft = xscreensaver_function_table;
827 /* osso_context_t *osso; */
829 XWindowAttributes xgwa;
835 Widget toplevel2 = 0;
839 Window on_window = 0;
847 progname = argv[0]; /* reset later */
848 progclass = ft->progclass;
851 ft->setup_cb (ft, ft->setup_arg);
856 /* We have to do this on SGI to prevent the background color from being
857 overridden by the current desktop color scheme (we'd like our backgrounds
858 to be black, thanks.) This should be the same as setting the
859 "*useSchemes: none" resource, but it's not -- if that resource is
860 present in the `default_defaults' above, it doesn't work, though it
861 does work when passed as an -xrm arg on the command line. So screw it,
862 turn them off from C instead.
864 SgiUseSchemes ("none");
867 toplevel = XtAppInitialize (&app, progclass, merged_options,
868 merged_options_size, &argc, argv,
869 merged_defaults, 0, 0);
871 dpy = XtDisplay (toplevel);
873 XtGetApplicationNameAndClass (dpy,
875 (char **) &progclass);
877 /* half-assed way of avoiding buffer-overrun attacks. */
878 if (strlen (progname) >= 100) ((char *) progname)[100] = 0;
880 XSetErrorHandler (screenhack_ehandler);
882 XA_WM_PROTOCOLS = XInternAtom (dpy, "WM_PROTOCOLS", False);
883 XA_WM_DELETE_WINDOW = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
886 char *v = (char *) strdup(strchr(screensaver_id, ' '));
887 char *s1, *s2, *s3, *s4;
888 s1 = (char *) strchr(v, ' '); s1++;
889 s2 = (char *) strchr(s1, ' ');
890 s3 = (char *) strchr(v, '('); s3++;
891 s4 = (char *) strchr(s3, ')');
894 sprintf (version, "%s: from the XScreenSaver %s distribution (%s.)",
905 Bool help_p = (!strcmp(argv[1], "-help") ||
906 !strcmp(argv[1], "--help"));
907 fprintf (stderr, "%s\n", version);
908 for (s = progclass; *s; s++) fprintf(stderr, " ");
909 fprintf (stderr, " http://www.jwz.org/xscreensaver/\n\n");
912 fprintf(stderr, "Unrecognised option: %s\n", argv[1]);
913 fprintf (stderr, "Options include: ");
914 for (i = 0; i < merged_options_size; i++)
916 char *sw = merged_options [i].option;
917 Bool argp = (merged_options [i].argKind == XrmoptionSepArg);
918 int size = strlen (sw) + (argp ? 6 : 0) + 2;
921 fprintf (stderr, "\n\t\t ");
925 fprintf (stderr, "%s", sw);
926 if (argp) fprintf (stderr, " <arg>");
927 if (i != merged_options_size - 1) fprintf (stderr, ", ");
930 fprintf (stderr, ".\n");
935 fprintf (stderr, "\nResources:\n\n");
936 for (i = 0; i < merged_options_size; i++)
938 const char *opt = merged_options [i].option;
939 const char *res = merged_options [i].specifier + 1;
940 const char *val = merged_options [i].value;
941 char *s = get_string_resource (dpy, (char *) res, (char *) res);
946 while (L > 0 && (s[L-1] == ' ' || s[L-1] == '\t'))
950 fprintf (stderr, " %-16s %-18s ", opt, res);
951 if (merged_options [i].argKind == XrmoptionSepArg)
953 fprintf (stderr, "[%s]", (s ? s : "?"));
957 fprintf (stderr, "%s", (val ? val : "(null)"));
958 if (val && s && !strcasecmp (val, s))
959 fprintf (stderr, " [default]");
961 fprintf (stderr, "\n");
963 fprintf (stderr, "\n");
967 exit (help_p ? 0 : 1);
972 for (s = merged_defaults; *s; s++)
976 free (merged_options);
977 free (merged_defaults);
981 dont_clear = get_boolean_resource (dpy, "dontClearRoot", "Boolean");
982 mono_p = get_boolean_resource (dpy, "mono", "Boolean");
983 if (CellsOfScreen (DefaultScreenOfDisplay (dpy)) <= 2)
986 root_p = get_boolean_resource (dpy, "root", "Boolean");
989 char *s = get_string_resource (dpy, "windowID", "WindowID");
991 on_window = get_integer_resource (dpy, "windowID", "WindowID");
995 view = get_integer_resource (dpy, "view", "view");
996 fprintf(stderr, "View %i\n", view);
999 window = (Window) on_window;
1000 XtDestroyWidget (toplevel);
1001 XGetWindowAttributes (dpy, window, &xgwa);
1002 visual_warning (xgwa.screen, window, xgwa.visual, xgwa.colormap, True);
1004 /* Select KeyPress and resize events on the external window.
1006 xgwa.your_event_mask |= KeyPressMask | StructureNotifyMask;
1007 XSelectInput (dpy, window, xgwa.your_event_mask);
1009 /* Select ButtonPress and ButtonRelease events on the external window,
1010 if no other app has already selected them (only one app can select
1011 ButtonPress at a time: BadAccess results.)
1013 if (! (xgwa.all_event_masks & (ButtonPressMask | ButtonReleaseMask)))
1014 XSelectInput (dpy, window,
1015 (xgwa.your_event_mask |
1016 ButtonPressMask | ButtonReleaseMask));
1020 window = VirtualRootWindowOfScreen (XtScreen (toplevel));
1021 XtDestroyWidget (toplevel);
1022 XGetWindowAttributes (dpy, window, &xgwa);
1023 /* With RANDR, the root window can resize! */
1024 XSelectInput (dpy, window, xgwa.your_event_mask | StructureNotifyMask);
1025 visual_warning (xgwa.screen, window, xgwa.visual, xgwa.colormap, False);
1029 Widget new = make_shell (XtScreen (toplevel), toplevel,
1030 toplevel->core.width,
1031 toplevel->core.height);
1032 if (new != toplevel)
1034 XtDestroyWidget (toplevel);
1038 init_window (dpy, toplevel, version);
1039 window = XtWindow (toplevel);
1040 XGetWindowAttributes (dpy, window, &xgwa);
1043 if (get_boolean_resource (dpy, "pair", "Boolean"))
1045 toplevel2 = make_shell (xgwa.screen, 0,
1046 toplevel->core.width,
1047 toplevel->core.height);
1048 init_window (dpy, toplevel2, version);
1049 window2 = XtWindow (toplevel2);
1051 # endif /* DEBUG_PAIR */
1056 unsigned int bg = get_pixel_resource (dpy, xgwa.colormap,
1057 "background", "Background");
1058 XSetWindowBackground (dpy, window, bg);
1059 XClearWindow (dpy, window);
1063 XSetWindowBackground (dpy, window2, bg);
1064 XClearWindow (dpy, window2);
1069 if (!root_p && !on_window)
1070 /* wait for it to be mapped */
1071 XIfEvent (dpy, &event, MapNotify_event_p, (XPointer) window);
1075 /* This is the one and only place that the random-number generator is
1076 seeded in any screenhack. You do not need to seed the RNG again,
1077 it is done for you before your code is invoked. */
1078 # undef ya_rand_init
1081 run_screenhack_table (dpy, window,
1087 XtDestroyWidget (toplevel);
1088 XtDestroyApplicationContext (app);