[68698ba] | 1 | /*
|
---|
[a0aeb8f] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[68698ba] | 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 hello
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Hello world (in UI)
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <gfx/coord.h>
|
---|
| 36 | #include <stdio.h>
|
---|
| 37 | #include <str.h>
|
---|
| 38 | #include <ui/fixed.h>
|
---|
| 39 | #include <ui/label.h>
|
---|
| 40 | #include <ui/resource.h>
|
---|
| 41 | #include <ui/ui.h>
|
---|
| 42 | #include <ui/window.h>
|
---|
| 43 | #include "hello.h"
|
---|
| 44 |
|
---|
| 45 | static void wnd_close(ui_window_t *, void *);
|
---|
| 46 |
|
---|
| 47 | static ui_window_cb_t window_cb = {
|
---|
| 48 | .close = wnd_close
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | /** Window close button was clicked.
|
---|
| 52 | *
|
---|
| 53 | * @param window Window
|
---|
| 54 | * @param arg Argument (hello)
|
---|
| 55 | */
|
---|
| 56 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
| 57 | {
|
---|
| 58 | hello_t *hello = (hello_t *) arg;
|
---|
| 59 |
|
---|
| 60 | ui_quit(hello->ui);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /** Run hello world on display server. */
|
---|
| 64 | static errno_t hello(const char *display_spec)
|
---|
| 65 | {
|
---|
| 66 | ui_t *ui = NULL;
|
---|
| 67 | ui_wnd_params_t params;
|
---|
| 68 | ui_window_t *window = NULL;
|
---|
| 69 | hello_t hello;
|
---|
| 70 | gfx_rect_t rect;
|
---|
| 71 | ui_resource_t *ui_res;
|
---|
| 72 | errno_t rc;
|
---|
| 73 |
|
---|
| 74 | rc = ui_create(display_spec, &ui);
|
---|
| 75 | if (rc != EOK) {
|
---|
| 76 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
| 77 | return rc;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | ui_wnd_params_init(¶ms);
|
---|
| 81 | params.caption = "Hello World";
|
---|
[a0aeb8f] | 82 | if (ui_is_textmode(ui)) {
|
---|
| 83 | params.rect.p0.x = 0;
|
---|
| 84 | params.rect.p0.y = 0;
|
---|
| 85 | params.rect.p1.x = 24;
|
---|
| 86 | params.rect.p1.y = 5;
|
---|
| 87 | } else {
|
---|
| 88 | params.rect.p0.x = 0;
|
---|
| 89 | params.rect.p0.y = 0;
|
---|
| 90 | params.rect.p1.x = 200;
|
---|
| 91 | params.rect.p1.y = 60;
|
---|
| 92 | }
|
---|
[68698ba] | 93 |
|
---|
| 94 | memset((void *) &hello, 0, sizeof(hello));
|
---|
| 95 | hello.ui = ui;
|
---|
| 96 |
|
---|
| 97 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 98 | if (rc != EOK) {
|
---|
| 99 | printf("Error creating window.\n");
|
---|
| 100 | return rc;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | ui_window_set_cb(window, &window_cb, (void *) &hello);
|
---|
| 104 | hello.window = window;
|
---|
| 105 |
|
---|
| 106 | ui_res = ui_window_get_res(window);
|
---|
| 107 |
|
---|
| 108 | rc = ui_fixed_create(&hello.fixed);
|
---|
| 109 | if (rc != EOK) {
|
---|
| 110 | printf("Error creating fixed layout.\n");
|
---|
| 111 | return rc;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | rc = ui_label_create(ui_res, "Hello, world!", &hello.label);
|
---|
| 115 | if (rc != EOK) {
|
---|
| 116 | printf("Error creating label.\n");
|
---|
| 117 | return rc;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[a0aeb8f] | 120 | ui_window_get_app_rect(window, &rect);
|
---|
[68698ba] | 121 | ui_label_set_rect(hello.label, &rect);
|
---|
| 122 | ui_label_set_halign(hello.label, gfx_halign_center);
|
---|
[a0aeb8f] | 123 | ui_label_set_valign(hello.label, gfx_valign_center);
|
---|
[68698ba] | 124 |
|
---|
| 125 | rc = ui_fixed_add(hello.fixed, ui_label_ctl(hello.label));
|
---|
| 126 | if (rc != EOK) {
|
---|
| 127 | printf("Error adding control to layout.\n");
|
---|
| 128 | return rc;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | ui_window_add(window, ui_fixed_ctl(hello.fixed));
|
---|
| 132 |
|
---|
| 133 | rc = ui_window_paint(window);
|
---|
| 134 | if (rc != EOK) {
|
---|
| 135 | printf("Error painting window.\n");
|
---|
| 136 | return rc;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | ui_run(ui);
|
---|
| 140 |
|
---|
| 141 | ui_window_destroy(window);
|
---|
| 142 | ui_destroy(ui);
|
---|
| 143 |
|
---|
| 144 | return EOK;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | static void print_syntax(void)
|
---|
| 148 | {
|
---|
| 149 | printf("Syntax: hello [-d <display-spec>]\n");
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | int main(int argc, char *argv[])
|
---|
| 153 | {
|
---|
[552b69f] | 154 | const char *display_spec = UI_ANY_DEFAULT;
|
---|
[68698ba] | 155 | errno_t rc;
|
---|
| 156 | int i;
|
---|
| 157 |
|
---|
| 158 | i = 1;
|
---|
| 159 | while (i < argc && argv[i][0] == '-') {
|
---|
| 160 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 161 | ++i;
|
---|
| 162 | if (i >= argc) {
|
---|
| 163 | printf("Argument missing.\n");
|
---|
| 164 | print_syntax();
|
---|
| 165 | return 1;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | display_spec = argv[i++];
|
---|
| 169 | } else {
|
---|
| 170 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 171 | print_syntax();
|
---|
| 172 | return 1;
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | if (i < argc) {
|
---|
| 177 | print_syntax();
|
---|
| 178 | return 1;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | rc = hello(display_spec);
|
---|
| 182 | if (rc != EOK)
|
---|
| 183 | return 1;
|
---|
| 184 |
|
---|
| 185 | return 0;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /** @}
|
---|
| 189 | */
|
---|