gfxconsole.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2008-2010 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * @file
  25. * @brief Manage graphics console
  26. *
  27. * This file contains functions to provide stdout to the graphics console.
  28. *
  29. * @ingroup graphics
  30. */
  31. #include <debug.h>
  32. #include <lib/gfx.h>
  33. #include <lib/gfxconsole.h>
  34. #include <lib/font.h>
  35. #include <dev/display.h>
  36. /** @addtogroup graphics
  37. * @{
  38. */
  39. /**
  40. * @brief Represent state of graphics console
  41. */
  42. static struct {
  43. gfx_surface *surface;
  44. uint rows, columns;
  45. uint extray; // extra pixels left over if the rows doesn't fit precisely
  46. uint x, y;
  47. uint32_t front_color;
  48. uint32_t back_color;
  49. } gfxconsole;
  50. static void gfxconsole_putc(char c)
  51. {
  52. static enum { NORMAL, ESCAPE } state = NORMAL;
  53. static uint32_t p_num = 0;
  54. switch (state) {
  55. case NORMAL:
  56. {
  57. if(c == '\n' || c == '\r') {
  58. gfxconsole.x = 0;
  59. gfxconsole.y++;
  60. } else if (c == 0x1b) {
  61. p_num = 0;
  62. state = ESCAPE;
  63. } else {
  64. font_draw_char(gfxconsole.surface, c, gfxconsole.x * FONT_X, gfxconsole.y * FONT_Y, gfxconsole.front_color);
  65. gfxconsole.x++;
  66. }
  67. break;
  68. }
  69. case ESCAPE:
  70. {
  71. if (c >= '0' && c <= '9') {
  72. p_num = (p_num * 10) + (c - '0');
  73. } else if (c == 'D') {
  74. if (p_num <= gfxconsole.x)
  75. gfxconsole.x -= p_num;
  76. state = NORMAL;
  77. } else if (c == '[') {
  78. // eat this character
  79. } else {
  80. font_draw_char(gfxconsole.surface, c, gfxconsole.x * FONT_X, gfxconsole.y * FONT_Y, gfxconsole.front_color);
  81. gfxconsole.x++;
  82. state = NORMAL;
  83. }
  84. break;
  85. }
  86. }
  87. if(gfxconsole.x >= gfxconsole.columns) {
  88. gfxconsole.x = 0;
  89. gfxconsole.y++;
  90. }
  91. if(gfxconsole.y >= gfxconsole.rows) {
  92. // scroll up
  93. gfx_copyrect(gfxconsole.surface, 0, FONT_Y, gfxconsole.surface->width, gfxconsole.surface->height - FONT_Y - gfxconsole.extray, 0, 0);
  94. gfxconsole.y--;
  95. gfx_fillrect(gfxconsole.surface, 0, gfxconsole.surface->height - FONT_Y - gfxconsole.extray, gfxconsole.surface->width, FONT_Y, gfxconsole.back_color);
  96. gfx_flush(gfxconsole.surface);
  97. }
  98. }
  99. /**
  100. * @brief Initialize graphics console on given drawing surface.
  101. *
  102. * The graphics console subsystem is initialized, and registered as
  103. * an output device for debug output.
  104. */
  105. void gfxconsole_start(gfx_surface *surface)
  106. {
  107. DEBUG_ASSERT(gfxconsole.surface == NULL);
  108. // set up the surface
  109. gfxconsole.surface = surface;
  110. // calculate how many rows/columns we have
  111. gfxconsole.rows = surface->height / FONT_Y;
  112. gfxconsole.columns = surface->width / FONT_X;
  113. gfxconsole.extray = surface->height - (gfxconsole.rows * FONT_Y);
  114. dprintf(SPEW, "gfxconsole: rows %d, columns %d, extray %d\n", gfxconsole.rows, gfxconsole.columns, gfxconsole.extray);
  115. // start in the upper left
  116. gfxconsole.x = 0;
  117. gfxconsole.y = 0;
  118. // colors are white and black for now
  119. gfxconsole.front_color = 0xffffffff;
  120. gfxconsole.back_color = 0;
  121. // register for debug callbacks
  122. register_debug_output(&gfxconsole_putc);
  123. }
  124. /**
  125. * @brief Initialize graphics console on default display
  126. */
  127. void gfxconsole_start_on_display(void)
  128. {
  129. static bool started = false;
  130. if (started)
  131. return;
  132. /* pop up the console */
  133. struct display_info info;
  134. display_get_info(&info);
  135. gfx_surface *s = gfx_create_surface_from_display(&info);
  136. gfxconsole_start(s);
  137. started = true;
  138. }