render.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <unistd.h> // usleep
  6. #include "render.h"
  7. #include "terminal.h"
  8. #include "zf_log.h"
  9. #include "utils.h"
  10. extern struct console_details console;
  11. struct render current_render;
  12. int render_overlimit = 0;
  13. void reset_render(void) {
  14. current_render.speed = 0;
  15. current_render.effect = 0;
  16. render_overlimit = 0;
  17. }
  18. int ms_sleep(unsigned int ms) {
  19. int result = 0;
  20. struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L};
  21. do {
  22. struct timespec ts_sleep = ts;
  23. result = nanosleep(&ts_sleep, &ts);
  24. } while ((-1 == result));
  25. return result;
  26. }
  27. void render_sleep(void) {
  28. if (render_overlimit)
  29. return;
  30. if (current_render.speed) { // * 100 still too slow.
  31. ms_sleep(current_render.speed * 10);
  32. }
  33. }
  34. /*
  35. Well SNAP! Mystic numbers don't remotely match ANSI color codes.
  36. 00 : Sets the current foreground to Black 0;30
  37. 01 : Sets the current foreground to Dark Blue 0;34
  38. 02 : Sets the current foreground to Dark Green 0;32
  39. 03 : Sets the current foreground to Dark Cyan 0;36
  40. 04 : Sets the current foreground to Dark Red 0;31
  41. 05 : Sets the current foreground to Dark Magenta 0;35
  42. 06 : Sets the current foreground to Brown 0;33
  43. 07 : Sets the current foreground to Grey 0;37
  44. 08 : Sets the current foreground to Dark Grey 1;30
  45. 09 : Sets the current foreground to Light Blue 1;34
  46. 10 : Sets the current foreground to Light Green 1;32
  47. 11 : Sets the current foreground to Light Cyan 1;36
  48. 12 : Sets the current foreground to Light Red 1;31
  49. 13 : Sets the current foreground to Light Magenta 1;35
  50. 14 : Sets the current foreground to Yellow 1;33
  51. 15 : Sets the current foreground to White 1;37
  52. 16 : Sets the current background to Black 40
  53. 17 : Sets the current background to Blue 44
  54. 18 : Sets the current background to Green 42
  55. 19 : Sets the current background to Cyan 46
  56. 20 : Sets the current background to Red 41
  57. 21 : Sets the current background to Magenta 45
  58. 22 : Sets the current background to Brown 43
  59. 23 : Sets the current background to Grey 47
  60. 24 : Sets the current background to black with blinking foreground 5;40
  61. 25 : Sets the current background to blue with blinking foreground 5;44
  62. 26 : Sets the current background to green with blinking foreground 5;42
  63. 27 : Sets the current background to cyan with blinking foreground 5;46
  64. 28 : Sets the current background to red with blinking foreground 5;41
  65. 29 : Sets the current background to magenta with blinking foreground 5;45
  66. 30 : Sets the current background to brown with blinking foreground 5;43
  67. 31 : Sets the current background to grey with blinking foreground 5;47
  68. Other things that Mystic does ...
  69. [A## - Move the cursor up ## lines
  70. [B## - Move the cursor down ## lines
  71. [C## - Move the cursor forward (to the right) ## columns
  72. [D## - Move the cursor backwards (to the left) ## columns
  73. [K - Clear from the current cursor position to the end of the line
  74. [L - Move cursor and erase data backwards from current column to column ##
  75. [X## - Move cursor to X coordinate ##
  76. [Y## - Move cursor to Y coordinate ##
  77. BS - Sends 1 destructive backspace sequence (ASCII 8-32-8)
  78. CL - Clears the screen (ANSI 1,1 locate and [2J or ASCII 12)
  79. CR - Send a carrage return and line feed (move to next line)
  80. RA - Restore the saved text attribute color
  81. RS - Restore the saved user's terminal screen
  82. SA - Save the current text attribute color
  83. SS - Save the entire user's terminal screen
  84. */
  85. // Covert MYSTIC color to (Proper) ANSI COLOR.
  86. const int MYSTIC[] = {0, 4, 2, 6, 1, 5, 3, 7};
  87. // ANSI_color = MYSTIC[ odd_mystic_color % 8 ]
  88. void write_color(int fd, int color) {
  89. char buffer[12];
  90. switch (color) {
  91. case 0:
  92. case 1:
  93. case 2:
  94. case 3:
  95. case 4:
  96. case 5:
  97. case 6:
  98. case 7:
  99. sprintf(buffer, "\x1b[0;3%dm", MYSTIC[color]);
  100. break;
  101. case 8:
  102. case 9:
  103. case 10:
  104. case 11:
  105. case 12:
  106. case 13:
  107. case 14:
  108. case 15:
  109. sprintf(buffer, "\x1b[0;1;3%dm", MYSTIC[color - 8]);
  110. break;
  111. case 16:
  112. case 17:
  113. case 18:
  114. case 19:
  115. case 20:
  116. case 21:
  117. case 22:
  118. case 23:
  119. sprintf(buffer, "\x1b[4%dm", MYSTIC[color - 16]);
  120. break;
  121. case 24:
  122. case 25:
  123. case 26:
  124. case 27:
  125. case 28:
  126. case 29:
  127. case 30:
  128. case 31:
  129. sprintf(buffer, "\x1b[5;4%dm", MYSTIC[color - 24]);
  130. break;
  131. default:
  132. buffer[0] = 0;
  133. break;
  134. }
  135. ZF_LOGD("write_color( %d ): %s", color, repr(buffer));
  136. write(fd, buffer, strlen(buffer));
  137. }
  138. int send_file(int fd, char *filename) {
  139. FILE *fp;
  140. char buffer[100];
  141. int read;
  142. fp = fopen(filename, "rb");
  143. if (fp == NULL) {
  144. ZF_LOGD("Failed to open %s", filename);
  145. return 0;
  146. }
  147. while ((read = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  148. write(fd, buffer, read);
  149. };
  150. fclose(fp);
  151. return 1;
  152. }
  153. void send_goto(int fd, int x, int y) {
  154. char gbuffer[16];
  155. sprintf(gbuffer, "\x1b[%d;%dH", y, x);
  156. write(fd, gbuffer, strlen(gbuffer));
  157. }
  158. int send_file(int fd, int x, int y, char *filename) {
  159. FILE *fp;
  160. char buffer[100];
  161. int read;
  162. fp = fopen(filename, "rb");
  163. if (fp == NULL) {
  164. ZF_LOGD("Failed to open %s", filename);
  165. return 0;
  166. }
  167. send_goto(fd, x, y);
  168. y++;
  169. while ((read = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
  170. char *cp, *last_cp;
  171. buffer[read] = 0;
  172. last_cp = buffer;
  173. while ((cp = strchr(last_cp, '\n')) != NULL) {
  174. *cp = 0;
  175. write(fd, last_cp, strlen(last_cp));
  176. send_goto(fd, x, y);
  177. y++;
  178. last_cp = cp + 1;
  179. };
  180. write(fd, last_cp, strlen(last_cp));
  181. };
  182. fclose(fp);
  183. return 1;
  184. }
  185. /**
  186. * process_trigger( fd, *cp )
  187. *
  188. * This process a command trigger.
  189. * It has seen TRIGGER, and now it is
  190. * processing whatever comes after it.
  191. * It will perform the process, and
  192. * return the char * of whatever is next.
  193. */
  194. const char *process_trigger(int fd, const char *cp) {
  195. char ch;
  196. int i, x, y;
  197. ch = *cp;
  198. cp++;
  199. switch (ch) {
  200. case 'D':
  201. i = 0;
  202. if (isdigit(*cp)) {
  203. i = (*cp) - '0';
  204. cp++;
  205. };
  206. if (isdigit(*cp)) {
  207. i *= 10;
  208. i += (*cp) - '0';
  209. cp++;
  210. };
  211. if ((i > 0) && (i < 80)) {
  212. ZF_LOGI("DEL %02d", i);
  213. for (x = 0; x < i; x++) {
  214. write(fd, "\b \b", 3);
  215. }
  216. };
  217. break;
  218. case 'C': {
  219. i = 0;
  220. if (*cp == 'R') {
  221. cp++;
  222. const char *restore = color_restore(&console);
  223. write(fd, restore, strlen(restore));
  224. break;
  225. }
  226. if (isdigit(*cp)) {
  227. i = (*cp) - '0';
  228. cp++;
  229. };
  230. if (isdigit(*cp)) {
  231. i *= 10;
  232. i += (*cp) - '0';
  233. cp++;
  234. };
  235. write_color(fd, i);
  236. } break;
  237. case 'F':
  238. case 'f': {
  239. int pos = 0;
  240. if (ch == 'f') {
  241. pos = 1;
  242. x = (*cp) - '0';
  243. cp++;
  244. x *= 10;
  245. x += (*cp) - '0';
  246. cp++;
  247. y = (*cp) - '0';
  248. cp++;
  249. y *= 10;
  250. y += (*cp) - '0';
  251. cp++;
  252. ZF_LOGI("file at (%d, %d)", x, y);
  253. }
  254. // Ok, look for filename
  255. char ansifile[32] = "./hh/";
  256. char *ap = ansifile + strlen(ansifile);
  257. while (*cp != '.') {
  258. *ap = *cp;
  259. ap++;
  260. *ap = 0;
  261. cp++;
  262. };
  263. strcat(ansifile, ".ans");
  264. cp++;
  265. ZF_LOGD("FILE [%s]", ansifile);
  266. if (pos) {
  267. send_file(fd, x, y, ansifile);
  268. } else {
  269. send_file(fd, ansifile);
  270. };
  271. } break;
  272. case 'G': {
  273. x = 0;
  274. if (isdigit(*cp)) {
  275. x = (*cp) - '0';
  276. cp++;
  277. };
  278. if (isdigit(*cp)) {
  279. x *= 10;
  280. x += (*cp) - '0';
  281. cp++;
  282. };
  283. y = 0;
  284. if (isdigit(*cp)) {
  285. y = (*cp) - '0';
  286. cp++;
  287. };
  288. if (isdigit(*cp)) {
  289. y *= 10;
  290. y += (*cp) - '0';
  291. cp++;
  292. };
  293. char buffer[20]; // row ; column H
  294. ZF_LOGD("GOTO (%d,%d)", x, y);
  295. sprintf(buffer, "\x1b[%d;%dH", y, x);
  296. write(fd, buffer, strlen(buffer));
  297. } break;
  298. case 'R': {
  299. i = 0;
  300. if (isdigit(*cp)) {
  301. i = (*cp) - '0';
  302. cp++;
  303. };
  304. if ((i > 0) && (i < 10)) {
  305. ZF_LOGI("RENDER %d", i);
  306. current_render.effect = i;
  307. } else {
  308. current_render.effect = 0;
  309. }
  310. } break;
  311. case 'S': {
  312. i = 0;
  313. if (isdigit(*cp)) {
  314. i = (*cp) - '0';
  315. cp++;
  316. };
  317. if ((i > 0) && (i < 10)) {
  318. ZF_LOGI("SPEED %d", i);
  319. current_render.speed = i;
  320. } else {
  321. current_render.speed = 0;
  322. }
  323. } break;
  324. case 'P': {
  325. i = 0;
  326. if (isdigit(*cp)) {
  327. i = (*cp) - '0';
  328. cp++;
  329. };
  330. if ((i > 0) && (i < 10)) {
  331. ZF_LOGI("PAWS %d", i);
  332. // sleep(i);
  333. if (!render_overlimit) {
  334. sleep(i);
  335. };
  336. }
  337. } break;
  338. }
  339. return cp;
  340. }
  341. /**
  342. * render_effect( fd, ch )
  343. *
  344. * Displays the given character with whatever
  345. * rendering effect is currently active.
  346. * (If any).
  347. */
  348. void render_effect(int fd, char ch) {
  349. int effect = current_render.effect;
  350. int l;
  351. char space = ' ';
  352. char bs = '\b';
  353. switch (effect) {
  354. case 1:
  355. // CHAR + SPC + BS
  356. render_sleep();
  357. write(fd, &ch, 1);
  358. render_sleep();
  359. write(fd, &space, 1);
  360. render_sleep();
  361. render_sleep();
  362. write(fd, &bs, 1);
  363. break;
  364. case 2:
  365. // CHAR + 8 spaces + 8 BS
  366. render_sleep();
  367. write(fd, &ch, 1);
  368. for (l = 0; l < 8; l++) {
  369. render_sleep();
  370. write(fd, &space, 1);
  371. }
  372. for (l = 0; l < 8; l++) {
  373. render_sleep();
  374. write(fd, &bs, 1);
  375. }
  376. break;
  377. case 0:
  378. default:
  379. // NORMAL
  380. render_sleep();
  381. write(fd, &ch, 1);
  382. break;
  383. }
  384. }
  385. /**
  386. * render( fd, string_out )
  387. *
  388. * Render an entire string.
  389. * Handles TRIGGER.
  390. * Renders with effects.
  391. */
  392. void render(int fd, const char *string_out, int len) {
  393. const char *cp = string_out;
  394. const char *trigger = cp;
  395. time_t start = time(NULL);
  396. int elapsed;
  397. int over = 0;
  398. reset_render();
  399. // ZF_LOGV("render(%d, %s)", fd, repr(string_out));
  400. ZF_LOGV_MEM(string_out, len, "render(%d, %d bytes):", fd, len);
  401. // Check our time from time to time.
  402. // If we start running long, disable sleeps.
  403. while ((trigger = strnstr(cp, len - (cp - string_out), TRIGGER)) != NULL) {
  404. // There is special things to handle in here.
  405. while (cp != trigger) {
  406. elapsed = time(NULL) - start;
  407. if (elapsed > SLEEP_LIMIT) {
  408. render_overlimit = 1;
  409. current_render.speed = 0;
  410. };
  411. // write(fd, cp, 1 );
  412. render_effect(fd, *cp);
  413. cp++;
  414. };
  415. ZF_LOGI("at trigger: (%s)", cp);
  416. cp += strlen(TRIGGER);
  417. // Ok, we're pointing at the trigger -- do something.
  418. cp = process_trigger(fd, cp);
  419. ZF_LOGI("after trigger: (%s)", cp);
  420. };
  421. ZF_LOGI("no more triggers, finish it up: (%s)", cp);
  422. // We still might be under a rendering effect.
  423. while (cp < (string_out + len)) {
  424. elapsed = time(NULL) - start;
  425. if (elapsed > SLEEP_LIMIT) {
  426. render_overlimit = 1;
  427. current_render.speed = 0;
  428. };
  429. // write(fd, cp, 1);
  430. render_effect(fd, *cp);
  431. cp++;
  432. }
  433. }