ウィンドウの移動監視

xwatch : ターゲットウィンドウ window_id の移動とリサイズを監視する

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>

int main(int argc, char **argv)
{
    Display         *d;
    Window          w;
    int             x, y;
    long            mask = StructureNotifyMask;
    XEvent          e;
    XConfigureEvent *xce;

    /* 起動引数処理 */
    if (argc < 2) {
        fprintf(stderr, "usage: xwatch window_id\n");
        exit(1);
    }
    w = (Window)strtol(argv[1], NULL, 0);

    /* ターゲットウィンドウの移動を監視する */
    d = XOpenDisplay(NULL);
    XSelectInput(d, w, mask);
    for (;;) {
        XNextEvent(d, &e);
        switch (e.type) {
        case ConfigureNotify:
            xce = (XConfigureEvent*)&e;
            fprintf(stderr, "<XEVENT> type=%d window=0x%x\n", xce->type, xce->window);
            if (xce->window == w) {
                fprintf(stderr, "ConfigureNotify :\n");
                fprintf(stderr, "    window = 0x%x\n", xce->window);
                fprintf(stderr, "    x      = %d\n",   xce->x);
                fprintf(stderr, "    y      = %d\n",   xce->y);
                fprintf(stderr, "    w      = %d\n",   xce->width);
                fprintf(stderr, "    h      = %d\n",   xce->height);
                continue;
            }
            break;
        default:
            break;
        }
    }
    XCloseDisplay(d);
    return 0;
}