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:
585
idd/LGCommon/CPipeEndpoint.cpp
Normal file
585
idd/LGCommon/CPipeEndpoint.cpp
Normal file
@@ -0,0 +1,585 @@
|
||||
/**
|
||||
* 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 "CPipeEndpoint.h"
|
||||
|
||||
#include "CDebug.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
|
||||
const DWORD CPipeEndpoint::CLIENT_RETRY_INITIAL_MS = 100;
|
||||
const DWORD CPipeEndpoint::CLIENT_RETRY_MAX_MS = 2000;
|
||||
const DWORD CPipeEndpoint::SERVER_RETRY_MS = 1000;
|
||||
const DWORD CPipeEndpoint::WRITE_TIMEOUT_MS = 250;
|
||||
const DWORD CPipeEndpoint::WAIT_FIRST_OBJECT_VALUE = 0;
|
||||
|
||||
bool CPipeEndpoint::IsDisconnectedError(DWORD error)
|
||||
{
|
||||
return error == ERROR_BROKEN_PIPE ||
|
||||
error == ERROR_NO_DATA ||
|
||||
error == ERROR_PIPE_NOT_CONNECTED;
|
||||
}
|
||||
|
||||
CPipeEndpoint::PipeIoResult CPipeEndpoint::WaitForOverlapped(
|
||||
HANDLE pipe,
|
||||
HANDLE ioEvent,
|
||||
OVERLAPPED * overlapped,
|
||||
DWORD * transferred,
|
||||
DWORD timeoutMs)
|
||||
{
|
||||
const HANDLE waitHandles[] = { ioEvent, m_stopEvent };
|
||||
const DWORD waitResult = WaitForMultipleObjects(
|
||||
_countof(waitHandles), waitHandles, FALSE, timeoutMs);
|
||||
|
||||
if (waitResult == WAIT_TIMEOUT)
|
||||
{
|
||||
CancelIoEx(pipe, overlapped);
|
||||
if (GetOverlappedResult(pipe, overlapped, transferred, TRUE))
|
||||
return PipeIoResult::Success;
|
||||
DEBUG_WARN("Named pipe write timed out");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
if (waitResult == WAIT_FIRST_OBJECT_VALUE + 1)
|
||||
{
|
||||
CancelIoEx(pipe, overlapped);
|
||||
GetOverlappedResult(pipe, overlapped, transferred, TRUE);
|
||||
return PipeIoResult::Stopped;
|
||||
}
|
||||
|
||||
if (waitResult != WAIT_FIRST_OBJECT_VALUE)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to wait for named pipe I/O");
|
||||
CancelIoEx(pipe, overlapped);
|
||||
GetOverlappedResult(pipe, overlapped, transferred, TRUE);
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
if (GetOverlappedResult(pipe, overlapped, transferred, FALSE))
|
||||
return PipeIoResult::Success;
|
||||
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_OPERATION_ABORTED &&
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
return PipeIoResult::Stopped;
|
||||
|
||||
if (IsDisconnectedError(error))
|
||||
return PipeIoResult::Disconnected;
|
||||
|
||||
DEBUG_WARN_HR(error, "Named pipe I/O failed");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
CPipeEndpoint::PipeIoResult CPipeEndpoint::ReadMessage(
|
||||
HANDLE pipe,
|
||||
HANDLE ioEvent,
|
||||
void * message,
|
||||
DWORD messageSize,
|
||||
DWORD * bytesRead)
|
||||
{
|
||||
ResetEvent(ioEvent);
|
||||
OVERLAPPED overlapped = {};
|
||||
overlapped.hEvent = ioEvent;
|
||||
|
||||
if (ReadFile(pipe, message, messageSize, bytesRead, &overlapped))
|
||||
return PipeIoResult::Success;
|
||||
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_IO_PENDING)
|
||||
return WaitForOverlapped(
|
||||
pipe, ioEvent, &overlapped, bytesRead);
|
||||
|
||||
if (error == ERROR_OPERATION_ABORTED &&
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
return PipeIoResult::Stopped;
|
||||
|
||||
if (IsDisconnectedError(error))
|
||||
return PipeIoResult::Disconnected;
|
||||
|
||||
if (error == ERROR_MORE_DATA)
|
||||
{
|
||||
DEBUG_ERROR("Named pipe message exceeds the negotiated frame size");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
DEBUG_WARN_HR(error, "Failed to read from named pipe");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
CPipeEndpoint::PipeIoResult CPipeEndpoint::WriteMessage(
|
||||
HANDLE pipe,
|
||||
const void * message,
|
||||
DWORD messageSize)
|
||||
{
|
||||
HANDLE ioEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!ioEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe write event");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
OVERLAPPED overlapped = {};
|
||||
overlapped.hEvent = ioEvent;
|
||||
DWORD bytesWritten = 0;
|
||||
PipeIoResult result = PipeIoResult::Success;
|
||||
|
||||
if (!WriteFile(pipe, message, messageSize, &bytesWritten, &overlapped))
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_IO_PENDING)
|
||||
result = WaitForOverlapped(
|
||||
pipe,
|
||||
ioEvent,
|
||||
&overlapped,
|
||||
&bytesWritten,
|
||||
WRITE_TIMEOUT_MS);
|
||||
else if (IsDisconnectedError(error))
|
||||
result = PipeIoResult::Disconnected;
|
||||
else if (error == ERROR_OPERATION_ABORTED &&
|
||||
WaitForSingleObject(m_stopEvent, 0) ==
|
||||
WAIT_FIRST_OBJECT_VALUE)
|
||||
result = PipeIoResult::Stopped;
|
||||
else
|
||||
{
|
||||
DEBUG_WARN_HR(error, "Failed to write to named pipe");
|
||||
result = PipeIoResult::Error;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == PipeIoResult::Success && bytesWritten != messageSize)
|
||||
{
|
||||
DEBUG_ERROR(
|
||||
"Short named pipe write, expected %lu bytes, wrote %lu bytes",
|
||||
messageSize,
|
||||
bytesWritten);
|
||||
result = PipeIoResult::Error;
|
||||
}
|
||||
|
||||
CloseHandle(ioEvent);
|
||||
return result;
|
||||
}
|
||||
|
||||
CPipeEndpoint::~CPipeEndpoint()
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::Start(
|
||||
const wchar_t * pipeName,
|
||||
Mode mode,
|
||||
size_t messageSize)
|
||||
{
|
||||
Stop();
|
||||
|
||||
if (!pipeName || !*pipeName || !messageSize || messageSize > MAXDWORD)
|
||||
return false;
|
||||
|
||||
m_pipeName = pipeName;
|
||||
m_mode = mode;
|
||||
m_messageSize = messageSize;
|
||||
m_stopEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!m_stopEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe stop event");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_mode == Mode::Server)
|
||||
{
|
||||
HANDLE pipe = CreateServerPipe();
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(m_stopEvent);
|
||||
m_stopEvent = nullptr;
|
||||
return false;
|
||||
}
|
||||
PublishPipe(pipe);
|
||||
}
|
||||
|
||||
m_running.store(true);
|
||||
m_thread = CreateThread(nullptr, 0, ThreadProc, this, 0, nullptr);
|
||||
if (!m_thread)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe thread");
|
||||
m_running.store(false);
|
||||
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(m_pipe);
|
||||
m_pipe = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
|
||||
CloseHandle(m_stopEvent);
|
||||
m_stopEvent = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::Stop()
|
||||
{
|
||||
m_running.store(false);
|
||||
if (m_stopEvent)
|
||||
SetEvent(m_stopEvent);
|
||||
|
||||
AcquireSRWLockShared(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE)
|
||||
CancelIoEx(m_pipe, nullptr);
|
||||
ReleaseSRWLockShared(&m_pipeLock);
|
||||
|
||||
if (m_thread)
|
||||
{
|
||||
WaitForSingleObject(m_thread, INFINITE);
|
||||
CloseHandle(m_thread);
|
||||
m_thread = nullptr;
|
||||
}
|
||||
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(m_pipe);
|
||||
m_pipe = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
|
||||
if (m_stopEvent)
|
||||
{
|
||||
CloseHandle(m_stopEvent);
|
||||
m_stopEvent = nullptr;
|
||||
}
|
||||
|
||||
m_connected.store(false);
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::Send(const void * message, size_t size)
|
||||
{
|
||||
if (!message || size != m_messageSize || !IsRunning() || !IsConnected())
|
||||
return false;
|
||||
|
||||
bool success = false;
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE && IsConnected())
|
||||
{
|
||||
const PipeIoResult result = WriteMessage(
|
||||
m_pipe,
|
||||
message,
|
||||
static_cast<DWORD>(size));
|
||||
success = result == PipeIoResult::Success;
|
||||
if (!success)
|
||||
{
|
||||
m_connected.store(false);
|
||||
CancelIoEx(m_pipe, nullptr);
|
||||
}
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
return success;
|
||||
}
|
||||
|
||||
DWORD WINAPI CPipeEndpoint::ThreadProc(void * context)
|
||||
{
|
||||
static_cast<CPipeEndpoint *>(context)->Thread();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::Thread()
|
||||
{
|
||||
if (m_mode == Mode::Server)
|
||||
RunServer();
|
||||
else
|
||||
RunClient();
|
||||
|
||||
m_running.store(false);
|
||||
m_connected.store(false);
|
||||
}
|
||||
|
||||
HANDLE CPipeEndpoint::CreateServerPipe()
|
||||
{
|
||||
const DWORD bufferSize = static_cast<DWORD>(
|
||||
std::max<size_t>(m_messageSize, 1024));
|
||||
|
||||
HANDLE pipe = CreateNamedPipeW(
|
||||
m_pipeName.c_str(),
|
||||
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
||||
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
|
||||
1,
|
||||
bufferSize,
|
||||
bufferSize,
|
||||
0,
|
||||
nullptr);
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe %ls", m_pipeName.c_str());
|
||||
return pipe;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::RunServer()
|
||||
{
|
||||
HANDLE ioEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!ioEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe I/O event");
|
||||
AcquireSRWLockShared(&m_pipeLock);
|
||||
const HANDLE pipe = m_pipe;
|
||||
ReleaseSRWLockShared(&m_pipeLock);
|
||||
if (pipe != INVALID_HANDLE_VALUE)
|
||||
ClosePipe(pipe);
|
||||
return;
|
||||
}
|
||||
|
||||
HANDLE pipe = INVALID_HANDLE_VALUE;
|
||||
AcquireSRWLockShared(&m_pipeLock);
|
||||
pipe = m_pipe;
|
||||
ReleaseSRWLockShared(&m_pipeLock);
|
||||
|
||||
while (IsRunning())
|
||||
{
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
pipe = CreateServerPipe();
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (!WaitForRetry(SERVER_RETRY_MS))
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
PublishPipe(pipe);
|
||||
}
|
||||
|
||||
ResetEvent(ioEvent);
|
||||
OVERLAPPED overlapped = {};
|
||||
overlapped.hEvent = ioEvent;
|
||||
DWORD transferred = 0;
|
||||
PipeIoResult connectResult = PipeIoResult::Success;
|
||||
|
||||
if (!ConnectNamedPipe(pipe, &overlapped))
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_PIPE_CONNECTED)
|
||||
connectResult = PipeIoResult::Success;
|
||||
else if (error == ERROR_IO_PENDING)
|
||||
connectResult = WaitForOverlapped(
|
||||
pipe, ioEvent, &overlapped, &transferred);
|
||||
else if (error == ERROR_OPERATION_ABORTED && !IsRunning())
|
||||
connectResult = PipeIoResult::Stopped;
|
||||
else
|
||||
{
|
||||
DEBUG_WARN_HR(error, "Failed to accept named pipe client");
|
||||
connectResult = PipeIoResult::Error;
|
||||
}
|
||||
}
|
||||
|
||||
if (connectResult != PipeIoResult::Success)
|
||||
{
|
||||
ClosePipe(pipe);
|
||||
pipe = INVALID_HANDLE_VALUE;
|
||||
if (connectResult == PipeIoResult::Stopped || !IsRunning())
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!IsRunning())
|
||||
break;
|
||||
|
||||
if (!IsRunning() ||
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
break;
|
||||
|
||||
m_connected.store(true);
|
||||
DEBUG_INFO("Named pipe client connected: %ls", m_pipeName.c_str());
|
||||
if (m_handler)
|
||||
m_handler->OnPipeConnected();
|
||||
|
||||
ReadMessages(pipe);
|
||||
|
||||
m_connected.store(false);
|
||||
if (m_handler)
|
||||
m_handler->OnPipeDisconnected();
|
||||
DEBUG_INFO("Named pipe client disconnected: %ls", m_pipeName.c_str());
|
||||
|
||||
if (!DisconnectNamedPipe(pipe))
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error != ERROR_PIPE_NOT_CONNECTED)
|
||||
{
|
||||
DEBUG_WARN_HR(error, "Failed to disconnect named pipe client");
|
||||
ClosePipe(pipe);
|
||||
pipe = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pipe != INVALID_HANDLE_VALUE)
|
||||
ClosePipe(pipe);
|
||||
CloseHandle(ioEvent);
|
||||
}
|
||||
|
||||
void CPipeEndpoint::RunClient()
|
||||
{
|
||||
DWORD retryDelay = CLIENT_RETRY_INITIAL_MS;
|
||||
DWORD lastConnectError = ERROR_SUCCESS;
|
||||
|
||||
while (IsRunning())
|
||||
{
|
||||
if (m_handler && !m_handler->ShouldReconnect())
|
||||
break;
|
||||
|
||||
HANDLE pipe = CreateFileW(
|
||||
m_pipeName.c_str(),
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0,
|
||||
nullptr,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED,
|
||||
nullptr);
|
||||
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error != lastConnectError)
|
||||
{
|
||||
DEBUG_TRACE_HR(
|
||||
error, "Named pipe is not available yet: %ls", m_pipeName.c_str());
|
||||
lastConnectError = error;
|
||||
}
|
||||
|
||||
if (!WaitForRetry(retryDelay))
|
||||
break;
|
||||
retryDelay = (std::min)(retryDelay * 2, CLIENT_RETRY_MAX_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
DWORD mode = PIPE_READMODE_MESSAGE;
|
||||
if (!SetNamedPipeHandleState(pipe, &mode, nullptr, nullptr))
|
||||
{
|
||||
DEBUG_WARN_HR(GetLastError(), "Failed to set named pipe message mode");
|
||||
CloseHandle(pipe);
|
||||
if (!WaitForRetry(retryDelay))
|
||||
break;
|
||||
retryDelay = (std::min)(retryDelay * 2, CLIENT_RETRY_MAX_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!IsRunning() ||
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
{
|
||||
CloseHandle(pipe);
|
||||
break;
|
||||
}
|
||||
|
||||
PublishPipe(pipe);
|
||||
m_connected.store(true);
|
||||
retryDelay = CLIENT_RETRY_INITIAL_MS;
|
||||
lastConnectError = ERROR_SUCCESS;
|
||||
DEBUG_INFO("Named pipe connected: %ls", m_pipeName.c_str());
|
||||
if (m_handler)
|
||||
m_handler->OnPipeConnected();
|
||||
|
||||
ReadMessages(pipe);
|
||||
|
||||
m_connected.store(false);
|
||||
if (m_handler)
|
||||
m_handler->OnPipeDisconnected();
|
||||
DEBUG_INFO("Named pipe disconnected: %ls", m_pipeName.c_str());
|
||||
ClosePipe(pipe);
|
||||
|
||||
if (!WaitForRetry(retryDelay))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::ReadMessages(HANDLE pipe)
|
||||
{
|
||||
HANDLE ioEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!ioEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe read event");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> message(m_messageSize);
|
||||
bool success = true;
|
||||
while (IsRunning() && IsConnected())
|
||||
{
|
||||
DWORD bytesRead = 0;
|
||||
const PipeIoResult result = ReadMessage(
|
||||
pipe,
|
||||
ioEvent,
|
||||
message.data(),
|
||||
static_cast<DWORD>(message.size()),
|
||||
&bytesRead);
|
||||
if (result != PipeIoResult::Success)
|
||||
{
|
||||
success = result == PipeIoResult::Disconnected ||
|
||||
result == PipeIoResult::Stopped;
|
||||
break;
|
||||
}
|
||||
|
||||
if (bytesRead != message.size())
|
||||
{
|
||||
DEBUG_ERROR(
|
||||
"Invalid named pipe frame size, expected %llu bytes, received %lu",
|
||||
static_cast<unsigned long long>(message.size()),
|
||||
bytesRead);
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!IsRunning() ||
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
break;
|
||||
|
||||
if (m_handler && !m_handler->OnPipeMessage(
|
||||
message.data(), message.size()))
|
||||
{
|
||||
DEBUG_ERROR("Named pipe peer sent an invalid message");
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(ioEvent);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::WaitForRetry(DWORD delayMs)
|
||||
{
|
||||
return IsRunning() &&
|
||||
WaitForSingleObject(m_stopEvent, delayMs) == WAIT_TIMEOUT;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::PublishPipe(HANDLE pipe)
|
||||
{
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
m_pipe = pipe;
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
}
|
||||
|
||||
void CPipeEndpoint::ClosePipe(HANDLE pipe)
|
||||
{
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe == pipe)
|
||||
m_pipe = INVALID_HANDLE_VALUE;
|
||||
CloseHandle(pipe);
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
}
|
||||
131
idd/LGCommon/CPipeEndpoint.h
Normal file
131
idd/LGCommon/CPipeEndpoint.h
Normal file
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* 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 <Windows.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <stddef.h>
|
||||
#include <string>
|
||||
|
||||
class IPipeEndpointHandler
|
||||
{
|
||||
public:
|
||||
virtual ~IPipeEndpointHandler() = default;
|
||||
|
||||
virtual void OnPipeConnected() {}
|
||||
virtual void OnPipeDisconnected() {}
|
||||
virtual bool ShouldReconnect() { return true; }
|
||||
virtual bool OnPipeMessage(
|
||||
_In_reads_bytes_(size) const void * message,
|
||||
_In_ size_t size) = 0;
|
||||
};
|
||||
|
||||
class CPipeEndpoint
|
||||
{
|
||||
public:
|
||||
enum class Mode
|
||||
{
|
||||
Server,
|
||||
Client,
|
||||
};
|
||||
|
||||
CPipeEndpoint() = default;
|
||||
~CPipeEndpoint();
|
||||
|
||||
CPipeEndpoint(const CPipeEndpoint &) = delete;
|
||||
CPipeEndpoint & operator=(const CPipeEndpoint &) = delete;
|
||||
|
||||
bool Start(
|
||||
_In_z_ const wchar_t * pipeName,
|
||||
_In_ Mode mode,
|
||||
_In_ size_t messageSize);
|
||||
void Stop();
|
||||
|
||||
bool Send(
|
||||
_In_reads_bytes_(size) const void * message,
|
||||
_In_ size_t size);
|
||||
|
||||
bool IsRunning() const { return m_running.load(); }
|
||||
bool IsConnected() const { return m_connected.load(); }
|
||||
|
||||
void SetHandler(_In_opt_ IPipeEndpointHandler * handler)
|
||||
{
|
||||
m_handler = handler;
|
||||
}
|
||||
|
||||
private:
|
||||
enum class PipeIoResult
|
||||
{
|
||||
Success,
|
||||
Disconnected,
|
||||
Stopped,
|
||||
Error,
|
||||
};
|
||||
|
||||
static const DWORD CLIENT_RETRY_INITIAL_MS;
|
||||
static const DWORD CLIENT_RETRY_MAX_MS;
|
||||
static const DWORD SERVER_RETRY_MS;
|
||||
static const DWORD WRITE_TIMEOUT_MS;
|
||||
static const DWORD WAIT_FIRST_OBJECT_VALUE;
|
||||
|
||||
static bool IsDisconnectedError(_In_ DWORD error);
|
||||
PipeIoResult WaitForOverlapped(
|
||||
_In_ HANDLE pipe,
|
||||
_In_ HANDLE ioEvent,
|
||||
_Inout_ OVERLAPPED * overlapped,
|
||||
_Out_ DWORD * transferred,
|
||||
_In_ DWORD timeoutMs = INFINITE);
|
||||
PipeIoResult ReadMessage(
|
||||
_In_ HANDLE pipe,
|
||||
_In_ HANDLE ioEvent,
|
||||
_Out_writes_bytes_(messageSize) void * message,
|
||||
_In_ DWORD messageSize,
|
||||
_Out_ DWORD * bytesRead);
|
||||
PipeIoResult WriteMessage(
|
||||
_In_ HANDLE pipe,
|
||||
_In_reads_bytes_(messageSize) const void * message,
|
||||
_In_ DWORD messageSize);
|
||||
|
||||
static DWORD WINAPI ThreadProc(_In_ void * context);
|
||||
|
||||
void Thread();
|
||||
void RunServer();
|
||||
void RunClient();
|
||||
bool ReadMessages(_In_ HANDLE pipe);
|
||||
HANDLE CreateServerPipe();
|
||||
bool WaitForRetry(_In_ DWORD delayMs);
|
||||
void PublishPipe(_In_ HANDLE pipe);
|
||||
void ClosePipe(_In_ HANDLE pipe);
|
||||
|
||||
std::wstring m_pipeName;
|
||||
size_t m_messageSize = 0;
|
||||
Mode m_mode = Mode::Client;
|
||||
IPipeEndpointHandler * m_handler = nullptr;
|
||||
|
||||
std::atomic<bool> m_running { false };
|
||||
std::atomic<bool> m_connected { false };
|
||||
|
||||
SRWLOCK m_pipeLock = SRWLOCK_INIT;
|
||||
HANDLE m_pipe = INVALID_HANDLE_VALUE;
|
||||
HANDLE m_thread = nullptr;
|
||||
HANDLE m_stopEvent = nullptr;
|
||||
};
|
||||
51
idd/LGCommon/InputPipeProtocol.h
Normal file
51
idd/LGCommon/InputPipeProtocol.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* 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 <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static constexpr wchar_t LG_INPUT_PIPE_NAME[] =
|
||||
L"\\\\.\\pipe\\LookingGlassIDDInput";
|
||||
|
||||
static constexpr uint32_t LG_INPUT_PIPE_MAGIC = 0x5049474c;
|
||||
static constexpr uint16_t LG_INPUT_PIPE_VERSION = 1;
|
||||
static constexpr size_t LG_INPUT_PIPE_MAX_REPORT_SIZE = 64;
|
||||
|
||||
enum LGInputPipeMessageType : uint16_t
|
||||
{
|
||||
LG_INPUT_PIPE_MESSAGE_REPORT = 1,
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct LGInputPipeMessage
|
||||
{
|
||||
uint32_t magic;
|
||||
uint16_t version;
|
||||
uint16_t type;
|
||||
uint32_t payloadSize;
|
||||
uint64_t sequence;
|
||||
uint8_t payload[LG_INPUT_PIPE_MAX_REPORT_SIZE];
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
static_assert(sizeof(LGInputPipeMessage) == 84,
|
||||
"LGInputPipeMessage wire layout changed");
|
||||
83
idd/LGCommon/LGCommon.vcxproj
Normal file
83
idd/LGCommon/LGCommon.vcxproj
Normal file
@@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="12.0"
|
||||
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{ACB90E34-01CA-4B86-813B-3D20904994C6}</ProjectGuid>
|
||||
<RootNamespace>LGCommon</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.26100.0</WindowsTargetPlatformVersion>
|
||||
<UMDF_VERSION_MAJOR>2</UMDF_VERSION_MAJOR>
|
||||
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
|
||||
<Platform Condition="'$(Platform)' == ''">Win32</Platform>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<PlatformToolset>WindowsUserModeDriver10.0</PlatformToolset>
|
||||
<TargetVersion>Windows10</TargetVersion>
|
||||
<DriverTargetPlatform>Universal</DriverTargetPlatform>
|
||||
<DriverType>UMDF</DriverType>
|
||||
<UMDF_VERSION_MINOR>25</UMDF_VERSION_MINOR>
|
||||
<UMDF_MINIMUM_VERSION_REQUIRED>25</UMDF_MINIMUM_VERSION_REQUIRED>
|
||||
<_NT_TARGET_VERSION>0xA000005</_NT_TARGET_VERSION>
|
||||
<Driver_SpectreMitigation>Spectre</Driver_SpectreMitigation>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" Label="Configuration">
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release'" Label="Configuration">
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings" />
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
|
||||
Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
|
||||
Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup>
|
||||
<TargetName>LGCommon</TargetName>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalOptions>/EHsc /D_ATL_NO_WIN_SUPPORT %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CDebug.cpp" />
|
||||
<ClCompile Include="CPipeEndpoint.cpp" />
|
||||
<ClCompile Include="RefreshRate.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CDebug.h" />
|
||||
<ClInclude Include="CPipeEndpoint.h" />
|
||||
<ClInclude Include="CSRWLock.h" />
|
||||
<ClInclude Include="DefaultDisplayModes.h" />
|
||||
<ClInclude Include="InputPipeProtocol.h" />
|
||||
<ClInclude Include="PipeMsg.h" />
|
||||
<ClInclude Include="RefreshRate.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets" />
|
||||
</Project>
|
||||
48
idd/LGCommon/LGCommon.vcxproj.filters
Normal file
48
idd/LGCommon/LGCommon.vcxproj.filters
Normal file
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0"
|
||||
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cxx;cc</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4B04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CDebug.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CPipeEndpoint.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="RefreshRate.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CDebug.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CPipeEndpoint.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CSRWLock.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="DefaultDisplayModes.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="InputPipeProtocol.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="PipeMsg.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="RefreshRate.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LG_PIPE_NAME "\\\\.\\pipe\\LookingGlassIDD"
|
||||
static constexpr wchar_t LG_PIPE_NAME[] = L"\\\\.\\pipe\\LookingGlassIDD";
|
||||
|
||||
struct LGPipeMsg
|
||||
{
|
||||
@@ -68,3 +68,5 @@ struct LGPipeMsg
|
||||
resolutionRejected;
|
||||
};
|
||||
};
|
||||
|
||||
static_assert(sizeof(LGPipeMsg) == 20, "LGPipeMsg wire layout changed");
|
||||
|
||||
@@ -16,14 +16,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LGMP", "..\repos\LGMP\LGMP.
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LGIddHelper", "LGIddHelper\LGIddHelper.vcxproj", "{0045D7AD-3F26-4B87-81CB-78D18839596D}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LGCommon", "LGCommon", "{ACB90E34-01CA-4B86-813B-3D20904994C6}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
LGCommon\CDebug.cpp = LGCommon\CDebug.cpp
|
||||
LGCommon\CDebug.h = LGCommon\CDebug.h
|
||||
LGCommon\CSRWLock.h = LGCommon\CSRWLock.h
|
||||
LGCommon\DefaultDisplayModes.h = LGCommon\DefaultDisplayModes.h
|
||||
LGCommon\PipeMsg.h = LGCommon\PipeMsg.h
|
||||
EndProjectSection
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LGCommon", "LGCommon\LGCommon.vcxproj", "{ACB90E34-01CA-4B86-813B-3D20904994C6}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LGIddInstall", "LGIddInstall\LGIddInstall.vcxproj", "{23EC8370-5F3B-4D18-8C31-E89A154F3666}"
|
||||
EndProject
|
||||
@@ -47,6 +40,14 @@ Global
|
||||
{1CBF3DAA-0726-4F5F-88A2-04D95FB6591A}.Release-LGInput|x64.ActiveCfg = Release-LGInput|x64
|
||||
{1CBF3DAA-0726-4F5F-88A2-04D95FB6591A}.Release-LGInput|x64.Build.0 = Release-LGInput|x64
|
||||
{1CBF3DAA-0726-4F5F-88A2-04D95FB6591A}.Release-LGInput|x64.Deploy.0 = Release-LGInput|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Debug|x64.Build.0 = Debug|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Debug-LGInput|x64.ActiveCfg = Debug|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Debug-LGInput|x64.Build.0 = Debug|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Release|x64.ActiveCfg = Release|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Release|x64.Build.0 = Release|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Release-LGInput|x64.ActiveCfg = Release|x64
|
||||
{ACB90E34-01CA-4B86-813B-3D20904994C6}.Release-LGInput|x64.Build.0 = Release|x64
|
||||
{2477B25B-CB62-4AD9-A260-CE5F00D77EEB}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{2477B25B-CB62-4AD9-A260-CE5F00D77EEB}.Debug|x64.Build.0 = Debug|x64
|
||||
{2477B25B-CB62-4AD9-A260-CE5F00D77EEB}.Debug-LGInput|x64.ActiveCfg = Debug|x64
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "CDebug.h"
|
||||
#include "platform/CPlatformInfo.h"
|
||||
#include "VersionInfo.h"
|
||||
#include "ipc/CInputPipeServer.h"
|
||||
#include "ipc/CPipeServer.h"
|
||||
|
||||
NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)
|
||||
@@ -45,6 +46,13 @@ NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING Reg
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!g_inputPipeServer.Init())
|
||||
{
|
||||
status = STATUS_UNSUCCESSFUL;
|
||||
DEBUG_ERROR("Failed to setup the LGInput IPC pipe");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
WDF_DRIVER_CONFIG config;
|
||||
WDF_OBJECT_ATTRIBUTES attributes;
|
||||
|
||||
@@ -66,6 +74,8 @@ NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING Reg
|
||||
return status;
|
||||
|
||||
fail:
|
||||
g_inputPipeServer.DeInit();
|
||||
g_pipe.DeInit();
|
||||
#if UMDF_VERSION_MAJOR == 2 && UMDF_VERSION_MINOR == 0
|
||||
WPP_CLEANUP();
|
||||
#else
|
||||
@@ -91,6 +101,7 @@ VOID LGIddEvtDriverContextCleanup(_In_ WDFOBJECT DriverObject)
|
||||
|
||||
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry");
|
||||
|
||||
g_inputPipeServer.DeInit();
|
||||
g_pipe.DeInit();
|
||||
|
||||
#if UMDF_VERSION_MAJOR == 2 && UMDF_VERSION_MINOR == 0
|
||||
|
||||
@@ -32,9 +32,9 @@
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="$(SolutionDir)LGCommon\*.cpp" />
|
||||
<ClCompile Include="Device.cpp" />
|
||||
<ClCompile Include="Driver.cpp" />
|
||||
<ClCompile Include="ipc\CInputPipeServer.cpp" />
|
||||
<ClCompile Include="ipc\CPipeServer.cpp" />
|
||||
<ClCompile Include="display\CDisplayConfiguration.cpp" />
|
||||
<ClCompile Include="display\CEdid.cpp" />
|
||||
@@ -70,11 +70,11 @@
|
||||
<ClCompile Include="platform\CPlatformInfo.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="$(SolutionDir)LGCommon\*.h" />
|
||||
<ClInclude Include="Device.h" />
|
||||
<ClInclude Include="Driver.h" />
|
||||
<ClInclude Include="Public.h" />
|
||||
<ClInclude Include="Trace.h" />
|
||||
<ClInclude Include="ipc\CInputPipeServer.h" />
|
||||
<ClInclude Include="ipc\CPipeServer.h" />
|
||||
<ClInclude Include="display\CDisplayConfiguration.h" />
|
||||
<ClInclude Include="display\CEdid.h" />
|
||||
@@ -303,6 +303,12 @@
|
||||
<FilesToPackage Include="$(ProjectDir)..\LGIddHelper\VERSION" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LGCommon\LGCommon.vcxproj">
|
||||
<Project>{acb90e34-01ca-4b86-813b-3d20904994c6}</Project>
|
||||
<SetConfiguration Condition="'$(CurrentSolutionConfigurationContents)'==''">Configuration=$(LGBaseConfiguration)</SetConfiguration>
|
||||
<SetPlatform Condition="'$(CurrentSolutionConfigurationContents)'==''">Platform=$(Platform)</SetPlatform>
|
||||
<AdditionalProperties Condition="'$(CurrentSolutionConfigurationContents)'==''">SolutionDir=$(LGDriverSolutionDir)</AdditionalProperties>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\LGInput\LGInput.vcxproj">
|
||||
<Project>{2477b25b-cb62-4ad9-a260-ce5f00d77eeb}</Project>
|
||||
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
|
||||
|
||||
@@ -35,9 +35,6 @@
|
||||
<Filter Include="Platform">
|
||||
<UniqueIdentifier>{1C677205-7587-4037-9E36-92DFE600B985}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Common">
|
||||
<UniqueIdentifier>{938E49D6-F954-4EBE-80AB-E0F67E677F27}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Utilities">
|
||||
<UniqueIdentifier>{98768720-86D6-4A83-9EBD-2C8BFB51D793}</UniqueIdentifier>
|
||||
</Filter>
|
||||
@@ -52,9 +49,6 @@
|
||||
</Inf>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="$(SolutionDir)LGCommon\*.h">
|
||||
<Filter>Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Device.h">
|
||||
<Filter>Driver</Filter>
|
||||
</ClInclude>
|
||||
@@ -70,6 +64,9 @@
|
||||
<ClInclude Include="ipc\CPipeServer.h">
|
||||
<Filter>IPC</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ipc\CInputPipeServer.h">
|
||||
<Filter>IPC</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="display\CDisplayConfiguration.h">
|
||||
<Filter>Display</Filter>
|
||||
</ClInclude>
|
||||
@@ -195,9 +192,6 @@
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="$(SolutionDir)LGCommon\*.cpp">
|
||||
<Filter>Common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Driver.cpp">
|
||||
<Filter>Driver</Filter>
|
||||
</ClCompile>
|
||||
@@ -207,6 +201,9 @@
|
||||
<ClCompile Include="ipc\CPipeServer.cpp">
|
||||
<Filter>IPC</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ipc\CInputPipeServer.cpp">
|
||||
<Filter>IPC</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="display\CDisplayConfiguration.cpp">
|
||||
<Filter>Display</Filter>
|
||||
</ClCompile>
|
||||
|
||||
75
idd/LGIdd/ipc/CInputPipeServer.cpp
Normal file
75
idd/LGIdd/ipc/CInputPipeServer.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* 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 "ipc/CInputPipeServer.h"
|
||||
|
||||
#include "CDebug.h"
|
||||
#include "InputPipeProtocol.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
CInputPipeServer g_inputPipeServer;
|
||||
|
||||
bool CInputPipeServer::Init()
|
||||
{
|
||||
AcquireSRWLockExclusive(&m_sendLock);
|
||||
m_sequence = 0;
|
||||
ReleaseSRWLockExclusive(&m_sendLock);
|
||||
|
||||
m_endpoint.SetHandler(this);
|
||||
return m_endpoint.Start(
|
||||
LG_INPUT_PIPE_NAME,
|
||||
CPipeEndpoint::Mode::Server,
|
||||
sizeof(LGInputPipeMessage));
|
||||
}
|
||||
|
||||
void CInputPipeServer::DeInit()
|
||||
{
|
||||
m_endpoint.Stop();
|
||||
}
|
||||
|
||||
bool CInputPipeServer::SendReport(const void * report, size_t size)
|
||||
{
|
||||
if (!report || !size || size > LG_INPUT_PIPE_MAX_REPORT_SIZE)
|
||||
return false;
|
||||
|
||||
LGInputPipeMessage message = {};
|
||||
message.magic = LG_INPUT_PIPE_MAGIC;
|
||||
message.version = LG_INPUT_PIPE_VERSION;
|
||||
message.type = LG_INPUT_PIPE_MESSAGE_REPORT;
|
||||
message.payloadSize = static_cast<uint32_t>(size);
|
||||
memcpy(message.payload, report, size);
|
||||
|
||||
AcquireSRWLockExclusive(&m_sendLock);
|
||||
message.sequence = ++m_sequence;
|
||||
const bool sent = m_endpoint.Send(&message, sizeof(message));
|
||||
ReleaseSRWLockExclusive(&m_sendLock);
|
||||
return sent;
|
||||
}
|
||||
|
||||
bool CInputPipeServer::OnPipeMessage(
|
||||
const void * message,
|
||||
size_t size)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(message);
|
||||
UNREFERENCED_PARAMETER(size);
|
||||
DEBUG_WARN("LGInput sent an unexpected message");
|
||||
return false;
|
||||
}
|
||||
49
idd/LGIdd/ipc/CInputPipeServer.h
Normal file
49
idd/LGIdd/ipc/CInputPipeServer.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* 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 CInputPipeServer : private IPipeEndpointHandler
|
||||
{
|
||||
public:
|
||||
~CInputPipeServer() { DeInit(); }
|
||||
|
||||
bool Init();
|
||||
void DeInit();
|
||||
|
||||
bool SendReport(
|
||||
_In_reads_bytes_(size) const void * report,
|
||||
_In_ size_t size);
|
||||
bool IsConnected() const { return m_endpoint.IsConnected(); }
|
||||
|
||||
private:
|
||||
bool OnPipeMessage(const void * message, size_t size) override;
|
||||
|
||||
CPipeEndpoint m_endpoint;
|
||||
SRWLOCK m_sendLock = SRWLOCK_INIT;
|
||||
uint64_t m_sequence = 0;
|
||||
};
|
||||
|
||||
extern CInputPipeServer g_inputPipeServer;
|
||||
@@ -26,234 +26,73 @@ CPipeServer g_pipe;
|
||||
|
||||
bool CPipeServer::Init()
|
||||
{
|
||||
_DeInit();
|
||||
|
||||
m_pipe.Attach(CreateNamedPipeA(
|
||||
m_endpoint.SetHandler(this);
|
||||
return m_endpoint.Start(
|
||||
LG_PIPE_NAME,
|
||||
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
||||
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
|
||||
1,
|
||||
1024,
|
||||
1024,
|
||||
0,
|
||||
NULL));
|
||||
|
||||
if (!m_pipe.IsValid())
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create the named pipe");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_signal.Attach(CreateEvent(NULL, TRUE, FALSE, NULL));
|
||||
if (!m_signal.IsValid())
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create pipe signal event");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_running = true;
|
||||
m_thread.Attach(CreateThread(
|
||||
NULL,
|
||||
0,
|
||||
_pipeThread,
|
||||
(LPVOID)this,
|
||||
0,
|
||||
NULL));
|
||||
|
||||
if (!m_thread.IsValid())
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create the pipe thread");
|
||||
return false;
|
||||
}
|
||||
|
||||
DEBUG_TRACE("Pipe Initialized");
|
||||
return true;
|
||||
}
|
||||
|
||||
void CPipeServer::_DeInit()
|
||||
{
|
||||
m_running = false;
|
||||
m_connected = false;
|
||||
if (m_signal.IsValid())
|
||||
SetEvent(m_signal.Get());
|
||||
|
||||
if (m_thread.IsValid())
|
||||
{
|
||||
WaitForSingleObject(m_thread.Get(), INFINITE);
|
||||
m_thread.Close();
|
||||
}
|
||||
|
||||
if (m_pipe.IsValid())
|
||||
{
|
||||
FlushFileBuffers(m_pipe.Get());
|
||||
m_pipe.Close();
|
||||
}
|
||||
|
||||
m_signal.Close();
|
||||
CPipeEndpoint::Mode::Server,
|
||||
sizeof(LGPipeMsg));
|
||||
}
|
||||
|
||||
void CPipeServer::DeInit()
|
||||
{
|
||||
DEBUG_TRACE("Pipe Stopping");
|
||||
_DeInit();
|
||||
DEBUG_TRACE("Pipe Stopped");
|
||||
{
|
||||
m_endpoint.Stop();
|
||||
}
|
||||
|
||||
void CPipeServer::Thread()
|
||||
void CPipeServer::OnPipeConnected()
|
||||
{
|
||||
DEBUG_TRACE("Pipe thread started");
|
||||
AcquireSRWLockExclusive(&m_queueLock);
|
||||
std::vector<LGPipeMsg> queued;
|
||||
queued.swap(m_queue);
|
||||
|
||||
HandleT<EventTraits> ioEvent(CreateEvent(NULL, TRUE, FALSE, NULL));
|
||||
if (!ioEvent.IsValid())
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Can't create event for overlapped I/O!");
|
||||
WaitForSingleObject(m_signal.Get(), 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
while(m_running)
|
||||
{
|
||||
m_connected = false;
|
||||
|
||||
OVERLAPPED overlapped = { 0 };
|
||||
overlapped.hEvent = ioEvent.Get();
|
||||
|
||||
if (!ConnectNamedPipe(m_pipe.Get(), &overlapped))
|
||||
for (size_t i = 0; i < queued.size(); ++i)
|
||||
if (!m_endpoint.Send(&queued[i], sizeof(queued[i])))
|
||||
{
|
||||
DWORD dwError = GetLastError();
|
||||
switch (dwError) {
|
||||
case ERROR_PIPE_CONNECTED:
|
||||
break;
|
||||
case ERROR_IO_PENDING:
|
||||
{
|
||||
HANDLE hWait[] = { ioEvent.Get(), m_signal.Get() };
|
||||
switch (WaitForMultipleObjects(2, hWait, FALSE, INFINITE))
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
break;
|
||||
case WAIT_OBJECT_0 + 1:
|
||||
DEBUG_INFO("Connect interrupted by signal");
|
||||
CancelIo(m_pipe.Get());
|
||||
WaitForSingleObject(ioEvent.Get(), INFINITE);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
DEBUG_ERROR_HR(dwError, "Error connecting to the named pipe");
|
||||
goto end;
|
||||
}
|
||||
for (; i < queued.size(); ++i)
|
||||
QueueMsgLocked(queued[i]);
|
||||
break;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_queueLock);
|
||||
}
|
||||
|
||||
bool CPipeServer::OnPipeMessage(const void * message, size_t size)
|
||||
{
|
||||
if (size != sizeof(LGPipeMsg))
|
||||
return false;
|
||||
|
||||
const LGPipeMsg & msg = *static_cast<const LGPipeMsg *>(message);
|
||||
if (msg.size != sizeof(msg))
|
||||
return false;
|
||||
|
||||
switch (msg.type)
|
||||
{
|
||||
case LGPipeMsg::RELOADSETTINGS:
|
||||
HandleReloadSettings();
|
||||
return true;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unknown message type %d", msg.type);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void CPipeServer::QueueMsgLocked(const LGPipeMsg & msg)
|
||||
{
|
||||
for (LGPipeMsg & queued : m_queue)
|
||||
if (queued.type == msg.type)
|
||||
{
|
||||
queued = msg;
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_TRACE("Client connected");
|
||||
|
||||
m_connected = true;
|
||||
|
||||
for (const auto& msg : m_queue)
|
||||
WriteMsg(msg);
|
||||
m_queue.clear();
|
||||
|
||||
while (m_running && m_connected)
|
||||
{
|
||||
LGPipeMsg msg;
|
||||
|
||||
if (!ReadFile(m_pipe.Get(), &msg, sizeof(msg), NULL, &overlapped))
|
||||
{
|
||||
DWORD dwError = GetLastError();
|
||||
if (dwError != ERROR_IO_PENDING)
|
||||
{
|
||||
DEBUG_ERROR_HR(dwError, "ReadFile Failed");
|
||||
break;
|
||||
}
|
||||
|
||||
HANDLE hWait[] = { ioEvent.Get(), m_signal.Get() };
|
||||
switch (WaitForMultipleObjects(2, hWait, FALSE, INFINITE))
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
break;
|
||||
case WAIT_OBJECT_0 + 1:
|
||||
DEBUG_INFO("I/O interrupted by signal");
|
||||
CancelIo(m_pipe.Get());
|
||||
WaitForSingleObject(ioEvent.Get(), INFINITE);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
DWORD bytesRead;
|
||||
GetOverlappedResult(m_pipe.Get(), &overlapped, &bytesRead, TRUE);
|
||||
|
||||
if (bytesRead != sizeof(msg))
|
||||
{
|
||||
DEBUG_ERROR("Corrupted data, expected %lld bytes, read %lld bytes", sizeof msg, bytesRead);
|
||||
break;
|
||||
}
|
||||
|
||||
if (msg.size != sizeof(msg))
|
||||
{
|
||||
DEBUG_ERROR("Corrupted data, expected %lld bytes, actual message size: %lld bytes", sizeof msg, msg.size);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (msg.type)
|
||||
{
|
||||
case LGPipeMsg::RELOADSETTINGS:
|
||||
HandleReloadSettings();
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unknown message type %d", msg.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_TRACE("Client disconnected");
|
||||
DisconnectNamedPipe(m_pipe.Get());
|
||||
|
||||
if (m_running)
|
||||
ResetEvent(m_signal.Get());
|
||||
}
|
||||
|
||||
end:
|
||||
m_running = false;
|
||||
m_connected = false;
|
||||
DEBUG_TRACE("Pipe thread shutdown");
|
||||
m_queue.push_back(msg);
|
||||
}
|
||||
|
||||
void CPipeServer::WriteMsg(const LGPipeMsg & msg)
|
||||
{
|
||||
if (!m_connected)
|
||||
{
|
||||
// Not connected yet: keep only the latest message of each type. These are
|
||||
// all latest-state-wins messages, so a burst (e.g. display mode changes
|
||||
// while resizing) must collapse to the final state rather than replay every
|
||||
// intermediate value when the helper reconnects.
|
||||
for (auto & queued : m_queue)
|
||||
if (queued.type == msg.type)
|
||||
{
|
||||
queued = msg;
|
||||
return;
|
||||
}
|
||||
m_queue.push_back(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
DWORD written;
|
||||
if (!WriteFile(m_pipe.Get(), &msg, sizeof(msg), &written, NULL))
|
||||
{
|
||||
DWORD err = GetLastError();
|
||||
if (err == ERROR_BROKEN_PIPE || err == ERROR_NO_DATA)
|
||||
{
|
||||
DEBUG_WARN_HR(err, "Client disconnected, failed to write");
|
||||
m_connected = false;
|
||||
SetEvent(m_signal.Get());
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_WARN_HR(err, "WriteFile failed on the pipe");
|
||||
return;
|
||||
}
|
||||
|
||||
FlushFileBuffers(m_pipe.Get());
|
||||
AcquireSRWLockExclusive(&m_queueLock);
|
||||
if (!m_endpoint.Send(&msg, sizeof(msg)))
|
||||
QueueMsgLocked(msg);
|
||||
ReleaseSRWLockExclusive(&m_queueLock);
|
||||
}
|
||||
|
||||
void CPipeServer::HandleReloadSettings()
|
||||
@@ -276,7 +115,7 @@ void CPipeServer::SetDeviceContext(CDeviceContext * context)
|
||||
void CPipeServer::SetCursorPos(uint32_t x, uint32_t y)
|
||||
{
|
||||
// do not send cursor messages if we are not connected or they will end up queued
|
||||
if (!m_connected)
|
||||
if (!m_endpoint.IsConnected())
|
||||
return;
|
||||
|
||||
LGPipeMsg msg = {};
|
||||
@@ -284,7 +123,9 @@ void CPipeServer::SetCursorPos(uint32_t x, uint32_t y)
|
||||
msg.type = LGPipeMsg::SETCURSORPOS;
|
||||
msg.curorPos.x = x;
|
||||
msg.curorPos.y = y;
|
||||
WriteMsg(msg);
|
||||
// Cursor position is transient. If the connection is lost during this
|
||||
// write, drop it instead of replaying stale coordinates after reconnect.
|
||||
m_endpoint.Send(&msg, sizeof(msg));
|
||||
}
|
||||
|
||||
void CPipeServer::SetDisplayMode(
|
||||
|
||||
@@ -23,40 +23,31 @@
|
||||
#include <windows.h>
|
||||
#include <wdf.h>
|
||||
#include <stdint.h>
|
||||
#include <wrl.h>
|
||||
#include <vector>
|
||||
|
||||
#include "CPipeEndpoint.h"
|
||||
#include "PipeMsg.h"
|
||||
|
||||
using namespace Microsoft::WRL;
|
||||
using namespace Microsoft::WRL::Wrappers;
|
||||
using namespace Microsoft::WRL::Wrappers::HandleTraits;
|
||||
|
||||
class CDeviceContext;
|
||||
|
||||
class CPipeServer
|
||||
class CPipeServer : private IPipeEndpointHandler
|
||||
{
|
||||
private:
|
||||
HandleT<HANDLETraits> m_pipe;
|
||||
HandleT<HANDLENullTraits> m_thread;
|
||||
HandleT<EventTraits> m_signal;
|
||||
std::vector<LGPipeMsg> m_queue;
|
||||
|
||||
bool m_running = false;
|
||||
bool m_connected = false;
|
||||
CPipeEndpoint m_endpoint;
|
||||
SRWLOCK m_queueLock = SRWLOCK_INIT;
|
||||
std::vector<LGPipeMsg> m_queue;
|
||||
|
||||
SRWLOCK m_deviceContextLock = SRWLOCK_INIT;
|
||||
CDeviceContext * m_deviceContext = nullptr;
|
||||
|
||||
void _DeInit();
|
||||
|
||||
static DWORD WINAPI _pipeThread(LPVOID lpParam) { ((CPipeServer*)lpParam)->Thread(); return 0; }
|
||||
void Thread();
|
||||
|
||||
void WriteMsg(const LGPipeMsg & msg);
|
||||
void QueueMsgLocked(const LGPipeMsg & msg);
|
||||
|
||||
void HandleReloadSettings();
|
||||
|
||||
void OnPipeConnected() override;
|
||||
bool OnPipeMessage(const void * message, size_t size) override;
|
||||
|
||||
public:
|
||||
~CPipeServer() { DeInit(); }
|
||||
|
||||
|
||||
@@ -114,51 +114,16 @@ bool CPipeClient::Init()
|
||||
return false;
|
||||
}
|
||||
|
||||
m_signal.Attach(CreateEvent(NULL, TRUE, FALSE, NULL));
|
||||
if (!m_signal.IsValid())
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create pipe signal event");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_running = true;
|
||||
m_thread.Attach(CreateThread(
|
||||
NULL,
|
||||
0,
|
||||
_pipeThread,
|
||||
(LPVOID)this,
|
||||
0,
|
||||
NULL));
|
||||
|
||||
if (!m_thread.IsValid())
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create the pipe thread");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
m_endpoint.SetHandler(this);
|
||||
return m_endpoint.Start(
|
||||
LG_PIPE_NAME,
|
||||
CPipeEndpoint::Mode::Client,
|
||||
sizeof(LGPipeMsg));
|
||||
}
|
||||
|
||||
void CPipeClient::DeInit()
|
||||
{
|
||||
m_connected = false;
|
||||
m_running = false;
|
||||
if (m_signal.IsValid())
|
||||
SetEvent(m_signal.Get());
|
||||
|
||||
if (m_thread.IsValid())
|
||||
{
|
||||
WaitForSingleObject(m_thread.Get(), INFINITE);
|
||||
m_thread.Close();
|
||||
}
|
||||
|
||||
if (m_pipe.IsValid())
|
||||
{
|
||||
FlushFileBuffers(m_pipe.Get());
|
||||
m_pipe.Close();
|
||||
}
|
||||
|
||||
m_signal.Close();
|
||||
m_endpoint.Stop();
|
||||
}
|
||||
|
||||
bool CPipeClient::IsLGIddDeviceAttached()
|
||||
@@ -228,28 +193,12 @@ void CPipeClient::SetActiveDesktop()
|
||||
|
||||
void CPipeClient::WriteMsg(const LGPipeMsg& msg)
|
||||
{
|
||||
DWORD written;
|
||||
if (!WriteFile(m_pipe.Get(), &msg, sizeof(msg), &written, NULL))
|
||||
{
|
||||
DWORD err = GetLastError();
|
||||
if (err == ERROR_BROKEN_PIPE)
|
||||
{
|
||||
DEBUG_WARN_HR(err, "Client disconnected, failed to write");
|
||||
m_connected = false;
|
||||
SetEvent(m_signal.Get());
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_WARN_HR(err, "WriteFile failed on the pipe");
|
||||
return;
|
||||
}
|
||||
|
||||
FlushFileBuffers(m_pipe.Get());
|
||||
m_endpoint.Send(&msg, sizeof(msg));
|
||||
}
|
||||
|
||||
void CPipeClient::ReloadSettings()
|
||||
{
|
||||
if (!m_connected)
|
||||
if (!m_endpoint.IsConnected())
|
||||
return;
|
||||
|
||||
LGPipeMsg msg = {};
|
||||
@@ -258,6 +207,14 @@ void CPipeClient::ReloadSettings()
|
||||
WriteMsg(msg);
|
||||
}
|
||||
|
||||
bool CPipeClient::ShouldReconnect()
|
||||
{
|
||||
const bool attached = IsLGIddDeviceAttached();
|
||||
if (!attached)
|
||||
DEBUG_INFO("Looking Glass Indirect Display Device was removed");
|
||||
return attached;
|
||||
}
|
||||
|
||||
bool CPipeClient::EnsureOnlyDisplayLocked()
|
||||
{
|
||||
std::vector<DisplayState> displays;
|
||||
@@ -372,124 +329,37 @@ bool CPipeClient::EnsureOnlyDisplay()
|
||||
return result;
|
||||
}
|
||||
|
||||
void CPipeClient::Thread()
|
||||
bool CPipeClient::OnPipeMessage(const void * message, size_t size)
|
||||
{
|
||||
DEBUG_INFO("Pipe thread started");
|
||||
if (size != sizeof(LGPipeMsg))
|
||||
return false;
|
||||
|
||||
HandleT<EventTraits> ioEvent(CreateEvent(NULL, TRUE, FALSE, NULL));
|
||||
if (!ioEvent.IsValid())
|
||||
const LGPipeMsg & msg = *static_cast<const LGPipeMsg *>(message);
|
||||
if (msg.size != sizeof(msg))
|
||||
return false;
|
||||
|
||||
switch (msg.type)
|
||||
{
|
||||
DEBUG_ERROR("Can't create event for overlapped I/O!");
|
||||
WaitForSingleObject(m_signal.Get(), 5000);
|
||||
return;
|
||||
case LGPipeMsg::SETCURSORPOS:
|
||||
HandleSetCursorPos(msg);
|
||||
return true;
|
||||
|
||||
case LGPipeMsg::SETDISPLAYMODE:
|
||||
HandleSetDisplayMode(msg);
|
||||
return true;
|
||||
|
||||
case LGPipeMsg::GPUSTATUS:
|
||||
HandleGPUStatus(msg);
|
||||
return true;
|
||||
|
||||
case LGPipeMsg::RESOLUTIONREJECTED:
|
||||
HandleResolutionRejected(msg);
|
||||
return true;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unknown message type %d", msg.type);
|
||||
return true;
|
||||
}
|
||||
|
||||
while (m_running)
|
||||
{
|
||||
if (!IsLGIddDeviceAttached())
|
||||
{
|
||||
m_running = false;
|
||||
DEBUG_ERROR("Device is no longer available, shutting down");
|
||||
break;
|
||||
}
|
||||
|
||||
m_pipe.Attach(CreateFile(
|
||||
TEXT(LG_PIPE_NAME),
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED,
|
||||
NULL
|
||||
));
|
||||
|
||||
if (!m_pipe.IsValid())
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to open the named pipe");
|
||||
WaitForSingleObject(m_signal.Get(), 5000);
|
||||
continue;
|
||||
}
|
||||
|
||||
m_connected = true;
|
||||
DEBUG_INFO("Pipe connected");
|
||||
|
||||
while (m_running && m_connected)
|
||||
{
|
||||
LGPipeMsg msg;
|
||||
|
||||
OVERLAPPED overlapped = { 0 };
|
||||
overlapped.hEvent = ioEvent.Get();
|
||||
|
||||
if (!ReadFile(m_pipe.Get(), &msg, sizeof(msg), NULL, &overlapped))
|
||||
{
|
||||
DWORD dwError = GetLastError();
|
||||
if (dwError != ERROR_IO_PENDING)
|
||||
{
|
||||
DEBUG_ERROR_HR(dwError, "ReadFile Failed");
|
||||
break;
|
||||
}
|
||||
|
||||
HANDLE hWait[] = { ioEvent.Get(), m_signal.Get() };
|
||||
switch (WaitForMultipleObjects(2, hWait, FALSE, INFINITE))
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
break;
|
||||
case WAIT_OBJECT_0 + 1:
|
||||
DEBUG_INFO("I/O interrupted by signal");
|
||||
CancelIo(m_pipe.Get());
|
||||
WaitForSingleObject(ioEvent.Get(), INFINITE);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
DWORD bytesRead;
|
||||
GetOverlappedResult(m_pipe.Get(), &overlapped, &bytesRead, TRUE);
|
||||
|
||||
if (bytesRead != sizeof(msg))
|
||||
{
|
||||
DEBUG_ERROR("Corrupted data, expected %lld bytes, read %lld bytes", sizeof msg, bytesRead);
|
||||
break;
|
||||
}
|
||||
|
||||
if (msg.size != sizeof(msg))
|
||||
{
|
||||
DEBUG_ERROR("Corrupted data, expected %lld bytes, actual message size: %lld bytes", sizeof msg, msg.size);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (msg.type)
|
||||
{
|
||||
case LGPipeMsg::SETCURSORPOS:
|
||||
HandleSetCursorPos(msg);
|
||||
break;
|
||||
|
||||
case LGPipeMsg::SETDISPLAYMODE:
|
||||
HandleSetDisplayMode(msg);
|
||||
break;
|
||||
|
||||
case LGPipeMsg::GPUSTATUS:
|
||||
HandleGPUStatus(msg);
|
||||
break;
|
||||
|
||||
case LGPipeMsg::RESOLUTIONREJECTED:
|
||||
HandleResolutionRejected(msg);
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unknown message type %d", msg.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_pipe.Close();
|
||||
m_connected = false;
|
||||
DEBUG_INFO("Pipe closed");
|
||||
|
||||
if (m_running)
|
||||
ResetEvent(m_signal.Get());
|
||||
}
|
||||
|
||||
DEBUG_INFO("Pipe thread shutdown");
|
||||
}
|
||||
|
||||
void CPipeClient::HandleSetCursorPos(const LGPipeMsg& msg)
|
||||
|
||||
@@ -22,28 +22,16 @@
|
||||
|
||||
#include <windows.h>
|
||||
#include <stdint.h>
|
||||
#include <wrl.h>
|
||||
|
||||
#include "CPipeEndpoint.h"
|
||||
#include "PipeMsg.h"
|
||||
|
||||
using namespace Microsoft::WRL;
|
||||
using namespace Microsoft::WRL::Wrappers;
|
||||
using namespace Microsoft::WRL::Wrappers::HandleTraits;
|
||||
|
||||
class CPipeClient
|
||||
class CPipeClient : private IPipeEndpointHandler
|
||||
{
|
||||
private:
|
||||
HandleT<HANDLETraits> m_pipe;
|
||||
HandleT<HANDLENullTraits> m_thread;
|
||||
HandleT<EventTraits> m_signal;
|
||||
|
||||
bool m_running = false;
|
||||
bool m_connected = false;
|
||||
CPipeEndpoint m_endpoint;
|
||||
SRWLOCK m_displayLock = SRWLOCK_INIT;
|
||||
|
||||
static DWORD WINAPI _pipeThread(LPVOID lpParam) { ((CPipeClient*)lpParam)->Thread(); return 0; }
|
||||
void Thread();
|
||||
|
||||
void WriteMsg(const LGPipeMsg& msg);
|
||||
|
||||
void SetActiveDesktop();
|
||||
@@ -55,6 +43,9 @@ private:
|
||||
void HandleGPUStatus(const LGPipeMsg& msg);
|
||||
void HandleResolutionRejected(const LGPipeMsg& msg);
|
||||
|
||||
bool ShouldReconnect() override;
|
||||
bool OnPipeMessage(const void * message, size_t size) override;
|
||||
|
||||
public:
|
||||
~CPipeClient() { DeInit(); }
|
||||
|
||||
@@ -62,7 +53,7 @@ public:
|
||||
|
||||
bool Init();
|
||||
void DeInit();
|
||||
bool IsRunning() { return m_running; }
|
||||
bool IsRunning() { return m_endpoint.IsRunning(); }
|
||||
|
||||
void ReloadSettings();
|
||||
bool EnsureOnlyDisplay();
|
||||
|
||||
@@ -75,7 +75,7 @@
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>WIN32;_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)LGCommon;$(SolutionDir)..\common\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
@@ -127,12 +127,11 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>_CONSOLE;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<LanguageStandard_C>Default</LanguageStandard_C>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)LGCommon;$(SolutionDir)..\common\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@@ -161,7 +160,6 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<LanguageStandard_C>Default</LanguageStandard_C>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)LGCommon;$(SolutionDir)..\common\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
@@ -181,8 +179,16 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd
|
||||
<InputResourceManifests>$(ProjectDir)HighDPI.manifest</InputResourceManifests>
|
||||
</Manifest>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<TreatWChar_tAsBuiltInType>false</TreatWChar_tAsBuiltInType>
|
||||
<RuntimeTypeInfo>false</RuntimeTypeInfo>
|
||||
<CallingConvention>StdCall</CallingConvention>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="$(SolutionDir)LGCommon\*.cpp" />
|
||||
<ClCompile Include="CButton.cpp" />
|
||||
<ClCompile Include="CCheckbox.cpp" />
|
||||
<ClCompile Include="CConfigWindow.cpp" />
|
||||
@@ -199,7 +205,6 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd
|
||||
<ClCompile Include="UIHelpers.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CLInclude Include="$(SolutionDir)LGCommon\*.h" />
|
||||
<ClInclude Include="CButton.h" />
|
||||
<ClInclude Include="CCheckbox.h" />
|
||||
<ClInclude Include="CConfigWindow.h" />
|
||||
@@ -224,6 +229,11 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd
|
||||
<ItemGroup>
|
||||
<Manifest Include="HighDPI.manifest" />
|
||||
</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" />
|
||||
<Target Name="GenerateVersionInfo" BeforeTargets="ClCompile">
|
||||
@@ -251,4 +261,4 @@ copy /Y "$(ProjectDir)VERSION" "$(SolutionDir)$(Platform)\$(Configuration)\LGIdd
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\MSBuilder.Git.0.3.0\build\MSBuilder.Git.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuilder.Git.0.3.0\build\MSBuilder.Git.props'))" />
|
||||
</Target>
|
||||
</Project>
|
||||
</Project>
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="$(SolutionDir)LGCommon\*.cpp" />
|
||||
<ClCompile Include="CPipeClient.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
@@ -60,7 +59,6 @@
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CLInclude Include="$(SolutionDir)LGCommon\*.h" />
|
||||
<ClInclude Include="CPipeClient.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
@@ -115,4 +113,4 @@
|
||||
<ItemGroup>
|
||||
<Manifest Include="HighDPI.manifest" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
||||
@@ -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