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