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.

преди 18 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 */