Subversion Repositories HelenOS

Rev

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

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