Subversion Repositories HelenOS

Rev

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

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