[c8cf261] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 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
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Display management
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <disp_srv.h>
|
---|
| 37 | #include <errno.h>
|
---|
[159776f] | 38 | #include <gfx/context.h>
|
---|
[c8cf261] | 39 | #include <io/log.h>
|
---|
[6af4b4f] | 40 | #include <stdlib.h>
|
---|
[b3c185b6] | 41 | #include "client.h"
|
---|
[6af4b4f] | 42 | #include "window.h"
|
---|
[b3c185b6] | 43 | #include "display.h"
|
---|
[c8cf261] | 44 |
|
---|
| 45 | static errno_t disp_window_create(void *, sysarg_t *);
|
---|
| 46 | static errno_t disp_window_destroy(void *, sysarg_t);
|
---|
[b3c185b6] | 47 | static errno_t disp_get_event(void *, sysarg_t *, display_wnd_ev_t *);
|
---|
[c8cf261] | 48 |
|
---|
| 49 | display_ops_t display_srv_ops = {
|
---|
| 50 | .window_create = disp_window_create,
|
---|
[b3c185b6] | 51 | .window_destroy = disp_window_destroy,
|
---|
| 52 | .get_event = disp_get_event
|
---|
[c8cf261] | 53 | };
|
---|
| 54 |
|
---|
| 55 | static errno_t disp_window_create(void *arg, sysarg_t *rwnd_id)
|
---|
| 56 | {
|
---|
[6af4b4f] | 57 | errno_t rc;
|
---|
[b3c185b6] | 58 | ds_client_t *client = (ds_client_t *) arg;
|
---|
[6af4b4f] | 59 | ds_window_t *wnd;
|
---|
| 60 |
|
---|
[c8cf261] | 61 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create()");
|
---|
[6af4b4f] | 62 |
|
---|
[b3c185b6] | 63 | rc = ds_window_create(client, &wnd);
|
---|
[6af4b4f] | 64 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create() - ds_window_create -> %d", rc);
|
---|
| 65 | if (rc != EOK)
|
---|
| 66 | return rc;
|
---|
| 67 |
|
---|
| 68 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create() -> EOK, id=%zu",
|
---|
| 69 | wnd->id);
|
---|
[22faaf2] | 70 |
|
---|
| 71 | wnd->dpos.x = ((wnd->id - 1) & 1) * 400;
|
---|
| 72 | wnd->dpos.y = ((wnd->id - 1) & 2) / 2 * 300;
|
---|
| 73 |
|
---|
[6af4b4f] | 74 | *rwnd_id = wnd->id;
|
---|
[c8cf261] | 75 | return EOK;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static errno_t disp_window_destroy(void *arg, sysarg_t wnd_id)
|
---|
| 79 | {
|
---|
[b3c185b6] | 80 | ds_client_t *client = (ds_client_t *) arg;
|
---|
[6af4b4f] | 81 | ds_window_t *wnd;
|
---|
| 82 |
|
---|
[b3c185b6] | 83 | wnd = ds_client_find_window(client, wnd_id);
|
---|
[6af4b4f] | 84 | if (wnd == NULL)
|
---|
| 85 | return ENOENT;
|
---|
| 86 |
|
---|
[c8cf261] | 87 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_destroy()");
|
---|
[b3c185b6] | 88 | ds_client_remove_window(wnd);
|
---|
[648e2ac] | 89 | ds_window_destroy(wnd);
|
---|
[6af4b4f] | 90 | return EOK;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[b3c185b6] | 93 | static errno_t disp_get_event(void *arg, sysarg_t *wnd_id,
|
---|
| 94 | display_wnd_ev_t *event)
|
---|
| 95 | {
|
---|
| 96 | ds_client_t *client = (ds_client_t *) arg;
|
---|
| 97 | ds_window_t *wnd;
|
---|
| 98 | errno_t rc;
|
---|
| 99 |
|
---|
| 100 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_get_event()");
|
---|
| 101 |
|
---|
| 102 | rc = ds_client_get_event(client, &wnd, event);
|
---|
| 103 | if (rc != EOK)
|
---|
| 104 | return rc;
|
---|
| 105 |
|
---|
| 106 | *wnd_id = wnd->id;
|
---|
| 107 | return EOK;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[6af4b4f] | 110 | /** Create display.
|
---|
| 111 | *
|
---|
[159776f] | 112 | * @param gc Graphics context for displaying output
|
---|
[6af4b4f] | 113 | * @param rdisp Place to store pointer to new display.
|
---|
| 114 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 115 | */
|
---|
[159776f] | 116 | errno_t ds_display_create(gfx_context_t *gc, ds_display_t **rdisp)
|
---|
[6af4b4f] | 117 | {
|
---|
| 118 | ds_display_t *disp;
|
---|
| 119 |
|
---|
| 120 | disp = calloc(1, sizeof(ds_display_t));
|
---|
| 121 | if (disp == NULL)
|
---|
| 122 | return ENOMEM;
|
---|
| 123 |
|
---|
[b3c185b6] | 124 | list_initialize(&disp->clients);
|
---|
[159776f] | 125 | disp->gc = gc;
|
---|
[b3c185b6] | 126 | disp->next_wnd_id = 1;
|
---|
[6af4b4f] | 127 | *rdisp = disp;
|
---|
| 128 | return EOK;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /** Destroy display.
|
---|
| 132 | *
|
---|
| 133 | * @param disp Display
|
---|
| 134 | */
|
---|
| 135 | void ds_display_destroy(ds_display_t *disp)
|
---|
| 136 | {
|
---|
[b3c185b6] | 137 | assert(list_empty(&disp->clients));
|
---|
[6af4b4f] | 138 | free(disp);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[b3c185b6] | 141 | /** Add client to display.
|
---|
[6af4b4f] | 142 | *
|
---|
| 143 | * @param disp Display
|
---|
[b3c185b6] | 144 | * @param client client
|
---|
[6af4b4f] | 145 | */
|
---|
[b3c185b6] | 146 | void ds_display_add_client(ds_display_t *disp, ds_client_t *client)
|
---|
[6af4b4f] | 147 | {
|
---|
[b3c185b6] | 148 | assert(client->display == NULL);
|
---|
| 149 | assert(!link_used(&client->lclients));
|
---|
[6af4b4f] | 150 |
|
---|
[b3c185b6] | 151 | client->display = disp;
|
---|
| 152 | list_append(&client->lclients, &disp->clients);
|
---|
[c8cf261] | 153 | }
|
---|
| 154 |
|
---|
[b3c185b6] | 155 | /** Remove client from display.
|
---|
[6af4b4f] | 156 | *
|
---|
[b3c185b6] | 157 | * @param client client
|
---|
[6af4b4f] | 158 | */
|
---|
[b3c185b6] | 159 | void ds_display_remove_client(ds_client_t *client)
|
---|
[6af4b4f] | 160 | {
|
---|
[b3c185b6] | 161 | list_remove(&client->lclients);
|
---|
| 162 | client->display = NULL;
|
---|
[6af4b4f] | 163 | }
|
---|
| 164 |
|
---|
[b3c185b6] | 165 | /** Get first client in display.
|
---|
[6af4b4f] | 166 | *
|
---|
| 167 | * @param disp Display
|
---|
[b3c185b6] | 168 | * @return First client or @c NULL if there is none
|
---|
[6af4b4f] | 169 | */
|
---|
[b3c185b6] | 170 | ds_client_t *ds_display_first_client(ds_display_t *disp)
|
---|
[6af4b4f] | 171 | {
|
---|
[b3c185b6] | 172 | link_t *link = list_first(&disp->clients);
|
---|
[6af4b4f] | 173 |
|
---|
[b3c185b6] | 174 | if (link == NULL)
|
---|
| 175 | return NULL;
|
---|
[6af4b4f] | 176 |
|
---|
[b3c185b6] | 177 | return list_get_instance(link, ds_client_t, lclients);
|
---|
[6af4b4f] | 178 | }
|
---|
| 179 |
|
---|
[b3c185b6] | 180 | /** Get next client in display.
|
---|
[6af4b4f] | 181 | *
|
---|
[b3c185b6] | 182 | * @param client Current client
|
---|
| 183 | * @return Next client or @c NULL if there is none
|
---|
[6af4b4f] | 184 | */
|
---|
[b3c185b6] | 185 | ds_client_t *ds_display_next_client(ds_client_t *client)
|
---|
[6af4b4f] | 186 | {
|
---|
[b3c185b6] | 187 | link_t *link = list_next(&client->lclients, &client->display->clients);
|
---|
[6af4b4f] | 188 |
|
---|
| 189 | if (link == NULL)
|
---|
| 190 | return NULL;
|
---|
| 191 |
|
---|
[b3c185b6] | 192 | return list_get_instance(link, ds_client_t, lclients);
|
---|
[6af4b4f] | 193 | }
|
---|
| 194 |
|
---|
[b3c185b6] | 195 | /** Find window in all clients by ID.
|
---|
[6af4b4f] | 196 | *
|
---|
[b3c185b6] | 197 | * XXX This is just a hack needed to match GC connection to a window,
|
---|
| 198 | * as we don't have a good safe way to pass the GC endpoint to our client
|
---|
| 199 | * on demand.
|
---|
| 200 | *
|
---|
| 201 | * @param display Display
|
---|
| 202 | * @param id Window ID
|
---|
[6af4b4f] | 203 | */
|
---|
[b3c185b6] | 204 | #include <stdio.h>
|
---|
| 205 | ds_window_t *ds_display_find_window(ds_display_t *display, ds_wnd_id_t id)
|
---|
[6af4b4f] | 206 | {
|
---|
[b3c185b6] | 207 | ds_client_t *client;
|
---|
| 208 | ds_window_t *wnd;
|
---|
[6af4b4f] | 209 |
|
---|
[b3c185b6] | 210 | printf("ds_display_find_window: id=0x%lx\n", id);
|
---|
| 211 |
|
---|
| 212 | client = ds_display_first_client(display);
|
---|
| 213 | while (client != NULL) {
|
---|
| 214 | printf("ds_display_find_window: client=%p\n", client);
|
---|
| 215 | wnd = ds_client_find_window(client, id);
|
---|
| 216 | if (wnd != NULL) {
|
---|
| 217 | printf("ds_display_find_window: found wnd=%p id=0x%lx\n",
|
---|
| 218 | wnd, wnd->id);
|
---|
| 219 | return wnd;
|
---|
| 220 | }
|
---|
| 221 | client = ds_display_next_client(client);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | printf("ds_display_find_window: not found\n");
|
---|
| 225 | return NULL;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | errno_t ds_display_post_kbd_event(ds_display_t *display, kbd_event_t *event)
|
---|
| 229 | {
|
---|
| 230 | ds_client_t *client;
|
---|
| 231 | ds_window_t *wnd;
|
---|
| 232 |
|
---|
| 233 | // XXX Correctly determine destination window
|
---|
| 234 |
|
---|
| 235 | client = ds_display_first_client(display);
|
---|
| 236 | if (client == NULL)
|
---|
| 237 | return EOK;
|
---|
| 238 |
|
---|
| 239 | wnd = ds_client_first_window(client);
|
---|
| 240 | if (wnd == NULL)
|
---|
| 241 | return EOK;
|
---|
[6af4b4f] | 242 |
|
---|
[b3c185b6] | 243 | return ds_client_post_kbd_event(client, wnd, event);
|
---|
[6af4b4f] | 244 | }
|
---|
| 245 |
|
---|
[c8cf261] | 246 | /** @}
|
---|
| 247 | */
|
---|