blob: 7a5376989ee1e4f7e32a5d9a304c43d3ccd1a64d [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"
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090047#include "ivi-layout-private.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
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900124static void
125layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
126{
127 assert(ivisurf->layout_surface != NULL);
128
129 ivi_layout_surface_destroy(ivisurf->layout_surface);
130 ivisurf->layout_surface = NULL;
131
132 ivisurf->surface->configure = NULL;
133 ivisurf->surface->configure_private = NULL;
134 ivisurf->surface = NULL;
135
136 // destroy weston_surface destroy signal.
137 wl_list_remove(&ivisurf->surface_destroy_listener.link);
138}
139
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900140/*
141 * The ivi_surface wl_resource destructor.
142 *
143 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
144 */
145static void
146shell_destroy_shell_surface(struct wl_resource *resource)
147{
148 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
Nobuhiko Tanibata68098422015-06-22 15:31:08 +0900149
150 if (ivisurf == NULL)
151 return;
152
153 assert(ivisurf->resource == resource);
154
155 if (ivisurf->layout_surface != NULL)
156 layout_surface_cleanup(ivisurf);
157
158 wl_list_remove(&ivisurf->link);
159
160 free(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900161}
162
163/* Gets called through the weston_surface destroy signal. */
164static void
165shell_handle_surface_destroy(struct wl_listener *listener, void *data)
166{
167 struct ivi_shell_surface *ivisurf =
168 container_of(listener, struct ivi_shell_surface,
169 surface_destroy_listener);
170
171 assert(ivisurf != NULL);
172
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900173 if (ivisurf->layout_surface != NULL)
174 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900175}
176
177/* Gets called, when a client sends ivi_surface.destroy request. */
178static void
179surface_destroy(struct wl_client *client, struct wl_resource *resource)
180{
181 /*
182 * Fires the wl_resource destroy signal, and then calls
183 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
184 */
185 wl_resource_destroy(resource);
186}
187
188static const struct ivi_surface_interface surface_implementation = {
189 surface_destroy,
190};
191
192/**
193 * Request handler for ivi_application.surface_create.
194 *
195 * Creates an ivi_surface protocol object associated with the given wl_surface.
196 * ivi_surface protocol object is represented by struct ivi_shell_surface.
197 *
198 * \param client The client.
199 * \param resource The ivi_application protocol object.
200 * \param id_surface The IVI surface ID.
201 * \param surface_resource The wl_surface protocol object.
202 * \param id The protocol object id for the new ivi_surface protocol object.
203 *
204 * The wl_surface is given the ivi_surface role and associated with a unique
205 * IVI ID which is used to identify the surface in a controller
206 * (window manager).
207 */
208static void
209application_surface_create(struct wl_client *client,
210 struct wl_resource *resource,
211 uint32_t id_surface,
212 struct wl_resource *surface_resource,
213 uint32_t id)
214{
215 struct ivi_shell *shell = wl_resource_get_user_data(resource);
216 struct ivi_shell_surface *ivisurf;
217 struct ivi_layout_surface *layout_surface;
218 struct weston_surface *weston_surface =
219 wl_resource_get_user_data(surface_resource);
220 struct wl_resource *res;
221
222 if (weston_surface_set_role(weston_surface, "ivi_surface",
223 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
224 return;
225
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900226 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900227
228 /* check if id_ivi is already used for wl_surface*/
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300229 if (layout_surface == NULL) {
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900230 wl_resource_post_error(resource,
231 IVI_APPLICATION_ERROR_IVI_ID,
232 "surface_id is already assigned "
233 "by another app");
234 return;
235 }
236
237 ivisurf = zalloc(sizeof *ivisurf);
238 if (ivisurf == NULL) {
239 wl_resource_post_no_memory(resource);
240 return;
241 }
242
243 wl_list_init(&ivisurf->link);
244 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
245
246 ivisurf->shell = shell;
247 ivisurf->id_surface = id_surface;
248
249 ivisurf->width = 0;
250 ivisurf->height = 0;
251 ivisurf->layout_surface = layout_surface;
Pekka Paalanen1f821932016-03-15 16:57:51 +0200252
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900253 /*
254 * The following code relies on wl_surface destruction triggering
255 * immediateweston_surface destruction
256 */
257 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
258 wl_signal_add(&weston_surface->destroy_signal,
259 &ivisurf->surface_destroy_listener);
260
261 ivisurf->surface = weston_surface;
262
263 weston_surface->configure = ivi_shell_surface_configure;
264 weston_surface->configure_private = ivisurf;
265
266 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
267 if (res == NULL) {
268 wl_client_post_no_memory(client);
269 return;
270 }
271
272 ivisurf->resource = res;
273
274 wl_resource_set_implementation(res, &surface_implementation,
275 ivisurf, shell_destroy_shell_surface);
276}
277
278static const struct ivi_application_interface application_implementation = {
279 application_surface_create
280};
281
282/*
283 * Handle wl_registry.bind of ivi_application global singleton.
284 */
285static void
286bind_ivi_application(struct wl_client *client,
287 void *data, uint32_t version, uint32_t id)
288{
289 struct ivi_shell *shell = data;
290 struct wl_resource *resource;
291
292 resource = wl_resource_create(client, &ivi_application_interface,
293 1, id);
294
295 wl_resource_set_implementation(resource,
296 &application_implementation,
297 shell, NULL);
298}
299
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900300struct weston_view *
301get_default_view(struct weston_surface *surface)
302{
303 struct ivi_shell_surface *shsurf;
304 struct weston_view *view;
305
306 if (!surface || wl_list_empty(&surface->views))
307 return NULL;
308
309 shsurf = get_ivi_shell_surface(surface);
310 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900311 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900312 if (view)
313 return view;
314 }
315
316 wl_list_for_each(view, &surface->views, surface_link) {
317 if (weston_view_is_mapped(view))
318 return view;
319 }
320
321 return container_of(surface->views.next,
322 struct weston_view, surface_link);
323}
324
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900325/*
326 * Called through the compositor's destroy signal.
327 */
328static void
329shell_destroy(struct wl_listener *listener, void *data)
330{
331 struct ivi_shell *shell =
332 container_of(listener, struct ivi_shell, destroy_listener);
333 struct ivi_shell_surface *ivisurf, *next;
334
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300335 text_backend_destroy(shell->text_backend);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900336 input_panel_destroy(shell);
337
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900338 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
339 wl_list_remove(&ivisurf->link);
340 free(ivisurf);
341 }
342
343 free(shell);
344}
345
346static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500347terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
348 uint32_t key, void *data)
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200349{
350 struct weston_compositor *compositor = data;
351
352 wl_display_terminate(compositor->wl_display);
353}
354
355static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200356init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
357 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900358{
359 shell->compositor = compositor;
360
361 wl_list_init(&shell->ivi_surface_list);
362
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900363 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200364
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200365 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200366 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200367
368 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
369 MODIFIER_CTRL | MODIFIER_ALT,
370 terminate_binding,
371 compositor);
372 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900373}
374
375static int
376ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200377 struct weston_compositor *compositor,
378 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900379{
380 int result = 0;
381 struct weston_config *config = compositor->config;
382 struct weston_config_section *section;
383
Pekka Paalanen8a005252015-03-20 15:53:53 +0200384 const struct weston_option ivi_shell_options[] = {
385 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
386 };
387
388 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
389 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900390
391 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
392
Pekka Paalanen8a005252015-03-20 15:53:53 +0200393 if (!dest->ivi_module &&
394 weston_config_section_get_string(section, "ivi-module",
395 &dest->ivi_module, NULL) < 0) {
396 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900397 result = -1;
398 }
399
Pekka Paalanene35b2232015-02-19 17:08:44 +0200400 weston_config_section_get_bool(section, "developermode",
401 &dest->developermode, 0);
402
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900403 return result;
404}
405
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900406static void
407activate_binding(struct weston_seat *seat,
408 struct weston_view *focus_view)
409{
410 struct weston_surface *focus = focus_view->surface;
411 struct weston_surface *main_surface =
412 weston_surface_get_main_surface(focus);
413
414 if (get_ivi_shell_surface(main_surface) == NULL)
415 return;
416
417 weston_surface_activate(focus, seat);
418}
419
420static void
421click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
422 uint32_t button, void *data)
423{
424 if (pointer->grab != &pointer->default_grab)
425 return;
426 if (pointer->focus == NULL)
427 return;
428
429 activate_binding(pointer->seat, pointer->focus);
430}
431
432static void
433touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
434 void *data)
435{
436 if (touch->grab != &touch->default_grab)
437 return;
438 if (touch->focus == NULL)
439 return;
440
441 activate_binding(touch->seat, touch->focus);
442}
443
444static void
445shell_add_bindings(struct weston_compositor *compositor,
446 struct ivi_shell *shell)
447{
448 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
449 click_to_activate_binding,
450 shell);
451 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
452 click_to_activate_binding,
453 shell);
454 weston_compositor_add_touch_binding(compositor, 0,
455 touch_to_activate_binding,
456 shell);
457}
458
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900459/*
460 * Initialization of ivi-shell.
461 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900462WL_EXPORT int
463module_init(struct weston_compositor *compositor,
464 int *argc, char *argv[])
465{
466 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900467 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200468 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900469
470 shell = zalloc(sizeof *shell);
471 if (shell == NULL)
472 return -1;
473
Pekka Paalanen8a005252015-03-20 15:53:53 +0200474 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200475 return -1;
476
477 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900478
479 shell->destroy_listener.notify = shell_destroy;
480 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
481
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900482 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200483 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900484
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300485 shell->text_backend = text_backend_init(compositor);
486 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +0000487 goto out_settings;
488
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900489 if (wl_global_create(compositor->wl_display,
490 &ivi_application_interface, 1,
491 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200492 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900493
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900494 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900495 shell_add_bindings(compositor, shell);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900496
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900497 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200498 if (load_controller_modules(compositor, setting.ivi_module,
499 argc, argv) < 0)
500 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900501
Pekka Paalanene35b2232015-02-19 17:08:44 +0200502 retval = 0;
503
504out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900505 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200506
507 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900508}