Created
July 1, 2020 20:59
-
-
Save boulabiar/7f6f03daf80c8ff76749a967ebe8f9e1 to your computer and use it in GitHub Desktop.
Get the FocusIn FocusOut events from xcb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sources: | |
// https://xcb.freedesktop.org/tutorial/events/ | |
// https://xcb.freedesktop.org/manual/group__XCB____API.html | |
// https://github.com/wmutils/opt/blob/master/wew.c | |
// | |
// compile with: | |
// gcc focus.c -lxcb | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <inttypes.h> | |
#include <xcb/xcb.h> | |
int | |
main () | |
{ | |
/* Open the connection to the X server */ | |
xcb_connection_t *connection = xcb_connect (NULL, NULL); | |
/* Get the first screen */ | |
xcb_screen_t *screen = xcb_setup_roots_iterator (xcb_get_setup (connection)).data; | |
/* Create the window */ | |
xcb_window_t window = xcb_generate_id (connection); | |
uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; | |
uint32_t values[2] = {screen->white_pixel, | |
XCB_EVENT_MASK_FOCUS_CHANGE }; | |
xcb_create_window (connection, | |
0, /* depth */ | |
window, | |
screen->root, /* parent window */ | |
0, 0, /* x, y */ | |
150, 150, /* width, height */ | |
10, /* border_width */ | |
XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */ | |
screen->root_visual, /* visual */ | |
mask, values ); /* masks */ | |
/* Map the window on the screen */ | |
xcb_map_window (connection, window); | |
xcb_flush (connection); | |
xcb_generic_event_t *event; | |
while ( (event = xcb_wait_for_event (connection)) ) { | |
switch (event->response_type & ~0x80) { | |
case XCB_EXPOSE: { | |
xcb_expose_event_t *expose = (xcb_expose_event_t *)event; | |
printf ("Window %"PRIu32" exposed. Region to be redrawn at location (%"PRIu16",%"PRIu16"), with dimension (%"PRIu16",%"PRIu16")\n", | |
expose->window, expose->x, expose->y, expose->width, expose->height ); | |
break; | |
} | |
case XCB_FOCUS_IN: { | |
xcb_focus_in_event_t *kr = (xcb_focus_in_event_t *)event; | |
printf ("Focus In %"PRIu32"\n", | |
kr->event); | |
break; | |
} | |
case XCB_FOCUS_OUT: { | |
xcb_focus_in_event_t *kr = (xcb_focus_in_event_t *)event; | |
printf ("Focus Out %"PRIu32"\n", | |
kr->event); | |
break; | |
} | |
default: | |
/* Unknown event type, ignore it */ | |
printf ("Unknown event: %"PRIu8"\n", | |
event->response_type); | |
break; | |
} | |
free (event); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// modified example from | |
// https://github.com/enn/xcb-examples/blob/master/window_event.c | |
// compile with: | |
// gcc focus.c -lxcb | |
// important : | |
// When you move the window using the topbar or click the bar itself, you have FocusOut while doing so. | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <inttypes.h> | |
#include <xcb/xcb.h> | |
xcb_connection_t *c; | |
xcb_screen_t *screen; | |
/* win */ | |
xcb_drawable_t win; | |
/* stard/end point storage for line drawing */ | |
xcb_point_t line_start; | |
xcb_point_t line_end; | |
xcb_drawable_t create_sub_window(xcb_drawable_t parent, xcb_gravity_t gravity) { | |
xcb_drawable_t swin; | |
uint32_t mask = 0; | |
uint32_t values4[4]; | |
swin = xcb_generate_id(c); | |
mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_WIN_GRAVITY | XCB_CW_EVENT_MASK; | |
values4[0] = screen->white_pixel; | |
values4[1] = screen->black_pixel; | |
values4[2] = gravity; | |
values4[3] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_KEY_PRESS; | |
xcb_create_window (c, | |
XCB_COPY_FROM_PARENT, | |
swin, | |
parent, | |
0, 0, | |
50, 50, | |
2, | |
XCB_WINDOW_CLASS_INPUT_OUTPUT, | |
screen->root_visual, | |
mask, values4); | |
return swin; | |
} | |
void create_window() { | |
uint32_t mask = 0; | |
uint32_t values[3]; | |
win = xcb_generate_id(c); | |
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; | |
values[0] = screen->white_pixel; | |
values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_FOCUS_CHANGE ; | |
xcb_create_window (c, | |
screen->root_depth, | |
win, | |
screen->root, | |
0, 0, | |
54,54, /* border size big enough for the subwindows to fit in */ | |
0, | |
XCB_WINDOW_CLASS_INPUT_OUTPUT, | |
screen->root_visual, | |
mask, values); | |
xcb_map_window (c, win); | |
} | |
void event_loop() { | |
xcb_generic_event_t *e; | |
while ((e = xcb_wait_for_event (c))) { | |
switch (e->response_type & ~0x80) { | |
case XCB_FOCUS_IN: { | |
xcb_focus_in_event_t *kr = (xcb_focus_in_event_t *)e; | |
printf ("Focus In %"PRIu32"\n", | |
kr->event); | |
break; | |
} | |
case XCB_FOCUS_OUT: { | |
xcb_focus_in_event_t *kr = (xcb_focus_in_event_t *)e; | |
printf ("Focus Out %"PRIu32"\n", | |
kr->event); | |
break; | |
} | |
/* ESC to exit */ | |
case XCB_KEY_PRESS: { | |
xcb_key_press_event_t *ev; | |
ev = (xcb_key_press_event_t *)e; | |
if (ev->detail == 9) return; | |
break; | |
} | |
} | |
free (e); | |
} | |
} | |
int main(void) { | |
int i; | |
/* Open the connection to the X server */ | |
c = xcb_connect (NULL, NULL); | |
/* Get the first screen */ | |
screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data; | |
/* make the main window */ | |
create_window(); | |
create_window(); | |
create_window(); | |
create_window(); | |
/* Create a subwindow for each gravity type */ | |
create_sub_window(win, XCB_GRAVITY_NORTH_WEST); | |
create_sub_window(win, XCB_GRAVITY_NORTH_EAST); | |
create_sub_window(win, XCB_GRAVITY_SOUTH_WEST); | |
create_sub_window(win, XCB_GRAVITY_SOUTH_EAST); | |
/* map the subwindows */ | |
xcb_map_subwindows(c, win); | |
xcb_flush(c); | |
event_loop(); | |
puts("bye!"); | |
return EXIT_SUCCESS; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment