summaryrefslogtreecommitdiff
path: root/ivi-layermanagement-examples/layer-add-surfaces/src/layer-add-surfaces.c
blob: a271da2c2102e242ad93313e9c83ca1a11b2a6a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2015 Advanced Driver Information Technology Joint Venture GmbH
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <pthread.h>
#include <getopt.h>

#include "ilm_control.h"

t_ilm_uint screenWidth;
t_ilm_uint screenHeight;
static t_ilm_uint layer = 0;
pthread_mutex_t mutex;
static pthread_cond_t  waiterVariable = PTHREAD_COND_INITIALIZER;
static int number_of_surfaces = 0;
char display_name[256] = {0};

static void configure_ilm_surface(t_ilm_uint id, t_ilm_uint width, t_ilm_uint height)
{
    ilm_surfaceSetDestinationRectangle(id, 0, 0, width, height);
    ilm_surfaceSetSourceRectangle(id, 0, 0, width, height);
    ilm_surfaceSetVisibility(id, ILM_TRUE);
    ilm_layerAddSurface(layer,id);
    ilm_surfaceRemoveNotification(id);

    ilm_commitChanges();
    pthread_cond_signal( &waiterVariable );

    printf("layer-add-surfaces: surface (%u) configured with:\n"
           "    dst region: x:0 y:0 w:%u h:%u\n"
           "    src region: x:0 y:0 w:%u h:%u\n"
           "    visibility: TRUE\n"
           "    added to layer (%u)\n", id, width, height, width, height,layer);
}

static void surfaceCallbackFunction(t_ilm_uint id, struct ilmSurfaceProperties* sp, t_ilm_notification_mask m)
{
    if ((unsigned)m & ILM_NOTIFICATION_CONFIGURED)
    {
        configure_ilm_surface(id, sp->origSourceWidth, sp->origSourceHeight);
    }
}

static void callbackFunction(ilmObjectType object, t_ilm_uint id, t_ilm_bool created, void *user_data)
{
    (void)user_data;
    struct ilmSurfaceProperties sp;

    if (object == ILM_SURFACE) {
        if (created) {
            if (number_of_surfaces > 0) {
                number_of_surfaces--;
                printf("layer-add-surfaces: surface (%d) created\n",id);
                // always get configured event to follow the surface changings
                ilm_surfaceAddNotification(id,&surfaceCallbackFunction);
                ilm_commitChanges();
                ilm_getPropertiesOfSurface(id, &sp);

                if ((sp.origSourceWidth != 0) && (sp.origSourceHeight !=0))
                {   // surface is already configured
                    configure_ilm_surface(id, sp.origSourceWidth, sp.origSourceHeight);
                }
            }
        }
        else if(!created)
            printf("layer-add-surfaces: surface (%u) destroyed\n",id);
    } else if (object == ILM_LAYER) {
        if (created)
            printf("layer-add-surfaces: layer (%u) created\n",id);
        else if(!created)
            printf("layer-add-surfaces: layer (%u) destroyed\n",id);
    }
}

static void shutdownCallbackFunction(t_ilm_shutdown_error_type error_type,
                                     int errornum,
                                     void *user_data)
{
    (void) user_data;

    switch (error_type) {
        case ILM_ERROR_WAYLAND:
        {
            printf("layer-add-surfaces: exit, ilm shutdown due to wayland error: %s\n",
                   strerror(errornum));
            break;
        }
        case ILM_ERROR_POLL:
        {
            printf("layer-add-surfaces: exit, ilm shutdown due to poll error: %s\n",
                   strerror(errornum));
            break;
        }
        default:
        {
            printf("layer-add-surfaces: exit, ilm shutdown due to unknown reason: %s\n",
                   strerror(errornum));
        }
    }

    exit(1);
}

/* Choose the display with the largest resolution.*/
static t_ilm_uint choose_screen(void)
{
    struct ilmScreenProperties screenProperties;
    t_ilm_uint* screen_IDs = NULL;
    t_ilm_uint screen_ID = 0;
    t_ilm_uint screen_count = 0;
    t_ilm_uint choosen_width = 0;
    t_ilm_uint choosen_height = 0;
    t_ilm_uint i;

    ilm_getScreenIDs(&screen_count, &screen_IDs);

    for (i = 0; i<screen_count; i++)
    {
        ilm_getPropertiesOfScreen(screen_IDs[i], &screenProperties);
        if (!strcmp(screenProperties.connectorName, display_name)) {
            choosen_width = screenProperties.screenWidth;
            choosen_height = screenProperties.screenHeight;
            screen_ID = screen_IDs[i];
            break;
        }
        else if (screenProperties.screenWidth > choosen_width) {
            choosen_width = screenProperties.screenWidth;
            choosen_height = screenProperties.screenHeight;
            screen_ID = screen_IDs[i];
        }
    }

    screenWidth = choosen_width;
    screenHeight = choosen_height;

    free(screen_IDs);

    return screen_ID;
}

static int
usage(int ret)
{
    fprintf(stderr, "    -h,  --help                  display this help and exit.\n"
                    "    -d,  --display-name          name of the display which will be used,\n"
                    "                                 e.g.: HDMI-A-1, LVDS1\n"
                    "                                 If it is not set, display with highest resolution is used.\n"
                    "    -l,  --layer-id              id of the used ILM layer. It has to be set\n"
                    "    -s,  --surface-count         number of surfaces which will be added to\n"
                    "                                 the layer. It has to be set\n");
    exit(ret);
}

void parse_options(int argc, char *argv[])
{
    int opt;
    static const struct option options[] = {
        { "help",              no_argument, NULL, 'h' },
        { "layer-id",              required_argument, 0, 'l' },
        { "surface-count",           required_argument, 0, 's' },
        { "display-name", required_argument, NULL, 'd' },
        { 0,                   0,           NULL, 0 }
    };

    while (1) {
        opt = getopt_long(argc, argv, "hl:s:d:", options, NULL);

        if (opt == -1)
            break;

        switch (opt) {
            case 'h':
                usage(0);
                break;
            case 'l':
                layer = atoi(optarg);
                break;
            case 's':
                number_of_surfaces = atoi(optarg);
                break;
            case 'd':
                strcpy(display_name, optarg);
                break;
            default:
                usage(-1);
                break;
        }
    }

    printf("layer-add-surfaces: layer (%u) on display (%s) created, waiting for %d surfaces ...\n",
               layer,
               display_name,
               number_of_surfaces);
}

int main (int argc, char *argv[])
{
    // Get command-line options
    if ( argc < 3) {
        usage(-1);
    }

    // Check the first character of the first parameter
    if (!strncmp(argv[1], "-", 1)) {
        parse_options(argc, argv);
    } else {
        layer = strtol(argv[1], NULL, 0);
        number_of_surfaces = strtol(argv[2], NULL, 0);
    }

    if (!number_of_surfaces || !layer)
        usage(-1);

    pthread_mutexattr_t a;
    if (pthread_mutexattr_init(&a) != 0)
    {
       return -1;
    }

    if (pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE) != 0)
    {
       pthread_mutexattr_destroy(&a);
       return -1;
    }

    if (pthread_mutex_init(&mutex, &a) != 0)
    {
        pthread_mutexattr_destroy(&a);
        fprintf(stderr, "layer-add-surfaces: failed to initialize pthread_mutex\n");
        return -1;
    }

    pthread_mutexattr_destroy(&a);

    t_ilm_layer renderOrder[1];
    t_ilm_uint screen_ID;
    renderOrder[0] = layer;
    if (ilm_init() == ILM_FAILED) {
        fprintf(stderr, "layer-add-surfaces: ilm_init failed\n");
        return -1;
    }

    ilm_registerShutdownNotification(shutdownCallbackFunction, NULL);

    screen_ID = choose_screen();
    ilm_layerCreateWithDimension(&layer, screenWidth, screenHeight);
    printf("layer-add-surfaces: layer (%d) destination region: x:0 y:0 w:%u h:%u\n", layer, screenWidth, screenHeight);
    ilm_layerSetVisibility(layer,ILM_TRUE);
    printf("layer-add-surfaces: layer (%d) visibility TRUE\n", layer);
    ilm_displaySetRenderOrder(screen_ID, renderOrder, 1);
    ilm_commitChanges();
    ilm_registerNotification(callbackFunction, NULL);

    while (number_of_surfaces > 0) {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait( &waiterVariable, &mutex);
    }

    ilm_unregisterNotification();
    ilm_destroy();

    return 0;
}