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