blob: 1b6467c257cac07f11d21b69cb79d2cb63a85291 [file] [log] [blame]
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +09001/*
2 * Copyright (C) 2013 DENSO CORPORATION
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23/*
24 * ivi-shell supports a type of shell for In-Vehicle Infotainment system.
25 * In-Vehicle Infotainment system traditionally manages surfaces with global
26 * identification. A protocol, ivi_application, supports such a feature
27 * by implementing a request, ivi_application::surface_creation defined in
28 * ivi_application.xml.
29 *
30 * The ivi-shell explicitly loads a module to add business logic like how to
31 * layout surfaces by using internal ivi-layout APIs.
32 */
33#include "config.h"
34
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090035#include <string.h>
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090036#include <dlfcn.h>
37#include <limits.h>
38#include <assert.h>
39
40#include "ivi-shell.h"
41#include "ivi-application-server-protocol.h"
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090042#include "ivi-layout-export.h"
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090043#include "ivi-layout-private.h"
44
45#include "../shared/os-compatibility.h"
46
47/* Representation of ivi_surface protocol object. */
48struct ivi_shell_surface
49{
50 struct wl_resource* resource;
51 struct ivi_shell *shell;
52 struct ivi_layout_surface *layout_surface;
53
54 struct weston_surface *surface;
55 struct wl_listener surface_destroy_listener;
56
57 uint32_t id_surface;
58
59 int32_t width;
60 int32_t height;
61
62 struct wl_list link;
63
64 struct wl_listener configured_listener;
65};
66
67struct ivi_shell_setting
68{
69 char *ivi_module;
70};
71
72/*
73 * Implementation of ivi_surface
74 */
75
76static void
77surface_configure_notify(struct wl_listener *listener, void *data)
78{
79 struct ivi_layout_surface *layout_surf =
80 (struct ivi_layout_surface *)data;
81
82 struct ivi_shell_surface *shell_surf =
83 container_of(listener,
84 struct ivi_shell_surface,
85 configured_listener);
86
87 int32_t dest_width = 0;
88 int32_t dest_height = 0;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090089
90 ivi_layout_surface_get_dimension(layout_surf,
91 &dest_width, &dest_height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090092
93 if (shell_surf->resource)
94 ivi_surface_send_configure(shell_surf->resource,
95 dest_width, dest_height);
96}
97
98static void
99ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
100
101static struct ivi_shell_surface *
102get_ivi_shell_surface(struct weston_surface *surface)
103{
104 if (surface->configure == ivi_shell_surface_configure)
105 return surface->configure_private;
106
107 return NULL;
108}
109
110static void
111ivi_shell_surface_configure(struct weston_surface *surface,
112 int32_t sx, int32_t sy)
113{
114 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
115 struct weston_view *view;
116 float from_x;
117 float from_y;
118 float to_x;
119 float to_y;
120
121 if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
122 return;
123
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900124 view = ivi_layout_get_weston_view(ivisurf->layout_surface);
125
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900126 if (view == NULL)
127 return;
128
129 if (ivisurf->width != surface->width ||
130 ivisurf->height != surface->height) {
131 ivisurf->width = surface->width;
132 ivisurf->height = surface->height;
133
134 weston_view_to_global_float(view, 0, 0, &from_x, &from_y);
135 weston_view_to_global_float(view, sx, sy, &to_x, &to_y);
136
137 weston_view_set_position(view,
138 view->geometry.x + to_x - from_x,
139 view->geometry.y + to_y - from_y);
140 weston_view_update_transform(view);
141
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900142 ivi_layout_surface_configure(ivisurf->layout_surface,
143 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900144 }
145}
146
147/*
148 * The ivi_surface wl_resource destructor.
149 *
150 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
151 */
152static void
153shell_destroy_shell_surface(struct wl_resource *resource)
154{
155 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
156 if (ivisurf != NULL) {
157 ivisurf->resource = NULL;
158 }
159}
160
161/* Gets called through the weston_surface destroy signal. */
162static void
163shell_handle_surface_destroy(struct wl_listener *listener, void *data)
164{
165 struct ivi_shell_surface *ivisurf =
166 container_of(listener, struct ivi_shell_surface,
167 surface_destroy_listener);
168
169 assert(ivisurf != NULL);
170
171 if (ivisurf->surface!=NULL) {
172 ivisurf->surface->configure = NULL;
173 ivisurf->surface->configure_private = NULL;
174 ivisurf->surface = NULL;
175 }
176
177 wl_list_remove(&ivisurf->surface_destroy_listener.link);
178 wl_list_remove(&ivisurf->link);
179
180 if (ivisurf->resource != NULL) {
181 wl_resource_set_user_data(ivisurf->resource, NULL);
182 ivisurf->resource = NULL;
183 }
184 free(ivisurf);
185
186}
187
188/* Gets called, when a client sends ivi_surface.destroy request. */
189static void
190surface_destroy(struct wl_client *client, struct wl_resource *resource)
191{
192 /*
193 * Fires the wl_resource destroy signal, and then calls
194 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
195 */
196 wl_resource_destroy(resource);
197}
198
199static const struct ivi_surface_interface surface_implementation = {
200 surface_destroy,
201};
202
203/**
204 * Request handler for ivi_application.surface_create.
205 *
206 * Creates an ivi_surface protocol object associated with the given wl_surface.
207 * ivi_surface protocol object is represented by struct ivi_shell_surface.
208 *
209 * \param client The client.
210 * \param resource The ivi_application protocol object.
211 * \param id_surface The IVI surface ID.
212 * \param surface_resource The wl_surface protocol object.
213 * \param id The protocol object id for the new ivi_surface protocol object.
214 *
215 * The wl_surface is given the ivi_surface role and associated with a unique
216 * IVI ID which is used to identify the surface in a controller
217 * (window manager).
218 */
219static void
220application_surface_create(struct wl_client *client,
221 struct wl_resource *resource,
222 uint32_t id_surface,
223 struct wl_resource *surface_resource,
224 uint32_t id)
225{
226 struct ivi_shell *shell = wl_resource_get_user_data(resource);
227 struct ivi_shell_surface *ivisurf;
228 struct ivi_layout_surface *layout_surface;
229 struct weston_surface *weston_surface =
230 wl_resource_get_user_data(surface_resource);
231 struct wl_resource *res;
232
233 if (weston_surface_set_role(weston_surface, "ivi_surface",
234 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
235 return;
236
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900237 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900238
239 /* check if id_ivi is already used for wl_surface*/
240 if (layout_surface == NULL){
241 wl_resource_post_error(resource,
242 IVI_APPLICATION_ERROR_IVI_ID,
243 "surface_id is already assigned "
244 "by another app");
245 return;
246 }
247
248 ivisurf = zalloc(sizeof *ivisurf);
249 if (ivisurf == NULL) {
250 wl_resource_post_no_memory(resource);
251 return;
252 }
253
254 wl_list_init(&ivisurf->link);
255 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
256
257 ivisurf->shell = shell;
258 ivisurf->id_surface = id_surface;
259
260 ivisurf->width = 0;
261 ivisurf->height = 0;
262 ivisurf->layout_surface = layout_surface;
263 ivisurf->configured_listener.notify = surface_configure_notify;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900264 ivi_layout_surface_add_configured_listener(layout_surface,
265 &ivisurf->configured_listener);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900266 /*
267 * The following code relies on wl_surface destruction triggering
268 * immediateweston_surface destruction
269 */
270 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
271 wl_signal_add(&weston_surface->destroy_signal,
272 &ivisurf->surface_destroy_listener);
273
274 ivisurf->surface = weston_surface;
275
276 weston_surface->configure = ivi_shell_surface_configure;
277 weston_surface->configure_private = ivisurf;
278
279 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
280 if (res == NULL) {
281 wl_client_post_no_memory(client);
282 return;
283 }
284
285 ivisurf->resource = res;
286
287 wl_resource_set_implementation(res, &surface_implementation,
288 ivisurf, shell_destroy_shell_surface);
289}
290
291static const struct ivi_application_interface application_implementation = {
292 application_surface_create
293};
294
295/*
296 * Handle wl_registry.bind of ivi_application global singleton.
297 */
298static void
299bind_ivi_application(struct wl_client *client,
300 void *data, uint32_t version, uint32_t id)
301{
302 struct ivi_shell *shell = data;
303 struct wl_resource *resource;
304
305 resource = wl_resource_create(client, &ivi_application_interface,
306 1, id);
307
308 wl_resource_set_implementation(resource,
309 &application_implementation,
310 shell, NULL);
311}
312
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900313struct weston_view *
314get_default_view(struct weston_surface *surface)
315{
316 struct ivi_shell_surface *shsurf;
317 struct weston_view *view;
318
319 if (!surface || wl_list_empty(&surface->views))
320 return NULL;
321
322 shsurf = get_ivi_shell_surface(surface);
323 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900324 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900325 if (view)
326 return view;
327 }
328
329 wl_list_for_each(view, &surface->views, surface_link) {
330 if (weston_view_is_mapped(view))
331 return view;
332 }
333
334 return container_of(surface->views.next,
335 struct weston_view, surface_link);
336}
337
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900338/*
339 * Called through the compositor's destroy signal.
340 */
341static void
342shell_destroy(struct wl_listener *listener, void *data)
343{
344 struct ivi_shell *shell =
345 container_of(listener, struct ivi_shell, destroy_listener);
346 struct ivi_shell_surface *ivisurf, *next;
347
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900348 input_panel_destroy(shell);
349
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900350 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
351 wl_list_remove(&ivisurf->link);
352 free(ivisurf);
353 }
354
355 free(shell);
356}
357
358static void
359init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell)
360{
361 shell->compositor = compositor;
362
363 wl_list_init(&shell->ivi_surface_list);
364
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900365 weston_layer_init(&shell->input_panel_layer, NULL);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900366}
367
368static int
369ivi_shell_setting_create(struct ivi_shell_setting *dest,
370 struct weston_compositor *compositor)
371{
372 int result = 0;
373 struct weston_config *config = compositor->config;
374 struct weston_config_section *section;
375
376 if (NULL == dest)
377 return -1;
378
379 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
380
Ondřej Majerech4bb99292014-12-06 02:49:18 +0100381 if (weston_config_section_get_string(section, "ivi-module",
382 &dest->ivi_module, NULL) != 0) {
383 weston_log("ivi-shell: No ivi-module set in config\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900384 result = -1;
385 }
386
387 return result;
388}
389
390/*
391 * Initialization of ivi-shell.
392 */
393static int
394ivi_load_modules(struct weston_compositor *compositor, const char *modules,
395 int *argc, char *argv[])
396{
397 const char *p, *end;
398 char buffer[256];
399 int (*module_init)(struct weston_compositor *compositor,
400 int *argc, char *argv[]);
401
402 if (modules == NULL)
403 return 0;
404
405 p = modules;
406 while (*p) {
407 end = strchrnul(p, ',');
408 snprintf(buffer, sizeof buffer, "%.*s", (int)(end - p), p);
409
410 module_init = weston_load_module(buffer, "module_init");
411 if (module_init)
412 module_init(compositor, argc, argv);
413
414 p = end;
415 while (*p == ',')
416 p++;
417 }
418
419 return 0;
420}
421
422WL_EXPORT int
423module_init(struct weston_compositor *compositor,
424 int *argc, char *argv[])
425{
426 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900427 struct ivi_shell_setting setting = { };
428
429 shell = zalloc(sizeof *shell);
430 if (shell == NULL)
431 return -1;
432
433 init_ivi_shell(compositor, shell);
434
435 shell->destroy_listener.notify = shell_destroy;
436 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
437
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900438 if (input_panel_setup(shell) < 0)
439 return -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900440
441 if (wl_global_create(compositor->wl_display,
442 &ivi_application_interface, 1,
443 shell, bind_ivi_application) == NULL)
444 return -1;
445
446 if (ivi_shell_setting_create(&setting, compositor) != 0)
447 return -1;
448
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900449 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900450
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900451
452 /* Call module_init of ivi-modules which are defined in weston.ini */
453 if (ivi_load_modules(compositor, setting.ivi_module, argc, argv) < 0) {
454 free(setting.ivi_module);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900455 return -1;
456 }
457
458 free(setting.ivi_module);
459 return 0;
460}