You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

caca_conio.c 7.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 int unget_ch = -1;
  29. static int kbhit_ch = -1;
  30. static char pass_buffer[BUFSIZ];
  31. static char cgets_buffer[BUFSIZ];
  32. static void conio_init(void);
  33. static void conio_refresh(void);
  34. static void conio_fini(void);
  35. /** \brief DOS conio.h cgets() equivalent */
  36. char * caca_conio_cgets(char *str)
  37. {
  38. conio_init();
  39. /* TODO: implement this function */
  40. cgets_buffer[0] = '\0';
  41. return cgets_buffer;
  42. }
  43. /** \brief DOS conio.h clreol() equivalent */
  44. void caca_conio_clreol(void)
  45. {
  46. conio_init();
  47. /* TODO: implement this function */
  48. }
  49. /** \brief DOS conio.h clrscr() equivalent */
  50. void caca_conio_clrscr(void)
  51. {
  52. conio_init();
  53. caca_clear_canvas(cv);
  54. conio_refresh();
  55. }
  56. /** \brief DOS conio.h cprintf() equivalent */
  57. int caca_conio_cprintf(const char *format, ...)
  58. {
  59. va_list args;
  60. int ret;
  61. conio_init();
  62. va_start(args, format);
  63. ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
  64. va_end(args);
  65. caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv));
  66. conio_refresh();
  67. return ret;
  68. }
  69. /** \brief DOS conio.h cputs() equivalent */
  70. int caca_conio_cputs(const char *str)
  71. {
  72. conio_init();
  73. /* TODO: implement this function */
  74. return 0;
  75. }
  76. /** \brief DOS conio.h cscanf() equivalent */
  77. int caca_conio_cscanf(char *format, ...)
  78. {
  79. conio_init();
  80. /* TODO: implement this function */
  81. return 0;
  82. }
  83. /** \brief DOS conio.h delay() equivalent */
  84. void caca_conio_delay(int i)
  85. {
  86. conio_init();
  87. _caca_sleep(i * 1000);
  88. }
  89. /** \brief DOS conio.h delline() equivalent */
  90. void caca_conio_delline(void)
  91. {
  92. conio_init();
  93. /* TODO: implement this function */
  94. }
  95. /** \brief DOS conio.h getch() equivalent */
  96. int caca_conio_getch(void)
  97. {
  98. caca_event_t ev;
  99. conio_init();
  100. if(unget_ch >= 0)
  101. {
  102. int tmp = unget_ch;
  103. unget_ch = -1;
  104. return tmp;
  105. }
  106. if(kbhit_ch >= 0)
  107. {
  108. int tmp = kbhit_ch;
  109. kbhit_ch = -1;
  110. return tmp;
  111. }
  112. caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1);
  113. return caca_get_event_key_ch(&ev);
  114. }
  115. /** \brief DOS conio.h getche() equivalent */
  116. int caca_conio_getche(void)
  117. {
  118. conio_init();
  119. /* TODO: implement this function */
  120. return 0;
  121. }
  122. /** \brief DOS conio.h getpass() equivalent */
  123. char * caca_conio_getpass(const char *prompt)
  124. {
  125. conio_init();
  126. /* TODO: implement this function */
  127. pass_buffer[0] = '\0';
  128. return pass_buffer;
  129. }
  130. /** \brief DOS conio.h gettext() equivalent */
  131. int caca_conio_gettext(int left, int top, int right, int bottom, void *destin)
  132. {
  133. conio_init();
  134. /* TODO: implement this function */
  135. return 0;
  136. }
  137. /** \brief DOS conio.h gettextinfo() equivalent */
  138. void caca_conio_gettextinfo(struct caca_conio_text_info *r)
  139. {
  140. conio_init();
  141. /* TODO: implement this function */
  142. }
  143. /** \brief DOS conio.h gotoxy() equivalent */
  144. void caca_conio_gotoxy(int x, int y)
  145. {
  146. conio_init();
  147. caca_gotoxy(cv, x - 1, y - 1);
  148. conio_refresh();
  149. }
  150. /** \brief DOS conio.h highvideo() equivalent */
  151. void caca_conio_highvideo(void)
  152. {
  153. conio_init();
  154. /* TODO: implement this function */
  155. }
  156. /** \brief DOS conio.h insline() equivalent */
  157. void caca_conio_insline(void)
  158. {
  159. conio_init();
  160. /* TODO: implement this function */
  161. }
  162. /** \brief DOS conio.h kbhit() equivalent */
  163. int caca_conio_kbhit(void)
  164. {
  165. caca_event_t ev;
  166. conio_init();
  167. if(kbhit_ch >= 0)
  168. return 1;
  169. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
  170. {
  171. kbhit_ch = caca_get_event_key_ch(&ev);
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. /** \brief DOS conio.h lowvideo() equivalent */
  177. void caca_conio_lowvideo(void)
  178. {
  179. conio_init();
  180. /* TODO: implement this function */
  181. }
  182. /** \brief DOS conio.h movetext() equivalent */
  183. int caca_conio_movetext(int left, int top, int right, int bottom,
  184. int destleft, int desttop)
  185. {
  186. conio_init();
  187. /* TODO: implement this function */
  188. return 0;
  189. }
  190. /** \brief DOS conio.h normvideo() equivalent */
  191. void caca_conio_normvideo(void)
  192. {
  193. conio_init();
  194. /* TODO: implement this function */
  195. }
  196. /** \brief DOS conio.h nosound() equivalent */
  197. void caca_conio_nosound(void)
  198. {
  199. conio_init();
  200. /* TODO: implement this function */
  201. }
  202. /** \brief DOS stdio.h printf() equivalent */
  203. int caca_conio_printf(const char *format, ...)
  204. {
  205. va_list args;
  206. int ret;
  207. conio_init();
  208. va_start(args, format);
  209. ret = caca_vprintf(cv, caca_wherex(cv), caca_wherey(cv), format, args);
  210. va_end(args);
  211. caca_gotoxy(cv, caca_wherex(cv) + ret, caca_wherey(cv));
  212. conio_refresh();
  213. return 0;
  214. }
  215. /** \brief DOS conio.h putch() equivalent */
  216. int caca_conio_putch(int ch)
  217. {
  218. conio_init();
  219. /* TODO: implement this function */
  220. return 0;
  221. }
  222. /** \brief DOS conio.h puttext() equivalent */
  223. int caca_conio_puttext(int left, int top, int right, int bottom, void *destin)
  224. {
  225. conio_init();
  226. /* TODO: implement this function */
  227. return 0;
  228. }
  229. /** \brief DOS conio.h _setcursortype() equivalent */
  230. void caca_conio__setcursortype(int cur_t)
  231. {
  232. conio_init();
  233. /* TODO: implement this function */
  234. }
  235. /** \brief DOS conio.h sound() equivalent */
  236. void caca_conio_sound(int i)
  237. {
  238. conio_init();
  239. /* TODO: implement this function */
  240. }
  241. /** \brief DOS conio.h textattr() equivalent */
  242. void caca_conio_textattr(int newattr)
  243. {
  244. conio_init();
  245. /* TODO: implement this function */
  246. }
  247. /** \brief DOS conio.h textbackground() equivalent */
  248. void caca_conio_textbackground(int newcolor)
  249. {
  250. conio_init();
  251. caca_set_color_ansi(cv, caca_attr_to_ansi_fg(caca_get_attr(cv, -1, -1)),
  252. newcolor);
  253. }
  254. /** \brief DOS conio.h textcolor() equivalent */
  255. void caca_conio_textcolor(int newcolor)
  256. {
  257. conio_init();
  258. caca_set_color_ansi(cv, newcolor,
  259. caca_attr_to_ansi_bg(caca_get_attr(cv, -1, -1)));
  260. }
  261. /** \brief DOS conio.h textmode() equivalent */
  262. void caca_conio_textmode(int newmode)
  263. {
  264. conio_init();
  265. /* TODO: implement this function */
  266. }
  267. /** \brief DOS conio.h ungetch() equivalent */
  268. int caca_conio_ungetch(int ch)
  269. {
  270. conio_init();
  271. if(unget_ch >= 0)
  272. return EOF;
  273. unget_ch = ch;
  274. return ch;
  275. }
  276. /** \brief DOS conio.h wherex() equivalent */
  277. int caca_conio_wherex(void)
  278. {
  279. conio_init();
  280. return caca_wherex(cv) + 1;
  281. }
  282. /** \brief DOS conio.h wherey() equivalent */
  283. int caca_conio_wherey(void)
  284. {
  285. conio_init();
  286. return caca_wherey(cv) + 1;
  287. }
  288. /** \brief DOS conio.h window() equivalent */
  289. void caca_conio_window(int left, int top, int right, int bottom)
  290. {
  291. conio_init();
  292. /* TODO: implement this function */
  293. }
  294. /* XXX: the following functions are local. */
  295. static void conio_init(void)
  296. {
  297. if(!cv)
  298. cv = caca_create_canvas(80, 25);
  299. if(!dp)
  300. {
  301. dp = caca_create_display(cv);
  302. caca_set_cursor(dp, 1);
  303. #if defined HAVE_ATEXIT
  304. atexit(conio_fini);
  305. #endif
  306. }
  307. }
  308. static void conio_refresh(void)
  309. {
  310. caca_refresh_display(dp);
  311. }
  312. static void conio_fini(void)
  313. {
  314. caca_free_display(dp);
  315. dp = NULL;
  316. caca_free_canvas(cv);
  317. cv = NULL;
  318. }