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