00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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);
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;
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
00193 sysputs("\033[0;25r");
00194 }
00195