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 Dan Heller <argv@sun.com>.
17 #include "screenhack.h"
20 extern FILE *popen (const char *, const char *);
21 extern int pclose (FILE *);
23 #define font_height(font) (font->ascent + font->descent)
24 #define FONT_NAME "-*-times-*-*-*-*-18-*-*-*-*-*-*-*"
28 static int Width, Height;
29 static GC fg_gc, bg_gc, text_fg_gc, text_bg_gc;
31 static char *get_words (void);
33 static XFontStruct *font;
34 static char *def_words = "I'm out running around.";
35 static void walk (int dir);
36 static void talk (int erase);
37 static void talk_1 (void);
38 static int think (void);
39 static unsigned long interval;
40 static unsigned long look (void);
41 static Pixmap left1, left2, right1, right2;
42 static Pixmap left_front, right_front, front, down;
44 static char *program, *orig_program, *filename, *text;
47 #define FROM_PROGRAM 2
50 static int getwordsfrom;
54 static int state; /* indicates states: walking or getting passwd */
56 static void (*next_fn) (void);
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
89 static char **bits[] = {
90 nose_l1_xpm, nose_l2_xpm, nose_r1_xpm, nose_r2_xpm,
91 nose_f2_xpm, nose_f3_xpm, nose_f1_xpm, nose_f4_xpm
93 for (i = 0; i < sizeof (images) / sizeof(*images); i++)
95 XWindowAttributes xgwa;
96 XpmAttributes xpmattrs;
99 xpmattrs.valuemask = 0;
101 XGetWindowAttributes (dpy, window, &xgwa);
104 xpmattrs.valuemask |= XpmCloseness;
105 xpmattrs.closeness = 40000;
108 xpmattrs.valuemask |= XpmVisual;
109 xpmattrs.visual = xgwa.visual;
112 xpmattrs.valuemask |= XpmDepth;
113 xpmattrs.depth = xgwa.depth;
116 xpmattrs.valuemask |= XpmColormap;
117 xpmattrs.colormap = xgwa.colormap;
120 result = XpmCreatePixmapFromData(dpy, window, bits[i],
121 &pixmap, 0 /* mask */, &xpmattrs);
122 if (!pixmap || (result != XpmSuccess && result != XpmColorError))
124 fprintf (stderr, "%s: Can't load nose images\n", progname);
130 static unsigned char *bits[] = {
131 nose_l1_bits, nose_l2_bits, nose_r1_bits, nose_r2_bits,
132 nose_f2_bits, nose_f3_bits, nose_f1_bits, nose_f4_bits
135 for (i = 0; i < sizeof (images) / sizeof(*images); i++)
137 XCreatePixmapFromBitmapData(dpy, window,
138 (char *) bits[i], 64, 64, 1, 0, 1)))
140 fprintf (stderr, "%s: Can't load nose images\n", progname);
162 register int tries = 0;
164 if ((random() & 1) && think())
166 talk(0); /* sets timeout to itself */
169 if (!(random() % 3) && (interval = look()))
174 interval = 20 + random() % 100;
178 length = Width / 100 + random() % 90, tries = 8;
181 switch (random() % 8)
184 if (x - X_INCR * length >= 5)
188 if (x + X_INCR * length <= Width - 70)
192 if (y - (Y_INCR * length) >= 5)
193 dir = UP, interval = 40;
196 if (y + Y_INCR * length <= Height - 70)
197 dir = DOWN, interval = 20;
200 if (x - X_INCR * length >= 5 && y - (Y_INCR * length) >= 5)
204 if (x + X_INCR * length <= Width - 70 &&
205 y - Y_INCR * length >= 5)
209 if (x - X_INCR * length >= 5 &&
210 y + Y_INCR * length <= Height - 70)
214 if (x + X_INCR * length <= Width - 70 &&
215 y + Y_INCR * length <= Height - 70)
216 dir = (RIGHT | DOWN);
230 # define COPY(dpy,frame,window,gc,x,y,w,h,x2,y2) \
231 XCopyArea (dpy,frame,window,gc,x,y,w,h,x2,y2)
233 # define COPY(dpy,frame,window,gc,x,y,w,h,x2,y2) \
234 XCopyPlane(dpy,frame,window,gc,x,y,w,h,x2,y2,1L)
240 register int incr = 0;
245 if (dir & (LEFT | RIGHT))
246 { /* left/right movement (mabye up/down too) */
247 up = -up; /* bouncing effect (even if hit a wall) */
251 frame = (up < 0) ? left1 : left2;
256 frame = (up < 0) ? right1 : right2;
258 if ((lastdir == FRONT || lastdir == DOWN) && dir & UP)
262 * workaround silly bug that leaves screen dust when guy is
263 * facing forward or down and moves up-left/right.
265 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y);
268 /* note that maybe neither UP nor DOWN is set! */
269 if (dir & UP && y > Y_INCR)
271 else if (dir & DOWN && y < Height - 64)
274 /* Explicit up/down movement only (no left/right) */
276 COPY(dpy, front, window, fg_gc, 0, 0, 64, 64, x, y -= Y_INCR);
277 else if (dir == DOWN)
278 COPY(dpy, down, window, fg_gc, 0, 0, 64, 64, x, y += Y_INCR);
279 else if (dir == FRONT && frame != front)
285 else if (lastdir & RIGHT)
289 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y);
294 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, --x, y + up);
297 else if (dir & RIGHT)
300 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, ++x, y + up);
313 if (getwordsfrom == FROM_PROGRAM)
323 talk(int force_erase)
344 /* clear what we've written */
345 if (talking || force_erase)
349 XFillRectangle(dpy, window, bg_gc, s_rect.x - 5, s_rect.y - 5,
350 s_rect.width + 10, s_rect.height + 10);
356 /* might as well check the window for size changes now... */
357 XWindowAttributes xgwa;
358 XGetWindowAttributes (dpy, window, &xgwa);
359 Width = xgwa.width + 2;
360 Height = xgwa.height + 2;
366 p = strcpy(buf, words);
368 if (!(p2 = strchr(p, '\n')) || !p2[1])
370 total = strlen (words);
371 strcpy (args[0], words);
372 width = XTextWidth(font, words, total);
376 /* p2 now points to the first '\n' */
377 for (height = 0; p; height++)
381 if ((w = XTextWidth(font, p, p2 - p)) > width)
383 total += p2 - p; /* total chars; count to determine reading
385 (void) strcpy(args[height], p);
386 if (height == MAXLINES - 1)
388 puts("Message too long!");
392 if (!(p2 = strchr(p, '\n')))
398 * Figure out the height and width in pixels (height, width) extend the
399 * new box by 15 pixels on the sides (30 total) top and bottom.
401 s_rect.width = width + 30;
402 s_rect.height = height * font_height(font) + 30;
403 if (x - s_rect.width - 10 < 5)
405 else if ((s_rect.x = x + 32 - (s_rect.width + 15) / 2)
406 + s_rect.width + 15 > Width - 5)
407 s_rect.x = Width - 15 - s_rect.width;
408 if (y - s_rect.height - 10 < 5)
409 s_rect.y = y + 64 + 5;
411 s_rect.y = y - 5 - s_rect.height;
413 XFillRectangle(dpy, window, text_bg_gc,
414 s_rect.x, s_rect.y, s_rect.width, s_rect.height);
416 /* make a box that's 5 pixels thick. Then add a thin box inside it */
417 XSetLineAttributes(dpy, text_fg_gc, 5, 0, 0, 0);
418 XDrawRectangle(dpy, window, text_fg_gc,
419 s_rect.x, s_rect.y, s_rect.width - 1, s_rect.height - 1);
420 XSetLineAttributes(dpy, text_fg_gc, 0, 0, 0, 0);
421 XDrawRectangle(dpy, window, text_fg_gc,
422 s_rect.x + 7, s_rect.y + 7, s_rect.width - 15, s_rect.height - 15);
425 Y = 15 + font_height(font);
427 /* now print each string in reverse order (start at bottom of box) */
428 for (Z = 0; Z < height; Z++)
430 XDrawString(dpy, window, text_fg_gc, s_rect.x + X, s_rect.y + Y,
431 args[Z], strlen(args[Z]));
432 Y += font_height(font);
434 interval = (total / 15) * 1000;
435 if (interval < 2000) interval = 2000;
439 static void talk_1 (void)
450 COPY(dpy, (random() & 1) ? down : front, window, fg_gc,
458 COPY(dpy, (random() & 1) ? left_front : right_front,
459 window, fg_gc, 0, 0, 64, 64, x, y);
464 COPY(dpy, (random() & 1) ? left1 : right1, window, fg_gc,
473 char *mode = get_string_resource ("mode", "Mode");
475 program = get_string_resource ("program", "Program");
476 filename = get_string_resource ("filename", "Filename");
477 text = get_string_resource ("text", "Text");
479 if (program) /* get stderr on stdout, so it shows up on the window */
481 orig_program = program;
482 program = (char *) malloc (strlen (program) + 10);
483 strcpy (program, "( ");
484 strcat (program, orig_program);
485 strcat (program, " ) 2>&1");
488 if (!mode || !strcmp (mode, "program"))
489 getwordsfrom = FROM_PROGRAM;
490 else if (!strcmp (mode, "file"))
491 getwordsfrom = FROM_FILE;
492 else if (!strcmp (mode, "string"))
493 getwordsfrom = FROM_RESRC;
497 "%s: mode must be program, file, or string, not %s\n",
502 if (getwordsfrom == FROM_PROGRAM && !program)
504 fprintf (stderr, "%s: no program specified.\n", progname);
507 if (getwordsfrom == FROM_FILE && !filename)
509 fprintf (stderr, "%s: no file specified.\n", progname);
516 static int first_time = 1;
522 static char buf[BUFSIZ];
523 register char *p = buf;
527 switch (getwordsfrom)
531 if ((pp = popen(program, "r")))
533 while (fgets(p, sizeof(buf) - strlen(buf), pp))
535 if (strlen(buf) + 1 < sizeof(buf))
536 p = buf + strlen(buf);
542 sprintf (buf, "\"%s\" produced no output!", orig_program);
543 else if (!first_time &&
544 (strstr (buf, ": not found") ||
545 strstr (buf, ": Not found")))
546 switch (random () % 20)
548 case 1: strcat (buf, "( Get with the program, bub. )\n");
551 "( I blow my nose at you, you silly person! ) \n"); break;
553 "\nThe resource you want to\nset is `noseguy.program'\n");
556 strcat(buf,"\nHelp!! Help!!\nAAAAAAGGGGHHH!! \n\n"); break;
557 case 5: strcpy (buf, "You have new mail.\n"); break;
559 strcat(buf,"( Hello? Are you paying attention? )\n");break;
561 strcat (buf, "sh: what kind of fool do you take me for? \n");
575 if ((pp = fopen(filename, "r")))
577 while (fgets(p, sizeof(buf) - strlen(buf), pp))
579 if (strlen(buf) + 1 < sizeof(buf))
580 p = buf + strlen(buf);
586 sprintf (buf, "file \"%s\" is empty!", filename);
591 sprintf (buf, "couldn't read file \"%s\"!", filename);
603 if (!p || *p == '\0')
610 char *progclass = "Noseguy";
612 char *defaults [] = {
613 ".background: black",
614 ".foreground: gray80",
620 "*program: " ZIPPY_PROGRAM,
621 "noseguy.font: -*-new century schoolbook-*-r-*-*-*-180-*-*-*-*-*-*",
625 XrmOptionDescRec options [] = {
626 { "-mode", ".mode", XrmoptionSepArg, 0 },
627 { "-program", ".program", XrmoptionSepArg, 0 },
628 { "-text", ".text", XrmoptionSepArg, 0 },
629 { "-filename", ".filename", XrmoptionSepArg, 0 },
630 { "-font", ".font", XrmoptionSepArg, 0 },
631 { "-text-foreground", ".textForeground", XrmoptionSepArg, 0 },
632 { "-text-background", ".textBackground", XrmoptionSepArg, 0 },
638 noseguy_init (Display *d, Window w)
640 unsigned long fg, bg, text_fg, text_bg;
641 XWindowAttributes xgwa;
643 char *fontname = get_string_resource ("font", "Font");
649 XGetWindowAttributes (dpy, window, &xgwa);
650 Width = xgwa.width + 2;
651 Height = xgwa.height + 2;
652 cmap = xgwa.colormap;
657 if (!fontname || !(font = XLoadQueryFont(dpy, fontname)))
659 list = XListFonts(dpy, FONT_NAME, 32767, &foo);
660 for (i = 0; i < foo; i++)
661 if ((font = XLoadQueryFont(dpy, list[i])))
665 fprintf (stderr, "%s: Can't find a large font.", progname);
668 XFreeFontNames(list);
671 fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
672 bg = get_pixel_resource ("background", "Background", dpy, cmap);
673 text_fg = get_pixel_resource ("textForeground", "Foreground", dpy, cmap);
674 text_bg = get_pixel_resource ("textBackground", "Background", dpy, cmap);
675 /* notice when unspecified */
676 if (! get_string_resource ("textForeground", "Foreground"))
678 if (! get_string_resource ("textBackground", "Background"))
681 gcvalues.font = font->fid;
682 gcvalues.graphics_exposures = False;
683 gcvalues.foreground = fg;
684 gcvalues.background = bg;
685 fg_gc = XCreateGC (dpy, window,
686 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
688 gcvalues.foreground = bg;
689 gcvalues.background = fg;
690 bg_gc = XCreateGC (dpy, window,
691 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
693 gcvalues.foreground = text_fg;
694 gcvalues.background = text_bg;
695 text_fg_gc = XCreateGC (dpy, window,
696 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
698 gcvalues.foreground = text_bg;
699 gcvalues.background = text_fg;
700 text_bg_gc = XCreateGC (dpy, window,
701 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
709 screenhack (Display *d, Window w)
717 screenhack_handle_events (dpy);
718 usleep (interval * 1000);