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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since deb2e55 was 2f6ad06, checked in by Jakub Jermar <jakub@…>, 9 years ago

Include task.h

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