Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

509 righe
9.8 KiB

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