x11_global_shortcut_listener.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2021 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 UI_BASE_X_X11_GLOBAL_SHORTCUT_LISTENER_H_
  5. #define UI_BASE_X_X11_GLOBAL_SHORTCUT_LISTENER_H_
  6. #include <stdint.h>
  7. #include <set>
  8. #include "base/memory/raw_ptr.h"
  9. #include "ui/events/keycodes/keyboard_codes.h"
  10. #include "ui/events/platform/platform_event_dispatcher.h"
  11. #include "ui/gfx/x/xproto.h"
  12. namespace x11 {
  13. class Connection;
  14. }
  15. namespace ui {
  16. class KeyEvent;
  17. // X11-specific implementation of the class that listens for global shortcuts.
  18. class COMPONENT_EXPORT(UI_BASE_X) XGlobalShortcutListener
  19. : public PlatformEventDispatcher {
  20. public:
  21. XGlobalShortcutListener();
  22. XGlobalShortcutListener(const XGlobalShortcutListener&) = delete;
  23. XGlobalShortcutListener& operator=(const XGlobalShortcutListener&) = delete;
  24. ~XGlobalShortcutListener() override;
  25. // PlatformEventDispatcher:
  26. bool CanDispatchEvent(const PlatformEvent& event) override;
  27. uint32_t DispatchEvent(const PlatformEvent& event) override;
  28. protected:
  29. // Called when the previously registered key combination is pressed.
  30. // The implementation should forward the output to the owner.
  31. virtual void OnKeyPressed(KeyboardCode key_code,
  32. bool is_alt_down,
  33. bool is_ctrl_down,
  34. bool is_shift_down) = 0;
  35. void StartListening();
  36. void StopListening();
  37. bool RegisterAccelerator(KeyboardCode key_code,
  38. bool is_alt_down,
  39. bool is_ctrl_down,
  40. bool is_shift_down);
  41. void UnregisterAccelerator(KeyboardCode key_code,
  42. bool is_alt_down,
  43. bool is_ctrl_down,
  44. bool is_shift_down);
  45. private:
  46. // Due to how system key grabbing works on X11, we have to be a bit greedy and
  47. // register combinations that we will later reject (see the comment for
  48. // kModifiersMasks in the cc file). For that we store registered combinations
  49. // and filter the incoming events against that registry before notifying the
  50. // observer. This tuple describes the meaningful parts of the event; booleans
  51. // 1, 2, and 3 hold states of Alt, Control, and Shift keys, respectively.
  52. using Accelerator = std::tuple<KeyboardCode, bool, bool, bool>;
  53. // Invoked when a global shortcut is pressed.
  54. void OnKeyPressEvent(const KeyEvent& event);
  55. // Whether this object is listening for global shortcuts.
  56. bool is_listening_ = false;
  57. // Key combinations that we are interested in.
  58. std::set<Accelerator> registered_combinations_;
  59. // The x11 default display and the native root window.
  60. raw_ptr<x11::Connection> connection_;
  61. x11::Window x_root_window_;
  62. };
  63. } // namespace ui
  64. #endif // UI_BASE_X_X11_GLOBAL_SHORTCUT_LISTENER_H_