void Initialize()

in src/keyboard_x.cc [29:83]


  void Initialize(Display* display) {
    alt_modifier = 0;
    meta_modifier = 0;
    num_lock_modifier = 0;
    mode_switch_modifier = 0;
    level3_modifier = 0;  // AltGr is often mapped to the level3 modifier
    level5_modifier = 0;  // AltGr is mapped to the level5 modifier in the Neo layout family
    effective_group_index = 0;

    if (!display) {
      return;
    }

    // See https://www.x.org/releases/X11R7.6/doc/libX11/specs/XKB/xkblib.html#determining_keyboard_state
    XkbStateRec xkbState;
    XkbGetState(display, XkbUseCoreKbd, &xkbState);
    effective_group_index = xkbState.group;

    XModifierKeymap* mod_map = XGetModifierMapping(display);
    int max_mod_keys = mod_map->max_keypermod;
    for (int mod_index = 0; mod_index <= 8; ++mod_index) {
      for (int key_index = 0; key_index < max_mod_keys; ++key_index) {
        int key = mod_map->modifiermap[mod_index * max_mod_keys + key_index];
        if (!key) {
          continue;
        }

        int keysym = XkbKeycodeToKeysym(display, key, 0, 0);
        if (!keysym) {
          continue;
        }

        if (keysym == XK_Alt_L || keysym == XK_Alt_R) {
          alt_modifier = 1 << mod_index;
        }
        if (keysym == XK_Mode_switch) {
          mode_switch_modifier = 1 << mod_index;
        }
        if (keysym == XK_Meta_L || keysym == XK_Super_L || keysym == XK_Meta_R || keysym == XK_Super_R) {
          meta_modifier = 1 << mod_index;
        }
        if (keysym == XK_Num_Lock) {
          num_lock_modifier = 1 << mod_index;
        }
        if (keysym == XK_ISO_Level3_Shift) {
          level3_modifier = 1 << mod_index;
        }
        if (keysym == XK_ISO_Level5_Shift) {
          level5_modifier = 1 << mod_index;
        }
      }
    }

    XFreeModifiermap(mod_map);
  }