File: WPEToplevel.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (549 lines) | stat: -rw-r--r-- 16,483 bytes parent folder | download
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 * Copyright (C) 2024 Igalia S.L.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WPEToplevel.h"

#include "WPEBufferDMABufFormats.h"
#include "WPEDisplay.h"
#include "WPEViewPrivate.h"
#include <wtf/HashSet.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GWeakPtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/WTFString.h>

#if USE(LIBDRM)
#include <drm_fourcc.h>
#endif

/**
 * WPEToplevel:
 *
 */
struct _WPEToplevelPrivate {
    GWeakPtr<WPEDisplay> display;
    HashSet<WPEView*> views;

    int width;
    int height;
    gdouble scale { 1 };
    WPEToplevelState state;
    bool closed;
#if USE(LIBDRM)
    GRefPtr<WPEBufferDMABufFormats> overridenDMABufFormats;
#endif
};

WEBKIT_DEFINE_TYPE(WPEToplevel, wpe_toplevel, G_TYPE_OBJECT)

enum {
    PROP_0,

    PROP_DISPLAY,

    N_PROPERTIES
};

static std::array<GParamSpec*, N_PROPERTIES> sObjProperties;

static void wpeToplevelSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
{
    auto* toplevel = WPE_TOPLEVEL(object);

    switch (propId) {
    case PROP_DISPLAY:
        toplevel->priv->display.reset(WPE_DISPLAY(g_value_get_object(value)));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}

static void wpeToplevelGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
{
    auto* toplevel = WPE_TOPLEVEL(object);

    switch (propId) {
    case PROP_DISPLAY:
        g_value_set_object(value, wpe_toplevel_get_display(toplevel));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
    }
}

static void wpe_toplevel_class_init(WPEToplevelClass* toplevelClass)
{
    GObjectClass* objectClass = G_OBJECT_CLASS(toplevelClass);
    objectClass->set_property = wpeToplevelSetProperty;
    objectClass->get_property = wpeToplevelGetProperty;

    /**
     * WPEToplevel:display:
     *
     * The #WPEDisplay of the toplevel.
     */
    sObjProperties[PROP_DISPLAY] =
        g_param_spec_object(
            "display",
            nullptr, nullptr,
            WPE_TYPE_DISPLAY,
            static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    g_object_class_install_properties(objectClass, N_PROPERTIES, sObjProperties.data());
}

void wpeToplevelAddView(WPEToplevel* toplevel, WPEView* view)
{
    toplevel->priv->views.add(view);
}

void wpeToplevelRemoveView(WPEToplevel* toplevel, WPEView* view)
{
    toplevel->priv->views.remove(view);
}

/**
 * wpe_toplevel_get_display:
 * @toplevel: a #WPEToplevel
 *
 * Get the #WPEDisplay of @toplevel
 *
 * Returns: (transfer none) (nullable): a #WPEDisplay or %NULL
 */
WPEDisplay* wpe_toplevel_get_display(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), nullptr);

    return toplevel->priv->display.get();
}

/**
 * wpe_toplevel_set_title:
 * @toplevel: a #WPEToplevel
 * @title: (nullable): title to set, or %NULL
 *
 * Set the @toplevel title
 */
void wpe_toplevel_set_title(WPEToplevel* toplevel, const char* title)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    if (toplevelClass->set_title)
        toplevelClass->set_title(toplevel, title);
}

/**
 * wpe_toplevel_get_max_views:
 * @toplevel: a #WPEToplevel
 *
 * Get the maximum number of #WPEView that @toplevel can contain.
 *
 * Returns: the maximum number of views supported.
 */
guint wpe_toplevel_get_max_views(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), 0);

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->get_max_views ? toplevelClass->get_max_views(toplevel) : 1;
}

/**
 * wpe_toplevel_get_n_views:
 * @toplevel: a #WPEToplevel
 *
 * Get the number of #WPEView contained by @toplevel
 *
 * Returns: the number of view in @toplevel
 */
