Adapt to _qpa file rename in qtbase.
[profile/ivi/qtwayland.git] / src / compositor / wayland_wrapper / wlcompositor.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the Qt Compositor.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "wlcompositor.h"
42
43 #include "waylandinput.h"
44 #include "wldisplay.h"
45 #include "wlsurface.h"
46 #include "waylandcompositor.h"
47 #include "wldatadevicemanager.h"
48 #include "wldatadevice.h"
49 #include "wlextendedoutput.h"
50 #include "wlextendedsurface.h"
51 #include "wlsubsurface.h"
52 #include "wlshellsurface.h"
53 #include "wltouch.h"
54 #include "wlqtkey.h"
55 #include "wlinputdevice.h"
56 #include "wlregion.h"
57
58 #include <QWindow>
59 #include <QSocketNotifier>
60 #include <QScreen>
61 #include <qpa/qplatformscreen.h>
62 #include <QGuiApplication>
63 #include <qpa/qplatformscreenpageflipper.h>
64 #include <QDebug>
65
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <stddef.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <unistd.h>
73
74 #include <sys/mman.h>
75 #include <sys/select.h>
76 #include <sys/time.h>
77
78 #include <wayland-server.h>
79
80 #include "hardware_integration/graphicshardwareintegration.h"
81 #include "waylandwindowmanagerintegration.h"
82
83 namespace Wayland {
84
85 static Compositor *compositor;
86
87 void compositor_create_surface(struct wl_client *client,
88                                struct wl_resource *resource, uint32_t id)
89 {
90      static_cast<Compositor *>(resource->data)->createSurface(client,id);
91 }
92
93 void compositor_create_region(struct wl_client *client,
94                               struct wl_resource *compositor, uint32_t id)
95 {
96     Q_UNUSED(compositor);
97     new Region(client, id);
98 }
99
100 const static struct wl_compositor_interface compositor_interface = {
101     compositor_create_surface,
102     compositor_create_region
103 };
104
105 void Compositor::bind_func(struct wl_client *client, void *data,
106                       uint32_t version, uint32_t id)
107 {
108     Q_UNUSED(version);
109     wl_client_add_object(client,&wl_compositor_interface, &compositor_interface, id,data);
110 }
111
112
113 Compositor *Compositor::instance()
114 {
115     return compositor;
116 }
117
118 Compositor::Compositor(WaylandCompositor *qt_compositor)
119     : m_display(new Display)
120     , m_default_input_device(0)
121     , m_pageFlipper(0)
122     , m_current_frame(0)
123     , m_last_queued_buf(-1)
124     , m_qt_compositor(qt_compositor)
125     , m_orientation(Qt::PrimaryOrientation)
126     , m_directRenderSurface(0)
127     , m_directRenderContext(0)
128 #if defined (QT_COMPOSITOR_WAYLAND_GL)
129     , m_graphics_hw_integration(0)
130 #endif
131     , m_outputExtension(0)
132     , m_surfaceExtension(0)
133     , m_subSurfaceExtension(0)
134     , m_touchExtension(0)
135     , m_retainNotify(0)
136 {
137     compositor = this;
138
139 #if defined (QT_COMPOSITOR_WAYLAND_GL)
140     QWindow *window = qt_compositor->window();
141     if (window && window->surfaceType() != QWindow::RasterSurface)
142         m_graphics_hw_integration = GraphicsHardwareIntegration::createGraphicsHardwareIntegration(qt_compositor);
143 #endif
144     m_windowManagerIntegration = new WindowManagerServerIntegration(qt_compositor, this);
145
146     wl_display_add_global(m_display->handle(),&wl_compositor_interface,this,Compositor::bind_func);
147
148     m_data_device_manager =  new DataDeviceManager(this);
149
150     wl_display_add_global(m_display->handle(),&wl_output_interface, &m_output_global,OutputGlobal::output_bind_func);
151
152     m_shell = new Shell();
153     wl_display_add_global(m_display->handle(), &wl_shell_interface, m_shell, Shell::bind_func);
154
155     wl_display_init_shm(m_display->handle());
156
157     m_outputExtension = new OutputExtensionGlobal(this);
158     m_surfaceExtension = new SurfaceExtensionGlobal(this);
159     m_qtkeyExtension = new QtKeyExtensionGlobal(this);
160     m_touchExtension = new TouchExtensionGlobal(this);
161
162     if (wl_display_add_socket(m_display->handle(), qt_compositor->socketName())) {
163         fprintf(stderr, "Fatal: Failed to open server socket\n");
164         exit(EXIT_FAILURE);
165     }
166
167     m_loop = wl_display_get_event_loop(m_display->handle());
168
169     int fd = wl_event_loop_get_fd(m_loop);
170
171     QSocketNotifier *sockNot = new QSocketNotifier(fd, QSocketNotifier::Read, this);
172     connect(sockNot, SIGNAL(activated(int)), this, SLOT(processWaylandEvents()));
173
174     qRegisterMetaType<SurfaceBuffer*>("SurfaceBuffer*");
175     //initialize distancefieldglyphcache here
176 }
177
178 Compositor::~Compositor()
179 {
180     delete m_shell;
181     delete m_outputExtension;
182     delete m_surfaceExtension;
183     delete m_subSurfaceExtension;
184     delete m_touchExtension;
185     delete m_qtkeyExtension;
186
187     delete m_default_wayland_input_device;
188     delete m_data_device_manager;
189
190 #ifdef QT_COMPOSITOR_WAYLAND_GL
191     delete m_graphics_hw_integration;
192 #endif
193
194     delete m_display;
195 }
196
197 void Compositor::frameFinished(Surface *surface)
198 {
199     if (surface && m_dirty_surfaces.contains(surface)) {
200         m_dirty_surfaces.remove(surface);
201         surface->sendFrameCallback();
202     } else if (!surface) {
203         QSet<Surface *> dirty = m_dirty_surfaces;
204         m_dirty_surfaces.clear();
205         foreach (Surface *surface, dirty)
206             surface->sendFrameCallback();
207     }
208 }
209
210 void Compositor::createSurface(struct wl_client *client, uint32_t id)
211 {
212     Surface *surface = new Surface(client,id, this);
213
214     m_surfaces << surface;
215
216     m_qt_compositor->surfaceCreated(surface->waylandSurface());
217 }
218
219 struct wl_client *Compositor::getClientFromWinId(uint winId) const
220 {
221     Surface *surface = getSurfaceFromWinId(winId);
222     if (surface)
223         return surface->base()->resource.client;
224
225     return 0;
226 }
227
228 Surface *Compositor::getSurfaceFromWinId(uint winId) const
229 {
230     foreach (Surface *surface, m_surfaces) {
231         if (surface->id() == winId)
232             return surface;
233     }
234
235     return 0;
236 }
237
238 QImage Compositor::image(uint winId) const
239 {
240     foreach (Surface *surface, m_surfaces) {
241         if (surface->id() == winId) {
242             return surface->image();
243         }
244     }
245
246     return QImage();
247 }
248
249 uint Compositor::currentTimeMsecs()
250 {
251     //### we throw away the time information
252     struct timeval tv;
253     int ret = gettimeofday(&tv, 0);
254     if (ret == 0)
255         return tv.tv_sec*1000 + tv.tv_usec/1000;
256     return 0;
257 }
258
259 void Compositor::releaseBuffer(SurfaceBuffer *screenBuffer)
260 {
261     screenBuffer->scheduledRelease();
262 }
263
264 void Compositor::processWaylandEvents()
265 {
266     int ret = wl_event_loop_dispatch(m_loop, 0);
267     if (ret)
268         fprintf(stderr, "wl_event_loop_dispatch error: %d\n", ret);
269 }
270
271 void Compositor::surfaceDestroyed(Surface *surface)
272 {
273     if (defaultInputDevice()->mouseFocus() == surface)
274         defaultInputDevice()->setMouseFocus(0, QPoint(), QPoint());
275     m_surfaces.removeOne(surface);
276     m_dirty_surfaces.remove(surface);
277     if (m_directRenderSurface == surface)
278         setDirectRenderSurface(0, 0);
279     waylandCompositor()->surfaceAboutToBeDestroyed(surface->waylandSurface());
280 }
281
282 void Compositor::markSurfaceAsDirty(Wayland::Surface *surface)
283 {
284     m_dirty_surfaces.insert(surface);
285 }
286
287 void Compositor::destroyClient(WaylandClient *c)
288 {
289     wl_client *client = static_cast<wl_client *>(c);
290     if (client) {
291         m_windowManagerIntegration->removeClient(client);
292         wl_client_destroy(client);
293     }
294 }
295
296 QWindow *Compositor::window() const
297 {
298     return m_qt_compositor->window();
299 }
300
301 GraphicsHardwareIntegration * Compositor::graphicsHWIntegration() const
302 {
303 #ifdef QT_COMPOSITOR_WAYLAND_GL
304     return m_graphics_hw_integration;
305 #else
306     return 0;
307 #endif
308 }
309
310 void Compositor::initializeHardwareIntegration()
311 {
312 #ifdef QT_COMPOSITOR_WAYLAND_GL
313     if (m_graphics_hw_integration)
314         m_graphics_hw_integration->initializeHardware(m_display);
315 #endif
316 }
317
318 void Compositor::initializeDefaultInputDevice()
319 {
320     m_default_wayland_input_device = new WaylandInputDevice(m_qt_compositor);
321     m_default_input_device = m_default_wayland_input_device->handle();
322 }
323
324 void Compositor::initializeWindowManagerProtocol()
325 {
326     m_windowManagerIntegration->initialize(m_display);
327 }
328
329 void Compositor::enableSubSurfaceExtension()
330 {
331     if (!m_subSurfaceExtension) {
332         m_subSurfaceExtension = new SubSurfaceExtensionGlobal(this);
333     }
334 }
335
336 bool Compositor::setDirectRenderSurface(Surface *surface, QOpenGLContext *context)
337 {
338 #ifdef QT_COMPOSITOR_WAYLAND_GL
339     if (!m_pageFlipper) {
340         m_pageFlipper = QGuiApplication::primaryScreen()->handle()->pageFlipper();
341     }
342
343     if (m_graphics_hw_integration && m_graphics_hw_integration->setDirectRenderSurface(surface ? surface->waylandSurface() : 0)) {
344         m_directRenderSurface = surface;
345         m_directRenderContext = context;
346         return true;
347     }
348 #else
349     Q_UNUSED(surface);
350 #endif
351     return false;
352 }
353
354 QList<struct wl_client *> Compositor::clients() const
355 {
356     QList<struct wl_client *> list;
357     foreach (Surface *surface, m_surfaces) {
358         struct wl_client *client = surface->base()->resource.client;
359         if (!list.contains(client))
360             list.append(client);
361     }
362     return list;
363 }
364
365 void Compositor::setScreenOrientation(Qt::ScreenOrientation orientation)
366 {
367     m_orientation = orientation;
368
369     QList<struct wl_client*> clientList = clients();
370     for (int i = 0; i < clientList.length(); ++i) {
371         struct wl_client *client = clientList.at(i);
372         Output *output = m_output_global.outputForClient(client);
373         Q_ASSERT(output);
374         if (output->extendedOutput()){
375             output->extendedOutput()->sendOutputOrientation(orientation);
376         }
377     }
378 }
379
380 Qt::ScreenOrientation Compositor::screenOrientation() const
381 {
382     return m_orientation;
383 }
384
385 void Compositor::setOutputGeometry(const QRect &geometry)
386 {
387     m_output_global.setGeometry(geometry);
388 }
389
390 QRect Compositor::outputGeometry() const
391 {
392     return m_output_global.geometry();
393 }
394
395 void Compositor::setClientFullScreenHint(bool value)
396 {
397     m_windowManagerIntegration->setShowIsFullScreen(value);
398 }
399
400 InputDevice* Compositor::defaultInputDevice()
401 {
402     return m_default_input_device;
403 }
404
405 QList<Wayland::Surface *> Compositor::surfacesForClient(wl_client *client)
406 {
407     QList<Wayland::Surface *> ret;
408
409     for (int i=0; i < m_surfaces.count(); ++i) {
410         if (m_surfaces.at(i)->base()->resource.client == client) {
411             ret.append(m_surfaces.at(i));
412         }
413     }
414     return ret;
415 }
416
417 void Compositor::configureTouchExtension(int flags)
418 {
419     if (m_touchExtension)
420         m_touchExtension->setFlags(flags);
421 }
422
423 void Compositor::setRetainedSelectionWatcher(RetainedSelectionFunc func, void *param)
424 {
425     m_retainNotify = func;
426     m_retainNotifyParam = param;
427 }
428
429 bool Compositor::wantsRetainedSelection() const
430 {
431     return m_retainNotify != 0;
432 }
433
434 void Compositor::feedRetainedSelectionData(QMimeData *data)
435 {
436     if (m_retainNotify) {
437         m_retainNotify(data, m_retainNotifyParam);
438     }
439 }
440
441 void Compositor::scheduleReleaseBuffer(SurfaceBuffer *screenBuffer)
442 {
443     QMetaObject::invokeMethod(this,"releaseBuffer",Q_ARG(SurfaceBuffer*,screenBuffer));
444 }
445
446 void Compositor::overrideSelection(QMimeData *data)
447 {
448     m_data_device_manager->overrideSelection(*data);
449 }
450
451 bool Compositor::isDragging() const
452 {
453     return false;
454 }
455
456 void Compositor::sendDragMoveEvent(const QPoint &global, const QPoint &local,
457                                             Surface *surface)
458 {
459     Q_UNUSED(global);
460     Q_UNUSED(local);
461     Q_UNUSED(surface);
462 //    Drag::instance()->dragMove(global, local, surface);
463 }
464
465 void Compositor::sendDragEndEvent()
466 {
467 //    Drag::instance()->dragEnd();
468 }
469
470 } // namespace Wayland