text.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. * @addtogroup graphics
  25. * @{
  26. */
  27. /**
  28. * @file
  29. * @brief Console text display
  30. *
  31. * This module displays text on the console. The text is retained so that
  32. * it can be redisplayed if the display is cleared.
  33. *
  34. * Output is to the default graphics display
  35. */
  36. #include <debug.h>
  37. #include <list.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <dev/display.h>
  41. #include <lib/gfx.h>
  42. #include <lib/font.h>
  43. #include <lib/text.h>
  44. #define TEXT_COLOR 0xffffffff
  45. static struct list_node text_list = LIST_INITIAL_VALUE(text_list);
  46. struct text_line {
  47. struct list_node node;
  48. const char *str;
  49. int x, y;
  50. };
  51. /**
  52. * @brief Add a string to the console text
  53. */
  54. void text_draw(int x, int y, const char *string)
  55. {
  56. struct text_line *line = malloc(sizeof(struct text_line));
  57. line->str = strdup(string);
  58. line->x = x;
  59. line->y = y;
  60. list_add_head(&text_list, &line->node);
  61. text_update();
  62. }
  63. /**
  64. * @brief Refresh the display
  65. */
  66. void text_update(void)
  67. {
  68. struct display_info info;
  69. display_get_info(&info);
  70. /* get the display's surface */
  71. gfx_surface *surface = gfx_create_surface_from_display(&info);
  72. struct text_line *line;
  73. list_for_every_entry(&text_list, line, struct text_line, node) {
  74. const char *c;
  75. int x = line->x;
  76. for (c = line->str; *c; c++) {
  77. font_draw_char(surface, *c, x, line->y, TEXT_COLOR);
  78. x += FONT_X;
  79. }
  80. }
  81. gfx_flush(surface);
  82. gfx_surface_destroy(surface);
  83. }