Subversion Repositories HelenOS

Rev

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

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