source: mainline/uspace/app/terminal/terminal.c@ ce862ac

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

Terminate terminal if the child task has terminated

Try saying that three times quickly.

  • Property mode set to 100644
File size: 25.6 KB
Line 
1/*
2 * Copyright (c) 2021 Jiri Svoboda
3 * Copyright (c) 2012 Petr Koupy
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 terminal
31 * @{
32 */
33/**
34 * @file Terminal application
35 */
36
37#include <adt/list.h>
38#include <adt/prodcons.h>
39#include <as.h>
40#include <errno.h>
41#include <fbfont/font-8x16.h>
42#include <io/chargrid.h>
43#include <fibril.h>
44#include <gfx/bitmap.h>
45#include <gfx/context.h>
46#include <gfx/render.h>
47#include <io/con_srv.h>
48#include <io/concaps.h>
49#include <io/console.h>
50#include <io/pixelmap.h>
51#include <task.h>
52#include <stdarg.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <str.h>
56#include <ui/resource.h>
57#include <ui/ui.h>
58#include <ui/wdecor.h>
59#include <ui/window.h>
60
61#include "terminal.h"
62
63#define NAME "terminal"
64#define NAMESPACE "terminal"
65
66#define LOCFS_MOUNT_POINT "/loc"
67
68#define APP_GETTERM "/app/getterm"
69
70#define TERM_CAPS \
71 (CONSOLE_CAP_STYLE | CONSOLE_CAP_INDEXED | CONSOLE_CAP_RGB)
72
73static LIST_INITIALIZE(terms);
74
75static errno_t term_open(con_srvs_t *, con_srv_t *);
76static errno_t term_close(con_srv_t *);
77static errno_t term_read(con_srv_t *, void *, size_t, size_t *);
78static errno_t term_write(con_srv_t *, void *, size_t, size_t *);
79static void term_sync(con_srv_t *);
80static void term_clear(con_srv_t *);
81static void term_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
82static errno_t term_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
83static errno_t term_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
84static errno_t term_get_color_cap(con_srv_t *, console_caps_t *);
85static void term_set_style(con_srv_t *, console_style_t);
86static void term_set_color(con_srv_t *, console_color_t, console_color_t,
87 console_color_attr_t);
88static void term_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
89static void term_set_cursor_visibility(con_srv_t *, bool);
90static errno_t term_get_event(con_srv_t *, cons_event_t *);
91static errno_t term_map(con_srv_t *, sysarg_t, sysarg_t, charfield_t **);
92static void term_unmap(con_srv_t *);
93static void term_buf_update(con_srv_t *, sysarg_t, sysarg_t, sysarg_t,
94 sysarg_t);
95
96static con_ops_t con_ops = {
97 .open = term_open,
98 .close = term_close,
99 .read = term_read,
100 .write = term_write,
101 .sync = term_sync,
102 .clear = term_clear,
103 .set_pos = term_set_pos,
104 .get_pos = term_get_pos,
105 .get_size = term_get_size,
106 .get_color_cap = term_get_color_cap,
107 .set_style = term_set_style,
108 .set_color = term_set_color,
109 .set_rgb_color = term_set_rgb_color,
110 .set_cursor_visibility = term_set_cursor_visibility,
111 .get_event = term_get_event,
112 .map = term_map,
113 .unmap = term_unmap,
114 .update = term_buf_update
115};
116
117static void terminal_close_event(ui_window_t *, void *);
118static void terminal_focus_event(ui_window_t *, void *);
119static void terminal_kbd_event(ui_window_t *, void *, kbd_event_t *);
120static void terminal_pos_event(ui_window_t *, void *, pos_event_t *);
121static void terminal_unfocus_event(ui_window_t *, void *);
122
123static ui_window_cb_t terminal_window_cb = {
124 .close = terminal_close_event,
125 .focus = terminal_focus_event,
126 .kbd = terminal_kbd_event,
127 .pos = terminal_pos_event,
128 .unfocus = terminal_unfocus_event
129};
130
131static errno_t terminal_wait_fibril(void *);
132
133static terminal_t *srv_to_terminal(con_srv_t *srv)
134{
135 return srv->srvs->sarg;
136}
137
138static errno_t getterm(task_wait_t *wait, const char *svc, const char *app)
139{
140 return task_spawnl(NULL, wait, APP_GETTERM, APP_GETTERM, svc,
141 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
142}
143
144static pixel_t color_table[16] = {
145 [COLOR_BLACK] = PIXEL(255, 0, 0, 0),
146 [COLOR_BLUE] = PIXEL(255, 0, 0, 170),
147 [COLOR_GREEN] = PIXEL(255, 0, 170, 0),
148 [COLOR_CYAN] = PIXEL(255, 0, 170, 170),
149 [COLOR_RED] = PIXEL(255, 170, 0, 0),
150 [COLOR_MAGENTA] = PIXEL(255, 170, 0, 170),
151 [COLOR_YELLOW] = PIXEL(255, 170, 85, 0),
152 [COLOR_WHITE] = PIXEL(255, 170, 170, 170),
153
154 [COLOR_BLACK + 8] = PIXEL(255, 85, 85, 85),
155 [COLOR_BLUE + 8] = PIXEL(255, 85, 85, 255),
156 [COLOR_GREEN + 8] = PIXEL(255, 85, 255, 85),
157 [COLOR_CYAN + 8] = PIXEL(255, 85, 255, 255),
158 [COLOR_RED + 8] = PIXEL(255, 255, 85, 85),
159 [COLOR_MAGENTA + 8] = PIXEL(255, 255, 85, 255),
160 [COLOR_YELLOW + 8] = PIXEL(255, 255, 255, 85),
161 [COLOR_WHITE + 8] = PIXEL(255, 255, 255, 255),
162};
163
164static inline void attrs_rgb(char_attrs_t attrs, pixel_t *bgcolor, pixel_t *fgcolor)
165{
166 switch (attrs.type) {
167 case CHAR_ATTR_STYLE:
168 switch (attrs.val.style) {
169 case STYLE_NORMAL:
170 *bgcolor = color_table[COLOR_WHITE + 8];
171 *fgcolor = color_table[COLOR_BLACK];
172 break;
173 case STYLE_EMPHASIS:
174 *bgcolor = color_table[COLOR_WHITE + 8];
175 *fgcolor = color_table[COLOR_RED + 8];
176 break;
177 case STYLE_INVERTED:
178 *bgcolor = color_table[COLOR_BLACK];
179 *fgcolor = color_table[COLOR_WHITE + 8];
180 break;
181 case STYLE_SELECTED:
182 *bgcolor = color_table[COLOR_RED + 8];
183 *fgcolor = color_table[COLOR_WHITE + 8];
184 break;
185 }
186 break;
187 case CHAR_ATTR_INDEX:
188 *bgcolor = color_table[(attrs.val.index.bgcolor & 7)];
189 *fgcolor = color_table[(attrs.val.index.fgcolor & 7) |
190 ((attrs.val.index.attr & CATTR_BRIGHT) ? 8 : 0)];
191 break;
192 case CHAR_ATTR_RGB:
193 *bgcolor = 0xff000000 | attrs.val.rgb.bgcolor;
194 *fgcolor = 0xff000000 | attrs.val.rgb.fgcolor;
195 break;
196 }
197}
198
199static void term_update_region(terminal_t *term, sysarg_t x, sysarg_t y,
200 sysarg_t w, sysarg_t h)
201{
202 gfx_rect_t rect;
203 gfx_rect_t nupdate;
204
205 rect.p0.x = x;
206 rect.p0.y = y;
207 rect.p1.x = x + w;
208 rect.p1.y = y + h;
209
210 gfx_rect_envelope(&term->update, &rect, &nupdate);
211 term->update = nupdate;
212}
213
214static void term_update_char(terminal_t *term, pixelmap_t *pixelmap,
215 sysarg_t sx, sysarg_t sy, sysarg_t col, sysarg_t row)
216{
217 charfield_t *field =
218 chargrid_charfield_at(term->backbuf, col, row);
219
220 bool inverted = chargrid_cursor_at(term->backbuf, col, row);
221
222 sysarg_t bx = sx + (col * FONT_WIDTH);
223 sysarg_t by = sy + (row * FONT_SCANLINES);
224
225 pixel_t bgcolor = 0;
226 pixel_t fgcolor = 0;
227
228 if (inverted)
229 attrs_rgb(field->attrs, &fgcolor, &bgcolor);
230 else
231 attrs_rgb(field->attrs, &bgcolor, &fgcolor);
232
233 // FIXME: Glyph type should be actually uint32_t
234 // for full UTF-32 coverage.
235
236 uint16_t glyph = fb_font_glyph(field->ch, NULL);
237
238 for (unsigned int y = 0; y < FONT_SCANLINES; y++) {
239 pixel_t *dst = pixelmap_pixel_at(pixelmap, bx, by + y);
240 pixel_t *dst_max = pixelmap_pixel_at(pixelmap, bx + FONT_WIDTH - 1, by + y);
241 if (!dst || !dst_max)
242 continue;
243 int count = FONT_WIDTH;
244 while (count-- != 0) {
245 *dst++ = (fb_font[glyph][y] & (1 << count)) ? fgcolor : bgcolor;
246 }
247 }
248 term_update_region(term, bx, by, FONT_WIDTH, FONT_SCANLINES);
249}
250
251static bool term_update_scroll(terminal_t *term, pixelmap_t *pixelmap,
252 sysarg_t sx, sysarg_t sy)
253{
254 sysarg_t top_row = chargrid_get_top_row(term->frontbuf);
255
256 if (term->top_row == top_row) {
257 return false;
258 }
259
260 term->top_row = top_row;
261
262 for (sysarg_t row = 0; row < term->rows; row++) {
263 for (sysarg_t col = 0; col < term->cols; col++) {
264 charfield_t *front_field =
265 chargrid_charfield_at(term->frontbuf, col, row);
266 charfield_t *back_field =
267 chargrid_charfield_at(term->backbuf, col, row);
268 bool update = false;
269
270 if (front_field->ch != back_field->ch) {
271 back_field->ch = front_field->ch;
272 update = true;
273 }
274
275 if (!attrs_same(front_field->attrs, back_field->attrs)) {
276 back_field->attrs = front_field->attrs;
277 update = true;
278 }
279
280 front_field->flags &= ~CHAR_FLAG_DIRTY;
281
282 if (update) {
283 term_update_char(term, pixelmap, sx, sy, col, row);
284 }
285 }
286 }
287
288 return true;
289}
290
291static bool term_update_cursor(terminal_t *term, pixelmap_t *pixelmap,
292 sysarg_t sx, sysarg_t sy)
293{
294 bool update = false;
295
296 sysarg_t front_col;
297 sysarg_t front_row;
298 chargrid_get_cursor(term->frontbuf, &front_col, &front_row);
299
300 sysarg_t back_col;
301 sysarg_t back_row;
302 chargrid_get_cursor(term->backbuf, &back_col, &back_row);
303
304 bool front_visibility =
305 chargrid_get_cursor_visibility(term->frontbuf) &&
306 term->is_focused;
307 bool back_visibility =
308 chargrid_get_cursor_visibility(term->backbuf);
309
310 if (front_visibility != back_visibility) {
311 chargrid_set_cursor_visibility(term->backbuf,
312 front_visibility);
313 term_update_char(term, pixelmap, sx, sy, back_col, back_row);
314 update = true;
315 }
316
317 if ((front_col != back_col) || (front_row != back_row)) {
318 chargrid_set_cursor(term->backbuf, front_col, front_row);
319 term_update_char(term, pixelmap, sx, sy, back_col, back_row);
320 term_update_char(term, pixelmap, sx, sy, front_col, front_row);
321 update = true;
322 }
323
324 return update;
325}
326
327static void term_update(terminal_t *term)
328{
329 pixelmap_t pixelmap;
330 gfx_bitmap_alloc_t alloc;
331 gfx_coord2_t pos;
332 errno_t rc;
333
334 rc = gfx_bitmap_get_alloc(term->bmp, &alloc);
335 if (rc != EOK) {
336 return;
337 }
338
339 fibril_mutex_lock(&term->mtx);
340 pixelmap.width = term->w;
341 pixelmap.height = term->h;
342 pixelmap.data = alloc.pixels;
343
344 bool update = false;
345 sysarg_t sx = 0;
346 sysarg_t sy = 0;
347
348 if (term_update_scroll(term, &pixelmap, sx, sy)) {
349 update = true;
350 } else {
351 for (sysarg_t y = 0; y < term->rows; y++) {
352 for (sysarg_t x = 0; x < term->cols; x++) {
353 charfield_t *front_field =
354 chargrid_charfield_at(term->frontbuf, x, y);
355 charfield_t *back_field =
356 chargrid_charfield_at(term->backbuf, x, y);
357 bool cupdate = false;
358
359 if ((front_field->flags & CHAR_FLAG_DIRTY) ==
360 CHAR_FLAG_DIRTY) {
361 if (front_field->ch != back_field->ch) {
362 back_field->ch = front_field->ch;
363 cupdate = true;
364 }
365
366 if (!attrs_same(front_field->attrs,
367 back_field->attrs)) {
368 back_field->attrs = front_field->attrs;
369 cupdate = true;
370 }
371
372 front_field->flags &= ~CHAR_FLAG_DIRTY;
373 }
374
375 if (cupdate) {
376 term_update_char(term, &pixelmap, sx, sy, x, y);
377 update = true;
378 }
379 }
380 }
381 }
382
383 if (term_update_cursor(term, &pixelmap, sx, sy))
384 update = true;
385
386 if (update) {
387 pos.x = 4;
388 pos.y = 26;
389 (void) gfx_bitmap_render(term->bmp, &term->update, &pos);
390
391 term->update.p0.x = 0;
392 term->update.p0.y = 0;
393 term->update.p1.x = 0;
394 term->update.p1.y = 0;
395 }
396
397 fibril_mutex_unlock(&term->mtx);
398}
399
400static void term_repaint(terminal_t *term)
401{
402 pixelmap_t pixelmap;
403 gfx_bitmap_alloc_t alloc;
404 errno_t rc;
405
406 rc = gfx_bitmap_get_alloc(term->bmp, &alloc);
407 if (rc != EOK) {
408 printf("Error getting bitmap allocation info.\n");
409 return;
410 }
411
412 fibril_mutex_lock(&term->mtx);
413
414 pixelmap.width = term->w;
415 pixelmap.height = term->h;
416 pixelmap.data = alloc.pixels;
417
418 sysarg_t sx = 0;
419 sysarg_t sy = 0;
420
421 if (!term_update_scroll(term, &pixelmap, sx, sy)) {
422 for (sysarg_t y = 0; y < term->rows; y++) {
423 for (sysarg_t x = 0; x < term->cols; x++) {
424 charfield_t *front_field =
425 chargrid_charfield_at(term->frontbuf, x, y);
426 charfield_t *back_field =
427 chargrid_charfield_at(term->backbuf, x, y);
428
429 back_field->ch = front_field->ch;
430 back_field->attrs = front_field->attrs;
431 front_field->flags &= ~CHAR_FLAG_DIRTY;
432
433 term_update_char(term, &pixelmap, sx, sy, x, y);
434 }
435 }
436 }
437
438 term_update_cursor(term, &pixelmap, sx, sy);
439
440 fibril_mutex_unlock(&term->mtx);
441}
442
443static errno_t term_open(con_srvs_t *srvs, con_srv_t *srv)
444{
445 return EOK;
446}
447
448static errno_t term_close(con_srv_t *srv)
449{
450 return EOK;
451}
452
453static errno_t term_read(con_srv_t *srv, void *buf, size_t size, size_t *nread)
454{
455 terminal_t *term = srv_to_terminal(srv);
456 uint8_t *bbuf = buf;
457 size_t pos = 0;
458
459 /*
460 * Read input from keyboard and copy it to the buffer.
461 * We need to handle situation when wchar is split by 2 following
462 * reads.
463 */
464 while (pos < size) {
465 /* Copy to the buffer remaining characters. */
466 while ((pos < size) && (term->char_remains_len > 0)) {
467 bbuf[pos] = term->char_remains[0];
468 pos++;
469
470 /* Unshift the array. */
471 for (size_t i = 1; i < term->char_remains_len; i++)
472 term->char_remains[i - 1] = term->char_remains[i];
473
474 term->char_remains_len--;
475 }
476
477 /* Still not enough? Then get another key from the queue. */
478 if (pos < size) {
479 link_t *link = prodcons_consume(&term->input_pc);
480 cons_event_t *event = list_get_instance(link, cons_event_t, link);
481
482 /* Accept key presses of printable chars only. */
483 if (event->type == CEV_KEY && event->ev.key.type == KEY_PRESS &&
484 event->ev.key.c != 0) {
485 char32_t tmp[2] = {
486 event->ev.key.c,
487 0
488 };
489
490 wstr_to_str(term->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
491 term->char_remains_len = str_size(term->char_remains);
492 }
493
494 free(event);
495 }
496 }
497
498 *nread = size;
499 return EOK;
500}
501
502static void term_write_char(terminal_t *term, wchar_t ch)
503{
504 sysarg_t updated = 0;
505
506 fibril_mutex_lock(&term->mtx);
507
508 switch (ch) {
509 case '\n':
510 updated = chargrid_newline(term->frontbuf);
511 break;
512 case '\r':
513 break;
514 case '\t':
515 updated = chargrid_tabstop(term->frontbuf, 8);
516 break;
517 case '\b':
518 updated = chargrid_backspace(term->frontbuf);
519 break;
520 default:
521 updated = chargrid_putuchar(term->frontbuf, ch, true);
522 }
523
524 fibril_mutex_unlock(&term->mtx);
525
526 if (updated > 1)
527 term_update(term);
528}
529
530static errno_t term_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
531{
532 terminal_t *term = srv_to_terminal(srv);
533
534 size_t off = 0;
535 while (off < size)
536 term_write_char(term, str_decode(data, &off, size));
537
538 gfx_update(term->gc);
539 *nwritten = size;
540 return EOK;
541}
542
543static void term_sync(con_srv_t *srv)
544{
545 terminal_t *term = srv_to_terminal(srv);
546
547 term_update(term);
548 gfx_update(term->gc);
549}
550
551static void term_clear(con_srv_t *srv)
552{
553 terminal_t *term = srv_to_terminal(srv);
554
555 fibril_mutex_lock(&term->mtx);
556 chargrid_clear(term->frontbuf);
557 fibril_mutex_unlock(&term->mtx);
558
559 term_update(term);
560 gfx_update(term->gc);
561}
562
563static void term_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
564{
565 terminal_t *term = srv_to_terminal(srv);
566
567 fibril_mutex_lock(&term->mtx);
568 chargrid_set_cursor(term->frontbuf, col, row);
569 fibril_mutex_unlock(&term->mtx);
570
571 term_update(term);
572 gfx_update(term->gc);
573}
574
575static errno_t term_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
576{
577 terminal_t *term = srv_to_terminal(srv);
578
579 fibril_mutex_lock(&term->mtx);
580 chargrid_get_cursor(term->frontbuf, col, row);
581 fibril_mutex_unlock(&term->mtx);
582
583 return EOK;
584}
585
586static errno_t term_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
587{
588 terminal_t *term = srv_to_terminal(srv);
589
590 fibril_mutex_lock(&term->mtx);
591 *cols = term->cols;
592 *rows = term->rows;
593 fibril_mutex_unlock(&term->mtx);
594
595 return EOK;
596}
597
598static errno_t term_get_color_cap(con_srv_t *srv, console_caps_t *caps)
599{
600 (void) srv;
601 *caps = TERM_CAPS;
602
603 return EOK;
604}
605
606static void term_set_style(con_srv_t *srv, console_style_t style)
607{
608 terminal_t *term = srv_to_terminal(srv);
609
610 fibril_mutex_lock(&term->mtx);
611 chargrid_set_style(term->frontbuf, style);
612 fibril_mutex_unlock(&term->mtx);
613}
614
615static void term_set_color(con_srv_t *srv, console_color_t bgcolor,
616 console_color_t fgcolor, console_color_attr_t attr)
617{
618 terminal_t *term = srv_to_terminal(srv);
619
620 fibril_mutex_lock(&term->mtx);
621 chargrid_set_color(term->frontbuf, bgcolor, fgcolor, attr);
622 fibril_mutex_unlock(&term->mtx);
623}
624
625static void term_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
626 pixel_t fgcolor)
627{
628 terminal_t *term = srv_to_terminal(srv);
629
630 fibril_mutex_lock(&term->mtx);
631 chargrid_set_rgb_color(term->frontbuf, bgcolor, fgcolor);
632 fibril_mutex_unlock(&term->mtx);
633}
634
635static void term_set_cursor_visibility(con_srv_t *srv, bool visible)
636{
637 terminal_t *term = srv_to_terminal(srv);
638
639 fibril_mutex_lock(&term->mtx);
640 chargrid_set_cursor_visibility(term->frontbuf, visible);
641 fibril_mutex_unlock(&term->mtx);
642
643 term_update(term);
644 gfx_update(term->gc);
645}
646
647static errno_t term_get_event(con_srv_t *srv, cons_event_t *event)
648{
649 terminal_t *term = srv_to_terminal(srv);
650 link_t *link = prodcons_consume(&term->input_pc);
651 cons_event_t *ev = list_get_instance(link, cons_event_t, link);
652
653 *event = *ev;
654 free(ev);
655 return EOK;
656}
657
658/** Create shared buffer for efficient rendering.
659 *
660 * @param srv Console server
661 * @param cols Number of columns in buffer
662 * @param rows Number of rows in buffer
663 * @param rbuf Place to store pointer to new sharable buffer
664 *
665 * @return EOK on sucess or an error code
666 */
667static errno_t term_map(con_srv_t *srv, sysarg_t cols, sysarg_t rows,
668 charfield_t **rbuf)
669{
670 terminal_t *term = srv_to_terminal(srv);
671 void *buf;
672
673 fibril_mutex_lock(&term->mtx);
674
675 if (term->ubuf != NULL) {
676 fibril_mutex_unlock(&term->mtx);
677 return EBUSY;
678 }
679
680 buf = as_area_create(AS_AREA_ANY, cols * rows * sizeof(charfield_t),
681 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
682 if (buf == AS_MAP_FAILED) {
683 fibril_mutex_unlock(&term->mtx);
684 return ENOMEM;
685 }
686
687 term->ucols = cols;
688 term->urows = rows;
689 term->ubuf = buf;
690 fibril_mutex_unlock(&term->mtx);
691
692 *rbuf = buf;
693 return EOK;
694}
695
696/** Delete shared buffer.
697 *
698 * @param srv Console server
699 */
700static void term_unmap(con_srv_t *srv)
701{
702 terminal_t *term = srv_to_terminal(srv);
703 void *buf;
704
705 fibril_mutex_lock(&term->mtx);
706
707 buf = term->ubuf;
708 term->ubuf = NULL;
709
710 if (buf != NULL)
711 as_area_destroy(buf);
712
713 fibril_mutex_unlock(&term->mtx);
714}
715
716/** Update area of terminal from shared buffer.
717 *
718 * @param srv Console server
719 * @param c0 Column coordinate of top-left corner (inclusive)
720 * @param r0 Row coordinate of top-left corner (inclusive)
721 * @param c1 Column coordinate of bottom-right corner (exclusive)
722 * @param r1 Row coordinate of bottom-right corner (exclusive)
723 */
724static void term_buf_update(con_srv_t *srv, sysarg_t c0, sysarg_t r0,
725 sysarg_t c1, sysarg_t r1)
726{
727 terminal_t *term = srv_to_terminal(srv);
728 charfield_t *ch;
729 sysarg_t col, row;
730
731 fibril_mutex_lock(&term->mtx);
732
733 if (term->ubuf == NULL) {
734 fibril_mutex_unlock(&term->mtx);
735 return;
736 }
737
738 /* Make sure we have meaningful coordinates, within bounds */
739
740 if (c1 > term->ucols)
741 c1 = term->ucols;
742 if (c1 > term->cols)
743 c1 = term->cols;
744 if (c0 >= c1) {
745 fibril_mutex_unlock(&term->mtx);
746 return;
747 }
748 if (r1 > term->urows)
749 r1 = term->urows;
750 if (r1 > term->rows)
751 r1 = term->rows;
752 if (r0 >= r1) {
753 fibril_mutex_unlock(&term->mtx);
754 return;
755 }
756
757 /* Update front buffer from user buffer */
758
759 for (row = r0; row < r1; row++) {
760 for (col = c0; col < c1; col++) {
761 ch = chargrid_charfield_at(term->frontbuf, col, row);
762 *ch = term->ubuf[row * term->ucols + col];
763 }
764 }
765
766 fibril_mutex_unlock(&term->mtx);
767
768 /* Update terminal */
769 term_update(term);
770 gfx_update(term->gc);
771}
772
773static void deinit_terminal(terminal_t *term)
774{
775 list_remove(&term->link);
776
777 if (term->frontbuf)
778 chargrid_destroy(term->frontbuf);
779
780 if (term->backbuf)
781 chargrid_destroy(term->backbuf);
782}
783
784void terminal_destroy(terminal_t *term)
785{
786 deinit_terminal(term);
787 free(term);
788}
789
790static void terminal_queue_cons_event(terminal_t *term, cons_event_t *ev)
791{
792 /* Got key press/release event */
793 cons_event_t *event =
794 (cons_event_t *) malloc(sizeof(cons_event_t));
795 if (event == NULL)
796 return;
797
798 *event = *ev;
799 link_initialize(&event->link);
800
801 prodcons_produce(&term->input_pc, &event->link);
802}
803
804/** Handle window close event. */
805static void terminal_close_event(ui_window_t *window, void *arg)
806{
807 terminal_t *term = (terminal_t *) arg;
808
809 ui_quit(term->ui);
810}
811
812/** Handle window focus event. */
813static void terminal_focus_event(ui_window_t *window, void *arg)
814{
815 terminal_t *term = (terminal_t *) arg;
816
817 term->is_focused = true;
818 term_update(term);
819 gfx_update(term->gc);
820}
821
822/** Handle window keyboard event */
823static void terminal_kbd_event(ui_window_t *window, void *arg,
824 kbd_event_t *kbd_event)
825{
826 terminal_t *term = (terminal_t *) arg;
827 cons_event_t event;
828
829 event.type = CEV_KEY;
830 event.ev.key = *kbd_event;
831
832 terminal_queue_cons_event(term, &event);
833}
834
835/** Handle window position event */
836static void terminal_pos_event(ui_window_t *window, void *arg, pos_event_t *event)
837{
838 cons_event_t cevent;
839 terminal_t *term = (terminal_t *) arg;
840
841 sysarg_t sx = -term->off.x;
842 sysarg_t sy = -term->off.y;
843
844 if (event->type == POS_PRESS || event->type == POS_RELEASE ||
845 event->type == POS_DCLICK) {
846 cevent.type = CEV_POS;
847 cevent.ev.pos.type = event->type;
848 cevent.ev.pos.pos_id = event->pos_id;
849 cevent.ev.pos.btn_num = event->btn_num;
850
851 cevent.ev.pos.hpos = (event->hpos - sx) / FONT_WIDTH;
852 cevent.ev.pos.vpos = (event->vpos - sy) / FONT_SCANLINES;
853 terminal_queue_cons_event(term, &cevent);
854 }
855}
856
857/** Handle window unfocus event. */
858static void terminal_unfocus_event(ui_window_t *window, void *arg)
859{
860 terminal_t *term = (terminal_t *) arg;
861
862 term->is_focused = false;
863 term_update(term);
864 gfx_update(term->gc);
865}
866
867static void term_connection(ipc_call_t *icall, void *arg)
868{
869 terminal_t *term = NULL;
870
871 list_foreach(terms, link, terminal_t, cur) {
872 if (cur->dsid == (service_id_t) ipc_get_arg2(icall)) {
873 term = cur;
874 break;
875 }
876 }
877
878 if (term == NULL) {
879 async_answer_0(icall, ENOENT);
880 return;
881 }
882
883 if (!atomic_flag_test_and_set(&term->refcnt))
884 chargrid_set_cursor_visibility(term->frontbuf, true);
885
886 con_conn(icall, &term->srvs);
887}
888
889errno_t terminal_create(const char *display_spec, sysarg_t width,
890 sysarg_t height, terminal_flags_t flags, const char *command,
891 terminal_t **rterm)
892{
893 terminal_t *term;
894 gfx_bitmap_params_t params;
895 ui_wnd_params_t wparams;
896 gfx_rect_t rect;
897 gfx_coord2_t off;
898 gfx_rect_t wrect;
899 errno_t rc;
900
901 term = calloc(1, sizeof(terminal_t));
902 if (term == NULL) {
903 printf("Out of memory.\n");
904 return ENOMEM;
905 }
906
907 link_initialize(&term->link);
908 fibril_mutex_initialize(&term->mtx);
909 atomic_flag_clear(&term->refcnt);
910
911 prodcons_initialize(&term->input_pc);
912 term->char_remains_len = 0;
913
914 term->w = width;
915 term->h = height;
916
917 term->cols = width / FONT_WIDTH;
918 term->rows = height / FONT_SCANLINES;
919
920 term->frontbuf = NULL;
921 term->backbuf = NULL;
922
923 term->frontbuf = chargrid_create(term->cols, term->rows,
924 CHARGRID_FLAG_NONE);
925 if (!term->frontbuf) {
926 printf("Error creating front buffer.\n");
927 rc = ENOMEM;
928 goto error;
929 }
930
931 term->backbuf = chargrid_create(term->cols, term->rows,
932 CHARGRID_FLAG_NONE);
933 if (!term->backbuf) {
934 printf("Error creating back buffer.\n");
935 rc = ENOMEM;
936 goto error;
937 }
938
939 rect.p0.x = 0;
940 rect.p0.y = 0;
941 rect.p1.x = width;
942 rect.p1.y = height;
943
944 ui_wnd_params_init(&wparams);
945 wparams.caption = "Terminal";
946 if ((flags & tf_topleft) != 0)
947 wparams.placement = ui_wnd_place_top_left;
948
949 /*
950 * Compute window rectangle such that application area corresponds
951 * to rect
952 */
953 ui_wdecor_rect_from_app(wparams.style, &rect, &wrect);
954 off = wrect.p0;
955 gfx_rect_rtranslate(&off, &wrect, &wparams.rect);
956
957 term->off = off;
958
959 rc = ui_create(display_spec, &term->ui);
960 if (rc != EOK) {
961 printf("Error creating UI on %s.\n", display_spec);
962 goto error;
963 }
964
965 rc = ui_window_create(term->ui, &wparams, &term->window);
966 if (rc != EOK) {
967 printf("Error creating window.\n");
968 goto error;
969 }
970
971 term->gc = ui_window_get_gc(term->window);
972 term->ui_res = ui_window_get_res(term->window);
973
974 ui_window_set_cb(term->window, &terminal_window_cb, (void *) term);
975
976 gfx_bitmap_params_init(&params);
977 params.rect.p0.x = 0;
978 params.rect.p0.y = 0;
979 params.rect.p1.x = width;
980 params.rect.p1.y = height;
981
982 rc = gfx_bitmap_create(term->gc, &params, NULL, &term->bmp);
983 if (rc != EOK) {
984 printf("Error allocating screen bitmap.\n");
985 goto error;
986 }
987
988 chargrid_clear(term->frontbuf);
989 chargrid_clear(term->backbuf);
990 term->top_row = 0;
991
992 async_set_fallback_port_handler(term_connection, NULL);
993 con_srvs_init(&term->srvs);
994 term->srvs.ops = &con_ops;
995 term->srvs.sarg = term;
996
997 rc = loc_server_register(NAME);
998 if (rc != EOK) {
999 printf("Error registering server.\n");
1000 rc = EIO;
1001 goto error;
1002 }
1003
1004 char vc[LOC_NAME_MAXLEN + 1];
1005 snprintf(vc, LOC_NAME_MAXLEN, "%s/%" PRIu64, NAMESPACE,
1006 task_get_id());
1007
1008 rc = loc_service_register(vc, &term->dsid);
1009 if (rc != EOK) {
1010 printf("Error registering service.\n");
1011 rc = EIO;
1012 goto error;
1013 }
1014
1015 list_append(&term->link, &terms);
1016 rc = getterm(&term->wait, vc, command);
1017 if (rc != EOK)
1018 goto error;
1019
1020 term->wfid = fibril_create(terminal_wait_fibril, term);
1021 if (term->wfid == 0)
1022 goto error;
1023
1024 fibril_add_ready(term->wfid);
1025
1026 term->is_focused = true;
1027
1028 term->update.p0.x = 0;
1029 term->update.p0.y = 0;
1030 term->update.p1.x = 0;
1031 term->update.p1.y = 0;
1032
1033 term_repaint(term);
1034
1035 *rterm = term;
1036 return EOK;
1037error:
1038 if (term->window != NULL)
1039 ui_window_destroy(term->window);
1040 if (term->ui != NULL)
1041 ui_destroy(term->ui);
1042 if (term->frontbuf != NULL)
1043 chargrid_destroy(term->frontbuf);
1044 if (term->backbuf != NULL)
1045 chargrid_destroy(term->backbuf);
1046 free(term);
1047 return rc;
1048}
1049
1050static errno_t terminal_wait_fibril(void *arg)
1051{
1052 terminal_t *term = (terminal_t *)arg;
1053 task_exit_t texit;
1054 int retval;
1055
1056 /*
1057 * XXX There is no way to break the sleep if the task does not
1058 * exit.
1059 */
1060 (void) task_wait(&term->wait, &texit, &retval);
1061 ui_quit(term->ui);
1062 return EOK;
1063}
1064
1065/** @}
1066 */
Note: See TracBrowser for help on using the repository browser.