[c0757e1f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
| 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 display-cfg
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Display configuration utility (UI)
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <gfx/coord.h>
|
---|
| 36 | #include <stdio.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | #include <str.h>
|
---|
| 39 | #include <ui/fixed.h>
|
---|
| 40 | #include <ui/resource.h>
|
---|
| 41 | #include <ui/tabset.h>
|
---|
| 42 | #include <ui/ui.h>
|
---|
| 43 | #include <ui/window.h>
|
---|
| 44 | #include "display-cfg.h"
|
---|
| 45 | #include "seats.h"
|
---|
| 46 |
|
---|
| 47 | static void wnd_close(ui_window_t *, void *);
|
---|
| 48 |
|
---|
| 49 | static ui_window_cb_t window_cb = {
|
---|
| 50 | .close = wnd_close
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | /** Window close button was clicked.
|
---|
| 54 | *
|
---|
| 55 | * @param window Window
|
---|
| 56 | * @param arg Argument (dcfg)
|
---|
| 57 | */
|
---|
| 58 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
| 59 | {
|
---|
| 60 | display_cfg_t *dcfg = (display_cfg_t *) arg;
|
---|
| 61 |
|
---|
| 62 | ui_quit(dcfg->ui);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** Create display configuration dialog.
|
---|
| 66 | *
|
---|
| 67 | * @param display_spec Display specification
|
---|
| 68 | * @param rdcfg Place to store pointer to new display configuration
|
---|
| 69 | * @return EOK on success or an error code
|
---|
| 70 | */
|
---|
| 71 | static errno_t display_cfg_create(const char *display_spec,
|
---|
| 72 | display_cfg_t **rdcfg)
|
---|
| 73 | {
|
---|
| 74 | ui_t *ui = NULL;
|
---|
| 75 | ui_wnd_params_t params;
|
---|
| 76 | ui_window_t *window = NULL;
|
---|
| 77 | display_cfg_t *dcfg = NULL;
|
---|
| 78 | gfx_rect_t rect;
|
---|
| 79 | ui_resource_t *ui_res;
|
---|
| 80 | errno_t rc;
|
---|
| 81 |
|
---|
| 82 | dcfg = calloc(1, sizeof(display_cfg_t));
|
---|
| 83 | if (dcfg == NULL) {
|
---|
| 84 | printf("Out of memory.\n");
|
---|
| 85 | return ENOMEM;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | rc = dispcfg_open(DISPCFG_DEFAULT, NULL, NULL, &dcfg->dispcfg);
|
---|
| 89 | if (rc != EOK) {
|
---|
| 90 | printf("Error opening display configuration service.\n");
|
---|
| 91 | goto error;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | rc = ui_create(display_spec, &ui);
|
---|
| 95 | if (rc != EOK) {
|
---|
| 96 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
| 97 | goto error;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | ui_wnd_params_init(¶ms);
|
---|
| 101 | params.caption = "Display Configuration";
|
---|
| 102 | if (ui_is_textmode(ui)) {
|
---|
| 103 | params.rect.p0.x = 0;
|
---|
| 104 | params.rect.p0.y = 0;
|
---|
| 105 | params.rect.p1.x = 70;
|
---|
| 106 | params.rect.p1.y = 23;
|
---|
| 107 | } else {
|
---|
| 108 | params.rect.p0.x = 0;
|
---|
| 109 | params.rect.p0.y = 0;
|
---|
| 110 | params.rect.p1.x = 470;
|
---|
| 111 | params.rect.p1.y = 350;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | dcfg->ui = ui;
|
---|
| 115 |
|
---|
| 116 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 117 | if (rc != EOK) {
|
---|
| 118 | printf("Error creating window.\n");
|
---|
| 119 | goto error;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | ui_window_set_cb(window, &window_cb, (void *)dcfg);
|
---|
| 123 | dcfg->window = window;
|
---|
| 124 |
|
---|
| 125 | ui_res = ui_window_get_res(window);
|
---|
| 126 |
|
---|
| 127 | rc = ui_fixed_create(&dcfg->fixed);
|
---|
| 128 | if (rc != EOK) {
|
---|
| 129 | printf("Error creating fixed layout.\n");
|
---|
| 130 | return rc;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | rc = ui_tab_set_create(ui_res, &dcfg->tabset);
|
---|
| 134 | if (rc != EOK) {
|
---|
| 135 | printf("Error creating tab set.\n");
|
---|
| 136 | return rc;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | ui_window_get_app_rect(window, &rect);
|
---|
| 140 | ui_tab_set_set_rect(dcfg->tabset, &rect);
|
---|
| 141 |
|
---|
| 142 | rc = ui_fixed_add(dcfg->fixed, ui_tab_set_ctl(dcfg->tabset));
|
---|
| 143 | if (rc != EOK) {
|
---|
| 144 | printf("Error adding control to layout.\n");
|
---|
| 145 | return rc;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | rc = dcfg_seats_create(dcfg, &dcfg->seats);
|
---|
| 149 | if (rc != EOK)
|
---|
| 150 | goto error;
|
---|
| 151 |
|
---|
| 152 | ui_window_add(window, ui_fixed_ctl(dcfg->fixed));
|
---|
| 153 |
|
---|
| 154 | rc = ui_window_paint(window);
|
---|
| 155 | if (rc != EOK) {
|
---|
| 156 | printf("Error painting window.\n");
|
---|
| 157 | return rc;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | *rdcfg = dcfg;
|
---|
| 161 | return EOK;
|
---|
| 162 | error:
|
---|
| 163 | if (dcfg->seats != NULL)
|
---|
| 164 | dcfg_seats_destroy(dcfg->seats);
|
---|
| 165 | if (dcfg->tabset != NULL)
|
---|
| 166 | ui_tab_set_destroy(dcfg->tabset);
|
---|
| 167 | if (dcfg->fixed != NULL)
|
---|
| 168 | ui_fixed_destroy(dcfg->fixed);
|
---|
| 169 | if (dcfg->ui != NULL)
|
---|
| 170 | ui_destroy(ui);
|
---|
| 171 | if (dcfg->dispcfg != NULL)
|
---|
| 172 | dispcfg_close(dcfg->dispcfg);
|
---|
| 173 | free(dcfg);
|
---|
| 174 | return rc;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** Destroy display configuration dialog.
|
---|
| 178 | *
|
---|
| 179 | * @param dcfg Display configuration dialog
|
---|
| 180 | */
|
---|
| 181 | static void display_cfg_destroy(display_cfg_t *dcfg)
|
---|
| 182 | {
|
---|
| 183 | ui_window_destroy(dcfg->window);
|
---|
| 184 | ui_destroy(dcfg->ui);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | static void print_syntax(void)
|
---|
| 188 | {
|
---|
| 189 | printf("Syntax: display-cfg [-d <display-spec>]\n");
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | int main(int argc, char *argv[])
|
---|
| 193 | {
|
---|
| 194 | const char *display_spec = UI_ANY_DEFAULT;
|
---|
| 195 | display_cfg_t *dcfg;
|
---|
| 196 | errno_t rc;
|
---|
| 197 | int i;
|
---|
| 198 |
|
---|
| 199 | i = 1;
|
---|
| 200 | while (i < argc && argv[i][0] == '-') {
|
---|
| 201 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 202 | ++i;
|
---|
| 203 | if (i >= argc) {
|
---|
| 204 | printf("Argument missing.\n");
|
---|
| 205 | print_syntax();
|
---|
| 206 | return 1;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | display_spec = argv[i++];
|
---|
| 210 | } else {
|
---|
| 211 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 212 | print_syntax();
|
---|
| 213 | return 1;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | if (i < argc) {
|
---|
| 218 | print_syntax();
|
---|
| 219 | return 1;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | rc = display_cfg_create(display_spec, &dcfg);
|
---|
| 223 | if (rc != EOK)
|
---|
| 224 | return 1;
|
---|
| 225 |
|
---|
| 226 | ui_run(dcfg->ui);
|
---|
| 227 | display_cfg_destroy(dcfg);
|
---|
| 228 |
|
---|
| 229 | return 0;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /** @}
|
---|
| 233 | */
|
---|