blob: 8333214d007839faabd50ceb6d6accd2a830b14f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2008-2010, 2015 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 <assert.h>
35#include <lk/init.h>
36#include <lib/gfx.h>
37#include <lib/gfxconsole.h>
38#include <lib/font.h>
39#include <dev/display.h>
40
41/** @addtogroup graphics
42 * @{
43 */
44
45/**
46 * @brief Represent state of graphics console
47 */
48static struct {
49 gfx_surface *surface;
50 uint rows, columns;
51 uint extray; // extra pixels left over if the rows doesn't fit precisely
52
53 uint x, y;
54
55 uint32_t front_color;
56 uint32_t back_color;
57} gfxconsole;
58
59static void gfxconsole_putc(char c)
60{
61 static enum { NORMAL, ESCAPE } state = NORMAL;
62 static uint32_t p_num = 0;
63
64 switch (state) {
65 case NORMAL: {
66 if (c == '\n' || c == '\r') {
67 gfxconsole.x = 0;
68 gfxconsole.y++;
69 } else if (c == 0x1b) {
70 p_num = 0;
71 state = ESCAPE;
72 } else {
73 font_draw_char(gfxconsole.surface, c, gfxconsole.x * FONT_X, gfxconsole.y * FONT_Y, gfxconsole.front_color);
74 gfxconsole.x++;
75 }
76 break;
77 }
78
79 case ESCAPE: {
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
110void gfxconsole_print_callback(print_callback_t *cb, const char *str, size_t len)
111{
112 for (size_t i = 0; i < len; i++) {
113 gfxconsole_putc(str[i]);
114 }
115}
116
117static print_callback_t cb = {
118 { 0 },
119 gfxconsole_print_callback
120};
121
122/**
123 * @brief Initialize graphics console on given drawing surface.
124 *
125 * The graphics console subsystem is initialized, and registered as
126 * an output device for debug output.
127 */
128void gfxconsole_start(gfx_surface *surface)
129{
130 DEBUG_ASSERT(gfxconsole.surface == NULL);
131
132 // set up the surface
133 gfxconsole.surface = surface;
134
135 // calculate how many rows/columns we have
136 gfxconsole.rows = surface->height / FONT_Y;
137 gfxconsole.columns = surface->width / FONT_X;
138 gfxconsole.extray = surface->height - (gfxconsole.rows * FONT_Y);
139
140 dprintf(SPEW, "gfxconsole: rows %d, columns %d, extray %d\n", gfxconsole.rows, gfxconsole.columns, gfxconsole.extray);
141
142 // start in the upper left
143 gfxconsole.x = 0;
144 gfxconsole.y = 0;
145
146 // colors are white and black for now
147 gfxconsole.front_color = 0xffffffff;
148 gfxconsole.back_color = 0;
149
150 // register for debug callbacks
151 register_print_callback(&cb);
152}
153
154/**
155 * @brief Initialize graphics console on default display
156 */
157void gfxconsole_start_on_display(void)
158{
159 static bool started = false;
160
161 if (started)
162 return;
163
164 /* pop up the console */
165 struct display_info info;
166 if (display_get_info(&info) < 0)
167 return;
168
169 gfx_surface *s = gfx_create_surface_from_display(&info);
170 gfxconsole_start(s);
171 started = true;
172}
173
174static void gfxconsole_init_hook(uint level)
175{
176 gfxconsole_start_on_display();
177}
178
179LK_INIT_HOOK(gfxconsole, &gfxconsole_init_hook, LK_INIT_LEVEL_PLATFORM);
180
181// vim: set noexpandtab: