Home | History | Annotate | Download | only in evdev
      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.
      4 
      5 #include "ui/events/ozone/evdev/event_device_info.h"
      6 
      7 #include <linux/input.h>
      8 
      9 #include "base/logging.h"
     10 #include "base/threading/thread_restrictions.h"
     11 
     12 namespace ui {
     13 
     14 namespace {
     15 
     16 bool GetEventBits(int fd, unsigned int type, void* buf, unsigned int size) {
     17   base::ThreadRestrictions::AssertIOAllowed();
     18 
     19   if (ioctl(fd, EVIOCGBIT(type, size), buf) < 0) {
     20     DLOG(ERROR) << "failed EVIOCGBIT(" << type << ", " << size << ") on fd "
     21                 << fd;
     22     return false;
     23   }
     24 
     25   return true;
     26 }
     27 
     28 bool GetPropBits(int fd, void* buf, unsigned int size) {
     29   base::ThreadRestrictions::AssertIOAllowed();
     30 
     31   if (ioctl(fd, EVIOCGPROP(size), buf) < 0) {
     32     DLOG(ERROR) << "failed EVIOCGPROP(" << size << ") on fd " << fd;
     33     return false;
     34   }
     35 
     36   return true;
     37 }
     38 
     39 bool BitIsSet(const unsigned long* bits, unsigned int bit) {
     40   return (bits[bit / EVDEV_LONG_BITS] & (1UL << (bit % EVDEV_LONG_BITS)));
     41 }
     42 
     43 }  // namespace
     44 
     45 EventDeviceInfo::EventDeviceInfo() {
     46   memset(ev_bits_, 0, sizeof(ev_bits_));
     47   memset(key_bits_, 0, sizeof(key_bits_));
     48   memset(rel_bits_, 0, sizeof(rel_bits_));
     49   memset(abs_bits_, 0, sizeof(abs_bits_));
     50   memset(msc_bits_, 0, sizeof(msc_bits_));
     51   memset(sw_bits_, 0, sizeof(sw_bits_));
     52   memset(led_bits_, 0, sizeof(led_bits_));
     53   memset(prop_bits_, 0, sizeof(prop_bits_));
     54 }
     55 
     56 EventDeviceInfo::~EventDeviceInfo() {}
     57 
     58 bool EventDeviceInfo::Initialize(int fd) {
     59   if (!GetEventBits(fd, 0, ev_bits_, sizeof(ev_bits_)))
     60     return false;
     61 
     62   if (!GetEventBits(fd, EV_KEY, key_bits_, sizeof(key_bits_)))
     63     return false;
     64 
     65   if (!GetEventBits(fd, EV_REL, rel_bits_, sizeof(rel_bits_)))
     66     return false;
     67 
     68   if (!GetEventBits(fd, EV_ABS, abs_bits_, sizeof(abs_bits_)))
     69     return false;
     70 
     71   if (!GetEventBits(fd, EV_MSC, msc_bits_, sizeof(msc_bits_)))
     72     return false;
     73 
     74   if (!GetEventBits(fd, EV_SW, sw_bits_, sizeof(sw_bits_)))
     75     return false;
     76 
     77   if (!GetEventBits(fd, EV_LED, led_bits_, sizeof(led_bits_)))
     78     return false;
     79 
     80   if (!GetPropBits(fd, prop_bits_, sizeof(prop_bits_)))
     81     return false;
     82 
     83   return true;
     84 }
     85 
     86 bool EventDeviceInfo::HasEventType(unsigned int type) const {
     87   if (type > EV_MAX)
     88     return false;
     89   return BitIsSet(ev_bits_, type);
     90 }
     91 
     92 bool EventDeviceInfo::HasKeyEvent(unsigned int code) const {
     93   if (code > KEY_MAX)
     94     return false;
     95   return BitIsSet(key_bits_, code);
     96 }
     97 
     98 bool EventDeviceInfo::HasRelEvent(unsigned int code) const {
     99   if (code > REL_MAX)
    100     return false;
    101   return BitIsSet(rel_bits_, code);
    102 }
    103 
    104 bool EventDeviceInfo::HasAbsEvent(unsigned int code) const {
    105   if (code > ABS_MAX)
    106     return false;
    107   return BitIsSet(abs_bits_, code);
    108 }
    109 
    110 bool EventDeviceInfo::HasMscEvent(unsigned int code) const {
    111   if (code > MSC_MAX)
    112     return false;
    113   return BitIsSet(msc_bits_, code);
    114 }
    115 
    116 bool EventDeviceInfo::HasSwEvent(unsigned int code) const {
    117   if (code > SW_MAX)
    118     return false;
    119   return BitIsSet(sw_bits_, code);
    120 }
    121 
    122 bool EventDeviceInfo::HasLedEvent(unsigned int code) const {
    123   if (code > LED_MAX)
    124     return false;
    125   return BitIsSet(led_bits_, code);
    126 }
    127 
    128 bool EventDeviceInfo::HasProp(unsigned int code) const {
    129   if (code > INPUT_PROP_MAX)
    130     return false;
    131   return BitIsSet(prop_bits_, code);
    132 }
    133 
    134 }  // namespace ui
    135