summaryrefslogtreecommitdiff
path: root/x-placement-manager.c
blob: 35a86445324ac6760a010a3c0ad2404367730384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
x-placement-manager - center misbehaving apps for no-wm
Written in 2010 by Patrick Haller

To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty. You should have
received a copy of the CC0 Public Domain Dedication along with this software.
If not, see http://creativecommons.org/publicdomain/zero/1.0/ */

#include <X11/Xlib.h>
#include <X11/Xproto.h>
#include <X11/cursorfont.h>

/* place all clients in the center
 */

void place_center(Display *d, XEvent *e);

static void (*handler[LASTEvent]) (Display *d, XEvent *) = {
    [MapRequest] = place_center,
    [ConfigureNotify] = place_center
};

/// fun on down

void
place_center(Display *d, XEvent *e) {
    static XWindowAttributes r_wa;
    static XWindowAttributes c_wa;
    Window r = DefaultRootWindow(d);
    Window trans = None;
    XMapRequestEvent *ev = &e->xmaprequest;
    int x, y;

    if (!XGetWindowAttributes(d, r, &r_wa))
        return;
    if (!XGetWindowAttributes(d, ev->window, &c_wa))
        return;
    if (c_wa.override_redirect)
        return;
    if(XGetTransientForHint(d, ev->window, &trans))
        return;
    x = r_wa.width/2 - c_wa.width/2;
    y = r_wa.height/2 - c_wa.height/2;
    if (x < 0)
        x = 0;
    if (y < 0)
        y = 0;
    XMoveResizeWindow(d, ev->window, x, y,
                      c_wa.width > r_wa.width ? r_wa.width : c_wa.width,
                      c_wa.height > r_wa.height ? r_wa.height : c_wa.height
                     );
}

int
main(int argc, char **argv) {
    Display *dpy;
    XEvent e;
    XSetWindowAttributes wa;

    if(!(dpy = XOpenDisplay(NULL)))
        return 1;

    wa.cursor = XCreateFontCursor(dpy, XC_left_ptr);
    wa.event_mask = SubstructureNotifyMask|StructureNotifyMask;
    XChangeWindowAttributes(dpy, DefaultRootWindow(dpy), CWEventMask|CWCursor, &wa);

    for(;;) {
        XNextEvent(dpy, &e);
        if(handler[e.type])
            handler[e.type](dpy, &e);
    }
}