Path: blob/trunk/cpp/webdriver-interactions/interactions_linux_common.cpp
2867 views
/*1Copyright 2007-2010 WebDriver committers2Copyright 2007-2010 Google Inc.34Licensed under the Apache License, Version 2.0 (the "License");5you may not use this file except in compliance with the License.6You may obtain a copy of the License at78http://www.apache.org/licenses/LICENSE-2.0910Unless required by applicable law or agreed to in writing, software11distributed under the License is distributed on an "AS IS" BASIS,12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13See the License for the specific language governing permissions and14limitations under the License.15*/1617#include <ctime>18#include <string>19#include <iostream>20#include <fstream>2122#include "interactions.h"23#include "logging.h"2425#include <gdk/gdk.h>26#include <gdk/gdkkeysyms.h>27#include <X11/Xlib.h>28#include <time.h>29#include <stdlib.h>30#include <assert.h>31#include <list>32#include <algorithm>33#include <functional>3435#include "translate_keycode_linux.h"36#include "interactions_linux.h"3738using namespace std;3940guint32 gLatestEventTime = 0;4142// This is the timestamp needed in the GDK events.43guint32 TimeSinceBootMsec()44{45struct timespec clk_tm;46const int msec_nsec_factor = 1000000;47const int sec_msec_factor = 1000;4849int clk_ret = clock_gettime(CLOCK_MONOTONIC, &clk_tm);50if (clk_ret == 0)51{52return (clk_tm.tv_sec * sec_msec_factor +53(clk_tm.tv_nsec / msec_nsec_factor));54}55return 0;56}5758void sleep_for_ms(int sleep_time_ms)59{60struct timespec sleep_time;61sleep_time.tv_sec = sleep_time_ms / 1000;62sleep_time.tv_nsec = (sleep_time_ms % 1000) * 1000000;63nanosleep(&sleep_time, NULL);64}6566bool is_gdk_keyboard_event(GdkEvent* ev)67{68return ((ev->type == GDK_KEY_PRESS) || (ev->type == GDK_KEY_RELEASE));69}7071bool is_gdk_mouse_event(GdkEvent* ev)72{73return ((ev->type == GDK_BUTTON_PRESS) || (ev->type == GDK_BUTTON_RELEASE) ||74(ev->type == GDK_MOTION_NOTIFY) || (ev->type == GDK_2BUTTON_PRESS));75}7677bool event_earlier_than(GdkEvent* ev, guint32 compare_time)78{79assert(is_gdk_keyboard_event(ev) || is_gdk_mouse_event(ev));80return (ev->key.time <= compare_time);81}8283void print_key_event(GdkEvent* p_ev)84{85if (!((p_ev->type == GDK_KEY_PRESS) || (p_ev->type == GDK_KEY_RELEASE))) {86LOG(DEBUG) << "Not a key event.";87return;88}89const gchar* gdk_name = gdk_keyval_name(p_ev->key.keyval);90const char* kNameUnknown = "UNKNOWN";91const char* print_name = (gdk_name != NULL ? gdk_name : kNameUnknown);9293std::string ev_type = (p_ev->type == GDK_KEY_PRESS ? "press" : "release");94LOG(DEBUG) << "Type: " << ev_type << "Key code: " << p_ev->key.keyval <<95" (" << print_name << ") time: " <<96p_ev->key.time << " state: " << p_ev->key.state << " hw keycode: "97<< (int) p_ev->key.hardware_keycode << " ";98}99100bool additional_events_to_wait_for(GdkEvent* p_event)101{102return ((p_event->type == GDK_LEAVE_NOTIFY) ||103(p_event->type == GDK_ENTER_NOTIFY));104105}106107void init_logging()108{109#ifdef INTERACTIONS_DEBUG110static bool log_initalized = false;111if (!log_initalized) {112LOG::Level("DEBUG");113LOG::File(INTERACTIONS_LOG_FILE, "a");114log_initalized = true;115}116#endif117}118119extern "C"120{121bool pending_input_events()122{123LOG(DEBUG) << "Waiting for all events to be processed. Latest: " << gLatestEventTime;124GdkEvent* lastEvent = gdk_event_peek();125LOG(DEBUG) << "Got event: " <<126(lastEvent != NULL ? lastEvent->type : 0);127if ((lastEvent != NULL) && is_gdk_keyboard_event(lastEvent)) {128print_key_event(lastEvent);129}130131bool ret_val = false;132if (lastEvent != NULL &&133(((is_gdk_keyboard_event(lastEvent) || is_gdk_mouse_event(lastEvent)) &&134event_earlier_than(lastEvent, gLatestEventTime))135|| (additional_events_to_wait_for(lastEvent)))) {136ret_val = true;137}138139if (lastEvent != NULL) {140gdk_event_free(lastEvent);141}142LOG(DEBUG) << "Returning: " << ret_val;143144return ret_val;145}146147// Does nothing on Linux148void stopPersistentEventFiring()149{150}151152void setEnablePersistentHover(bool enablePersistentHover)153{154}155156}157158159