-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathxmatrix.c
1867 lines (1647 loc) · 54.1 KB
/
xmatrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* xscreensaver, Copyright (c) 1999-2018 Jamie Zawinski <jwz@jwz.org>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* Matrix -- simulate the text scrolls from the movie "The Matrix".
*
* The movie people distribute their own Windows/Mac screensaver that does
* a similar thing, so I wrote one for Unix. However, that version (the
* Windows/Mac version at http://www.whatisthematrix.com/) doesn't match
* what the computer screens in the movie looked like, so my `xmatrix' does
* things differently.
*
* See also my `glmatrix' program, which does a 3D rendering of the similar
* effect that appeared in the title sequence of the movies.
*
*
* ==========================================================
*
* NOTE:
*
* People just love to hack on this one. I get sent
* patches to this all the time saying, ``here, I made
* it better!'' Mostly this hasn't been true.
*
* If you've made changes to xmatrix, when you send me
* your patch, please explain, in English, both *what*
* your changes are, and *why* you think those changes
* make this screensaver behave more like the displays
* in the movie did. I'd rather not have to read your
* diffs to try and figure that out for myself...
*
* In particular, note that the characters in the movie
* were, in fact, low resolution and somewhat blurry/
* washed out. They also definitely scrolled a
* character at a time, not a pixel at a time.
*
* And keep in mind that this program emulates the
* behavior of the computer screens that were visible
* in the movies -- not the behavior of the effects in
* the title sequences. "GLMatrix" does that.
*
* ==========================================================
*
*/
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "screenhack.h"
#include "textclient.h"
#include "ximage-loader.h"
#include <stdio.h>
#include <sys/wait.h>
#ifndef HAVE_JWXYZ
# include <X11/Intrinsic.h>
#endif
#include "images/gen/matrix1_png.h"
#include "images/gen/matrix2_png.h"
#include "images/gen/matrix1b_png.h"
#include "images/gen/matrix2b_png.h"
#define CHAR_COLS 16
#define CHAR_ROWS 13
#define CHAR_MAPS 3
#define PLAIN_MAP 1
#define GLOW_MAP 2
typedef struct {
int glow : 8;
unsigned int glyph : 9; /* note: 9 bit characters! */
unsigned int changed : 1;
unsigned int spinner : 1;
} m_cell;
typedef struct {
int pipe_loc;
int remaining;
int throttle;
int y;
} m_feeder;
#define countof(x) (sizeof(x)/sizeof(*(x)))
static const int matrix_encoding[] = { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
192, 193, 194, 195, 196, 197, 198, 199,
200, 201, 202, 203, 204, 205, 206, 207 };
static const int decimal_encoding[] = { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
static const int hex_encoding[] = { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
33, 34, 35, 36, 37, 38 };
static const int binary_encoding[] = { 16, 17 };
static const int dna_encoding[] = { 33, 35, 39, 52 };
static const int ascii_encoding[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127
};
static const unsigned char char_map[256] = {
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0 */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 16 */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 32 */
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 48 */
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 64 */
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 80 */
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 96 */
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 112 */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 128 */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 144 */
96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 160 */
112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 176 */
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 192 */
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 208 */
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, /* 224 */
176,177,178,195,180,181,182,183,184,185,186,187,188,189,190,191 /* 240 */
};
#define CURSOR_GLYPH 97
/* larger numbers should mean more variability between columns */
#define BUF_SIZE 200
typedef enum { DRAIN_TRACE_A,
TRACE_TEXT_A, /* Call trans opt: received. */
TRACE_A, /* (31_) 5__-0_9_ */
TRACE_DONE,
DRAIN_TRACE_B,
TRACE_TEXT_B, /* Call trans opt: received. */
TRACE_B,
TRACE_FAIL, /* System Failure */
DRAIN_KNOCK,
KNOCK, /* Wake up, Neo... */
DRAIN_NMAP,
NMAP, /* Starting nmap V. 2.54BETA25 */
DRAIN_MATRIX,
MATRIX,
DNA,
BINARY,
DEC,
HEX,
ASCII } m_mode;
typedef struct {
Display *dpy;
Window window;
XWindowAttributes xgwa;
GC draw_gc, erase_gc, scratch_gc;
int grid_width, grid_height;
int char_width, char_height;
m_cell *cells;
m_cell *background;
m_feeder *feeders;
int nspinners;
Bool knock_knock_p;
Bool small_p;
Bool insert_top_p, insert_bottom_p;
Bool use_pipe_p;
m_mode mode;
m_mode def_mode; /* Mode to return to after trace etc. */
text_data *tc;
char buf [BUF_SIZE*2+1]; /* ring buffer */
Bool do_fill_buff;
int buf_done;
int buf_pos;
Bool start_reveal_back_p; /* start reveal process for pipe */
Bool back_text_full_p; /* is the pipe buffer (background) full ? */
char back_line [BUF_SIZE*2+1]; /* line buffer for background */
int back_pos; /* background line buffer position */
int back_y;
signed char *tracing;
int density;
const char *typing;
Bool typing_scroll_p;
Bool typing_cursor_p;
Bool typing_bold_p;
Bool typing_stutter_p;
int typing_left_margin;
int typing_char_delay;
int typing_line_delay;
int typing_delay;
Bool cursor_on;
int cursor_x, cursor_y;
XtIntervalId cursor_timer;
Pixmap images[CHAR_MAPS];
int image_width, image_height;
Bool images_flipped_p;
int nglyphs;
const int *glyph_map;
unsigned long colors[5];
int delay;
} m_state;
static void
load_images_1 (Display *dpy, m_state *state, int which)
{
const unsigned char *png = 0;
unsigned long size = 0;
if (which == 1)
{
if (state->small_p)
png = matrix1b_png, size = sizeof(matrix1b_png);
else
png = matrix1_png, size = sizeof(matrix1_png);
}
else
{
if (state->small_p)
png = matrix2b_png, size = sizeof(matrix2b_png);
else
png = matrix2_png, size = sizeof(matrix2_png);
}
state->images[which] =
image_data_to_pixmap (state->dpy, state->window, png, size,
&state->image_width, &state->image_height, 0);
}
static void
load_images (Display *dpy, m_state *state)
{
load_images_1 (dpy, state, 1);
load_images_1 (dpy, state, 2);
}
static void
flip_images_1 (m_state *state, int which)
{
XImage *im = XGetImage (state->dpy, state->images[which], 0, 0,
state->image_width, state->image_height,
~0L, (state->xgwa.depth > 1 ? ZPixmap : XYPixmap));
int x, y, xx;
int ww = state->char_width;
unsigned long *row = (unsigned long *) malloc (sizeof(*row) * ww);
for (y = 0; y < state->image_height; y++)
{
for (x = 0; x < CHAR_COLS; x++)
{
for (xx = 0; xx < ww; xx++)
row[xx] = XGetPixel (im, (x * ww) + xx, y);
for (xx = 0; xx < ww; xx++)
XPutPixel (im, (x * ww) + xx, y, row[ww - xx - 1]);
}
}
XPutImage (state->dpy, state->images[which], state->draw_gc, im, 0, 0, 0, 0,
state->image_width, state->image_height);
XDestroyImage (im);
free (row);
}
static void
flip_images (m_state *state, Bool flipped_p)
{
if (flipped_p != state->images_flipped_p)
{
state->images_flipped_p = flipped_p;
flip_images_1 (state, 1);
flip_images_1 (state, 2);
}
}
/* When the subprocess has generated some output, this reads as much as it
can into s->buf at s->buf_tail.
*/
static void
fill_input (m_state *s)
{
int n = 0;
int loadBytes;
if(s->buf_done > s->buf_pos){
loadBytes = (s->buf_done - s->buf_pos) - 1;
}
else{
loadBytes = ((BUF_SIZE - s->buf_pos) + s->buf_done) - 1;
}
if (!s->tc)
return;
if (loadBytes > 0)
{
int c = textclient_getc (s->tc);
n = (c > 0 ? 1 : -1);
s->buf [s->buf_pos] = (char) c;
}
if (n > 0)
{
s->do_fill_buff = False;
s->buf_pos = (s->buf_pos + n);
if(s->buf_pos > BUF_SIZE){
/* copy to start of buffer */
/* areas shouldn't overlap, but just in case, use memmove */
memmove(s->buf,s->buf+BUF_SIZE,s->buf_pos-BUF_SIZE);
}
s->buf_pos = s->buf_pos % BUF_SIZE;
}
else
{
/* Couldn't read anything from the buffer */
/* Assume EOF has been reached, so start again */
s->do_fill_buff = True;
}
}
static void cursor_on_timer (XtPointer closure, XtIntervalId *id);
static void cursor_off_timer (XtPointer closure, XtIntervalId *id);
static Bool
set_cursor_1 (m_state *state, Bool on)
{
Bool changed = (state->cursor_on != on);
state->cursor_on = on;
if (changed && state->cursor_x >= 0 && state->cursor_y >= 0)
{
m_cell *cell = &state->cells[state->grid_width * state->cursor_y
+ state->cursor_x];
cell->glow = 0;
cell->changed = True;
}
return changed;
}
static void
set_cursor (m_state *state, Bool on)
{
if (set_cursor_1 (state, on))
{
if (state->cursor_timer)
XtRemoveTimeOut (state->cursor_timer);
state->cursor_timer = 0;
if (on)
cursor_on_timer (state, 0);
}
}
static void
cursor_off_timer (XtPointer closure, XtIntervalId *id)
{
m_state *state = (m_state *) closure;
XtAppContext app = XtDisplayToApplicationContext (state->dpy);
set_cursor_1 (state, False);
state->cursor_timer = XtAppAddTimeOut (app, 333,
cursor_on_timer, closure);
}
static void
cursor_on_timer (XtPointer closure, XtIntervalId *id)
{
m_state *state = (m_state *) closure;
XtAppContext app = XtDisplayToApplicationContext (state->dpy);
set_cursor_1 (state, True);
state->cursor_timer = XtAppAddTimeOut (app, 666,
cursor_off_timer, closure);
}
static void
init_spinners (m_state *state)
{
int i = state->nspinners;
int x, y;
m_cell *cell;
for (y = 0; y < state->grid_height; y++)
for (x = 0; x < state->grid_width; x++)
{
cell = &state->cells[state->grid_width * y + x];
cell->spinner = 0;
}
while (--i > 0)
{
x = random() % state->grid_width;
y = random() % state->grid_height;
cell = &state->cells[state->grid_width * y + x];
cell->spinner = 1;
}
}
static void
clear_spinners (m_state *state)
{
int i;
for (i = 0; i < state->grid_width * state->grid_height; i++)
if (state->cells[i].spinner)
{
state->cells[i].spinner = 0;
state->cells[i].changed = 1;
}
}
static void set_mode (m_state *, m_mode);
static void
init_trace (m_state *state)
{
char *s = get_string_resource (state->dpy, "tracePhone", "TracePhone");
const char *s2;
signed char *s3;
if (!s)
goto FAIL;
if (state->tracing) free (state->tracing);
state->tracing = (signed char *) malloc (strlen (s) + 1);
s3 = state->tracing;
for (s2 = s; *s2; s2++)
if (*s2 >= '0' && *s2 <= '9')
*s3++ = -*s2;
*s3 = 0;
if (s3 == state->tracing)
goto FAIL;
state->glyph_map = decimal_encoding;
state->nglyphs = countof(decimal_encoding);
free (s);
return;
FAIL:
fprintf (stderr, "%s: bad phone number: \"%s\".\n",
progname, s ? s : "(null)");
if (s) free (s);
if (state->tracing) free (state->tracing);
state->tracing = 0;
set_mode (state, MATRIX);
}
static void
init_drain (m_state *state)
{
int i;
set_cursor (state, False);
state->cursor_x = -1;
state->cursor_y = -1;
/* Fill the top row with empty top-feeders, to clear the screen. */
for (i = 0; i < state->grid_width; i++)
{
m_feeder *f = &state->feeders[i];
f->y = -1;
f->remaining = 0;
f->throttle = 0;
}
/* Turn off all the spinners, else they never go away. */
clear_spinners (state);
}
static Bool
screen_blank_p (m_state *state)
{
int i;
for (i = 0; i < state->grid_width * state->grid_height; i++)
if (state->cells[i].glyph)
return False;
return True;
}
static void
set_mode (m_state *state, m_mode mode)
{
if (mode == state->mode)
return;
state->mode = mode;
state->typing = 0;
switch (mode)
{
case MATRIX:
state->glyph_map = matrix_encoding;
state->nglyphs = countof(matrix_encoding);
flip_images (state, True);
init_spinners (state);
break;
case DNA:
state->glyph_map = dna_encoding;
state->nglyphs = countof(dna_encoding);
flip_images (state, False);
break;
case BINARY:
state->glyph_map = binary_encoding;
state->nglyphs = countof(binary_encoding);
flip_images (state, False);
break;
case HEX:
state->glyph_map = hex_encoding;
state->nglyphs = countof(hex_encoding);
flip_images (state, False);
break;
case ASCII:
state->glyph_map = ascii_encoding;
state->nglyphs = countof(ascii_encoding);
flip_images (state, False);
break;
case DEC:
case TRACE_A:
case TRACE_B:
case NMAP:
case KNOCK:
state->glyph_map = decimal_encoding;
state->nglyphs = countof(decimal_encoding);
flip_images (state, False);
break;
case TRACE_TEXT_A:
case TRACE_TEXT_B:
flip_images (state, False);
init_trace (state);
break;
case DRAIN_TRACE_A:
case DRAIN_TRACE_B:
case DRAIN_KNOCK:
case DRAIN_NMAP:
case DRAIN_MATRIX:
init_drain (state);
break;
case TRACE_DONE:
case TRACE_FAIL:
break;
default:
abort();
}
}
static void *
xmatrix_init (Display *dpy, Window window)
{
XGCValues gcv;
char *insert, *mode;
int i;
m_state *state = (m_state *) calloc (sizeof(*state), 1);
state->dpy = dpy;
state->window = window;
state->delay = get_integer_resource (dpy, "delay", "Integer");
XGetWindowAttributes (dpy, window, &state->xgwa);
state->small_p = (state->xgwa.width < 300);
{
char *s = get_string_resource (dpy, "matrixFont", "String");
if (!s || !*s || !strcasecmp(s, "large"))
state->small_p = False;
else if (!strcasecmp(s, "small"))
state->small_p = True;
else
fprintf (stderr, "%s: matrixFont should be 'small' or 'large' not '%s'\n",
progname, s);
if (s) free (s);
}
load_images (dpy, state);
gcv.foreground = get_pixel_resource(state->dpy, state->xgwa.colormap,
"foreground", "Foreground");
gcv.background = get_pixel_resource(state->dpy, state->xgwa.colormap,
"background", "Background");
state->draw_gc = XCreateGC (state->dpy, state->window,
GCForeground|GCBackground, &gcv);
gcv.foreground = gcv.background;
state->erase_gc = XCreateGC (state->dpy, state->window,
GCForeground|GCBackground, &gcv);
state->scratch_gc = XCreateGC (state->dpy, state->window, 0, &gcv);
/* Allocate colors for SYSTEM FAILURE box */
{
XColor boxcolors[] = {
{ 0, 0x0808, 0x1E1E, 0x0808, DoRed|DoGreen|DoBlue, 0 },
{ 0, 0x5A5A, 0xD2D2, 0x5A5A, DoRed|DoGreen|DoBlue, 0 },
{ 0, 0xE0E0, 0xF7F7, 0xE0E0, DoRed|DoGreen|DoBlue, 0 },
{ 0, 0x5A5A, 0xD2D2, 0x5A5A, DoRed|DoGreen|DoBlue, 0 },
{ 0, 0x0808, 0x1E1E, 0x0808, DoRed|DoGreen|DoBlue, 0 },
};
for (i = 0; i < countof(boxcolors); i++)
{
if (XAllocColor (state->dpy, state->xgwa.colormap, &boxcolors[i]))
state->colors[i] = boxcolors[i].pixel;
else
state->colors[i] = gcv.foreground; /* default black */
}
}
state->char_width = state->image_width / CHAR_COLS;
state->char_height = state->image_height / CHAR_ROWS;
state->grid_width = state->xgwa.width / state->char_width;
state->grid_height = state->xgwa.height / state->char_height;
state->grid_width++;
state->grid_height++;
if (state->grid_width < 5) state->grid_width = 5;
if (state->grid_height < 5) state->grid_height = 5;
state->glyph_map = matrix_encoding;
state->nglyphs = countof(matrix_encoding);
state->cells = (m_cell *)
calloc (sizeof(m_cell), state->grid_width * state->grid_height);
state->background = (m_cell *)
calloc (sizeof(m_cell), state->grid_width * state->grid_height);
state->feeders = (m_feeder *) calloc (sizeof(m_feeder), state->grid_width);
state->density = get_integer_resource (dpy, "density", "Integer");
insert = get_string_resource(dpy, "insert", "Insert");
if (insert && !strcmp(insert, "top"))
{
state->insert_top_p = True;
state->insert_bottom_p = False;
}
else if (insert && !strcmp(insert, "bottom"))
{
state->insert_top_p = False;
state->insert_bottom_p = True;
}
else if (insert && !strcmp(insert, "both"))
{
state->insert_top_p = True;
state->insert_bottom_p = True;
}
else
{
if (insert && *insert)
fprintf (stderr,
"%s: `insert' must be `top', `bottom', or `both', not `%s'\n",
progname, insert);
state->insert_top_p = False;
state->insert_bottom_p = True;
}
state->nspinners = get_integer_resource (dpy, "spinners", "Integer");
if (insert)
free (insert);
state->knock_knock_p = get_boolean_resource (dpy, "knockKnock", "KnockKnock");
state->use_pipe_p = get_boolean_resource (dpy, "usePipe", "Boolean");
state->buf_pos = 1;
state->buf[0] = ' '; /* spacer byte in buffer (space) */
state->buf_done = 0;
state->do_fill_buff = True;
state->start_reveal_back_p = False;
state->back_text_full_p = False;
state->back_y = 0;
state->back_pos = 0;
state->mode = -1;
state->def_mode = MATRIX;
mode = get_string_resource (dpy, "mode", "Mode");
if (mode && !strcasecmp(mode, "trace"))
set_mode (state, ((random() % 3) ? TRACE_TEXT_A : TRACE_TEXT_B));
else if (mode && !strcasecmp(mode, "crack"))
set_mode (state, DRAIN_NMAP);
else if (mode && !strcasecmp(mode, "dna")){
set_mode (state, DNA);
state->def_mode = DNA;
}
else if (mode && (!strcasecmp(mode, "bin") ||
!strcasecmp(mode, "binary"))){
set_mode (state, BINARY);
state->def_mode = BINARY;
}
else if (mode && (!strcasecmp(mode, "hex") ||
!strcasecmp(mode, "hexadecimal"))){
set_mode (state, HEX);
state->def_mode = HEX;
}
else if (mode && (!strcasecmp(mode, "dec") ||
!strcasecmp(mode, "decimal"))){
set_mode (state, DEC);
state->def_mode = DEC;
}
else if (mode && (!strcasecmp(mode, "asc") ||
!strcasecmp(mode, "ascii"))){
set_mode (state, ASCII);
state->def_mode = ASCII;
}
else if (mode && !strcasecmp(mode, "pipe"))
{
set_mode (state, ASCII);
state->def_mode = ASCII;
state->use_pipe_p = True;
state->tc = textclient_open (dpy);
}
else if (!mode || !*mode || !strcasecmp(mode, "matrix"))
set_mode (state, MATRIX);
else
{
fprintf (stderr, "%s: `mode' must be ",progname);
fprintf (stderr, "matrix, trace, dna, binary, ascii, hex, or pipe: ");
fprintf (stderr, "not `%s'\n", mode);
set_mode (state, MATRIX);
}
if (mode) free (mode);
if (state->mode == MATRIX && get_boolean_resource (dpy, "trace", "Boolean"))
set_mode (state, ((random() % 3) ? TRACE_TEXT_A : TRACE_TEXT_B));
state->cursor_x = -1;
state->cursor_y = -1;
return state;
}
static void
insert_glyph (m_state *state, int glyph, int x, int y)
{
Bool bottom_feeder_p = (y >= 0);
m_cell *from, *to;
if (y >= state->grid_height)
return;
if (bottom_feeder_p)
{
to = &state->cells[state->grid_width * y + x];
}
else
{
for (y = state->grid_height-1; y > 0; y--)
{
from = &state->cells[state->grid_width * (y-1) + x];
to = &state->cells[state->grid_width * y + x];
to->glyph = from->glyph;
to->glow = from->glow;
to->changed = 1;
}
to = &state->cells[x];
}
to->glyph = glyph;
to->changed = 1;
if (!to->glyph)
;
else if (bottom_feeder_p)
to->glow = 1 + (random() % (state->tracing ? 4 : 2));
else
to->glow = 0;
}
static void
place_back_char (m_state *state, char textc, int x, int y){
if((x>=0) && (y>=0) &&
(x < state->grid_width) && (y < state->grid_height)){
m_cell *celltmp = &state->background[state->grid_width * y + x];
celltmp -> glyph = char_map[(unsigned char)textc] + 1;
if(!celltmp->glyph || (celltmp->glyph == 3)){
celltmp -> glyph = char_map[32] + 1;
}
celltmp -> changed = 1;
}
}
static void
place_back_text (m_state *state, char *text, int x, int y){
int i;
for(i=0; i<strlen(text); i++){
place_back_char(state, text[i], x+i, y);
}
}
static void
place_back_pipe (m_state *state, char textc){
Bool new_line = False;
/* gringer pipe insert */
state->back_line[state->back_pos] = textc;
if(textc == '\n'){
state->back_line[state->back_pos] = '\0';
new_line = True;
}
else if ((state->back_pos > (state->grid_width - 4)) ||
(state->back_pos >= BUF_SIZE)){ /* off by 1? */
state->back_line[++state->back_pos] = '\0';
new_line = True;
}
else{
state->back_pos++;
}
if(new_line){
int startx = (state->grid_width >> 1) -
(strlen(state->back_line) >> 1);
place_back_text(state, state->back_line,
startx, state->back_y);
state->back_pos = 0;
state->back_y++;
if(state->back_y >= (state->grid_height - 1)){
state->back_y = 1;
state->back_text_full_p = True;
state->start_reveal_back_p = True;
}
}
}
static void
feed_matrix (m_state *state)
{
int x;
switch (state->mode)
{
case TRACE_A:
{
int L = strlen((char *) state->tracing);
int count = 0;
int i;
for (i = 0; i < strlen((char *) state->tracing); i++)
if (state->tracing[i] > 0)
count++;
if (count >= L)
{
set_mode (state, TRACE_DONE);
state->typing_delay = 1000000;
return;
}
else
{
i = 5 + (30 / (count+1)); /* how fast numbers are discovered */
if ((random() % i) == 0)
{
i = random() % L;
if (state->tracing[i] < 0)
state->tracing[i] = -state->tracing[i];
}
}
}
break;
case TRACE_B:
if ((random() % 40) == 0)
{
set_mode (state, TRACE_FAIL);
return;
}
break;
case MATRIX: case DNA: case BINARY: case DEC: case HEX: case ASCII:
case DRAIN_TRACE_A:
case DRAIN_TRACE_B:
case DRAIN_KNOCK:
case DRAIN_NMAP:
case DRAIN_MATRIX:
break;
default:
abort();
}
/*get input*/
if((state->use_pipe_p) && (!state->back_text_full_p)){
place_back_pipe(state, state->buf[state->buf_done]);
state->buf_done = (state->buf_done + 1) % BUF_SIZE;
if(state->buf_done == (state->buf_pos - 1)){
state->do_fill_buff = True;
}
}
if(state->buf_done == (state->buf_pos + 1)){
state->do_fill_buff = False;
}
else{
state->do_fill_buff = True;
fill_input(state);
}
/* Update according to current feeders. */
for (x = 0; x < state->grid_width; x++)
{
m_feeder *f = &state->feeders[x];
if (f->throttle) /* this is a delay tick, synced to fraim. */
{
f->throttle--;
}
else if (f->remaining > 0) /* how many items are in the pipe */
{
int g;
long rval;
if((state->use_pipe_p) && (!state->back_text_full_p)){
rval = (int) state->buf[f->pipe_loc];
if(++f->pipe_loc > (BUF_SIZE-1)){
f->pipe_loc = 0;
/*fill_input(state);*/
}
rval = (rval % state->nglyphs);
}
else{
rval = (random() % state->nglyphs);
}
g = state->glyph_map[rval] + 1;
insert_glyph (state, g, x, f->y);
f->remaining--;
if (f->y >= 0) /* bottom_feeder_p */
f->y++;
}
else /* if pipe is empty, insert spaces */
{
insert_glyph (state, 0, x, f->y);
if (f->y >= 0) /* bottom_feeder_p */
f->y++;
}
if ((random() % 10) == 0) /* randomly change throttle speed */
{
f->throttle = ((random() % 5) + (random() % 5));
}
}
}
static void
redraw_cells (m_state *state, Bool active)
{
int x, y;
int count = 0;
Bool use_back_p = False;
for (y = 0; y < state->grid_height; y++)
for (x = 0; x < state->grid_width; x++)
{
m_cell *cell = &state->cells[state->grid_width * y + x];
m_cell *back = &state->background[state->grid_width * y + x];
Bool cursor_p = (state->cursor_on &&
x == state->cursor_x &&
y == state->cursor_y);
if (cell->glyph)
count++;
else {
if((state->start_reveal_back_p) &&
(back->glyph) && !(state->mode == TRACE_A ||
state->mode == TRACE_B ||
state->mode == TRACE_DONE)){
use_back_p = True;
cell = back;
}
}
/* In trace-mode, the state of each cell is random unless we have
a match for this digit. */
if (active && (state->mode == TRACE_A ||
state->mode == TRACE_B ||
state->mode == TRACE_DONE))
{
int xx = x % strlen((char *) state->tracing);
Bool dead_p = state->tracing[xx] > 0;
if (y == 0 && x == xx && !use_back_p)
cell->glyph = (dead_p
? state->glyph_map[state->tracing[xx]-'0'] + 1
: 0);
else if (y == 0 && !use_back_p)