Subversion Repositories HelenOS

Rev

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

Rev 4419 Rev 4420
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 <kbd.h>
38
#include <kbd.h>
39
#include <kbd/keycode.h>
39
#include <kbd/keycode.h>
40
#include <ipc/fb.h>
40
#include <ipc/fb.h>
41
#include <ipc/services.h>
41
#include <ipc/services.h>
42
#include <errno.h>
42
#include <errno.h>
43
#include <key_buffer.h>
43
#include <key_buffer.h>
44
#include <ipc/console.h>
44
#include <ipc/console.h>
45
#include <unistd.h>
45
#include <unistd.h>
46
#include <async.h>
46
#include <async.h>
47
#include <libadt/fifo.h>
47
#include <libadt/fifo.h>
48
#include <screenbuffer.h>
48
#include <screenbuffer.h>
49
#include <sys/mman.h>
49
#include <sys/mman.h>
50
#include <stdio.h>
50
#include <stdio.h>
51
#include <string.h>
51
#include <string.h>
52
#include <sysinfo.h>
52
#include <sysinfo.h>
53
#include <event.h>
53
#include <event.h>
54
 
54
 
55
#include "console.h"
55
#include "console.h"
56
#include "gcons.h"
56
#include "gcons.h"
57
 
57
 
58
#define MAX_KEYREQUESTS_BUFFERED 32
58
#define MAX_KEYREQUESTS_BUFFERED 32
59
 
59
 
60
#define NAME "console"
60
#define NAME "console"
61
 
61
 
62
/** Index of currently used virtual console.
62
/** Index of currently used virtual console.
63
 */
63
 */
64
int active_console = 0;
64
int active_console = 0;
65
int prev_console = 0;
65
int prev_console = 0;
66
 
66
 
-
 
67
/** Phone to the keyboard driver. */
-
 
68
static int kbd_phone;
-
 
69
 
67
/** Information about framebuffer */
70
/** Information about framebuffer */
68
struct {
71
struct {
69
    int phone;      /**< Framebuffer phone */
72
    int phone;      /**< Framebuffer phone */
70
    ipcarg_t rows;      /**< Framebuffer rows */
73
    ipcarg_t rows;      /**< Framebuffer rows */
71
    ipcarg_t cols;      /**< Framebuffer columns */
74
    ipcarg_t cols;      /**< Framebuffer columns */
72
} fb_info;
75
} fb_info;
73
 
76
 
74
typedef struct {
77
typedef struct {
75
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
78
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
76
    /** Buffer for unsatisfied request for keys. */
79
    /** Buffer for unsatisfied request for keys. */
77
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
80
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
78
        MAX_KEYREQUESTS_BUFFERED); 
81
        MAX_KEYREQUESTS_BUFFERED); 
79
    int keyrequest_counter;     /**< Number of requests in buffer. */
82
    int keyrequest_counter;     /**< Number of requests in buffer. */
80
    int client_phone;       /**< Phone to connected client. */
83
    int client_phone;       /**< Phone to connected client. */
81
    int used;           /**< 1 if this virtual console is
84
    int used;           /**< 1 if this virtual console is
82
                     * connected to some client.*/
85
                     * connected to some client.*/
83
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
86
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
84
                     * contents and related settings. */
87
                     * contents and related settings. */
85
} connection_t;
88
} connection_t;
86
 
89
 
87
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
90
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
88
                         * consoles */
91
                         * consoles */
89
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
92
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
90
                         * with framebufer used for
93
                         * with framebufer used for
91
                         * faster virtual console
94
                         * faster virtual console
92
                         * switching */
95
                         * switching */
93
 
96
 
94
/** Information on row-span yet unsent to FB driver. */
97
/** Information on row-span yet unsent to FB driver. */
95
struct {
98
struct {
96
    int row;        /**< Row where the span lies. */
99
    int row;        /**< Row where the span lies. */
97
    int col;        /**< Leftmost column of the span. */
100
    int col;        /**< Leftmost column of the span. */
98
    int n;          /**< Width of the span. */
101
    int n;          /**< Width of the span. */
99
} fb_pending;
102
} fb_pending;
100
 
103
 
101
/** Size of cwrite_buf. */
104
/** Size of cwrite_buf. */
102
#define CWRITE_BUF_SIZE 256
105
#define CWRITE_BUF_SIZE 256
103
 
106
 
104
/** Buffer for receiving data via the CONSOLE_WRITE call from the client. */
107
/** Buffer for receiving data via the CONSOLE_WRITE call from the client. */
105
static char cwrite_buf[CWRITE_BUF_SIZE];
108
static char cwrite_buf[CWRITE_BUF_SIZE];
106
 
109
 
107
static void fb_putchar(wchar_t c, int row, int col);
110
static void fb_putchar(wchar_t c, int row, int col);
108
 
111
 
109
 
112
 
110
/** Find unused virtual console.
113
/** Find unused virtual console.
111
 *
114
 *
112
 */
115
 */
113
static int find_free_connection(void)
116
static int find_free_connection(void)
114
{
117
{
115
    int i;
118
    int i;
116
   
119
   
117
    for (i = 0; i < CONSOLE_COUNT; i++) {
120
    for (i = 0; i < CONSOLE_COUNT; i++) {
118
        if (!connections[i].used)
121
        if (!connections[i].used)
119
            return i;
122
            return i;
120
    }
123
    }
121
    return -1;
124
    return -1;
122
}
125
}
123
 
126
 
124
static void clrscr(void)
127
static void clrscr(void)
125
{
128
{
126
    async_msg_0(fb_info.phone, FB_CLEAR);
129
    async_msg_0(fb_info.phone, FB_CLEAR);
127
}
130
}
128
 
131
 
129
static void curs_visibility(bool visible)
132
static void curs_visibility(bool visible)
130
{
133
{
131
    async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
134
    async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
132
}
135
}
133
 
