source: mainline/uspace/lib/gui/terminal.c@ 8c95dff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8c95dff was 3f06dae, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Button press support in terminal widget and tinput.

  • Property mode set to 100644
File size: 19.9 KB
RevLine 
[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
62static LIST_INITIALIZE(terms);
63
[5d94b16c]64static int term_open(con_srvs_t *, con_srv_t *);
65static int term_close(con_srv_t *);
66static int term_read(con_srv_t *, void *, size_t);
67static int term_write(con_srv_t *, void *, size_t);
68static void term_sync(con_srv_t *);
69static void term_clear(con_srv_t *);
70static void term_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
71static int term_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
72static int term_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
73static int term_get_color_cap(con_srv_t *, console_caps_t *);
74static void term_set_style(con_srv_t *, console_style_t);
75static void term_set_color(con_srv_t *, console_color_t, console_color_t,
76 console_color_attr_t);
77static void term_set_rgb_color(con_srv_t *, pixel_t, pixel_t);
78static void term_set_cursor_visibility(con_srv_t *, bool);
[902f0906]79static int term_get_event(con_srv_t *, cons_event_t *);
[5d94b16c]80
81static 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
99static terminal_t *srv_to_terminal(con_srv_t *srv)
100{
101 return srv->srvs->sarg;
102}
103
[6d5e378]104static 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
118static 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
138static 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
174static 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++) {
[7dba813]199 pixel_t *dst = pixelmap_pixel_at(
200 surface_pixmap_access(surface), bx, by + y);
[ba733e83]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;
[7dba813]204 int count = FONT_WIDTH;
205 while (count-- != 0) {
206 *dst++ = (fb_font[glyph][y] & (1 << count)) ? fgcolor : bgcolor;
[6d5e378]207 }
208 }
[7dba813]209 surface_add_damaged_region(surface, bx, by, FONT_WIDTH, FONT_SCANLINES);
[6d5e378]210}
211
212static 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
250static 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
285static 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
346static 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
[5d94b16c]385static int term_open(con_srvs_t *srvs, con_srv_t *srv)
[6d5e378]386{
[5d94b16c]387 return EOK;
[6d5e378]388}
389
[5d94b16c]390static int term_close(con_srv_t *srv)
[6d5e378]391{
[5d94b16c]392 return EOK;
[6d5e378]393}
394
[5d94b16c]395static int term_read(con_srv_t *srv, void *buf, size_t size)
[6d5e378]396{
[5d94b16c]397 terminal_t *term = srv_to_terminal(srv);
398 uint8_t *bbuf = buf;
[6d5e378]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)) {
[5d94b16c]409 bbuf[pos] = term->char_remains[0];
[6d5e378]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);
[3f06dae]422 cons_event_t *event = list_get_instance(link, cons_event_t, link);
[6d5e378]423
424 /* Accept key presses of printable chars only. */
[3f06dae]425 if (event->type == CEV_KEY && event->ev.key.type == KEY_PRESS &&
426 event->ev.key.c != 0) {
[6d5e378]427 wchar_t tmp[2] = {
[3f06dae]428 event->ev.key.c,
[6d5e378]429 0
430 };
431
432 wstr_to_str(term->char_remains, UTF8_CHAR_BUFFER_SIZE, tmp);
433 term->char_remains_len = str_size(term->char_remains);
434 }
435
436 free(event);
437 }
438 }
439
[5d94b16c]440 return size;
[6d5e378]441}
442
443static void term_write_char(terminal_t *term, wchar_t ch)
444{
445 sysarg_t updated = 0;
446
447 fibril_mutex_lock(&term->mtx);
448
449 switch (ch) {
450 case '\n':
451 updated = chargrid_newline(term->frontbuf);
452 break;
453 case '\r':
454 break;
455 case '\t':
456 updated = chargrid_tabstop(term->frontbuf, 8);
457 break;
458 case '\b':
459 updated = chargrid_backspace(term->frontbuf);
460 break;
461 default:
462 updated = chargrid_putchar(term->frontbuf, ch, true);
463 }
464
465 fibril_mutex_unlock(&term->mtx);
466
467 if (updated > 1)
468 term_update(term);
469}
470
[5d94b16c]471static int term_write(con_srv_t *srv, void *data, size_t size)
[6d5e378]472{
[5d94b16c]473 terminal_t *term = srv_to_terminal(srv);
[6d5e378]474
475 size_t off = 0;
476 while (off < size)
[5d94b16c]477 term_write_char(term, str_decode(data, &off, size));
478
479 return size;
480}
481
482static void term_sync(con_srv_t *srv)
483{
484 terminal_t *term = srv_to_terminal(srv);
[6d5e378]485
[5d94b16c]486 term_update(term);
[6d5e378]487}
488
[5d94b16c]489static void term_clear(con_srv_t *srv)
[6d5e378]490{
[5d94b16c]491 terminal_t *term = srv_to_terminal(srv);
492
[6d5e378]493 fibril_mutex_lock(&term->mtx);
494 chargrid_clear(term->frontbuf);
495 fibril_mutex_unlock(&term->mtx);
496
497 term_update(term);
498}
499
[5d94b16c]500static void term_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
501{
502 terminal_t *term = srv_to_terminal(srv);
503
504 fibril_mutex_lock(&term->mtx);
505 chargrid_set_cursor(term->frontbuf, col, row);
506 fibril_mutex_unlock(&term->mtx);
507
508 term_update(term);
509}
510
511static int term_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
512{
513 terminal_t *term = srv_to_terminal(srv);
514
515 fibril_mutex_lock(&term->mtx);
516 chargrid_get_cursor(term->frontbuf, col, row);
517 fibril_mutex_unlock(&term->mtx);
518
519 return EOK;
520}
521
522static int term_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
[6d5e378]523{
[5d94b16c]524 terminal_t *term = srv_to_terminal(srv);
[6d5e378]525
526 fibril_mutex_lock(&term->mtx);
[5d94b16c]527 *cols = term->cols;
528 *rows = term->rows;
[6d5e378]529 fibril_mutex_unlock(&term->mtx);
530
[5d94b16c]531 return EOK;
[6d5e378]532}
533
[5d94b16c]534static int term_get_color_cap(con_srv_t *srv, console_caps_t *caps)
[6d5e378]535{
[5d94b16c]536 (void) srv;
537 *caps = TERM_CAPS;
538
539 return EOK;
540}
541
542static void term_set_style(con_srv_t *srv, console_style_t style)
543{
544 terminal_t *term = srv_to_terminal(srv);
545
[6d5e378]546 fibril_mutex_lock(&term->mtx);
547 chargrid_set_style(term->frontbuf, style);
548 fibril_mutex_unlock(&term->mtx);
549}
550
[5d94b16c]551static void term_set_color(con_srv_t *srv, console_color_t bgcolor,
[6d5e378]552 console_color_t fgcolor, console_color_attr_t attr)
553{
[5d94b16c]554 terminal_t *term = srv_to_terminal(srv);
555
[6d5e378]556 fibril_mutex_lock(&term->mtx);
557 chargrid_set_color(term->frontbuf, bgcolor, fgcolor, attr);
558 fibril_mutex_unlock(&term->mtx);
559}
560
[5d94b16c]561static void term_set_rgb_color(con_srv_t *srv, pixel_t bgcolor,
[6d5e378]562 pixel_t fgcolor)
563{
[5d94b16c]564 terminal_t *term = srv_to_terminal(srv);
565
[6d5e378]566 fibril_mutex_lock(&term->mtx);
567 chargrid_set_rgb_color(term->frontbuf, bgcolor, fgcolor);
568 fibril_mutex_unlock(&term->mtx);
569}
570
[5d94b16c]571static void term_set_cursor_visibility(con_srv_t *srv, bool visible)
[6d5e378]572{
[5d94b16c]573 terminal_t *term = srv_to_terminal(srv);
574
575 fibril_mutex_lock(&term->mtx);
576 chargrid_set_cursor_visibility(term->frontbuf, visible);
577 fibril_mutex_unlock(&term->mtx);
578
579 term_update(term);
580}
581
[902f0906]582static int term_get_event(con_srv_t *srv, cons_event_t *event)
[5d94b16c]583{
584 terminal_t *term = srv_to_terminal(srv);
[6d5e378]585 link_t *link = prodcons_consume(&term->input_pc);
[3f06dae]586 cons_event_t *ev = list_get_instance(link, cons_event_t, link);
[6d5e378]587
[3f06dae]588 *event = *ev;
589 free(ev);
[5d94b16c]590 return EOK;
[6d5e378]591}
592
593void deinit_terminal(terminal_t *term)
594{
595 list_remove(&term->link);
596 widget_deinit(&term->widget);
597
598 if (term->frontbuf)
599 chargrid_destroy(term->frontbuf);
600
601 if (term->backbuf)
602 chargrid_destroy(term->backbuf);
603}
604
605static void terminal_destroy(widget_t *widget)
606{
607 terminal_t *term = (terminal_t *) widget;
608
609 deinit_terminal(term);
610 free(term);
611}
612
613static void terminal_reconfigure(widget_t *widget)
614{
615 /* No-op */
616}
617
618static void terminal_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
619 sysarg_t width, sysarg_t height)
620{
621 terminal_t *term = (terminal_t *) widget;
622
623 widget_modify(widget, hpos, vpos, width, height);
624 widget->width_ideal = width;
625 widget->height_ideal = height;
626
627 term_damage(term);
628}
629
630static void terminal_repaint(widget_t *widget)
631{
632 terminal_t *term = (terminal_t *) widget;
633
634 term_damage(term);
635}
636
[3f06dae]637static void terminal_queue_cons_event(terminal_t *term, cons_event_t *ev)
[6d5e378]638{
639 /* Got key press/release event */
[3f06dae]640 cons_event_t *event =
641 (cons_event_t *) malloc(sizeof(cons_event_t));
[6d5e378]642 if (event == NULL)
643 return;
644
[3f06dae]645 *event = *ev;
[6d5e378]646 link_initialize(&event->link);
647
648 prodcons_produce(&term->input_pc, &event->link);
649}
650
[3f06dae]651/* Got key press/release event */
652static void terminal_handle_keyboard_event(widget_t *widget,
653 kbd_event_t kbd_event)
[6d5e378]654{
[3f06dae]655 terminal_t *term = (terminal_t *) widget;
656 cons_event_t event;
657
658 event.type = CEV_KEY;
659 event.ev.key = kbd_event;
660
661 terminal_queue_cons_event(term, &event);
662}
663
664static void terminal_handle_position_event(widget_t *widget, pos_event_t pos_event)
665{
666 cons_event_t event;
667 terminal_t *term = (terminal_t *) widget;
668 sysarg_t sx = term->widget.hpos;
669 sysarg_t sy = term->widget.vpos;
670
671 if (pos_event.type == POS_PRESS) {
672 event.type = CEV_POS;
673 event.ev.pos.type = pos_event.type;
674 event.ev.pos.pos_id = pos_event.pos_id;
675 event.ev.pos.btn_num = pos_event.btn_num;
676
677 event.ev.pos.hpos = (pos_event.hpos - sx) / FONT_WIDTH;
678 event.ev.pos.vpos = (pos_event.vpos - sy) / FONT_SCANLINES;
679 terminal_queue_cons_event(term, &event);
680 }
[6d5e378]681}
682
683static void term_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
684{
685 terminal_t *term = NULL;
686
687 list_foreach(terms, link) {
688 terminal_t *cur = list_get_instance(link, terminal_t, link);
689
690 if (cur->dsid == (service_id_t) IPC_GET_ARG1(*icall)) {
691 term = cur;
692 break;
693 }
694 }
695
696 if (term == NULL) {
697 async_answer_0(iid, ENOENT);
698 return;
699 }
700
701 if (atomic_postinc(&term->refcnt) == 0)
[5d94b16c]702 chargrid_set_cursor_visibility(term->frontbuf, true);
[6d5e378]703
[5d94b16c]704 con_conn(iid, icall, &term->srvs);
[6d5e378]705}
706
707bool init_terminal(terminal_t *term, widget_t *parent, sysarg_t width,
708 sysarg_t height)
709{
710 widget_init(&term->widget, parent);
711
712 link_initialize(&term->link);
713 fibril_mutex_initialize(&term->mtx);
714 atomic_set(&term->refcnt, 0);
715
716 prodcons_initialize(&term->input_pc);
717 term->char_remains_len = 0;
718
719 term->widget.width = width;
720 term->widget.height = height;
721 term->widget.width_ideal = width;
722 term->widget.height_ideal = height;
723
724 term->widget.destroy = terminal_destroy;
725 term->widget.reconfigure = terminal_reconfigure;
726 term->widget.rearrange = terminal_rearrange;
727 term->widget.repaint = terminal_repaint;
728 term->widget.handle_keyboard_event = terminal_handle_keyboard_event;
729 term->widget.handle_position_event = terminal_handle_position_event;
730
731 term->cols = width / FONT_WIDTH;
732 term->rows = height / FONT_SCANLINES;
733
734 term->frontbuf = NULL;
735 term->backbuf = NULL;
736
737 term->frontbuf = chargrid_create(term->cols, term->rows,
738 CHARGRID_FLAG_NONE);
739 if (!term->frontbuf) {
740 widget_deinit(&term->widget);
741 return false;
742 }
743
744 term->backbuf = chargrid_create(term->cols, term->rows,
745 CHARGRID_FLAG_NONE);
746 if (!term->backbuf) {
747 widget_deinit(&term->widget);
748 return false;
749 }
750
751 chargrid_clear(term->frontbuf);
752 chargrid_clear(term->backbuf);
753 term->top_row = 0;
754
755 async_set_client_connection(term_connection);
[5d94b16c]756 con_srvs_init(&term->srvs);
757 term->srvs.ops = &con_ops;
758 term->srvs.sarg = term;
759
[6d5e378]760 int rc = loc_server_register(NAME);
761 if (rc != EOK) {
762 widget_deinit(&term->widget);
763 return false;
764 }
765
766 char vc[LOC_NAME_MAXLEN + 1];
767 snprintf(vc, LOC_NAME_MAXLEN, "%s/%" PRIu64, NAMESPACE,
768 task_get_id());
769
770 rc = loc_service_register(vc, &term->dsid);
771 if (rc != EOK) {
772 widget_deinit(&term->widget);
773 return false;
774 }
775
776 list_append(&term->link, &terms);
777 getterm(vc, "/app/bdsh");
778
779 return true;
780}
781
782terminal_t *create_terminal(widget_t *parent, sysarg_t width, sysarg_t height)
783{
784 terminal_t *term = (terminal_t *) malloc(sizeof(terminal_t));
785 if (!term)
786 return NULL;
787
788 bool ret = init_terminal(term, parent, width, height);
789 if (!ret) {
790 free(term);
791 return NULL;
792 }
793
794 return term;
795}
796
797/** @}
798 */
Note: See TracBrowser for help on using the repository browser.