1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.com>
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
12 /* Make a little guy with a big nose and a hat wanter around the screen,
13 spewing out messages. Derived from xnlock by Dan Heller <argv@sun.com>.
16 #include "screenhack.h"
20 extern FILE *popen (const char *, const char *);
21 extern int pclose (FILE *);
24 #define Pixel unsigned long
26 #define font_height(font) (font->ascent + font->descent)
27 #define FONT_NAME "-*-times-*-*-*-*-18-*-*-*-*-*-*-*"
31 static int Width, Height;
32 static GC fg_gc, bg_gc, text_fg_gc, text_bg_gc;
33 static char *words, *get_words();
35 static XFontStruct *font;
36 static char *def_words = "I'm out running around.";
37 static void init_images(), walk(), talk();
39 static unsigned long interval, look();
40 static Pixmap left0, left1, right0, right1;
41 static Pixmap left_front, right_front, front, down;
43 static char *program, *orig_program, *filename, *text;
46 #define FROM_PROGRAM 2
49 static int getwordsfrom;
53 static int state; /* indicates states: walking or getting passwd */
55 static void (*next_fn) ();
57 #include "noses/nose.0.left"
58 #include "noses/nose.1.left"
59 #include "noses/nose.0.right"
60 #include "noses/nose.1.right"
61 #include "noses/nose.left.front"
62 #include "noses/nose.right.front"
63 #include "noses/nose.front"
64 #include "noses/nose.down"
69 static Pixmap *images[] = {
70 &left0, &left1, &right0, &right1,
71 &left_front, &right_front, &front, &down
73 static unsigned char *bits[] = {
74 nose_0_left_bits, nose_1_left_bits, nose_0_right_bits,
75 nose_1_right_bits, nose_left_front_bits, nose_right_front_bits,
76 nose_front_bits, nose_down_bits
80 for (i = 0; i < sizeof (images) / sizeof (images[0]); i++)
82 XCreatePixmapFromBitmapData(dpy, window,
83 (char *) bits[i], 64, 64, 1, 0, 1)))
85 fprintf (stderr, "%s: Can't load nose images", progname);
106 register int tries = 0;
108 if ((random() & 1) && think())
110 talk(0); /* sets timeout to itself */
113 if (!(random() % 3) && (interval = look()))
118 interval = 20 + random() % 100;
122 length = Width / 100 + random() % 90, tries = 8;
125 switch (random() % 8)
128 if (x - X_INCR * length >= 5)
132 if (x + X_INCR * length <= Width - 70)
136 if (y - (Y_INCR * length) >= 5)
137 dir = UP, interval = 40;
140 if (y + Y_INCR * length <= Height - 70)
141 dir = DOWN, interval = 20;
144 if (x - X_INCR * length >= 5 && y - (Y_INCR * length) >= 5)
148 if (x + X_INCR * length <= Width - 70 &&
149 y - Y_INCR * length >= 5)
153 if (x - X_INCR * length >= 5 &&
154 y + Y_INCR * length <= Height - 70)
158 if (x + X_INCR * length <= Width - 70 &&
159 y + Y_INCR * length <= Height - 70)
160 dir = (RIGHT | DOWN);
177 register int incr = 0;
182 if (dir & (LEFT | RIGHT))
183 { /* left/right movement (mabye up/down too) */
184 up = -up; /* bouncing effect (even if hit a wall) */
188 frame = (up < 0) ? left0 : left1;
193 frame = (up < 0) ? right0 : right1;
195 if ((lastdir == FRONT || lastdir == DOWN) && dir & UP)
199 * workaround silly bug that leaves screen dust when guy is
200 * facing forward or down and moves up-left/right.
202 XCopyPlane(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y, 1L);
205 /* note that maybe neither UP nor DOWN is set! */
206 if (dir & UP && y > Y_INCR)
208 else if (dir & DOWN && y < Height - 64)
211 /* Explicit up/down movement only (no left/right) */
213 XCopyPlane(dpy, front, window, fg_gc,
214 0, 0, 64, 64, x, y -= Y_INCR, 1L);
215 else if (dir == DOWN)
216 XCopyPlane(dpy, down, window, fg_gc,
217 0, 0, 64, 64, x, y += Y_INCR, 1L);
218 else if (dir == FRONT && frame != front)
224 else if (lastdir & RIGHT)
228 XCopyPlane(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y, 1L);
233 XCopyPlane(dpy, frame, window, fg_gc,
234 0, 0, 64, 64, --x, y + up, 1L);
237 else if (dir & RIGHT)
240 XCopyPlane(dpy, frame, window, fg_gc,
241 0, 0, 64, 64, ++x, y + up, 1L);
254 if (getwordsfrom == FROM_PROGRAM)
255 words = get_words(0, (char **) 0);
286 /* clear what we've written */
287 if (talking || force_erase)
291 XFillRectangle(dpy, window, bg_gc, s_rect.x - 5, s_rect.y - 5,
292 s_rect.width + 10, s_rect.height + 10);
298 /* might as well check the window for size changes now... */
299 XWindowAttributes xgwa;
300 XGetWindowAttributes (dpy, window, &xgwa);
301 Width = xgwa.width + 2;
302 Height = xgwa.height + 2;
308 p = strcpy(buf, words);
310 if (!(p2 = index(p, '\n')) || !p2[1])
312 total = strlen (words);
313 strcpy (args[0], words);
314 width = XTextWidth(font, words, total);
318 /* p2 now points to the first '\n' */
319 for (height = 0; p; height++)
323 if ((w = XTextWidth(font, p, p2 - p)) > width)
325 total += p2 - p; /* total chars; count to determine reading
327 (void) strcpy(args[height], p);
328 if (height == MAXLINES - 1)
330 puts("Message too long!");
334 if (!(p2 = index(p, '\n')))
340 * Figure out the height and width in pixels (height, width) extend the
341 * new box by 15 pixels on the sides (30 total) top and bottom.
343 s_rect.width = width + 30;
344 s_rect.height = height * font_height(font) + 30;
345 if (x - s_rect.width - 10 < 5)
347 else if ((s_rect.x = x + 32 - (s_rect.width + 15) / 2)
348 + s_rect.width + 15 > Width - 5)
349 s_rect.x = Width - 15 - s_rect.width;
350 if (y - s_rect.height - 10 < 5)
351 s_rect.y = y + 64 + 5;
353 s_rect.y = y - 5 - s_rect.height;
355 XFillRectangle(dpy, window, text_bg_gc,
356 s_rect.x, s_rect.y, s_rect.width, s_rect.height);
358 /* make a box that's 5 pixels thick. Then add a thin box inside it */
359 XSetLineAttributes(dpy, text_fg_gc, 5, 0, 0, 0);
360 XDrawRectangle(dpy, window, text_fg_gc,
361 s_rect.x, s_rect.y, s_rect.width - 1, s_rect.height - 1);
362 XSetLineAttributes(dpy, text_fg_gc, 0, 0, 0, 0);
363 XDrawRectangle(dpy, window, text_fg_gc,
364 s_rect.x + 7, s_rect.y + 7, s_rect.width - 15, s_rect.height - 15);
367 Y = 15 + font_height(font);
369 /* now print each string in reverse order (start at bottom of box) */
370 for (Z = 0; Z < height; Z++)
372 XDrawString(dpy, window, text_fg_gc, s_rect.x + X, s_rect.y + Y,
373 args[Z], strlen(args[Z]));
374 Y += font_height(font);
376 interval = (total / 15) * 1000;
377 if (interval < 2000) interval = 2000;
386 XCopyPlane(dpy, (random() & 1) ? down : front, window, fg_gc,
387 0, 0, 64, 64, x, y, 1L);
394 XCopyPlane(dpy, (random() & 1) ? left_front : right_front,
395 window, fg_gc, 0, 0, 64, 64, x, y, 1L);
400 XCopyPlane(dpy, (random() & 1) ? left0 : right0, window, fg_gc,
401 0, 0, 64, 64, x, y, 1L);
409 char *mode = get_string_resource ("mode", "Mode");
411 program = get_string_resource ("program", "Program");
412 filename = get_string_resource ("filename", "Filename");
413 text = get_string_resource ("text", "Text");
415 if (program) /* get stderr on stdout, so it shows up on the window */
417 orig_program = program;
418 program = (char *) malloc (strlen (program) + 10);
419 strcpy (program, "( ");
420 strcat (program, orig_program);
421 strcat (program, " ) 2>&1");
424 if (!mode || !strcmp (mode, "program"))
425 getwordsfrom = FROM_PROGRAM;
426 else if (!strcmp (mode, "file"))
427 getwordsfrom = FROM_FILE;
428 else if (!strcmp (mode, "string"))
429 getwordsfrom = FROM_RESRC;
433 "%s: mode must be program, file, or string, not %s\n",
438 if (getwordsfrom == FROM_PROGRAM && !program)
440 fprintf (stderr, "%s: no program specified.\n", progname);
443 if (getwordsfrom == FROM_FILE && !filename)
445 fprintf (stderr, "%s: no file specified.\n", progname);
452 static int first_time = 1;
458 static char buf[BUFSIZ];
459 register char *p = buf;
463 switch (getwordsfrom)
466 if ((pp = popen(program, "r")))
468 while (fgets(p, sizeof(buf) - strlen(buf), pp))
470 if (strlen(buf) + 1 < sizeof(buf))
471 p = buf + strlen(buf);
477 sprintf (buf, "\"%s\" produced no output!", orig_program);
478 else if (!first_time &&
479 !strcmp (buf, "sh: fortune: not found\n"))
480 switch (random () % 20)
482 case 1: strcat (buf, "( Get with the program, bub. )\n");
485 "( I blow my nose at you, you silly person! ) \n"); break;
487 "\nThe resource you want to\nset is `noseguy.program'\n");
490 strcat(buf,"\nHelp!! Help!!\nAAAAAAGGGGHHH!! \n\n"); break;
491 case 5: strcpy (buf, "You have new mail.\n"); break;
493 strcat(buf,"( Hello? Are you paying attention? )\n");break;
495 strcat (buf, "sh: what kind of fool do you take me for? \n");
508 if ((pp = fopen(filename, "r")))
510 while (fgets(p, sizeof(buf) - strlen(buf), pp))
512 if (strlen(buf) + 1 < sizeof(buf))
513 p = buf + strlen(buf);
519 sprintf (buf, "file \"%s\" is empty!", filename);
524 sprintf (buf, "couldn't read file \"%s\"!", filename);
536 if (!p || *p == '\0')
543 char *progclass = "Noseguy";
545 char *defaults [] = {
546 "Noseguy.background: black", /* to placate SGI */
547 "Noseguy.foreground: white",
549 "*program: fortune -s",
550 "noseguy.font: -*-new century schoolbook-*-r-*-*-*-180-*-*-*-*-*-*",
554 XrmOptionDescRec options [] = {
555 { "-mode", ".mode", XrmoptionSepArg, 0 },
556 { "-program", ".program", XrmoptionSepArg, 0 },
557 { "-text", ".text", XrmoptionSepArg, 0 },
558 { "-filename", ".filename", XrmoptionSepArg, 0 },
559 { "-font", ".font", XrmoptionSepArg, 0 },
560 { "-text-foreground", ".textForeground", XrmoptionSepArg, 0 },
561 { "-text-background", ".textBackground", XrmoptionSepArg, 0 }
563 int options_size = (sizeof (options) / sizeof (options[0]));
571 Pixel fg, bg, text_fg, text_bg;
572 XWindowAttributes xgwa;
574 char *fontname = get_string_resource ("font", "Font");
580 XGetWindowAttributes (dpy, window, &xgwa);
581 Width = xgwa.width + 2;
582 Height = xgwa.height + 2;
583 cmap = xgwa.colormap;
588 if (!fontname || !(font = XLoadQueryFont(dpy, fontname)))
590 list = XListFonts(dpy, FONT_NAME, 32767, &foo);
591 for (i = 0; i < foo; i++)
592 if ((font = XLoadQueryFont(dpy, list[i])))
596 fprintf (stderr, "%s: Can't find a large font.", progname);
599 XFreeFontNames(list);
602 fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
603 bg = get_pixel_resource ("background", "Background", dpy, cmap);
604 text_fg = get_pixel_resource ("textForeground", "Foreground", dpy, cmap);
605 text_bg = get_pixel_resource ("textBackground", "Background", dpy, cmap);
606 /* notice when unspecified */
607 if (! get_string_resource ("textForeground", "Foreground"))
609 if (! get_string_resource ("textBackground", "Background"))
612 gcvalues.font = font->fid;
613 gcvalues.graphics_exposures = False;
614 gcvalues.foreground = fg;
615 gcvalues.background = bg;
616 fg_gc = XCreateGC (dpy, window,
617 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
619 gcvalues.foreground = bg;
620 gcvalues.background = fg;
621 bg_gc = XCreateGC (dpy, window,
622 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
624 gcvalues.foreground = text_fg;
625 gcvalues.background = text_bg;
626 text_fg_gc = XCreateGC (dpy, window,
627 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
629 gcvalues.foreground = text_bg;
630 gcvalues.background = text_fg;
631 text_bg_gc = XCreateGC (dpy, window,
632 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
650 usleep (interval * 1000);