blob: ec3a52be596b7b1c88227ba735793e2d0d7f0161 [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"
48
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090049/* Representation of ivi_surface protocol object. */
50struct ivi_shell_surface
51{
52 struct wl_resource* resource;
53 struct ivi_shell *shell;
54 struct ivi_layout_surface *layout_surface;
55
56 struct weston_surface *surface;
57 struct wl_listener surface_destroy_listener;
58
59 uint32_t id_surface;
60
61 int32_t width;
62 int32_t height;
63
64 struct wl_list link;
65
66 struct wl_listener configured_listener;
67};
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
80surface_configure_notify(struct wl_listener *listener, void *data)
81{
82 struct ivi_layout_surface *layout_surf =
83 (struct ivi_layout_surface *)data;
84
85 struct ivi_shell_surface *shell_surf =
86 container_of(listener,
87 struct ivi_shell_surface,
88 configured_listener);
89
90 int32_t dest_width = 0;
91 int32_t dest_height = 0;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +090092
93 ivi_layout_surface_get_dimension(layout_surf,
94 &dest_width, &dest_height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +090095
96 if (shell_surf->resource)
97 ivi_surface_send_configure(shell_surf->resource,
98 dest_width, dest_height);
99}
100
101static void
102ivi_shell_surface_configure(struct weston_surface *, int32_t, int32_t);
103
104static struct ivi_shell_surface *
105get_ivi_shell_surface(struct weston_surface *surface)
106{
107 if (surface->configure == ivi_shell_surface_configure)
108 return surface->configure_private;
109
110 return NULL;
111}
112
113static void
114ivi_shell_surface_configure(struct weston_surface *surface,
115 int32_t sx, int32_t sy)
116{
117 struct ivi_shell_surface *ivisurf = get_ivi_shell_surface(surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900118
119 if (surface->width == 0 || surface->height == 0 || ivisurf == NULL)
120 return;
121
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900122 if (ivisurf->width != surface->width ||
123 ivisurf->height != surface->height) {
124 ivisurf->width = surface->width;
125 ivisurf->height = surface->height;
126
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900127 ivi_layout_surface_configure(ivisurf->layout_surface,
128 surface->width, surface->height);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900129 }
130}
131
132/*
133 * The ivi_surface wl_resource destructor.
134 *
135 * Gets called via ivi_surface.destroy request or automatic wl_client clean-up.
136 */
137static void
138shell_destroy_shell_surface(struct wl_resource *resource)
139{
140 struct ivi_shell_surface *ivisurf = wl_resource_get_user_data(resource);
141 if (ivisurf != NULL) {
142 ivisurf->resource = NULL;
143 }
144}
145
146/* Gets called through the weston_surface destroy signal. */
147static void
148shell_handle_surface_destroy(struct wl_listener *listener, void *data)
149{
150 struct ivi_shell_surface *ivisurf =
151 container_of(listener, struct ivi_shell_surface,
152 surface_destroy_listener);
153
154 assert(ivisurf != NULL);
155
156 if (ivisurf->surface!=NULL) {
157 ivisurf->surface->configure = NULL;
158 ivisurf->surface->configure_private = NULL;
159 ivisurf->surface = NULL;
160 }
161
162 wl_list_remove(&ivisurf->surface_destroy_listener.link);
163 wl_list_remove(&ivisurf->link);
164
165 if (ivisurf->resource != NULL) {
166 wl_resource_set_user_data(ivisurf->resource, NULL);
167 ivisurf->resource = NULL;
168 }
169 free(ivisurf);
170
171}
172
173/* Gets called, when a client sends ivi_surface.destroy request. */
174static void
175surface_destroy(struct wl_client *client, struct wl_resource *resource)
176{
177 /*
178 * Fires the wl_resource destroy signal, and then calls
179 * ivi_surface wl_resource destructor: shell_destroy_shell_surface()
180 */
181 wl_resource_destroy(resource);
182}
183
184static const struct ivi_surface_interface surface_implementation = {
185 surface_destroy,
186};
187
188/**
189 * Request handler for ivi_application.surface_create.
190 *
191 * Creates an ivi_surface protocol object associated with the given wl_surface.
192 * ivi_surface protocol object is represented by struct ivi_shell_surface.
193 *
194 * \param client The client.
195 * \param resource The ivi_application protocol object.
196 * \param id_surface The IVI surface ID.
197 * \param surface_resource The wl_surface protocol object.
198 * \param id The protocol object id for the new ivi_surface protocol object.
199 *
200 * The wl_surface is given the ivi_surface role and associated with a unique
201 * IVI ID which is used to identify the surface in a controller
202 * (window manager).
203 */
204static void
205application_surface_create(struct wl_client *client,
206 struct wl_resource *resource,
207 uint32_t id_surface,
208 struct wl_resource *surface_resource,
209 uint32_t id)
210{
211 struct ivi_shell *shell = wl_resource_get_user_data(resource);
212 struct ivi_shell_surface *ivisurf;
213 struct ivi_layout_surface *layout_surface;
214 struct weston_surface *weston_surface =
215 wl_resource_get_user_data(surface_resource);
216 struct wl_resource *res;
217
218 if (weston_surface_set_role(weston_surface, "ivi_surface",
219 resource, IVI_APPLICATION_ERROR_ROLE) < 0)
220 return;
221
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900222 layout_surface = ivi_layout_surface_create(weston_surface, id_surface);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900223
224 /* check if id_ivi is already used for wl_surface*/
225 if (layout_surface == NULL){
226 wl_resource_post_error(resource,
227 IVI_APPLICATION_ERROR_IVI_ID,
228 "surface_id is already assigned "
229 "by another app");
230 return;
231 }
232
233 ivisurf = zalloc(sizeof *ivisurf);
234 if (ivisurf == NULL) {
235 wl_resource_post_no_memory(resource);
236 return;
237 }
238
239 wl_list_init(&ivisurf->link);
240 wl_list_insert(&shell->ivi_surface_list, &ivisurf->link);
241
242 ivisurf->shell = shell;
243 ivisurf->id_surface = id_surface;
244
245 ivisurf->width = 0;
246 ivisurf->height = 0;
247 ivisurf->layout_surface = layout_surface;
248 ivisurf->configured_listener.notify = surface_configure_notify;
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900249 ivi_layout_surface_add_configured_listener(layout_surface,
250 &ivisurf->configured_listener);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900251 /*
252 * The following code relies on wl_surface destruction triggering
253 * immediateweston_surface destruction
254 */
255 ivisurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
256 wl_signal_add(&weston_surface->destroy_signal,
257 &ivisurf->surface_destroy_listener);
258
259 ivisurf->surface = weston_surface;
260
261 weston_surface->configure = ivi_shell_surface_configure;
262 weston_surface->configure_private = ivisurf;
263
264 res = wl_resource_create(client, &ivi_surface_interface, 1, id);
265 if (res == NULL) {
266 wl_client_post_no_memory(client);
267 return;
268 }
269
270 ivisurf->resource = res;
271
272 wl_resource_set_implementation(res, &surface_implementation,
273 ivisurf, shell_destroy_shell_surface);
274}
275
276static const struct ivi_application_interface application_implementation = {
277 application_surface_create
278};
279
280/*
281 * Handle wl_registry.bind of ivi_application global singleton.
282 */
283static void
284bind_ivi_application(struct wl_client *client,
285 void *data, uint32_t version, uint32_t id)
286{
287 struct ivi_shell *shell = data;
288 struct wl_resource *resource;
289
290 resource = wl_resource_create(client, &ivi_application_interface,
291 1, id);
292
293 wl_resource_set_implementation(resource,
294 &application_implementation,
295 shell, NULL);
296}
297
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900298struct weston_view *
299get_default_view(struct weston_surface *surface)
300{
301 struct ivi_shell_surface *shsurf;
302 struct weston_view *view;
303
304 if (!surface || wl_list_empty(&surface->views))
305 return NULL;
306
307 shsurf = get_ivi_shell_surface(surface);
308 if (shsurf && shsurf->layout_surface) {
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900309 view = ivi_layout_get_weston_view(shsurf->layout_surface);
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900310 if (view)
311 return view;
312 }
313
314 wl_list_for_each(view, &surface->views, surface_link) {
315 if (weston_view_is_mapped(view))
316 return view;
317 }
318
319 return container_of(surface->views.next,
320 struct weston_view, surface_link);
321}
322
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900323/*
324 * Called through the compositor's destroy signal.
325 */
326static void
327shell_destroy(struct wl_listener *listener, void *data)
328{
329 struct ivi_shell *shell =
330 container_of(listener, struct ivi_shell, destroy_listener);
331 struct ivi_shell_surface *ivisurf, *next;
332
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900333 input_panel_destroy(shell);
334
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900335 wl_list_for_each_safe(ivisurf, next, &shell->ivi_surface_list, link) {
336 wl_list_remove(&ivisurf->link);
337 free(ivisurf);
338 }
339
340 free(shell);
341}
342
343static void
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200344terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
345 void *data)
346{
347 struct weston_compositor *compositor = data;
348
349 wl_display_terminate(compositor->wl_display);
350}
351
352static void
Pekka Paalanene35b2232015-02-19 17:08:44 +0200353init_ivi_shell(struct weston_compositor *compositor, struct ivi_shell *shell,
354 const struct ivi_shell_setting *setting)
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900355{
356 shell->compositor = compositor;
357
358 wl_list_init(&shell->ivi_surface_list);
359
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900360 weston_layer_init(&shell->input_panel_layer, NULL);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200361
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200362 if (setting->developermode) {
Pekka Paalanene35b2232015-02-19 17:08:44 +0200363 weston_install_debug_key_binding(compositor, MODIFIER_SUPER);
Pekka Paalanen6c7a1c72015-02-19 17:12:19 +0200364
365 weston_compositor_add_key_binding(compositor, KEY_BACKSPACE,
366 MODIFIER_CTRL | MODIFIER_ALT,
367 terminate_binding,
368 compositor);
369 }
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900370}
371
372static int
373ivi_shell_setting_create(struct ivi_shell_setting *dest,
Pekka Paalanen8a005252015-03-20 15:53:53 +0200374 struct weston_compositor *compositor,
375 int *argc, char *argv[])
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900376{
377 int result = 0;
378 struct weston_config *config = compositor->config;
379 struct weston_config_section *section;
380
Pekka Paalanen8a005252015-03-20 15:53:53 +0200381 const struct weston_option ivi_shell_options[] = {
382 { WESTON_OPTION_STRING, "ivi-module", 0, &dest->ivi_module },
383 };
384
385 parse_options(ivi_shell_options, ARRAY_LENGTH(ivi_shell_options),
386 argc, argv);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900387
388 section = weston_config_get_section(config, "ivi-shell", NULL, NULL);
389
Pekka Paalanen8a005252015-03-20 15:53:53 +0200390 if (!dest->ivi_module &&
391 weston_config_section_get_string(section, "ivi-module",
392 &dest->ivi_module, NULL) < 0) {
393 weston_log("Error: ivi-shell: No ivi-module set\n");
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900394 result = -1;
395 }
396
Pekka Paalanene35b2232015-02-19 17:08:44 +0200397 weston_config_section_get_bool(section, "developermode",
398 &dest->developermode, 0);
399
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900400 return result;
401}
402
403/*
404 * Initialization of ivi-shell.
405 */
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900406WL_EXPORT int
407module_init(struct weston_compositor *compositor,
408 int *argc, char *argv[])
409{
410 struct ivi_shell *shell;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900411 struct ivi_shell_setting setting = { };
Pekka Paalanene35b2232015-02-19 17:08:44 +0200412 int retval = -1;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900413
414 shell = zalloc(sizeof *shell);
415 if (shell == NULL)
416 return -1;
417
Pekka Paalanen8a005252015-03-20 15:53:53 +0200418 if (ivi_shell_setting_create(&setting, compositor, argc, argv) != 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200419 return -1;
420
421 init_ivi_shell(compositor, shell, &setting);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900422
423 shell->destroy_listener.notify = shell_destroy;
424 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
425
Nobuhiko Tanibata0038b732014-11-27 13:25:34 +0900426 if (input_panel_setup(shell) < 0)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200427 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900428
Murray Calavera9a51cd72015-06-10 21:16:02 +0000429 if (text_backend_init(compositor) < 0)
430 goto out_settings;
431
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900432 if (wl_global_create(compositor->wl_display,
433 &ivi_application_interface, 1,
434 shell, bind_ivi_application) == NULL)
Pekka Paalanene35b2232015-02-19 17:08:44 +0200435 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900436
Nobuhiko Tanibata28dc18c2014-12-15 13:22:31 +0900437 ivi_layout_init_with_compositor(compositor);
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900438
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900439 /* Call module_init of ivi-modules which are defined in weston.ini */
Pekka Paalanene35b2232015-02-19 17:08:44 +0200440 if (load_controller_modules(compositor, setting.ivi_module,
441 argc, argv) < 0)
442 goto out_settings;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900443
Pekka Paalanene35b2232015-02-19 17:08:44 +0200444 retval = 0;
445
446out_settings:
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900447 free(setting.ivi_module);
Pekka Paalanene35b2232015-02-19 17:08:44 +0200448
449 return retval;
Nobuhiko Tanibata487adc42014-11-27 13:22:37 +0900450}