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.
 
 
 
 
 
 

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