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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a53ed3a was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

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