136
 
134
static void curs_hide_sync(void)
137
static void curs_hide_sync(void)
135
{
138
{
136
    ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
139
    ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
137
}
140
}
138
 
141
 
139
static void curs_goto(int row, int col)
142
static void curs_goto(int row, int col)
140
{
143
{
141
    async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
144
    async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
142
}
145
}
143
 
146
 
-
 
147
static void screen_yield(void)
-
 
148
{
-
 
149
    ipc_call_sync_0_0(fb_info.phone, FB_SCREEN_YIELD);
-
 
150
}
-
 
151
 
-
 
152
static void screen_reclaim(void)
-
 
153
{
-
 
154
    ipc_call_sync_0_0(fb_info.phone, FB_SCREEN_RECLAIM);
-
 
155
}
-
 
156
 
-
 
157
static void kbd_yield(void)
-
 
158
{
-
 
159
    ipc_call_sync_0_0(kbd_phone, KBD_YIELD);
-
 
160
}
-
 
161
 
-
 
162
static void kbd_reclaim(void)
-
 
163
{
-
 
164
    ipc_call_sync_0_0(kbd_phone, KBD_RECLAIM);
-
 
165
}
-
 
166
 
144
static void set_style(int style)
167
static void set_style(int style)
145
{
168
{
146
    async_msg_1(fb_info.phone, FB_SET_STYLE, style);
169
    async_msg_1(fb_info.phone, FB_SET_STYLE, style);
147
}
170
}
148
 
171
 
149
static void set_color(int fgcolor, int bgcolor, int flags)
172
static void set_color(int fgcolor, int bgcolor, int flags)
150
{
173
{
151
    async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
174
    async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
152
}
175
}
153
 
176
 
154
static void set_rgb_color(int fgcolor, int bgcolor)
177
static void set_rgb_color(int fgcolor, int bgcolor)
155
{
178
{
156
    async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor);
179
    async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor);
157
}
180
}
158
 
181
 
159
static void set_attrs(attrs_t *attrs)
182
static void set_attrs(attrs_t *attrs)
160
{
183
{
161
    switch (attrs->t) {
184
    switch (attrs->t) {
162
    case at_style:
185
    case at_style:
163
        set_style(attrs->a.s.style);
186
        set_style(attrs->a.s.style);
164
        break;
187
        break;
165
 
188
 
166
    case at_idx:
189
    case at_idx:
167
        set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
190
        set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
168
            attrs->a.i.flags);
191
            attrs->a.i.flags);
169
        break;
192
        break;
170
 
193
 
171
    case at_rgb:
194
    case at_rgb:
172
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
195
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
173
        break;
196
        break;
174
    }
197
    }
175
}
198
}
176
 
199
 
177
/** Send an area of screenbuffer to the FB driver. */
200
/** Send an area of screenbuffer to the FB driver. */
178
static void fb_update_area(connection_t *conn, int x, int y, int w, int h)
201
static void fb_update_area(connection_t *conn, int x, int y, int w, int h)
179
{
202
{
180
    int i, j;
203
    int i, j;
181
    int rc;
204
    int rc;
182
    attrs_t *attrs;
205
    attrs_t *attrs;
183
    keyfield_t *field;
206
    keyfield_t *field;
184
 
207
 
185
    if (interbuffer) {
208
    if (interbuffer) {
186
        for (j = 0; j < h; j++) {
209
        for (j = 0; j < h; j++) {
187
            for (i = 0; i < w; i++) {
210
            for (i = 0; i < w; i++) {
188
                interbuffer[i + j * w] =
211
                interbuffer[i + j * w] =
189
                    *get_field_at(&conn->screenbuffer,
212
                    *get_field_at(&conn->screenbuffer,
190
                    x + i, y + j);
213
                    x + i, y + j);
191
            }
214
            }
192
        }
215
        }
193
 
216
 
194
        rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
217
        rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
195
            x, y, w, h);
218
            x, y, w, h);
196
    } else {
219
    } else {
197
        rc = ENOTSUP;
220
        rc = ENOTSUP;
198
    }
221
    }
199
 
222
 
200
    if (rc != 0) {
223
    if (rc != 0) {
201
        /*
224
        /*
202
        attrs = &conn->screenbuffer.attrs;
225
        attrs = &conn->screenbuffer.attrs;
203
 
226
 
204
        for (j = 0; j < h; j++) {
227
        for (j = 0; j < h; j++) {
205
            for (i = 0; i < w; i++) {
228
            for (i = 0; i < w; i++) {
206
                field = get_field_at(&conn->screenbuffer,
229
                field = get_field_at(&conn->screenbuffer,
207
                    x + i, y + j);
230
                    x + i, y + j);
208
                if (!attrs_same(*attrs, field->attrs))
231
                if (!attrs_same(*attrs, field->attrs))
209
                    set_attrs(&field->attrs);
232
                    set_attrs(&field->attrs);
210
                attrs = &field->attrs;
233
                attrs = &field->attrs;
211
 
234
 
212
                fb_putchar(field->character, y + j, x + i);
235
                fb_putchar(field->character, y + j, x + i);
213
            }
236
            }
214
        }*/
237
        }*/
215
    }
238
    }
216
}
239
}
217
 
240
 
218
/** Flush pending cells to FB. */
241
/** Flush pending cells to FB. */
219
static void fb_pending_flush(void)
242
static void fb_pending_flush(void)
220
{
243
{
221
    screenbuffer_t *scr;
244
    screenbuffer_t *scr;
222
 
245
 
223
    scr = &(connections[active_console].screenbuffer);
246
    scr = &(connections[active_console].screenbuffer);
224
 
247
 
225
    if (fb_pending.n > 0) {
248
    if (fb_pending.n > 0) {
226
        fb_update_area(&connections[active_console], fb_pending.col,
249
        fb_update_area(&connections[active_console], fb_pending.col,
227
            fb_pending.row, fb_pending.n, 1);
250
            fb_pending.row, fb_pending.n, 1);
228
        fb_pending.n = 0;
251
        fb_pending.n = 0;
229
    }
252
    }
230
}
253
}
231
 
