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