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

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

Decorate terminal window

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