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 /* 49 static void 50 scroll_up() 51 { 52 } 53 */ 54 55 56 // #pragma mark - 57 58 59 Console::Console() 60 : ConsoleNode() 61 { 62 } 63 64 65 ssize_t 66 Console::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) 67 { 68 // don't seek in character devices 69 // not implemented (and not yet? needed) 70 return B_ERROR; 71 } 72 73 74 ssize_t 75 Console::WriteAt(void *cookie, off_t /*pos*/, const void *buffer, size_t bufferSize) 76 { 77 return 0; 78 } 79 80 81 // #pragma mark - 82 83 84 VTConsole::VTConsole() 85 : ConsoleNode() 86 { 87 } 88 89 void 90 VTConsole::ClearScreen() 91 { 92 WriteAt(NULL, 0LL, "\033E", 2); 93 } 94 95 96 void 97 VTConsole::SetCursor(int32 x, int32 y) 98 { 99 char buff[] = "\033Y "; 100 x = MIN(79,MAX(0,x)); 101 y = MIN(24,MAX(0,y)); 102 buff[3] += (char)x; 103 buff[2] += (char)y; 104 WriteAt(NULL, 0LL, buff, 4); 105 } 106 107 108 void 109 VTConsole::SetColor(int32 foreground, int32 background) 110 { 111 static const char cmap[] = { 112 15, 4, 2, 6, 1, 5, 3, 7, 113 8, 12, 10, 14, 9, 13, 11, 0 }; 114 char buff[] = "\033b \033c "; 115 116 if (foreground < 0 && foreground >= 16) 117 return; 118 if (background < 0 && background >= 16) 119 return; 120 121 buff[2] += cmap[foreground]; 122 buff[5] += cmap[background]; 123 WriteAt(NULL, 0LL, buff, 6); 124 } 125 126 127 // #pragma mark - 128 129 130 SerialConsole::SerialConsole() 131 : VTConsole() 132 { 133 } 134 135 136 ssize_t 137 SerialConsole::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) 138 { 139 // don't seek in character devices 140 // not implemented (and not yet? needed) 141 return B_ERROR; 142 } 143 144 145 ssize_t 146 SerialConsole::WriteAt(void *cookie, off_t /*pos*/, const void *buffer, size_t bufferSize) 147 { 148 serial_puts((const char *)buffer, bufferSize); 149 return bufferSize; 150 } 151 152 153 // #pragma mark - 154 155 156 void 157 console_clear_screen(void) 158 { 159 sSerial.ClearScreen(); 160 } 161 162 163 int32 164 console_width(void) 165 { 166 return 80; 167 } 168 169 170 int32 171 console_height(void) 172 { 173 return 25; 174 } 175 176 177 void 178 console_set_cursor(int32 x, int32 y) 179 { 180 sSerial.SetCursor(x, y); 181 } 182 183 184 void 185 console_show_cursor(void) 186 { 187 } 188 189 190 void 191 console_hide_cursor(void) 192 { 193 } 194 195 196 void 197 console_set_color(int32 foreground, int32 background) 198 { 199 sSerial.SetColor(foreground, background); 200 } 201 202 203 int 204 console_wait_for_key(void) 205 { 206 return 0; 207 } 208 209 210 status_t 211 console_init(void) 212 { 213 stdin = (FILE *)&sSerial; 214 stdout = stderr = (FILE *)&sSerial; 215 return B_OK; 216 } 217 218