Subversion Repositories HelenOS

Rev

Rev 3769 | Rev 4142 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006 Ondrej Palkovsky
  3.  * Copyright (c) 2008 Martin Decky
  4.  * Copyright (c) 2008 Pavel Rimsky
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * - Redistributions of source code must retain the above copyright
  12.  *   notice, this list of conditions and the following disclaimer.
  13.  * - Redistributions in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in the
  15.  *   documentation and/or other materials provided with the distribution.
  16.  * - The name of the author may not be used to endorse or promote products
  17.  *   derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. /**
  32.  * @defgroup serial Serial console
  33.  * @brief    Serial console services (putc, puts, clear screen, cursor goto,...)
  34.  * @{
  35.  */
  36.  
  37. /** @file
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <ipc/ipc.h>
  42. #include <async.h>
  43. #include <ipc/fb.h>
  44. #include <bool.h>
  45. #include <errno.h>
  46. #include <console/color.h>
  47. #include <console/style.h>
  48.  
  49. #include "../console/screenbuffer.h"
  50. #include "main.h"
  51. #include "serial_console.h"
  52.  
  53. #define MAX_CONTROL 20
  54.  
  55. static void serial_sgr(const unsigned int mode);
  56.  
  57. static int scr_width;
  58. static int scr_height;
  59. static bool color = true;   /** True if producing color output. */
  60. static putc_function_t putc_function;
  61.  
  62. /* Allow only 1 connection */
  63. static int client_connected = 0;
  64.  
  65. enum sgr_color_index {
  66.     CI_BLACK    = 0,
  67.     CI_RED      = 1,
  68.     CI_GREEN    = 2,
  69.     CI_BROWN    = 3,
  70.     CI_BLUE     = 4,
  71.     CI_MAGENTA  = 5,
  72.     CI_CYAN     = 6,
  73.     CI_WHITE    = 7,
  74. };
  75.  
  76. enum sgr_command {
  77.     SGR_RESET   = 0,
  78.     SGR_BOLD    = 1,
  79.     SGR_BLINK   = 5,
  80.     SGR_REVERSE = 7,
  81.     SGR_NORMAL_INT  = 22,
  82.     SGR_BLINK_OFF   = 25,
  83.     SGR_REVERSE_OFF = 27,
  84.     SGR_FGCOLOR = 30,
  85.     SGR_BGCOLOR = 40
  86. };
  87.  
  88. static int color_map[] = {
  89.     [COLOR_BLACK]   = CI_BLACK,
  90.     [COLOR_BLUE]    = CI_RED,
  91.     [COLOR_GREEN]   = CI_GREEN,
  92.     [COLOR_CYAN]    = CI_CYAN,
  93.     [COLOR_RED] = CI_RED,
  94.     [COLOR_MAGENTA] = CI_MAGENTA,
  95.     [COLOR_YELLOW]  = CI_BROWN,
  96.     [COLOR_WHITE]   = CI_WHITE
  97. };
  98.  
  99. void serial_puts(char *str)
  100. {
  101.     while (*str)
  102.         putc_function(*(str++));
  103. }
  104.  
  105. void serial_goto(const unsigned int row, const unsigned int col)
  106. {
  107.     if ((row > scr_height) || (col > scr_width))
  108.         return;
  109.    
  110.     char control[MAX_CONTROL];
  111.     snprintf(control, MAX_CONTROL, "\033[%u;%uf", row + 1, col + 1);
  112.     serial_puts(control);
  113. }
  114.  
  115. void serial_clrscr(void)
  116. {
  117.     /* Initialize graphic rendition attributes. */
  118.     serial_sgr(SGR_RESET);
  119.     if (color) {
  120.         serial_sgr(SGR_FGCOLOR + CI_BLACK);
  121.         serial_sgr(SGR_BGCOLOR + CI_WHITE);
  122.     }
  123.  
  124.     serial_puts("\033[2J");
  125. }
  126.  
  127. void serial_scroll(int i)
  128. {
  129.     if (i > 0) {
  130.         serial_goto(scr_height - 1, 0);
  131.         while (i--)
  132.             serial_puts("\033D");
  133.     } else if (i < 0) {
  134.         serial_goto(0, 0);
  135.         while (i++)
  136.             serial_puts("\033M");
  137.     }
  138. }
  139.  
  140. /** ECMA-48 Set Graphics Rendition. */
  141. static void serial_sgr(const unsigned int mode)
  142. {
  143.     char control[MAX_CONTROL];
  144.     snprintf(control, MAX_CONTROL, "\033[%um", mode);
  145.     serial_puts(control);
  146. }
  147.  
  148. /** Set scrolling region. */
  149. void serial_set_scroll_region(unsigned last_row)
  150. {
  151.     char control[MAX_CONTROL];
  152.     snprintf(control, MAX_CONTROL, "\033[0;%ur", last_row);
  153.     serial_puts(control);
  154. }
  155.  
  156. void serial_cursor_disable(void)
  157. {
  158.     serial_puts("\033[?25l");
  159. }
  160.  
  161. void serial_cursor_enable(void)
  162. {
  163.     serial_puts("\033[?25h");
  164. }
  165.  
  166. void serial_console_init(putc_function_t putc_fn, uint32_t w, uint32_t h)
  167. {
  168.     scr_width = w;
  169.     scr_height = h;
  170.     putc_function = putc_fn;
  171. }
  172.  
  173. static void serial_set_style(int style)
  174. {
  175.     if (style == STYLE_EMPHASIS) {
  176.         if (color) {
  177.             serial_sgr(SGR_RESET);
  178.             serial_sgr(SGR_FGCOLOR + CI_RED);
  179.             serial_sgr(SGR_BGCOLOR + CI_WHITE);
  180.         }
  181.         serial_sgr(SGR_BOLD);
  182.     } else {
  183.         if (color) {
  184.             serial_sgr(SGR_RESET);
  185.             serial_sgr(SGR_FGCOLOR + CI_BLACK);
  186.             serial_sgr(SGR_BGCOLOR + CI_WHITE);
  187.         }
  188.         serial_sgr(SGR_NORMAL_INT);
  189.     }
  190. }
  191.  
  192. static void serial_set_idx(unsigned fgcolor, unsigned bgcolor,
  193.     unsigned flags)
  194. {
  195.     if (color) {
  196.         serial_sgr(SGR_RESET);
  197.         serial_sgr(SGR_FGCOLOR + color_map[fgcolor]);
  198.         serial_sgr(SGR_BGCOLOR + color_map[bgcolor]);
  199.     } else {
  200.         if (fgcolor < bgcolor)
  201.             serial_sgr(SGR_RESET);
  202.         else
  203.             serial_sgr(SGR_REVERSE);
  204.     }  
  205. }
  206.  
  207. static void serial_set_rgb(uint32_t fgcolor, uint32_t bgcolor)
  208. {
  209.     if (fgcolor < bgcolor)
  210.         serial_sgr(SGR_REVERSE_OFF);
  211.     else
  212.         serial_sgr(SGR_REVERSE);   
  213. }
  214.  
  215. static void serial_set_attrs(const attrs_t *a)
  216. {
  217.     switch (a->t) {
  218.     case at_style: serial_set_style(a->a.s.style); break;
  219.     case at_rgb: serial_set_rgb(a->a.r.fg_color, a->a.r.bg_color); break;
  220.     case at_idx: serial_set_idx(a->a.i.fg_color,
  221.         a->a.i.bg_color, a->a.i.flags); break;
  222.     default: break;
  223.     }
  224. }
  225.  
  226. static void draw_text_data(keyfield_t *data)
  227. {
  228.     int i, j;
  229.     attrs_t *a0, *a1;
  230.  
  231.     serial_goto(0, 0);
  232.     a0 = &data[0].attrs;
  233.     serial_set_attrs(a0);
  234.  
  235.     for (i = 0; i < scr_height; i++) {
  236.         for (j = 0; j < scr_width; j++) {
  237.             a1 = &data[i * scr_width + j].attrs;
  238.             if (!attrs_same(*a0, *a1))
  239.                 serial_set_attrs(a1);
  240.             (*putc_function)(data[i * scr_width + j].character);
  241.             a0 = a1;
  242. /*      scr_addr[i * 2] = data[i].character;
  243.         scr_addr[i * 2 + 1] = attr_to_ega_style(&data[i].attrs);*/
  244.         }
  245.     }
  246. }
  247.  
  248. /**
  249.  * Main function of the thread serving client connections.
  250.  */
  251. void serial_client_connection(ipc_callid_t iid, ipc_call_t *icall)
  252. {
  253.     int retval;
  254.     ipc_callid_t callid;
  255.     ipc_call_t call;
  256.     keyfield_t *interbuf = NULL;
  257.     size_t intersize = 0;
  258.  
  259.     char c;
  260.     int lastcol = 0;
  261.     int lastrow = 0;
  262.     int newcol;
  263.     int newrow;
  264.     int fgcolor;
  265.     int bgcolor;
  266.     int flags;
  267.     int style;
  268.     int i;
  269.  
  270.    
  271.     if (client_connected) {
  272.         ipc_answer_0(iid, ELIMIT);
  273.         return;
  274.     }
  275.    
  276.     client_connected = 1;
  277.     ipc_answer_0(iid, EOK);
  278.    
  279.     /* Clear the terminal, set scrolling region
  280.        to 0 - height rows. */
  281.     serial_clrscr();
  282.     serial_goto(0, 0);
  283.     serial_set_scroll_region(scr_height);
  284.    
  285.     while (true) {
  286.         callid = async_get_call(&call);
  287.         switch (IPC_GET_METHOD(call)) {
  288.         case IPC_M_PHONE_HUNGUP:
  289.             client_connected = 0;
  290.             ipc_answer_0(callid, EOK);
  291.             return;
  292.         case IPC_M_SHARE_OUT:
  293.             /* We accept one area for data interchange */
  294.             intersize = IPC_GET_ARG2(call);
  295.             if (intersize >= scr_width * scr_height *
  296.                 sizeof(*interbuf)) {
  297.                 receive_comm_area(callid, &call,
  298.                     (void *) &interbuf);
  299.                 continue;
  300.             }
  301.             retval = EINVAL;
  302.             break;
  303.         case FB_DRAW_TEXT_DATA:
  304.             if (!interbuf) {
  305.                 retval = EINVAL;
  306.                 break;
  307.             }
  308.             draw_text_data(interbuf);
  309.             retval = 0;
  310.             break;
  311.         case FB_PUTCHAR:
  312.             c = IPC_GET_ARG1(call);
  313.             newrow = IPC_GET_ARG2(call);
  314.             newcol = IPC_GET_ARG3(call);
  315.             if ((lastcol != newcol) || (lastrow != newrow))
  316.                 serial_goto(newrow, newcol);
  317.             lastcol = newcol + 1;
  318.             lastrow = newrow;
  319.             (*putc_function)(c);
  320.             retval = 0;
  321.             break;
  322.         case FB_CURSOR_GOTO:
  323.             newrow = IPC_GET_ARG1(call);
  324.             newcol = IPC_GET_ARG2(call);
  325.             serial_goto(newrow, newcol);
  326.             lastrow = newrow;
  327.             lastcol = newcol;
  328.             retval = 0;
  329.             break;
  330.         case FB_GET_CSIZE:
  331.             ipc_answer_2(callid, EOK, scr_height, scr_width);
  332.             continue;
  333.         case FB_CLEAR:
  334.             serial_clrscr();
  335.             retval = 0;
  336.             break;
  337.         case FB_SET_STYLE:
  338.             style = IPC_GET_ARG1(call);
  339.             serial_set_style(style);
  340.             retval = 0;
  341.             break;
  342.         case FB_SET_COLOR:
  343.             fgcolor = IPC_GET_ARG1(call);
  344.             bgcolor = IPC_GET_ARG2(call);
  345.             flags = IPC_GET_ARG3(call);
  346.  
  347.             serial_set_idx(fgcolor, bgcolor, flags);
  348.             retval = 0;
  349.             break;
  350.         case FB_SET_RGB_COLOR:
  351.             fgcolor = IPC_GET_ARG1(call);
  352.             bgcolor = IPC_GET_ARG2(call);
  353.  
  354.             serial_set_rgb(fgcolor, bgcolor);
  355.             retval = 0;
  356.             break;
  357.         case FB_SCROLL:
  358.             i = IPC_GET_ARG1(call);
  359.             if ((i > scr_height) || (i < -scr_height)) {
  360.                 retval = EINVAL;
  361.                 break;
  362.             }
  363.             serial_scroll(i);
  364.             serial_goto(lastrow, lastcol);
  365.             retval = 0;
  366.             break;
  367.         case FB_CURSOR_VISIBILITY:
  368.             if(IPC_GET_ARG1(call))
  369.                 serial_cursor_enable();
  370.             else
  371.                 serial_cursor_disable();
  372.             retval = 0;
  373.             break;
  374.         default:
  375.             retval = ENOENT;
  376.         }
  377.         ipc_answer_0(callid, retval);
  378.     }
  379. }
  380.  
  381. /**
  382.  * @}
  383.  */
  384.