x11_user_input_monitor.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 UI_BASE_X_X11_USER_INPUT_MONITOR_H_
  5. #define UI_BASE_X_X11_USER_INPUT_MONITOR_H_
  6. #include <memory>
  7. #include "base/component_export.h"
  8. #include "base/files/file_descriptor_watcher_posix.h"
  9. #include "base/memory/shared_memory_mapping.h"
  10. #include "base/task/current_thread.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "ui/events/keyboard_event_counter.h"
  13. #include "ui/gfx/x/connection.h"
  14. #include "ui/gfx/x/xinput.h"
  15. namespace ui {
  16. // This is the actual implementation of event monitoring. It's separated from
  17. // UserInputMonitorLinux since it needs to be deleted on the IO thread.
  18. class COMPONENT_EXPORT(UI_BASE_X) XUserInputMonitor
  19. : public base::SupportsWeakPtr<XUserInputMonitor>,
  20. public base::CurrentThread::DestructionObserver,
  21. public x11::EventObserver {
  22. public:
  23. using WriteKeyPressCallback = base::RepeatingCallback<
  24. void(const base::WritableSharedMemoryMapping& shmem, uint32_t count)>;
  25. explicit XUserInputMonitor(
  26. const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
  27. XUserInputMonitor(const XUserInputMonitor&) = delete;
  28. XUserInputMonitor& operator=(const XUserInputMonitor&) = delete;
  29. ~XUserInputMonitor() override;
  30. uint32_t GetKeyPressCount() const;
  31. void StartMonitor(WriteKeyPressCallback& callback);
  32. void StartMonitorWithMapping(WriteKeyPressCallback& callback,
  33. base::WritableSharedMemoryMapping mapping);
  34. void StopMonitor();
  35. private:
  36. // base::CurrentThread::DestructionObserver:
  37. void WillDestroyCurrentMessageLoop() override;
  38. // x11::EventObserver:
  39. void OnEvent(const x11::Event& event) override;
  40. void OnConnectionData();
  41. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  42. // Used for sharing key press count value.
  43. std::unique_ptr<base::WritableSharedMemoryMapping> key_press_count_mapping_;
  44. // The following members should only be accessed on the IO thread.
  45. std::unique_ptr<base::FileDescriptorWatcher::Controller> watch_controller_;
  46. std::unique_ptr<x11::Connection> connection_;
  47. KeyboardEventCounter counter_;
  48. WriteKeyPressCallback write_key_press_callback_;
  49. };
  50. } // namespace ui
  51. #endif // UI_BASE_X_X11_USER_INPUT_MONITOR_H_