blob: 896ba1b4bd9594cb5f55934eaf9701dd8cb73d29 [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
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900133static void
134layout_surface_cleanup(struct ivi_shell_surface *ivisurf)
135{
136 assert(ivisurf->layout_surface != NULL);
137
138 ivi_layout_surface_destroy(ivisurf->layout_surface);
139 ivisurf->layout_surface = NULL;
140
141 ivisurf->surface->configure = NULL;
142 ivisurf->surface->configure_private = NULL;
143 ivisurf->surface = NULL;
144
145 // destroy weston_surface destroy signal.
146 wl_list_remove(&ivisurf->surface_destroy_listener.link);
147}
148
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900149/*
150 * The ivi_surface wl_resource destructor.
151 *
152 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
153 */
154static void
155shell_destroy_shell_surface(struct wl_resource *resource)
156{
157 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
Nobuhiko Tanibata68098422015-06-22 15:31:08 +0900158
159 if (ivisurf == NULL)
160 return;
161
162 assert(ivisurf->resource == resource);
163
164 if (ivisurf->layout_surface != NULL)
165 layout_surface_cleanup(ivisurf);
166
167 wl_list_remove(&ivisurf->link);
168
169 free(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900170}
171
172/* Gets called through the weston_surface destroy signal. */
173static void
174shell_handle_surface_destroy(struct wl_listener *listener, void *data)
175{
176 struct ivi_shell_surface *ivisurf =
177 container_of(listener, struct ivi_shell_surface,
178 surface_destroy_listener);
179
180 assert(ivisurf != NULL);
181
Nobuhiko Tanibata6f6c9382015-06-22 15:30:53 +0900182 if (ivisurf->layout_surface != NULL)
183 layout_surface_cleanup(ivisurf);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900184}
185
186/* Gets called, when a client sends ivi_surface.destroy request. */
187static void
188surface_destroy(struct wl_client *client, struct wl_resource *resource)
189{
190 /*
191 * Fires the wl_resource destroy signal, and then calls
192 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
193 */
194 wl_resource_destroy(resource);
195}
196
197static const struct ivi_surface_interface surface_implementation = {
198 surface_destroy,
199};
200
201/**
202 * Request handler for ivi_application.surface_create.
203 *
204 * Creates an ivi_surface protocol object associated with the given wl_surface.
205 * ivi_surface protocol object is represented by struct ivi_shell_surface.
206 *
207 * \param client The client.
208 * \param resource The ivi_application protocol object.
209 * \param id_surface The IVI surface ID.
210 * \param surface_resource The wl_surface protocol object.
211 * \param id The protocol object id for the new ivi_surface protocol object.
212 *
213 * The wl_surface is given the ivi_surface role and associated with a unique
214 * IVI ID which is used to identify the surface in a controller
215 * (window manager).
216 */
217static void
218application_surface_create(struct wl_client *client,
219 struct wl_resource *resource,
220 uint32_t id_surface,
221 struct wl_resource *surface_resource,
222 uint32_t id)
223{
224 struct ivi_shell *shell = wl_resource_get_user_data(resource);
225 struct ivi_shell_surface *ivisurf;
226 struct ivi_layout_surface *layout_surface;
227 struct weston_surface *weston_surface =
228 wl_resource_get_user_data(surface_resource);
229 struct wl_resource *res;
230
231 if (weston_surface_set_role(weston_surface, "ivi_surface",
232 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
233 return;
234
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900235 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900236
237 /* check if id_ivi is already used for wl_surface*/
238 if (layout_surface == NULL){
239 wl_resource_post_error(resource,
240 IVI_APPLICATION_ERROR_IVI_ID,
241 "surface_id is already assigned "
242 "by another app");
243 return;
244 }
245
246 ivisurf = zalloc(sizeof *ivisurf);
247 if (ivisurf == NULL) {
248 wl_resource_post_no_memory(resource);
249 return;
250 }
251
252 wl_list_init(&ivisurf->link);
253 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
254
255 ivisurf->shell = shell;
256 ivisurf->id_surface = id_surface;
257
258 ivisurf->width = 0;
259 ivisurf->height = 0;
260 ivisurf->layout_surface = layout_surface;
261 ivisurf->configured_listener.notify = surface_configure_notify;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900262 ivi_layout_surface_add_configured_listener(layout_surface,
263 &ivisurf->configured_listener);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900264 /*
265 * The following code relies on wl_surface destruction triggering
266 * immediateweston_surface destruction
267 */
268 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
269 wl_signal_add(&weston_surface->destroy_signal,
270 &ivisurf->surface_destroy_listener);
271
272 ivisurf->surface = weston_surface;
273
274 weston_surface->configure = ivi_shell_surface_configure;
275 weston_surface->configure_private = ivisurf;
276
277 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
278 if (res == NULL) {
279 wl_client_post_no_memory(client);
280 return;
281 }
282
283 ivisurf->resource = res;
284
285 wl_resource_set_implementation(res, &surface_implementation,
286 ivisurf, shell_destroy_shell_surface);
287}
288
289static const struct ivi_application_interface application_implementation = {
290 application_surface_create
291};
292
293/*
294 * Handle wl_registry.bind of ivi_application global singleton.
295 */
296static void
297bind_ivi_application(struct wl_client *client,
298 void *data, uint32_t version, uint32_t id)
299{
300 struct ivi_shell *shell = data;
301 struct wl_resource *resource;
302
303 resource = wl_resource_create(client, &ivi_application_interface,
304 1, id);
305
306 wl_resource_set_implementation(resource,
307 &application_implementation,
308 shell, NULL);
309}
310
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900311struct weston_view *
312get_default_view(struct weston_surface *surface)
313{
314 struct ivi_shell_surface *shsurf;
315 struct weston_view *view;
316
317 if (!surface || wl_list_empty(&surface->views))
318 return NULL;
319
320 shsurf = get_ivi_shell_surface(surface);
321 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900322 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900323 if (view)
324 return view;
325 }
326
327 wl_list_for_each(view, &surface->views, surface_link) {
328 if (weston_view_is_mapped(view))
329 return view;
330 }
331
332 return container_of(surface->views.next,
333 struct weston_view, surface_link);
334}
335
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900336/*
337 * Called through the compositor's destroy signal.
338 */
339static void
340shell_destroy(struct wl_listener *listener, void *data)
341{
342 struct ivi_shell *shell =
343 container_of(listener, struct ivi_shell, destroy_listener);
344 struct ivi_shell_surface *ivisurf, *next;
345
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900346 input_panel_destroy(shell);
347
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900348 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
349 wl_list_remove(&ivisurf->link);
350 free(ivisurf);
351 }
352
353 free(shell);
354}
355
356static void
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200357terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
358 void *data)
359{
360 struct weston_compositor *compositor = data;
361
362 wl_display_terminate(compositor->wl_display);
363}
364
365static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200366init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
367 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900368{
369 shell->compositor = compositor;
370
371 wl_list_init(&shell->ivi_surface_list);
372
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900373 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200374
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200375 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200376 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200377
378 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
379 MODIFIER_CTRL | MODIFIER_ALT,
380 terminate_binding,
381 compositor);
382 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900383}
384
385static int
386ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200387 struct weston_compositor *compositor,
388 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900389{
390 int result = 0;
391 struct weston_config *config = compositor->config;
392 struct weston_config_section *section;
393
Pekka Paalanen8a005252015-03-20 15:53:53 +0200394 const struct weston_option ivi_shell_options[] = {
395 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
396 };
397
398 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
399 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900400
401 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
402
Pekka Paalanen8a005252015-03-20 15:53:53 +0200403 if (!dest->ivi_module &&
404 weston_config_section_get_string(section, "ivi-module",
405 &dest->ivi_module, NULL) < 0) {
406 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900407 result = -1;
408 }
409
Pekka Paalanene35b2232015-02-19 17:08:44 +0200410 weston_config_section_get_bool(section, "developermode",
411 &dest->developermode, 0);
412
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900413 return result;
414}
415
416/*
417 * Initialization of ivi-shell.
418 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900419WL_EXPORT int
420module_init(struct weston_compositor *compositor,
421 int *argc, char *argv[])
422{
423 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900424 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200425 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900426
427 shell = zalloc(sizeof *shell);
428 if (shell == NULL)
429 return -1;
430
Pekka Paalanen8a005252015-03-20 15:53:53 +0200431 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200432 return -1;
433
434 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900435
436 shell->destroy_listener.notify = shell_destroy;
437 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
438
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900439 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200440 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900441
Murray Calavera9a51cd72015-06-10 21:16:02 +0000442 if (text_backend_init(compositor) < 0)
443 goto out_settings;
444
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900445 if (wl_global_create(compositor->wl_display,
446 &ivi_application_interface, 1,
447 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200448 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900449
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900450 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900451
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900452 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200453 if (load_controller_modules(compositor, setting.ivi_module,
454 argc, argv) < 0)
455 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900456
Pekka Paalanene35b2232015-02-19 17:08:44 +0200457 retval = 0;
458
459out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900460 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200461
462 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900463}