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