blob: 8f1fb136e9853543fb5b825fe090bff094a13931 [file] [log] [blame]
Travis Geiselbrecht99b2d1d2010-05-21 22:57:18 -07001/*
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/**
25 * @file
26 * @brief Manage graphics console
27 *
28 * This file contains functions to provide stdout to the graphics console.
29 *
30 * @ingroup graphics
31 */
32
33#include <debug.h>
34#include <lib/gfx.h>
35#include <lib/gfxconsole.h>
36#include <lib/font.h>
37#include <dev/display.h>
38
39/** @addtogroup graphics
40 * @{
41 */
42
43/**
44 * @brief Represent state of graphics console
45 */
46static struct {
47 gfx_surface *surface;
48 uint rows, columns;
49 uint extray; // extra pixels left over if the rows doesn't fit precisely
50
51 uint x, y;
52
53 uint32_t front_color;
54 uint32_t back_color;
55} gfxconsole;
56
57static void gfxconsole_putc(char c)
58{
59 static enum { NORMAL, ESCAPE } state = NORMAL;
60 static uint32_t p_num = 0;
61
62 switch (state) {
63 case NORMAL:
64 {
65 if(c == '\n' || c == '\r') {
66 gfxconsole.x = 0;
67 gfxconsole.y++;
68 } else if (c == 0x1b) {
69 p_num = 0;
70 state = ESCAPE;
71 } else {
72 font_draw_char(gfxconsole.surface, c, gfxconsole.x * FONT_X, gfxconsole.y * FONT_Y, gfxconsole.front_color);
73 gfxconsole.x++;
74 }
75 break;
76 }
77
78 case ESCAPE:
79 {
80 if (c >= '0' && c <= '9') {
81 p_num = (p_num * 10) + (c - '0');
82 } else if (c == 'D') {
83 if (p_num <= gfxconsole.x)
84 gfxconsole.x -= p_num;
85 state = NORMAL;
86 } else if (c == '[') {
87 // eat this character
88 } else {
89 font_draw_char(gfxconsole.surface, c, gfxconsole.x * FONT_X, gfxconsole.y * FONT_Y, gfxconsole.front_color);
90 gfxconsole.x++;
91 state = NORMAL;
92 }
93 break;
94 }
95 }
96
97 if(gfxconsole.x >= gfxconsole.columns) {
98 gfxconsole.x = 0;
99 gfxconsole.y++;
100 }
101 if(gfxconsole.y >= gfxconsole.rows) {
102 // scroll up
103 gfx_copyrect(gfxconsole.surface, 0, FONT_Y, gfxconsole.surface->width, gfxconsole.surface->height - FONT_Y - gfxconsole.extray, 0, 0);
104 gfxconsole.y--;
105 gfx_fillrect(gfxconsole.surface, 0, gfxconsole.surface->height - FONT_Y - gfxconsole.extray, gfxconsole.surface->width, FONT_Y, gfxconsole.back_color);
106 gfx_flush(gfxconsole.surface);
107 }
108}
109
110/**
111 * @brief Initialize graphics console on given drawing surface.
112 *
113 * The graphics console subsystem is initialized, and registered as
114 * an output device for debug output.
115 */
116void gfxconsole_start(gfx_surface *surface)
117{
118 DEBUG_ASSERT(gfxconsole.surface == NULL);
119
120 // set up the surface
121 gfxconsole.surface = surface;
122
123 // calculate how many rows/columns we have
124 gfxconsole.rows = surface->height / FONT_Y;
125 gfxconsole.columns = surface->width / FONT_X;
126 gfxconsole.extray = surface->height - (gfxconsole.rows * FONT_Y);
127
128 dprintf(SPEW, "gfxconsole: rows %d, columns %d, extray %d\n", gfxconsole.rows, gfxconsole.columns, gfxconsole.extray);
129
130 // start in the upper left
131 gfxconsole.x = 0;
132 gfxconsole.y = 0;
133
134 // colors are white and black for now
135 gfxconsole.front_color = 0xffffffff;
136 gfxconsole.back_color = 0;
137
138 // register for debug callbacks
139 register_debug_output(&gfxconsole_putc);
140}
141
142/**
143 * @brief Initialize graphics console on default display
144 */
145void gfxconsole_start_on_display(void)
146{
147 static bool started = false;
148
149 if (started)
150 return;
151
152 /* pop up the console */
153 struct display_info info;
154 display_get_info(&info);
155 gfx_surface *s = gfx_create_surface_from_display(&info);
156 gfxconsole_start(s);
157 started = true;
158}
159