1/*
2 * Copyright (C) 2017 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebAutomationSession.h"
28
29#include "WebAutomationSessionMacros.h"
30#include "WebPageProxy.h"
31#include <WebCore/GtkUtilities.h>
32#include <gtk/gtk.h>
33
34namespace WebKit {
35using namespace WebCore;
36
37static unsigned modifiersToEventState(OptionSet<WebEvent::Modifier> modifiers)
38{
39 unsigned state = 0;
40 if (modifiers.contains(WebEvent::Modifier::ControlKey))
41 state |= GDK_CONTROL_MASK;
42 if (modifiers.contains(WebEvent::Modifier::ShiftKey))
43 state |= GDK_SHIFT_MASK;
44 if (modifiers.contains(WebEvent::Modifier::AltKey))
45 state |= GDK_META_MASK;
46 if (modifiers.contains(WebEvent::Modifier::CapsLockKey))
47 state |= GDK_LOCK_MASK;
48 return state;
49}
50
51static unsigned mouseButtonToGdkButton(WebMouseEvent::Button button)
52{
53 switch (button) {
54 case WebMouseEvent::NoButton:
55 case WebMouseEvent::LeftButton:
56 return GDK_BUTTON_PRIMARY;
57 case WebMouseEvent::MiddleButton:
58 return GDK_BUTTON_MIDDLE;
59 case WebMouseEvent::RightButton:
60 return GDK_BUTTON_SECONDARY;
61 }
62 return GDK_BUTTON_PRIMARY;
63}
64
65static void doMouseEvent(GdkEventType type, GtkWidget* widget, const WebCore::IntPoint& location, unsigned button, unsigned state)
66{
67 ASSERT(type == GDK_BUTTON_PRESS || type == GDK_BUTTON_RELEASE);
68
69 GUniquePtr<GdkEvent> event(gdk_event_new(type));
70 event->button.window = gtk_widget_get_window(widget);
71 g_object_ref(event->button.window);
72 event->button.time = GDK_CURRENT_TIME;
73 event->button.x = location.x();
74 event->button.y = location.y();
75 event->button.axes = 0;
76 event->button.state = state;
77 event->button.button = button;
78 event->button.device = gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gtk_widget_get_display(widget)));
79 int xRoot, yRoot;
80 gdk_window_get_root_coords(gtk_widget_get_window(widget), location.x(), location.y(), &xRoot, &yRoot);
81 event->button.x_root = xRoot;
82 event->button.y_root = yRoot;
83 gtk_main_do_event(event.get());
84}
85
86static void doMotionEvent(GtkWidget* widget, const WebCore::IntPoint& location, unsigned state)
87{
88 GUniquePtr<GdkEvent> event(gdk_event_new(GDK_MOTION_NOTIFY));
89 event->motion.window = gtk_widget_get_window(widget);
90 g_object_ref(event->motion.window);
91 event->motion.time = GDK_CURRENT_TIME;
92 event->motion.x = location.x();
93 event->motion.y = location.y();
94 event->motion.axes = 0;
95 event->motion.state = state;
96 event->motion.device = gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gtk_widget_get_display(widget)));
97 int xRoot, yRoot;
98 gdk_window_get_root_coords(gtk_widget_get_window(widget), location.x(), location.y(), &xRoot, &yRoot);
99 event->motion.x_root = xRoot;
100 event->motion.y_root = yRoot;
101 gtk_main_do_event(event.get());
102}
103
104void WebAutomationSession::platformSimulateMouseInteraction(WebPageProxy& page, MouseInteraction interaction, WebMouseEvent::Button button, const WebCore::IntPoint& locationInView, OptionSet<WebEvent::Modifier> keyModifiers)
105{
106 unsigned gdkButton = mouseButtonToGdkButton(button);
107 auto modifier = stateModifierForGdkButton(gdkButton);
108 unsigned state = modifiersToEventState(keyModifiers) | m_currentModifiers;
109
110 switch (interaction) {
111 case MouseInteraction::Move:
112 doMotionEvent(page.viewWidget(), locationInView, state);
113 break;
114 case MouseInteraction::Down:
115 doMouseEvent(GDK_BUTTON_PRESS, page.viewWidget(), locationInView, gdkButton, state);
116 m_currentModifiers |= modifier;
117 break;
118 case MouseInteraction::Up:
119 doMouseEvent(GDK_BUTTON_RELEASE, page.viewWidget(), locationInView, gdkButton, state);
120 m_currentModifiers &= ~modifier;
121 break;
122 case MouseInteraction::SingleClick:
123 doMouseEvent(GDK_BUTTON_PRESS, page.viewWidget(), locationInView, gdkButton, state);
124 doMouseEvent(GDK_BUTTON_RELEASE, page.viewWidget(), locationInView, gdkButton, state | modifier);
125 break;
126 case MouseInteraction::DoubleClick:
127 doMouseEvent(GDK_BUTTON_PRESS, page.viewWidget(), locationInView, gdkButton, state);
128 doMouseEvent(GDK_BUTTON_RELEASE, page.viewWidget(), locationInView, gdkButton, state | modifier);
129 doMouseEvent(GDK_BUTTON_PRESS, page.viewWidget(), locationInView, gdkButton, state);
130 doMouseEvent(GDK_BUTTON_RELEASE, page.viewWidget(), locationInView, gdkButton, state | modifier);
131 break;
132 }
133}
134
135static void doKeyStrokeEvent(GdkEventType type, GtkWidget* widget, unsigned keyVal, unsigned state, bool doReleaseAfterPress = false)
136{
137 ASSERT(type == GDK_KEY_PRESS || type == GDK_KEY_RELEASE);
138
139 GUniquePtr<GdkEvent> event(gdk_event_new(type));
140 event->key.keyval = keyVal;
141
142 event->key.time = GDK_CURRENT_TIME;
143 event->key.window = gtk_widget_get_window(widget);
144 g_object_ref(event->key.window);
145 gdk_event_set_device(event.get(), gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gtk_widget_get_display(widget))));
146 event->key.state = state;
147
148 // When synthesizing an event, an invalid hardware_keycode value can cause it to be badly processed by GTK+.
149 GUniqueOutPtr<GdkKeymapKey> keys;
150 int keysCount;
151 if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), keyVal, &keys.outPtr(), &keysCount) && keysCount)
152 event->key.hardware_keycode = keys.get()[0].keycode;
153
154 gtk_main_do_event(event.get());
155 if (doReleaseAfterPress) {
156 ASSERT(type == GDK_KEY_PRESS);
157 event->key.type = GDK_KEY_RELEASE;
158 gtk_main_do_event(event.get());
159 }
160}
161
162static int keyCodeForVirtualKey(Inspector::Protocol::Automation::VirtualKey key)
163{
164 switch (key) {
165 case Inspector::Protocol::Automation::VirtualKey::Shift:
166 return GDK_KEY_Shift_R;
167 case Inspector::Protocol::Automation::VirtualKey::Control:
168 return GDK_KEY_Control_R;
169 case Inspector::Protocol::Automation::VirtualKey::Alternate:
170 return GDK_KEY_Alt_L;
171 case Inspector::Protocol::Automation::VirtualKey::Meta:
172 return GDK_KEY_Meta_R;
173 case Inspector::Protocol::Automation::VirtualKey::Command:
174 return GDK_KEY_Execute;
175 case Inspector::Protocol::Automation::VirtualKey::Help:
176 return GDK_KEY_Help;
177 case Inspector::Protocol::Automation::VirtualKey::Backspace:
178 return GDK_KEY_BackSpace;
179 case Inspector::Protocol::Automation::VirtualKey::Tab:
180 return GDK_KEY_Tab;
181 case Inspector::Protocol::Automation::VirtualKey::Clear:
182 return GDK_KEY_Clear;
183 case Inspector::Protocol::Automation::VirtualKey::Enter:
184 return GDK_KEY_Return;
185 case Inspector::Protocol::Automation::VirtualKey::Pause:
186 return GDK_KEY_Pause;
187 case Inspector::Protocol::Automation::VirtualKey::Cancel:
188 return GDK_KEY_Cancel;
189 case Inspector::Protocol::Automation::VirtualKey::Escape:
190 return GDK_KEY_Escape;
191 case Inspector::Protocol::Automation::VirtualKey::PageUp:
192 return GDK_KEY_Page_Up;
193 case Inspector::Protocol::Automation::VirtualKey::PageDown:
194 return GDK_KEY_Page_Down;
195 case Inspector::Protocol::Automation::VirtualKey::End:
196 return GDK_KEY_End;
197 case Inspector::Protocol::Automation::VirtualKey::Home:
198 return GDK_KEY_Home;
199 case Inspector::Protocol::Automation::VirtualKey::LeftArrow:
200 return GDK_KEY_Left;
201 case Inspector::Protocol::Automation::VirtualKey::UpArrow:
202 return GDK_KEY_Up;
203 case Inspector::Protocol::Automation::VirtualKey::RightArrow:
204 return GDK_KEY_Right;
205 case Inspector::Protocol::Automation::VirtualKey::DownArrow:
206 return GDK_KEY_Down;
207 case Inspector::Protocol::Automation::VirtualKey::Insert:
208 return GDK_KEY_Insert;
209 case Inspector::Protocol::Automation::VirtualKey::Delete:
210 return GDK_KEY_Delete;
211 case Inspector::Protocol::Automation::VirtualKey::Space:
212 return GDK_KEY_space;
213 case Inspector::Protocol::Automation::VirtualKey::Semicolon:
214 return GDK_KEY_semicolon;
215 case Inspector::Protocol::Automation::VirtualKey::Equals:
216 return GDK_KEY_equal;
217 case Inspector::Protocol::Automation::VirtualKey::Return:
218 return GDK_KEY_Return;
219 case Inspector::Protocol::Automation::VirtualKey::NumberPad0:
220 return GDK_KEY_KP_0;
221 case Inspector::Protocol::Automation::VirtualKey::NumberPad1:
222 return GDK_KEY_KP_1;
223 case Inspector::Protocol::Automation::VirtualKey::NumberPad2:
224 return GDK_KEY_KP_2;
225 case Inspector::Protocol::Automation::VirtualKey::NumberPad3:
226 return GDK_KEY_KP_3;
227 case Inspector::Protocol::Automation::VirtualKey::NumberPad4:
228 return GDK_KEY_KP_4;
229 case Inspector::Protocol::Automation::VirtualKey::NumberPad5:
230 return GDK_KEY_KP_5;
231 case Inspector::Protocol::Automation::VirtualKey::NumberPad6:
232 return GDK_KEY_KP_6;
233 case Inspector::Protocol::Automation::VirtualKey::NumberPad7:
234 return GDK_KEY_KP_7;
235 case Inspector::Protocol::Automation::VirtualKey::NumberPad8:
236 return GDK_KEY_KP_8;
237 case Inspector::Protocol::Automation::VirtualKey::NumberPad9:
238 return GDK_KEY_KP_9;
239 case Inspector::Protocol::Automation::VirtualKey::NumberPadMultiply:
240 return GDK_KEY_KP_Multiply;
241 case Inspector::Protocol::Automation::VirtualKey::NumberPadAdd:
242 return GDK_KEY_KP_Add;
243 case Inspector::Protocol::Automation::VirtualKey::NumberPadSubtract:
244 return GDK_KEY_KP_Subtract;
245 case Inspector::Protocol::Automation::VirtualKey::NumberPadSeparator:
246 return GDK_KEY_KP_Separator;
247 case Inspector::Protocol::Automation::VirtualKey::NumberPadDecimal:
248 return GDK_KEY_KP_Decimal;
249 case Inspector::Protocol::Automation::VirtualKey::NumberPadDivide:
250 return GDK_KEY_KP_Divide;
251 case Inspector::Protocol::Automation::VirtualKey::Function1:
252 return GDK_KEY_F1;
253 case Inspector::Protocol::Automation::VirtualKey::Function2:
254 return GDK_KEY_F2;
255 case Inspector::Protocol::Automation::VirtualKey::Function3:
256 return GDK_KEY_F3;
257 case Inspector::Protocol::Automation::VirtualKey::Function4:
258 return GDK_KEY_F4;
259 case Inspector::Protocol::Automation::VirtualKey::Function5:
260 return GDK_KEY_F5;
261 case Inspector::Protocol::Automation::VirtualKey::Function6:
262 return GDK_KEY_F6;
263 case Inspector::Protocol::Automation::VirtualKey::Function7:
264 return GDK_KEY_F7;
265 case Inspector::Protocol::Automation::VirtualKey::Function8:
266 return GDK_KEY_F8;
267 case Inspector::Protocol::Automation::VirtualKey::Function9:
268 return GDK_KEY_F9;
269 case Inspector::Protocol::Automation::VirtualKey::Function10:
270 return GDK_KEY_F10;
271 case Inspector::Protocol::Automation::VirtualKey::Function11:
272 return GDK_KEY_F11;
273 case Inspector::Protocol::Automation::VirtualKey::Function12:
274 return GDK_KEY_F12;
275 }
276
277 ASSERT_NOT_REACHED();
278 return 0;
279}
280
281static unsigned modifiersForKeyCode(unsigned keyCode)
282{
283 switch (keyCode) {
284 case GDK_KEY_Shift_R:
285 return GDK_SHIFT_MASK;
286 case GDK_KEY_Control_R:
287 return GDK_CONTROL_MASK;
288 case GDK_KEY_Alt_L:
289 return GDK_MOD1_MASK;
290 case GDK_KEY_Meta_R:
291 return GDK_META_MASK;
292 }
293 return 0;
294}
295
296void WebAutomationSession::platformSimulateKeyboardInteraction(WebPageProxy& page, KeyboardInteraction interaction, WTF::Variant<VirtualKey, CharKey>&& key)
297{
298 unsigned keyCode;
299 WTF::switchOn(key,
300 [&] (VirtualKey virtualKey) {
301 keyCode = keyCodeForVirtualKey(virtualKey);
302 },
303 [&] (CharKey charKey) {
304 keyCode = gdk_unicode_to_keyval(g_utf8_get_char(&charKey));
305 }
306 );
307 unsigned modifiers = modifiersForKeyCode(keyCode);
308
309 switch (interaction) {
310 case KeyboardInteraction::KeyPress:
311 m_currentModifiers |= modifiers;
312 doKeyStrokeEvent(GDK_KEY_PRESS, page.viewWidget(), keyCode, m_currentModifiers);
313 break;
314 case KeyboardInteraction::KeyRelease:
315 m_currentModifiers &= ~modifiers;
316 doKeyStrokeEvent(GDK_KEY_RELEASE, page.viewWidget(), keyCode, m_currentModifiers);
317 break;
318 case KeyboardInteraction::InsertByKey:
319 doKeyStrokeEvent(GDK_KEY_PRESS, page.viewWidget(), keyCode, m_currentModifiers, true);
320 break;
321 }
322}
323
324void WebAutomationSession::platformSimulateKeySequence(WebPageProxy& page, const String& keySequence)
325{
326 CString keySequenceUTF8 = keySequence.utf8();
327 const char* p = keySequenceUTF8.data();
328 do {
329 doKeyStrokeEvent(GDK_KEY_PRESS, page.viewWidget(), gdk_unicode_to_keyval(g_utf8_get_char(p)), m_currentModifiers, true);
330 p = g_utf8_next_char(p);
331 } while (*p);
332}
333
334} // namespace WebKit
335
336