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
21class VTConsole : public ConsoleNode {
22public:
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
34class SerialConsole : public VTConsole {
35public:
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
45extern ConsoleNode* gConsoleNode;
46static SerialConsole sSerial;
47
48FILE* stdin;
49FILE* stdout;
50FILE* stderr;
51
52
53//	#pragma mark -
54
55
56VTConsole::VTConsole()
57	:
58	ConsoleNode()
59{
60}
61
62
63void
64VTConsole::ClearScreen()
65{
66	WriteAt(NULL, 0LL, "\033[2J", 4);
67}
68
69
70int32
71VTConsole::Width()
72{
73	// TODO?
74	return 80;
75}
76
77
78int32
79VTConsole::Height()
80{
81	// TODO?
82	return 25;
83}
84
85
86void
87VTConsole::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
98void
99VTConsole::SetCursorVisible(bool visible)
100{
101	// TODO?
102}
103
104
105void
106VTConsole::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
129SerialConsole::SerialConsole()
130	: VTConsole()
131{
132}
133
134
135ssize_t
136SerialConsole::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
144ssize_t
145SerialConsole::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
156int
157console_wait_for_key(void)
158{
159	union key key;
160	key.ax = serial_getc(true);
161	return key.code.ascii;
162}
163
164
165status_t
166console_init(void)
167{
168	gConsoleNode = &sSerial;
169	stdin = (FILE *)&sSerial;
170	stdout = (FILE *)&sSerial;
171	stderr = (FILE *)&sSerial;
172	return B_OK;
173}
174