Subversion Repositories HelenOS

Rev

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

Rev 4153 Rev 4263
Line 46... Line 46...
46
#include <async.h>
46
#include <async.h>
47
#include <libadt/fifo.h>
47
#include <libadt/fifo.h>
48
#include <screenbuffer.h>
48
#include <screenbuffer.h>
49
#include <sys/mman.h>
49
#include <sys/mman.h>
50
#include <stdio.h>
50
#include <stdio.h>
-
 
51
#include <string.h>
51
#include <sysinfo.h>
52
#include <sysinfo.h>
-
 
53
#include <event.h>
52
 
54
 
53
#include "console.h"
55
#include "console.h"
54
#include "gcons.h"
56
#include "gcons.h"
55
 
57
 
56
#define MAX_KEYREQUESTS_BUFFERED 32
58
#define MAX_KEYREQUESTS_BUFFERED 32
Line 60... Line 62...
60
/** Index of currently used virtual console.
62
/** Index of currently used virtual console.
61
 */
63
 */
62
int active_console = 0;
64
int active_console = 0;
63
int prev_console = 0;
65
int prev_console = 0;
64
 
66
 
65
/** Information about framebuffer
67
/** Information about framebuffer */
66
 */
-
 
67
struct {
68
struct {
68
    int phone;      /**< Framebuffer phone */
69
    int phone;      /**< Framebuffer phone */
69
    ipcarg_t rows;      /**< Framebuffer rows */
70
    ipcarg_t rows;      /**< Framebuffer rows */
70
    ipcarg_t cols;      /**< Framebuffer columns */
71
    ipcarg_t cols;      /**< Framebuffer columns */
71
} fb_info;
72
} fb_info;
Line 88... Line 89...
88
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
89
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
89
                         * with framebufer used for
90
                         * with framebufer used for
90
                         * faster virtual console
91
                         * faster virtual console
91
                         * switching */
92
                         * switching */
92
 
93
 
-
 
94
/** Information on row-span yet unsent to FB driver. */
-
 
95
struct {
-
 
96
    int row;        /**< Row where the span lies. */
-
 
97
    int col;        /**< Leftmost column of the span. */
-
 
98
    int n;          /**< Width of the span. */
-
 
99
} fb_pending;
-
 
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];
-
 
106
 
-
 
107
static void fb_putchar(wchar_t c, int row, int col);
-
 
108
 
93
 
109
 
94
/** Find unused virtual console.
110
/** Find unused virtual console.
95
 *
111
 *
96
 */
112
 */
97
static int find_free_connection(void)
113
static int find_free_connection(void)
Line 156... Line 172...
156
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
172
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
157
        break;
173
        break;
158
    }
174
    }
159
}
175
}
160
 
176
 
-
 
177
/** Send an area of screenbuffer to the FB driver. */
161
static void prtchr(char c, int row, int col)
178
static void fb_update_area(connection_t *conn, int x, int y, int w, int h)
162
{
179
{
-
 
180
    int i, j;
-
 
181
    int rc;
-
 
182
    attrs_t *attrs;
-
 
183
    keyfield_t *field;
-
 
184
 
-
 
185
    if (interbuffer) {
-
 
186
        for (j = 0; j < h; j++) {
-
 
187
            for (i = 0; i < w; i++) {
-
 
188
                interbuffer[i + j * w] =
-
 
189
                    *get_field_at(&conn->screenbuffer,
-
 
190
                    x + i, y + j);
-
 
191
            }
-
 
192
        }
-
 
193
 
163
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
194
        rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
-
 
195
            x, y, w, h);
-
 
196
    } else {
-
 
197
        rc = ENOTSUP;
-
 
198
    }
-
 
199
 
-
 
200
    if (rc != 0) {
-
 
201
        /*
-
 
202
        attrs = &conn->screenbuffer.attrs;
-
 
203
 
-
 
204
        for (j = 0; j < h; j++) {
-
 
205
            for (i = 0; i < w; i++) {
-
 
206
                field = get_field_at(&conn->screenbuffer,
-
 
207
                    x + i, y + j);
-
 
208
                if (!attrs_same(*attrs, field->attrs))
-
 
209
                    set_attrs(&field->attrs);
-
 
210
                attrs = &field->attrs;
-
 
211
 
-
 
212
                fb_putchar(field->character, y + j, x + i);
-
 
213
            }
-
 
214
        }*/
-
 
215
    }
164
}
216
}
165
 
217
 
166
/** Check key and process special keys.
218
/** Flush pending cells to FB. */
-
 
