Skip to content

Instantly share code, notes, and snippets.

@gammy
Created June 3, 2014 06:54
Show Gist options
  • Select an option

  • Save gammy/f2c74c7663d93e03bfeb to your computer and use it in GitHub Desktop.

Select an option

Save gammy/f2c74c7663d93e03bfeb to your computer and use it in GitHub Desktop.
Set window focus on desired window ID.
/* Set window focus on desired window ID.
gcc -Wall -lX11 raisefocus.c
*/
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
// This routine is taken directly from wmctr:main.c
static int client_msg(Display *disp, Window win, char *msg, /* {{{ */
unsigned long data0, unsigned long data1,
unsigned long data2, unsigned long data3,
unsigned long data4) {
XEvent event;
long mask = SubstructureRedirectMask | SubstructureNotifyMask;
event.xclient.type = ClientMessage;
event.xclient.serial = 0;
event.xclient.send_event = True;
event.xclient.message_type = XInternAtom(disp, msg, False);
event.xclient.window = win;
event.xclient.format = 32;
event.xclient.data.l[0] = data0;
event.xclient.data.l[1] = data1;
event.xclient.data.l[2] = data2;
event.xclient.data.l[3] = data3;
event.xclient.data.l[4] = data4;
if (XSendEvent(disp, DefaultRootWindow(disp), False, mask, &event)) {
return EXIT_SUCCESS;
}
else {
fprintf(stderr, "Cannot send %s event.\n", msg);
return EXIT_FAILURE;
}
}/*}}}*/
int main(int argc, char *argv[]) {
if(argc < 2) {
fprintf(stderr, "Usage: %s <window id>\n",
basename(argv[0]));
return(EXIT_SUCCESS);
}
Window winid = strtol(argv[1], NULL, 0);
printf("Window id: 0x%lx\n", winid);
Display *disp = XOpenDisplay(NULL);
if (! disp) {
fputs("Cannot open display.\n", stderr);
return(EXIT_FAILURE);
}
int r = client_msg(disp,
winid,
"_NET_ACTIVE_WINDOW",
0, 0, 0, 0, 0);
XCloseDisplay(disp);
return(r);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment