Subversion Repositories HelenOS-historic

Rev

Rev 1542 | Rev 1631 | 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
 
1630 palkovsky 40
#define WIDTH 80
41
#define HEIGHT 25
42
 
1490 palkovsky 43
/* Allow only 1 connection */
44
static int client_connected = 0;
45
 
46
static void sysput(char c)
47
{
48
    __SYSCALL3(SYS_IO, 1, (sysarg_t)&c, (sysarg_t) 1);
49
}
50
 
1492 palkovsky 51
static void sysputs(char *s)
52
{
1542 palkovsky 53
    while (*s) {
54
        sysput(*(s++));
55
    }
56
//  __SYSCALL3(SYS_IO, 1, (sysarg_t)s, strlen(s));
1492 palkovsky 57
}
58
 
1498 palkovsky 59
/** Send clearscreen sequence to console */
60
static void clrscr(void)
61
{
62
    sysputs("\033[2J");
63
}
64
 
65
/** Send ansi sequence to console to change cursor position */
1492 palkovsky 66
static void curs_goto(unsigned int row, unsigned int col)
67
{
68
    char control[20];
69
 
1498 palkovsky 70
    if (row > 200 || col > 200)
1492 palkovsky 71
        return;
72
 
1500 palkovsky 73
    snprintf(control, 20, "\033[%d;%df",row+1, col+1);
1492 palkovsky 74
    sysputs(control);
75
}
76
 
1534 palkovsky 77
static void set_style(int mode)
78
{
79
    char control[20];
80
 
1535 palkovsky 81
    snprintf(control, 20, "\033[%dm", mode);
1534 palkovsky 82
    sysputs(control);
83
}
84
 
1630 palkovsky 85
static void scroll(int i)
86
{
87
    if (i > 0) {
88
        curs_goto(HEIGHT-1, 0);
89
        while (i--)
90
            sysputs("\033D");
91
    } else if (i < 0) {
92
        curs_goto(0,0);
93
        while (i++)
94
            sysputs("\033M");
95
    }
96
}
97
 
1498 palkovsky 98
/** ANSI terminal emulation main thread */
1490 palkovsky 99
static void sysio_client_connection(ipc_callid_t iid, ipc_call_t *icall)
100
{
101
    int retval;
102
    ipc_callid_t callid;
103
    ipc_call_t call;
104
    char c;
1492 palkovsky 105
    int lastcol=0;
106
    int lastrow=0;
107
    int newcol,newrow;
1534 palkovsky 108
    int fgcolor,bgcolor;
1630 palkovsky 109
    int i;
1490 palkovsky 110
 
111
    if (client_connected) {
112
        ipc_answer_fast(iid, ELIMIT, 0,0);
113
        return;
114
    }
115
    client_connected = 1;
116
    ipc_answer_fast(iid, 0, 0, 0); /* Accept connection */
117
    while (1) {
118
        callid = async_get_call(&call);
119
        switch (IPC_GET_METHOD(call)) {
120
        case IPC_M_PHONE_HUNGUP:
121
            client_connected = 0;
122
            ipc_answer_fast(callid,0,0,0);
123
            return; /* Exit thread */
124
        case FB_PUTCHAR:
125
            c = IPC_GET_ARG1(call);
1492 palkovsky 126
            newrow = IPC_GET_ARG2(call);
127
            newcol = IPC_GET_ARG3(call);
128
            if (lastcol != newcol || lastrow!=newrow)
129
                curs_goto(newrow, newcol);
130
            lastcol = newcol + 1;
131
            lastrow = newrow;
1490 palkovsky 132
            sysput(c);
133
            retval = 0;
134
            break;
1492 palkovsky 135
        case FB_CURSOR_GOTO:
136
            newrow = IPC_GET_ARG1(call);
137
            newcol = IPC_GET_ARG2(call);
138
            curs_goto(newrow, newcol);
1630 palkovsky 139
            lastrow = newrow;
140
            lastcol = newcol;
1492 palkovsky 141
            break;
1490 palkovsky 142
        case FB_GET_CSIZE:
1630 palkovsky 143
            ipc_answer_fast(callid, 0, HEIGHT, WIDTH);
1490 palkovsky 144
            continue;
1492 palkovsky 145
        case FB_CLEAR:
1498 palkovsky 146
            clrscr();
1492 palkovsky 147
            retval = 0;
148
            break;
1534 palkovsky 149
        case FB_SET_STYLE:
150
            fgcolor = IPC_GET_ARG1(call);
151
            bgcolor = IPC_GET_ARG2(call);
1630 palkovsky 152
            if (bgcolor == 0xf0f0f0)
1534 palkovsky 153
                set_style(0);
1630 palkovsky 154
            else if (fgcolor > bgcolor)
155
                set_style(0);
1534 palkovsky 156
            else
157
                set_style(7);
158
            retval = 0;
159
            break;
1630 palkovsky 160
        case FB_SCROLL:
161
            i = IPC_GET_ARG1(call);
162
            if (i > HEIGHT || i < -HEIGHT) {
163
                retval = EINVAL;
164
                break;
165
            }
166
            scroll(i);
167
            curs_goto(lastrow, lastcol);
168
            retval = 0;
169
            break;
170
 
1490 palkovsky 171
        default:
172
            retval = ENOENT;
173
        }
174
        ipc_answer_fast(callid,retval,0,0);
175
    }
176
}
177
 
1498 palkovsky 178
/** ANSI terminal emulation initialization */
1490 palkovsky 179
void sysio_init(void)
180
{
181
    async_set_client_connection(sysio_client_connection);
1498 palkovsky 182
    clrscr();
1492 palkovsky 183
    curs_goto(0,0);
1630 palkovsky 184
    /* Set scrolling region to 0-25 lines */
185
    sysputs("\033[0;25r");
1490 palkovsky 186
}