summaryrefslogtreecommitdiff
path: root/chromium/ui/events/ozone/keyboard/event_auto_repeat_handler.h
blob: c17e9e1a54564b162de36ed566bdac94934680de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_EVENTS_OZONE_KEYBOARD_EVENT_AUTO_REPEAT_HANDLER_H_
#define UI_EVENTS_OZONE_KEYBOARD_EVENT_AUTO_REPEAT_HANDLER_H_

#include "base/callback.h"
#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"

namespace ui {

class COMPONENT_EXPORT(EVENTS_OZONE) EventAutoRepeatHandler {
 public:
  class Delegate {
   public:
    // Gives the client a chance to flush the input queue
    // cancelling possible spurios auto repeat keys.
    // Useful under janky situations.
    virtual void FlushInput(base::OnceClosure closure) = 0;
    virtual void DispatchKey(unsigned int key,
                             unsigned int scan_code,
                             bool down,
                             bool repeat,
                             base::TimeTicks timestamp,
                             int device_id,
                             int flags) = 0;
  };

  explicit EventAutoRepeatHandler(Delegate* delegate);

  EventAutoRepeatHandler(const EventAutoRepeatHandler&) = delete;
  EventAutoRepeatHandler& operator=(const EventAutoRepeatHandler&) = delete;

  ~EventAutoRepeatHandler();

  void UpdateKeyRepeat(unsigned int key,
                       unsigned int scan_code,
                       bool down,
                       bool suppress_auto_repeat,
                       int device_id);
  void StopKeyRepeat();

  // Configuration for key repeat.
  bool IsAutoRepeatEnabled();
  void SetAutoRepeatEnabled(bool enabled);
  void SetAutoRepeatRate(const base::TimeDelta& delay,
                         const base::TimeDelta& interval);
  void GetAutoRepeatRate(base::TimeDelta* delay, base::TimeDelta* interval);

 private:
  static constexpr unsigned int kInvalidKey = 0;

  void StartKeyRepeat(unsigned int key, unsigned int scan_code, int device_id);
  void ScheduleKeyRepeat(const base::TimeDelta& delay);
  void OnRepeatTimeout(unsigned int sequence);
  void OnRepeatCommit(unsigned int sequence);

  // Key repeat state.
  bool auto_repeat_enabled_ = true;
  unsigned int repeat_key_ = kInvalidKey;
  unsigned int repeat_scan_code_ = 0;
  unsigned int repeat_sequence_ = 0;
  int repeat_device_id_ = 0;
  base::TimeDelta repeat_delay_;
  base::TimeDelta repeat_interval_;

  Delegate* delegate_ = nullptr;

  base::WeakPtrFactory<EventAutoRepeatHandler> weak_ptr_factory_{this};
};

}  // namespace ui

#endif  // UI_EVENTS_OZONE_KEYBOARD_EVENT_AUTO_REPEAT_HANDLER_H_