254
 
232
/** Mark a character cell as changed.
255
/** Mark a character cell as changed.
233
 *
256
 *
234
 * This adds the cell to the pending rowspan if possible. Otherwise
257
 * This adds the cell to the pending rowspan if possible. Otherwise
235
 * the old span is flushed first.
258
 * the old span is flushed first.
236
 */
259
 */
237
static void cell_mark_changed(int row, int col)
260
static void cell_mark_changed(int row, int col)
238
{
261
{
239
    if (fb_pending.n != 0) {
262
    if (fb_pending.n != 0) {
240
        if (row != fb_pending.row ||
263
        if (row != fb_pending.row ||
241
            col != fb_pending.col + fb_pending.n) {
264
            col != fb_pending.col + fb_pending.n) {
242
            fb_pending_flush();
265
            fb_pending_flush();
243
        }
266
        }
244
    }
267
    }
245
 
268
 
246
    if (fb_pending.n == 0) {
269
    if (fb_pending.n == 0) {
247
        fb_pending.row = row;
270
        fb_pending.row = row;
248
        fb_pending.col = col;
271
        fb_pending.col = col;
249
    }
272
    }
250
 
273
 
251
    ++fb_pending.n;
274
    ++fb_pending.n;
252
}
275
}
253
 
276
 
254
 
277
 
255
/** Print a character to the active VC with buffering. */
278
/** Print a character to the active VC with buffering. */
256
static void fb_putchar(wchar_t c, int row, int col)
279
static void fb_putchar(wchar_t c, int row, int col)
257
{
280
{
258
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
281
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
259
}
282
}
260
 
283
 
261
/** Process a character from the client (TTY emulation). */
284
/** Process a character from the client (TTY emulation). */
262
static void write_char(int console, wchar_t ch)
285
static void write_char(int console, wchar_t ch)
263
{
286
{
264
    bool flush_cursor = false;
287
    bool flush_cursor = false;
265
    screenbuffer_t *scr = &(connections[console].screenbuffer);
288
    screenbuffer_t *scr = &(connections[console].screenbuffer);
266
 
289
 
267
    switch (ch) {
290
    switch (ch) {
268
    case '\n':
291
    case '\n':
269
        fb_pending_flush();
292
        fb_pending_flush();
270
        flush_cursor = true;
293
        flush_cursor = true;
271
        scr->position_y++;
294
        scr->position_y++;
272
        scr->position_x = 0;
295
        scr->position_x = 0;
273
        break;
296
        break;
274
    case '\r':
297
    case '\r':
275
        break;
298
        break;
276
    case '\t':
299
    case '\t':
277
        scr->position_x += 8;
300
        scr->position_x += 8;
278
        scr->position_x -= scr->position_x % 8;
301
        scr->position_x -= scr->position_x % 8;
279
        break;
302
        break;
280
    case '\b':
303
    case '\b':
281
        if (scr->position_x == 0)
304
        if (scr->position_x == 0)
282
            break;
305
            break;
283
        scr->position_x--;
306
        scr->position_x--;
284
        if (console == active_console)
307
        if (console == active_console)
285
            cell_mark_changed(scr->position_y, scr->position_x);
308
            cell_mark_changed(scr->position_y, scr->position_x);
286
        screenbuffer_putchar(scr, ' ');
309
        screenbuffer_putchar(scr, ' ');
287
        break;
310
        break;
288
    default:   
311
    default:   
289
        if (console == active_console)
312
        if (console == active_console)
290
            cell_mark_changed(scr->position_y, scr->position_x);
313
            cell_mark_changed(scr->position_y, scr->position_x);
291
 
314
 
292
        screenbuffer_putchar(scr, ch);
315
        screenbuffer_putchar(scr, ch);
293
        scr->position_x++;
316
        scr->position_x++;
294
    }
317
    }
295
 
318
 
296
    if (scr->position_x >= scr->size_x) {
319
    if (scr->position_x >= scr->size_x) {
297
        flush_cursor = true;
320
        flush_cursor = true;
298
        scr->position_y++;
321
        scr->position_y++;
299
    }
322
    }
300
   
323
   
301
    if (scr->position_y >= scr->size_y) {
324
    if (scr->position_y >= scr->size_y) {
302
        fb_pending_flush();
325
        fb_pending_flush();
303
        scr->position_y = scr->size_y - 1;
326
        scr->position_y = scr->size_y - 1;
304
        screenbuffer_clear_line(scr, scr->top_line);
327
        screenbuffer_clear_line(scr, scr->top_line);
305
        scr->top_line = (scr->top_line + 1) % scr->size_y;
328
        scr->top_line = (scr->top_line + 1) % scr->size_y;
306
        if (console == active_console)
329
        if (console == active_console)
307
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
330
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
308
    }
331
    }
309
 
332
 
310
    scr->position_x = scr->position_x % scr->size_x;
333
    scr->position_x = scr->position_x % scr->size_x;
311
 
334
 
312
    if (console == active_console && flush_cursor)
335
    if (console == active_console && flush_cursor)
313
        curs_goto(scr->position_y, scr->position_x);
336
        curs_goto(scr->position_y, scr->position_x);
314
}
337
}
315
 
338
 
