Subversion Repositories HelenOS

Rev

Rev 4153 | Rev 4327 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1445 cejka 1
/*
2071 jermar 2
 * Copyright (c) 2006 Josef Cejka
1445 cejka 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
1649 cejka 28
 
1740 jermar 29
/** @addtogroup console
1649 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
2618 jermar 35
#include <libc.h>
1451 cejka 36
#include <fb.h>
1445 cejka 37
#include <ipc/ipc.h>
4153 mejdrech 38
#include <kbd.h>
39
#include <kbd/keycode.h>
1451 cejka 40
#include <ipc/fb.h>
1445 cejka 41
#include <ipc/services.h>
42
#include <errno.h>
1451 cejka 43
#include <key_buffer.h>
4153 mejdrech 44
#include <ipc/console.h>
1453 palkovsky 45
#include <unistd.h>
46
#include <async.h>
1481 cejka 47
#include <libadt/fifo.h>
1487 cejka 48
#include <screenbuffer.h>
1867 jermar 49
#include <sys/mman.h>
3084 decky 50
#include <stdio.h>
4263 mejdrech 51
#include <string.h>
4153 mejdrech 52
#include <sysinfo.h>
4263 mejdrech 53
#include <event.h>
1445 cejka 54
 
4153 mejdrech 55
#include "console.h"
1522 palkovsky 56
#include "gcons.h"
57
 
1481 cejka 58
#define MAX_KEYREQUESTS_BUFFERED 32
1451 cejka 59
 
3084 decky 60
#define NAME "console"
1445 cejka 61
 
1526 cejka 62
/** Index of currently used virtual console.
63
 */
1512 cejka 64
int active_console = 0;
4153 mejdrech 65
int prev_console = 0;
1453 palkovsky 66
 
4263 mejdrech 67
/** Information about framebuffer */
1487 cejka 68
struct {
69
    int phone;      /**< Framebuffer phone */
1506 cejka 70
    ipcarg_t rows;      /**< Framebuffer rows */
71
    ipcarg_t cols;      /**< Framebuffer columns */
1487 cejka 72
} fb_info;
73
 
1451 cejka 74
typedef struct {
1526 cejka 75
    keybuffer_t keybuffer;      /**< Buffer for incoming keys. */
2025 jermar 76
    /** Buffer for unsatisfied request for keys. */
77
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t,
78
        MAX_KEYREQUESTS_BUFFERED); 
1526 cejka 79
    int keyrequest_counter;     /**< Number of requests in buffer. */
80
    int client_phone;       /**< Phone to connected client. */
2025 jermar 81
    int used;           /**< 1 if this virtual console is
82
                     * connected to some client.*/
83
    screenbuffer_t screenbuffer;    /**< Screenbuffer for saving screen
84
                     * contents and related settings. */
1451 cejka 85
} connection_t;
86
 
2025 jermar 87
static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual
88
                         * consoles */
89
static keyfield_t *interbuffer = NULL;      /**< Pointer to memory shared
90
                         * with framebufer used for
91
                         * faster virtual console
2069 jermar 92
                         * switching */
1506 cejka 93
 
4263 mejdrech 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;
1506 cejka 100
 
4263 mejdrech 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
 
109
 
1526 cejka 110
/** Find unused virtual console.
111
 *
112
 */
1574 palkovsky 113
static int find_free_connection(void)
1451 cejka 114
{
2069 jermar 115
    int i;
1451 cejka 116
 
2069 jermar 117
    for (i = 0; i < CONSOLE_COUNT; i++) {
1574 palkovsky 118
        if (!connections[i].used)
1451 cejka 119
            return i;
120
    }
1574 palkovsky 121
    return -1;
1451 cejka 122
}
123
 
1552 palkovsky 124
static void clrscr(void)
125
{
2621 jermar 126
    async_msg_0(fb_info.phone, FB_CLEAR);
1552 palkovsky 127
}
128
 
