Changes in uspace/srv/hid/fb/serial_console.c [369a5f8:19f857a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/hid/fb/serial_console.c
r369a5f8 r19f857a 31 31 /** 32 32 * @defgroup serial Serial console 33 * @brief Serial console services (putc, puts, clear screen, cursor goto,...)33 * @brief Serial console services (putc, puts, clear screen, cursor goto,...) 34 34 * @{ 35 */ 35 */ 36 36 37 37 /** @file … … 47 47 #include <io/style.h> 48 48 #include <str.h> 49 #include <io/screenbuffer.h> 50 49 50 #include "../console/screenbuffer.h" 51 51 #include "main.h" 52 52 #include "serial_console.h" … … 54 54 #define MAX_CONTROL 20 55 55 56 static ipcarg_t scr_width; 57 static ipcarg_t scr_height; 58 static bool color = true; /**< True if producing color output. */ 59 static bool utf8 = false; /**< True if producing UTF8 output. */ 56 static void serial_sgr(const unsigned int mode); 57 void serial_putchar(wchar_t ch); 58 59 static unsigned int scr_width; 60 static unsigned int scr_height; 61 static bool color = true; /** True if producing color output. */ 62 static bool utf8 = false; /** True if producing UTF8 output. */ 60 63 static putc_function_t putc_function; 61 62 static ipcarg_t lastcol = 0;63 static ipcarg_t lastrow = 0;64 static attrs_t cur_attr = {65 .t = at_style,66 .a.s.style = STYLE_NORMAL67 };68 64 69 65 /* Allow only 1 connection */ … … 71 67 72 68 enum sgr_color_index { 73 CI_BLACK 74 CI_RED 75 CI_GREEN 76 CI_BROWN 77 CI_BLUE 78 CI_MAGENTA 79 CI_CYAN 80 CI_WHITE = 769 CI_BLACK = 0, 70 CI_RED = 1, 71 CI_GREEN = 2, 72 CI_BROWN = 3, 73 CI_BLUE = 4, 74 CI_MAGENTA = 5, 75 CI_CYAN = 6, 76 CI_WHITE = 7, 81 77 }; 82 78 83 79 enum sgr_command { 84 SGR_RESET 85 SGR_BOLD 86 SGR_BLINK 87 SGR_REVERSE 88 SGR_NORMAL_INT 89 SGR_BLINK_OFF 80 SGR_RESET = 0, 81 SGR_BOLD = 1, 82 SGR_BLINK = 5, 83 SGR_REVERSE = 7, 84 SGR_NORMAL_INT = 22, 85 SGR_BLINK_OFF = 25, 90 86 SGR_REVERSE_OFF = 27, 91 SGR_FGCOLOR 92 SGR_BGCOLOR 87 SGR_FGCOLOR = 30, 88 SGR_BGCOLOR = 40 93 89 }; 94 90 95 91 static int color_map[] = { 96 [COLOR_BLACK] 97 [COLOR_BLUE] 98 [COLOR_GREEN] 99 [COLOR_CYAN] 100 [COLOR_RED] 92 [COLOR_BLACK] = CI_BLACK, 93 [COLOR_BLUE] = CI_RED, 94 [COLOR_GREEN] = CI_GREEN, 95 [COLOR_CYAN] = CI_CYAN, 96 [COLOR_RED] = CI_RED, 101 97 [COLOR_MAGENTA] = CI_MAGENTA, 102 [COLOR_YELLOW] 103 [COLOR_WHITE] 98 [COLOR_YELLOW] = CI_BROWN, 99 [COLOR_WHITE] = CI_WHITE 104 100 }; 105 101 … … 110 106 } 111 107 112 static void serial_putchar(wchar_t ch) 113 { 108 void serial_putchar(wchar_t ch) 109 { 110 char buf[STR_BOUNDS(1)]; 111 size_t offs; 112 size_t i; 113 114 114 if (utf8 != true) { 115 115 if (ch >= 0 && ch < 128) 116 116 (*putc_function)((uint8_t) ch); 117 else 117 else 118 118 (*putc_function)('?'); 119 120 119 return; 121 120 } 122 123 size_t offs = 0; 124 char buf[STR_BOUNDS(1)]; 121 122 offs = 0; 125 123 if (chr_encode(ch, buf, &offs, STR_BOUNDS(1)) == EOK) { 126 size_t i;127 124 for (i = 0; i < offs; i++) 128 125 (*putc_function)(buf[i]); 129 } else 126 } else { 130 127 (*putc_function)('?'); 131 } 132 133 void serial_goto(const ipcarg_t col, const ipcarg_t row) 128 } 129 130 } 131 132 void serial_goto(const unsigned int col, const unsigned int row) 134 133 { 135 134 if ((col > scr_width) || (row > scr_height)) … … 141 140 } 142 141 142 void serial_clrscr(void) 143 { 144 /* Initialize graphic rendition attributes. */ 145 serial_sgr(SGR_RESET); 146 if (color) { 147 serial_sgr(SGR_FGCOLOR + CI_BLACK); 148 serial_sgr(SGR_BGCOLOR + CI_WHITE); 149 } 150 151 serial_puts("\033[2J"); 152 } 153 154 void serial_scroll(int i) 155 { 156 if (i > 0) { 157 serial_goto(0, scr_height - 1); 158 while (i--) 159 serial_puts("\033D"); 160 } else if (i < 0) { 161 serial_goto(0, 0); 162 while (i++) 163 serial_puts("\033M"); 164 } 165 } 166 143 167 /** ECMA-48 Set Graphics Rendition. */ 144 168 static void serial_sgr(const unsigned int mode) … … 149 173 } 150 174 151 static void serial_set_style(console_style_t style) 152 { 153 switch (style) { 154 case STYLE_EMPHASIS: 175 /** Set scrolling region. */ 176 void serial_set_scroll_region(unsigned last_row) 177 { 178 char control[MAX_CONTROL]; 179 snprintf(control, MAX_CONTROL, "\033[0;%ur", last_row); 180 serial_puts(control); 181 } 182 183 void serial_cursor_disable(void) 184 { 185 serial_puts("\033[?25l"); 186 } 187 188 void serial_cursor_enable(void) 189 { 190 serial_puts("\033[?25h"); 191 } 192 193 void serial_console_init(putc_function_t putc_fn, uint32_t w, uint32_t h) 194 { 195 scr_width = w; 196 scr_height = h; 197 putc_function = putc_fn; 198 } 199 200 static void serial_set_style(int style) 201 { 202 if (style == STYLE_EMPHASIS) { 155 203 if (color) { 156 204 serial_sgr(SGR_RESET); … … 159 207 } 160 208 serial_sgr(SGR_BOLD); 161 break; 162 case STYLE_INVERTED: 163 if (color) { 164 serial_sgr(SGR_RESET); 165 serial_sgr(SGR_FGCOLOR + CI_WHITE); 166 serial_sgr(SGR_BGCOLOR + CI_BLACK); 167 serial_sgr(SGR_NORMAL_INT); 168 } else 169 serial_sgr(SGR_REVERSE); 170 break; 171 case STYLE_SELECTED: 172 if (color) { 173 serial_sgr(SGR_RESET); 174 serial_sgr(SGR_FGCOLOR + CI_WHITE); 175 serial_sgr(SGR_BGCOLOR + CI_RED); 176 serial_sgr(SGR_NORMAL_INT); 177 } else { 178 serial_sgr(SGR_BOLD); 179 serial_sgr(SGR_REVERSE); 180 } 181 break; 182 default: 209 } else { 183 210 if (color) { 184 211 serial_sgr(SGR_RESET); … … 190 217 } 191 218 192 static void serial_set_idx(u int8_t fgcolor, uint8_tbgcolor,193 u int8_tflags)219 static void serial_set_idx(unsigned fgcolor, unsigned bgcolor, 220 unsigned flags) 194 221 { 195 222 if (color) { … … 207 234 static void serial_set_rgb(uint32_t fgcolor, uint32_t bgcolor) 208 235 { 209 serial_sgr(SGR_RESET);210 211 236 if (fgcolor < bgcolor) 212 237 serial_sgr(SGR_REVERSE_OFF); … … 215 240 } 216 241 217 static void serial_set_attrs( attrs_t *a)242 static void serial_set_attrs(const attrs_t *a) 218 243 { 219 244 switch (a->t) { … … 225 250 break; 226 251 case at_idx: 227 serial_set_idx(a->a.i.fg_color, a->a.i.bg_color,228 a->a.i. flags);252 serial_set_idx(a->a.i.fg_color, 253 a->a.i.bg_color, a->a.i.flags); 229 254 break; 230 } 231 } 232 233 void serial_clrscr(void) 234 { 235 /* Initialize graphic rendition attributes. */ 236 serial_sgr(SGR_RESET); 237 if (color) { 238 serial_sgr(SGR_FGCOLOR + CI_BLACK); 239 serial_sgr(SGR_BGCOLOR + CI_WHITE); 240 } 241 242 serial_puts("\033[2J"); 243 244 serial_set_attrs(&cur_attr); 245 } 246 247 void serial_scroll(ssize_t i) 248 { 249 if (i > 0) { 250 serial_goto(0, scr_height - 1); 251 while (i--) 252 serial_puts("\033D"); 253 } else if (i < 0) { 254 serial_goto(0, 0); 255 while (i++) 256 serial_puts("\033M"); 257 } 258 } 259 260 /** Set scrolling region. */ 261 void serial_set_scroll_region(ipcarg_t last_row) 262 { 263 char control[MAX_CONTROL]; 264 snprintf(control, MAX_CONTROL, "\033[0;%ur", last_row); 265 serial_puts(control); 266 } 267 268 void serial_cursor_disable(void) 269 { 270 serial_puts("\033[?25l"); 271 } 272 273 void serial_cursor_enable(void) 274 { 275 serial_puts("\033[?25h"); 276 } 277 278 void serial_console_init(putc_function_t putc_fn, ipcarg_t w, ipcarg_t h) 279 { 280 scr_width = w; 281 scr_height = h; 282 putc_function = putc_fn; 283 } 284 285 255 default: 256 break; 257 } 258 } 286 259 287 260 /** Draw text data to viewport. … … 289 262 * @param vport Viewport id 290 263 * @param data Text data. 291 * @param x Leftmost column of the area. 292 * @param y Topmost row of the area. 293 * @param w Number of rows. 294 * @param h Number of columns. 295 * 264 * @param x Leftmost column of the area. 265 * @param y Topmost row of the area. 266 * @param w Number of rows. 267 * @param h Number of columns. 296 268 */ 297 static void draw_text_data(keyfield_t *data, ipcarg_t x, ipcarg_t y, 298 ipcarg_t w, ipcarg_t h) 299 { 269 static void draw_text_data(keyfield_t *data, unsigned int x, 270 unsigned int y, unsigned int w, unsigned int h) 271 { 272 unsigned int i, j; 273 keyfield_t *field; 274 attrs_t *a0, *a1; 275 300 276 serial_goto(x, y); 301 ipcarg_t i; 302 ipcarg_t j; 303 304 attrs_t *a0 = &data[0].attrs; 305 277 a0 = &data[0].attrs; 278 serial_set_attrs(a0); 279 306 280 for (j = 0; j < h; j++) { 307 if ( (j > 0) && (w != scr_width))281 if (j > 0 && w != scr_width) 308 282 serial_goto(x, j); 309 283 310 284 for (i = 0; i < w; i++) { 311 attrs_t *a1 = &data[j * w + i].attrs; 312 313 if (!attrs_same(*a0, *a1)) { 285 field = &data[j * w + i]; 286 287 a1 = &field->attrs; 288 if (!attrs_same(*a0, *a1)) 314 289 serial_set_attrs(a1); 315 a0 = a1; 316 } 317 318 serial_putchar(data[j * w + i].character); 290 serial_putchar(field->character); 291 a0 = a1; 319 292 } 320 293 } 321 294 } 295 296 unsigned int lastcol = 0; 297 unsigned int lastrow = 0; 322 298 323 299 /** … … 326 302 void serial_client_connection(ipc_callid_t iid, ipc_call_t *icall) 327 303 { 304 int retval; 305 ipc_callid_t callid; 306 ipc_call_t call; 328 307 keyfield_t *interbuf = NULL; 329 308 size_t intersize = 0; 309 310 wchar_t c; 311 unsigned int col; 312 unsigned int row; 313 unsigned int w; 314 unsigned int h; 315 int i; 316 317 attrs_t cur_attr; 330 318 331 319 if (client_connected) { … … 336 324 client_connected = 1; 337 325 ipc_answer_0(iid, EOK); 326 327 cur_attr.t = at_style; 328 cur_attr.a.s.style = STYLE_NORMAL; 338 329 339 330 /* Clear the terminal, set scrolling region … … 344 335 345 336 while (true) { 346 ipc_call_t call; 347 ipc_callid_t callid = async_get_call(&call); 348 349 wchar_t c; 350 ipcarg_t col; 351 ipcarg_t row; 352 ipcarg_t w; 353 ipcarg_t h; 354 attrs_t attr; 355 ssize_t rows; 356 357 int retval; 358 337 callid = async_get_call(&call); 359 338 switch (IPC_GET_METHOD(call)) { 360 339 case IPC_M_PHONE_HUNGUP: 361 340 client_connected = 0; 362 341 ipc_answer_0(callid, EOK); 363 364 /* Exit thread */365 342 return; 366 343 case IPC_M_SHARE_OUT: … … 373 350 continue; 374 351 } 375 376 352 retval = EINVAL; 377 353 break; … … 381 357 w = IPC_GET_ARG3(call); 382 358 h = IPC_GET_ARG4(call); 383 384 359 if (!interbuf) { 385 360 retval = EINVAL; 386 361 break; 387 362 } 388 389 363 if ((col + w > scr_width) || (row + h > scr_height)) { 390 364 retval = EINVAL; 391 365 break; 392 366 } 393 394 367 draw_text_data(interbuf, col, row, w, h); 395 368 lastcol = col + w; … … 401 374 col = IPC_GET_ARG2(call); 402 375 row = IPC_GET_ARG3(call); 403 404 376 if ((lastcol != col) || (lastrow != row)) 405 377 serial_goto(col, row); 406 407 378 lastcol = col + 1; 408 379 lastrow = row; … … 430 401 break; 431 402 case FB_SET_STYLE: 432 attr.t = at_style; 433 attr.a.s.style = IPC_GET_ARG1(call); 403 cur_attr.t = at_style; 404 cur_attr.a.s.style = IPC_GET_ARG1(call); 405 cur_attr.a.i.bg_color = IPC_GET_ARG2(call); 434 406 serial_set_attrs(&cur_attr); 407 435 408 retval = 0; 436 409 break; 437 410 case FB_SET_COLOR: 438 attr.t = at_idx;439 attr.a.i.fg_color = IPC_GET_ARG1(call);440 attr.a.i.bg_color = IPC_GET_ARG2(call);441 attr.a.i.flags = IPC_GET_ARG3(call);411 cur_attr.t = at_idx; 412 cur_attr.a.i.fg_color = IPC_GET_ARG1(call); 413 cur_attr.a.i.bg_color = IPC_GET_ARG2(call); 414 cur_attr.a.i.flags = IPC_GET_ARG3(call); 442 415 serial_set_attrs(&cur_attr); 416 443 417 retval = 0; 444 418 break; 445 419 case FB_SET_RGB_COLOR: 446 attr.t = at_rgb;447 attr.a.r.fg_color = IPC_GET_ARG1(call);448 attr.a.r.bg_color = IPC_GET_ARG2(call);420 cur_attr.t = at_rgb; 421 cur_attr.a.i.fg_color = IPC_GET_ARG1(call); 422 cur_attr.a.i.bg_color = IPC_GET_ARG2(call); 449 423 serial_set_attrs(&cur_attr); 424 450 425 retval = 0; 451 426 break; 452 427 case FB_SCROLL: 453 rows = IPC_GET_ARG1(call); 454 455 if (rows >= 0) { 456 if ((ipcarg_t) rows > scr_height) { 457 retval = EINVAL; 458 break; 459 } 460 } else { 461 if ((ipcarg_t) (-rows) > scr_height) { 462 retval = EINVAL; 463 break; 464 } 428 i = IPC_GET_ARG1(call); 429 if ((i > (int) scr_height) || (i < -((int) scr_height))) { 430 retval = EINVAL; 431 break; 465 432 } 466 467 serial_scroll(rows); 433 serial_scroll(i); 468 434 serial_goto(lastcol, lastrow); 469 435 retval = 0; … … 485 451 case FB_SCREEN_RECLAIM: 486 452 serial_clrscr(); 453 serial_set_attrs(&cur_attr); 487 454 retval = 0; 488 455 break;
Note:
See TracChangeset
for help on using the changeset viewer.