xref: /haiku/src/system/boot/platform/u-boot/console.cpp (revision 0c65f35f47abb22783d093bf0ac1453b96689a9e)
1 /*
2  * Copyright 2004-2005, Axel D?rfler, axeld@pinc-software.de.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  *
5  * Copyright 2009 Jonas Sundström, jonas@kirilla.com
6  * All rights reserved. Distributed under the terms of the MIT License.
7  */
8 
9 
10 #include "console.h"
11 #include "keyboard.h"
12 #include "serial.h"
13 
14 #include <SupportDefs.h>
15 #include <util/kernel_cpp.h>
16 #include <boot/stage2.h>
17 
18 #include <string.h>
19 
20 
21 class Console : public ConsoleNode {
22 public:
23 							Console();
24 
25 	virtual	ssize_t			ReadAt(void* cookie, off_t pos, void* buffer,
26 								size_t bufferSize);
27 	virtual	ssize_t			WriteAt(void* cookie, off_t pos,
28 								const void* buffer, size_t bufferSize);
29 };
30 
31 
32 class VTConsole : public ConsoleNode {
33 public:
34 							VTConsole();
35 			void			ClearScreen();
36 			void			SetCursor(int32 x, int32 y);
37 			void			SetColor(int32 foreground, int32 background);
38 };
39 
40 
41 class SerialConsole : public VTConsole {
42 public:
43 							SerialConsole();
44 
45 	virtual ssize_t			ReadAt(void *cookie, off_t pos, void *buffer,
46 								size_t bufferSize);
47 	virtual ssize_t			WriteAt(void *cookie, off_t pos, const void *buffer,
48 								size_t bufferSize);
49 };
50 
51 
52 static Console sInput;
53 static Console sOutput;
54 static SerialConsole sSerial;
55 
56 FILE* stdin;
57 FILE* stdout;
58 FILE* stderr;
59 
60 
61 //	#pragma mark -
62 
63 
64 Console::Console()
65 	:
66 	ConsoleNode()
67 {
68 }
69 
70 
71 ssize_t
72 Console::ReadAt(void* cookie, off_t pos, void* buffer, size_t bufferSize)
73 {
74 	// don't seek in character devices
75 	// not implemented (and not yet? needed)
76 	return B_ERROR;
77 }
78 
79 
80 ssize_t
81 Console::WriteAt(void* cookie, off_t /*pos*/, const void* buffer,
82 	size_t bufferSize)
83 {
84 	return 0;
85 }
86 
87 
88 //	#pragma mark -
89 
90 
91 VTConsole::VTConsole()
92 	:
93 	ConsoleNode()
94 {
95 }
96 
97 
98 void
99 VTConsole::ClearScreen()
100 {
101 	WriteAt(NULL, 0LL, "\033[2J", 4);
102 }
103 
104 
105 void
106 VTConsole::SetCursor(int32 x, int32 y)
107 {
108 	char buffer[9];
109 	x = MIN(80, MAX(1, x));
110 	y = MIN(25, MAX(1, y));
111 	int len = snprintf(buffer, sizeof(buffer),
112 		"\033[%" B_PRId32 ";%" B_PRId32 "H", y, x);
113 	WriteAt(NULL, 0LL, buffer, len);
114 }
115 
116 
117 void
118 VTConsole::SetColor(int32 foreground, int32 background)
119 {
120 	static const char cmap[] = {
121 		0, 4, 2, 6, 1, 5, 3, 7 };
122 	char buffer[12];
123 
124 	if (foreground < 0 || foreground >= 8)
125 		return;
126 	if (background < 0 || background >= 8)
127 		return;
128 
129 	// We assume normal display attributes here
130 	int len = snprintf(buffer, sizeof(buffer),
131 		"\033[%" B_PRId32 ";%" B_PRId32 "m",
132 		cmap[foreground] + 30, cmap[background] + 40);
133 
134 	WriteAt(NULL, 0LL, buffer, len);
135 }
136 
137 
138 //     #pragma mark -
139 
140 
141 SerialConsole::SerialConsole()
142 	: VTConsole()
143 {
144 }
145 
146 
147 ssize_t
148 SerialConsole::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize)
149 {
150 	// don't seek in character devices
151 	// not implemented (and not yet? needed)
152 	return B_ERROR;
153 }
154 
155 
156 ssize_t
157 SerialConsole::WriteAt(void *cookie, off_t /*pos*/, const void *buffer,
158 	size_t bufferSize)
159 {
160 	serial_puts((const char *)buffer, bufferSize);
161 	return bufferSize;
162 }
163 
164 
165 //     #pragma mark -
166 
167 
168 void
169 console_clear_screen(void)
170 {
171 	sSerial.ClearScreen();
172 }
173 
174 
175 int32
176 console_width(void)
177 {
178 	return 80;
179 }
180 
181 
182 int32
183 console_height(void)
184 {
185 	return 25;
186 }
187 
188 
189 void
190 console_set_cursor(int32 x, int32 y)
191 {
192 	sSerial.SetCursor(x, y);
193 }
194 
195 
196 void
197 console_show_cursor(void)
198 {
199 }
200 
201 
202 void
203 console_hide_cursor(void)
204 {
205 }
206 
207 
208 int
209 console_wait_for_key(void)
210 {
211 	union key key;
212 	key.ax = serial_getc(true);
213 	return key.code.ascii;
214 }
215 
216 
217 void
218 console_set_color(int32 foreground, int32 background)
219 {
220 	sSerial.SetColor(foreground, background);
221 }
222 
223 
224 status_t
225 console_init(void)
226 {
227 	stdin = (FILE *)&sSerial;
228 	stdout = (FILE *)&sSerial;
229 	stderr = (FILE *)&sSerial;
230 	return B_OK;
231 }
232 
233