PatchworkOS
Loading...
Searching...
No Matches
log_screen.c
Go to the documentation of this file.
2
4#include <kernel/log/glyphs.h>
5#include <kernel/sync/lock.h>
6
7#include <string.h>
8#include <sys/math.h>
9
10static boot_gop_t gop = {0};
11static log_screen_pos_t cursor = {0, 0};
12static log_screen_t screen = {0};
13
15
21
23{
24 if (screen.invalidStart.x == 0 && screen.invalidStart.y == 0 && screen.invalidEnd.x == 0 &&
25 screen.invalidEnd.y == 0)
26 {
27 screen.invalidStart = *pos;
28 screen.invalidEnd = (log_screen_pos_t){pos->x + 1, pos->y + 1};
29 }
30 else
31 {
36 }
37
40}
41
42static void log_screen_put(char chr)
43{
44 if (chr == '\n' || chr < ' ')
45 {
46 chr = ' ';
47 }
48
50 const glyph_t* glyph = &cache->glyphs[(uint8_t)chr];
51
53
54 uint64_t pixelX = cursor.x * GLYPH_WIDTH;
55 for (uint64_t y = 0; y < GLYPH_HEIGHT; y++)
56 {
57 memcpy(&line->pixels[pixelX + y * SCREEN_LINE_STRIDE], &glyph->pixels[y * GLYPH_WIDTH],
58 sizeof(uint32_t) * GLYPH_WIDTH);
59 }
60
61 line->length = MAX(line->length, cursor.x + 1);
63}
64
65static void log_screen_flush(void)
66{
68 {
70
71 for (uint64_t pixelY = 0; pixelY < GLYPH_HEIGHT; pixelY++)
72 {
76 }
77 }
78
81}
82
97
98static void log_screen_scroll(void)
99{
100 uint64_t newCursorY = cursor.y != 0 ? cursor.y - 1 : 0;
101 for (uint64_t y = 0; y < newCursorY; y++)
102 {
105
106 for (uint64_t offsetY = 0; offsetY < GLYPH_HEIGHT; offsetY++)
107 {
108 memcpy(&gop.virtAddr[(offsetY + y * GLYPH_HEIGHT) * gop.stride],
109 &newLine->pixels[offsetY * SCREEN_LINE_STRIDE], newLine->length * GLYPH_WIDTH * sizeof(uint32_t));
110 }
111
112 if (line->length > newLine->length)
113 {
114 for (uint64_t offsetY = 0; offsetY < GLYPH_HEIGHT; offsetY++)
115 {
116 memset32(&gop.virtAddr[(offsetY + y * GLYPH_HEIGHT) * gop.stride + newLine->length * GLYPH_WIDTH],
117 0xFF000000, (line->length - newLine->length) * GLYPH_WIDTH);
118 }
119 }
120 }
121
122 log_screen_line_t* last = log_screen_get_line(newCursorY);
123 for (uint64_t offsetY = 0; offsetY < GLYPH_HEIGHT; offsetY++)
124 {
125 memset32(&gop.virtAddr[(offsetY + newCursorY * GLYPH_HEIGHT) * gop.stride], 0xFF000000,
126 last->length * GLYPH_WIDTH);
127 }
128
129 cursor.y = newCursorY;
133
135 currentLine->length = 0;
136}
137
138static void log_screen_advance_cursor(char chr)
139{
140 if (chr == '\n')
141 {
142 cursor.y++;
143 cursor.x = 0;
144
145 if (cursor.y >= screen.height)
146 {
148 }
149 }
150 else if (cursor.x >= screen.width)
151 {
152 cursor.y++;
153 cursor.x = 0;
154
155 if (cursor.y >= screen.height)
156 {
158 }
159
160 for (uint64_t i = 0; i < SCREEN_WRAP_INDENT; i++)
161 {
162 log_screen_put(' ');
163 cursor.x++;
164 }
165 }
166 else
167 {
168 cursor.x++;
169 }
170}
171
173{
175
176 cursor = (log_screen_pos_t){0, 0};
177
181
182 for (uint64_t i = 0; i < screen.height; i++)
183 {
184 screen.lines[i].length = 0;
185 memset(screen.lines[i].pixels, 0, sizeof(screen.lines[i].pixels));
186 }
187
188 for (uint64_t y = 0; y < gop.height; y++)
189 {
190 memset32(&gop.virtAddr[y * gop.stride], 0xFF000000, gop.width);
191 }
192}
193
195{
196 return screen.width;
197}
198
200{
201 return screen.height;
202}
203
204void log_screen_write(const char* string, uint64_t length)
205{
207
208 for (uint64_t i = 0; i < length; i++)
209 {
210 char chr = string[i];
211 log_screen_put(chr);
213 }
214
216}
static const glyph_cache_t cache
Definition glyphs.c:5
#define GLYPH_HEIGHT
Definition glyphs.h:7
const glyph_cache_t * glyph_cache_get(void)
Definition glyphs.c:3848
#define GLYPH_WIDTH
Definition glyphs.h:8
#define SCREEN_WRAP_INDENT
Number of spaces to indent when a line wraps.
Definition log_screen.h:22
void log_screen_write(const char *string, uint64_t length)
Write a string to the screen.
Definition log_screen.c:204
uint64_t log_screen_get_width(void)
Get screen width in characters.
Definition log_screen.c:194
#define SCREEN_LINE_MAX_LENGTH
Maximum number of characters in a single line.
Definition log_screen.h:27
void log_screen_init(const boot_gop_t *bootGop)
Initialize the screen logging.
Definition log_screen.c:83
void log_screen_clear(void)
Clear the screen.
Definition log_screen.c:172
uint64_t log_screen_get_height(void)
Get screen height in characters.
Definition log_screen.c:199
#define SCREEN_LINE_STRIDE
The stride of a screen line in pixels.
Definition log_screen.h:32
#define LOCK_CREATE
Create a lock initializer. @macro LOCK_CREATE.
Definition lock.h:66
#define LOCK_SCOPE(lock)
Acquires a lock for the reminder of the current scope.
Definition lock.h:57
#define CONFIG_SCREEN_MAX_LINES
Maximum screen lines configuration.
Definition config.h:164
#define MIN(x, y)
Definition math.h:16
#define MAX(x, y)
Definition math.h:15
static void log_screen_scroll(void)
Definition log_screen.c:98
static void log_screen_invalidate(const log_screen_pos_t *pos)
Definition log_screen.c:22
static boot_gop_t gop
Definition log_screen.c:10
static log_screen_line_t * log_screen_get_line(uint64_t y)
Definition log_screen.c:16
static void log_screen_put(char chr)
Definition log_screen.c:42
static lock_t lock
Definition log_screen.c:14
static log_screen_pos_t cursor
Definition log_screen.c:11
static void log_screen_advance_cursor(char chr)
Definition log_screen.c:138
static log_screen_t screen
Definition log_screen.c:12
static void log_screen_flush(void)
Definition log_screen.c:65
int64_t y
Definition main.c:153
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
_PUBLIC void * memset32(void *s, __UINT32_TYPE__ c, size_t n)
Definition memset32.c:4
_PUBLIC void * memcpy(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
Definition memcpy.c:4
_PUBLIC void * memset(void *s, int c, size_t n)
Definition memset.c:4
uint32_t width
Definition boot_info.h:36
uint32_t * virtAddr
Definition boot_info.h:34
uint32_t height
Definition boot_info.h:37
uint32_t stride
Definition boot_info.h:38
glyph_t glyphs[GLYPH_AMOUNT]
Definition glyphs.h:18
uint32_t pixels[GLYPH_HEIGHT *GLYPH_WIDTH]
Definition glyphs.h:13
A simple ticket lock implementation.
Definition lock.h:43
A single line in the screen buffer.
Definition log_screen.h:47
uint64_t length
The distance from the start of the line to the end of the furthest away char, in chars.
Definition log_screen.h:48
uint32_t pixels[GLYPH_HEIGHT *SCREEN_LINE_STRIDE]
The pixel data for the line.
Definition log_screen.h:49
Represents a position on the screen in character coordinates.
Definition log_screen.h:38
The screen buffer.
Definition log_screen.h:56
log_screen_pos_t invalidStart
The start of the invalid region in the buffer, forms a rectangle with invalidEnd.
Definition log_screen.h:61
log_screen_pos_t invalidEnd
The end of the invalid region in the buffer, forms a rectangle with invalidStart.
Definition log_screen.h:62
uint64_t height
The height of the buffer in chars.
Definition log_screen.h:58
log_screen_line_t lines[CONFIG_SCREEN_MAX_LINES]
The lines in the buffer, acts as a circular buffer.
Definition log_screen.h:63
uint64_t width
The width of the buffer in chars.
Definition log_screen.h:57
uint64_t firstLineIndex
The index of the first line in the buffer.
Definition log_screen.h:59