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
Line 
1/*
2 * Copyright (c) 2021 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
91/** Input server proxy */
92static input_t *input;
93static bool active = false;
94
95/** Session to the output server */
96static async_sess_t *output_sess;
97
98/** Output dimensions */
99static sysarg_t cols;
100static sysarg_t rows;
101
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
112/** Array of data for virtual consoles */
113static console_t consoles[CONSOLE_COUNT];
114
115/** Mutex for console switching */
116static FIBRIL_MUTEX_INITIALIZE(switch_mtx);
117
118static console_t *active_console = &consoles[0];
119
120static errno_t input_ev_active(input_t *);
121static errno_t input_ev_deactive(input_t *);
122static errno_t input_ev_key(input_t *, kbd_event_type_t, keycode_t, keymod_t, char32_t);
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);
126static errno_t input_ev_dclick(input_t *, int);
127
128static input_ev_ops_t input_ev_ops = {
129 .active = input_ev_active,
130 .deactive = input_ev_deactive,
131 .key = input_ev_key,
132 .move = input_ev_move,
133 .abs_move = input_ev_abs_move,
134 .button = input_ev_button,
135 .dclick = input_ev_dclick
136};
137
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 *);
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);
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 *);
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);
153static errno_t cons_get_event(con_srv_t *, cons_event_t *);
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);
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,
174 .get_event = cons_get_event,
175 .map = cons_map,
176 .unmap = cons_unmap,
177 .update = cons_buf_update
178};
179
180static void pointer_draw(void);
181static void pointer_undraw(void);
182
183static console_t *srv_to_console(con_srv_t *srv)
184{
185 return srv->srvs->sarg;
186}
187
188static void cons_update(console_t *cons)
189{
190 fibril_mutex_lock(&switch_mtx);
191 fibril_mutex_lock(&cons->mtx);
192
193 if ((active) && (cons == active_console)) {
194 output_update(output_sess, cons->fbid);
195 output_cursor_update(output_sess, cons->fbid);
196 }
197
198 fibril_mutex_unlock(&cons->mtx);
199 fibril_mutex_unlock(&switch_mtx);
200}
201
202static void cons_update_cursor(console_t *cons)
203{
204 fibril_mutex_lock(&switch_mtx);
205 fibril_mutex_lock(&cons->mtx);
206
207 if ((active) && (cons == active_console))
208 output_cursor_update(output_sess, cons->fbid);
209
210 fibril_mutex_unlock(&cons->mtx);
211 fibril_mutex_unlock(&switch_mtx);
212}
213
214static void cons_damage(console_t *cons)
215{
216 fibril_mutex_lock(&switch_mtx);
217 fibril_mutex_lock(&cons->mtx);
218
219 if ((active) && (cons == active_console)) {
220 output_damage(output_sess, cons->fbid, 0, 0, cons->cols,
221 cons->rows);
222 output_cursor_update(output_sess, cons->fbid);
223 }
224
225 fibril_mutex_unlock(&cons->mtx);
226 fibril_mutex_unlock(&switch_mtx);
227}
228
229static void cons_switch(unsigned int index)
230{
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;
238
239 return;
240 }
241
242 if (index > CONSOLE_COUNT)
243 return;
244
245 console_t *cons = &consoles[index];
246
247 fibril_mutex_lock(&switch_mtx);
248 pointer_undraw();
249
250 if (cons == active_console) {
251 fibril_mutex_unlock(&switch_mtx);
252 return;
253 }
254
255 active_console = cons;
256
257 pointer_draw();
258 fibril_mutex_unlock(&switch_mtx);
259
260 cons_damage(cons);
261}
262
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
340static errno_t input_ev_active(input_t *input)
341{
342 active = true;
343 output_claim(output_sess);
344 cons_damage(active_console);
345
346 return EOK;
347}
348
349static errno_t input_ev_deactive(input_t *input)
350{
351 active = false;
352 output_yield(output_sess);
353
354 return EOK;
355}
356
357static errno_t input_ev_key(input_t *input, kbd_event_type_t type, keycode_t key,
358 keymod_t mods, char32_t c)
359{
360 cons_event_t event;
361
362 if ((key >= KC_F1) && (key <= KC_F1 + CONSOLE_COUNT) &&
363 ((mods & KM_CTRL) == 0)) {
364 cons_switch(key - KC_F1);
365 } else {
366 /* Got key press/release event */
367 event.type = CEV_KEY;
368
369 event.ev.key.type = type;
370 event.ev.key.key = key;
371 event.ev.key.mods = mods;
372 event.ev.key.c = c;
373
374 console_queue_cons_event(active_console, &event);
375 }
376
377 return EOK;
378}
379
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
417static errno_t input_ev_move(input_t *input, int dx, int dy)
418{
419 pointer_update(pointer_x + dx, pointer_y + dy);
420 return EOK;
421}
422
423static errno_t input_ev_abs_move(input_t *input, unsigned x, unsigned y,
424 unsigned max_x, unsigned max_y)
425{
426 pointer_update(mouse_scale_x * cols * x / max_x, mouse_scale_y * rows * y / max_y);
427 return EOK;
428}
429
430static errno_t input_ev_button(input_t *input, int bnum, int bpress)
431{
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);
441 return EOK;
442}
443
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
458/** Process a character from the client (TTY emulation). */
459static void cons_write_char(console_t *cons, char32_t ch)
460{
461 sysarg_t updated = 0;
462
463 fibril_mutex_lock(&cons->mtx);
464 pointer_undraw();
465
466 switch (ch) {
467 case '\n':
468 updated = chargrid_newline(cons->frontbuf);
469 break;
470 case '\r':
471 break;
472 case '\t':
473 updated = chargrid_tabstop(cons->frontbuf, 8);
474 break;
475 case '\b':
476 updated = chargrid_backspace(cons->frontbuf);
477 break;
478 default:
479 updated = chargrid_putuchar(cons->frontbuf, ch, true);
480 }
481
482 pointer_draw();
483 fibril_mutex_unlock(&cons->mtx);
484
485 if (updated > 1)
486 cons_update(cons);
487}
488
489static void cons_set_cursor_vis(console_t *cons, bool visible)
490{
491 fibril_mutex_lock(&cons->mtx);
492 pointer_undraw();
493 chargrid_set_cursor_visibility(cons->frontbuf, visible);
494 pointer_draw();
495 fibril_mutex_unlock(&cons->mtx);
496
497 cons_update_cursor(cons);
498}
499
500static errno_t cons_open(con_srvs_t *srvs, con_srv_t *srv)
501{
502 return EOK;
503}
504
505static errno_t cons_close(con_srv_t *srv)
506{
507 return EOK;
508}
509
510static errno_t cons_read(con_srv_t *srv, void *buf, size_t size, size_t *nread)
511{
512 uint8_t *bbuf = buf;
513 console_t *cons = srv_to_console(srv);
514 size_t pos = 0;
515
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 */
521 while (pos < size) {
522 /* Copy to the buffer remaining characters. */
523 while ((pos < size) && (cons->char_remains_len > 0)) {
524 bbuf[pos] = cons->char_remains[0];
525 pos++;
526
527 /* Unshift the array. */
528 for (size_t i = 1; i < cons->char_remains_len; i++)
529 cons->char_remains[i - 1] = cons->char_remains[i];
530
531 cons->char_remains_len--;
532 }
533
534 /* Still not enough? Then get another key from the queue. */
535 if (pos < size) {
536 link_t *link = prodcons_consume(&cons->input_pc);
537 cons_event_t *event = list_get_instance(link,
538 cons_event_t, link);
539
540 /* Accept key presses of printable chars only. */
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 };
544 wstr_to_str(cons->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
545 cons->char_remains_len = str_size(cons->char_remains);
546 }
547
548 free(event);
549 }
550 }
551
552 *nread = size;
553 return EOK;
554}
555
556static errno_t cons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
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));
563
564 *nwritten = size;
565 return EOK;
566}
567
568static void cons_sync(con_srv_t *srv)
569{
570 console_t *cons = srv_to_console(srv);
571
572 cons_update(cons);
573}
574
575static void cons_clear(con_srv_t *srv)
576{
577 console_t *cons = srv_to_console(srv);
578
579 fibril_mutex_lock(&cons->mtx);
580 pointer_undraw();
581 chargrid_clear(cons->frontbuf);
582 pointer_draw();
583 fibril_mutex_unlock(&cons->mtx);
584
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);
591
592 fibril_mutex_lock(&cons->mtx);
593 pointer_undraw();
594 chargrid_set_cursor(cons->frontbuf, col, row);
595 pointer_draw();
596 fibril_mutex_unlock(&cons->mtx);
597
598 cons_update_cursor(cons);
599}
600
601static errno_t cons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
602{
603 console_t *cons = srv_to_console(srv);
604
605 fibril_mutex_lock(&cons->mtx);
606 chargrid_get_cursor(cons->frontbuf, col, row);
607 fibril_mutex_unlock(&cons->mtx);
608
609 return EOK;
610}
611
612static errno_t cons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
613{
614 console_t *cons = srv_to_console(srv);
615
616 fibril_mutex_lock(&cons->mtx);
617 *cols = cons->cols;
618 *rows = cons->rows;
619 fibril_mutex_unlock(&cons->mtx);
620
621 return EOK;
622}
623
624static errno_t cons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
625{
626 console_t *cons = srv_to_console(srv);
627
628 fibril_mutex_lock(&cons->mtx);
629 *ccaps = cons->ccaps;
630 fibril_mutex_unlock(&cons->mtx);
631
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);
638
639 fibril_mutex_lock(&cons->mtx);
640 chargrid_set_style(cons->frontbuf, style);
641 fibril_mutex_unlock(&cons->mtx);
642}
643
644static void cons_set_color(con_srv_t *srv, console_color_t bgcolor,
645 console_color_t fgcolor, console_color_attr_t attr)
646{
647 console_t *cons = srv_to_console(srv);
648
649 fibril_mutex_lock(&cons->mtx);
650 chargrid_set_color(cons->frontbuf, bgcolor, fgcolor, attr);
651 fibril_mutex_unlock(&cons->mtx);
652}
653
654static void cons_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
655 pixel_t fgcolor)
656{
657 console_t *cons = srv_to_console(srv);
658
659 fibril_mutex_lock(&cons->mtx);
660 chargrid_set_rgb_color(cons->frontbuf, bgcolor, fgcolor);
661 fibril_mutex_unlock(&cons->mtx);
662}
663
664static void cons_set_cursor_visibility(con_srv_t *srv, bool visible)
665{
666 console_t *cons = srv_to_console(srv);
667
668 cons_set_cursor_vis(cons, visible);
669}
670
671static errno_t cons_get_event(con_srv_t *srv, cons_event_t *event)
672{
673 console_t *cons = srv_to_console(srv);
674 link_t *link = prodcons_consume(&cons->input_pc);
675 cons_event_t *cevent = list_get_instance(link, cons_event_t, link);
676
677 *event = *cevent;
678 free(cevent);
679 return EOK;
680}
681
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
783 pointer_undraw();
784
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
792 pointer_draw();
793 fibril_mutex_unlock(&cons->mtx);
794
795 /* Update console */
796 cons_update(cons);
797}
798
799static void client_connection(ipc_call_t *icall, void *arg)
800{
801 console_t *cons = NULL;
802
803 for (size_t i = 0; i < CONSOLE_COUNT; i++) {
804 if (consoles[i].dsid == (service_id_t) ipc_get_arg2(icall)) {
805 cons = &consoles[i];
806 break;
807 }
808 }
809
810 if (cons == NULL) {
811 async_answer_0(icall, ENOENT);
812 return;
813 }
814
815 if (!atomic_flag_test_and_set(&cons->refcnt))
816 cons_set_cursor_vis(cons, true);
817
818 con_conn(icall, &cons->srvs);
819}
820
821static errno_t input_connect(const char *svc)
822{
823 async_sess_t *sess;
824 service_id_t dsid;
825
826 errno_t rc = loc_service_get_id(svc, &dsid, 0);
827 if (rc != EOK) {
828 printf("%s: Input service %s not found\n", NAME, svc);
829 return rc;
830 }
831
832 sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
833 if (sess == NULL) {
834 printf("%s: Unable to connect to input service %s\n", NAME,
835 svc);
836 return EIO;
837 }
838
839 rc = input_open(sess, &input_ev_ops, NULL, &input);
840 if (rc != EOK) {
841 async_hangup(sess);
842 printf("%s: Unable to communicate with service %s (%s)\n",
843 NAME, svc, str_error(rc));
844 return rc;
845 }
846
847 return EOK;
848}
849
850static async_sess_t *output_connect(const char *svc)
851{
852 async_sess_t *sess;
853 service_id_t dsid;
854
855 errno_t rc = loc_service_get_id(svc, &dsid, 0);
856 if (rc == EOK) {
857 sess = loc_service_connect(dsid, INTERFACE_OUTPUT, 0);
858 if (sess == NULL) {
859 printf("%s: Unable to connect to output service %s\n",
860 NAME, svc);
861 return NULL;
862 }
863 } else
864 return NULL;
865
866 return sess;
867}
868
869static bool console_srv_init(char *input_svc, char *output_svc)
870{
871 /* Connect to input service */
872 errno_t rc = input_connect(input_svc);
873 if (rc != EOK)
874 return false;
875
876 /* Connect to output service */
877 output_sess = output_connect(output_svc);
878 if (output_sess == NULL)
879 return false;
880
881 /* Register server */
882 async_set_fallback_port_handler(client_connection, NULL);
883 rc = loc_server_register(NAME);
884 if (rc != EOK) {
885 printf("%s: Unable to register server (%s)\n", NAME,
886 str_error(rc));
887 return false;
888 }
889
890 output_get_dimensions(output_sess, &cols, &rows);
891 output_set_style(output_sess, STYLE_NORMAL);
892
893 console_caps_t ccaps;
894 output_get_caps(output_sess, &ccaps);
895
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;
903 atomic_flag_clear(&consoles[i].refcnt);
904 fibril_mutex_initialize(&consoles[i].mtx);
905 prodcons_initialize(&consoles[i].input_pc);
906 consoles[i].char_remains_len = 0;
907
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);
913
914 if (consoles[i].frontbuf == NULL) {
915 printf("%s: Unable to allocate frontbuffer %zu\n", NAME, i);
916 return false;
917 }
918
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 }
925
926 con_srvs_init(&consoles[i].srvs);
927 consoles[i].srvs.ops = &con_ops;
928 consoles[i].srvs.sarg = &consoles[i];
929
930 char vc[LOC_NAME_MAXLEN + 1];
931 snprintf(vc, LOC_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
932
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 }
937 }
938
939 input_activate(input);
940 active = true;
941 cons_damage(active_console);
942 }
943
944 return true;
945}
946
947static void usage(char *name)
948{
949 printf("Usage: %s <input_dev> <output_dev>\n", name);
950}
951
952int main(int argc, char *argv[])
953{
954 if (argc < 3) {
955 usage(argv[0]);
956 return -1;
957 }
958
959 printf("%s: HelenOS Console service\n", NAME);
960
961 if (!console_srv_init(argv[1], argv[2]))
962 return -1;
963
964 printf("%s: Accepting connections\n", NAME);
965 task_retval(0);
966 async_manager();
967
968 /* Never reached */
969 return 0;
970}
971
972/** @}
973 */
Note: See TracBrowser for help on using the repository browser.