2016-04-18 09:00:57 -07:00
|
|
|
// Copyright (c) 2016 Brian Barto
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation; either version 3 of the License, or (at your option)
|
|
|
|
// any later version. See COPYING for more details.
|
|
|
|
|
2016-04-12 13:18:53 -07:00
|
|
|
#include "nms.h"
|
2016-04-18 09:00:57 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ncurses.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2016-04-19 15:57:29 -07:00
|
|
|
#define TAB_SIZE 4
|
|
|
|
#define TYPE_EFFECT_SPEED 4 // miliseconds per char
|
|
|
|
#define JUMBLE_SECONDS 2 // number of seconds for jumble effect
|
|
|
|
#define JUMBLE_LOOP_SPEED 35 // miliseconds between each jumble
|
|
|
|
#define REVEAL_LOOP_SPEED 100 // miliseconds (must be evenly divisible by 5)
|
|
|
|
|
2016-04-18 09:00:57 -07:00
|
|
|
#define SPACE 32
|
|
|
|
#define NEWLINE 10
|
|
|
|
#define TAB 9
|
|
|
|
|
|
|
|
// Window position structure, linked list. Keeps track of every
|
|
|
|
// character's position on the terminal, as well as other attributes.
|
|
|
|
struct winpos {
|
|
|
|
char source;
|
|
|
|
char mask;
|
|
|
|
int row;
|
|
|
|
int col;
|
|
|
|
int s1_time;
|
|
|
|
int s2_time;
|
|
|
|
struct winpos *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Function prototypes (internal)
|
|
|
|
char getMaskChar(void);
|
|
|
|
|
|
|
|
/*
|
2016-04-18 15:39:56 -07:00
|
|
|
* void nmsexec(char *)
|
2016-04-18 09:00:57 -07:00
|
|
|
*
|
|
|
|
* DESCR:
|
2016-04-18 15:39:56 -07:00
|
|
|
* Displays the characters stored in the / char * / parameter
|
2016-04-18 09:00:57 -07:00
|
|
|
*
|
|
|
|
*/
|
2016-04-20 09:23:37 -07:00
|
|
|
char nmsexec(char *src) {
|
2016-04-18 09:00:57 -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
|
|
|
|
int termSizeRows, termSizeCols;
|
|
|
|
int c, n, x = 0, y = 0;
|
|
|
|
int r_time, r_time_l, r_time_s;
|
|
|
|
int ms, ls;
|
|
|
|
bool first = true;
|
2016-04-20 09:23:37 -07:00
|
|
|
char ret = 0;
|
2016-04-18 09:00:57 -07:00
|
|
|
|
|
|
|
// Start and initialize curses mode
|
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
scrollok(stdscr, true);
|
|
|
|
|
|
|
|
// Setting up and starting colors if terminal supports them
|
|
|
|
if (has_colors()) {
|
|
|
|
start_color();
|
|
|
|
init_pair(1, COLOR_BLUE, COLOR_BLACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get terminal window size
|
|
|
|
getmaxyx(stdscr, termSizeRows, termSizeCols);
|
|
|
|
|
|
|
|
// Seed my random number generator with the current time
|
|
|
|
srand(time(NULL));
|
2016-04-04 19:07:35 -07:00
|
|
|
|
|
|
|
// Geting input
|
2016-04-18 09:00:57 -07:00
|
|
|
n = 0;
|
2016-04-18 15:39:56 -07:00
|
|
|
while ((c = src[n++]) != '\0') {
|
2016-04-18 09:00:57 -07:00
|
|
|
if (c == NEWLINE) {
|
|
|
|
++y;
|
|
|
|
x = 0;
|
|
|
|
} else if (c == TAB && x + 4 <= termSizeCols) {
|
2016-04-19 15:57:29 -07:00
|
|
|
x += TAB_SIZE;
|
2016-04-18 09:00:57 -07:00
|
|
|
} else if (isspace(c)) {
|
|
|
|
if (++x > termSizeCols) {
|
|
|
|
++y;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
list_pointer->source = c;
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
list_pointer->row = y;
|
|
|
|
list_pointer->col = x;
|
|
|
|
list_pointer->s1_time = r_time > 1000 ? r_time_l : r_time;
|
|
|
|
list_pointer->s2_time = r_time > 1000 ? r_time_s : 0;
|
|
|
|
list_pointer->next = NULL;
|
|
|
|
|
|
|
|
if (++x > termSizeCols) {
|
|
|
|
++y;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initially display the characters in the terminal with a 'type effect'.
|
|
|
|
list_pointer = start;
|
|
|
|
while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
|
|
|
|
mvaddch(list_pointer->row, list_pointer->col, list_pointer->mask);
|
|
|
|
refresh();
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
list_pointer = list_pointer->next;
|
2016-04-19 15:57:29 -07:00
|
|
|
usleep(TYPE_EFFECT_SPEED * 1000);
|
2016-04-06 17:28:18 -07:00
|
|
|
}
|
2016-04-04 19:07:35 -07:00
|
|
|
|
2016-04-19 10:46:58 -07:00
|
|
|
// Reopen stdin for interactive input (keyboard), then require user
|
|
|
|
// to press a key to continue.
|
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
if (!freopen ("/dev/tty", "r", stdin))
|
|
|
|
sleep(1);
|
|
|
|
else
|
|
|
|
getch();
|
2016-04-19 12:05:21 -07:00
|
|
|
else
|
|
|
|
getch();
|
2016-04-18 09:00:57 -07:00
|
|
|
|
|
|
|
// Jumble loop
|
|
|
|
x = 0;
|
2016-04-19 15:57:29 -07:00
|
|
|
while (x < (JUMBLE_SECONDS * 1000) / JUMBLE_LOOP_SPEED) {
|
2016-04-18 09:00:57 -07:00
|
|
|
list_pointer = start;
|
|
|
|
while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
|
|
|
|
mvaddch(list_pointer->row, list_pointer->col, list_pointer->mask);
|
|
|
|
refresh();
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
}
|
2016-04-19 15:57:29 -07:00
|
|
|
usleep(JUMBLE_LOOP_SPEED * 1000);
|
2016-04-18 09:00:57 -07:00
|
|
|
++x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reveal loop
|
|
|
|
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;
|
|
|
|
while (list_pointer != NULL && list_pointer->row <= termSizeRows) {
|
|
|
|
if (list_pointer->s1_time > 0) {
|
|
|
|
loop = true;
|
2016-04-19 15:57:29 -07:00
|
|
|
list_pointer->s1_time -= REVEAL_LOOP_SPEED;
|
2016-04-18 09:00:57 -07:00
|
|
|
if (list_pointer->s1_time % s1_remask_time == 0) {
|
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
}
|
|
|
|
} else if (list_pointer->s2_time > 0) {
|
|
|
|
loop = true;
|
2016-04-19 15:57:29 -07:00
|
|
|
list_pointer->s2_time -= REVEAL_LOOP_SPEED;
|
2016-04-18 09:00:57 -07:00
|
|
|
list_pointer->mask = getMaskChar();
|
|
|
|
} else {
|
|
|
|
list_pointer->mask = list_pointer->source;
|
|
|
|
attron(A_BOLD);
|
|
|
|
if (has_colors())
|
|
|
|
attron(COLOR_PAIR(1));
|
|
|
|
}
|
|
|
|
mvaddch(list_pointer->row, list_pointer->col, list_pointer->mask);
|
|
|
|
refresh();
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
|
|
|
|
attroff(A_BOLD);
|
|
|
|
if (has_colors())
|
|
|
|
attroff(COLOR_PAIR(1));
|
|
|
|
}
|
2016-04-19 15:57:29 -07:00
|
|
|
usleep(REVEAL_LOOP_SPEED * 1000);
|
2016-04-18 09:00:57 -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) {
|
|
|
|
attron(A_BOLD);
|
|
|
|
if (has_colors())
|
|
|
|
attron(COLOR_PAIR(1));
|
|
|
|
prevRow = list_pointer->row;
|
|
|
|
scroll(stdscr);
|
|
|
|
while (list_pointer != NULL) {
|
|
|
|
while (list_pointer->row > prevRow) {
|
|
|
|
scroll(stdscr);
|
|
|
|
++prevRow;
|
|
|
|
}
|
|
|
|
mvaddch(termSizeRows -1, list_pointer->col, list_pointer->source);
|
|
|
|
refresh();
|
|
|
|
//mvaddch(0, 0, list_pointer->source);
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
}
|
|
|
|
attroff(A_BOLD);
|
|
|
|
if (has_colors())
|
|
|
|
attroff(COLOR_PAIR(1));
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:46:58 -07:00
|
|
|
// If stdin is set to the keyboard, user must press a key to continue
|
|
|
|
if (isatty(STDIN_FILENO))
|
2016-04-20 09:23:37 -07:00
|
|
|
ret = getch();
|
2016-04-19 10:46:58 -07:00
|
|
|
else
|
|
|
|
sleep(2);
|
|
|
|
|
2016-04-18 09:00:57 -07:00
|
|
|
// End curses mode
|
|
|
|
endwin();
|
|
|
|
|
|
|
|
// Freeing the list.
|
|
|
|
list_pointer = start;
|
|
|
|
while (list_pointer != NULL) {
|
|
|
|
temp = list_pointer;
|
|
|
|
list_pointer = list_pointer->next;
|
|
|
|
free(temp);
|
|
|
|
}
|
2016-04-20 09:23:37 -07:00
|
|
|
|
|
|
|
return ret;
|
2016-04-18 09:00:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* void getMaskChar(void)
|
|
|
|
*
|
|
|
|
* DESCR:
|
2016-04-18 15:39:56 -07:00
|
|
|
* Returns a random character from the maskChars string.
|
2016-04-18 09:00:57 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
char getMaskChar(void) {
|
|
|
|
char *maskChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"1234567890"
|
|
|
|
"!@#$%^&*()-_=+{}[]:;|\\\"'<>,.?/";
|
|
|
|
|
|
|
|
return maskChars[rand() % strlen(maskChars)];
|
2016-04-04 19:07:35 -07:00
|
|
|
}
|