A Lighthouse with Windows

Published Wednesday February 10th, 2010
10 Comments on A Lighthouse with Windows
Posted in Graphics Items, Graphics View, Lighthouse, Qt

Have you ever wondered how a window system actually works ? I have. When I was preparing my Widgets In-Depth talk for Qt Developer Days last year, I decided I should implement a simple little window system as bonus material.
This blog post will be a rough description of the window system I built using Qt, and a port of Qt to the new system.

Windows And Systems

As you probably know, the window system is the process that manages the windows on your screen.
A window, also known as a window surface, can be described as an area of memory that will be drawn to the screen. Window surfaces are typically arranged in a scene-graph to keep track of them. Finally, window systems typically run as a server process. When other processes that want to show something on screen, they have to talk to it through some form of inter-process communication.


The toy window system up and running.

Window Surface

We need something that represents the memory area used to paint the window contents. In theory, this can be just any class representing a piece of memory, like QByteArray, and paint into it. But I’m lazy. To keep our system simple, I share the window surface with the client and let the client paint directly into that memory. I do this by using QSharedMemory to represent the Window Surface.

Scene Graph

I’m guessing you already figured this one out. QGraphicsScene does exactly what we want. It provides features that really come in handy when implementing a window system, like input handling and item caching. For more fancy features, we even get item transparency, transformations and graphics effects.

Inter-Process Communication

I already mentioned that I’m using QSharedMemory for the window surfaces. But to handle things like user input and window updates we need to be able to pass messages between the client and the server. The protocol I’m using consists of three types of messages:

  • Request Messages
  • Reply Messages
  • Event Messages
  • Requests are messages sent from the client to the server asking for something, for example a new window. A reply is sent from the server, informing of the result of the request. Request-response pairs are synchronous. However, in our system most requests will not have a corresponding response, in which case the request will be asynchronous.

    Events are messages sent from the server, informing the client that something has happened. A typical event would be user input to a window. Since the server doesn’t expect any response from the client, events are always asynchronous.

    To transfere these messages back and forth, I use QTcpServer and QTcpSocket.

    Lighthouse by The Wandering Angel on flickr
    Lighthouse by The Wandering Angel on flickr

    Ports And Lighthouses

    A window system is pretty boring if there is nothing running on it. Since I am not implementing a standard protocol, I also have to take care of the client side. Luckily there is the Lighthouse project. Lighthouse is a research project that aims to make it easier to port Qt to different graphics systems.
    A port to a new window system takes the form of a plugin that implements a representation of a window surface for the client, and functionality for sending and receiving messages from the window system server.

    It’s All Yours

    I’ve avoided going into too much detail about the inner workings of the window system and the Qt port. The implementations are surprisingly small, so I recommend that you take a look at the code if you are interested.
    The code for the window system is available here, and the code for the graphics plugin is available here (make sure you use the Lighthouse branch to build the plugin). Enjoy and happy hacking! πŸ™‚

    Do you like this? Share it
    Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

    Posted in Graphics Items, Graphics View, Lighthouse, Qt

    10 comments

    illissius says:

    Doesn’t using QGraphicsView — itself a widget — to implement a windowing system present chicken-and-egg problems?

    mbm says:

    @illissius, It may be a bit confusing, but we are dealing with two window systems: the native window system (in my case Quartz Compositor in OS X) and the window system we have implemented ourselves. Our window system just happens to be displaying everything in a native window.

    Dragan says:

    X re-designed with good support for HW accelerated back ends?

    jflatt says:

    In the graphics plugin, qgraphicssystem_dd.cpp, line 147, looks like it should be QEvent::MouseButtonRelease

    Tuxie says:

    Yay! Soon we can implement the Squeak look’n’feel in KDE! πŸ˜€

    Owen says:

    We have been wanting to use Qt for an in game UI for a while, but, of course, the problem with doing that is that implementing our own backend from start to finish never sounded like fun.

    Lighthouse therefore sounds like an excellent solution to our problem πŸ™‚

    Benjamin says:

    Isn’t that the solution to the crashes of KDE’s Plasma? If the plasmoids were run in separate processes, we would not have a complete failure when a single plasmoid crash.
    Any KDE developer around to steal the ideas of mbm? πŸ˜‰

    anon says:

    so, the logical question is could I run the Lighthouse on top of Wayland?

    of course to get all that modern desktop stuff would be to run kwin on top of Lighthouse… but that’s a pipe dream.

    TomCooksey says:

    Lighthouse on Wayland is indeed a possibility. πŸ™‚

    KWin on Wayland on the other hand makes no sense. The part of the graphics stack KWin implements on X11 desktops is a core part of Wayland itself (see wayland-system-compositor.c). Because both window management and composition is done in the server process, there are fewer context switches making Wayland a much nicer architecture (and, indecently, much more like QWS).

    satoshi says:

    Hi. It seems to be very interesting. Where can I read and learn more about this? Any recommended book/website, maybe? How should I search for this? “Windows Systems”? Thank you.

    Commenting closed.

    Get started today with Qt Download now