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.9 KiB

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