source: mainline/uspace/app/terminal/terminal.c@ 8edec53

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

Support double-click

Needed to open Navigator entries using the mouse.

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