219
static void fb_pending_flush(void)
-
 
220
{
-
 
221
    screenbuffer_t *scr;
-
 
222
 
-
 
223
    scr = &(connections[active_console].screenbuffer);
-
 
224
 
-
 
225
    if (fb_pending.n > 0) {
-
 
226
        fb_update_area(&connections[active_console], fb_pending.col,
-
 
227
            fb_pending.row, fb_pending.n, 1);
-
 
228
        fb_pending.n = 0;
167
 *
229
    }
-
 
230
}
-
 
231
 
-
 
232
/** Mark a character cell as changed.
168
 *
233
 *
-
 
234
 * This adds the cell to the pending rowspan if possible. Otherwise
-
 
235
 * the old span is flushed first.
169
 */
236
 */
-
 
237
static void cell_mark_changed(int row, int col)
-
 
238
{
-
 
239
    if (fb_pending.n != 0) {
-
 
240
        if (row != fb_pending.row ||
-
 
241
            col != fb_pending.col + fb_pending.n) {
-
 
242
            fb_pending_flush();
-
 
243
        }
-
 
244
    }
-
 
245
 
-
 
246
    if (fb_pending.n == 0) {
-
 
247
        fb_pending.row = row;
-
 
248
        fb_pending.col = col;
-
 
249
    }
-
 
250
 
-
 
251
    ++fb_pending.n;
-
 
252
}
-
 
253
 
-
 
254
 
-
 
255
/** Print a character to the active VC with buffering. */
170
static void write_char(int console, char key)
256
static void fb_putchar(wchar_t c, int row, int col)
171
{
257
{
-
 
258
    async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col);
-
 
259
}
-
 
260
 
-
 
261
/** Process a character from the client (TTY emulation). */
-
 
262
static void write_char(int console, wchar_t ch)
-
 
263
{
-
 
264
    bool flush_cursor = false;
172
    screenbuffer_t *scr = &(connections[console].screenbuffer);
265
    screenbuffer_t *scr = &(connections[console].screenbuffer);
173
   
266
 
174
    switch (key) {
267
    switch (ch) {
175
    case '\n':
268
    case '\n':
-
 
269
        fb_pending_flush();
-
 
270
        flush_cursor = true;
176
        scr->position_y++;
271
        scr->position_y++;
177
        scr->position_x = 0;
272
        scr->position_x = 0;
178
        break;
273
        break;
179
    case '\r':
274
    case '\r':
180
        break;
275
        break;
Line 185... Line 280...
185
    case '\b':
280
    case '\b':
186
        if (scr->position_x == 0)
281
        if (scr->position_x == 0)
187
            break;
282
            break;
188
        scr->position_x--;
283
        scr->position_x--;
189
        if (console == active_console)
284
        if (console == active_console)
190
            prtchr(' ', scr->position_y, scr->position_x);
285
            cell_mark_changed(scr->position_y, scr->position_x);
191
        screenbuffer_putchar(scr, ' ');
286
        screenbuffer_putchar(scr, ' ');
192
        break;
287
        break;
193
    default:   
288
    default:   
194
        if (console == active_console)
289
        if (console == active_console)
195
            prtchr(key, scr->position_y, scr->position_x);
290
            cell_mark_changed(scr->position_y, scr->position_x);
196
 
291
 
197
        screenbuffer_putchar(scr, key);
292
        screenbuffer_putchar(scr, ch);
198
        scr->position_x++;
293
        scr->position_x++;
199
    }
294
    }
200
   
295
 
201
    scr->position_y += (scr->position_x >= scr->size_x);
296
    if (scr->position_x >= scr->size_x) {
-
 
297
        flush_cursor = true;
-
 
298
        scr->position_y++;
-
 
299
    }
202
   
300
   
203
    if (scr->position_y >= scr->size_y) {
301
    if (scr->position_y >= scr->size_y) {
-
 
302
        fb_pending_flush();
204
        scr->position_y = scr->size_y - 1;
303
        scr->position_y = scr->size_y - 1;
205
        screenbuffer_clear_line(scr, scr->top_line);
304
        screenbuffer_clear_line(scr, scr->top_line);
206
        scr->top_line = (scr->top_line + 1) % scr->size_y;
305
        scr->top_line = (scr->top_line + 1) % scr->size_y;
207
        if (console == active_console)
306
        if (console == active_console)
208
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
307
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
209
    }
308
    }
210
   
309
 
211
    scr->position_x = scr->position_x % scr->size_x;
310
    scr->position_x = scr->position_x % scr->size_x;
212
   
311
 
213
    if (console == active_console)
312
    if (console == active_console && flush_cursor)
214
        curs_goto(scr->position_y, scr->position_x);
313
        curs_goto(scr->position_y, scr->position_x);
215
   
-
 
216
}
314
}
217
 