316
/** Switch to new console */
339
/** Switch to new console */
317
static void change_console(int newcons)
340
static void change_console(int newcons)
318
{
341
{
319
    connection_t *conn;
342
    connection_t *conn;
320
    int i, j, rc;
343
    int i, j, rc;
321
    keyfield_t *field;
344
    keyfield_t *field;
322
    attrs_t *attrs;
345
    attrs_t *attrs;
323
   
346
   
324
    if (newcons == active_console)
347
    if (newcons == active_console)
325
        return;
348
        return;
326
 
349
 
327
    fb_pending_flush();
350
    fb_pending_flush();
328
 
351
 
329
    if (newcons == KERNEL_CONSOLE) {
352
    if (newcons == KERNEL_CONSOLE) {
330
        async_serialize_start();
353
        async_serialize_start();
331
        curs_hide_sync();
354
        curs_hide_sync();
332
        gcons_in_kernel();
355
        gcons_in_kernel();
-
 
356
        screen_yield();
-
 
357
        kbd_yield();
333
        async_serialize_end();
358
        async_serialize_end();
-
 
359
 
334
       
360
       
335
        if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
361
        if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
336
            prev_console = active_console;
362
            prev_console = active_console;
337
            active_console = KERNEL_CONSOLE;
363
            active_console = KERNEL_CONSOLE;
338
        } else
364
        } else
339
            newcons = active_console;
365
            newcons = active_console;
340
    }
366
    }
341
   
367
   
342
    if (newcons != KERNEL_CONSOLE) {
368
    if (newcons != KERNEL_CONSOLE) {
343
        async_serialize_start();
369
        async_serialize_start();
344
       
370
       
345
        if (active_console == KERNEL_CONSOLE)
371
        if (active_console == KERNEL_CONSOLE) {
-
 
372
            screen_reclaim();
-
 
373
            kbd_reclaim();
346
            gcons_redraw_console();
374
            gcons_redraw_console();
-
 
375
        }
347
       
376
       
348
        active_console = newcons;
377
        active_console = newcons;
349
        gcons_change_console(newcons);
378
        gcons_change_console(newcons);
350
        conn = &connections[active_console];
379
        conn = &connections[active_console];
351
       
380
       
352
        set_attrs(&conn->screenbuffer.attrs);
381
        set_attrs(&conn->screenbuffer.attrs);
353
        curs_visibility(false);
382
        curs_visibility(false);
354
        if (interbuffer) {
383
        if (interbuffer) {
355
            for (j = 0; j < conn->screenbuffer.size_y; j++) {
384
            for (j = 0; j < conn->screenbuffer.size_y; j++) {
356
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
385
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
357
                    unsigned int size_x;
386
                    unsigned int size_x;
358
                   
387
                   
359
                    size_x = conn->screenbuffer.size_x;
388
                    size_x = conn->screenbuffer.size_x;
360
                    interbuffer[j * size_x + i] =
389
                    interbuffer[j * size_x + i] =
361
                        *get_field_at(&conn->screenbuffer, i, j);
390
                        *get_field_at(&conn->screenbuffer, i, j);
362
                }
391
                }
363
            }
392
            }
364
            /* This call can preempt, but we are already at the end */
393
            /* This call can preempt, but we are already at the end */
365
            rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
394
            rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
366
                0, 0, conn->screenbuffer.size_x,
395
                0, 0, conn->screenbuffer.size_x,
367
                conn->screenbuffer.size_y);
396
                conn->screenbuffer.size_y);
368
        }
397
        }
369
       
398
       
370
        if ((!interbuffer) || (rc != 0)) {
399
        if ((!interbuffer) || (rc != 0)) {
371
            set_attrs(&conn->screenbuffer.attrs);
400
            set_attrs(&conn->screenbuffer.attrs);
372
            clrscr();
401
            clrscr();
373
            attrs = &conn->screenbuffer.attrs;
402
            attrs = &conn->screenbuffer.attrs;
374
           
403
           
375
            for (j = 0; j < conn->screenbuffer.size_y; j++)
404
            for (j = 0; j < conn->screenbuffer.size_y; j++)
376
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
405
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
377
                    field = get_field_at(&conn->screenbuffer, i, j);
406
                    field = get_field_at(&conn->screenbuffer, i, j);
378
                    if (!attrs_same(*attrs, field->attrs))
407
                    if (!attrs_same(*attrs, field->attrs))
379
                        set_attrs(&field->attrs);
408
                        set_attrs(&field->attrs);
380
                    attrs = &field->attrs;
409
                    attrs = &field->attrs;
381
                    if ((field->character == ' ') &&
410
                    if ((field->character == ' ') &&
382
                        (attrs_same(field->attrs,
411
                        (attrs_same(field->attrs,
383
                        conn->screenbuffer.attrs)))
412
                        conn->screenbuffer.attrs)))
384
                        continue;
413
                        continue;
385
 
414
 
386
                    fb_putchar(field->character, j, i);
415
                    fb_putchar(field->character, j, i);
387
                }
416
                }
388
        }
417
        }
389
       
418
       
390
        curs_goto(conn->screenbuffer.position_y,
419
        curs_goto(conn->screenbuffer.position_y,
391
            conn->screenbuffer.position_x);
420
            conn->screenbuffer.position_x);
392
        curs_visibility(conn->screenbuffer.is_cursor_visible);
421
        curs_visibility(conn->screenbuffer.is_cursor_visible);
393
       
422
       
394
        async_serialize_end();
423
        async_serialize_end();
395
    }
424
    }
396
}
425
}
397
 
426
 
