blob: 9c11f6fca9f977af60eecb2f6b0b1fbe31a41208 [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{
84 if (surface->configure == ivi_shell_surface_configure)
85 return surface->configure_private;
86
87 return NULL;
88}
89
Pekka Paalanen1f821932016-03-15 16:57:51 +020090void
91shell_surface_send_configure(struct weston_surface *surface,
92 int32_t width, int32_t height)
93{
94 struct ivi_shell_surface *shsurf;
95
96 shsurf = get_ivi_shell_surface(surface);
97 assert(shsurf);
98 if (!shsurf)
99 return;
100
101 if (shsurf->resource)
102 ivi_surface_send_configure(shsurf->resource, width, height);
103}
104
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900105static void
106ivi_shell_surface_configure(struct weston_surface *surface,
107 int32_t sx, int32_t sy)
108{
109 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900110
111 if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
112 return;
113
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900114 if (ivisurf->width != surface->width ||
115 ivisurf->height != surface->height) {
116 ivisurf->width = surface->width;
117 ivisurf->height = surface->height;
118
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900119 ivi_layout_surface_configure(ivisurf->layout_surface,
120 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900121 }
122}
123
Pekka Paalanen13281f62016-03-07 16:19:37 +0200124static int
125ivi_shell_surface_get_label(struct weston_surface *surface,
126 char *buf,
127 size_t len)
128{
129 struct ivi_shell_surface *shell_surf = get_ivi_shell_surface(surface);
130
131 if (!shell_surf)
132 return snprintf(buf, len, "unidentified window in ivi-shell");
133
134 return snprintf(buf, len, "ivi-surface %#x", shell_surf->id_surface);
135}
136
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900137static void
138layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
139{
140 assert(ivisurf->layout_surface != NULL);
141
142 ivi_layout_surface_destroy(ivisurf->layout_surface);
143 ivisurf->layout_surface = NULL;
144
145 ivisurf->surface->configure = NULL;
146 ivisurf->surface->configure_private = NULL;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200147 weston_surface_set_label_func(ivisurf->surface, NULL);
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900148 ivisurf->surface = NULL;
149
150 // destroy weston_surface destroy signal.
151 wl_list_remove(&ivisurf->surface_destroy_listener.link);
152}
153
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900154/*
155 * The ivi_surface wl_resource destructor.
156 *
157 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
158 */
159static void
160shell_destroy_shell_surface(struct wl_resource *resource)
161{
162 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
Nobuhiko Tanibata68098422015-06-22 15:31:08 +0900163
164 if (ivisurf == NULL)
165 return;
166
167 assert(ivisurf->resource == resource);
168
169 if (ivisurf->layout_surface != NULL)
170 layout_surface_cleanup(ivisurf);
171
172 wl_list_remove(&ivisurf->link);
173
174 free(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900175}
176
177/* Gets called through the weston_surface destroy signal. */
178static void
179shell_handle_surface_destroy(struct wl_listener *listener, void *data)
180{
181 struct ivi_shell_surface *ivisurf =
182 container_of(listener, struct ivi_shell_surface,
183 surface_destroy_listener);
184
185 assert(ivisurf != NULL);
186
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900187 if (ivisurf->layout_surface != NULL)
188 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900189}
190
191/* Gets called, when a client sends ivi_surface.destroy request. */
192static void
193surface_destroy(struct wl_client *client, struct wl_resource *resource)
194{
195 /*
196 * Fires the wl_resource destroy signal, and then calls
197 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
198 */
199 wl_resource_destroy(resource);
200}
201
202static const struct ivi_surface_interface surface_implementation = {
203 surface_destroy,
204};
205
206/**
207 * Request handler for ivi_application.surface_create.
208 *
209 * Creates an ivi_surface protocol object associated with the given wl_surface.
210 * ivi_surface protocol object is represented by struct ivi_shell_surface.
211 *
212 * \param client The client.
213 * \param resource The ivi_application protocol object.
214 * \param id_surface The IVI surface ID.
215 * \param surface_resource The wl_surface protocol object.
216 * \param id The protocol object id for the new ivi_surface protocol object.
217 *
218 * The wl_surface is given the ivi_surface role and associated with a unique
219 * IVI ID which is used to identify the surface in a controller
220 * (window manager).
221 */
222static void
223application_surface_create(struct wl_client *client,
224 struct wl_resource *resource,
225 uint32_t id_surface,
226 struct wl_resource *surface_resource,
227 uint32_t id)
228{
229 struct ivi_shell *shell = wl_resource_get_user_data(resource);
230 struct ivi_shell_surface *ivisurf;
231 struct ivi_layout_surface *layout_surface;
232 struct weston_surface *weston_surface =
233 wl_resource_get_user_data(surface_resource);
234 struct wl_resource *res;
235
236 if (weston_surface_set_role(weston_surface, "ivi_surface",
237 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
238 return;
239
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900240 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900241
242 /* check if id_ivi is already used for wl_surface*/
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300243 if (layout_surface == NULL) {
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900244 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;
Pekka Paalanen1f821932016-03-15 16:57:51 +0200266
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900267 /*
268 * The following code relies on wl_surface destruction triggering
269 * immediateweston_surface destruction
270 */
271 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
272 wl_signal_add(&weston_surface->destroy_signal,
273 &ivisurf->surface_destroy_listener);
274
275 ivisurf->surface = weston_surface;
276
277 weston_surface->configure = ivi_shell_surface_configure;
278 weston_surface->configure_private = ivisurf;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200279 weston_surface_set_label_func(weston_surface,
280 ivi_shell_surface_get_label);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900281
282 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
283 if (res == NULL) {
284 wl_client_post_no_memory(client);
285 return;
286 }
287
288 ivisurf->resource = res;
289
290 wl_resource_set_implementation(res, &surface_implementation,
291 ivisurf, shell_destroy_shell_surface);
292}
293
294static const struct ivi_application_interface application_implementation = {
295 application_surface_create
296};
297
298/*
299 * Handle wl_registry.bind of ivi_application global singleton.
300 */
301static void
302bind_ivi_application(struct wl_client *client,
303 void *data, uint32_t version, uint32_t id)
304{
305 struct ivi_shell *shell = data;
306 struct wl_resource *resource;
307
308 resource = wl_resource_create(client, &ivi_application_interface,
309 1, id);
310
311 wl_resource_set_implementation(resource,
312 &application_implementation,
313 shell, NULL);
314}
315
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900316struct weston_view *
317get_default_view(struct weston_surface *surface)
318{
319 struct ivi_shell_surface *shsurf;
320 struct weston_view *view;
321
322 if (!surface || wl_list_empty(&surface->views))
323 return NULL;
324
325 shsurf = get_ivi_shell_surface(surface);
326 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900327 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900328 if (view)
329 return view;
330 }
331
332 wl_list_for_each(view, &surface->views, surface_link) {
333 if (weston_view_is_mapped(view))
334 return view;
335 }
336
337 return container_of(surface->views.next,
338 struct weston_view, surface_link);
339}
340
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900341/*
342 * Called through the compositor's destroy signal.
343 */
344static void
345shell_destroy(struct wl_listener *listener, void *data)
346{
347 struct ivi_shell *shell =
348 container_of(listener, struct ivi_shell, destroy_listener);
349 struct ivi_shell_surface *ivisurf, *next;
350
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300351 text_backend_destroy(shell->text_backend);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900352 input_panel_destroy(shell);
353
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900354 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
355 wl_list_remove(&ivisurf->link);
356 free(ivisurf);
357 }
358
359 free(shell);
360}
361
362static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500363terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
364 uint32_t key, void *data)
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200365{
366 struct weston_compositor *compositor = data;
367
368 wl_display_terminate(compositor->wl_display);
369}
370
371static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200372init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
373 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900374{
375 shell->compositor = compositor;
376
377 wl_list_init(&shell->ivi_surface_list);
378
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900379 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200380
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200381 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200382 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200383
384 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
385 MODIFIER_CTRL | MODIFIER_ALT,
386 terminate_binding,
387 compositor);
388 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900389}
390
391static int
392ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200393 struct weston_compositor *compositor,
394 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900395{
396 int result = 0;
397 struct weston_config *config = compositor->config;
398 struct weston_config_section *section;
399
Pekka Paalanen8a005252015-03-20 15:53:53 +0200400 const struct weston_option ivi_shell_options[] = {
401 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
402 };
403
404 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
405 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900406
407 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
408
Pekka Paalanen8a005252015-03-20 15:53:53 +0200409 if (!dest->ivi_module &&
410 weston_config_section_get_string(section, "ivi-module",
411 &dest->ivi_module, NULL) < 0) {
412 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900413 result = -1;
414 }
415
Pekka Paalanene35b2232015-02-19 17:08:44 +0200416 weston_config_section_get_bool(section, "developermode",
417 &dest->developermode, 0);
418
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900419 return result;
420}
421
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900422static void
423activate_binding(struct weston_seat *seat,
424 struct weston_view *focus_view)
425{
426 struct weston_surface *focus = focus_view->surface;
427 struct weston_surface *main_surface =
428 weston_surface_get_main_surface(focus);
429
430 if (get_ivi_shell_surface(main_surface) == NULL)
431 return;
432
433 weston_surface_activate(focus, seat);
434}
435
436static void
437click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
438 uint32_t button, void *data)
439{
440 if (pointer->grab != &pointer->default_grab)
441 return;
442 if (pointer->focus == NULL)
443 return;
444
445 activate_binding(pointer->seat, pointer->focus);
446}
447
448static void
449touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
450 void *data)
451{
452 if (touch->grab != &touch->default_grab)
453 return;
454 if (touch->focus == NULL)
455 return;
456
457 activate_binding(touch->seat, touch->focus);
458}
459
460static void
461shell_add_bindings(struct weston_compositor *compositor,
462 struct ivi_shell *shell)
463{
464 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
465 click_to_activate_binding,
466 shell);
467 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
468 click_to_activate_binding,
469 shell);
470 weston_compositor_add_touch_binding(compositor, 0,
471 touch_to_activate_binding,
472 shell);
473}
474
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900475/*
476 * Initialization of ivi-shell.
477 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900478WL_EXPORT int
479module_init(struct weston_compositor *compositor,
480 int *argc, char *argv[])
481{
482 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900483 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200484 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900485
486 shell = zalloc(sizeof *shell);
487 if (shell == NULL)
488 return -1;
489
Pekka Paalanen8a005252015-03-20 15:53:53 +0200490 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200491 return -1;
492
493 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900494
495 shell->destroy_listener.notify = shell_destroy;
496 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
497
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900498 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200499 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900500
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300501 shell->text_backend = text_backend_init(compositor);
502 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +0000503 goto out_settings;
504
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900505 if (wl_global_create(compositor->wl_display,
506 &ivi_application_interface, 1,
507 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200508 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900509
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900510 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900511 shell_add_bindings(compositor, shell);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900512
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900513 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200514 if (load_controller_modules(compositor, setting.ivi_module,
515 argc, argv) < 0)
516 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900517
Pekka Paalanene35b2232015-02-19 17:08:44 +0200518 retval = 0;
519
520out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900521 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200522
523 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900524}