xorg-gtest-device.h 4.64 KB
Newer Older
1 2 3 4 5
/*******************************************************************************
 *
 * X testing environment - Google Test environment feat. dummy x server
 *
 * Copyright (C) 2012 Canonical Ltd.
Peter Hutterer's avatar
Peter Hutterer committed
6
 * Copyright © 2012 Red Hat, Inc.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 ******************************************************************************/

#ifndef XORG_GTEST_EVEMU_DEVICE_H_
#define XORG_GTEST_EVEMU_DEVICE_H_

#include <memory>
#include <string>

extern "C" {

#include <evemu.h>

} // extern "C"

namespace xorg {
namespace testing {
namespace evemu {

/**
Peter Hutterer's avatar
Peter Hutterer committed
46
 * @class Device xorg-gtest-device.h xorg/gtest/evemu/xorg-gtest-device.h
47
 *
48
 * evemu input device for replaying events through the Linux uinput
49 50 51 52 53 54 55 56 57 58
 * evdev subsystem.
 *
 * Use the Recording class to play back a specific recording.
 */

class Device {
 public:
  /**
   * Create a new device context.
   *
59
   * @param [in] path Path to evemu device property file.
60 61 62 63 64 65 66 67
   *
   * @throws std::runtime_error if the device property file could not be found
   *         or the device could not be created.
   */
  explicit Device(const std::string& path);
  ~Device();

  /**
68
   * Play a evemu recording through the device.
69 70 71 72
   *
   * Plays the recording from the beginning through the end. This call will
   * block until the recording has finished.
   *
73
   * @param [in] path Path to evemu recording file.
74 75 76 77 78
   *
   * @throws std::runtime_error if playback failed for any reason.
   */
  void Play(const std::string& path) const;

79 80 81 82 83 84 85 86 87 88 89 90 91 92
  /**
   * Play a single event through the device.
   *
   * Plays an event with the given type, code and value through the device.
   *
   * @param [in] type Evdev interface event type, e.g. EV_ABS, EV_REL, EV_KEY.
   * @param [in] code Evdev interface event code, e.g. ABS_X, REL_Y, BTN_LEFT
   * @param [in] value Event value
   * @param [in] sync If true, submit an EV_SYN event after this event
   *
   * @throws std::runtime_error if playback failed for any reason.
   */
  void PlayOne(int type, int code, int value, bool sync = false);

93 94 95 96 97 98 99 100 101 102 103 104
  /**
   * Return the /dev/input/eventX device node for this device.
   *
   * Note that evemu doesn't know the device node, so we traverse the file
   * system looking for it. There is a tiny chance of the device node being
   * wrong, or the device disappearing before we find it. If the device
   * node cannot be found, an empty string is returned.
   *
   * @return The string representing the device node
   */
  const std::string& GetDeviceNode(void);

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  /**
   * Check if a device supports a specific event.
   *
   * @param [in] type Type of the event (EV_REL, EV_ABS, ...)
   * @param [in] code Event code (ABS_X, REL_Y, ...)
   *
   * @return true if this device supports this event or false otherwise.
   */
  bool HasEvent(int type, int code);

  /**
   * Retrieve data about an absolute axis on this device.
   *
   * @param [in] code The axis to query (e.g. ABS_X)
   * @param [out] min Min value for this axis
   * @param [out] max Max value for this axis
   * @param [out] fuzz Fuzz value for this axis
   * @param [out] flat Flat value for this axis
   * @param [out] resolution Resolution of this axis
   *
   * @return false if this device doesn't have this axis, or true on success
   */
  bool GetAbsData(int code, int *min, int *max, int *fuzz = NULL, int *flat = NULL, int *resolution = NULL);


130 131 132 133 134 135 136
 private:
  struct Private;
  std::auto_ptr<Private> d_;

  /* Disable copy constructor & assignment operator */
  Device(const Device&);
  Device& operator=(const Device&);
137 138

  void GuessDeviceNode(time_t ctime);
139 140 141 142 143 144 145
};

} // namespace evemu
} // namespace testing
} // namespace xorg

#endif // XORG_GTEST_EVEMU_DEVICE_H_