summaryrefslogtreecommitdiffstats
path: root/src/compositor/wayland_wrapper/wlcompositor.cpp
blob: 545d14e3dba016f0ac353fe9ca810e3f35e1dc7a (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
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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt Compositor.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * 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.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** 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
** OWNER 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."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "wlcompositor.h"

#include "waylandinput.h"
#include "wldisplay.h"
#include "wlshmbuffer.h"
#include "wlsurface.h"
#include "waylandcompositor.h"
#include "wldatadevicemanager.h"
#include "wldatadevice.h"
#include "wlextendedoutput.h"
#include "wlextendedsurface.h"
#include "wlsubsurface.h"
#include "wlshellsurface.h"
#include "wltouch.h"
#include "wlinputdevice.h"

#include <QWindow>
#include <QSocketNotifier>
#include <QDebug>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/mman.h>
#include <sys/select.h>
#include <sys/time.h>

#include <wayland-server.h>

#include "hardware_integration/graphicshardwareintegration.h"
#include "waylandwindowmanagerintegration.h"

namespace Wayland {

static Compositor *compositor;

void compositor_create_surface(struct wl_client *client,
                               struct wl_resource *resource, uint32_t id)
{
     static_cast<Compositor *>(resource->data)->createSurface(client,id);
}

const static struct wl_compositor_interface compositor_interface = {
    compositor_create_surface
};

void Compositor::bind_func(struct wl_client *client, void *data,
                      uint32_t version, uint32_t id)
{
    Q_UNUSED(version);
    wl_client_add_object(client,&wl_compositor_interface, &compositor_interface, id,data);
}


Compositor *Compositor::instance()
{
    return compositor;
}

Compositor::Compositor(WaylandCompositor *qt_compositor)
    : m_display(new Display)
    , m_default_input_device(0)
    , m_shm(m_display)
    , m_current_frame(0)
    , m_last_queued_buf(-1)
    , m_qt_compositor(qt_compositor)
    , m_orientation(Qt::PrimaryOrientation)
    , m_directRenderSurface(0)
#if defined (QT_COMPOSITOR_WAYLAND_GL)
    , m_graphics_hw_integration(0)
#endif
    , m_outputExtension(0)
    , m_surfaceExtension(0)
    , m_subSurfaceExtension(0)
    , m_touchExtension(0)
    , m_retainNotify(0)
{
    compositor = this;
    qDebug() << "Compositor instance is" << this;

#if defined (QT_COMPOSITOR_WAYLAND_GL)
    QWindow *window = qt_compositor->window();
    if (window && window->surfaceType() != QWindow::RasterSurface)
        m_graphics_hw_integration = GraphicsHardwareIntegration::createGraphicsHardwareIntegration(qt_compositor);
#endif
    m_windowManagerIntegration = new WindowManagerServerIntegration(this);

    wl_display_add_global(m_display->handle(),&wl_compositor_interface,this,Compositor::bind_func);

    m_data_device_manager =  new DataDeviceManager(this);

    wl_display_add_global(m_display->handle(),&wl_output_interface, &m_output_global,OutputGlobal::output_bind_func);

    wl_display_add_global(m_display->handle(), &wl_shell_interface, &m_shell, Shell::bind_func);

    m_shell = new Shell();
    m_outputExtension = new OutputExtensionGlobal(this);
    m_surfaceExtension = new SurfaceExtensionGlobal(this);

    if (wl_display_add_socket(m_display->handle(), qt_compositor->socketName())) {
        fprintf(stderr, "Fatal: Failed to open server socket\n");
        exit(EXIT_FAILURE);
    }

    m_loop = wl_display_get_event_loop(m_display->handle());

    int fd = wl_event_loop_get_fd(m_loop);

    QSocketNotifier *sockNot = new QSocketNotifier(fd, QSocketNotifier::Read, this);
    connect(sockNot, SIGNAL(activated(int)), this, SLOT(processWaylandEvents()));

    //initialize distancefieldglyphcache here
}

Compositor::~Compositor()
{
    delete m_default_input_device;
    delete m_data_device_manager;

    delete m_display;
}

void Compositor::frameFinished(Surface *surface)
{
    if (surface && m_dirty_surfaces.contains(surface)) {
        m_dirty_surfaces.remove(surface);
        surface->sendFrameCallback();
    } else if (!surface) {
        QSet<Surface *> dirty = m_dirty_surfaces;
        m_dirty_surfaces.clear();
        foreach (Surface *surface, dirty)
            surface->sendFrameCallback();
    }
}

void Compositor::createSurface(struct wl_client *client, uint32_t id)
{
    Surface *surface = new Surface(client,id, this);
    printf("Compositor::createSurface: %p %d\n", client, id);

    m_surfaces << surface;

    m_qt_compositor->surfaceCreated(surface->waylandSurface());
}

struct wl_client *Compositor::getClientFromWinId(uint winId) const
{
    Surface *surface = getSurfaceFromWinId(winId);
    if (surface)
        return surface->base()->resource.client;

    return 0;
}

Surface *Compositor::getSurfaceFromWinId(uint winId) const
{
    foreach (Surface *surface, m_surfaces) {
        if (surface->id() == winId)
            return surface;
    }

    return 0;
}

QImage Compositor::image(uint winId) const
{
    printf("Compositor::image(%x)\n", winId);
    foreach (Surface *surface, m_surfaces) {
        if (surface->id() == winId) {
            return surface->image();
        }
    }

    return QImage();
}

uint Compositor::currentTimeMsecs()
{
    //### we throw away the time information
    struct timeval tv;
    int ret = gettimeofday(&tv, 0);
    if (ret == 0)
        return tv.tv_sec*1000 + tv.tv_usec/1000;
    return 0;
}

void Compositor::releaseBuffer(void *bufferHandle)
{
    struct wl_buffer *buffer = static_cast<struct wl_buffer*>(bufferHandle);
    if (buffer) {
        //qDebug() << "WL_BUFFER_RELEASE" << buffer<< buffer->resource.client;
        wl_resource_post_event(&buffer->resource, WL_BUFFER_RELEASE);
    }

}

void Compositor::processWaylandEvents()
{
    int ret = wl_event_loop_dispatch(m_loop, 0);
    if (ret)
        fprintf(stderr, "wl_event_loop_dispatch error: %d\n", ret);
}

void Compositor::surfaceDestroyed(Surface *surface)
{
    if (defaultInputDevice()->mouseFocus() == surface)
        defaultInputDevice()->setMouseFocus(0, QPoint(), QPoint());
    m_surfaces.removeOne(surface);
    m_dirty_surfaces.remove(surface);
    if (m_directRenderSurface == surface)
        setDirectRenderSurface(0);
}

void Compositor::markSurfaceAsDirty(Wayland::Surface *surface)
{
    m_dirty_surfaces.insert(surface);
}

void Compositor::destroyClientForSurface(Surface *surface)
{
    wl_client *client = surface->base()->resource.client;

    if (client) {
        m_windowManagerIntegration->removeClient(client);
        wl_client_destroy(client);
    }
}

QWindow *Compositor::window() const
{
    return m_qt_compositor->window();
}

GraphicsHardwareIntegration * Compositor::graphicsHWIntegration() const
{
#ifdef QT_COMPOSITOR_WAYLAND_GL
    return m_graphics_hw_integration;
#else
    return 0;
#endif
}

void Compositor::initializeHardwareIntegration()
{
#ifdef QT_COMPOSITOR_WAYLAND_GL
    if (m_graphics_hw_integration)
        m_graphics_hw_integration->initializeHardware(m_display);
#endif
}

void Compositor::initializeDefaultInputDevice()
{
    WaylandInputDevice *defaultInput = new WaylandInputDevice(m_qt_compositor);
    m_default_input_device = defaultInput->handle();
}

void Compositor::initializeWindowManagerProtocol()
{
    m_windowManagerIntegration->initialize(m_display);
}

void Compositor::enableSubSurfaceExtension()
{
    if (!m_subSurfaceExtension) {
        m_subSurfaceExtension = new SubSurfaceExtensionGlobal(this);
    }
}

bool Compositor::setDirectRenderSurface(Surface *surface)
{
#ifdef QT_COMPOSITOR_WAYLAND_GL
    if (m_graphics_hw_integration && m_graphics_hw_integration->setDirectRenderSurface(surface ? surface->waylandSurface() : 0)) {
        m_directRenderSurface = surface;
        return true;
    }
#else
    Q_UNUSED(surface);
#endif
    return false;
}

QList<struct wl_client *> Compositor::clients() const
{
    QList<struct wl_client *> list;
    foreach (Surface *surface, m_surfaces) {
        struct wl_client *client = surface->base()->resource.client;
        if (!list.contains(client))
            list.append(client);
    }
    return list;
}

void Compositor::setScreenOrientation(Qt::ScreenOrientation orientation)
{
    m_orientation = orientation;

    QList<struct wl_client*> clientList = clients();
    for (int i = 0; i < clientList.length(); ++i) {
        struct wl_client *client = clientList.at(i);
        Output *output = m_output_global.outputForClient(client);
        Q_ASSERT(output);
        if (output->extendedOutput()){
            output->extendedOutput()->sendOutputOrientation(orientation);
        }
    }
}

Qt::ScreenOrientation Compositor::screenOrientation() const
{
    return m_orientation;
}

void Compositor::setOutputGeometry(const QRect &geometry)
{
    m_output_global.setGeometry(geometry);
}

InputDevice* Compositor::defaultInputDevice()
{
    return m_default_input_device;
}

QList<Wayland::Surface *> Compositor::surfacesForClient(wl_client *client)
{
    QList<Wayland::Surface *> ret;

    for (int i=0; i < m_surfaces.count(); ++i) {
        if (m_surfaces.at(i)->base()->resource.client == client) {
            ret.append(m_surfaces.at(i));
        }
    }
    return ret;
}

void Compositor::enableTouchExtension()
{
    if (!m_touchExtension) {
        m_touchExtension = new TouchExtensionGlobal(this);
    }
}

void Compositor::configureTouchExtension(int flags)
{
    if (m_touchExtension)
        m_touchExtension->setFlags(flags);
}

void Compositor::setRetainedSelectionWatcher(RetainedSelectionFunc func, void *param)
{
    m_retainNotify = func;
    m_retainNotifyParam = param;
}

bool Compositor::wantsRetainedSelection() const
{
    return m_retainNotify != 0;
}

void Compositor::feedRetainedSelectionData(QMimeData *data)
{
    if (m_retainNotify) {
        m_retainNotify(data, m_retainNotifyParam);
    }
}

void Compositor::overrideSelection(QMimeData *data)
{
    m_data_device_manager->overrideSelection(*data);
}

bool Compositor::isDragging() const
{
    return false;
}

void Compositor::sendDragMoveEvent(const QPoint &global, const QPoint &local,
                                            Surface *surface)
{
    Q_UNUSED(global);
    Q_UNUSED(local);
    Q_UNUSED(surface);
//    Drag::instance()->dragMove(global, local, surface);
}

void Compositor::sendDragEndEvent()
{
//    Drag::instance()->dragEnd();
}

} // namespace Wayland