guint wpe_toplevel_get_n_views(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), 0);

    return toplevel->priv->views.size();
}

/**
 * wpe_toplevel_foreach_view:
 * @toplevel: a #WPEToplevel
 * @func: (scope call): the function to call for each #WPEView
 * @user_data: user data to pass to the function
 *
 * Call @func for each #WPEView of @toplevel
 */
void wpe_toplevel_foreach_view(WPEToplevel* toplevel, WPEToplevelForeachViewFunc func, gpointer userData)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    for (auto& view : copyToVectorOf<GRefPtr<WPEView>>(toplevel->priv->views)) {
        if (func(toplevel, view.get(), userData))
            return;
    }
}

/**
 * wpe_toplevel_closed:
 * @toplevel: a #WPEToplevel
 *
 * Set @toplevel as closed if not already closed.
 *
 * This function should only be called by #WPEToplevel derived classes
 * in platform implementations.
 */
void wpe_toplevel_closed(WPEToplevel* toplevel)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    if (toplevel->priv->closed)
        return;

    toplevel->priv->closed = true;
    for (auto& view : copyToVectorOf<GRefPtr<WPEView>>(toplevel->priv->views))
        wpe_view_closed(view.get());
}

/**
 * wpe_toplevel_get_size:
 * @toplevel: a #WPEToplevel
 * @width: (out) (nullable): return location for width, or %NULL
 * @height: (out) (nullable): return location for width, or %NULL
 *
 * Get the @vtoplevel size in logical coordinates
 */
void wpe_toplevel_get_size(WPEToplevel* toplevel, int* width, int* height)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    if (width)
        *width = toplevel->priv->width;
    if (height)
        *height = toplevel->priv->height;
}

/**
 * wpe_toplevel_resize:
 * @toplevel: a #WPEToplevel
 * @width: width in logical coordinates
 * @height: height in logical coordinates
 *
 * Request that the @toplevel is resized at @width x @height.
 *
 * Returns: %TRUE if resizing is supported and given dimensions are
 *    different than current size, otherwise %FALSE
 */
gboolean wpe_toplevel_resize(WPEToplevel* toplevel, int width, int height)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), FALSE);

    auto* priv = toplevel->priv;
    if (priv->width == width && priv->height == height)
        return FALSE;

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->resize ? toplevelClass->resize(toplevel, width, height) : FALSE;
}

/**
 * wpe_toplevel_resized:
 * @toplevel: a #WPEToplevel
 * @width: width in logical coordinates
 * @height: height in logical coordinates
 *
 * Update @toplevel size.
 *
 * This function should only be called by #WPEToplevel derived classes
 * in platform implementations.
 */
void wpe_toplevel_resized(WPEToplevel* toplevel, int width, int height)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    auto* priv = toplevel->priv;
    priv->width = width;
    priv->height = height;
}

/**
 * wpe_toplevel_get_state:
 * @toplevel: a #WPEToplevel
 *
 * Get the current state of @toplevel
 *
 * Returns: a #WPEToplevelState
 */
WPEToplevelState wpe_toplevel_get_state(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), WPE_TOPLEVEL_STATE_NONE);

    return toplevel->priv->state;
}

/**
 * wpe_toplevel_state_changed:
 * @toplevel: a #WPEToplevel
 * @state: a set of #WPEToplevelState
 *
 * Update the current state of @toplevel
 *
 * This function should only be called by #WPEToplevel derived classes
 * in platform implementations.
 */
void wpe_toplevel_state_changed(WPEToplevel* toplevel, WPEToplevelState state)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    if (toplevel->priv->state == state)
        return;

    toplevel->priv->state = state;
    for (auto& view : copyToVectorOf<GRefPtr<WPEView>>(toplevel->priv->views))
        wpeViewToplevelStateChanged(view.get(), state);
}

