2b78357fe1afa538309d03915d6bb667e2fa79f3
[AGL/meta-agl-demo.git] / recipes-graphics / wayland / wayland-ivi-extension / 0002-ivi-id-agent-added-ivi-id-agent.patch
1 From 42fc715a430068cdb4484e2cb119418da8ac4e6f Mon Sep 17 00:00:00 2001
2 From: Michael Teyfel <mteyfel@de.adit-jv.com>
3 Date: Fri, 12 Oct 2018 16:46:57 +0200
4 Subject: [PATCH 2/3] ivi-id-agent: added ivi-id-agent
5
6 This is a reference implementation of an ivi-id-agent plugin. It
7 creates surface-ids for desktop-surfaces depending on the configuration
8 provided in "weston.ini". For more please refer to the reference
9 implementation in this commit.
10
11 The only public interface available is the "id_agent_module_init"
12 function. It is responsible for initialization of structs and reading
13 the configuration.
14
15 In the reference "weston.ini" two types of configurations can be found:
16
17 [desktop-app] is used to configure a particular application. Therefore
18 the desired surface-id must be provided. Moreover "app-title" is the
19 title that is provided by the desktop application (xdg-protocol).
20 "app-id" behaves accordingly. Although both parameters can be set, it
21 is not mandatory. Finally at least one has to be set.
22
23 [desktop-app-default] enables the id-agent to generate generic
24 surface-ids for unconfigured applications, e.g. for development. This
25 tag is optional. To generate the id an interval starting from
26 "default-surface-id" to "default-surface-id-max" is used. The id is
27 incremented until the interval is exceeded.
28
29 In the function "get_id" a surface-id is assigned by means of the
30 configuration. It can be adjusted, if another behavior is desired.
31 In this plugin the parameters, that are described above, are evaluated.
32
33 To use these patches please also apply the dedicated patches for
34 weston: https://github.com/mtey/weston/tree/xdg_support_ivi_id_agent
35 Since libweston-desktop is used to introduce desktop-surface support,
36 xdg-protocol is supported.
37
38 Signed-off-by: Michael Teyfel <mteyfel@de.adit-jv.com>
39 ---
40  ivi-id-agent-modules/ivi-id-agent/CMakeLists.txt   |  69 ++++
41  .../ivi-id-agent/src/ivi-id-agent.c                | 381 +++++++++++++++++++++
42  ivi-id-agent-modules/ivi-id-agent/weston.ini.in    |  20 ++
43  3 files changed, 470 insertions(+)
44  create mode 100644 ivi-id-agent-modules/ivi-id-agent/CMakeLists.txt
45  create mode 100644 ivi-id-agent-modules/ivi-id-agent/src/ivi-id-agent.c
46  create mode 100644 ivi-id-agent-modules/ivi-id-agent/weston.ini.in
47
48 diff --git a/ivi-id-agent-modules/ivi-id-agent/CMakeLists.txt b/ivi-id-agent-modules/ivi-id-agent/CMakeLists.txt
49 new file mode 100644
50 index 0000000..2def105
51 --- /dev/null
52 +++ b/ivi-id-agent-modules/ivi-id-agent/CMakeLists.txt
53 @@ -0,0 +1,69 @@
54 +###############################################################################
55 +#
56 +# Copyright (C) 2017 Advanced Driver Information Technology Joint Venture GmbH
57 +#
58 +#
59 +# Licensed under the Apache License, Version 2.0 (the "License");
60 +# you may not use this file except in compliance with the License.
61 +# You may obtain a copy of the License at
62 +#
63 +#      http://www.apache.org/licenses/LICENSE-2.0
64 +#
65 +# Unless required by applicable law or agreed to in writing, software
66 +# distributed under the License is distributed on an "AS IS" BASIS,
67 +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
68 +# See the License for the specific language governing permissions and
69 +# limitations under the License.
70 +#
71 +###############################################################################
72 +
73 +cmake_minimum_required (VERSION 2.6)
74 +
75 +project(ivi-id-agent)
76 +
77 +find_package(PkgConfig REQUIRED)
78 +pkg_check_modules(WAYLAND_SERVER wayland-server REQUIRED)
79 +pkg_check_modules(WESTON weston>=2.0.0 REQUIRED)
80 +pkg_check_modules(PIXMAN pixman-1 REQUIRED)
81 +pkg_check_modules(LIBWESTON_DESKTOP libweston-desktop-2 REQUIRED)
82 +
83 +find_package(Threads REQUIRED)
84 +
85 +include_directories(
86 +    src
87 +    ${WAYLAND_SERVER_INCLUDE_DIRS}
88 +    ${WESTON_INCLUDE_DIRS}
89 +    ${PIXMAN_INCLUDE_DIRS}
90 +)
91 +
92 +link_directories(
93 +    ${WAYLAND_SERVER_LIBRARY_DIRS}
94 +    ${PIXMAN_LIBRARY_DIRS}
95 +)
96 +
97 +
98 +add_library(${PROJECT_NAME} MODULE
99 +    src/ivi-id-agent.c
100 +)
101 +
102 +set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")
103 +
104 +add_dependencies(${PROJECT_NAME}
105 +    ${WAYLAND_SERVER_LIBRARIES}
106 +    ${PIXMAN_LIBRARIES}
107 +)
108 +
109 +set(LIBS
110 +    ${LIBS}
111 +    ${WAYLAND_SERVER_LIBRARIES}
112 +    ${LIBWESTON_DESKTOP_LIBRARIES}
113 +)
114 +
115 +set(CMAKE_C_LDFLAGS "-module -avoid-version")
116 +
117 +target_link_libraries(${PROJECT_NAME} ${LIBS})
118 +
119 +install (
120 +    TARGETS             ${PROJECT_NAME}
121 +    LIBRARY DESTINATION lib${LIB_SUFFIX}/weston
122 +)
123 diff --git a/ivi-id-agent-modules/ivi-id-agent/src/ivi-id-agent.c b/ivi-id-agent-modules/ivi-id-agent/src/ivi-id-agent.c
124 new file mode 100644
125 index 0000000..9bc115d
126 --- /dev/null
127 +++ b/ivi-id-agent-modules/ivi-id-agent/src/ivi-id-agent.c
128 @@ -0,0 +1,381 @@
129 +/*
130 + * Copyright (C) 2017 Advanced Driver Information Technology Joint Venture GmbH
131 + *
132 + * Permission to use, copy, modify, distribute, and sell this software and
133 + * its documentation for any purpose is hereby granted without fee, provided
134 + * that the above copyright notice appear in all copies and that both that
135 + * copyright notice and this permission notice appear in supporting
136 + * documentation, and that the name of the copyright holders not be used in
137 + * advertising or publicity pertaining to distribution of the software
138 + * without specific, written prior permission.  The copyright holders make
139 + * no representations about the suitability of this software for any
140 + * purpose.  It is provided "as is" without express or implied warranty.
141 + *
142 + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
143 + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
144 + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
145 + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
146 + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
147 + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
148 + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
149 + */
150 +
151 +#include <stdlib.h>
152 +#include <stdio.h>
153 +#include <string.h>
154 +#include <limits.h>
155 +
156 +#include <weston.h>
157 +#include <libweston-desktop.h>
158 +#include "config-parser.h"
159 +#include <weston/ivi-layout-export.h>
160 +
161 +#ifndef INVALID_ID
162 +#define INVALID_ID 0xFFFFFFFF
163 +#endif
164 +
165 +struct db_elem
166 +{
167 +    struct wl_list link;
168 +    uint32_t surface_id;
169 +    char *cfg_app_id;
170 +    char *cfg_title;
171 +    struct ivi_layout_surface *layout_surface;
172 +};
173 +
174 +struct ivi_id_agent
175 +{
176 +    uint32_t default_behavior_set;
177 +    uint32_t default_surface_id;
178 +    uint32_t default_surface_id_max;
179 +    struct wl_list app_list;
180 +    struct weston_compositor *compositor;
181 +    const struct ivi_layout_interface *interface;
182 +
183 +    struct wl_listener desktop_surface_configured;
184 +    struct wl_listener destroy_listener;
185 +    struct wl_listener surface_removed;
186 +};
187 +
188 +static int32_t
189 +check_config_parameter(char *cfg_val, char *val)
190 +{
191 +    if (cfg_val == NULL)
192 +        return IVI_SUCCEEDED;
193 +    else if (val == NULL || strcmp(cfg_val, val) != 0)
194 +        return IVI_FAILED;
195 +
196 +    return IVI_SUCCEEDED;
197 +}
198 +
199 +static int32_t
200 +get_id_from_config(struct ivi_id_agent *ida, struct ivi_layout_surface
201 +        *layout_surface) {
202 +    struct db_elem *db_elem;
203 +    char *temp_app_id = NULL;
204 +    char *temp_title = NULL;
205 +    int ret = IVI_FAILED;
206 +
207 +    struct weston_surface *weston_surface =
208 +            ida->interface->surface_get_weston_surface(layout_surface);
209 +
210 +    /* Get app id and title */
211 +    struct weston_desktop_surface *wds = weston_surface_get_desktop_surface(
212 +            weston_surface);
213 +
214 +    if (weston_desktop_surface_get_app_id(wds) != NULL)
215 +        temp_app_id = strdup(weston_desktop_surface_get_app_id(wds));
216 +
217 +    if (weston_desktop_surface_get_title(wds) != NULL)
218 +        temp_title = strdup(weston_desktop_surface_get_title(wds));
219 +
220 +    /*
221 +     * Check for every config parameter to be fulfilled. This part must be
222 +     * extended, if additional attributes are desired to be checked.
223 +     */
224 +    wl_list_for_each(db_elem, &ida->app_list, link)
225 +    {
226 +        if (check_config_parameter(db_elem->cfg_app_id, temp_app_id) == 0) {
227 +            if (check_config_parameter(db_elem->cfg_title, temp_title) == 0) {
228 +                /* Found configuration for application. */
229 +                int res = ida->interface->surface_set_id(layout_surface,
230 +                        db_elem->surface_id);
231 +                if (res)
232 +                    continue;
233 +
234 +                db_elem->layout_surface = layout_surface;
235 +                ret = IVI_SUCCEEDED;
236 +
237 +                break;
238 +            }
239 +        }
240 +    }
241 +
242 +    free(temp_app_id);
243 +    free(temp_title);
244 +
245 +    return ret;
246 +}
247 +
248 +/*
249 + * This function generates the id of a surface in regard to the desired
250 + * parameters. For implementation of different behavior in id generation please
251 + * adjust this function.
252 + * In this implementation the app_id and/or title of the application is used for
253 + * identification. It is also possible to use the pid, uid or gid for example.
254 + */
255 +static int32_t
256 +get_id(struct ivi_id_agent *ida, struct ivi_layout_surface *layout_surface)
257 +{
258 +    if (get_id_from_config(ida, layout_surface) == IVI_SUCCEEDED)
259 +        return IVI_SUCCEEDED;
260 +
261 +    /* No default layer available */
262 +    if (ida->default_behavior_set == 0) {
263 +        weston_log("ivi-id-agent: Could not find configuration for application\n");
264 +        goto ivi_failed;
265 +
266 +    /* Default behavior for unknown applications */
267 +    } else if (ida->default_surface_id < ida->default_surface_id_max) {
268 +        weston_log("ivi-id-agent: No configuration for application adding to "
269 +                "default layer\n");
270 +
271 +        /*
272 +         * Check if ivi-shell application already created an application with
273 +         * desired surface_id
274 +         */
275 +        struct ivi_layout_surface *temp_layout_surf =
276 +                ida->interface->get_surface_from_id(
277 +                        ida->default_surface_id);
278 +        if ((temp_layout_surf != NULL) && (temp_layout_surf != layout_surface)) {
279 +            weston_log("ivi-id-agent: surface_id already used by an ivi-shell "
280 +                                "application\n");
281 +            goto ivi_failed;
282 +        }
283 +
284 +        ida->interface->surface_set_id(layout_surface,
285 +                                              ida->default_surface_id);
286 +        ida->default_surface_id++;
287 +
288 +    } else {
289 +        weston_log("ivi-id-agent: Interval for default surface_id generation "
290 +                "exceeded\n");
291 +        goto ivi_failed;
292 +    }
293 +
294 +    return IVI_SUCCEEDED;
295 +
296 +ivi_failed:
297 +    return IVI_FAILED;
298 +}
299 +
300 +static void
301 +desktop_surface_event_configure(struct wl_listener *listener,
302 +        void *data)
303 +{
304 +    struct ivi_id_agent *ida = wl_container_of(listener, ida,
305 +            desktop_surface_configured);
306 +
307 +    struct ivi_layout_surface *layout_surface =
308 +            (struct ivi_layout_surface *) data;
309 +
310 +    if (get_id(ida, layout_surface) == IVI_FAILED)
311 +        weston_log("ivi-id-agent: Could not create surface_id for application\n");
312 +}
313 +
314 +static void
315 +surface_event_remove(struct wl_listener *listener, void *data) {
316 +    struct ivi_id_agent *ida = wl_container_of(listener, ida,
317 +                surface_removed);
318 +    struct ivi_layout_surface *layout_surface =
319 +                (struct ivi_layout_surface *) data;
320 +    struct db_elem *db_elem = NULL;
321 +
322 +    wl_list_for_each(db_elem, &ida->app_list, link)
323 +    {
324 +        if(db_elem->layout_surface == layout_surface) {
325 +            db_elem->layout_surface = NULL;
326 +            break;
327 +        }
328 +    }
329 +}
330 +
331 +static int32_t deinit(struct ivi_id_agent *ida);
332 +
333 +static void
334 +id_agent_module_deinit(struct wl_listener *listener, void *data) {
335 +    (void)data;
336 +    struct ivi_id_agent *ida = wl_container_of(listener, ida, destroy_listener);
337 +
338 +    deinit(ida);
339 +}
340 +
341 +static int32_t
342 +check_config(struct db_elem *curr_db_elem, struct ivi_id_agent *ida)
343 +{
344 +    struct db_elem *db_elem;
345 +
346 +    if (ida->default_surface_id <= curr_db_elem->surface_id
347 +            && curr_db_elem->surface_id <= ida->default_surface_id_max) {
348 +        weston_log("ivi-id-agent: surface_id: %d in default id interval "
349 +                "[%d, %d] (CONFIG ERROR)\n", curr_db_elem->surface_id,
350 +                ida->default_surface_id, ida->default_surface_id_max);
351 +        goto ivi_failed;
352 +    }
353 +
354 +    wl_list_for_each(db_elem, &ida->app_list, link)
355 +    {
356 +        if(curr_db_elem == db_elem)
357 +            continue;
358 +
359 +        if (db_elem->surface_id == curr_db_elem->surface_id) {
360 +            weston_log("ivi-id-agent: Duplicate surface_id: %d (CONFIG ERROR)\n",
361 +                    curr_db_elem->surface_id);
362 +            goto ivi_failed;
363 +        }
364 +    }
365 +
366 +    return IVI_SUCCEEDED;
367 +
368 +ivi_failed:
369 +    return IVI_FAILED;
370 +}
371 +
372 +static int32_t
373 +read_config(struct ivi_id_agent *ida)
374 +{
375 +    struct weston_config *config = NULL;
376 +    struct weston_config_section *section = NULL;
377 +    const char *name = NULL;
378 +
379 +    config = wet_get_config(ida->compositor);
380 +    if (!config)
381 +        goto ivi_failed;
382 +
383 +    section = weston_config_get_section(config, "desktop-app-default", NULL,
384 +            NULL);
385 +
386 +    if (section) {
387 +        weston_log("ivi-id-agent: Default behavior for unknown applications is "
388 +                "set\n");
389 +        ida->default_behavior_set = 1;
390 +
391 +        weston_config_section_get_uint(section, "default-surface-id",
392 +                &ida->default_surface_id, INVALID_ID);
393 +        weston_config_section_get_uint(section, "default-surface-id-max",
394 +                &ida->default_surface_id_max, INVALID_ID);
395 +
396 +        if (ida->default_surface_id == INVALID_ID ||
397 +                ida->default_surface_id_max == INVALID_ID) {
398 +            weston_log("ivi-id-agent: Missing configuration for default "
399 +                    "behavior\n");
400 +            ida->default_behavior_set = 0;
401 +        }
402 +    } else {
403 +        ida->default_behavior_set = 0;
404 +    }
405 +
406 +    section = NULL;
407 +    while (weston_config_next_section(config, &section, &name)) {
408 +        struct db_elem *db_elem = NULL;
409 +
410 +        if (strcmp(name, "desktop-app") != 0)
411 +            continue;
412 +
413 +        db_elem = calloc(1, sizeof *db_elem);
414 +        if (db_elem == NULL) {
415 +            weston_log("ivi-id-agent: No memory to allocate\n");
416 +            goto ivi_failed;
417 +        }
418 +
419 +        wl_list_insert(&ida->app_list, &db_elem->link);
420 +
421 +        weston_config_section_get_uint(section, "surface-id",
422 +                         &db_elem->surface_id, INVALID_ID);
423 +
424 +        if (db_elem->surface_id == INVALID_ID) {
425 +            weston_log("ivi-id-agent: surface-id is not set in configuration\n");
426 +            goto ivi_failed;
427 +        }
428 +
429 +        weston_config_section_get_string(section, "app-id",
430 +                         &db_elem->cfg_app_id, NULL);
431 +        weston_config_section_get_string(section, "app-title",
432 +                         &db_elem->cfg_title, NULL);
433 +
434 +        if (db_elem->cfg_app_id == NULL && db_elem->cfg_title == NULL) {
435 +            weston_log("ivi-id-agent: Every parameter is NULL in app "
436 +                    "configuration\n");
437 +            goto ivi_failed;
438 +        }
439 +
440 +        if (check_config(db_elem, ida) == IVI_FAILED) {
441 +            weston_log("ivi-id-agent: No valid config found, deinit...\n");
442 +            goto ivi_failed;
443 +        }
444 +    }
445 +
446 +    if(ida->default_behavior_set == 0 && wl_list_empty(&ida->app_list)) {
447 +        weston_log("ivi-id-agent: No valid config found, deinit...\n");
448 +        goto ivi_failed;
449 +    }
450 +
451 +    return IVI_SUCCEEDED;
452 +
453 +ivi_failed:
454 +    return IVI_FAILED;
455 +}
456 +
457 +WL_EXPORT int32_t
458 +id_agent_module_init(struct weston_compositor *compositor,
459 +        const struct ivi_layout_interface *interface)
460 +{
461 +    struct ivi_id_agent *ida = NULL;
462 +
463 +    ida = calloc(1, sizeof *ida);
464 +    if (ida == NULL) {
465 +        weston_log("failed to allocate ivi_id_agent\n");
466 +        goto ivi_failed;
467 +    }
468 +
469 +    ida->compositor = compositor;
470 +    ida->interface = interface;
471 +    ida->desktop_surface_configured.notify = desktop_surface_event_configure;
472 +    ida->destroy_listener.notify = id_agent_module_deinit;
473 +    ida->surface_removed.notify = surface_event_remove;
474 +
475 +    wl_signal_add(&compositor->destroy_signal, &ida->destroy_listener);
476 +    ida->interface->add_listener_configure_desktop_surface(
477 +            &ida->desktop_surface_configured);
478 +    interface->add_listener_remove_surface(&ida->surface_removed);
479 +
480 +    wl_list_init(&ida->app_list);
481 +    if(read_config(ida) != 0) {
482 +        weston_log("ivi-id-agent: Read config failed\n");
483 +        deinit(ida);
484 +        goto ivi_failed;
485 +    }
486 +
487 +    return IVI_SUCCEEDED;
488 +
489 +ivi_failed:
490 +    return IVI_FAILED;
491 +}
492 +
493 +static int32_t
494 +deinit(struct ivi_id_agent *ida)
495 +{
496 +    struct db_elem *db_elem;
497 +    wl_list_for_each(db_elem, &ida->app_list, link) {
498 +        free(db_elem->cfg_app_id);
499 +        free(db_elem->cfg_title);
500 +        free(db_elem);
501 +    }
502 +
503 +    wl_list_remove(&ida->desktop_surface_configured.link);
504 +    wl_list_remove(&ida->destroy_listener.link);
505 +    wl_list_remove(&ida->surface_removed.link);
506 +    free(ida);
507 +
508 +    return IVI_SUCCEEDED;
509 +}
510 diff --git a/ivi-id-agent-modules/ivi-id-agent/weston.ini.in b/ivi-id-agent-modules/ivi-id-agent/weston.ini.in
511 new file mode 100644
512 index 0000000..48a196c
513 --- /dev/null
514 +++ b/ivi-id-agent-modules/ivi-id-agent/weston.ini.in
515 @@ -0,0 +1,20 @@
516 +[core]
517 +shell=ivi-shell.so
518 +require-input=false
519 +
520 +[ivi-shell]
521 +ivi-module=ivi-controller.so
522 +ivi-input-module=ivi-input-controller.so
523 +ivi-id-agent-module=ivi-id-agent.so
524 +
525 +[desktop-app]
526 +surface-id=111
527 +app-title=Flower
528 +
529 +[desktop-app]
530 +surface-id=251
531 +app-title=Flower
532 +
533 +[desktop-app-default]
534 +default-surface-id=2000000
535 +default-surface-id-max=2001000
536 -- 
537 2.7.4
538