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.
 
 
 
 
 
 

195 rivejä
4.7 KiB

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