315
 
218
/** Switch to new console */
316
/** Switch to new console */
219
static void change_console(int newcons)
317
static void change_console(int newcons)
220
{
318
{
Line 223... Line 321...
223
    keyfield_t *field;
321
    keyfield_t *field;
224
    attrs_t *attrs;
322
    attrs_t *attrs;
225
   
323
   
226
    if (newcons == active_console)
324
    if (newcons == active_console)
227
        return;
325
        return;
-
 
326
 
-
 
327
    fb_pending_flush();
228
   
328
 
229
    if (newcons == KERNEL_CONSOLE) {
329
    if (newcons == KERNEL_CONSOLE) {
230
        async_serialize_start();
330
        async_serialize_start();
231
        curs_hide_sync();
331
        curs_hide_sync();
232
        gcons_in_kernel();
332
        gcons_in_kernel();
233
        async_serialize_end();
333
        async_serialize_end();
Line 250... Line 350...
250
        conn = &connections[active_console];
350
        conn = &connections[active_console];
251
       
351
       
252
        set_attrs(&conn->screenbuffer.attrs);
352
        set_attrs(&conn->screenbuffer.attrs);
253
        curs_visibility(false);
353
        curs_visibility(false);
254
        if (interbuffer) {
354
        if (interbuffer) {
255
            for (i = 0; i < conn->screenbuffer.size_x; i++)
355
            for (j = 0; j < conn->screenbuffer.size_y; j++) {
256
                for (j = 0; j < conn->screenbuffer.size_y; j++) {
356
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
257
                    unsigned int size_x;
357
                    unsigned int size_x;
258
                   
358
                   
259
                    size_x = conn->screenbuffer.size_x;
359
                    size_x = conn->screenbuffer.size_x;
260
                    interbuffer[i + j * size_x] =
360
                    interbuffer[j * size_x + i] =
261
                        *get_field_at(&conn->screenbuffer, i, j);
361
                        *get_field_at(&conn->screenbuffer, i, j);
262
                }
362
                }
-
 
363
            }
263
            /* This call can preempt, but we are already at the end */
364
            /* This call can preempt, but we are already at the end */
264
            rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA);
365
            rc = async_req_4_0(fb_info.phone, FB_DRAW_TEXT_DATA,
-
 
366
                0, 0, conn->screenbuffer.size_x,
-
 
367
                conn->screenbuffer.size_y);
265
        }
368
        }
266
       
369
       
267
        if ((!interbuffer) || (rc != 0)) {
370
        if ((!interbuffer) || (rc != 0)) {
268
            set_attrs(&conn->screenbuffer.attrs);
371
            set_attrs(&conn->screenbuffer.attrs);
269
            clrscr();
372
            clrscr();
Line 278... Line 381...
278
                    if ((field->character == ' ') &&
381
                    if ((field->character == ' ') &&
279
                        (attrs_same(field->attrs,
382
                        (attrs_same(field->attrs,
280
                        conn->screenbuffer.attrs)))
383
                        conn->screenbuffer.attrs)))
281
                        continue;
384
                        continue;
282
 
385
 
283
                    prtchr(field->character, j, i);
386
                    fb_putchar(field->character, j, i);
284
                }
387
                }
285
        }
388
        }
286
       
389
       
287
        curs_goto(conn->screenbuffer.position_y,
390
        curs_goto(conn->screenbuffer.position_y,
288
            conn->screenbuffer.position_x);
391
            conn->screenbuffer.position_x);
Line 331... Line 434...
331
            /* switch to another virtual console */
434
            /* switch to another virtual console */
332
           
435
           
333
            conn = &connections[active_console];
436
            conn = &connections[active_console];
334
 
437
 
335
            if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
438
            if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
336
                CONSOLE_COUNT)) {
439
                CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
337
                if (ev.key == KC_F12)
440
                if (ev.key == KC_F12)
338
                    change_console(KERNEL_CONSOLE);
441
                    change_console(KERNEL_CONSOLE);
339
                else
442
                else
340
                    change_console(ev.key - KC_F1);
443
                    change_console(ev.key - KC_F1);
341
                break;
444
                break;
Line 358... Line 461...
358
        }
461
        }
359
        ipc_answer_0(callid, retval);
462
        ipc_answer_0(callid, retval);
