xref: /haiku/src/system/boot/platform/u-boot/console.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
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 VTConsole : public ConsoleNode {
22 public:
23 							VTConsole();
24 
25 	virtual void			ClearScreen();
26 	virtual int32			Width();
27 	virtual int32			Height();
28 	virtual void			SetCursor(int32 x, int32 y);
29 	virtual void			SetCursorVisible(bool visible);
30 	virtual void			SetColors(int32 foreground, int32 background);
31 };
32 
33 
34 class SerialConsole : public VTConsole {
35 public:
36 							SerialConsole();
37 
38 	virtual ssize_t			ReadAt(void *cookie, off_t pos, void *buffer,
39 								size_t bufferSize);
40 	virtual ssize_t			WriteAt(void *cookie, off_t pos, const void *buffer,
41 								size_t bufferSize);
42 };
43 
44 
45 extern ConsoleNode* gConsoleNode;
46 static SerialConsole sSerial;
47 
48 FILE* stdin;
49 FILE* stdout;
50 FILE* stderr;
51 
52 
53 //	#pragma mark -
54 
55 
56 VTConsole::VTConsole()
57 	:
58 	ConsoleNode()
59 {
60 }
61 
62 
63 void
64 VTConsole::ClearScreen()
65 {
66 	WriteAt(NULL, 0LL, "\033[2J", 4);
67 }
68 
69 
70 int32
71 VTConsole::Width()
72 {
73 	// TODO?
74 	return 80;
75 }
76 
77 
78 int32
79 VTConsole::Height()
80 {
81 	// TODO?
82 	return 25;
83 }
84 
85 
86 void
87 VTConsole::SetCursor(int32 x, int32 y)
88 {
89 	char buffer[9];
90 	x = MIN(80, MAX(1, x));
91 	y = MIN(25, MAX(1, y));
92 	int len = snprintf(buffer, sizeof(buffer),
93 		"\033[%" B_PRId32 ";%" B_PRId32 "H", y, x);
94 	WriteAt(NULL, 0LL, buffer, len);
95 }
96 
97 
98 void
99 VTConsole::SetCursorVisible(bool visible)
100 {
101 	// TODO?
102 }
103 
104 
105 void
106 VTConsole::SetColors(int32 foreground, int32 background)
107 {
108 	static const char cmap[] = {
109 		0, 4, 2, 6, 1, 5, 3, 7 };
110 	char buffer[12];
111 
112 	if (foreground < 0 || foreground >= 8)
113 		return;
114 	if (background < 0 || background >= 8)
115 		return;
116 
117 	// We assume normal display attributes here
118 	int len = snprintf(buffer, sizeof(buffer),
119 		"\033[%" B_PRId32 ";%" B_PRId32 "m",
120 		cmap[foreground] + 30, cmap[background] + 40);
121 
122 	WriteAt(NULL, 0LL, buffer, len);
123 }
124 
125 
126 //     #pragma mark -
127 
128 
129 SerialConsole::SerialConsole()
130 	: VTConsole()
131 {
132 }
133 
134 
135 ssize_t
136 SerialConsole::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize)
137 {
138 	// don't seek in character devices
139 	// not implemented (and not yet? needed)
140 	return B_ERROR;
141 }
142 
143 
144 ssize_t
145 SerialConsole::WriteAt(void *cookie, off_t /*pos*/, const void *buffer,
146 	size_t bufferSize)
147 {
148 	serial_puts((const char *)buffer, bufferSize);
149 	return bufferSize;
150 }
151 
152 
153 //     #pragma mark -
154 
155 
156 int
157 console_wait_for_key(void)
158 {
159 	union key key;
160 	key.ax = serial_getc(true);
161 	return key.code.ascii;
162 }
163 
164 
165 status_t
166 console_init(void)
167 {
168 	gConsoleNode = &sSerial;
169 	stdin = (FILE *)&sSerial;
170 	stdout = (FILE *)&sSerial;
171 	stderr = (FILE *)&sSerial;
172 	return B_OK;
173 }
174