1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/events/ozone/evdev/event_device_info.h"
7 #include <linux/input.h>
9 #include "base/logging.h"
10 #include "base/threading/thread_restrictions.h"
16 bool GetEventBits(int fd, unsigned int type, void* buf, unsigned int size) {
17 if (ioctl(fd, EVIOCGBIT(type, size), buf) < 0) {
18 DLOG(ERROR) << "failed EVIOCGBIT(" << type << ", " << size << ") on fd "
26 bool GetPropBits(int fd, void* buf, unsigned int size) {
27 if (ioctl(fd, EVIOCGPROP(size), buf) < 0) {
28 DLOG(ERROR) << "failed EVIOCGPROP(" << size << ") on fd " << fd;
35 bool BitIsSet(const unsigned long* bits, unsigned int bit) {
36 return (bits[bit / EVDEV_LONG_BITS] & (1UL << (bit % EVDEV_LONG_BITS)));
39 bool GetAbsInfo(int fd, int code, struct input_absinfo* absinfo) {
40 if (ioctl(fd, EVIOCGABS(code), absinfo)) {
41 DLOG(ERROR) << "failed EVIOCGABS(" << code << ") on fd " << fd;
49 EventDeviceInfo::EventDeviceInfo() {
50 memset(ev_bits_, 0, sizeof(ev_bits_));
51 memset(key_bits_, 0, sizeof(key_bits_));
52 memset(rel_bits_, 0, sizeof(rel_bits_));
53 memset(abs_bits_, 0, sizeof(abs_bits_));
54 memset(msc_bits_, 0, sizeof(msc_bits_));
55 memset(sw_bits_, 0, sizeof(sw_bits_));
56 memset(led_bits_, 0, sizeof(led_bits_));
57 memset(prop_bits_, 0, sizeof(prop_bits_));
58 memset(abs_info_, 0, sizeof(abs_info_));
61 EventDeviceInfo::~EventDeviceInfo() {}
63 bool EventDeviceInfo::Initialize(int fd) {
64 if (!GetEventBits(fd, 0, ev_bits_, sizeof(ev_bits_)))
67 if (!GetEventBits(fd, EV_KEY, key_bits_, sizeof(key_bits_)))
70 if (!GetEventBits(fd, EV_REL, rel_bits_, sizeof(rel_bits_)))
73 if (!GetEventBits(fd, EV_ABS, abs_bits_, sizeof(abs_bits_)))
76 if (!GetEventBits(fd, EV_MSC, msc_bits_, sizeof(msc_bits_)))
79 if (!GetEventBits(fd, EV_SW, sw_bits_, sizeof(sw_bits_)))
82 if (!GetEventBits(fd, EV_LED, led_bits_, sizeof(led_bits_)))
85 if (!GetPropBits(fd, prop_bits_, sizeof(prop_bits_)))
88 for (unsigned int i = 0; i < ABS_CNT; ++i)
90 if (!GetAbsInfo(fd, i, &abs_info_[i]))
96 bool EventDeviceInfo::HasEventType(unsigned int type) const {
99 return BitIsSet(ev_bits_, type);
102 bool EventDeviceInfo::HasKeyEvent(unsigned int code) const {
105 return BitIsSet(key_bits_, code);
108 bool EventDeviceInfo::HasRelEvent(unsigned int code) const {
111 return BitIsSet(rel_bits_, code);
114 bool EventDeviceInfo::HasAbsEvent(unsigned int code) const {
117 return BitIsSet(abs_bits_, code);
120 bool EventDeviceInfo::HasMscEvent(unsigned int code) const {
123 return BitIsSet(msc_bits_, code);
126 bool EventDeviceInfo::HasSwEvent(unsigned int code) const {
129 return BitIsSet(sw_bits_, code);
132 bool EventDeviceInfo::HasLedEvent(unsigned int code) const {
135 return BitIsSet(led_bits_, code);
138 bool EventDeviceInfo::HasProp(unsigned int code) const {
139 if (code > INPUT_PROP_MAX)
141 return BitIsSet(prop_bits_, code);
144 int32 EventDeviceInfo::GetAbsMinimum(unsigned int code) const {
145 return abs_info_[code].minimum;
148 int32 EventDeviceInfo::GetAbsMaximum(unsigned int code) const {
149 return abs_info_[code].maximum;
152 bool EventDeviceInfo::HasAbsXY() const {
153 if (HasAbsEvent(ABS_X) && HasAbsEvent(ABS_Y))
156 if (HasAbsEvent(ABS_MT_POSITION_X) && HasAbsEvent(ABS_MT_POSITION_Y))
162 bool EventDeviceInfo::HasRelXY() const {
163 return HasRelEvent(REL_X) && HasRelEvent(REL_Y);
166 bool EventDeviceInfo::IsMappedToScreen() const {
167 // Device position is mapped directly to the screen.
168 if (HasProp(INPUT_PROP_DIRECT))
171 // Device position moves the cursor.
172 if (HasProp(INPUT_PROP_POINTER))
175 // Tablets are mapped to the screen.
176 if (HasKeyEvent(BTN_TOOL_PEN) || HasKeyEvent(BTN_STYLUS) ||
177 HasKeyEvent(BTN_STYLUS2))
180 // Touchpads are not mapped to the screen.
181 if (HasKeyEvent(BTN_LEFT) || HasKeyEvent(BTN_MIDDLE) ||
182 HasKeyEvent(BTN_RIGHT) || HasKeyEvent(BTN_TOOL_FINGER))
185 // Touchscreens are mapped to the screen.