I am working with X11 and want to perform a re-parent. I got an example hello world application from Rosetta Code in C. I made some modification to make it 2 windows.
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void) {
Display *d;
Window w, w2;
XEvent e;
const char *msg = "Hello, World!";
int s;
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 1000, 1000, 1,
BlackPixel(d, s), WhitePixel(d, s));
w2 = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 100, 100, 1,
BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w); //map the bigger window
XSelectInput(d, w2, ExposureMask | KeyPressMask);
XMapWindow(d, w2); //map the smaller window
XFlush(d);
XUnmapWindow(d, w2); //unmap the smaller window to reparent it
XFlush(d);
XReparentWindow(d, w2, w, 500, 500);
XMapWindow(d, w2); //remap the smaller window
while (1) {
XNextEvent(d, &e);
if (e.type == Expose) {
XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
XFillRectangle(d, w2, DefaultGC(d, s), 20, 20, 10, 10);
XDrawString(d, w2, DefaultGC(d, s), 10, 50, msg, strlen(msg));
}
if (e.type == KeyPress)
break;
}
XCloseDisplay(d);
return 0;
}
But this code doesn't work; it displays two separate windows:
But when I remove the lines labeled map the smaller window and unmap the smaller window to reparent it, I get the correct result:
Shouldn't both yield the same correct result. All I am doing in the original is mapping, then immediately unmapping.