398
/** Handler for keyboard */
427
/** Handler for keyboard */
399
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
428
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
400
{
429
{
401
    ipc_callid_t callid;
430
    ipc_callid_t callid;
402
    ipc_call_t call;
431
    ipc_call_t call;
403
    int retval;
432
    int retval;
404
    kbd_event_t ev;
433
    kbd_event_t ev;
405
    connection_t *conn;
434
    connection_t *conn;
406
    int newcon;
435
    int newcon;
407
   
436
   
408
    /* Ignore parameters, the connection is alread opened */
437
    /* Ignore parameters, the connection is alread opened */
409
    while (1) {
438
    while (1) {
410
        callid = async_get_call(&call);
439
        callid = async_get_call(&call);
411
        switch (IPC_GET_METHOD(call)) {
440
        switch (IPC_GET_METHOD(call)) {
412
        case IPC_M_PHONE_HUNGUP:
441
        case IPC_M_PHONE_HUNGUP:
413
            /* TODO: Handle hangup */
442
            /* TODO: Handle hangup */
414
            return;
443
            return;
415
        case KBD_MS_LEFT:
444
        case KBD_MS_LEFT:
416
            newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
445
            newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
417
            if (newcon != -1)
446
            if (newcon != -1)
418
                change_console(newcon);
447
                change_console(newcon);
419
            retval = 0;
448
            retval = 0;
420
            break;
449
            break;
421
        case KBD_MS_MOVE:
450
        case KBD_MS_MOVE:
422
            gcons_mouse_move(IPC_GET_ARG1(call),
451
            gcons_mouse_move(IPC_GET_ARG1(call),
423
                IPC_GET_ARG2(call));
452
                IPC_GET_ARG2(call));
424
            retval = 0;
453
            retval = 0;
425
            break;
454
            break;
426
        case KBD_EVENT:
455
        case KBD_EVENT:
427
            /* Got event from keyboard driver. */
456
            /* Got event from keyboard driver. */
428
            retval = 0;
457
            retval = 0;
429
            ev.type = IPC_GET_ARG1(call);
458
            ev.type = IPC_GET_ARG1(call);
430
            ev.key = IPC_GET_ARG2(call);
459
            ev.key = IPC_GET_ARG2(call);
431
            ev.mods = IPC_GET_ARG3(call);
460
            ev.mods = IPC_GET_ARG3(call);
432
            ev.c = IPC_GET_ARG4(call);
461
            ev.c = IPC_GET_ARG4(call);
433
           
462
           
434
            /* switch to another virtual console */
463
            /* switch to another virtual console */
435
           
464
           
436
            conn = &connections[active_console];
465
            conn = &connections[active_console];
437
 
466
 
438
            if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
467
            if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
439
                CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
468
                CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
440
                if (ev.key == KC_F12)
469
                if (ev.key == KC_F12)
441
                    change_console(KERNEL_CONSOLE);
470
                    change_console(KERNEL_CONSOLE);
442
                else
471
                else
443
                    change_console(ev.key - KC_F1);
472
                    change_console(ev.key - KC_F1);
444
                break;
473
                break;
445
            }
474
            }
446
           
475
           
447
            /* if client is awaiting key, send it */
476
            /* if client is awaiting key, send it */
448
            if (conn->keyrequest_counter > 0) {    
477
            if (conn->keyrequest_counter > 0) {    
449
                conn->keyrequest_counter--;
478
                conn->keyrequest_counter--;
450
                ipc_answer_4(fifo_pop(conn->keyrequests), EOK,
479
                ipc_answer_4(fifo_pop(conn->keyrequests), EOK,
451
                    ev.type, ev.key, ev.mods, ev.c);
480
                    ev.type, ev.key, ev.mods, ev.c);
452
                break;
481
                break;
453
            }
482
            }
454
 
483
 
455
            keybuffer_push(&conn->keybuffer, &ev);
484
            keybuffer_push(&conn->keybuffer, &ev);
456
            retval = 0;
485
            retval = 0;
457
 
486
 
458
            break;
487
            break;
459
        default:
488
        default:
460
            retval = ENOENT;
489
            retval = ENOENT;
461
        }
490
        }
462
        ipc_answer_0(callid, retval);
491
        ipc_answer_0(callid, retval);
463
    }
492
    }
464
}
493
}
465
 
494
 
466
/** Handle CONSOLE_WRITE call. */
495
/** Handle CONSOLE_WRITE call. */
467
static void cons_write(int consnum, ipc_callid_t rid, ipc_call_t *request)
496
static void cons_write(int consnum, ipc_callid_t rid, ipc_call_t *request)
468
{
497
{
469
    ipc_callid_t callid;
498
    ipc_callid_t callid;
470
    size_t size;
499
    size_t size;
471
    wchar_t ch;
500
    wchar_t ch;
472
    size_t off;
501
    size_t off;
473
 
502
 
474
    if (!ipc_data_write_receive(&callid, &size)) {
503
    if (!ipc_data_write_receive(&callid, &size)) {
475
        ipc_answer_0(callid, EINVAL);
504
        ipc_answer_0(callid, EINVAL);
476
        ipc_answer_0(rid, EINVAL);
505
        ipc_answer_0(rid, EINVAL);
-
 
506
        return;
477
    }
507
    }
478
 
508
 
479
    if (size > CWRITE_BUF_SIZE)
509
    if (size > CWRITE_BUF_SIZE)
480
        size = CWRITE_BUF_SIZE;
510
        size = CWRITE_BUF_SIZE;
481
 
511
 
482
    (void) ipc_data_write_finalize(callid, cwrite_buf, size);
512
    (void) ipc_data_write_finalize(callid, cwrite_buf, size);
483
 
513
 
-
 
514
    async_serialize_start();
-
 
515
 
484
    off = 0;
516
    off = 0;
485
    while (off < size) {
517
    while (off < size) {
486
        ch = str_decode(cwrite_buf, &off, size);
518
        ch = str_decode(cwrite_buf, &off, size);
487
        write_char(consnum, ch);
519
        write_char(consnum, ch);
488
    }
520
    }
489
 
521
 
-
 
522
    async_serialize_end();
-
 
523
 
490
    gcons_notify_char(consnum);
524
    gcons_notify_char(consnum);
491
    ipc_answer_1(rid, EOK, size);
525
    ipc_answer_1(rid, EOK, size);
492
}
526
}
493
 
527
 
