source: mainline/uspace/srv/hid/console/console.c@ 1924bd43

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1924bd43 was 024fcc5, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Do not discard input events when in kcon (#377)

  • Property mode set to 100644
File size: 24.7 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 <ipc/input.h>
39#include <ipc/console.h>
40#include <ipc/vfs.h>
41#include <errno.h>
42#include <str_error.h>
43#include <loc.h>
44#include <event.h>
45#include <io/keycode.h>
46#include <screenbuffer.h>
47#include <fb.h>
48#include <imgmap.h>
49#include <align.h>
50#include <malloc.h>
51#include <as.h>
52#include <fibril_synch.h>
53#include "images.h"
54#include "console.h"
55
56#define NAME "console"
57#define NAMESPACE "term"
58
59#define CONSOLE_TOP 66
60#define CONSOLE_MARGIN 12
61
62#define STATE_START 100
63#define STATE_TOP 8
64#define STATE_SPACE 4
65#define STATE_WIDTH 48
66#define STATE_HEIGHT 48
67
68typedef enum {
69 CONS_DISCONNECTED = 0,
70 CONS_DISCONNECTED_SELECTED,
71 CONS_SELECTED,
72 CONS_IDLE,
73 CONS_DATA,
74 CONS_KERNEL,
75 CONS_LAST
76} console_state_t;
77
78typedef struct {
79 atomic_t refcnt; /**< Connection reference count */
80 prodcons_t input_pc; /**< Incoming keyboard events */
81
82 fibril_mutex_t mtx; /**< Lock protecting mutable fields */
83
84 size_t index; /**< Console index */
85 console_state_t state; /**< Console state */
86 service_id_t dsid; /**< Service handle */
87
88 vp_handle_t state_vp; /**< State icon viewport */
89 sysarg_t cols; /**< Number of columns */
90 sysarg_t rows; /**< Number of rows */
91 console_caps_t ccaps; /**< Console capabilities */
92
93 screenbuffer_t *frontbuf; /**< Front buffer */
94 frontbuf_handle_t fbid; /**< Front buffer handle */
95} console_t;
96
97typedef enum {
98 GRAPHICS_NONE = 0,
99 GRAPHICS_BASIC = 1,
100 GRAPHICS_FULL = 2
101} graphics_state_t;
102
103/** Current console state */
104static graphics_state_t graphics_state = GRAPHICS_NONE;
105
106/** State icons */
107static imagemap_handle_t state_icons[CONS_LAST];
108
109/** Session to the input server */
110static async_sess_t *input_sess;
111
112/** Session to the framebuffer server */
113static async_sess_t *fb_sess;
114
115/** Framebuffer resolution */
116static sysarg_t xres;
117static sysarg_t yres;
118
119/** Array of data for virtual consoles */
120static console_t consoles[CONSOLE_COUNT];
121
122/** Mutex for console switching */
123static FIBRIL_MUTEX_INITIALIZE(switch_mtx);
124
125static console_t *prev_console = &consoles[0];
126static console_t *active_console = &consoles[0];
127static console_t *kernel_console = &consoles[KERNEL_CONSOLE];
128
129static imgmap_t *logo_img;
130static imgmap_t *nameic_img;
131
132static imgmap_t *anim_1_img;
133static imgmap_t *anim_2_img;
134static imgmap_t *anim_3_img;
135static imgmap_t *anim_4_img;
136
137static imagemap_handle_t anim_1;
138static imagemap_handle_t anim_2;
139static imagemap_handle_t anim_3;
140static imagemap_handle_t anim_4;
141
142static sequence_handle_t anim_seq;
143
144static imgmap_t *cons_data_img;
145static imgmap_t *cons_dis_img;
146static imgmap_t *cons_dis_sel_img;
147static imgmap_t *cons_idle_img;
148static imgmap_t *cons_kernel_img;
149static imgmap_t *cons_sel_img;
150
151static vp_handle_t logo_vp;
152static imagemap_handle_t logo_handle;
153
154static vp_handle_t nameic_vp;
155static imagemap_handle_t nameic_handle;
156
157static vp_handle_t screen_vp;
158static vp_handle_t console_vp;
159
160struct {
161 sysarg_t x;
162 sysarg_t y;
163
164 sysarg_t btn_x;
165 sysarg_t btn_y;
166
167 bool pressed;
168} mouse;
169
170static void cons_redraw_state(console_t *cons)
171{
172 if (graphics_state == GRAPHICS_FULL) {
173 fibril_mutex_lock(&cons->mtx);
174
175 fb_vp_imagemap_damage(fb_sess, cons->state_vp,
176 state_icons[cons->state], 0, 0, STATE_WIDTH, STATE_HEIGHT);
177
178 if ((cons->state != CONS_DISCONNECTED) &&
179 (cons->state != CONS_KERNEL) &&
180 (cons->state != CONS_DISCONNECTED_SELECTED)) {
181 char data[5];
182 snprintf(data, 5, "%zu", cons->index + 1);
183
184 for (size_t i = 0; data[i] != 0; i++)
185 fb_vp_putchar(fb_sess, cons->state_vp, i + 2, 1, data[i]);
186 }
187
188 fibril_mutex_unlock(&cons->mtx);
189 }
190}
191
192static void cons_kernel_sequence_start(console_t *cons)
193{
194 if (graphics_state == GRAPHICS_FULL) {
195 fibril_mutex_lock(&cons->mtx);
196
197 fb_vp_sequence_start(fb_sess, cons->state_vp, anim_seq);
198 fb_vp_imagemap_damage(fb_sess, cons->state_vp,
199 state_icons[cons->state], 0, 0, STATE_WIDTH, STATE_HEIGHT);
200
201 fibril_mutex_unlock(&cons->mtx);
202 }
203}
204
205static void cons_update_state(console_t *cons, console_state_t state)
206{
207 bool update = false;
208
209 fibril_mutex_lock(&cons->mtx);
210
211 if (cons->state != state) {
212 cons->state = state;
213 update = true;
214 }
215
216 fibril_mutex_unlock(&cons->mtx);
217
218 if (update)
219 cons_redraw_state(cons);
220}
221
222static void cons_notify_data(console_t *cons)
223{
224 fibril_mutex_lock(&switch_mtx);
225
226 if (cons != active_console)
227 cons_update_state(cons, CONS_DATA);
228
229 fibril_mutex_unlock(&switch_mtx);
230}
231
232static void cons_notify_connect(console_t *cons)
233{
234 fibril_mutex_lock(&switch_mtx);
235
236 if (cons == active_console)
237 cons_update_state(cons, CONS_SELECTED);
238 else
239 cons_update_state(cons, CONS_IDLE);
240
241 fibril_mutex_unlock(&switch_mtx);
242}
243
244static void cons_notify_disconnect(console_t *cons)
245{
246 fibril_mutex_lock(&switch_mtx);
247
248 if (cons == active_console)
249 cons_update_state(cons, CONS_DISCONNECTED_SELECTED);
250 else
251 cons_update_state(cons, CONS_DISCONNECTED);
252
253 fibril_mutex_unlock(&switch_mtx);
254}
255
256static void cons_update(console_t *cons)
257{
258 fibril_mutex_lock(&switch_mtx);
259 fibril_mutex_lock(&cons->mtx);
260
261 if ((cons == active_console) && (active_console != kernel_console)) {
262 fb_vp_update(fb_sess, console_vp, cons->fbid);
263 fb_vp_cursor_update(fb_sess, console_vp, cons->fbid);
264 }
265
266 fibril_mutex_unlock(&cons->mtx);
267 fibril_mutex_unlock(&switch_mtx);
268}
269
270static void cons_update_cursor(console_t *cons)
271{
272 fibril_mutex_lock(&switch_mtx);
273 fibril_mutex_lock(&cons->mtx);
274
275 if ((cons == active_console) && (active_console != kernel_console))
276 fb_vp_cursor_update(fb_sess, console_vp, cons->fbid);
277
278 fibril_mutex_unlock(&cons->mtx);
279 fibril_mutex_unlock(&switch_mtx);
280}
281
282static void cons_clear(console_t *cons)
283{
284 fibril_mutex_lock(&cons->mtx);
285 screenbuffer_clear(cons->frontbuf);
286 fibril_mutex_unlock(&cons->mtx);
287
288 cons_update(cons);
289}
290
291static void cons_damage_all(console_t *cons)
292{
293 fibril_mutex_lock(&switch_mtx);
294 fibril_mutex_lock(&cons->mtx);
295
296 if ((cons == active_console) && (active_console != kernel_console)) {
297 fb_vp_damage(fb_sess, console_vp, cons->fbid, 0, 0, cons->cols,
298 cons->rows);
299 fb_vp_cursor_update(fb_sess, console_vp, cons->fbid);
300 }
301
302 fibril_mutex_unlock(&cons->mtx);
303 fibril_mutex_unlock(&switch_mtx);
304}
305
306static void cons_switch(console_t *cons)
307{
308 fibril_mutex_lock(&switch_mtx);
309
310 if (cons == active_console) {
311 fibril_mutex_unlock(&switch_mtx);
312 return;
313 }
314
315 if (cons == kernel_console) {
316 fb_yield(fb_sess);
317 if (!console_kcon()) {
318 fb_claim(fb_sess);
319 fibril_mutex_unlock(&switch_mtx);
320 return;
321 }
322 }
323
324 if (active_console == kernel_console)
325 fb_claim(fb_sess);
326
327 prev_console = active_console;
328 active_console = cons;
329
330 if (prev_console->state == CONS_DISCONNECTED_SELECTED)
331 cons_update_state(prev_console, CONS_DISCONNECTED);
332 else
333 cons_update_state(prev_console, CONS_IDLE);
334
335 if ((cons->state == CONS_DISCONNECTED) ||
336 (cons->state == CONS_DISCONNECTED_SELECTED))
337 cons_update_state(cons, CONS_DISCONNECTED_SELECTED);
338 else
339 cons_update_state(cons, CONS_SELECTED);
340
341 fibril_mutex_unlock(&switch_mtx);
342
343 cons_damage_all(cons);
344}
345
346static console_t *cons_get_active_uspace(void)
347{
348 fibril_mutex_lock(&switch_mtx);
349
350 console_t *active_uspace = active_console;
351 if (active_uspace == kernel_console) {
352 active_uspace = prev_console;
353 }
354 assert(active_uspace != kernel_console);
355
356 fibril_mutex_unlock(&switch_mtx);
357
358 return active_uspace;
359}
360
361static ssize_t limit(ssize_t val, ssize_t lo, ssize_t hi)
362{
363 if (val > hi)
364 return hi;
365
366 if (val < lo)
367 return lo;
368
369 return val;
370}
371
372static void cons_mouse_move(sysarg_t dx, sysarg_t dy)
373{
374 ssize_t sx = (ssize_t) dx;
375 ssize_t sy = (ssize_t) dy;
376
377 mouse.x = limit(mouse.x + sx, 0, xres);
378 mouse.y = limit(mouse.y + sy, 0, yres);
379
380 fb_pointer_update(fb_sess, mouse.x, mouse.y, true);
381}
382
383static console_t *cons_find_icon(sysarg_t x, sysarg_t y)
384{
385 sysarg_t status_start =
386 STATE_START + (xres - 800) / 2 + CONSOLE_MARGIN;
387
388 if ((y < STATE_TOP) || (y >= STATE_TOP + STATE_HEIGHT))
389 return NULL;
390
391 if (x < status_start)
392 return NULL;
393
394 if (x >= status_start + (STATE_WIDTH + STATE_SPACE) * CONSOLE_COUNT)
395 return NULL;
396
397 if (((x - status_start) % (STATE_WIDTH + STATE_SPACE)) >= STATE_WIDTH)
398 return NULL;
399
400 sysarg_t btn = (x - status_start) / (STATE_WIDTH + STATE_SPACE);
401
402 if (btn < CONSOLE_COUNT)
403 return consoles + btn;
404
405 return NULL;
406}
407
408/** Handle mouse click
409 *
410 * @param state Button state (true - pressed, false - depressed)
411 *
412 */
413static console_t *cons_mouse_button(bool state)
414{
415 if (graphics_state != GRAPHICS_FULL)
416 return NULL;
417
418 if (state) {
419 console_t *cons = cons_find_icon(mouse.x, mouse.y);
420 if (cons != NULL) {
421 mouse.btn_x = mouse.x;
422 mouse.btn_y = mouse.y;
423 mouse.pressed = true;
424 }
425
426 return NULL;
427 }
428
429 if ((!state) && (!mouse.pressed))
430 return NULL;
431
432 console_t *cons = cons_find_icon(mouse.x, mouse.y);
433 if (cons == cons_find_icon(mouse.btn_x, mouse.btn_y))
434 return cons;
435
436 mouse.pressed = false;
437 return NULL;
438}
439
440static void input_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
441{
442 /* Ignore parameters, the connection is already opened */
443 while (true) {
444 ipc_call_t call;
445 ipc_callid_t callid = async_get_call(&call);
446
447 if (!IPC_GET_IMETHOD(call)) {
448 /* TODO: Handle hangup */
449 async_hangup(input_sess);
450 return;
451 }
452
453 kbd_event_type_t type;
454 keycode_t key;
455 keymod_t mods;
456 wchar_t c;
457
458 switch (IPC_GET_IMETHOD(call)) {
459 case INPUT_EVENT_KEY:
460 type = IPC_GET_ARG1(call);
461 key = IPC_GET_ARG2(call);
462 mods = IPC_GET_ARG3(call);
463 c = IPC_GET_ARG4(call);
464
465 if ((key >= KC_F1) && (key < KC_F1 + CONSOLE_COUNT) &&
466 ((mods & KM_CTRL) == 0))
467 cons_switch(&consoles[key - KC_F1]);
468 else {
469 /* Got key press/release event */
470 kbd_event_t *event =
471 (kbd_event_t *) malloc(sizeof(kbd_event_t));
472 if (event == NULL) {
473 async_answer_0(callid, ENOMEM);
474 break;
475 }
476
477 link_initialize(&event->link);
478 event->type = type;
479 event->key = key;
480 event->mods = mods;
481 event->c = c;
482
483 /* Kernel console does not read events
484 * from us, so we will redirect them
485 * to the (last) active userspace console
486 * if necessary.
487 */
488 console_t *target_console = cons_get_active_uspace();
489
490 prodcons_produce(&target_console->input_pc,
491 &event->link);
492 }
493
494 async_answer_0(callid, EOK);
495 break;
496 case INPUT_EVENT_MOVE:
497 cons_mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
498 async_answer_0(callid, EOK);
499 break;
500 case INPUT_EVENT_BUTTON:
501 /* Got pointer button press/release event */
502 if (IPC_GET_ARG1(call) == 1) {
503 console_t *cons =
504 cons_mouse_button((bool) IPC_GET_ARG2(call));
505 if (cons != NULL)
506 cons_switch(cons);
507 }
508 async_answer_0(callid, EOK);
509 break;
510 default:
511 async_answer_0(callid, EINVAL);
512 }
513 }
514}
515
516/** Process a character from the client (TTY emulation). */
517static void cons_write_char(console_t *cons, wchar_t ch)
518{
519 sysarg_t updated = 0;
520
521 fibril_mutex_lock(&cons->mtx);
522
523 switch (ch) {
524 case '\n':
525 updated = screenbuffer_newline(cons->frontbuf);
526 break;
527 case '\r':
528 break;
529 case '\t':
530 updated = screenbuffer_tabstop(cons->frontbuf, 8);
531 break;
532 case '\b':
533 updated = screenbuffer_backspace(cons->frontbuf);
534 break;
535 default:
536 updated = screenbuffer_putchar(cons->frontbuf, ch, true);
537 }
538
539 fibril_mutex_unlock(&cons->mtx);
540
541 if (updated > 1)
542 cons_update(cons);
543}
544
545static void cons_set_cursor(console_t *cons, sysarg_t col, sysarg_t row)
546{
547 fibril_mutex_lock(&cons->mtx);
548 screenbuffer_set_cursor(cons->frontbuf, col, row);
549 fibril_mutex_unlock(&cons->mtx);
550
551 cons_update_cursor(cons);
552}
553
554static void cons_set_cursor_visibility(console_t *cons, bool visible)
555{
556 fibril_mutex_lock(&cons->mtx);
557 screenbuffer_set_cursor_visibility(cons->frontbuf, visible);
558 fibril_mutex_unlock(&cons->mtx);
559
560 cons_update_cursor(cons);
561}
562
563static void cons_get_cursor(console_t *cons, ipc_callid_t iid, ipc_call_t *icall)
564{
565 sysarg_t col;
566 sysarg_t row;
567
568 fibril_mutex_lock(&cons->mtx);
569 screenbuffer_get_cursor(cons->frontbuf, &col, &row);
570 fibril_mutex_unlock(&cons->mtx);
571
572 async_answer_2(iid, EOK, col, row);
573}
574
575static void cons_write(console_t *cons, ipc_callid_t iid, ipc_call_t *icall)
576{
577 void *buf;
578 size_t size;
579 int rc = async_data_write_accept(&buf, false, 0, 0, 0, &size);
580
581 if (rc != EOK) {
582 async_answer_0(iid, rc);
583 return;
584 }
585
586 size_t off = 0;
587 while (off < size)
588 cons_write_char(cons, str_decode(buf, &off, size));
589
590 async_answer_1(iid, EOK, size);
591 free(buf);
592
593 cons_notify_data(cons);
594}
595
596static void cons_read(console_t *cons, ipc_callid_t iid, ipc_call_t *icall)
597{
598 ipc_callid_t callid;
599 size_t size;
600 if (!async_data_read_receive(&callid, &size)) {
601 async_answer_0(callid, EINVAL);
602 async_answer_0(iid, EINVAL);
603 return;
604 }
605
606 char *buf = (char *) malloc(size);
607 if (buf == NULL) {
608 async_answer_0(callid, ENOMEM);
609 async_answer_0(iid, ENOMEM);
610 return;
611 }
612
613 size_t pos = 0;
614 while (pos < size) {
615 link_t *link = prodcons_consume(&cons->input_pc);
616 kbd_event_t *event = list_get_instance(link, kbd_event_t, link);
617
618 if (event->type == KEY_PRESS) {
619 buf[pos] = event->c;
620 pos++;
621 }
622
623 free(event);
624 }
625
626 (void) async_data_read_finalize(callid, buf, size);
627 async_answer_1(iid, EOK, size);
628 free(buf);
629}
630
631static void cons_set_style(console_t *cons, console_style_t style)
632{
633 fibril_mutex_lock(&cons->mtx);
634 screenbuffer_set_style(cons->frontbuf, style);
635 fibril_mutex_unlock(&cons->mtx);
636}
637
638static void cons_set_color(console_t *cons, console_color_t bgcolor,
639 console_color_t fgcolor, console_color_attr_t attr)
640{
641 fibril_mutex_lock(&cons->mtx);
642 screenbuffer_set_color(cons->frontbuf, bgcolor, fgcolor, attr);
643 fibril_mutex_unlock(&cons->mtx);
644}
645
646static void cons_set_rgb_color(console_t *cons, pixel_t bgcolor,
647 pixel_t fgcolor)
648{
649 fibril_mutex_lock(&cons->mtx);
650 screenbuffer_set_rgb_color(cons->frontbuf, bgcolor, fgcolor);
651 fibril_mutex_unlock(&cons->mtx);
652}
653
654static void cons_get_event(console_t *cons, ipc_callid_t iid, ipc_call_t *icall)
655{
656 link_t *link = prodcons_consume(&cons->input_pc);
657 kbd_event_t *event = list_get_instance(link, kbd_event_t, link);
658
659 async_answer_4(iid, EOK, event->type, event->key, event->mods, event->c);
660 free(event);
661}
662
663static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
664{
665 console_t *cons = NULL;
666
667 for (size_t i = 0; i < CONSOLE_COUNT; i++) {
668 if (i == KERNEL_CONSOLE)
669 continue;
670
671 if (consoles[i].dsid == (service_id_t) IPC_GET_ARG1(*icall)) {
672 cons = &consoles[i];
673 break;
674 }
675 }
676
677 if (cons == NULL) {
678 async_answer_0(iid, ENOENT);
679 return;
680 }
681
682 if (atomic_postinc(&cons->refcnt) == 0) {
683 cons_set_cursor_visibility(cons, true);
684 cons_notify_connect(cons);
685 }
686
687 /* Accept the connection */
688 async_answer_0(iid, EOK);
689
690 while (true) {
691 ipc_call_t call;
692 ipc_callid_t callid = async_get_call(&call);
693
694 if (!IPC_GET_IMETHOD(call)) {
695 if (atomic_postdec(&cons->refcnt) == 1)
696 cons_notify_disconnect(cons);
697
698 return;
699 }
700
701 switch (IPC_GET_IMETHOD(call)) {
702 case VFS_OUT_READ:
703 cons_read(cons, callid, &call);
704 break;
705 case VFS_OUT_WRITE:
706 cons_write(cons, callid, &call);
707 break;
708 case VFS_OUT_SYNC:
709 cons_update(cons);
710 async_answer_0(callid, EOK);
711 break;
712 case CONSOLE_CLEAR:
713 cons_clear(cons);
714 async_answer_0(callid, EOK);
715 break;
716 case CONSOLE_GOTO:
717 cons_set_cursor(cons, IPC_GET_ARG1(call), IPC_GET_ARG2(call));
718 async_answer_0(callid, EOK);
719 break;
720 case CONSOLE_GET_POS:
721 cons_get_cursor(cons, callid, &call);
722 break;
723 case CONSOLE_GET_SIZE:
724 async_answer_2(callid, EOK, cons->cols, cons->rows);
725 break;
726 case CONSOLE_GET_COLOR_CAP:
727 async_answer_1(callid, EOK, cons->ccaps);
728 break;
729 case CONSOLE_SET_STYLE:
730 cons_set_style(cons, IPC_GET_ARG1(call));
731 async_answer_0(callid, EOK);
732 break;
733 case CONSOLE_SET_COLOR:
734 cons_set_color(cons, IPC_GET_ARG1(call), IPC_GET_ARG2(call),
735 IPC_GET_ARG3(call));
736 async_answer_0(callid, EOK);
737 break;
738 case CONSOLE_SET_RGB_COLOR:
739 cons_set_rgb_color(cons, IPC_GET_ARG1(call), IPC_GET_ARG2(call));
740 async_answer_0(callid, EOK);
741 break;
742 case CONSOLE_CURSOR_VISIBILITY:
743 cons_set_cursor_visibility(cons, IPC_GET_ARG1(call));
744 async_answer_0(callid, EOK);
745 break;
746 case CONSOLE_GET_EVENT:
747 cons_get_event(cons, callid, &call);
748 break;
749 default:
750 async_answer_0(callid, EINVAL);
751 }
752 }
753}
754
755static async_sess_t *input_connect(const char *svc)
756{
757 async_sess_t *sess;
758 service_id_t dsid;
759
760 int rc = loc_service_get_id(svc, &dsid, 0);
761 if (rc == EOK) {
762 sess = loc_service_connect(EXCHANGE_ATOMIC, dsid, 0);
763 if (sess == NULL) {
764 printf("%s: Unable to connect to input service %s\n", NAME,
765 svc);
766 return NULL;
767 }
768 } else
769 return NULL;
770
771 async_exch_t *exch = async_exchange_begin(sess);
772 rc = async_connect_to_me(exch, 0, 0, 0, input_events, NULL);
773 async_exchange_end(exch);
774
775 if (rc != EOK) {
776 async_hangup(sess);
777 printf("%s: Unable to create callback connection to service %s (%s)\n",
778 NAME, svc, str_error(rc));
779 return NULL;
780 }
781
782 return sess;
783}
784
785static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
786{
787 cons_switch(prev_console);
788}
789
790static async_sess_t *fb_connect(const char *svc)
791{
792 async_sess_t *sess;
793 service_id_t dsid;
794
795 int rc = loc_service_get_id(svc, &dsid, 0);
796 if (rc == EOK) {
797 sess = loc_service_connect(EXCHANGE_SERIALIZE, dsid, 0);
798 if (sess == NULL) {
799 printf("%s: Unable to connect to framebuffer service %s\n",
800 NAME, svc);
801 return NULL;
802 }
803 } else
804 return NULL;
805
806 return sess;
807}
808
809static bool console_srv_init(char *input_svc, char *fb_svc)
810{
811 /* Avoid double initialization */
812 if (graphics_state != GRAPHICS_NONE)
813 return false;
814
815 /* Connect to input service */
816 input_sess = input_connect(input_svc);
817 if (input_sess == NULL)
818 return false;
819
820 /* Connect to framebuffer service */
821 fb_sess = fb_connect(fb_svc);
822 if (fb_sess == NULL)
823 return false;
824
825 /* Register server */
826 int rc = loc_server_register(NAME, client_connection);
827 if (rc < 0) {
828 printf("%s: Unable to register server (%s)\n", NAME,
829 str_error(rc));
830 return false;
831 }
832
833 fb_get_resolution(fb_sess, &xres, &yres);
834
835 /* Initialize the screen */
836 screen_vp = fb_vp_create(fb_sess, 0, 0, xres, yres);
837
838 if ((xres >= 800) && (yres >= 600)) {
839 logo_vp = fb_vp_create(fb_sess, xres - 66, 2, 64, 60);
840 logo_img = imgmap_decode_tga((void *) helenos_tga,
841 helenos_tga_size, IMGMAP_FLAG_SHARED);
842 logo_handle = fb_imagemap_create(fb_sess, logo_img);
843
844 nameic_vp = fb_vp_create(fb_sess, 5, 17, 100, 26);
845 nameic_img = imgmap_decode_tga((void *) nameic_tga,
846 nameic_tga_size, IMGMAP_FLAG_SHARED);
847 nameic_handle = fb_imagemap_create(fb_sess, nameic_img);
848
849 cons_data_img = imgmap_decode_tga((void *) cons_data_tga,
850 cons_data_tga_size, IMGMAP_FLAG_SHARED);
851 cons_dis_img = imgmap_decode_tga((void *) cons_dis_tga,
852 cons_dis_tga_size, IMGMAP_FLAG_SHARED);
853 cons_dis_sel_img = imgmap_decode_tga((void *) cons_dis_sel_tga,
854 cons_dis_sel_tga_size, IMGMAP_FLAG_SHARED);
855 cons_idle_img = imgmap_decode_tga((void *) cons_idle_tga,
856 cons_idle_tga_size, IMGMAP_FLAG_SHARED);
857 cons_kernel_img = imgmap_decode_tga((void *) cons_kernel_tga,
858 cons_kernel_tga_size, IMGMAP_FLAG_SHARED);
859 cons_sel_img = imgmap_decode_tga((void *) cons_sel_tga,
860 cons_sel_tga_size, IMGMAP_FLAG_SHARED);
861
862 state_icons[CONS_DISCONNECTED] =
863 fb_imagemap_create(fb_sess, cons_dis_img);
864 state_icons[CONS_DISCONNECTED_SELECTED] =
865 fb_imagemap_create(fb_sess, cons_dis_sel_img);
866 state_icons[CONS_SELECTED] =
867 fb_imagemap_create(fb_sess, cons_sel_img);
868 state_icons[CONS_IDLE] =
869 fb_imagemap_create(fb_sess, cons_idle_img);
870 state_icons[CONS_DATA] =
871 fb_imagemap_create(fb_sess, cons_data_img);
872 state_icons[CONS_KERNEL] =
873 fb_imagemap_create(fb_sess, cons_kernel_img);
874
875 anim_1_img = imgmap_decode_tga((void *) anim_1_tga,
876 anim_1_tga_size, IMGMAP_FLAG_SHARED);
877 anim_2_img = imgmap_decode_tga((void *) anim_2_tga,
878 anim_2_tga_size, IMGMAP_FLAG_SHARED);
879 anim_3_img = imgmap_decode_tga((void *) anim_3_tga,
880 anim_3_tga_size, IMGMAP_FLAG_SHARED);
881 anim_4_img = imgmap_decode_tga((void *) anim_4_tga,
882 anim_4_tga_size, IMGMAP_FLAG_SHARED);
883
884 anim_1 = fb_imagemap_create(fb_sess, anim_1_img);
885 anim_2 = fb_imagemap_create(fb_sess, anim_2_img);
886 anim_3 = fb_imagemap_create(fb_sess, anim_3_img);
887 anim_4 = fb_imagemap_create(fb_sess, anim_4_img);
888
889 anim_seq = fb_sequence_create(fb_sess);
890 fb_sequence_add_imagemap(fb_sess, anim_seq, anim_1);
891 fb_sequence_add_imagemap(fb_sess, anim_seq, anim_2);
892 fb_sequence_add_imagemap(fb_sess, anim_seq, anim_3);
893 fb_sequence_add_imagemap(fb_sess, anim_seq, anim_4);
894
895 console_vp = fb_vp_create(fb_sess, CONSOLE_MARGIN, CONSOLE_TOP,
896 xres - 2 * CONSOLE_MARGIN, yres - (CONSOLE_TOP + CONSOLE_MARGIN));
897
898 fb_vp_clear(fb_sess, screen_vp);
899 fb_vp_imagemap_damage(fb_sess, logo_vp, logo_handle,
900 0, 0, 64, 60);
901 fb_vp_imagemap_damage(fb_sess, nameic_vp, nameic_handle,
902 0, 0, 100, 26);
903
904 graphics_state = GRAPHICS_FULL;
905 } else {
906 console_vp = screen_vp;
907 graphics_state = GRAPHICS_BASIC;
908 }
909
910 fb_vp_set_style(fb_sess, console_vp, STYLE_NORMAL);
911 fb_vp_clear(fb_sess, console_vp);
912
913 sysarg_t cols;
914 sysarg_t rows;
915 fb_vp_get_dimensions(fb_sess, console_vp, &cols, &rows);
916
917 console_caps_t ccaps;
918 fb_vp_get_caps(fb_sess, console_vp, &ccaps);
919
920 mouse.x = xres / 2;
921 mouse.y = yres / 2;
922 mouse.pressed = false;
923
924 /* Inititalize consoles */
925 for (size_t i = 0; i < CONSOLE_COUNT; i++) {
926 consoles[i].index = i;
927 atomic_set(&consoles[i].refcnt, 0);
928 fibril_mutex_initialize(&consoles[i].mtx);
929 prodcons_initialize(&consoles[i].input_pc);
930
931 if (graphics_state == GRAPHICS_FULL) {
932 /* Create state buttons */
933 consoles[i].state_vp =
934 fb_vp_create(fb_sess, STATE_START + (xres - 800) / 2 +
935 CONSOLE_MARGIN + i * (STATE_WIDTH + STATE_SPACE),
936 STATE_TOP, STATE_WIDTH, STATE_HEIGHT);
937 }
938
939 if (i == KERNEL_CONSOLE) {
940 consoles[i].state = CONS_KERNEL;
941 cons_redraw_state(&consoles[i]);
942 cons_kernel_sequence_start(&consoles[i]);
943 continue;
944 }
945
946 if (i == 0)
947 consoles[i].state = CONS_DISCONNECTED_SELECTED;
948 else
949 consoles[i].state = CONS_DISCONNECTED;
950
951 consoles[i].cols = cols;
952 consoles[i].rows = rows;
953 consoles[i].ccaps = ccaps;
954 consoles[i].frontbuf =
955 screenbuffer_create(cols, rows, SCREENBUFFER_FLAG_SHARED);
956
957 if (consoles[i].frontbuf == NULL) {
958 printf("%s: Unable to allocate frontbuffer %zu\n", NAME, i);
959 return false;
960 }
961
962 consoles[i].fbid = fb_frontbuf_create(fb_sess, consoles[i].frontbuf);
963 if (consoles[i].fbid == 0) {
964 printf("%s: Unable to create frontbuffer %zu\n", NAME, i);
965 return false;
966 }
967
968 cons_redraw_state(&consoles[i]);
969
970 char vc[LOC_NAME_MAXLEN + 1];
971 snprintf(vc, LOC_NAME_MAXLEN, "%s/vc%zu", NAMESPACE, i);
972
973 if (loc_service_register(vc, &consoles[i].dsid) != EOK) {
974 printf("%s: Unable to register device %s\n", NAME, vc);
975 return false;
976 }
977 }
978
979 /* Receive kernel notifications */
980 async_set_interrupt_received(interrupt_received);
981 rc = event_subscribe(EVENT_KCONSOLE, 0);
982 if (rc != EOK)
983 printf("%s: Failed to register kconsole notifications (%s)\n",
984 NAME, str_error(rc));
985
986 return true;
987}
988
989static void usage(void)
990{
991 printf("Usage: console <input_dev> <framebuffer_dev>\n");
992}
993
994int main(int argc, char *argv[])
995{
996 if (argc < 3) {
997 usage();
998 return -1;
999 }
1000
1001 printf("%s: HelenOS Console service\n", NAME);
1002
1003 if (!console_srv_init(argv[1], argv[2]))
1004 return -1;
1005
1006 printf("%s: Accepting connections\n", NAME);
1007 task_retval(0);
1008 async_manager();
1009
1010 return 0;
1011}
1012
1013/** @}
1014 */
Note: See TracBrowser for help on using the repository browser.