4153 mejdrech 129
static void curs_visibility(bool visible)
1552 palkovsky 130
{
4153 mejdrech 131
    async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible);
1552 palkovsky 132
}
133
 
4153 mejdrech 134
static void curs_hide_sync(void)
135
{
136
    ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false);
137
}
138
 
1552 palkovsky 139
static void curs_goto(int row, int col)
140
{
1610 palkovsky 141
    async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col);
1552 palkovsky 142
}
143
 
4153 mejdrech 144
static void set_style(int style)
1552 palkovsky 145
{
4153 mejdrech 146
    async_msg_1(fb_info.phone, FB_SET_STYLE, style);
1552 palkovsky 147
}
148
 
4153 mejdrech 149
static void set_color(int fgcolor, int bgcolor, int flags)
1552 palkovsky 150
{
4153 mejdrech 151
    async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags);
1552 palkovsky 152
}
153
 
4153 mejdrech 154
static void set_rgb_color(int fgcolor, int bgcolor)
155
{
156
    async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor);
157
}
158
 
159
static void set_attrs(attrs_t *attrs)
160
{
161
    switch (attrs->t) {
162
    case at_style:
163
        set_style(attrs->a.s.style);
164
        break;
165
 
166
    case at_idx:
167
        set_color(attrs->a.i.fg_color, attrs->a.i.bg_color,
168
            attrs->a.i.flags);
169
        break;
170
 
171
    case at_rgb:
172
        set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color);
173
        break;
174
    }
175
}
176
 
4263 mejdrech 177
/** Send an area of screenbuffer to the FB driver. */
178
static void fb_update_area(connection_t *conn, int x, int y, int w, int h)
1552 palkovsky 179
{
4263 mejdrech 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
 
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
    }
1552 palkovsky 216
}
217
 
4263 mejdrech 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;
229
    }
230
}
231
 
232
/** Mark a character cell as changed.
1497 cejka 233
 *
4263 mejdrech 234
 * This adds the cell to the pending rowspan if possible. Otherwise
235
 * the old span is flushed first.
2566 jermar 236
 */
4263 mejdrech 237
static void cell_mark_changed(int row, int col)
1497 cejka 238
{
4263 mejdrech 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. */
256
static void fb_putchar(wchar_t c, int row, int col)
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;
1497 cejka 265
    screenbuffer_t *scr = &(connections[console].screenbuffer);
4263 mejdrech 266
 
267
    switch (ch) {
2025 jermar 268
    case '\n':
4263 mejdrech 269
        fb_pending_flush();
270
        flush_cursor = true;
2069 jermar 271
        scr->position_y++;
272
        scr->position_x = 0;
2025 jermar 273
        break;
274
    case '\r':
275
        break;
276
    case '\t':
277
        scr->position_x += 8;
278
        scr->position_x -= scr->position_x % 8;
279
        break;
280
    case '\b':
281
        if (scr->position_x == 0)
1497 cejka 282
            break;
2025 jermar 283
        scr->position_x--;
284
        if (console == active_console)
4263 mejdrech 285
            cell_mark_changed(scr->position_y, scr->position_x);
2025 jermar 286
        screenbuffer_putchar(scr, ' ');
287
        break;
288
    default:   
289
        if (console == active_console)
4263 mejdrech 290
            cell_mark_changed(scr->position_y, scr->position_x);
1497 cejka 291
 
4263 mejdrech 292
        screenbuffer_putchar(scr, ch);
2025 jermar 293
        scr->position_x++;
1497 cejka 294
    }
4263 mejdrech 295
 
296
    if (scr->position_x >= scr->size_x) {
297
        flush_cursor = true;
298
        scr->position_y++;
299
    }
1497 cejka 300
 
301
    if (scr->position_y >= scr->size_y) {
4263 mejdrech 302
        fb_pending_flush();
1497 cejka 303
        scr->position_y = scr->size_y - 1;
1674 palkovsky 304
        screenbuffer_clear_line(scr, scr->top_line);
2069 jermar 305
        scr->top_line = (scr->top_line + 1) % scr->size_y;
1518 palkovsky 306
        if (console == active_console)
2621 jermar 307
            async_msg_1(fb_info.phone, FB_SCROLL, 1);
1497 cejka 308
    }
4263 mejdrech 309
 
1497 cejka 310
    scr->position_x = scr->position_x % scr->size_x;
4263 mejdrech 311
 
312
    if (console == active_console && flush_cursor)
1552 palkovsky 313
        curs_goto(scr->position_y, scr->position_x);
1497 cejka 314
}
315
 
