Subversion Repositories HelenOS-historic

Rev

Rev 1535 | Rev 1630 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1490 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
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
#include <async.h>
30
#include <ipc/fb.h>
31
#include <ipc/ipc.h>
32
#include <libc.h>
33
#include <errno.h>
1492 palkovsky 34
#include <string.h>
35
#include <libc.h>
36
#include <stdio.h>
1490 palkovsky 37
 
38
#include "sysio.h"
39
 
40
/* Allow only 1 connection */
41
static int client_connected = 0;
42
 
43
static void sysput(char c)
44
{
45
    __SYSCALL3(SYS_IO, 1, (sysarg_t)&c, (sysarg_t) 1);
46
}
47
 
1492 palkovsky 48
static void sysputs(char *s)
49
{
1542 palkovsky 50
    while (*s) {
51
        sysput(*(s++));
52
    }
53
//  __SYSCALL3(SYS_IO, 1, (sysarg_t)s, strlen(s));
1492 palkovsky 54
}
55
 
1498 palkovsky 56
/** Send clearscreen sequence to console */
57
static void clrscr(void)
58
{
59
    sysputs("\033[2J");
60
}
61
 
62
/** Send ansi sequence to console to change cursor position */
1492 palkovsky 63
static void curs_goto(unsigned int row, unsigned int col)
64
{
65
    char control[20];
66
 
1498 palkovsky 67
    if (row > 200 || col > 200)
1492 palkovsky 68
        return;
69
 
1500 palkovsky 70
    snprintf(control, 20, "\033[%d;%df",row+1, col+1);
1492 palkovsky 71
    sysputs(control);
72
}
73
 
1534 palkovsky 74
static void set_style(int mode)
75
{
76
    char control[20];
77
 
1535 palkovsky 78
    snprintf(control, 20, "\033[%dm", mode);
1534 palkovsky 79
    sysputs(control);
80
}
81
 
1498 palkovsky 82
/** ANSI terminal emulation main thread */
1490 palkovsky 83
static void sysio_client_connection(ipc_callid_t iid, ipc_call_t *icall)
84
{
85
    int retval;
86
    ipc_callid_t callid;
87
    ipc_call_t call;
88
    char c;
1492 palkovsky 89
    int lastcol=0;
90
    int lastrow=0;
91
    int newcol,newrow;
1534 palkovsky 92
    int fgcolor,bgcolor;
1490 palkovsky 93
 
94
    if (client_connected) {
95
        ipc_answer_fast(iid, ELIMIT, 0,0);
96
        return;
97
    }
98
    client_connected = 1;
99
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
100
    while (1) {
101
        callid = async_get_call(&call);
102
        switch (IPC_GET_METHOD(call)) {
103
        case IPC_M_PHONE_HUNGUP:
104
            client_connected = 0;
105
            ipc_answer_fast(callid,0,0,0);
106
            return; /* Exit thread */
107
        case FB_PUTCHAR:
108
            c = IPC_GET_ARG1(call);
1492 palkovsky 109
            newrow = IPC_GET_ARG2(call);
110
            newcol = IPC_GET_ARG3(call);
111
            if (lastcol != newcol || lastrow!=newrow)
112
                curs_goto(newrow, newcol);
113
            lastcol = newcol + 1;
114
            lastrow = newrow;
1490 palkovsky 115
            sysput(c);
116
            retval = 0;
117
            break;
1492 palkovsky 118
        case FB_CURSOR_GOTO:
119
            newrow = IPC_GET_ARG1(call);
120
            newcol = IPC_GET_ARG2(call);
121
            curs_goto(newrow, newcol);
122
            break;
1490 palkovsky 123
        case FB_GET_CSIZE:
124
            ipc_answer_fast(callid, 0, 25, 80);
125
            continue;
1492 palkovsky 126
        case FB_CLEAR:
1498 palkovsky 127
            clrscr();
1492 palkovsky 128
            retval = 0;
129
            break;
1534 palkovsky 130
        case FB_SET_STYLE:
131
            fgcolor = IPC_GET_ARG1(call);
132
            bgcolor = IPC_GET_ARG2(call);
133
            if (fgcolor > bgcolor)
134
                set_style(0);
135
            else
136
                set_style(7);
137
            retval = 0;
138
            break;
1490 palkovsky 139
        default:
140
            retval = ENOENT;
141
        }
142
        ipc_answer_fast(callid,retval,0,0);
143
    }
144
}
145
 
1498 palkovsky 146
/** ANSI terminal emulation initialization */
1490 palkovsky 147
void sysio_init(void)
148
{
149
    async_set_client_connection(sysio_client_connection);
1498 palkovsky 150
    clrscr();
1492 palkovsky 151
    curs_goto(0,0);
1490 palkovsky 152
}