[LINUX] Self-made window manager (Do you have an X window manager? 2)

This time, I would like to make my own window manager. Last time, I talked about the fact that the X server does not need a window manager, and since it is a sequel to it, [Last time, "Do you have an X window manager?"](Https://qiita.com/ai56go See also / items / 1b8bfeede2b467ac0667).

OS used: ArchLinux (VirtualBox guest)

Premise

We will proceed on the assumption that the following packages etc. are installed in advance. -Last installed package ・ Gcc ・ Wget ・ Nano (text editor such as)

As before, let's start with the state of the tty console.

First, the source code

tinywm.c


/* TinyWM is written by Nick Welch <[email protected]>, 2005.
 *
 * This software is in the public domain
 * and is provided AS IS, with NO WARRANTY. */

#include <X11/Xlib.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main()
{
    Display * dpy;
    Window root;
    XWindowAttributes attr;
    XButtonEvent start;
    XEvent ev;

    if(!(dpy = XOpenDisplay(0x0))) return 1;

    root = DefaultRootWindow(dpy);

    XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), Mod1Mask, root,
            True, GrabModeAsync, GrabModeAsync);
    XGrabButton(dpy, 1, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
            GrabModeAsync, None, None);
    XGrabButton(dpy, 3, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync,
            GrabModeAsync, None, None);

    for(;;)
    {
        XNextEvent(dpy, &ev);
        if(ev.type == KeyPress && ev.xkey.subwindow != None)
            XRaiseWindow(dpy, ev.xkey.subwindow);
        else if(ev.type == ButtonPress && ev.xbutton.subwindow != None)
        {
            XGrabPointer(dpy, ev.xbutton.subwindow, True,
                    PointerMotionMask|ButtonReleaseMask, GrabModeAsync,
                    GrabModeAsync, None, None, CurrentTime);
            XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
            start = ev.xbutton;
        }
        else if(ev.type == MotionNotify)
        {
            int xdiff, ydiff;
            while(XCheckTypedEvent(dpy, MotionNotify, &ev));
            xdiff = ev.xbutton.x_root - start.x_root;
            ydiff = ev.xbutton.y_root - start.y_root;
            XMoveResizeWindow(dpy, ev.xmotion.window,
                attr.x + (start.button==1 ? xdiff : 0),
                attr.y + (start.button==1 ? ydiff : 0),
                MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
                MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
        }
        else if(ev.type == ButtonRelease)
            XUngrabPointer(dpy, CurrentTime);
    }
}

This is a copy of the source code of http://incise.org/tinywm.html.

If you want to download from the internet, there are the following methods.

mkdir ~/work1
cd ~/work1
wget https://raw.githubusercontent.com/mackstann/tinywm/master/tinywm.c

There are three places in the source code, Mod1Mask, which means the Alt key. With VirtualBox guests, the host side preferentially uses the Alt key, which doesn't work. Change all three to ControlMask.

nano tinywm.c
#Edit with a text editor

Then compile

gcc -o tinywm tinywm.c -lX11

You should have a file called tinywm. This is a freshly made window manager.

And start the X server

Set the window manager (tinywm) and xterm to run immediately after starting the X server.

echo "~/work1/tinywm& xterm" > ~/.xinitrc
#Caution,.Overwrite xinitrc, if it exists.

Type the following command to start the X server.

startx

VirtualBox_ArchLinux_26_01_2020_09_28_12.png

The function is ・ Ctrl + left click to move (originally Alt + left click) -Ctrl + right click to resize (originally Alt + right click) ・ Press Ctrl + F1 to bring it to the front (originally Alt + F1) There is only one, but it is much more comfortable than the previous "Are you an X window manager?" Where you entered commands to move and resize.

Shut down the X server

Like last time, with the first (.xinitrc) executed xterm

exit

Type to exit the X server and return to the first screen.

Finally

This time, I compiled the window manager from the source file and ran it. The source file is as short as 50 lines, and for those who find the window manager "severe and inaccessible", they realize that it's not much different from a normal X app, and they feel more familiar with it. Isn't it?

Recommended Posts

Self-made window manager (Do you have an X window manager? 2)
What you can and cannot do with Tensorflow 2.x