blob: c1af78c271f67141d32b42220f45090d2bbf673a [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"
Giulio Camuffod52f3b72016-06-02 21:48:11 +030049#include "weston.h"
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090050
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090051/* 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;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090067};
68
69struct ivi_shell_setting
70{
71 char *ivi_module;
Pekka Paalanene35b2232015-02-19 17:08:44 +020072 int developermode;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090073};
74
75/*
76 * Implementation of ivi_surface
77 */
78
79static void
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090080ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
81
82static struct ivi_shell_surface *
83get_ivi_shell_surface(struct weston_surface *surface)
84{
Pekka Paalanen94cb06a2016-03-16 14:54:12 +020085 struct ivi_shell_surface *shsurf;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090086
Pekka Paalanen94cb06a2016-03-16 14:54:12 +020087 if (surface->configure != ivi_shell_surface_configure)
88 return NULL;
89
90 shsurf = surface->configure_private;
91 assert(shsurf);
92 assert(shsurf->surface == surface);
93
94 return shsurf;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090095}
96
Pekka Paalaneneaa43fc2016-04-12 16:06:58 +030097struct ivi_layout_surface *
98shell_get_ivi_layout_surface(struct weston_surface *surface)
99{
100 struct ivi_shell_surface *shsurf;
101
102 shsurf = get_ivi_shell_surface(surface);
103 if (!shsurf)
104 return NULL;
105
106 return shsurf->layout_surface;
107}
108
Pekka Paalanen1f821932016-03-15 16:57:51 +0200109void
110shell_surface_send_configure(struct weston_surface *surface,
111 int32_t width, int32_t height)
112{
113 struct ivi_shell_surface *shsurf;
114
115 shsurf = get_ivi_shell_surface(surface);
116 assert(shsurf);
117 if (!shsurf)
118 return;
119
120 if (shsurf->resource)
121 ivi_surface_send_configure(shsurf->resource, width, height);
122}
123
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900124static void
125ivi_shell_surface_configure(struct weston_surface *surface,
126 int32_t sx, int32_t sy)
127{
128 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900129
Pekka Paalanenfd45f602016-03-16 15:05:03 +0200130 assert(ivisurf);
131 if (!ivisurf)
132 return;
133
134 if (surface->width == 0 || surface->height == 0)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900135 return;
136
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900137 if (ivisurf->width != surface->width ||
138 ivisurf->height != surface->height) {
139 ivisurf->width = surface->width;
140 ivisurf->height = surface->height;
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
Pekka Paalanen13281f62016-03-07 16:19:37 +0200147static int
148ivi_shell_surface_get_label(struct weston_surface *surface,
149 char *buf,
150 size_t len)
151{
152 struct ivi_shell_surface *shell_surf = get_ivi_shell_surface(surface);
153
154 if (!shell_surf)
155 return snprintf(buf, len, "unidentified window in ivi-shell");
156
157 return snprintf(buf, len, "ivi-surface %#x", shell_surf->id_surface);
158}
159
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900160static void
161layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
162{
163 assert(ivisurf->layout_surface != NULL);
164
165 ivi_layout_surface_destroy(ivisurf->layout_surface);
166 ivisurf->layout_surface = NULL;
167
168 ivisurf->surface->configure = NULL;
169 ivisurf->surface->configure_private = NULL;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200170 weston_surface_set_label_func(ivisurf->surface, NULL);
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900171 ivisurf->surface = NULL;
172
173 // destroy weston_surface destroy signal.
174 wl_list_remove(&ivisurf->surface_destroy_listener.link);
175}
176
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900177/*
178 * The ivi_surface wl_resource destructor.
179 *
180 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
181 */
182static void
183shell_destroy_shell_surface(struct wl_resource *resource)
184{
185 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
Nobuhiko Tanibata68098422015-06-22 15:31:08 +0900186
187 if (ivisurf == NULL)
188 return;
189
190 assert(ivisurf->resource == resource);
191
192 if (ivisurf->layout_surface != NULL)
193 layout_surface_cleanup(ivisurf);
194
195 wl_list_remove(&ivisurf->link);
196
197 free(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900198}
199
200/* Gets called through the weston_surface destroy signal. */
201static void
202shell_handle_surface_destroy(struct wl_listener *listener, void *data)
203{
204 struct ivi_shell_surface *ivisurf =
205 container_of(listener, struct ivi_shell_surface,
206 surface_destroy_listener);
207
208 assert(ivisurf != NULL);
209
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900210 if (ivisurf->layout_surface != NULL)
211 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900212}
213
214/* Gets called, when a client sends ivi_surface.destroy request. */
215static void
216surface_destroy(struct wl_client *client, struct wl_resource *resource)
217{
218 /*
219 * Fires the wl_resource destroy signal, and then calls
220 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
221 */
222 wl_resource_destroy(resource);
223}
224
225static const struct ivi_surface_interface surface_implementation = {
226 surface_destroy,
227};
228
229/**
230 * Request handler for ivi_application.surface_create.
231 *
232 * Creates an ivi_surface protocol object associated with the given wl_surface.
233 * ivi_surface protocol object is represented by struct ivi_shell_surface.
234 *
235 * \param client The client.
236 * \param resource The ivi_application protocol object.
237 * \param id_surface The IVI surface ID.
238 * \param surface_resource The wl_surface protocol object.
239 * \param id The protocol object id for the new ivi_surface protocol object.
240 *
241 * The wl_surface is given the ivi_surface role and associated with a unique
242 * IVI ID which is used to identify the surface in a controller
243 * (window manager).
244 */
245static void
246application_surface_create(struct wl_client *client,
247 struct wl_resource *resource,
248 uint32_t id_surface,
249 struct wl_resource *surface_resource,
250 uint32_t id)
251{
252 struct ivi_shell *shell = wl_resource_get_user_data(resource);
253 struct ivi_shell_surface *ivisurf;
254 struct ivi_layout_surface *layout_surface;
255 struct weston_surface *weston_surface =
256 wl_resource_get_user_data(surface_resource);
257 struct wl_resource *res;
258
259 if (weston_surface_set_role(weston_surface, "ivi_surface",
260 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
261 return;
262
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900263 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900264
265 /* check if id_ivi is already used for wl_surface*/
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300266 if (layout_surface == NULL) {
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900267 wl_resource_post_error(resource,
268 IVI_APPLICATION_ERROR_IVI_ID,
269 "surface_id is already assigned "
270 "by another app");
271 return;
272 }
273
274 ivisurf = zalloc(sizeof *ivisurf);
275 if (ivisurf == NULL) {
276 wl_resource_post_no_memory(resource);
277 return;
278 }
279
280 wl_list_init(&ivisurf->link);
281 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
282
283 ivisurf->shell = shell;
284 ivisurf->id_surface = id_surface;
285
286 ivisurf->width = 0;
287 ivisurf->height = 0;
288 ivisurf->layout_surface = layout_surface;
Pekka Paalanen1f821932016-03-15 16:57:51 +0200289
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900290 /*
291 * The following code relies on wl_surface destruction triggering
292 * immediateweston_surface destruction
293 */
294 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
295 wl_signal_add(&weston_surface->destroy_signal,
296 &ivisurf->surface_destroy_listener);
297
298 ivisurf->surface = weston_surface;
299
300 weston_surface->configure = ivi_shell_surface_configure;
301 weston_surface->configure_private = ivisurf;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200302 weston_surface_set_label_func(weston_surface,
303 ivi_shell_surface_get_label);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900304
305 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
306 if (res == NULL) {
307 wl_client_post_no_memory(client);
308 return;
309 }
310
311 ivisurf->resource = res;
312
313 wl_resource_set_implementation(res, &surface_implementation,
314 ivisurf, shell_destroy_shell_surface);
315}
316
317static const struct ivi_application_interface application_implementation = {
318 application_surface_create
319};
320
321/*
322 * Handle wl_registry.bind of ivi_application global singleton.
323 */
324static void
325bind_ivi_application(struct wl_client *client,
326 void *data, uint32_t version, uint32_t id)
327{
328 struct ivi_shell *shell = data;
329 struct wl_resource *resource;
330
331 resource = wl_resource_create(client, &ivi_application_interface,
332 1, id);
333
334 wl_resource_set_implementation(resource,
335 &application_implementation,
336 shell, NULL);
337}
338
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900339struct weston_view *
340get_default_view(struct weston_surface *surface)
341{
342 struct ivi_shell_surface *shsurf;
343 struct weston_view *view;
344
345 if (!surface || wl_list_empty(&surface->views))
346 return NULL;
347
348 shsurf = get_ivi_shell_surface(surface);
349 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900350 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900351 if (view)
352 return view;
353 }
354
355 wl_list_for_each(view, &surface->views, surface_link) {
356 if (weston_view_is_mapped(view))
357 return view;
358 }
359
360 return container_of(surface->views.next,
361 struct weston_view, surface_link);
362}
363
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900364/*
365 * Called through the compositor's destroy signal.
366 */
367static void
368shell_destroy(struct wl_listener *listener, void *data)
369{
370 struct ivi_shell *shell =
371 container_of(listener, struct ivi_shell, destroy_listener);
372 struct ivi_shell_surface *ivisurf, *next;
373
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300374 text_backend_destroy(shell->text_backend);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900375 input_panel_destroy(shell);
376
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900377 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
378 wl_list_remove(&ivisurf->link);
379 free(ivisurf);
380 }
381
382 free(shell);
383}
384
385static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500386terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
387 uint32_t key, void *data)
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200388{
389 struct weston_compositor *compositor = data;
390
391 wl_display_terminate(compositor->wl_display);
392}
393
394static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200395init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
396 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900397{
398 shell->compositor = compositor;
399
400 wl_list_init(&shell->ivi_surface_list);
401
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900402 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200403
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200404 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200405 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200406
407 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
408 MODIFIER_CTRL | MODIFIER_ALT,
409 terminate_binding,
410 compositor);
411 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900412}
413
414static int
415ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200416 struct weston_compositor *compositor,
417 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900418{
419 int result = 0;
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300420 struct weston_config *config = wet_get_config(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900421 struct weston_config_section *section;
422
Pekka Paalanen8a005252015-03-20 15:53:53 +0200423 const struct weston_option ivi_shell_options[] = {
424 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
425 };
426
427 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
428 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900429
430 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
431
Pekka Paalanen8a005252015-03-20 15:53:53 +0200432 if (!dest->ivi_module &&
433 weston_config_section_get_string(section, "ivi-module",
434 &dest->ivi_module, NULL) < 0) {
435 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900436 result = -1;
437 }
438
Pekka Paalanene35b2232015-02-19 17:08:44 +0200439 weston_config_section_get_bool(section, "developermode",
440 &dest->developermode, 0);
441
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900442 return result;
443}
444
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900445static void
446activate_binding(struct weston_seat *seat,
447 struct weston_view *focus_view)
448{
449 struct weston_surface *focus = focus_view->surface;
450 struct weston_surface *main_surface =
451 weston_surface_get_main_surface(focus);
452
453 if (get_ivi_shell_surface(main_surface) == NULL)
454 return;
455
456 weston_surface_activate(focus, seat);
457}
458
459static void
460click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
461 uint32_t button, void *data)
462{
463 if (pointer->grab != &pointer->default_grab)
464 return;
465 if (pointer->focus == NULL)
466 return;
467
468 activate_binding(pointer->seat, pointer->focus);
469}
470
471static void
472touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
473 void *data)
474{
475 if (touch->grab != &touch->default_grab)
476 return;
477 if (touch->focus == NULL)
478 return;
479
480 activate_binding(touch->seat, touch->focus);
481}
482
483static void
484shell_add_bindings(struct weston_compositor *compositor,
485 struct ivi_shell *shell)
486{
487 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
488 click_to_activate_binding,
489 shell);
490 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
491 click_to_activate_binding,
492 shell);
493 weston_compositor_add_touch_binding(compositor, 0,
494 touch_to_activate_binding,
495 shell);
496}
497
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900498/*
499 * Initialization of ivi-shell.
500 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900501WL_EXPORT int
502module_init(struct weston_compositor *compositor,
503 int *argc, char *argv[])
504{
505 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900506 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200507 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900508
509 shell = zalloc(sizeof *shell);
510 if (shell == NULL)
511 return -1;
512
Pekka Paalanen8a005252015-03-20 15:53:53 +0200513 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200514 return -1;
515
516 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900517
518 shell->destroy_listener.notify = shell_destroy;
519 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
520
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900521 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200522 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900523
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300524 shell->text_backend = text_backend_init(compositor);
525 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +0000526 goto out_settings;
527
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900528 if (wl_global_create(compositor->wl_display,
529 &ivi_application_interface, 1,
530 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200531 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900532
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900533 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900534 shell_add_bindings(compositor, shell);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900535
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900536 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200537 if (load_controller_modules(compositor, setting.ivi_module,
538 argc, argv) < 0)
539 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900540
Pekka Paalanene35b2232015-02-19 17:08:44 +0200541 retval = 0;
542
543out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900544 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200545
546 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900547}