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