1 /* xscreensaver, Copyright (c) 1992, 1996, 1997, 1998, 2005
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 void walk (int dir);
37 static void talk (int erase);
38 static void talk_1 (void);
39 static int think (void);
40 static unsigned long interval;
41 static unsigned long look (void);
42 static Pixmap left1, left2, right1, right2;
43 static Pixmap left_front, right_front, front, down;
45 static char *program, *orig_program, *filename, *text;
48 #define FROM_PROGRAM 2
54 static int state; /* indicates states: walking or getting passwd */
56 static void (*next_fn) (void);
58 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
59 # include "images/noseguy/nose-f1.xpm"
60 # include "images/noseguy/nose-f2.xpm"
61 # include "images/noseguy/nose-f3.xpm"
62 # include "images/noseguy/nose-f4.xpm"
63 # include "images/noseguy/nose-l1.xpm"
64 # include "images/noseguy/nose-l2.xpm"
65 # include "images/noseguy/nose-r1.xpm"
66 # include "images/noseguy/nose-r2.xpm"
68 # include "images/noseguy/nose-f1.xbm"
69 # include "images/noseguy/nose-f2.xbm"
70 # include "images/noseguy/nose-f3.xbm"
71 # include "images/noseguy/nose-f4.xbm"
72 # include "images/noseguy/nose-l1.xbm"
73 # include "images/noseguy/nose-l2.xbm"
74 # include "images/noseguy/nose-r1.xbm"
75 # include "images/noseguy/nose-r2.xbm"
81 static Pixmap *images[] = {
82 &left1, &left2, &right1, &right2,
83 &left_front, &right_front, &front, &down
86 #if defined(HAVE_GDK_PIXBUF) || defined(HAVE_XPM)
88 static char **bits[] = {
89 nose_l1_xpm, nose_l2_xpm, nose_r1_xpm, nose_r2_xpm,
90 nose_f2_xpm, nose_f3_xpm, nose_f1_xpm, nose_f4_xpm
94 for (i = 0; i < sizeof (images) / sizeof(*images); i++)
96 Pixmap pixmap = xpm_data_to_pixmap (dpy, window, bits[i],
100 fprintf (stderr, "%s: Can't load nose images\n", progname);
106 static unsigned char *bits[] = {
107 nose_l1_bits, nose_l2_bits, nose_r1_bits, nose_r2_bits,
108 nose_f2_bits, nose_f3_bits, nose_f1_bits, nose_f4_bits
111 for (i = 0; i < sizeof (images) / sizeof(*images); i++)
113 XCreatePixmapFromBitmapData(dpy, window,
114 (char *) bits[i], 64, 64, 1, 0, 1)))
116 fprintf (stderr, "%s: Can't load nose images\n", progname);
138 register int tries = 0;
140 if ((random() & 1) && think())
142 talk(0); /* sets timeout to itself */
145 if (!(random() % 3) && (interval = look()))
150 interval = 20 + random() % 100;
154 length = Width / 100 + random() % 90, tries = 8;
157 switch (random() % 8)
160 if (x - X_INCR * length >= 5)
164 if (x + X_INCR * length <= Width - 70)
168 if (y - (Y_INCR * length) >= 5)
169 dir = UP, interval = 40;
172 if (y + Y_INCR * length <= Height - 70)
173 dir = DOWN, interval = 20;
176 if (x - X_INCR * length >= 5 && y - (Y_INCR * length) >= 5)
180 if (x + X_INCR * length <= Width - 70 &&
181 y - Y_INCR * length >= 5)
185 if (x - X_INCR * length >= 5 &&
186 y + Y_INCR * length <= Height - 70)
190 if (x + X_INCR * length <= Width - 70 &&
191 y + Y_INCR * length <= Height - 70)
192 dir = (RIGHT | DOWN);
206 # define COPY(dpy,frame,window,gc,x,y,w,h,x2,y2) \
207 XCopyArea (dpy,frame,window,gc,x,y,w,h,x2,y2)
209 # define COPY(dpy,frame,window,gc,x,y,w,h,x2,y2) \
210 XCopyPlane(dpy,frame,window,gc,x,y,w,h,x2,y2,1L)
216 register int incr = 0;
221 if (dir & (LEFT | RIGHT))
222 { /* left/right movement (mabye up/down too) */
223 up = -up; /* bouncing effect (even if hit a wall) */
227 frame = (up < 0) ? left1 : left2;
232 frame = (up < 0) ? right1 : right2;
234 if ((lastdir == FRONT || lastdir == DOWN) && dir & UP)
238 * workaround silly bug that leaves screen dust when guy is
239 * facing forward or down and moves up-left/right.
241 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y);
244 /* note that maybe neither UP nor DOWN is set! */
245 if (dir & UP && y > Y_INCR)
247 else if (dir & DOWN && y < Height - 64)
250 /* Explicit up/down movement only (no left/right) */
252 COPY(dpy, front, window, fg_gc, 0, 0, 64, 64, x, y -= Y_INCR);
253 else if (dir == DOWN)
254 COPY(dpy, down, window, fg_gc, 0, 0, 64, 64, x, y += Y_INCR);
255 else if (dir == FRONT && frame != front)
261 else if (lastdir & RIGHT)
265 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, x, y);
270 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, --x, y + up);
273 else if (dir & RIGHT)
276 COPY(dpy, frame, window, fg_gc, 0, 0, 64, 64, ++x, y + up);
298 #define BUFSIZ ((MAXLINES + 1) * 100)
302 talk(int force_erase)
323 /* clear what we've written */
324 if (talking || force_erase)
328 XFillRectangle(dpy, window, bg_gc, s_rect.x - 5, s_rect.y - 5,
329 s_rect.width + 10, s_rect.height + 10);
335 /* might as well check the window for size changes now... */
336 XWindowAttributes xgwa;
337 XGetWindowAttributes (dpy, window, &xgwa);
338 Width = xgwa.width + 2;
339 Height = xgwa.height + 2;
345 p = strcpy(buf, words);
347 for (p2 = p; *p2; p2++)
348 if (*p2 == '\t') *p2 = ' ';
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 program = get_string_resource ("program", "Program");
456 filename = get_string_resource ("filename", "Filename");
457 text = get_string_resource ("text", "Text");
459 if (program) /* get stderr on stdout, so it shows up on the window */
461 orig_program = program;
462 program = (char *) malloc (strlen (program) + 10);
463 strcpy (program, "( ");
464 strcat (program, orig_program);
465 strcat (program, " ) 2>&1");
471 static int first_time = 1;
477 static char buf[BUFSIZ];
478 register char *p = buf;
482 if ((pp = popen(program, "r")))
484 while (fgets(p, sizeof(buf) - strlen(buf), pp))
486 if (strlen(buf) + 1 < sizeof(buf))
487 p = buf + strlen(buf);
493 sprintf (buf, "\"%s\" produced no output!", orig_program);
494 else if (!first_time &&
495 (strstr (buf, ": not found") ||
496 strstr (buf, ": Not found") ||
497 strstr (buf, ": command not found") ||
498 strstr (buf, ": Command not found")))
499 switch (random () % 20)
501 case 1: strcat (buf, "( Get with the program, bub. )\n");
504 "( I blow my nose at you, you silly person! ) \n"); break;
506 "\nThe resource you want to\nset is `noseguy.program'\n");
509 strcat(buf,"\nHelp!! Help!!\nAAAAAAGGGGHHH!! \n\n"); break;
510 case 5: strcpy (buf, "You have new mail.\n"); break;
512 strcat(buf,"( Hello? Are you paying attention? )\n");break;
514 strcat (buf, "sh: what kind of fool do you take me for? \n");
530 char *progclass = "Noseguy";
532 char *defaults [] = {
533 ".background: black",
534 ".foreground: gray80",
535 "*program: xscreensaver-text --cols 40 | head -15",
536 "noseguy.font: -*-new century schoolbook-*-r-*-*-*-180-*-*-*-*-*-*",
540 XrmOptionDescRec options [] = {
541 { "-mode", ".mode", XrmoptionSepArg, 0 },
542 { "-program", ".program", XrmoptionSepArg, 0 },
543 { "-text", ".text", XrmoptionSepArg, 0 },
544 { "-filename", ".filename", XrmoptionSepArg, 0 },
545 { "-font", ".font", XrmoptionSepArg, 0 },
546 { "-text-foreground", ".textForeground", XrmoptionSepArg, 0 },
547 { "-text-background", ".textBackground", XrmoptionSepArg, 0 },
553 noseguy_init (Display *d, Window w)
555 unsigned long fg, bg, text_fg, text_bg;
556 XWindowAttributes xgwa;
558 char *fontname = get_string_resource ("font", "Font");
564 XGetWindowAttributes (dpy, window, &xgwa);
565 Width = xgwa.width + 2;
566 Height = xgwa.height + 2;
567 cmap = xgwa.colormap;
572 if (!fontname || !(font = XLoadQueryFont(dpy, fontname)))
574 list = XListFonts(dpy, FONT_NAME, 32767, &foo);
575 for (i = 0; i < foo; i++)
576 if ((font = XLoadQueryFont(dpy, list[i])))
580 fprintf (stderr, "%s: Can't find a large font.", progname);
583 XFreeFontNames(list);
586 fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
587 bg = get_pixel_resource ("background", "Background", dpy, cmap);
588 text_fg = get_pixel_resource ("textForeground", "Foreground", dpy, cmap);
589 text_bg = get_pixel_resource ("textBackground", "Background", dpy, cmap);
590 /* notice when unspecified */
591 if (! get_string_resource ("textForeground", "Foreground"))
593 if (! get_string_resource ("textBackground", "Background"))
596 gcvalues.font = font->fid;
597 gcvalues.graphics_exposures = False;
598 gcvalues.foreground = fg;
599 gcvalues.background = bg;
600 fg_gc = XCreateGC (dpy, window,
601 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
603 gcvalues.foreground = bg;
604 gcvalues.background = fg;
605 bg_gc = XCreateGC (dpy, window,
606 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
608 gcvalues.foreground = text_fg;
609 gcvalues.background = text_bg;
610 text_fg_gc = XCreateGC (dpy, window,
611 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
613 gcvalues.foreground = text_bg;
614 gcvalues.background = text_fg;
615 text_bg_gc = XCreateGC (dpy, window,
616 GCForeground|GCBackground|GCGraphicsExposures|GCFont,
624 screenhack (Display *d, Window w)
632 screenhack_handle_events (dpy);
633 usleep (interval * 1000);