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