blob: f9c25839a4cc21c2b8234f288b1151ec520bdcba [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;
66
67 struct wl_listener configured_listener;
68};
69
70struct ivi_shell_setting
71{
72 char *ivi_module;
Pekka Paalanene35b2232015-02-19 17:08:44 +020073 int developermode;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090074};
75
76/*
77 * Implementation of ivi_surface
78 */
79
80static void
81surface_configure_notify(struct wl_listener *listener, void *data)
82{
83 struct ivi_layout_surface *layout_surf =
84 (struct ivi_layout_surface *)data;
85
86 struct ivi_shell_surface *shell_surf =
87 container_of(listener,
88 struct ivi_shell_surface,
89 configured_listener);
90
91 int32_t dest_width = 0;
92 int32_t dest_height = 0;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090093
94 ivi_layout_surface_get_dimension(layout_surf,
95 &dest_width, &dest_height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090096
97 if (shell_surf->resource)
98 ivi_surface_send_configure(shell_surf->resource,
99 dest_width, dest_height);
100}
101
102static void
103ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
104
105static struct ivi_shell_surface *
106get_ivi_shell_surface(struct weston_surface *surface)
107{
108 if (surface->configure == ivi_shell_surface_configure)
109 return surface->configure_private;
110
111 return NULL;
112}
113
114static void
115ivi_shell_surface_configure(struct weston_surface *surface,
116 int32_t sx, int32_t sy)
117{
118 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900119
120 if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
121 return;
122
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900123 if (ivisurf->width != surface->width ||
124 ivisurf->height != surface->height) {
125 ivisurf->width = surface->width;
126 ivisurf->height = surface->height;
127
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900128 ivi_layout_surface_configure(ivisurf->layout_surface,
129 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900130 }
131}
132
133/*
134 * The ivi_surface wl_resource destructor.
135 *
136 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
137 */
138static void
139shell_destroy_shell_surface(struct wl_resource *resource)
140{
141 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
142 if (ivisurf != NULL) {
143 ivisurf->resource = NULL;
144 }
145}
146
147/* Gets called through the weston_surface destroy signal. */
148static void
149shell_handle_surface_destroy(struct wl_listener *listener, void *data)
150{
151 struct ivi_shell_surface *ivisurf =
152 container_of(listener, struct ivi_shell_surface,
153 surface_destroy_listener);
154
155 assert(ivisurf != NULL);
156
157 if (ivisurf->surface!=NULL) {
158 ivisurf->surface->configure = NULL;
159 ivisurf->surface->configure_private = NULL;
160 ivisurf->surface = NULL;
161 }
162
163 wl_list_remove(&ivisurf->surface_destroy_listener.link);
164 wl_list_remove(&ivisurf->link);
165
166 if (ivisurf->resource != NULL) {
167 wl_resource_set_user_data(ivisurf->resource, NULL);
168 ivisurf->resource = NULL;
169 }
170 free(ivisurf);
171
172}
173
174/* Gets called, when a client sends ivi_surface.destroy request. */
175static void
176surface_destroy(struct wl_client *client, struct wl_resource *resource)
177{
178 /*
179 * Fires the wl_resource destroy signal, and then calls
180 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
181 */
182 wl_resource_destroy(resource);
183}
184
185static const struct ivi_surface_interface surface_implementation = {
186 surface_destroy,
187};
188
189/**
190 * Request handler for ivi_application.surface_create.
191 *
192 * Creates an ivi_surface protocol object associated with the given wl_surface.
193 * ivi_surface protocol object is represented by struct ivi_shell_surface.
194 *
195 * \param client The client.
196 * \param resource The ivi_application protocol object.
197 * \param id_surface The IVI surface ID.
198 * \param surface_resource The wl_surface protocol object.
199 * \param id The protocol object id for the new ivi_surface protocol object.
200 *
201 * The wl_surface is given the ivi_surface role and associated with a unique
202 * IVI ID which is used to identify the surface in a controller
203 * (window manager).
204 */
205static void
206application_surface_create(struct wl_client *client,
207 struct wl_resource *resource,
208 uint32_t id_surface,
209 struct wl_resource *surface_resource,
210 uint32_t id)
211{
212 struct ivi_shell *shell = wl_resource_get_user_data(resource);
213 struct ivi_shell_surface *ivisurf;
214 struct ivi_layout_surface *layout_surface;
215 struct weston_surface *weston_surface =
216 wl_resource_get_user_data(surface_resource);
217 struct wl_resource *res;
218
219 if (weston_surface_set_role(weston_surface, "ivi_surface",
220 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
221 return;
222
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900223 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900224
225 /* check if id_ivi is already used for wl_surface*/
226 if (layout_surface == NULL){
227 wl_resource_post_error(resource,
228 IVI_APPLICATION_ERROR_IVI_ID,
229 "surface_id is already assigned "
230 "by another app");
231 return;
232 }
233
234 ivisurf = zalloc(sizeof *ivisurf);
235 if (ivisurf == NULL) {
236 wl_resource_post_no_memory(resource);
237 return;
238 }
239
240 wl_list_init(&ivisurf->link);
241 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
242
243 ivisurf->shell = shell;
244 ivisurf->id_surface = id_surface;
245
246 ivisurf->width = 0;
247 ivisurf->height = 0;
248 ivisurf->layout_surface = layout_surface;
249 ivisurf->configured_listener.notify = surface_configure_notify;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900250 ivi_layout_surface_add_configured_listener(layout_surface,
251 &ivisurf->configured_listener);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900252 /*
253 * The following code relies on wl_surface destruction triggering
254 * immediateweston_surface destruction
255 */
256 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
257 wl_signal_add(&weston_surface->destroy_signal,
258 &ivisurf->surface_destroy_listener);
259
260 ivisurf->surface = weston_surface;
261
262 weston_surface->configure = ivi_shell_surface_configure;
263 weston_surface->configure_private = ivisurf;
264
265 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
266 if (res == NULL) {
267 wl_client_post_no_memory(client);
268 return;
269 }
270
271 ivisurf->resource = res;
272
273 wl_resource_set_implementation(res, &surface_implementation,
274 ivisurf, shell_destroy_shell_surface);
275}
276
277static const struct ivi_application_interface application_implementation = {
278 application_surface_create
279};
280
281/*
282 * Handle wl_registry.bind of ivi_application global singleton.
283 */
284static void
285bind_ivi_application(struct wl_client *client,
286 void *data, uint32_t version, uint32_t id)
287{
288 struct ivi_shell *shell = data;
289 struct wl_resource *resource;
290
291 resource = wl_resource_create(client, &ivi_application_interface,
292 1, id);
293
294 wl_resource_set_implementation(resource,
295 &application_implementation,
296 shell, NULL);
297}
298
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900299struct weston_view *
300get_default_view(struct weston_surface *surface)
301{
302 struct ivi_shell_surface *shsurf;
303 struct weston_view *view;
304
305 if (!surface || wl_list_empty(&surface->views))
306 return NULL;
307
308 shsurf = get_ivi_shell_surface(surface);
309 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900310 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900311 if (view)
312 return view;
313 }
314
315 wl_list_for_each(view, &surface->views, surface_link) {
316 if (weston_view_is_mapped(view))
317 return view;
318 }
319
320 return container_of(surface->views.next,
321 struct weston_view, surface_link);
322}
323
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900324/*
325 * Called through the compositor's destroy signal.
326 */
327static void
328shell_destroy(struct wl_listener *listener, void *data)
329{
330 struct ivi_shell *shell =
331 container_of(listener, struct ivi_shell, destroy_listener);
332 struct ivi_shell_surface *ivisurf, *next;
333
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900334 input_panel_destroy(shell);
335
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900336 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
337 wl_list_remove(&ivisurf->link);
338 free(ivisurf);
339 }
340
341 free(shell);
342}
343
344static void
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200345terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
346 void *data)
347{
348 struct weston_compositor *compositor = data;
349
350 wl_display_terminate(compositor->wl_display);
351}
352
353static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200354init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
355 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900356{
357 shell->compositor = compositor;
358
359 wl_list_init(&shell->ivi_surface_list);
360
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900361 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200362
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200363 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200364 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200365
366 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
367 MODIFIER_CTRL | MODIFIER_ALT,
368 terminate_binding,
369 compositor);
370 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900371}
372
373static int
374ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200375 struct weston_compositor *compositor,
376 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900377{
378 int result = 0;
379 struct weston_config *config = compositor->config;
380 struct weston_config_section *section;
381
Pekka Paalanen8a005252015-03-20 15:53:53 +0200382 const struct weston_option ivi_shell_options[] = {
383 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
384 };
385
386 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
387 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900388
389 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
390
Pekka Paalanen8a005252015-03-20 15:53:53 +0200391 if (!dest->ivi_module &&
392 weston_config_section_get_string(section, "ivi-module",
393 &dest->ivi_module, NULL) < 0) {
394 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900395 result = -1;
396 }
397
Pekka Paalanene35b2232015-02-19 17:08:44 +0200398 weston_config_section_get_bool(section, "developermode",
399 &dest->developermode, 0);
400
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900401 return result;
402}
403
404/*
405 * Initialization of ivi-shell.
406 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900407WL_EXPORT int
408module_init(struct weston_compositor *compositor,
409 int *argc, char *argv[])
410{
411 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900412 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200413 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900414
415 shell = zalloc(sizeof *shell);
416 if (shell == NULL)
417 return -1;
418
Pekka Paalanen8a005252015-03-20 15:53:53 +0200419 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200420 return -1;
421
422 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900423
424 shell->destroy_listener.notify = shell_destroy;
425 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
426
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900427 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200428 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900429
Murray Calavera9a51cd72015-06-10 21:16:02 +0000430 if (text_backend_init(compositor) < 0)
431 goto out_settings;
432
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900433 if (wl_global_create(compositor->wl_display,
434 &ivi_application_interface, 1,
435 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200436 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900437
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900438 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900439
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900440 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200441 if (load_controller_modules(compositor, setting.ivi_module,
442 argc, argv) < 0)
443 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900444
Pekka Paalanene35b2232015-02-19 17:08:44 +0200445 retval = 0;
446
447out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900448 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200449
450 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900451}