494
/** Default thread for new connections */
528
/** Default thread for new connections */
495
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
529
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
496
{
530
{
497
    ipc_callid_t callid;
531
    ipc_callid_t callid;
498
    ipc_call_t call;
532
    ipc_call_t call;
499
    int consnum;
533
    int consnum;
500
    ipcarg_t arg1, arg2, arg3, arg4;
534
    ipcarg_t arg1, arg2, arg3, arg4;
501
    connection_t *conn;
535
    connection_t *conn;
502
    screenbuffer_t *scr;
536
    screenbuffer_t *scr;
503
   
537
   
504
    if ((consnum = find_free_connection()) == -1) {
538
    if ((consnum = find_free_connection()) == -1) {
505
        ipc_answer_0(iid, ELIMIT);
539
        ipc_answer_0(iid, ELIMIT);
506
        return;
540
        return;
507
    }
541
    }
508
    conn = &connections[consnum];
542
    conn = &connections[consnum];
509
    conn->used = 1;
543
    conn->used = 1;
510
   
544
   
511
    async_serialize_start();
545
    async_serialize_start();
512
    gcons_notify_connect(consnum);
546
    gcons_notify_connect(consnum);
513
    conn->client_phone = IPC_GET_ARG5(*icall);
547
    conn->client_phone = IPC_GET_ARG5(*icall);
514
    screenbuffer_clear(&conn->screenbuffer);
548
    screenbuffer_clear(&conn->screenbuffer);
-
 
549
    if (consnum == active_console)
-
 
550
        clrscr();
515
   
551
   
516
    /* Accept the connection */
552
    /* Accept the connection */
517
    ipc_answer_0(iid, EOK);
553
    ipc_answer_0(iid, EOK);
518
   
554
   
519
    while (1) {
555
    while (1) {
520
        async_serialize_end();
556
        async_serialize_end();
521
        callid = async_get_call(&call);
557
        callid = async_get_call(&call);
522
        async_serialize_start();
558
        async_serialize_start();
523
       
559
       
524
        arg1 = 0;
560
        arg1 = 0;
525
        arg2 = 0;
561
        arg2 = 0;
526
        arg3 = 0;
562
        arg3 = 0;
527
        arg4 = 0;
563
        arg4 = 0;
528
 
564
 
529
        switch (IPC_GET_METHOD(call)) {
565
        switch (IPC_GET_METHOD(call)) {
530
        case IPC_M_PHONE_HUNGUP:
566
        case IPC_M_PHONE_HUNGUP:
531
            gcons_notify_disconnect(consnum);
567
            gcons_notify_disconnect(consnum);
532
           
568
           
533
            /* Answer all pending requests */
569
            /* Answer all pending requests */
534
            while (conn->keyrequest_counter > 0) {
570
            while (conn->keyrequest_counter > 0) {
535
                conn->keyrequest_counter--;
571
                conn->keyrequest_counter--;
536
                ipc_answer_0(fifo_pop(conn->keyrequests),
572
                ipc_answer_0(fifo_pop(conn->keyrequests),
537
                    ENOENT);
573
                    ENOENT);
538
                break;
574
                break;
539
            }
575
            }
540
            conn->used = 0;
576
            conn->used = 0;
541
            return;
577
            return;
542
        case CONSOLE_PUTCHAR:
578
        case CONSOLE_PUTCHAR:
543
            write_char(consnum, IPC_GET_ARG1(call));
579
            write_char(consnum, IPC_GET_ARG1(call));
544
            gcons_notify_char(consnum);
580
            gcons_notify_char(consnum);
545
            break;
581
            break;
546
        case CONSOLE_WRITE:
582
        case CONSOLE_WRITE:
-
 
583
            async_serialize_end();
547
            cons_write(consnum, callid, &call);
584
            cons_write(consnum, callid, &call);
-
 
585
            async_serialize_start();
548
            continue;
586
            continue;
549
        case CONSOLE_CLEAR:
587
        case CONSOLE_CLEAR:
550
            /* Send message to fb */
588
            /* Send message to fb */
551
            if (consnum == active_console) {
589
            if (consnum == active_console) {
552
                async_msg_0(fb_info.phone, FB_CLEAR);
590
                async_msg_0(fb_info.phone, FB_CLEAR);
553
            }
591
            }
554
           
592
           
555
            screenbuffer_clear(&conn->screenbuffer);
593
            screenbuffer_clear(&conn->screenbuffer);
556
           
594
           
557
            break;
595
            break;
558
        case CONSOLE_GOTO:
596
        case CONSOLE_GOTO:
559
            screenbuffer_goto(&conn->screenbuffer,
597
            screenbuffer_goto(&conn->screenbuffer,
560
                IPC_GET_ARG2(call), IPC_GET_ARG1(call));
598
                IPC_GET_ARG2(call), IPC_GET_ARG1(call));
561
            if (consnum == active_console)
599
            if (consnum == active_console)
562
                curs_goto(IPC_GET_ARG1(call),
600
                curs_goto(IPC_GET_ARG1(call),
563
                    IPC_GET_ARG2(call));
601
                    IPC_GET_ARG2(call));
564
            break;
602
            break;
565
        case CONSOLE_GETSIZE:
603
        case CONSOLE_GETSIZE:
566
            arg1 = fb_info.rows;
604
            arg1 = fb_info.rows;
567
            arg2 = fb_info.cols;
605
            arg2 = fb_info.cols;
568
            break;
606
            break;
569
        case CONSOLE_FLUSH:
607
        case CONSOLE_FLUSH:
570
            fb_pending_flush();
608
            fb_pending_flush();
571
            if (consnum == active_console) {
609
            if (consnum == active_console) {
572
                async_req_0_0(fb_info.phone, FB_FLUSH);
610
                async_req_0_0(fb_info.phone, FB_FLUSH);
573
 
611
 
574
                scr = &(connections[consnum].screenbuffer);
612
                scr = &(connections[consnum].screenbuffer);
575
                curs_goto(scr->position_y, scr->position_x);
613
                curs_goto(scr->position_y, scr->position_x);
576
            }
614
            }
577
            break;
615
            break;
578
        case CONSOLE_SET_STYLE:
616
        case CONSOLE_SET_STYLE:
579
            fb_pending_flush();
617
            fb_pending_flush();
580
            arg1 = IPC_GET_ARG1(call);
618
            arg1 = IPC_GET_ARG1(call);
581
            screenbuffer_set_style(&conn->screenbuffer, arg1);
619
            screenbuffer_set_style(&conn->screenbuffer, arg1);
582
            if (consnum == active_console)
620
            if (consnum == active_console)
583
                set_style(arg1);
621
                set_style(arg1);
584
            break;
622
            break;
585
        case CONSOLE_SET_COLOR:
623
        case CONSOLE_SET_COLOR:
586
            fb_pending_flush();
624
            fb_pending_flush();
587
            arg1 = IPC_GET_ARG1(call);
625
            arg1 = IPC_GET_ARG1(call);
588
            arg2 = IPC_GET_ARG2(call);
626
            arg2 = IPC_GET_ARG2(call);
589
            arg3 = IPC_GET_ARG3(call);
627
            arg3 = IPC_GET_ARG3(call);
590
            screenbuffer_set_color(&conn->screenbuffer, arg1,
628
            screenbuffer_set_color(&conn->screenbuffer, arg1,
591
                arg2, arg3);
629
                arg2, arg3);
592
            if (consnum == active_console)
630
            if (consnum == active_console)
593
                set_color(arg1, arg2, arg3);
631
                set_color(arg1, arg2, arg3);
594
            break;
632
            break;
595
        case CONSOLE_SET_RGB_COLOR:
633
        case CONSOLE_SET_RGB_COLOR:
596
            fb_pending_flush();
634
            fb_pending_flush();
597
            arg1 = IPC_GET_ARG1(call);
635
            arg1 = IPC_GET_ARG1(call);
598
            arg2 = IPC_GET_ARG2(call);
636
            arg2 = IPC_GET_ARG2(call);
599
            screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
637
            screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
600
                arg2);
638
                arg2);
601
            if (consnum == active_console)
639
            if (consnum == active_console)
602
                set_rgb_color(arg1, arg2);
640
                set_rgb_color(arg1, arg2);
603
            break;
641
            break;
604
        case CONSOLE_CURSOR_VISIBILITY:
642
        case CONSOLE_CURSOR_VISIBILITY:
605
            fb_pending_flush();
643
            fb_pending_flush();
606
            arg1 = IPC_GET_ARG1(call);
644
            arg1 = IPC_GET_ARG1(call);
607
            conn->screenbuffer.is_cursor_visible = arg1;
645
            conn->screenbuffer.is_cursor_visible = arg1;
608
            if (consnum == active_console)
646
            if (consnum == active_console)
609
                curs_visibility(arg1);
647
                curs_visibility(arg1);
610
            break;
648
            break;
611
        case CONSOLE_GETKEY:
649
        case CONSOLE_GETKEY:
612
            if (keybuffer_empty(&conn->keybuffer)) {
650
            if (keybuffer_empty(&conn->keybuffer)) {
613
                /* buffer is empty -> store request */
651
                /* buffer is empty -> store request */
614
                if (conn->keyrequest_counter <
652
                if (conn->keyrequest_counter <
615
                    MAX_KEYREQUESTS_BUFFERED) {
653
                    MAX_KEYREQUESTS_BUFFERED) {
616
                    fifo_push(conn->keyrequests, callid);
654
                    fifo_push(conn->keyrequests, callid);
617
                    conn->keyrequest_counter++;
655
                    conn->keyrequest_counter++;
618
                } else {
656
                } else {
619
                    /*
657
                    /*
620
                     * No key available and too many
658
                     * No key available and too many
621
                     * requests => fail.
659
                     * requests => fail.
622
                    */
660
                    */
623
                    ipc_answer_0(callid, ELIMIT);
661
                    ipc_answer_0(callid, ELIMIT);
624
                }
662
                }
625
                continue;
663
                continue;
626
            }
664
            }
627
            kbd_event_t ev;
665
            kbd_event_t ev;
628
            keybuffer_pop(&conn->keybuffer, &ev);
666
            keybuffer_pop(&conn->keybuffer, &ev);
629
            arg1 = ev.type;
667
            arg1 = ev.type;
630
            arg2 = ev.key;
668
            arg2 = ev.key;
631
            arg3 = ev.mods;
669
            arg3 = ev.mods;
632
            arg4 = ev.c;
670
            arg4 = ev.c;
633
            break;
671
            break;
634
        case CONSOLE_KCON_ENABLE:
672
        case CONSOLE_KCON_ENABLE:
635
            change_console(KERNEL_CONSOLE);
673
            change_console(KERNEL_CONSOLE);
636
            break;
674
            break;
637
        }
675
        }
638
        ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
676
        ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
639
    }
677
    }
