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