Subversion Repositories HelenOS

Rev

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

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