26

I want to create a simple stacking window manager (in C) for private use, mainly for the purpose of learning and challenging myself.

I've looked through twm's source code which has relatively few bells and whistles but it seems very low level since it's not based on a widget toolkit.1 Would using a toolkit such as GTK+ be preferable? I'm afraid that some of the code and libraries in twm might be too antiquated (edit: deprecated) and I want the window manager to use relatively modern libraries. For the sake of understanding I would also be interested in suggestions how to start a window manager from scratch — there aren't many tutorials for this purpose.

Update: For those thinking of similar projects: I ended up using Common Lisp and the CLX library. tinywm-lisp served as a basis and the brilliant CLFSWM and Stumpwm were a great help. For reference I used the CLX — Common LISP X Interface (PDF warning) and #xlib on Freenode.

Iceland_jack
  • 6,848
  • 7
  • 37
  • 46
  • Widgets basically live inside windows, so most of what a window manager does can't depend on widgets. – Jerry Coffin Aug 12 '10 at 22:40
  • I though so, that's why I referenced Wikipedia that; “[twm is] written in C directly against Xlib rather than based on a widget toolkit.” I assumed that meant more modern window managers _are_ based on widget toolkits. – Iceland_jack Aug 12 '10 at 22:50
  • 4
    For some reason this question made me think of [this](http://www.art.net/~hopkins/Don/unix-haters/x-windows/disaster.html)... – Brian Nixon Aug 12 '10 at 23:07

8 Answers8

26

Whatever you do, use XCB and not Xlib. It' modern, asynchronous, simpler and gives you direct access to the X11 protocol.

Serge
  • 418
  • 3
  • 6
  • 3
    See this was one of the reasons I asked! `quinmars` recommended I use `Xlib` which most window managers seem to use, I haven't seen any window managers that use `XCB` (except Awesome) — is that just because all the other window mangers are old or is there a reason to use `XCB` nowadays when starting from scratch? The `Xlib` code in `twm` still works some twenty years later with the newest verion og Xorg — will `XCB` code still work in that time? – Iceland_jack Aug 13 '10 at 18:56
  • 2
    To help other people who might be interested in doing the same thing I'd recommend the `mcwm` (http://hack.org/mc/hacks/mcwm/) which is based entirely on `XCB`. – Iceland_jack Aug 24 '10 at 10:33
  • 2
    @Baldur: this post is quite old, but I'm getting into XCB now. The documentation is very incomplete, but the API is very transparent and closely maps XLib concepts, so it's not really a problem. I wholeheartedly recommend you look at awesome's source code (I learn xcb through awesome personally). It deserves its name. – Alexandre C. Aug 06 '11 at 22:16
10

I've written an in-depth tutorial series that shows you how to write an X11 window manager in C++:

In addition, you can check out a simple example window manager, basic_wm, at

It's heavily commented for pedagogical purposes.

On Xlib vs XCB - I would recommend Xlib over XCB if you're first starting out. XCB is more efficient, but it's much more low-level and much more verbose (think assembly language for the GUI). You don't want to burden yourself with premature optimization until you already have a prototype working.

Chuan Ji
  • 119
  • 1
  • 4
6

A very minimalistic WM is wm2. I haven't read the source code and hence I don't know if it is a teaching example. Of course you can use libraries that already do much of the abstraction and drawing work for you like gdk and gtk. But since this project is only for personal learning, I'd go the hard way and use Xlib directly.

Here are some links that might be useful for you:

quinmars
  • 11,175
  • 8
  • 32
  • 41
  • I'll use Xlib then. Do you know of any window managers that use something like GTK+ for "abstraction and drawing work"? – Iceland_jack Aug 13 '10 at 14:51
  • AFAIK, the API of xcb is not stable yet, since you do not plan to release your WM ever this shouldn't be a show stopper. – quinmars Aug 14 '10 at 13:04
  • There are several WMs using gtk to draw their widgets, like Metacity, the xfce wm, lxde. I don't know how far they are using gdk. Or if they are using xlib or xcb directly. – quinmars Aug 14 '10 at 17:10
6

Have a look at the code for dwm. The codebase is beautiful and easy to understand. The entire thing is about 2000 lines.

Dave
  • 10,964
  • 3
  • 32
  • 54
2

http://code.google.com/p/partiwm/ is an attempt to write a window manager (tiling, not stacking) from scratch, it might be useful to you to read through the code.

zwol
  • 135,547
  • 38
  • 252
  • 361
2

metacity uses gtk for certain UI elements. See its HACKING and README.

see also Where are some good Xlib programming guides?

Community
  • 1
  • 1
Havoc P
  • 8,365
  • 1
  • 31
  • 46
2

For the sake of understanding I would also be interested in suggestions how to start a window manager from scratch

You may want to have a look at the aewm window manager. The code is small and it does the basics quite well. It's written in C and uses Xlib.

http://www.red-bean.com/decklin/aewm/

Frank Hale
  • 1,886
  • 1
  • 17
  • 31
0

Xmonad might be a good place to start.

Gaius
  • 2,556
  • 1
  • 24
  • 43