Subversion Repositories HelenOS

Rev

Rev 3767 | Rev 4141 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3767 Rev 3769
1
/*
1
/*
2
 * Copyright (c) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * Copyright (c) 2008 Martin Decky
3
 * Copyright (c) 2008 Martin Decky
4
 * Copyright (c) 2008 Pavel Rimsky
4
 * Copyright (c) 2008 Pavel Rimsky
5
 * All rights reserved.
5
 * All rights reserved.
6
 *
6
 *
7
 * Redistribution and use in source and binary forms, with or without
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
8
 * modification, are permitted provided that the following conditions
9
 * are met:
9
 * are met:
10
 *
10
 *
11
 * - Redistributions of source code must retain the above copyright
11
 * - Redistributions of source code must retain the above copyright
12
 *   notice, this list of conditions and the following disclaimer.
12
 *   notice, this list of conditions and the following disclaimer.
13
 * - Redistributions in binary form must reproduce the above copyright
13
 * - Redistributions in binary form must reproduce the above copyright
14
 *   notice, this list of conditions and the following disclaimer in the
14
 *   notice, this list of conditions and the following disclaimer in the
15
 *   documentation and/or other materials provided with the distribution.
15
 *   documentation and/or other materials provided with the distribution.
16
 * - The name of the author may not be used to endorse or promote products
16
 * - The name of the author may not be used to endorse or promote products
17
 *   derived from this software without specific prior written permission.
17
 *   derived from this software without specific prior written permission.
18
 *
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
29
 */
30
 
30
 
31
/**
31
/**
32
 * @defgroup serial Serial console
32
 * @defgroup serial Serial console
33
 * @brief    Serial console services (putc, puts, clear screen, cursor goto,...)
33
 * @brief    Serial console services (putc, puts, clear screen, cursor goto,...)
34
 * @{
34
 * @{
35
 */
35
 */
36
 
36
 
37
/** @file
37
/** @file
38
 */
38
 */
39
 
39
 
40
#include <stdio.h>
40
#include <stdio.h>
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
42
#include <async.h>
42
#include <async.h>
43
#include <ipc/fb.h>
43
#include <ipc/fb.h>
44
#include <bool.h>
44
#include <bool.h>
45
#include <errno.h>
45
#include <errno.h>
-
 
46
#include <console/color.h>
46
#include <console/style.h>
47
#include <console/style.h>
47
 
48
 
48
#include "serial_console.h"
49
#include "serial_console.h"
49
 
50
 
50
#define MAX_CONTROL 20
51
#define MAX_CONTROL 20
51
 
52
 
-
 
53
static void serial_sgr(const unsigned int mode);
-
 
54
 
52
static int width;
55
static int width;
53
static int height;
56
static int height;
-
 
57
static bool color = true;   /** True if producing color output. */
54
static putc_function_t putc_function;
58
static putc_function_t putc_function;
55
 
59
 
56
/* Allow only 1 connection */
60
/* Allow only 1 connection */
57
static int client_connected = 0;
61
static int client_connected = 0;
58
 
62
 
-
 
63
enum sgr_color_index {
-
 
64
    CI_BLACK    = 0,
-
 
65
    CI_RED      = 1,
-
 
66
    CI_GREEN    = 2,
-
 
67
    CI_BROWN    = 3,
-
 
68
    CI_BLUE     = 4,
-
 
69
    CI_MAGENTA  = 5,
-
 
70
    CI_CYAN     = 6,
-
 
71
    CI_WHITE    = 7,
-
 
72
};
-
 
73
 
-
 
74
enum sgr_command {
-
 
75
    SGR_RESET   = 0,
-
 
76
    SGR_BOLD    = 1,
-
 
77
    SGR_BLINK   = 5,
-
 
78
    SGR_REVERSE = 7,
-
 
79
    SGR_NORMAL_INT  = 22,
-
 
80
    SGR_BLINK_OFF   = 25,
-
 
81
    SGR_REVERSE_OFF = 27,
-
 
82
    SGR_FGCOLOR = 30,
-
 
83
    SGR_BGCOLOR = 40
-
 
84
};
-
 
85
 
-
 
