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

180 行
4.3 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 VGA input and output driver
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if defined(USE_VGA)
  20. #include "cucul.h"
  21. #include "caca.h"
  22. #include "caca_internals.h"
  23. /* Address of the VGA screen */
  24. #define VGA_SCREEN ((char *)(intptr_t)0x000b8000)
  25. static uint8_t const vga_colors[][4] =
  26. {
  27. /* Colour values range from 0x00 to 0x3f */
  28. { 0, 0x00, 0x00, 0x00 },
  29. { 1, 0x00, 0x00, 0x1f },
  30. { 2, 0x00, 0x1f, 0x00 },
  31. { 3, 0x00, 0x1f, 0x1f },
  32. { 4, 0x1f, 0x00, 0x00 },
  33. { 5, 0x1f, 0x00, 0x1f },
  34. { 0x14, 0x1f, 0x1f, 0x00 },
  35. { 7, 0x1f, 0x1f, 0x1f },
  36. { 0x38, 0x0f, 0x0f, 0x0f },
  37. { 0x39, 0x0f, 0x0f, 0x3f },
  38. { 0x3a, 0x0f, 0x3f, 0x0f },
  39. { 0x3b, 0x0f, 0x3f, 0x3f },
  40. { 0x3c, 0x3f, 0x0f, 0x0f },
  41. { 0x3d, 0x3f, 0x0f, 0x3f },
  42. { 0x3e, 0x3f, 0x3f, 0x0f },
  43. { 0x3f, 0x3f, 0x3f, 0x3f },
  44. };
  45. static int vga_init_graphics(caca_display_t *dp)
  46. {
  47. int i;
  48. uint8_t tmp;
  49. /* Blank screen */
  50. memset(VGA_SCREEN, 0, 80 * 25 * 2);
  51. /* Fill VGA palette */
  52. for(i = 0; i < 16; i++)
  53. {
  54. outb(vga_colors[i][0], 0x3c8);
  55. outb(vga_colors[i][1], 0x3c9);
  56. outb(vga_colors[i][2], 0x3c9);
  57. outb(vga_colors[i][3], 0x3c9);
  58. }
  59. /* Hide cursor */
  60. outb(0x0a, 0x3d4);
  61. tmp = inb(0x3d5);
  62. tmp |= 0x20;
  63. outb(0x0a, 0x3d4);
  64. outb(tmp, 0x3d5);
  65. /* We don't have much choice */
  66. dp->resize.allow = 1;
  67. cucul_set_canvas_size(dp->cv, 80, 25);
  68. dp->resize.allow = 0;
  69. return 0;
  70. }
  71. static int vga_end_graphics(caca_display_t *dp)
  72. {
  73. uint8_t tmp;
  74. /* Show cursor again */
  75. outb(0x0a, 0x3d4);
  76. tmp = inb(0x3d5);
  77. tmp &= 0xdf;
  78. outb(0x0a, 0x3d4);
  79. outb(tmp, 0x3d5);
  80. return 0;
  81. }
  82. static int vga_set_display_title(caca_display_t *dp, char const *title)
  83. {
  84. /* Unsupported, of course. */
  85. return -1;
  86. }
  87. static unsigned int vga_get_display_width(caca_display_t const *dp)
  88. {
  89. /* Fallback to a 320x200 screen */
  90. return 320;
  91. }
  92. static unsigned int vga_get_display_height(caca_display_t const *dp)
  93. {
  94. /* Fallback to a 320x200 screen */
  95. return 200;
  96. }
  97. static void vga_display(caca_display_t *dp)
  98. {
  99. char *screen = (char *)(intptr_t)0x000b8000;
  100. uint32_t const *cvchars = (uint32_t const *)cucul_get_canvas_chars(dp->cv);
  101. uint32_t const *cvattrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv);
  102. unsigned int width = cucul_get_canvas_width(dp->cv);
  103. unsigned int height = cucul_get_canvas_height(dp->cv);
  104. int n;
  105. for(n = height * width; n--; )
  106. {
  107. char ch = cucul_utf32_to_cp437(*cvchars++);
  108. if(n && *cvchars == CUCUL_MAGIC_FULLWIDTH)
  109. {
  110. *screen++ = '[';
  111. *screen++ = cucul_attr_to_ansi(*cvattrs++);
  112. ch = ']';
  113. cvchars++;
  114. n--;
  115. }
  116. *screen++ = ch;
  117. *screen++ = cucul_attr_to_ansi(*cvattrs++);
  118. }
  119. }
  120. static void vga_handle_resize(caca_display_t *dp)
  121. {
  122. /* We know nothing about our window */
  123. dp->resize.w = cucul_get_canvas_width(dp->cv);
  124. dp->resize.h = cucul_get_canvas_height(dp->cv);
  125. }
  126. static int vga_get_event(caca_display_t *dp, caca_privevent_t *ev)
  127. {
  128. /* FIXME */
  129. ev->type = CACA_EVENT_NONE;
  130. return 0;
  131. }
  132. /*
  133. * Driver initialisation
  134. */
  135. int vga_install(caca_display_t *dp)
  136. {
  137. dp->drv.driver = CACA_DRIVER_VGA;
  138. dp->drv.init_graphics = vga_init_graphics;
  139. dp->drv.end_graphics = vga_end_graphics;
  140. dp->drv.set_display_title = vga_set_display_title;
  141. dp->drv.get_display_width = vga_get_display_width;
  142. dp->drv.get_display_height = vga_get_display_height;
  143. dp->drv.display = vga_display;
  144. dp->drv.handle_resize = vga_handle_resize;
  145. dp->drv.get_event = vga_get_event;
  146. dp->drv.set_mouse = NULL;
  147. dp->drv.set_cursor = NULL;
  148. return 0;
  149. }
  150. #endif /* USE_VGA */