http://www.jwz.org/xscreensaver/xscreensaver-5.12.tar.gz
[xscreensaver] / hacks / xjack.c
1 /* xscreensaver, Copyright (c) 1997-2008 Jamie Zawinski <jwz@jwz.org>
2  *
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 
9  * implied warranty.
10  *
11  * Wendy, let me explain something to you.  Whenever you come in here and
12  * interrupt me, you're BREAKING my CONCENTRATION.  You're DISTRACTING me!
13  * And it will then take me time to get back to where I was. You understand?
14  * Now, we're going to make a new rule.  When you come in here and you hear
15  * me typing, or whether you DON'T hear me typing, or whatever the FUCK you
16  * hear me doing; when I'm in here, it means that I am working, THAT means
17  * don't come in!  Now, do you think you can handle that?
18  */
19
20 #include <ctype.h>
21 #include "screenhack.h"
22
23 static const char *source = "All work and no play makes Jack a dull boy.  ";
24 /* If you're here because you're thinking about making the above string be
25    customizable, then you don't get the joke.  You loser. */
26
27 struct state {
28   Display *dpy;
29   Window window;
30   XWindowAttributes xgwa;
31   XFontStruct *font;
32   GC gc;
33
34   const char *s;
35   int columns, rows;            /* characters */
36   int left, right;              /* characters */
37   int char_width, line_height;  /* pixels */
38   int x, y;                     /* characters */
39   int mode;
40   int hspace;                   /* pixels */
41   int vspace;                   /* pixels */
42   Bool break_para;
43   Bool caps;
44   int sentences;
45   int paras;
46   int scrolling;
47   int subscrolling;
48   int pining;
49
50   int delay;
51 };
52
53
54 static void
55 xjack_reshape (Display *dpy, Window window, void *closure, 
56                  unsigned int w, unsigned int h)
57 {
58   struct state *st = (struct state *) closure;
59   XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
60   st->columns = (st->xgwa.width  - st->hspace - st->hspace) / st->char_width;
61   st->rows    = (st->xgwa.height - st->vspace - st->vspace) / st->line_height;
62   st->rows--;
63   st->columns--;
64
65   /* If the window is stupidly small, just truncate. */
66   if (st->rows < 4)     st->rows = 4;
67   if (st->columns < 12) st->columns = 12;
68
69   if (st->y > st->rows)    st->y = st->rows-1;
70   if (st->x > st->columns) st->x = st->columns-2;
71
72   if (st->right > st->columns) st->right = st->columns;
73   if (st->left > st->columns-20) st->left = st->columns-20;
74   if (st->left < 0) st->left = 0;
75 }
76
77
78 static void *
79 xjack_init (Display *dpy, Window window)
80 {
81   struct state *st = (struct state *) calloc (1, sizeof(*st));
82   XGCValues gcv;
83   char *fontname;
84
85   st->dpy = dpy;
86   st->window = window;
87   st->s = source;
88   st->delay = get_integer_resource (st->dpy, "delay", "Integer");
89   fontname = get_string_resource (st->dpy, "font", "Font");
90   st->font = XLoadQueryFont (st->dpy, fontname);
91
92   if (!st->font)
93     st->font = XLoadQueryFont (st->dpy, "-*-*-medium-r-*-*-*-240-*-*-m-*-*-*");
94   if (!st->font)
95     st->font = XLoadQueryFont (st->dpy,
96                                "-*-courier-medium-r-*-*-*-180-*-*-m-*-*-*");
97   if (!st->font)
98     st->font = XLoadQueryFont (st->dpy, "-*-*-*-r-*-*-*-240-*-*-m-*-*-*");
99   if (!st->font)
100     {
101       fprintf(stderr, "no big fixed-width font like \"%s\"\n", fontname);
102       exit(1);
103     }
104
105   XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
106
107   gcv.font = st->font->fid;
108   gcv.foreground = get_pixel_resource (st->dpy, st->xgwa.colormap,
109                                        "foreground", "Foreground");
110   gcv.background = get_pixel_resource (st->dpy, st->xgwa.colormap,
111                                        "background", "Background");
112   st->gc = XCreateGC (st->dpy, st->window,
113                       (GCFont | GCForeground | GCBackground), &gcv);
114
115   st->char_width = 
116     (st->font->per_char
117      ? st->font->per_char['n'-st->font->min_char_or_byte2].rbearing
118      : st->font->min_bounds.rbearing);
119   st->line_height = st->font->ascent + st->font->descent + 1;
120
121   xjack_reshape (dpy, window, st, st->xgwa.width, st->xgwa.height);
122
123   if (st->columns >= 21)
124     {
125       st->left = 0xFF & (random() % ((st->columns / 2)+1));
126       st->right = st->left + (0xFF & (random() % (st->columns - st->left - 10)
127                                       + 10));
128     }
129   st->x = 0;
130   st->y = 0;
131
132   if (st->xgwa.width > 200 && st->xgwa.height > 200)
133     st->hspace = st->vspace = 40;
134
135   return st;
136 }
137
138 static unsigned long
139 xjack_scroll (struct state *st)
140 {
141   st->break_para = 0;
142   if (st->subscrolling)
143     {
144       int inc = st->line_height / 7;
145       XCopyArea (st->dpy, st->window, st->window, st->gc,
146                  0, inc,
147                  st->xgwa.width, st->xgwa.height - inc,
148                  0, 0);
149
150       /* See? It's OK. He saw it on the television. */
151       XClearArea (st->dpy, st->window,
152                   0, st->xgwa.height - inc, st->xgwa.width, inc,
153                   False);
154
155       st->subscrolling -= inc;
156       if (st->subscrolling <= 0)
157         st->subscrolling = 0;
158       if (st->subscrolling == 0)
159         {
160           if (st->scrolling > 0)
161             st->scrolling--;
162           st->y--;
163         }
164       return st->delay / 1000;
165     }
166   else if (st->scrolling)
167     st->subscrolling = st->line_height;
168
169   if (st->y < 0)
170     st->y = 0;
171   else if (st->y >= st->rows-1)
172     st->y = st->rows-1;
173
174   return st->delay;
175 }
176
177 static unsigned long
178 xjack_pine (struct state *st)
179 {
180   /* See also http://catalog.com/hopkins/unix-haters/login.html */
181   const char *n1 = "NFS server overlook not responding, still trying...";
182   const char *n2 = "NFS server overlook ok.";
183   int prev = st->pining;
184
185   if (!st->pining)
186     st->pining = 1 + (random() % 3);
187
188   if (prev)
189     while (*n2)
190       {
191         XDrawString (st->dpy, st->window, st->gc,
192                      (st->x * st->char_width) + st->hspace,
193                      ((st->y * st->line_height) + st->vspace
194                       + st->font->ascent),
195                      (char *) n2, 1);
196         st->x++;
197         if (st->x >= st->columns) st->x = 0, st->y++;
198         n2++;
199       }
200   st->y++;
201   st->x = 0;
202   st->pining--;
203
204   if (st->pining)
205     while (*n1)
206       {
207         XDrawString (st->dpy, st->window, st->gc,
208                      (st->x * st->char_width) + st->hspace,
209                      ((st->y * st->line_height) + st->vspace
210                       + st->font->ascent),
211                      (char *) n1, 1);
212         st->x++;
213         if (st->x >= st->columns) st->x = 0, st->y++;
214         n1++;
215       }
216
217   return 5000000;
218 }
219
220
221 static unsigned long
222 xjack_draw (Display *dpy, Window window, void *closure)
223 {
224   struct state *st = (struct state *) closure;
225   int this_delay = st->delay;
226   int word_length = 0;
227   const char *s2;
228
229   if (st->scrolling)
230     return xjack_scroll (st);
231   if (st->pining)
232     return xjack_pine (st);
233
234   for (s2 = st->s; *s2 && *s2 != ' '; s2++)
235     word_length++;
236
237   if (st->break_para ||
238       (*st->s != ' ' &&
239        (st->x + word_length) >= st->right))
240     {
241       st->x = st->left;
242       st->y++;
243
244       if (st->break_para)
245         st->y++;
246
247       if (st->mode == 1 || st->mode == 2)
248         {
249           /* 1 = left margin goes southwest; 2 = southeast */
250           st->left += (st->mode == 1 ? 1 : -1);
251           if (st->left >= st->right - 10)
252             {
253               if ((st->right < (st->columns - 10)) && (random() & 1))
254                 st->right += (0xFF & (random() % (st->columns - st->right)));
255               else
256                 st->mode = 2;
257             }
258           else if (st->left <= 0)
259             {
260               st->left = 0;
261               st->mode = 1;
262             }
263         }
264       else if (st->mode == 3 || st->mode == 4)
265         {
266           /* 3 = right margin goes southeast; 4 = southwest */
267           st->right += (st->mode == 3 ? 1 : -1);
268           if (st->right >= st->columns)
269             {
270               st->right = st->columns;
271               st->mode = 4;
272             }
273           else if (st->right <= st->left + 10)
274             st->mode = 3;
275         }
276
277       if (st->y >= st->rows-1)  /* bottom of page */
278         {
279           /* scroll by 1-5 lines */
280           st->scrolling = (random() % 5 ? 0 : (0xFF & (random() % 5))) + 1;
281           if (st->break_para)
282             st->scrolling++;
283
284           /* but sometimes scroll by a whole page */
285           if (0 == (random() % 100))
286             st->scrolling += st->rows;
287
288           return xjack_scroll (st);
289         }
290     }
291
292   if (*st->s != ' ')
293     {
294       char c = *st->s;
295       int xshift = 0, yshift = 0;
296       if (0 == random() % 50)
297         {
298           xshift = random() % ((st->char_width / 3) + 1);      /* mis-strike */
299           yshift = random() % ((st->line_height / 6) + 1);
300           if (0 == (random() % 3))
301             yshift *= 2;
302           if (random() & 1)
303             xshift = -xshift;
304           if (random() & 1)
305             yshift = -yshift;
306         }
307
308       if (0 == (random() % 250))        /* introduce adjascent-key typo */
309         {
310           static const char * const typo[] = {
311             "asqw", "ASQW", "bgvhn", "cxdfv", "dserfcx", "ewsdrf",
312             "Jhuikmn", "kjiol,m", "lkop;.,", "mnjk,", "nbhjm", "oiklp09",
313             "pol;(-0", "redft54", "sawedxz", "uyhji87", "wqase32",
314             "yuhgt67", ".,l;/", 0 };
315           int i = 0;
316           while (typo[i] && typo[i][0] != c)
317             i++;
318           if (typo[i])
319             c = typo[i][0xFF & ((random() % strlen(typo[i]+1)) + 1)];
320         }
321
322       /* caps typo */
323       if (c >= 'a' && c <= 'z' && (st->caps || 0 == (random() % 350)))
324         {
325           c -= ('a'-'A');
326           if (c == 'O' && random() & 1)
327             c = '0';
328         }
329
330     OVERSTRIKE:
331       XDrawString (st->dpy, st->window, st->gc,
332                    (st->x * st->char_width) + st->hspace + xshift,
333                    ((st->y * st->line_height) + st->vspace + st->font->ascent
334                     + yshift),
335                    &c, 1);
336       if (xshift == 0 && yshift == 0 && (0 == (random() & 3000)))
337         {
338           if (random() & 1)
339             xshift--;
340           else
341             yshift--;
342           goto OVERSTRIKE;
343         }
344
345       if ((tolower(c) != tolower(*st->s))
346           ? (0 == (random() % 10))              /* backup to correct */
347           : (0 == (random() % 400)))    /* fail to advance */
348         {
349           st->x--;
350           st->s--;
351           if (st->delay)
352             st->delay += (0xFFFF & (st->delay + 
353                                     (random() % (st->delay * 10))));
354         }
355     }
356
357   st->x++;
358   st->s++;
359
360   if (0 == random() % 200)
361     {
362       if (random() & 1 && st->s != source)
363         st->s--;        /* duplicate character */
364       else if (*st->s)
365         st->s++;        /* skip character */
366     }
367
368   if (*st->s == 0)
369     {
370       st->sentences++;
371       st->caps = (0 == random() % 40);  /* capitalize sentence */
372
373       if (0 == (random() % 10) ||       /* randomly break paragraph */
374           (st->mode == 0 &&
375            ((0 == (random() % 10)) || st->sentences > 20)))
376         {
377           st->break_para = True;
378           st->sentences = 0;
379           st->paras++;
380
381           if (random() & 1)             /* mode=0 50% of the time */
382             st->mode = 0;
383           else
384             st->mode = (0xFF & (random() % 5));
385
386           if (0 == (random() % 2))      /* re-pick margins */
387             {
388               st->left = 0xFF & (random() % (st->columns / 3));
389               st->right = (st->columns -
390                            (0xFF & (random() % (st->columns / 3))));
391
392               if (0 == random() % 3)    /* sometimes be wide */
393                 st->right = st->left + ((st->right - st->left) / 2);
394             }
395
396           if (st->right - st->left <= 10)       /* introduce sanity */
397             {
398               st->left = 0;
399               st->right = st->columns;
400             }
401
402           if (st->right - st->left > 50)        /* if wide, shrink and move */
403             {
404               st->left += (0xFF & (random() % ((st->columns - 50) + 1)));
405               st->right = st->left + (0xFF & ((random() % 40) + 10));
406             }
407
408           /* oh, gag. */
409           if (st->mode == 0 &&
410               st->right - st->left < 25 &&
411               st->columns > 40)
412             {
413               st->right += 20;
414               if (st->right > st->columns)
415                 st->left -= (st->right - st->columns);
416             }
417         }
418       st->s = source;
419     }
420
421   if (st->delay)
422     {
423       if (0 == random() % 3)
424         this_delay += (0xFFFFFF & ((random() % (st->delay * 5)) + 1));
425
426       if (st->break_para)
427         this_delay += (0xFFFFFF & ((random() % (st->delay * 15)) + 1));
428     }
429
430   if (st->paras > 5 &&
431       (0 == (random() % 1000)) &&
432       st->y < st->rows-2)
433     return xjack_pine (st);
434
435   return this_delay;
436 }
437
438 static Bool
439 xjack_event (Display *dpy, Window window, void *closure, XEvent *event)
440 {
441   struct state *st = (struct state *) closure;
442   if (event->xany.type == ButtonPress)
443     {
444       st->scrolling++;
445       return True;
446     }
447
448   return False;
449 }
450
451 static void
452 xjack_free (Display *dpy, Window window, void *closure)
453 {
454   struct state *st = (struct state *) closure;
455   free (st);
456 }
457
458
459 static const char *xjack_defaults [] = {
460   ".background:         #FFF0B4",
461   ".foreground:         #000000",
462   "*fpsSolid:           true",
463 #ifdef HAVE_COCOA
464   ".font:               American Typewriter 24",
465 #else
466   ".font:               -*-courier-medium-r-*-*-*-240-*-*-m-*-*-*",
467 #endif
468   "*delay:              50000",
469   0
470 };
471
472 static XrmOptionDescRec xjack_options [] = {
473   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
474   { "-font",            ".font",        XrmoptionSepArg, 0 },
475   { 0, 0, 0, 0 }
476 };
477
478 XSCREENSAVER_MODULE ("XJack", xjack)