[common] clipboard: define streamed clipboard ABIs

This commit is contained in:
Geoffrey McRae
2026-08-14 04:05:15 +10:00
parent 1e4f274553
commit 4602f20e62
9 changed files with 453 additions and 2 deletions

View File

@@ -0,0 +1,116 @@
/**
* 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 "CClipboardRing.h"
#include "Atomic.h"
#include <string.h>
void CClipboardRing::Initialize(
ClipboardMapping& mapping, uint64_t epoch)
{
memset(&mapping, 0, sizeof(mapping));
mapping.magic = LG_CLIPBOARD_MAPPING_MAGIC;
mapping.version = LG_CLIPBOARD_MAPPING_VERSION;
mapping.size = sizeof(mapping);
mapping.slotBytes = KVMFR_CLIPBOARD_DATA_BYTES;
mapping.slotCount = KVMFR_CLIPBOARD_SLOT_COUNT;
mapping.epoch = epoch;
}
bool CClipboardRing::Valid(
const ClipboardMapping& mapping, uint64_t epoch)
{
if (mapping.magic != LG_CLIPBOARD_MAPPING_MAGIC ||
mapping.version != LG_CLIPBOARD_MAPPING_VERSION ||
mapping.size != sizeof(mapping) ||
mapping.slotBytes != KVMFR_CLIPBOARD_DATA_BYTES ||
mapping.slotCount != KVMFR_CLIPBOARD_SLOT_COUNT ||
!mapping.epoch || mapping.epoch != epoch)
return false;
ClipboardRing& helper =
const_cast<ClipboardRing&>(mapping.helperToIdd);
ClipboardRing& idd =
const_cast<ClipboardRing&>(mapping.iddToHelper);
const uint32_t helperProduced = Atomic::Load(helper.produced);
const uint32_t helperConsumed = Atomic::Load(helper.consumed);
const uint32_t iddProduced = Atomic::Load(idd.produced);
const uint32_t iddConsumed = Atomic::Load(idd.consumed);
return helperProduced - helperConsumed <= KVMFR_CLIPBOARD_SLOT_COUNT &&
iddProduced - iddConsumed <= KVMFR_CLIPBOARD_SLOT_COUNT;
}
ClipboardRingSlot * CClipboardRing::BeginWrite(
ClipboardRing& ring, uint32_t& ticket)
{
const uint32_t produced = Atomic::Load(ring.produced);
const uint32_t consumed = Atomic::Load(ring.consumed);
const uint32_t pending = produced - consumed;
if (pending >= KVMFR_CLIPBOARD_SLOT_COUNT)
return nullptr;
ticket = produced;
return &ring.slots[produced % KVMFR_CLIPBOARD_SLOT_COUNT];
}
bool CClipboardRing::EndWrite(ClipboardRing& ring, uint32_t ticket)
{
if (Atomic::Load(ring.produced) != ticket)
return false;
Atomic::Fence();
Atomic::Store(ring.produced, ticket + 1U);
return true;
}
const ClipboardRingSlot * CClipboardRing::BeginRead(
ClipboardRing& ring, uint32_t& ticket)
{
const uint32_t consumed = Atomic::Load(ring.consumed);
const uint32_t pending = Atomic::Load(ring.produced) - consumed;
if (!pending || pending > KVMFR_CLIPBOARD_SLOT_COUNT)
return nullptr;
Atomic::Fence();
ticket = consumed;
return &ring.slots[consumed % KVMFR_CLIPBOARD_SLOT_COUNT];
}
bool CClipboardRing::EndRead(ClipboardRing& ring, uint32_t ticket)
{
if (Atomic::Load(ring.consumed) != ticket)
return false;
Atomic::Fence();
Atomic::Store(ring.consumed, ticket + 1U);
return true;
}
bool CClipboardRing::Valid(const ClipboardRingSlot& slot)
{
return slot.header.version == KVMFR_CLIPBOARD_VERSION &&
slot.header.type >= KVMFR_CLIPBOARD_MESSAGE_CLAIM &&
slot.header.type <= KVMFR_CLIPBOARD_MESSAGE_GRANT &&
slot.header.length <= KVMFR_CLIPBOARD_DATA_BYTES &&
(slot.header.type == KVMFR_CLIPBOARD_MESSAGE_DATA ||
!slot.header.length);
}

View File

@@ -0,0 +1,40 @@
/**
* 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 "ClipboardRing.h"
class CClipboardRing
{
public:
static void Initialize(ClipboardMapping& mapping, uint64_t epoch);
static bool Valid(const ClipboardMapping& mapping, uint64_t epoch);
static ClipboardRingSlot * BeginWrite(
ClipboardRing& ring, uint32_t& ticket);
static bool EndWrite(ClipboardRing& ring, uint32_t ticket);
static const ClipboardRingSlot * BeginRead(
ClipboardRing& ring, uint32_t& ticket);
static bool EndRead(ClipboardRing& ring, uint32_t ticket);
static bool Valid(const ClipboardRingSlot& slot);
};

View File

@@ -0,0 +1,64 @@
/**
* 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 "common/KVMFRClipboard.h"
#include <stddef.h>
#include <stdint.h>
static constexpr uint32_t LG_CLIPBOARD_MAPPING_MAGIC = 0x4c474342U;
static constexpr uint32_t LG_CLIPBOARD_MAPPING_VERSION = 1U;
struct ClipboardRingSlot
{
KVMFRClipboardSlotHeader header;
uint8_t data[KVMFR_CLIPBOARD_DATA_BYTES];
};
struct alignas(64) ClipboardRing
{
uint32_t produced;
uint32_t consumed;
uint32_t reserved[14];
ClipboardRingSlot slots[KVMFR_CLIPBOARD_SLOT_COUNT];
};
struct alignas(64) ClipboardMapping
{
uint32_t magic;
uint32_t version;
uint32_t size;
uint32_t slotBytes;
uint32_t slotCount;
uint32_t reserved0;
uint64_t epoch;
uint8_t reserved1[32];
ClipboardRing helperToIdd;
ClipboardRing iddToHelper;
};
static_assert(sizeof(KVMFRClipboardSlotHeader) == 64,
"clipboard slot header alignment changed");
static_assert(offsetof(ClipboardMapping, helperToIdd) == 64,
"clipboard mapping header layout changed");
static_assert(offsetof(ClipboardMapping, iddToHelper) % 64 == 0,
"clipboard rings must remain cache-line aligned");

View File

@@ -65,13 +65,16 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CClipboardRing.cpp" />
<ClCompile Include="CDebug.cpp" />
<ClCompile Include="CPipeEndpoint.cpp" />
<ClCompile Include="RefreshRate.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CClipboardRing.h" />
<ClInclude Include="Atomic.h" />
<ClInclude Include="CDebug.h" />
<ClInclude Include="ClipboardRing.h" />
<ClInclude Include="CPipeEndpoint.h" />
<ClInclude Include="CSRWLock.h" />
<ClInclude Include="DefaultDisplayModes.h" />

View File

@@ -12,6 +12,9 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CClipboardRing.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="CDebug.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -26,9 +29,15 @@
<ClInclude Include="Atomic.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CClipboardRing.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CDebug.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ClipboardRing.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CPipeEndpoint.h">
<Filter>Header Files</Filter>
</ClInclude>

View File

@@ -39,7 +39,11 @@ struct LGPipeMsg
RECOVERY_OFF,
RECOVERY_ON,
RECOVERY_FAILED,
RECOVERY_NO_DISPLAY
RECOVERY_NO_DISPLAY,
CLIPBOARD_SETUP,
CLIPBOARD_READY,
CLIPBOARD_KICK,
CLIPBOARD_RESET
}
type;
@@ -85,6 +89,34 @@ struct LGPipeMsg
uint32_t request;
}
recovery;
struct
{
uint64_t handle;
uint32_t bytes;
}
clipboardSetup;
struct
{
uint64_t epoch;
uint32_t status;
}
clipboardReady;
struct
{
uint64_t epoch;
uint32_t rings;
}
clipboardKick;
struct
{
uint64_t epoch;
uint32_t reason;
}
clipboardReset;
};
};
#pragma pack(pop)