360
    }
463
    }
361
}
464
}
362
 
465
 
-
 
466
/** Handle CONSOLE_WRITE call. */
-
 
467
static void cons_write(int consnum, ipc_callid_t rid, ipc_call_t *request)
-
 
468
{
-
 
469
    ipc_callid_t callid;
-
 
470
    size_t size;
-
 
471
    wchar_t ch;
-
 
472
    size_t off;
-
 
473
 
-
 
474
    if (!ipc_data_write_receive(&callid, &size)) {
-
 
475
        ipc_answer_0(callid, EINVAL);
-
 
476
        ipc_answer_0(rid, EINVAL);
-
 
477
    }
-
 
478
 
-
 
479
    if (size > CWRITE_BUF_SIZE)
-
 
480
        size = CWRITE_BUF_SIZE;
-
 
481
 
-
 
482
    (void) ipc_data_write_finalize(callid, cwrite_buf, size);
-
 
483
 
-
 
484
    off = 0;
-
 
485
    while (off < size) {
-
 
486
        ch = str_decode(cwrite_buf, &off, size);
-
 
487
        write_char(consnum, ch);
-
 
488
    }
-
 
489
 
-
 
490
    gcons_notify_char(consnum);
-
 
491
    ipc_answer_1(rid, EOK, size);
-
 
492
}
-
 
493
 
363
/** Default thread for new connections */
494
/** Default thread for new connections */
364
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
495
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
365
{
496
{
366
    ipc_callid_t callid;
497
    ipc_callid_t callid;
367
    ipc_call_t call;
498
    ipc_call_t call;
368
    int consnum;
499
    int consnum;
369
    ipcarg_t arg1, arg2, arg3, arg4;
500
    ipcarg_t arg1, arg2, arg3, arg4;
370
    connection_t *conn;
501
    connection_t *conn;
-
 
502
    screenbuffer_t *scr;
371
   
503
   
372
    if ((consnum = find_free_connection()) == -1) {
504
    if ((consnum = find_free_connection()) == -1) {
373
        ipc_answer_0(iid, ELIMIT);
505
        ipc_answer_0(iid, ELIMIT);
374
        return;
506
        return;
375
    }
507
    }
Line 409... Line 541...
409
            return;
541
            return;
410
        case CONSOLE_PUTCHAR:
542
        case CONSOLE_PUTCHAR:
411
            write_char(consnum, IPC_GET_ARG1(call));
543
            write_char(consnum, IPC_GET_ARG1(call));
412
            gcons_notify_char(consnum);
544
            gcons_notify_char(consnum);
413
            break;
545
            break;
-
 
546
        case CONSOLE_WRITE:
-
 
547
            cons_write(consnum, callid, &call);
-
 
548
            continue;
414
        case CONSOLE_CLEAR:
549
        case CONSOLE_CLEAR:
415
            /* Send message to fb */
550
            /* Send message to fb */
416
            if (consnum == active_console) {
551
            if (consnum == active_console) {
417
                async_msg_0(fb_info.phone, FB_CLEAR);
552
                async_msg_0(fb_info.phone, FB_CLEAR);
418
            }
553
            }
Line 430... Line 565...
430
        case CONSOLE_GETSIZE:
565
        case CONSOLE_GETSIZE:
431
            arg1 = fb_info.rows;
566
            arg1 = fb_info.rows;
432
            arg2 = fb_info.cols;
567
            arg2 = fb_info.cols;
433
            break;
568
            break;
434
        case CONSOLE_FLUSH:
569
        case CONSOLE_FLUSH:
-
 
570
            fb_pending_flush();
435
            if (consnum == active_console)
571
            if (consnum == active_console) {
436
                async_req_0_0(fb_info.phone, FB_FLUSH);
572
                async_req_0_0(fb_info.phone, FB_FLUSH);
-
 
573
 
-
 
574
                scr = &(connections[consnum].screenbuffer);
-
 
575
                curs_goto(scr->position_y, scr->position_x);
-
 
576
            }
437
            break;
577
            break;
438
        case CONSOLE_SET_STYLE:
578
        case CONSOLE_SET_STYLE:
-
 
579
            fb_pending_flush();
439
            arg1 = IPC_GET_ARG1(call);
580
            arg1 = IPC_GET_ARG1(call);
440
            screenbuffer_set_style(&conn->screenbuffer, arg1);
581
            screenbuffer_set_style(&conn->screenbuffer, arg1);
441
            if (consnum == active_console)
582
            if (consnum == active_console)
442
                set_style(arg1);
583
                set_style(arg1);
443
            break;
584
            break;
444
        case CONSOLE_SET_COLOR:
585
        case CONSOLE_SET_COLOR:
-
 
586
            fb_pending_flush();
445
            arg1 = IPC_GET_ARG1(call);
587
            arg1 = IPC_GET_ARG1(call);
446
            arg2 = IPC_GET_ARG2(call);
588
            arg2 = IPC_GET_ARG2(call);
447
            arg3 = IPC_GET_ARG3(call);
589
            arg3 = IPC_GET_ARG3(call);
448
            screenbuffer_set_color(&conn->screenbuffer, arg1,
590
            screenbuffer_set_color(&conn->screenbuffer, arg1,
449
                arg2, arg3);
591
                arg2, arg3);
450
            if (consnum == active_console)
592
            if (consnum == active_console)
451
                set_color(arg1, arg2, arg3);
593
                set_color(arg1, arg2, arg3);
452
            break;
594
            break;
453
        case CONSOLE_SET_RGB_COLOR:
595
        case CONSOLE_SET_RGB_COLOR:
-
 
596
            fb_pending_flush();
454
            arg1 = IPC_GET_ARG1(call);
597
            arg1 = IPC_GET_ARG1(call);
455
            arg2 = IPC_GET_ARG2(call);
598
            arg2 = IPC_GET_ARG2(call);
456
            screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
599
            screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
457
                arg2);
600
                arg2);
