Subversion Repositories HelenOS

Rev

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