source: mainline/uspace/srv/hid/console/console.c@ 1433ecda

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 16.2 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup console
30 * @{
31 */
32/** @file
33 */
34
35#include <async.h>
36#include <stdio.h>
37#include <adt/prodcons.h>
38#include <io/input.h>
39#include <ipc/vfs.h>
40#include <errno.h>
41#include <str_error.h>
42#include <loc.h>
43#include <io/con_srv.h>
44#include <io/kbd_event.h>
45#include <io/keycode.h>
46#include <io/chargrid.h>
47#include <io/output.h>
48#include <align.h>
49#include <as.h>
50#include <task.h>
51#include <fibril_synch.h>
52#include <stdlib.h>
53#include <str.h>
54#include "console.h"
55
56#define NAME "console"
57#define NAMESPACE "term"
58
59#define UTF8_CHAR_BUFFER_SIZE (STR_BOUNDS(1) + 1)
60
61typedef struct {
62 atomic_t refcnt; /**< Connection reference count */
63 prodcons_t input_pc; /**< Incoming keyboard events */
64
65 /**
66 * Not yet sent bytes of last char event.
67 */
68 char char_remains[UTF8_CHAR_BUFFER_SIZE];
69 size_t char_remains_len; /**< Number of not yet sent bytes. */
70
71 fibril_mutex_t mtx; /**< Lock protecting mutable fields */
72
73 size_t index; /**< Console index */
74 service_id_t dsid; /**< Service handle */
75
76 sysarg_t cols; /**< Number of columns */
77 sysarg_t rows; /**< Number of rows */
78 console_caps_t ccaps; /**< Console capabilities */
79
80 chargrid_t *frontbuf; /**< Front buffer */
81 frontbuf_handle_t fbid; /**< Front buffer handle */
82 con_srvs_t srvs; /**< Console service setup */
83} console_t;
84
85/** Input server proxy */
86static input_t *input;
87static bool active = false;
88
89/** Session to the output server */
90static async_sess_t *output_sess;
91
92/** Output dimensions */
93static sysarg_t cols;
94static sysarg_t rows;
95
96/** Array of data for virtual consoles */
97static console_t consoles[CONSOLE_COUNT];
98
99/** Mutex for console switching */
100static FIBRIL_MUTEX_INITIALIZE(switch_mtx);
101
102static console_t *active_console = &consoles[0];
103
104static errno_t input_ev_active(input_t *);
105static errno_t input_ev_deactive(input_t *);
106static errno_t input_ev_key(input_t *, kbd_event_type_t, keycode_t, keymod_t, wchar_t);
107static errno_t input_ev_move(input_t *, int, int);
108static errno_t input_ev_abs_move(input_t *, unsigned, unsigned, unsigned, unsigned);
109static errno_t input_ev_button(input_t *, int, int);
110
111static input_ev_ops_t input_ev_ops = {
112 .active = input_ev_active,
113 .deactive = input_ev_deactive,
114 .key = input_ev_key,
115 .move = input_ev_move,
116 .abs_move = input_ev_abs_move,
117 .button = input_ev_button
118};
119
120static errno_t cons_open(con_srvs_t *, con_srv_t *);
121static errno_t cons_close(con_srv_t *);
122static errno_t cons_read(con_srv_t *, void *, size_t, size_t *);
123static errno_t cons_write(con_srv_t *, void *, size_t, size_t *);
124static void cons_sync(con_srv_t *);
125static void cons_clear(con_srv_t *);
126static void cons_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
127static errno_t cons_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
128static errno_t cons_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
129static errno_t cons_get_color_cap(con_srv_t *, console_caps_t *);
130static void cons_set_style(con_srv_t *, console_style_t);
131static void cons_set_color(con_srv_t *, console_color_t, console_color_t,
132 console_color_attr_t);
133static void cons_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
134static void cons_set_cursor_visibility(con_srv_t *, bool);
135static errno_t cons_get_event(con_srv_t *, cons_event_t *);
136
137static con_ops_t con_ops = {
138 .open = cons_open,
139 .close = cons_close,
140 .read = cons_read,
141 .write = cons_write,
142 .sync = cons_sync,
143 .clear = cons_clear,
144 .set_pos = cons_set_pos,
145 .get_pos = cons_get_pos,
146 .get_size = cons_get_size,
147 .get_color_cap = cons_get_color_cap,
148 .set_style = cons_set_style,
149 .set_color = cons_set_color,
150 .set_rgb_color = cons_set_rgb_color,
151 .set_cursor_visibility = cons_set_cursor_visibility,
152 .get_event = cons_get_event
153};
154
155static console_t *srv_to_console(con_srv_t *srv)
156{
157 return srv->srvs->sarg;
158}
159
160static void cons_update(console_t *cons)
161{
162 fibril_mutex_lock(&switch_mtx);
163 fibril_mutex_lock(&cons->mtx);
164
165 if ((active) && (cons == active_console)) {
166 output_update(output_sess, cons->fbid);
167 output_cursor_update(output_sess, cons->fbid);
168 }
169
170 fibril_mutex_unlock(&cons->mtx);
171 fibril_mutex_unlock(&switch_mtx);
172}
173
174static void cons_update_cursor(console_t *cons)
175{
176 fibril_mutex_lock(&switch_mtx);
177 fibril_mutex_lock(&cons->mtx);
178
179 if ((active) && (cons == active_console))
180 output_cursor_update(output_sess, cons->fbid);
181
182 fibril_mutex_unlock(&cons->mtx);
183 fibril_mutex_unlock(&switch_mtx);
184}
185
186static void cons_damage(console_t *cons)
187{
188 fibril_mutex_lock(&switch_mtx);
189 fibril_mutex_lock(&cons->mtx);
190
191 if ((active) && (cons == active_console)) {
192 output_damage(output_sess, cons->fbid, 0, 0, cons->cols,
193 cons->rows);
194 output_cursor_update(output_sess, cons->fbid);
195 }
196
197 fibril_mutex_unlock(&cons->mtx);
198 fibril_mutex_unlock(&switch_mtx);
199}
200
201static void cons_switch(unsigned int index)
202{
203 /*
204 * The first undefined index is reserved
205 * for switching to the kernel console.
206 */
207 if (index == CONSOLE_COUNT) {
208 if (console_kcon())
209 active = false;
210
211 return;
212 }
213
214 if (index > CONSOLE_COUNT)
215 return;
216
217 console_t *cons = &consoles[index];
218
219 fibril_mutex_lock(&switch_mtx);
220
221 if (cons == active_console) {
222 fibril_mutex_unlock(&switch_mtx);
223 return;
224 }
225
226 active_console = cons;
227
228 fibril_mutex_unlock(&switch_mtx);
229
230 cons_damage(cons);
231}
232
233static errno_t input_ev_active(input_t *input)
234{
235 active = true;
236 output_claim(output_sess);
237 cons_damage(active_console);
238
239 return EOK;
240}
241
242static errno_t input_ev_deactive(input_t *input)
243{
244 active = false;
245 output_yield(output_sess);
246
247 return EOK;
248}
249
250static errno_t input_ev_key(input_t *input, kbd_event_type_t type, keycode_t key,
251 keymod_t mods, wchar_t c)
252{
253 if ((key >= KC_F1) && (key <= KC_F1 + CONSOLE_COUNT) &&
254 ((mods & KM_CTRL) == 0)) {
255 cons_switch(key - KC_F1);
256 } else {
257 /* Got key press/release event */
258 kbd_event_t *event =
259 (kbd_event_t *) malloc(sizeof(kbd_event_t));
260 if (event == NULL) {
261 return ENOMEM;
262 }
263
264 link_initialize(&event->link);
265 event->type = type;
266 event->key = key;
267 event->mods = mods;
268 event->c = c;
269
270 prodcons_produce(&active_console->input_pc,
271 &event->link);
272 }
273
274 return EOK;
275}
276
277static errno_t input_ev_move(input_t *input, int dx, int dy)
278{
279 return EOK;
280}
281
282static errno_t input_ev_abs_move(input_t *input, unsigned x, unsigned y,
283 unsigned max_x, unsigned max_y)
284{
285 return EOK;
286}
287
288static errno_t input_ev_button(input_t *input, int bnum, int bpress)
289{
290 return EOK;
291}
292
293/** Process a character from the client (TTY emulation). */
294static void cons_write_char(console_t *cons, wchar_t ch)
295{
296 sysarg_t updated = 0;
297
298 fibril_mutex_lock(&cons->mtx);
299
300 switch (ch) {
301 case '\n':
302 updated = chargrid_newline(cons->frontbuf);
303 break;
304 case '\r':
305 break;
306 case '\t':
307 updated = chargrid_tabstop(cons->frontbuf, 8);
308 break;
309 case '\b':
310 updated = chargrid_backspace(cons->frontbuf);
311 break;
312 default:
313 updated = chargrid_putchar(cons->frontbuf, ch, true);
314 }
315
316 fibril_mutex_unlock(&cons->mtx);
317
318 if (updated > 1)
319 cons_update(cons);
320}
321
322static void cons_set_cursor_vis(console_t *cons, bool visible)
323{
324 fibril_mutex_lock(&cons->mtx);
325 chargrid_set_cursor_visibility(cons->frontbuf, visible);
326 fibril_mutex_unlock(&cons->mtx);
327
328 cons_update_cursor(cons);
329}
330
331static errno_t cons_open(con_srvs_t *srvs, con_srv_t *srv)
332{
333 return EOK;
334}
335
336static errno_t cons_close(con_srv_t *srv)
337{
338 return EOK;
339}
340
341static errno_t cons_read(con_srv_t *srv, void *buf, size_t size, size_t *nread)
342{
343 uint8_t *bbuf = buf;
344 console_t *cons = srv_to_console(srv);
345 size_t pos = 0;
346
347 /*
348 * Read input from keyboard and copy it to the buffer.
349 * We need to handle situation when wchar is split by 2 following
350 * reads.
351 */
352 while (pos < size) {
353 /* Copy to the buffer remaining characters. */
354 while ((pos < size) && (cons->char_remains_len > 0)) {
355 bbuf[pos] = cons->char_remains[0];
356 pos++;
357
358 /* Unshift the array. */
359 for (size_t i = 1; i < cons->char_remains_len; i++)
360 cons->char_remains[i - 1] = cons->char_remains[i];
361
362 cons->char_remains_len--;
363 }
364
365 /* Still not enough? Then get another key from the queue. */
366 if (pos < size) {
367 link_t *link = prodcons_consume(&cons->input_pc);
368 kbd_event_t *event = list_get_instance(link, kbd_event_t, link);
369
370 /* Accept key presses of printable chars only. */
371 if ((event->type == KEY_PRESS) && (event->c != 0)) {
372 wchar_t tmp[2] = { event->c, 0 };
373 wstr_to_str(cons->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
374 cons->char_remains_len = str_size(cons->char_remains);
375 }
376
377 free(event);
378 }
379 }
380
381 *nread = size;
382 return EOK;
383}
384
385static errno_t cons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
386{
387 console_t *cons = srv_to_console(srv);
388
389 size_t off = 0;
390 while (off < size)
391 cons_write_char(cons, str_decode(data, &off, size));
392
393 *nwritten = size;
394 return EOK;
395}
396
397static void cons_sync(con_srv_t *srv)
398{
399 console_t *cons = srv_to_console(srv);
400
401 cons_update(cons);
402}
403
404static void cons_clear(con_srv_t *srv)
405{
406 console_t *cons = srv_to_console(srv);
407
408 fibril_mutex_lock(&cons->mtx);
409 chargrid_clear(cons->frontbuf);
410 fibril_mutex_unlock(&cons->mtx);
411
412 cons_update(cons);
413}
414
415static void cons_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
416{
417 console_t *cons = srv_to_console(srv);
418
419 fibril_mutex_lock(&cons->mtx);
420 chargrid_set_cursor(cons->frontbuf, col, row);
421 fibril_mutex_unlock(&cons->mtx);
422
423 cons_update_cursor(cons);
424}
425
426static errno_t cons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
427{
428 console_t *cons = srv_to_console(srv);
429
430 fibril_mutex_lock(&cons->mtx);
431 chargrid_get_cursor(cons->frontbuf, col, row);
432 fibril_mutex_unlock(&cons->mtx);
433
434 return EOK;
435}
436
437static errno_t cons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
438{
439 console_t *cons = srv_to_console(srv);
440
441 fibril_mutex_lock(&cons->mtx);
442 *cols = cons->cols;
443 *rows = cons->rows;
444 fibril_mutex_unlock(&cons->mtx);
445
446 return EOK;
447}
448
449static errno_t cons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
450{
451 console_t *cons = srv_to_console(srv);
452
453 fibril_mutex_lock(&cons->mtx);
454 *ccaps = cons->ccaps;
455 fibril_mutex_unlock(&cons->mtx);
456
457 return EOK;
458}
459
460static void cons_set_style(con_srv_t *srv, console_style_t style)
461{
462 console_t *cons = srv_to_console(srv);
463
464 fibril_mutex_lock(&cons->mtx);
465 chargrid_set_style(cons->frontbuf, style);
466 fibril_mutex_unlock(&cons->mtx);
467}
468
469static void cons_set_color(con_srv_t *srv, console_color_t bgcolor,
470 console_color_t fgcolor, console_color_attr_t attr)
471{
472 console_t *cons = srv_to_console(srv);
473
474 fibril_mutex_lock(&cons->mtx);
475 chargrid_set_color(cons->frontbuf, bgcolor, fgcolor, attr);
476 fibril_mutex_unlock(&cons->mtx);
477}
478
479static void cons_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
480 pixel_t fgcolor)
481{
482 console_t *cons = srv_to_console(srv);
483
484 fibril_mutex_lock(&cons->mtx);
485 chargrid_set_rgb_color(cons->frontbuf, bgcolor, fgcolor);
486 fibril_mutex_unlock(&cons->mtx);
487}
488
489static void cons_set_cursor_visibility(con_srv_t *srv, bool visible)
490{
491 console_t *cons = srv_to_console(srv);
492
493 cons_set_cursor_vis(cons, visible);
494}
495
496static errno_t cons_get_event(con_srv_t *srv, cons_event_t *event)
497{
498 console_t *cons = srv_to_console(srv);
499 link_t *link = prodcons_consume(&cons->input_pc);
500 kbd_event_t *kevent = list_get_instance(link, kbd_event_t, link);
501
502 event->type = CEV_KEY;
503 event->ev.key = *kevent;
504
505 free(kevent);
506 return EOK;
507}
508
509static void client_connection(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg)
510{
511 console_t *cons = NULL;
512
513 for (size_t i = 0; i < CONSOLE_COUNT; i++) {
514 if (consoles[i].dsid == (service_id_t) IPC_GET_ARG2(*icall)) {
515 cons = &consoles[i];
516 break;
517 }
518 }
519
520 if (cons == NULL) {
521 async_answer_0(icall_handle, ENOENT);
522 return;
523 }
524
525 if (atomic_postinc(&cons->refcnt) == 0)
526 cons_set_cursor_vis(cons, true);
527
528 con_conn(icall_handle, icall, &cons->srvs);
529}
530
531static errno_t input_connect(const char *svc)
532{
533 async_sess_t *sess;
534 service_id_t dsid;
535
536 errno_t rc = loc_service_get_id(svc, &dsid, 0);
537 if (rc != EOK) {
538 printf("%s: Input service %s not found\n", NAME, svc);
539 return rc;
540 }
541
542 sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
543 if (sess == NULL) {
544 printf("%s: Unable to connect to input service %s\n", NAME,
545 svc);
546 return EIO;
547 }
548
549 rc = input_open(sess, &input_ev_ops, NULL, &input);
550 if (rc != EOK) {
551 async_hangup(sess);
552 printf("%s: Unable to communicate with service %s (%s)\n",
553 NAME, svc, str_error(rc));
554 return rc;
555 }
556
557 return EOK;
558}
559
560static async_sess_t *output_connect(const char *svc)
561{
562 async_sess_t *sess;
563 service_id_t dsid;
564
565 errno_t rc = loc_service_get_id(svc, &dsid, 0);
566 if (rc == EOK) {
567 sess = loc_service_connect(dsid, INTERFACE_OUTPUT, 0);
568 if (sess == NULL) {
569 printf("%s: Unable to connect to output service %s\n",
570 NAME, svc);
571 return NULL;
572 }
573 } else
574 return NULL;
575
576 return sess;
577}
578
579static bool console_srv_init(char *input_svc, char *output_svc)
580{
581 /* Connect to input service */
582 errno_t rc = input_connect(input_svc);
583 if (rc != EOK)
584 return false;
585
586 /* Connect to output service */
587 output_sess = output_connect(output_svc);
588 if (output_sess == NULL)
589 return false;
590
591 /* Register server */
592 async_set_fallback_port_handler(client_connection, NULL);
593 rc = loc_server_register(NAME);
594 if (rc != EOK) {
595 printf("%s: Unable to register server (%s)\n", NAME,
596 str_error(rc));
597 return false;
598 }
599
600 output_get_dimensions(output_sess, &cols, &rows);
601 output_set_style(output_sess, STYLE_NORMAL);
602
603 console_caps_t ccaps;
604 output_get_caps(output_sess, &ccaps);
605
606 /*
607 * Inititalize consoles only if there are
608 * actually some output devices.
609 */
610 if (ccaps != 0) {
611 for (size_t i = 0; i < CONSOLE_COUNT; i++) {
612 consoles[i].index = i;
613 atomic_set(&consoles[i].refcnt, 0);
614 fibril_mutex_initialize(&consoles[i].mtx);
615 prodcons_initialize(&consoles[i].input_pc);
616 consoles[i].char_remains_len = 0;
617
618 consoles[i].cols = cols;
619 consoles[i].rows = rows;
620 consoles[i].ccaps = ccaps;
621 consoles[i].frontbuf =
622 chargrid_create(cols, rows, CHARGRID_FLAG_SHARED);
623
624 if (consoles[i].frontbuf == NULL) {
625 printf("%s: Unable to allocate frontbuffer %zu\n", NAME, i);
626 return false;
627 }
628
629 consoles[i].fbid = output_frontbuf_create(output_sess,
630 consoles[i].frontbuf);
631 if (consoles[i].fbid == 0) {
632 printf("%s: Unable to create frontbuffer %zu\n", NAME, i);
633 return false;
634 }
635
636 con_srvs_init(&consoles[i].srvs);
637 consoles[i].srvs.ops = &con_ops;
638 consoles[i].srvs.sarg = &consoles[i];
639
640 char vc[LOC_NAME_MAXLEN + 1];
641 snprintf(vc, LOC_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
642
643 if (loc_service_register(vc, &consoles[i].dsid) != EOK) {
644 printf("%s: Unable to register device %s\n", NAME, vc);
645 return false;
646 }
647 }
648
649 input_activate(input);
650 }
651
652 return true;
653}
654
655static void usage(char *name)
656{
657 printf("Usage: %s <input_dev> <output_dev>\n", name);
658}
659
660int main(int argc, char *argv[])
661{
662 if (argc < 3) {
663 usage(argv[0]);
664 return -1;
665 }
666
667 printf("%s: HelenOS Console service\n", NAME);
668
669 if (!console_srv_init(argv[1], argv[2]))
670 return -1;
671
672 printf("%s: Accepting connections\n", NAME);
673 task_retval(0);
674 async_manager();
675
676 /* Never reached */
677 return 0;
678}
679
680/** @}
681 */
Note: See TracBrowser for help on using the repository browser.