Subversion Repositories HelenOS

Rev

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

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