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

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ce862ac was 8edec53, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Support double-click

Needed to open Navigator entries using the mouse.

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