Libpointing
An open-source cross-platform library to get raw events from pointing devices and master transfer functions.
 All Classes Functions Variables Typedefs Enumerator Pages
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 
22 #ifdef __APPLE__
23 #include <IOKit/hid/IOHIDManager.h>
24 #define identifier IOHIDDeviceRef
25 #endif
26 
27 #ifdef __linux__
28 #define identifier std::string
29 #endif
30 
31 #ifdef _WIN32
32 #include <windows.h>
33 #define identifier HANDLE
34 #endif
35 
36 #ifndef POINTINGDEVICEMANAGER_H
37 #define POINTINGDEVICEMANAGER_H
38 
39 namespace pointing
40 {
41  class SystemPointingDevice;
42 
44  {
45  URI devURI;
46 
47  int vendorID = 0;
48  int productID = 0;
49 
50  std::string vendor = "???";
51  std::string product = "???";
52 
53  // To use set of PointingDeviceDescriptors
54  bool operator < (const PointingDeviceDescriptor& rhs) const;
55  };
56 
57  typedef void (*DeviceUpdateCallback)(void *context, const PointingDeviceDescriptor &descriptor, bool wasAdded);
58  typedef std::set<PointingDeviceDescriptor> PointingDescriptorSet;
59 
64  typedef PointingDescriptorSet::iterator PointingDescriptorIterator;
65  typedef PointingDescriptorSet::const_iterator PointingDescriptorConstIterator;
67 
68  struct CallbackInfo
69  {
70  DeviceUpdateCallback callbackFunc;
71  void *context;
72  CallbackInfo(DeviceUpdateCallback callbackFunc, void *context)
73  :callbackFunc(callbackFunc),context(context) { }
74 
75  // To use the set of the CallbackInfos
76  bool operator < (const CallbackInfo& rhs) const;
77  };
78 
88  {
89  friend class SystemPointingDevice;
90 
91  private:
92  PointingDescriptorSet descriptors;
93 
94  void addDescriptor(PointingDeviceDescriptor &descriptor);
95  void removeDescriptor(PointingDeviceDescriptor &descriptor);
96 
97  std::set<CallbackInfo> callbackInfos;
98 
99  void callCallbackFunctions(PointingDeviceDescriptor &descriptor, bool wasAdded);
100 
101  static PointingDeviceManager *singleManager;
102 
103  void convertAnyCandidates();
104 
105  void matchCandidates();
106 
107  protected:
108  typedef std::list<SystemPointingDevice *> PointingList;
109 
110  // This struct can be extended in subclasses to add
111  // platform-specific data
113  {
115  PointingList pointingList;
116  virtual ~PointingDeviceData() {}
117  };
118 
119  std::map<identifier, PointingDeviceData *> devMap;
120 
121  PointingList candidates;
122  int debugLevel = 0;
123 
124  // Should be implemented by a subclass
125  virtual void processMatching(PointingDeviceData *pdd, SystemPointingDevice *device)=0;
126 
127  void activateDevice(SystemPointingDevice *device, PointingDeviceData *pdd);
128 
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  virtual ~PointingDeviceManager(void) {}
204  };
205 }
206 
207 #endif // POINTINGDEVICEMANAGER_H
URI anyToSpecific(const URI &anyURI) const
anyToSpecific Converts a given URI into platform-specific unique URI
Definition: PointingDeviceManager.cpp:109
The PointingDeviceManager class is a helper class which enumerates the list of existing pointing devi...
Definition: PointingDeviceManager.h:87
The SystemPointingDevice class is used to represent Pointing Devices connected to the computer...
Definition: SystemPointingDevice.h:28
void registerDevice(identifier key, PointingDeviceData *pdd)
Called from subclasses.
Definition: PointingDeviceManager.cpp:226
Definition: PointingDeviceManager.h:112
void addDeviceUpdateCallback(DeviceUpdateCallback callback, void *context)
Adds the callback function which is called when a device was added or removed.
Definition: PointingDeviceManager.cpp:154
void removeDeviceUpdateCallback(DeviceUpdateCallback callback, void *context)
Removes the callback function which is called when a device was added or removed. ...
Definition: PointingDeviceManager.cpp:160
size_t size() const
size
Definition: PointingDeviceManager.h:193
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:133
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:256
Definition: PointingDeviceManager.h:43
Definition: PointingDeviceManager.h:68