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

driver_conio.c 4.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains the libcaca DOS/conio.h input and output driver
  15. */
  16. #include "config.h"
  17. #include "common.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. #include "cucul.h"
  28. #include "cucul_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. _cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
  51. dp->drv.p->ti.screenheight);
  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(dp->cv->width, dp->cv->height);
  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 unsigned int conio_get_display_width(caca_display_t *dp)
  71. {
  72. /* Fallback to a 6x10 font */
  73. return dp->cv->width * 6;
  74. }
  75. static unsigned int conio_get_display_height(caca_display_t *dp)
  76. {
  77. /* Fallback to a 6x10 font */
  78. return dp->cv->height * 10;
  79. }
  80. static void conio_display(caca_display_t *dp)
  81. {
  82. char *screen = dp->drv.p->screen;
  83. uint32_t *attrs = dp->cv->attrs;
  84. uint32_t *chars = dp->cv->chars;
  85. unsigned int n;
  86. for(n = dp->cv->height * dp->cv->width; n--; )
  87. {
  88. char ch = cucul_utf32_to_cp437(*chars++);
  89. if(n && *chars == CUCUL_MAGIC_FULLWIDTH)
  90. {
  91. *screen++ = '[';
  92. *screen++ = cucul_attr_to_ansi(*attrs++);
  93. ch = ']';
  94. chars++;
  95. n--;
  96. }
  97. *screen++ = ch;
  98. *screen++ = cucul_attr_to_ansi(*attrs++);
  99. }
  100. # if defined(SCREENUPDATE_IN_PC_H)
  101. ScreenUpdate(dp->drv.p->screen);
  102. # else
  103. /* FIXME */
  104. # endif
  105. }
  106. static void conio_handle_resize(caca_display_t *dp)
  107. {
  108. /* We know nothing about our window */
  109. dp->resize.w = dp->cv->width;
  110. dp->resize.h = dp->cv->height;
  111. }
  112. static int conio_get_event(caca_display_t *dp, caca_event_t *ev)
  113. {
  114. unsigned char ch;
  115. caca_event_t release;
  116. if(!_conio_kbhit())
  117. {
  118. ev->type = CACA_EVENT_NONE;
  119. return 0;
  120. }
  121. ch = getch();
  122. ev->type = CACA_EVENT_KEY_PRESS;
  123. ev->data.key.ch = ch;
  124. ev->data.key.utf32 = (uint32_t)ch;
  125. ev->data.key.utf8[0] = ch;
  126. ev->data.key.utf8[1] = '\0';
  127. release = *ev;
  128. release.type = CACA_EVENT_KEY_RELEASE;
  129. _push_event(dp, &release);
  130. return 1;
  131. }
  132. /*
  133. * Driver initialisation
  134. */
  135. int conio_install(caca_display_t *dp)
  136. {
  137. dp->drv.driver = CACA_DRIVER_CONIO;
  138. dp->drv.init_graphics = conio_init_graphics;
  139. dp->drv.end_graphics = conio_end_graphics;
  140. dp->drv.set_display_title = conio_set_display_title;
  141. dp->drv.get_display_width = conio_get_display_width;
  142. dp->drv.get_display_height = conio_get_display_height;
  143. dp->drv.display = conio_display;
  144. dp->drv.handle_resize = conio_handle_resize;
  145. dp->drv.get_event = conio_get_event;
  146. dp->drv.set_mouse = NULL;
  147. return 0;
  148. }
  149. #endif /* USE_CONIO */