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

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