Minimal CPP▲
Minimal CPP is a minimalistic compositor example implementing a complete Qt Wayland Compositor using C++. The C++ API of QtWaylandCompositor is low-level and intended for specialized applications, such as supporting hardware features, or if Qt Quick is not available. The QML API offers more convenience and functionality. For comparison, the Minimal QML example implements more functionality with 30 lines of QML than this example does in 300+ lines.
This example is split in two parts. The Wayland logic is contained in the Compositor class, and the user interface is in the Window class.
I. Window▲
The Window class is fairly straight-forward. To display the Wayland surfaces, it iterates through the compositor's views and renders them on the screen using QOpenGLTextureBlitter:
void
Window::
paintGL()
{
m_compositor-&
gt;startRender();
QOpenGLFunctions *
functions =
context()-&
gt;functions();
functions-&
gt;glClearColor(.4
f, .7
f, .1
f, 0.5
f);
functions-&
gt;glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
GLenum currentTarget =
GL_TEXTURE_2D;
m_textureBlitter.bind(currentTarget);
functions-&
gt;glEnable(GL_BLEND);
functions-&
gt;glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
const
auto
views =
m_compositor-&
gt;views();
for
(View *
view : views) {
auto
texture =
view-&
gt;getTexture();
if
(!
texture)
continue
;
if
(texture-&
gt;target() !=
currentTarget) {
currentTarget =
texture-&
gt;target();
m_textureBlitter.bind(currentTarget);
}
GLuint textureId =
texture-&
gt;textureId();
QWaylandSurface *
surface =
view-&
gt;surface();
if
(surface &
amp;&
amp; surface-&
gt;hasContent()) {
QSize s =
surface-&
gt;destinationSize();
view-&
gt;initPosition(size(), s);
QPointF pos =
view-&
gt;globalPosition();
QRectF surfaceGeometry(pos, s);
QOpenGLTextureBlitter::
Origin surfaceOrigin =
view-&
gt;currentBuffer().origin() ==
QWaylandSurface::
OriginTopLeft
? QOpenGLTextureBlitter::
OriginTopLeft
:
QOpenGLTextureBlitter::
OriginBottomLeft;
QMatrix4x4 targetTransform =
QOpenGLTextureBlitter::
targetTransform(surfaceGeometry, QRect(QPoint(), size()));
m_textureBlitter.blit(textureId, targetTransform, surfaceOrigin);
}
}
m_textureBlitter.release();
m_compositor-&
gt;endRender();
}
All keyboard and mouse events are delivered to the compositor. For example:
void
Window::
mousePressEvent(QMouseEvent *
event)
{
m_compositor-&
gt;handleMousePress(event-&
gt;position().toPoint(), event-&
gt;button());
}
II. Compositor▲
The Compositor class is more complex, since it has to implement much of the logic that would be handled by WaylandCompositor and WaylandQuickItem in a QML-based compositor.
The create function sets up the compositor, using the IviApplication, which is the most basic shell extension. The function is called after the OpenGL context has been initialized:
void
Compositor::
create()
{
QWaylandOutput *
output =
new
QWaylandOutput(this
, m_window);
QWaylandOutputMode mode(m_window-&
gt;size(), 60000
);
output-&
gt;addMode(mode, true
);
QWaylandCompositor::
create();
output-&
gt;setCurrentMode(mode);
m_iviApplication =
new
QWaylandIviApplication(this
);
connect(m_iviApplication, &
amp;QWaylandIviApplication::
iviSurfaceCreated, this
, &
amp;Compositor::
onIviSurfaceCreated);
}
All the logic for mouse events and keyboard focus has to be implemented manually, including implicit mouse grabs (sending all mouse moves to the surface that received the initial mouse press). Note that mouse press events in the Wayland protocol do not contain the mouse position, so we always have to send a mouse move when we reveive a mouse press:
void
Compositor::
handleMousePress(const
QPoint &
amp;position, Qt::
MouseButton button)
{
if
(!
m_mouseView) {
if
((m_mouseView =
viewAt(position)))
raise(m_mouseView);
}
auto
*
seat =
defaultSeat();
seat-&
gt;sendMouseMoveEvent(m_mouseView, mapToView(m_mouseView, position));
seat-&
gt;sendMousePressEvent(button);
}
For a mouse release, we end the implicit grab and notify the surface at the current mouse position:
void
Compositor::
handleMousePress(const
QPoint &
amp;position, Qt::
MouseButton button)
{
if
(!
m_mouseView) {
if
((m_mouseView =
viewAt(position)))
raise(m_mouseView);
}
auto
*
seat =
defaultSeat();
seat-&
gt;sendMouseMoveEvent(m_mouseView, mapToView(m_mouseView, position));
seat-&
gt;sendMousePressEvent(button);
}
When we are notified of a new surface, we create a View to keep track of it, and connect signals so we can handle updates.
void
Compositor::
onIviSurfaceCreated(QWaylandIviSurface *
iviSurface)
{
View *
view =
new
View(iviSurface-&
gt;iviId());
view-&
gt;setSurface(iviSurface-&
gt;surface());
view-&
gt;setOutput(outputFor(m_window));
m_views &
lt;&
lt; view;
connect(view, &
amp;QWaylandView::
surfaceDestroyed, this
, &
amp;Compositor::
viewSurfaceDestroyed);
connect(iviSurface-&
gt;surface(), &
amp;QWaylandSurface::
redraw, this
, &
amp;Compositor::
triggerRender);
}
The View class subclasses QWaylandView, which represents a specific view of a surface. The advance function updates the view's current buffer and returns true if there is new content. The getTexture function makes the buffer contents available as an OpenGL texture for the benefit of the Window class:
QOpenGLTexture *
View::
getTexture() {
if
(advance())
m_texture =
currentBuffer().toOpenGLTexture();
return
m_texture;
}