x11_keyboard_hook.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_KEYBOARD_HOOK_H_
  5. #define UI_BASE_X_X11_KEYBOARD_HOOK_H_
  6. #include <vector>
  7. #include "base/containers/flat_set.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/threading/thread_checker.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "ui/gfx/native_widget_types.h"
  12. #include "ui/gfx/x/connection.h"
  13. namespace ui {
  14. enum class DomCode;
  15. class KeyEvent;
  16. // X11-specific implementation of the class that intercepts keyboard input.
  17. class COMPONENT_EXPORT(UI_BASE_X) XKeyboardHook {
  18. public:
  19. using KeyEventCallback = base::RepeatingCallback<void(KeyEvent* event)>;
  20. explicit XKeyboardHook(gfx::AcceleratedWidget accelerated_widget);
  21. XKeyboardHook(const XKeyboardHook&) = delete;
  22. XKeyboardHook& operator=(const XKeyboardHook&) = delete;
  23. virtual ~XKeyboardHook();
  24. protected:
  25. bool RegisterHook(const absl::optional<base::flat_set<DomCode>>& dom_codes);
  26. private:
  27. // Helper methods for setting up key event capture.
  28. void CaptureAllKeys();
  29. void CaptureSpecificKeys(
  30. const absl::optional<base::flat_set<DomCode>>& dom_codes);
  31. void CaptureKeyForDomCode(DomCode dom_code);
  32. THREAD_CHECKER(thread_checker_);
  33. // Tracks the keys that were grabbed.
  34. std::vector<int> grabbed_keys_;
  35. // The x11 default connection and the owner's native window.
  36. const raw_ptr<x11::Connection> connection_ = nullptr;
  37. const x11::Window x_window_ = x11::Window::None;
  38. };
  39. } // namespace ui
  40. #endif // UI_BASE_X_X11_KEYBOARD_HOOK_H_