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