blob: d136f460ec162e5fe3f7fd7a4f4ddcceac84b8d2 [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
Pekka Paalanenfd45f602016-03-16 15:05:03 +0200111 assert(ivisurf);
112 if (!ivisurf)
113 return;
114
115 if (surface->width == 0 || surface->height == 0)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900116 return;
117
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900118 if (ivisurf->width != surface->width ||
119 ivisurf->height != surface->height) {
120 ivisurf->width = surface->width;
121 ivisurf->height = surface->height;
122
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900123 ivi_layout_surface_configure(ivisurf->layout_surface,
124 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900125 }
126}
127
Pekka Paalanen13281f62016-03-07 16:19:37 +0200128static int
129ivi_shell_surface_get_label(struct weston_surface *surface,
130 char *buf,
131 size_t len)
132{
133 struct ivi_shell_surface *shell_surf = get_ivi_shell_surface(surface);
134
135 if (!shell_surf)
136 return snprintf(buf, len, "unidentified window in ivi-shell");
137
138 return snprintf(buf, len, "ivi-surface %#x", shell_surf->id_surface);
139}
140
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900141static void
142layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
143{
144 assert(ivisurf->layout_surface != NULL);
145
146 ivi_layout_surface_destroy(ivisurf->layout_surface);
147 ivisurf->layout_surface = NULL;
148
149 ivisurf->surface->configure = NULL;
150 ivisurf->surface->configure_private = NULL;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200151 weston_surface_set_label_func(ivisurf->surface, NULL);
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900152 ivisurf->surface = NULL;
153
154 // destroy weston_surface destroy signal.
155 wl_list_remove(&ivisurf->surface_destroy_listener.link);
156}
157
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900158/*
159 * The ivi_surface wl_resource destructor.
160 *
161 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
162 */
163static void
164shell_destroy_shell_surface(struct wl_resource *resource)
165{
166 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
Nobuhiko Tanibata68098422015-06-22 15:31:08 +0900167
168 if (ivisurf == NULL)
169 return;
170
171 assert(ivisurf->resource == resource);
172
173 if (ivisurf->layout_surface != NULL)
174 layout_surface_cleanup(ivisurf);
175
176 wl_list_remove(&ivisurf->link);
177
178 free(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900179}
180
181/* Gets called through the weston_surface destroy signal. */
182static void
183shell_handle_surface_destroy(struct wl_listener *listener, void *data)
184{
185 struct ivi_shell_surface *ivisurf =
186 container_of(listener, struct ivi_shell_surface,
187 surface_destroy_listener);
188
189 assert(ivisurf != NULL);
190
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900191 if (ivisurf->layout_surface != NULL)
192 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900193}
194
195/* Gets called, when a client sends ivi_surface.destroy request. */
196static void
197surface_destroy(struct wl_client *client, struct wl_resource *resource)
198{
199 /*
200 * Fires the wl_resource destroy signal, and then calls
201 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
202 */
203 wl_resource_destroy(resource);
204}
205
206static const struct ivi_surface_interface surface_implementation = {
207 surface_destroy,
208};
209
210/**
211 * Request handler for ivi_application.surface_create.
212 *
213 * Creates an ivi_surface protocol object associated with the given wl_surface.
214 * ivi_surface protocol object is represented by struct ivi_shell_surface.
215 *
216 * \param client The client.
217 * \param resource The ivi_application protocol object.
218 * \param id_surface The IVI surface ID.
219 * \param surface_resource The wl_surface protocol object.
220 * \param id The protocol object id for the new ivi_surface protocol object.
221 *
222 * The wl_surface is given the ivi_surface role and associated with a unique
223 * IVI ID which is used to identify the surface in a controller
224 * (window manager).
225 */
226static void
227application_surface_create(struct wl_client *client,
228 struct wl_resource *resource,
229 uint32_t id_surface,
230 struct wl_resource *surface_resource,
231 uint32_t id)
232{
233 struct ivi_shell *shell = wl_resource_get_user_data(resource);
234 struct ivi_shell_surface *ivisurf;
235 struct ivi_layout_surface *layout_surface;
236 struct weston_surface *weston_surface =
237 wl_resource_get_user_data(surface_resource);
238 struct wl_resource *res;
239
240 if (weston_surface_set_role(weston_surface, "ivi_surface",
241 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
242 return;
243
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900244 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900245
246 /* check if id_ivi is already used for wl_surface*/
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300247 if (layout_surface == NULL) {
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900248 wl_resource_post_error(resource,
249 IVI_APPLICATION_ERROR_IVI_ID,
250 "surface_id is already assigned "
251 "by another app");
252 return;
253 }
254
255 ivisurf = zalloc(sizeof *ivisurf);
256 if (ivisurf == NULL) {
257 wl_resource_post_no_memory(resource);
258 return;
259 }
260
261 wl_list_init(&ivisurf->link);
262 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
263
264 ivisurf->shell = shell;
265 ivisurf->id_surface = id_surface;
266
267 ivisurf->width = 0;
268 ivisurf->height = 0;
269 ivisurf->layout_surface = layout_surface;
Pekka Paalanen1f821932016-03-15 16:57:51 +0200270
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900271 /*
272 * The following code relies on wl_surface destruction triggering
273 * immediateweston_surface destruction
274 */
275 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
276 wl_signal_add(&weston_surface->destroy_signal,
277 &ivisurf->surface_destroy_listener);
278
279 ivisurf->surface = weston_surface;
280
281 weston_surface->configure = ivi_shell_surface_configure;
282 weston_surface->configure_private = ivisurf;
Pekka Paalanen13281f62016-03-07 16:19:37 +0200283 weston_surface_set_label_func(weston_surface,
284 ivi_shell_surface_get_label);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900285
286 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
287 if (res == NULL) {
288 wl_client_post_no_memory(client);
289 return;
290 }
291
292 ivisurf->resource = res;
293
294 wl_resource_set_implementation(res, &surface_implementation,
295 ivisurf, shell_destroy_shell_surface);
296}
297
298static const struct ivi_application_interface application_implementation = {
299 application_surface_create
300};
301
302/*
303 * Handle wl_registry.bind of ivi_application global singleton.
304 */
305static void
306bind_ivi_application(struct wl_client *client,
307 void *data, uint32_t version, uint32_t id)
308{
309 struct ivi_shell *shell = data;
310 struct wl_resource *resource;
311
312 resource = wl_resource_create(client, &ivi_application_interface,
313 1, id);
314
315 wl_resource_set_implementation(resource,
316 &application_implementation,
317 shell, NULL);
318}
319
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900320struct weston_view *
321get_default_view(struct weston_surface *surface)
322{
323 struct ivi_shell_surface *shsurf;
324 struct weston_view *view;
325
326 if (!surface || wl_list_empty(&surface->views))
327 return NULL;
328
329 shsurf = get_ivi_shell_surface(surface);
330 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900331 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900332 if (view)
333 return view;
334 }
335
336 wl_list_for_each(view, &surface->views, surface_link) {
337 if (weston_view_is_mapped(view))
338 return view;
339 }
340
341 return container_of(surface->views.next,
342 struct weston_view, surface_link);
343}
344
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900345/*
346 * Called through the compositor's destroy signal.
347 */
348static void
349shell_destroy(struct wl_listener *listener, void *data)
350{
351 struct ivi_shell *shell =
352 container_of(listener, struct ivi_shell, destroy_listener);
353 struct ivi_shell_surface *ivisurf, *next;
354
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300355 text_backend_destroy(shell->text_backend);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900356 input_panel_destroy(shell);
357
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900358 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
359 wl_list_remove(&ivisurf->link);
360 free(ivisurf);
361 }
362
363 free(shell);
364}
365
366static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500367terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
368 uint32_t key, void *data)
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200369{
370 struct weston_compositor *compositor = data;
371
372 wl_display_terminate(compositor->wl_display);
373}
374
375static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200376init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
377 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900378{
379 shell->compositor = compositor;
380
381 wl_list_init(&shell->ivi_surface_list);
382
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900383 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200384
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200385 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200386 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200387
388 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
389 MODIFIER_CTRL | MODIFIER_ALT,
390 terminate_binding,
391 compositor);
392 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900393}
394
395static int
396ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200397 struct weston_compositor *compositor,
398 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900399{
400 int result = 0;
401 struct weston_config *config = compositor->config;
402 struct weston_config_section *section;
403
Pekka Paalanen8a005252015-03-20 15:53:53 +0200404 const struct weston_option ivi_shell_options[] = {
405 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
406 };
407
408 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
409 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900410
411 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
412
Pekka Paalanen8a005252015-03-20 15:53:53 +0200413 if (!dest->ivi_module &&
414 weston_config_section_get_string(section, "ivi-module",
415 &dest->ivi_module, NULL) < 0) {
416 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900417 result = -1;
418 }
419
Pekka Paalanene35b2232015-02-19 17:08:44 +0200420 weston_config_section_get_bool(section, "developermode",
421 &dest->developermode, 0);
422
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900423 return result;
424}
425
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900426static void
427activate_binding(struct weston_seat *seat,
428 struct weston_view *focus_view)
429{
430 struct weston_surface *focus = focus_view->surface;
431 struct weston_surface *main_surface =
432 weston_surface_get_main_surface(focus);
433
434 if (get_ivi_shell_surface(main_surface) == NULL)
435 return;
436
437 weston_surface_activate(focus, seat);
438}
439
440static void
441click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
442 uint32_t button, void *data)
443{
444 if (pointer->grab != &pointer->default_grab)
445 return;
446 if (pointer->focus == NULL)
447 return;
448
449 activate_binding(pointer->seat, pointer->focus);
450}
451
452static void
453touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
454 void *data)
455{
456 if (touch->grab != &touch->default_grab)
457 return;
458 if (touch->focus == NULL)
459 return;
460
461 activate_binding(touch->seat, touch->focus);
462}
463
464static void
465shell_add_bindings(struct weston_compositor *compositor,
466 struct ivi_shell *shell)
467{
468 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
469 click_to_activate_binding,
470 shell);
471 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
472 click_to_activate_binding,
473 shell);
474 weston_compositor_add_touch_binding(compositor, 0,
475 touch_to_activate_binding,
476 shell);
477}
478
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900479/*
480 * Initialization of ivi-shell.
481 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900482WL_EXPORT int
483module_init(struct weston_compositor *compositor,
484 int *argc, char *argv[])
485{
486 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900487 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200488 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900489
490 shell = zalloc(sizeof *shell);
491 if (shell == NULL)
492 return -1;
493
Pekka Paalanen8a005252015-03-20 15:53:53 +0200494 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200495 return -1;
496
497 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900498
499 shell->destroy_listener.notify = shell_destroy;
500 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
501
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900502 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200503 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900504
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300505 shell->text_backend = text_backend_init(compositor);
506 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +0000507 goto out_settings;
508
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900509 if (wl_global_create(compositor->wl_display,
510 &ivi_application_interface, 1,
511 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200512 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900513
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900514 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900515 shell_add_bindings(compositor, shell);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900516
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900517 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200518 if (load_controller_modules(compositor, setting.ivi_module,
519 argc, argv) < 0)
520 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900521
Pekka Paalanene35b2232015-02-19 17:08:44 +0200522 retval = 0;
523
524out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900525 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200526
527 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900528}