- /* …we handle all events in a row: */
- do {
- xcb_event_handle(&evenths, event);
- xcb_aux_sync(evenths.c);
- free(event);
- } while ((event = xcb_poll_for_event(evenths.c)));
-
- /* Make sure all replies are handled/discarded */
- xcb_aux_sync(evenths.c);
-
- /* Afterwards, there may be new events available which would
- * not trigger the select() (libev) immediately, so we check
- * again (and don’t bail out of the loop). */
+ xcb_event_handle(&evenths, event);
+ free(event);
+ }
+}
+
+/*
+ * When using xmodmap to change the keyboard mapping, this event
+ * is only sent via XKB. Therefore, we need this special handler.
+ *
+ */
+static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
+ DLOG("Handling XKB event\n");
+ XkbEvent ev;
+
+ /* When using xmodmap, every change (!) gets an own event.
+ * Therefore, we just read all events and only handle the
+ * mapping_notify once. */
+ bool mapping_changed = false;
+ while (XPending(xkbdpy)) {
+ XNextEvent(xkbdpy, (XEvent*)&ev);
+ /* While we should never receive a non-XKB event,
+ * better do sanity checking */
+ if (ev.type != xkb_event_base)
+ continue;
+
+ if (ev.any.xkb_type == XkbMapNotify) {
+ mapping_changed = true;
+ continue;
+ }
+
+ if (ev.any.xkb_type != XkbStateNotify) {
+ ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
+ continue;
+ }
+
+ /* See The XKB Extension: Library Specification, section 14.1 */
+ /* We check if the current group (each group contains
+ * two levels) has been changed. Mode_switch activates
+ * group XkbGroup2Index */
+ if (xkb_current_group == ev.state.group)
+ continue;
+
+ xkb_current_group = ev.state.group;
+
+ if (ev.state.group == XkbGroup2Index) {
+ DLOG("Mode_switch enabled\n");
+ grab_all_keys(global_conn, true);
+ }
+
+ if (ev.state.group == XkbGroup1Index) {
+ DLOG("Mode_switch disabled\n");
+ ungrab_all_keys(global_conn);
+ grab_all_keys(global_conn, false);
+ }