/**
 * wpe_toplevel_get_scale:
 * @toplevel: a #WPEToplevel
 *
 * Get the @toplevel scale
 *
 * Returns: the toplevel scale
 */
gdouble wpe_toplevel_get_scale(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), 1.);

    return toplevel->priv->scale;
}

/**
 * wpe_toplevel_scale_changed:
 * @toplevel: a #WPEToplevel
 * @scale: the new scale
 *
 * Update the @toplevel scale.
 *
 * This function should only be called by #WPEToplevel derived classes
 * in platform implementations.
 */
void wpe_toplevel_scale_changed(WPEToplevel* toplevel, gdouble scale)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));
    g_return_if_fail(scale > 0);

    if (toplevel->priv->scale == scale)
        return;

    toplevel->priv->scale = scale;
    for (auto& view : copyToVectorOf<GRefPtr<WPEView>>(toplevel->priv->views))
        wpeViewScaleChanged(view.get(), scale);
}

/**
 * wpe_toplevel_get_screen:
 * @toplevel: a #WPEToplevel
 *
 * Get current #WPEScreen of @toplevel
 *
 * Returns: (transfer none) (nullable): a #WPEScreen, or %NULL
 */
WPEScreen* wpe_toplevel_get_screen(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), nullptr);

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->get_screen ? toplevelClass->get_screen(toplevel) : nullptr;
}

/**
 * wpe_toplevel_screen_changed:
 * @toplevel: a #WPEToplevel
 *
 * Notify that @toplevel screen has changed.
 *
 * This function should only be called by #WPEToplevel derived classes
 * in platform implementations.
 */
void wpe_toplevel_screen_changed(WPEToplevel* toplevel)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    for (auto& view : copyToVectorOf<GRefPtr<WPEView>>(toplevel->priv->views))
        wpeViewScreenChanged(view.get());
}

/**
 * wpe_toplevel_fullscreen:
 * @toplevel: a #WPEToplevel
 *
 * Request that the @toplevel goes into a fullscreen state.
 *
 * Returns: %TRUE if fullscreen is supported, otherwise %FALSE
 */
gboolean wpe_toplevel_fullscreen(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), FALSE);

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->set_fullscreen ? toplevelClass->set_fullscreen(toplevel, TRUE) : FALSE;
}

/**
 * wpe_toplevel_unfullscreen:
 * @toplevel: a #WPEToplevel
 *
 * Request that the @toplevel leaves a fullscreen state.
 *
 * Returns: %TRUE if unfullscreen is supported, otherwise %FALSE
 */
gboolean wpe_toplevel_unfullscreen(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), FALSE);

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->set_fullscreen ? toplevelClass->set_fullscreen(toplevel, FALSE) : FALSE;
}

/**
 * wpe_toplevel_maximize:
 * @toplevel: a #WPEToplevel
 *
 * Request that the @toplevel is maximized. If the toplevel is already maximized this function
 * does nothing.
 *
 * Returns: %TRUE if maximize is supported, otherwise %FALSE
 */
gboolean wpe_toplevel_maximize(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), FALSE);

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->set_maximized ? toplevelClass->set_maximized(toplevel, TRUE) : FALSE;
}

/**
 * wpe_toplevel_unmaximize:
 * @toplevel: a #WPEToplevel
 *
 * Request that the @toplevel is unmaximized. If the toplevel is not maximized this function
 * does nothing.
 *
 * Returns: %TRUE if maximize is supported, otherwise %FALSE
 */
gboolean wpe_toplevel_unmaximize(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), FALSE);

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->set_maximized ? toplevelClass->set_maximized(toplevel, FALSE) : FALSE;
}

/**
 * wpe_toplevel_minimize:
 * @toplevel: a #WPEToplevel
 *
 * Request that the @toplevel is minimized.
 *
 * Returns: %TRUE if minimize is supported, otherwise %FALSE
 */
gboolean wpe_toplevel_minimize(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), FALSE);

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    return toplevelClass->set_minimized ? toplevelClass->set_minimized(toplevel) : FALSE;
}

