您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

170 行
3.7 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file driver_conio.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief DOS/conio.h driver
  15. *
  16. * This file contains the libcaca DOS/conio.h input and output driver
  17. */
  18. #include "config.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_t *kk)
  36. {
  37. kk->drv.p = malloc(sizeof(struct driver_private));
  38. _wscroll = 0;
  39. _setcursortype(_NOCURSOR);
  40. clrscr();
  41. gettextinfo(&kk->drv.p->ti);
  42. kk->drv.p->screen = malloc(2 * kk->drv.p->ti.screenwidth
  43. * kk->drv.p->ti.screenheight * sizeof(char));
  44. if(kk->drv.p->screen == NULL)
  45. return -1;
  46. # if defined(SCREENUPDATE_IN_PC_H)
  47. ScreenRetrieve(kk->drv.p->screen);
  48. # else
  49. /* FIXME */
  50. # endif
  51. _cucul_set_size(kk->qq, kk->drv.p->ti.screenwidth,
  52. kk->drv.p->ti.screenheight);
  53. return 0;
  54. }
  55. static int conio_end_graphics(caca_t *kk)
  56. {
  57. _wscroll = 1;
  58. textcolor((enum COLORS)WHITE);
  59. textbackground((enum COLORS)BLACK);
  60. gotoxy(kk->qq->width, kk->qq->height);
  61. cputs("\r\n");
  62. _setcursortype(_NORMALCURSOR);
  63. free(kk->drv.p->screen);
  64. free(kk->drv.p);
  65. return 0;
  66. }
  67. static int conio_set_window_title(caca_t *kk, char const *title)
  68. {
  69. return 0;
  70. }
  71. static unsigned int conio_get_window_width(caca_t *kk)
  72. {
  73. /* Fallback to a 6x10 font */
  74. return kk->qq->width * 6;
  75. }
  76. static unsigned int conio_get_window_height(caca_t *kk)
  77. {
  78. /* Fallback to a 6x10 font */
  79. return kk->qq->height * 10;
  80. }
  81. static void conio_display(caca_t *kk)
  82. {
  83. char *screen = kk->drv.p->screen;
  84. uint8_t *attr = kk->qq->attr;
  85. uint32_t *chars = kk->qq->chars;
  86. int n;
  87. for(n = kk->qq->height * kk->qq->width; n--; )
  88. {
  89. *screen++ = _cucul_utf32_to_cp437(*chars++);
  90. *screen++ = *attr++;
  91. }
  92. # if defined(SCREENUPDATE_IN_PC_H)
  93. ScreenUpdate(kk->drv.p->screen);
  94. # else
  95. /* FIXME */
  96. # endif
  97. }
  98. static void conio_handle_resize(caca_t *kk)
  99. {
  100. /* We know nothing about our window */
  101. kk->resize.w = kk->qq->width;
  102. kk->resize.h = kk->qq->height;
  103. }
  104. static int conio_get_event(caca_t *kk, struct caca_event *ev)
  105. {
  106. unsigned char ch;
  107. struct caca_event release;
  108. if(!_conio_kbhit())
  109. {
  110. ev->type = CACA_EVENT_NONE;
  111. return 0;
  112. }
  113. ch = getch();
  114. ev->type = CACA_EVENT_KEY_PRESS;
  115. ev->data.key.c = ch;
  116. ev->data.key.ucs4 = (uint32_t)ch;
  117. ev->data.key.utf8[0] = ch;
  118. ev->data.key.utf8[1] = '\0';
  119. release = *ev;
  120. release.type = CACA_EVENT_KEY_RELEASE;
  121. _push_event(kk, &release);
  122. return 1;
  123. }
  124. /*
  125. * Driver initialisation
  126. */
  127. void conio_init_driver(caca_t *kk)
  128. {
  129. kk->drv.driver = CACA_DRIVER_CONIO;
  130. kk->drv.init_graphics = conio_init_graphics;
  131. kk->drv.end_graphics = conio_end_graphics;
  132. kk->drv.set_window_title = conio_set_window_title;
  133. kk->drv.get_window_width = conio_get_window_width;
  134. kk->drv.get_window_height = conio_get_window_height;
  135. kk->drv.display = conio_display;
  136. kk->drv.handle_resize = conio_handle_resize;
  137. kk->drv.get_event = conio_get_event;
  138. }
  139. #endif /* USE_CONIO */