1// Copyright (c) 2012 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#ifndef UI_EVENTS_X_DEVICE_LIST_CACHE_X_H_
6#define UI_EVENTS_X_DEVICE_LIST_CACHE_X_H_
7
8#include <X11/extensions/XInput.h>
9#include <X11/extensions/XInput2.h>
10
11#include <map>
12
13#include "base/basictypes.h"
14#include "ui/events/events_base_export.h"
15
16template <typename T> struct DefaultSingletonTraits;
17
18typedef struct _XDisplay Display;
19
20template <typename T>
21struct DeviceList {
22  DeviceList() : devices(NULL), count(0) {
23  }
24  T& operator[] (int x) {
25    return devices[x];
26  }
27  const T& operator[](int x) const { return devices[x]; }
28  T* devices;
29  int count;
30};
31
32typedef struct DeviceList<XDeviceInfo> XDeviceList;
33typedef struct DeviceList<XIDeviceInfo> XIDeviceList;
34
35namespace ui {
36
37// A class to cache the current XInput device list. This minimized the
38// round-trip time to the X server whenever such a device list is needed. The
39// update function will be called on each incoming XI_HierarchyChanged event.
40class EVENTS_BASE_EXPORT DeviceListCacheX {
41 public:
42  static DeviceListCacheX* GetInstance();
43
44  void UpdateDeviceList(Display* display);
45
46  // Returns the list of devices associated with |display|. Uses the old X11
47  // protocol to get the list of the devices.
48  const XDeviceList& GetXDeviceList(Display* display);
49
50  // Returns the list of devices associated with |display|. Uses the newer
51  // XINPUT2 protocol to get the list of devices. Before making this call, make
52  // sure that XInput2 support is available (e.g. by calling
53  // IsXInput2Available()).
54  const XIDeviceList& GetXI2DeviceList(Display* display);
55
56 private:
57  friend struct DefaultSingletonTraits<DeviceListCacheX>;
58
59  DeviceListCacheX();
60  ~DeviceListCacheX();
61
62  std::map<Display*, XDeviceList> x_dev_list_map_;
63  std::map<Display*, XIDeviceList> xi_dev_list_map_;
64
65  DISALLOW_COPY_AND_ASSIGN(DeviceListCacheX);
66};
67
68}  // namespace ui
69
70#endif  // UI_EVENTS_X_DEVICE_LIST_CACHE_X_H_
71
72