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

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

Allow console application to set the terminal window caption

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