source: mainline/uspace/srv/hid/display/display.c@ 648e2ac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 648e2ac was 648e2ac, checked in by Jiri Svoboda <jiri@…>, 6 years ago

ds_window_delete should be ds_window_destroy

  • Property mode set to 100644
File size: 6.1 KB
Line 
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>
38#include <gfx/context.h>
39#include <io/log.h>
40#include <stdlib.h>
41#include "client.h"
42#include "window.h"
43#include "display.h"
44
45static errno_t disp_window_create(void *, sysarg_t *);
46static errno_t disp_window_destroy(void *, sysarg_t);
47static errno_t disp_get_event(void *, sysarg_t *, display_wnd_ev_t *);
48
49display_ops_t display_srv_ops = {
50 .window_create = disp_window_create,
51 .window_destroy = disp_window_destroy,
52 .get_event = disp_get_event
53};
54
55static errno_t disp_window_create(void *arg, sysarg_t *rwnd_id)
56{
57 errno_t rc;
58 ds_client_t *client = (ds_client_t *) arg;
59 ds_window_t *wnd;
60
61 log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create()");
62
63 rc = ds_window_create(client, &wnd);
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);
70
71 wnd->dpos.x = ((wnd->id - 1) & 1) * 400;
72 wnd->dpos.y = ((wnd->id - 1) & 2) / 2 * 300;
73
74 *rwnd_id = wnd->id;
75 return EOK;
76}
77
78static errno_t disp_window_destroy(void *arg, sysarg_t wnd_id)
79{
80 ds_client_t *client = (ds_client_t *) arg;
81 ds_window_t *wnd;
82
83 wnd = ds_client_find_window(client, wnd_id);
84 if (wnd == NULL)
85 return ENOENT;
86
87 log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_destroy()");
88 ds_client_remove_window(wnd);
89 ds_window_destroy(wnd);
90 return EOK;
91}
92
93static 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
110/** Create display.
111 *
112 * @param gc Graphics context for displaying output
113 * @param rdisp Place to store pointer to new display.
114 * @return EOK on success, ENOMEM if out of memory
115 */
116errno_t ds_display_create(gfx_context_t *gc, ds_display_t **rdisp)
117{
118 ds_display_t *disp;
119
120 disp = calloc(1, sizeof(ds_display_t));
121 if (disp == NULL)
122 return ENOMEM;
123
124 list_initialize(&disp->clients);
125 disp->gc = gc;
126 disp->next_wnd_id = 1;
127 *rdisp = disp;
128 return EOK;
129}
130
131/** Destroy display.
132 *
133 * @param disp Display
134 */
135void ds_display_destroy(ds_display_t *disp)
136{
137 assert(list_empty(&disp->clients));
138 free(disp);
139}
140
141/** Add client to display.
142 *
143 * @param disp Display
144 * @param client client
145 */
146void ds_display_add_client(ds_display_t *disp, ds_client_t *client)
147{
148 assert(client->display == NULL);
149 assert(!link_used(&client->lclients));
150
151 client->display = disp;
152 list_append(&client->lclients, &disp->clients);
153}
154
155/** Remove client from display.
156 *
157 * @param client client
158 */
159void ds_display_remove_client(ds_client_t *client)
160{
161 list_remove(&client->lclients);
162 client->display = NULL;
163}
164
165/** Get first client in display.
166 *
167 * @param disp Display
168 * @return First client or @c NULL if there is none
169 */
170ds_client_t *ds_display_first_client(ds_display_t *disp)
171{
172 link_t *link = list_first(&disp->clients);
173
174 if (link == NULL)
175 return NULL;
176
177 return list_get_instance(link, ds_client_t, lclients);
178}
179
180/** Get next client in display.
181 *
182 * @param client Current client
183 * @return Next client or @c NULL if there is none
184 */
185ds_client_t *ds_display_next_client(ds_client_t *client)
186{
187 link_t *link = list_next(&client->lclients, &client->display->clients);
188
189 if (link == NULL)
190 return NULL;
191
192 return list_get_instance(link, ds_client_t, lclients);
193}
194
195/** Find window in all clients by ID.
196 *
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
203 */
204#include <stdio.h>
205ds_window_t *ds_display_find_window(ds_display_t *display, ds_wnd_id_t id)
206{
207 ds_client_t *client;
208 ds_window_t *wnd;
209
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
228errno_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;
242
243 return ds_client_post_kbd_event(client, wnd, event);
244}
245
246/** @}
247 */
Note: See TracBrowser for help on using the repository browser.