Subversion Repositories HelenOS

Rev

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

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