Subversion Repositories HelenOS-historic

Rev

Rev 1504 | Rev 1514 | 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.  
  30. #include <kbd.h>
  31. #include <fb.h>
  32. #include <ipc/ipc.h>
  33. #include <ipc/fb.h>
  34. #include <ipc/services.h>
  35. #include <errno.h>
  36. #include <key_buffer.h>
  37. #include <console.h>
  38. #include <unistd.h>
  39. #include <async.h>
  40. #include <libadt/fifo.h>
  41. #include <screenbuffer.h>
  42.  
  43. #define CONSOLE_COUNT 12
  44. #define MAX_KEYREQUESTS_BUFFERED 32
  45.  
  46. #define NAME "CONSOLE"
  47.  
  48. int active_console = 1;
  49.  
  50. struct {
  51.     int phone;      /**< Framebuffer phone */
  52.     ipcarg_t rows;      /**< Framebuffer rows */
  53.     ipcarg_t cols;      /**< Framebuffer columns */
  54. } fb_info;
  55.  
  56. typedef struct {
  57.     keybuffer_t keybuffer;
  58.     FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
  59.     int keyrequest_counter;
  60.     int client_phone;
  61.     int used;
  62.     screenbuffer_t screenbuffer;
  63. } connection_t;
  64.  
  65.  
  66.  
  67. connection_t connections[CONSOLE_COUNT];
  68.  
  69. static int find_free_connection()
  70. {
  71.     int i = 0;
  72.    
  73.     while (i < CONSOLE_COUNT) {
  74.         if (connections[i].used == 0)
  75.             return i;
  76.         ++i;
  77.     }
  78.     return CONSOLE_COUNT;
  79. }
  80.  
  81.  
  82. static int find_connection(int client_phone)
  83. {
  84.     int i = 0;
  85.    
  86.     while (i < CONSOLE_COUNT) {
  87.         if (connections[i].client_phone == client_phone)
  88.             return i;
  89.         ++i;
  90.     }
  91.     return  CONSOLE_COUNT;
  92. }
  93.  
  94. /** Check key and process special keys.
  95.  *
  96.  * */
  97. static void write_char(int console, char key)
  98. {
  99.     screenbuffer_t *scr = &(connections[console].screenbuffer);
  100.    
  101.     switch (key) {
  102.         case '\n':
  103.             scr->position_y += 1;
  104.             scr->position_x =  0;
  105.             break;
  106.         case '\r':
  107.             break;
  108.         case '\t':
  109.             scr->position_x += 8;
  110.             scr->position_x -= scr->position_x % 8;
  111.             break;
  112.         case '\b':
  113.             if (scr->position_x == 0)
  114.                 break;
  115.  
  116.             scr->position_x--;
  117.  
  118.             if (console == active_console) {
  119.                 ipc_call_async_3(fb_info.phone, FB_PUTCHAR, ' ', scr->position_y, scr->position_x, NULL, NULL);
  120.             }
  121.    
  122.             screenbuffer_putchar(scr, ' ');
  123.            
  124.             break;
  125.         default:   
  126.             if (console == active_console) {
  127.                 ipc_call_async_3(fb_info.phone, FB_PUTCHAR, key, scr->position_y, scr->position_x, NULL, NULL);
  128.             }
  129.    
  130.             screenbuffer_putchar(scr, key);
  131.             scr->position_x++;
  132.     }
  133.    
  134.     scr->position_y += (scr->position_x >= scr->size_x);
  135.    
  136.     if (scr->position_y >= scr->size_y) {
  137.         scr->position_y = scr->size_y - 1;
  138.         screenbuffer_clear_line(scr, scr->top_line++);
  139.         ipc_call_async(fb_info.phone, FB_SCROLL, 1, NULL, NULL);
  140.     }
  141.    
  142.     scr->position_x = scr->position_x % scr->size_x;
  143.    
  144.     if (console == active_console) 
  145.         ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, scr->position_y, scr->position_x, NULL, NULL);
  146.    
  147. }
  148.  
  149.  
  150. /* Handler for keyboard */
  151. static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
  152. {
  153.     ipc_callid_t callid;
  154.     ipc_call_t call;
  155.     int retval;
  156.     int i, j;
  157.     char c,d;
  158.     connection_t *conn;
  159.     keyfield_t *interbuffer = NULL;
  160.  
  161. //  interbuffer = mmap(,, PROTO_READ|PROTO_WRITE, MAP_ANONYMOUS, , );  
  162.    
  163.     /* Ignore parameters, the connection is alread opened */
  164.     while (1) {
  165.         callid = async_get_call(&call);
  166.         switch (IPC_GET_METHOD(call)) {
  167.         case IPC_M_PHONE_HUNGUP:
  168.             ipc_answer_fast(callid,0,0,0);
  169.             /* TODO: Handle hangup */
  170.             return;
  171.         case KBD_PUSHCHAR:
  172.             /* got key from keyboard driver */
  173.            
  174.             retval = 0;
  175.             c = IPC_GET_ARG1(call);
  176.             /* switch to another virtual console */
  177.            
  178.             conn = &connections[active_console];
  179. //          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
  180.             if ((c >= '1') && (c < '1' + CONSOLE_COUNT)) {
  181.                 /*FIXME: draw another console content from buffer */
  182.                 if (c - '1' == active_console)
  183.                         break;
  184.                 active_console = c - '1';
  185.                 conn = &connections[active_console];
  186.  
  187.                 ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 0, NULL, NULL);
  188.                 ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
  189.                
  190.                 for (i = 0; i < conn->screenbuffer.size_x; i++)
  191.                     for (j = 0; j < conn->screenbuffer.size_y; j++) {
  192.                         d = get_field_at(&(conn->screenbuffer),i, j)->character;
  193.                         if (d && d != ' ')
  194.                             ipc_call_async_3(fb_info.phone, FB_PUTCHAR, d, j, i, NULL, NULL);
  195.                     }
  196.  
  197.                 ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x, NULL, NULL);
  198.                 ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL, NULL);
  199.  
  200.                 break;
  201.             }
  202.            
  203.             /* if client is awaiting key, send it */
  204.             if (conn->keyrequest_counter > 0) {    
  205.                 conn->keyrequest_counter--;
  206.                 ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
  207.                 break;
  208.             }
  209.            
  210.             /*FIXME: else store key to its buffer */
  211.             keybuffer_push(&conn->keybuffer, c);
  212.            
  213.             break;
  214.         default:
  215.             retval = ENOENT;
  216.         }      
  217.         ipc_answer_fast(callid, retval, 0, 0);
  218.     }
  219. }
  220.  
  221. /** Default thread for new connections */
  222. void client_connection(ipc_callid_t iid, ipc_call_t *icall)
  223. {
  224.     ipc_callid_t callid;
  225.     ipc_call_t call;
  226.     int consnum;
  227.     ipcarg_t arg1;
  228.  
  229.     if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
  230.         ipc_answer_fast(iid,ELIMIT,0,0);
  231.         return;
  232.     }
  233.  
  234.     connections[consnum].used = 1;
  235.     connections[consnum].client_phone = IPC_GET_ARG3(call);
  236.     screenbuffer_clear(&(connections[consnum].screenbuffer));
  237.    
  238.     /* Accept the connection */
  239.     ipc_answer_fast(iid,0,0,0);
  240.    
  241.     while (1) {
  242.         callid = async_get_call(&call);
  243.         switch (IPC_GET_METHOD(call)) {
  244.         case IPC_M_PHONE_HUNGUP:
  245.             /* TODO */
  246.             ipc_answer_fast(callid, 0,0,0);
  247.             return;
  248.         case CONSOLE_PUTCHAR:
  249.             write_char(consnum, IPC_GET_ARG1(call));
  250.             break;
  251.         case CONSOLE_CLEAR:
  252.             /* Send message to fb */
  253.             if (consnum == active_console) {
  254.                 ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
  255.             }
  256.            
  257.             screenbuffer_clear(&(connections[consnum].screenbuffer));
  258.            
  259.             break;
  260.         case CONSOLE_GOTO:
  261.            
  262.             screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
  263.            
  264.             break;
  265.  
  266.         case CONSOLE_GETCHAR:
  267.             if (keybuffer_empty(&(connections[consnum].keybuffer))) {
  268.                 /* buffer is empty -> store request */
  269.                 if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
  270.                     fifo_push(connections[consnum].keyrequests, callid);
  271.                     connections[consnum].keyrequest_counter++;
  272.                 } else {
  273.                     /* no key available and too many requests => fail */
  274.                     ipc_answer_fast(callid, ELIMIT, 0, 0);
  275.                 }
  276.                 continue;
  277.             };
  278.             keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
  279.            
  280.             break;
  281.         }
  282.         ipc_answer_fast(callid, 0, arg1, 0);
  283.     }
  284. }
  285.  
  286. int main(int argc, char *argv[])
  287. {
  288.     ipcarg_t phonehash;
  289.     int kbd_phone, fb_phone;
  290.     ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
  291.     int i;
  292.  
  293.     async_set_client_connection(client_connection);
  294.    
  295.     /* Connect to keyboard driver */
  296.  
  297.     while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
  298.         usleep(10000);
  299.     };
  300.    
  301.     if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
  302.         return -1;
  303.     };
  304.  
  305.     /* Connect to framebuffer driver */
  306.    
  307.     while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
  308.         usleep(10000);
  309.     }
  310.    
  311.     ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
  312.     ipc_call_sync(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL);
  313.    
  314.     /* Init virtual consoles */
  315.     for (i = 0; i < CONSOLE_COUNT; i++) {
  316.         connections[i].used = 0;
  317.         keybuffer_init(&(connections[i].keybuffer));
  318.        
  319.         connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
  320.         connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
  321.         connections[i].keyrequest_counter = 0;
  322.        
  323.         if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
  324.             /*FIXME: handle error */
  325.             return -1;
  326.         }
  327.     }
  328.    
  329.     async_new_connection(phonehash, 0, NULL, keyboard_events);
  330.    
  331.     ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, 0, 0, NULL, NULL);
  332.  
  333.     if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
  334.         return -1;
  335.     };
  336.    
  337.     async_manager();
  338.  
  339.     return 0;  
  340. }
  341.