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

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

Change wording

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