Changeset f03d1308 in mainline
- Timestamp:
- 2020-10-28T12:42:11Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8009dc27
- Parents:
- d284ce9
- git-author:
- Jiri Svoboda <jiri@…> (2020-10-28 12:41:11)
- git-committer:
- Jiri Svoboda <jiri@…> (2020-10-28 12:42:11)
- Location:
- uspace
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/terminal/main.c
rd284ce9 rf03d1308 1 1 /* 2 * Copyright (c) 20 19Jiri Svoboda2 * Copyright (c) 2020 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 33 33 */ 34 34 35 #include <display.h>36 35 #include <stdio.h> 37 36 #include <task.h> 37 #include <ui/ui.h> 38 38 #include "terminal.h" 39 39 … … 43 43 static void print_syntax(void) 44 44 { 45 printf("Syntax: %s [-d <display >]\n", NAME);45 printf("Syntax: %s [-d <display-spec>]\n", NAME); 46 46 } 47 47 48 48 int main(int argc, char *argv[]) 49 49 { 50 const char *display_svc = DISPLAY_DEFAULT; 51 display_t *display = NULL; 50 const char *display_spec = UI_DISPLAY_DEFAULT; 52 51 terminal_t *terminal = NULL; 53 52 errno_t rc; … … 64 63 } 65 64 66 display_s vc = argv[i++];65 display_spec = argv[i++]; 67 66 } else { 68 67 printf("Invalid option '%s'.\n", argv[i]); … … 77 76 } 78 77 79 rc = display_open(display_svc, &display); 80 if (rc != EOK) { 81 printf("%s: Error opening display.\n", NAME); 78 rc = terminal_create(display_spec, 640, 480, &terminal); 79 if (rc != EOK) 82 80 return 1; 83 }84 85 rc = terminal_create(display, 640, 480, &terminal);86 if (rc != EOK) {87 display_close(display);88 return 1;89 }90 81 91 82 task_retval(0); -
uspace/app/terminal/terminal.c
rd284ce9 rf03d1308 1 1 /* 2 * Copyright (c) 20 19Jiri Svoboda2 * Copyright (c) 2020 Jiri Svoboda 3 3 * Copyright (c) 2012 Petr Koupy 4 4 * All rights reserved. … … 32 32 */ 33 33 /** 34 * @file Terminal application using display service for output34 * @file Terminal application 35 35 */ 36 36 … … 51 51 #include <str.h> 52 52 #include <ui/resource.h> 53 #include <ui/ui.h> 53 54 #include <ui/wdecor.h> 55 #include <ui/window.h> 54 56 55 57 #include "terminal.h" … … 102 104 }; 103 105 104 static void terminal_close_event(void *); 105 static void terminal_focus_event(void *); 106 static void terminal_kbd_event(void *, kbd_event_t *); 107 static void terminal_pos_event(void *, pos_event_t *); 108 static void terminal_unfocus_event(void *); 109 110 static display_wnd_cb_t terminal_wnd_cb = { 111 .close_event = terminal_close_event, 112 .focus_event = terminal_focus_event, 113 .kbd_event = terminal_kbd_event, 114 .pos_event = terminal_pos_event, 115 .unfocus_event = terminal_unfocus_event 116 }; 117 118 static void terminal_wd_close(ui_wdecor_t *, void *); 119 static void terminal_wd_move(ui_wdecor_t *, void *, gfx_coord2_t *); 120 121 static ui_wdecor_cb_t wdecor_cb = { 122 .close = terminal_wd_close, 123 .move = terminal_wd_move 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 124 118 }; 125 119 … … 677 671 678 672 /** Handle window close event. */ 679 static void terminal_close_event( void *arg)673 static void terminal_close_event(ui_window_t *window, void *arg) 680 674 { 681 675 terminal_t *term = (terminal_t *) arg; … … 688 682 689 683 /** Handle window focus event. */ 690 static void terminal_focus_event( void *arg)684 static void terminal_focus_event(ui_window_t *window, void *arg) 691 685 { 692 686 terminal_t *term = (terminal_t *) arg; 693 687 694 if (term->wdecor != NULL) { 695 ui_wdecor_set_active(term->wdecor, true); 696 ui_wdecor_paint(term->wdecor); 697 698 term->is_focused = true; 699 term_update(term); 700 } 688 term->is_focused = true; 689 term_update(term); 701 690 } 702 691 703 692 /** Handle window keyboard event */ 704 static void terminal_kbd_event(void *arg, kbd_event_t *kbd_event) 693 static void terminal_kbd_event(ui_window_t *window, void *arg, 694 kbd_event_t *kbd_event) 705 695 { 706 696 terminal_t *term = (terminal_t *) arg; … … 714 704 715 705 /** Handle window position event */ 716 static void terminal_pos_event( void *arg, pos_event_t *event)706 static void terminal_pos_event(ui_window_t *window, void *arg, pos_event_t *event) 717 707 { 718 708 cons_event_t cevent; 719 709 terminal_t *term = (terminal_t *) arg; 720 721 /* Make sure we don't process events until fully initialized */722 if (term->wdecor == NULL)723 return;724 725 ui_wdecor_pos_event(term->wdecor, event);726 710 727 711 sysarg_t sx = -term->off.x; … … 741 725 742 726 /** Handle window unfocus event. */ 743 static void terminal_unfocus_event( void *arg)727 static void terminal_unfocus_event(ui_window_t *window, void *arg) 744 728 { 745 729 terminal_t *term = (terminal_t *) arg; 746 730 747 if (term->wdecor != NULL) { 748 ui_wdecor_set_active(term->wdecor, false); 749 ui_wdecor_paint(term->wdecor); 750 751 term->is_focused = false; 752 term_update(term); 753 } 754 } 755 756 /** Window decoration requested window closure. 757 * 758 * @param wdecor Window decoration 759 * @param arg Argument (demo) 760 */ 761 static void terminal_wd_close(ui_wdecor_t *wdecor, void *arg) 762 { 763 terminal_t *term = (terminal_t *) arg; 764 765 (void) term; 766 767 // XXX This is not really a clean way of terminating 768 exit(0); 769 } 770 771 /** Window decoration requested window move. 772 * 773 * @param wdecor Window decoration 774 * @param arg Argument (demo) 775 * @param pos Position where the title bar was pressed 776 */ 777 static void terminal_wd_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos) 778 { 779 terminal_t *term = (terminal_t *) arg; 780 781 if (term->window != NULL) 782 (void) display_window_move_req(term->window, pos); 731 term->is_focused = false; 732 term_update(term); 783 733 } 784 734 … … 805 755 } 806 756 807 errno_t terminal_create( display_t *display, sysarg_t width, sysarg_t height,808 terminal_t **rterm)757 errno_t terminal_create(const char *display_spec, sysarg_t width, 758 sysarg_t height, terminal_t **rterm) 809 759 { 810 760 terminal_t *term; 811 761 gfx_bitmap_params_t params; 812 display_wnd_params_t wparams;762 ui_wnd_params_t wparams; 813 763 gfx_rect_t rect; 814 764 gfx_coord2_t off; … … 859 809 rect.p1.y = height; 860 810 861 display_wnd_params_init(&wparams); 811 ui_wnd_params_init(&wparams); 812 wparams.caption = "Terminal"; 862 813 863 814 /* … … 871 822 term->off = off; 872 823 873 rc = display_window_create(display, &wparams, &terminal_wnd_cb, 874 (void *) term, &term->window); 824 rc = ui_create(display_spec, &term->ui); 825 if (rc != EOK) { 826 printf("Error creating UI on %s.\n", display_spec); 827 goto error; 828 } 829 830 rc = ui_window_create(term->ui, &wparams, &term->window); 875 831 if (rc != EOK) { 876 832 printf("Error creating window.\n"); … … 878 834 } 879 835 880 rc = display_window_get_gc(term->window, &term->gc); 881 if (rc != EOK) { 882 printf("Error getting window GC.\n"); 883 goto error; 884 } 885 886 rc = ui_resource_create(term->gc, &term->ui_res); 887 if (rc != EOK) { 888 printf("Error creating UI.\n"); 889 goto error; 890 } 891 892 rc = ui_wdecor_create(term->ui_res, "Terminal", &term->wdecor); 893 if (rc != EOK) { 894 printf("Error creating window decoration.\n"); 895 goto error; 896 } 897 898 ui_wdecor_set_rect(term->wdecor, &wparams.rect); 899 ui_wdecor_set_cb(term->wdecor, &wdecor_cb, (void *) term); 900 901 (void) ui_wdecor_paint(term->wdecor); 836 term->gc = ui_window_get_gc(term->window); 837 term->ui_res = ui_window_get_res(term->window); 838 839 ui_window_set_cb(term->window, &terminal_window_cb, (void *) term); 902 840 903 841 gfx_bitmap_params_init(¶ms); … … 943 881 getterm(vc, "/app/bdsh"); 944 882 883 term->is_focused = true; 884 945 885 term->update.p0.x = 0; 946 886 term->update.p0.y = 0; … … 953 893 return EOK; 954 894 error: 955 if (term->wdecor != NULL)956 ui_wdecor_destroy(term->wdecor);957 if (term->ui_res != NULL)958 ui_resource_destroy(term->ui_res);959 if (term->gc != NULL)960 gfx_context_delete(term->gc);961 895 if (term->window != NULL) 962 display_window_destroy(term->window); 896 ui_window_destroy(term->window); 897 if (term->ui != NULL) 898 ui_destroy(term->ui); 963 899 if (term->frontbuf != NULL) 964 900 chargrid_destroy(term->frontbuf); -
uspace/app/terminal/terminal.h
rd284ce9 rf03d1308 38 38 #define TERMINAL_H 39 39 40 #include <display.h>41 40 #include <errno.h> 42 41 #include <fibril_synch.h> … … 50 49 #include <stdatomic.h> 51 50 #include <str.h> 52 #include <ui/ resource.h>53 #include <ui/w decor.h>51 #include <ui/ui.h> 52 #include <ui/window.h> 54 53 55 54 #define UTF8_CHAR_BUFFER_SIZE (STR_BOUNDS(1) + 1) 56 55 57 56 typedef struct { 58 display_window_t *window; 57 ui_t *ui; 58 ui_window_t *window; 59 ui_resource_t *ui_res; 59 60 gfx_context_t *gc; 61 60 62 gfx_bitmap_t *bmp; 61 63 sysarg_t w; … … 64 66 gfx_coord2_t off; 65 67 bool is_focused; 66 67 ui_resource_t *ui_res;68 ui_wdecor_t *wdecor;69 68 70 69 fibril_mutex_t mtx; … … 86 85 } terminal_t; 87 86 88 extern errno_t terminal_create( display_t*, sysarg_t, sysarg_t, terminal_t **);87 extern errno_t terminal_create(const char *, sysarg_t, sysarg_t, terminal_t **); 89 88 extern void terminal_destroy(terminal_t *); 90 89 -
uspace/lib/ui/include/types/ui/window.h
rd284ce9 rf03d1308 37 37 #define _UI_TYPES_WINDOW_H 38 38 39 #include <io/kbd_event.h> 39 40 #include <io/pos_event.h> 40 41 … … 53 54 typedef struct ui_window_cb { 54 55 void (*close)(ui_window_t *, void *); 56 void (*focus)(ui_window_t *, void *); 57 void (*kbd)(ui_window_t *, void *, kbd_event_t *); 55 58 void (*pos)(ui_window_t *, void *, pos_event_t *); 59 void (*unfocus)(ui_window_t *, void *); 56 60 } ui_window_cb_t; 57 61 -
uspace/lib/ui/private/window.h
rd284ce9 rf03d1308 40 40 #include <display.h> 41 41 #include <gfx/context.h> 42 #include <io/kbd_event.h> 42 43 #include <io/pos_event.h> 43 44 … … 64 65 65 66 extern void ui_window_close(ui_window_t *); 67 extern void ui_window_focus(ui_window_t *); 68 extern void ui_window_kbd(ui_window_t *, kbd_event_t *); 66 69 extern void ui_window_pos(ui_window_t *, pos_event_t *); 70 extern void ui_window_unfocus(ui_window_t *); 67 71 68 72 #endif -
uspace/lib/ui/src/window.c
rd284ce9 rf03d1308 37 37 #include <errno.h> 38 38 #include <gfx/context.h> 39 #include <io/kbd_event.h> 39 40 #include <io/pos_event.h> 40 41 #include <mem.h> … … 221 222 ui_wdecor_paint(window->wdecor); 222 223 } 224 225 ui_window_focus(window); 223 226 } 224 227 … … 229 232 230 233 (void) window; 231 (void) kbd_event;234 ui_window_kbd(window, kbd_event); 232 235 } 233 236 … … 254 257 ui_wdecor_paint(window->wdecor); 255 258 } 259 260 ui_window_unfocus(window); 256 261 } 257 262 … … 291 296 } 292 297 298 /** Send window focus event. 299 * 300 * @param window Window 301 */ 302 void ui_window_focus(ui_window_t *window) 303 { 304 if (window->cb != NULL && window->cb->focus != NULL) 305 window->cb->focus(window, window->arg); 306 } 307 308 /** Send window keyboard event. 309 * 310 * @param window Window 311 */ 312 void ui_window_kbd(ui_window_t *window, kbd_event_t *kbd) 313 { 314 if (window->cb != NULL && window->cb->kbd != NULL) 315 window->cb->kbd(window, window->arg, kbd); 316 } 317 293 318 /** Send window position event. 294 319 * … … 301 326 } 302 327 328 /** Send window unfocus event. 329 * 330 * @param window Window 331 */ 332 void ui_window_unfocus(ui_window_t *window) 333 { 334 if (window->cb != NULL && window->cb->unfocus != NULL) 335 window->cb->unfocus(window, window->arg); 336 } 337 303 338 /** @} 304 339 */ -
uspace/lib/ui/test/window.c
rd284ce9 rf03d1308 29 29 #include <gfx/context.h> 30 30 #include <gfx/coord.h> 31 #include <io/kbd_event.h> 31 32 #include <io/pos_event.h> 32 33 #include <mem.h> … … 43 44 44 45 static void test_window_close(ui_window_t *, void *); 46 static void test_window_focus(ui_window_t *, void *); 47 static void test_window_kbd(ui_window_t *, void *, kbd_event_t *); 45 48 static void test_window_pos(ui_window_t *, void *, pos_event_t *); 49 static void test_window_unfocus(ui_window_t *, void *); 46 50 47 51 static ui_window_cb_t test_window_cb = { 48 52 .close = test_window_close, 49 .pos = test_window_pos 53 .focus = test_window_focus, 54 .kbd = test_window_kbd, 55 .pos = test_window_pos, 56 .unfocus = test_window_unfocus 50 57 }; 51 58 … … 55 62 typedef struct { 56 63 bool close; 64 bool focus; 65 bool kbd; 66 kbd_event_t kbd_event; 57 67 bool pos; 58 68 pos_event_t pos_event; 69 bool unfocus; 59 70 } test_cb_resp_t; 60 71 … … 151 162 ui_window_close(window); 152 163 PCUT_ASSERT_TRUE(resp.close); 164 165 ui_window_destroy(window); 166 ui_destroy(ui); 167 } 168 169 /** ui_window_focus() calls focus callback set via ui_window_set_cb() */ 170 PCUT_TEST(focus) 171 { 172 errno_t rc; 173 ui_t *ui = NULL; 174 ui_wnd_params_t params; 175 ui_window_t *window = NULL; 176 test_cb_resp_t resp; 177 178 rc = ui_create_disp(NULL, &ui); 179 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 180 181 ui_wnd_params_init(¶ms); 182 params.caption = "Hello"; 183 184 rc = ui_window_create(ui, ¶ms, &window); 185 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 186 PCUT_ASSERT_NOT_NULL(window); 187 188 /* Focus callback with no callbacks set */ 189 ui_window_focus(window); 190 191 /* Focus callback with focus callback not implemented */ 192 ui_window_set_cb(window, &dummy_window_cb, NULL); 193 ui_window_focus(window); 194 195 /* Focus callback with real callback set */ 196 resp.close = false; 197 ui_window_set_cb(window, &test_window_cb, &resp); 198 ui_window_focus(window); 199 PCUT_ASSERT_TRUE(resp.focus); 200 201 ui_window_destroy(window); 202 ui_destroy(ui); 203 } 204 205 /** ui_window_kbd() calls kbd callback set via ui_window_set_cb() */ 206 PCUT_TEST(kbd) 207 { 208 errno_t rc; 209 ui_t *ui = NULL; 210 ui_wnd_params_t params; 211 ui_window_t *window = NULL; 212 kbd_event_t kbd_event; 213 test_cb_resp_t resp; 214 215 rc = ui_create_disp(NULL, &ui); 216 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 217 218 ui_wnd_params_init(¶ms); 219 params.caption = "Hello"; 220 221 rc = ui_window_create(ui, ¶ms, &window); 222 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 223 PCUT_ASSERT_NOT_NULL(window); 224 225 kbd_event.type = POS_PRESS; 226 kbd_event.key = KC_X; 227 kbd_event.mods = 0; 228 kbd_event.c = 'x'; 229 230 /* Kbd callback with no callbacks set */ 231 ui_window_kbd(window, &kbd_event); 232 233 /* Kbd callback with kbd callback not implemented */ 234 ui_window_set_cb(window, &dummy_window_cb, NULL); 235 ui_window_kbd(window, &kbd_event); 236 237 /* Kbd callback with real callback set */ 238 resp.kbd = false; 239 ui_window_set_cb(window, &test_window_cb, &resp); 240 ui_window_kbd(window, &kbd_event); 241 PCUT_ASSERT_TRUE(resp.kbd); 242 PCUT_ASSERT_EQUALS(kbd_event.type, resp.kbd_event.type); 243 PCUT_ASSERT_INT_EQUALS(kbd_event.key, resp.kbd_event.key); 244 PCUT_ASSERT_INT_EQUALS(kbd_event.mods, resp.kbd_event.mods); 245 PCUT_ASSERT_INT_EQUALS(kbd_event.c, resp.kbd_event.c); 153 246 154 247 ui_window_destroy(window); … … 204 297 } 205 298 299 /** ui_window_unfocus() calls unfocus callback set via ui_window_set_cb() */ 300 PCUT_TEST(unfocus) 301 { 302 errno_t rc; 303 ui_t *ui = NULL; 304 ui_wnd_params_t params; 305 ui_window_t *window = NULL; 306 test_cb_resp_t resp; 307 308 rc = ui_create_disp(NULL, &ui); 309 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 310 311 ui_wnd_params_init(¶ms); 312 params.caption = "Hello"; 313 314 rc = ui_window_create(ui, ¶ms, &window); 315 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 316 PCUT_ASSERT_NOT_NULL(window); 317 318 /* Unfocus callback with no callbacks set */ 319 ui_window_unfocus(window); 320 321 /* Unfocus callback with unfocus callback not implemented */ 322 ui_window_set_cb(window, &dummy_window_cb, NULL); 323 ui_window_unfocus(window); 324 325 /* Unfocus callback with real callback set */ 326 resp.close = false; 327 ui_window_set_cb(window, &test_window_cb, &resp); 328 ui_window_unfocus(window); 329 PCUT_ASSERT_TRUE(resp.unfocus); 330 331 ui_window_destroy(window); 332 ui_destroy(ui); 333 } 334 206 335 static void test_window_close(ui_window_t *window, void *arg) 207 336 { … … 209 338 210 339 resp->close = true; 340 } 341 342 static void test_window_focus(ui_window_t *window, void *arg) 343 { 344 test_cb_resp_t *resp = (test_cb_resp_t *) arg; 345 346 resp->focus = true; 347 } 348 349 static void test_window_kbd(ui_window_t *window, void *arg, 350 kbd_event_t *event) 351 { 352 test_cb_resp_t *resp = (test_cb_resp_t *) arg; 353 354 resp->kbd = true; 355 resp->kbd_event = *event; 211 356 } 212 357 … … 220 365 } 221 366 367 static void test_window_unfocus(ui_window_t *window, void *arg) 368 { 369 test_cb_resp_t *resp = (test_cb_resp_t *) arg; 370 371 resp->unfocus = true; 372 } 373 222 374 PCUT_EXPORT(window);
Note:
See TracChangeset
for help on using the changeset viewer.