source: mainline/uspace/lib/gui/terminal.c@ c300bb5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c300bb5 was fc15120, checked in by Dzejrou <dzejrou@…>, 7 years ago

c: CTRL-D now inserts EOF

  • Property mode set to 100644
File size: 20.0 KB
Line 
1/*
2 * Copyright (c) 2012 Petr Koupy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup gui
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <errno.h>
37#include <stdlib.h>
38#include <io/chargrid.h>
39#include <surface.h>
40#include <gfx/font-8x16.h>
41#include <io/con_srv.h>
42#include <io/concaps.h>
43#include <io/console.h>
44#include <task.h>
45#include <adt/list.h>
46#include <adt/prodcons.h>
47#include <atomic.h>
48#include <stdarg.h>
49#include <str.h>
50#include "window.h"
51#include "terminal.h"
52
53#define NAME "vterm"
54#define NAMESPACE "vterm"
55
56#define LOCFS_MOUNT_POINT "/loc"
57
58#define APP_GETTERM "/app/getterm"
59
60#define TERM_CAPS \
61 (CONSOLE_CAP_STYLE | CONSOLE_CAP_INDEXED | CONSOLE_CAP_RGB)
62
63static LIST_INITIALIZE(terms);
64
65static errno_t term_open(con_srvs_t *, con_srv_t *);
66static errno_t term_close(con_srv_t *);
67static errno_t term_read(con_srv_t *, void *, size_t, size_t *);
68static errno_t term_write(con_srv_t *, void *, size_t, size_t *);
69static void term_sync(con_srv_t *);
70static void term_clear(con_srv_t *);
71static void term_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
72static errno_t term_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
73static errno_t term_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
74static errno_t term_get_color_cap(con_srv_t *, console_caps_t *);
75static void term_set_style(con_srv_t *, console_style_t);
76static void term_set_color(con_srv_t *, console_color_t, console_color_t,
77 console_color_attr_t);
78static void term_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
79static void term_set_cursor_visibility(con_srv_t *, bool);
80static errno_t term_get_event(con_srv_t *, cons_event_t *);
81
82static con_ops_t con_ops = {
83 .open = term_open,
84 .close = term_close,
85 .read = term_read,
86 .write = term_write,
87 .sync = term_sync,
88 .clear = term_clear,
89 .set_pos = term_set_pos,
90 .get_pos = term_get_pos,
91 .get_size = term_get_size,
92 .get_color_cap = term_get_color_cap,
93 .set_style = term_set_style,
94 .set_color = term_set_color,
95 .set_rgb_color = term_set_rgb_color,
96 .set_cursor_visibility = term_set_cursor_visibility,
97 .get_event = term_get_event
98};
99
100static terminal_t *srv_to_terminal(con_srv_t *srv)
101{
102 return srv->srvs->sarg;
103}
104
105static void getterm(const char *svc, const char *app)
106{
107 task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
108 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
109}
110
111static pixel_t color_table[16] = {
112 [COLOR_BLACK] = PIXEL(255, 0, 0, 0),
113 [COLOR_BLUE] = PIXEL(255, 0, 0, 240),
114 [COLOR_GREEN] = PIXEL(255, 0, 240, 0),
115 [COLOR_CYAN] = PIXEL(255, 0, 240, 240),
116 [COLOR_RED] = PIXEL(255, 240, 0, 0),
117 [COLOR_MAGENTA] = PIXEL(255, 240, 0, 240),
118 [COLOR_YELLOW] = PIXEL(255, 240, 240, 0),
119 [COLOR_WHITE] = PIXEL(255, 240, 240, 240),
120
121 [COLOR_BLACK + 8] = PIXEL(255, 0, 0, 0),
122 [COLOR_BLUE + 8] = PIXEL(255, 0, 0, 255),
123 [COLOR_GREEN + 8] = PIXEL(255, 0, 255, 0),
124 [COLOR_CYAN + 8] = PIXEL(255, 0, 255, 255),
125 [COLOR_RED + 8] = PIXEL(255, 255, 0, 0),
126 [COLOR_MAGENTA + 8] = PIXEL(255, 255, 0, 255),
127 [COLOR_YELLOW + 8] = PIXEL(255, 255, 255, 0),
128 [COLOR_WHITE + 8] = PIXEL(255, 255, 255, 255),
129};
130
131static inline void attrs_rgb(char_attrs_t attrs, pixel_t *bgcolor, pixel_t *fgcolor)
132{
133 switch (attrs.type) {
134 case CHAR_ATTR_STYLE:
135 switch (attrs.val.style) {
136 case STYLE_NORMAL:
137 *bgcolor = color_table[COLOR_WHITE];
138 *fgcolor = color_table[COLOR_BLACK];
139 break;
140 case STYLE_EMPHASIS:
141 *bgcolor = color_table[COLOR_WHITE];
142 *fgcolor = color_table[COLOR_RED];
143 break;
144 case STYLE_INVERTED:
145 *bgcolor = color_table[COLOR_BLACK];
146 *fgcolor = color_table[COLOR_WHITE];
147 break;
148 case STYLE_SELECTED:
149 *bgcolor = color_table[COLOR_RED];
150 *fgcolor = color_table[COLOR_WHITE];
151 break;
152 }
153 break;
154 case CHAR_ATTR_INDEX:
155 *bgcolor = color_table[(attrs.val.index.bgcolor & 7) |
156 ((attrs.val.index.attr & CATTR_BRIGHT) ? 8 : 0)];
157 *fgcolor = color_table[(attrs.val.index.fgcolor & 7) |
158 ((attrs.val.index.attr & CATTR_BRIGHT) ? 8 : 0)];
159 break;
160 case CHAR_ATTR_RGB:
161 *bgcolor = 0xff000000 | attrs.val.rgb.bgcolor;
162 *fgcolor = 0xff000000 | attrs.val.rgb.fgcolor;
163 break;
164 }
165}
166
167static void term_update_char(terminal_t *term, surface_t *surface,
168 sysarg_t sx, sysarg_t sy, sysarg_t col, sysarg_t row)
169{
170 charfield_t *field =
171 chargrid_charfield_at(term->backbuf, col, row);
172
173 bool inverted = chargrid_cursor_at(term->backbuf, col, row);
174
175 sysarg_t bx = sx + (col * FONT_WIDTH);
176 sysarg_t by = sy + (row * FONT_SCANLINES);
177
178 pixel_t bgcolor = 0;
179 pixel_t fgcolor = 0;
180
181 if (inverted)
182 attrs_rgb(field->attrs, &fgcolor, &bgcolor);
183 else
184 attrs_rgb(field->attrs, &bgcolor, &fgcolor);
185
186 // FIXME: Glyph type should be actually uint32_t
187 // for full UTF-32 coverage.
188
189 uint16_t glyph = fb_font_glyph(field->ch, NULL);
190
191 for (unsigned int y = 0; y < FONT_SCANLINES; y++) {
192 pixel_t *dst = pixelmap_pixel_at(
193 surface_pixmap_access(surface), bx, by + y);
194 pixel_t *dst_max = pixelmap_pixel_at(
195 surface_pixmap_access(surface), bx + FONT_WIDTH - 1, by + y);
196 if (!dst || !dst_max)
197 continue;
198 int count = FONT_WIDTH;
199 while (count-- != 0) {
200 *dst++ = (fb_font[glyph][y] & (1 << count)) ? fgcolor : bgcolor;
201 }
202 }
203 surface_add_damaged_region(surface, bx, by, FONT_WIDTH, FONT_SCANLINES);
204}
205
206static bool term_update_scroll(terminal_t *term, surface_t *surface,
207 sysarg_t sx, sysarg_t sy)
208{
209 sysarg_t top_row = chargrid_get_top_row(term->frontbuf);
210
211 if (term->top_row == top_row)
212 return false;
213
214 term->top_row = top_row;
215
216 for (sysarg_t row = 0; row < term->rows; row++) {
217 for (sysarg_t col = 0; col < term->cols; col++) {
218 charfield_t *front_field =
219 chargrid_charfield_at(term->frontbuf, col, row);
220 charfield_t *back_field =
221 chargrid_charfield_at(term->backbuf, col, row);
222 bool update = false;
223
224 if (front_field->ch != back_field->ch) {
225 back_field->ch = front_field->ch;
226 update = true;
227 }
228
229 if (!attrs_same(front_field->attrs, back_field->attrs)) {
230 back_field->attrs = front_field->attrs;
231 update = true;
232 }
233
234 front_field->flags &= ~CHAR_FLAG_DIRTY;
235
236 if (update)
237 term_update_char(term, surface, sx, sy, col, row);
238 }
239 }
240
241 return true;
242}
243
244static bool term_update_cursor(terminal_t *term, surface_t *surface,
245 sysarg_t sx, sysarg_t sy)
246{
247 bool damage = false;
248
249 sysarg_t front_col;
250 sysarg_t front_row;
251 chargrid_get_cursor(term->frontbuf, &front_col, &front_row);
252
253 sysarg_t back_col;
254 sysarg_t back_row;
255 chargrid_get_cursor(term->backbuf, &back_col, &back_row);
256
257 bool front_visibility =
258 chargrid_get_cursor_visibility(term->frontbuf) &&
259 term->widget.window->is_focused;
260 bool back_visibility =
261 chargrid_get_cursor_visibility(term->backbuf);
262
263 if (front_visibility != back_visibility) {
264 chargrid_set_cursor_visibility(term->backbuf,
265 front_visibility);
266 term_update_char(term, surface, sx, sy, back_col, back_row);
267 damage = true;
268 }
269
270 if ((front_col != back_col) || (front_row != back_row)) {
271 chargrid_set_cursor(term->backbuf, front_col, front_row);
272 term_update_char(term, surface, sx, sy, back_col, back_row);
273 term_update_char(term, surface, sx, sy, front_col, front_row);
274 damage = true;
275 }
276
277 return damage;
278}
279
280static void term_update(terminal_t *term)
281{
282 fibril_mutex_lock(&term->mtx);
283
284 surface_t *surface = window_claim(term->widget.window);
285 if (!surface) {
286 window_yield(term->widget.window);
287 fibril_mutex_unlock(&term->mtx);
288 return;
289 }
290
291 bool damage = false;
292 sysarg_t sx = term->widget.hpos;
293 sysarg_t sy = term->widget.vpos;
294
295 if (term_update_scroll(term, surface, sx, sy)) {
296 damage = true;
297 } else {
298 for (sysarg_t y = 0; y < term->rows; y++) {
299 for (sysarg_t x = 0; x < term->cols; x++) {
300 charfield_t *front_field =
301 chargrid_charfield_at(term->frontbuf, x, y);
302 charfield_t *back_field =
303 chargrid_charfield_at(term->backbuf, x, y);
304 bool update = false;
305
306 if ((front_field->flags & CHAR_FLAG_DIRTY) ==
307 CHAR_FLAG_DIRTY) {
308 if (front_field->ch != back_field->ch) {
309 back_field->ch = front_field->ch;
310 update = true;
311 }
312
313 if (!attrs_same(front_field->attrs,
314 back_field->attrs)) {
315 back_field->attrs = front_field->attrs;
316 update = true;
317 }
318
319 front_field->flags &= ~CHAR_FLAG_DIRTY;
320 }
321
322 if (update) {
323 term_update_char(term, surface, sx, sy, x, y);
324 damage = true;
325 }
326 }
327 }
328 }
329
330 if (term_update_cursor(term, surface, sx, sy))
331 damage = true;
332
333 window_yield(term->widget.window);
334
335 if (damage)
336 window_damage(term->widget.window);
337
338 fibril_mutex_unlock(&term->mtx);
339}
340
341static void term_damage(terminal_t *term)
342{
343 fibril_mutex_lock(&term->mtx);
344
345 surface_t *surface = window_claim(term->widget.window);
346 if (!surface) {
347 window_yield(term->widget.window);
348 fibril_mutex_unlock(&term->mtx);
349 return;
350 }
351
352 sysarg_t sx = term->widget.hpos;
353 sysarg_t sy = term->widget.vpos;
354
355 if (!term_update_scroll(term, surface, sx, sy)) {
356 for (sysarg_t y = 0; y < term->rows; y++) {
357 for (sysarg_t x = 0; x < term->cols; x++) {
358 charfield_t *front_field =
359 chargrid_charfield_at(term->frontbuf, x, y);
360 charfield_t *back_field =
361 chargrid_charfield_at(term->backbuf, x, y);
362
363 back_field->ch = front_field->ch;
364 back_field->attrs = front_field->attrs;
365 front_field->flags &= ~CHAR_FLAG_DIRTY;
366
367 term_update_char(term, surface, sx, sy, x, y);
368 }
369 }
370 }
371
372 term_update_cursor(term, surface, sx, sy);
373
374 window_yield(term->widget.window);
375 window_damage(term->widget.window);
376
377 fibril_mutex_unlock(&term->mtx);
378}
379
380static errno_t term_open(con_srvs_t *srvs, con_srv_t *srv)
381{
382 return EOK;
383}
384
385static errno_t term_close(con_srv_t *srv)
386{
387 return EOK;
388}
389
390static errno_t term_read(con_srv_t *srv, void *buf, size_t size, size_t *nread)
391{
392 terminal_t *term = srv_to_terminal(srv);
393 uint8_t *bbuf = buf;
394 size_t pos = 0;
395
396 /*
397 * Read input from keyboard and copy it to the buffer.
398 * We need to handle situation when wchar is split by 2 following
399 * reads.
400 */
401 while (pos < size) {
402 /* Copy to the buffer remaining characters. */
403 while ((pos < size) && (term->char_remains_len > 0)) {
404 bbuf[pos] = term->char_remains[0];
405 pos++;
406
407 /* Unshift the array. */
408 for (size_t i = 1; i < term->char_remains_len; i++)
409 term->char_remains[i - 1] = term->char_remains[i];
410
411 term->char_remains_len--;
412 }
413
414 /* Still not enough? Then get another key from the queue. */
415 if (pos < size) {
416 link_t *link = prodcons_consume(&term->input_pc);
417 cons_event_t *event = list_get_instance(link, cons_event_t, link);
418
419 /* Ctrl-D inputs end-of-file. */
420 if ((event->ev.key.mods & KM_LCTRL) != 0 &&
421 event->ev.key.key == KC_D && event->ev.key.type == KEY_PRESS) {
422 term->char_remains[term->char_remains_len] = EOF;
423 term->char_remains_len = str_size(term->char_remains);
424
425 /* Accept key presses of printable chars only. */
426 } else if (event->type == CEV_KEY && event->ev.key.type == KEY_PRESS &&
427 event->ev.key.c != 0) {
428 wchar_t tmp[2] = {
429 event->ev.key.c,
430 0
431 };
432
433 wstr_to_str(term->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
434 term->char_remains_len = str_size(term->char_remains);
435 }
436
437 free(event);
438 }
439 }
440
441 *nread = size;
442 return EOK;
443}
444
445static void term_write_char(terminal_t *term, wchar_t ch)
446{
447 sysarg_t updated = 0;
448
449 fibril_mutex_lock(&term->mtx);
450
451 switch (ch) {
452 case '\n':
453 updated = chargrid_newline(term->frontbuf);
454 break;
455 case '\r':
456 break;
457 case '\t':
458 updated = chargrid_tabstop(term->frontbuf, 8);
459 break;
460 case '\b':
461 updated = chargrid_backspace(term->frontbuf);
462 break;
463 default:
464 updated = chargrid_putwchar(term->frontbuf, ch, true);
465 }
466
467 fibril_mutex_unlock(&term->mtx);
468
469 if (updated > 1)
470 term_update(term);
471}
472
473static errno_t term_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
474{
475 terminal_t *term = srv_to_terminal(srv);
476
477 size_t off = 0;
478 while (off < size)
479 term_write_char(term, str_decode(data, &off, size));
480
481 *nwritten = size;
482 return EOK;
483}
484
485static void term_sync(con_srv_t *srv)
486{
487 terminal_t *term = srv_to_terminal(srv);
488
489 term_update(term);
490}
491
492static void term_clear(con_srv_t *srv)
493{
494 terminal_t *term = srv_to_terminal(srv);
495
496 fibril_mutex_lock(&term->mtx);
497 chargrid_clear(term->frontbuf);
498 fibril_mutex_unlock(&term->mtx);
499
500 term_update(term);
501}
502
503static void term_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
504{
505 terminal_t *term = srv_to_terminal(srv);
506
507 fibril_mutex_lock(&term->mtx);
508 chargrid_set_cursor(term->frontbuf, col, row);
509 fibril_mutex_unlock(&term->mtx);
510
511 term_update(term);
512}
513
514static errno_t term_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
515{
516 terminal_t *term = srv_to_terminal(srv);
517
518 fibril_mutex_lock(&term->mtx);
519 chargrid_get_cursor(term->frontbuf, col, row);
520 fibril_mutex_unlock(&term->mtx);
521
522 return EOK;
523}
524
525static errno_t term_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
526{
527 terminal_t *term = srv_to_terminal(srv);
528
529 fibril_mutex_lock(&term->mtx);
530 *cols = term->cols;
531 *rows = term->rows;
532 fibril_mutex_unlock(&term->mtx);
533
534 return EOK;
535}
536
537static errno_t term_get_color_cap(con_srv_t *srv, console_caps_t *caps)
538{
539 (void) srv;
540 *caps = TERM_CAPS;
541
542 return EOK;
543}
544
545static void term_set_style(con_srv_t *srv, console_style_t style)
546{
547 terminal_t *term = srv_to_terminal(srv);
548
549 fibril_mutex_lock(&term->mtx);
550 chargrid_set_style(term->frontbuf, style);
551 fibril_mutex_unlock(&term->mtx);
552}
553
554static void term_set_color(con_srv_t *srv, console_color_t bgcolor,
555 console_color_t fgcolor, console_color_attr_t attr)
556{
557 terminal_t *term = srv_to_terminal(srv);
558
559 fibril_mutex_lock(&term->mtx);
560 chargrid_set_color(term->frontbuf, bgcolor, fgcolor, attr);
561 fibril_mutex_unlock(&term->mtx);
562}
563
564static void term_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
565 pixel_t fgcolor)
566{
567 terminal_t *term = srv_to_terminal(srv);
568
569 fibril_mutex_lock(&term->mtx);
570 chargrid_set_rgb_color(term->frontbuf, bgcolor, fgcolor);
571 fibril_mutex_unlock(&term->mtx);
572}
573
574static void term_set_cursor_visibility(con_srv_t *srv, bool visible)
575{
576 terminal_t *term = srv_to_terminal(srv);
577
578 fibril_mutex_lock(&term->mtx);
579 chargrid_set_cursor_visibility(term->frontbuf, visible);
580 fibril_mutex_unlock(&term->mtx);
581
582 term_update(term);
583}
584
585static errno_t term_get_event(con_srv_t *srv, cons_event_t *event)
586{
587 terminal_t *term = srv_to_terminal(srv);
588 link_t *link = prodcons_consume(&term->input_pc);
589 cons_event_t *ev = list_get_instance(link, cons_event_t, link);
590
591 *event = *ev;
592 free(ev);
593 return EOK;
594}
595
596void deinit_terminal(terminal_t *term)
597{
598 list_remove(&term->link);
599 widget_deinit(&term->widget);
600
601 if (term->frontbuf)
602 chargrid_destroy(term->frontbuf);
603
604 if (term->backbuf)
605 chargrid_destroy(term->backbuf);
606}
607
608static void terminal_destroy(widget_t *widget)
609{
610 terminal_t *term = (terminal_t *) widget;
611
612 deinit_terminal(term);
613 free(term);
614}
615
616static void terminal_reconfigure(widget_t *widget)
617{
618 /* No-op */
619}
620
621static void terminal_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
622 sysarg_t width, sysarg_t height)
623{
624 terminal_t *term = (terminal_t *) widget;
625
626 widget_modify(widget, hpos, vpos, width, height);
627 widget->width_ideal = width;
628 widget->height_ideal = height;
629
630 term_damage(term);
631}
632
633static void terminal_repaint(widget_t *widget)
634{
635 terminal_t *term = (terminal_t *) widget;
636
637 term_damage(term);
638}
639
640static void terminal_queue_cons_event(terminal_t *term, cons_event_t *ev)
641{
642 /* Got key press/release event */
643 cons_event_t *event =
644 (cons_event_t *) malloc(sizeof(cons_event_t));
645 if (event == NULL)
646 return;
647
648 *event = *ev;
649 link_initialize(&event->link);
650
651 prodcons_produce(&term->input_pc, &event->link);
652}
653
654/* Got key press/release event */
655static void terminal_handle_keyboard_event(widget_t *widget,
656 kbd_event_t kbd_event)
657{
658 terminal_t *term = (terminal_t *) widget;
659 cons_event_t event;
660
661 event.type = CEV_KEY;
662 event.ev.key = kbd_event;
663
664 terminal_queue_cons_event(term, &event);
665}
666
667static void terminal_handle_position_event(widget_t *widget, pos_event_t pos_event)
668{
669 cons_event_t event;
670 terminal_t *term = (terminal_t *) widget;
671 sysarg_t sx = term->widget.hpos;
672 sysarg_t sy = term->widget.vpos;
673
674 if (pos_event.type == POS_PRESS) {
675 event.type = CEV_POS;
676 event.ev.pos.type = pos_event.type;
677 event.ev.pos.pos_id = pos_event.pos_id;
678 event.ev.pos.btn_num = pos_event.btn_num;
679
680 event.ev.pos.hpos = (pos_event.hpos - sx) / FONT_WIDTH;
681 event.ev.pos.vpos = (pos_event.vpos - sy) / FONT_SCANLINES;
682 terminal_queue_cons_event(term, &event);
683 }
684}
685
686static void term_connection(ipc_call_t *icall, void *arg)
687{
688 terminal_t *term = NULL;
689
690 list_foreach(terms, link, terminal_t, cur) {
691 if (cur->dsid == (service_id_t) IPC_GET_ARG2(*icall)) {
692 term = cur;
693 break;
694 }
695 }
696
697 if (term == NULL) {
698 async_answer_0(icall, ENOENT);
699 return;
700 }
701
702 if (atomic_postinc(&term->refcnt) == 0)
703 chargrid_set_cursor_visibility(term->frontbuf, true);
704
705 con_conn(icall, &term->srvs);
706}
707
708bool init_terminal(terminal_t *term, widget_t *parent, const void *data,
709 sysarg_t width, sysarg_t height)
710{
711 widget_init(&term->widget, parent, data);
712
713 link_initialize(&term->link);
714 fibril_mutex_initialize(&term->mtx);
715 atomic_set(&term->refcnt, 0);
716
717 prodcons_initialize(&term->input_pc);
718 term->char_remains_len = 0;
719
720 term->widget.width = width;
721 term->widget.height = height;
722 term->widget.width_ideal = width;
723 term->widget.height_ideal = height;
724
725 term->widget.destroy = terminal_destroy;
726 term->widget.reconfigure = terminal_reconfigure;
727 term->widget.rearrange = terminal_rearrange;
728 term->widget.repaint = terminal_repaint;
729 term->widget.handle_keyboard_event = terminal_handle_keyboard_event;
730 term->widget.handle_position_event = terminal_handle_position_event;
731
732 term->cols = width / FONT_WIDTH;
733 term->rows = height / FONT_SCANLINES;
734
735 term->frontbuf = NULL;
736 term->backbuf = NULL;
737
738 term->frontbuf = chargrid_create(term->cols, term->rows,
739 CHARGRID_FLAG_NONE);
740 if (!term->frontbuf) {
741 widget_deinit(&term->widget);
742 return false;
743 }
744
745 term->backbuf = chargrid_create(term->cols, term->rows,
746 CHARGRID_FLAG_NONE);
747 if (!term->backbuf) {
748 widget_deinit(&term->widget);
749 return false;
750 }
751
752 chargrid_clear(term->frontbuf);
753 chargrid_clear(term->backbuf);
754 term->top_row = 0;
755
756 async_set_fallback_port_handler(term_connection, NULL);
757 con_srvs_init(&term->srvs);
758 term->srvs.ops = &con_ops;
759 term->srvs.sarg = term;
760
761 errno_t rc = loc_server_register(NAME);
762 if (rc != EOK) {
763 widget_deinit(&term->widget);
764 return false;
765 }
766
767 char vc[LOC_NAME_MAXLEN + 1];
768 snprintf(vc, LOC_NAME_MAXLEN, "%s/%" PRIu64, NAMESPACE,
769 task_get_id());
770
771 rc = loc_service_register(vc, &term->dsid);
772 if (rc != EOK) {
773 widget_deinit(&term->widget);
774 return false;
775 }
776
777 list_append(&term->link, &terms);
778 getterm(vc, "/app/bdsh");
779
780 return true;
781}
782
783terminal_t *create_terminal(widget_t *parent, const void *data, sysarg_t width,
784 sysarg_t height)
785{
786 terminal_t *term = (terminal_t *) malloc(sizeof(terminal_t));
787 if (!term)
788 return NULL;
789
790 bool ret = init_terminal(term, parent, data, width, height);
791 if (!ret) {
792 free(term);
793 return NULL;
794 }
795
796 return term;
797}
798
799/** @}
800 */
Note: See TracBrowser for help on using the repository browser.