xkb_tracker.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_EXO_XKB_TRACKER_H_
  5. #define COMPONENTS_EXO_XKB_TRACKER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ui/base/buildflags.h"
  9. #if BUILDFLAG(USE_XKBCOMMON)
  10. #include <xkbcommon/xkbcommon.h>
  11. #include "base/memory/free_deleter.h"
  12. #include "ui/events/event_constants.h"
  13. #include "ui/events/keycodes/scoped_xkb.h" // nogncheck
  14. #include "ui/events/ozone/layout/xkb/xkb_modifier_converter.h"
  15. #endif
  16. namespace exo {
  17. struct KeyboardModifiers;
  18. // Tracks the state of XKB. If Chrome is configured not to link against
  19. // libxkbcommon this class is empty.
  20. // TODO(hidehiko): Share the state between wl_keyboard and zwp_text_input.
  21. class XkbTracker {
  22. public:
  23. XkbTracker();
  24. XkbTracker(const XkbTracker&) = delete;
  25. XkbTracker& operator=(const XkbTracker&) = delete;
  26. ~XkbTracker();
  27. #if BUILDFLAG(USE_XKBCOMMON)
  28. // Updates the XKB keymap based on the given keyboard layout name.
  29. void UpdateKeyboardLayout(const std::string& name);
  30. // Updates the XKB modifier state. |modifier_flags| is a bitset of
  31. // ui::EventFlags.
  32. void UpdateKeyboardModifiers(int modifier_flags);
  33. // Returns the keysym for the given XKB keycode, based on the current
  34. // keymap and its modifier state.
  35. uint32_t GetKeysym(uint32_t xkb_keycode) const;
  36. // Returns the XKB keymap data.
  37. std::unique_ptr<char, base::FreeDeleter> GetKeymap() const;
  38. // Returns the current keyboard modifiers.
  39. KeyboardModifiers GetModifiers() const;
  40. private:
  41. void UpdateKeyboardLayoutInternal(const xkb_rule_names* names);
  42. void UpdateKeyboardModifiersInternal();
  43. // Keeps the modifiers, so that modifiers can be recalculated
  44. // on keyboard layout update.
  45. // For CrOS we treat numlock as always on.
  46. int modifier_flags_ = ui::EF_NUM_LOCK_ON;
  47. // The XKB state used for the keyboard.
  48. std::unique_ptr<xkb_context, ui::XkbContextDeleter> xkb_context_{
  49. xkb_context_new(XKB_CONTEXT_NO_FLAGS)};
  50. std::unique_ptr<xkb_keymap, ui::XkbKeymapDeleter> xkb_keymap_;
  51. std::unique_ptr<xkb_state, ui::XkbStateDeleter> xkb_state_;
  52. ui::XkbModifierConverter xkb_modifier_converter_{{}};
  53. #endif // BUILDFLAG(USE_XKBCOMMON)
  54. };
  55. } // namespace exo
  56. #endif // COMPONENTS_EXO_XKB_TRACKER_H_