Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

223 řádky
6.2 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file driver_conio.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief DOS/conio.h driver
  15. *
  16. * This file contains the libcaca DOS/conio.h input and output driver
  17. */
  18. #include "config.h"
  19. #if defined(USE_CONIO)
  20. #include <dos.h>
  21. #include <conio.h>
  22. #if defined(SCREENUPDATE_IN_PC_H)
  23. # include <pc.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include "caca.h"
  27. #include "caca_internals.h"
  28. #include "cucul.h"
  29. #include "cucul_internals.h"
  30. static uint8_t conio_utf32_to_cp437(uint32_t);
  31. struct driver_private
  32. {
  33. struct text_info ti;
  34. char *screen;
  35. };
  36. static int conio_init_graphics(caca_t *kk)
  37. {
  38. kk->drv.p = malloc(sizeof(struct driver_private));
  39. _wscroll = 0;
  40. _setcursortype(_NOCURSOR);
  41. clrscr();
  42. gettextinfo(&kk->drv.p->ti);
  43. kk->drv.p->screen = malloc(2 * kk->drv.p->ti.screenwidth
  44. * kk->drv.p->ti.screenheight * sizeof(char));
  45. if(kk->drv.p->screen == NULL)
  46. return -1;
  47. # if defined(SCREENUPDATE_IN_PC_H)
  48. ScreenRetrieve(kk->drv.p->screen);
  49. # else
  50. /* FIXME */
  51. # endif
  52. _cucul_set_size(kk->qq, kk->drv.p->ti.screenwidth,
  53. kk->drv.p->ti.screenheight);
  54. return 0;
  55. }
  56. static int conio_end_graphics(caca_t *kk)
  57. {
  58. _wscroll = 1;
  59. textcolor((enum COLORS)WHITE);
  60. textbackground((enum COLORS)BLACK);
  61. gotoxy(kk->qq->width, kk->qq->height);
  62. cputs("\r\n");
  63. _setcursortype(_NORMALCURSOR);
  64. free(kk->drv.p->screen);
  65. free(kk->drv.p);
  66. return 0;
  67. }
  68. static int conio_set_window_title(caca_t *kk, char const *title)
  69. {
  70. return 0;
  71. }
  72. static unsigned int conio_get_window_width(caca_t *kk)
  73. {
  74. /* Fallback to a 6x10 font */
  75. return kk->qq->width * 6;
  76. }
  77. static unsigned int conio_get_window_height(caca_t *kk)
  78. {
  79. /* Fallback to a 6x10 font */
  80. return kk->qq->height * 10;
  81. }
  82. static void conio_display(caca_t *kk)
  83. {
  84. char *screen = kk->drv.p->screen;
  85. uint8_t *attr = kk->qq->attr;
  86. uint32_t *chars = kk->qq->chars;
  87. int n;
  88. for(n = kk->qq->height * kk->qq->width; n--; )
  89. {
  90. *screen++ = conio_utf32_to_cp437(*chars++);
  91. *screen++ = *attr++;
  92. }
  93. # if defined(SCREENUPDATE_IN_PC_H)
  94. ScreenUpdate(kk->drv.p->screen);
  95. # else
  96. /* FIXME */
  97. # endif
  98. }
  99. static void conio_handle_resize(caca_t *kk)
  100. {
  101. /* We know nothing about our window */
  102. kk->resize.w = kk->qq->width;
  103. kk->resize.h = kk->qq->height;
  104. }
  105. static unsigned int conio_get_event(caca_t *kk)
  106. {
  107. unsigned int event;
  108. if(!_conio_kbhit())
  109. return CACA_EVENT_NONE;
  110. event = getch();
  111. _push_event(kk, CACA_EVENT_KEY_RELEASE | event);
  112. return CACA_EVENT_KEY_PRESS | event;
  113. }
  114. /*
  115. * XXX: following functions are local
  116. */
  117. static uint8_t conio_utf32_to_cp437(uint32_t c)
  118. {
  119. static uint32_t const lookup1[] =
  120. {
  121. /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
  122. 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
  123. 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
  124. /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
  125. 0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
  126. 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
  127. };
  128. static uint32_t const lookup2[] =
  129. {
  130. /* 0x7f: ⌂ */
  131. 0x2302,
  132. /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
  133. 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
  134. 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
  135. /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
  136. 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
  137. 0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
  138. /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
  139. 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
  140. 0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
  141. /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
  142. 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
  143. 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
  144. /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
  145. 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
  146. 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
  147. /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
  148. 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
  149. 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
  150. /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
  151. 0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
  152. 0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
  153. /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
  154. 0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
  155. 0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
  156. };
  157. unsigned int i;
  158. if(c < 0x00000020)
  159. return '?';
  160. if(c < 0x00000080)
  161. return c;
  162. for(i = 0; i < sizeof(lookup1) / sizeof(*lookup1); i++)
  163. if(lookup1[i] == c)
  164. return 0x01 + i;
  165. for(i = 0; i < sizeof(lookup2) / sizeof(*lookup2); i++)
  166. if(lookup2[i] == c)
  167. return 0x7f + i;
  168. return '?';
  169. }
  170. /*
  171. * Driver initialisation
  172. */
  173. void conio_init_driver(caca_t *kk)
  174. {
  175. kk->drv.driver = CACA_DRIVER_CONIO;
  176. kk->drv.init_graphics = conio_init_graphics;
  177. kk->drv.end_graphics = conio_end_graphics;
  178. kk->drv.set_window_title = conio_set_window_title;
  179. kk->drv.get_window_width = conio_get_window_width;
  180. kk->drv.get_window_height = conio_get_window_height;
  181. kk->drv.display = conio_display;
  182. kk->drv.handle_resize = conio_handle_resize;
  183. kk->drv.get_event = conio_get_event;
  184. }
  185. #endif /* USE_CONIO */