blob: a1473530db0ae56e8fec327c3772f71c3877dfba [file] [log] [blame]
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +09001/*
2 * Copyright (C) 2013 DENSO CORPORATION
3 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090011 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090024 */
25
26/*
27 * ivi-shell supports a type of shell for In-Vehicle Infotainment system.
28 * In-Vehicle Infotainment system traditionally manages surfaces with global
29 * identification. A protocol, ivi_application, supports such a feature
30 * by implementing a request, ivi_application::surface_creation defined in
31 * ivi_application.xml.
32 *
33 * The ivi-shell explicitly loads a module to add business logic like how to
34 * layout surfaces by using internal ivi-layout APIs.
35 */
36#include "config.h"
37
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090038#include <string.h>
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090039#include <dlfcn.h>
40#include <limits.h>
41#include <assert.h>
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +020042#include <linux/input.h>
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090043
44#include "ivi-shell.h"
45#include "ivi-application-server-protocol.h"
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090046#include "ivi-layout-export.h"
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090047#include "ivi-layout-private.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070048#include "shared/helpers.h"
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090049
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090050/* Representation of ivi_surface protocol object. */
51struct ivi_shell_surface
52{
53 struct wl_resource* resource;
54 struct ivi_shell *shell;
55 struct ivi_layout_surface *layout_surface;
56
57 struct weston_surface *surface;
58 struct wl_listener surface_destroy_listener;
59
60 uint32_t id_surface;
61
62 int32_t width;
63 int32_t height;
64
65 struct wl_list link;
66
67 struct wl_listener configured_listener;
68};
69
70struct ivi_shell_setting
71{
72 char *ivi_module;
Pekka Paalanene35b2232015-02-19 17:08:44 +020073 int developermode;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090074};
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;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090093
94 ivi_layout_surface_get_dimension(layout_surf,
95 &dest_width, &dest_height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090096
97 if (shell_surf->resource)
98 ivi_surface_send_configure(shell_surf->resource,
99 dest_width, dest_height);
100}
101
102static void
103ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
104
105static struct ivi_shell_surface *
106get_ivi_shell_surface(struct weston_surface *surface)
107{
108 if (surface->configure == ivi_shell_surface_configure)
109 return surface->configure_private;
110
111 return NULL;
112}
113
114static void
115ivi_shell_surface_configure(struct weston_surface *surface,
116 int32_t sx, int32_t sy)
117{
118 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900119
120 if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
121 return;
122
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900123 if (ivisurf->width != surface->width ||
124 ivisurf->height != surface->height) {
125 ivisurf->width = surface->width;
126 ivisurf->height = surface->height;
127
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900128 ivi_layout_surface_configure(ivisurf->layout_surface,
129 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900130 }
131}
132
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900133static void
134layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
135{
136 assert(ivisurf->layout_surface != NULL);
137
138 ivi_layout_surface_destroy(ivisurf->layout_surface);
139 ivisurf->layout_surface = NULL;
140
141 ivisurf->surface->configure = NULL;
142 ivisurf->surface->configure_private = NULL;
143 ivisurf->surface = NULL;
144
145 // destroy weston_surface destroy signal.
146 wl_list_remove(&ivisurf->surface_destroy_listener.link);
147}
148
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900149/*
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
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900173 if (ivisurf->layout_surface != NULL)
174 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900175
176 if (ivisurf->resource != NULL) {
177 wl_resource_set_user_data(ivisurf->resource, NULL);
178 ivisurf->resource = NULL;
179 }
180 free(ivisurf);
181
182}
183
184/* Gets called, when a client sends ivi_surface.destroy request. */
185static void
186surface_destroy(struct wl_client *client, struct wl_resource *resource)
187{
188 /*
189 * Fires the wl_resource destroy signal, and then calls
190 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
191 */
192 wl_resource_destroy(resource);
193}
194
195static const struct ivi_surface_interface surface_implementation = {
196 surface_destroy,
197};
198
199/**
200 * Request handler for ivi_application.surface_create.
201 *
202 * Creates an ivi_surface protocol object associated with the given wl_surface.
203 * ivi_surface protocol object is represented by struct ivi_shell_surface.
204 *
205 * \param client The client.
206 * \param resource The ivi_application protocol object.
207 * \param id_surface The IVI surface ID.
208 * \param surface_resource The wl_surface protocol object.
209 * \param id The protocol object id for the new ivi_surface protocol object.
210 *
211 * The wl_surface is given the ivi_surface role and associated with a unique
212 * IVI ID which is used to identify the surface in a controller
213 * (window manager).
214 */
215static void
216application_surface_create(struct wl_client *client,
217 struct wl_resource *resource,
218 uint32_t id_surface,
219 struct wl_resource *surface_resource,
220 uint32_t id)
221{
222 struct ivi_shell *shell = wl_resource_get_user_data(resource);
223 struct ivi_shell_surface *ivisurf;
224 struct ivi_layout_surface *layout_surface;
225 struct weston_surface *weston_surface =
226 wl_resource_get_user_data(surface_resource);
227 struct wl_resource *res;
228
229 if (weston_surface_set_role(weston_surface, "ivi_surface",
230 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
231 return;
232
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900233 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900234
235 /* check if id_ivi is already used for wl_surface*/
236 if (layout_surface == NULL){
237 wl_resource_post_error(resource,
238 IVI_APPLICATION_ERROR_IVI_ID,
239 "surface_id is already assigned "
240 "by another app");
241 return;
242 }
243
244 ivisurf = zalloc(sizeof *ivisurf);
245 if (ivisurf == NULL) {
246 wl_resource_post_no_memory(resource);
247 return;
248 }
249
250 wl_list_init(&ivisurf->link);
251 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
252
253 ivisurf->shell = shell;
254 ivisurf->id_surface = id_surface;
255
256 ivisurf->width = 0;
257 ivisurf->height = 0;
258 ivisurf->layout_surface = layout_surface;
259 ivisurf->configured_listener.notify = surface_configure_notify;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900260 ivi_layout_surface_add_configured_listener(layout_surface,
261 &ivisurf->configured_listener);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900262 /*
263 * The following code relies on wl_surface destruction triggering
264 * immediateweston_surface destruction
265 */
266 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
267 wl_signal_add(&weston_surface->destroy_signal,
268 &ivisurf->surface_destroy_listener);
269
270 ivisurf->surface = weston_surface;
271
272 weston_surface->configure = ivi_shell_surface_configure;
273 weston_surface->configure_private = ivisurf;
274
275 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
276 if (res == NULL) {
277 wl_client_post_no_memory(client);
278 return;
279 }
280
281 ivisurf->resource = res;
282
283 wl_resource_set_implementation(res, &surface_implementation,
284 ivisurf, shell_destroy_shell_surface);
285}
286
287static const struct ivi_application_interface application_implementation = {
288 application_surface_create
289};
290
291/*
292 * Handle wl_registry.bind of ivi_application global singleton.
293 */
294static void
295bind_ivi_application(struct wl_client *client,
296 void *data, uint32_t version, uint32_t id)
297{
298 struct ivi_shell *shell = data;
299 struct wl_resource *resource;
300
301 resource = wl_resource_create(client, &ivi_application_interface,
302 1, id);
303
304 wl_resource_set_implementation(resource,
305 &application_implementation,
306 shell, NULL);
307}
308
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900309struct weston_view *
310get_default_view(struct weston_surface *surface)
311{
312 struct ivi_shell_surface *shsurf;
313 struct weston_view *view;
314
315 if (!surface || wl_list_empty(&surface->views))
316 return NULL;
317
318 shsurf = get_ivi_shell_surface(surface);
319 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900320 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900321 if (view)
322 return view;
323 }
324
325 wl_list_for_each(view, &surface->views, surface_link) {
326 if (weston_view_is_mapped(view))
327 return view;
328 }
329
330 return container_of(surface->views.next,
331 struct weston_view, surface_link);
332}
333
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900334/*
335 * Called through the compositor's destroy signal.
336 */
337static void
338shell_destroy(struct wl_listener *listener, void *data)
339{
340 struct ivi_shell *shell =
341 container_of(listener, struct ivi_shell, destroy_listener);
342 struct ivi_shell_surface *ivisurf, *next;
343
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900344 input_panel_destroy(shell);
345
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900346 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
347 wl_list_remove(&ivisurf->link);
348 free(ivisurf);
349 }
350
351 free(shell);
352}
353
354static void
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200355terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
356 void *data)
357{
358 struct weston_compositor *compositor = data;
359
360 wl_display_terminate(compositor->wl_display);
361}
362
363static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200364init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
365 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900366{
367 shell->compositor = compositor;
368
369 wl_list_init(&shell->ivi_surface_list);
370
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900371 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200372
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200373 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200374 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200375
376 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
377 MODIFIER_CTRL | MODIFIER_ALT,
378 terminate_binding,
379 compositor);
380 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900381}
382
383static int
384ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200385 struct weston_compositor *compositor,
386 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900387{
388 int result = 0;
389 struct weston_config *config = compositor->config;
390 struct weston_config_section *section;
391
Pekka Paalanen8a005252015-03-20 15:53:53 +0200392 const struct weston_option ivi_shell_options[] = {
393 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
394 };
395
396 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
397 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900398
399 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
400
Pekka Paalanen8a005252015-03-20 15:53:53 +0200401 if (!dest->ivi_module &&
402 weston_config_section_get_string(section, "ivi-module",
403 &dest->ivi_module, NULL) < 0) {
404 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900405 result = -1;
406 }
407
Pekka Paalanene35b2232015-02-19 17:08:44 +0200408 weston_config_section_get_bool(section, "developermode",
409 &dest->developermode, 0);
410
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900411 return result;
412}
413
414/*
415 * Initialization of ivi-shell.
416 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900417WL_EXPORT int
418module_init(struct weston_compositor *compositor,
419 int *argc, char *argv[])
420{
421 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900422 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200423 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900424
425 shell = zalloc(sizeof *shell);
426 if (shell == NULL)
427 return -1;
428
Pekka Paalanen8a005252015-03-20 15:53:53 +0200429 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200430 return -1;
431
432 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900433
434 shell->destroy_listener.notify = shell_destroy;
435 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
436
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900437 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200438 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900439
Murray Calavera9a51cd72015-06-10 21:16:02 +0000440 if (text_backend_init(compositor) < 0)
441 goto out_settings;
442
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900443 if (wl_global_create(compositor->wl_display,
444 &ivi_application_interface, 1,
445 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200446 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900447
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900448 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900449
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900450 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200451 if (load_controller_modules(compositor, setting.ivi_module,
452 argc, argv) < 0)
453 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900454
Pekka Paalanene35b2232015-02-19 17:08:44 +0200455 retval = 0;
456
457out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900458 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200459
460 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900461}