/**
 * wpe_toplevel_get_preferred_dma_buf_formats:
 * @toplevel: a #WPEToplevel
 *
 * Get the list of preferred DMA-BUF buffer formats for @toplevel.
 *
 * Returns: (transfer none) (nullable): a #WPEBufferDMABufFormats
 */
WPEBufferDMABufFormats* wpe_toplevel_get_preferred_dma_buf_formats(WPEToplevel* toplevel)
{
    g_return_val_if_fail(WPE_IS_TOPLEVEL(toplevel), nullptr);

    auto* priv = toplevel->priv;
#if USE(LIBDRM)
    if (priv->overridenDMABufFormats)
        return priv->overridenDMABufFormats.get();

    const char* formatString = getenv("WPE_DMABUF_BUFFER_FORMAT");
    if (formatString && *formatString) {
        auto tokens = String::fromUTF8(formatString).split(':');
        if (!tokens.isEmpty() && tokens[0].length() >= 2 && tokens[0].length() <= 4) {
            guint32 format = fourcc_code(tokens[0][0], tokens[0][1], tokens[0].length() > 2 ? tokens[0][2] : ' ', tokens[0].length() > 3 ? tokens[0][3] : ' ');
            char* endptr = nullptr;
            guint64 modifier = tokens.size() > 1 ? g_ascii_strtoull(tokens[1].ascii().data(), &endptr, 16) : DRM_FORMAT_MOD_INVALID;
            if (!(modifier == G_MAXUINT64 && errno == ERANGE) && !(!modifier && !endptr)) {
                WPEBufferDMABufFormatUsage usage = WPE_BUFFER_DMA_BUF_FORMAT_USAGE_RENDERING;
                if (tokens.size() > 2) {
                    if (tokens[2] == "rendering"_s)
                        usage = WPE_BUFFER_DMA_BUF_FORMAT_USAGE_RENDERING;
                    else if (tokens[2] == "mapping"_s)
                        usage = WPE_BUFFER_DMA_BUF_FORMAT_USAGE_MAPPING;
                    else if (tokens[2] == "scanout"_s)
                        usage = WPE_BUFFER_DMA_BUF_FORMAT_USAGE_SCANOUT;
                }
                auto* builder = wpe_buffer_dma_buf_formats_builder_new(priv->display ? wpe_display_get_drm_render_node(priv->display.get()) : nullptr);
                wpe_buffer_dma_buf_formats_builder_append_group(builder, nullptr, usage);
                wpe_buffer_dma_buf_formats_builder_append_format(builder, format, modifier);
                priv->overridenDMABufFormats = adoptGRef(wpe_buffer_dma_buf_formats_builder_end(builder));
                return priv->overridenDMABufFormats.get();
            }
        }

        WTFLogAlways("Invalid format %s set in WPE_DMABUF_BUFFER_FORMAT, ignoring...", formatString);
    }
#endif

    auto* toplevelClass = WPE_TOPLEVEL_GET_CLASS(toplevel);
    if (toplevelClass->get_preferred_dma_buf_formats)
        return toplevelClass->get_preferred_dma_buf_formats(toplevel);

    return priv->display ? wpe_display_get_preferred_dma_buf_formats(priv->display.get()) : nullptr;
}

/**
 * wpe_toplevel_preferred_dma_buf_formats_changed:
 * @toplevel: a #WPEToplevel
 *
 * Notify that @toplevel preferred DMA-BUF formats have changed.
 *
 * This function should only be called by #WPEToplevel derived classes
 * in platform implementations.
 */
void wpe_toplevel_preferred_dma_buf_formats_changed(WPEToplevel* toplevel)
{
    g_return_if_fail(WPE_IS_TOPLEVEL(toplevel));

    for (auto& view : copyToVectorOf<GRefPtr<WPEView>>(toplevel->priv->views))
        wpeViewPreferredDMABufFormatsChanged(view.get());
}