blob: 8bf3e80a892d58f1560ba0a4411d4ed97918cc1b [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"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030049#include "compositor/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{
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900342 struct weston_view *view;
343
344 if (!surface || wl_list_empty(&surface->views))
345 return NULL;
346
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900347 wl_list_for_each(view, &surface->views, surface_link) {
348 if (weston_view_is_mapped(view))
349 return view;
350 }
351
352 return container_of(surface->views.next,
353 struct weston_view, surface_link);
354}
355
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900356/*
357 * Called through the compositor's destroy signal.
358 */
359static void
360shell_destroy(struct wl_listener *listener, void *data)
361{
362 struct ivi_shell *shell =
363 container_of(listener, struct ivi_shell, destroy_listener);
364 struct ivi_shell_surface *ivisurf, *next;
365
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300366 text_backend_destroy(shell->text_backend);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900367 input_panel_destroy(shell);
368
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900369 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
370 wl_list_remove(&ivisurf->link);
371 free(ivisurf);
372 }
373
374 free(shell);
375}
376
377static void
Derek Foreman8ae2db52015-07-15 13:00:36 -0500378terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
379 uint32_t key, void *data)
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200380{
381 struct weston_compositor *compositor = data;
382
383 wl_display_terminate(compositor->wl_display);
384}
385
386static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200387init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
388 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900389{
390 shell->compositor = compositor;
391
392 wl_list_init(&shell->ivi_surface_list);
393
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900394 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200395
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200396 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200397 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200398
399 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
400 MODIFIER_CTRL | MODIFIER_ALT,
401 terminate_binding,
402 compositor);
403 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900404}
405
406static int
407ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200408 struct weston_compositor *compositor,
409 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900410{
411 int result = 0;
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300412 struct weston_config *config = wet_get_config(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900413 struct weston_config_section *section;
414
Pekka Paalanen8a005252015-03-20 15:53:53 +0200415 const struct weston_option ivi_shell_options[] = {
416 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
417 };
418
419 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
420 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900421
422 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
423
Pekka Paalanen8a005252015-03-20 15:53:53 +0200424 if (!dest->ivi_module &&
425 weston_config_section_get_string(section, "ivi-module",
426 &dest->ivi_module, NULL) < 0) {
427 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900428 result = -1;
429 }
430
Pekka Paalanene35b2232015-02-19 17:08:44 +0200431 weston_config_section_get_bool(section, "developermode",
432 &dest->developermode, 0);
433
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900434 return result;
435}
436
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900437static void
438activate_binding(struct weston_seat *seat,
439 struct weston_view *focus_view)
440{
441 struct weston_surface *focus = focus_view->surface;
442 struct weston_surface *main_surface =
443 weston_surface_get_main_surface(focus);
444
445 if (get_ivi_shell_surface(main_surface) == NULL)
446 return;
447
448 weston_surface_activate(focus, seat);
449}
450
451static void
452click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
453 uint32_t button, void *data)
454{
455 if (pointer->grab != &pointer->default_grab)
456 return;
457 if (pointer->focus == NULL)
458 return;
459
460 activate_binding(pointer->seat, pointer->focus);
461}
462
463static void
464touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
465 void *data)
466{
467 if (touch->grab != &touch->default_grab)
468 return;
469 if (touch->focus == NULL)
470 return;
471
472 activate_binding(touch->seat, touch->focus);
473}
474
475static void
476shell_add_bindings(struct weston_compositor *compositor,
477 struct ivi_shell *shell)
478{
479 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
480 click_to_activate_binding,
481 shell);
482 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
483 click_to_activate_binding,
484 shell);
485 weston_compositor_add_touch_binding(compositor, 0,
486 touch_to_activate_binding,
487 shell);
488}
489
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900490/*
491 * Initialization of ivi-shell.
492 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900493WL_EXPORT int
494module_init(struct weston_compositor *compositor,
495 int *argc, char *argv[])
496{
497 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900498 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200499 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900500
501 shell = zalloc(sizeof *shell);
502 if (shell == NULL)
503 return -1;
504
Pekka Paalanen8a005252015-03-20 15:53:53 +0200505 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200506 return -1;
507
508 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900509
510 shell->destroy_listener.notify = shell_destroy;
511 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
512
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900513 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200514 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900515
Pekka Paalanenaa9536a2015-06-24 16:09:17 +0300516 shell->text_backend = text_backend_init(compositor);
517 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +0000518 goto out_settings;
519
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900520 if (wl_global_create(compositor->wl_display,
521 &ivi_application_interface, 1,
522 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200523 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900524
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900525 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata0627b4a2015-12-09 15:03:47 +0900526 shell_add_bindings(compositor, shell);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900527
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900528 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200529 if (load_controller_modules(compositor, setting.ivi_module,
530 argc, argv) < 0)
531 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900532
Pekka Paalanene35b2232015-02-19 17:08:44 +0200533 retval = 0;
534
535out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900536 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200537
538 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900539}