blob: e8cddae22a5018008938d15ec5bd864d97d2918a [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"
42#include "ivi-layout-private.h"
43
44#include "../shared/os-compatibility.h"
45
46/* Representation of ivi_surface protocol object. */
47struct ivi_shell_surface
48{
49 struct wl_resource* resource;
50 struct ivi_shell *shell;
51 struct ivi_layout_surface *layout_surface;
52
53 struct weston_surface *surface;
54 struct wl_listener surface_destroy_listener;
55
56 uint32_t id_surface;
57
58 int32_t width;
59 int32_t height;
60
61 struct wl_list link;
62
63 struct wl_listener configured_listener;
64};
65
66struct ivi_shell_setting
67{
68 char *ivi_module;
69};
70
71/*
72 * Implementation of ivi_surface
73 */
74
75static void
76surface_configure_notify(struct wl_listener *listener, void *data)
77{
78 struct ivi_layout_surface *layout_surf =
79 (struct ivi_layout_surface *)data;
80
81 struct ivi_shell_surface *shell_surf =
82 container_of(listener,
83 struct ivi_shell_surface,
84 configured_listener);
85
86 int32_t dest_width = 0;
87 int32_t dest_height = 0;
88 shell_surf->shell->ivi_layout->get_surface_dimension(layout_surf,
89 &dest_width, &dest_height);
90
91 if (shell_surf->resource)
92 ivi_surface_send_configure(shell_surf->resource,
93 dest_width, dest_height);
94}
95
96static void
97ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
98
99static struct ivi_shell_surface *
100get_ivi_shell_surface(struct weston_surface *surface)
101{
102 if (surface->configure == ivi_shell_surface_configure)
103 return surface->configure_private;
104
105 return NULL;
106}
107
108static void
109ivi_shell_surface_configure(struct weston_surface *surface,
110 int32_t sx, int32_t sy)
111{
112 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
113 struct weston_view *view;
114 float from_x;
115 float from_y;
116 float to_x;
117 float to_y;
118
119 if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
120 return;
121
122 view = ivisurf->shell->ivi_layout->get_weston_view(ivisurf->layout_surface);
123 if (view == NULL)
124 return;
125
126 if (ivisurf->width != surface->width ||
127 ivisurf->height != surface->height) {
128 ivisurf->width = surface->width;
129 ivisurf->height = surface->height;
130
131 weston_view_to_global_float(view, 0, 0, &from_x, &from_y);
132 weston_view_to_global_float(view, sx, sy, &to_x, &to_y);
133
134 weston_view_set_position(view,
135 view->geometry.x + to_x - from_x,
136 view->geometry.y + to_y - from_y);
137 weston_view_update_transform(view);
138
139 ivisurf->shell->ivi_layout->surface_configure(ivisurf->layout_surface,
140 surface->width, surface->height);
141 }
142}
143
144/*
145 * The ivi_surface wl_resource destructor.
146 *
147 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
148 */
149static void
150shell_destroy_shell_surface(struct wl_resource *resource)
151{
152 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
153 if (ivisurf != NULL) {
154 ivisurf->resource = NULL;
155 }
156}
157
158/* Gets called through the weston_surface destroy signal. */
159static void
160shell_handle_surface_destroy(struct wl_listener *listener, void *data)
161{
162 struct ivi_shell_surface *ivisurf =
163 container_of(listener, struct ivi_shell_surface,
164 surface_destroy_listener);
165
166 assert(ivisurf != NULL);
167
168 if (ivisurf->surface!=NULL) {
169 ivisurf->surface->configure = NULL;
170 ivisurf->surface->configure_private = NULL;
171 ivisurf->surface = NULL;
172 }
173
174 wl_list_remove(&ivisurf->surface_destroy_listener.link);
175 wl_list_remove(&ivisurf->link);
176
177 if (ivisurf->resource != NULL) {
178 wl_resource_set_user_data(ivisurf->resource, NULL);
179 ivisurf->resource = NULL;
180 }
181 free(ivisurf);
182
183}
184
185/* Gets called, when a client sends ivi_surface.destroy request. */
186static void
187surface_destroy(struct wl_client *client, struct wl_resource *resource)
188{
189 /*
190 * Fires the wl_resource destroy signal, and then calls
191 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
192 */
193 wl_resource_destroy(resource);
194}
195
196static const struct ivi_surface_interface surface_implementation = {
197 surface_destroy,
198};
199
200/**
201 * Request handler for ivi_application.surface_create.
202 *
203 * Creates an ivi_surface protocol object associated with the given wl_surface.
204 * ivi_surface protocol object is represented by struct ivi_shell_surface.
205 *
206 * \param client The client.
207 * \param resource The ivi_application protocol object.
208 * \param id_surface The IVI surface ID.
209 * \param surface_resource The wl_surface protocol object.
210 * \param id The protocol object id for the new ivi_surface protocol object.
211 *
212 * The wl_surface is given the ivi_surface role and associated with a unique
213 * IVI ID which is used to identify the surface in a controller
214 * (window manager).
215 */
216static void
217application_surface_create(struct wl_client *client,
218 struct wl_resource *resource,
219 uint32_t id_surface,
220 struct wl_resource *surface_resource,
221 uint32_t id)
222{
223 struct ivi_shell *shell = wl_resource_get_user_data(resource);
224 struct ivi_shell_surface *ivisurf;
225 struct ivi_layout_surface *layout_surface;
226 struct weston_surface *weston_surface =
227 wl_resource_get_user_data(surface_resource);
228 struct wl_resource *res;
229
230 if (weston_surface_set_role(weston_surface, "ivi_surface",
231 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
232 return;
233
234 layout_surface = shell->ivi_layout->surface_create(weston_surface,
235 id_surface);
236
237 /* check if id_ivi is already used for wl_surface*/
238 if (layout_surface == NULL){
239 wl_resource_post_error(resource,
240 IVI_APPLICATION_ERROR_IVI_ID,
241 "surface_id is already assigned "
242 "by another app");
243 return;
244 }
245
246 ivisurf = zalloc(sizeof *ivisurf);
247 if (ivisurf == NULL) {
248 wl_resource_post_no_memory(resource);
249 return;
250 }
251
252 wl_list_init(&ivisurf->link);
253 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
254
255 ivisurf->shell = shell;
256 ivisurf->id_surface = id_surface;
257
258 ivisurf->width = 0;
259 ivisurf->height = 0;
260 ivisurf->layout_surface = layout_surface;
261 ivisurf->configured_listener.notify = surface_configure_notify;
262 ivisurf->shell->ivi_layout->add_surface_configured_listener(layout_surface,
263 &ivisurf->configured_listener);
264
265 /*
266 * The following code relies on wl_surface destruction triggering
267 * immediateweston_surface destruction
268 */
269 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
270 wl_signal_add(&weston_surface->destroy_signal,
271 &ivisurf->surface_destroy_listener);
272
273 ivisurf->surface = weston_surface;
274
275 weston_surface->configure = ivi_shell_surface_configure;
276 weston_surface->configure_private = ivisurf;
277
278 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
279 if (res == NULL) {
280 wl_client_post_no_memory(client);
281 return;
282 }
283
284 ivisurf->resource = res;
285
286 wl_resource_set_implementation(res, &surface_implementation,
287 ivisurf, shell_destroy_shell_surface);
288}
289
290static const struct ivi_application_interface application_implementation = {
291 application_surface_create
292};
293
294/*
295 * Handle wl_registry.bind of ivi_application global singleton.
296 */
297static void
298bind_ivi_application(struct wl_client *client,
299 void *data, uint32_t version, uint32_t id)
300{
301 struct ivi_shell *shell = data;
302 struct wl_resource *resource;
303
304 resource = wl_resource_create(client, &ivi_application_interface,
305 1, id);
306
307 wl_resource_set_implementation(resource,
308 &application_implementation,
309 shell, NULL);
310}
311
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900312struct weston_view *
313get_default_view(struct weston_surface *surface)
314{
315 struct ivi_shell_surface *shsurf;
316 struct weston_view *view;
317
318 if (!surface || wl_list_empty(&surface->views))
319 return NULL;
320
321 shsurf = get_ivi_shell_surface(surface);
322 if (shsurf && shsurf->layout_surface) {
323 view = shsurf->shell->ivi_layout->get_weston_view(shsurf->layout_surface);
324 if (view)
325 return view;
326 }
327
328 wl_list_for_each(view, &surface->views, surface_link) {
329 if (weston_view_is_mapped(view))
330 return view;
331 }
332
333 return container_of(surface->views.next,
334 struct weston_view, surface_link);
335}
336
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900337/*
338 * Called through the compositor's destroy signal.
339 */
340static void
341shell_destroy(struct wl_listener *listener, void *data)
342{
343 struct ivi_shell *shell =
344 container_of(listener, struct ivi_shell, destroy_listener);
345 struct ivi_shell_surface *ivisurf, *next;
346
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900347 input_panel_destroy(shell);
348
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900349 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
350 wl_list_remove(&ivisurf->link);
351 free(ivisurf);
352 }
353
354 free(shell);
355}
356
357static void
358init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell)
359{
360 shell->compositor = compositor;
361
362 wl_list_init(&shell->ivi_surface_list);
363
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900364 weston_layer_init(&shell->input_panel_layer, NULL);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900365}
366
367static int
368ivi_shell_setting_create(struct ivi_shell_setting *dest,
369 struct weston_compositor *compositor)
370{
371 int result = 0;
372 struct weston_config *config = compositor->config;
373 struct weston_config_section *section;
374
375 if (NULL == dest)
376 return -1;
377
378 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
379
Ondřej Majerech4bb99292014-12-06 02:49:18 +0100380 if (weston_config_section_get_string(section, "ivi-module",
381 &dest->ivi_module, NULL) != 0) {
382 weston_log("ivi-shell: No ivi-module set in config\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900383 result = -1;
384 }
385
386 return result;
387}
388
389/*
390 * Initialization of ivi-shell.
391 */
392static int
393ivi_load_modules(struct weston_compositor *compositor, const char *modules,
394 int *argc, char *argv[])
395{
396 const char *p, *end;
397 char buffer[256];
398 int (*module_init)(struct weston_compositor *compositor,
399 int *argc, char *argv[]);
400
401 if (modules == NULL)
402 return 0;
403
404 p = modules;
405 while (*p) {
406 end = strchrnul(p, ',');
407 snprintf(buffer, sizeof buffer, "%.*s", (int)(end - p), p);
408
409 module_init = weston_load_module(buffer, "module_init");
410 if (module_init)
411 module_init(compositor, argc, argv);
412
413 p = end;
414 while (*p == ',')
415 p++;
416 }
417
418 return 0;
419}
420
421WL_EXPORT int
422module_init(struct weston_compositor *compositor,
423 int *argc, char *argv[])
424{
425 struct ivi_shell *shell;
426 char ivi_layout_path[PATH_MAX];
427 void *module;
428 struct ivi_shell_setting setting = { };
429
430 shell = zalloc(sizeof *shell);
431 if (shell == NULL)
432 return -1;
433
434 init_ivi_shell(compositor, shell);
435
436 shell->destroy_listener.notify = shell_destroy;
437 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
438
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900439 if (input_panel_setup(shell) < 0)
440 return -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900441
442 if (wl_global_create(compositor->wl_display,
443 &ivi_application_interface, 1,
444 shell, bind_ivi_application) == NULL)
445 return -1;
446
447 if (ivi_shell_setting_create(&setting, compositor) != 0)
448 return -1;
449
450 /*
451 * load module:ivi-layout
452 * ivi_layout_interface is referred by ivi-shell to use ivi-layout.
453 * The reason why the following code is written newly without
454 * using weston_load_module is it doesn't open library with
455 * RTLD_GLOBAL option.
456 */
457 snprintf(ivi_layout_path, sizeof ivi_layout_path,
458 "%s/%s", MODULEDIR, "ivi-layout.so");
459 module = dlopen(ivi_layout_path, RTLD_NOW | RTLD_NOLOAD);
460 if (module) {
461 weston_log("ivi-shell: Module '%s' already loaded\n",
462 ivi_layout_path);
463 dlclose(module);
464 return -1;
465 }
466
467 weston_log("ivi-shell: Loading module '%s'\n", ivi_layout_path);
468 module = dlopen(ivi_layout_path, RTLD_NOW | RTLD_GLOBAL);
469 if (!module) {
470 weston_log("ivi-shell: Failed to load module: %s\n", dlerror());
471 return -1;
472 }
473
474 shell->ivi_layout = dlsym(module,"ivi_layout_interface");
475 if (!shell->ivi_layout){
476 weston_log("ivi-shell: couldn't find ivi_layout_interface in '%s'\n", ivi_layout_path);
477 free(setting.ivi_module);
478 dlclose(module);
479 return -1;
480 }
481
482 shell->ivi_layout->init_with_compositor(compositor);
483
484 /* Call module_init of ivi-modules which are defined in weston.ini */
485 if (ivi_load_modules(compositor, setting.ivi_module, argc, argv) < 0) {
486 free(setting.ivi_module);
487 dlclose(module);
488 return -1;
489 }
490
491 free(setting.ivi_module);
492 return 0;
493}