Subversion Repositories HelenOS-historic

Rev

Rev 1476 | Rev 1487 | 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.  
  42. static void sysput(char c)
  43. {
  44.     __SYSCALL3(SYS_IO, 1, &c, 1);
  45.  
  46. }
  47. //#define CONSOLE_COUNT VFB_CONNECTIONS
  48. #define CONSOLE_COUNT 8
  49. #define MAX_KEYREQUESTS_BUFFERED 32
  50.  
  51. #define NAME "CONSOLE"
  52.  
  53. int active_console = 1;
  54.  
  55. typedef struct {
  56.     keybuffer_t keybuffer;
  57.     FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
  58.     int keyrequest_counter;
  59.     int client_phone;
  60.     int vfb_phone;
  61.     int used;
  62. } connection_t;
  63.  
  64. connection_t connections[CONSOLE_COUNT];
  65.  
  66. static int find_free_connection()
  67. {
  68.     int i = 0;
  69.    
  70.     while (i < CONSOLE_COUNT) {
  71.         if (connections[i].used == 0)
  72.             return i;
  73.         ++i;
  74.     }
  75.     return CONSOLE_COUNT;
  76. }
  77.  
  78.  
  79. static int find_connection(int client_phone)
  80. {
  81.     int i = 0;
  82.    
  83.     while (i < CONSOLE_COUNT) {
  84.         if (connections[i].client_phone == client_phone)
  85.             return i;
  86.         ++i;
  87.     }
  88.     return  CONSOLE_COUNT;
  89. }
  90.  
  91. /* Handler for keyboard */
  92. static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
  93. {
  94.     ipc_callid_t callid;
  95.     ipc_call_t call;
  96.     int retval;
  97.     int i;
  98.     char c;
  99.  
  100.     /* Ignore parameters, the connection is alread opened */
  101.     while (1) {
  102.         callid = async_get_call(&call);
  103.         switch (IPC_GET_METHOD(call)) {
  104.         case IPC_M_PHONE_HUNGUP:
  105.             ipc_answer_fast(callid,0,0,0);
  106.             /* TODO: Handle hangup */
  107.             return;
  108.         case KBD_PUSHCHAR:
  109.             /* got key from keyboard driver */
  110.            
  111.             retval = 0;
  112.             c = IPC_GET_ARG1(call);
  113. //          ipc_call_sync_2(connections[3].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
  114.        
  115.             /* switch to another virtual console */
  116.            
  117.             if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
  118.                 active_console = c - KBD_KEY_F1;
  119.                 break;
  120.             }
  121.            
  122.             /* if client is awaiting key, send it */
  123.             if (connections[active_console].keyrequest_counter > 0) {      
  124.                 connections[active_console].keyrequest_counter--;
  125.                 ipc_answer_fast(fifo_pop(connections[active_console].keyrequests), 0, c, 0);
  126.                 break;
  127.             }
  128.            
  129.             /*FIXME: else store key to its buffer */
  130.             keybuffer_push(&(connections[active_console].keybuffer), c);
  131.            
  132.             /* Send it to first FB, DEBUG */
  133. //          ipc_call_async_2(connections[0].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG1(call),NULL,NULL);
  134. //          ipc_call_sync_2(connections[4].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
  135.  
  136.             break;
  137.         default:
  138.             retval = ENOENT;
  139.         }      
  140.         ipc_answer_fast(callid, retval, 0, 0);
  141.     }
  142. }
  143.  
  144. /** Default thread for new connections */
  145. void client_connection(ipc_callid_t iid, ipc_call_t *icall)
  146. {
  147.     ipc_callid_t callid;
  148.     ipc_call_t call;
  149.     int consnum;
  150.     ipcarg_t arg1;
  151.  
  152.     if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
  153.         ipc_answer_fast(iid,ELIMIT,0,0);
  154.         return;
  155.     }
  156.     connections[consnum].used = 1;
  157.     connections[consnum].client_phone = IPC_GET_ARG3(call);
  158.  
  159.     /* Accept the connection */
  160.     ipc_answer_fast(iid,0,0,0);
  161.    
  162.     while (1) {
  163.         callid = async_get_call(&call);
  164.         switch (IPC_GET_METHOD(call)) {
  165.         case IPC_M_PHONE_HUNGUP:
  166.             /* TODO */
  167.             ipc_answer_fast(callid, 0,0,0);
  168.             return;
  169.         case CONSOLE_PUTCHAR:
  170.             if (consnum != active_console) {
  171.             }
  172.             /* Send message to fb */
  173.             ipc_call_sync_2(connections[consnum].vfb_phone, FB_PUTCHAR, IPC_GET_ARG1(call), IPC_GET_ARG2(call), NULL, NULL);
  174. //          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG2(call),NULL,NULL);
  175.             break;
  176.         case CONSOLE_CLEAR:
  177.             break;
  178.         case CONSOLE_GOTO:
  179.             break;
  180.  
  181.         case CONSOLE_GETCHAR:
  182.             if (keybuffer_empty(&(connections[consnum].keybuffer))) {
  183.                 /* buffer is empty -> store request */
  184.                 if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
  185.                     fifo_push(connections[consnum].keyrequests, callid);
  186.                     connections[consnum].keyrequest_counter++;
  187.                 } else {
  188.                     /* no key available and too many requests => fail */
  189.                     ipc_answer_fast(callid, ELIMIT, 0, 0);
  190.                 }
  191.                 continue;
  192.             };
  193.             keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
  194. //          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, arg1,NULL,NULL);
  195.            
  196.             break;
  197.         }
  198.         ipc_answer_fast(callid, 0, arg1, 0);
  199.     }
  200. }
  201.  
  202. int main(int argc, char *argv[])
  203. {
  204.     ipcarg_t phonehash;
  205.     int kbd_phone, fb_phone;
  206.     ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
  207.     int i;
  208.    
  209.     /* Connect to keyboard driver */
  210.  
  211.     while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
  212.         usleep(10000);
  213.     };
  214.    
  215.     if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
  216.         return -1;
  217.     };
  218.     async_new_connection(phonehash, 0, NULL, keyboard_events);
  219.  
  220.     /* Connect to framebuffer driver */
  221.    
  222.     for (i = 0; i < CONSOLE_COUNT; i++) {
  223.         connections[i].used = 0;
  224.         keybuffer_init(&(connections[i].keybuffer));
  225.        
  226.         /* TODO: init key_buffer */
  227.         while ((connections[i].vfb_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
  228.             usleep(10000);
  229.         }
  230.         connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
  231.         connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
  232.         connections[i].keyrequest_counter = 0;
  233.     }
  234.    
  235.     if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
  236.         return -1;
  237.     };
  238.    
  239.     async_manager();
  240.  
  241.     return 0;  
  242. }
  243.