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 #define BUFSIZ ((MAXLINES + 1) * 100)
305 talk(int force_erase)
326 /* clear what we've written */
327 if (talking || force_erase)
331 XFillRectangle(dpy, window, bg_gc, s_rect.x - 5, s_rect.y - 5,
332 s_rect.width + 10, s_rect.height + 10);
338 /* might as well check the window for size changes now... */
339 XWindowAttributes xgwa;
340 XGetWindowAttributes (dpy, window, &xgwa);
341 Width = xgwa.width + 2;
342 Height = xgwa.height + 2;
348 p = strcpy(buf, words);
350 if (!(p2 = strchr(p, '\n')) || !p2[1])
352 total = strlen (words);
353 strcpy (args[0], words);
354 width = XTextWidth(font, words, total);
358 /* p2 now points to the first '\n' */
359 for (height = 0; p; height++)
363 if ((w = XTextWidth(font, p, p2 - p)) > width)
365 total += p2 - p; /* total chars; count to determine reading
367 (void) strcpy(args[height], p);
368 if (height == MAXLINES - 1)
370 /* puts("Message too long!"); */
374 if (!(p2 = strchr(p, '\n')))
380 * Figure out the height and width in pixels (height, width) extend the
381 * new box by 15 pixels on the sides (30 total) top and bottom.
383 s_rect.width = width + 30;
384 s_rect.height = height * font_height(font) + 30;
385 if (x - s_rect.width - 10 < 5)
387 else if ((s_rect.x = x + 32 - (s_rect.width + 15) / 2)
388 + s_rect.width + 15 > Width - 5)
389 s_rect.x = Width - 15 - s_rect.width;
390 if (y - s_rect.height - 10 < 5)
391 s_rect.y = y + 64 + 5;
393 s_rect.y = y - 5 - s_rect.height;
395 XFillRectangle(dpy, window, text_bg_gc,
396 s_rect.x, s_rect.y, s_rect.width, s_rect.height);
398 /* make a box that's 5 pixels thick. Then add a thin box inside it */
399 XSetLineAttributes(dpy, text_fg_gc, 5, 0, 0, 0);
400 XDrawRectangle(dpy, window, text_fg_gc,
401 s_rect.x, s_rect.y, s_rect.width - 1, s_rect.height - 1);
402 XSetLineAttributes(dpy, text_fg_gc, 0, 0, 0, 0);
403 XDrawRectangle(dpy, window, text_fg_gc,
404 s_rect.x + 7, s_rect.y + 7, s_rect.width - 15, s_rect.height - 15);
407 Y = 15 + font_height(font);
409 /* now print each string in reverse order (start at bottom of box) */
410 for (Z = 0; Z < height; Z++)
412 XDrawString(dpy, window, text_fg_gc, s_rect.x + X, s_rect.y + Y,
413 args[Z], strlen(args[Z]));
414 Y += font_height(font);
416 interval = (total / 15) * 1000;
417 if (interval < 2000) interval = 2000;
421 static void talk_1 (void)
432 COPY(dpy, (random() & 1) ? down : front, window, fg_gc,
440 COPY(dpy, (random() & 1) ? left_front : right_front,
441 window, fg_gc, 0, 0, 64, 64, x, y);
446 COPY(dpy, (random() & 1) ? left1 : right1, window, fg_gc,
455 char *mode = get_string_resource ("mode", "Mode");
457 program = get_string_resource ("program", "Program");
458 filename = get_string_resource ("filename", "Filename");
459 text = get_string_resource ("text", "Text");
461 if (program) /* get stderr on stdout, so it shows up on the window */
463 orig_program = program;
464 program = (char *) malloc (strlen (program) + 10);
465 strcpy (program, "( ");
466 strcat (program, orig_program);
467 strcat (program, " ) 2>&1");
470 if (!mode || !strcmp (mode, "program"))
471 getwordsfrom = FROM_PROGRAM;
472 else if (!strcmp (mode, "file"))
473 getwordsfrom = FROM_FILE;
474 else if (!strcmp (mode, "string"))
475 getwordsfrom = FROM_RESRC;
479 "%s: mode must be program, file, or string, not %s\n",
484 if (getwordsfrom == FROM_PROGRAM && !program)
486 fprintf (stderr, "%s: no program specified.\n", progname);
489 if (getwordsfrom == FROM_FILE && !filename)
491 fprintf (stderr, "%s: no file specified.\n", progname);
498 static int first_time = 1;
504 static char buf[BUFSIZ];
505 register char *p = buf;
509 switch (getwordsfrom)
513 if ((pp = popen(program, "r")))
515 while (fgets(p, sizeof(buf) - strlen(buf), pp))
517 if (strlen(buf) + 1 < sizeof(buf))
518 p = buf + strlen(buf);
524 sprintf (buf, "\"%s\" produced no output!", orig_program);
525 else if (!first_time &&
526 (strstr (buf, ": not found") ||
527 strstr (buf, ": Not found") ||
528 strstr (buf, ": command not found") ||
529 strstr (buf, ": Command not found")))
530 switch (random () % 20)
532 case 1: strcat (buf, "( Get with the program, bub. )\n");
535 "( I blow my nose at you, you silly person! ) \n"); break;
537 "\nThe resource you want to\nset is `noseguy.program'\n");
540 strcat(buf,"\nHelp!! Help!!\nAAAAAAGGGGHHH!! \n\n"); break;
541 case 5: strcpy (buf, "You have new mail.\n"); break;
543 strcat(buf,"( Hello? Are you paying attention? )\n");break;
545 strcat (buf, "sh: what kind of fool do you take me for? \n");
559 if ((pp = fopen(filename, "r")))
561 while (fgets(p, sizeof(buf) - strlen(buf), pp))
563 if (strlen(buf) + 1 < sizeof(buf))
564 p = buf + strlen(buf);
570 sprintf (buf, "file \"%s\" is empty!", filename);
575 sprintf (buf, "couldn't read file \"%s\"!", filename);
587 if (!p || *p == '\0')
594 char *progclass = "Noseguy";
596 char *defaults [] = {
597 ".background: black",
598 ".foreground: gray80",
604 "*program: " FORTUNE_PROGRAM,
605 "noseguy.font: -*-new century schoolbook-*-r-*-*-*-180-*-*-*-*-*-*",
609 XrmOptionDescRec options [] = {
610 { "-mode", ".mode", XrmoptionSepArg, 0 },
611 { "-program", ".program", XrmoptionSepArg, 0 },
612 { "-text", ".text", XrmoptionSepArg, 0 },
613 { "-filename", ".filename", XrmoptionSepArg, 0 },
614 { "-font", ".font", XrmoptionSepArg, 0 },
615 { "-text-foreground", ".textForeground", XrmoptionSepArg, 0 },
616 { "-text-background", ".textBackground", XrmoptionSepArg, 0 },
622 noseguy_init (Display *d, Window w)
624 unsigned long fg, bg, text_fg, text_bg;
625 XWindowAttributes xgwa;
627 char *fontname = get_string_resource ("font", "Font");
633 XGetWindowAttributes (dpy, window, &xgwa);
634 Width = xgwa.width + 2;
635 Height = xgwa.height + 2;
636 cmap = xgwa.colormap;
641 if (!fontname || !(font = XLoadQueryFont(dpy, fontname)))
643 list = XListFonts(dpy, FONT_NAME, 32767, &foo);
644 for (i = 0; i < foo; i++)
645 if ((font = XLoadQueryFont(dpy, list[i])))
649 fprintf (stderr, "%s: Can't find a large font.", progname);
652 XFreeFontNames(list);
655 fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
656 bg = get_pixel_resource ("background", "Background", dpy, cmap);
657 text_fg = get_pixel_resource ("textForeground", "Foreground", dpy, cmap);
658 text_bg = get_pixel_resource ("textBackground", "Background", dpy, cmap);
659 /* notice when unspecified */
660 if (! get_string_resource ("textForeground", "Foreground"))
662 if (! get_string_resource ("textBackground", "Background"))
665 gcvalues.font = font->fid;
666 gcvalues.graphics_exposures = False;
667 gcvalues.foreground = fg;
668 gcvalues.background = bg;
669 fg_gc = XCreateGC (dpy, window,
670 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
672 gcvalues.foreground = bg;
673 gcvalues.background = fg;
674 bg_gc = XCreateGC (dpy, window,
675 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
677 gcvalues.foreground = text_fg;
678 gcvalues.background = text_bg;
679 text_fg_gc = XCreateGC (dpy, window,
680 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
682 gcvalues.foreground = text_bg;
683 gcvalues.background = text_fg;
684 text_bg_gc = XCreateGC (dpy, window,
685 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
693 screenhack (Display *d, Window w)
701 screenhack_handle_events (dpy);
702 usleep (interval * 1000);