2016-04-04 19:07:35 -07:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdio.h>
|
2016-04-08 12:34:07 -07:00
|
|
|
#include <string.h>
|
2016-04-04 19:36:00 -07:00
|
|
|
#include <stdlib.h>
|
2016-04-04 19:07:35 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
2016-04-06 17:28:18 -07:00
|
|
|
#include <stdbool.h>
|
2016-04-08 12:34:07 -07:00
|
|
|
#include <time.h>
|
2016-04-04 19:07:35 -07:00
|
|
|
|
2016-04-07 11:12:30 -07:00
|
|
|
#define SPACE 32
|
|
|
|
#define NEWLINE 10
|
2016-04-04 19:07:35 -07:00
|
|
|
|
2016-04-10 15:30:44 -07:00
|
|
|
#define KNRM "\x1B[0m"
|
|
|
|
#define KMAG "\x1B[35m"
|
|
|
|
#define KCYN "\x1B[36m"
|
|
|
|
|
2016-04-04 19:07:35 -07:00
|
|
|
int getTermSizeRows(void);
|
|
|
|
int getTermSizeCols(void);
|
|
|
|
void clearTermWindow(int, int);
|
2016-04-08 12:34:07 -07:00
|
|
|
char getMaskChar(void);
|
2016-04-04 19:07:35 -07:00
|
|
|
|
|
|
|
int main(void) {
|
2016-04-06 17:28:18 -07:00
|
|
|
struct winpos {
|
|
|
|
char source;
|
2016-04-08 12:34:07 -07:00
|
|
|
char mask;
|
2016-04-06 17:28:18 -07:00
|
|
|
int row;
|
|
|
|
int col;
|
2016-04-10 15:30:44 -07:00
|
|
|
int s1_time;
|
|
|
|
int s2_time;
|
2016-04-06 17:28:18 -07:00
|
|
|
struct winpos *next;
|
|
|
|
};
|
2016-04-07 11:49:47 -07:00
|
|
|
struct winpos *list_pointer = NULL;
|
|
|
|
struct winpos *start; // Always points to start of list
|
|
|
|
struct winpos *temp; // Used for free()ing the list
|
2016-04-04 19:07:35 -07:00
|
|
|
int termSizeRows = getTermSizeRows();
|
|
|
|
int termSizeCols = getTermSizeCols();
|
2016-04-10 12:51:05 -07:00
|
|
|
int c, x = 1, y = 1;
|
2016-04-10 15:30:44 -07:00
|
|
|
int r_time, r_time_l, r_time_s;
|
2016-04-10 12:51:05 -07:00
|
|
|
int ms, ls;
|
|
|
|
bool first = true;
|
2016-04-04 19:07:35 -07:00
|
|
|
|
2016-04-08 12:34:07 -07:00
|
|
|
// Seed my random number generator with the current time
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2016-04-04 19:07:35 -07:00
|
|
|
// Geting input
|
|
|
|
while ((c = getchar()) != EOF) {
|
2016-04-07 11:27:27 -07:00
|
|
|
if (c == NEWLINE) {
|
|
|
|
++y;
|
|
|
|
x = 1;
|
|
|
|
} else if (isspace(c)) {
|
2016-04-11 10:47:53 -07:00
|
|
|
if (++x > termSizeCols) {
|
|
|
|
++y;
|
|
|
|
x = 1;
|
|
|
|
}
|
2016-04-07 11:27:27 -07:00
|
|
|
} else {
|
2016-04-07 11:49:47 -07:00
|
|
|
if (first) {
|
|
|
|
list_pointer = malloc(sizeof(struct winpos));
|
|
|
|
start = list_pointer;
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
list_pointer->next = malloc(sizeof(struct winpos));
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
}
|
2016-04-06 17:28:18 -07:00
|
|
|
|
2016-04-10 15:30:44 -07:00
|
|
|
r_time = rand() % 50;
|
|
|
|
r_time_s = r_time * .25;
|
|
|
|
r_time_l = r_time * .75;
|
|
|
|
r_time *= 100;
|
|
|
|
r_time_s *= 100;
|
|
|
|
r_time_l *= 100;
|
|
|
|
|
2016-04-07 11:27:27 -07:00
|
|
|
list_pointer->source = c;
|
2016-04-08 12:34:07 -07:00
|
|
|
list_pointer->mask = getMaskChar();
|
2016-04-07 11:27:27 -07:00
|
|
|
list_pointer->row = y;
|
|
|
|
list_pointer->col = x;
|
2016-04-10 15:30:44 -07:00
|
|
|
list_pointer->s1_time = r_time > 1000 ? r_time_l : r_time;
|
|
|
|
list_pointer->s2_time = r_time > 1000 ? r_time_s : 0;
|
2016-04-08 12:34:07 -07:00
|
|
|
list_pointer->next = NULL;
|
2016-04-06 17:28:18 -07:00
|
|
|
|
2016-04-11 10:47:53 -07:00
|
|
|
if (++x > termSizeCols) {
|
|
|
|
++y;
|
|
|
|
x = 1;
|
|
|
|
}
|
2016-04-06 17:28:18 -07:00
|
|
|
}
|
2016-04-04 19:07:35 -07:00
|
|
|
}
|
2016-04-04 19:36:00 -07:00
|
|
|
|
2016-04-06 17:28:18 -07:00
|
|
|
clearTermWindow(termSizeRows, termSizeCols);
|
|
|
|
|
2016-04-11 13:14:02 -07:00
|
|
|
// Initially display the characters in the terminal with a 'type effect'.
|
|
|
|
ms = 5; // miliseconds, used for usleep()
|
|
|
|
list_pointer = start;
|
|
|
|
while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
|
|
|
|
printf("\033[%i;%iH%c", list_pointer->row, list_pointer->col, list_pointer->mask);
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
fflush(stdout);
|
|
|
|
usleep(ms * 1000);
|
|
|
|
}
|
2016-04-10 15:43:36 -07:00
|
|
|
|
2016-04-10 12:51:05 -07:00
|
|
|
// TODO: pause with getchar() - something about the input stream being redirected
|
|
|
|
// to a file is causing getchar() to immediately return here.
|
2016-04-11 13:14:02 -07:00
|
|
|
sleep(1);
|
2016-04-10 12:51:05 -07:00
|
|
|
|
|
|
|
// Jumble loop
|
|
|
|
ms = 35; // miliseconds, used for usleep()
|
|
|
|
ls = 2; // loop seconds, number of seconds to loop
|
|
|
|
x = 0;
|
|
|
|
while (x < (ls * 1000) / ms) {
|
|
|
|
list_pointer = start;
|
2016-04-11 10:47:53 -07:00
|
|
|
while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
|
2016-04-10 12:51:05 -07:00
|
|
|
printf("\033[%i;%iH%c", list_pointer->row, list_pointer->col, list_pointer->mask);
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
}
|
2016-04-10 15:52:13 -07:00
|
|
|
fflush(stdout);
|
2016-04-10 12:51:05 -07:00
|
|
|
usleep(ms * 1000);
|
|
|
|
++x;
|
2016-04-06 17:28:18 -07:00
|
|
|
}
|
2016-04-10 12:51:05 -07:00
|
|
|
|
2016-04-04 19:36:00 -07:00
|
|
|
|
2016-04-10 12:51:05 -07:00
|
|
|
// Reveal loop
|
2016-04-10 15:30:44 -07:00
|
|
|
ms = 100; // miliseconds to sleep (must always evenly divisible by 5)
|
|
|
|
int s1_remask_time = 500; // time, in milliseconds, we change the mask for stage 1
|
|
|
|
bool loop = true;
|
|
|
|
while (loop) {
|
|
|
|
loop = false;
|
|
|
|
list_pointer = start;
|
2016-04-11 10:47:53 -07:00
|
|
|
while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
|
2016-04-10 15:30:44 -07:00
|
|
|
if (list_pointer->s1_time > 0) {
|
|
|
|
loop = true;
|
|
|
|
list_pointer->s1_time -= ms;
|
|
|
|
if (list_pointer->s1_time % s1_remask_time == 0) {
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
}
|
|
|
|
} else if (list_pointer->s2_time > 0) {
|
|
|
|
loop = true;
|
|
|
|
list_pointer->s2_time -= ms;
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
} else {
|
|
|
|
list_pointer->mask = list_pointer->source;
|
|
|
|
printf(KCYN);
|
|
|
|
}
|
|
|
|
printf("\033[%i;%iH%c", list_pointer->row, list_pointer->col, list_pointer->mask);
|
|
|
|
printf(KNRM);
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
}
|
2016-04-10 15:52:13 -07:00
|
|
|
fflush(stdout);
|
2016-04-10 15:30:44 -07:00
|
|
|
usleep(ms * 1000);
|
2016-04-10 12:51:05 -07:00
|
|
|
}
|
2016-04-10 15:30:44 -07:00
|
|
|
|
|
|
|
printf("\n");
|
2016-04-10 12:51:05 -07:00
|
|
|
|
2016-04-11 10:47:53 -07:00
|
|
|
// Printing remaining characters from list if we stopped short due to
|
|
|
|
// a terminal row limitation. i.e. the number of textual rows in the input
|
|
|
|
// stream were greater than the number of rows in the terminal.
|
|
|
|
int prevRow;
|
|
|
|
if (list_pointer != NULL) {
|
|
|
|
prevRow = list_pointer->row;
|
|
|
|
printf(KCYN);
|
|
|
|
while (list_pointer != NULL) {
|
|
|
|
while (list_pointer->row > prevRow) {
|
|
|
|
printf("\n");
|
|
|
|
++prevRow;
|
|
|
|
}
|
|
|
|
printf("\033[%i;%iH%c", termSizeRows, list_pointer->col, list_pointer->source);
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
}
|
|
|
|
printf(KNRM);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-07 11:49:47 -07:00
|
|
|
// Freeing the list.
|
|
|
|
list_pointer = start;
|
2016-04-08 12:34:07 -07:00
|
|
|
while (list_pointer != NULL) {
|
2016-04-06 17:28:18 -07:00
|
|
|
temp = list_pointer;
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
|
2016-04-04 19:07:35 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getTermSizeRows(void) {
|
|
|
|
struct winsize w;
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
|
|
|
|
return w.ws_row;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getTermSizeCols(void) {
|
|
|
|
struct winsize w;
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
|
|
|
|
return w.ws_col;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearTermWindow(int pRows, int pCols) {
|
|
|
|
int row, col;
|
|
|
|
|
|
|
|
// Clearing window
|
|
|
|
for (row = 1; row <= pRows; ++row) {
|
|
|
|
for (col = 1; col <= pCols; ++col)
|
|
|
|
printf("\033[%i;%iH%c", row, col, SPACE);
|
|
|
|
printf("\033[%i;%iH%c", row, col, NEWLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position cursor at the top
|
|
|
|
printf("\033[%i;%iH", 1, 1);
|
|
|
|
}
|
2016-04-08 12:34:07 -07:00
|
|
|
|
|
|
|
char getMaskChar(void) {
|
|
|
|
char *maskChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"1234567890";
|
|
|
|
|
|
|
|
return maskChars[rand() % strlen(maskChars)];
|
|
|
|
}
|