1 | /*
|
---|
2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
3 | * Copyright (c) 2012 Petr Koupy
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup terminal
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file Terminal application
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/list.h>
|
---|
38 | #include <adt/prodcons.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <fbfont/font-8x16.h>
|
---|
41 | #include <io/chargrid.h>
|
---|
42 | #include <gfx/bitmap.h>
|
---|
43 | #include <gfx/context.h>
|
---|
44 | #include <io/con_srv.h>
|
---|
45 | #include <io/concaps.h>
|
---|
46 | #include <io/console.h>
|
---|
47 | #include <io/pixelmap.h>
|
---|
48 | #include <task.h>
|
---|
49 | #include <stdarg.h>
|
---|
50 | #include <stdlib.h>
|
---|
51 | #include <str.h>
|
---|
52 | #include <ui/resource.h>
|
---|
53 | #include <ui/ui.h>
|
---|
54 | #include <ui/wdecor.h>
|
---|
55 | #include <ui/window.h>
|
---|
56 |
|
---|
57 | #include "terminal.h"
|
---|
58 |
|
---|
59 | #define NAME "terminal"
|
---|
60 | #define NAMESPACE "terminal"
|
---|
61 |
|
---|
62 | #define LOCFS_MOUNT_POINT "/loc"
|
---|
63 |
|
---|
64 | #define APP_GETTERM "/app/getterm"
|
---|
65 |
|
---|
66 | #define TERM_CAPS \
|
---|
67 | (CONSOLE_CAP_STYLE | CONSOLE_CAP_INDEXED | CONSOLE_CAP_RGB)
|
---|
68 |
|
---|
69 | static LIST_INITIALIZE(terms);
|
---|
70 |
|
---|
71 | static errno_t term_open(con_srvs_t *, con_srv_t *);
|
---|
72 | static errno_t term_close(con_srv_t *);
|
---|
73 | static errno_t term_read(con_srv_t *, void *, size_t, size_t *);
|
---|
74 | static errno_t term_write(con_srv_t *, void *, size_t, size_t *);
|
---|
75 | static void term_sync(con_srv_t *);
|
---|
76 | static void term_clear(con_srv_t *);
|
---|
77 | static void term_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
|
---|
78 | static errno_t term_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
79 | static errno_t term_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
80 | static errno_t term_get_color_cap(con_srv_t *, console_caps_t *);
|
---|
81 | static void term_set_style(con_srv_t *, console_style_t);
|
---|
82 | static void term_set_color(con_srv_t *, console_color_t, console_color_t,
|
---|
83 | console_color_attr_t);
|
---|
84 | static void term_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
|
---|
85 | static void term_set_cursor_visibility(con_srv_t *, bool);
|
---|
86 | static errno_t term_get_event(con_srv_t *, cons_event_t *);
|
---|
87 |
|
---|
88 | static con_ops_t con_ops = {
|
---|
89 | .open = term_open,
|
---|
90 | .close = term_close,
|
---|
91 | .read = term_read,
|
---|
92 | .write = term_write,
|
---|
93 | .sync = term_sync,
|
---|
94 | .clear = term_clear,
|
---|
95 | .set_pos = term_set_pos,
|
---|
96 | .get_pos = term_get_pos,
|
---|
97 | .get_size = term_get_size,
|
---|
98 | .get_color_cap = term_get_color_cap,
|
---|
99 | .set_style = term_set_style,
|
---|
100 | .set_color = term_set_color,
|
---|
101 | .set_rgb_color = term_set_rgb_color,
|
---|
102 | .set_cursor_visibility = term_set_cursor_visibility,
|
---|
103 | .get_event = term_get_event
|
---|
104 | };
|
---|
105 |
|
---|
106 | static void terminal_close_event(ui_window_t *, void *);
|
---|
107 | static void terminal_focus_event(ui_window_t *, void *);
|
---|
108 | static void terminal_kbd_event(ui_window_t *, void *, kbd_event_t *);
|
---|
109 | static void terminal_pos_event(ui_window_t *, void *, pos_event_t *);
|
---|
110 | static void terminal_unfocus_event(ui_window_t *, void *);
|
---|
111 |
|
---|
112 | static ui_window_cb_t terminal_window_cb = {
|
---|
113 | .close = terminal_close_event,
|
---|
114 | .focus = terminal_focus_event,
|
---|
115 | .kbd = terminal_kbd_event,
|
---|
116 | .pos = terminal_pos_event,
|
---|
117 | .unfocus = terminal_unfocus_event
|
---|
118 | };
|
---|
119 |
|
---|
120 | static terminal_t *srv_to_terminal(con_srv_t *srv)
|
---|
121 | {
|
---|
122 | return srv->srvs->sarg;
|
---|
123 | }
|
---|
124 |
|
---|
125 | static void getterm(const char *svc, const char *app)
|
---|
126 | {
|
---|
127 | task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
|
---|
128 | LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
|
---|
129 | }
|
---|
130 |
|
---|
131 | static pixel_t color_table[16] = {
|
---|
132 | [COLOR_BLACK] = PIXEL(255, 0, 0, 0),
|
---|
133 | [COLOR_BLUE] = PIXEL(255, 0, 0, 240),
|
---|
134 | [COLOR_GREEN] = PIXEL(255, 0, 240, 0),
|
---|
135 | [COLOR_CYAN] = PIXEL(255, 0, 240, 240),
|
---|
136 | [COLOR_RED] = PIXEL(255, 240, 0, 0),
|
---|
137 | [COLOR_MAGENTA] = PIXEL(255, 240, 0, 240),
|
---|
138 | [COLOR_YELLOW] = PIXEL(255, 240, 240, 0),
|
---|
139 | [COLOR_WHITE] = PIXEL(255, 240, 240, 240),
|
---|
140 |
|
---|
141 | [COLOR_BLACK + 8] = PIXEL(255, 0, 0, 0),
|
---|
142 | [COLOR_BLUE + 8] = PIXEL(255, 0, 0, 255),
|
---|
143 | [COLOR_GREEN + 8] = PIXEL(255, 0, 255, 0),
|
---|
144 | [COLOR_CYAN + 8] = PIXEL(255, 0, 255, 255),
|
---|
145 | [COLOR_RED + 8] = PIXEL(255, 255, 0, 0),
|
---|
146 | [COLOR_MAGENTA + 8] = PIXEL(255, 255, 0, 255),
|
---|
147 | [COLOR_YELLOW + 8] = PIXEL(255, 255, 255, 0),
|
---|
148 | [COLOR_WHITE + 8] = PIXEL(255, 255, 255, 255),
|
---|
149 | };
|
---|
150 |
|
---|
151 | static inline void attrs_rgb(char_attrs_t attrs, pixel_t *bgcolor, pixel_t *fgcolor)
|
---|
152 | {
|
---|
153 | switch (attrs.type) {
|
---|
154 | case CHAR_ATTR_STYLE:
|
---|
155 | switch (attrs.val.style) {
|
---|
156 | case STYLE_NORMAL:
|
---|
157 | *bgcolor = color_table[COLOR_WHITE];
|
---|
158 | *fgcolor = color_table[COLOR_BLACK];
|
---|
159 | break;
|
---|
160 | case STYLE_EMPHASIS:
|
---|
161 | *bgcolor = color_table[COLOR_WHITE];
|
---|
162 | *fgcolor = color_table[COLOR_RED];
|
---|
163 | break;
|
---|
164 | case STYLE_INVERTED:
|
---|
165 | *bgcolor = color_table[COLOR_BLACK];
|
---|
166 | *fgcolor = color_table[COLOR_WHITE];
|
---|
167 | break;
|
---|
168 | case STYLE_SELECTED:
|
---|
169 | *bgcolor = color_table[COLOR_RED];
|
---|
170 | *fgcolor = color_table[COLOR_WHITE];
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | break;
|
---|
174 | case CHAR_ATTR_INDEX:
|
---|
175 | *bgcolor = color_table[(attrs.val.index.bgcolor & 7) |
|
---|
176 | ((attrs.val.index.attr & CATTR_BRIGHT) ? 8 : 0)];
|
---|
177 | *fgcolor = color_table[(attrs.val.index.fgcolor & 7) |
|
---|
178 | ((attrs.val.index.attr & CATTR_BRIGHT) ? 8 : 0)];
|
---|
179 | break;
|
---|
180 | case CHAR_ATTR_RGB:
|
---|
181 | *bgcolor = 0xff000000 | attrs.val.rgb.bgcolor;
|
---|
182 | *fgcolor = 0xff000000 | attrs.val.rgb.fgcolor;
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | static void term_update_region(terminal_t *term, sysarg_t x, sysarg_t y,
|
---|
188 | sysarg_t w, sysarg_t h)
|
---|
189 | {
|
---|
190 | gfx_rect_t rect;
|
---|
191 | gfx_rect_t nupdate;
|
---|
192 |
|
---|
193 | rect.p0.x = x;
|
---|
194 | rect.p0.y = y;
|
---|
195 | rect.p1.x = x + w;
|
---|
196 | rect.p1.y = y + h;
|
---|
197 |
|
---|
198 | gfx_rect_envelope(&term->update, &rect, &nupdate);
|
---|
199 | term->update = nupdate;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static void term_update_char(terminal_t *term, pixelmap_t *pixelmap,
|
---|
203 | sysarg_t sx, sysarg_t sy, sysarg_t col, sysarg_t row)
|
---|
204 | {
|
---|
205 | charfield_t *field =
|
---|
206 | chargrid_charfield_at(term->backbuf, col, row);
|
---|
207 |
|
---|
208 | bool inverted = chargrid_cursor_at(term->backbuf, col, row);
|
---|
209 |
|
---|
210 | sysarg_t bx = sx + (col * FONT_WIDTH);
|
---|
211 | sysarg_t by = sy + (row * FONT_SCANLINES);
|
---|
212 |
|
---|
213 | pixel_t bgcolor = 0;
|
---|
214 | pixel_t fgcolor = 0;
|
---|
215 |
|
---|
216 | if (inverted)
|
---|
217 | attrs_rgb(field->attrs, &fgcolor, &bgcolor);
|
---|
218 | else
|
---|
219 | attrs_rgb(field->attrs, &bgcolor, &fgcolor);
|
---|
220 |
|
---|
221 | // FIXME: Glyph type should be actually uint32_t
|
---|
222 | // for full UTF-32 coverage.
|
---|
223 |
|
---|
224 | uint16_t glyph = fb_font_glyph(field->ch, NULL);
|
---|
225 |
|
---|
226 | for (unsigned int y = 0; y < FONT_SCANLINES; y++) {
|
---|
227 | pixel_t *dst = pixelmap_pixel_at(pixelmap, bx, by + y);
|
---|
228 | pixel_t *dst_max = pixelmap_pixel_at(pixelmap, bx + FONT_WIDTH - 1, by + y);
|
---|
229 | if (!dst || !dst_max)
|
---|
230 | continue;
|
---|
231 | int count = FONT_WIDTH;
|
---|
232 | while (count-- != 0) {
|
---|
233 | *dst++ = (fb_font[glyph][y] & (1 << count)) ? fgcolor : bgcolor;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | term_update_region(term, bx, by, FONT_WIDTH, FONT_SCANLINES);
|
---|
237 | }
|
---|
238 |
|
---|
239 | static bool term_update_scroll(terminal_t *term, pixelmap_t *pixelmap,
|
---|
240 | sysarg_t sx, sysarg_t sy)
|
---|
241 | {
|
---|
242 | sysarg_t top_row = chargrid_get_top_row(term->frontbuf);
|
---|
243 |
|
---|
244 | if (term->top_row == top_row) {
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 |
|
---|
248 | term->top_row = top_row;
|
---|
249 |
|
---|
250 | for (sysarg_t row = 0; row < term->rows; row++) {
|
---|
251 | for (sysarg_t col = 0; col < term->cols; col++) {
|
---|
252 | charfield_t *front_field =
|
---|
253 | chargrid_charfield_at(term->frontbuf, col, row);
|
---|
254 | charfield_t *back_field =
|
---|
255 | chargrid_charfield_at(term->backbuf, col, row);
|
---|
256 | bool update = false;
|
---|
257 |
|
---|
258 | if (front_field->ch != back_field->ch) {
|
---|
259 | back_field->ch = front_field->ch;
|
---|
260 | update = true;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (!attrs_same(front_field->attrs, back_field->attrs)) {
|
---|
264 | back_field->attrs = front_field->attrs;
|
---|
265 | update = true;
|
---|
266 | }
|
---|
267 |
|
---|
268 | front_field->flags &= ~CHAR_FLAG_DIRTY;
|
---|
269 |
|
---|
270 | if (update) {
|
---|
271 | term_update_char(term, pixelmap, sx, sy, col, row);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | return true;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static bool term_update_cursor(terminal_t *term, pixelmap_t *pixelmap,
|
---|
280 | sysarg_t sx, sysarg_t sy)
|
---|
281 | {
|
---|
282 | bool update = false;
|
---|
283 |
|
---|
284 | sysarg_t front_col;
|
---|
285 | sysarg_t front_row;
|
---|
286 | chargrid_get_cursor(term->frontbuf, &front_col, &front_row);
|
---|
287 |
|
---|
288 | sysarg_t back_col;
|
---|
289 | sysarg_t back_row;
|
---|
290 | chargrid_get_cursor(term->backbuf, &back_col, &back_row);
|
---|
291 |
|
---|
292 | bool front_visibility =
|
---|
293 | chargrid_get_cursor_visibility(term->frontbuf) &&
|
---|
294 | term->is_focused;
|
---|
295 | bool back_visibility =
|
---|
296 | chargrid_get_cursor_visibility(term->backbuf);
|
---|
297 |
|
---|
298 | if (front_visibility != back_visibility) {
|
---|
299 | chargrid_set_cursor_visibility(term->backbuf,
|
---|
300 | front_visibility);
|
---|
301 | term_update_char(term, pixelmap, sx, sy, back_col, back_row);
|
---|
302 | update = true;
|
---|
303 | }
|
---|
304 |
|
---|
305 | if ((front_col != back_col) || (front_row != back_row)) {
|
---|
306 | chargrid_set_cursor(term->backbuf, front_col, front_row);
|
---|
307 | term_update_char(term, pixelmap, sx, sy, back_col, back_row);
|
---|
308 | term_update_char(term, pixelmap, sx, sy, front_col, front_row);
|
---|
309 | update = true;
|
---|
310 | }
|
---|
311 |
|
---|
312 | return update;
|
---|
313 | }
|
---|
314 |
|
---|
315 | static void term_update(terminal_t *term)
|
---|
316 | {
|
---|
317 | pixelmap_t pixelmap;
|
---|
318 | gfx_bitmap_alloc_t alloc;
|
---|
319 | gfx_coord2_t pos;
|
---|
320 | errno_t rc;
|
---|
321 |
|
---|
322 | rc = gfx_bitmap_get_alloc(term->bmp, &alloc);
|
---|
323 | if (rc != EOK) {
|
---|
324 | return;
|
---|
325 | }
|
---|
326 |
|
---|
327 | fibril_mutex_lock(&term->mtx);
|
---|
328 | pixelmap.width = term->w;
|
---|
329 | pixelmap.height = term->h;
|
---|
330 | pixelmap.data = alloc.pixels;
|
---|
331 |
|
---|
332 | bool update = false;
|
---|
333 | sysarg_t sx = 0/*term->widget.hpos*/;
|
---|
334 | sysarg_t sy = 0/*term->widget.vpos*/;
|
---|
335 |
|
---|
336 | if (term_update_scroll(term, &pixelmap, sx, sy)) {
|
---|
337 | update = true;
|
---|
338 | } else {
|
---|
339 | for (sysarg_t y = 0; y < term->rows; y++) {
|
---|
340 | for (sysarg_t x = 0; x < term->cols; x++) {
|
---|
341 | charfield_t *front_field =
|
---|
342 | chargrid_charfield_at(term->frontbuf, x, y);
|
---|
343 | charfield_t *back_field =
|
---|
344 | chargrid_charfield_at(term->backbuf, x, y);
|
---|
345 | bool update = false;
|
---|
346 |
|
---|
347 | if ((front_field->flags & CHAR_FLAG_DIRTY) ==
|
---|
348 | CHAR_FLAG_DIRTY) {
|
---|
349 | if (front_field->ch != back_field->ch) {
|
---|
350 | back_field->ch = front_field->ch;
|
---|
351 | update = true;
|
---|
352 | }
|
---|
353 |
|
---|
354 | if (!attrs_same(front_field->attrs,
|
---|
355 | back_field->attrs)) {
|
---|
356 | back_field->attrs = front_field->attrs;
|
---|
357 | update = true;
|
---|
358 | }
|
---|
359 |
|
---|
360 | front_field->flags &= ~CHAR_FLAG_DIRTY;
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (update) {
|
---|
364 | term_update_char(term, &pixelmap, sx, sy, x, y);
|
---|
365 | update = true;
|
---|
366 | }
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | if (term_update_cursor(term, &pixelmap, sx, sy))
|
---|
372 | update = true;
|
---|
373 |
|
---|
374 | if (update) {
|
---|
375 | pos.x = 4;
|
---|
376 | pos.y = 26;
|
---|
377 | (void) gfx_bitmap_render(term->bmp, &term->update, &pos);
|
---|
378 |
|
---|
379 | term->update.p0.x = 0;
|
---|
380 | term->update.p0.y = 0;
|
---|
381 | term->update.p1.x = 0;
|
---|
382 | term->update.p1.y = 0;
|
---|
383 | }
|
---|
384 |
|
---|
385 | fibril_mutex_unlock(&term->mtx);
|
---|
386 | }
|
---|
387 |
|
---|
388 | static void term_repaint(terminal_t *term)
|
---|
389 | {
|
---|
390 | pixelmap_t pixelmap;
|
---|
391 | gfx_bitmap_alloc_t alloc;
|
---|
392 | errno_t rc;
|
---|
393 |
|
---|
394 | rc = gfx_bitmap_get_alloc(term->bmp, &alloc);
|
---|
395 | if (rc != EOK) {
|
---|
396 | printf("Error getting bitmap allocation info.\n");
|
---|
397 | return;
|
---|
398 | }
|
---|
399 |
|
---|
400 | fibril_mutex_lock(&term->mtx);
|
---|
401 |
|
---|
402 | pixelmap.width = term->w;
|
---|
403 | pixelmap.height = term->h;
|
---|
404 | pixelmap.data = alloc.pixels;
|
---|
405 |
|
---|
406 | sysarg_t sx = 0;
|
---|
407 | sysarg_t sy = 0;
|
---|
408 |
|
---|
409 | if (!term_update_scroll(term, &pixelmap, sx, sy)) {
|
---|
410 | for (sysarg_t y = 0; y < term->rows; y++) {
|
---|
411 | for (sysarg_t x = 0; x < term->cols; x++) {
|
---|
412 | charfield_t *front_field =
|
---|
413 | chargrid_charfield_at(term->frontbuf, x, y);
|
---|
414 | charfield_t *back_field =
|
---|
415 | chargrid_charfield_at(term->backbuf, x, y);
|
---|
416 |
|
---|
417 | back_field->ch = front_field->ch;
|
---|
418 | back_field->attrs = front_field->attrs;
|
---|
419 | front_field->flags &= ~CHAR_FLAG_DIRTY;
|
---|
420 |
|
---|
421 | term_update_char(term, &pixelmap, sx, sy, x, y);
|
---|
422 | }
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | term_update_cursor(term, &pixelmap, sx, sy);
|
---|
427 |
|
---|
428 | fibril_mutex_unlock(&term->mtx);
|
---|
429 | }
|
---|
430 |
|
---|
431 | static errno_t term_open(con_srvs_t *srvs, con_srv_t *srv)
|
---|
432 | {
|
---|
433 | return EOK;
|
---|
434 | }
|
---|
435 |
|
---|
436 | static errno_t term_close(con_srv_t *srv)
|
---|
437 | {
|
---|
438 | return EOK;
|
---|
439 | }
|
---|
440 |
|
---|
441 | static errno_t term_read(con_srv_t *srv, void *buf, size_t size, size_t *nread)
|
---|
442 | {
|
---|
443 | terminal_t *term = srv_to_terminal(srv);
|
---|
444 | uint8_t *bbuf = buf;
|
---|
445 | size_t pos = 0;
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Read input from keyboard and copy it to the buffer.
|
---|
449 | * We need to handle situation when wchar is split by 2 following
|
---|
450 | * reads.
|
---|
451 | */
|
---|
452 | while (pos < size) {
|
---|
453 | /* Copy to the buffer remaining characters. */
|
---|
454 | while ((pos < size) && (term->char_remains_len > 0)) {
|
---|
455 | bbuf[pos] = term->char_remains[0];
|
---|
456 | pos++;
|
---|
457 |
|
---|
458 | /* Unshift the array. */
|
---|
459 | for (size_t i = 1; i < term->char_remains_len; i++)
|
---|
460 | term->char_remains[i - 1] = term->char_remains[i];
|
---|
461 |
|
---|
462 | term->char_remains_len--;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /* Still not enough? Then get another key from the queue. */
|
---|
466 | if (pos < size) {
|
---|
467 | link_t *link = prodcons_consume(&term->input_pc);
|
---|
468 | cons_event_t *event = list_get_instance(link, cons_event_t, link);
|
---|
469 |
|
---|
470 | /* Accept key presses of printable chars only. */
|
---|
471 | if (event->type == CEV_KEY && event->ev.key.type == KEY_PRESS &&
|
---|
472 | event->ev.key.c != 0) {
|
---|
473 | char32_t tmp[2] = {
|
---|
474 | event->ev.key.c,
|
---|
475 | 0
|
---|
476 | };
|
---|
477 |
|
---|
478 | wstr_to_str(term->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
|
---|
479 | term->char_remains_len = str_size(term->char_remains);
|
---|
480 | }
|
---|
481 |
|
---|
482 | free(event);
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | *nread = size;
|
---|
487 | return EOK;
|
---|
488 | }
|
---|
489 |
|
---|
490 | static void term_write_char(terminal_t *term, wchar_t ch)
|
---|
491 | {
|
---|
492 | sysarg_t updated = 0;
|
---|
493 |
|
---|
494 | fibril_mutex_lock(&term->mtx);
|
---|
495 |
|
---|
496 | switch (ch) {
|
---|
497 | case '\n':
|
---|
498 | updated = chargrid_newline(term->frontbuf);
|
---|
499 | break;
|
---|
500 | case '\r':
|
---|
501 | break;
|
---|
502 | case '\t':
|
---|
503 | updated = chargrid_tabstop(term->frontbuf, 8);
|
---|
504 | break;
|
---|
505 | case '\b':
|
---|
506 | updated = chargrid_backspace(term->frontbuf);
|
---|
507 | break;
|
---|
508 | default:
|
---|
509 | updated = chargrid_putuchar(term->frontbuf, ch, true);
|
---|
510 | }
|
---|
511 |
|
---|
512 | fibril_mutex_unlock(&term->mtx);
|
---|
513 |
|
---|
514 | if (updated > 1)
|
---|
515 | term_update(term);
|
---|
516 | }
|
---|
517 |
|
---|
518 | static errno_t term_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
|
---|
519 | {
|
---|
520 | terminal_t *term = srv_to_terminal(srv);
|
---|
521 |
|
---|
522 | size_t off = 0;
|
---|
523 | while (off < size)
|
---|
524 | term_write_char(term, str_decode(data, &off, size));
|
---|
525 |
|
---|
526 | *nwritten = size;
|
---|
527 | return EOK;
|
---|
528 | }
|
---|
529 |
|
---|
530 | static void term_sync(con_srv_t *srv)
|
---|
531 | {
|
---|
532 | terminal_t *term = srv_to_terminal(srv);
|
---|
533 |
|
---|
534 | term_update(term);
|
---|
535 | }
|
---|
536 |
|
---|
537 | static void term_clear(con_srv_t *srv)
|
---|
538 | {
|
---|
539 | terminal_t *term = srv_to_terminal(srv);
|
---|
540 |
|
---|
541 | fibril_mutex_lock(&term->mtx);
|
---|
542 | chargrid_clear(term->frontbuf);
|
---|
543 | fibril_mutex_unlock(&term->mtx);
|
---|
544 |
|
---|
545 | term_update(term);
|
---|
546 | }
|
---|
547 |
|
---|
548 | static void term_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
|
---|
549 | {
|
---|
550 | terminal_t *term = srv_to_terminal(srv);
|
---|
551 |
|
---|
552 | fibril_mutex_lock(&term->mtx);
|
---|
553 | chargrid_set_cursor(term->frontbuf, col, row);
|
---|
554 | fibril_mutex_unlock(&term->mtx);
|
---|
555 |
|
---|
556 | term_update(term);
|
---|
557 | }
|
---|
558 |
|
---|
559 | static errno_t term_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
|
---|
560 | {
|
---|
561 | terminal_t *term = srv_to_terminal(srv);
|
---|
562 |
|
---|
563 | fibril_mutex_lock(&term->mtx);
|
---|
564 | chargrid_get_cursor(term->frontbuf, col, row);
|
---|
565 | fibril_mutex_unlock(&term->mtx);
|
---|
566 |
|
---|
567 | return EOK;
|
---|
568 | }
|
---|
569 |
|
---|
570 | static errno_t term_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
|
---|
571 | {
|
---|
572 | terminal_t *term = srv_to_terminal(srv);
|
---|
573 |
|
---|
574 | fibril_mutex_lock(&term->mtx);
|
---|
575 | *cols = term->cols;
|
---|
576 | *rows = term->rows;
|
---|
577 | fibril_mutex_unlock(&term->mtx);
|
---|
578 |
|
---|
579 | return EOK;
|
---|
580 | }
|
---|
581 |
|
---|
582 | static errno_t term_get_color_cap(con_srv_t *srv, console_caps_t *caps)
|
---|
583 | {
|
---|
584 | (void) srv;
|
---|
585 | *caps = TERM_CAPS;
|
---|
586 |
|
---|
587 | return EOK;
|
---|
588 | }
|
---|
589 |
|
---|
590 | static void term_set_style(con_srv_t *srv, console_style_t style)
|
---|
591 | {
|
---|
592 | terminal_t *term = srv_to_terminal(srv);
|
---|
593 |
|
---|
594 | fibril_mutex_lock(&term->mtx);
|
---|
595 | chargrid_set_style(term->frontbuf, style);
|
---|
596 | fibril_mutex_unlock(&term->mtx);
|
---|
597 | }
|
---|
598 |
|
---|
599 | static void term_set_color(con_srv_t *srv, console_color_t bgcolor,
|
---|
600 | console_color_t fgcolor, console_color_attr_t attr)
|
---|
601 | {
|
---|
602 | terminal_t *term = srv_to_terminal(srv);
|
---|
603 |
|
---|
604 | fibril_mutex_lock(&term->mtx);
|
---|
605 | chargrid_set_color(term->frontbuf, bgcolor, fgcolor, attr);
|
---|
606 | fibril_mutex_unlock(&term->mtx);
|
---|
607 | }
|
---|
608 |
|
---|
609 | static void term_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
|
---|
610 | pixel_t fgcolor)
|
---|
611 | {
|
---|
612 | terminal_t *term = srv_to_terminal(srv);
|
---|
613 |
|
---|
614 | fibril_mutex_lock(&term->mtx);
|
---|
615 | chargrid_set_rgb_color(term->frontbuf, bgcolor, fgcolor);
|
---|
616 | fibril_mutex_unlock(&term->mtx);
|
---|
617 | }
|
---|
618 |
|
---|
619 | static void term_set_cursor_visibility(con_srv_t *srv, bool visible)
|
---|
620 | {
|
---|
621 | terminal_t *term = srv_to_terminal(srv);
|
---|
622 |
|
---|
623 | fibril_mutex_lock(&term->mtx);
|
---|
624 | chargrid_set_cursor_visibility(term->frontbuf, visible);
|
---|
625 | fibril_mutex_unlock(&term->mtx);
|
---|
626 |
|
---|
627 | term_update(term);
|
---|
628 | }
|
---|
629 |
|
---|
630 | static errno_t term_get_event(con_srv_t *srv, cons_event_t *event)
|
---|
631 | {
|
---|
632 | terminal_t *term = srv_to_terminal(srv);
|
---|
633 | link_t *link = prodcons_consume(&term->input_pc);
|
---|
634 | cons_event_t *ev = list_get_instance(link, cons_event_t, link);
|
---|
635 |
|
---|
636 | *event = *ev;
|
---|
637 | free(ev);
|
---|
638 | return EOK;
|
---|
639 | }
|
---|
640 |
|
---|
641 | static void deinit_terminal(terminal_t *term)
|
---|
642 | {
|
---|
643 | list_remove(&term->link);
|
---|
644 |
|
---|
645 | if (term->frontbuf)
|
---|
646 | chargrid_destroy(term->frontbuf);
|
---|
647 |
|
---|
648 | if (term->backbuf)
|
---|
649 | chargrid_destroy(term->backbuf);
|
---|
650 | }
|
---|
651 |
|
---|
652 | void terminal_destroy(terminal_t *term)
|
---|
653 | {
|
---|
654 | deinit_terminal(term);
|
---|
655 | free(term);
|
---|
656 | }
|
---|
657 |
|
---|
658 | static void terminal_queue_cons_event(terminal_t *term, cons_event_t *ev)
|
---|
659 | {
|
---|
660 | /* Got key press/release event */
|
---|
661 | cons_event_t *event =
|
---|
662 | (cons_event_t *) malloc(sizeof(cons_event_t));
|
---|
663 | if (event == NULL)
|
---|
664 | return;
|
---|
665 |
|
---|
666 | *event = *ev;
|
---|
667 | link_initialize(&event->link);
|
---|
668 |
|
---|
669 | prodcons_produce(&term->input_pc, &event->link);
|
---|
670 | }
|
---|
671 |
|
---|
672 | /** Handle window close event. */
|
---|
673 | static void terminal_close_event(ui_window_t *window, void *arg)
|
---|
674 | {
|
---|
675 | terminal_t *term = (terminal_t *) arg;
|
---|
676 |
|
---|
677 | (void) term;
|
---|
678 |
|
---|
679 | // XXX This is not really a clean way of terminating
|
---|
680 | exit(0);
|
---|
681 | }
|
---|
682 |
|
---|
683 | /** Handle window focus event. */
|
---|
684 | static void terminal_focus_event(ui_window_t *window, void *arg)
|
---|
685 | {
|
---|
686 | terminal_t *term = (terminal_t *) arg;
|
---|
687 |
|
---|
688 | term->is_focused = true;
|
---|
689 | term_update(term);
|
---|
690 | }
|
---|
691 |
|
---|
692 | /** Handle window keyboard event */
|
---|
693 | static void terminal_kbd_event(ui_window_t *window, void *arg,
|
---|
694 | kbd_event_t *kbd_event)
|
---|
695 | {
|
---|
696 | terminal_t *term = (terminal_t *) arg;
|
---|
697 | cons_event_t event;
|
---|
698 |
|
---|
699 | event.type = CEV_KEY;
|
---|
700 | event.ev.key = *kbd_event;
|
---|
701 |
|
---|
702 | terminal_queue_cons_event(term, &event);
|
---|
703 | }
|
---|
704 |
|
---|
705 | /** Handle window position event */
|
---|
706 | static void terminal_pos_event(ui_window_t *window, void *arg, pos_event_t *event)
|
---|
707 | {
|
---|
708 | cons_event_t cevent;
|
---|
709 | terminal_t *term = (terminal_t *) arg;
|
---|
710 |
|
---|
711 | sysarg_t sx = -term->off.x;
|
---|
712 | sysarg_t sy = -term->off.y;
|
---|
713 |
|
---|
714 | if (event->type == POS_PRESS) {
|
---|
715 | cevent.type = CEV_POS;
|
---|
716 | cevent.ev.pos.type = event->type;
|
---|
717 | cevent.ev.pos.pos_id = event->pos_id;
|
---|
718 | cevent.ev.pos.btn_num = event->btn_num;
|
---|
719 |
|
---|
720 | cevent.ev.pos.hpos = (event->hpos - sx) / FONT_WIDTH;
|
---|
721 | cevent.ev.pos.vpos = (event->vpos - sy) / FONT_SCANLINES;
|
---|
722 | terminal_queue_cons_event(term, &cevent);
|
---|
723 | }
|
---|
724 | }
|
---|
725 |
|
---|
726 | /** Handle window unfocus event. */
|
---|
727 | static void terminal_unfocus_event(ui_window_t *window, void *arg)
|
---|
728 | {
|
---|
729 | terminal_t *term = (terminal_t *) arg;
|
---|
730 |
|
---|
731 | term->is_focused = false;
|
---|
732 | term_update(term);
|
---|
733 | }
|
---|
734 |
|
---|
735 | static void term_connection(ipc_call_t *icall, void *arg)
|
---|
736 | {
|
---|
737 | terminal_t *term = NULL;
|
---|
738 |
|
---|
739 | list_foreach(terms, link, terminal_t, cur) {
|
---|
740 | if (cur->dsid == (service_id_t) ipc_get_arg2(icall)) {
|
---|
741 | term = cur;
|
---|
742 | break;
|
---|
743 | }
|
---|
744 | }
|
---|
745 |
|
---|
746 | if (term == NULL) {
|
---|
747 | async_answer_0(icall, ENOENT);
|
---|
748 | return;
|
---|
749 | }
|
---|
750 |
|
---|
751 | if (!atomic_flag_test_and_set(&term->refcnt))
|
---|
752 | chargrid_set_cursor_visibility(term->frontbuf, true);
|
---|
753 |
|
---|
754 | con_conn(icall, &term->srvs);
|
---|
755 | }
|
---|
756 |
|
---|
757 | errno_t terminal_create(const char *display_spec, sysarg_t width,
|
---|
758 | sysarg_t height, terminal_flags_t flags, terminal_t **rterm)
|
---|
759 | {
|
---|
760 | terminal_t *term;
|
---|
761 | gfx_bitmap_params_t params;
|
---|
762 | ui_wnd_params_t wparams;
|
---|
763 | gfx_rect_t rect;
|
---|
764 | gfx_coord2_t off;
|
---|
765 | gfx_rect_t wrect;
|
---|
766 | errno_t rc;
|
---|
767 |
|
---|
768 | term = calloc(1, sizeof(terminal_t));
|
---|
769 | if (term == NULL) {
|
---|
770 | printf("Out of memory.\n");
|
---|
771 | return ENOMEM;
|
---|
772 | }
|
---|
773 |
|
---|
774 | link_initialize(&term->link);
|
---|
775 | fibril_mutex_initialize(&term->mtx);
|
---|
776 | atomic_flag_clear(&term->refcnt);
|
---|
777 |
|
---|
778 | prodcons_initialize(&term->input_pc);
|
---|
779 | term->char_remains_len = 0;
|
---|
780 |
|
---|
781 | term->w = width;
|
---|
782 | term->h = height;
|
---|
783 |
|
---|
784 | term->cols = width / FONT_WIDTH;
|
---|
785 | term->rows = height / FONT_SCANLINES;
|
---|
786 |
|
---|
787 | term->frontbuf = NULL;
|
---|
788 | term->backbuf = NULL;
|
---|
789 |
|
---|
790 | term->frontbuf = chargrid_create(term->cols, term->rows,
|
---|
791 | CHARGRID_FLAG_NONE);
|
---|
792 | if (!term->frontbuf) {
|
---|
793 | printf("Error creating front buffer.\n");
|
---|
794 | rc = ENOMEM;
|
---|
795 | goto error;
|
---|
796 | }
|
---|
797 |
|
---|
798 | term->backbuf = chargrid_create(term->cols, term->rows,
|
---|
799 | CHARGRID_FLAG_NONE);
|
---|
800 | if (!term->backbuf) {
|
---|
801 | printf("Error creating back buffer.\n");
|
---|
802 | rc = ENOMEM;
|
---|
803 | goto error;
|
---|
804 | }
|
---|
805 |
|
---|
806 | rect.p0.x = 0;
|
---|
807 | rect.p0.y = 0;
|
---|
808 | rect.p1.x = width;
|
---|
809 | rect.p1.y = height;
|
---|
810 |
|
---|
811 | ui_wnd_params_init(&wparams);
|
---|
812 | wparams.caption = "Terminal";
|
---|
813 | if ((flags & tf_topleft) != 0)
|
---|
814 | wparams.placement = ui_wnd_place_top_left;
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * Compute window rectangle such that application area corresponds
|
---|
818 | * to rect
|
---|
819 | */
|
---|
820 | ui_wdecor_rect_from_app(wparams.style, &rect, &wrect);
|
---|
821 | off = wrect.p0;
|
---|
822 | gfx_rect_rtranslate(&off, &wrect, &wparams.rect);
|
---|
823 |
|
---|
824 | term->off = off;
|
---|
825 |
|
---|
826 | rc = ui_create(display_spec, &term->ui);
|
---|
827 | if (rc != EOK) {
|
---|
828 | printf("Error creating UI on %s.\n", display_spec);
|
---|
829 | goto error;
|
---|
830 | }
|
---|
831 |
|
---|
832 | rc = ui_window_create(term->ui, &wparams, &term->window);
|
---|
833 | if (rc != EOK) {
|
---|
834 | printf("Error creating window.\n");
|
---|
835 | goto error;
|
---|
836 | }
|
---|
837 |
|
---|
838 | term->gc = ui_window_get_gc(term->window);
|
---|
839 | term->ui_res = ui_window_get_res(term->window);
|
---|
840 |
|
---|
841 | ui_window_set_cb(term->window, &terminal_window_cb, (void *) term);
|
---|
842 |
|
---|
843 | gfx_bitmap_params_init(¶ms);
|
---|
844 | params.rect.p0.x = 0;
|
---|
845 | params.rect.p0.y = 0;
|
---|
846 | params.rect.p1.x = width;
|
---|
847 | params.rect.p1.y = height;
|
---|
848 |
|
---|
849 | rc = gfx_bitmap_create(term->gc, ¶ms, NULL, &term->bmp);
|
---|
850 | if (rc != EOK) {
|
---|
851 | printf("Error allocating screen bitmap.\n");
|
---|
852 | goto error;
|
---|
853 | }
|
---|
854 |
|
---|
855 | chargrid_clear(term->frontbuf);
|
---|
856 | chargrid_clear(term->backbuf);
|
---|
857 | term->top_row = 0;
|
---|
858 |
|
---|
859 | async_set_fallback_port_handler(term_connection, NULL);
|
---|
860 | con_srvs_init(&term->srvs);
|
---|
861 | term->srvs.ops = &con_ops;
|
---|
862 | term->srvs.sarg = term;
|
---|
863 |
|
---|
864 | rc = loc_server_register(NAME);
|
---|
865 | if (rc != EOK) {
|
---|
866 | printf("Error registering server.\n");
|
---|
867 | rc = EIO;
|
---|
868 | goto error;
|
---|
869 | }
|
---|
870 |
|
---|
871 | char vc[LOC_NAME_MAXLEN + 1];
|
---|
872 | snprintf(vc, LOC_NAME_MAXLEN, "%s/%" PRIu64, NAMESPACE,
|
---|
873 | task_get_id());
|
---|
874 |
|
---|
875 | rc = loc_service_register(vc, &term->dsid);
|
---|
876 | if (rc != EOK) {
|
---|
877 | printf("Error registering service.\n");
|
---|
878 | rc = EIO;
|
---|
879 | goto error;
|
---|
880 | }
|
---|
881 |
|
---|
882 | list_append(&term->link, &terms);
|
---|
883 | getterm(vc, "/app/bdsh");
|
---|
884 |
|
---|
885 | term->is_focused = true;
|
---|
886 |
|
---|
887 | term->update.p0.x = 0;
|
---|
888 | term->update.p0.y = 0;
|
---|
889 | term->update.p1.x = 0;
|
---|
890 | term->update.p1.y = 0;
|
---|
891 |
|
---|
892 | term_repaint(term);
|
---|
893 |
|
---|
894 | *rterm = term;
|
---|
895 | return EOK;
|
---|
896 | error:
|
---|
897 | if (term->window != NULL)
|
---|
898 | ui_window_destroy(term->window);
|
---|
899 | if (term->ui != NULL)
|
---|
900 | ui_destroy(term->ui);
|
---|
901 | if (term->frontbuf != NULL)
|
---|
902 | chargrid_destroy(term->frontbuf);
|
---|
903 | if (term->backbuf != NULL)
|
---|
904 | chargrid_destroy(term->backbuf);
|
---|
905 | free(term);
|
---|
906 | return rc;
|
---|
907 | }
|
---|
908 |
|
---|
909 | /** @}
|
---|
910 | */
|
---|