source: mainline/uspace/app/terminal/terminal.c@ 2ab8ab3

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

Client-side UI rendering

It is possible to turn on and off and if turned on, one can also
enable or disable window double buffering (currently both options
are build-time).

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