Sometimes you want to activate a window that doesn't belong to you. I wasted way too much time for that knowledge to be lost, so I'm just going to share it here. One would think XRaiseWindow
is straightforward enough, but no. Not at all.
#include <X11/Xlib.h>
int WindowBringToFront(unsigned long id)
{
Display* display = XOpenDisplay(NULL); // you probably want to do this somewhere else
if (!display)
return -1;
XEvent wm_event = { 0 };
wm_event.xclient.type = ClientMessage;
wm_event.xclient.send_event = true;
wm_event.xclient.message_type = XInternAtom(
display
"_NET_ACTIVE_WINDOW",
true
);
wm_event.xclient.window = (Window)id;
wm_event.xclient.format = 32; // man 3 XSendEvent
Status xresult = XSendEvent(
display,
DefaultRootWindow(display),
false,
SubstructureRedirectMask
| SubstructureNotifyMask,
&wm_event
);
if (!xresult)
return -1;
return 0;
}
Code released under MIT license. Grab it, fellow corporate code monkey.