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 #include <stdint.h>
6
7 #include "pdf/ppapi_migration/input_event_conversions.h"
8
9 #include "base/notreached.h"
10 #include "pdf/ppapi_migration/geometry_conversions.h"
11 #include "ppapi/cpp/input_event.h"
12 #include "ppapi/cpp/var.h"
13 #include "ui/gfx/geometry/point_conversions.h"
14
15 namespace {
16
GetEventType(const PP_InputEvent_Type & input_type)17 chrome_pdf::InputEventType GetEventType(const PP_InputEvent_Type& input_type) {
18 switch (input_type) {
19 case PP_INPUTEVENT_TYPE_MOUSEDOWN:
20 return chrome_pdf::InputEventType::kMouseDown;
21 case PP_INPUTEVENT_TYPE_MOUSEUP:
22 return chrome_pdf::InputEventType::kMouseUp;
23 case PP_INPUTEVENT_TYPE_MOUSEMOVE:
24 return chrome_pdf::InputEventType::kMouseMove;
25 case PP_INPUTEVENT_TYPE_MOUSEENTER:
26 return chrome_pdf::InputEventType::kMouseEnter;
27 case PP_INPUTEVENT_TYPE_MOUSELEAVE:
28 return chrome_pdf::InputEventType::kMouseLeave;
29 case PP_INPUTEVENT_TYPE_WHEEL:
30 return chrome_pdf::InputEventType::kWheel;
31 case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
32 return chrome_pdf::InputEventType::kRawKeyDown;
33 case PP_INPUTEVENT_TYPE_KEYDOWN:
34 return chrome_pdf::InputEventType::kKeyDown;
35 case PP_INPUTEVENT_TYPE_KEYUP:
36 return chrome_pdf::InputEventType::kKeyUp;
37 case PP_INPUTEVENT_TYPE_CHAR:
38 return chrome_pdf::InputEventType::kChar;
39 case PP_INPUTEVENT_TYPE_CONTEXTMENU:
40 return chrome_pdf::InputEventType::kContextMenu;
41 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START:
42 return chrome_pdf::InputEventType::kImeCompositionStart;
43 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE:
44 return chrome_pdf::InputEventType::kImeCompositionUpdate;
45 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END:
46 return chrome_pdf::InputEventType::kImeCompositionEnd;
47 case PP_INPUTEVENT_TYPE_IME_TEXT:
48 return chrome_pdf::InputEventType::kImeText;
49 case PP_INPUTEVENT_TYPE_TOUCHSTART:
50 return chrome_pdf::InputEventType::kTouchStart;
51 case PP_INPUTEVENT_TYPE_TOUCHMOVE:
52 return chrome_pdf::InputEventType::kTouchMove;
53 case PP_INPUTEVENT_TYPE_TOUCHEND:
54 return chrome_pdf::InputEventType::kTouchEnd;
55 case PP_INPUTEVENT_TYPE_TOUCHCANCEL:
56 return chrome_pdf::InputEventType::kTouchCancel;
57 default:
58 NOTREACHED();
59 return chrome_pdf::InputEventType::kNone;
60 }
61 }
62
GetInputEventMouseButtonType(const PP_InputEvent_MouseButton & mouse_button_type)63 chrome_pdf::InputEventMouseButtonType GetInputEventMouseButtonType(
64 const PP_InputEvent_MouseButton& mouse_button_type) {
65 switch (mouse_button_type) {
66 case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
67 return chrome_pdf::InputEventMouseButtonType::kLeft;
68 case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
69 return chrome_pdf::InputEventMouseButtonType::kMiddle;
70 case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
71 return chrome_pdf::InputEventMouseButtonType::kRight;
72 default:
73 return chrome_pdf::InputEventMouseButtonType::kNone;
74 }
75 }
76
IsKeyboardEventType(chrome_pdf::InputEventType event_type)77 bool IsKeyboardEventType(chrome_pdf::InputEventType event_type) {
78 switch (event_type) {
79 case chrome_pdf::InputEventType::kRawKeyDown:
80 case chrome_pdf::InputEventType::kKeyDown:
81 case chrome_pdf::InputEventType::kKeyUp:
82 case chrome_pdf::InputEventType::kChar:
83 return true;
84 default:
85 return false;
86 }
87 }
88
IsMouseEventType(chrome_pdf::InputEventType event_type)89 bool IsMouseEventType(chrome_pdf::InputEventType event_type) {
90 switch (event_type) {
91 case chrome_pdf::InputEventType::kMouseDown:
92 case chrome_pdf::InputEventType::kMouseUp:
93 case chrome_pdf::InputEventType::kMouseMove:
94 case chrome_pdf::InputEventType::kMouseEnter:
95 case chrome_pdf::InputEventType::kMouseLeave:
96 return true;
97 default:
98 return false;
99 }
100 }
101
IsTouchEventType(chrome_pdf::InputEventType event_type)102 bool IsTouchEventType(chrome_pdf::InputEventType event_type) {
103 switch (event_type) {
104 case chrome_pdf::InputEventType::kTouchStart:
105 case chrome_pdf::InputEventType::kTouchMove:
106 case chrome_pdf::InputEventType::kTouchEnd:
107 case chrome_pdf::InputEventType::kTouchCancel:
108 return true;
109 default:
110 return false;
111 }
112 }
113
114 } // namespace
115
116 namespace chrome_pdf {
117
InputEvent(InputEventType event_type,double time_stamp,uint32_t modifiers)118 InputEvent::InputEvent(InputEventType event_type,
119 double time_stamp,
120 uint32_t modifiers)
121 : event_type_(event_type), time_stamp_(time_stamp), modifiers_(modifiers) {}
122
123 InputEvent::InputEvent(const InputEvent& other) = default;
124
125 InputEvent& InputEvent::operator=(const InputEvent& other) = default;
126
127 InputEvent::~InputEvent() = default;
128
KeyboardInputEvent(InputEventType event_type,double time_stamp,uint32_t modifiers,uint32_t keyboard_code,const std::string & key_char)129 KeyboardInputEvent::KeyboardInputEvent(InputEventType event_type,
130 double time_stamp,
131 uint32_t modifiers,
132 uint32_t keyboard_code,
133 const std::string& key_char)
134 : InputEvent(event_type, time_stamp, modifiers),
135 keyboard_code_(keyboard_code),
136 key_char_(key_char) {
137 DCHECK(IsKeyboardEventType(GetEventType()));
138 }
139
140 KeyboardInputEvent::KeyboardInputEvent(const KeyboardInputEvent& other) =
141 default;
142
143 KeyboardInputEvent& KeyboardInputEvent::operator=(
144 const KeyboardInputEvent& other) = default;
145
146 KeyboardInputEvent::~KeyboardInputEvent() = default;
147
MouseInputEvent(InputEventType event_type,double time_stamp,uint32_t modifiers,InputEventMouseButtonType mouse_button_type,const gfx::Point & point,int32_t click_count,const gfx::Point & movement)148 MouseInputEvent::MouseInputEvent(InputEventType event_type,
149 double time_stamp,
150 uint32_t modifiers,
151 InputEventMouseButtonType mouse_button_type,
152 const gfx::Point& point,
153 int32_t click_count,
154 const gfx::Point& movement)
155 : InputEvent(event_type, time_stamp, modifiers),
156 mouse_button_type_(mouse_button_type),
157 point_(point),
158 click_count_(click_count),
159 movement_(movement) {
160 DCHECK(IsMouseEventType(GetEventType()));
161 }
162
163 MouseInputEvent::MouseInputEvent(const MouseInputEvent& other) = default;
164
165 MouseInputEvent& MouseInputEvent::operator=(const MouseInputEvent& other) =
166 default;
167
168 MouseInputEvent::~MouseInputEvent() = default;
169
TouchInputEvent(InputEventType event_type,double time_stamp,uint32_t modifiers,const gfx::PointF & target_touch_point,int32_t touch_count)170 TouchInputEvent::TouchInputEvent(InputEventType event_type,
171 double time_stamp,
172 uint32_t modifiers,
173 const gfx::PointF& target_touch_point,
174 int32_t touch_count)
175 : InputEvent(event_type, time_stamp, modifiers),
176 target_touch_point_(target_touch_point),
177 touch_count_(touch_count) {
178 DCHECK(IsTouchEventType(GetEventType()));
179 }
180
181 TouchInputEvent::TouchInputEvent(const TouchInputEvent& other) = default;
182
183 TouchInputEvent& TouchInputEvent::operator=(const TouchInputEvent& other) =
184 default;
185
186 TouchInputEvent::~TouchInputEvent() = default;
187
NoneInputEvent()188 NoneInputEvent::NoneInputEvent()
189 : InputEvent(InputEventType::kNone, 0, kInputEventModifierNone) {}
190
191 NoneInputEvent::NoneInputEvent(const NoneInputEvent& other) = default;
192
193 NoneInputEvent& NoneInputEvent::operator=(const NoneInputEvent& other) =
194 default;
195
196 NoneInputEvent::~NoneInputEvent() = default;
197
GetKeyboardInputEvent(const pp::KeyboardInputEvent & event)198 KeyboardInputEvent GetKeyboardInputEvent(const pp::KeyboardInputEvent& event) {
199 return KeyboardInputEvent(GetEventType(event.GetType()), event.GetTimeStamp(),
200 event.GetModifiers(), event.GetKeyCode(),
201 event.GetCharacterText().AsString());
202 }
203
GetMouseInputEvent(const pp::MouseInputEvent & event)204 MouseInputEvent GetMouseInputEvent(const pp::MouseInputEvent& event) {
205 return MouseInputEvent(
206 GetEventType(event.GetType()), event.GetTimeStamp(), event.GetModifiers(),
207 GetInputEventMouseButtonType(event.GetButton()),
208 PointFromPPPoint(event.GetPosition()), event.GetClickCount(),
209 PointFromPPPoint(event.GetMovement()));
210 }
211
GetTouchInputEvent(const pp::TouchInputEvent & event)212 TouchInputEvent GetTouchInputEvent(const pp::TouchInputEvent& event) {
213 pp::FloatPoint point =
214 event.GetTouchByIndex(PP_TOUCHLIST_TYPE_TARGETTOUCHES, 0).position();
215 return TouchInputEvent(GetEventType(event.GetType()), event.GetTimeStamp(),
216 event.GetModifiers(), PointFFromPPFloatPoint(point),
217 event.GetTouchCount(PP_TOUCHLIST_TYPE_TARGETTOUCHES));
218 }
219
220 } // namespace chrome_pdf
221