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