86
static int color_map[] = {
-
 
87
    [COLOR_BLACK]   = CI_BLACK,
-
 
88
    [COLOR_BLUE]    = CI_RED,
-
 
89
    [COLOR_GREEN]   = CI_GREEN,
-
 
90
    [COLOR_CYAN]    = CI_CYAN,
-
 
91
    [COLOR_RED] = CI_RED,
-
 
92
    [COLOR_MAGENTA] = CI_MAGENTA,
-
 
93
    [COLOR_YELLOW]  = CI_BROWN,
-
 
94
    [COLOR_WHITE]   = CI_WHITE
-
 
95
};
-
 
96
 
59
void serial_puts(char *str)
97
void serial_puts(char *str)
60
{
98
{
61
    while (*str)
99
    while (*str)
62
        putc_function(*(str++));
100
        putc_function(*(str++));
63
}
101
}
64
 
102
 
65
void serial_goto(const unsigned int row, const unsigned int col)
103
void serial_goto(const unsigned int row, const unsigned int col)
66
{
104
{
67
    if ((row > height) || (col > width))
105
    if ((row > height) || (col > width))
68
        return;
106
        return;
69
   
107
   
70
    char control[20];
108
    char control[MAX_CONTROL];
71
    snprintf(control, 20, "\033[%u;%uf", row + 1, col + 1);
109
    snprintf(control, MAX_CONTROL, "\033[%u;%uf", row + 1, col + 1);
72
    serial_puts(control);
110
    serial_puts(control);
73
}
111
}
74
 
112
 
75
void serial_clrscr(void)
113
void serial_clrscr(void)
76
{
114
{
-
 
115
    /* Initialize graphic rendition attributes. */
-
 
116
    serial_sgr(SGR_RESET);
-
 
117
    if (color) {
-
 
118
        serial_sgr(SGR_FGCOLOR + CI_BLACK);
-
 
119
        serial_sgr(SGR_BGCOLOR + CI_WHITE);
-
 
120
    }
-
 
121
 
77
    serial_puts("\033[2J");
122
    serial_puts("\033[2J");
78
}
123
}
79
 
124
 
80
void serial_scroll(int i)
125
void serial_scroll(int i)
81
{
126
{
82
    if (i > 0) {
127
    if (i > 0) {
83
        serial_goto(height - 1, 0);
128
        serial_goto(height - 1, 0);
84
        while (i--)
129
        while (i--)
85
            serial_puts("\033D");
130
            serial_puts("\033D");
86
    } else if (i < 0) {
131
    } else if (i < 0) {
87
        serial_goto(0, 0);
132
        serial_goto(0, 0);
88
        while (i++)
133
        while (i++)
89
            serial_puts("\033M");
134
            serial_puts("\033M");
90
    }
135
    }
91
}
136
}
92
 
137
 
93
/** ECMA-48 Set Graphics Rendition. */
138
/** ECMA-48 Set Graphics Rendition. */
94
static void serial_sgr(const unsigned int mode)
139
static void serial_sgr(const unsigned int mode)
95
{
140
{
96
    char control[MAX_CONTROL];
141
    char control[MAX_CONTROL];
97
    snprintf(control, MAX_CONTROL, "\033[%um", mode);
142
    snprintf(control, MAX_CONTROL, "\033[%um", mode);
98
    serial_puts(control);
143
    serial_puts(control);
99
}
144
}
100
 
145
 
101
/** Set scrolling region. */
146
/** Set scrolling region. */
102
void serial_set_scroll_region(unsigned last_row)
147
void serial_set_scroll_region(unsigned last_row)
103
{
148
{
104
    char control[MAX_CONTROL];
149
    char control[MAX_CONTROL];
105
    snprintf(control, MAX_CONTROL, "\033[0;%ur", last_row);
150
    snprintf(control, MAX_CONTROL, "\033[0;%ur", last_row);
106
    serial_puts(control);
151
    serial_puts(control);
107
}
152
}
108
 
153
 
109
void serial_cursor_disable(void)
154
void serial_cursor_disable(void)
110
{
155
{
111
    serial_puts("\033[?25l");
156
    serial_puts("\033[?25l");
112
}
157
}
113
 
158
 
114
void serial_cursor_enable(void)
159
void serial_cursor_enable(void)
115
{
160
{
116
    serial_puts("\033[?25h");
161
    serial_puts("\033[?25h");
117
}
162
}
118
 
163
 
119
void serial_console_init(putc_function_t putc_fn, uint32_t w, uint32_t h)
164
void serial_console_init(putc_function_t putc_fn, uint32_t w, uint32_t h)
120
{
165
{
121
    width = w;
166
    width = w;
122
    height = h;
167
    height = h;
123
    putc_function = putc_fn;
168
    putc_function = putc_fn;
124
}
169
}
125
 
170
 
126
/**
171
/**
127
 * Main function of the thread serving client connections.
172
 * Main function of the thread serving client connections.
128
 */
173
 */
129
void serial_client_connection(ipc_callid_t iid, ipc_call_t *icall)
174
void serial_client_connection(ipc_callid_t iid, ipc_call_t *icall)
130
{
175
{
131
    int retval;
176
    int retval;
132
    ipc_callid_t callid;
177
    ipc_callid_t callid;
133
    ipc_call_t call;
178
    ipc_call_t call;
134
    char c;
179
    char c;
135
    int lastcol = 0;
180
    int lastcol = 0;
136
    int lastrow = 0;
181
    int lastrow = 0;
137
    int newcol;
182
    int newcol;
138
    int newrow;
183
    int newrow;
139
    int fgcolor;
184
    int fgcolor;
140
    int bgcolor;
185
    int bgcolor;
141
    int style;
186
    int style;
142
    int i;
187
    int i;
143
   
188
   
144
    if (client_connected) {
189
    if (client_connected) {
145
        ipc_answer_0(iid, ELIMIT);
190
        ipc_answer_0(iid, ELIMIT);
146
        return;
191
        return;
147
    }
192
    }
148
   
193
   
149
    client_connected = 1;
194
    client_connected = 1;
150
    ipc_answer_0(iid, EOK);
195
    ipc_answer_0(iid, EOK);
151
   
196
   
152
    /* Clear the terminal, set scrolling region
197
    /* Clear the terminal, set scrolling region
153
       to 0 - height rows. */
198
       to 0 - height rows. */
154
    serial_clrscr();
199
    serial_clrscr();
155
    serial_goto(0, 0);
200
    serial_goto(0, 0);
156
    serial_set_scroll_region(height);
201
    serial_set_scroll_region(height);
157
   
202
   
158
    while (true) {
203
    while (true) {
159
        callid = async_get_call(&call);
204
        callid = async_get_call(&call);
160
        switch (IPC_GET_METHOD(call)) {
205
        switch (IPC_GET_METHOD(call)) {
161
        case IPC_M_PHONE_HUNGUP:
206
        case IPC_M_PHONE_HUNGUP:
162
            client_connected = 0;
207
            client_connected = 0;
163
            ipc_answer_0(callid, EOK);
208
            ipc_answer_0(callid, EOK);
164
            return;
209
            return;
165
        case FB_PUTCHAR:
210
        case FB_PUTCHAR:
166
            c = IPC_GET_ARG1(call);
211
            c = IPC_GET_ARG1(call);
167
            newrow = IPC_GET_ARG2(call);
212
            newrow = IPC_GET_ARG2(call);
168
            newcol = IPC_GET_ARG3(call);
213
            newcol = IPC_GET_ARG3(call);
169
            if ((lastcol != newcol) || (lastrow != newrow))
214
            if ((lastcol != newcol) || (lastrow != newrow))
170
                serial_goto(newrow, newcol);
215
                serial_goto(newrow, newcol);
171
            lastcol = newcol + 1;
216
            lastcol = newcol + 1;
172
            lastrow = newrow;
217
            lastrow = newrow;
173
            (*putc_function)(c);
218
            (*putc_function)(c);
174
            retval = 0;
219
            retval = 0;
175
            break;
220
            break;
176
        case FB_CURSOR_GOTO:
221
        case FB_CURSOR_GOTO:
177
            newrow = IPC_GET_ARG1(call);
222
            newrow = IPC_GET_ARG1(call);
178
            newcol = IPC_GET_ARG2(call);
223
            newcol = IPC_GET_ARG2(call);
179
            serial_goto(newrow, newcol);
224
            serial_goto(newrow, newcol);
180
            lastrow = newrow;
225
            lastrow = newrow;
181
            lastcol = newcol;
226
            lastcol = newcol;
182
            retval = 0;
227
            retval = 0;
183
            break;
228
            break;
184
        case FB_GET_CSIZE:
229
        case FB_GET_CSIZE:
185
            ipc_answer_2(callid, EOK, height, width);
230
            ipc_answer_2(callid, EOK, height, width);
186
            continue;
231
            continue;
187
        case FB_CLEAR:
232
        case FB_CLEAR:
188
            serial_clrscr();
233
            serial_clrscr();
189
            retval = 0;
234
            retval = 0;
190
            break;
235
            break;
191
        case FB_SET_STYLE:
236
        case FB_SET_STYLE:
192
            style =  IPC_GET_ARG1(call);
237
            style =  IPC_GET_ARG1(call);
193
            if (style == STYLE_EMPHASIS)
238
            if (style == STYLE_EMPHASIS) {
-
 
239
                if (color) {
-
 
240
                    serial_sgr(SGR_RESET);
-
 
241
                    serial_sgr(SGR_FGCOLOR + CI_RED);
-
 
242
                    serial_sgr(SGR_BGCOLOR + CI_WHITE);
-
 
243
                }
194
                serial_sgr(1);
244
                serial_sgr(SGR_BOLD);
195
            else
245
            } else {
-
 
246
                if (color) {
196
                serial_sgr(0);
247
                    serial_sgr(SGR_RESET);
-
 
248
                    serial_sgr(SGR_FGCOLOR + CI_BLACK);
-
 
249
                    serial_sgr(SGR_BGCOLOR + CI_WHITE);
-
 
250
                }
-
 
251
                serial_sgr(SGR_NORMAL_INT);
-
 
252
            }
197
            retval = 0;
253
            retval = 0;
198
            break;
254
            break;
199
        case FB_SET_COLOR:
255
        case FB_SET_COLOR:
200
            fgcolor = IPC_GET_ARG1(call);
256
            fgcolor = IPC_GET_ARG1(call);
201
            bgcolor = IPC_GET_ARG2(call);
257
            bgcolor = IPC_GET_ARG2(call);
-
 
258
 
-
 
259
            if (color) {
-
 
260
                serial_sgr(SGR_RESET);
-
 
261
                serial_sgr(SGR_FGCOLOR + color_map[fgcolor]);
-
 
262
                serial_sgr(SGR_BGCOLOR + color_map[bgcolor]);
-
 
263
            } else {
202
            if (fgcolor < bgcolor)
264
                if (fgcolor < bgcolor)
203
                serial_sgr(0);
265
                    serial_sgr(SGR_RESET);
204
            else
266
                else
205
                serial_sgr(7);
267
                    serial_sgr(SGR_REVERSE);
-
 
268
            }
206
            retval = 0;
269
            retval = 0;
207
            break;
270
            break;
208
        case FB_SET_RGB_COLOR:
271
        case FB_SET_RGB_COLOR:
209
            fgcolor = IPC_GET_ARG1(call);
272
            fgcolor = IPC_GET_ARG1(call);
210
            bgcolor = IPC_GET_ARG2(call);
273
            bgcolor = IPC_GET_ARG2(call);
211
            if (fgcolor < bgcolor)
274
            if (fgcolor < bgcolor)
212
                serial_sgr(0);
275
                serial_sgr(SGR_REVERSE_OFF);
213
            else
276
            else
214
                serial_sgr(7);
277
                serial_sgr(SGR_REVERSE);
215
            retval = 0;
278
            retval = 0;
216
            break;
279
            break;
217
        case FB_SCROLL:
280
        case FB_SCROLL:
218
            i = IPC_GET_ARG1(call);
281
            i = IPC_GET_ARG1(call);
219
            if ((i > height) || (i < -height)) {
282
            if ((i > height) || (i < -height)) {
220
                retval = EINVAL;
283
                retval = EINVAL;
221
                break;
284
                break;
222
            }
285
            }
223
            serial_scroll(i);
286
            serial_scroll(i);
224
            serial_goto(lastrow, lastcol);
287
            serial_goto(lastrow, lastcol);
225
            retval = 0;
288
            retval = 0;
226
            break;
289
            break;
227
        case FB_CURSOR_VISIBILITY:
290
        case FB_CURSOR_VISIBILITY:
228
            if(IPC_GET_ARG1(call))
291
            if(IPC_GET_ARG1(call))
229
                serial_cursor_enable();
292
                serial_cursor_enable();
230
            else
293
            else
231
                serial_cursor_disable();
294
                serial_cursor_disable();
232
            retval = 0;
295
            retval = 0;
233
            break;
296
            break;
234
        default:
297
        default:
235
            retval = ENOENT;
298
            retval = ENOENT;
236
        }
299
        }
237
        ipc_answer_0(callid, retval);
300
        ipc_answer_0(callid, retval);
238
    }
301
    }
239
}
302
}
240
 
303
 
241
/**
304
/**
242
 * @}
305
 * @}
243
 */
306
 */
244
 
307