sysio.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Ondrej Palkovsky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <async.h>
00038 #include <ipc/fb.h>
00039 #include <ipc/ipc.h>
00040 #include <libc.h>
00041 #include <errno.h>
00042 #include <string.h>
00043 #include <libc.h>
00044 #include <stdio.h>
00045 
00046 #include "sysio.h"
00047 
00048 #define WIDTH 80
00049 #define HEIGHT 25
00050 
00051 /* Allow only 1 connection */
00052 static int client_connected = 0;
00053 
00054 static void sysput(char c)
00055 {
00056         __SYSCALL3(SYS_IO, 1, (sysarg_t)&c, (sysarg_t) 1);
00057 }
00058 
00059 static void sysputs(char *s)
00060 {
00061         while (*s) {
00062                 sysput(*(s++));
00063         }
00064 //      __SYSCALL3(SYS_IO, 1, (sysarg_t)s, strlen(s));
00065 }
00066 
00068 static void clrscr(void)
00069 {
00070         sysputs("\033[2J");
00071 }
00072 
00074 static void curs_goto(unsigned int row, unsigned int col)
00075 {
00076         char control[20];
00077 
00078         if (row > 200 || col > 200)
00079                 return;
00080 
00081         snprintf(control, 20, "\033[%d;%df",row+1, col+1);
00082         sysputs(control);
00083 }
00084 
00085 static void set_style(int mode)
00086 {
00087         char control[20];
00088         
00089         snprintf(control, 20, "\033[%dm", mode);
00090         sysputs(control);
00091 }
00092 
00093 static void scroll(int i)
00094 {
00095         if (i > 0) {
00096                 curs_goto(HEIGHT-1, 0);
00097                 while (i--)
00098                         sysputs("\033D");
00099         } else if (i < 0) {
00100                 curs_goto(0,0);
00101                 while (i++)
00102                         sysputs("\033M");
00103         }
00104 }
00105 
00107 static void sysio_client_connection(ipc_callid_t iid, ipc_call_t *icall)
00108 {
00109         int retval;
00110         ipc_callid_t callid;
00111         ipc_call_t call;
00112         char c;
00113         int lastcol=0;
00114         int lastrow=0;
00115         int newcol,newrow;
00116         int fgcolor,bgcolor;
00117         int i;
00118 
00119         if (client_connected) {
00120                 ipc_answer_fast(iid, ELIMIT, 0,0);
00121                 return;
00122         }
00123         
00124         client_connected = 1;
00125         ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
00126         while (1) {
00127                 callid = async_get_call(&call);
00128                 switch (IPC_GET_METHOD(call)) {
00129                         case IPC_M_PHONE_HUNGUP:
00130                                 client_connected = 0;
00131                                 ipc_answer_fast(callid, 0, 0, 0);
00132                                 return; /* Exit thread */
00133                         case FB_PUTCHAR:
00134                                 c = IPC_GET_ARG1(call);
00135                                 newrow = IPC_GET_ARG2(call);
00136                                 newcol = IPC_GET_ARG3(call);
00137                                 if ((lastcol != newcol) || (lastrow != newrow))
00138                                         curs_goto(newrow, newcol);
00139                                 lastcol = newcol + 1;
00140                                 lastrow = newrow;
00141                                 sysput(c);
00142                                 retval = 0;
00143                                 break;
00144                         case FB_CURSOR_GOTO:
00145                                 newrow = IPC_GET_ARG1(call);
00146                                 newcol = IPC_GET_ARG2(call);
00147                                 curs_goto(newrow, newcol);
00148                                 lastrow = newrow;
00149                                 lastcol = newcol;
00150                                 retval = 0;
00151                                 break;
00152                         case FB_GET_CSIZE:
00153                                 ipc_answer_fast(callid, 0, HEIGHT, WIDTH);
00154                                 continue;
00155                         case FB_CLEAR:
00156                                 clrscr();
00157                                 retval = 0;
00158                                 break;
00159                         case FB_SET_STYLE:
00160                                 fgcolor = IPC_GET_ARG1(call);
00161                                 bgcolor = IPC_GET_ARG2(call);
00162                                 if (fgcolor < bgcolor)
00163                                         set_style(0);
00164                                 else
00165                                         set_style(7);
00166                                 retval = 0;
00167                                 break;
00168                         case FB_SCROLL:
00169                                 i = IPC_GET_ARG1(call);
00170                                 if ((i > HEIGHT) || (i < -HEIGHT)) {
00171                                         retval = EINVAL;
00172                                         break;
00173                                 }
00174                                 scroll(i);
00175                                 curs_goto(lastrow, lastcol);
00176                                 retval = 0;
00177                                 break;
00178                         default:
00179                                 retval = ENOENT;
00180                 }
00181                 
00182                 ipc_answer_fast(callid, retval, 0, 0);
00183         }
00184 }
00185 
00187 void sysio_init(void)
00188 {
00189         async_set_client_connection(sysio_client_connection);
00190         clrscr();
00191         curs_goto(0,0);
00192         /* Set scrolling region to 0-25 lines */
00193         sysputs("\033[0;25r");
00194 }
00195 

Generated on Sun Jun 18 18:00:18 2006 for HelenOS Userspace (ia64) by  doxygen 1.4.6