Subversion Repositories HelenOS

Rev

Rev 4439 | Rev 4668 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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