1552 palkovsky 316
/** Switch to new console */
317
static void change_console(int newcons)
318
{
319
    connection_t *conn;
1673 palkovsky 320
    int i, j, rc;
1578 cejka 321
    keyfield_t *field;
4153 mejdrech 322
    attrs_t *attrs;
323
 
1552 palkovsky 324
    if (newcons == active_console)
325
        return;
4263 mejdrech 326
 
327
    fb_pending_flush();
328
 
1555 palkovsky 329
    if (newcons == KERNEL_CONSOLE) {
1717 palkovsky 330
        async_serialize_start();
4153 mejdrech 331
        curs_hide_sync();
332
        gcons_in_kernel();
1717 palkovsky 333
        async_serialize_end();
4153 mejdrech 334
 
335
        if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) {
336
            prev_console = active_console;
337
            active_console = KERNEL_CONSOLE;
338
        } else
339
            newcons = active_console;
1552 palkovsky 340
    }
1578 cejka 341
 
4153 mejdrech 342
    if (newcons != KERNEL_CONSOLE) {
343
        async_serialize_start();
344
 
345
        if (active_console == KERNEL_CONSOLE)
346
            gcons_redraw_console();
347
 
348
        active_console = newcons;
349
        gcons_change_console(newcons);
350
        conn = &connections[active_console];
351
 
352
        set_attrs(&conn->screenbuffer.attrs);
353
        curs_visibility(false);
354
        if (interbuffer) {
4263 mejdrech 355
            for (j = 0; j < conn->screenbuffer.size_y; j++) {
356
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
4153 mejdrech 357
                    unsigned int size_x;
358
 
359
                    size_x = conn->screenbuffer.size_x;
4263 mejdrech 360
                    interbuffer[j * size_x + i] =
4153 mejdrech 361
                        *get_field_at(&conn->screenbuffer, i, j);
362
                }
4263 mejdrech 363
            }
4153 mejdrech 364
            /* This call can preempt, but we are already at the end */
4263 mejdrech 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);
4153 mejdrech 368
        }
369
 
370
        if ((!interbuffer) || (rc != 0)) {
371
            set_attrs(&conn->screenbuffer.attrs);
372
            clrscr();
373
            attrs = &conn->screenbuffer.attrs;
374
 
375
            for (j = 0; j < conn->screenbuffer.size_y; j++)
376
                for (i = 0; i < conn->screenbuffer.size_x; i++) {
377
                    field = get_field_at(&conn->screenbuffer, i, j);
378
                    if (!attrs_same(*attrs, field->attrs))
379
                        set_attrs(&field->attrs);
380
                    attrs = &field->attrs;
381
                    if ((field->character == ' ') &&
382
                        (attrs_same(field->attrs,
383
                        conn->screenbuffer.attrs)))
384
                        continue;
1578 cejka 385
 
4263 mejdrech 386
                    fb_putchar(field->character, j, i);
4153 mejdrech 387
                }
388
        }
389
 
390
        curs_goto(conn->screenbuffer.position_y,
391
            conn->screenbuffer.position_x);
392
        curs_visibility(conn->screenbuffer.is_cursor_visible);
393
 
394
        async_serialize_end();
1552 palkovsky 395
    }
396
}
397
 
