Subversion Repositories HelenOS-historic

Rev

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