From http://www.jwz.org/xscreensaver/xscreensaver-5.39.tar.gz
[xscreensaver] / hacks / xjack.c
1 /* xscreensaver, Copyright (c) 1997-2013 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   XClearWindow (st->dpy, st->window);
77 }
78
79
80 static void *
81 xjack_init (Display *dpy, Window window)
82 {
83   struct state *st = (struct state *) calloc (1, sizeof(*st));
84   XGCValues gcv;
85   char *fontname;
86
87   st->dpy = dpy;
88   st->window = window;
89   st->s = source;
90   st->delay = get_integer_resource (st->dpy, "delay", "Integer");
91   fontname = get_string_resource (st->dpy, "font", "Font");
92
93   XGetWindowAttributes (st->dpy, st->window, &st->xgwa);
94
95   if (st->xgwa.width <= 480)
96     fontname = "-*-courier-medium-r-*-*-*-180-*-*-m-*-*-*";
97
98   st->font = load_font_retry (st->dpy, fontname);
99   if (!st->font) abort();
100
101   gcv.font = st->font->fid;
102   gcv.foreground = get_pixel_resource (st->dpy, st->xgwa.colormap,
103                                        "foreground", "Foreground");
104   gcv.background = get_pixel_resource (st->dpy, st->xgwa.colormap,
105                                        "background", "Background");
106   st->gc = XCreateGC (st->dpy, st->window,
107                       (GCFont | GCForeground | GCBackground), &gcv);
108
109   st->char_width = 
110     (st->font->per_char
111      ? st->font->per_char['n'-st->font->min_char_or_byte2].rbearing
112      : st->font->min_bounds.rbearing);
113   st->line_height = st->font->ascent + st->font->descent + 1;
114
115   xjack_reshape (dpy, window, st, st->xgwa.width, st->xgwa.height);
116
117   st->left = 0xFF & (random() % ((st->columns / 2)+1));
118   st->right = st->left + (0xFF & (random() % (st->columns - st->left)
119                                   + 10));
120   if (st->right < st->left + 10) st->right = st->left + 10;
121   if (st->right > st->columns)   st->right = st->columns;
122
123   st->x = st->left;
124   st->y = 0;
125
126   if (st->xgwa.width > 200 && st->xgwa.height > 200)
127     st->hspace = st->vspace = 40;
128
129   return st;
130 }
131
132 static unsigned long
133 xjack_scroll (struct state *st)
134 {
135   st->break_para = 0;
136   if (st->subscrolling)
137     {
138       int inc = st->line_height / 7;
139       XCopyArea (st->dpy, st->window, st->window, st->gc,
140                  0, inc,
141                  st->xgwa.width, st->xgwa.height - inc,
142                  0, 0);
143
144       /* See? It's OK. He saw it on the television. */
145       XClearArea (st->dpy, st->window,
146                   0, st->xgwa.height - inc, st->xgwa.width, inc,
147                   False);
148
149       st->subscrolling -= inc;
150       if (st->subscrolling <= 0)
151         st->subscrolling = 0;
152       if (st->subscrolling == 0)
153         {
154           if (st->scrolling > 0)
155             st->scrolling--;
156           st->y--;
157         }
158       return st->delay / 1000;
159     }
160   else if (st->scrolling)
161     st->subscrolling = st->line_height;
162
163   if (st->y < 0)
164     st->y = 0;
165   else if (st->y >= st->rows-1)
166     st->y = st->rows-1;
167
168   return st->delay;
169 }
170
171 static unsigned long
172 xjack_pine (struct state *st)
173 {
174   /* See also http://catalog.com/hopkins/unix-haters/login.html */
175   const char *n1 = "NFS server overlook not responding, still trying...";
176   const char *n2 = "NFS server overlook ok.";
177   int prev = st->pining;
178
179   if (!st->pining)
180     st->pining = 1 + (random() % 3);
181
182   if (prev)
183     while (*n2)
184       {
185         XDrawString (st->dpy, st->window, st->gc,
186                      (st->x * st->char_width) + st->hspace,
187                      ((st->y * st->line_height) + st->vspace
188                       + st->font->ascent),
189                      (char *) n2, 1);
190         st->x++;
191         if (st->x >= st->columns) st->x = 0, st->y++;
192         n2++;
193       }
194   st->y++;
195   st->x = 0;
196   st->pining--;
197
198   if (st->pining)
199     while (*n1)
200       {
201         XDrawString (st->dpy, st->window, st->gc,
202                      (st->x * st->char_width) + st->hspace,
203                      ((st->y * st->line_height) + st->vspace
204                       + st->font->ascent),
205                      (char *) n1, 1);
206         st->x++;
207         if (st->x >= st->columns) st->x = 0, st->y++;
208         n1++;
209       }
210
211   return 5000000;
212 }
213
214
215 static unsigned long
216 xjack_draw (Display *dpy, Window window, void *closure)
217 {
218   struct state *st = (struct state *) closure;
219   int this_delay = st->delay;
220   int word_length = 0;
221   const char *s2;
222
223   if (st->scrolling)
224     return xjack_scroll (st);
225   if (st->pining)
226     return xjack_pine (st);
227
228   for (s2 = st->s; *s2 && *s2 != ' '; s2++)
229     word_length++;
230
231   if (st->break_para ||
232       (*st->s != ' ' &&
233        (st->x + word_length) >= st->right))
234     {
235       st->x = st->left;
236       st->y++;
237
238       if (st->break_para)
239         st->y++;
240
241       st->break_para = 0;
242
243       if (st->mode == 1 || st->mode == 2)
244         {
245           /* 1 = left margin goes southwest; 2 = southeast */
246           st->left += (st->mode == 1 ? 1 : -1);
247           if (st->left >= st->right - 10)
248             {
249               if ((st->right < (st->columns - 10)) && (random() & 1))
250                 st->right += (0xFF & (random() % (st->columns - st->right)));
251               else
252                 st->mode = 2;
253             }
254           else if (st->left <= 0)
255             {
256               st->left = 0;
257               st->mode = 1;
258             }
259         }
260       else if (st->mode == 3 || st->mode == 4)
261         {
262           /* 3 = right margin goes southeast; 4 = southwest */
263           st->right += (st->mode == 3 ? 1 : -1);
264           if (st->right >= st->columns)
265             {
266               st->right = st->columns;
267               st->mode = 4;
268             }
269           else if (st->right <= st->left + 10)
270             st->mode = 3;
271         }
272
273       if (st->y >= st->rows-1)  /* bottom of page */
274         {
275 # if 0    /* Nah, this looks bad. */
276
277           /* scroll by 1-5 lines */
278           st->scrolling = (random() % 5 ? 0 : (0xFF & (random() % 5))) + 1;
279
280           if (st->break_para)
281             st->scrolling++;
282
283           /* but sometimes scroll by a whole page */
284           if (0 == (random() % 100))
285             st->scrolling += st->rows;
286 # else
287           st->scrolling = 1;
288 # endif
289
290           return xjack_scroll (st);
291         }
292     }
293
294   if (*st->s != ' ')
295     {
296       char c = *st->s;
297       int xshift = 0, yshift = 0;
298       if (0 == random() % 50)
299         {
300           xshift = random() % ((st->char_width / 3) + 1);      /* mis-strike */
301           yshift = random() % ((st->line_height / 6) + 1);
302           if (0 == (random() % 3))
303             yshift *= 2;
304           if (random() & 1)
305             xshift = -xshift;
306           if (random() & 1)
307             yshift = -yshift;
308         }
309
310       if (0 == (random() % 250))        /* introduce adjascent-key typo */
311         {
312           static const char * const typo[] = {
313             "asqw", "ASQW", "bgvhn", "cxdfv", "dserfcx", "ewsdrf",
314             "Jhuikmn", "kjiol,m", "lkop;.,", "mnjk,", "nbhjm", "oiklp09",
315             "pol;(-0", "redft54", "sawedxz", "uyhji87", "wqase32",
316             "yuhgt67", ".,l;/", 0 };
317           int i = 0;
318           while (typo[i] && typo[i][0] != c)
319             i++;
320           if (typo[i])
321             c = typo[i][0xFF & ((random() % strlen(typo[i]+1)) + 1)];
322         }
323
324       /* caps typo */
325       if (c >= 'a' && c <= 'z' && (st->caps || 0 == (random() % 350)))
326         {
327           c -= ('a'-'A');
328           if (c == 'O' && random() & 1)
329             c = '0';
330         }
331
332     OVERSTRIKE:
333       XDrawString (st->dpy, st->window, st->gc,
334                    (st->x * st->char_width) + st->hspace + xshift,
335                    ((st->y * st->line_height) + st->vspace + st->font->ascent
336                     + yshift),
337                    &c, 1);
338       if (xshift == 0 && yshift == 0 && (0 == (random() & 3000)))
339         {
340           if (random() & 1)
341             xshift--;
342           else
343             yshift--;
344           goto OVERSTRIKE;
345         }
346
347       if ((tolower(c) != tolower(*st->s))
348           ? (0 == (random() % 10))      /* backup to correct */
349           : (0 == (random() % 400)))    /* fail to advance */
350         {
351           st->x--;
352           st->s--;
353           if (st->delay)
354             st->delay += (0xFFFF & (st->delay + 
355                                     (random() % (st->delay * 10))));
356         }
357     }
358
359   st->x++;
360   st->s++;
361
362   if (0 == random() % 200)
363     {
364       if (random() & 1 && st->s != source)
365         st->s--;        /* duplicate character */
366       else if (*st->s)
367         st->s++;        /* skip character */
368     }
369
370   if (*st->s == 0)
371     {
372       st->sentences++;
373       st->caps = (0 == random() % 40);  /* capitalize sentence */
374
375       if (0 == (random() % 10) ||       /* randomly break paragraph */
376           (st->mode == 0 &&
377            ((0 == (random() % 10)) || st->sentences > 20)))
378         {
379           st->break_para = True;
380           st->sentences = 0;
381           st->paras++;
382
383           if (random() & 1)             /* mode=0 50% of the time */
384             st->mode = 0;
385           else
386             st->mode = (0xFF & (random() % 5));
387
388           if (0 == (random() % 2))      /* re-pick margins */
389             {
390               st->left = 0xFF & (random() % (st->columns / 3));
391               st->right = (st->columns -
392                            (0xFF & (random() % (st->columns / 3))));
393
394               if (0 == random() % 3)    /* sometimes be wide */
395                 st->right = st->left + ((st->right - st->left) / 2);
396             }
397
398           if (st->right - st->left <= 10)       /* introduce sanity */
399             {
400               st->left = 0;
401               st->right = st->columns;
402             }
403
404           if (st->right - st->left > 50)        /* if wide, shrink and move */
405             {
406               st->left += (0xFF & (random() % ((st->columns - 50) + 1)));
407               st->right = st->left + (0xFF & ((random() % 40) + 10));
408             }
409
410           /* oh, gag. */
411           if (st->mode == 0 &&
412               st->right - st->left < 25 &&
413               st->columns > 40)
414             {
415               st->right += 20;
416               if (st->right > st->columns)
417                 st->left -= (st->right - st->columns);
418             }
419
420           if (st->right - st->left < 5)
421             st->left = st->right - 5;
422           if (st->left < 0)
423             st->left = 0;
424           if (st->right - st->left < 5)
425             st->right = st->left + 5;
426         }
427       st->s = source;
428     }
429
430   if (st->delay)
431     {
432       if (0 == random() % 3)
433         this_delay += (0xFFFFFF & ((random() % (st->delay * 5)) + 1));
434
435       if (st->break_para)
436         this_delay += (0xFFFFFF & ((random() % (st->delay * 15)) + 1));
437     }
438
439   if (st->paras > 5 &&
440       (0 == (random() % 1000)) &&
441       st->y < st->rows-2)
442     return xjack_pine (st);
443
444   return this_delay;
445 }
446
447 static Bool
448 xjack_event (Display *dpy, Window window, void *closure, XEvent *event)
449 {
450   struct state *st = (struct state *) closure;
451   if (event->xany.type == ButtonPress)
452     {
453       st->scrolling++;
454       return True;
455     }
456
457   return False;
458 }
459
460 static void
461 xjack_free (Display *dpy, Window window, void *closure)
462 {
463   struct state *st = (struct state *) closure;
464   free (st);
465 }
466
467
468 static const char *xjack_defaults [] = {
469   ".background:         #FFF0B4",
470   ".foreground:         #000000",
471   "*fpsSolid:           true",
472 #ifdef HAVE_COCOA
473   ".font:               American Typewriter 24",
474 #else
475   ".font:               -*-courier-medium-r-*-*-*-240-*-*-m-*-*-*",
476 #endif
477   "*delay:              50000",
478   0
479 };
480
481 static XrmOptionDescRec xjack_options [] = {
482   { "-delay",           ".delay",       XrmoptionSepArg, 0 },
483   { "-font",            ".font",        XrmoptionSepArg, 0 },
484   { 0, 0, 0, 0 }
485 };
486
487 XSCREENSAVER_MODULE ("XJack", xjack)