Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

571 rader
11 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2012 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What the Fuck You Want
  9. * to Public License, Version 2, as published by Sam Hocevar. See
  10. * http://www.wtfpl.net/ for more details.
  11. */
  12. /*
  13. * This file contains a full conio.h reimplementation. More information
  14. * on conio.h can be found on http://poli.cs.vsb.cz/c/help/conio.htm
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # include <stdio.h>
  19. # include <stdlib.h>
  20. #endif
  21. #include "caca.h"
  22. #include "caca_internals.h"
  23. #include "caca_conio.h"
  24. static caca_canvas_t *cv;
  25. static caca_display_t *dp;
  26. static caca_timer_t refresh_timer = {0, 0};
  27. static uint64_t refresh_ticks;
  28. static int unget_ch = -1;
  29. static int kbhit_ch = -1;
  30. static char pass_buffer[8 + 1];
  31. static void conio_init(void);
  32. static void conio_refresh(void);
  33. static void conio_fini(void);
  34. int caca_conio_directvideo = 0;
  35. int caca_conio__wscroll = 1;
  36. /** \brief DOS conio.h cgets() equivalent */
  37. char * caca_conio_cgets(char *str)
  38. {
  39. int len = ((uint8_t *)str)[0];
  40. int pos = 0;
  41. conio_init();
  42. while (pos < len)
  43. {
  44. int ch = caca_conio_getch();
  45. if (ch == '\n' || ch == '\r')
  46. break;
  47. str[2 + pos] = (char)(uint8_t)ch;
  48. /* FIXME: handle scrolling */
  49. caca_put_char(cv, caca_wherex(cv), caca_wherey(cv), ch);
  50. caca_gotoxy(cv, caca_wherex(cv) + 1, caca_wherey(cv));
  51. pos++;
  52. }
  53. str[2 + pos] = '\0';
  54. str[1] = (char)(uint8_t)pos;
  55. conio_refresh();
  56. return str + 2;
  57. }
  58. /** \brief DOS conio.h clreol() equivalent */
  59. void caca_conio_clreol(void)
  60. {
  61. conio_init();
  62. /* FIXME: must work within the currently active text window */
  63. caca_fill_box(cv, caca_wherex(cv), caca_wherey(cv),
  64. caca_get_canvas_width(cv), caca_wherey(cv), ' ');
  65. conio_refresh();
  66. }
  67. /** \brief DOS conio.h clrscr() equivalent */
  68. void caca_conio_clrscr(void)
  69. {
  70. conio_init();
  71. /* FIXME: must work within the currently active text window */
  72. caca_clear_canvas(cv);
  73. caca_gotoxy(cv, 0, 0);
  74. conio_refresh();
  75. }
  76. /** \brief DOS conio.h cprintf() equivalent */
  77. int caca_conio_cprintf(const char *format, ...)
  78. {
  79. va_list args;
  80. int ret;
  81. conio_init();
  82. /* FIXME: handle scrolling */
  83. va_start(args, format);
  84. ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
  85. va_end(args);
  86. caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv));
  87. conio_refresh();
  88. return ret;
  89. }
  90. /** \brief DOS conio.h cputs() equivalent */
  91. int caca_conio_cputs(const char *str)
  92. {
  93. int ch;
  94. conio_init();
  95. while ((ch = (uint8_t)*str++))
  96. {
  97. /* FIXME: handle windows, scrolling, '\n' and '\r' */
  98. caca_put_char(cv, caca_wherex(cv), caca_wherey(cv), ch);
  99. caca_gotoxy(cv, caca_wherex(cv) + 1, caca_wherey(cv));
  100. }
  101. conio_refresh();
  102. return ch;
  103. }
  104. /** \brief DOS stdio.h cscanf() equivalent */
  105. int caca_conio_cscanf(char *format, ...)
  106. {
  107. conio_init();
  108. /* TODO: implement this function */
  109. return 0;
  110. }
  111. /** \brief DOS dos.h delay() equivalent */
  112. void caca_conio_delay(unsigned int milliseconds)
  113. {
  114. int64_t usec = (int64_t)milliseconds * 1000;
  115. caca_timer_t timer = {0, 0};
  116. conio_init();
  117. _caca_getticks(&timer);
  118. /* Refresh screen as long as we have enough time */
  119. while(usec > 5000)
  120. {
  121. conio_refresh();
  122. _caca_sleep(5000);
  123. usec -= _caca_getticks(&timer);
  124. }
  125. if(usec > 0)
  126. _caca_sleep(usec);
  127. conio_refresh();
  128. }
  129. /** \brief DOS conio.h delline() equivalent */
  130. void caca_conio_delline(void)
  131. {
  132. conio_init();
  133. /* TODO: implement this function */
  134. }
  135. /** \brief DOS conio.h getch() equivalent */
  136. int caca_conio_getch(void)
  137. {
  138. caca_event_t ev;
  139. int ret;
  140. conio_init();
  141. if(unget_ch >= 0)
  142. {
  143. int tmp = unget_ch;
  144. unget_ch = -1;
  145. return tmp;
  146. }
  147. if(kbhit_ch >= 0)
  148. {
  149. int tmp = kbhit_ch;
  150. kbhit_ch = -1;
  151. return tmp;
  152. }
  153. while(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 1000) == 0)
  154. conio_refresh();
  155. ret = caca_get_event_key_ch(&ev);
  156. conio_refresh();
  157. return ret;
  158. }
  159. /** \brief DOS conio.h getche() equivalent */
  160. int caca_conio_getche(void)
  161. {
  162. /* conio_init() is called here. */
  163. int tmp = caca_conio_getch();
  164. /* conio_refresh() is called here. */
  165. caca_conio_printf("%c", tmp);
  166. return tmp;
  167. }
  168. /** \brief DOS conio.h getpass() equivalent */
  169. char * caca_conio_getpass(const char *prompt)
  170. {
  171. int pos = 0;
  172. conio_init();
  173. while (pos < 8)
  174. {
  175. int ch = caca_conio_getch();
  176. if (ch == '\n' || ch == '\r')
  177. break;
  178. pass_buffer[pos] = (char)(uint8_t)ch;
  179. pos++;
  180. }
  181. pass_buffer[pos] = '\0';
  182. conio_refresh();
  183. return pass_buffer;
  184. }
  185. /** \brief DOS conio.h gettext() equivalent */
  186. int caca_conio_gettext(int left, int top, int right, int bottom, void *destin)
  187. {
  188. conio_init();
  189. /* TODO: implement this function */
  190. return 0;
  191. }
  192. /** \brief DOS conio.h gettextinfo() equivalent */
  193. void caca_conio_gettextinfo(struct caca_conio_text_info *r)
  194. {
  195. conio_init();
  196. /* TODO: implement this function */
  197. }
  198. /** \brief DOS conio.h gotoxy() equivalent */
  199. void caca_conio_gotoxy(int x, int y)
  200. {
  201. conio_init();
  202. caca_gotoxy(cv, x - 1, y - 1);
  203. conio_refresh();
  204. }
  205. /** \brief DOS conio.h highvideo() equivalent */
  206. void caca_conio_highvideo(void)
  207. {
  208. conio_init();
  209. /* TODO: implement this function */
  210. }
  211. /** \brief DOS conio.h insline() equivalent */
  212. void caca_conio_insline(void)
  213. {
  214. conio_init();
  215. /* TODO: implement this function */
  216. }
  217. /** \brief DOS conio.h kbhit() equivalent */
  218. int caca_conio_kbhit(void)
  219. {
  220. static caca_timer_t timer = {0, 0};
  221. static int last_failed = 0;
  222. caca_event_t ev;
  223. conio_init();
  224. /* If last call failed and this call is made less than 100µs
  225. * afterwards, we assume the caller is in a busy loop and we
  226. * delay it slightly to avoid resource leakage. */
  227. if(last_failed && _caca_getticks(&timer) < 100)
  228. {
  229. _caca_sleep(1000);
  230. conio_refresh();
  231. }
  232. last_failed = 0;
  233. if(kbhit_ch >= 0)
  234. return 1;
  235. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
  236. {
  237. kbhit_ch = caca_get_event_key_ch(&ev);
  238. return 1;
  239. }
  240. last_failed = 1;
  241. return 0;
  242. }
  243. /** \brief DOS conio.h lowvideo() equivalent */
  244. void caca_conio_lowvideo(void)
  245. {
  246. conio_init();
  247. /* TODO: implement this function */
  248. }
  249. /** \brief DOS conio.h movetext() equivalent */
  250. int caca_conio_movetext(int left, int top, int right, int bottom,
  251. int destleft, int desttop)
  252. {
  253. caca_canvas_t *tmp;
  254. conio_init();
  255. if (left < 1 || top < 1 || left > right || top > bottom
  256. || destleft < 1 || desttop < 1 || destleft > right
  257. || desttop > bottom || right > caca_get_canvas_width(cv)
  258. || bottom > caca_get_canvas_width(cv))
  259. return 0;
  260. tmp = caca_create_canvas(right - left + 1, bottom - top + 1);
  261. caca_blit(tmp, 1 - left, 1 - top, cv, NULL);
  262. caca_blit(cv, destleft - 1, desttop - 1, tmp, NULL);
  263. conio_refresh();
  264. return 1;
  265. }
  266. /** \brief DOS conio.h normvideo() equivalent */
  267. void caca_conio_normvideo(void)
  268. {
  269. conio_init();
  270. /* TODO: implement this function */
  271. }
  272. /** \brief DOS dos.h nosound() equivalent */
  273. void caca_conio_nosound(void)
  274. {
  275. conio_init();
  276. /* TODO: implement this function */
  277. }
  278. /** \brief DOS stdio.h printf() equivalent */
  279. int caca_conio_printf(const char *format, ...)
  280. {
  281. va_list args;
  282. int ret;
  283. conio_init();
  284. va_start(args, format);
  285. ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
  286. va_end(args);
  287. caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv));
  288. conio_refresh();
  289. return 0;
  290. }
  291. /** \brief DOS conio.h putch() equivalent */
  292. int caca_conio_putch(int ch)
  293. {
  294. conio_init();
  295. /* FIXME: handle scrolling, windows */
  296. caca_put_char(cv, caca_wherex(cv), caca_wherey(cv), ch);
  297. caca_gotoxy(cv, caca_wherex(cv) + 1, caca_wherey(cv));
  298. return ch;
  299. }
  300. /** \brief DOS conio.h puttext() equivalent */
  301. int caca_conio_puttext(int left, int top, int right, int bottom, void *destin)
  302. {
  303. conio_init();
  304. /* TODO: implement this function */
  305. return 0;
  306. }
  307. /** \brief DOS conio.h _setcursortype() equivalent */
  308. void caca_conio__setcursortype(int cur_t)
  309. {
  310. conio_init();
  311. switch(cur_t)
  312. {
  313. case CACA_CONIO__NOCURSOR:
  314. caca_set_cursor(dp, 0);
  315. break;
  316. case CACA_CONIO__SOLIDCURSOR:
  317. case CACA_CONIO__NORMALCURSOR:
  318. caca_set_cursor(dp, 1);
  319. break;
  320. }
  321. conio_refresh();
  322. }
  323. /** \brief DOS dos.h sleep() equivalent */
  324. void caca_conio_sleep(unsigned int seconds)
  325. {
  326. int64_t usec = (int64_t)seconds * 1000000;
  327. caca_timer_t timer = {0, 0};
  328. conio_init();
  329. _caca_getticks(&timer);
  330. /* Refresh screen as long as we have enough time */
  331. while(usec > 5000)
  332. {
  333. conio_refresh();
  334. _caca_sleep(5000);
  335. usec -= _caca_getticks(&timer);
  336. }
  337. if(usec > 0)
  338. _caca_sleep(usec);
  339. conio_refresh();
  340. }
  341. /** \brief DOS dos.h sound() equivalent */
  342. void caca_conio_sound(unsigned int frequency)
  343. {
  344. conio_init();
  345. /* TODO: implement this function */
  346. }
  347. /** \brief DOS conio.h textattr() equivalent */
  348. void caca_conio_textattr(int newattr)
  349. {
  350. conio_init();
  351. /* TODO: implement this function */
  352. }
  353. /** \brief DOS conio.h textbackground() equivalent */
  354. void caca_conio_textbackground(int newcolor)
  355. {
  356. conio_init();
  357. caca_set_color_ansi(cv, caca_attr_to_ansi_fg(caca_get_attr(cv, -1, -1)),
  358. newcolor);
  359. }
  360. /** \brief DOS conio.h textcolor() equivalent */
  361. void caca_conio_textcolor(int newcolor)
  362. {
  363. conio_init();
  364. caca_set_color_ansi(cv, newcolor,
  365. caca_attr_to_ansi_bg(caca_get_attr(cv, -1, -1)));
  366. }
  367. /** \brief DOS conio.h textmode() equivalent */
  368. void caca_conio_textmode(int newmode)
  369. {
  370. conio_init();
  371. /* TODO: implement this function */
  372. }
  373. /** \brief DOS conio.h ungetch() equivalent */
  374. int caca_conio_ungetch(int ch)
  375. {
  376. conio_init();
  377. if(unget_ch >= 0)
  378. return EOF;
  379. unget_ch = ch;
  380. return ch;
  381. }
  382. /** \brief DOS conio.h wherex() equivalent */
  383. int caca_conio_wherex(void)
  384. {
  385. conio_init();
  386. return caca_wherex(cv) + 1;
  387. }
  388. /** \brief DOS conio.h wherey() equivalent */
  389. int caca_conio_wherey(void)
  390. {
  391. conio_init();
  392. return caca_wherey(cv) + 1;
  393. }
  394. /** \brief DOS conio.h window() equivalent */
  395. void caca_conio_window(int left, int top, int right, int bottom)
  396. {
  397. conio_init();
  398. /* TODO: implement this function */
  399. }
  400. /* XXX: the following functions are local. */
  401. static void conio_init(void)
  402. {
  403. if(!cv)
  404. cv = caca_create_canvas(80, 25);
  405. if(!dp)
  406. {
  407. dp = caca_create_display(cv);
  408. caca_refresh_display(dp);
  409. caca_set_cursor(dp, 1);
  410. _caca_getticks(&refresh_timer);
  411. refresh_ticks = 0;
  412. #if defined HAVE_ATEXIT
  413. atexit(conio_fini);
  414. #endif
  415. }
  416. }
  417. static void conio_refresh(void)
  418. {
  419. refresh_ticks += _caca_getticks(&refresh_timer);
  420. if(refresh_ticks > 10000)
  421. {
  422. caca_refresh_display(dp);
  423. _caca_getticks(&refresh_timer);
  424. refresh_ticks = 0;
  425. }
  426. }
  427. static void conio_fini(void)
  428. {
  429. caca_free_display(dp);
  430. dp = NULL;
  431. caca_free_canvas(cv);
  432. cv = NULL;
  433. }