1526 cejka 398
/** Handler for keyboard */
1453 palkovsky 399
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
1445 cejka 400
{
1453 palkovsky 401
    ipc_callid_t callid;
1445 cejka 402
    ipc_call_t call;
1453 palkovsky 403
    int retval;
4153 mejdrech 404
    kbd_event_t ev;
1489 palkovsky 405
    connection_t *conn;
1717 palkovsky 406
    int newcon;
1506 cejka 407
 
1453 palkovsky 408
    /* Ignore parameters, the connection is alread opened */
409
    while (1) {
410
        callid = async_get_call(&call);
411
        switch (IPC_GET_METHOD(call)) {
412
        case IPC_M_PHONE_HUNGUP:
413
            /* TODO: Handle hangup */
414
            return;
1717 palkovsky 415
        case KBD_MS_LEFT:
416
            newcon = gcons_mouse_btn(IPC_GET_ARG1(call));
417
            if (newcon != -1)
418
                change_console(newcon);
1721 palkovsky 419
            retval = 0;
1717 palkovsky 420
            break;
1707 palkovsky 421
        case KBD_MS_MOVE:
2025 jermar 422
            gcons_mouse_move(IPC_GET_ARG1(call),
2539 jermar 423
                IPC_GET_ARG2(call));
1721 palkovsky 424
            retval = 0;
1707 palkovsky 425
            break;
4153 mejdrech 426
        case KBD_EVENT:
427
            /* Got event from keyboard driver. */
428
            retval = 0;
429
            ev.type = IPC_GET_ARG1(call);
430
            ev.key = IPC_GET_ARG2(call);
431
            ev.mods = IPC_GET_ARG3(call);
432
            ev.c = IPC_GET_ARG4(call);
1465 cejka 433
 
1453 palkovsky 434
            /* switch to another virtual console */
1481 cejka 435
 
1489 palkovsky 436
            conn = &connections[active_console];
4153 mejdrech 437
 
438
            if ((ev.key >= KC_F1) && (ev.key < KC_F1 +
4263 mejdrech 439
                CONSOLE_COUNT) && ((ev.mods & KM_CTRL) == 0)) {
4153 mejdrech 440
                if (ev.key == KC_F12)
1555 palkovsky 441
                    change_console(KERNEL_CONSOLE);
1552 palkovsky 442
                else
4153 mejdrech 443
                    change_console(ev.key - KC_F1);
1453 palkovsky 444
                break;
445
            }
1481 cejka 446
 
447
            /* if client is awaiting key, send it */
1489 palkovsky 448
            if (conn->keyrequest_counter > 0) {    
449
                conn->keyrequest_counter--;
4153 mejdrech 450
                ipc_answer_4(fifo_pop(conn->keyrequests), EOK,
451
                    ev.type, ev.key, ev.mods, ev.c);
1481 cejka 452
                break;
453
            }
4153 mejdrech 454
 
455
            keybuffer_push(&conn->keybuffer, &ev);
1721 palkovsky 456
            retval = 0;
4153 mejdrech 457
 
1453 palkovsky 458
            break;
459
        default:
1459 palkovsky 460
            retval = ENOENT;
1610 palkovsky 461
        }
2619 jermar 462
        ipc_answer_0(callid, retval);
1453 palkovsky 463
    }
464
}
465
 
4263 mejdrech 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
 
