summaryrefslogtreecommitdiffstats
path: root/ui/base/x/valuators.cc
blob: ed88edc6813648381258e6cd372d1da900068463 (plain)
1
2
3
4
5
6
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/x/valuators.h"

#include <X11/extensions/XInput2.h>

#include "base/memory/singleton.h"
#include "ui/base/touch/touch_factory.h"
#include "ui/base/x/x11_util.h"

namespace {

#define AXIS_LABEL_ABS_MT_TOUCH_MAJOR "Abs MT Touch Major"
#define AXIS_LABEL_ABS_MT_TOUCH_MINOR "Abs MT Touch Minor"
#define AXIS_LABEL_ABS_MT_ORIENTATION "Abs MT Orientation"
#define AXIS_LABEL_ABS_MT_PRESSURE    "Abs MT Pressure"
#define AXIS_LABEL_ABS_MT_SLOT_ID     "Abs MT Slot ID"
#define AXIS_LABEL_ABS_MT_TRACKING_ID "Abs MT Tracking ID"

// Given the Valuator, return the correspoding XIValuatorClassInfo using
// the X device information through Atom name matching.
XIValuatorClassInfo* FindValuator(Display* display,
                                  XIDeviceInfo* info,
                                  ui::ValuatorTracker::Valuator val) {
  // Lookup table for mapping Valuator to Atom string used in X.
  // A full set of Atom strings can be found at xserver-properties.h.
  static struct {
    ui::ValuatorTracker::Valuator val;
    Atom atom;
  } kValuatorAtom[] = {
    { ui::ValuatorTracker::VAL_TOUCH_MAJOR,
      XInternAtom(ui::GetXDisplay(), AXIS_LABEL_ABS_MT_TOUCH_MAJOR, false) },
    { ui::ValuatorTracker::VAL_TOUCH_MINOR,
      XInternAtom(ui::GetXDisplay(), AXIS_LABEL_ABS_MT_TOUCH_MINOR, false) },
    { ui::ValuatorTracker::VAL_ORIENTATION,
      XInternAtom(ui::GetXDisplay(), AXIS_LABEL_ABS_MT_ORIENTATION, false) },
    { ui::ValuatorTracker::VAL_PRESSURE,
      XInternAtom(ui::GetXDisplay(), AXIS_LABEL_ABS_MT_PRESSURE, false) },
#if !defined(USE_XI2_MT)
    // For Slot ID, See this chromeos revision: http://git.chromium.org/gitweb/?
    //        p=chromiumos/overlays/chromiumos-overlay.git;
    //        a=commit;h=9164d0a75e48c4867e4ef4ab51f743ae231c059a
    { ui::ValuatorTracker::VAL_SLOT_ID,
      XInternAtom(ui::GetXDisplay(), AXIS_LABEL_ABS_MT_SLOT_ID, false) },
#endif
    { ui::ValuatorTracker::VAL_TRACKING_ID,
      XInternAtom(ui::GetXDisplay(), AXIS_LABEL_ABS_MT_TRACKING_ID, false) },
    { ui::ValuatorTracker::VAL_LAST_ENTRY, None },
  };

  Atom atom = None;

  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kValuatorAtom); i++) {
    if (val == kValuatorAtom[i].val) {
      atom = kValuatorAtom[i].atom;
      break;
    }
  }

  if (atom == None)
    return NULL;

  for (int i = 0; i < info->num_classes; i++) {
    if (info->classes[i]->type != XIValuatorClass)
      continue;
    XIValuatorClassInfo* v =
        reinterpret_cast<XIValuatorClassInfo*>(info->classes[i]);
    if (v->label && atom == v->label)
      return v;
  }

  return NULL;
}

}  // namespace

namespace ui {

ValuatorTracker::ValuatorTracker() {
  SetupValuator();
}

ValuatorTracker::~ValuatorTracker() {
}

// static
ValuatorTracker* ValuatorTracker::GetInstance() {
  return Singleton<ValuatorTracker>::get();
}

bool ValuatorTracker::ExtractValuator(const XEvent& xev,
                                      Valuator val,
                                      float* value) {
  XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(xev.xcookie.data);
  if (xiev->sourceid >= kMaxDeviceNum || xiev->deviceid >= kMaxDeviceNum)
    return false;
  int val_index = valuator_lookup_[xiev->sourceid][val];
  if (val_index >= 0) {
    if (XIMaskIsSet(xiev->valuators.mask, val_index)) {
      double* valuators = xiev->valuators.values;
      while (val_index--) {
        if (XIMaskIsSet(xiev->valuators.mask, val_index))
          ++valuators;
      }
      *value = *valuators;
      last_seen_valuator_[xiev->deviceid][val] = *value;
      return true;
    } else {
      *value = last_seen_valuator_[xiev->deviceid][val];
    }
  }

#if defined(USE_XI2_MT)
  // With XInput2 MT, Tracking ID is provided in the detail field.
  if (val == VAL_TRACKING_ID) {
    *value = xiev->detail;
    return true;
  }
#endif

  return false;
}

bool ValuatorTracker::NormalizeValuator(unsigned int deviceid,
                                        Valuator val,
                                        float* value) {
  float max_value;
  float min_value;
  if (GetValuatorRange(deviceid, val, &min_value, &max_value)) {
    *value = (*value - min_value) / (max_value - min_value);
    DCHECK(*value >= 0.0 && *value <= 1.0);
    return true;
  }
  return false;
}

bool ValuatorTracker::GetValuatorRange(unsigned int deviceid,
                                       Valuator val,
                                       float* min,
                                       float* max) {
  if (valuator_lookup_[deviceid][val] >= 0) {
    *min = valuator_min_[deviceid][val];
    *max = valuator_max_[deviceid][val];
    return true;
  }
  return false;
}

void ValuatorTracker::SetupValuator() {
  memset(valuator_lookup_, -1, sizeof(valuator_lookup_));
  memset(valuator_min_, 0, sizeof(valuator_min_));
  memset(valuator_max_, 0, sizeof(valuator_max_));
  memset(last_seen_valuator_, 0, sizeof(last_seen_valuator_));

  Display* display = GetXDisplay();
  int ndevice;
  XIDeviceInfo* info_list = XIQueryDevice(display, XIAllDevices, &ndevice);
  TouchFactory* factory = TouchFactory::GetInstance();

  for (int i = 0; i < ndevice; i++) {
    XIDeviceInfo* info = info_list + i;

    if (!factory->IsTouchDevice(info->deviceid))
      continue;

    for (int j = 0; j < VAL_LAST_ENTRY; j++) {
      Valuator val = static_cast<Valuator>(j);
      XIValuatorClassInfo* valuator = FindValuator(display, info, val);
      if (valuator) {
        valuator_lookup_[info->deviceid][j] = valuator->number;
        valuator_min_[info->deviceid][j] = valuator->min;
        valuator_max_[info->deviceid][j] = valuator->max;
      }
    }
  }

  if (info_list)
    XIFreeDeviceInfo(info_list);
}

}  // namespace ui