napi_value _GetCurrentKeyboardLayout()

in src/keyboard_x.cc [245:277]


napi_value _GetCurrentKeyboardLayout(napi_env env, napi_callback_info info) {

  napi_value result;
  NAPI_CALL(env, napi_get_null(env, &result));

  Display *display;
  if (!(display = XOpenDisplay(""))) {
    return result;
  }

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

  XkbRF_VarDefsRec vdr;
  char *tmp = NULL;
  int res = XkbRF_GetNamesProp(display, &tmp, &vdr);
  if (res) {
    NAPI_CALL(env, napi_create_object(env, &result));

    NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "model", vdr.model ? vdr.model : ""));
    NAPI_CALL(env, napi_set_named_property_int32(env, result, "group", effective_group_index));
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "layout", vdr.layout ? vdr.layout : ""));
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "variant", vdr.variant ? vdr.variant : ""));
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "options", vdr.options ? vdr.options : ""));
    NAPI_CALL(env, napi_set_named_property_string_utf8(env, result, "rules", tmp ? tmp : ""));
  }

  XFlush(display);
  XCloseDisplay(display);
  return result;
}