Libpointing
An open-source cross-platform library to get raw events from pointing devices and master transfer functions.
PointingDeviceManager.h
1 /* -*- mode: c++ -*-
2  *
3  * pointing/input/PointingDeviceManager.h --
4  *
5  * Initial software
6  * Authors: Izzat Mukhanov
7  * Copyright © Inria
8  *
9  * http://libpointing.org/
10  *
11  * This software may be used and distributed according to the terms of
12  * the GNU General Public License version 2 or any later version.
13  *
14  */
15 
16 #include <set>
17 #include <string>
18 #include <list>
19 #include <map>
20 #include <pointing/utils/URI.h>
21 #include <pointing/input/SystemPointingDevice.h>
22 
23 #ifdef __APPLE__
24 #include <IOKit/hid/IOHIDManager.h>
25 #define identifier IOHIDDeviceRef
26 #endif
27 
28 #ifdef __linux__
29 #define identifier std::string
30 #endif
31 
32 #ifdef _WIN32
33 #include <windows.h>
34 #define identifier HANDLE
35 #endif
36 
37 #ifndef POINTINGDEVICEMANAGER_H
38 #define POINTINGDEVICEMANAGER_H
39 
40 namespace pointing
41 {
43  {
44  URI devURI;
45 
46  int vendorID = 0;
47  int productID = 0;
48 
49  std::string vendor = "???";
50  std::string product = "???";
51 
52  // To use set of PointingDeviceDescriptors
53  bool operator < (const PointingDeviceDescriptor& rhs) const;
54  };
55 
56  typedef void (*DeviceUpdateCallback)(void *context, const PointingDeviceDescriptor &descriptor, bool wasAdded);
57  typedef std::set<PointingDeviceDescriptor> PointingDescriptorSet;
58 
63  typedef PointingDescriptorSet::iterator PointingDescriptorIterator;
64  typedef PointingDescriptorSet::const_iterator PointingDescriptorConstIterator;
66 
67  struct CallbackInfo
68  {
69  DeviceUpdateCallback callbackFunc;
70  void *context;
71  CallbackInfo(DeviceUpdateCallback callbackFunc, void *context)
72  :callbackFunc(callbackFunc),context(context) { }
73 
74  // To use the set of the CallbackInfos
75  bool operator < (const CallbackInfo& rhs) const;
76  };
77 
87  {
88  friend class SystemPointingDevice;
89 
90  protected:
91 
92  typedef std::list<SystemPointingDevice *> PointingList;
93 
94  // This struct can be extended in subclasses to add
95  // platform-specific data
97  {
99  PointingList pointingList;
100  };
101 
102  std::map<identifier, PointingDeviceData *> devMap;
103 
104  DeviceUpdateCallback callback = NULL;
105 
106  virtual ~PointingDeviceManager(void) {}
107  static PointingDeviceManager *singleManager;
108 
109  PointingDescriptorSet descriptors;
110 
111  std::set<CallbackInfo> callbackInfos;
112 
113  void callCallbackFunctions(PointingDeviceDescriptor &descriptor, bool wasAdded);
114 
115  void addDescriptor(PointingDeviceDescriptor &descriptor);
116  void removeDescriptor(PointingDeviceDescriptor &descriptor);
117 
118  PointingList candidates;
119  int debugLevel = 0;
120 
121  void convertAnyCandidates();
122 
123  void matchCandidates();
124 
125  // Should be implemented by a subclass
126  virtual void processMatching(PointingDeviceData *pdd, SystemPointingDevice *device)=0;
127 
128  void activateDevice(SystemPointingDevice *device, PointingDeviceData *pdd);
129 
135  void registerDevice(identifier key, PointingDeviceData *pdd);
136  bool unregisterDevice(identifier);
138 
139  void printDeviceInfo(PointingDeviceData *pdd, bool add);
140 
141  PointingDeviceData *findDataForDevice(SystemPointingDevice *device);
142 
148  virtual void addPointingDevice(SystemPointingDevice *device);
149  virtual void removePointingDevice(SystemPointingDevice *device);
151 
152  public:
153 
158  void addDeviceUpdateCallback(DeviceUpdateCallback callback, void *context);
159 
164  void removeDeviceUpdateCallback(DeviceUpdateCallback callback, void *context);
165 
170  static PointingDeviceManager *get();
171 
177  URI anyToSpecific(const URI &anyURI) const;
178 
185  URI generalizeAny(const URI &anyURI) const;
186 
187  //static void destroy();
188 
193  size_t size() const { return descriptors.size(); }
194 
195  /*
196  * Delegate the iteration to the inner set of the descriptors
197  */
199  PointingDescriptorIterator begin() { return descriptors.begin(); }
200  PointingDescriptorIterator end() { return descriptors.end(); }
202  };
203 }
204 
205 #endif // POINTINGDEVICEMANAGER_H
Definition: PointingDeviceManager.h:67
void registerDevice(identifier key, PointingDeviceData *pdd)
Called from subclasses.
Definition: PointingDeviceManager.cpp:225
The PointingDeviceManager class is a helper class which enumerates the list of existing pointing devi...
Definition: PointingDeviceManager.h:86
URI anyToSpecific(const URI &anyURI) const
anyToSpecific Converts a given URI into platform-specific unique URI
Definition: PointingDeviceManager.cpp:108
virtual void removePointingDevice(SystemPointingDevice *device)
Whenever there is a PointingDevice is created or deleted those methods are called internally from a S...
Definition: PointingDeviceManager.cpp:269
URI generalizeAny(const URI &anyURI) const
generalizeAny Remove all arguments from the given any: URI except for vendor vendor and product argum...
Definition: PointingDeviceManager.cpp:132
virtual void addPointingDevice(SystemPointingDevice *device)
Whenever there is a PointingDevice is created or deleted those methods are called internally from a S...
Definition: PointingDeviceManager.cpp:255
Definition: PointingDeviceManager.h:96
Definition: DummyPointingDevice.cpp:23
size_t size() const
size
Definition: PointingDeviceManager.h:193
The SystemPointingDevice class is used to represent Pointing Devices connected to the computer...
Definition: SystemPointingDevice.h:28
Definition: PointingDeviceManager.h:42
void removeDeviceUpdateCallback(DeviceUpdateCallback callback, void *context)
Removes the callback function which is called when a device was added or removed. ...
Definition: PointingDeviceManager.cpp:159
bool unregisterDevice(identifier)
Called from subclasses.
Definition: PointingDeviceManager.cpp:234
void addDeviceUpdateCallback(DeviceUpdateCallback callback, void *context)
Adds the callback function which is called when a device was added or removed.
Definition: PointingDeviceManager.cpp:153