458
            if (consnum == active_console)
601
            if (consnum == active_console)
459
                set_rgb_color(arg1, arg2);
602
                set_rgb_color(arg1, arg2);
460
            break;
603
            break;
461
        case CONSOLE_CURSOR_VISIBILITY:
604
        case CONSOLE_CURSOR_VISIBILITY:
-
 
605
            fb_pending_flush();
462
            arg1 = IPC_GET_ARG1(call);
606
            arg1 = IPC_GET_ARG1(call);
463
            conn->screenbuffer.is_cursor_visible = arg1;
607
            conn->screenbuffer.is_cursor_visible = arg1;
464
            if (consnum == active_console)
608
            if (consnum == active_console)
465
                curs_visibility(arg1);
609
                curs_visibility(arg1);
466
            break;
610
            break;
Line 485... Line 629...
485
            arg1 = ev.type;
629
            arg1 = ev.type;
486
            arg2 = ev.key;
630
            arg2 = ev.key;
487
            arg3 = ev.mods;
631
            arg3 = ev.mods;
488
            arg4 = ev.c;
632
            arg4 = ev.c;
489
            break;
633
            break;
-
 
634
        case CONSOLE_KCON_ENABLE:
-
 
635
            change_console(KERNEL_CONSOLE);
-
 
636
            break;
490
        }
637
        }
491
        ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
638
        ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
492
    }
639
    }
493
}
640
}
494
 
641
 
Line 562... Line 709...
562
 
709
 
563
    /* Set up shared memory buffer. */
710
    /* Set up shared memory buffer. */
564
    ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
711
    ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
565
    interbuffer = as_get_mappable_page(ib_size);
712
    interbuffer = as_get_mappable_page(ib_size);
566
 
713
 
-
 
714
    fb_pending.n = 0;
-
 
715
 
567
    if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
716
    if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
568
        AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
717
        AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
569
        interbuffer = NULL;
718
        interbuffer = NULL;
570
    }
719
    }
571
 
720
 
Line 584... Line 733...
584
    /* Register at NS */
733
    /* Register at NS */
585
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
734
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
586
        return -1;
735
        return -1;
587
   
736
   
588
    /* Receive kernel notifications */
737
    /* Receive kernel notifications */
589
//  if (sysinfo_value("kconsole.present")) {
-
 
590
//      int inr = sysinfo_value("kconsole.inr");
-
 
591
//      if (ipc_register_irq(inr, device_assign_devno(), 0, NULL) != EOK)
738
    if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
592
//          printf(NAME ": Error registering kconsole notifications\n");
739
        printf(NAME ": Error registering kconsole notifications\n");
593
//      
740
       
594
//      async_set_interrupt_received(interrupt_received);
741
    async_set_interrupt_received(interrupt_received);
595
//  }
-
 
596
   
742
   
597
    // FIXME: avoid connectiong to itself, keep using klog
743
    // FIXME: avoid connectiong to itself, keep using klog
598
    // printf(NAME ": Accepting connections\n");
744
    // printf(NAME ": Accepting connections\n");
599
    async_manager();
745
    async_manager();
600
   
746