summaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/OVR_Linux_HIDDevice.h
blob: 0f4c7f01d3c27699b29e2c229328cb4c07654aff (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
/************************************************************************************
Filename    :   OVR_Linux_HIDDevice.h
Content     :   Linux HID device implementation.
Created     :   June 13, 2013
Authors     :   Brant Lewis

Copyright   :   Copyright 2013 Oculus VR, Inc. All Rights reserved.

Licensed under the Oculus VR SDK License Version 2.0 (the "License"); 
you may not use the Oculus VR SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*************************************************************************************/

#ifndef OVR_LINUX_HIDDevice_h
#define OVR_LINUX_HIDDevice_h

#include "OVR_HIDDevice.h"
#include "OVR_Linux_DeviceManager.h"
#include <libudev.h>

namespace OVR { namespace Linux {

class HIDDeviceManager;

//-------------------------------------------------------------------------------------
// ***** Linux HIDDevice

class HIDDevice : public OVR::HIDDevice, public DeviceManagerThread::Notifier
{
private:
    friend class HIDDeviceManager;

public:
    HIDDevice(HIDDeviceManager* manager);

    // This is a minimal constructor used during enumeration for us to pass
    // a HIDDevice to the visit function (so that it can query feature reports).
    HIDDevice(HIDDeviceManager* manager, int device_handle);
    
    virtual ~HIDDevice();

    bool HIDInitialize(const String& path);
    void HIDShutdown();
    
    virtual bool SetFeatureReport(UByte* data, UInt32 length);
	virtual bool GetFeatureReport(UByte* data, UInt32 length);

    // DeviceManagerThread::Notifier
    void OnEvent(int i, int fd);
    UInt64 OnTicks(UInt64 ticksMks);

    bool OnDeviceNotification(MessageType messageType,
                              HIDDeviceDesc* device_info,
                              bool* error);

private:
    bool initInfo();
    bool openDevice(const char* dev_path);
    void closeDevice(bool wasUnplugged);
    void closeDeviceOnIOError();
    bool setupDevicePluggedInNotification();

    bool                    InMinimalMode;
    HIDDeviceManager*       HIDManager;
    int                     DeviceHandle;     // file handle to the device
    HIDDeviceDesc           DevDesc;
    
    enum { ReadBufferSize = 96 };
    UByte                   ReadBuffer[ReadBufferSize];

    UInt16                  InputReportBufferLength;
    UInt16                  OutputReportBufferLength;
    UInt16                  FeatureReportBufferLength;
};


//-------------------------------------------------------------------------------------
// ***** Linux HIDDeviceManager

class HIDDeviceManager : public OVR::HIDDeviceManager, public DeviceManagerThread::Notifier
{
	friend class HIDDevice;

public:
    HIDDeviceManager(Linux::DeviceManager* Manager);
    virtual ~HIDDeviceManager();

    virtual bool Initialize();
    virtual void Shutdown();

    virtual bool Enumerate(HIDEnumerateVisitor* enumVisitor);
    virtual OVR::HIDDevice* Open(const String& path);

    static HIDDeviceManager* CreateInternal(DeviceManager* manager);

    void OnEvent(int i, int fd);
    
private:
    bool initializeManager();
    bool initVendorProductVersion(udev_device* device, HIDDeviceDesc* pDevDesc);
    bool getPath(udev_device* device, String* pPath);
    bool getIntProperty(udev_device* device, const char* key, int32_t* pResult);
    bool getStringProperty(udev_device* device,
                           const char* propertyName,
                           OVR::String* pResult);
    bool getFullDesc(udev_device* device, HIDDeviceDesc* desc);
    bool GetDescriptorFromPath(const char* dev_path, HIDDeviceDesc* desc);
    
    bool AddNotificationDevice(HIDDevice* device);
    bool RemoveNotificationDevice(HIDDevice* device);
    
    DeviceManager*           DevManager;

    udev*                    UdevInstance;     // a handle to the udev library instance
    udev_monitor*            HIDMonitor;
    int                      HIDMonHandle;     // the udev_monitor file handle

    Array<HIDDevice*>        NotificationDevices;
};

}} // namespace OVR::Linux

#endif // OVR_Linux_HIDDevice_h