Subversion Repositories HelenOS

Rev

Rev 3735 | Rev 3767 | 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 <ipc/console.h>
  45. #include <unistd.h>
  46. #include <async.h>
  47. #include <libadt/fifo.h>
  48. #include <screenbuffer.h>
  49. #include <sys/mman.h>
  50. #include <stdio.h>
  51.  
  52. #include "gcons.h"
  53.  
  54. #define MAX_KEYREQUESTS_BUFFERED 32
  55.  
  56. #define NAME "console"
  57.  
  58. /** Index of currently used virtual console.
  59.  */
  60. int active_console = 0;
  61.  
  62. /** Information about framebuffer
  63.  */
  64. struct {
  65.     int phone;      /**< Framebuffer phone */
  66.     ipcarg_t rows;      /**< Framebuffer rows */
  67.     ipcarg_t cols;      /**< Framebuffer columns */
  68. } fb_info;
  69.  
  70. typedef struct {
  71.     keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
  72.     /** Buffer for unsatisfied request for keys. */
  73.     FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
  74.         MAX_KEYREQUESTS_BUFFERED); 
  75.     int keyrequest_counter;     /**< Number of requests in buffer. */
  76.     int client_phone;       /**< Phone to connected client. */
  77.     int used;           /**< 1 if this virtual console is
  78.                      * connected to some client.*/
  79.     screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
  80.                      * contents and related settings. */
  81. } connection_t;
  82.  
  83. static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
  84.                          * consoles */
  85. static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
  86.                          * with framebufer used for
  87.                          * faster virtual console
  88.                          * switching */
  89.  
  90.  
  91. /** Find unused virtual console.
  92.  *
  93.  */
  94. static int find_free_connection(void)
  95. {
  96.     int i;
  97.    
  98.     for (i = 0; i < CONSOLE_COUNT; i++) {
  99.         if (!connections[i].used)
  100.             return i;
  101.     }
  102.     return -1;
  103. }
  104.  
  105. static void clrscr(void)
  106. {
  107.     async_msg_0(fb_info.phone, FB_CLEAR);
  108. }
  109.  
  110. static void curs_visibility(bool visible)
  111. {
  112.     async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
  113. }
  114.  
  115. static void curs_hide_sync(void)
  116. {
  117.     ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
  118. }
  119.  
  120. static void curs_goto(int row, int col)
  121. {
  122.     async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
  123. }
  124.  
  125. static void set_style(style_t *style)
  126. {
  127.     async_msg_2(fb_info.phone, FB_SET_STYLE, style->fg_color,
  128.         style->bg_color);
  129. }
  130.  
  131. static void set_style_col(int fgcolor, int bgcolor)
  132. {
  133.     async_msg_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor);
  134. }
  135.  
  136. static void prtchr(char c, int row, int col)
  137. {
  138.     async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
  139. }
  140.  
  141. /** Check key and process special keys.
  142.  *
  143.  *
  144.  */
  145. static void write_char(int console, char key)
  146. {
  147.     screenbuffer_t *scr = &(connections[console].screenbuffer);
  148.    
  149.     switch (key) {
  150.     case '\n':
  151.         scr->position_y++;
  152.         scr->position_x = 0;
  153.         break;
  154.     case '\r':
  155.         break;
  156.     case '\t':
  157.         scr->position_x += 8;
  158.         scr->position_x -= scr->position_x % 8;
  159.         break;
  160.     case '\b':
  161.         if (scr->position_x == 0)
  162.             break;
  163.         scr->position_x--;
  164.         if (console == active_console)
  165.             prtchr(' ', scr->position_y, scr->position_x);
  166.         screenbuffer_putchar(scr, ' ');
  167.         break;
  168.     default:   
  169.         if (console == active_console)
  170.             prtchr(key, scr->position_y, scr->position_x);
  171.  
  172.         screenbuffer_putchar(scr, key);
  173.         scr->position_x++;
  174.     }
  175.    
  176.     scr->position_y += (scr->position_x >= scr->size_x);
  177.    
  178.     if (scr->position_y >= scr->size_y) {
  179.         scr->position_y = scr->size_y - 1;
  180.         screenbuffer_clear_line(scr, scr->top_line);
  181.         scr->top_line = (scr->top_line + 1) % scr->size_y;
  182.         if (console == active_console)
  183.             async_msg_1(fb_info.phone, FB_SCROLL, 1);
  184.     }
  185.    
  186.     scr->position_x = scr->position_x % scr->size_x;
  187.    
  188.     if (console == active_console)
  189.         curs_goto(scr->position_y, scr->position_x);
  190.    
  191. }
  192.  
  193. /** Switch to new console */
  194. static void change_console(int newcons)
  195. {
  196.     connection_t *conn;
  197.     int i, j, rc;
  198.     keyfield_t *field;
  199.     style_t *style;
  200.    
  201.     if (newcons == active_console)
  202.         return;
  203.    
  204.     if (newcons == KERNEL_CONSOLE) {
  205.         async_serialize_start();
  206.         curs_hide_sync();
  207.         gcons_in_kernel();
  208.         async_serialize_end();
  209.        
  210.         if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE))
  211.             active_console = KERNEL_CONSOLE;
  212.         else
  213.             newcons = active_console;
  214.     }
  215.    
  216.     if (newcons != KERNEL_CONSOLE) {
  217.         async_serialize_start();
  218.        
  219.         if (active_console == KERNEL_CONSOLE)
  220.             gcons_redraw_console();
  221.        
  222.         active_console = newcons;
  223.         gcons_change_console(newcons);
  224.         conn = &connections[active_console];
  225.        
  226.         set_style(&conn->screenbuffer.style);
  227.         curs_visibility(false);
  228.         if (interbuffer) {
  229.             for (i = 0; i < conn->screenbuffer.size_x; i++)
  230.                 for (j = 0; j < conn->screenbuffer.size_y; j++) {
  231.                     unsigned int size_x;
  232.                    
  233.                     size_x = conn->screenbuffer.size_x;
  234.                     interbuffer[i + j * size_x] =
  235.                         *get_field_at(&conn->screenbuffer, i, j);
  236.                 }
  237.             /* This call can preempt, but we are already at the end */
  238.             rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA);      
  239.         }
  240.        
  241.         if ((!interbuffer) || (rc != 0)) {
  242.             set_style(&conn->screenbuffer.style);
  243.             clrscr();
  244.             style = &conn->screenbuffer.style;
  245.            
  246.             for (j = 0; j < conn->screenbuffer.size_y; j++)
  247.                 for (i = 0; i < conn->screenbuffer.size_x; i++) {
  248.                     field = get_field_at(&conn->screenbuffer, i, j);
  249.                     if (!style_same(*style, field->style))
  250.                         set_style(&field->style);
  251.                     style = &field->style;
  252.                     if ((field->character == ' ') &&
  253.                         (style_same(field->style,
  254.                         conn->screenbuffer.style)))
  255.                         continue;
  256.                    
  257.                     prtchr(field->character, j, i);
  258.                 }
  259.         }
  260.        
  261.         curs_goto(conn->screenbuffer.position_y,
  262.             conn->screenbuffer.position_x);
  263.         curs_visibility(conn->screenbuffer.is_cursor_visible);
  264.        
  265.         async_serialize_end();
  266.     }
  267. }
  268.  
  269. /** Handler for keyboard */
  270. static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
  271. {
  272.     ipc_callid_t callid;
  273.     ipc_call_t call;
  274.     int retval;
  275.     int c;
  276.     connection_t *conn;
  277.     int newcon;
  278.    
  279.     /* Ignore parameters, the connection is alread opened */
  280.     while (1) {
  281.         callid = async_get_call(&call);
  282.         switch (IPC_GET_METHOD(call)) {
  283.         case IPC_M_PHONE_HUNGUP:
  284.             /* TODO: Handle hangup */
  285.             return;
  286.         case KBD_MS_LEFT:
  287.             newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
  288.             if (newcon != -1)
  289.                 change_console(newcon);
  290.             retval = 0;
  291.             break;
  292.         case KBD_MS_MOVE:
  293.             gcons_mouse_move(IPC_GET_ARG1(call),
  294.                 IPC_GET_ARG2(call));
  295.             retval = 0;
  296.             break;
  297.         case KBD_PUSHCHAR:
  298.             /* got key from keyboard driver */
  299.             retval = 0;
  300.             c = IPC_GET_ARG1(call);
  301.             /* switch to another virtual console */
  302.            
  303.             conn = &connections[active_console];
  304. /*
  305.  *          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 +
  306.  *              CONSOLE_COUNT)) {
  307.  */
  308.             if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
  309.                 if (c == 0x112)
  310.                     change_console(KERNEL_CONSOLE);
  311.                 else
  312.                     change_console(c - 0x101);
  313.                 break;
  314.             }
  315.            
  316.             /* if client is awaiting key, send it */
  317.             if (conn->keyrequest_counter > 0) {    
  318.                 conn->keyrequest_counter--;
  319.                 ipc_answer_1(fifo_pop(conn->keyrequests), EOK,
  320.                     c);
  321.                 break;
  322.             }
  323.            
  324.             keybuffer_push(&conn->keybuffer, c);
  325.             retval = 0;
  326.            
  327.             break;
  328.         default:
  329.             retval = ENOENT;
  330.         }
  331.         ipc_answer_0(callid, retval);
  332.     }
  333. }
  334.  
  335. /** Default thread for new connections */
  336. static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
  337. {
  338.     ipc_callid_t callid;
  339.     ipc_call_t call;
  340.     int consnum;
  341.     ipcarg_t arg1, arg2;
  342.     connection_t *conn;
  343.  
  344.     if ((consnum = find_free_connection()) == -1) {
  345.         ipc_answer_0(iid, ELIMIT);
  346.         return;
  347.     }
  348.     conn = &connections[consnum];
  349.     conn->used = 1;
  350.    
  351.     async_serialize_start();
  352.     gcons_notify_connect(consnum);
  353.     conn->client_phone = IPC_GET_ARG5(*icall);
  354.     screenbuffer_clear(&conn->screenbuffer);
  355.    
  356.     /* Accept the connection */
  357.     ipc_answer_0(iid, EOK);
  358.  
  359.     while (1) {
  360.         async_serialize_end();
  361.         callid = async_get_call(&call);
  362.         async_serialize_start();
  363.  
  364.         arg1 = 0;
  365.         arg2 = 0;
  366.         switch (IPC_GET_METHOD(call)) {
  367.         case IPC_M_PHONE_HUNGUP:
  368.             gcons_notify_disconnect(consnum);
  369.            
  370.             /* Answer all pending requests */
  371.             while (conn->keyrequest_counter > 0) {     
  372.                 conn->keyrequest_counter--;
  373.                 ipc_answer_0(fifo_pop(conn->keyrequests),
  374.                     ENOENT);
  375.                 break;
  376.             }
  377.             conn->used = 0;
  378.             return;
  379.         case CONSOLE_PUTCHAR:
  380.             write_char(consnum, IPC_GET_ARG1(call));
  381.             gcons_notify_char(consnum);
  382.             break;
  383.         case CONSOLE_CLEAR:
  384.             /* Send message to fb */
  385.             if (consnum == active_console) {
  386.                 async_msg_0(fb_info.phone, FB_CLEAR);
  387.             }
  388.            
  389.             screenbuffer_clear(&conn->screenbuffer);
  390.            
  391.             break;
  392.         case CONSOLE_GOTO:
  393.             screenbuffer_goto(&conn->screenbuffer,
  394.                 IPC_GET_ARG2(call), IPC_GET_ARG1(call));
  395.             if (consnum == active_console)
  396.                 curs_goto(IPC_GET_ARG1(call),
  397.                     IPC_GET_ARG2(call));
  398.             break;
  399.         case CONSOLE_GETSIZE:
  400.             arg1 = fb_info.rows;
  401.             arg2 = fb_info.cols;
  402.             break;
  403.         case CONSOLE_FLUSH:
  404.             if (consnum == active_console)
  405.                 async_req_0_0(fb_info.phone, FB_FLUSH);
  406.             break;
  407.         case CONSOLE_SET_STYLE:
  408.             arg1 = IPC_GET_ARG1(call);
  409.             arg2 = IPC_GET_ARG2(call);
  410.             screenbuffer_set_style(&conn->screenbuffer, arg1,
  411.                 arg2);
  412.             if (consnum == active_console)
  413.                 set_style_col(arg1, arg2);
  414.             break;
  415.         case CONSOLE_CURSOR_VISIBILITY:
  416.             arg1 = IPC_GET_ARG1(call);
  417.             conn->screenbuffer.is_cursor_visible = arg1;
  418.             if (consnum == active_console)
  419.                 curs_visibility(arg1);
  420.             break;
  421.         case CONSOLE_GETCHAR:
  422.             if (keybuffer_empty(&conn->keybuffer)) {
  423.                 /* buffer is empty -> store request */
  424.                 if (conn->keyrequest_counter <
  425.                     MAX_KEYREQUESTS_BUFFERED) {
  426.                     fifo_push(conn->keyrequests, callid);
  427.                     conn->keyrequest_counter++;
  428.                 } else {
  429.                     /*
  430.                      * No key available and too many
  431.                      * requests => fail.
  432.                     */
  433.                     ipc_answer_0(callid, ELIMIT);
  434.                 }
  435.                 continue;
  436.             }
  437.             int ch;
  438.             keybuffer_pop(&conn->keybuffer, &ch);
  439.             arg1 = ch;
  440.             break;
  441.         }
  442.         ipc_answer_2(callid, EOK, arg1, arg2);
  443.     }
  444. }
  445.  
  446. int main(int argc, char *argv[])
  447. {
  448.     printf(NAME ": HelenOS Console service\n");
  449.    
  450.     ipcarg_t phonehash;
  451.     int kbd_phone;
  452.     int i;
  453.  
  454.     async_set_client_connection(client_connection);
  455.    
  456.     /* Connect to keyboard driver */
  457.  
  458.     kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
  459.     while (kbd_phone < 0) {
  460.         usleep(10000);
  461.         kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
  462.     }
  463.    
  464.     if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
  465.         return -1;
  466.     async_new_connection(phonehash, 0, NULL, keyboard_events);
  467.    
  468.     /* Connect to framebuffer driver */
  469.     fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
  470.     while (fb_info.phone < 0) {
  471.         usleep(10000);
  472.         fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
  473.     }
  474.    
  475.     /* Initialize gcons */
  476.     gcons_init(fb_info.phone);
  477.     /* Synchronize, the gcons can have something in queue */
  478.     async_req_0_0(fb_info.phone, FB_FLUSH);
  479.    
  480.     async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
  481.         &fb_info.cols);
  482.     set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
  483.     clrscr();
  484.    
  485.     /* Init virtual consoles */
  486.     for (i = 0; i < CONSOLE_COUNT; i++) {
  487.         connections[i].used = 0;
  488.         keybuffer_init(&connections[i].keybuffer);
  489.        
  490.         connections[i].keyrequests.head = 0;
  491.         connections[i].keyrequests.tail = 0;
  492.         connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
  493.         connections[i].keyrequest_counter = 0;
  494.        
  495.         if (screenbuffer_init(&connections[i].screenbuffer,
  496.             fb_info.cols, fb_info.rows) == NULL) {
  497.             /* FIXME: handle error */
  498.             return -1;
  499.         }
  500.     }
  501.     connections[KERNEL_CONSOLE].used = 1;
  502.    
  503.     interbuffer = mmap(NULL,
  504.         sizeof(keyfield_t) * fb_info.cols * fb_info.rows,
  505.         PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  506.     if (!interbuffer) {
  507.         if (ipc_share_out_start(fb_info.phone, interbuffer,
  508.             AS_AREA_READ) != EOK) {
  509.             munmap(interbuffer,
  510.                 sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
  511.             interbuffer = NULL;
  512.         }
  513.     }
  514.  
  515.     curs_goto(0, 0);
  516.     curs_visibility(
  517.         connections[active_console].screenbuffer.is_cursor_visible);
  518.  
  519.     /* Register at NS */
  520.     if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
  521.         return -1;
  522.    
  523.     // FIXME: avoid connectiong to itself, keep using klog
  524.     // printf(NAME ": Accepting connections\n");
  525.     async_manager();
  526.  
  527.     return 0;  
  528. }
  529.  
  530. /** @}
  531.  */
  532.