25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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