Make WaylandSurface a QObject to have a signal API.
authorSamuel Rødal <samuel.rodal@nokia.com>
Tue, 15 Mar 2011 11:11:34 +0000 (12:11 +0100)
committerSamuel Rødal <samuel.rodal@nokia.com>
Tue, 15 Mar 2011 11:11:34 +0000 (12:11 +0100)
examples/qwidget-compositor/main.cpp
src/qt-compositor/private/wlcompositor.cpp
src/qt-compositor/private/wlcompositor.h
src/qt-compositor/private/wlsurface.cpp
src/qt-compositor/qtcompositor.h
src/qt-compositor/waylandsurface.cpp
src/qt-compositor/waylandsurface.h

index 659058b..58c87c8 100644 (file)
@@ -61,6 +61,7 @@ class QWidgetCompositor : public QGLWidget, public WaylandCompositor
 class QWidgetCompositor : public QWidget, public WaylandCompositor
 #endif
 {
+    Q_OBJECT
 public:
     QWidgetCompositor() : WaylandCompositor(this), m_dragSurface(0) {
         setMouseTracking(true);
@@ -73,36 +74,39 @@ public:
         }
     }
 
-protected:
-
-    void surfaceCreated(WaylandSurface *) {
-        update();
-    }
-
-    void surfaceDestroyed(WaylandSurface *surface) {
+private slots:
+    void surfaceDestroyed(QObject *object) {
+        WaylandSurface *surface = qobject_cast<WaylandSurface *>(object);
         m_surfaces.removeAll(surface);
         if (m_surfaces.isEmpty())
             setInputFocus(0);
         update();
     }
 
-    void surfaceMapped(WaylandSurface *surface, const QRect &rect) {
+    void surfaceMapped(const QRect &rect) {
+        WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
         QPoint pos;
-        int index = m_surfaces.indexOf(surface);
-        if (index == -1) {
+        if (!m_surfaces.contains(surface)) {
             uint px = 1 + (qrand() % (width() - rect.width() - 2));
             uint py = 1 + (qrand() % (height() - rect.height() - 2));
             pos = QPoint(px, py);
             surface->setGeometry(QRect(pos, rect.size()));
             m_surfaces.append(surface);
         } else {
-            m_surfaces[index]->setGeometry(rect);
+            surface->setGeometry(rect);
         }
         setInputFocus(surface);
         update();
     }
 
-    void surfaceDamaged(WaylandSurface *surface, const QRect &rect) {
+    void surfaceDamaged(const QRect &rect) {
+        WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
+        surfaceDamaged(surface, rect);
+    }
+
+protected:
+    void surfaceDamaged(WaylandSurface *surface, const QRect &rect)
+    {
 #ifdef QT_COMPOSITOR_WAYLAND_GL
         Q_UNUSED(surface);
         Q_UNUSED(rect);
@@ -112,6 +116,13 @@ protected:
 #endif
     }
 
+    void surfaceCreated(WaylandSurface *surface) {
+        connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
+        connect(surface, SIGNAL(mapped(const QRect &)), this, SLOT(surfaceMapped(const QRect &)));
+        connect(surface, SIGNAL(damaged(const QRect &)), this, SLOT(surfaceDamaged(const QRect &)));
+        update();
+    }
+
     void paintEvent(QPaintEvent *) {
         QPainter p(this);
 
@@ -243,3 +254,4 @@ int main(int argc, char *argv[])
 
 }
 
+#include "main.moc"
index 1e9aebe..8650899 100644 (file)
@@ -285,15 +285,9 @@ void Compositor::processWaylandEvents()
         fprintf(stderr, "wl_event_loop_dispatch error: %d\n", ret);
 }
 
-void Compositor::surfaceResized(Surface *surface, const QSize &size)
-{
-    m_qt_compositor->surfaceMapped(surface->handle(), QRect(QPoint(), size));
-}
-
 void Compositor::surfaceDestroyed(Surface *surface)
 {
     m_surfaces.removeOne(surface);
-    m_qt_compositor->surfaceDestroyed(surface->handle());
 }
 
 void Compositor::setInputFocus(Surface *surface)
@@ -306,11 +300,6 @@ void Compositor::setInputFocus(Surface *surface)
     wl_input_device_set_pointer_focus(&m_input, base, time, 0, 0, 0, 0);
 }
 
-void Compositor::surfaceDamaged(Surface *surface, const QRect &rect)
-{
-    m_qt_compositor->surfaceDamaged(surface->handle(), rect);
-}
-
 QWidget * Compositor::topLevelWidget() const
 {
     return m_qt_compositor->topLevelWidget();
index 2bf5bc3..e596f48 100644 (file)
@@ -75,10 +75,7 @@ public:
     struct wl_input_device *inputDevice() { return &m_input; }
 
     void createSurface(struct wl_client *client, int id);
-
-    void surfaceResized(Surface *surface, const QSize &size);
     void surfaceDestroyed(Surface *surface);
-    void surfaceDamaged(Surface *surface, const QRect &rect);
 
     uint currentTimeMsecs() const;
 
index f6ee13f..aa00ce6 100644 (file)
@@ -160,6 +160,7 @@ Surface::~Surface()
 {
     Q_D(Surface);
     d->compositor->surfaceDestroyed(this);
+    delete d->qtSurface;
 }
 
 WaylandSurface::Type Surface::type() const
@@ -171,7 +172,7 @@ WaylandSurface::Type Surface::type() const
 void Surface::damage(const QRect &rect)
 {
     Q_D(Surface);
-    d->compositor->surfaceDamaged(this, rect);
+    emit d->qtSurface->damaged(rect);
 }
 
 QImage Surface::image() const
@@ -197,7 +198,7 @@ void Surface::attachHWBuffer(struct wl_buffer *buffer)
     glDeleteTextures(1,&d->texture_id);
 
     d->texture_id = d->compositor->graphicsHWIntegration()->createTextureFromBuffer(buffer);
-    d->compositor->surfaceResized(this,QSize(buffer->width,buffer->height));
+    emit d->qtSurface->mapped(QRect(QPoint(), QSize(buffer->width, buffer->height)));
 }
 
 GLuint Surface::textureId() const
@@ -212,7 +213,7 @@ void Surface::attachShm(Wayland::ShmBuffer *shm_buffer)
     Q_D(Surface);
     d->shm_buffer = shm_buffer;
     d->type = WaylandSurface::Shm;
-    d->compositor->surfaceResized(this, shm_buffer->size());
+    emit d->qtSurface->mapped(QRect(QPoint(), shm_buffer->size()));
 }
 
 
index eb96065..955b3a6 100644 (file)
@@ -75,9 +75,6 @@ public:
     QWidget *topLevelWidget()const;
 
     virtual void surfaceCreated(WaylandSurface *surface) = 0;
-    virtual void surfaceDestroyed(WaylandSurface *surface) = 0;
-    virtual void surfaceMapped(WaylandSurface *surface, const QRect &rect) = 0;
-    virtual void surfaceDamaged(WaylandSurface *surface, const QRect &rect) = 0;
 
     Wayland::Compositor *handle() const;
 private:
index e32dabd..32d66c4 100644 (file)
@@ -2,7 +2,7 @@
 
 #include "private/wlsurface.h"
 
-class WaylandSurfacePrivate
+class WaylandSurfacePrivate : public QObjectPrivate
 {
 public:
     WaylandSurfacePrivate(Wayland::Surface *srfc)
@@ -14,7 +14,7 @@ public:
 };
 
 WaylandSurface::WaylandSurface(Wayland::Surface *surface)
-    : d_ptr(new WaylandSurfacePrivate(surface))
+    : QObject(*new WaylandSurfacePrivate(surface))
 {
 
 }
index 4ecfda0..5214a0f 100644 (file)
@@ -14,11 +14,11 @@ namespace Wayland {
 class Surface;
 }
 
-class WaylandSurface
+class WaylandSurface : public QObject
 {
+    Q_OBJECT
     Q_DECLARE_PRIVATE(WaylandSurface)
 public:
-
     enum Type {
         Invalid,
         Shm,
@@ -45,10 +45,12 @@ public:
     void sendKeyReleaseEvent(uint code);
 
     Wayland::Surface *handle() const;
-protected:
-    QScopedPointer<WaylandSurfacePrivate> d_ptr;
-private:
-    Q_DISABLE_COPY(WaylandSurface)
+
+signals:
+    void mapped(const QRect &rect);
+    void damaged(const QRect &rect);
+
+    friend class Wayland::Surface;
 };
 
 #endif // WAYLANDSURFACE_H