blob: 59ffe0c2e610149f5f1f2e40d43a71b7c5435a63 [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"
Pekka Paalanen32ca7912016-03-15 17:21:00 +020047#include "ivi-layout-shell.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;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090066};
67
68struct ivi_shell_setting
69{
70 char *ivi_module;
Pekka Paalanene35b2232015-02-19 17:08:44 +020071 int developermode;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090072};
73
74/*
75 * Implementation of ivi_surface
76 */
77
78static void
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090079ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
80
81static struct ivi_shell_surface *
82get_ivi_shell_surface(struct weston_surface *surface)
83{
Pekka Paalanen94cb06a2016-03-16 14:54:12 +020084 struct ivi_shell_surface *shsurf;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090085
Pekka Paalanen94cb06a2016-03-16 14:54:12 +020086 if (surface->configure != ivi_shell_surface_configure)
87 return NULL;
88
89 shsurf = surface->configure_private;
90 assert(shsurf);
91 assert(shsurf->surface == surface);
92
93 return shsurf;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090094}
95
Pekka Paalaneneaa43fc2016-04-12 16:06:58 +030096struct ivi_layout_surface *
97shell_get_ivi_layout_surface(struct weston_surface *surface)
98{
99 struct ivi_shell_surface *shsurf;
100
101 shsurf = get_ivi_shell_surface(surface);
102 if (!shsurf)
103 return NULL;
104
105 return shsurf->layout_surface;
106}
107
Pekka Paalanen1f821932016-03-15 16:57:51 +0200108void
109shell_surface_send_configure(struct weston_surface *surface,
110 int32_t width, int32_t height)
111{
112 struct ivi_shell_surface *shsurf;
113
114 shsurf = get_ivi_shell_surface(surface);
115 assert(shsurf);
116 if (!shsurf)
117 return;
118
119 if (shsurf->resource)
120 ivi_surface_send_configure(shsurf->resource, width, height);
121}
122
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900123static void
124ivi_shell_surface_configure(struct weston_surface *surface,
125 int32_t sx, int32_t sy)
126{
127 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900128
Pekka Paalanenfd45f602016-03-16 15:05:03 +0200129 assert(ivisurf);
130 if (!ivisurf)
131 return;
132
133 if (surface->width == 0 || surface->height == 0)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900134 return;
135
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900136 if (ivisurf->width != surface->width ||
137 ivisurf->height != surface->height) {
138 ivisurf->width = surface->width;
139 ivisurf->height = surface->height;
140
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900141 ivi_layout_surface_configure(ivisurf->layout_surface,
142 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900143 }
144}
145
Pekka Paalanen13281f62016-03-07 16:19:37 +0200146static int
147ivi_shell_surface_get_label(struct weston_surface *surface,
148 char *buf,
149 size_t len)
150{
151 struct ivi_shell_surface *shell_surf = get_ivi_shell_surface(surface);
152
153 if (!shell_surf)
154 return snprintf(buf, len, "unidentified window in ivi-shell");
155
156 return snprintf(buf, len, "ivi-surface %#x", shell_surf->id_surface);
157}
158
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900159static void
160layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
161{
162 assert(ivisurf->layout_surface != NULL);
163
164 ivi_layout_surface_destroy(ivisurf->layout_surface);
165 ivisurf->layout_surface = NULL;
166
167 ivisurf->surface->configure = NULL;
168 ivisurf->surface->configure_private = NULL;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200169 weston_surface_set_label_func(ivisurf->surface, NULL);
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900170 ivisurf->surface = NULL;
171
172 // destroy weston_surface destroy signal.
173 wl_list_remove(&ivisurf->surface_destroy_listener.link);
174}
175
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900176/*
177 * The ivi_surface wl_resource destructor.
178 *
179 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
180 */
181static void
182shell_destroy_shell_surface(struct wl_resource *resource)
183{
184 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
Nobuhiko Tanibata68098422015-06-22 15:31:08 +0900185
186 if (ivisurf == NULL)
187 return;
188
189 assert(ivisurf->resource == resource);
190
191 if (ivisurf->layout_surface != NULL)
192 layout_surface_cleanup(ivisurf);
193
194 wl_list_remove(&ivisurf->link);
195
196 free(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900197}
198
199/* Gets called through the weston_surface destroy signal. */
200static void
201shell_handle_surface_destroy(struct wl_listener *listener, void *data)
202{
203 struct ivi_shell_surface *ivisurf =
204 container_of(listener, struct ivi_shell_surface,
205 surface_destroy_listener);
206
207 assert(ivisurf != NULL);
208
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900209 if (ivisurf->layout_surface != NULL)
210 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900211}
212
213/* Gets called, when a client sends ivi_surface.destroy request. */
214static void
215surface_destroy(struct wl_client *client, struct wl_resource *resource)
216{
217 /*
218 * Fires the wl_resource destroy signal, and then calls
219 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
220 */
221 wl_resource_destroy(resource);
222}
223
224static const struct ivi_surface_interface surface_implementation = {
225 surface_destroy,
226};
227
228/**
229 * Request handler for ivi_application.surface_create.
230 *
231 * Creates an ivi_surface protocol object associated with the given wl_surface.
232 * ivi_surface protocol object is represented by struct ivi_shell_surface.
233 *
234 * \param client The client.
235 * \param resource The ivi_application protocol object.
236 * \param id_surface The IVI surface ID.
237 * \param surface_resource The wl_surface protocol object.
238 * \param id The protocol object id for the new ivi_surface protocol object.
239 *
240 * The wl_surface is given the ivi_surface role and associated with a unique
241 * IVI ID which is used to identify the surface in a controller
242 * (window manager).
243 */
244static void
245application_surface_create(struct wl_client *client,
246 struct wl_resource *resource,
247 uint32_t id_surface,
248 struct wl_resource *surface_resource,
249 uint32_t id)
250{
251 struct ivi_shell *shell = wl_resource_get_user_data(resource);
252 struct ivi_shell_surface *ivisurf;
253 struct ivi_layout_surface *layout_surface;
254 struct weston_surface *weston_surface =
255 wl_resource_get_user_data(surface_resource);
256 struct wl_resource *res;
257
258 if (weston_surface_set_role(weston_surface, "ivi_surface",
259 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
260 return;
261
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900262 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900263
264 /* check if id_ivi is already used for wl_surface*/
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300265 if (layout_surface == NULL) {
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900266 wl_resource_post_error(resource,
267 IVI_APPLICATION_ERROR_IVI_ID,
268 "surface_id is already assigned "
269 "by another app");
270 return;
271 }
272
273 ivisurf = zalloc(sizeof *ivisurf);
274 if (ivisurf == NULL) {
275 wl_resource_post_no_memory(resource);
276 return;
277 }
278
279 wl_list_init(&ivisurf->link);
280 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
281
282 ivisurf->shell = shell;
283 ivisurf->id_surface = id_surface;
284
285 ivisurf->width = 0;
286 ivisurf->height = 0;
287 ivisurf->layout_surface = layout_surface;
Pekka Paalanen1f821932016-03-15 16:57:51 +0200288
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900289 /*
290 * The following code relies on wl_surface destruction triggering
291 * immediateweston_surface destruction
292 */
293 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
294 wl_signal_add(&weston_surface->destroy_signal,
295 &ivisurf->surface_destroy_listener);
296
297 ivisurf->surface = weston_surface;
298
299 weston_surface->configure = ivi_shell_surface_configure;
300 weston_surface->configure_private = ivisurf;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200301 weston_surface_set_label_func(weston_surface,
302 ivi_shell_surface_get_label);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900303
304 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
305 if (res == NULL) {
306 wl_client_post_no_memory(client);
307 return;
308 }
309
310 ivisurf->resource = res;
311
312 wl_resource_set_implementation(res, &surface_implementation,
313 ivisurf, shell_destroy_shell_surface);
314}
315
316static const struct ivi_application_interface application_implementation = {
317 application_surface_create
318};
319
320/*
321 * Handle wl_registry.bind of ivi_application global singleton.
322 */
323static void
324bind_ivi_application(struct wl_client *client,
325 void *data, uint32_t version, uint32_t id)
326{
327 struct ivi_shell *shell = data;
328 struct wl_resource *resource;
329
330 resource = wl_resource_create(client, &ivi_application_interface,
331 1, id);
332
333 wl_resource_set_implementation(resource,
334 &application_implementation,
335 shell, NULL);
336}
337
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900338struct weston_view *
339get_default_view(struct weston_surface *surface)
340{
341 struct ivi_shell_surface *shsurf;
342 struct weston_view *view;
343
344 if (!surface || wl_list_empty(&surface->views))
345 return NULL;
346
347 shsurf = get_ivi_shell_surface(surface);
348 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900349 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900350 if (view)
351 return view;
352 }
353
354 wl_list_for_each(view, &surface->views, surface_link) {
355 if (weston_view_is_mapped(view))
356 return view;
357 }
358
359 return container_of(surface->views.next,
360 struct weston_view, surface_link);
361}
362
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900363/*
364 * Called through the compositor's destroy signal.
365 */
366static void
367shell_destroy(struct wl_listener *listener, void *data)
368{
369 struct ivi_shell *shell =
370 container_of(listener, struct ivi_shell, destroy_listener);
371 struct ivi_shell_surface *ivisurf, *next;
372
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300373 text_backend_destroy(shell->text_backend);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900374 input_panel_destroy(shell);
375
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900376 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
377 wl_list_remove(&ivisurf->link);
378 free(ivisurf);
379 }
380
381 free(shell);
382}
383
384static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500385terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
386 uint32_t key, void *data)
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200387{
388 struct weston_compositor *compositor = data;
389
390 wl_display_terminate(compositor->wl_display);
391}
392
393static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200394init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
395 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900396{
397 shell->compositor = compositor;
398
399 wl_list_init(&shell->ivi_surface_list);
400
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900401 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200402
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200403 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200404 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200405
406 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
407 MODIFIER_CTRL | MODIFIER_ALT,
408 terminate_binding,
409 compositor);
410 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900411}
412
413static int
414ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200415 struct weston_compositor *compositor,
416 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900417{
418 int result = 0;
419 struct weston_config *config = compositor->config;
420 struct weston_config_section *section;
421
Pekka Paalanen8a005252015-03-20 15:53:53 +0200422 const struct weston_option ivi_shell_options[] = {
423 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
424 };
425
426 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
427 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900428
429 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
430
Pekka Paalanen8a005252015-03-20 15:53:53 +0200431 if (!dest->ivi_module &&
432 weston_config_section_get_string(section, "ivi-module",
433 &dest->ivi_module, NULL) < 0) {
434 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900435 result = -1;
436 }
437
Pekka Paalanene35b2232015-02-19 17:08:44 +0200438 weston_config_section_get_bool(section, "developermode",
439 &dest->developermode, 0);
440
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900441 return result;
442}
443
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900444static void
445activate_binding(struct weston_seat *seat,
446 struct weston_view *focus_view)
447{
448 struct weston_surface *focus = focus_view->surface;
449 struct weston_surface *main_surface =
450 weston_surface_get_main_surface(focus);
451
452 if (get_ivi_shell_surface(main_surface) == NULL)
453 return;
454
455 weston_surface_activate(focus, seat);
456}
457
458static void
459click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
460 uint32_t button, void *data)
461{
462 if (pointer->grab != &pointer->default_grab)
463 return;
464 if (pointer->focus == NULL)
465 return;
466
467 activate_binding(pointer->seat, pointer->focus);
468}
469
470static void
471touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
472 void *data)
473{
474 if (touch->grab != &touch->default_grab)
475 return;
476 if (touch->focus == NULL)
477 return;
478
479 activate_binding(touch->seat, touch->focus);
480}
481
482static void
483shell_add_bindings(struct weston_compositor *compositor,
484 struct ivi_shell *shell)
485{
486 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
487 click_to_activate_binding,
488 shell);
489 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
490 click_to_activate_binding,
491 shell);
492 weston_compositor_add_touch_binding(compositor, 0,
493 touch_to_activate_binding,
494 shell);
495}
496
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900497/*
498 * Initialization of ivi-shell.
499 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900500WL_EXPORT int
501module_init(struct weston_compositor *compositor,
502 int *argc, char *argv[])
503{
504 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900505 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200506 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900507
508 shell = zalloc(sizeof *shell);
509 if (shell == NULL)
510 return -1;
511
Pekka Paalanen8a005252015-03-20 15:53:53 +0200512 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200513 return -1;
514
515 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900516
517 shell->destroy_listener.notify = shell_destroy;
518 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
519
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900520 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200521 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900522
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300523 shell->text_backend = text_backend_init(compositor);
524 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +0000525 goto out_settings;
526
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900527 if (wl_global_create(compositor->wl_display,
528 &ivi_application_interface, 1,
529 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200530 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900531
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900532 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900533 shell_add_bindings(compositor, shell);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900534
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900535 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200536 if (load_controller_modules(compositor, setting.ivi_module,
537 argc, argv) < 0)
538 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900539
Pekka Paalanene35b2232015-02-19 17:08:44 +0200540 retval = 0;
541
542out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900543 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200544
545 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900546}