source: mainline/uspace/srv/hid/console/console.c@ dd495c9

Last change on this file since dd495c9 was 49aaa0e, checked in by Jiri Svoboda <jiri@…>, 18 months ago

Switch virtual consoles using Alt-Fn or Shift-Fn

Shift-Fn is useful because Alt-Fn is usually taken by desktop
environment when running in an emulator.

  • Property mode set to 100644
File size: 23.8 KB
RevLine 
[51c1b003]1/*
[49aaa0e]2 * Copyright (c) 2024 Jiri Svoboda
[7c014d1]3 * Copyright (c) 2011 Martin Decky
[51c1b003]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
[ce5bcb4]29
[231a60a]30/** @addtogroup console
[1601f3c]31 * @{
[ce5bcb4]32 */
33/** @file
34 */
35
[7c014d1]36#include <async.h>
37#include <stdio.h>
38#include <adt/prodcons.h>
[a635535]39#include <io/console.h>
[111d2d6]40#include <io/input.h>
[7c014d1]41#include <ipc/vfs.h>
[51c1b003]42#include <errno.h>
[30db06c]43#include <str_error.h>
[15f3c3f]44#include <loc.h>
[5d94b16c]45#include <io/con_srv.h>
[111d2d6]46#include <io/kbd_event.h>
[7c014d1]47#include <io/keycode.h>
[6d5e378]48#include <io/chargrid.h>
49#include <io/output.h>
[7c014d1]50#include <align.h>
51#include <as.h>
[2f6ad06]52#include <task.h>
[1e4cada]53#include <fibril_synch.h>
[508b0df1]54#include <stdatomic.h>
[38d150e]55#include <stdlib.h>
[1d6dd2a]56#include <str.h>
[9805cde]57#include "console.h"
[369a5f8]58
[1313ee9]59#define NAME "console"
60#define NAMESPACE "term"
[79ae36dd]61
[6d5e378]62#define UTF8_CHAR_BUFFER_SIZE (STR_BOUNDS(1) + 1)
[a58bc8b]63
[79460ae]64typedef struct {
[508b0df1]65 atomic_flag refcnt; /**< Connection reference count */
[5d1ff11]66 prodcons_t input_pc; /**< Incoming console events */
[a35b458]67
[6d5e378]68 /**
69 * Not yet sent bytes of last char event.
70 */
71 char char_remains[UTF8_CHAR_BUFFER_SIZE];
72 size_t char_remains_len; /**< Number of not yet sent bytes. */
[a35b458]73
[6d5e378]74 fibril_mutex_t mtx; /**< Lock protecting mutable fields */
[a35b458]75
[6d5e378]76 size_t index; /**< Console index */
77 service_id_t dsid; /**< Service handle */
[a35b458]78
[6d5e378]79 sysarg_t cols; /**< Number of columns */
80 sysarg_t rows; /**< Number of rows */
81 console_caps_t ccaps; /**< Console capabilities */
[a35b458]82
[68a552f]83 sysarg_t ucols; /**< Number of columns in user buffer */
84 sysarg_t urows; /**< Number of rows in user buffer */
85 charfield_t *ubuf; /**< User buffer */
86
[6d5e378]87 chargrid_t *frontbuf; /**< Front buffer */
88 frontbuf_handle_t fbid; /**< Front buffer handle */
[5d94b16c]89 con_srvs_t srvs; /**< Console service setup */
[424cd43]90} console_t;
[79460ae]91
[4c6fd56]92static loc_srv_t *console_srv;
93
[111d2d6]94/** Input server proxy */
95static input_t *input;
[593e023]96static bool active = false;
[7c014d1]97
[6d5e378]98/** Session to the output server */
99static async_sess_t *output_sess;
[7c014d1]100
[6d5e378]101/** Output dimensions */
102static sysarg_t cols;
103static sysarg_t rows;
[7c014d1]104
[5d1ff11]105/** Mouse pointer X coordinate */
106static int pointer_x;
107/** Mouse pointer Y coordinate */
108static int pointer_y;
109/** Character under mouse cursor */
110static charfield_t pointer_bg;
111
112static int mouse_scale_x = 4;
113static int mouse_scale_y = 8;
114
[1601f3c]115/** Array of data for virtual consoles */
[424cd43]116static console_t consoles[CONSOLE_COUNT];
117
[7c014d1]118/** Mutex for console switching */
119static FIBRIL_MUTEX_INITIALIZE(switch_mtx);
120
121static console_t *active_console = &consoles[0];
[1601f3c]122
[b7fd2a0]123static errno_t input_ev_active(input_t *);
124static errno_t input_ev_deactive(input_t *);
[60ebe63]125static errno_t input_ev_key(input_t *, unsigned, kbd_event_type_t, keycode_t,
126 keymod_t, char32_t);
127static errno_t input_ev_move(input_t *, unsigned, int, int);
128static errno_t input_ev_abs_move(input_t *, unsigned, unsigned, unsigned,
129 unsigned, unsigned);
130static errno_t input_ev_button(input_t *, unsigned, int, int);
131static errno_t input_ev_dclick(input_t *, unsigned, int);
[111d2d6]132
133static input_ev_ops_t input_ev_ops = {
[593e023]134 .active = input_ev_active,
135 .deactive = input_ev_deactive,
[111d2d6]136 .key = input_ev_key,
137 .move = input_ev_move,
138 .abs_move = input_ev_abs_move,
[8edec53]139 .button = input_ev_button,
140 .dclick = input_ev_dclick
[111d2d6]141};
142
[b7fd2a0]143static errno_t cons_open(con_srvs_t *, con_srv_t *);
144static errno_t cons_close(con_srv_t *);
145static errno_t cons_read(con_srv_t *, void *, size_t, size_t *);
146static errno_t cons_write(con_srv_t *, void *, size_t, size_t *);
[5d94b16c]147static void cons_sync(con_srv_t *);
148static void cons_clear(con_srv_t *);
149static void cons_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
[b7fd2a0]150static errno_t cons_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
151static errno_t cons_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
152static errno_t cons_get_color_cap(con_srv_t *, console_caps_t *);
[5d94b16c]153static void cons_set_style(con_srv_t *, console_style_t);
154static void cons_set_color(con_srv_t *, console_color_t, console_color_t,
155 console_color_attr_t);
156static void cons_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
157static void cons_set_cursor_visibility(con_srv_t *, bool);
[b48e680f]158static errno_t cons_set_caption(con_srv_t *, const char *);
[b7fd2a0]159static errno_t cons_get_event(con_srv_t *, cons_event_t *);
[68a552f]160static errno_t cons_map(con_srv_t *, sysarg_t, sysarg_t, charfield_t **);
161static void cons_unmap(con_srv_t *);
162static void cons_buf_update(con_srv_t *, sysarg_t, sysarg_t, sysarg_t,
163 sysarg_t);
[5d94b16c]164
165static con_ops_t con_ops = {
166 .open = cons_open,
167 .close = cons_close,
168 .read = cons_read,
169 .write = cons_write,
170 .sync = cons_sync,
171 .clear = cons_clear,
172 .set_pos = cons_set_pos,
173 .get_pos = cons_get_pos,
174 .get_size = cons_get_size,
175 .get_color_cap = cons_get_color_cap,
176 .set_style = cons_set_style,
177 .set_color = cons_set_color,
178 .set_rgb_color = cons_set_rgb_color,
179 .set_cursor_visibility = cons_set_cursor_visibility,
[b48e680f]180 .set_caption = cons_set_caption,
[68a552f]181 .get_event = cons_get_event,
182 .map = cons_map,
183 .unmap = cons_unmap,
184 .update = cons_buf_update
[5d94b16c]185};
186
[5d1ff11]187static void pointer_draw(void);
188static void pointer_undraw(void);
189
[5d94b16c]190static console_t *srv_to_console(con_srv_t *srv)
191{
192 return srv->srvs->sarg;
193}
194
[7c014d1]195static void cons_update(console_t *cons)
[9805cde]196{
[7c014d1]197 fibril_mutex_lock(&switch_mtx);
198 fibril_mutex_lock(&cons->mtx);
[a35b458]199
[593e023]200 if ((active) && (cons == active_console)) {
[6d5e378]201 output_update(output_sess, cons->fbid);
202 output_cursor_update(output_sess, cons->fbid);
[7c014d1]203 }
[a35b458]204
[7c014d1]205 fibril_mutex_unlock(&cons->mtx);
206 fibril_mutex_unlock(&switch_mtx);
[9805cde]207}
208
[7c014d1]209static void cons_update_cursor(console_t *cons)
[9805cde]210{
[7c014d1]211 fibril_mutex_lock(&switch_mtx);
212 fibril_mutex_lock(&cons->mtx);
[a35b458]213
[593e023]214 if ((active) && (cons == active_console))
[6d5e378]215 output_cursor_update(output_sess, cons->fbid);
[a35b458]216
[7c014d1]217 fibril_mutex_unlock(&cons->mtx);
218 fibril_mutex_unlock(&switch_mtx);
[429acb9]219}
220
[6d5e378]221static void cons_damage(console_t *cons)
[d2cc7e1]222{
[7c014d1]223 fibril_mutex_lock(&switch_mtx);
224 fibril_mutex_lock(&cons->mtx);
[a35b458]225
[593e023]226 if ((active) && (cons == active_console)) {
[6d5e378]227 output_damage(output_sess, cons->fbid, 0, 0, cons->cols,
[7c014d1]228 cons->rows);
[6d5e378]229 output_cursor_update(output_sess, cons->fbid);
[dc033a1]230 }
[a35b458]231
[7c014d1]232 fibril_mutex_unlock(&cons->mtx);
233 fibril_mutex_unlock(&switch_mtx);
[d2cc7e1]234}
235
[593e023]236static void cons_switch(unsigned int index)
[d2cc7e1]237{
[593e023]238 /*
239 * The first undefined index is reserved
240 * for switching to the kernel console.
241 */
242 if (index == CONSOLE_COUNT) {
243 if (console_kcon())
244 active = false;
[a35b458]245
[593e023]246 return;
247 }
[a35b458]248
[593e023]249 if (index > CONSOLE_COUNT)
250 return;
[a35b458]251
[593e023]252 console_t *cons = &consoles[index];
[a35b458]253
[7c014d1]254 fibril_mutex_lock(&switch_mtx);
[5d1ff11]255 pointer_undraw();
[a35b458]256
[7c014d1]257 if (cons == active_console) {
258 fibril_mutex_unlock(&switch_mtx);
259 return;
260 }
[a35b458]261
[7c014d1]262 active_console = cons;
[a35b458]263
[5d1ff11]264 pointer_draw();
[7c014d1]265 fibril_mutex_unlock(&switch_mtx);
[a35b458]266
[6d5e378]267 cons_damage(cons);
[d2cc7e1]268}
269
[5d1ff11]270/** Draw mouse pointer. */
271static void pointer_draw(void)
272{
273 charfield_t *ch;
274 int col, row;
275
276 /* Downscale coordinates to text resolution */
277 col = pointer_x / mouse_scale_x;
278 row = pointer_y / mouse_scale_y;
279
280 /* Make sure they are in range */
281 if (col < 0 || row < 0 || col >= (int)cols || row >= (int)rows)
282 return;
283
284 ch = chargrid_charfield_at(active_console->frontbuf, col, row);
285
286 /*
287 * Store background attributes for undrawing the pointer.
288 * This is necessary as styles cannot be inverted with
289 * round trip (unlike RGB or INDEX)
290 */
291 pointer_bg = *ch;
292
293 /* In general the color should be a one's complement of the background */
294 if (ch->attrs.type == CHAR_ATTR_INDEX) {
295 ch->attrs.val.index.bgcolor ^= 0xf;
296 ch->attrs.val.index.fgcolor ^= 0xf;
297 } else if (ch->attrs.type == CHAR_ATTR_RGB) {
298 ch->attrs.val.rgb.fgcolor ^= 0xffffff;
299 ch->attrs.val.rgb.bgcolor ^= 0xffffff;
300 } else if (ch->attrs.type == CHAR_ATTR_STYLE) {
301 /* Don't have a proper inverse for each style */
302 if (ch->attrs.val.style == STYLE_INVERTED)
303 ch->attrs.val.style = STYLE_NORMAL;
304 else
305 ch->attrs.val.style = STYLE_INVERTED;
306 }
307
308 /* Make sure the cell gets updated */
309 ch->flags |= CHAR_FLAG_DIRTY;
310}
311
312/** Undraw mouse pointer. */
313static void pointer_undraw(void)
314{
315 charfield_t *ch;
316 int col, row;
317
318 col = pointer_x / mouse_scale_x;
319 row = pointer_y / mouse_scale_y;
320 if (col < 0 || row < 0 || col >= (int)cols || row >= (int)rows)
321 return;
322
323 ch = chargrid_charfield_at(active_console->frontbuf, col, row);
324 *ch = pointer_bg;
325 ch->flags |= CHAR_FLAG_DIRTY;
326}
327
328/** Queue console event.
329 *
330 * @param cons Console
331 * @param ev Console event
332 */
333static void console_queue_cons_event(console_t *cons, cons_event_t *ev)
334{
335 /* Got key press/release event */
336 cons_event_t *event =
337 (cons_event_t *) malloc(sizeof(cons_event_t));
338 if (event == NULL)
339 return;
340
341 *event = *ev;
342 link_initialize(&event->link);
343
344 prodcons_produce(&cons->input_pc, &event->link);
345}
346
[b7fd2a0]347static errno_t input_ev_active(input_t *input)
[024fcc5]348{
[593e023]349 active = true;
350 output_claim(output_sess);
351 cons_damage(active_console);
[a35b458]352
[593e023]353 return EOK;
354}
355
[b7fd2a0]356static errno_t input_ev_deactive(input_t *input)
[593e023]357{
358 active = false;
359 output_yield(output_sess);
[a35b458]360
[593e023]361 return EOK;
[429acb9]362}
[10569b1]363
[60ebe63]364static errno_t input_ev_key(input_t *input, unsigned kbd_id,
365 kbd_event_type_t type, keycode_t key, keymod_t mods, char32_t c)
[51c1b003]366{
[5d1ff11]367 cons_event_t event;
[49aaa0e]368 bool alt;
369 bool shift;
[5d1ff11]370
[49aaa0e]371 alt = (mods & KM_ALT) != 0 && (mods & (KM_CTRL | KM_SHIFT)) == 0;
372 shift = (mods & KM_SHIFT) != 0 && (mods & (KM_CTRL | KM_ALT)) == 0;
373
374 /* Switch console on Alt+Fn or Shift+Fn */
[593e023]375 if ((key >= KC_F1) && (key <= KC_F1 + CONSOLE_COUNT) &&
[49aaa0e]376 (alt || shift)) {
[593e023]377 cons_switch(key - KC_F1);
[111d2d6]378 } else {
379 /* Got key press/release event */
[5d1ff11]380 event.type = CEV_KEY;
[a35b458]381
[60ebe63]382 (void)kbd_id;
[5d1ff11]383 event.ev.key.type = type;
384 event.ev.key.key = key;
385 event.ev.key.mods = mods;
386 event.ev.key.c = c;
[a35b458]387
[5d1ff11]388 console_queue_cons_event(active_console, &event);
[7c014d1]389 }
[a35b458]390
[111d2d6]391 return EOK;
392}
393
[5d1ff11]394/** Update pointer position.
395 *
396 * @param new_x New X coordinate (in pixels)
397 * @param new_y New Y coordinate (in pixels)
398 */
399static void pointer_update(int new_x, int new_y)
400{
401 bool upd_pointer;
402
403 /* Make sure coordinates are in range */
404
405 if (new_x < 0)
406 new_x = 0;
407 if (new_x >= (int)cols * mouse_scale_x)
408 new_x = cols * mouse_scale_x - 1;
409 if (new_y < 0)
410 new_y = 0;
411 if (new_y >= (int)rows * mouse_scale_y)
412 new_y = rows * mouse_scale_y - 1;
413
414 /* Determine if pointer moved to a different character cell */
415 upd_pointer = (new_x / mouse_scale_x != pointer_x / mouse_scale_x) ||
416 (new_y / mouse_scale_y != pointer_y / mouse_scale_y);
417
418 if (upd_pointer)
419 pointer_undraw();
420
421 /* Store new pointer position */
422 pointer_x = new_x;
423 pointer_y = new_y;
424
425 if (upd_pointer) {
426 pointer_draw();
427 cons_update(active_console);
428 }
429}
430
[60ebe63]431static errno_t input_ev_move(input_t *input, unsigned pos_id, int dx, int dy)
[111d2d6]432{
[60ebe63]433 (void) pos_id;
[5d1ff11]434 pointer_update(pointer_x + dx, pointer_y + dy);
[111d2d6]435 return EOK;
436}
437
[60ebe63]438static errno_t input_ev_abs_move(input_t *input, unsigned pos_id, unsigned x,
439 unsigned y, unsigned max_x, unsigned max_y)
[111d2d6]440{
[60ebe63]441 (void)pos_id;
[5d1ff11]442 pointer_update(mouse_scale_x * cols * x / max_x, mouse_scale_y * rows * y / max_y);
[111d2d6]443 return EOK;
444}
445
[60ebe63]446static errno_t input_ev_button(input_t *input, unsigned pos_id, int bnum,
447 int bpress)
[111d2d6]448{
[5d1ff11]449 cons_event_t event;
450
[60ebe63]451 (void)pos_id;
452
[5d1ff11]453 event.type = CEV_POS;
454 event.ev.pos.type = bpress ? POS_PRESS : POS_RELEASE;
455 event.ev.pos.btn_num = bnum;
456 event.ev.pos.hpos = pointer_x / mouse_scale_x;
457 event.ev.pos.vpos = pointer_y / mouse_scale_y;
458
459 console_queue_cons_event(active_console, &event);
[111d2d6]460 return EOK;
[7c014d1]461}
[1875a0c]462
[60ebe63]463static errno_t input_ev_dclick(input_t *input, unsigned pos_id, int bnum)
[8edec53]464{
465 cons_event_t event;
466
[60ebe63]467 (void)pos_id;
468
[8edec53]469 event.type = CEV_POS;
470 event.ev.pos.type = POS_DCLICK;
471 event.ev.pos.btn_num = bnum;
472 event.ev.pos.hpos = pointer_x / mouse_scale_x;
473 event.ev.pos.vpos = pointer_y / mouse_scale_y;
474
475 console_queue_cons_event(active_console, &event);
476 return EOK;
477}
478
[7c014d1]479/** Process a character from the client (TTY emulation). */
[28a5ebd]480static void cons_write_char(console_t *cons, char32_t ch)
[7c014d1]481{
482 sysarg_t updated = 0;
[a35b458]483
[7c014d1]484 fibril_mutex_lock(&cons->mtx);
[5d1ff11]485 pointer_undraw();
[a35b458]486
[7c014d1]487 switch (ch) {
488 case '\n':
[6d5e378]489 updated = chargrid_newline(cons->frontbuf);
[7c014d1]490 break;
491 case '\r':
492 break;
493 case '\t':
[6d5e378]494 updated = chargrid_tabstop(cons->frontbuf, 8);
[7c014d1]495 break;
496 case '\b':
[6d5e378]497 updated = chargrid_backspace(cons->frontbuf);
[7c014d1]498 break;
499 default:
[28a5ebd]500 updated = chargrid_putuchar(cons->frontbuf, ch, true);
[9f51afc]501 }
[a35b458]502
[5d1ff11]503 pointer_draw();
[7c014d1]504 fibril_mutex_unlock(&cons->mtx);
[a35b458]505
[7c014d1]506 if (updated > 1)
507 cons_update(cons);
508}
509
[5d94b16c]510static void cons_set_cursor_vis(console_t *cons, bool visible)
[7c014d1]511{
512 fibril_mutex_lock(&cons->mtx);
[5d1ff11]513 pointer_undraw();
[6d5e378]514 chargrid_set_cursor_visibility(cons->frontbuf, visible);
[5d1ff11]515 pointer_draw();
[7c014d1]516 fibril_mutex_unlock(&cons->mtx);
[a35b458]517
[7c014d1]518 cons_update_cursor(cons);
519}
520
[b7fd2a0]521static errno_t cons_open(con_srvs_t *srvs, con_srv_t *srv)
[7c014d1]522{
[5d94b16c]523 return EOK;
[9f51afc]524}
525
[b7fd2a0]526static errno_t cons_close(con_srv_t *srv)
[d2cc7e1]527{
[5d94b16c]528 return EOK;
[424cd43]529}
530
[b7fd2a0]531static errno_t cons_read(con_srv_t *srv, void *buf, size_t size, size_t *nread)
[424cd43]532{
[5d94b16c]533 uint8_t *bbuf = buf;
534 console_t *cons = srv_to_console(srv);
[424cd43]535 size_t pos = 0;
[a35b458]536
[a58bc8b]537 /*
538 * Read input from keyboard and copy it to the buffer.
539 * We need to handle situation when wchar is split by 2 following
540 * reads.
541 */
[7c014d1]542 while (pos < size) {
[a58bc8b]543 /* Copy to the buffer remaining characters. */
544 while ((pos < size) && (cons->char_remains_len > 0)) {
[5d94b16c]545 bbuf[pos] = cons->char_remains[0];
[424cd43]546 pos++;
[a35b458]547
[a58bc8b]548 /* Unshift the array. */
[e435537]549 for (size_t i = 1; i < cons->char_remains_len; i++)
[a58bc8b]550 cons->char_remains[i - 1] = cons->char_remains[i];
[a35b458]551
[a58bc8b]552 cons->char_remains_len--;
553 }
[a35b458]554
[a58bc8b]555 /* Still not enough? Then get another key from the queue. */
556 if (pos < size) {
557 link_t *link = prodcons_consume(&cons->input_pc);
[5d1ff11]558 cons_event_t *event = list_get_instance(link,
559 cons_event_t, link);
[a35b458]560
[a58bc8b]561 /* Accept key presses of printable chars only. */
[5d1ff11]562 if (event->type == CEV_KEY && event->ev.key.type == KEY_PRESS &&
563 (event->ev.key.c != 0)) {
564 char32_t tmp[2] = { event->ev.key.c, 0 };
[a58bc8b]565 wstr_to_str(cons->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
566 cons->char_remains_len = str_size(cons->char_remains);
567 }
[a35b458]568
[a58bc8b]569 free(event);
[424cd43]570 }
571 }
[a35b458]572
[c8211849]573 *nread = size;
574 return EOK;
[5d94b16c]575}
576
[b7fd2a0]577static errno_t cons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
[5d94b16c]578{
579 console_t *cons = srv_to_console(srv);
580
581 size_t off = 0;
582 while (off < size)
583 cons_write_char(cons, str_decode(data, &off, size));
[a35b458]584
[c8211849]585 *nwritten = size;
586 return EOK;
[5d94b16c]587}
588
589static void cons_sync(con_srv_t *srv)
590{
591 console_t *cons = srv_to_console(srv);
[a35b458]592
[5d94b16c]593 cons_update(cons);
[424cd43]594}
595
[5d94b16c]596static void cons_clear(con_srv_t *srv)
[424cd43]597{
[5d94b16c]598 console_t *cons = srv_to_console(srv);
[a35b458]599
[5d94b16c]600 fibril_mutex_lock(&cons->mtx);
[5d1ff11]601 pointer_undraw();
[5d94b16c]602 chargrid_clear(cons->frontbuf);
[5d1ff11]603 pointer_draw();
[5d94b16c]604 fibril_mutex_unlock(&cons->mtx);
[a35b458]605
[5d94b16c]606 cons_update(cons);
607}
608
609static void cons_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
610{
611 console_t *cons = srv_to_console(srv);
[a35b458]612
[5d94b16c]613 fibril_mutex_lock(&cons->mtx);
[5d1ff11]614 pointer_undraw();
[5d94b16c]615 chargrid_set_cursor(cons->frontbuf, col, row);
[5d1ff11]616 pointer_draw();
[5d94b16c]617 fibril_mutex_unlock(&cons->mtx);
[a35b458]618
[5d94b16c]619 cons_update_cursor(cons);
620}
621
[b7fd2a0]622static errno_t cons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
[5d94b16c]623{
624 console_t *cons = srv_to_console(srv);
[a35b458]625
[5d94b16c]626 fibril_mutex_lock(&cons->mtx);
627 chargrid_get_cursor(cons->frontbuf, col, row);
628 fibril_mutex_unlock(&cons->mtx);
[a35b458]629
[5d94b16c]630 return EOK;
631}
632
[b7fd2a0]633static errno_t cons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
[5d94b16c]634{
635 console_t *cons = srv_to_console(srv);
[a35b458]636
[5d94b16c]637 fibril_mutex_lock(&cons->mtx);
638 *cols = cons->cols;
639 *rows = cons->rows;
640 fibril_mutex_unlock(&cons->mtx);
[a35b458]641
[5d94b16c]642 return EOK;
643}
644
[b7fd2a0]645static errno_t cons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
[5d94b16c]646{
647 console_t *cons = srv_to_console(srv);
[a35b458]648
[5d94b16c]649 fibril_mutex_lock(&cons->mtx);
650 *ccaps = cons->ccaps;
651 fibril_mutex_unlock(&cons->mtx);
[a35b458]652
[5d94b16c]653 return EOK;
654}
655
656static void cons_set_style(con_srv_t *srv, console_style_t style)
657{
658 console_t *cons = srv_to_console(srv);
[a35b458]659
[7c014d1]660 fibril_mutex_lock(&cons->mtx);
[6d5e378]661 chargrid_set_style(cons->frontbuf, style);
[7c014d1]662 fibril_mutex_unlock(&cons->mtx);
663}
664
[5d94b16c]665static void cons_set_color(con_srv_t *srv, console_color_t bgcolor,
[7c014d1]666 console_color_t fgcolor, console_color_attr_t attr)
667{
[5d94b16c]668 console_t *cons = srv_to_console(srv);
[a35b458]669
[7c014d1]670 fibril_mutex_lock(&cons->mtx);
[6d5e378]671 chargrid_set_color(cons->frontbuf, bgcolor, fgcolor, attr);
[7c014d1]672 fibril_mutex_unlock(&cons->mtx);
673}
674
[5d94b16c]675static void cons_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
[7c014d1]676 pixel_t fgcolor)
677{
[5d94b16c]678 console_t *cons = srv_to_console(srv);
[a35b458]679
[7c014d1]680 fibril_mutex_lock(&cons->mtx);
[6d5e378]681 chargrid_set_rgb_color(cons->frontbuf, bgcolor, fgcolor);
[7c014d1]682 fibril_mutex_unlock(&cons->mtx);
683}
684
[5d94b16c]685static void cons_set_cursor_visibility(con_srv_t *srv, bool visible)
686{
687 console_t *cons = srv_to_console(srv);
[a35b458]688
[5d94b16c]689 cons_set_cursor_vis(cons, visible);
690}
691
[b48e680f]692static errno_t cons_set_caption(con_srv_t *srv, const char *caption)
693{
694 console_t *cons = srv_to_console(srv);
695
696 (void) cons;
697 (void) caption;
698 return EOK;
699}
700
[b7fd2a0]701static errno_t cons_get_event(con_srv_t *srv, cons_event_t *event)
[7c014d1]702{
[5d94b16c]703 console_t *cons = srv_to_console(srv);
[7c014d1]704 link_t *link = prodcons_consume(&cons->input_pc);
[5d1ff11]705 cons_event_t *cevent = list_get_instance(link, cons_event_t, link);
[a35b458]706
[5d1ff11]707 *event = *cevent;
708 free(cevent);
[5d94b16c]709 return EOK;
[d2cc7e1]710}
711
[68a552f]712/** Create shared buffer for efficient rendering.
713 *
714 * @param srv Console server
715 * @param cols Number of columns in buffer
716 * @param rows Number of rows in buffer
717 * @param rbuf Place to store pointer to new sharable buffer
718 *
719 * @return EOK on sucess or an error code
720 */
721static errno_t cons_map(con_srv_t *srv, sysarg_t cols, sysarg_t rows,
722 charfield_t **rbuf)
723{
724 console_t *cons = srv_to_console(srv);
725 void *buf;
726
727 fibril_mutex_lock(&cons->mtx);
728
729 if (cons->ubuf != NULL) {
730 fibril_mutex_unlock(&cons->mtx);
731 return EBUSY;
732 }
733
734 buf = as_area_create(AS_AREA_ANY, cols * rows * sizeof(charfield_t),
735 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
736 if (buf == AS_MAP_FAILED) {
737 fibril_mutex_unlock(&cons->mtx);
738 return ENOMEM;
739 }
740
741 cons->ucols = cols;
742 cons->urows = rows;
743 cons->ubuf = buf;
744 fibril_mutex_unlock(&cons->mtx);
745
746 *rbuf = buf;
747 return EOK;
748}
749
750/** Delete shared buffer.
751 *
752 * @param srv Console server
753 */
754static void cons_unmap(con_srv_t *srv)
755{
756 console_t *cons = srv_to_console(srv);
757 void *buf;
758
759 fibril_mutex_lock(&cons->mtx);
760
761 buf = cons->ubuf;
762 cons->ubuf = NULL;
763
764 if (buf != NULL)
765 as_area_destroy(buf);
766
767 fibril_mutex_unlock(&cons->mtx);
768}
769
770/** Update area of console from shared buffer.
771 *
772 * @param srv Console server
773 * @param c0 Column coordinate of top-left corner (inclusive)
774 * @param r0 Row coordinate of top-left corner (inclusive)
775 * @param c1 Column coordinate of bottom-right corner (exclusive)
776 * @param r1 Row coordinate of bottom-right corner (exclusive)
777 */
778static void cons_buf_update(con_srv_t *srv, sysarg_t c0, sysarg_t r0,
779 sysarg_t c1, sysarg_t r1)
780{
781 console_t *cons = srv_to_console(srv);
782 charfield_t *ch;
783 sysarg_t col, row;
784
785 fibril_mutex_lock(&cons->mtx);
786
787 if (cons->ubuf == NULL) {
788 fibril_mutex_unlock(&cons->mtx);
789 return;
790 }
791
792 /* Make sure we have meaningful coordinates, within bounds */
793
794 if (c1 > cons->ucols)
795 c1 = cons->ucols;
796 if (c1 > cons->cols)
797 c1 = cons->cols;
798 if (c0 >= c1) {
799 fibril_mutex_unlock(&cons->mtx);
800 return;
801 }
802 if (r1 > cons->urows)
803 r1 = cons->urows;
804 if (r1 > cons->rows)
805 r1 = cons->rows;
806 if (r0 >= r1) {
807 fibril_mutex_unlock(&cons->mtx);
808 return;
809 }
810
811 /* Update front buffer from user buffer */
812
[5d1ff11]813 pointer_undraw();
814
[68a552f]815 for (row = r0; row < r1; row++) {
816 for (col = c0; col < c1; col++) {
817 ch = chargrid_charfield_at(cons->frontbuf, col, row);
818 *ch = cons->ubuf[row * cons->ucols + col];
819 }
820 }
821
[5d1ff11]822 pointer_draw();
[68a552f]823 fibril_mutex_unlock(&cons->mtx);
824
825 /* Update console */
826 cons_update(cons);
827}
828
[984a9ba]829static void client_connection(ipc_call_t *icall, void *arg)
[eaf34f7]830{
[424cd43]831 console_t *cons = NULL;
[a35b458]832
[7c014d1]833 for (size_t i = 0; i < CONSOLE_COUNT; i++) {
[fafb8e5]834 if (consoles[i].dsid == (service_id_t) ipc_get_arg2(icall)) {
[424cd43]835 cons = &consoles[i];
836 break;
837 }
838 }
[a35b458]839
[424cd43]840 if (cons == NULL) {
[984a9ba]841 async_answer_0(icall, ENOENT);
[eaf34f7]842 return;
843 }
[a35b458]844
[508b0df1]845 if (!atomic_flag_test_and_set(&cons->refcnt))
[5d94b16c]846 cons_set_cursor_vis(cons, true);
[a35b458]847
[984a9ba]848 con_conn(icall, &cons->srvs);
[eaf34f7]849}
850
[b7fd2a0]851static errno_t input_connect(const char *svc)
[eaf34f7]852{
[a40dea3]853 async_sess_t *sess;
[7c014d1]854 service_id_t dsid;
[a35b458]855
[b7fd2a0]856 errno_t rc = loc_service_get_id(svc, &dsid, 0);
[111d2d6]857 if (rc != EOK) {
858 printf("%s: Input service %s not found\n", NAME, svc);
859 return rc;
860 }
861
[f9b2cb4c]862 sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
[111d2d6]863 if (sess == NULL) {
864 printf("%s: Unable to connect to input service %s\n", NAME,
865 svc);
866 return EIO;
867 }
[a35b458]868
[111d2d6]869 rc = input_open(sess, &input_ev_ops, NULL, &input);
[700af62]870 if (rc != EOK) {
[a40dea3]871 async_hangup(sess);
[111d2d6]872 printf("%s: Unable to communicate with service %s (%s)\n",
[7c014d1]873 NAME, svc, str_error(rc));
[111d2d6]874 return rc;
[4904de8]875 }
[a35b458]876
[111d2d6]877 return EOK;
[700af62]878}
879
[6d5e378]880static async_sess_t *output_connect(const char *svc)
[700af62]881{
[7c014d1]882 async_sess_t *sess;
883 service_id_t dsid;
[a35b458]884
[b7fd2a0]885 errno_t rc = loc_service_get_id(svc, &dsid, 0);
[7c014d1]886 if (rc == EOK) {
[f9b2cb4c]887 sess = loc_service_connect(dsid, INTERFACE_OUTPUT, 0);
[7c014d1]888 if (sess == NULL) {
[6d5e378]889 printf("%s: Unable to connect to output service %s\n",
[7c014d1]890 NAME, svc);
891 return NULL;
892 }
893 } else
894 return NULL;
[a35b458]895
[7c014d1]896 return sess;
897}
898
[6d5e378]899static bool console_srv_init(char *input_svc, char *output_svc)
[7c014d1]900{
901 /* Connect to input service */
[b7fd2a0]902 errno_t rc = input_connect(input_svc);
[111d2d6]903 if (rc != EOK)
[700af62]904 return false;
[a35b458]905
[6d5e378]906 /* Connect to output service */
907 output_sess = output_connect(output_svc);
908 if (output_sess == NULL)
[79ae36dd]909 return false;
[a35b458]910
[15f3c3f]911 /* Register server */
[b688fd8]912 async_set_fallback_port_handler(client_connection, NULL);
[4c6fd56]913 rc = loc_server_register(NAME, &console_srv);
[2f90b46]914 if (rc != EOK) {
[7c014d1]915 printf("%s: Unable to register server (%s)\n", NAME,
916 str_error(rc));
[424cd43]917 return false;
918 }
[a35b458]919
[6d5e378]920 output_get_dimensions(output_sess, &cols, &rows);
921 output_set_style(output_sess, STYLE_NORMAL);
[a35b458]922
[7c014d1]923 console_caps_t ccaps;
[6d5e378]924 output_get_caps(output_sess, &ccaps);
[a35b458]925
[593e023]926 /*
927 * Inititalize consoles only if there are
928 * actually some output devices.
929 */
930 if (ccaps != 0) {
931 for (size_t i = 0; i < CONSOLE_COUNT; i++) {
932 consoles[i].index = i;
[508b0df1]933 atomic_flag_clear(&consoles[i].refcnt);
[593e023]934 fibril_mutex_initialize(&consoles[i].mtx);
935 prodcons_initialize(&consoles[i].input_pc);
936 consoles[i].char_remains_len = 0;
[a35b458]937
[593e023]938 consoles[i].cols = cols;
939 consoles[i].rows = rows;
940 consoles[i].ccaps = ccaps;
941 consoles[i].frontbuf =
942 chargrid_create(cols, rows, CHARGRID_FLAG_SHARED);
[a35b458]943
[593e023]944 if (consoles[i].frontbuf == NULL) {
945 printf("%s: Unable to allocate frontbuffer %zu\n", NAME, i);
946 return false;
947 }
[a35b458]948
[593e023]949 consoles[i].fbid = output_frontbuf_create(output_sess,
950 consoles[i].frontbuf);
951 if (consoles[i].fbid == 0) {
952 printf("%s: Unable to create frontbuffer %zu\n", NAME, i);
953 return false;
954 }
[a35b458]955
[593e023]956 con_srvs_init(&consoles[i].srvs);
957 consoles[i].srvs.ops = &con_ops;
958 consoles[i].srvs.sarg = &consoles[i];
[a35b458]959
[593e023]960 char vc[LOC_NAME_MAXLEN + 1];
961 snprintf(vc, LOC_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
[a35b458]962
[4c6fd56]963 if (loc_service_register(console_srv, vc,
964 &consoles[i].dsid) != EOK) {
[593e023]965 printf("%s: Unable to register device %s\n", NAME, vc);
966 return false;
967 }
[7c014d1]968 }
[a35b458]969
[593e023]970 input_activate(input);
[0350033]971 active = true;
972 cons_damage(active_console);
[424cd43]973 }
[a35b458]974
[424cd43]975 return true;
976}
977
[6d5e378]978static void usage(char *name)
[47a350f]979{
[6d5e378]980 printf("Usage: %s <input_dev> <output_dev>\n", name);
[47a350f]981}
982
[424cd43]983int main(int argc, char *argv[])
984{
[7c014d1]985 if (argc < 3) {
[6d5e378]986 usage(argv[0]);
[47a350f]987 return -1;
988 }
[a35b458]989
[7c014d1]990 printf("%s: HelenOS Console service\n", NAME);
[a35b458]991
[7c014d1]992 if (!console_srv_init(argv[1], argv[2]))
[424cd43]993 return -1;
[a35b458]994
[7c014d1]995 printf("%s: Accepting connections\n", NAME);
996 task_retval(0);
[eaf34f7]997 async_manager();
[a35b458]998
[6d5e378]999 /* Never reached */
[3ad953c]1000 return 0;
[51c1b003]1001}
[516ff92]1002
[ce5bcb4]1003/** @}
1004 */
Note: See TracBrowser for help on using the repository browser.