1 /* xscreensaver, Copyright (c) 1992, 1996, 1997, 1998
2 * 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
13 /* Make a little guy with a big nose and a hat wanter around the screen,
14 spewing out messages. Derived from xnlock by
15 Dan Heller <argv@danheller.com>.
18 #include "screenhack.h"
19 #include "xpm-pixmap.h"
22 extern FILE *popen (const char *, const char *);
23 extern int pclose (FILE *);
25 #define font_height(font) (font->ascent + font->descent)
26 #define FONT_NAME "-*-times-*-*-*-*-18-*-*-*-*-*-*-*"
30 static int Width, Height;
31 static GC fg_gc, bg_gc, text_fg_gc, text_bg_gc;
33 static char *get_words (void);
35 static XFontStruct *font;
36 static char *def_words = "I'm out running around.";
37 static void walk (int dir);
38 static void talk (int erase);
39 static void talk_1 (void);
40 static int think (void);
41 static unsigned long interval;
42 static unsigned long look (void);
43 static Pixmap left1, left2, right1, right2;
44 static Pixmap left_front, right_front, front, down;
46 static char *program, *orig_program, *filename, *text;
49 #define FROM_PROGRAM 2
52 static int getwordsfrom;
56 static int state; /* indicates states: walking or getting passwd */
58 static void (*next_fn) (void);
60 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
61 # include "images/noseguy/nose-f1.xpm"
62 # include "images/noseguy/nose-f2.xpm"
63 # include "images/noseguy/nose-f3.xpm"
64 # include "images/noseguy/nose-f4.xpm"
65 # include "images/noseguy/nose-l1.xpm"
66 # include "images/noseguy/nose-l2.xpm"
67 # include "images/noseguy/nose-r1.xpm"
68 # include "images/noseguy/nose-r2.xpm"
70 # include "images/noseguy/nose-f1.xbm"
71 # include "images/noseguy/nose-f2.xbm"
72 # include "images/noseguy/nose-f3.xbm"
73 # include "images/noseguy/nose-f4.xbm"
74 # include "images/noseguy/nose-l1.xbm"
75 # include "images/noseguy/nose-l2.xbm"
76 # include "images/noseguy/nose-r1.xbm"
77 # include "images/noseguy/nose-r2.xbm"
83 static Pixmap *images[] = {
84 &left1, &left2, &right1, &right2,
85 &left_front, &right_front, &front, &down
88 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
90 static char **bits[] = {
91 nose_l1_xpm, nose_l2_xpm, nose_r1_xpm, nose_r2_xpm,
92 nose_f2_xpm, nose_f3_xpm, nose_f1_xpm, nose_f4_xpm
96 for (i = 0; i < sizeof (images) / sizeof(*images); i++)
98 Pixmap pixmap = xpm_data_to_pixmap (dpy, window, bits[i],
102 fprintf (stderr, "%s: Can't load nose images\n", progname);
108 static unsigned char *bits[] = {
109 nose_l1_bits, nose_l2_bits, nose_r1_bits, nose_r2_bits,
110 nose_f2_bits, nose_f3_bits, nose_f1_bits, nose_f4_bits
113 for (i = 0; i < sizeof (images) / sizeof(*images); i++)
115 XCreatePixmapFromBitmapData(dpy, window,
116 (char *) bits[i], 64, 64, 1, 0, 1)))
118 fprintf (stderr, "%s: Can't load nose images\n", progname);
140 register int tries = 0;
142 if ((random() & 1) && think())
144 talk(0); /* sets timeout to itself */
147 if (!(random() % 3) && (interval = look()))
152 interval = 20 + random() % 100;
156 length = Width / 100 + random() % 90, tries = 8;
159 switch (random() % 8)
162 if (x - X_INCR * length >= 5)
166 if (x + X_INCR * length <= Width - 70)
170 if (y - (Y_INCR * length) >= 5)
171 dir = UP, interval = 40;
174 if (y + Y_INCR * length <= Height - 70)
175 dir = DOWN, interval = 20;
178 if (x - X_INCR * length >= 5 && y - (Y_INCR * length) >= 5)
182 if (x + X_INCR * length <= Width - 70 &&
183 y - Y_INCR * length >= 5)
187 if (x - X_INCR * length >= 5 &&
188 y + Y_INCR * length <= Height - 70)
192 if (x + X_INCR * length <= Width - 70 &&
193 y + Y_INCR * length <= Height - 70)
194 dir = (RIGHT | DOWN);
208 # define COPY(dpy,frame,window,gc,x,y,w,h,x2,y2) \
209 XCopyArea (dpy,frame,window,gc,x,y,w,h,x2,y2)
211 # define COPY(dpy,frame,window,gc,x,y,w,h,x2,y2) \
212 XCopyPlane(dpy,frame,window,gc,x,y,w,h,x2,y2,1L)
218 register int incr = 0;
223 if (dir & (LEFT | RIGHT))
224 { /* left/right movement (mabye up/down too) */
225 up = -up; /* bouncing effect (even if hit a wall) */
229 frame = (up < 0) ? left1 : left2;
234 frame = (up < 0) ? right1 : right2;
236 if ((lastdir == FRONT || lastdir == DOWN) && dir & UP)
240 * workaround silly bug that leaves screen dust when guy is
241 * facing forward or down and moves up-left/right.
243 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y);
246 /* note that maybe neither UP nor DOWN is set! */
247 if (dir & UP && y > Y_INCR)
249 else if (dir & DOWN && y < Height - 64)
252 /* Explicit up/down movement only (no left/right) */
254 COPY(dpy, front, window, fg_gc, 0, 0, 64, 64, x, y -= Y_INCR);
255 else if (dir == DOWN)
256 COPY(dpy, down, window, fg_gc, 0, 0, 64, 64, x, y += Y_INCR);
257 else if (dir == FRONT && frame != front)
263 else if (lastdir & RIGHT)
267 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y);
272 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, --x, y + up);
275 else if (dir & RIGHT)
278 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, ++x, y + up);
291 if (getwordsfrom == FROM_PROGRAM)
301 talk(int force_erase)
322 /* clear what we've written */
323 if (talking || force_erase)
327 XFillRectangle(dpy, window, bg_gc, s_rect.x - 5, s_rect.y - 5,
328 s_rect.width + 10, s_rect.height + 10);
334 /* might as well check the window for size changes now... */
335 XWindowAttributes xgwa;
336 XGetWindowAttributes (dpy, window, &xgwa);
337 Width = xgwa.width + 2;
338 Height = xgwa.height + 2;
344 p = strcpy(buf, words);
346 if (!(p2 = strchr(p, '\n')) || !p2[1])
348 total = strlen (words);
349 strcpy (args[0], words);
350 width = XTextWidth(font, words, total);
354 /* p2 now points to the first '\n' */
355 for (height = 0; p; height++)
359 if ((w = XTextWidth(font, p, p2 - p)) > width)
361 total += p2 - p; /* total chars; count to determine reading
363 (void) strcpy(args[height], p);
364 if (height == MAXLINES - 1)
366 puts("Message too long!");
370 if (!(p2 = strchr(p, '\n')))
376 * Figure out the height and width in pixels (height, width) extend the
377 * new box by 15 pixels on the sides (30 total) top and bottom.
379 s_rect.width = width + 30;
380 s_rect.height = height * font_height(font) + 30;
381 if (x - s_rect.width - 10 < 5)
383 else if ((s_rect.x = x + 32 - (s_rect.width + 15) / 2)
384 + s_rect.width + 15 > Width - 5)
385 s_rect.x = Width - 15 - s_rect.width;
386 if (y - s_rect.height - 10 < 5)
387 s_rect.y = y + 64 + 5;
389 s_rect.y = y - 5 - s_rect.height;
391 XFillRectangle(dpy, window, text_bg_gc,
392 s_rect.x, s_rect.y, s_rect.width, s_rect.height);
394 /* make a box that's 5 pixels thick. Then add a thin box inside it */
395 XSetLineAttributes(dpy, text_fg_gc, 5, 0, 0, 0);
396 XDrawRectangle(dpy, window, text_fg_gc,
397 s_rect.x, s_rect.y, s_rect.width - 1, s_rect.height - 1);
398 XSetLineAttributes(dpy, text_fg_gc, 0, 0, 0, 0);
399 XDrawRectangle(dpy, window, text_fg_gc,
400 s_rect.x + 7, s_rect.y + 7, s_rect.width - 15, s_rect.height - 15);
403 Y = 15 + font_height(font);
405 /* now print each string in reverse order (start at bottom of box) */
406 for (Z = 0; Z < height; Z++)
408 XDrawString(dpy, window, text_fg_gc, s_rect.x + X, s_rect.y + Y,
409 args[Z], strlen(args[Z]));
410 Y += font_height(font);
412 interval = (total / 15) * 1000;
413 if (interval < 2000) interval = 2000;
417 static void talk_1 (void)
428 COPY(dpy, (random() & 1) ? down : front, window, fg_gc,
436 COPY(dpy, (random() & 1) ? left_front : right_front,
437 window, fg_gc, 0, 0, 64, 64, x, y);
442 COPY(dpy, (random() & 1) ? left1 : right1, window, fg_gc,
451 char *mode = get_string_resource ("mode", "Mode");
453 program = get_string_resource ("program", "Program");
454 filename = get_string_resource ("filename", "Filename");
455 text = get_string_resource ("text", "Text");
457 if (program) /* get stderr on stdout, so it shows up on the window */
459 orig_program = program;
460 program = (char *) malloc (strlen (program) + 10);
461 strcpy (program, "( ");
462 strcat (program, orig_program);
463 strcat (program, " ) 2>&1");
466 if (!mode || !strcmp (mode, "program"))
467 getwordsfrom = FROM_PROGRAM;
468 else if (!strcmp (mode, "file"))
469 getwordsfrom = FROM_FILE;
470 else if (!strcmp (mode, "string"))
471 getwordsfrom = FROM_RESRC;
475 "%s: mode must be program, file, or string, not %s\n",
480 if (getwordsfrom == FROM_PROGRAM && !program)
482 fprintf (stderr, "%s: no program specified.\n", progname);
485 if (getwordsfrom == FROM_FILE && !filename)
487 fprintf (stderr, "%s: no file specified.\n", progname);
494 static int first_time = 1;
500 static char buf[BUFSIZ];
501 register char *p = buf;
505 switch (getwordsfrom)
509 if ((pp = popen(program, "r")))
511 while (fgets(p, sizeof(buf) - strlen(buf), pp))
513 if (strlen(buf) + 1 < sizeof(buf))
514 p = buf + strlen(buf);
520 sprintf (buf, "\"%s\" produced no output!", orig_program);
521 else if (!first_time &&
522 (strstr (buf, ": not found") ||
523 strstr (buf, ": Not found") ||
524 strstr (buf, ": command not found") ||
525 strstr (buf, ": Command not found")))
526 switch (random () % 20)
528 case 1: strcat (buf, "( Get with the program, bub. )\n");
531 "( I blow my nose at you, you silly person! ) \n"); break;
533 "\nThe resource you want to\nset is `noseguy.program'\n");
536 strcat(buf,"\nHelp!! Help!!\nAAAAAAGGGGHHH!! \n\n"); break;
537 case 5: strcpy (buf, "You have new mail.\n"); break;
539 strcat(buf,"( Hello? Are you paying attention? )\n");break;
541 strcat (buf, "sh: what kind of fool do you take me for? \n");
555 if ((pp = fopen(filename, "r")))
557 while (fgets(p, sizeof(buf) - strlen(buf), pp))
559 if (strlen(buf) + 1 < sizeof(buf))
560 p = buf + strlen(buf);
566 sprintf (buf, "file \"%s\" is empty!", filename);
571 sprintf (buf, "couldn't read file \"%s\"!", filename);
583 if (!p || *p == '\0')
590 char *progclass = "Noseguy";
592 char *defaults [] = {
593 ".background: black",
594 ".foreground: gray80",
600 "*program: " FORTUNE_PROGRAM,
601 "noseguy.font: -*-new century schoolbook-*-r-*-*-*-180-*-*-*-*-*-*",
605 XrmOptionDescRec options [] = {
606 { "-mode", ".mode", XrmoptionSepArg, 0 },
607 { "-program", ".program", XrmoptionSepArg, 0 },
608 { "-text", ".text", XrmoptionSepArg, 0 },
609 { "-filename", ".filename", XrmoptionSepArg, 0 },
610 { "-font", ".font", XrmoptionSepArg, 0 },
611 { "-text-foreground", ".textForeground", XrmoptionSepArg, 0 },
612 { "-text-background", ".textBackground", XrmoptionSepArg, 0 },
618 noseguy_init (Display *d, Window w)
620 unsigned long fg, bg, text_fg, text_bg;
621 XWindowAttributes xgwa;
623 char *fontname = get_string_resource ("font", "Font");
629 XGetWindowAttributes (dpy, window, &xgwa);
630 Width = xgwa.width + 2;
631 Height = xgwa.height + 2;
632 cmap = xgwa.colormap;
637 if (!fontname || !(font = XLoadQueryFont(dpy, fontname)))
639 list = XListFonts(dpy, FONT_NAME, 32767, &foo);
640 for (i = 0; i < foo; i++)
641 if ((font = XLoadQueryFont(dpy, list[i])))
645 fprintf (stderr, "%s: Can't find a large font.", progname);
648 XFreeFontNames(list);
651 fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
652 bg = get_pixel_resource ("background", "Background", dpy, cmap);
653 text_fg = get_pixel_resource ("textForeground", "Foreground", dpy, cmap);
654 text_bg = get_pixel_resource ("textBackground", "Background", dpy, cmap);
655 /* notice when unspecified */
656 if (! get_string_resource ("textForeground", "Foreground"))
658 if (! get_string_resource ("textBackground", "Background"))
661 gcvalues.font = font->fid;
662 gcvalues.graphics_exposures = False;
663 gcvalues.foreground = fg;
664 gcvalues.background = bg;
665 fg_gc = XCreateGC (dpy, window,
666 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
668 gcvalues.foreground = bg;
669 gcvalues.background = fg;
670 bg_gc = XCreateGC (dpy, window,
671 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
673 gcvalues.foreground = text_fg;
674 gcvalues.background = text_bg;
675 text_fg_gc = XCreateGC (dpy, window,
676 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
678 gcvalues.foreground = text_bg;
679 gcvalues.background = text_fg;
680 text_bg_gc = XCreateGC (dpy, window,
681 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
689 screenhack (Display *d, Window w)
697 screenhack_handle_events (dpy);
698 usleep (interval * 1000);