Guest User

Untitled

a guest
Oct 26th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <xcb/xcb.h>
  6. #include <xcb/xcb_ewmh.h>
  7.  
  8. xcb_atom_t intern_atom(xcb_connection_t *c, const char *name){
  9.     xcb_intern_atom_cookie_t atomCookie;
  10.     xcb_intern_atom_reply_t *atomReply;
  11.  
  12.     atomCookie = xcb_intern_atom(c, 0, strlen(name), name);
  13.     atomReply = xcb_intern_atom_reply(c, atomCookie, NULL);
  14.  
  15.     xcb_atom_t res = atomReply->atom;
  16.     free(atomReply);
  17.     return res;
  18. }
  19.  
  20. int main(void){
  21.     xcb_ewmh_connection_t c;
  22.     xcb_screen_t *screen;
  23.  
  24.     xcb_intern_atom_cookie_t *cookies;
  25.     c.connection = xcb_connect(NULL, NULL);
  26.     cookies = xcb_ewmh_init_atoms(c.connection, &c);
  27.     xcb_ewmh_init_atoms_replies(&c, cookies, NULL);
  28.  
  29.     xcb_atom_t activeWindowAtom = intern_atom(c.connection, "_NET_ACTIVE_WINDOW");
  30.     if(activeWindowAtom == XCB_NONE){
  31.         xcb_disconnect(c.connection);
  32.         return EXIT_FAILURE;
  33.     }
  34.  
  35.     const xcb_setup_t *setup = xcb_get_setup(c.connection);
  36.     screen = xcb_setup_roots_iterator(setup).data;
  37.     uint32_t values[] = {XCB_EVENT_MASK_PROPERTY_CHANGE};
  38.     xcb_change_window_attributes(c.connection, screen->root, XCB_CW_EVENT_MASK, values);
  39.  
  40.     xcb_flush(c.connection);
  41.  
  42.     xcb_generic_event_t *ev;
  43.     while((ev = xcb_wait_for_event(c.connection)) != NULL){
  44.         if(ev->response_type == XCB_PROPERTY_NOTIFY){
  45.             xcb_get_property_cookie_t cookie;
  46.             xcb_window_t activeWindow;
  47.  
  48.             cookie = xcb_ewmh_get_active_window(&c, 0);
  49.             xcb_ewmh_get_active_window_reply(&c, cookie, &activeWindow, NULL);
  50.  
  51.             printf("active window id: %u\n", activeWindow);
  52.         }
  53.     }
  54.  
  55.  
  56.     xcb_disconnect(c.connection);
  57.  
  58.     return EXIT_SUCCESS;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment