Subversion Repositories HelenOS

Rev

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

Rev 3761 Rev 3767
1
/*
1
/*
2
 * Copyright (c) 2006 Josef Cejka
2
 * Copyright (c) 2006 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup console
29
/** @addtogroup console
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <libc.h>
35
#include <libc.h>
36
#include <fb.h>
36
#include <fb.h>
37
#include <ipc/ipc.h>
37
#include <ipc/ipc.h>
38
#include <keys.h>
38
#include <keys.h>
39
#include <ipc/fb.h>
39
#include <ipc/fb.h>
40
#include <ipc/services.h>
40
#include <ipc/services.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <key_buffer.h>
42
#include <key_buffer.h>
43
#include <console.h>
-
 
44
#include <ipc/console.h>
43
#include <ipc/console.h>
45
#include <unistd.h>
44
#include <unistd.h>
46
#include <async.h>
45
#include <async.h>
47
#include <libadt/fifo.h>
46
#include <libadt/fifo.h>
48
#include <screenbuffer.h>
47
#include <screenbuffer.h>
49
#include <sys/mman.h>
48
#include <sys/mman.h>
50
#include <stdio.h>
49
#include <stdio.h>
51
#include <sysinfo.h>
50
#include <sysinfo.h>
52
 
51
 
-
 
52
#include "console.h"
53
#include "gcons.h"
53
#include "gcons.h"
54
 
54
 
55
#define MAX_KEYREQUESTS_BUFFERED 32
55
#define MAX_KEYREQUESTS_BUFFERED 32
56
 
56
 
57
#define NAME "console"
57
#define NAME "console"
58
 
58
 
59
/** Index of currently used virtual console.
59
/** Index of currently used virtual console.
60
 */
60
 */
61
int active_console = 0;
61
int active_console = 0;
62
int prev_console = 0;
62
int prev_console = 0;
63
 
63
 
64
/** Information about framebuffer
64
/** Information about framebuffer
65
 */
65
 */
66
struct {
66
struct {
67
    int phone;      /**< Framebuffer phone */
67
    int phone;      /**< Framebuffer phone */
68
    ipcarg_t rows;      /**< Framebuffer rows */
68
    ipcarg_t rows;      /**< Framebuffer rows */
69
    ipcarg_t cols;      /**< Framebuffer columns */
69
    ipcarg_t cols;      /**< Framebuffer columns */
70
} fb_info;
70
} fb_info;
71
 
71
 
72
typedef struct {
72
typedef struct {
73
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
73
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
74
    /** Buffer for unsatisfied request for keys. */
74
    /** Buffer for unsatisfied request for keys. */
75
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
75
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
76
        MAX_KEYREQUESTS_BUFFERED); 
76
        MAX_KEYREQUESTS_BUFFERED); 
77
    int keyrequest_counter;     /**< Number of requests in buffer. */
77
    int keyrequest_counter;     /**< Number of requests in buffer. */
78
    int client_phone;       /**< Phone to connected client. */
78
    int client_phone;       /**< Phone to connected client. */
79
    int used;           /**< 1 if this virtual console is
79
    int used;           /**< 1 if this virtual console is
80
                     * connected to some client.*/
80
                     * connected to some client.*/
81
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
81
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
82
                     * contents and related settings. */
82
                     * contents and related settings. */
83
} connection_t;
83
} connection_t;
84
 
84
 
85
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
85
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
86
                         * consoles */
86
                         * consoles */
87
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
87
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
88
                         * with framebufer used for
88
                         * with framebufer used for
89
                         * faster virtual console
89
                         * faster virtual console
90
                         * switching */
90
                         * switching */
91
 
91
 
92
 
92
 
93
/** Find unused virtual console.
93
/** Find unused virtual console.
94
 *
94
 *
95
 */
95
 */
96
static int find_free_connection(void)
96
static int find_free_connection(void)
97
{
97
{
98
    int i;
98
    int i;
99
   
99
   
100
    for (i = 0; i < CONSOLE_COUNT; i++) {
100
    for (i = 0; i < CONSOLE_COUNT; i++) {
101
        if (!connections[i].used)
101
        if (!connections[i].used)
102
            return i;
102
            return i;
103
    }
103
    }
104
    return -1;
104
    return -1;
105
}
105
}
106
 
106
 
107
static void clrscr(void)
107
static void clrscr(void)
108
{
108
{
109
    async_msg_0(fb_info.phone, FB_CLEAR);
109
    async_msg_0(fb_info.phone, FB_CLEAR);
110
}
110
}
111
 
111
 
112
static void curs_visibility(bool visible)
112
static void curs_visibility(bool visible)
113
{
113
{
114
    async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
114
    async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
115
}
115
}
116
 
116
 
117
static void curs_hide_sync(void)
117
static void curs_hide_sync(void)
118
{
118
{
119
    ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
119
    ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
120
}
120
}
121
 
121
 
122
static void curs_goto(int row, int col)
122
static void curs_goto(int row, int col)
123
{
123
{
124
    async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
124
    async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
125
}
125
}
126
 
126
 
127
static void set_style(style_t *style)
127
static void set_style(int style)
128
{
128
{
129
    async_msg_2(fb_info.phone, FB_SET_STYLE, style->fg_color,
129
    async_msg_1(fb_info.phone, FB_SET_STYLE, style);
130
        style->bg_color);
-
 
131
}
130
}
132
 
131
 
133
static void set_style_col(int fgcolor, int bgcolor)
132
static void set_color(int fgcolor, int bgcolor, int flags)
134
{
133
{
-
 
134
    async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
-
 
135
}
-
 
136
 
-
 
137
static void set_rgb_color(int fgcolor, int bgcolor)
-
 
138
{
135
    async_msg_2(fb_info.phone, FB_SET_STYLE, fgcolor, bgcolor);
139
    async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor);
-
 
140
}
-
 
141
 
-
 
142
static void set_attrs(attrs_t *attrs)
-
 
143
{
-
 
144
    switch (attrs->t) {
-
 
145
    case at_style:
-
 
146
        set_style(attrs->a.s.style);
-
 
147
        break;
-
 
148
 
-
 
149
    case at_idx:
-
 
150
        set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
-
 
151
            attrs->a.i.flags);
-
 
152
        break;
-
 
153
 
-
 
154
    case at_rgb:
-
 
155
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
-
 
156
        break;
-
 
157
    }
136
}
158
}
137
 
159
 
138
static void prtchr(char c, int row, int col)
160
static void prtchr(char c, int row, int col)
139
{
161
{
140
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
162
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
141
}
163
}
142
 
164
 
143
/** Check key and process special keys.
165
/** Check key and process special keys.
144
 *
166
 *
145
 *
167
 *
146
 */
168
 */
147
static void write_char(int console, char key)
169
static void write_char(int console, char key)
148
{
170
{
149
    screenbuffer_t *scr = &(connections[console].screenbuffer);
171
    screenbuffer_t *scr = &(connections[console].screenbuffer);
150
   
172
   
151
    switch (key) {
173
    switch (key) {
152
    case '\n':
174
    case '\n':
153
        scr->position_y++;
175
        scr->position_y++;
154
        scr->position_x = 0;
176
        scr->position_x = 0;
155
        break;
177
        break;
156
    case '\r':
178
    case '\r':
157
        break;
179
        break;
158
    case '\t':
180
    case '\t':
159
        scr->position_x += 8;
181
        scr->position_x += 8;
160
        scr->position_x -= scr->position_x % 8;
182
        scr->position_x -= scr->position_x % 8;
161
        break;
183
        break;
162
    case '\b':
184
    case '\b':
163
        if (scr->position_x == 0)
185
        if (scr->position_x == 0)
164
            break;
186
            break;
165
        scr->position_x--;
187
        scr->position_x--;
166
        if (console == active_console)
188
        if (console == active_console)
167
            prtchr(' ', scr->position_y, scr->position_x);
189
            prtchr(' ', scr->position_y, scr->position_x);
168
        screenbuffer_putchar(scr, ' ');
190
        screenbuffer_putchar(scr, ' ');
169
        break;
191
        break;
170
    default:   
192
    default:   
171
        if (console == active_console)
193
        if (console == active_console)
172
            prtchr(key, scr->position_y, scr->position_x);
194
            prtchr(key, scr->position_y, scr->position_x);
173
 
195
 
174
        screenbuffer_putchar(scr, key);
196
        screenbuffer_putchar(scr, key);
175
        scr->position_x++;
197
        scr->position_x++;
176
    }
198
    }
177
   
199
   
178
    scr->position_y += (scr->position_x >= scr->size_x);
200
    scr->position_y += (scr->position_x >= scr->size_x);
179
   
201
   
180
    if (scr->position_y >= scr->size_y) {
202
    if (scr->position_y >= scr->size_y) {
181
        scr->position_y = scr->size_y - 1;
203
        scr->position_y = scr->size_y - 1;
182
        screenbuffer_clear_line(scr, scr->top_line);
204
        screenbuffer_clear_line(scr, scr->top_line);
183
        scr->top_line = (scr->top_line + 1) % scr->size_y;
205
        scr->top_line = (scr->top_line + 1) % scr->size_y;
184
        if (console == active_console)
206
        if (console == active_console)
185
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
207
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
186
    }
208
    }
187
   
209
   
188
    scr->position_x = scr->position_x % scr->size_x;
210
    scr->position_x = scr->position_x % scr->size_x;
189
   
211
   
190
    if (console == active_console)
212
    if (console == active_console)
191
        curs_goto(scr->position_y, scr->position_x);
213
        curs_goto(scr->position_y, scr->position_x);
192
   
214
   
193
}
215
}
194
 
216
 
195
/** Switch to new console */
217
/** Switch to new console */
196
static void change_console(int newcons)
218
static void change_console(int newcons)
197
{
219
{
198
    connection_t *conn;
220
    connection_t *conn;
199
    int i, j, rc;
221
    int i, j, rc;
200
    keyfield_t *field;
222
    keyfield_t *field;
201
    style_t *style;
223
    attrs_t *attrs;
202
   
224
   
203
    if (newcons == active_console)
225
    if (newcons == active_console)
204
        return;
226
        return;
205
   
227
   
206
    if (newcons == KERNEL_CONSOLE) {
228
    if (newcons == KERNEL_CONSOLE) {
207
        async_serialize_start();
229
        async_serialize_start();
208
        curs_hide_sync();
230
        curs_hide_sync();
209
        gcons_in_kernel();
231
        gcons_in_kernel();
210
        async_serialize_end();
232
        async_serialize_end();
211
       
233
       
212
        if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
234
        if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
213
            prev_console = active_console;
235
            prev_console = active_console;
214
            active_console = KERNEL_CONSOLE;
236
            active_console = KERNEL_CONSOLE;
215
        } else
237
        } else
216
            newcons = active_console;
238
            newcons = active_console;
217
    }
239
    }
218
   
240
   
219
    if (newcons != KERNEL_CONSOLE) {
241
    if (newcons != KERNEL_CONSOLE) {
220
        async_serialize_start();
242
        async_serialize_start();
221
       
243
       
222
        if (active_console == KERNEL_CONSOLE)
244
        if (active_console == KERNEL_CONSOLE)
223
            gcons_redraw_console();
245
            gcons_redraw_console();
224
       
246
       
225
        active_console = newcons;
247
        active_console = newcons;
226
        gcons_change_console(newcons);
248
        gcons_change_console(newcons);
227
        conn = &connections[active_console];
249
        conn = &connections[active_console];
228
       
250
       
229
        set_style(&conn->screenbuffer.style);
251
        set_attrs(&conn->screenbuffer.attrs);
230
        curs_visibility(false);
252
        curs_visibility(false);
231
        if (interbuffer) {
253
        if (interbuffer) {
232
            for (i = 0; i < conn->screenbuffer.size_x; i++)
254
            for (i = 0; i < conn->screenbuffer.size_x; i++)
233
                for (j = 0; j < conn->screenbuffer.size_y; j++) {
255
                for (j = 0; j < conn->screenbuffer.size_y; j++) {
234
                    unsigned int size_x;
256
                    unsigned int size_x;
235
                   
257
                   
236
                    size_x = conn->screenbuffer.size_x;
258
                    size_x = conn->screenbuffer.size_x;
237
                    interbuffer[i + j * size_x] =
259
                    interbuffer[i + j * size_x] =
238
                        *get_field_at(&conn->screenbuffer, i, j);
260
                        *get_field_at(&conn->screenbuffer, i, j);
239
                }
261
                }
240
            /* This call can preempt, but we are already at the end */
262
            /* This call can preempt, but we are already at the end */
241
            rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA);
263
            rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA);
242
        }
264
        }
243
       
265
       
244
        if ((!interbuffer) || (rc != 0)) {
266
        if ((!interbuffer) || (rc != 0)) {
245
            set_style(&conn->screenbuffer.style);
267
            set_attrs(&conn->screenbuffer.attrs);
246
            clrscr();
268
            clrscr();
247
            style = &conn->screenbuffer.style;
269
            attrs = &conn->screenbuffer.attrs;
248
           
270
           
249
            for (j = 0; j < conn->screenbuffer.size_y; j++)
271
            for (j = 0; j < conn->screenbuffer.size_y; j++)
250
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
272
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
251
                    field = get_field_at(&conn->screenbuffer, i, j);
273
                    field = get_field_at(&conn->screenbuffer, i, j);
252
                    if (!style_same(*style, field->style))
274
                    if (!attrs_same(*attrs, field->attrs))
253
                        set_style(&field->style);
275
                        set_attrs(&field->attrs);
254
                    style = &field->style;
276
                    attrs = &field->attrs;
255
                    if ((field->character == ' ') &&
277
                    if ((field->character == ' ') &&
256
                        (style_same(field->style,
278
                        (attrs_same(field->attrs,
257
                        conn->screenbuffer.style)))
279
                        conn->screenbuffer.attrs)))
258
                        continue;
280
                        continue;
259
                   
281
 
260
                    prtchr(field->character, j, i);
282
                    prtchr(field->character, j, i);
261
                }
283
                }
262
        }
284
        }
263
       
285
       
264
        curs_goto(conn->screenbuffer.position_y,
286
        curs_goto(conn->screenbuffer.position_y,
265
            conn->screenbuffer.position_x);
287
            conn->screenbuffer.position_x);
266
        curs_visibility(conn->screenbuffer.is_cursor_visible);
288
        curs_visibility(conn->screenbuffer.is_cursor_visible);
267
       
289
       
268
        async_serialize_end();
290
        async_serialize_end();
269
    }
291
    }
270
}
292
}
271
 
293
 
272
/** Handler for keyboard */
294
/** Handler for keyboard */
273
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
295
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
274
{
296
{
275
    ipc_callid_t callid;
297
    ipc_callid_t callid;
276
    ipc_call_t call;
298
    ipc_call_t call;
277
    int retval;
299
    int retval;
278
    int c;
300
    int c;
279
    connection_t *conn;
301
    connection_t *conn;
280
    int newcon;
302
    int newcon;
281
   
303
   
282
    /* Ignore parameters, the connection is alread opened */
304
    /* Ignore parameters, the connection is alread opened */
283
    while (1) {
305
    while (1) {
284
        callid = async_get_call(&call);
306
        callid = async_get_call(&call);
285
        switch (IPC_GET_METHOD(call)) {
307
        switch (IPC_GET_METHOD(call)) {
286
        case IPC_M_PHONE_HUNGUP:
308
        case IPC_M_PHONE_HUNGUP:
287
            /* TODO: Handle hangup */
309
            /* TODO: Handle hangup */
288
            return;
310
            return;
289
        case KBD_MS_LEFT:
311
        case KBD_MS_LEFT:
290
            newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
312
            newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
291
            if (newcon != -1)
313
            if (newcon != -1)
292
                change_console(newcon);
314
                change_console(newcon);
293
            retval = 0;
315
            retval = 0;
294
            break;
316
            break;
295
        case KBD_MS_MOVE:
317
        case KBD_MS_MOVE:
296
            gcons_mouse_move(IPC_GET_ARG1(call),
318
            gcons_mouse_move(IPC_GET_ARG1(call),
297
                IPC_GET_ARG2(call));
319
                IPC_GET_ARG2(call));
298
            retval = 0;
320
            retval = 0;
299
            break;
321
            break;
300
        case KBD_PUSHCHAR:
322
        case KBD_PUSHCHAR:
301
            /* got key from keyboard driver */
323
            /* got key from keyboard driver */
302
            retval = 0;
324
            retval = 0;
303
            c = IPC_GET_ARG1(call);
325
            c = IPC_GET_ARG1(call);
304
            /* switch to another virtual console */
326
            /* switch to another virtual console */
305
           
327
           
306
            conn = &connections[active_console];
328
            conn = &connections[active_console];
307
/*
329
/*
308
 *          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 +
330
 *          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 +
309
 *              CONSOLE_COUNT)) {
331
 *              CONSOLE_COUNT)) {
310
 */
332
 */
311
            if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
333
            if ((c >= 0x101) && (c < 0x101 + CONSOLE_COUNT)) {
312
                if (c == 0x112)
334
                if (c == 0x112)
313
                    change_console(KERNEL_CONSOLE);
335
                    change_console(KERNEL_CONSOLE);
314
                else
336
                else
315
                    change_console(c - 0x101);
337
                    change_console(c - 0x101);
316
                break;
338
                break;
317
            }
339
            }
318
           
340
           
319
            /* if client is awaiting key, send it */
341
            /* if client is awaiting key, send it */
320
            if (conn->keyrequest_counter > 0) {    
342
            if (conn->keyrequest_counter > 0) {    
321
                conn->keyrequest_counter--;
343
                conn->keyrequest_counter--;
322
                ipc_answer_1(fifo_pop(conn->keyrequests), EOK,
344
                ipc_answer_1(fifo_pop(conn->keyrequests), EOK,
323
                    c);
345
                    c);
324
                break;
346
                break;
325
            }
347
            }
326
           
348
           
327
            keybuffer_push(&conn->keybuffer, c);
349
            keybuffer_push(&conn->keybuffer, c);
328
            retval = 0;
350
            retval = 0;
329
           
351
           
330
            break;
352
            break;
331
        default:
353
        default:
332
            retval = ENOENT;
354
            retval = ENOENT;
333
        }
355
        }
334
        ipc_answer_0(callid, retval);
356
        ipc_answer_0(callid, retval);
335
    }
357
    }
336
}
358
}
337
 
359
 
338
/** Default thread for new connections */
360
/** Default thread for new connections */
339
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
361
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
340
{
362
{
341
    ipc_callid_t callid;
363
    ipc_callid_t callid;
342
    ipc_call_t call;
364
    ipc_call_t call;
343
    int consnum;
365
    int consnum;
344
    ipcarg_t arg1, arg2;
366
    ipcarg_t arg1, arg2, arg3;
345
    connection_t *conn;
367
    connection_t *conn;
346
   
368
   
347
    if ((consnum = find_free_connection()) == -1) {
369
    if ((consnum = find_free_connection()) == -1) {
348
        ipc_answer_0(iid, ELIMIT);
370
        ipc_answer_0(iid, ELIMIT);
349
        return;
371
        return;
350
    }
372
    }
351
    conn = &connections[consnum];
373
    conn = &connections[consnum];
352
    conn->used = 1;
374
    conn->used = 1;
353
   
375
   
354
    async_serialize_start();
376
    async_serialize_start();
355
    gcons_notify_connect(consnum);
377
    gcons_notify_connect(consnum);
356
    conn->client_phone = IPC_GET_ARG5(*icall);
378
    conn->client_phone = IPC_GET_ARG5(*icall);
357
    screenbuffer_clear(&conn->screenbuffer);
379
    screenbuffer_clear(&conn->screenbuffer);
358
   
380
   
359
    /* Accept the connection */
381
    /* Accept the connection */
360
    ipc_answer_0(iid, EOK);
382
    ipc_answer_0(iid, EOK);
361
   
383
   
362
    while (1) {
384
    while (1) {
363
        async_serialize_end();
385
        async_serialize_end();
364
        callid = async_get_call(&call);
386
        callid = async_get_call(&call);
365
        async_serialize_start();
387
        async_serialize_start();
366
       
388
       
367
        arg1 = 0;
389
        arg1 = 0;
368
        arg2 = 0;
390
        arg2 = 0;
369
        switch (IPC_GET_METHOD(call)) {
391
        switch (IPC_GET_METHOD(call)) {
370
        case IPC_M_PHONE_HUNGUP:
392
        case IPC_M_PHONE_HUNGUP:
371
            gcons_notify_disconnect(consnum);
393
            gcons_notify_disconnect(consnum);
372
           
394
           
373
            /* Answer all pending requests */
395
            /* Answer all pending requests */
374
            while (conn->keyrequest_counter > 0) {
396
            while (conn->keyrequest_counter > 0) {
375
                conn->keyrequest_counter--;
397
                conn->keyrequest_counter--;
376
                ipc_answer_0(fifo_pop(conn->keyrequests),
398
                ipc_answer_0(fifo_pop(conn->keyrequests),
377
                    ENOENT);
399
                    ENOENT);
378
                break;
400
                break;
379
            }
401
            }
380
            conn->used = 0;
402
            conn->used = 0;
381
            return;
403
            return;
382
        case CONSOLE_PUTCHAR:
404
        case CONSOLE_PUTCHAR:
383
            write_char(consnum, IPC_GET_ARG1(call));
405
            write_char(consnum, IPC_GET_ARG1(call));
384
            gcons_notify_char(consnum);
406
            gcons_notify_char(consnum);
385
            break;
407
            break;
386
        case CONSOLE_CLEAR:
408
        case CONSOLE_CLEAR:
387
            /* Send message to fb */
409
            /* Send message to fb */
388
            if (consnum == active_console) {
410
            if (consnum == active_console) {
389
                async_msg_0(fb_info.phone, FB_CLEAR);
411
                async_msg_0(fb_info.phone, FB_CLEAR);
390
            }
412
            }
391
           
413
           
392
            screenbuffer_clear(&conn->screenbuffer);
414
            screenbuffer_clear(&conn->screenbuffer);
393
           
415
           
394
            break;
416
            break;
395
        case CONSOLE_GOTO:
417
        case CONSOLE_GOTO:
396
            screenbuffer_goto(&conn->screenbuffer,
418
            screenbuffer_goto(&conn->screenbuffer,
397
                IPC_GET_ARG2(call), IPC_GET_ARG1(call));
419
                IPC_GET_ARG2(call), IPC_GET_ARG1(call));
398
            if (consnum == active_console)
420
            if (consnum == active_console)
399
                curs_goto(IPC_GET_ARG1(call),
421
                curs_goto(IPC_GET_ARG1(call),
400
                    IPC_GET_ARG2(call));
422
                    IPC_GET_ARG2(call));
401
            break;
423
            break;
402
        case CONSOLE_GETSIZE:
424
        case CONSOLE_GETSIZE:
403
            arg1 = fb_info.rows;
425
            arg1 = fb_info.rows;
404
            arg2 = fb_info.cols;
426
            arg2 = fb_info.cols;
405
            break;
427
            break;
406
        case CONSOLE_FLUSH:
428
        case CONSOLE_FLUSH:
407
            if (consnum == active_console)
429
            if (consnum == active_console)
408
                async_req_0_0(fb_info.phone, FB_FLUSH);
430
                async_req_0_0(fb_info.phone, FB_FLUSH);
409
            break;
431
            break;
410
        case CONSOLE_SET_STYLE:
432
        case CONSOLE_SET_STYLE:
411
            arg1 = IPC_GET_ARG1(call);
433
            arg1 = IPC_GET_ARG1(call);
-
 
434
            screenbuffer_set_style(&conn->screenbuffer, arg1);
-
 
435
            if (consnum == active_console)
-
 
436
                set_style(arg1);
-
 
437
            break;
-
 
438
        case CONSOLE_SET_COLOR:
-
 
439
            arg1 = IPC_GET_ARG1(call);
-
 
440
            arg2 = IPC_GET_ARG2(call);
-
 
441
            arg3 = IPC_GET_ARG3(call);
-
 
442
            screenbuffer_set_color(&conn->screenbuffer, arg1,
-
 
443
                arg2, arg3);
-
 
444
            if (consnum == active_console)
-
 
445
                set_color(arg1, arg2, arg3);
-
 
446
            break;
-
 
447
        case CONSOLE_SET_RGB_COLOR:
-
 
448
            arg1 = IPC_GET_ARG1(call);
412
            arg2 = IPC_GET_ARG2(call);
449
            arg2 = IPC_GET_ARG2(call);
413
            screenbuffer_set_style(&conn->screenbuffer, arg1,
450
            screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
414
                arg2);
451
                arg2);
415
            if (consnum == active_console)
452
            if (consnum == active_console)
416
                set_style_col(arg1, arg2);
453
                set_rgb_color(arg1, arg2);
417
            break;
454
            break;
418
        case CONSOLE_CURSOR_VISIBILITY:
455
        case CONSOLE_CURSOR_VISIBILITY:
419
            arg1 = IPC_GET_ARG1(call);
456
            arg1 = IPC_GET_ARG1(call);
420
            conn->screenbuffer.is_cursor_visible = arg1;
457
            conn->screenbuffer.is_cursor_visible = arg1;
421
            if (consnum == active_console)
458
            if (consnum == active_console)
422
                curs_visibility(arg1);
459
                curs_visibility(arg1);
423
            break;
460
            break;
424
        case CONSOLE_GETCHAR:
461
        case CONSOLE_GETCHAR:
425
            if (keybuffer_empty(&conn->keybuffer)) {
462
            if (keybuffer_empty(&conn->keybuffer)) {
426
                /* buffer is empty -> store request */
463
                /* buffer is empty -> store request */
427
                if (conn->keyrequest_counter <
464
                if (conn->keyrequest_counter <
428
                    MAX_KEYREQUESTS_BUFFERED) {
465
                    MAX_KEYREQUESTS_BUFFERED) {
429
                    fifo_push(conn->keyrequests, callid);
466
                    fifo_push(conn->keyrequests, callid);
430
                    conn->keyrequest_counter++;
467
                    conn->keyrequest_counter++;
431
                } else {
468
                } else {
432
                    /*
469
                    /*
433
                     * No key available and too many
470
                     * No key available and too many
434
                     * requests => fail.
471
                     * requests => fail.
435
                    */
472
                    */
436
                    ipc_answer_0(callid, ELIMIT);
473
                    ipc_answer_0(callid, ELIMIT);
437
                }
474
                }
438
                continue;
475
                continue;
439
            }
476
            }
440
            int ch;
477
            int ch;
441
            keybuffer_pop(&conn->keybuffer, &ch);
478
            keybuffer_pop(&conn->keybuffer, &ch);
442
            arg1 = ch;
479
            arg1 = ch;
443
            break;
480
            break;
444
        }
481
        }
445
        ipc_answer_2(callid, EOK, arg1, arg2);
482
        ipc_answer_2(callid, EOK, arg1, arg2);
446
    }
483
    }
447
}
484
}
448
 
485
 
449
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
486
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
450
{
487
{
451
    change_console(prev_console);
488
    change_console(prev_console);
452
}
489
}
453
 
490
 
454
int main(int argc, char *argv[])
491
int main(int argc, char *argv[])
455
{
492
{
456
    printf(NAME ": HelenOS Console service\n");
493
    printf(NAME ": HelenOS Console service\n");
457
   
494
   
458
    ipcarg_t phonehash;
495
    ipcarg_t phonehash;
459
    int kbd_phone;
496
    int kbd_phone;
460
    int i;
497
    int i;
461
   
498
   
462
    async_set_client_connection(client_connection);
499
    async_set_client_connection(client_connection);
463
   
500
   
464
    /* Connect to keyboard driver */
501
    /* Connect to keyboard driver */
465
   
502
   
466
    kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
503
    kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
467
    while (kbd_phone < 0) {
504
    while (kbd_phone < 0) {
468
        usleep(10000);
505
        usleep(10000);
469
        kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
506
        kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
470
    }
507
    }
471
   
508
   
472
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
509
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
473
        return -1;
510
        return -1;
474
    async_new_connection(phonehash, 0, NULL, keyboard_events);
511
    async_new_connection(phonehash, 0, NULL, keyboard_events);
475
   
512
   
476
    /* Connect to framebuffer driver */
513
    /* Connect to framebuffer driver */
477
    fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
514
    fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
478
    while (fb_info.phone < 0) {
515
    while (fb_info.phone < 0) {
479
        usleep(10000);
516
        usleep(10000);
480
        fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
517
        fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0);
481
    }
518
    }
482
   
519
   
483
    /* Initialize gcons */
520
    /* Initialize gcons */
484
    gcons_init(fb_info.phone);
521
    gcons_init(fb_info.phone);
485
    /* Synchronize, the gcons can have something in queue */
522
    /* Synchronize, the gcons can have something in queue */
486
    async_req_0_0(fb_info.phone, FB_FLUSH);
523
    async_req_0_0(fb_info.phone, FB_FLUSH);
487
   
524
   
488
    async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
525
    async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
489
        &fb_info.cols);
526
        &fb_info.cols);
490
    set_style_col(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
527
    set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
491
    clrscr();
528
    clrscr();
492
   
529
   
493
    /* Init virtual consoles */
530
    /* Init virtual consoles */
494
    for (i = 0; i < CONSOLE_COUNT; i++) {
531
    for (i = 0; i < CONSOLE_COUNT; i++) {
495
        connections[i].used = 0;
532
        connections[i].used = 0;
496
        keybuffer_init(&connections[i].keybuffer);
533
        keybuffer_init(&connections[i].keybuffer);
497
       
534
       
498
        connections[i].keyrequests.head = 0;
535
        connections[i].keyrequests.head = 0;
499
        connections[i].keyrequests.tail = 0;
536
        connections[i].keyrequests.tail = 0;
500
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
537
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
501
        connections[i].keyrequest_counter = 0;
538
        connections[i].keyrequest_counter = 0;
502
       
539
       
503
        if (screenbuffer_init(&connections[i].screenbuffer,
540
        if (screenbuffer_init(&connections[i].screenbuffer,
504
            fb_info.cols, fb_info.rows) == NULL) {
541
            fb_info.cols, fb_info.rows) == NULL) {
505
            /* FIXME: handle error */
542
            /* FIXME: handle error */
506
            return -1;
543
            return -1;
507
        }
544
        }
508
    }
545
    }
509
    connections[KERNEL_CONSOLE].used = 1;
546
    connections[KERNEL_CONSOLE].used = 1;
510
   
547
   
511
    interbuffer = mmap(NULL,
548
    interbuffer = mmap(NULL,
512
        sizeof(keyfield_t) * fb_info.cols * fb_info.rows,
549
        sizeof(keyfield_t) * fb_info.cols * fb_info.rows,
513
        PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
550
        PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
514
    if (!interbuffer) {
551
    if (!interbuffer) {
515
        if (ipc_share_out_start(fb_info.phone, interbuffer,
552
        if (ipc_share_out_start(fb_info.phone, interbuffer,
516
            AS_AREA_READ) != EOK) {
553
            AS_AREA_READ) != EOK) {
517
            munmap(interbuffer,
554
            munmap(interbuffer,
518
                sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
555
                sizeof(keyfield_t) * fb_info.cols * fb_info.rows);
519
            interbuffer = NULL;
556
            interbuffer = NULL;
520
        }
557
        }
521
    }
558
    }
522
   
559
   
523
    curs_goto(0, 0);
560
    curs_goto(0, 0);
524
    curs_visibility(
561
    curs_visibility(
525
        connections[active_console].screenbuffer.is_cursor_visible);
562
        connections[active_console].screenbuffer.is_cursor_visible);
526
   
563
   
527
    /* Register at NS */
564
    /* Register at NS */
528
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
565
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
529
        return -1;
566
        return -1;
530
   
567
   
531
    /* Receive kernel notifications */
568
    /* Receive kernel notifications */
532
    if (sysinfo_value("kconsole.present")) {
569
    if (sysinfo_value("kconsole.present")) {
533
        int devno = sysinfo_value("kconsole.devno");
570
        int devno = sysinfo_value("kconsole.devno");
534
        int inr = sysinfo_value("kconsole.inr");
571
        int inr = sysinfo_value("kconsole.inr");
535
        if (ipc_register_irq(inr, devno, 0, NULL) != EOK)
572
        if (ipc_register_irq(inr, devno, 0, NULL) != EOK)
536
            printf(NAME ": Error registering kconsole notifications\n");
573
            printf(NAME ": Error registering kconsole notifications\n");
537
       
574
       
538
        async_set_interrupt_received(interrupt_received);
575
        async_set_interrupt_received(interrupt_received);
539
    }
576
    }
540
   
577
   
541
    // FIXME: avoid connectiong to itself, keep using klog
578
    // FIXME: avoid connectiong to itself, keep using klog
542
    // printf(NAME ": Accepting connections\n");
579
    // printf(NAME ": Accepting connections\n");
543
    async_manager();
580
    async_manager();
544
   
581
   
545
    return 0;
582
    return 0;
546
}
583
}
547
 
584
 
548
/** @}
585
/** @}
549
 */
586
 */
550
 
587