1453 palkovsky 494
/** Default thread for new connections */
1518 palkovsky 495
static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
1453 palkovsky 496
{
1445 cejka 497
    ipc_callid_t callid;
1453 palkovsky 498
    ipc_call_t call;
499
    int consnum;
4153 mejdrech 500
    ipcarg_t arg1, arg2, arg3, arg4;
1592 palkovsky 501
    connection_t *conn;
4263 mejdrech 502
    screenbuffer_t *scr;
4153 mejdrech 503
 
1574 palkovsky 504
    if ((consnum = find_free_connection()) == -1) {
2619 jermar 505
        ipc_answer_0(iid, ELIMIT);
1453 palkovsky 506
        return;
507
    }
1592 palkovsky 508
    conn = &connections[consnum];
1610 palkovsky 509
    conn->used = 1;
1555 palkovsky 510
 
1610 palkovsky 511
    async_serialize_start();
1555 palkovsky 512
    gcons_notify_connect(consnum);
2637 cejka 513
    conn->client_phone = IPC_GET_ARG5(*icall);
1592 palkovsky 514
    screenbuffer_clear(&conn->screenbuffer);
1497 cejka 515
 
1453 palkovsky 516
    /* Accept the connection */
2619 jermar 517
    ipc_answer_0(iid, EOK);
4153 mejdrech 518
 
1453 palkovsky 519
    while (1) {
1610 palkovsky 520
        async_serialize_end();
1453 palkovsky 521
        callid = async_get_call(&call);
1610 palkovsky 522
        async_serialize_start();
4153 mejdrech 523
 
2566 jermar 524
        arg1 = 0;
525
        arg2 = 0;
4153 mejdrech 526
        arg3 = 0;
527
        arg4 = 0;
528
 
1453 palkovsky 529
        switch (IPC_GET_METHOD(call)) {
530
        case IPC_M_PHONE_HUNGUP:
1592 palkovsky 531
            gcons_notify_disconnect(consnum);
1610 palkovsky 532
 
1592 palkovsky 533
            /* Answer all pending requests */
4153 mejdrech 534
            while (conn->keyrequest_counter > 0) {
1592 palkovsky 535
                conn->keyrequest_counter--;
2619 jermar 536
                ipc_answer_0(fifo_pop(conn->keyrequests),
537
                    ENOENT);
1592 palkovsky 538
                break;
539
            }
1616 palkovsky 540
            conn->used = 0;
1453 palkovsky 541
            return;
542
        case CONSOLE_PUTCHAR:
1497 cejka 543
            write_char(consnum, IPC_GET_ARG1(call));
1528 palkovsky 544
            gcons_notify_char(consnum);
1453 palkovsky 545
            break;
4263 mejdrech 546
        case CONSOLE_WRITE:
547
            cons_write(consnum, callid, &call);
548
            continue;
1476 cejka 549
        case CONSOLE_CLEAR:
1487 cejka 550
            /* Send message to fb */
551
            if (consnum == active_console) {
2621 jermar 552
                async_msg_0(fb_info.phone, FB_CLEAR);
1487 cejka 553
            }
554
 
1592 palkovsky 555
            screenbuffer_clear(&conn->screenbuffer);
1487 cejka 556
 
1476 cejka 557
            break;
558
        case CONSOLE_GOTO:
2025 jermar 559
            screenbuffer_goto(&conn->screenbuffer,
2539 jermar 560
                IPC_GET_ARG2(call), IPC_GET_ARG1(call));
1567 palkovsky 561
            if (consnum == active_console)
2025 jermar 562
                curs_goto(IPC_GET_ARG1(call),
2539 jermar 563
                    IPC_GET_ARG2(call));
1476 cejka 564
            break;
1521 cejka 565
        case CONSOLE_GETSIZE:
1528 palkovsky 566
            arg1 = fb_info.rows;
567
            arg2 = fb_info.cols;
1521 cejka 568
            break;
1523 cejka 569
        case CONSOLE_FLUSH:
4263 mejdrech 570
            fb_pending_flush();
571
            if (consnum == active_console) {
2621 jermar 572
                async_req_0_0(fb_info.phone, FB_FLUSH);
4263 mejdrech 573
 
574
                scr = &(connections[consnum].screenbuffer);
575
                curs_goto(scr->position_y, scr->position_x);
576
            }
1523 cejka 577
            break;
1525 cejka 578
        case CONSOLE_SET_STYLE:
4263 mejdrech 579
            fb_pending_flush();
1525 cejka 580
            arg1 = IPC_GET_ARG1(call);
4153 mejdrech 581
            screenbuffer_set_style(&conn->screenbuffer, arg1);
582
            if (consnum == active_console)
583
                set_style(arg1);
584
            break;
585
        case CONSOLE_SET_COLOR:
4263 mejdrech 586
            fb_pending_flush();
4153 mejdrech 587
            arg1 = IPC_GET_ARG1(call);
1525 cejka 588
            arg2 = IPC_GET_ARG2(call);
4153 mejdrech 589
            arg3 = IPC_GET_ARG3(call);
590
            screenbuffer_set_color(&conn->screenbuffer, arg1,
591
                arg2, arg3);
592
            if (consnum == active_console)
593
                set_color(arg1, arg2, arg3);
594
            break;
595
        case CONSOLE_SET_RGB_COLOR:
4263 mejdrech 596
            fb_pending_flush();
4153 mejdrech 597
            arg1 = IPC_GET_ARG1(call);
598
            arg2 = IPC_GET_ARG2(call);
599
            screenbuffer_set_rgb_color(&conn->screenbuffer, arg1,
2539 jermar 600
                arg2);
1525 cejka 601
            if (consnum == active_console)
4153 mejdrech 602
                set_rgb_color(arg1, arg2);
1525 cejka 603
            break;
1575 cejka 604
        case CONSOLE_CURSOR_VISIBILITY:
4263 mejdrech 605
            fb_pending_flush();
1575 cejka 606
            arg1 = IPC_GET_ARG1(call);
1592 palkovsky 607
            conn->screenbuffer.is_cursor_visible = arg1;
1575 cejka 608
            if (consnum == active_console)
609
                curs_visibility(arg1);
610
            break;
4153 mejdrech 611
        case CONSOLE_GETKEY:
1592 palkovsky 612
            if (keybuffer_empty(&conn->keybuffer)) {
1481 cejka 613
                /* buffer is empty -> store request */
2025 jermar 614
                if (conn->keyrequest_counter <
615
                    MAX_KEYREQUESTS_BUFFERED) {
1592 palkovsky 616
                    fifo_push(conn->keyrequests, callid);
617
                    conn->keyrequest_counter++;
1481 cejka 618
                } else {
2025 jermar 619
                    /*
620
                     * No key available and too many
621
                     * requests => fail.
622
                    */
2619 jermar 623
                    ipc_answer_0(callid, ELIMIT);
1481 cejka 624
                }
625
                continue;
2025 jermar 626
            }
4153 mejdrech 627
            kbd_event_t ev;
628
            keybuffer_pop(&conn->keybuffer, &ev);
629
            arg1 = ev.type;
630
            arg2 = ev.key;
631
            arg3 = ev.mods;
632
            arg4 = ev.c;
1453 palkovsky 633
            break;
4263 mejdrech 634
        case CONSOLE_KCON_ENABLE:
635
            change_console(KERNEL_CONSOLE);
636
            break;
1453 palkovsky 637
        }
4153 mejdrech 638
        ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4);
1453 palkovsky 639
    }
