Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  
  3.     KMatrix: Simulates moving matrix like in famous film.
  4.     Copyright (C) 2021-2022  Vitaliy Krylov
  5.  
  6.     This program is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program.  If not, see <https://www.gnu.org/licenses/>.
  18.  
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h> // Especially for memcpy()
  24. #include <conio.h>
  25. #include <sys/ksys.h>
  26.  
  27. #define bool unsigned char
  28. #define true 1
  29. #define false 0
  30. #define nullptr 0
  31. #define dword unsigned int
  32.  
  33. #define CONSOLE_WIDTH 80
  34. #define CONSOLE_HEIGHT 25
  35.  
  36. enum BUTTONS {
  37.         BTN_QUIT = 1
  38. };
  39.  
  40. #define SYSFN04_FONT_W 8
  41. #define SYSFN04_FONT_H 14
  42.  
  43. static inline void _ksys_set_input_mode(char mode) {
  44.         asm_inline(
  45.                 "int $0x40"
  46.                 ::"a"(66), "b"(1), "c"(mode)
  47.         );
  48. }
  49.  
  50. // Slightly edited implementation from glibc
  51. unsigned int rand_r_seed = 0;
  52. int rand_r() {
  53.         unsigned int *seed = &rand_r_seed;
  54.         unsigned int next = *seed;
  55.         int result;
  56.  
  57.         next *= 1103515245;
  58.         next += 12345;
  59.         result = (unsigned int) (next / 65536) % 2048;
  60.  
  61.         next *= 1103515245;
  62.         next += 12345;
  63.         result <<= 10;
  64.         result ^= (unsigned int) (next / 65536) % 1024;
  65.  
  66.         next *= 1103515245;
  67.         next += 12345;
  68.         result <<= 10;
  69.         result ^= (unsigned int) (next / 65536) % 1024;
  70.  
  71.         *seed = next;
  72.  
  73.         return result;
  74. }
  75. //
  76.  
  77. //<locales>
  78. /*
  79.         STR - string;
  80.         ERR - error;
  81.         INST - instant (must be formatted;  mainly for alloc. errors when it's better not to use concat and e.g.);
  82. */
  83.  
  84. //all:
  85. const char STR_PROGRAM_NAME[] = "KMatrix";
  86.  
  87. //en_US:
  88. const char STR_PROGRAM_MOTD[] = "Matrix for KolibriOS";
  89. const char STR_ERR_INST_CONCAT[] = "[KMatrix] concat(): No free RAM left, string corrupted.\n";
  90. const char STR_ERR_INST_WRITEERRORF[] = "[KMatrix] writeErrorF(): Allocation error... | ";
  91. const char STR_ERR_INST_QUEUE_QALLOCMAIN[] = "[KMatrix] Q_alloc(): Queue allocation failed.\n";
  92. const char STR_ERR_INST_QUEUE_QALLOCDATA[] = "[KMatrix] Q_alloc(): Queue.data allocation failed.\n";
  93. const char STR_ERR_INST_QUEUE_QPUSH[] = "[KMatrix] Q_push(): Data allocation failed.\n";
  94. const char STR_ERR_QUEUE_QPOP[] = "Q_pop(): Wrong use.";
  95. const char STR_ERR_QUEUE_QFRONT[] = "Q_front(): Wrong use.";
  96. const char STR_ERR_CONSOLEINIT[] = "Unable to initialize console.";
  97. const char STR_ERR_INST_MAIN_MROWALLOC[] = "[KMatrix] MatrixRow allocation error.\n";
  98. const char STR_ERR_INST_MAIN_MROWSECALLOC[] = "[KMatrix] MatrixRow->sequence allocation error.\n";
  99. const char STR_ERR_INST_MAIN_TCURALLOC[] = "[KMatrix] Failed to create transparent cursor.\n";
  100. const char STR_ERR_MAIN_TCURLOAD[] = "Failed to load transparent cursor.";
  101. const char STR_MSG_HELP_USAGE[] = "Usage: %s [OPTION]...\n";
  102. const char* STR_MSG_HELP[] = {
  103.         "Simulates moving matrix like in famous film.\n\n",
  104.  
  105.         "\t-F, --foreground\t<COLOR>\tset color of letters;\n",
  106.         "\t\t\tavailable colors: black, red, green, brown,\n",
  107.         "\t\t\tblue, purple, turqoise, white\n",
  108.         "\t\t\tor (available only in graphic mode):\n",
  109.         "\t\t\t<R,G,B>   For example: '-F 255,0,255'\n",
  110.         "\t-B, --background\t<COLOR>\tset color of background;\n",
  111.         "\t\t\tavailable colors: black, red, green, brown,\n",
  112.         "\t\t\tblue, purple, turqoise, white\n",
  113.         "\t\t\tor (available only in graphic mode):\n",
  114.         "\t\t\t<R,G,B>   For example: '-B 255,0,255'\n",
  115.         "\t-H, --highlight\t<COLOR>\tset color of lead symbols;\n",
  116.         "\t\t\tavailable colors: black, red, green, brown,\n",
  117.         "\t\t\tblue, purple, turqoise, white\n",
  118.         "\t\t\tor (available only in graphic mode):\n",
  119.         "\t\t\t<R,G,B>   For example: '-H 255,0,255'\n",
  120.         "\t-S, --speed\t<VALUE>\tset speed of falling letters;\n",
  121.         "\t\t\tit must be an integer and be more than 0;\n",
  122.         "\t\t\tdetermines how many falls will be per second;\n",
  123.         "\t\t\tnotice: formula of waiting is floor(100 / speed),\n",
  124.         "\t\t\tit means that some speed values will give same looking\n",
  125.         "\t@ss\t\trun this program as screensaver (normally you willn't need it)\n",
  126.         "\t--ccopt\t\toptimize fg. color changing (will be faster but could be changed not fully)\n",
  127.         "\t--shell\t\trun this program in shell\n",
  128.         "\t--help\t\tdisplay this help and exit\n",
  129.         "\t--version\t\toutput version information and exit\n\n",
  130.  
  131.         "If program launched in graphical mode, you can do this:\n",
  132.         "\t[1]-[8] for color changing\n",
  133.         "\t[~] -- switch what to change (FG/BG/HL)\n",
  134.         "\t[+]/[-] for speed changing (there is 5 modes)\n",
  135.         "\t[ESC] -- close this program\n\n",
  136.  
  137.         "Defaults when nothing has overriden:\n",
  138.         "\t--foreground green\n",
  139.         "\t--background black\n",
  140.         "\t--highlight white\n",
  141.         "\t--speed 10\n"
  142. };
  143. const char* STR_MSG_VERSION[] = {
  144.         "KMatrix 1.2\n",
  145.         "Copyright (C) 2021-2022 Vitaliy Krylov\n",
  146.         "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n",
  147.         "This is free software: you are free to change and redistribute it.\n",
  148.         "This program comes with ABSOLUTELY NO WARRANTY.\n\n",
  149.  
  150.         "It is in no way affiliated in any way with the movie \"The Matrix\", \"Warner Bros\" nor any of its affiliates in any way.\n\n",
  151.  
  152.         "Written by Vitaliy Krylov.\n"
  153. };
  154. const char STR_MSG_NOARG[] = "%s: option '%s' requires an argument\n";
  155. const char STR_MSG_HELPADVICE[] = "Try '%s --help' for more information.\n";
  156. const char STR_MSG_INVARG_COLOR[] = "%s: invalid color: '%s'\n";
  157. const char STR_MSG_INVARG_SPEED[] = "%s: invalid speed: '%s'\n";
  158. const char STR_MSG_UNRARG[] = "%s: unrecognized option '%s'\n";
  159. const char STR_MSG_CON_RGB[] = "%s: it's not allowed to set RGB-color in console mode\n";
  160. const char STR_MSG_CON_SS[] = "%s: it's not allowed to run in @ss mode in console mode\n";
  161. //</locales>
  162.  
  163. char* STR_PROGRAM_TITLENAME; // Init in main()
  164. const char ss_path[] = "/sys/@ss";
  165.  
  166. // Normally could be output to stderr
  167. void writeError(const char* text) {
  168.         _ksys_debug_puts(text);
  169. }
  170. //Formatted error output
  171. void writeErrorF(const char* text) {
  172.         int t_size = strlen(STR_PROGRAM_NAME)+5+strlen(text); // = 1+strlen(STR_PROGRAM_NAME)+2+strlen(text)+1+1
  173.         char* t = malloc(t_size);
  174.         if (!t) {
  175.                 _ksys_debug_puts(STR_ERR_INST_WRITEERRORF);
  176.                 _ksys_debug_puts(text);
  177.                 _ksys_debug_puts("\n");
  178.                 return;
  179.         }
  180.         t[t_size-1] = '\0';
  181.         sprintf(t, "[%s] %s\n", STR_PROGRAM_NAME, text);
  182.         _ksys_debug_puts(t);
  183.         free(t);
  184. }
  185.  
  186. // Do not forget to free it
  187. // If no RAM left, it will try to return "\0". If not, then 0...
  188. char* concat(const char* s1, const char* s2) {
  189.         const size_t len1 = strlen(s1);
  190.         const size_t len2 = strlen(s2);
  191.         char* res = (char*)malloc(len1 + len2 + 1);
  192.         if (!res) {
  193.                 writeError(STR_ERR_INST_CONCAT);
  194.                 res = (char*)malloc(1);
  195.                 if (res) res[0] = '\0';
  196.         } else {
  197.                 memcpy(res, s1, len1);
  198.                 memcpy(res + len1, s2, len2 + 1);
  199.         }
  200.         return res;
  201. }
  202.  
  203. //<Queue>
  204. typedef struct {
  205.         unsigned int queue_size;
  206.         unsigned int sizeoftype;
  207.         void* data;
  208.         unsigned int data_maxsize;
  209.         unsigned int data_begin;
  210.         unsigned int data_size;
  211. } Queue;
  212.  
  213. //queue_size = 0 if want dynamic size choosing
  214. Queue* Q_alloc(unsigned int sizeoftype, unsigned int queue_size) {
  215.         Queue* this = (Queue*)malloc(sizeof(Queue));
  216.         if (!this) {
  217.                 writeError(STR_ERR_INST_QUEUE_QALLOCMAIN);
  218.                 return nullptr;
  219.         }
  220.         this->sizeoftype = sizeoftype;
  221.         this->data_begin = 0;
  222.         this->data_size = 0;
  223.         if (queue_size) this->queue_size = queue_size;
  224.         else this->queue_size = 0x8000 / sizeoftype;
  225.         this->data_maxsize = this->queue_size;
  226.         this->data = (void*)malloc(this->data_maxsize * sizeoftype);
  227.         if (!this->data) {
  228.                 writeError(STR_ERR_INST_QUEUE_QALLOCDATA);
  229.                 free(this);
  230.                 return nullptr;
  231.         }
  232.         return this;
  233. }
  234.  
  235. void Q_free(Queue* this) {
  236.         free(this->data);
  237.         free(this);
  238. }
  239.  
  240. //$value -- it is a reference to what you want to add
  241. void Q_push(Queue* this, const void* value) {
  242.         if (this->data_size == this->data_maxsize) {
  243.                 void* olddata = this->data;
  244.                 this->data = (void*)malloc((this->data_maxsize + this->queue_size) * this->sizeoftype);
  245.                 if (!this->data) {
  246.                         writeError(STR_ERR_INST_QUEUE_QPUSH);
  247.                         this->data = olddata;
  248.                         return;
  249.                 }
  250.  
  251.                 int data_i = 0;
  252.                 for (int i = this->data_begin; i < this->data_maxsize; ++i) memcpy(this->data + this->sizeoftype * (data_i++), olddata + this->sizeoftype * i, this->sizeoftype);
  253.                 for (int i = 0; i < this->data_begin; ++i) memcpy(this->data + this->sizeoftype * (data_i++), olddata + this->sizeoftype * i, this->sizeoftype);
  254.  
  255.                 free(olddata);
  256.                 this->data_maxsize += this->queue_size;
  257.                 this->data_begin = 0;
  258.         }
  259.         if (this->data_begin + this->data_size < this->data_maxsize) memcpy(this->data + this->sizeoftype * (this->data_begin + this->data_size), value, this->sizeoftype);
  260.         else memcpy(this->data + this->sizeoftype * (this->data_begin + this->data_size - this->data_maxsize), value, this->sizeoftype);
  261.         ++this->data_size;
  262. }
  263.  
  264. void Q_pop(Queue* this, void* returnValue) {
  265.         if (this->data_size > 0) {
  266.                 if (returnValue) memcpy(returnValue, this->data + this->sizeoftype * this->data_begin, this->sizeoftype);
  267.                 ++this->data_begin;
  268.                 if (this->data_begin == this->data_maxsize)
  269.                         this->data_begin = 0;
  270.                 --this->data_size;
  271.         } else writeErrorF(STR_ERR_QUEUE_QPOP);
  272. }
  273.  
  274. //WARNING: Be sure not to pass nullptr
  275. void Q_front(Queue* this, void* returnValue) {
  276.         if (this->data_size > 0) memcpy(returnValue, this->data + this->sizeoftype * this->data_begin, this->sizeoftype);
  277.         else writeErrorF(STR_ERR_QUEUE_QFRONT);
  278. }
  279.  
  280. unsigned int Q_size(Queue* this) {
  281.         return this->data_size;
  282. }
  283.  
  284. //NOTICE: Doesn't free used memory. Will use it for next pushes.
  285. void Q_clear(Queue* this) {
  286.         this->data_begin = 0;
  287.         this->data_size = 0;
  288. }
  289.  
  290. //WARNING: Be sure not to pass nullptr
  291. void Q_at(Queue* this, int i, void* returnValue) {
  292.         if (this->data_begin + i < this->data_maxsize)
  293.                 memcpy(returnValue, this->data + this->sizeoftype * (this->data_begin + i), this->sizeoftype);
  294.         else
  295.                 memcpy(returnValue, this->data + this->sizeoftype * (this->data_begin + i - this->data_maxsize), this->sizeoftype);
  296. }
  297. //</Queue>
  298.  
  299. void consoleInit() {
  300.         if (con_init()) { // Init fail
  301.                 writeErrorF(STR_ERR_CONSOLEINIT);
  302.                 exit(2);
  303.         }
  304.         (*con_set_title)(STR_PROGRAM_TITLENAME);
  305. }
  306.  
  307. void consoleExit() {
  308.         (*con_exit)(0);
  309. }
  310.  
  311. int getColorByID(int id) {
  312.         switch (id) {
  313.                 case 0:
  314.                         return 0x000000;
  315.                 case 1:
  316.                         return 0x800000;
  317.                 case 2:
  318.                         return 0x008000;
  319.                 case 3:
  320.                         return 0x808000;
  321.                 case 4:
  322.                         return 0x000080;
  323.                 case 5:
  324.                         return 0x800080;
  325.                 case 6:
  326.                         return 0x008080;
  327.                 case 7:
  328.                         return 0xc0c0c0;
  329.         }
  330. }
  331.  
  332. //perc should be from 0 to 1
  333. uint32_t getColorBetween(float perc, uint32_t col1, uint32_t col2) {
  334.         uint8_t* col1c = (uint8_t*)&col1;// { a, r, g, b }
  335.         uint8_t* col2c = (uint8_t*)&col2;
  336.         uint32_t res = col1;
  337.         uint8_t* resc = (uint8_t*)&res;
  338.         for (int i = 0; i < 4; ++i) resc[i] += ((int)col2c[i]-(int)col1c[i])*perc;
  339.         return res;
  340. }
  341.  
  342. typedef struct {
  343.         int pos;
  344.         int length;
  345.         int gradLen;
  346.         char* sequence; // if first element is '\0' then its an empty row
  347. } MatrixRow;
  348.  
  349. #include <time.h>
  350.  
  351. int main(int argc, char** argv) {
  352.         char* t = concat(STR_PROGRAM_NAME, " -- ");
  353.         STR_PROGRAM_TITLENAME = concat(t, STR_PROGRAM_MOTD);
  354.         free(t);
  355.  
  356.         int fgcolor = 32, bgcolor = 40, hlcolor = 37, matrixDelay = 10;
  357.         uint32_t fgcolorrgb = 0, bgcolorrgb = 0, hlcolorrgb = 0;
  358.         bool runInShell = false;
  359.         bool ssRun = false;
  360.         bool ccopt = false;
  361.         for (int i = 1; i < argc; ++i) {
  362.                 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
  363.                         consoleInit();
  364.                         printf(STR_MSG_HELP_USAGE, argv[0]);
  365.                         for (int j = 0; j < sizeof(STR_MSG_HELP)/sizeof(STR_MSG_HELP[0]); ++j) puts(STR_MSG_HELP[j]);
  366.                         consoleExit();
  367.                         return 0;
  368.                 } else if (!strcmp(argv[i], "--version")) {
  369.                         consoleInit();
  370.                         for (int j = 0; j < sizeof(STR_MSG_VERSION)/sizeof(STR_MSG_VERSION[0]); ++j) puts(STR_MSG_VERSION[j]);
  371.                         consoleExit();
  372.                         return 0;
  373.                 } else if (!strcmp(argv[i], "--foreground") || !strcmp(argv[i], "-F")) {
  374.                         if (i + 1 < argc) {
  375.                                 if (!strcmp(argv[i+1], "black")) {
  376.                                         fgcolor = 30;
  377.                                 } else if (!strcmp(argv[i+1], "red")) {
  378.                                         fgcolor = 31;
  379.                                 } else if (!strcmp(argv[i+1], "green")) {
  380.                                         fgcolor = 32;
  381.                                 } else if (!strcmp(argv[i+1], "brown")) {
  382.                                         fgcolor = 33;
  383.                                 } else if (!strcmp(argv[i+1], "blue")) {
  384.                                         fgcolor = 34;
  385.                                 } else if (!strcmp(argv[i+1], "purple")) {
  386.                                         fgcolor = 35;
  387.                                 } else if (!strcmp(argv[i+1], "turqoise")) {
  388.                                         fgcolor = 36;
  389.                                 } else if (!strcmp(argv[i+1], "white")) {
  390.                                         fgcolor = 37;
  391.                                 } else {
  392.                                         uint32_t rValue = -1, gValue = -1, bValue = -1;
  393.                                         if (sscanf(argv[i+1], "%d,%d,%d", &rValue, &gValue, &bValue) != 3 ||
  394.                                                         rValue < 0 || rValue > 255 ||
  395.                                                         gValue < 0 || gValue > 255 ||
  396.                                                         bValue < 0 || bValue > 255) {
  397.                                                 consoleInit();
  398.                                                 printf(STR_MSG_INVARG_COLOR, argv[0], argv[i+1]);
  399.                                                 consoleExit();
  400.                                                 return 1;
  401.                                         }
  402.                                         fgcolor = -1;
  403.                                         fgcolorrgb = bValue | (gValue << 8) | (rValue << 16);
  404.                                 }
  405.                                 i += 1;
  406.                         } else {
  407.                                 consoleInit();
  408.                                 printf(STR_MSG_NOARG, argv[0], argv[i]);
  409.                                 printf(STR_MSG_HELPADVICE, argv[0]);
  410.                                 consoleExit();
  411.                                 return 1;
  412.                         }
  413.                 } else if (!strcmp(argv[i], "--background") || !strcmp(argv[i], "-B")) {
  414.                         if (i + 1 < argc) {
  415.                                 if (!strcmp(argv[i+1], "black")) {
  416.                                         bgcolor = 40;
  417.                                 } else if (!strcmp(argv[i+1], "red")) {
  418.                                         bgcolor = 41;
  419.                                 } else if (!strcmp(argv[i+1], "green")) {
  420.                                         bgcolor = 42;
  421.                                 } else if (!strcmp(argv[i+1], "brown")) {
  422.                                         bgcolor = 43;
  423.                                 } else if (!strcmp(argv[i+1], "blue")) {
  424.                                         bgcolor = 44;
  425.                                 } else if (!strcmp(argv[i+1], "purple")) {
  426.                                         bgcolor = 45;
  427.                                 } else if (!strcmp(argv[i+1], "turqoise")) {
  428.                                         bgcolor = 46;
  429.                                 } else if (!strcmp(argv[i+1], "white")) {
  430.                                         bgcolor = 47;
  431.                                 } else {
  432.                                         uint32_t rValue = -1, gValue = -1, bValue = -1;
  433.                                         if (sscanf(argv[i+1], "%d,%d,%d", &rValue, &gValue, &bValue) != 3 ||
  434.                                                         rValue < 0 || rValue > 255 ||
  435.                                                         gValue < 0 || gValue > 255 ||
  436.                                                         bValue < 0 || bValue > 255) {
  437.                                                 consoleInit();
  438.                                                 printf(STR_MSG_INVARG_COLOR, argv[0], argv[i+1]);
  439.                                                 consoleExit();
  440.                                                 return 1;
  441.                                         }
  442.                                         bgcolor = -1;
  443.                                         bgcolorrgb = bValue | (gValue << 8) | (rValue << 16);
  444.                                 }
  445.                                 i += 1;
  446.                         } else {
  447.                                 consoleInit();
  448.                                 printf(STR_MSG_NOARG, argv[0], argv[i]);
  449.                                 printf(STR_MSG_HELPADVICE, argv[0]);
  450.                                 consoleExit();
  451.                                 return 1;
  452.                         }
  453.                 } else if (!strcmp(argv[i], "--highlight") || !strcmp(argv[i], "-H")) {
  454.                         if (i + 1 < argc) {
  455.                                 if (!strcmp(argv[i+1], "black")) {
  456.                                         hlcolor = 30;
  457.                                 } else if (!strcmp(argv[i+1], "red")) {
  458.                                         hlcolor = 31;
  459.                                 } else if (!strcmp(argv[i+1], "green")) {
  460.                                         hlcolor = 32;
  461.                                 } else if (!strcmp(argv[i+1], "brown")) {
  462.                                         hlcolor = 33;
  463.                                 } else if (!strcmp(argv[i+1], "blue")) {
  464.                                         hlcolor = 34;
  465.                                 } else if (!strcmp(argv[i+1], "purple")) {
  466.                                         hlcolor = 35;
  467.                                 } else if (!strcmp(argv[i+1], "turqoise")) {
  468.                                         hlcolor = 36;
  469.                                 } else if (!strcmp(argv[i+1], "white")) {
  470.                                         hlcolor = 37;
  471.                                 } else {
  472.                                         uint32_t rValue = -1, gValue = -1, bValue = -1;
  473.                                         if (sscanf(argv[i+1], "%d,%d,%d", &rValue, &gValue, &bValue) != 3 ||
  474.                                                         rValue < 0 || rValue > 255 ||
  475.                                                         gValue < 0 || gValue > 255 ||
  476.                                                         bValue < 0 || bValue > 255) {
  477.                                                 consoleInit();
  478.                                                 printf(STR_MSG_INVARG_COLOR, argv[0], argv[i+1]);
  479.                                                 consoleExit();
  480.                                                 return 1;
  481.                                         }
  482.                                         hlcolor = -1;
  483.                                         hlcolorrgb = bValue | (gValue << 8) | (rValue << 16);
  484.                                 }
  485.                                 i += 1;
  486.                         } else {
  487.                                 consoleInit();
  488.                                 printf(STR_MSG_NOARG, argv[0], argv[i]);
  489.                                 printf(STR_MSG_HELPADVICE, argv[0]);
  490.                                 consoleExit();
  491.                                 return 1;
  492.                         }
  493.                 } else if (!strcmp(argv[i], "--speed") || !strcmp(argv[i], "-S")) {
  494.                         if (i + 1 < argc) {
  495.                                 int speedValue = 0;
  496.                                 if (!sscanf(argv[i+1], "%d", &speedValue) || speedValue <= 0) {
  497.                                         consoleInit();
  498.                                         printf(STR_MSG_INVARG_SPEED, argv[0], argv[i+1]);
  499.                                         consoleExit();
  500.                                         return 1;
  501.                                 }
  502.                                 matrixDelay = 100 / speedValue;
  503.                                 i += 1;
  504.                         } else {
  505.                                 consoleInit();
  506.                                 printf(STR_MSG_NOARG, argv[0], argv[i]);
  507.                                 printf(STR_MSG_HELPADVICE, argv[0]);
  508.                                 consoleExit();
  509.                                 return 1;
  510.                         }
  511.                 } else if (!strcmp(argv[i], "--shell")) {
  512.                         runInShell = true;
  513.                 } else if (!strcmp(argv[i], "@ss")) {
  514.                         ssRun = true;
  515.                 } else if (!strcmp(argv[i], "--ccopt")) {
  516.                         ccopt = true;
  517.                 } else {
  518.                         consoleInit();
  519.                         printf(STR_MSG_UNRARG, argv[0], argv[i]);
  520.                         printf(STR_MSG_HELPADVICE, argv[0]);
  521.                         consoleExit();
  522.                         return 1;
  523.                 }
  524.         }
  525.  
  526.         if (runInShell && (fgcolor == -1 || bgcolor == -1 || hlcolor == -1)) {
  527.                 consoleInit();
  528.                 printf(STR_MSG_CON_RGB, argv[0]);
  529.                 printf(STR_MSG_HELPADVICE, argv[0]);
  530.                 consoleExit();
  531.                 return 1;
  532.         }
  533.  
  534.         if (runInShell && ssRun) {
  535.                 consoleInit();
  536.                 printf(STR_MSG_CON_SS, argv[0]);
  537.                 printf(STR_MSG_HELPADVICE, argv[0]);
  538.                 consoleExit();
  539.                 return 1;
  540.         }
  541.  
  542.         int bakedTable_size = '~'-'!';
  543.         char bakedTable[bakedTable_size];
  544.         for (char c = '!'; c < '\\'; ++c) bakedTable[c-'!'] = c;
  545.         for (char c = '\\'+1; c <= '~'; ++c) bakedTable[c-'!'-1] = c;
  546.  
  547.         rand_r_seed = time(NULL);
  548.  
  549.         int colorType = 0; // { FG = 0, BG = 1, HL = 2 }
  550.         int delayTypesSize = 5;
  551.         int delayTypes[] = { 33, 22, 10, 4, 1};
  552.         int delayTypeIdx = -1;
  553.  
  554.         // I divide it in two parts because we don't want some additional operations in while(true)   (checkings if it's running in shell)
  555.         if (runInShell) {
  556.                 consoleInit();
  557.                 int initCurHeight = (*con_get_cursor_height)();
  558.                 (*con_set_cursor_height)(0);
  559.                 printf("\033[%d;%dm", fgcolor, bgcolor);
  560.                 for (int i = 0; i < CONSOLE_WIDTH*CONSOLE_HEIGHT; ++i) putc(' ');
  561.                 bool lastWasHighlighted = false;
  562.  
  563.                 Queue* q[CONSOLE_WIDTH/2];
  564.                 for (int x = 0; x < CONSOLE_WIDTH/2; ++x) q[x] = Q_alloc(sizeof(MatrixRow*), 0);
  565.                 MatrixRow* mr;
  566.                 while (true) {
  567.                         for (int x = 0; x < CONSOLE_WIDTH/2; ++x) {
  568.                                 if (Q_size(q[x])) {
  569.                                         Q_at(q[x], 0, &mr);
  570.                                         if (mr->pos + 1 == CONSOLE_HEIGHT) {
  571.                                                 Q_pop(q[x], 0);
  572.                                                 free(mr->sequence);
  573.                                                 free(mr);
  574.                                         }
  575.                                 }
  576.                                 for (int i = 0; i < Q_size(q[x]); ++i) {
  577.                                         Q_at(q[x], i, &mr);
  578.                                         int idx = mr->pos + mr->length - 1;
  579.                                         if (idx < CONSOLE_HEIGHT) {
  580.                                                 (*con_set_cursor_pos)(x*2, idx);
  581.                                                 char tc = (mr->sequence[0] == '\0') ? ' ' : mr->sequence[idx];
  582.                                                 if (!lastWasHighlighted) putc(tc);
  583.                                                 else { printf("\033[%dm%c", fgcolor, tc); lastWasHighlighted = 0; }
  584.                                         }
  585.                                         ++(mr->pos);
  586.                                         idx = mr->pos + mr->length - 1;
  587.                                         if (idx < CONSOLE_HEIGHT) {
  588.                                                 (*con_set_cursor_pos)(x*2, idx);
  589.                                                 char tc = (mr->sequence[0] == '\0') ? ' ' : mr->sequence[idx];
  590.                                                 if (lastWasHighlighted) putc(tc);
  591.                                                 else { printf("\033[%dm%c", hlcolor, tc); lastWasHighlighted = 1; }
  592.                                         }
  593.                                 }
  594.  
  595.                                 bool needToAdd = !Q_size(q[x]);
  596.                                 if (!needToAdd) {
  597.                                         Q_at(q[x], Q_size(q[x]) - 1, &mr);
  598.                                         needToAdd = (mr->pos > 0);
  599.                                 }
  600.                                 if (needToAdd) {
  601.                                         bool mrseq0 = 0;
  602.                                         if (Q_size(q[x])) mrseq0 = (mr->sequence[0] != '\0');
  603.                                         mr = (MatrixRow*)malloc(sizeof(MatrixRow));
  604.                                         if (!mr) {
  605.                                                 writeError(STR_ERR_INST_MAIN_MROWALLOC);
  606.                                                 continue;
  607.                                         }
  608.                                         mr->length = (rand_r() % CONSOLE_HEIGHT) + 1;
  609.                                         mr->pos = 1 - mr->length;
  610.                                         if (!mrseq0 && (rand_r() % 3) == 2) { // 33% chance of being row with symbols
  611.                                                 mr->sequence = (char*)malloc(CONSOLE_HEIGHT);
  612.                                                 if (!mr->sequence) {
  613.                                                         writeError(STR_ERR_INST_MAIN_MROWSECALLOC);
  614.                                                         free(mr);
  615.                                                         continue;
  616.                                                 }
  617.                                                 for (int i = 0; i < CONSOLE_HEIGHT; ++i)
  618.                                                         mr->sequence[i] = bakedTable[rand_r() % bakedTable_size];
  619.                                         } else {
  620.                                                 mr->sequence = (char*)malloc(1);
  621.                                                 if (!mr->sequence) {
  622.                                                         writeError(STR_ERR_INST_MAIN_MROWSECALLOC);
  623.                                                         free(mr);
  624.                                                         continue;
  625.                                                 }
  626.                                                 mr->sequence[0] = '\0';
  627.                                         }
  628.                                         Q_push(q[x], &mr);
  629.                                         (*con_set_cursor_pos)(x*2, 0);
  630.                                         char tc = (mr->sequence[0] == '\0') ? ' ' : mr->sequence[0];
  631.                                         if (lastWasHighlighted) putc(tc);
  632.                                         else { printf("\033[%dm%c", hlcolor, tc); lastWasHighlighted = 1; }
  633.                                 }
  634.                         }
  635.  
  636.                         _ksys_delay(matrixDelay);
  637.                 }
  638.  
  639.                 //I know it will not be executed.. but it better be executed when something sends STOP-like call when there will be better console.obj and others
  640.                 printf("\033[0m"); // Turning BG and FG to normal
  641.                 (*con_set_cursor_height)(initCurHeight);
  642.                 consoleExit();
  643.                 return 0;
  644.         } else {
  645.                 if (fgcolor != -1) fgcolorrgb = getColorByID(fgcolor % 10);
  646.                 if (bgcolor != -1) bgcolorrgb = getColorByID(bgcolor % 10);
  647.                 if (hlcolor != -1) hlcolorrgb = getColorByID(hlcolor % 10);
  648.  
  649.                 ksys_pos_t screenSize = _ksys_screen_size(); // WARNING: If user changes screen resolution while this program is running, here probably could occur bug
  650.                 screenSize.x += 1; screenSize.y += 1; // Yep, that's really needed :/
  651.  
  652.                 if (ssRun) _ksys_set_event_mask(0x27); // Enable mouse events
  653.                 _ksys_set_input_mode(1); // Enable scancode mode
  654.  
  655.                 // Make the cursor be transparent
  656.                 char* transpCur = (char*)calloc(32*32, 1);
  657.                 if (!transpCur) writeError(STR_ERR_INST_MAIN_TCURALLOC);
  658.                 else {
  659.                         uint32_t loadedCurHandle = _ksys_load_cursor(transpCur, 2);
  660.                         free(transpCur);
  661.                         if (!loadedCurHandle) writeErrorF(STR_ERR_MAIN_TCURLOAD);
  662.                         else _ksys_set_cursor(loadedCurHandle);
  663.                 }
  664.  
  665.                 int console_width = screenSize.x / SYSFN04_FONT_W;
  666.                 int console_height = screenSize.y / SYSFN04_FONT_H;
  667.  
  668.                 char ss[2];
  669.                 ss[1] = '\0';
  670.  
  671.                 Queue* q[console_width/2];
  672.                 for (int x = 0; x < console_width/2; ++x) q[x] = Q_alloc(sizeof(MatrixRow*), 0);
  673.                 MatrixRow* mr;
  674.  
  675.                 int event;
  676.                 char needsRedrawing = 0; // { NO = 0, YES = 1, ONLY_TEXT = 2 }
  677.                 while (true) {
  678.                         while ((event = _ksys_check_event()) != KSYS_EVENT_NONE) {
  679.                                 switch (event) {
  680.                                         case KSYS_EVENT_REDRAW:
  681.                                                 _ksys_create_window(0, 0, screenSize.x, screenSize.y, STR_PROGRAM_TITLENAME, bgcolorrgb, 0x31);
  682.                                                 needsRedrawing = 1;
  683.                                                 break;
  684.                                         case KSYS_EVENT_BUTTON:
  685.                                                 switch (_ksys_get_button()) {
  686.                                                         case BTN_QUIT:
  687.                                                                 return 0;
  688.                                                 }
  689.                                                 break;
  690.                                         case KSYS_EVENT_MOUSE:
  691.                                                 if (ssRun) {
  692.                                                         _ksys_exec(ss_path, "");
  693.                                                         return 0;
  694.                                                 }
  695.                                                 break;
  696.                                         case KSYS_EVENT_KEY:
  697.                                                 if (ssRun) {
  698.                                                         _ksys_exec(ss_path, "");
  699.                                                         return 0;
  700.                                                 }
  701.                                                 ksys_oskey_t kc = _ksys_get_key();
  702.                                                 if (kc.code == 1) // [ESC]
  703.                                                         return 0;
  704.                                                 else if (kc.code >= 2 && kc.code <= 9) { // from [1] to [8]
  705.                                                         switch (colorType) {
  706.                                                                 case 0: { // FG
  707.                                                                         fgcolor = 30 + (kc.code - 2);
  708.                                                                         uint32_t oldcolorrgb = fgcolorrgb;
  709.                                                                         fgcolorrgb = getColorByID(kc.code - 2);
  710.                                                                         if (oldcolorrgb != fgcolorrgb) {
  711.                                                                                 if (ccopt) { if (!needsRedrawing) needsRedrawing = 2; }
  712.                                                                                 else needsRedrawing = 1;
  713.                                                                         }
  714.                                                                         break;
  715.                                                                 }
  716.                                                                 case 1: { // BG
  717.                                                                         bgcolor = 40 + (kc.code - 2);
  718.                                                                         uint32_t oldcolorrgb = bgcolorrgb;
  719.                                                                         bgcolorrgb = getColorByID(kc.code - 2);
  720.                                                                         if (oldcolorrgb != bgcolorrgb) needsRedrawing = 1;
  721.                                                                         break;
  722.                                                                 }
  723.                                                                 case 2: // HL
  724.                                                                         hlcolor = 30 + (kc.code - 2);
  725.                                                                         hlcolorrgb = getColorByID(kc.code - 2);
  726.                                                                         break;
  727.                                                         }
  728.                                                 } else if (kc.code == 12 || kc.code == 13) { // [-] or [+]
  729.                                                         if (delayTypeIdx == -1) {
  730.                                                                 delayTypeIdx = 0;
  731.                                                                 while (delayTypeIdx < delayTypesSize && matrixDelay < delayTypes[delayTypeIdx]) ++delayTypeIdx;
  732.                                                                 if (delayTypeIdx == delayTypesSize) --delayTypeIdx;
  733.                                                         }
  734.                                                         if (kc.code == 12) { // Decrease
  735.                                                                 if (delayTypeIdx > 0) --delayTypeIdx;
  736.                                                         } else { // Increase
  737.                                                                 if (delayTypeIdx + 1 < delayTypesSize) ++delayTypeIdx;
  738.                                                         }
  739.                                                         matrixDelay = delayTypes[delayTypeIdx];
  740.                                                 } else if (kc.code == 41) { // [~]
  741.                                                         colorType++;
  742.                                                         if (colorType == 3) colorType = 0;
  743.                                                 }
  744.                                                 break;
  745.                                         default:
  746.                                                 break;
  747.                                 }
  748.                         }
  749.  
  750.                         if (needsRedrawing) {
  751.                                 if (needsRedrawing == 1) _ksys_draw_bar(0, 0, screenSize.x, screenSize.y, bgcolorrgb);
  752.                                 for (int x = 0; x < console_width/2; ++x) {
  753.                                         for (int i = 0; i < Q_size(q[x]); ++i) {
  754.                                                 Q_at(q[x], i, &mr);
  755.                                                 if (mr->sequence[0] != '\0') {
  756.                                                         int y = mr->pos;
  757.                                                         if (y < 0) y = 0;
  758.                                                         for (; y < mr->pos + mr->gradLen && y < console_height; ++y) {
  759.                                                                 ss[0] = mr->sequence[y];
  760.                                                                 _ksys_draw_text(ss, x*2*SYSFN04_FONT_W, y*SYSFN04_FONT_H, 0, 0x90000000 | getColorBetween(((float)y - mr->pos + 1) / (mr->gradLen + 1), bgcolorrgb, fgcolorrgb));
  761.                                                         }
  762.                                                         for (; y < mr->pos + mr->length - 1 && y < console_height; ++y) {
  763.                                                                 ss[0] = mr->sequence[y];
  764.                                                                 _ksys_draw_text(ss, x*2*SYSFN04_FONT_W, y*SYSFN04_FONT_H, 0, 0x90000000 | fgcolorrgb);
  765.                                                         }
  766.                                                         if (y == mr->pos + mr->length - 1 && y < console_height) {
  767.                                                                 ss[0] = mr->sequence[y];
  768.                                                                 _ksys_draw_text(ss, x*2*SYSFN04_FONT_W, y*SYSFN04_FONT_H, 0, 0x90000000 | hlcolorrgb);
  769.                                                         }
  770.                                                 }
  771.                                         }
  772.                                 }
  773.                                 needsRedrawing = 0;
  774.                         }
  775.  
  776.                         for (int x = 0; x < console_width/2; ++x) {
  777.                                 if (Q_size(q[x])) {
  778.                                         Q_at(q[x], 0, &mr);
  779.                                         if (mr->pos + 1 == console_height) {
  780.                                                 Q_pop(q[x], 0);
  781.                                                 free(mr->sequence);
  782.                                                 free(mr);
  783.                                         }
  784.                                 }
  785.                                 for (int i = 0; i < Q_size(q[x]); ++i) {
  786.                                         Q_at(q[x], i, &mr);
  787.                                         ++(mr->pos);
  788.                                         int idx;
  789.                                         if (mr->sequence[0] != '\0') {
  790.                                                 idx = mr->pos;
  791.                                                 if (idx < 0) idx = 0;
  792.                                                 for (; idx < mr->pos + mr->gradLen && idx < console_height; ++idx) {
  793.                                                         if (!ccopt) _ksys_draw_bar(x*2*SYSFN04_FONT_W, idx*SYSFN04_FONT_H, SYSFN04_FONT_W, SYSFN04_FONT_H, bgcolorrgb);
  794.                                                         ss[0] =  mr->sequence[idx];
  795.                                                         _ksys_draw_text(ss, x*2*SYSFN04_FONT_W, idx*SYSFN04_FONT_H, 0, 0x90000000 | getColorBetween(((float)idx - mr->pos + 1) / (mr->gradLen + 1), bgcolorrgb, fgcolorrgb));
  796.                                                 }
  797.                                                 idx = mr->pos + mr->length - 2;
  798.                                                 if (idx < console_height) {
  799.                                                         if (!ccopt) _ksys_draw_bar(x*2*SYSFN04_FONT_W, idx*SYSFN04_FONT_H, SYSFN04_FONT_W, SYSFN04_FONT_H, bgcolorrgb);
  800.                                                         ss[0] =  mr->sequence[idx];
  801.                                                         _ksys_draw_text(ss, x*2*SYSFN04_FONT_W, idx*SYSFN04_FONT_H, 0, 0x90000000 | fgcolorrgb);
  802.                                                 }
  803.                                         }
  804.                                         idx = mr->pos + mr->length - 1;
  805.                                         if (idx < console_height) {
  806.                                                 _ksys_draw_bar(x*2*SYSFN04_FONT_W, idx*SYSFN04_FONT_H, SYSFN04_FONT_W, SYSFN04_FONT_H, bgcolorrgb);
  807.                                                 if (mr->sequence[0] != '\0') {
  808.                                                         ss[0] =  mr->sequence[idx];
  809.                                                         _ksys_draw_text(ss, x*2*SYSFN04_FONT_W, idx*SYSFN04_FONT_H, 0, 0x90000000 | hlcolorrgb);
  810.                                                 }
  811.                                         }
  812.                                 }
  813.  
  814.                                 bool needToAdd = !Q_size(q[x]);
  815.                                 if (!needToAdd) {
  816.                                         Q_at(q[x], Q_size(q[x]) - 1, &mr);
  817.                                         needToAdd = (mr->pos > 0);
  818.                                 }
  819.                                 if (needToAdd) {
  820.                                         bool mrseq0 = 0;
  821.                                         if (Q_size(q[x])) mrseq0 = (mr->sequence[0] != '\0');
  822.                                         mr = (MatrixRow*)malloc(sizeof(MatrixRow));
  823.                                         if (!mr) {
  824.                                                 writeError(STR_ERR_INST_MAIN_MROWALLOC);
  825.                                                 continue;
  826.                                         }
  827.                                         mr->length = (rand_r() % console_height) + 1;
  828.                                         if (mr->length < 3) mr->gradLen = 0; //especially for len=2 (to avoid render twicely)
  829.                                         else                mr->gradLen = rand_r() % (mr->length / 2 + 1); // "mr->length / 2" probably can be changed to "mr->length - mr->length / 3" if you want to encounter maximum 2/3 tinted rows
  830.                                         mr->pos = 1 - mr->length;
  831.                                         if (!mrseq0 && (rand_r() % 3) == 2) { // 33% chance of being row with symbols
  832.                                                 mr->sequence = (char*)malloc(console_height);
  833.                                                 if (!mr->sequence) {
  834.                                                         writeError(STR_ERR_INST_MAIN_MROWSECALLOC);
  835.                                                         free(mr);
  836.                                                         continue;
  837.                                                 }
  838.                                                 for (int i = 0; i < console_height; ++i)
  839.                                                         mr->sequence[i] = bakedTable[rand_r() % bakedTable_size];
  840.                                         } else {
  841.                                                 mr->sequence = (char*)malloc(1);
  842.                                                 if (!mr->sequence) {
  843.                                                         writeError(STR_ERR_INST_MAIN_MROWSECALLOC);
  844.                                                         free(mr);
  845.                                                         continue;
  846.                                                 }
  847.                                                 mr->sequence[0] = '\0';
  848.                                         }
  849.                                         Q_push(q[x], &mr);
  850.                                         _ksys_draw_bar(x*2*SYSFN04_FONT_W, 0, SYSFN04_FONT_W, SYSFN04_FONT_H, bgcolorrgb);
  851.                                         if (mr->sequence[0] != '\0') {
  852.                                                 ss[0] =  mr->sequence[0];
  853.                                                 _ksys_draw_text(ss, x*2*SYSFN04_FONT_W, 0, 0, 0x90000000 | hlcolorrgb);
  854.                                         }
  855.                                 }
  856.                         }
  857.  
  858.                         _ksys_delay(matrixDelay);
  859.                 }
  860.         }
  861.         return 0;
  862. }
  863.