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

Last change on this file since ca48672 was ca48672, checked in by Jiri Svoboda <jiri@…>, 8 days ago

loc_service_register() needs to take a port ID argument.

  • Property mode set to 100644
File size: 29.6 KB
RevLine 
[70814b8e]1/*
[ca48672]2 * Copyright (c) 2025 Jiri Svoboda
[70814b8e]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/**
[f03d1308]34 * @file Terminal application
[70814b8e]35 */
36
37#include <adt/list.h>
38#include <adt/prodcons.h>
[68a552f]39#include <as.h>
[70814b8e]40#include <errno.h>
[26653c9]41#include <fbfont/font-8x16.h>
[ce862ac]42#include <fibril.h>
[70814b8e]43#include <gfx/bitmap.h>
44#include <gfx/context.h>
[2ab8ab3]45#include <gfx/render.h>
[70814b8e]46#include <io/con_srv.h>
47#include <io/concaps.h>
48#include <io/console.h>
49#include <io/pixelmap.h>
[899bdfd]50#include <macros.h>
[70814b8e]51#include <stdarg.h>
[68a552f]52#include <stdio.h>
[70814b8e]53#include <stdlib.h>
[899bdfd]54#include <str_error.h>
[70814b8e]55#include <str.h>
[899bdfd]56#include <task.h>
[dcfd422]57#include <ui/resource.h>
[f03d1308]58#include <ui/ui.h>
[dcfd422]59#include <ui/wdecor.h>
[f03d1308]60#include <ui/window.h>
[70814b8e]61
62#include "terminal.h"
63
64#define NAME "terminal"
65#define NAMESPACE "terminal"
66
67#define LOCFS_MOUNT_POINT "/loc"
68
69#define APP_GETTERM "/app/getterm"
70
71#define TERM_CAPS \
[09f41d3]72 (CONSOLE_CAP_CURSORCTL | CONSOLE_CAP_STYLE | CONSOLE_CAP_INDEXED | \
73 CONSOLE_CAP_RGB)
[70814b8e]74
[899bdfd]75#define SCROLLBACK_MAX_LINES 1000
76#define MIN_WINDOW_COLS 8
77#define MIN_WINDOW_ROWS 4
78
[70814b8e]79static LIST_INITIALIZE(terms);
80
[899bdfd]81#define COLOR_BRIGHT 8
82
83static const pixel_t _basic_colors[16] = {
84 [COLOR_BLACK] = PIXEL(255, 0, 0, 0),
85 [COLOR_RED] = PIXEL(255, 170, 0, 0),
86 [COLOR_GREEN] = PIXEL(255, 0, 170, 0),
87 [COLOR_YELLOW] = PIXEL(255, 170, 85, 0),
88 [COLOR_BLUE] = PIXEL(255, 0, 0, 170),
89 [COLOR_MAGENTA] = PIXEL(255, 170, 0, 170),
90 [COLOR_CYAN] = PIXEL(255, 0, 170, 170),
91 [COLOR_WHITE] = PIXEL(255, 170, 170, 170),
92
93 [COLOR_BLACK | COLOR_BRIGHT] = PIXEL(255, 85, 85, 85),
94 [COLOR_RED | COLOR_BRIGHT] = PIXEL(255, 255, 85, 85),
95 [COLOR_GREEN | COLOR_BRIGHT] = PIXEL(255, 85, 255, 85),
96 [COLOR_YELLOW | COLOR_BRIGHT] = PIXEL(255, 255, 255, 85),
97 [COLOR_BLUE | COLOR_BRIGHT] = PIXEL(255, 85, 85, 255),
98 [COLOR_MAGENTA | COLOR_BRIGHT] = PIXEL(255, 255, 85, 255),
99 [COLOR_CYAN | COLOR_BRIGHT] = PIXEL(255, 85, 255, 255),
100 [COLOR_WHITE | COLOR_BRIGHT] = PIXEL(255, 255, 255, 255),
101};
102
[70814b8e]103static errno_t term_open(con_srvs_t *, con_srv_t *);
104static errno_t term_close(con_srv_t *);
105static errno_t term_read(con_srv_t *, void *, size_t, size_t *);
106static errno_t term_write(con_srv_t *, void *, size_t, size_t *);
107static void term_sync(con_srv_t *);
108static void term_clear(con_srv_t *);
109static void term_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
110static errno_t term_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
111static errno_t term_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
112static errno_t term_get_color_cap(con_srv_t *, console_caps_t *);
113static void term_set_style(con_srv_t *, console_style_t);
114static void term_set_color(con_srv_t *, console_color_t, console_color_t,
115 console_color_attr_t);
116static void term_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
117static void term_set_cursor_visibility(con_srv_t *, bool);
[b48e680f]118static errno_t term_set_caption(con_srv_t *, const char *);
[70814b8e]119static errno_t term_get_event(con_srv_t *, cons_event_t *);
[68a552f]120static errno_t term_map(con_srv_t *, sysarg_t, sysarg_t, charfield_t **);
121static void term_unmap(con_srv_t *);
122static void term_buf_update(con_srv_t *, sysarg_t, sysarg_t, sysarg_t,
123 sysarg_t);
[70814b8e]124
125static con_ops_t con_ops = {
126 .open = term_open,
127 .close = term_close,
128 .read = term_read,
129 .write = term_write,
130 .sync = term_sync,
131 .clear = term_clear,
132 .set_pos = term_set_pos,
133 .get_pos = term_get_pos,
134 .get_size = term_get_size,
135 .get_color_cap = term_get_color_cap,
136 .set_style = term_set_style,
137 .set_color = term_set_color,
138 .set_rgb_color = term_set_rgb_color,
139 .set_cursor_visibility = term_set_cursor_visibility,
[b48e680f]140 .set_caption = term_set_caption,
[68a552f]141 .get_event = term_get_event,
142 .map = term_map,
143 .unmap = term_unmap,
144 .update = term_buf_update
[70814b8e]145};
146
[f03d1308]147static void terminal_close_event(ui_window_t *, void *);
[46a47c0]148static void terminal_focus_event(ui_window_t *, void *, unsigned);
[899bdfd]149static void terminal_resize_event(ui_window_t *, void *);
[f03d1308]150static void terminal_kbd_event(ui_window_t *, void *, kbd_event_t *);
151static void terminal_pos_event(ui_window_t *, void *, pos_event_t *);
[46a47c0]152static void terminal_unfocus_event(ui_window_t *, void *, unsigned);
[899bdfd]153static void terminal_maximize_event(ui_window_t *, void *);
154static void terminal_unmaximize_event(ui_window_t *, void *);
[f03d1308]155
156static ui_window_cb_t terminal_window_cb = {
157 .close = terminal_close_event,
158 .focus = terminal_focus_event,
[899bdfd]159 .resize = terminal_resize_event,
[f03d1308]160 .kbd = terminal_kbd_event,
161 .pos = terminal_pos_event,
[899bdfd]162 .unfocus = terminal_unfocus_event,
163 .maximize = terminal_maximize_event,
164 .unmaximize = terminal_unmaximize_event,
[70814b8e]165};
166
[ce862ac]167static errno_t terminal_wait_fibril(void *);
168
[70814b8e]169static terminal_t *srv_to_terminal(con_srv_t *srv)
170{
171 return srv->srvs->sarg;
172}
173
[ce862ac]174static errno_t getterm(task_wait_t *wait, const char *svc, const char *app)
[70814b8e]175{
[ce862ac]176 return task_spawnl(NULL, wait, APP_GETTERM, APP_GETTERM, svc,
[70814b8e]177 LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
178}
179
[899bdfd]180static pixel_t termui_color_to_pixel(termui_color_t c)
181{
182 uint8_t r, g, b;
183 termui_color_to_rgb(c, &r, &g, &b);
184 return PIXEL(255, r, g, b);
185}
[805a149]186
[899bdfd]187static termui_color_t termui_color_from_pixel(pixel_t pixel)
188{
189 return termui_color_from_rgb(RED(pixel), GREEN(pixel), BLUE(pixel));
190}
[70814b8e]191
[899bdfd]192static termui_cell_t charfield_to_termui_cell(terminal_t *term, const charfield_t *cf)
[70814b8e]193{
[899bdfd]194 termui_cell_t cell = { };
195
196 cell.glyph_idx = fb_font_glyph(cf->ch, NULL);
197
198 switch (cf->attrs.type) {
[70814b8e]199 case CHAR_ATTR_STYLE:
[899bdfd]200 switch (cf->attrs.val.style) {
[70814b8e]201 case STYLE_NORMAL:
[899bdfd]202 cell.bgcolor = term->default_bgcolor;
203 cell.fgcolor = term->default_fgcolor;
[70814b8e]204 break;
205 case STYLE_EMPHASIS:
[899bdfd]206 cell.bgcolor = term->emphasis_bgcolor;
207 cell.fgcolor = term->emphasis_fgcolor;
[70814b8e]208 break;
209 case STYLE_INVERTED:
[899bdfd]210 cell.bgcolor = term->default_bgcolor;
211 cell.fgcolor = term->default_fgcolor;
212 cell.inverted = 1;
[70814b8e]213 break;
214 case STYLE_SELECTED:
[899bdfd]215 cell.bgcolor = term->selection_bgcolor;
216 cell.fgcolor = term->selection_fgcolor;
[70814b8e]217 break;
218 }
219 break;
[899bdfd]220
[70814b8e]221 case CHAR_ATTR_INDEX:
[899bdfd]222 char_attr_index_t index = cf->attrs.val.index;
223
224 int bright = (index.attr & CATTR_BRIGHT) ? COLOR_BRIGHT : 0;
[4c2339b]225 pixel_t bgcolor = _basic_colors[index.bgcolor];
[899bdfd]226 pixel_t fgcolor = _basic_colors[index.fgcolor | bright];
227 cell.bgcolor = termui_color_from_pixel(bgcolor);
228 cell.fgcolor = termui_color_from_pixel(fgcolor);
229
230 if (index.attr & CATTR_BLINK)
231 cell.blink = 1;
232
[70814b8e]233 break;
[899bdfd]234
[70814b8e]235 case CHAR_ATTR_RGB:
[899bdfd]236 cell.bgcolor = termui_color_from_pixel(cf->attrs.val.rgb.bgcolor);
237 cell.fgcolor = termui_color_from_pixel(cf->attrs.val.rgb.fgcolor);
[70814b8e]238 break;
239 }
[899bdfd]240
241 return cell;
[70814b8e]242}
243
244static void term_update_region(terminal_t *term, sysarg_t x, sysarg_t y,
245 sysarg_t w, sysarg_t h)
246{
247 gfx_rect_t rect;
248 gfx_rect_t nupdate;
249
250 rect.p0.x = x;
251 rect.p0.y = y;
252 rect.p1.x = x + w;
253 rect.p1.y = y + h;
254
255 gfx_rect_envelope(&term->update, &rect, &nupdate);
256 term->update = nupdate;
257}
258
[899bdfd]259static void term_draw_cell(terminal_t *term, pixelmap_t *pixelmap, int col, int row, const termui_cell_t *cell)
[70814b8e]260{
[899bdfd]261 termui_color_t bg = cell->bgcolor;
262 if (bg == TERMUI_COLOR_DEFAULT)
263 bg = term->default_bgcolor;
[70814b8e]264
[899bdfd]265 termui_color_t fg = cell->fgcolor;
266 if (fg == TERMUI_COLOR_DEFAULT)
267 fg = term->default_fgcolor;
[70814b8e]268
[899bdfd]269 pixel_t bgcolor = termui_color_to_pixel(bg);
270 pixel_t fgcolor = termui_color_to_pixel(fg);
[70814b8e]271
[899bdfd]272 int bx = col * FONT_WIDTH;
273 int by = row * FONT_SCANLINES;
[70814b8e]274
[899bdfd]275 // TODO: support bold/italic/underline/strike/blink styling
276
277 if (cell->inverted ^ cell->cursor) {
278 pixel_t tmp = bgcolor;
279 bgcolor = fgcolor;
280 fgcolor = tmp;
281 }
[70814b8e]282
[899bdfd]283 uint32_t glyph = cell->glyph_idx;
284 assert(glyph < FONT_GLYPHS);
[70814b8e]285
[899bdfd]286 if (glyph == 0)
287 glyph = fb_font_glyph(U' ', NULL);
[70814b8e]288
289 for (unsigned int y = 0; y < FONT_SCANLINES; y++) {
290 pixel_t *dst = pixelmap_pixel_at(pixelmap, bx, by + y);
291 pixel_t *dst_max = pixelmap_pixel_at(pixelmap, bx + FONT_WIDTH - 1, by + y);
292 if (!dst || !dst_max)
293 continue;
294 int count = FONT_WIDTH;
295 while (count-- != 0) {
296 *dst++ = (fb_font[glyph][y] & (1 << count)) ? fgcolor : bgcolor;
297 }
298 }
[899bdfd]299
[70814b8e]300 term_update_region(term, bx, by, FONT_WIDTH, FONT_SCANLINES);
301}
302
[899bdfd]303static void term_render(terminal_t *term)
[70814b8e]304{
[2cf8f994]305 (void) gfx_bitmap_render(term->bmp, &term->update, &term->off);
[70814b8e]306
[899bdfd]307 term->update.p0.x = 0;
308 term->update.p0.y = 0;
309 term->update.p1.x = 0;
310 term->update.p1.y = 0;
311}
[70814b8e]312
[899bdfd]313static void termui_refresh_cb(void *userdata)
314{
315 terminal_t *term = userdata;
[70814b8e]316
[899bdfd]317 termui_force_viewport_update(term->termui, 0, termui_get_rows(term->termui));
[70814b8e]318}
319
[899bdfd]320static void termui_scroll_cb(void *userdata, int delta)
[70814b8e]321{
[899bdfd]322 (void) delta;
[70814b8e]323
[899bdfd]324 // Until we have support for hardware accelerated scrolling, just redraw everything.
325 termui_refresh_cb(userdata);
[70814b8e]326}
327
[899bdfd]328static pixelmap_t term_get_pixelmap(terminal_t *term)
[70814b8e]329{
[899bdfd]330 pixelmap_t pixelmap = { };
[70814b8e]331 gfx_bitmap_alloc_t alloc;
332
[899bdfd]333 errno_t rc = gfx_bitmap_get_alloc(term->bmp, &alloc);
334 if (rc != EOK)
335 return pixelmap;
[70814b8e]336
337 pixelmap.width = term->w;
338 pixelmap.height = term->h;
339 pixelmap.data = alloc.pixels;
[899bdfd]340 return pixelmap;
[70814b8e]341}
342
[899bdfd]343static void term_clear_bitmap(terminal_t *term, pixel_t color)
[70814b8e]344{
[899bdfd]345 pixelmap_t pixelmap = term_get_pixelmap(term);
346 if (pixelmap.data == NULL)
[70814b8e]347 return;
348
[899bdfd]349 sysarg_t pixels = pixelmap.height * pixelmap.width;
350 for (sysarg_t i = 0; i < pixels; i++)
351 pixelmap.data[i] = color;
[70814b8e]352
[899bdfd]353 term_update_region(term, 0, 0, pixelmap.width, pixelmap.height);
354}
[70814b8e]355
[899bdfd]356static void termui_update_cb(void *userdata, int col, int row, const termui_cell_t *cell, int len)
357{
358 terminal_t *term = userdata;
[70814b8e]359
[899bdfd]360 pixelmap_t pixelmap = term_get_pixelmap(term);
361 if (pixelmap.data == NULL)
362 return;
[70814b8e]363
[899bdfd]364 for (int i = 0; i < len; i++)
365 term_draw_cell(term, &pixelmap, col + i, row, &cell[i]);
[70814b8e]366}
367
368static errno_t term_open(con_srvs_t *srvs, con_srv_t *srv)
369{
370 return EOK;
371}
372
373static errno_t term_close(con_srv_t *srv)
374{
375 return EOK;
376}
377
378static errno_t term_read(con_srv_t *srv, void *buf, size_t size, size_t *nread)
379{
380 terminal_t *term = srv_to_terminal(srv);
381 uint8_t *bbuf = buf;
382 size_t pos = 0;
383
384 /*
385 * Read input from keyboard and copy it to the buffer.
386 * We need to handle situation when wchar is split by 2 following
387 * reads.
388 */
389 while (pos < size) {
390 /* Copy to the buffer remaining characters. */
391 while ((pos < size) && (term->char_remains_len > 0)) {
392 bbuf[pos] = term->char_remains[0];
393 pos++;
394
395 /* Unshift the array. */
396 for (size_t i = 1; i < term->char_remains_len; i++)
397 term->char_remains[i - 1] = term->char_remains[i];
398
399 term->char_remains_len--;
400 }
401
402 /* Still not enough? Then get another key from the queue. */
403 if (pos < size) {
404 link_t *link = prodcons_consume(&term->input_pc);
[e273e9e]405 terminal_event_t *qevent = list_get_instance(link,
406 terminal_event_t, link);
407 cons_event_t *event = &qevent->ev;
[70814b8e]408
409 /* Accept key presses of printable chars only. */
410 if (event->type == CEV_KEY && event->ev.key.type == KEY_PRESS &&
411 event->ev.key.c != 0) {
412 char32_t tmp[2] = {
413 event->ev.key.c,
414 0
415 };
416
417 wstr_to_str(term->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
418 term->char_remains_len = str_size(term->char_remains);
419 }
420
[e273e9e]421 free(qevent);
[70814b8e]422 }
423 }
424
425 *nread = size;
426 return EOK;
427}
428
429static void term_write_char(terminal_t *term, wchar_t ch)
430{
431 switch (ch) {
[899bdfd]432 case L'\n':
433 termui_put_crlf(term->termui);
[70814b8e]434 break;
[899bdfd]435 case L'\r':
436 termui_put_cr(term->termui);
[70814b8e]437 break;
[899bdfd]438 case L'\t':
439 termui_put_tab(term->termui);
[70814b8e]440 break;
[899bdfd]441 case L'\b':
442 termui_put_backspace(term->termui);
[70814b8e]443 break;
444 default:
[899bdfd]445 // TODO: For some languages, we might need support for combining
446 // characters. Currently, we assume every unicode code point is
447 // an individual printed character, which is not always the case.
448 termui_put_glyph(term->termui, fb_font_glyph(ch, NULL), 1);
449 break;
[70814b8e]450 }
451}
452
453static errno_t term_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
454{
455 terminal_t *term = srv_to_terminal(srv);
456
[899bdfd]457 fibril_mutex_lock(&term->mtx);
458
[70814b8e]459 size_t off = 0;
460 while (off < size)
461 term_write_char(term, str_decode(data, &off, size));
462
[899bdfd]463 fibril_mutex_unlock(&term->mtx);
464
465 term_render(term);
[2ab8ab3]466 gfx_update(term->gc);
[70814b8e]467 *nwritten = size;
[899bdfd]468
[70814b8e]469 return EOK;
470}
471
472static void term_sync(con_srv_t *srv)
473{
474 terminal_t *term = srv_to_terminal(srv);
475
[899bdfd]476 term_render(term);
[2ab8ab3]477 gfx_update(term->gc);
[70814b8e]478}
479
480static void term_clear(con_srv_t *srv)
481{
482 terminal_t *term = srv_to_terminal(srv);
483
484 fibril_mutex_lock(&term->mtx);
[899bdfd]485 termui_clear_screen(term->termui);
[70814b8e]486 fibril_mutex_unlock(&term->mtx);
487
[899bdfd]488 term_render(term);
[2ab8ab3]489 gfx_update(term->gc);
[70814b8e]490}
491
492static void term_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
493{
494 terminal_t *term = srv_to_terminal(srv);
495
496 fibril_mutex_lock(&term->mtx);
[899bdfd]497 termui_set_pos(term->termui, col, row);
[70814b8e]498 fibril_mutex_unlock(&term->mtx);
499
[899bdfd]500 term_render(term);
[2ab8ab3]501 gfx_update(term->gc);
[70814b8e]502}
503
504static errno_t term_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
505{
506 terminal_t *term = srv_to_terminal(srv);
507
508 fibril_mutex_lock(&term->mtx);
[899bdfd]509 int irow, icol;
510 termui_get_pos(term->termui, &icol, &irow);
[70814b8e]511 fibril_mutex_unlock(&term->mtx);
512
[899bdfd]513 *col = icol;
514 *row = irow;
515
[70814b8e]516 return EOK;
517}
518
519static errno_t term_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
520{
521 terminal_t *term = srv_to_terminal(srv);
522
523 fibril_mutex_lock(&term->mtx);
[899bdfd]524 *cols = termui_get_cols(term->termui);
525 *rows = termui_get_rows(term->termui);
[70814b8e]526 fibril_mutex_unlock(&term->mtx);
527
528 return EOK;
529}
530
531static errno_t term_get_color_cap(con_srv_t *srv, console_caps_t *caps)
532{
533 (void) srv;
534 *caps = TERM_CAPS;
535
536 return EOK;
537}
538
539static void term_set_style(con_srv_t *srv, console_style_t style)
540{
541 terminal_t *term = srv_to_terminal(srv);
542
[899bdfd]543 termui_cell_t cellstyle = { };
544
545 switch (style) {
546 case STYLE_NORMAL:
547 cellstyle.bgcolor = term->default_bgcolor;
548 cellstyle.fgcolor = term->default_fgcolor;
549 break;
550 case STYLE_EMPHASIS:
551 cellstyle.bgcolor = term->emphasis_bgcolor;
552 cellstyle.fgcolor = term->emphasis_fgcolor;
553 break;
554 case STYLE_INVERTED:
555 cellstyle.bgcolor = term->default_bgcolor;
556 cellstyle.fgcolor = term->default_fgcolor;
557 cellstyle.inverted = 1;
558 break;
559 case STYLE_SELECTED:
560 cellstyle.bgcolor = term->selection_bgcolor;
561 cellstyle.fgcolor = term->selection_fgcolor;
562 break;
563 }
564
[70814b8e]565 fibril_mutex_lock(&term->mtx);
[899bdfd]566 termui_set_style(term->termui, cellstyle);
[70814b8e]567 fibril_mutex_unlock(&term->mtx);
568}
569
570static void term_set_color(con_srv_t *srv, console_color_t bgcolor,
571 console_color_t fgcolor, console_color_attr_t attr)
572{
573 terminal_t *term = srv_to_terminal(srv);
574
[899bdfd]575 int bright = (attr & CATTR_BRIGHT) ? COLOR_BRIGHT : 0;
576
577 termui_cell_t cellstyle = { };
[4c2339b]578 cellstyle.bgcolor = termui_color_from_pixel(_basic_colors[bgcolor]);
[899bdfd]579 cellstyle.fgcolor = termui_color_from_pixel(_basic_colors[fgcolor | bright]);
580
581 if (attr & CATTR_BLINK)
582 cellstyle.blink = 1;
583
[70814b8e]584 fibril_mutex_lock(&term->mtx);
[899bdfd]585 termui_set_style(term->termui, cellstyle);
[70814b8e]586 fibril_mutex_unlock(&term->mtx);
587}
588
589static void term_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
590 pixel_t fgcolor)
591{
592 terminal_t *term = srv_to_terminal(srv);
[899bdfd]593 termui_cell_t cellstyle = {
594 .bgcolor = termui_color_from_pixel(bgcolor),
595 .fgcolor = termui_color_from_pixel(fgcolor),
596 };
[70814b8e]597
598 fibril_mutex_lock(&term->mtx);
[899bdfd]599 termui_set_style(term->termui, cellstyle);
[70814b8e]600 fibril_mutex_unlock(&term->mtx);
601}
602
603static void term_set_cursor_visibility(con_srv_t *srv, bool visible)
604{
605 terminal_t *term = srv_to_terminal(srv);
606
607 fibril_mutex_lock(&term->mtx);
[899bdfd]608 termui_set_cursor_visibility(term->termui, visible);
[70814b8e]609 fibril_mutex_unlock(&term->mtx);
610
[899bdfd]611 term_render(term);
[2ab8ab3]612 gfx_update(term->gc);
[70814b8e]613}
614
[b48e680f]615static errno_t term_set_caption(con_srv_t *srv, const char *caption)
616{
617 terminal_t *term = srv_to_terminal(srv);
618 const char *cap;
619
620 fibril_mutex_lock(&term->mtx);
621
622 if (str_size(caption) > 0)
623 cap = caption;
624 else
625 cap = "Terminal";
626
627 ui_window_set_caption(term->window, cap);
628 fibril_mutex_unlock(&term->mtx);
629
[899bdfd]630 term_render(term);
[b48e680f]631 gfx_update(term->gc);
632 return EOK;
633}
634
[70814b8e]635static errno_t term_get_event(con_srv_t *srv, cons_event_t *event)
636{
637 terminal_t *term = srv_to_terminal(srv);
638 link_t *link = prodcons_consume(&term->input_pc);
[e273e9e]639 terminal_event_t *ev = list_get_instance(link, terminal_event_t, link);
[70814b8e]640
[e273e9e]641 *event = ev->ev;
[70814b8e]642 free(ev);
643 return EOK;
644}
645
[68a552f]646/** Create shared buffer for efficient rendering.
647 *
648 * @param srv Console server
649 * @param cols Number of columns in buffer
650 * @param rows Number of rows in buffer
651 * @param rbuf Place to store pointer to new sharable buffer
652 *
653 * @return EOK on sucess or an error code
654 */
655static errno_t term_map(con_srv_t *srv, sysarg_t cols, sysarg_t rows,
656 charfield_t **rbuf)
657{
658 terminal_t *term = srv_to_terminal(srv);
659 void *buf;
660
661 fibril_mutex_lock(&term->mtx);
662
663 if (term->ubuf != NULL) {
664 fibril_mutex_unlock(&term->mtx);
665 return EBUSY;
666 }
667
668 buf = as_area_create(AS_AREA_ANY, cols * rows * sizeof(charfield_t),
669 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
670 if (buf == AS_MAP_FAILED) {
671 fibril_mutex_unlock(&term->mtx);
672 return ENOMEM;
673 }
674
675 term->ucols = cols;
676 term->urows = rows;
677 term->ubuf = buf;
[899bdfd]678
679 /* Scroll back to active screen. */
680 termui_history_scroll(term->termui, INT_MAX);
681
[68a552f]682 fibril_mutex_unlock(&term->mtx);
683
684 *rbuf = buf;
685 return EOK;
686}
687
688/** Delete shared buffer.
689 *
690 * @param srv Console server
691 */
692static void term_unmap(con_srv_t *srv)
693{
694 terminal_t *term = srv_to_terminal(srv);
695 void *buf;
696
697 fibril_mutex_lock(&term->mtx);
698
699 buf = term->ubuf;
700 term->ubuf = NULL;
701
[899bdfd]702 termui_wipe_screen(term->termui, 0);
[68a552f]703
704 fibril_mutex_unlock(&term->mtx);
[899bdfd]705
706 /* Update terminal */
707 term_render(term);
708 gfx_update(term->gc);
709
710 if (buf != NULL)
711 as_area_destroy(buf);
[68a552f]712}
713
714/** Update area of terminal from shared buffer.
715 *
716 * @param srv Console server
717 * @param c0 Column coordinate of top-left corner (inclusive)
718 * @param r0 Row coordinate of top-left corner (inclusive)
719 * @param c1 Column coordinate of bottom-right corner (exclusive)
720 * @param r1 Row coordinate of bottom-right corner (exclusive)
721 */
722static void term_buf_update(con_srv_t *srv, sysarg_t c0, sysarg_t r0,
723 sysarg_t c1, sysarg_t r1)
724{
725 terminal_t *term = srv_to_terminal(srv);
726
727 fibril_mutex_lock(&term->mtx);
728
729 if (term->ubuf == NULL) {
730 fibril_mutex_unlock(&term->mtx);
731 return;
732 }
733
734 /* Make sure we have meaningful coordinates, within bounds */
[899bdfd]735 c1 = min(c1, term->ucols);
736 c1 = min(c1, (sysarg_t) termui_get_cols(term->termui));
737 r1 = min(r1, term->urows);
738 r1 = min(r1, (sysarg_t) termui_get_rows(term->termui));
[68a552f]739
[899bdfd]740 if (c0 >= c1 || r0 >= r1) {
[68a552f]741 fibril_mutex_unlock(&term->mtx);
742 return;
743 }
744
745 /* Update front buffer from user buffer */
746
[899bdfd]747 for (sysarg_t row = r0; row < r1; row++) {
748 termui_cell_t *cells = termui_get_active_row(term->termui, row);
749
750 for (sysarg_t col = c0; col < c1; col++) {
751 cells[col] = charfield_to_termui_cell(term, &term->ubuf[row * term->ucols + col]);
[68a552f]752 }
[899bdfd]753
754 termui_update_cb(term, c0, row, &cells[c0], c1 - c0);
[68a552f]755 }
756
757 fibril_mutex_unlock(&term->mtx);
758
759 /* Update terminal */
[899bdfd]760 term_render(term);
[68a552f]761 gfx_update(term->gc);
762}
763
[899bdfd]764static errno_t terminal_window_resize(terminal_t *term)
[70814b8e]765{
[899bdfd]766 gfx_rect_t rect;
767 ui_window_get_app_rect(term->window, &rect);
768
769 int width = rect.p1.x - rect.p0.x;
770 int height = rect.p1.y - rect.p0.y;
771
772 if (!term->gc)
773 term->gc = ui_window_get_gc(term->window);
774 else
775 assert(term->gc == ui_window_get_gc(term->window));
776
777 if (!term->ui_res)
778 term->ui_res = ui_window_get_res(term->window);
779 else
780 assert(term->ui_res == ui_window_get_res(term->window));
781
782 gfx_bitmap_t *new_bmp;
783 gfx_bitmap_params_t params;
784 gfx_bitmap_params_init(&params);
785 params.rect.p0.x = 0;
786 params.rect.p0.y = 0;
787 params.rect.p1.x = width;
788 params.rect.p1.y = height;
789
790 errno_t rc = gfx_bitmap_create(term->gc, &params, NULL, &new_bmp);
791 if (rc != EOK) {
792 fprintf(stderr, "Error allocating new screen bitmap: %s\n", str_error(rc));
793 return rc;
794 }
795
796 if (term->bmp) {
797 rc = gfx_bitmap_destroy(term->bmp);
798 if (rc != EOK)
799 fprintf(stderr, "Error deallocating old screen bitmap: %s\n", str_error(rc));
800 }
[70814b8e]801
[899bdfd]802 term->bmp = new_bmp;
803 term->w = width;
804 term->h = height;
[70814b8e]805
[899bdfd]806 term_clear_bitmap(term, termui_color_to_pixel(term->default_bgcolor));
807
808 return EOK;
[70814b8e]809}
810
811void terminal_destroy(terminal_t *term)
812{
[899bdfd]813 list_remove(&term->link);
814
815 termui_destroy(term->termui);
816
817 if (term->ubuf)
818 as_area_destroy(term->ubuf);
819
[2cf8f994]820 ui_destroy(term->ui);
[70814b8e]821 free(term);
822}
823
824static void terminal_queue_cons_event(terminal_t *term, cons_event_t *ev)
825{
826 /* Got key press/release event */
[e273e9e]827 terminal_event_t *event =
828 (terminal_event_t *) malloc(sizeof(terminal_event_t));
[70814b8e]829 if (event == NULL)
830 return;
831
[e273e9e]832 event->ev = *ev;
[70814b8e]833 link_initialize(&event->link);
834
835 prodcons_produce(&term->input_pc, &event->link);
836}
837
[dcfd422]838/** Handle window close event. */
[f03d1308]839static void terminal_close_event(ui_window_t *window, void *arg)
[dcfd422]840{
841 terminal_t *term = (terminal_t *) arg;
842
[ce862ac]843 ui_quit(term->ui);
[dcfd422]844}
845
846/** Handle window focus event. */
[46a47c0]847static void terminal_focus_event(ui_window_t *window, void *arg,
848 unsigned nfocus)
[dcfd422]849{
850 terminal_t *term = (terminal_t *) arg;
851
[46a47c0]852 (void)nfocus;
[f03d1308]853 term->is_focused = true;
[899bdfd]854 term_render(term);
[2ab8ab3]855 gfx_update(term->gc);
[dcfd422]856}
857
[899bdfd]858static void terminal_resize_handler(ui_window_t *window, void *arg)
859{
860 terminal_t *term = (terminal_t *) arg;
861
862 fibril_mutex_lock(&term->mtx);
863
864 errno_t rc = terminal_window_resize(term);
865 if (rc == EOK) {
866 (void) termui_resize(term->termui, term->w / FONT_WIDTH, term->h / FONT_SCANLINES, SCROLLBACK_MAX_LINES);
867 termui_refresh_cb(term);
868 term_render(term);
869 gfx_update(term->gc);
870
871 cons_event_t event = { .type = CEV_RESIZE };
872 terminal_queue_cons_event(term, &event);
873 }
874
875 fibril_mutex_unlock(&term->mtx);
876}
877
878static void terminal_resize_event(ui_window_t *window, void *arg)
879{
880 ui_window_def_resize(window);
881 terminal_resize_handler(window, arg);
882}
883
884static void terminal_maximize_event(ui_window_t *window, void *arg)
885{
886 ui_window_def_maximize(window);
887 terminal_resize_handler(window, arg);
888}
889
890static void terminal_unmaximize_event(ui_window_t *window, void *arg)
891{
892 ui_window_def_unmaximize(window);
893 terminal_resize_handler(window, arg);
894}
895
[dcfd422]896/** Handle window keyboard event */
[f03d1308]897static void terminal_kbd_event(ui_window_t *window, void *arg,
898 kbd_event_t *kbd_event)
[70814b8e]899{
900 terminal_t *term = (terminal_t *) arg;
901 cons_event_t event;
902
903 event.type = CEV_KEY;
904 event.ev.key = *kbd_event;
905
[899bdfd]906 const int PAGE_ROWS = (termui_get_rows(term->termui) * 2) / 3;
907
908 fibril_mutex_lock(&term->mtx);
909
910 if (!term->ubuf && kbd_event->type == KEY_PRESS &&
911 (kbd_event->key == KC_PAGE_UP || kbd_event->key == KC_PAGE_DOWN)) {
912
913 termui_history_scroll(term->termui,
914 (kbd_event->key == KC_PAGE_UP) ? -PAGE_ROWS : PAGE_ROWS);
915
916 term_render(term);
917 gfx_update(term->gc);
918 } else {
919 terminal_queue_cons_event(term, &event);
920 }
921
922 fibril_mutex_unlock(&term->mtx);
[70814b8e]923}
924
[dcfd422]925/** Handle window position event */
[f03d1308]926static void terminal_pos_event(ui_window_t *window, void *arg, pos_event_t *event)
[dcfd422]927{
[5a43bd0]928 cons_event_t cevent;
[dcfd422]929 terminal_t *term = (terminal_t *) arg;
930
[899bdfd]931 switch (event->type) {
932 case POS_UPDATE:
933 return;
934
935 case POS_PRESS:
936 case POS_RELEASE:
937 case POS_DCLICK:
938 }
939
940 /* Ignore mouse events when we're in scrollback mode. */
941 if (termui_scrollback_is_active(term->termui))
942 return;
943
[2cf8f994]944 sysarg_t sx = term->off.x;
945 sysarg_t sy = term->off.y;
[5a43bd0]946
[899bdfd]947 if (event->hpos < sx || event->vpos < sy)
948 return;
949
950 cevent.type = CEV_POS;
951 cevent.ev.pos.type = event->type;
952 cevent.ev.pos.pos_id = event->pos_id;
953 cevent.ev.pos.btn_num = event->btn_num;
954
955 cevent.ev.pos.hpos = (event->hpos - sx) / FONT_WIDTH;
956 cevent.ev.pos.vpos = (event->vpos - sy) / FONT_SCANLINES;
[5a43bd0]957
[899bdfd]958 /* Filter out events outside the terminal area. */
959 int cols = termui_get_cols(term->termui);
960 int rows = termui_get_rows(term->termui);
961
962 if (cevent.ev.pos.hpos < (sysarg_t) cols && cevent.ev.pos.vpos < (sysarg_t) rows)
[5a43bd0]963 terminal_queue_cons_event(term, &cevent);
[dcfd422]964}
965
966/** Handle window unfocus event. */
[46a47c0]967static void terminal_unfocus_event(ui_window_t *window, void *arg,
968 unsigned nfocus)
[dcfd422]969{
970 terminal_t *term = (terminal_t *) arg;
971
[46a47c0]972 if (nfocus == 0) {
973 term->is_focused = false;
[899bdfd]974 term_render(term);
[46a47c0]975 gfx_update(term->gc);
976 }
[dcfd422]977}
978
[70814b8e]979static void term_connection(ipc_call_t *icall, void *arg)
980{
981 terminal_t *term = NULL;
982
983 list_foreach(terms, link, terminal_t, cur) {
984 if (cur->dsid == (service_id_t) ipc_get_arg2(icall)) {
985 term = cur;
986 break;
987 }
988 }
989
990 if (term == NULL) {
991 async_answer_0(icall, ENOENT);
992 return;
993 }
994
995 if (!atomic_flag_test_and_set(&term->refcnt))
[899bdfd]996 termui_set_cursor_visibility(term->termui, true);
[70814b8e]997
998 con_conn(icall, &term->srvs);
999}
1000
[899bdfd]1001static errno_t term_init_window(terminal_t *term, const char *display_spec,
1002 gfx_coord_t width, gfx_coord_t height,
1003 gfx_coord_t min_width, gfx_coord_t min_height,
1004 terminal_flags_t flags)
1005{
1006 gfx_rect_t min_rect = { { 0, 0 }, { min_width, min_height } };
[9aa51406]1007 gfx_rect_t wmin_rect;
1008 gfx_rect_t wrect;
[899bdfd]1009
[2cf8f994]1010 errno_t rc = ui_create(display_spec, &term->ui);
1011 if (rc != EOK) {
1012 printf("Error creating UI on %s.\n", display_spec);
1013 return rc;
1014 }
1015
[899bdfd]1016 ui_wnd_params_t wparams;
1017 ui_wnd_params_init(&wparams);
1018 wparams.caption = "Terminal";
1019 wparams.style |= ui_wds_maximize_btn | ui_wds_resizable;
[2cf8f994]1020
[899bdfd]1021 if ((flags & tf_topleft) != 0)
1022 wparams.placement = ui_wnd_place_top_left;
1023
[2cf8f994]1024 if (ui_is_fullscreen(term->ui)) {
1025 wparams.placement = ui_wnd_place_full_screen;
1026 wparams.style &= ~ui_wds_decorated;
[899bdfd]1027 }
1028
1029 /* Compute wrect such that application area corresponds to rect. */
1030 ui_wdecor_rect_from_app(term->ui, wparams.style, &min_rect, &wrect);
[9aa51406]1031 gfx_rect_rtranslate(&wrect.p0, &wrect, &wmin_rect);
1032 wparams.min_size = wmin_rect.p1;
[899bdfd]1033
1034 gfx_rect_t rect = { { 0, 0 }, { width, height } };
1035 ui_wdecor_rect_from_app(term->ui, wparams.style, &rect, &rect);
[2cf8f994]1036 term->off.x = -rect.p0.x;
1037 term->off.y = -rect.p0.y;
1038 printf("off=%d,%d\n", term->off.x, term->off.y);
1039 gfx_rect_translate(&term->off, &rect, &wparams.rect);
1040 printf("wparams.rect=%d,%d,%d,%d\n",
1041 wparams.rect.p0.x,
1042 wparams.rect.p1.x,
1043 wparams.rect.p0.y,
1044 wparams.rect.p1.y);
[899bdfd]1045
[9aa51406]1046 rc = ui_window_create(term->ui, &wparams, &term->window);
1047 if (rc != EOK)
1048 return rc;
[899bdfd]1049
[9aa51406]1050 ui_window_set_cb(term->window, &terminal_window_cb, (void *) term);
[899bdfd]1051 return terminal_window_resize(term);
1052}
1053
[f03d1308]1054errno_t terminal_create(const char *display_spec, sysarg_t width,
[7b11315]1055 sysarg_t height, terminal_flags_t flags, const char *command,
1056 terminal_t **rterm)
[70814b8e]1057{
1058 errno_t rc;
1059
[899bdfd]1060 terminal_t *term = calloc(1, sizeof(terminal_t));
[70814b8e]1061 if (term == NULL) {
1062 printf("Out of memory.\n");
1063 return ENOMEM;
1064 }
1065
1066 link_initialize(&term->link);
1067 fibril_mutex_initialize(&term->mtx);
1068 atomic_flag_clear(&term->refcnt);
1069
1070 prodcons_initialize(&term->input_pc);
1071 term->char_remains_len = 0;
1072
[899bdfd]1073 term->default_bgcolor = termui_color_from_pixel(_basic_colors[COLOR_WHITE | COLOR_BRIGHT]);
1074 term->default_fgcolor = termui_color_from_pixel(_basic_colors[COLOR_BLACK]);
[70814b8e]1075
[899bdfd]1076 term->emphasis_bgcolor = termui_color_from_pixel(_basic_colors[COLOR_WHITE | COLOR_BRIGHT]);
1077 term->emphasis_fgcolor = termui_color_from_pixel(_basic_colors[COLOR_RED | COLOR_BRIGHT]);
[70814b8e]1078
[899bdfd]1079 term->selection_bgcolor = termui_color_from_pixel(_basic_colors[COLOR_RED | COLOR_BRIGHT]);
1080 term->selection_fgcolor = termui_color_from_pixel(_basic_colors[COLOR_WHITE | COLOR_BRIGHT]);
[70814b8e]1081
[2cf8f994]1082 rc = term_init_window(term, display_spec, width, height,
1083 MIN_WINDOW_COLS * FONT_WIDTH, MIN_WINDOW_ROWS * FONT_SCANLINES, flags);
1084 if (rc != EOK) {
1085 printf("Error creating window (%s).\n", str_error(rc));
1086 goto error;
1087 }
1088
1089 term->termui = termui_create(term->w / FONT_WIDTH,
1090 term->h / FONT_SCANLINES, SCROLLBACK_MAX_LINES);
[899bdfd]1091 if (!term->termui) {
1092 printf("Error creating terminal UI.\n");
[70814b8e]1093 rc = ENOMEM;
1094 goto error;
1095 }
1096
[899bdfd]1097 termui_set_refresh_cb(term->termui, termui_refresh_cb, term);
1098 termui_set_scroll_cb(term->termui, termui_scroll_cb, term);
1099 termui_set_update_cb(term->termui, termui_update_cb, term);
[dcfd422]1100
[70814b8e]1101 async_set_fallback_port_handler(term_connection, NULL);
1102 con_srvs_init(&term->srvs);
1103 term->srvs.ops = &con_ops;
1104 term->srvs.sarg = term;
1105
[4c6fd56]1106 rc = loc_server_register(NAME, &term->srv);
[70814b8e]1107 if (rc != EOK) {
1108 printf("Error registering server.\n");
1109 rc = EIO;
1110 goto error;
1111 }
1112
1113 char vc[LOC_NAME_MAXLEN + 1];
1114 snprintf(vc, LOC_NAME_MAXLEN, "%s/%" PRIu64, NAMESPACE,
1115 task_get_id());
1116
[ca48672]1117 rc = loc_service_register(term->srv, vc, fallback_port_id, &term->dsid);
[70814b8e]1118 if (rc != EOK) {
1119 printf("Error registering service.\n");
1120 rc = EIO;
1121 goto error;
1122 }
1123
1124 list_append(&term->link, &terms);
[ce862ac]1125 rc = getterm(&term->wait, vc, command);
1126 if (rc != EOK)
1127 goto error;
1128
1129 term->wfid = fibril_create(terminal_wait_fibril, term);
1130 if (term->wfid == 0)
1131 goto error;
1132
1133 fibril_add_ready(term->wfid);
[70814b8e]1134
[f03d1308]1135 term->is_focused = true;
1136
[899bdfd]1137 termui_refresh_cb(term);
[5a43bd0]1138
[70814b8e]1139 *rterm = term;
1140 return EOK;
1141error:
[4c6fd56]1142 if (term->dsid != 0)
1143 loc_service_unregister(term->srv, term->dsid);
1144 if (term->srv != NULL)
1145 loc_server_unregister(term->srv);
[70814b8e]1146 if (term->window != NULL)
[f03d1308]1147 ui_window_destroy(term->window);
1148 if (term->ui != NULL)
1149 ui_destroy(term->ui);
[899bdfd]1150 if (term->termui != NULL)
1151 termui_destroy(term->termui);
[70814b8e]1152 free(term);
1153 return rc;
1154}
1155
[ce862ac]1156static errno_t terminal_wait_fibril(void *arg)
1157{
1158 terminal_t *term = (terminal_t *)arg;
1159 task_exit_t texit;
1160 int retval;
1161
1162 /*
1163 * XXX There is no way to break the sleep if the task does not
1164 * exit.
1165 */
1166 (void) task_wait(&term->wait, &texit, &retval);
1167 ui_quit(term->ui);
1168 return EOK;
1169}
1170
[70814b8e]1171/** @}
1172 */
Note: See TracBrowser for help on using the repository browser.