/
display.cpp
148 lines (132 loc) · 3.86 KB
/
display.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// PhoeniX OS Kernel library display functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "kernlib.hpp"
#include "efi.hpp"
class ConsoleDisplay: public Display {
private:
static char *const base;
static char *const top;
static const size_t size;
char *display;
void putc(const char c);
public:
ConsoleDisplay();
void write(const char *str);
void clean();
};
class EFIDisplay: public Display {
public:
EFIDisplay();
void write(const char *str);
void clean();
};
char *const ConsoleDisplay::base = reinterpret_cast<char*>(0xB8000);
char *const ConsoleDisplay::top = reinterpret_cast<char*>(0xB8FA0);
const size_t ConsoleDisplay::size = ConsoleDisplay::top - ConsoleDisplay::base;
ConsoleDisplay::ConsoleDisplay() {
if (EFI::getSystemTable() != 0) return;
display = base;
clean();
}
void ConsoleDisplay::putc(const char c) {
if (c == 0) return;
size_t pos = display - base;
if (c == '\n') {
display += 160 - (pos % 160);
} else if (c == '\t') {
do {
*(display++) = ' ';
*(display++) = 0x0F;
} while (pos % 8 != 0);
} else {
*(display++) = c;
*(display++) = 0x0F;
}
if (display >= top) {
Memory::copy(base, base + 160, size - 160);
display = top - 160;
Memory::fill(display, 0, 160);
}
}
void ConsoleDisplay::write(const char *str) {
uint64_t t = EnterCritical();
mutex.lock();
while (*str != 0) putc(*(str++));
mutex.release();
LeaveCritical(t);
}
void ConsoleDisplay::clean() {
mutex.lock();
Memory::fill(base, 0, size);
mutex.release();
}
EFIDisplay::EFIDisplay() {
const EFI_SYSTEM_TABLE *ST = EFI::getSystemTable();
ST->ConOut->Reset(ST->ConOut, 1);
uint64_t cols = 0, rows = 0, bestmode = 0;
for (uint64_t m = 0; m < ST->ConOut->Mode->MaxMode; m++) {
uint64_t c = 0, r = 0;
ST->ConOut->QueryMode(ST->ConOut, m, &c, &r);
if (c > cols || r > rows) {
cols = c; rows = r; bestmode = m;
}
}
if (ST->ConOut->Mode->Mode != bestmode)
ST->ConOut->SetMode(ST->ConOut, bestmode);
ST->ConOut->SetAttribute(ST->ConOut, {{ EFI_COLOR_WHITE, EFI_COLOR_BLACK }});
}
void EFIDisplay::clean() {
const EFI_SYSTEM_TABLE *ST = EFI::getSystemTable();
mutex.lock();
ST->ConOut->ClearScreen(ST->ConOut);
mutex.release();
}
void EFIDisplay::write(const char *str) {
size_t wlen = 0;
const char *s = str; char c;
while ((c = *s++) != 0)
wlen += (c == '\r') ? 0 : ((c == '\n') ? 2 : 1);
wchar_t *wstr =
reinterpret_cast<wchar_t*>(alloca((wlen + 1) * sizeof(wchar_t))),
*ws = wstr;
s = str;
while ((c = *s++) != 0) {
if (c == '\r') continue;
if (c == '\n') *ws++ = '\r';
*ws++ = c;
}
*ws++ = 0;
const EFI_SYSTEM_TABLE *ST = EFI::getSystemTable();
mutex.lock();
ST->ConOut->OutputString(ST->ConOut, wstr);
mutex.release();
}
static ConsoleDisplay sharedConsole;
Display *Display::instance = Display::initInstance();
Mutex Display::instanceMutex;
Display *Display::initInstance() {
if (EFI::getSystemTable()) return new EFIDisplay();
return &sharedConsole;
}
Display *Display::getInstance() {
if (instance) return instance;
instanceMutex.lock();
if (!instance)
instance = initInstance();
instanceMutex.release();
return instance;
}
Display::~Display() {}