640
}
678
}
641
 
679
 
642
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
680
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
643
{
681
{
644
    change_console(prev_console);
682
    change_console(prev_console);
645
}
683
}
646
 
684
 
647
int main(int argc, char *argv[])
685
int main(int argc, char *argv[])
648
{
686
{
649
    printf(NAME ": HelenOS Console service\n");
687
    printf(NAME ": HelenOS Console service\n");
650
   
688
   
651
    ipcarg_t phonehash;
689
    ipcarg_t phonehash;
652
    int kbd_phone;
-
 
653
    size_t ib_size;
690
    size_t ib_size;
654
    int i;
691
    int i;
655
   
692
   
656
    async_set_client_connection(client_connection);
693
    async_set_client_connection(client_connection);
657
   
694
   
658
    /* Connect to keyboard driver */
695
    /* Connect to keyboard driver */
659
    kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
696
    kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
660
    if (kbd_phone < 0) {
697
    if (kbd_phone < 0) {
661
        printf(NAME ": Failed to connect to keyboard service\n");
698
        printf(NAME ": Failed to connect to keyboard service\n");
662
        return -1;
699
        return -1;
663
    }
700
    }
664
   
701
   
665
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
702
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
666
        printf(NAME ": Failed to create callback from keyboard service\n");
703
        printf(NAME ": Failed to create callback from keyboard service\n");
667
        return -1;
704
        return -1;
668
    }
705
    }
