blob: c502c742b0f67ff5a76321e0328c3d5f83491a30 [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 Paalanen1f821932016-03-15 16:57:51 +020096void
97shell_surface_send_configure(struct weston_surface *surface,
98 int32_t width, int32_t height)
99{
100 struct ivi_shell_surface *shsurf;
101
102 shsurf = get_ivi_shell_surface(surface);
103 assert(shsurf);
104 if (!shsurf)
105 return;
106
107 if (shsurf->resource)
108 ivi_surface_send_configure(shsurf->resource, width, height);
109}
110
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900111static void
112ivi_shell_surface_configure(struct weston_surface *surface,
113 int32_t sx, int32_t sy)
114{
115 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900116
Pekka Paalanenfd45f602016-03-16 15:05:03 +0200117 assert(ivisurf);
118 if (!ivisurf)
119 return;
120
121 if (surface->width == 0 || surface->height == 0)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900122 return;
123
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900124 if (ivisurf->width != surface->width ||
125 ivisurf->height != surface->height) {
126 ivisurf->width = surface->width;
127 ivisurf->height = surface->height;
128
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900129 ivi_layout_surface_configure(ivisurf->layout_surface,
130 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900131 }
132}
133
Pekka Paalanen13281f62016-03-07 16:19:37 +0200134static int
135ivi_shell_surface_get_label(struct weston_surface *surface,
136 char *buf,
137 size_t len)
138{
139 struct ivi_shell_surface *shell_surf = get_ivi_shell_surface(surface);
140
141 if (!shell_surf)
142 return snprintf(buf, len, "unidentified window in ivi-shell");
143
144 return snprintf(buf, len, "ivi-surface %#x", shell_surf->id_surface);
145}
146
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900147static void
148layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
149{
150 assert(ivisurf->layout_surface != NULL);
151
152 ivi_layout_surface_destroy(ivisurf->layout_surface);
153 ivisurf->layout_surface = NULL;
154
155 ivisurf->surface->configure = NULL;
156 ivisurf->surface->configure_private = NULL;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200157 weston_surface_set_label_func(ivisurf->surface, NULL);
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900158 ivisurf->surface = NULL;
159
160 // destroy weston_surface destroy signal.
161 wl_list_remove(&ivisurf->surface_destroy_listener.link);
162}
163
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900164/*
165 * The ivi_surface wl_resource destructor.
166 *
167 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
168 */
169static void
170shell_destroy_shell_surface(struct wl_resource *resource)
171{
172 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
Nobuhiko Tanibata68098422015-06-22 15:31:08 +0900173
174 if (ivisurf == NULL)
175 return;
176
177 assert(ivisurf->resource == resource);
178
179 if (ivisurf->layout_surface != NULL)
180 layout_surface_cleanup(ivisurf);
181
182 wl_list_remove(&ivisurf->link);
183
184 free(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900185}
186
187/* Gets called through the weston_surface destroy signal. */
188static void
189shell_handle_surface_destroy(struct wl_listener *listener, void *data)
190{
191 struct ivi_shell_surface *ivisurf =
192 container_of(listener, struct ivi_shell_surface,
193 surface_destroy_listener);
194
195 assert(ivisurf != NULL);
196
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900197 if (ivisurf->layout_surface != NULL)
198 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900199}
200
201/* Gets called, when a client sends ivi_surface.destroy request. */
202static void
203surface_destroy(struct wl_client *client, struct wl_resource *resource)
204{
205 /*
206 * Fires the wl_resource destroy signal, and then calls
207 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
208 */
209 wl_resource_destroy(resource);
210}
211
212static const struct ivi_surface_interface surface_implementation = {
213 surface_destroy,
214};
215
216/**
217 * Request handler for ivi_application.surface_create.
218 *
219 * Creates an ivi_surface protocol object associated with the given wl_surface.
220 * ivi_surface protocol object is represented by struct ivi_shell_surface.
221 *
222 * \param client The client.
223 * \param resource The ivi_application protocol object.
224 * \param id_surface The IVI surface ID.
225 * \param surface_resource The wl_surface protocol object.
226 * \param id The protocol object id for the new ivi_surface protocol object.
227 *
228 * The wl_surface is given the ivi_surface role and associated with a unique
229 * IVI ID which is used to identify the surface in a controller
230 * (window manager).
231 */
232static void
233application_surface_create(struct wl_client *client,
234 struct wl_resource *resource,
235 uint32_t id_surface,
236 struct wl_resource *surface_resource,
237 uint32_t id)
238{
239 struct ivi_shell *shell = wl_resource_get_user_data(resource);
240 struct ivi_shell_surface *ivisurf;
241 struct ivi_layout_surface *layout_surface;
242 struct weston_surface *weston_surface =
243 wl_resource_get_user_data(surface_resource);
244 struct wl_resource *res;
245
246 if (weston_surface_set_role(weston_surface, "ivi_surface",
247 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
248 return;
249
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900250 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900251
252 /* check if id_ivi is already used for wl_surface*/
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300253 if (layout_surface == NULL) {
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900254 wl_resource_post_error(resource,
255 IVI_APPLICATION_ERROR_IVI_ID,
256 "surface_id is already assigned "
257 "by another app");
258 return;
259 }
260
261 ivisurf = zalloc(sizeof *ivisurf);
262 if (ivisurf == NULL) {
263 wl_resource_post_no_memory(resource);
264 return;
265 }
266
267 wl_list_init(&ivisurf->link);
268 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
269
270 ivisurf->shell = shell;
271 ivisurf->id_surface = id_surface;
272
273 ivisurf->width = 0;
274 ivisurf->height = 0;
275 ivisurf->layout_surface = layout_surface;
Pekka Paalanen1f821932016-03-15 16:57:51 +0200276
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900277 /*
278 * The following code relies on wl_surface destruction triggering
279 * immediateweston_surface destruction
280 */
281 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
282 wl_signal_add(&weston_surface->destroy_signal,
283 &ivisurf->surface_destroy_listener);
284
285 ivisurf->surface = weston_surface;
286
287 weston_surface->configure = ivi_shell_surface_configure;
288 weston_surface->configure_private = ivisurf;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200289 weston_surface_set_label_func(weston_surface,
290 ivi_shell_surface_get_label);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900291
292 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
293 if (res == NULL) {
294 wl_client_post_no_memory(client);
295 return;
296 }
297
298 ivisurf->resource = res;
299
300 wl_resource_set_implementation(res, &surface_implementation,
301 ivisurf, shell_destroy_shell_surface);
302}
303
304static const struct ivi_application_interface application_implementation = {
305 application_surface_create
306};
307
308/*
309 * Handle wl_registry.bind of ivi_application global singleton.
310 */
311static void
312bind_ivi_application(struct wl_client *client,
313 void *data, uint32_t version, uint32_t id)
314{
315 struct ivi_shell *shell = data;
316 struct wl_resource *resource;
317
318 resource = wl_resource_create(client, &ivi_application_interface,
319 1, id);
320
321 wl_resource_set_implementation(resource,
322 &application_implementation,
323 shell, NULL);
324}
325
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900326struct weston_view *
327get_default_view(struct weston_surface *surface)
328{
329 struct ivi_shell_surface *shsurf;
330 struct weston_view *view;
331
332 if (!surface || wl_list_empty(&surface->views))
333 return NULL;
334
335 shsurf = get_ivi_shell_surface(surface);
336 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900337 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900338 if (view)
339 return view;
340 }
341
342 wl_list_for_each(view, &surface->views, surface_link) {
343 if (weston_view_is_mapped(view))
344 return view;
345 }
346
347 return container_of(surface->views.next,
348 struct weston_view, surface_link);
349}
350
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900351/*
352 * Called through the compositor's destroy signal.
353 */
354static void
355shell_destroy(struct wl_listener *listener, void *data)
356{
357 struct ivi_shell *shell =
358 container_of(listener, struct ivi_shell, destroy_listener);
359 struct ivi_shell_surface *ivisurf, *next;
360
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300361 text_backend_destroy(shell->text_backend);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900362 input_panel_destroy(shell);
363
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900364 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
365 wl_list_remove(&ivisurf->link);
366 free(ivisurf);
367 }
368
369 free(shell);
370}
371
372static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500373terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
374 uint32_t key, void *data)
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200375{
376 struct weston_compositor *compositor = data;
377
378 wl_display_terminate(compositor->wl_display);
379}
380
381static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200382init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
383 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900384{
385 shell->compositor = compositor;
386
387 wl_list_init(&shell->ivi_surface_list);
388
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900389 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200390
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200391 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200392 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200393
394 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
395 MODIFIER_CTRL | MODIFIER_ALT,
396 terminate_binding,
397 compositor);
398 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900399}
400
401static int
402ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200403 struct weston_compositor *compositor,
404 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900405{
406 int result = 0;
407 struct weston_config *config = compositor->config;
408 struct weston_config_section *section;
409
Pekka Paalanen8a005252015-03-20 15:53:53 +0200410 const struct weston_option ivi_shell_options[] = {
411 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
412 };
413
414 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
415 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900416
417 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
418
Pekka Paalanen8a005252015-03-20 15:53:53 +0200419 if (!dest->ivi_module &&
420 weston_config_section_get_string(section, "ivi-module",
421 &dest->ivi_module, NULL) < 0) {
422 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900423 result = -1;
424 }
425
Pekka Paalanene35b2232015-02-19 17:08:44 +0200426 weston_config_section_get_bool(section, "developermode",
427 &dest->developermode, 0);
428
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900429 return result;
430}
431
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900432static void
433activate_binding(struct weston_seat *seat,
434 struct weston_view *focus_view)
435{
436 struct weston_surface *focus = focus_view->surface;
437 struct weston_surface *main_surface =
438 weston_surface_get_main_surface(focus);
439
440 if (get_ivi_shell_surface(main_surface) == NULL)
441 return;
442
443 weston_surface_activate(focus, seat);
444}
445
446static void
447click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
448 uint32_t button, void *data)
449{
450 if (pointer->grab != &pointer->default_grab)
451 return;
452 if (pointer->focus == NULL)
453 return;
454
455 activate_binding(pointer->seat, pointer->focus);
456}
457
458static void
459touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
460 void *data)
461{
462 if (touch->grab != &touch->default_grab)
463 return;
464 if (touch->focus == NULL)
465 return;
466
467 activate_binding(touch->seat, touch->focus);
468}
469
470static void
471shell_add_bindings(struct weston_compositor *compositor,
472 struct ivi_shell *shell)
473{
474 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
475 click_to_activate_binding,
476 shell);
477 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
478 click_to_activate_binding,
479 shell);
480 weston_compositor_add_touch_binding(compositor, 0,
481 touch_to_activate_binding,
482 shell);
483}
484
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900485/*
486 * Initialization of ivi-shell.
487 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900488WL_EXPORT int
489module_init(struct weston_compositor *compositor,
490 int *argc, char *argv[])
491{
492 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900493 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200494 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900495
496 shell = zalloc(sizeof *shell);
497 if (shell == NULL)
498 return -1;
499
Pekka Paalanen8a005252015-03-20 15:53:53 +0200500 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200501 return -1;
502
503 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900504
505 shell->destroy_listener.notify = shell_destroy;
506 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
507
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900508 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200509 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900510
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300511 shell->text_backend = text_backend_init(compositor);
512 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +0000513 goto out_settings;
514
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900515 if (wl_global_create(compositor->wl_display,
516 &ivi_application_interface, 1,
517 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200518 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900519
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900520 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900521 shell_add_bindings(compositor, shell);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900522
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900523 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200524 if (load_controller_modules(compositor, setting.ivi_module,
525 argc, argv) < 0)
526 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900527
Pekka Paalanene35b2232015-02-19 17:08:44 +0200528 retval = 0;
529
530out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900531 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200532
533 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900534}