Subversion Repositories HelenOS

Rev

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