+static void drain_matrix (m_state *);
+
+static void
+handle_events (m_state *state)
+{
+ XSync (state->dpy, False);
+ while (XPending (state->dpy))
+ {
+ XEvent event;
+ XNextEvent (state->dpy, &event);
+
+ if (event.xany.type == ConfigureNotify)
+ {
+ int ow = state->grid_width;
+ int oh = state->grid_height;
+ XGetWindowAttributes (state->dpy, state->window, &state->xgwa);
+ 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;
+
+ if (ow != state->grid_width ||
+ oh != state->grid_height)
+ {
+ m_cell *ncells = (m_cell *)
+ calloc (sizeof(m_cell),
+ state->grid_width * state->grid_height);
+ m_feeder *nfeeders = (m_feeder *)
+ calloc (sizeof(m_feeder), state->grid_width);
+ int x, y, i;
+
+ /* fprintf(stderr, "resize: %d x %d ==> %d x %d\n",
+ ow, oh, state->grid_width, state->grid_height); */
+
+ for (y = 0; y < oh; y++)
+ for (x = 0; x < ow; x++)
+ if (x < ow && x < state->grid_width &&
+ y < oh && y < state->grid_height)
+ ncells[y * state->grid_width + x] =
+ state->cells[y * ow + x];
+ free (state->cells);
+ state->cells = ncells;
+
+ x = (ow < state->grid_width ? ow : state->grid_width);
+ for (i = 0; i < x; i++)
+ nfeeders[i] = state->feeders[i];
+ free (state->feeders);
+ state->feeders = nfeeders;
+ }
+ }
+ else if (event.xany.type == KeyPress)
+ {
+ KeySym keysym;
+ char c = 0;
+ XLookupString (&event.xkey, &c, 1, &keysym, 0);
+ if (c == '0' && !state->tracing)
+ {
+ drain_matrix (state);
+ return;
+ }
+ else if (c == '+' || c == '=' || c == '>' || c == '.')
+ {
+ state->density += 10;
+ if (state->density > 100)
+ state->density = 100;
+ else
+ return;
+ }
+ else if (c == '-' || c == '_' || c == '<' || c == ',')
+ {
+ state->density -= 10;
+ if (state->density < 0)
+ state->density = 0;
+ else
+ return;
+ }
+ else if (c == '[' || c == '(' || c == '{')
+ {
+ state->insert_top_p = True;
+ state->insert_bottom_p = False;
+ return;
+ }
+ else if (c == ']' || c == ')' || c == '}')
+ {
+ state->insert_top_p = False;
+ state->insert_bottom_p = True;
+ return;
+ }
+ else if (c == '\\' || c == '|')
+ {
+ state->insert_top_p = True;
+ state->insert_bottom_p = True;
+ return;
+ }
+ else if ((c == 't' || c == 'T') && state->mode == MATRIX)
+ {
+ state->mode = (c == 't' ? TRACEA1 : TRACEB1);
+ flip_images (state);
+ init_trace (state);
+ return;
+ }
+ else if ((c == 'c' || c == 'k') && state->mode == MATRIX)
+ {
+ drain_matrix (state);
+ state->mode = (c == 'c' ? NMAP : KNOCK);
+ flip_images (state);
+ return;
+ }
+ }
+
+ screenhack_handle_event (state->dpy, &event);
+ }
+}
+
+
+static void
+matrix_usleep (m_state *state, unsigned long delay)
+{
+ if (!delay) return;
+
+ if (state->cursor)
+ {
+ int blink_delay = 333000;
+ int tot_delay = 0;
+ m_cell *cursor = state->cursor;
+ while (tot_delay < delay)
+ {
+ if (state->cursor)
+ {
+ usleep (blink_delay * 2);
+ tot_delay += (2 * blink_delay);
+ state->cursor = NULL;
+ }
+ else
+ {
+ usleep (blink_delay);
+ tot_delay += blink_delay;
+ state->cursor = cursor;
+ }
+ cursor->changed = 1;
+ redraw_cells (state, False);
+ XSync (state->dpy, False);
+ handle_events (state);
+ }
+ }
+ else
+ {
+ XSync (state->dpy, False);
+ handle_events (state);
+ usleep (delay);
+ }
+}
+
+
+static void
+hack_text_1 (m_state *state,
+ int *xP, int *yP,
+ const char *s,
+ Bool typing_delay,
+ Bool transmit_delay,
+ Bool long_delay,
+ Bool visible_cursor,
+ Bool scroll_p)
+{
+ int x = *xP;
+ int y = *yP;
+ int i = state->grid_width * y + x;
+ Bool glow_p = False;
+ int long_delay_usecs = 1000000;
+
+ if (long_delay == -1)
+ long_delay = 0, long_delay_usecs /= 6;
+
+ if (y >= state->grid_height-1) return;
+
+ while (*s)
+ {
+ m_cell *cell;
+ Bool done_p = s[1] == '\000';
+
+ long_delay = done_p;
+
+ if (*s == '\n' || x >= state->grid_width - 1)
+ {
+ if (*s != '\n')
+ s--;
+ x = 0;
+ y++;
+ i = state->grid_width * y + x;
+
+ if (scroll_p)
+ {
+ int xx, yy;
+ for (yy = 0; yy < state->grid_height-1; yy++)
+ for (xx = 0; xx < state->grid_width; xx++)
+ {
+ int ii = yy * state->grid_width + xx;
+ int jj = (yy+1) * state->grid_width + xx;
+ state->cells[ii] = state->cells[jj];
+ state->cells[ii].changed = 1;
+ }
+ /* clear bottom row */
+ for (xx = 0; xx < state->grid_width; xx++)
+ {
+ int ii = yy * state->grid_width + xx;
+ state->cells[ii].glyph = 0;
+ state->cells[ii].changed = 1;
+ }
+ y--; /* move it back */
+ i = state->grid_width * y + x;
+ }
+
+ if (y >= state->grid_height) return;
+
+ cell = &state->cells[i];
+ if (visible_cursor)
+ {
+ cell->changed = 1;
+ state->cursor = cell;
+ }
+ }
+ else if (*s == '\010')
+ ;
+ else if (*s == '\002')
+ glow_p = True;
+ else
+ {
+ cell = &state->cells[i];
+ if (x < state->grid_width-1)
+ {
+ cell->glyph = char_map[(unsigned char) *s] + 1;
+ if (*s == ' ' || *s == '\t') cell->glyph = 0;
+ cell->changed = 1;
+ cell->glow = (glow_p ? 100 : 0);
+ if (visible_cursor)
+ {
+ m_cell *next = &state->cells[i + 1];
+ next->changed = 1;
+ state->cursor = next;
+ }
+ i++;
+ }
+ x++;
+ }
+ s++;
+ if (typing_delay || transmit_delay || long_delay)
+ {
+ redraw_cells (state, False);
+ XSync (state->dpy, False);
+ handle_events (state);
+ if (typing_delay)
+ {
+ usleep (50000);
+ if (typing_delay && 0 == random() % 3)
+ usleep (0xFFFFFF & ((random() % 250000) + 1));
+ }
+ else
+ if (long_delay)
+ matrix_usleep (state, long_delay_usecs);
+ else
+ usleep (20000);
+ }
+ }
+
+ *xP = x;
+ *yP = y;
+}
+
+
+static void
+zero_cells (m_state *state)
+{
+ int i;
+ for (i = 0; i < state->grid_height * state->grid_width; i++)
+ {
+ m_cell *cell = &state->cells[i];
+ cell->changed = (cell->glyph != 0);
+ cell->glyph = 0;
+ cell->glow = 0;
+ cell->spinner = 0;
+ }
+}
+
+
+static void
+hack_text (m_state *state)
+{
+ Bool typing_delay = False;
+ Bool transmit_delay = False;
+ Bool long_delay = False;
+ Bool visible_cursor = False;
+
+ switch (state->mode)
+ {
+ case KNOCK:
+ {
+ const char *blocks[] = {
+ "Wake up, Neo...",
+ "The Matrix has you...",
+ "Follow the white rabbit.",
+ " ",
+ "Knock, knock, Neo."
+ };
+ int nblocks = countof(blocks);
+ int j;
+ typing_delay = True;
+ transmit_delay = False;
+ long_delay = False;
+ visible_cursor = True;
+ for (j = 0; j < nblocks; j++)
+ {
+ int x = 3;
+ int y = 2;
+ const char *s = blocks[j];
+ if (!s[0] || !s[1]) typing_delay = False;
+ zero_cells (state);
+ hack_text_1 (state, &x, &y, s,
+ typing_delay, transmit_delay, -1,
+ visible_cursor, True);
+ matrix_usleep (state, 2000000);
+ }
+ }
+ break;
+
+ case TRACEA1: case TRACEB1:
+ {
+ const char *blocks[10];
+ int j, n = 0;
+
+ if (state->mode == TRACEA1)
+ blocks[n++] =
+ (state->grid_width >= 52
+ ? "Call trans opt: received. 2-19-98 13:24:18 REC:Log>"
+ : "Call trans opt: received.\n2-19-98 13:24:18 REC:Log>");
+ else
+ blocks[n++] =
+ (state->grid_width >= 52
+ ? "Call trans opt: received. 9-18-99 14:32:21 REC:Log>"
+ : "Call trans opt: received.\n9-18-99 14:32:21 REC:Log>");
+
+ if (state->mode == TRACEB1)
+ blocks[n++] = "WARNING: carrier anomaly";
+ blocks[n++] = "Trace program: running";
+
+ typing_delay = False;
+ transmit_delay = True;
+ long_delay = True;
+ visible_cursor = True;
+ for (j = 0; j < n; j++)
+ {
+ const char *s = blocks[j];
+ int x = 0;
+ int y = 0;
+ zero_cells (state);
+ hack_text_1 (state, &x, &y, s,
+ typing_delay, transmit_delay, long_delay,
+ visible_cursor, True);
+ }
+ matrix_usleep (state, 1000000);
+ }
+ break;
+
+ case SYSTEMFAILURE:
+ {
+ const char *s = "SYSTEM FAILURE";
+ int i;
+ float cx = ((int)state->grid_width - (int)strlen(s)) / 2 - 0.5;
+ float cy = (state->grid_height / 2) - 1.3;
+ int x, y;
+
+ if (cy < 0) cy = 0;
+ if (cx < 0) cx = 0;
+
+ XFillRectangle (state->dpy, state->window, state->erase_gc,
+ cx * state->char_width,
+ cy * state->char_height,
+ (strlen(s) + 1) * state->char_width,
+ state->char_height * 1.6);
+
+ for (i = -2; i < 3; i++)
+ {
+ XGCValues gcv;
+ gcv.foreground = state->colors[i + 2];
+ XChangeGC (state->dpy, state->scratch_gc, GCForeground, &gcv);
+ XDrawRectangle (state->dpy, state->window, state->scratch_gc,
+ cx * state->char_width - i,
+ cy * state->char_height - i,
+ (strlen(s) + 1) * state->char_width + (2 * i),
+ (state->char_height * 1.6) + (2 * i));
+ }
+
+ /* If we don't clear these out, part of the box may get overwritten */
+ for (i = 0; i < state->grid_height * state->grid_width; i++)
+ {
+ m_cell *cell = &state->cells[i];
+ cell->changed = 0;
+ }
+
+ x = ((int)state->grid_width - (int)strlen(s)) / 2;
+ y = (state->grid_height / 2) - 1;
+ if (y < 0) y = 0;
+ if (x < 0) x = 0;
+ hack_text_1 (state, &x, &y, s,
+ typing_delay, transmit_delay, long_delay,
+ visible_cursor, False);
+ }
+ break;
+
+ case NMAP:
+ {
+ /* Note that what Trinity is using here is moderately accurate:
+ She runs nmap (http://www.insecure.org/nmap/) then breaks in
+ with a (hypothetical) program called "sshnuke" that exploits
+ the (very real) SSHv1 CRC32 compensation attack detector bug
+ (http://staff.washington.edu/dittrich/misc/ssh-analysis.txt).
+
+ The command syntax of the power grid control software looks a
+ lot like Cisco IOS to me. (IOS is a descendant of VMS.)
+ */
+ const char *blocks[] = {
+ "# ",
+
+ "\001nmap 10.2.2.2\n",
+ "Starting nmap V. 2.54BETA25\n"
+
+ "\010", "\010", "\010",
+
+ "Insufficient responses for TCP sequencing (3), OS detection "
+ "may be less accurate\n"
+ "Interesting ports on 10.2.2.2:\n"
+ "(The 1538 ports scanned but not shown below are in state: "
+ "filtered)\n"
+ "Port state service\n"
+ "22/tcp open ssh\n"
+ "\n"
+ "No exact OS matches for host\n"
+ "\n"
+ "Nmap run completed -- 1 IP address (1 host up) scanned\n"
+ "# ",
+
+ "\001sshnuke 10.2.2.2 -rootpw=\"Z1ON0101\"\n",
+
+ "Connecting to 10.2.2.2:ssh ... ",
+
+ "successful.\n"
+ "Attempting to exploit SSHv1 CRC32 ... ",
+
+ "successful.\n"
+ "Resetting root password to \"Z1ON0101\".\n",
+
+ "System open: Access Level <9>\n"
+ "# ",
+
+ "\001ssh 10.2.2.2 -l root\n",
+
+ "root@10.2.2.2's password: ",
+
+ "\001\010\010\010\010\010\010\010\010\n",
+
+ "\n"
+ "RRF-CONTROL> ",
+
+ "\001disable grid nodes 21 - 48\n",
+
+ "\002Warning: Disabling nodes 21-48 will disconnect sector 11 "
+ "(27 nodes)\n"
+ "\n"
+ "\002 ARE YOU SURE? (y/n) ",
+
+ "\001\010\010y\n",
+ "\n"
+ };
+
+ int nblocks = countof(blocks);
+ int y = state->grid_height - 2;
+ int x, j;
+
+ visible_cursor = True;
+ x = 0;
+ zero_cells (state);
+ for (j = 0; j < nblocks; j++)
+ {
+ const char *s = blocks[j];
+ typing_delay = (*s == '\001');
+ if (typing_delay) s++;
+
+ long_delay = False;
+ hack_text_1 (state, &x, &y, s,
+ typing_delay, transmit_delay, long_delay,
+ visible_cursor, True);
+ }
+
+ typing_delay = False;
+ long_delay = False;
+ for (j = 21; j <= 48; j++)
+ {
+ char buf[100];
+ sprintf (buf, "\002Grid Node %d offline...\n", j);
+ hack_text_1 (state, &x, &y, buf,
+ typing_delay, transmit_delay, -1,
+ visible_cursor, True);
+
+ }
+ long_delay = True;
+ hack_text_1 (state, &x, &y, "\nRRF-CONTROL> ",
+ typing_delay, transmit_delay, long_delay,
+ visible_cursor, True);
+
+ /* De-glow all cells before draining them... */
+ for (j = 0; j < state->grid_height * state->grid_width; j++)
+ {
+ m_cell *cell = &state->cells[j];
+ cell->changed = (cell->glow != 0);
+ cell->glow = 0;
+ }
+ }
+ break;
+
+ default:
+ abort();
+ break;
+ }
+}
+
+
+static void
+drain_matrix (m_state *state)
+{
+ int delay = get_integer_resource ("delay", "Integer");
+ int i;
+
+ /* 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. */
+ 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;
+ }
+
+ /* Run the machine until there are no live cells left. */
+ while (1)
+ {
+ Bool any_cells_p = False;
+ for (i = 0; i < state->grid_width * state->grid_height; i++)
+ if (state->cells[i].glyph)
+ {
+ any_cells_p = True;
+ goto FOUND;
+ }
+ FOUND:
+ if (! any_cells_p)
+ return;
+
+ feed_matrix (state);
+ redraw_cells (state, True);
+ XSync (state->dpy, False);
+ handle_events (state);
+ if (delay) usleep (delay);
+ }
+}
+
+
+static void
+roll_state (m_state *state)
+{
+ int delay = 0;
+ switch (state->mode)
+ {
+ case TRACEA1:
+ state->mode = TRACEA2;
+ break;
+ case TRACEB1:
+ state->mode = TRACEB2;
+ break;
+
+ case TRACEA2:
+ {
+ Bool any = False;
+ int i;
+ for (i = 0; i < strlen((char *) state->tracing); i++)
+ if (state->tracing[i] < 0) any = True;
+
+ if (!any)
+ {
+ XSync (state->dpy, False);
+ matrix_usleep (state, 3000000);
+ state->mode = MATRIX;
+ state->glyph_map = matrix_encoding;
+ state->nglyphs = countof(matrix_encoding);
+ flip_images (state);
+ free (state->tracing);
+ state->tracing = 0;
+ }
+ else if ((random() % 20) == 0) /* how fast numbers are discovered */
+ {
+ int x = random() % strlen((char *) state->tracing);
+ if (state->tracing[x] < 0)
+ state->tracing[x] = -state->tracing[x];
+ }
+ break;
+ }
+
+ case TRACEB2:
+ {
+ /* reversed logic from TRACEA2 */
+ Bool any = False;
+ int i;
+ for (i = 0; i < strlen((char *) state->tracing); i++)
+ if (state->tracing[i] > 0) any = True;
+
+ if ((random() % 15) == 0) {
+ if (any)
+ state->mode = SYSTEMFAILURE;
+ else
+ {
+ int x = random() % strlen((char *) state->tracing);
+ if (state->tracing[x] < 0)
+ state->tracing[x] = -state->tracing[x];
+ }
+ }
+ break;
+ }
+
+ case SYSTEMFAILURE:
+ XSync (state->dpy, False);
+ matrix_usleep (state, 6000000);
+ state->mode = MATRIX;
+ state->glyph_map = matrix_encoding;
+ state->nglyphs = countof(matrix_encoding);
+ flip_images (state);
+ drain_matrix (state);
+ matrix_usleep (state, 2000000);
+ if (state->tracing) {
+ free (state->tracing);
+ state->tracing = 0;
+ }
+ break;
+
+ case KNOCK:
+ case NMAP:
+ state->mode = MATRIX;
+ state->glyph_map = matrix_encoding;
+ state->nglyphs = countof(matrix_encoding);
+ flip_images (state);
+ break;
+
+ case MATRIX:
+ if (state->knock_knock_p && (! (random() % 3500)))
+ {
+ drain_matrix (state);
+ if (! (random() % 5))
+ state->mode = NMAP;
+ else
+ state->mode = KNOCK;
+
+ flip_images (state);
+ }
+ break;
+
+ case DNA: case BINARY: case DEC: case HEX:
+ break;
+
+ default:
+ abort();
+ break;
+ }
+
+ matrix_usleep (state, delay * 1000000);
+ state->cursor = NULL;
+}
+
+