Path: blob/trunk/cpp/iedriver/ActionSimulators/PersistentEventSimulator.cpp
2868 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516#include "PersistentEventSimulator.h"1718#define THREAD_TERMINATION_WAIT_TIME_IN_MILLISECONDS 250019#define THREAD_SLEEP_TIME_IN_MILLISECONDS 102021namespace webdriver {2223PersistentEventSimulator::PersistentEventSimulator() {24this->target_window_handle_ = NULL;25this->x_coordinate_ = 0;26this->y_coordinate_ = 0;27this->button_state_ = 0;28this->event_firing_thread_handle_ = NULL;29this->is_firing_events_ = false;30}3132PersistentEventSimulator::~PersistentEventSimulator() {33if (this->event_firing_thread_handle_ != NULL) {34this->is_running_ = false;35::WaitForSingleObject(this->event_firing_thread_handle_,36THREAD_TERMINATION_WAIT_TIME_IN_MILLISECONDS);37::CloseHandle(this->event_firing_thread_handle_);38this->event_firing_thread_handle_ = NULL;39}40}4142DWORD WINAPI PersistentEventSimulator::MouseEventFiringFunction(LPVOID lpParam) {43PersistentEventSimulator* persistent_event_simulator;4445persistent_event_simulator = reinterpret_cast<PersistentEventSimulator*>(lpParam);46// busy-wait loop, waiting for 10 milliseconds between47// dispatching events. Since the thread is usually48// paused for short periods of time (tens of milliseconds),49// a more modern signalling method was not used.50while (persistent_event_simulator->is_running()) {51if (persistent_event_simulator->is_firing_events()) {52HWND target = persistent_event_simulator->target_window_handle();53if (::IsWindow(target)) {54::SendMessage(target,55WM_MOUSEMOVE,56persistent_event_simulator->button_state(),57MAKELPARAM(persistent_event_simulator->x_coordinate(),58persistent_event_simulator->y_coordinate()));59}60}61::Sleep(THREAD_SLEEP_TIME_IN_MILLISECONDS);62}6364return 0;65}6667void PersistentEventSimulator::StartPersistentEventFiring(HWND inputTo, long toX, long toY, WPARAM buttonValue) {68this->target_window_handle_ = inputTo;69this->x_coordinate_ = toX;70this->y_coordinate_ = toY;71this->button_state_ = buttonValue;72if (this->event_firing_thread_handle_ == NULL) {73this->is_running_ = true;74this->event_firing_thread_handle_ = CreateThread(NULL, // Security permissions.750, // default stack size.76MouseEventFiringFunction,77reinterpret_cast<LPVOID>(this),780, // default creation flags79NULL);80}81this->is_firing_events_ = true;82}8384void PersistentEventSimulator::StopPersistentEventFiring() {85if (this->event_firing_thread_handle_ != NULL) {86this->is_firing_events_ = false;87::Sleep(THREAD_SLEEP_TIME_IN_MILLISECONDS);88}89}9091} // namespace webdriver929394