Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

186 строки
4.4 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  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 the libcaca DOS/conio.h input and output driver
  16. */
  17. #include "config.h"
  18. #if defined(USE_CONIO)
  19. #include <dos.h>
  20. #include <conio.h>
  21. #if defined(SCREENUPDATE_IN_PC_H)
  22. # include <pc.h>
  23. #endif
  24. #include <stdlib.h>
  25. #include "caca.h"
  26. #include "caca_internals.h"
  27. struct driver_private
  28. {
  29. struct text_info ti;
  30. char *screen;
  31. };
  32. static int conio_init_graphics(caca_display_t *dp)
  33. {
  34. dp->drv.p = malloc(sizeof(struct driver_private));
  35. _wscroll = 0;
  36. _setcursortype(_NOCURSOR);
  37. clrscr();
  38. gettextinfo(&dp->drv.p->ti);
  39. dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
  40. * dp->drv.p->ti.screenheight * sizeof(char));
  41. if(dp->drv.p->screen == NULL)
  42. return -1;
  43. # if defined(SCREENUPDATE_IN_PC_H)
  44. ScreenRetrieve(dp->drv.p->screen);
  45. # else
  46. /* FIXME */
  47. # endif
  48. dp->resize.allow = 1;
  49. caca_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
  50. dp->drv.p->ti.screenheight);
  51. dp->resize.allow = 0;
  52. return 0;
  53. }
  54. static int conio_end_graphics(caca_display_t *dp)
  55. {
  56. _wscroll = 1;
  57. textcolor((enum COLORS)WHITE);
  58. textbackground((enum COLORS)BLACK);
  59. gotoxy(caca_get_canvas_width(dp->cv), caca_get_canvas_height(dp->cv));
  60. cputs("\r\n");
  61. _setcursortype(_NORMALCURSOR);
  62. free(dp->drv.p->screen);
  63. free(dp->drv.p);
  64. return 0;
  65. }
  66. static int conio_set_display_title(caca_display_t *dp, char const *title)
  67. {
  68. return -1;
  69. }
  70. static int conio_get_display_width(caca_display_t const *dp)
  71. {
  72. /* Fallback to a 6x10 font */
  73. return caca_get_canvas_width(dp->cv) * 6;
  74. }
  75. static int conio_get_display_height(caca_display_t const *dp)
  76. {
  77. /* Fallback to a 6x10 font */
  78. return caca_get_canvas_height(dp->cv) * 10;
  79. }
  80. static void conio_display(caca_display_t *dp)
  81. {
  82. char *screen = dp->drv.p->screen;
  83. uint32_t const *chars = (uint32_t const *)caca_get_canvas_chars(dp->cv);
  84. uint32_t const *attrs = (uint32_t const *)caca_get_canvas_attrs(dp->cv);
  85. int width = caca_get_canvas_width(dp->cv);
  86. int height = caca_get_canvas_height(dp->cv);
  87. int n;
  88. for(n = height * width; n--; )
  89. {
  90. char ch = caca_utf32_to_cp437(*chars++);
  91. if(n && *chars == CACA_MAGIC_FULLWIDTH)
  92. {
  93. *screen++ = '[';
  94. *screen++ = caca_attr_to_ansi(*attrs++);
  95. ch = ']';
  96. chars++;
  97. n--;
  98. }
  99. *screen++ = ch;
  100. *screen++ = caca_attr_to_ansi(*attrs++);
  101. }
  102. # if defined(SCREENUPDATE_IN_PC_H)
  103. ScreenUpdate(dp->drv.p->screen);
  104. # else
  105. /* FIXME */
  106. # endif
  107. }
  108. static void conio_handle_resize(caca_display_t *dp)
  109. {
  110. /* We know nothing about our window */
  111. dp->resize.w = caca_get_canvas_width(dp->cv);
  112. dp->resize.h = caca_get_canvas_height(dp->cv);
  113. }
  114. static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
  115. {
  116. uint8_t ch;
  117. caca_privevent_t release;
  118. if(!_conio_kbhit())
  119. {
  120. ev->type = CACA_EVENT_NONE;
  121. return 0;
  122. }
  123. ch = getch();
  124. ev->type = CACA_EVENT_KEY_PRESS;
  125. ev->data.key.ch = ch;
  126. ev->data.key.utf32 = ch;
  127. ev->data.key.utf8[0] = ch;
  128. ev->data.key.utf8[1] = '\0';
  129. release = *ev;
  130. release.type = CACA_EVENT_KEY_RELEASE;
  131. _push_event(dp, &release);
  132. return 1;
  133. }
  134. /*
  135. * Driver initialisation
  136. */
  137. int conio_install(caca_display_t *dp)
  138. {
  139. dp->drv.id = CACA_DRIVER_CONIO;
  140. dp->drv.driver = "conio";
  141. dp->drv.init_graphics = conio_init_graphics;
  142. dp->drv.end_graphics = conio_end_graphics;
  143. dp->drv.set_display_title = conio_set_display_title;
  144. dp->drv.get_display_width = conio_get_display_width;
  145. dp->drv.get_display_height = conio_get_display_height;
  146. dp->drv.display = conio_display;
  147. dp->drv.handle_resize = conio_handle_resize;
  148. dp->drv.get_event = conio_get_event;
  149. dp->drv.set_mouse = NULL;
  150. dp->drv.set_cursor = NULL;
  151. return 0;
  152. }
  153. #endif /* USE_CONIO */