640
}
641
 
4153 mejdrech 642
static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
643
{
644
    change_console(prev_console);
645
}
646
 
1453 palkovsky 647
int main(int argc, char *argv[])
648
{
3084 decky 649
    printf(NAME ": HelenOS Console service\n");
650
 
1453 palkovsky 651
    ipcarg_t phonehash;
1721 palkovsky 652
    int kbd_phone;
4153 mejdrech 653
    size_t ib_size;
1451 cejka 654
    int i;
4153 mejdrech 655
 
1490 palkovsky 656
    async_set_client_connection(client_connection);
1445 cejka 657
 
658
    /* Connect to keyboard driver */
4153 mejdrech 659
    kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
660
    if (kbd_phone < 0) {
661
        printf(NAME ": Failed to connect to keyboard service\n");
662
        return -1;
2025 jermar 663
    }
1445 cejka 664
 
4153 mejdrech 665
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
666
        printf(NAME ": Failed to create callback from keyboard service\n");
1445 cejka 667
        return -1;
4153 mejdrech 668
    }
669
 
1689 palkovsky 670
    async_new_connection(phonehash, 0, NULL, keyboard_events);
671
 
1445 cejka 672
    /* Connect to framebuffer driver */
4153 mejdrech 673
    fb_info.phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VIDEO, 0, 0);
