1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the Qt Compositor.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
41 #include "wlcompositor.h"
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"
55 #include "wlinputdevice.h"
59 #include <QSocketNotifier>
61 #include <qpa/qplatformscreen.h>
62 #include <QGuiApplication>
63 #include <qpa/qplatformscreenpageflipper.h>
75 #include <sys/select.h>
78 #include <wayland-server.h>
80 #include "hardware_integration/graphicshardwareintegration.h"
81 #include "waylandwindowmanagerintegration.h"
85 static Compositor *compositor;
87 void compositor_create_surface(struct wl_client *client,
88 struct wl_resource *resource, uint32_t id)
90 static_cast<Compositor *>(resource->data)->createSurface(client,id);
93 void compositor_create_region(struct wl_client *client,
94 struct wl_resource *compositor, uint32_t id)
97 new Region(client, id);
100 const static struct wl_compositor_interface compositor_interface = {
101 compositor_create_surface,
102 compositor_create_region
105 void Compositor::bind_func(struct wl_client *client, void *data,
106 uint32_t version, uint32_t id)
109 wl_client_add_object(client,&wl_compositor_interface, &compositor_interface, id,data);
113 Compositor *Compositor::instance()
118 Compositor::Compositor(WaylandCompositor *qt_compositor)
119 : m_display(new Display)
120 , m_default_input_device(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)
131 , m_outputExtension(0)
132 , m_surfaceExtension(0)
133 , m_subSurfaceExtension(0)
134 , m_touchExtension(0)
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);
144 m_windowManagerIntegration = new WindowManagerServerIntegration(qt_compositor, this);
146 wl_display_add_global(m_display->handle(),&wl_compositor_interface,this,Compositor::bind_func);
148 m_data_device_manager = new DataDeviceManager(this);
150 wl_display_add_global(m_display->handle(),&wl_output_interface, &m_output_global,OutputGlobal::output_bind_func);
152 m_shell = new Shell();
153 wl_display_add_global(m_display->handle(), &wl_shell_interface, m_shell, Shell::bind_func);
155 wl_display_init_shm(m_display->handle());
157 m_outputExtension = new OutputExtensionGlobal(this);
158 m_surfaceExtension = new SurfaceExtensionGlobal(this);
159 m_qtkeyExtension = new QtKeyExtensionGlobal(this);
160 m_touchExtension = new TouchExtensionGlobal(this);
162 if (wl_display_add_socket(m_display->handle(), qt_compositor->socketName())) {
163 fprintf(stderr, "Fatal: Failed to open server socket\n");
167 m_loop = wl_display_get_event_loop(m_display->handle());
169 int fd = wl_event_loop_get_fd(m_loop);
171 QSocketNotifier *sockNot = new QSocketNotifier(fd, QSocketNotifier::Read, this);
172 connect(sockNot, SIGNAL(activated(int)), this, SLOT(processWaylandEvents()));
174 qRegisterMetaType<SurfaceBuffer*>("SurfaceBuffer*");
175 //initialize distancefieldglyphcache here
178 Compositor::~Compositor()
181 delete m_outputExtension;
182 delete m_surfaceExtension;
183 delete m_subSurfaceExtension;
184 delete m_touchExtension;
185 delete m_qtkeyExtension;
187 delete m_default_wayland_input_device;
188 delete m_data_device_manager;
190 #ifdef QT_COMPOSITOR_WAYLAND_GL
191 delete m_graphics_hw_integration;
197 void Compositor::frameFinished(Surface *surface)
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();
210 void Compositor::createSurface(struct wl_client *client, uint32_t id)
212 Surface *surface = new Surface(client,id, this);
214 m_surfaces << surface;
216 m_qt_compositor->surfaceCreated(surface->waylandSurface());
219 struct wl_client *Compositor::getClientFromWinId(uint winId) const
221 Surface *surface = getSurfaceFromWinId(winId);
223 return surface->base()->resource.client;
228 Surface *Compositor::getSurfaceFromWinId(uint winId) const
230 foreach (Surface *surface, m_surfaces) {
231 if (surface->id() == winId)
238 QImage Compositor::image(uint winId) const
240 foreach (Surface *surface, m_surfaces) {
241 if (surface->id() == winId) {
242 return surface->image();
249 uint Compositor::currentTimeMsecs()
251 //### we throw away the time information
253 int ret = gettimeofday(&tv, 0);
255 return tv.tv_sec*1000 + tv.tv_usec/1000;
259 void Compositor::releaseBuffer(SurfaceBuffer *screenBuffer)
261 screenBuffer->scheduledRelease();
264 void Compositor::processWaylandEvents()
266 int ret = wl_event_loop_dispatch(m_loop, 0);
268 fprintf(stderr, "wl_event_loop_dispatch error: %d\n", ret);
271 void Compositor::surfaceDestroyed(Surface *surface)
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());
282 void Compositor::markSurfaceAsDirty(Wayland::Surface *surface)
284 m_dirty_surfaces.insert(surface);
287 void Compositor::destroyClient(WaylandClient *c)
289 wl_client *client = static_cast<wl_client *>(c);
291 m_windowManagerIntegration->removeClient(client);
292 wl_client_destroy(client);
296 QWindow *Compositor::window() const
298 return m_qt_compositor->window();
301 GraphicsHardwareIntegration * Compositor::graphicsHWIntegration() const
303 #ifdef QT_COMPOSITOR_WAYLAND_GL
304 return m_graphics_hw_integration;
310 void Compositor::initializeHardwareIntegration()
312 #ifdef QT_COMPOSITOR_WAYLAND_GL
313 if (m_graphics_hw_integration)
314 m_graphics_hw_integration->initializeHardware(m_display);
318 void Compositor::initializeDefaultInputDevice()
320 m_default_wayland_input_device = new WaylandInputDevice(m_qt_compositor);
321 m_default_input_device = m_default_wayland_input_device->handle();
324 void Compositor::initializeWindowManagerProtocol()
326 m_windowManagerIntegration->initialize(m_display);
329 void Compositor::enableSubSurfaceExtension()
331 if (!m_subSurfaceExtension) {
332 m_subSurfaceExtension = new SubSurfaceExtensionGlobal(this);
336 bool Compositor::setDirectRenderSurface(Surface *surface, QOpenGLContext *context)
338 #ifdef QT_COMPOSITOR_WAYLAND_GL
339 if (!m_pageFlipper) {
340 m_pageFlipper = QGuiApplication::primaryScreen()->handle()->pageFlipper();
343 if (m_graphics_hw_integration && m_graphics_hw_integration->setDirectRenderSurface(surface ? surface->waylandSurface() : 0)) {
344 m_directRenderSurface = surface;
345 m_directRenderContext = context;
354 QList<struct wl_client *> Compositor::clients() const
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))
365 void Compositor::setScreenOrientation(Qt::ScreenOrientation orientation)
367 m_orientation = orientation;
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);
374 if (output->extendedOutput()){
375 output->extendedOutput()->sendOutputOrientation(orientation);
380 Qt::ScreenOrientation Compositor::screenOrientation() const
382 return m_orientation;
385 void Compositor::setOutputGeometry(const QRect &geometry)
387 m_output_global.setGeometry(geometry);
390 QRect Compositor::outputGeometry() const
392 return m_output_global.geometry();
395 void Compositor::setClientFullScreenHint(bool value)
397 m_windowManagerIntegration->setShowIsFullScreen(value);
400 InputDevice* Compositor::defaultInputDevice()
402 return m_default_input_device;
405 QList<Wayland::Surface *> Compositor::surfacesForClient(wl_client *client)
407 QList<Wayland::Surface *> ret;
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));
417 void Compositor::configureTouchExtension(int flags)
419 if (m_touchExtension)
420 m_touchExtension->setFlags(flags);
423 void Compositor::setRetainedSelectionWatcher(RetainedSelectionFunc func, void *param)
425 m_retainNotify = func;
426 m_retainNotifyParam = param;
429 bool Compositor::wantsRetainedSelection() const
431 return m_retainNotify != 0;
434 void Compositor::feedRetainedSelectionData(QMimeData *data)
436 if (m_retainNotify) {
437 m_retainNotify(data, m_retainNotifyParam);
441 void Compositor::scheduleReleaseBuffer(SurfaceBuffer *screenBuffer)
443 QMetaObject::invokeMethod(this,"releaseBuffer",Q_ARG(SurfaceBuffer*,screenBuffer));
446 void Compositor::overrideSelection(QMimeData *data)
448 m_data_device_manager->overrideSelection(*data);
451 bool Compositor::isDragging() const
456 void Compositor::sendDragMoveEvent(const QPoint &global, const QPoint &local,
462 // Drag::instance()->dragMove(global, local, surface);
465 void Compositor::sendDragEndEvent()
467 // Drag::instance()->dragEnd();
470 } // namespace Wayland