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.
 
 
 
 
 
 

186 lines
4.2 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 "caca.h"
  27. #include "caca_internals.h"
  28. #include "cucul.h"
  29. #include "cucul_internals.h"
  30. struct driver_private
  31. {
  32. struct text_info ti;
  33. char *screen;
  34. };
  35. static int conio_init_graphics(caca_display_t *dp)
  36. {
  37. dp->drv.p = malloc(sizeof(struct driver_private));
  38. _wscroll = 0;
  39. _setcursortype(_NOCURSOR);
  40. clrscr();
  41. gettextinfo(&dp->drv.p->ti);
  42. dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
  43. * dp->drv.p->ti.screenheight * sizeof(char));
  44. if(dp->drv.p->screen == NULL)
  45. return -1;
  46. # if defined(SCREENUPDATE_IN_PC_H)
  47. ScreenRetrieve(dp->drv.p->screen);
  48. # else
  49. /* FIXME */
  50. # endif
  51. dp->resize.allow = 1;
  52. cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
  53. dp->drv.p->ti.screenheight);
  54. dp->resize.allow = 0;
  55. return 0;
  56. }
  57. static int conio_end_graphics(caca_display_t *dp)
  58. {
  59. _wscroll = 1;
  60. textcolor((enum COLORS)WHITE);
  61. textbackground((enum COLORS)BLACK);
  62. gotoxy(dp->cv->width, dp->cv->height);
  63. cputs("\r\n");
  64. _setcursortype(_NORMALCURSOR);
  65. free(dp->drv.p->screen);
  66. free(dp->drv.p);
  67. return 0;
  68. }
  69. static int conio_set_display_title(caca_display_t *dp, char const *title)
  70. {
  71. return -1;
  72. }
  73. static unsigned int conio_get_display_width(caca_display_t const *dp)
  74. {
  75. /* Fallback to a 6x10 font */
  76. return dp->cv->width * 6;
  77. }
  78. static unsigned int conio_get_display_height(caca_display_t const *dp)
  79. {
  80. /* Fallback to a 6x10 font */
  81. return dp->cv->height * 10;
  82. }
  83. static void conio_display(caca_display_t *dp)
  84. {
  85. char *screen = dp->drv.p->screen;
  86. uint32_t *attrs = dp->cv->attrs;
  87. uint32_t *chars = dp->cv->chars;
  88. unsigned int n;
  89. for(n = dp->cv->height * dp->cv->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 = dp->cv->width;
  113. dp->resize.h = dp->cv->height;
  114. }
  115. static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
  116. {
  117. unsigned char 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 = (uint32_t)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.driver = CACA_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 */