674
    if (fb_info.phone < 0) {
675
        printf(NAME ": Failed to connect to video service\n");
676
        return -1;
1487 cejka 677
    }
1552 palkovsky 678
 
4153 mejdrech 679
    /* Disable kernel output to the console */
680
    __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE);
681
 
1522 palkovsky 682
    /* Initialize gcons */
683
    gcons_init(fb_info.phone);
684
    /* Synchronize, the gcons can have something in queue */
2621 jermar 685
    async_req_0_0(fb_info.phone, FB_FLUSH);
1487 cejka 686
 
2621 jermar 687
    async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows,
2539 jermar 688
        &fb_info.cols);
4153 mejdrech 689
    set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
1552 palkovsky 690
    clrscr();
1487 cejka 691
 
692
    /* Init virtual consoles */
1451 cejka 693
    for (i = 0; i < CONSOLE_COUNT; i++) {
694
        connections[i].used = 0;
2539 jermar 695
        keybuffer_init(&connections[i].keybuffer);
1481 cejka 696
 
2539 jermar 697
        connections[i].keyrequests.head = 0;
698
        connections[i].keyrequests.tail = 0;
1481 cejka 699
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
700
        connections[i].keyrequest_counter = 0;
1487 cejka 701
 
2539 jermar 702
        if (screenbuffer_init(&connections[i].screenbuffer,
703
            fb_info.cols, fb_info.rows) == NULL) {
2069 jermar 704
            /* FIXME: handle error */
1487 cejka 705
            return -1;
706
        }
1451 cejka 707
    }
1574 palkovsky 708
    connections[KERNEL_CONSOLE].used = 1;
4153 mejdrech 709
 
710
    /* Set up shared memory buffer. */
711
    ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows;
712
    interbuffer = as_get_mappable_page(ib_size);
713
 
4263 mejdrech 714
    fb_pending.n = 0;
715
 
4153 mejdrech 716
    if (as_area_create(interbuffer, ib_size, AS_AREA_READ |
717
        AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) {
718
        interbuffer = NULL;
719
    }
720
 
721
    if (interbuffer) {
2678 jermar 722
        if (ipc_share_out_start(fb_info.phone, interbuffer,
2677 jermar 723
            AS_AREA_READ) != EOK) {
4153 mejdrech 724
            as_area_destroy(interbuffer);
1512 cejka 725
            interbuffer = NULL;
726
        }
727
    }
4153 mejdrech 728
 
2069 jermar 729
    curs_goto(0, 0);
2539 jermar 730
    curs_visibility(
731
        connections[active_console].screenbuffer.is_cursor_visible);
4153 mejdrech 732
 
1518 palkovsky 733
    /* Register at NS */
3084 decky 734
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0)
1445 cejka 735
        return -1;
736
 
4153 mejdrech 737
    /* Receive kernel notifications */
4263 mejdrech 738
    if (event_subscribe(EVENT_KCONSOLE, 0) != EOK)
739
        printf(NAME ": Error registering kconsole notifications\n");
740
 
741
    async_set_interrupt_received(interrupt_received);
4153 mejdrech 742
 
3084 decky 743
    // FIXME: avoid connectiong to itself, keep using klog
744
    // printf(NAME ": Accepting connections\n");
1453 palkovsky 745
    async_manager();
4153 mejdrech 746
 
747
    return 0;
748
}
1451 cejka 749
 
1649 cejka 750
/** @}
751
 */