Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

vga.c 4.7 KiB

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