669
   
706
   
670
    async_new_connection(phonehash, 0, NULL, keyboard_events);
707
    async_new_connection(phonehash, 0, NULL, keyboard_events);
671
   
708
   
672
    /* Connect to framebuffer driver */
709
    /* Connect to framebuffer driver */
673
    fb_info.phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VIDEO, 0, 0);
710
    fb_info.phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VIDEO, 0, 0);
674
    if (fb_info.phone < 0) {
711
    if (fb_info.phone < 0) {
675
        printf(NAME ": Failed to connect to video service\n");
712
        printf(NAME ": Failed to connect to video service\n");
676
        return -1;
713
        return -1;
677
    }
714
    }
678
   
715
   
679
    /* Disable kernel output to the console */
716
    /* Disable kernel output to the console */
680
    __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
717
    __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
681
   
718
   
682
    /* Initialize gcons */
719
    /* Initialize gcons */
683
    gcons_init(fb_info.phone);
720
    gcons_init(fb_info.phone);
684
    /* Synchronize, the gcons can have something in queue */
721
    /* Synchronize, the gcons can have something in queue */
685
    async_req_0_0(fb_info.phone, FB_FLUSH);
722
    async_req_0_0(fb_info.phone, FB_FLUSH);
686
   
723
   
687
    async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
724
    async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
688
        &fb_info.cols);
725
        &fb_info.cols);
689
    set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
726
    set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
690
    clrscr();
727
    clrscr();
691
   
728
   
692
    /* Init virtual consoles */
729
    /* Init virtual consoles */
693
    for (i = 0; i < CONSOLE_COUNT; i++) {
730
    for (i = 0; i < CONSOLE_COUNT; i++) {
694
        connections[i].used = 0;
731
        connections[i].used = 0;
695
        keybuffer_init(&connections[i].keybuffer);
732
        keybuffer_init(&connections[i].keybuffer);
696
       
733
       
697
        connections[i].keyrequests.head = 0;
734
        connections[i].keyrequests.head = 0;
698
        connections[i].keyrequests.tail = 0;
735
        connections[i].keyrequests.tail = 0;
699
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
736
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
700
        connections[i].keyrequest_counter = 0;
737
        connections[i].keyrequest_counter = 0;
701
       
738
       
702
        if (screenbuffer_init(&connections[i].screenbuffer,
739
        if (screenbuffer_init(&connections[i].screenbuffer,
703
            fb_info.cols, fb_info.rows) == NULL) {
740
            fb_info.cols, fb_info.rows) == NULL) {
704
            /* FIXME: handle error */
741
            /* FIXME: handle error */
705
            return -1;
742
            return -1;
706
        }
743
        }
707
    }
744
    }
708
    connections[KERNEL_CONSOLE].used = 1;
745
    connections[KERNEL_CONSOLE].used = 1;
709
 
746
 
710
    /* Set up shared memory buffer. */
747
    /* Set up shared memory buffer. */
711
    ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
748
    ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
712
    interbuffer = as_get_mappable_page(ib_size);
749
    interbuffer = as_get_mappable_page(ib_size);
713
 
750
 
714
    fb_pending.n = 0;
751
    fb_pending.n = 0;
715
 
752
 
716
    if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
753
    if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
717
        AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
754
        AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
718
        interbuffer = NULL;
755
        interbuffer = NULL;
719
    }
756
    }
720
 
757
 
721
    if (interbuffer) {
758
    if (interbuffer) {
722
        if (ipc_share_out_start(fb_info.phone, interbuffer,
759
        if (ipc_share_out_start(fb_info.phone, interbuffer,
723
            AS_AREA_READ) != EOK) {
760
            AS_AREA_READ) != EOK) {
724
            as_area_destroy(interbuffer);
761
            as_area_destroy(interbuffer);
725
            interbuffer = NULL;
762
            interbuffer = NULL;
726
        }
763
        }
727
    }
764
    }
728
   
765
   
729
    curs_goto(0, 0);
766
    curs_goto(0, 0);
730
    curs_visibility(
767
    curs_visibility(
731
        connections[active_console].screenbuffer.is_cursor_visible);
768
        connections[active_console].screenbuffer.is_cursor_visible);
732
   
769
   
733
    /* Register at NS */
770
    /* Register at NS */
734
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
771
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
735
        return -1;
772
        return -1;
736
   
773
   
737
    /* Receive kernel notifications */
774
    /* Receive kernel notifications */
738
    if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
775
    if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
739
        printf(NAME ": Error registering kconsole notifications\n");
776
        printf(NAME ": Error registering kconsole notifications\n");
740
       
777
       
741
    async_set_interrupt_received(interrupt_received);
778
    async_set_interrupt_received(interrupt_received);
742
   
779
   
743
    // FIXME: avoid connectiong to itself, keep using klog
780
    // FIXME: avoid connectiong to itself, keep using klog
744
    // printf(NAME ": Accepting connections\n");
781
    // printf(NAME ": Accepting connections\n");
745
    async_manager();
782
    async_manager();
746
   
783
   
747
    return 0;
784
    return 0;
748
}
785
}
749
 
786
 
750
/** @}
787
/** @}
751
 */
788
 */
752
 
789