Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2006 Josef Cejka
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup console
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <libc.h>
  36. #include <fb.h>
  37. #include <ipc/ipc.h>
  38. #include <keys.h>
  39. #include <ipc/fb.h>
  40. #include <ipc/services.h>
  41. #include <errno.h>
  42. #include <key_buffer.h>
  43. #include <console.h>
  44. #include <unistd.h>
  45. #include <async.h>
  46. #include <libadt/fifo.h>
  47. #include <screenbuffer.h>
  48. #include <sys/mman.h>
  49.  
  50. #include "gcons.h"
  51.  
  52. #define MAX_KEYREQUESTS_BUFFERED 32
  53.  
  54. #define NAME "CONSOLE"
  55.  
  56. /** Index of currently used virtual console.
  57.  */
  58. int active_console = 0;
  59.  
  60. /** Information about framebuffer
  61.  */
  62. struct {
  63.     int phone;      /**< Framebuffer phone */
  64.     ipcarg_t rows;      /**< Framebuffer rows */
  65.     ipcarg_t cols;      /**< Framebuffer columns */
  66. } fb_info;
  67.  
  68. typedef struct {
  69.     keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
  70.     /** Buffer for unsatisfied request for keys. */
  71.     FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
  72.         MAX_KEYREQUESTS_BUFFERED); 
  73.     int keyrequest_counter;     /**< Number of requests in buffer. */
  74.     int client_phone;       /**< Phone to connected client. */
  75.     int used;           /**< 1 if this virtual console is
  76.                      * connected to some client.*/
  77.     screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
  78.                      * contents and related settings. */
  79. } connection_t;
  80.  
  81. static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
  82.                          * consoles */
  83. static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
  84.                          * with framebufer used for
  85.                          * faster virtual console
  86.                          * switching */
  87.  
  88. static int kernel_pixmap = -1;  /**< Number of fb pixmap, where kernel
  89.                  * console is stored */
  90.  
  91.  
  92. /** Find unused virtual console.
  93.  *
  94.  */
  95. static int find_free_connection(void)
  96. {
  97.     int i;
  98.    
  99.     for (i = 0; i < CONSOLE_COUNT; i++) {
  100.         if (!connections[i].used)
  101.             return i;
  102.     }
  103.     return -1;
  104. }
  105.  
  106. static void clrscr(void)
  107. {
  108.     async_msg(fb_info.phone, FB_CLEAR, 0);
  109. }
  110.  
  111. static void curs_visibility(int v)
  112. {
  113.     async_msg(fb_info.phone, FB_CURSOR_VISIBILITY, v);
  114. }
  115.  
  116. static void curs_goto(int row, int col)
  117. {
  118.     async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
  119. }
  120.  
  121. static void set_style(style_t *style)
  122. {
  123.     async_msg_2(fb_info.phone, FB_SET_STYLE, style->fg_color,
  124.         style->bg_color);
  125. }
  126.  
  127. static void set_style_col(int fgcolor, int bgcolor)
  128. {
  129.     async_msg_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor);
  130. }
  131.  
  132. static void prtchr(char c, int row, int col)
  133. {
  134.     async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
  135. }
  136.  
  137. /** Check key and process special keys.
  138.  *
  139.  *
  140.  */
  141. static void write_char(int console, char key)
  142. {
  143.     screenbuffer_t *scr = &(connections[console].screenbuffer);
  144.    
  145.     switch (key) {
  146.     case '\n':
  147.         scr->position_y++;
  148.         scr->position_x = 0;
  149.         break;
  150.     case '\r':
  151.         break;
  152.     case '\t':
  153.         scr->position_x += 8;
  154.         scr->position_x -= scr->position_x % 8;
  155.         break;
  156.     case '\b':
  157.         if (scr->position_x == 0)
  158.             break;
  159.         scr->position_x--;
  160.         if (console == active_console)
  161.             prtchr(' ', scr->position_y, scr->position_x);
  162.         screenbuffer_putchar(scr, ' ');
  163.         break;
  164.     default:   
  165.         if (console == active_console)
  166.             prtchr(key, scr->position_y, scr->position_x);
  167.  
  168.         screenbuffer_putchar(scr, key);
  169.         scr->position_x++;
  170.     }
  171.    
  172.     scr->position_y += (scr->position_x >= scr->size_x);
  173.    
  174.     if (scr->position_y >= scr->size_y) {
  175.         scr->position_y = scr->size_y - 1;
  176.         screenbuffer_clear_line(scr, scr->top_line);
  177.         scr->top_line = (scr->top_line + 1) % scr->size_y;
  178.         if (console == active_console)
  179.             async_msg(fb_info.phone, FB_SCROLL, 1);
  180.     }
  181.    
  182.     scr->position_x = scr->position_x % scr->size_x;
  183.    
  184.     if (console == active_console)
  185.         curs_goto(scr->position_y, scr->position_x);
  186.    
  187. }
  188.  
  189. /** Save current screen to pixmap, draw old pixmap
  190.  *
  191.  * @param oldpixmap Old pixmap
  192.  * @return ID of pixmap of current screen
  193.  */
  194. static int switch_screens(int oldpixmap)
  195. {
  196.     int newpmap;
  197.        
  198.     /* Save screen */
  199.     newpmap = async_req(fb_info.phone, FB_VP2PIXMAP, 0, NULL);
  200.     if (newpmap < 0)
  201.         return -1;
  202.  
  203.     if (oldpixmap != -1) {
  204.         /* Show old screen */
  205.         async_msg_2(fb_info.phone, FB_VP_DRAW_PIXMAP, 0, oldpixmap);
  206.         /* Drop old pixmap */
  207.         async_msg(fb_info.phone, FB_DROP_PIXMAP, oldpixmap);
  208.     }
  209.    
  210.     return newpmap;
  211. }
  212.  
  213. /** Switch to new console */
  214. static void change_console(int newcons)
  215. {
  216.     connection_t *conn;
  217.     static int console_pixmap = -1;
  218.     int i, j, rc;
  219.     keyfield_t *field;
  220.     style_t *style;
  221.  
  222.     if (newcons == active_console)
  223.         return;
  224.  
  225.     if (newcons == KERNEL_CONSOLE) {
  226.         if (active_console == KERNEL_CONSOLE)
  227.             return;
  228.         active_console = KERNEL_CONSOLE;
  229.         curs_visibility(0);
  230.  
  231.         async_serialize_start();
  232.         if (kernel_pixmap == -1) {
  233.             /* store/restore unsupported */
  234.             set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
  235.             clrscr();
  236.         } else {
  237.             gcons_in_kernel();
  238.             console_pixmap = switch_screens(kernel_pixmap);
  239.             kernel_pixmap = -1;
  240.         }
  241.         async_serialize_end();
  242.  
  243.         __SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE);
  244.         return;
  245.     }
  246.    
  247.     async_serialize_start();
  248.  
  249.     if (console_pixmap != -1) {
  250.         kernel_pixmap = switch_screens(console_pixmap);
  251.         console_pixmap = -1;
  252.     }
  253.     active_console = newcons;
  254.     gcons_change_console(newcons);
  255.     conn = &connections[active_console];
  256.  
  257.     set_style(&conn->screenbuffer.style);
  258.     curs_visibility(0);
  259.     if (interbuffer) {
  260.         for (i = 0; i < conn->screenbuffer.size_x; i++)
  261.             for (j = 0; j < conn->screenbuffer.size_y; j++) {
  262.                 unsigned int size_x;
  263.  
  264.                 size_x = conn->screenbuffer.size_x;
  265.                 interbuffer[i + j * size_x] =
  266.                     *get_field_at(&conn->screenbuffer, i, j);
  267.             }
  268.         /* This call can preempt, but we are already at the end */
  269.         rc = async_req_2(fb_info.phone, FB_DRAW_TEXT_DATA, 0, 0, NULL,
  270.             NULL);     
  271.     }
  272.    
  273.     if ((!interbuffer) || (rc != 0)) {
  274.         set_style(&conn->screenbuffer.style);
  275.         clrscr();
  276.         style = &conn->screenbuffer.style;
  277.  
  278.         for (j = 0; j < conn->screenbuffer.size_y; j++)
  279.             for (i = 0; i < conn->screenbuffer.size_x; i++) {
  280.                 field = get_field_at(&conn->screenbuffer, i,
  281.                     j);
  282.                 if (!style_same(*style, field->style))
  283.                     set_style(&field->style);
  284.                 style = &field->style;
  285.                 if ((field->character == ' ') &&
  286.                     (style_same(field->style,
  287.                     conn->screenbuffer.style)))
  288.                     continue;
  289.  
  290.                 prtchr(field->character, j, i);
  291.             }
  292.     }
  293.    
  294.     curs_goto(conn->screenbuffer.position_y,
  295.         conn->screenbuffer.position_x);
  296.     curs_visibility(conn->screenbuffer.is_cursor_visible);
  297.  
  298.     async_serialize_end();
  299. }
  300.  
  301. /** Handler for keyboard */
  302. static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
  303. {
  304.     ipc_callid_t callid;
  305.     ipc_call_t call;
  306.     int retval;
  307.     int c;
  308.     connection_t *conn;
  309.     int newcon;
  310.    
  311.     /* Ignore parameters, the connection is alread opened */
  312.     while (1) {
  313.         callid = async_get_call(&call);
  314.         switch (IPC_GET_METHOD(call)) {
  315.         case IPC_M_PHONE_HUNGUP:
  316.             /* TODO: Handle hangup */
  317.             return;
  318.         case KBD_MS_LEFT:
  319.             newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
  320.             if (newcon != -1)
  321.                 change_console(newcon);
  322.             retval = 0;
  323.             break;
  324.         case KBD_MS_MOVE:
  325.             gcons_mouse_move(IPC_GET_ARG1(call),
  326.                 IPC_GET_ARG2(call));
  327.             retval = 0;
  328.             break;
  329.         case KBD_PUSHCHAR:
  330.             /* got key from keyboard driver */
  331.            
  332.             retval = 0;
  333.             c = IPC_GET_ARG1(call);
  334.             /* switch to another virtual console */
  335.            
  336.             conn = &connections[active_console];
  337. /*
  338.  *          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 +
  339.  *              CONSOLE_COUNT)) {
  340.  */
  341.             if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
  342.                 if (c == 0x112)
  343.                     change_console(KERNEL_CONSOLE);
  344.                 else
  345.                     change_console(c - 0x101);
  346.                 break;
  347.             }
  348.            
  349.             /* if client is awaiting key, send it */
  350.             if (conn->keyrequest_counter > 0) {    
  351.                 conn->keyrequest_counter--;
  352.                 ipc_answer_fast(fifo_pop(conn->keyrequests), 0,
  353.                     c, 0);
  354.                 break;
  355.             }
  356.            
  357.             keybuffer_push(&conn->keybuffer, c);
  358.             retval = 0;
  359.            
  360.             break;
  361.         default:
  362.             retval = ENOENT;
  363.         }
  364.         ipc_answer_fast(callid, retval, 0, 0);
  365.     }
  366. }
  367.  
  368. /** Default thread for new connections */
  369. static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
  370. {
  371.     ipc_callid_t callid;
  372.     ipc_call_t call;
  373.     int consnum;
  374.     ipcarg_t arg1, arg2;
  375.     connection_t *conn;
  376.  
  377.     if ((consnum = find_free_connection()) == -1) {
  378.         ipc_answer_fast(iid, ELIMIT, 0, 0);
  379.         return;
  380.     }
  381.     conn = &connections[consnum];
  382.     conn->used = 1;
  383.    
  384.     async_serialize_start();
  385.     gcons_notify_connect(consnum);
  386.     conn->client_phone = IPC_GET_ARG3(call);
  387.     screenbuffer_clear(&conn->screenbuffer);
  388.    
  389.     /* Accept the connection */
  390.     ipc_answer_fast(iid, 0, 0, 0);
  391.  
  392.     while (1) {
  393.         async_serialize_end();
  394.         callid = async_get_call(&call);
  395.         async_serialize_start();
  396.  
  397.         arg1 = 0;
  398.         arg2 = 0;
  399.         switch (IPC_GET_METHOD(call)) {
  400.         case IPC_M_PHONE_HUNGUP:
  401.             gcons_notify_disconnect(consnum);
  402.            
  403.             /* Answer all pending requests */
  404.             while (conn->keyrequest_counter > 0) {     
  405.                 conn->keyrequest_counter--;
  406.                 ipc_answer_fast(fifo_pop(conn->keyrequests),
  407.                     ENOENT, 0, 0);
  408.                 break;
  409.             }
  410.             conn->used = 0;
  411.             return;
  412.         case CONSOLE_PUTCHAR:
  413.             write_char(consnum, IPC_GET_ARG1(call));
  414.             gcons_notify_char(consnum);
  415.             break;
  416.         case CONSOLE_CLEAR:
  417.             /* Send message to fb */
  418.             if (consnum == active_console) {
  419.                 async_msg(fb_info.phone, FB_CLEAR, 0);
  420.             }
  421.            
  422.             screenbuffer_clear(&conn->screenbuffer);
  423.            
  424.             break;
  425.         case CONSOLE_GOTO:
  426.             screenbuffer_goto(&conn->screenbuffer,
  427.                 IPC_GET_ARG2(call), IPC_GET_ARG1(call));
  428.             if (consnum == active_console)
  429.                 curs_goto(IPC_GET_ARG1(call),
  430.                     IPC_GET_ARG2(call));
  431.             break;
  432.         case CONSOLE_GETSIZE:
  433.             arg1 = fb_info.rows;
  434.             arg2 = fb_info.cols;
  435.             break;
  436.         case CONSOLE_FLUSH:
  437.             if (consnum == active_console)
  438.                 async_req_2(fb_info.phone, FB_FLUSH, 0, 0,
  439.                     NULL, NULL);       
  440.             break;
  441.         case CONSOLE_SET_STYLE:
  442.             arg1 = IPC_GET_ARG1(call);
  443.             arg2 = IPC_GET_ARG2(call);
  444.             screenbuffer_set_style(&conn->screenbuffer, arg1,
  445.                 arg2);
  446.             if (consnum == active_console)
  447.                 set_style_col(arg1, arg2);
  448.             break;
  449.         case CONSOLE_CURSOR_VISIBILITY:
  450.             arg1 = IPC_GET_ARG1(call);
  451.             conn->screenbuffer.is_cursor_visible = arg1;
  452.             if (consnum == active_console)
  453.                 curs_visibility(arg1);
  454.             break;
  455.         case CONSOLE_GETCHAR:
  456.             if (keybuffer_empty(&conn->keybuffer)) {
  457.                 /* buffer is empty -> store request */
  458.                 if (conn->keyrequest_counter <
  459.                     MAX_KEYREQUESTS_BUFFERED) {
  460.                     fifo_push(conn->keyrequests, callid);
  461.                     conn->keyrequest_counter++;
  462.                 } else {
  463.                     /*
  464.                      * No key available and too many
  465.                      * requests => fail.
  466.                     */
  467.                     ipc_answer_fast(callid, ELIMIT, 0, 0);
  468.                 }
  469.                 continue;
  470.             }
  471.             keybuffer_pop(&conn->keybuffer, (int *) &arg1);
  472.             break;
  473.         }
  474.         ipc_answer_fast(callid, 0, arg1, arg2);
  475.     }
  476. }
  477.  
  478. int main(int argc, char *argv[])
  479. {
  480.     ipcarg_t phonehash;
  481.     int kbd_phone;
  482.     int i;
  483.  
  484.     async_set_client_connection(client_connection);
  485.    
  486.     /* Connect to keyboard driver */
  487.  
  488.     kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0);
  489.     while (kbd_phone < 0) {
  490.         usleep(10000);
  491.         kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0);
  492.     }
  493.    
  494.     if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0)
  495.         return -1;
  496.     async_new_connection(phonehash, 0, NULL, keyboard_events);
  497.    
  498.     /* Connect to framebuffer driver */
  499.    
  500.     fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0);
  501.     while (fb_info.phone < 0) {
  502.         usleep(10000);
  503.         fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0);
  504.     }
  505.    
  506.     /* Save old kernel screen */
  507.     kernel_pixmap = switch_screens(-1);
  508.  
  509.     /* Initialize gcons */
  510.     gcons_init(fb_info.phone);
  511.     /* Synchronize, the gcons can have something in queue */
  512.     async_req(fb_info.phone, FB_FLUSH, 0, NULL);
  513.     /* Enable double buffering */
  514.     async_msg_2(fb_info.phone, FB_VIEWPORT_DB, (sysarg_t) -1, 1);
  515.    
  516.     async_req_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &fb_info.rows,
  517.         &fb_info.cols);
  518.     set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
  519.     clrscr();
  520.    
  521.     /* Init virtual consoles */
  522.     for (i = 0; i < CONSOLE_COUNT; i++) {
  523.         connections[i].used = 0;
  524.         keybuffer_init(&connections[i].keybuffer);
  525.        
  526.         connections[i].keyrequests.head = 0;
  527.         connections[i].keyrequests.tail = 0;
  528.         connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
  529.         connections[i].keyrequest_counter = 0;
  530.        
  531.         if (screenbuffer_init(&connections[i].screenbuffer,
  532.             fb_info.cols, fb_info.rows) == NULL) {
  533.             /* FIXME: handle error */
  534.             return -1;
  535.         }
  536.     }
  537.     connections[KERNEL_CONSOLE].used = 1;
  538.    
  539.     interbuffer = mmap(NULL,
  540.         sizeof(keyfield_t) * fb_info.cols * fb_info.rows,
  541.         PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  542.     if (!interbuffer) {
  543.         if (async_req_3(fb_info.phone, IPC_M_AS_AREA_SEND,
  544.             (ipcarg_t) interbuffer, 0, AS_AREA_READ, NULL, NULL,
  545.             NULL) != 0) {
  546.             munmap(interbuffer,
  547.                 sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
  548.             interbuffer = NULL;
  549.         }
  550.     }
  551.  
  552.     curs_goto(0, 0);
  553.     curs_visibility(
  554.         connections[active_console].screenbuffer.is_cursor_visible);
  555.  
  556.     /* Register at NS */
  557.     if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
  558.         return -1;
  559.     }
  560.    
  561.     async_manager();
  562.  
  563.     return 0;  
  564. }
  565.  
  566. /** @}
  567.  */
  568.