I'm now zeroing in on the core of the project. I'm focusing on the bare necessities: how the WM will handle opening windows, manage focus, and perhaps handle basic movement within the X11 environment. The goal is to build a foundation.
Prioritizing Feature Implementation
I've sketched out the initial feature set. Now the focus shifts to the practical: coding up the core functionality. Handling MapRequest and UnmapNotify events takes priority. Following that, I'll integrate the essential input handling: key binds to launch a terminal and close windows are crucial for usability.
Structuring the Foundation
I'm now outlining the program structure, mapping out the main function's components: opening a display, error handling (crucial!), setting input masks to catch the events, grabbing the necessary keys for shortcuts, and the core event loop. I plan to use a switch statement to dispatch different event types.
Mapping Out the Events
I'm deep in the event loop design, meticulously planning the handlers. The MapRequest handler is now defined, utilizing XMapWindow and XSetInputFocus. Input handling for key presses and mouse clicks is also in progress. I will be sure to add the MotionNotify event.
Architecting the Core Logic
I'm now integrating the event handling with the input grabs. I'm focusing on "grabbing" specific keys and mouse buttons to intercept user actions. This setup includes error handling to manage the X11 connections.
Streamlining the Core
I'm now prioritizing the code's efficiency, specifically in the event loop. My approach involves refining the handlers for ButtonPress and MotionNotify events, critical for moving and resizing windows. I'll test the error handling to ensure robustness.
Synthesizing the Core
I'm now consolidating the plan. This includes the essentials: connecting to the display, setting up event handling, and defining the core behaviors: mapping windows, and handling user input. Key bindings and the loop structure are forming the basic foundation.
Formulating the Implementation
I'm now formulating a plan for the code structure, aiming for a clean, single-file design. The core includes, constants, key bindings, and main loop are defined, with an emphasis on keeping the program lean. The goal is to build a basic WM that opens, moves, resizes, and closes windows.
This is a complete, compilable example of a "Tiny Window Manager."
It is based on the famous TinyWM logic but expanded slightly to be usable. It does not use title bars or borders (non-reparenting), which keeps the code under 150 lines.
Features
Alt + Enter: Spawns a terminal (xterm).
Alt + F4: Closes the focused window.
Alt + Left Mouse Drag: Moves a window.
Alt + Right Mouse Drag: Resizes a window.
Alt + Tab: Cycles focus (basic implementation via XCirculateSubwindows).
The C Code (wm.c)
c
How to compile and run
1. Dependencies
You need the X11 development headers.
Debian/Ubuntu:sudo apt install libx11-dev
Arch:sudo pacman -S libx11
Fedora:sudo dnf install libX11-devel
2. Compile
bash
gcc -o mywm wm.c -lX11
3. Running it (Testing)
The safest way to test a new window manager is using Xephyr. Xephyr creates a nested X server (a window inside your current desktop that acts as a separate display).
Install Xephyr:
Debian/Ubuntu: sudo apt install xserver-xephyr
Arch: sudo pacman -S xorg-server-xephyr
Run the test:
Open a terminal.
Launch Xephyr on display :1:
bash
Xephyr -br -ac -noreset -screen 800x600 :1 &
Run your window manager on that display:
bash
DISPLAY=:1 ./mywm
How to use it inside Xephyr:
Click inside the black Xephyr window.
Press Alt + Enter. An xterm should appear. (Make sure xterm is installed).
Hold Alt + Left Click on the terminal to move it.
Hold Alt + Right Click to resize it.
Press Alt + F4 to close it.
How it works (Internals)
XOpenDisplay: Connects your C program to the X Server socket.
XGrabKey: Tells the X Server, "Intercept these specific keystrokes. Don't send them to the focused application; send them to me (the WM)."
XGrabButton: Same as XGrabKey, but for mouse clicks. We grab the Mod1Mask (Alt) so that normal clicks still go to applications, but Alt+Clicks come to us.
XNextEvent: Pauses the program until an event occurs.
key.subwindow: When we get a key press, ev.xkey.subwindow tells us which window the mouse was over. If it is None, the mouse was over the root background.
XMoveWindow / XResizeWindow: These are the core Xlib functions that manipulate the geometry of windows. We calculate the new numbers based on the difference (xdiff, ydiff) between where the mouse was when clicked and where it is now.
/*
* Minimalistic Window Manager
* Compile with: gcc -o mywm wm.c -lX11
*/
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
#define MOD Mod1Mask // Alt key
#define TE "xterm &" // Terminal command
int main(void) {
Display *dpy;
XWindowAttributes attr;
XButtonEvent start;
XEvent ev;
Window root;
// 1. Open connection to the X server
if (!(dpy = XOpenDisplay(0x0))) {
fprintf(stderr, "Cannot open display\n");
return 1;
}
root = DefaultRootWindow(dpy);
// 2. Grab global keys (Alt+Enter, Alt+F4, Alt+Tab)
// F4 to close window
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_F4), MOD, root,
True, GrabModeAsync, GrabModeAsync);
// Enter to spawn terminal
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Return), MOD, root,
True, GrabModeAsync, GrabModeAsync);
// Tab to cycle windows
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Tab), MOD, root,
True, GrabModeAsync, GrabModeAsync);
// 3. Grab mouse buttons for window manipulation
// Alt + Left Click (Move)
XGrabButton(dpy, 1, MOD, root, True, ButtonPressMask,
GrabModeAsync, GrabModeAsync, None, None);
// Alt + Right Click (Resize)
XGrabButton(dpy, 3, MOD, root, True, ButtonPressMask,
GrabModeAsync, GrabModeAsync, None, None);
printf("WM Started! use Alt+Enter to spawn, Alt+F4 to close.\n");
// 4. The Main Event Loop
for(;;) {
XNextEvent(dpy, &ev);
// A. key events
if (ev.type == KeyPress && ev.xkey.subwindow != None) {
// Close Window (Alt+F4)
if (ev.xkey.keycode == XKeysymToKeycode(dpy, XK_F4)) {
XDestroyWindow(dpy, ev.xkey.subwindow);
}
// Cycle Windows (Alt+Tab)
else if (ev.xkey.keycode == XKeysymToKeycode(dpy, XK_Tab)) {
XCirculateSubwindowsDown(dpy, root);
}
}
// Spawn Terminal (Alt+Enter) - handled on Root, not subwindow
if (ev.type == KeyPress && ev.xkey.keycode == XKeysymToKeycode(dpy, XK_Return)) {
system(TE);
}
// B. Mouse Button Press: setup for move/resize
if (ev.type == ButtonPress && ev.xbutton.subwindow != None) {
// Get current window attributes (pos/size) before move starts
XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
start = ev.xbutton; // Store mouse start position
// Bring window to front and focus it
XRaiseWindow(dpy, ev.xbutton.subwindow);
XSetInputFocus(dpy, ev.xbutton.subwindow, RevertToParent, CurrentTime);
}
// C. Mouse Motion: actually move/resize
if (ev.type == MotionNotify && start.subwindow != None) {
int xdiff = ev.xbutton.x_root - start.x_root;
int ydiff = ev.xbutton.y_root - start.y_root;
// Alt + Left Mouse: Move
if (start.button == 1) {
XMoveWindow(dpy, start.subwindow,
attr.x + xdiff,
attr.y + ydiff);
}
// Alt + Right Mouse: Resize
else if (start.button == 3) {
// Ensure we don't resize to 0 or negative
int width = attr.width + xdiff;
int height = attr.height + ydiff;
if (width < 1) width = 1;
if (height < 1) height = 1;
XResizeWindow(dpy, start.subwindow, width, height);
}
}
// D. Button Release: Stop tracking
if (ev.type == ButtonRelease) {
start.subwindow = None;
}
}
}