/* * matrix_ncurses.c — Matrix "digital rain" screensaver for terminal (UTF‑8) * * Build (Ubuntu): * sudo apt-get install build-essential libncursesw5-dev * gcc -O2 -Wall -Wextra -std=c11 matrix_ncurses.c -o matrix -lncursesw * * Run: * ./matrix # default settings * ./matrix --speed 1.2 --density 0.35 --bold --no-fade * ./matrix --help * * Notes: * - Uses wide characters (ncursesw) and UTF‑8 katakana set. * - Handles terminal resize at runtime. * - Press 'q' or ESC to quit. Press 'p' to pause/resume. */ #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include #ifndef ARRAY_SIZE #define ARRAY_SIZE(a) (int)(sizeof(a)/sizeof((a)[0])) #endif // Random helper static inline uint32_t rnd32(void){ static uint64_t s = 0; if(!s){ s = ((uint64_t)time(NULL) ^ (uintptr_t)&s) | 1ULL; } s ^= s << 7; s ^= s >> 9; s ^= s << 8; // xorshift-like return (uint32_t)s; } static inline int irand(int n){ return (int)(rnd32() % (uint32_t)n); } static inline double drand(void){ return (rnd32() / (double)UINT32_MAX); } // Katakana (and a few Latin) characters for the rain static const wchar_t KATAKANA[] = { L'ヲ',L'ァ',L'ィ',L'ゥ',L'ェ',L'ォ',L'ャ',L'ュ',L'ョ',L'ッ',L'ア',L'イ',L'ウ',L'エ',L'オ', L'カ',L'キ',L'ク',L'ケ',L'コ',L'サ',L'シ',L'ス',L'セ',L'ソ',L'タ',L'チ',L'ツ',L'テ',L'ト', L'ナ',L'ニ',L'ヌ',L'ネ',L'ノ',L'ハ',L'ヒ',L'フ',L'ヘ',L'ホ',L'マ',L'ミ',L'ム',L'メ',L'モ', L'ヤ',L'ユ',L'ヨ',L'ラ',L'リ',L'ル',L'レ',L'ロ',L'ワ',L'ン',L'0',L'1',L'2',L'3',L'4',L'5',L'6',L'7',L'8',L'9' }; typedef struct { int head_y; // current head row (can be negative before entering) int tail; // tail length in rows double speed; // rows per frame bool active; // whether this column is currently raining } Column; static void usage(const char *prog){ fprintf(stderr, "Usage: %s [--speed f] [--density 0..1] [--bold] [--no-fade] [--fps N]\n", prog); } int main(int argc, char **argv){ // Defaults double speed_mul = 1.0; // global speed multiplier double density = 0.25; // fraction of columns active bool bold_head = false; bool fade_trail = true; int target_fps = 60; // Parse args (very lightweight) for(int i=1;i1) density=1; } else if(!strcmp(argv[i],"--bold")){ bold_head = true; } else if(!strcmp(argv[i],"--no-fade")){ fade_trail = false; } else if(!strcmp(argv[i],"--fps") && i+1240) target_fps=240; } else if(!strcmp(argv[i],"--help")){ usage(argv[0]); return 0; } else { usage(argv[0]); return 1; } } setlocale(LC_ALL, ""); // enable UTF‑8/wide chars initscr(); start_color(); use_default_colors(); noecho(); curs_set(0); keypad(stdscr, TRUE); nodelay(stdscr, TRUE); // Define color pairs: 1 = dim green, 2 = bright green, 3 = white head init_pair(1, COLOR_GREEN, -1); init_pair(2, COLOR_GREEN, -1); init_pair(3, COLOR_WHITE, -1); int maxy, maxx; getmaxyx(stdscr, maxy, maxx); Column *cols = calloc((size_t)maxx, sizeof(Column)); if(!cols){ endwin(); fprintf(stderr, "OOM\n"); return 1; } // Initialize columns for(int x=0;x maxy){ // reset column when tail fully off-screen cols[x].active = drand() < density; // maybe go idle cols[x].head_y = -irand(maxy); cols[x].tail = 5 + irand(20); cols[x].speed = (0.4 + drand()*1.2) * speed_mul; } } else if(drand() < density/200.0){ // randomly start new rain cols[x].active = true; cols[x].head_y = -irand(maxy); cols[x].tail = 5 + irand(20); cols[x].speed = (0.4 + drand()*1.2) * speed_mul; } } // Draw // Fade entire screen slightly (simple approach using spaces + A_DIM) if(fade_trail){ attron(COLOR_PAIR(1)); attron(A_DIM); for(int y=0;y=maxy) continue; wchar_t wc = KATAKANA[irand(ARRAY_SIZE(KATAKANA))]; if(y==head){ // Head: bright if(bold_head) attron(A_BOLD); else attroff(A_BOLD); attron(COLOR_PAIR(3)); mvaddnwstr(y, x, &wc, 1); attroff(COLOR_PAIR(3)); if(bold_head) attroff(A_BOLD); } else { // Trail: normal green, dim if close to tail start int dist = head - y; if(dist > cols[x].tail-2) attron(A_DIM); else attroff(A_DIM); attron(COLOR_PAIR(2)); mvaddnwstr(y, x, &wc, 1); attroff(COLOR_PAIR(2)); if(dist > cols[x].tail-2) attroff(A_DIM); } } } } // Present refresh(); nanosleep(&ts, NULL); } endwin(); free(cols); return 0; }