25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

184 satır
4.3 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2012 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What the Fuck You Want
  9. * to Public License, Version 2, as published by Sam Hocevar. See
  10. * http://www.wtfpl.net/ for more details.
  11. */
  12. /*
  13. * This file contains the libcaca DOS/conio.h input and output driver
  14. */
  15. #include "config.h"
  16. #if defined(USE_CONIO)
  17. #include <dos.h>
  18. #include <conio.h>
  19. #if defined(SCREENUPDATE_IN_PC_H)
  20. # include <pc.h>
  21. #endif
  22. #include <stdlib.h>
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. struct driver_private
  26. {
  27. struct text_info ti;
  28. char *screen;
  29. };
  30. static int conio_init_graphics(caca_display_t *dp)
  31. {
  32. dp->drv.p = malloc(sizeof(struct driver_private));
  33. _wscroll = 0;
  34. _setcursortype(_NOCURSOR);
  35. clrscr();
  36. gettextinfo(&dp->drv.p->ti);
  37. dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth
  38. * dp->drv.p->ti.screenheight * sizeof(char));
  39. if(dp->drv.p->screen == NULL)
  40. return -1;
  41. # if defined(SCREENUPDATE_IN_PC_H)
  42. ScreenRetrieve(dp->drv.p->screen);
  43. # else
  44. /* FIXME */
  45. # endif
  46. dp->resize.allow = 1;
  47. caca_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth,
  48. dp->drv.p->ti.screenheight);
  49. dp->resize.allow = 0;
  50. return 0;
  51. }
  52. static int conio_end_graphics(caca_display_t *dp)
  53. {
  54. _wscroll = 1;
  55. textcolor((enum COLORS)WHITE);
  56. textbackground((enum COLORS)BLACK);
  57. gotoxy(caca_get_canvas_width(dp->cv), caca_get_canvas_height(dp->cv));
  58. cputs("\r\n");
  59. _setcursortype(_NORMALCURSOR);
  60. free(dp->drv.p->screen);
  61. free(dp->drv.p);
  62. return 0;
  63. }
  64. static int conio_set_display_title(caca_display_t *dp, char const *title)
  65. {
  66. return -1;
  67. }
  68. static int conio_get_display_width(caca_display_t const *dp)
  69. {
  70. /* Fallback to a 6x10 font */
  71. return caca_get_canvas_width(dp->cv) * 6;
  72. }
  73. static int conio_get_display_height(caca_display_t const *dp)
  74. {
  75. /* Fallback to a 6x10 font */
  76. return caca_get_canvas_height(dp->cv) * 10;
  77. }
  78. static void conio_display(caca_display_t *dp)
  79. {
  80. char *screen = dp->drv.p->screen;
  81. uint32_t const *chars = caca_get_canvas_chars(dp->cv);
  82. uint32_t const *attrs = caca_get_canvas_attrs(dp->cv);
  83. int width = caca_get_canvas_width(dp->cv);
  84. int height = caca_get_canvas_height(dp->cv);
  85. int n;
  86. for(n = height * width; n--; )
  87. {
  88. char ch = caca_utf32_to_cp437(*chars++);
  89. if(n && *chars == CACA_MAGIC_FULLWIDTH)
  90. {
  91. *screen++ = '[';
  92. *screen++ = caca_attr_to_ansi(*attrs++);
  93. ch = ']';
  94. chars++;
  95. n--;
  96. }
  97. *screen++ = ch;
  98. *screen++ = caca_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 = caca_get_canvas_width(dp->cv);
  110. dp->resize.h = caca_get_canvas_height(dp->cv);
  111. }
  112. static int conio_get_event(caca_display_t *dp, caca_privevent_t *ev)
  113. {
  114. uint8_t ch;
  115. caca_privevent_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 = 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.id = CACA_DRIVER_CONIO;
  138. dp->drv.driver = "conio";
  139. dp->drv.init_graphics = conio_init_graphics;
  140. dp->drv.end_graphics = conio_end_graphics;
  141. dp->drv.set_display_title = conio_set_display_title;
  142. dp->drv.get_display_width = conio_get_display_width;
  143. dp->drv.get_display_height = conio_get_display_height;
  144. dp->drv.display = conio_display;
  145. dp->drv.handle_resize = conio_handle_resize;
  146. dp->drv.get_event = conio_get_event;
  147. dp->drv.set_mouse = NULL;
  148. dp->drv.set_cursor = NULL;
  149. return 0;
  150. }
  151. #endif /* USE_CONIO */