Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/ActionSimulators/PersistentEventSimulator.cpp
2868 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "PersistentEventSimulator.h"
18
19
#define THREAD_TERMINATION_WAIT_TIME_IN_MILLISECONDS 2500
20
#define THREAD_SLEEP_TIME_IN_MILLISECONDS 10
21
22
namespace webdriver {
23
24
PersistentEventSimulator::PersistentEventSimulator() {
25
this->target_window_handle_ = NULL;
26
this->x_coordinate_ = 0;
27
this->y_coordinate_ = 0;
28
this->button_state_ = 0;
29
this->event_firing_thread_handle_ = NULL;
30
this->is_firing_events_ = false;
31
}
32
33
PersistentEventSimulator::~PersistentEventSimulator() {
34
if (this->event_firing_thread_handle_ != NULL) {
35
this->is_running_ = false;
36
::WaitForSingleObject(this->event_firing_thread_handle_,
37
THREAD_TERMINATION_WAIT_TIME_IN_MILLISECONDS);
38
::CloseHandle(this->event_firing_thread_handle_);
39
this->event_firing_thread_handle_ = NULL;
40
}
41
}
42
43
DWORD WINAPI PersistentEventSimulator::MouseEventFiringFunction(LPVOID lpParam) {
44
PersistentEventSimulator* persistent_event_simulator;
45
46
persistent_event_simulator = reinterpret_cast<PersistentEventSimulator*>(lpParam);
47
// busy-wait loop, waiting for 10 milliseconds between
48
// dispatching events. Since the thread is usually
49
// paused for short periods of time (tens of milliseconds),
50
// a more modern signalling method was not used.
51
while (persistent_event_simulator->is_running()) {
52
if (persistent_event_simulator->is_firing_events()) {
53
HWND target = persistent_event_simulator->target_window_handle();
54
if (::IsWindow(target)) {
55
::SendMessage(target,
56
WM_MOUSEMOVE,
57
persistent_event_simulator->button_state(),
58
MAKELPARAM(persistent_event_simulator->x_coordinate(),
59
persistent_event_simulator->y_coordinate()));
60
}
61
}
62
::Sleep(THREAD_SLEEP_TIME_IN_MILLISECONDS);
63
}
64
65
return 0;
66
}
67
68
void PersistentEventSimulator::StartPersistentEventFiring(HWND inputTo, long toX, long toY, WPARAM buttonValue) {
69
this->target_window_handle_ = inputTo;
70
this->x_coordinate_ = toX;
71
this->y_coordinate_ = toY;
72
this->button_state_ = buttonValue;
73
if (this->event_firing_thread_handle_ == NULL) {
74
this->is_running_ = true;
75
this->event_firing_thread_handle_ = CreateThread(NULL, // Security permissions.
76
0, // default stack size.
77
MouseEventFiringFunction,
78
reinterpret_cast<LPVOID>(this),
79
0, // default creation flags
80
NULL);
81
}
82
this->is_firing_events_ = true;
83
}
84
85
void PersistentEventSimulator::StopPersistentEventFiring() {
86
if (this->event_firing_thread_handle_ != NULL) {
87
this->is_firing_events_ = false;
88
::Sleep(THREAD_SLEEP_TIME_IN_MILLISECONDS);
89
}
90
}
91
92
} // namespace webdriver
93
94