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 
5 #ifndef PDF_PPAPI_MIGRATION_INPUT_EVENT_CONVERSIONS_H_
6 #define PDF_PPAPI_MIGRATION_INPUT_EVENT_CONVERSIONS_H_
7 
8 #include <stdint.h>
9 #include <string>
10 
11 #include "ui/gfx/geometry/point.h"
12 #include "ui/gfx/geometry/point_f.h"
13 
14 namespace pp {
15 class KeyboardInputEvent;
16 class MouseInputEvent;
17 class TouchInputEvent;
18 }  // namespace pp
19 
20 namespace chrome_pdf {
21 
22 enum InputEventModifier : uint32_t {
23   // None represents no modifier key specified.
24   kInputEventModifierNone = 0,
25   kInputEventModifierShiftKey = 1 << 0,
26   kInputEventModifierControlKey = 1 << 1,
27   kInputEventModifierAltKey = 1 << 2,
28   kInputEventModifierMetaKey = 1 << 3,
29   kInputEventModifierIsKeyPad = 1 << 4,
30   kInputEventModifierIsAutoRepeat = 1 << 5,
31   kInputEventModifierLeftButtonDown = 1 << 6,
32   kInputEventModifierMiddleButtonDown = 1 << 7,
33   kInputEventModifierRightButtonDown = 1 << 8,
34   kInputEventModifierCapsLockKey = 1 << 9,
35   kInputEventModifierNumLockKey = 1 << 10,
36   kInputEventModifierIsLeft = 1 << 11,
37   kInputEventModifierIsRight = 1 << 12,
38   kInputEventModifierIsPen = 1 << 13,
39   kInputEventModifierIsEraser = 1 << 14
40 };
41 
42 enum class InputEventType {
43   kNone,
44 
45   // Notification that a mouse button was pressed.
46   kMouseDown,
47 
48   // Notification that a mouse button was released.
49   kMouseUp,
50 
51   // Notification that a mouse button was moved when it is over the instance
52   // or dragged out of it.
53   kMouseMove,
54 
55   // Notification that the mouse entered the pdf view's bounds.
56   kMouseEnter,
57 
58   // Notification that a mouse left the pdf view's bounds.
59   kMouseLeave,
60 
61   // Notification that the scroll wheel was used.
62   kWheel,
63 
64   // Notification that a key transitioned from "up" to "down".
65   kRawKeyDown,
66 
67   // Notification that a key was pressed. This does not necessarily correspond
68   // to a character depending on the key and language. Use the
69   // kChar for character input.
70   kKeyDown,
71 
72   // Notification that a key was released.
73   kKeyUp,
74 
75   // Notification that a character was typed. Use this for text input. Key
76   // down events may generate 0, 1, or more than one character event depending
77   // on the key, locale, and operating system.
78   kChar,
79 
80   kContextMenu,
81 
82   // Notification that an input method composition process has just started.
83   kImeCompositionStart,
84 
85   // Notification that the input method composition string is updated.
86   kImeCompositionUpdate,
87 
88   // Notification that an input method composition process has completed.
89   kImeCompositionEnd,
90 
91   // Notification that an input method committed a string.
92   kImeText,
93 
94   // Notification that a finger was placed on a touch-enabled device.
95   kTouchStart,
96 
97   // Notification that a finger was moved on a touch-enabled device.
98   kTouchMove,
99 
100   // Notification that a finger was released on a touch-enabled device.
101   kTouchEnd,
102 
103   // Notification that a touch event was canceled.
104   kTouchCancel
105 };
106 
107 enum class InputEventMouseButtonType {
108   kNone = 0,
109   kLeft,
110   kMiddle,
111   kRight,
112 };
113 
114 class InputEvent {
115  public:
GetEventType()116   InputEventType GetEventType() const { return event_type_; }
117 
GetTimeStamp()118   double GetTimeStamp() const { return time_stamp_; }
119 
GetModifiers()120   uint32_t GetModifiers() const { return modifiers_; }
121 
122  protected:
123   // Base class is not intended to be instantiated directly. Use
124   // `NoneInputEvent` to get an event of type `InputEventType::kNone`.
125   InputEvent(InputEventType event_type, double time_stamp, uint32_t modifiers);
126   InputEvent(const InputEvent& other);
127   InputEvent& operator=(const InputEvent& other);
128   ~InputEvent();
129 
130  private:
131   InputEventType event_type_;
132   // The units are in seconds, but are not measured relative to any particular
133   // epoch, so the most you can do is compare two values.
134   double time_stamp_;
135   uint32_t modifiers_;
136 };
137 
138 class KeyboardInputEvent : public InputEvent {
139  public:
140   KeyboardInputEvent(InputEventType event_type,
141                      double time_stamp,
142                      uint32_t modifiers,
143                      uint32_t keyboard_code,
144                      const std::string& key_char);
145   KeyboardInputEvent(const KeyboardInputEvent& other);
146   KeyboardInputEvent& operator=(const KeyboardInputEvent& other);
147   ~KeyboardInputEvent();
148 
GetKeyCode()149   uint32_t GetKeyCode() const { return keyboard_code_; }
150 
GetKeyChar()151   const std::string& GetKeyChar() const { return key_char_; }
152 
153  private:
154   uint32_t keyboard_code_;
155   std::string key_char_;
156 };
157 
158 class MouseInputEvent : public InputEvent {
159  public:
160   MouseInputEvent(InputEventType event_type,
161                   double time_stamp,
162                   uint32_t modifier,
163                   InputEventMouseButtonType mouse_button_type,
164                   const gfx::Point& point,
165                   int32_t click_count,
166                   const gfx::Point& movement);
167   MouseInputEvent(const MouseInputEvent& other);
168   MouseInputEvent& operator=(const MouseInputEvent& other);
169   ~MouseInputEvent();
170 
GetButton()171   const InputEventMouseButtonType& GetButton() const {
172     return mouse_button_type_;
173   }
174 
GetPosition()175   const gfx::Point& GetPosition() const { return point_; }
176 
GetClickCount()177   int32_t GetClickCount() const { return click_count_; }
178 
GetMovement()179   const gfx::Point& GetMovement() const { return movement_; }
180 
181  private:
182   InputEventMouseButtonType mouse_button_type_;
183   gfx::Point point_;
184   int32_t click_count_ = 0;
185   gfx::Point movement_;
186 };
187 
188 class TouchInputEvent : public InputEvent {
189  public:
190   TouchInputEvent(InputEventType event_type,
191                   double time_stamp,
192                   uint32_t modifiers,
193                   const gfx::PointF& target_touch_point,
194                   int32_t touch_count);
195   TouchInputEvent(const TouchInputEvent& other);
196   TouchInputEvent& operator=(const TouchInputEvent& other);
197   ~TouchInputEvent();
198 
199   // `pp::TouchInputEvent` exposes a collection of target touch points. We can
200   // get all the points by passing the index of the point in the collection.
201   // However, with `chrome_pdf::TouchEvent` the number of target touch points
202   // are restricted to the first point. This is because PDFiumeEngine at present
203   // is dependent on only the first target touch point.
GetTargetTouchPoint()204   const gfx::PointF& GetTargetTouchPoint() const { return target_touch_point_; }
205 
GetTouchCount()206   int32_t GetTouchCount() const { return touch_count_; }
207 
208  private:
209   gfx::PointF target_touch_point_;
210   int32_t touch_count_ = 0;
211 };
212 
213 class NoneInputEvent : public InputEvent {
214  public:
215   NoneInputEvent();
216   NoneInputEvent(const NoneInputEvent& other);
217   NoneInputEvent& operator=(const NoneInputEvent& other);
218   ~NoneInputEvent();
219 };
220 
221 KeyboardInputEvent GetKeyboardInputEvent(const pp::KeyboardInputEvent& event);
222 
223 MouseInputEvent GetMouseInputEvent(const pp::MouseInputEvent& event);
224 
225 TouchInputEvent GetTouchInputEvent(const pp::TouchInputEvent& event);
226 
227 }  // namespace chrome_pdf
228 
229 #endif  // PDF_PPAPI_MIGRATION_INPUT_EVENT_CONVERSIONS_H_
230