Skip to content

Instantly share code, notes, and snippets.

@sdhand
Created June 20, 2018 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdhand/d39610dc3274649171e5c43cee527c13 to your computer and use it in GitHub Desktop.
Save sdhand/d39610dc3274649171e5c43cee527c13 to your computer and use it in GitHub Desktop.
//gcc centre.c -lxcb -lxcb-util -o centre
#include <stdio.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
int main (int argc, char **argv)
{
(void) argc;
(void) argv;
xcb_connection_t *c = xcb_connect(NULL, NULL);
if(xcb_connection_has_error(c)){
fputs("Failed to open display\n", stderr);
return 1;
}
xcb_screen_t *s = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
if(!s){
fputs("Couldn't get screen\n", stderr);
return 1;
}
xcb_get_input_focus_reply_t *f = xcb_get_input_focus_reply(c, xcb_get_input_focus(c), NULL);
if(!f){
fputs("Couldn't get focused window\n", stderr);
return 1;
}
xcb_window_t w = f->focus;
free(f);
if(w == s->root){
return 0;
}
xcb_get_geometry_reply_t *sg = xcb_get_geometry_reply(c, xcb_get_geometry(c, s->root), NULL);
if(!sg){
fputs("Couldn't get root window geometry\n", stderr);
return 1;
}
xcb_get_geometry_reply_t *fg = xcb_get_geometry_reply(c, xcb_get_geometry(c, w), NULL);
if(!fg){
fputs("Couldn't get focused window geometry\n", stderr);
return 1;
}
const uint32_t v[] = {(sg->width - fg->width)/2, (sg->height - fg->height)/2};
free(sg);
free(fg);
xcb_configure_window(c, w, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, v);
xcb_aux_sync(c);
if(c){
xcb_disconnect(c);
return 0;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment