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.
 
 
 
 
 
 

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