source: mainline/uspace/srv/hid/console/console.c@ 4c6fd56

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

loc_server_register() should be callable more than once (API only)

Now loc_server_register() returns a pointer to a loc_srv_t object,
that is then passed to loc_service_register() and
loc_service_add_to_cat().

Added loc_server_unregister() that unregisters the server
and frees the loc_srv_t object.

Updated all callers. The implementation, however, is a stub.
It is not actually possible to call loc_server_register() more
than once, yet.

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