mirror of
https://github.com/gnif/LookingGlass.git
synced 2026-08-09 08:41:31 +00:00
[idd] ipc: connect LGInput to LGIdd
Move the reusable named-pipe endpoint and shared support code into the LGCommon static library. Run a dedicated server in LGIdd and a reconnecting client in LGInput, with device-lifecycle handling and report framing. Refactor the helper pipe to use the same endpoint implementation.
This commit is contained in:
@@ -20,10 +20,13 @@
|
||||
|
||||
#include "CHIDDevice.h"
|
||||
|
||||
#include "CDebug.h"
|
||||
#include "CSRWLock.h"
|
||||
#include "HIDReports.h"
|
||||
#include "ipc/CInputPipeClient.h"
|
||||
|
||||
#include <hidport.h>
|
||||
#include <new>
|
||||
|
||||
static constexpr USHORT LG_INPUT_VENDOR_ID = 0x0000;
|
||||
static constexpr USHORT LG_INPUT_PRODUCT_ID = 0x0000;
|
||||
@@ -42,12 +45,14 @@ struct HIDDeviceContext
|
||||
HID_DEVICE_ATTRIBUTES attributes;
|
||||
HID_DESCRIPTOR descriptor;
|
||||
UCHAR keyboardLeds;
|
||||
SRWLOCK lifecycleLock;
|
||||
SRWLOCK reportLock;
|
||||
bool active;
|
||||
bool stopping;
|
||||
size_t reportHead;
|
||||
size_t reportCount;
|
||||
HIDQueuedReport reports[REPORT_QUEUE_LENGTH];
|
||||
CInputPipeClient * inputPipe;
|
||||
};
|
||||
|
||||
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(HIDDeviceContext, HIDGetDeviceContext);
|
||||
@@ -57,6 +62,12 @@ static HIDDeviceContext * s_device = nullptr;
|
||||
|
||||
EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL HIDEvtIoDeviceControl;
|
||||
EVT_WDF_OBJECT_CONTEXT_CLEANUP HIDEvtReportQueueCleanup;
|
||||
EVT_WDF_OBJECT_CONTEXT_CLEANUP HIDEvtDeviceCleanup;
|
||||
EVT_WDF_DEVICE_SELF_MANAGED_IO_INIT HIDEvtSelfManagedIoInit;
|
||||
EVT_WDF_DEVICE_SELF_MANAGED_IO_CLEANUP HIDEvtSelfManagedIoCleanup;
|
||||
EVT_WDF_DEVICE_SELF_MANAGED_IO_FLUSH HIDEvtSelfManagedIoFlush;
|
||||
EVT_WDF_DEVICE_SELF_MANAGED_IO_SUSPEND HIDEvtSelfManagedIoSuspend;
|
||||
EVT_WDF_DEVICE_SELF_MANAGED_IO_RESTART HIDEvtSelfManagedIoRestart;
|
||||
|
||||
static NTSTATUS CopyToRequest(
|
||||
_In_ WDFREQUEST request,
|
||||
@@ -191,6 +202,7 @@ static NTSTATUS ReadReport(
|
||||
|
||||
static NTSTATUS ActivateDevice(_Inout_ HIDDeviceContext * context)
|
||||
{
|
||||
CSRWExclusiveLock lifecycleLock(&context->lifecycleLock);
|
||||
CSRWExclusiveLock lock(&context->reportLock);
|
||||
if (context->stopping)
|
||||
return STATUS_DEVICE_NOT_READY;
|
||||
@@ -202,13 +214,16 @@ static NTSTATUS ActivateDevice(_Inout_ HIDDeviceContext * context)
|
||||
|
||||
static NTSTATUS DeactivateDevice(_Inout_ HIDDeviceContext * context)
|
||||
{
|
||||
CSRWExclusiveLock lock(&context->reportLock);
|
||||
if (context->stopping)
|
||||
return STATUS_DEVICE_NOT_READY;
|
||||
CSRWExclusiveLock lifecycleLock(&context->lifecycleLock);
|
||||
{
|
||||
CSRWExclusiveLock lock(&context->reportLock);
|
||||
if (context->stopping)
|
||||
return STATUS_DEVICE_NOT_READY;
|
||||
|
||||
context->active = false;
|
||||
context->reportHead = 0;
|
||||
context->reportCount = 0;
|
||||
context->active = false;
|
||||
context->reportHead = 0;
|
||||
context->reportCount = 0;
|
||||
}
|
||||
WdfIoQueuePurgeSynchronously(context->reportQueue);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
@@ -241,8 +256,21 @@ NTSTATUS CHIDDevice::Create(_Inout_ PWDFDEVICE_INIT deviceInit)
|
||||
{
|
||||
WdfFdoInitSetFilter(deviceInit);
|
||||
|
||||
WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
|
||||
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
|
||||
pnpPowerCallbacks.EvtDeviceSelfManagedIoInit = HIDEvtSelfManagedIoInit;
|
||||
pnpPowerCallbacks.EvtDeviceSelfManagedIoCleanup =
|
||||
HIDEvtSelfManagedIoCleanup;
|
||||
pnpPowerCallbacks.EvtDeviceSelfManagedIoFlush = HIDEvtSelfManagedIoFlush;
|
||||
pnpPowerCallbacks.EvtDeviceSelfManagedIoSuspend =
|
||||
HIDEvtSelfManagedIoSuspend;
|
||||
pnpPowerCallbacks.EvtDeviceSelfManagedIoRestart =
|
||||
HIDEvtSelfManagedIoRestart;
|
||||
WdfDeviceInitSetPnpPowerEventCallbacks(deviceInit, &pnpPowerCallbacks);
|
||||
|
||||
WDF_OBJECT_ATTRIBUTES attributes;
|
||||
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, HIDDeviceContext);
|
||||
attributes.EvtCleanupCallback = HIDEvtDeviceCleanup;
|
||||
|
||||
WDFDEVICE device;
|
||||
NTSTATUS status = WdfDeviceCreate(&deviceInit, &attributes, &device);
|
||||
@@ -251,8 +279,12 @@ NTSTATUS CHIDDevice::Create(_Inout_ PWDFDEVICE_INIT deviceInit)
|
||||
|
||||
HIDDeviceContext * context = HIDGetDeviceContext(device);
|
||||
RtlZeroMemory(context, sizeof(*context));
|
||||
InitializeSRWLock(&context->lifecycleLock);
|
||||
InitializeSRWLock(&context->reportLock);
|
||||
context->active = true;
|
||||
context->inputPipe = new (std::nothrow) CInputPipeClient;
|
||||
if (!context->inputPipe)
|
||||
return STATUS_INSUFFICIENT_RESOURCES;
|
||||
|
||||
context->attributes.Size =
|
||||
static_cast<ULONG>(sizeof(context->attributes));
|
||||
@@ -277,6 +309,11 @@ NTSTATUS CHIDDevice::Create(_Inout_ PWDFDEVICE_INIT deviceInit)
|
||||
|
||||
{
|
||||
CSRWExclusiveLock lock(&s_deviceLock);
|
||||
if (s_device)
|
||||
{
|
||||
DEBUG_ERROR("Only one LGInput device instance is supported");
|
||||
return STATUS_DEVICE_BUSY;
|
||||
}
|
||||
s_device = context;
|
||||
}
|
||||
return STATUS_SUCCESS;
|
||||
@@ -324,6 +361,22 @@ NTSTATUS CHIDDevice::SubmitReport(
|
||||
return status;
|
||||
}
|
||||
|
||||
NTSTATUS CHIDDevice::ClearReports()
|
||||
{
|
||||
CSRWSharedLock deviceLock(&s_deviceLock);
|
||||
HIDDeviceContext * context = s_device;
|
||||
if (!context)
|
||||
return STATUS_DEVICE_NOT_READY;
|
||||
|
||||
CSRWExclusiveLock reportLock(&context->reportLock);
|
||||
if (context->stopping)
|
||||
return STATUS_DEVICE_NOT_READY;
|
||||
|
||||
context->reportHead = 0;
|
||||
context->reportCount = 0;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
VOID HIDEvtIoDeviceControl(
|
||||
_In_ WDFQUEUE queue,
|
||||
_In_ WDFREQUEST request,
|
||||
@@ -398,3 +451,62 @@ VOID HIDEvtReportQueueCleanup(_In_ WDFOBJECT object)
|
||||
context->reportCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
VOID HIDEvtDeviceCleanup(_In_ WDFOBJECT object)
|
||||
{
|
||||
HIDDeviceContext * context = HIDGetDeviceContext((WDFDEVICE)object);
|
||||
if (context->inputPipe)
|
||||
{
|
||||
context->inputPipe->Stop();
|
||||
delete context->inputPipe;
|
||||
context->inputPipe = nullptr;
|
||||
}
|
||||
|
||||
CSRWExclusiveLock deviceLock(&s_deviceLock);
|
||||
if (s_device == context)
|
||||
s_device = nullptr;
|
||||
}
|
||||
|
||||
NTSTATUS HIDEvtSelfManagedIoInit(_In_ WDFDEVICE device)
|
||||
{
|
||||
HIDDeviceContext * context = HIDGetDeviceContext(device);
|
||||
if (!context->inputPipe || !context->inputPipe->Start())
|
||||
{
|
||||
DEBUG_ERROR("Failed to start the LGIdd input pipe client");
|
||||
return STATUS_INSUFFICIENT_RESOURCES;
|
||||
}
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
VOID HIDEvtSelfManagedIoCleanup(_In_ WDFDEVICE device)
|
||||
{
|
||||
HIDDeviceContext * context = HIDGetDeviceContext(device);
|
||||
if (context->inputPipe)
|
||||
context->inputPipe->Stop();
|
||||
}
|
||||
|
||||
VOID HIDEvtSelfManagedIoFlush(_In_ WDFDEVICE device)
|
||||
{
|
||||
HIDDeviceContext * context = HIDGetDeviceContext(device);
|
||||
if (context->inputPipe)
|
||||
context->inputPipe->Stop();
|
||||
}
|
||||
|
||||
NTSTATUS HIDEvtSelfManagedIoSuspend(_In_ WDFDEVICE device)
|
||||
{
|
||||
HIDDeviceContext * context = HIDGetDeviceContext(device);
|
||||
if (context->inputPipe)
|
||||
context->inputPipe->Stop();
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
NTSTATUS HIDEvtSelfManagedIoRestart(_In_ WDFDEVICE device)
|
||||
{
|
||||
HIDDeviceContext * context = HIDGetDeviceContext(device);
|
||||
if (!context->inputPipe || !context->inputPipe->Start())
|
||||
{
|
||||
DEBUG_ERROR("Failed to restart the LGIdd input pipe client");
|
||||
return STATUS_INSUFFICIENT_RESOURCES;
|
||||
}
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -30,4 +30,5 @@ public:
|
||||
static NTSTATUS SubmitReport(
|
||||
_In_reads_bytes_(size) const void * report,
|
||||
_In_ size_t size);
|
||||
static NTSTATUS ClearReports();
|
||||
};
|
||||
|
||||
@@ -20,23 +20,16 @@
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="$(ProjectDir)..\LGCommon\CDebug.cpp">
|
||||
<Link>Common\CDebug.cpp</Link>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CHIDDevice.cpp" />
|
||||
<ClCompile Include="Driver.cpp" />
|
||||
<ClCompile Include="HIDReports.cpp" />
|
||||
<ClCompile Include="ipc\CInputPipeClient.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="$(ProjectDir)..\LGCommon\CDebug.h">
|
||||
<Link>Common\CDebug.h</Link>
|
||||
</ClInclude>
|
||||
<ClInclude Include="$(ProjectDir)..\LGCommon\CSRWLock.h">
|
||||
<Link>Common\CSRWLock.h</Link>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CHIDDevice.h" />
|
||||
<ClInclude Include="Driver.h" />
|
||||
<ClInclude Include="HIDReports.h" />
|
||||
<ClInclude Include="ipc\CInputPipeClient.h" />
|
||||
<ClInclude Include="Trace.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -99,6 +92,11 @@
|
||||
<ItemGroup>
|
||||
<FilesToPackage Include="$(TargetPath)" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LGCommon\LGCommon.vcxproj">
|
||||
<Project>{acb90e34-01ca-4b86-813b-3d20904994c6}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets" />
|
||||
</Project>
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
<Filter Include="HID">
|
||||
<UniqueIdentifier>{D9688549-A0BE-46E6-8FAB-AE454D90E0FA}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Common">
|
||||
<UniqueIdentifier>{938E49D6-F954-4EBE-80AB-E0F67E677F27}</UniqueIdentifier>
|
||||
<Filter Include="IPC">
|
||||
<UniqueIdentifier>{6A9D97A5-1A6C-4A95-A2D0-A7C2D6BE459D}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -18,12 +18,6 @@
|
||||
</Inf>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="$(ProjectDir)..\LGCommon\CDebug.h">
|
||||
<Filter>Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="$(ProjectDir)..\LGCommon\CSRWLock.h">
|
||||
<Filter>Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CHIDDevice.h">
|
||||
<Filter>Driver</Filter>
|
||||
</ClInclude>
|
||||
@@ -33,14 +27,14 @@
|
||||
<ClInclude Include="HIDReports.h">
|
||||
<Filter>HID</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ipc\CInputPipeClient.h">
|
||||
<Filter>IPC</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Trace.h">
|
||||
<Filter>Driver</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="$(ProjectDir)..\LGCommon\CDebug.cpp">
|
||||
<Filter>Common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CHIDDevice.cpp">
|
||||
<Filter>Driver</Filter>
|
||||
</ClCompile>
|
||||
@@ -50,5 +44,8 @@
|
||||
<ClCompile Include="HIDReports.cpp">
|
||||
<Filter>HID</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ipc\CInputPipeClient.cpp">
|
||||
<Filter>IPC</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
93
idd/LGInput/ipc/CInputPipeClient.cpp
Normal file
93
idd/LGInput/ipc/CInputPipeClient.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "CInputPipeClient.h"
|
||||
|
||||
#include "CDebug.h"
|
||||
#include "InputPipeProtocol.h"
|
||||
#include "../CHIDDevice.h"
|
||||
|
||||
bool CInputPipeClient::Start()
|
||||
{
|
||||
m_lastSequence = 0;
|
||||
m_endpoint.SetHandler(this);
|
||||
return m_endpoint.Start(
|
||||
LG_INPUT_PIPE_NAME,
|
||||
CPipeEndpoint::Mode::Client,
|
||||
sizeof(LGInputPipeMessage));
|
||||
}
|
||||
|
||||
void CInputPipeClient::Stop()
|
||||
{
|
||||
const bool wasRunning = m_endpoint.IsRunning();
|
||||
m_endpoint.Stop();
|
||||
m_lastSequence = 0;
|
||||
if (wasRunning)
|
||||
CHIDDevice::ClearReports();
|
||||
}
|
||||
|
||||
void CInputPipeClient::OnPipeConnected()
|
||||
{
|
||||
m_lastSequence = 0;
|
||||
DEBUG_INFO("Connected to the LGIdd input transport");
|
||||
}
|
||||
|
||||
void CInputPipeClient::OnPipeDisconnected()
|
||||
{
|
||||
m_lastSequence = 0;
|
||||
CHIDDevice::ClearReports();
|
||||
DEBUG_INFO("Disconnected from the LGIdd input transport; reconnecting");
|
||||
}
|
||||
|
||||
bool CInputPipeClient::OnPipeMessage(
|
||||
const void * frame,
|
||||
size_t size)
|
||||
{
|
||||
if (size != sizeof(LGInputPipeMessage))
|
||||
return false;
|
||||
|
||||
const LGInputPipeMessage & message =
|
||||
*static_cast<const LGInputPipeMessage *>(frame);
|
||||
if (message.magic != LG_INPUT_PIPE_MAGIC ||
|
||||
message.version != LG_INPUT_PIPE_VERSION ||
|
||||
message.type != LG_INPUT_PIPE_MESSAGE_REPORT ||
|
||||
!message.payloadSize ||
|
||||
message.payloadSize > sizeof(message.payload))
|
||||
return false;
|
||||
|
||||
if (!message.sequence ||
|
||||
(m_lastSequence && message.sequence != m_lastSequence + 1))
|
||||
{
|
||||
DEBUG_WARN("LGInput pipe report sequence changed unexpectedly");
|
||||
return false;
|
||||
}
|
||||
m_lastSequence = message.sequence;
|
||||
|
||||
const NTSTATUS status =
|
||||
CHIDDevice::SubmitReport(message.payload, message.payloadSize);
|
||||
if (status == STATUS_INVALID_PARAMETER)
|
||||
return false;
|
||||
|
||||
if (status == STATUS_BUFFER_OVERFLOW)
|
||||
DEBUG_WARN("LGInput HID report queue is full; dropping a report");
|
||||
else if (!NT_SUCCESS(status) && status != STATUS_DEVICE_NOT_READY)
|
||||
DEBUG_WARN_HR(status, "Failed to submit an LGInput HID report");
|
||||
return true;
|
||||
}
|
||||
44
idd/LGInput/ipc/CInputPipeClient.h
Normal file
44
idd/LGInput/ipc/CInputPipeClient.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CPipeEndpoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class CInputPipeClient : private IPipeEndpointHandler
|
||||
{
|
||||
public:
|
||||
~CInputPipeClient() { Stop(); }
|
||||
|
||||
bool Start();
|
||||
void Stop();
|
||||
bool IsConnected() const { return m_endpoint.IsConnected(); }
|
||||
|
||||
private:
|
||||
void OnPipeConnected() override;
|
||||
void OnPipeDisconnected() override;
|
||||
bool OnPipeMessage(const void * message, size_t size) override;
|
||||
|
||||
CPipeEndpoint m_endpoint;
|
||||
uint64_t m_lastSequence = 0;
|
||||
};
|
||||
Reference in New Issue
Block a user