mirror of
https://github.com/gnif/LookingGlass.git
synced 2026-08-22 07:01:30 +00:00
[client] spice: move protocol into transport
This commit is contained in:
@@ -1151,17 +1151,19 @@ void app_stopVideo(bool stop)
|
||||
|
||||
bool app_useSpiceDisplay(bool enable)
|
||||
{
|
||||
if (!g_params.useSpice)
|
||||
if (!g_params.useSpice || !g_state.fallbackVideoOps ||
|
||||
g_state.fallbackVideoOps->type != LG_VIDEO_TYPE_SW_SURFACE ||
|
||||
!g_state.fallbackVideoOps->swSurface)
|
||||
return false;
|
||||
|
||||
atomic_store_explicit(&g_state.spiceDisplayRequested, enable,
|
||||
atomic_store_explicit(&g_state.fallbackDisplayRequested, enable,
|
||||
memory_order_release);
|
||||
|
||||
// if spice is not yet ready, flag the state we want for when it is
|
||||
if (!atomic_load_explicit(&g_state.spiceReady, memory_order_acquire))
|
||||
// if the fallback is not yet ready, retain the requested state
|
||||
if (!atomic_load_explicit(&g_state.fallbackReady, memory_order_acquire))
|
||||
return false;
|
||||
|
||||
bool active = atomic_load_explicit(&g_state.spiceDisplayActive,
|
||||
bool active = atomic_load_explicit(&g_state.fallbackDisplayActive,
|
||||
memory_order_acquire);
|
||||
if (active == enable)
|
||||
return active;
|
||||
@@ -1170,65 +1172,40 @@ bool app_useSpiceDisplay(bool enable)
|
||||
if (!enable && !atomic_load_explicit(
|
||||
&g_state.lgHostConnected, memory_order_acquire))
|
||||
{
|
||||
atomic_store_explicit(&g_state.spiceDisplayRequested, active,
|
||||
atomic_store_explicit(&g_state.fallbackDisplayRequested, active,
|
||||
memory_order_release);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool expected = false;
|
||||
if (!atomic_compare_exchange_strong_explicit(
|
||||
&g_state.spiceDisplayTransition, &expected, true,
|
||||
&g_state.fallbackDisplayTransition, &expected, true,
|
||||
memory_order_acquire, memory_order_relaxed))
|
||||
return atomic_load_explicit(&g_state.spiceDisplayActive,
|
||||
return atomic_load_explicit(&g_state.fallbackDisplayActive,
|
||||
memory_order_acquire);
|
||||
|
||||
active = atomic_load_explicit(&g_state.spiceDisplayActive,
|
||||
active = atomic_load_explicit(&g_state.fallbackDisplayActive,
|
||||
memory_order_relaxed);
|
||||
if (active == enable)
|
||||
goto done;
|
||||
|
||||
if (enable)
|
||||
{
|
||||
if (!purespice_hasChannel(PS_CHANNEL_DISPLAY) ||
|
||||
!purespice_hasChannel(PS_CHANNEL_CURSOR))
|
||||
goto fail;
|
||||
if (!g_state.fallbackVideoOps->swSurface->setActive(
|
||||
g_state.fallbackTransport.handle, enable))
|
||||
goto fail;
|
||||
|
||||
if (!purespice_connectChannel(PS_CHANNEL_DISPLAY))
|
||||
goto fail;
|
||||
|
||||
if (!purespice_connectChannel(PS_CHANNEL_CURSOR))
|
||||
{
|
||||
purespice_disconnectChannel(PS_CHANNEL_DISPLAY);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
renderQueue_swSurfaceShow(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!purespice_disconnectChannel(PS_CHANNEL_DISPLAY))
|
||||
goto fail;
|
||||
|
||||
if (!purespice_disconnectChannel(PS_CHANNEL_CURSOR))
|
||||
{
|
||||
purespice_connectChannel(PS_CHANNEL_DISPLAY);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
renderQueue_swSurfaceShow(false);
|
||||
}
|
||||
renderQueue_swSurfaceShow(enable);
|
||||
|
||||
active = enable;
|
||||
atomic_store_explicit(&g_state.spiceDisplayActive, active,
|
||||
atomic_store_explicit(&g_state.fallbackDisplayActive, active,
|
||||
memory_order_release);
|
||||
lgInput_useTransport(!active);
|
||||
overlayStatus_set(LG_USER_STATUS_SPICE, enable);
|
||||
|
||||
done:
|
||||
atomic_store_explicit(&g_state.spiceDisplayTransition, false,
|
||||
atomic_store_explicit(&g_state.fallbackDisplayTransition, false,
|
||||
memory_order_release);
|
||||
|
||||
enable = atomic_load_explicit(&g_state.spiceDisplayRequested,
|
||||
enable = atomic_load_explicit(&g_state.fallbackDisplayRequested,
|
||||
memory_order_acquire);
|
||||
if (enable != active)
|
||||
return app_useSpiceDisplay(enable);
|
||||
@@ -1238,7 +1215,7 @@ done:
|
||||
fail:
|
||||
DEBUG_ERROR("Failed to %s the SPICE display",
|
||||
enable ? "enable" : "disable");
|
||||
atomic_store_explicit(&g_state.spiceDisplayRequested, active,
|
||||
atomic_store_explicit(&g_state.fallbackDisplayRequested, active,
|
||||
memory_order_release);
|
||||
goto done;
|
||||
}
|
||||
|
||||
@@ -1,957 +0,0 @@
|
||||
/**
|
||||
* 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 "audio_spice.h"
|
||||
|
||||
#include "common/debug.h"
|
||||
#include "common/event.h"
|
||||
#include "common/locking.h"
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SPICE_AUDIO_TIMESTAMP_DISCONTINUITY_MS 2000
|
||||
|
||||
typedef struct SpiceAudioStream
|
||||
{
|
||||
bool active;
|
||||
uint32_t generation;
|
||||
LG_AudioFormat format;
|
||||
|
||||
bool volumeValid;
|
||||
uint8_t volumeChannels;
|
||||
uint16_t volume[LG_AUDIO_MAX_CHANNELS];
|
||||
bool muteValid;
|
||||
bool mute;
|
||||
}
|
||||
SpiceAudioStream;
|
||||
|
||||
typedef struct SpiceAudioEventTarget
|
||||
{
|
||||
const LG_AudioEventOps * events;
|
||||
void * opaque;
|
||||
uint32_t generation;
|
||||
}
|
||||
SpiceAudioEventTarget;
|
||||
|
||||
typedef struct SpiceAudioCallbackWaiter
|
||||
{
|
||||
struct SpiceAudioCallbackWaiter * next;
|
||||
LGEvent * event;
|
||||
unsigned int depth;
|
||||
}
|
||||
SpiceAudioCallbackWaiter;
|
||||
|
||||
typedef struct SpiceAudioCallbackWaitQueue
|
||||
{
|
||||
LG_Lock lock;
|
||||
atomic_uint count;
|
||||
SpiceAudioCallbackWaiter * waiters;
|
||||
}
|
||||
SpiceAudioCallbackWaitQueue;
|
||||
|
||||
static struct
|
||||
{
|
||||
LG_RWLock lock;
|
||||
|
||||
bool available;
|
||||
uint32_t statusGeneration;
|
||||
LG_AudioStatusFn statusCallback;
|
||||
void * statusOpaque;
|
||||
atomic_uint statusInFlight;
|
||||
SpiceAudioCallbackWaitQueue statusWait;
|
||||
|
||||
const LG_AudioEventOps * events;
|
||||
void * eventOpaque;
|
||||
uint32_t eventGeneration;
|
||||
atomic_uint inFlight;
|
||||
SpiceAudioCallbackWaitQueue eventWait;
|
||||
|
||||
SpiceAudioStream playback;
|
||||
SpiceAudioStream record;
|
||||
|
||||
bool playbackClockValid;
|
||||
uint32_t playbackMediaTime;
|
||||
int64_t playbackTime;
|
||||
uint64_t playbackPosition;
|
||||
LG_AudioClock playbackClock;
|
||||
}
|
||||
l_spice =
|
||||
{
|
||||
.lock =
|
||||
{
|
||||
.readers = ATOMIC_VAR_INIT(0),
|
||||
.writers = ATOMIC_VAR_INIT(0),
|
||||
.writer = ATOMIC_FLAG_INIT,
|
||||
},
|
||||
.statusInFlight = ATOMIC_VAR_INIT(0),
|
||||
.statusWait =
|
||||
{
|
||||
.lock = ATOMIC_FLAG_INIT,
|
||||
.count = ATOMIC_VAR_INIT(0),
|
||||
},
|
||||
.inFlight = ATOMIC_VAR_INIT(0),
|
||||
.eventWait =
|
||||
{
|
||||
.lock = ATOMIC_FLAG_INIT,
|
||||
.count = ATOMIC_VAR_INIT(0),
|
||||
},
|
||||
};
|
||||
|
||||
static _Thread_local unsigned int l_eventDepth;
|
||||
static _Thread_local unsigned int l_statusDepth;
|
||||
|
||||
static uint32_t nextGeneration(uint32_t generation)
|
||||
{
|
||||
if (++generation == 0)
|
||||
++generation;
|
||||
return generation;
|
||||
}
|
||||
|
||||
static LGEvent * createWaitEvent(void)
|
||||
{
|
||||
LGEvent * event = lgCreateEvent(true, 0);
|
||||
if (!event)
|
||||
DEBUG_FATAL("Failed to create SPICE audio wait event");
|
||||
return event;
|
||||
}
|
||||
|
||||
static void waitEvent(LGEvent * event)
|
||||
{
|
||||
if (!lgWaitEvent(event, TIMEOUT_INFINITE))
|
||||
DEBUG_FATAL("Failed to wait for SPICE audio event");
|
||||
}
|
||||
|
||||
static void signalWaitEvent(LGEvent * event)
|
||||
{
|
||||
if (!lgSignalEvent(event))
|
||||
DEBUG_FATAL("Failed to signal SPICE audio event");
|
||||
}
|
||||
|
||||
static void signalCallbackWaiters(
|
||||
SpiceAudioCallbackWaitQueue * queue, unsigned int remaining)
|
||||
{
|
||||
if (!atomic_load_explicit(&queue->count, memory_order_acquire))
|
||||
return;
|
||||
|
||||
LG_LOCK(queue->lock);
|
||||
SpiceAudioCallbackWaiter ** link = &queue->waiters;
|
||||
while (*link)
|
||||
{
|
||||
SpiceAudioCallbackWaiter * waiter = *link;
|
||||
if (remaining > waiter->depth)
|
||||
{
|
||||
link = &waiter->next;
|
||||
continue;
|
||||
}
|
||||
|
||||
*link = waiter->next;
|
||||
atomic_fetch_sub_explicit(
|
||||
&queue->count, 1, memory_order_release);
|
||||
signalWaitEvent(waiter->event);
|
||||
}
|
||||
LG_UNLOCK(queue->lock);
|
||||
}
|
||||
|
||||
static void endCallback(atomic_uint * inFlight,
|
||||
SpiceAudioCallbackWaitQueue * waitQueue)
|
||||
{
|
||||
const unsigned int previous = atomic_fetch_sub_explicit(
|
||||
inFlight, 1, memory_order_release);
|
||||
DEBUG_ASSERT(previous > 0);
|
||||
signalCallbackWaiters(waitQueue, previous - 1);
|
||||
}
|
||||
|
||||
static void waitCallbacks(atomic_uint * inFlight,
|
||||
SpiceAudioCallbackWaitQueue * waitQueue, unsigned int depth)
|
||||
{
|
||||
if (atomic_load_explicit(inFlight, memory_order_acquire) <= depth)
|
||||
return;
|
||||
|
||||
SpiceAudioCallbackWaiter waiter =
|
||||
{
|
||||
.event = createWaitEvent(),
|
||||
.depth = depth,
|
||||
};
|
||||
|
||||
bool queued = false;
|
||||
LG_LOCK(waitQueue->lock);
|
||||
if (atomic_load_explicit(inFlight, memory_order_acquire) > depth)
|
||||
{
|
||||
waiter.next = waitQueue->waiters;
|
||||
waitQueue->waiters = &waiter;
|
||||
atomic_fetch_add_explicit(
|
||||
&waitQueue->count, 1, memory_order_release);
|
||||
queued = true;
|
||||
}
|
||||
LG_UNLOCK(waitQueue->lock);
|
||||
|
||||
if (queued)
|
||||
{
|
||||
waitEvent(waiter.event);
|
||||
|
||||
/* The signaler owns the waiter until it releases the queue lock. */
|
||||
LG_LOCK(waitQueue->lock);
|
||||
LG_UNLOCK(waitQueue->lock);
|
||||
}
|
||||
|
||||
lgFreeEvent(waiter.event);
|
||||
}
|
||||
|
||||
/* l_spice.lock must be held exclusively while admitting a callback so detach
|
||||
* cannot invalidate the target between the snapshot and in-flight increment. */
|
||||
static bool beginEventNL(SpiceAudioEventTarget * target)
|
||||
{
|
||||
if (!l_spice.events)
|
||||
return false;
|
||||
|
||||
target->events = l_spice.events;
|
||||
target->opaque = l_spice.eventOpaque;
|
||||
target->generation = l_spice.eventGeneration;
|
||||
atomic_fetch_add_explicit(&l_spice.inFlight, 1, memory_order_relaxed);
|
||||
++l_eventDepth;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool playbackEventCurrent(const SpiceAudioEventTarget * target,
|
||||
uint32_t generation, bool active)
|
||||
{
|
||||
LG_LOCK_SHARED(l_spice.lock);
|
||||
const bool result =
|
||||
target->events == l_spice.events &&
|
||||
target->opaque == l_spice.eventOpaque &&
|
||||
target->generation == l_spice.eventGeneration &&
|
||||
l_spice.playback.generation == generation &&
|
||||
l_spice.playback.active == active;
|
||||
LG_UNLOCK_SHARED(l_spice.lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool recordEventCurrent(const SpiceAudioEventTarget * target,
|
||||
uint32_t generation, bool active)
|
||||
{
|
||||
LG_LOCK_SHARED(l_spice.lock);
|
||||
const bool result =
|
||||
target->events == l_spice.events &&
|
||||
target->opaque == l_spice.eventOpaque &&
|
||||
target->generation == l_spice.eventGeneration &&
|
||||
l_spice.record.generation == generation &&
|
||||
l_spice.record.active == active;
|
||||
LG_UNLOCK_SHARED(l_spice.lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void endEvent(void)
|
||||
{
|
||||
--l_eventDepth;
|
||||
endCallback(&l_spice.inFlight, &l_spice.eventWait);
|
||||
}
|
||||
|
||||
static bool sampleFormat(PSAudioFormat source,
|
||||
LG_AudioSampleFormat * format)
|
||||
{
|
||||
switch (source)
|
||||
{
|
||||
case PS_AUDIO_FMT_S16:
|
||||
*format = LG_AUDIO_FMT_S16_LE;
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void channelLayout(LG_AudioFormat * format)
|
||||
{
|
||||
memset(format->channels, 0, sizeof(format->channels));
|
||||
|
||||
switch (format->channelCount)
|
||||
{
|
||||
case 1:
|
||||
format->channels[0] = LG_AUDIO_CH_MONO;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
format->channels[0] = LG_AUDIO_CH_FRONT_LEFT;
|
||||
format->channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
format->channels[0] = LG_AUDIO_CH_FRONT_LEFT;
|
||||
format->channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
|
||||
format->channels[2] = LG_AUDIO_CH_FRONT_CENTER;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
format->channels[0] = LG_AUDIO_CH_FRONT_LEFT;
|
||||
format->channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
|
||||
format->channels[2] = LG_AUDIO_CH_REAR_LEFT;
|
||||
format->channels[3] = LG_AUDIO_CH_REAR_RIGHT;
|
||||
break;
|
||||
|
||||
case 5:
|
||||
format->channels[0] = LG_AUDIO_CH_FRONT_LEFT;
|
||||
format->channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
|
||||
format->channels[2] = LG_AUDIO_CH_FRONT_CENTER;
|
||||
format->channels[3] = LG_AUDIO_CH_REAR_LEFT;
|
||||
format->channels[4] = LG_AUDIO_CH_REAR_RIGHT;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
format->channels[0] = LG_AUDIO_CH_FRONT_LEFT;
|
||||
format->channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
|
||||
format->channels[2] = LG_AUDIO_CH_FRONT_CENTER;
|
||||
format->channels[3] = LG_AUDIO_CH_LFE;
|
||||
format->channels[4] = LG_AUDIO_CH_REAR_LEFT;
|
||||
format->channels[5] = LG_AUDIO_CH_REAR_RIGHT;
|
||||
break;
|
||||
|
||||
case 7:
|
||||
format->channels[0] = LG_AUDIO_CH_FRONT_LEFT;
|
||||
format->channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
|
||||
format->channels[2] = LG_AUDIO_CH_FRONT_CENTER;
|
||||
format->channels[3] = LG_AUDIO_CH_LFE;
|
||||
format->channels[4] = LG_AUDIO_CH_REAR_LEFT;
|
||||
format->channels[5] = LG_AUDIO_CH_REAR_RIGHT;
|
||||
format->channels[6] = LG_AUDIO_CH_REAR_CENTER;
|
||||
break;
|
||||
|
||||
case 8:
|
||||
format->channels[0] = LG_AUDIO_CH_FRONT_LEFT;
|
||||
format->channels[1] = LG_AUDIO_CH_FRONT_RIGHT;
|
||||
format->channels[2] = LG_AUDIO_CH_FRONT_CENTER;
|
||||
format->channels[3] = LG_AUDIO_CH_LFE;
|
||||
format->channels[4] = LG_AUDIO_CH_REAR_LEFT;
|
||||
format->channels[5] = LG_AUDIO_CH_REAR_RIGHT;
|
||||
format->channels[6] = LG_AUDIO_CH_SIDE_LEFT;
|
||||
format->channels[7] = LG_AUDIO_CH_SIDE_RIGHT;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static bool makeFormat(int channels, int sampleRate, PSAudioFormat source,
|
||||
LG_AudioFormat * format)
|
||||
{
|
||||
if (channels < 1 || channels > LG_AUDIO_MAX_CHANNELS || sampleRate < 1 ||
|
||||
!sampleFormat(source, &format->sampleFormat))
|
||||
return false;
|
||||
|
||||
format->sampleRate = sampleRate;
|
||||
format->channelCount = channels;
|
||||
channelLayout(format);
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t sampleSize(LG_AudioSampleFormat format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case LG_AUDIO_FMT_U8: return 1;
|
||||
case LG_AUDIO_FMT_S16_LE: return 2;
|
||||
case LG_AUDIO_FMT_S24_LE: return 3;
|
||||
case LG_AUDIO_FMT_S32_LE: return 4;
|
||||
case LG_AUDIO_FMT_F32_LE: return 4;
|
||||
case LG_AUDIO_FMT_F32_NE: return 4;
|
||||
case LG_AUDIO_FMT_F64_LE: return 8;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static LG_AudioClock playbackClockNL(uint32_t time)
|
||||
{
|
||||
bool discontinuity = false;
|
||||
|
||||
if (!l_spice.playbackClockValid)
|
||||
{
|
||||
l_spice.playbackClockValid = true;
|
||||
l_spice.playbackMediaTime = time;
|
||||
l_spice.playbackTime = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
const int32_t delta = (int32_t)(time - l_spice.playbackMediaTime);
|
||||
l_spice.playbackMediaTime = time;
|
||||
if (delta < 0 || delta > SPICE_AUDIO_TIMESTAMP_DISCONTINUITY_MS)
|
||||
{
|
||||
l_spice.playbackTime = 0;
|
||||
discontinuity = true;
|
||||
}
|
||||
else
|
||||
l_spice.playbackTime += (int64_t)delta * 1000000;
|
||||
}
|
||||
|
||||
l_spice.playbackClock = (LG_AudioClock)
|
||||
{
|
||||
.position = l_spice.playbackPosition,
|
||||
.time = l_spice.playbackTime,
|
||||
.rate = 0.0,
|
||||
.stable = !discontinuity,
|
||||
.discontinuity = discontinuity,
|
||||
};
|
||||
return l_spice.playbackClock;
|
||||
}
|
||||
|
||||
static void spiceSetStatusListener(void * opaque,
|
||||
LG_AudioStatusFn callback, void * callbackOpaque)
|
||||
{
|
||||
LG_AudioStatus status;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
l_spice.statusCallback = callback;
|
||||
l_spice.statusOpaque = callbackOpaque;
|
||||
status = (LG_AudioStatus)
|
||||
{
|
||||
.available = l_spice.available,
|
||||
.generation = l_spice.statusGeneration,
|
||||
};
|
||||
if (callback)
|
||||
{
|
||||
atomic_fetch_add_explicit(
|
||||
&l_spice.statusInFlight, 1, memory_order_relaxed);
|
||||
++l_statusDepth;
|
||||
}
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (callback)
|
||||
{
|
||||
callback(callbackOpaque, &status);
|
||||
--l_statusDepth;
|
||||
endCallback(&l_spice.statusInFlight, &l_spice.statusWait);
|
||||
}
|
||||
else
|
||||
waitCallbacks(&l_spice.statusInFlight,
|
||||
&l_spice.statusWait, l_statusDepth);
|
||||
}
|
||||
|
||||
static bool spiceAttach(void * opaque, const LG_AudioEventOps * events,
|
||||
void * eventOpaque)
|
||||
{
|
||||
if (!events)
|
||||
return false;
|
||||
|
||||
SpiceAudioEventTarget target;
|
||||
SpiceAudioStream playback;
|
||||
SpiceAudioStream record;
|
||||
LG_AudioClock playbackClock;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return false;
|
||||
}
|
||||
|
||||
l_spice.events = events;
|
||||
l_spice.eventOpaque = eventOpaque;
|
||||
l_spice.eventGeneration =
|
||||
nextGeneration(l_spice.eventGeneration);
|
||||
playback = l_spice.playback;
|
||||
record = l_spice.record;
|
||||
playbackClock = l_spice.playbackClock;
|
||||
dispatch = beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return true;
|
||||
|
||||
if (playback.active)
|
||||
{
|
||||
if (playbackEventCurrent(&target, playback.generation, true) &&
|
||||
target.events->playbackStart)
|
||||
{
|
||||
target.events->playbackStart(target.opaque,
|
||||
playback.generation, &playback.format, &playbackClock);
|
||||
if (playback.volumeValid &&
|
||||
playbackEventCurrent(&target, playback.generation, true) &&
|
||||
target.events->playbackVolume)
|
||||
target.events->playbackVolume(target.opaque,
|
||||
playback.generation,
|
||||
playback.volumeChannels, playback.volume);
|
||||
if (playback.muteValid &&
|
||||
playbackEventCurrent(&target, playback.generation, true) &&
|
||||
target.events->playbackMute)
|
||||
target.events->playbackMute(target.opaque,
|
||||
playback.generation, playback.mute);
|
||||
}
|
||||
}
|
||||
|
||||
if (record.active)
|
||||
{
|
||||
if (recordEventCurrent(&target, record.generation, true) &&
|
||||
target.events->recordStart)
|
||||
{
|
||||
target.events->recordStart(target.opaque,
|
||||
record.generation, &record.format);
|
||||
if (record.volumeValid &&
|
||||
recordEventCurrent(&target, record.generation, true) &&
|
||||
target.events->recordVolume)
|
||||
target.events->recordVolume(target.opaque, record.generation,
|
||||
record.volumeChannels, record.volume);
|
||||
if (record.muteValid &&
|
||||
recordEventCurrent(&target, record.generation, true) &&
|
||||
target.events->recordMute)
|
||||
target.events->recordMute(target.opaque,
|
||||
record.generation, record.mute);
|
||||
}
|
||||
}
|
||||
|
||||
endEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
static void spiceDetach(void * opaque)
|
||||
{
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
l_spice.events = NULL;
|
||||
l_spice.eventOpaque = NULL;
|
||||
l_spice.eventGeneration =
|
||||
nextGeneration(l_spice.eventGeneration);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
waitCallbacks(&l_spice.inFlight, &l_spice.eventWait, l_eventDepth);
|
||||
}
|
||||
|
||||
static bool spiceRecordData(void * opaque, uint32_t generation,
|
||||
const void * data, size_t frames, const LG_AudioClock * sourceClock)
|
||||
{
|
||||
size_t size = 0;
|
||||
bool valid = false;
|
||||
|
||||
LG_LOCK_SHARED(l_spice.lock);
|
||||
if (l_spice.available && l_spice.events && l_spice.record.active &&
|
||||
generation == l_spice.record.generation)
|
||||
{
|
||||
const size_t bytesPerSample =
|
||||
sampleSize(l_spice.record.format.sampleFormat);
|
||||
const size_t channels = l_spice.record.format.channelCount;
|
||||
if (bytesPerSample && frames <= SIZE_MAX / bytesPerSample / channels &&
|
||||
(frames == 0 || data))
|
||||
{
|
||||
size = frames * bytesPerSample * channels;
|
||||
valid = true;
|
||||
}
|
||||
}
|
||||
LG_UNLOCK_SHARED(l_spice.lock);
|
||||
|
||||
return valid && purespice_writeAudio((void *)data, size, 0);
|
||||
}
|
||||
|
||||
const LG_AudioOps LGA_Spice =
|
||||
{
|
||||
.name = "SPICE",
|
||||
.setStatusListener = spiceSetStatusListener,
|
||||
.attach = spiceAttach,
|
||||
.detach = spiceDetach,
|
||||
.recordData = spiceRecordData,
|
||||
.clockFeedback = NULL,
|
||||
};
|
||||
|
||||
void lgaSpice_setAvailable(bool available)
|
||||
{
|
||||
LG_AudioStatusFn callback;
|
||||
void * callbackOpaque;
|
||||
LG_AudioStatus status;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (l_spice.available == available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
l_spice.available = available;
|
||||
l_spice.statusGeneration =
|
||||
nextGeneration(l_spice.statusGeneration);
|
||||
if (!available)
|
||||
{
|
||||
l_spice.playback.active = false;
|
||||
l_spice.record.active = false;
|
||||
l_spice.playbackClockValid = false;
|
||||
l_spice.playbackPosition = 0;
|
||||
l_spice.playbackClock = (LG_AudioClock) { 0 };
|
||||
l_spice.playback.volumeValid = false;
|
||||
l_spice.playback.muteValid = false;
|
||||
l_spice.record.volumeValid = false;
|
||||
l_spice.record.muteValid = false;
|
||||
}
|
||||
|
||||
callback = l_spice.statusCallback;
|
||||
callbackOpaque = l_spice.statusOpaque;
|
||||
status = (LG_AudioStatus)
|
||||
{
|
||||
.available = available,
|
||||
.generation = l_spice.statusGeneration,
|
||||
};
|
||||
if (callback)
|
||||
{
|
||||
atomic_fetch_add_explicit(
|
||||
&l_spice.statusInFlight, 1, memory_order_relaxed);
|
||||
++l_statusDepth;
|
||||
}
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (callback)
|
||||
{
|
||||
callback(callbackOpaque, &status);
|
||||
--l_statusDepth;
|
||||
endCallback(&l_spice.statusInFlight, &l_spice.statusWait);
|
||||
}
|
||||
}
|
||||
|
||||
void lgaSpice_playbackStart(int channels, int sampleRate,
|
||||
PSAudioFormat sourceFormat, uint32_t time)
|
||||
{
|
||||
LG_AudioFormat format;
|
||||
if (!makeFormat(channels, sampleRate, sourceFormat, &format))
|
||||
{
|
||||
DEBUG_ERROR("Invalid SPICE playback format: %d channels, %d Hz, %d",
|
||||
channels, sampleRate, sourceFormat);
|
||||
return;
|
||||
}
|
||||
|
||||
SpiceAudioEventTarget target;
|
||||
SpiceAudioStream playback;
|
||||
LG_AudioClock clock;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
l_spice.playback.active = true;
|
||||
l_spice.playback.generation =
|
||||
nextGeneration(l_spice.playback.generation);
|
||||
l_spice.playback.format = format;
|
||||
l_spice.playbackClockValid = false;
|
||||
l_spice.playbackPosition = 0;
|
||||
clock = playbackClockNL(time);
|
||||
clock.stable = false;
|
||||
clock.discontinuity = true;
|
||||
l_spice.playbackClock = clock;
|
||||
playback = l_spice.playback;
|
||||
dispatch = beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (playbackEventCurrent(&target, playback.generation, true) &&
|
||||
target.events->playbackStart)
|
||||
{
|
||||
target.events->playbackStart(target.opaque,
|
||||
playback.generation, &playback.format, &clock);
|
||||
if (playback.volumeValid &&
|
||||
playbackEventCurrent(&target, playback.generation, true) &&
|
||||
target.events->playbackVolume)
|
||||
target.events->playbackVolume(target.opaque,
|
||||
playback.generation, playback.volumeChannels, playback.volume);
|
||||
if (playback.muteValid &&
|
||||
playbackEventCurrent(&target, playback.generation, true) &&
|
||||
target.events->playbackMute)
|
||||
target.events->playbackMute(target.opaque,
|
||||
playback.generation, playback.mute);
|
||||
}
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_playbackStop(void)
|
||||
{
|
||||
SpiceAudioEventTarget target;
|
||||
uint32_t generation;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.playback.active)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
generation = l_spice.playback.generation;
|
||||
l_spice.playback.active = false;
|
||||
l_spice.playbackClockValid = false;
|
||||
dispatch = beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (target.events->playbackStop &&
|
||||
playbackEventCurrent(&target, generation, false))
|
||||
target.events->playbackStop(target.opaque, generation);
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_playbackVolume(int channels, const uint16_t volume[])
|
||||
{
|
||||
if (channels < 1 || channels > LG_AUDIO_MAX_CHANNELS || !volume)
|
||||
return;
|
||||
|
||||
SpiceAudioEventTarget target;
|
||||
uint32_t generation;
|
||||
uint16_t snapshot[LG_AUDIO_MAX_CHANNELS];
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(l_spice.playback.volume, volume,
|
||||
(size_t)channels * sizeof(*volume));
|
||||
l_spice.playback.volumeChannels = channels;
|
||||
l_spice.playback.volumeValid = true;
|
||||
generation = l_spice.playback.generation;
|
||||
memcpy(snapshot, volume, (size_t)channels * sizeof(*volume));
|
||||
dispatch = l_spice.playback.active && beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (target.events->playbackVolume &&
|
||||
playbackEventCurrent(&target, generation, true))
|
||||
target.events->playbackVolume(
|
||||
target.opaque, generation, channels, snapshot);
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_playbackMute(bool mute)
|
||||
{
|
||||
SpiceAudioEventTarget target;
|
||||
uint32_t generation;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
l_spice.playback.mute = mute;
|
||||
l_spice.playback.muteValid = true;
|
||||
generation = l_spice.playback.generation;
|
||||
dispatch = l_spice.playback.active && beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (target.events->playbackMute &&
|
||||
playbackEventCurrent(&target, generation, true))
|
||||
target.events->playbackMute(target.opaque, generation, mute);
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_playbackData(uint8_t * data, size_t size, uint32_t time)
|
||||
{
|
||||
SpiceAudioEventTarget target;
|
||||
uint32_t generation;
|
||||
size_t frames;
|
||||
LG_AudioClock clock;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available || !l_spice.playback.active)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t bytesPerSample =
|
||||
sampleSize(l_spice.playback.format.sampleFormat);
|
||||
const size_t stride =
|
||||
bytesPerSample * l_spice.playback.format.channelCount;
|
||||
if (!stride || !size || !data || size % stride)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
DEBUG_ERROR("Invalid SPICE playback packet size: %zu", size);
|
||||
return;
|
||||
}
|
||||
|
||||
frames = size / stride;
|
||||
generation = l_spice.playback.generation;
|
||||
clock = playbackClockNL(time);
|
||||
l_spice.playbackPosition += frames;
|
||||
dispatch = beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (target.events->playbackData &&
|
||||
playbackEventCurrent(&target, generation, true))
|
||||
target.events->playbackData(target.opaque, generation,
|
||||
data, frames, &clock);
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_recordStart(int channels, int sampleRate,
|
||||
PSAudioFormat sourceFormat)
|
||||
{
|
||||
LG_AudioFormat format;
|
||||
if (!makeFormat(channels, sampleRate, sourceFormat, &format))
|
||||
{
|
||||
DEBUG_ERROR("Invalid SPICE record format: %d channels, %d Hz, %d",
|
||||
channels, sampleRate, sourceFormat);
|
||||
return;
|
||||
}
|
||||
|
||||
SpiceAudioEventTarget target;
|
||||
SpiceAudioStream record;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
l_spice.record.active = true;
|
||||
l_spice.record.generation = nextGeneration(l_spice.record.generation);
|
||||
l_spice.record.format = format;
|
||||
record = l_spice.record;
|
||||
dispatch = beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (recordEventCurrent(&target, record.generation, true) &&
|
||||
target.events->recordStart)
|
||||
{
|
||||
target.events->recordStart(
|
||||
target.opaque, record.generation, &record.format);
|
||||
if (record.volumeValid &&
|
||||
recordEventCurrent(&target, record.generation, true) &&
|
||||
target.events->recordVolume)
|
||||
target.events->recordVolume(target.opaque, record.generation,
|
||||
record.volumeChannels, record.volume);
|
||||
if (record.muteValid &&
|
||||
recordEventCurrent(&target, record.generation, true) &&
|
||||
target.events->recordMute)
|
||||
target.events->recordMute(
|
||||
target.opaque, record.generation, record.mute);
|
||||
}
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_recordStop(void)
|
||||
{
|
||||
SpiceAudioEventTarget target;
|
||||
uint32_t generation;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.record.active)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
generation = l_spice.record.generation;
|
||||
l_spice.record.active = false;
|
||||
dispatch = beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (target.events->recordStop &&
|
||||
recordEventCurrent(&target, generation, false))
|
||||
target.events->recordStop(target.opaque, generation);
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_recordVolume(int channels, const uint16_t volume[])
|
||||
{
|
||||
if (channels < 1 || channels > LG_AUDIO_MAX_CHANNELS || !volume)
|
||||
return;
|
||||
|
||||
SpiceAudioEventTarget target;
|
||||
uint32_t generation;
|
||||
uint16_t snapshot[LG_AUDIO_MAX_CHANNELS];
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(l_spice.record.volume, volume,
|
||||
(size_t)channels * sizeof(*volume));
|
||||
l_spice.record.volumeChannels = channels;
|
||||
l_spice.record.volumeValid = true;
|
||||
generation = l_spice.record.generation;
|
||||
memcpy(snapshot, volume, (size_t)channels * sizeof(*volume));
|
||||
dispatch = l_spice.record.active && beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (target.events->recordVolume &&
|
||||
recordEventCurrent(&target, generation, true))
|
||||
target.events->recordVolume(
|
||||
target.opaque, generation, channels, snapshot);
|
||||
endEvent();
|
||||
}
|
||||
|
||||
void lgaSpice_recordMute(bool mute)
|
||||
{
|
||||
SpiceAudioEventTarget target;
|
||||
uint32_t generation;
|
||||
bool dispatch;
|
||||
|
||||
LG_LOCK_EXCLUSIVE(l_spice.lock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
return;
|
||||
}
|
||||
|
||||
l_spice.record.mute = mute;
|
||||
l_spice.record.muteValid = true;
|
||||
generation = l_spice.record.generation;
|
||||
dispatch = l_spice.record.active && beginEventNL(&target);
|
||||
LG_UNLOCK_EXCLUSIVE(l_spice.lock);
|
||||
|
||||
if (!dispatch)
|
||||
return;
|
||||
|
||||
if (target.events->recordMute &&
|
||||
recordEventCurrent(&target, generation, true))
|
||||
target.events->recordMute(target.opaque, generation, mute);
|
||||
endEvent();
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef _H_LG_CLIENT_AUDIO_SPICE_
|
||||
#define _H_LG_CLIENT_AUDIO_SPICE_
|
||||
|
||||
#include "interface/audio.h"
|
||||
|
||||
#include <purespice.h>
|
||||
|
||||
extern const LG_AudioOps LGA_Spice;
|
||||
|
||||
void lgaSpice_setAvailable(bool available);
|
||||
|
||||
void lgaSpice_playbackStart(int channels, int sampleRate,
|
||||
PSAudioFormat format, uint32_t time);
|
||||
void lgaSpice_playbackStop(void);
|
||||
void lgaSpice_playbackVolume(int channels, const uint16_t volume[]);
|
||||
void lgaSpice_playbackMute(bool mute);
|
||||
void lgaSpice_playbackData(uint8_t * data, size_t size, uint32_t time);
|
||||
|
||||
void lgaSpice_recordStart(int channels, int sampleRate,
|
||||
PSAudioFormat format);
|
||||
void lgaSpice_recordStop(void);
|
||||
void lgaSpice_recordVolume(int channels, const uint16_t volume[]);
|
||||
void lgaSpice_recordMute(bool mute);
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,40 +0,0 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef _H_LG_CLIENT_AUDIO_USB_
|
||||
#define _H_LG_CLIENT_AUDIO_USB_
|
||||
|
||||
#include "interface/audio.h"
|
||||
#include "usbredir.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct LGA_USBState LGA_USBState;
|
||||
|
||||
LGA_USBState * lgaUsb_create(bool debug);
|
||||
/* Detach this provider and stop PureSpice before destroying its state. */
|
||||
void lgaUsb_destroy(LGA_USBState * state);
|
||||
|
||||
LG_USBRedir * lgaUsb_redir(LGA_USBState * state);
|
||||
uint64_t lgaUsb_processDelayNs(const LGA_USBState * state);
|
||||
|
||||
extern const LG_AudioOps LGA_USB;
|
||||
|
||||
#endif
|
||||
@@ -1,491 +0,0 @@
|
||||
/**
|
||||
* 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 "clipboard_spice.h"
|
||||
|
||||
#include "common/debug.h"
|
||||
#include "common/locking.h"
|
||||
|
||||
#include <stdatomic.h>
|
||||
|
||||
typedef struct ClipboardEventTarget
|
||||
{
|
||||
const LG_ClipboardEventOps * events;
|
||||
void * opaque;
|
||||
}
|
||||
ClipboardEventTarget;
|
||||
|
||||
typedef struct PendingRequest
|
||||
{
|
||||
bool pending;
|
||||
LG_ClipboardRequest request;
|
||||
LG_ClipboardData type;
|
||||
}
|
||||
PendingRequest;
|
||||
|
||||
static struct
|
||||
{
|
||||
LG_Lock stateLock;
|
||||
LG_Lock eventDispatch;
|
||||
LG_Lock statusDispatch;
|
||||
|
||||
bool available;
|
||||
uint32_t statusGeneration;
|
||||
LG_ClipboardStatusFn statusCallback;
|
||||
void * statusOpaque;
|
||||
|
||||
const LG_ClipboardEventOps * events;
|
||||
void * eventOpaque;
|
||||
|
||||
bool remoteNotice;
|
||||
LG_ClipboardData remoteType;
|
||||
PendingRequest read;
|
||||
PendingRequest write;
|
||||
|
||||
LG_ClipboardRequest requestSerial;
|
||||
}
|
||||
l_spice =
|
||||
{
|
||||
.stateLock = ATOMIC_FLAG_INIT,
|
||||
.eventDispatch = ATOMIC_FLAG_INIT,
|
||||
.statusDispatch = ATOMIC_FLAG_INIT,
|
||||
.remoteType = LG_CLIPBOARD_DATA_NONE,
|
||||
};
|
||||
|
||||
static uint32_t nextGeneration(uint32_t generation)
|
||||
{
|
||||
if (++generation == 0)
|
||||
++generation;
|
||||
return generation;
|
||||
}
|
||||
|
||||
static LG_ClipboardRequest nextRequestNL(void)
|
||||
{
|
||||
if (++l_spice.requestSerial == LG_CLIPBOARD_REQUEST_INVALID)
|
||||
++l_spice.requestSerial;
|
||||
return l_spice.requestSerial;
|
||||
}
|
||||
|
||||
static bool spiceType(PSDataType source, LG_ClipboardData * type)
|
||||
{
|
||||
switch (source)
|
||||
{
|
||||
case SPICE_DATA_TEXT : *type = LG_CLIPBOARD_DATA_TEXT; return true;
|
||||
case SPICE_DATA_PNG : *type = LG_CLIPBOARD_DATA_PNG ; return true;
|
||||
case SPICE_DATA_BMP : *type = LG_CLIPBOARD_DATA_BMP ; return true;
|
||||
case SPICE_DATA_TIFF : *type = LG_CLIPBOARD_DATA_TIFF; return true;
|
||||
case SPICE_DATA_JPEG : *type = LG_CLIPBOARD_DATA_JPEG; return true;
|
||||
case SPICE_DATA_NONE : break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool lgType(LG_ClipboardData source, PSDataType * type)
|
||||
{
|
||||
switch (source)
|
||||
{
|
||||
case LG_CLIPBOARD_DATA_TEXT : *type = SPICE_DATA_TEXT; return true;
|
||||
case LG_CLIPBOARD_DATA_PNG : *type = SPICE_DATA_PNG ; return true;
|
||||
case LG_CLIPBOARD_DATA_BMP : *type = SPICE_DATA_BMP ; return true;
|
||||
case LG_CLIPBOARD_DATA_TIFF : *type = SPICE_DATA_TIFF; return true;
|
||||
case LG_CLIPBOARD_DATA_JPEG : *type = SPICE_DATA_JPEG; return true;
|
||||
case LG_CLIPBOARD_DATA_NONE : *type = SPICE_DATA_NONE; return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void spiceSetStatusListener(void * opaque,
|
||||
LG_ClipboardStatusFn callback, void * callbackOpaque)
|
||||
{
|
||||
(void)opaque;
|
||||
LG_LOCK(l_spice.statusDispatch);
|
||||
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
l_spice.statusCallback = callback;
|
||||
l_spice.statusOpaque = callbackOpaque;
|
||||
const LG_ClipboardStatus status =
|
||||
{
|
||||
.available = l_spice.available,
|
||||
.generation = l_spice.statusGeneration,
|
||||
};
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (callback)
|
||||
callback(callbackOpaque, &status);
|
||||
LG_UNLOCK(l_spice.statusDispatch);
|
||||
}
|
||||
|
||||
static bool spiceAttach(void * opaque,
|
||||
const LG_ClipboardEventOps * events, void * eventOpaque)
|
||||
{
|
||||
(void)opaque;
|
||||
if (!events)
|
||||
return false;
|
||||
|
||||
LG_LOCK(l_spice.eventDispatch);
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
if (!l_spice.available)
|
||||
{
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
return false;
|
||||
}
|
||||
|
||||
l_spice.events = events;
|
||||
l_spice.eventOpaque = eventOpaque;
|
||||
const bool notice = l_spice.remoteNotice;
|
||||
const LG_ClipboardData type = l_spice.remoteType;
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (notice && events->notice)
|
||||
events->notice(eventOpaque, &type, 1);
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void spiceDetach(void * opaque)
|
||||
{
|
||||
(void)opaque;
|
||||
LG_LOCK(l_spice.eventDispatch);
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
const bool failWrite = l_spice.available && l_spice.write.pending;
|
||||
l_spice.events = NULL;
|
||||
l_spice.eventOpaque = NULL;
|
||||
l_spice.read = (PendingRequest) { 0 };
|
||||
l_spice.write = (PendingRequest) { 0 };
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
if (failWrite)
|
||||
purespice_clipboardDataStart(SPICE_DATA_NONE, 0);
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
}
|
||||
|
||||
static bool spiceRelease(void * opaque)
|
||||
{
|
||||
(void)opaque;
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
const bool available = l_spice.available;
|
||||
const bool failWrite = l_spice.write.pending;
|
||||
l_spice.write = (PendingRequest) { 0 };
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (!available)
|
||||
return false;
|
||||
|
||||
const bool failed = !failWrite ||
|
||||
purespice_clipboardDataStart(SPICE_DATA_NONE, 0);
|
||||
return purespice_clipboardRelease() && failed;
|
||||
}
|
||||
|
||||
static bool spiceNotifyTypes(void * opaque,
|
||||
const LG_ClipboardData types[], size_t count)
|
||||
{
|
||||
(void)opaque;
|
||||
if (count == 0)
|
||||
return spiceRelease(NULL);
|
||||
if (!types || count > LG_CLIPBOARD_DATA_NONE)
|
||||
return false;
|
||||
|
||||
PSDataType converted[LG_CLIPBOARD_DATA_NONE];
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
if (types[i] == LG_CLIPBOARD_DATA_NONE ||
|
||||
!lgType(types[i], &converted[i]))
|
||||
return false;
|
||||
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
const bool available = l_spice.available;
|
||||
const bool failWrite = l_spice.write.pending;
|
||||
l_spice.write = (PendingRequest) { 0 };
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (!available)
|
||||
return false;
|
||||
|
||||
const bool failed = !failWrite ||
|
||||
purespice_clipboardDataStart(SPICE_DATA_NONE, 0);
|
||||
return purespice_clipboardGrab(converted, (int)count) && failed;
|
||||
}
|
||||
|
||||
static bool spiceData(void * opaque, LG_ClipboardRequest request,
|
||||
LG_ClipboardData type, const void * data, size_t size)
|
||||
{
|
||||
(void)opaque;
|
||||
PSDataType converted;
|
||||
if (request == LG_CLIPBOARD_REQUEST_INVALID || !lgType(type, &converted) ||
|
||||
(type == LG_CLIPBOARD_DATA_NONE && size != 0) ||
|
||||
(size && !data))
|
||||
return false;
|
||||
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
const bool valid = l_spice.available && l_spice.write.pending &&
|
||||
l_spice.write.request == request &&
|
||||
(type == LG_CLIPBOARD_DATA_NONE || l_spice.write.type == type);
|
||||
if (valid)
|
||||
l_spice.write = (PendingRequest) { 0 };
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (!valid || !purespice_clipboardDataStart(converted, size))
|
||||
return false;
|
||||
|
||||
return !size || purespice_clipboardData(
|
||||
converted, (uint8_t *)data, size);
|
||||
}
|
||||
|
||||
static bool spiceRequest(void * opaque, LG_ClipboardRequest request,
|
||||
LG_ClipboardData type)
|
||||
{
|
||||
(void)opaque;
|
||||
PSDataType converted;
|
||||
if (request == LG_CLIPBOARD_REQUEST_INVALID ||
|
||||
type == LG_CLIPBOARD_DATA_NONE || !lgType(type, &converted))
|
||||
return false;
|
||||
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
const bool valid = l_spice.available && l_spice.events &&
|
||||
!l_spice.read.pending;
|
||||
if (valid)
|
||||
l_spice.read = (PendingRequest)
|
||||
{
|
||||
.pending = true,
|
||||
.request = request,
|
||||
.type = type,
|
||||
};
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (!valid)
|
||||
return false;
|
||||
|
||||
if (purespice_clipboardRequest(converted))
|
||||
return true;
|
||||
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
if (!l_spice.read.pending || l_spice.read.request != request)
|
||||
{
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
return true;
|
||||
}
|
||||
l_spice.read = (PendingRequest) { 0 };
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
return false;
|
||||
}
|
||||
|
||||
const LG_ClipboardOps LGC_Spice =
|
||||
{
|
||||
.name = "SPICE",
|
||||
.setStatusListener = spiceSetStatusListener,
|
||||
.attach = spiceAttach,
|
||||
.detach = spiceDetach,
|
||||
.release = spiceRelease,
|
||||
.notifyTypes = spiceNotifyTypes,
|
||||
.data = spiceData,
|
||||
.request = spiceRequest,
|
||||
};
|
||||
|
||||
void lgcSpice_setAvailable(bool available)
|
||||
{
|
||||
LG_LOCK(l_spice.statusDispatch);
|
||||
LG_LOCK(l_spice.eventDispatch);
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
|
||||
const bool changed = l_spice.available != available;
|
||||
if (changed)
|
||||
{
|
||||
l_spice.available = available;
|
||||
l_spice.statusGeneration = nextGeneration(
|
||||
l_spice.statusGeneration);
|
||||
}
|
||||
if (!available)
|
||||
{
|
||||
l_spice.remoteNotice = false;
|
||||
l_spice.remoteType = LG_CLIPBOARD_DATA_NONE;
|
||||
l_spice.read = (PendingRequest) { 0 };
|
||||
l_spice.write = (PendingRequest) { 0 };
|
||||
}
|
||||
|
||||
const LG_ClipboardStatusFn callback = l_spice.statusCallback;
|
||||
void * callbackOpaque = l_spice.statusOpaque;
|
||||
const LG_ClipboardStatus status =
|
||||
{
|
||||
.available = available,
|
||||
.generation = l_spice.statusGeneration,
|
||||
};
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
|
||||
if (changed && callback)
|
||||
callback(callbackOpaque, &status);
|
||||
LG_UNLOCK(l_spice.statusDispatch);
|
||||
}
|
||||
|
||||
void lgcSpice_notice(PSDataType source)
|
||||
{
|
||||
LG_ClipboardData type;
|
||||
if (!spiceType(source, &type))
|
||||
{
|
||||
if (source != SPICE_DATA_NONE)
|
||||
DEBUG_ERROR("Invalid SPICE clipboard notice type: %d", source);
|
||||
lgcSpice_release();
|
||||
return;
|
||||
}
|
||||
|
||||
LG_LOCK(l_spice.eventDispatch);
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
const bool failWrite = l_spice.write.pending;
|
||||
l_spice.remoteNotice = true;
|
||||
l_spice.remoteType = type;
|
||||
l_spice.read = (PendingRequest) { 0 };
|
||||
l_spice.write = (PendingRequest) { 0 };
|
||||
const ClipboardEventTarget target =
|
||||
{
|
||||
.events = l_spice.available ? l_spice.events : NULL,
|
||||
.opaque = l_spice.eventOpaque,
|
||||
};
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (failWrite)
|
||||
purespice_clipboardDataStart(SPICE_DATA_NONE, 0);
|
||||
if (target.events && target.events->notice)
|
||||
target.events->notice(target.opaque, &type, 1);
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
}
|
||||
|
||||
void lgcSpice_data(PSDataType source, uint8_t * buffer, uint32_t size)
|
||||
{
|
||||
LG_ClipboardData type = LG_CLIPBOARD_DATA_NONE;
|
||||
const bool converted = spiceType(source, &type);
|
||||
const bool validData = source == SPICE_DATA_NONE ||
|
||||
(converted && (!size || buffer));
|
||||
|
||||
LG_LOCK(l_spice.eventDispatch);
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
if (!l_spice.read.pending)
|
||||
{
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
DEBUG_WARN("Ignoring unsolicited SPICE clipboard data");
|
||||
return;
|
||||
}
|
||||
|
||||
const PendingRequest request = l_spice.read;
|
||||
l_spice.read = (PendingRequest) { 0 };
|
||||
const ClipboardEventTarget target =
|
||||
{
|
||||
.events = l_spice.available ? l_spice.events : NULL,
|
||||
.opaque = l_spice.eventOpaque,
|
||||
};
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (target.events && target.events->data)
|
||||
{
|
||||
if (!validData || (source != SPICE_DATA_NONE && type != request.type))
|
||||
{
|
||||
DEBUG_ERROR("Invalid SPICE clipboard response");
|
||||
target.events->data(target.opaque, request.request,
|
||||
LG_CLIPBOARD_DATA_NONE, NULL, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (type == LG_CLIPBOARD_DATA_TEXT && size)
|
||||
{
|
||||
uint8_t * output = buffer;
|
||||
for (uint32_t i = 0; i < size; ++i)
|
||||
if (buffer[i] != '\r')
|
||||
*output++ = buffer[i];
|
||||
size = (uint32_t)(output - buffer);
|
||||
}
|
||||
|
||||
target.events->data(target.opaque, request.request,
|
||||
type, buffer, size);
|
||||
}
|
||||
}
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
}
|
||||
|
||||
void lgcSpice_release(void)
|
||||
{
|
||||
LG_LOCK(l_spice.eventDispatch);
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
l_spice.remoteNotice = false;
|
||||
l_spice.remoteType = LG_CLIPBOARD_DATA_NONE;
|
||||
l_spice.read = (PendingRequest) { 0 };
|
||||
const ClipboardEventTarget target =
|
||||
{
|
||||
.events = l_spice.available ? l_spice.events : NULL,
|
||||
.opaque = l_spice.eventOpaque,
|
||||
};
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
if (target.events && target.events->release)
|
||||
target.events->release(target.opaque);
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
}
|
||||
|
||||
void lgcSpice_request(PSDataType source)
|
||||
{
|
||||
LG_ClipboardData type;
|
||||
if (!spiceType(source, &type))
|
||||
{
|
||||
DEBUG_ERROR("Invalid SPICE clipboard request type: %d", source);
|
||||
purespice_clipboardDataStart(SPICE_DATA_NONE, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
LG_LOCK(l_spice.eventDispatch);
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
const bool busy = l_spice.write.pending;
|
||||
const ClipboardEventTarget target =
|
||||
{
|
||||
.events = l_spice.available && !busy ?
|
||||
l_spice.events : NULL,
|
||||
.opaque = l_spice.eventOpaque,
|
||||
};
|
||||
LG_ClipboardRequest request = LG_CLIPBOARD_REQUEST_INVALID;
|
||||
if (target.events)
|
||||
{
|
||||
request = nextRequestNL();
|
||||
l_spice.write = (PendingRequest)
|
||||
{
|
||||
.pending = true,
|
||||
.request = request,
|
||||
.type = type,
|
||||
};
|
||||
}
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
|
||||
const bool accepted = target.events && target.events->request &&
|
||||
target.events->request(target.opaque, request, type);
|
||||
if (!accepted)
|
||||
{
|
||||
bool fail = !busy && request == LG_CLIPBOARD_REQUEST_INVALID;
|
||||
LG_LOCK(l_spice.stateLock);
|
||||
if (l_spice.write.pending && l_spice.write.request == request)
|
||||
{
|
||||
l_spice.write = (PendingRequest) { 0 };
|
||||
fail = true;
|
||||
}
|
||||
LG_UNLOCK(l_spice.stateLock);
|
||||
if (fail)
|
||||
purespice_clipboardDataStart(SPICE_DATA_NONE, 0);
|
||||
else if (busy)
|
||||
DEBUG_WARN("Ignoring overlapping SPICE clipboard request");
|
||||
}
|
||||
LG_UNLOCK(l_spice.eventDispatch);
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef _H_LG_CLIENT_CLIPBOARD_SPICE_
|
||||
#define _H_LG_CLIENT_CLIPBOARD_SPICE_
|
||||
|
||||
#include "interface/clipboard.h"
|
||||
|
||||
#include <purespice.h>
|
||||
|
||||
extern const LG_ClipboardOps LGC_Spice;
|
||||
|
||||
void lgcSpice_setAvailable(bool available);
|
||||
|
||||
void lgcSpice_notice(PSDataType type);
|
||||
void lgcSpice_data(PSDataType type, uint8_t * buffer, uint32_t size);
|
||||
void lgcSpice_release(void);
|
||||
void lgcSpice_request(PSDataType type);
|
||||
|
||||
#endif
|
||||
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
* 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 "input.h"
|
||||
#include "kb.h"
|
||||
|
||||
#include <purespice.h>
|
||||
#include <stddef.h>
|
||||
|
||||
static bool spiceSupports(void * opaque, LG_InputSupport support)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool spiceKeyDown(void * opaque, int key)
|
||||
{
|
||||
const uint32_t ps2 = linux_to_ps2[key];
|
||||
return !ps2 || purespice_keyDown(ps2);
|
||||
}
|
||||
|
||||
static bool spiceKeyUp(void * opaque, int key)
|
||||
{
|
||||
const uint32_t ps2 = linux_to_ps2[key];
|
||||
return !ps2 || purespice_keyUp(ps2);
|
||||
}
|
||||
|
||||
static bool spiceKeyboardLEDs(void * opaque, bool numLock, bool capsLock,
|
||||
bool scrollLock)
|
||||
{
|
||||
const uint32_t modifiers =
|
||||
(scrollLock ? 1 /* SPICE_SCROLL_LOCK_MODIFIER */ : 0) |
|
||||
(numLock ? 2 /* SPICE_NUM_LOCK_MODIFIER */ : 0) |
|
||||
(capsLock ? 4 /* SPICE_CAPS_LOCK_MODIFIER */ : 0);
|
||||
|
||||
return purespice_keyModifiers(modifiers);
|
||||
}
|
||||
|
||||
static bool spiceMouseMotion(void * opaque, int32_t x, int32_t y)
|
||||
{
|
||||
return purespice_mouseMotion(x, y);
|
||||
}
|
||||
|
||||
static bool spiceMousePress(void * opaque, unsigned int button)
|
||||
{
|
||||
if (button > 7)
|
||||
return true;
|
||||
|
||||
return purespice_mousePress(button);
|
||||
}
|
||||
|
||||
static bool spiceMouseRelease(void * opaque, unsigned int button)
|
||||
{
|
||||
if (button > 7)
|
||||
return true;
|
||||
|
||||
return purespice_mouseRelease(button);
|
||||
}
|
||||
|
||||
const LG_InputOps LGI_Spice =
|
||||
{
|
||||
.name = "SPICE",
|
||||
.supports = spiceSupports,
|
||||
.keyDown = spiceKeyDown,
|
||||
.keyUp = spiceKeyUp,
|
||||
.keyboardLEDs = spiceKeyboardLEDs,
|
||||
.mouseMotion = spiceMouseMotion,
|
||||
.mousePosition = NULL,
|
||||
.mousePress = spiceMousePress,
|
||||
.mouseRelease = spiceMouseRelease,
|
||||
.reset = NULL,
|
||||
};
|
||||
@@ -58,15 +58,8 @@
|
||||
#include "core.h"
|
||||
#include "app.h"
|
||||
#include "audio.h"
|
||||
#if ENABLE_AUDIO
|
||||
#include "audio_spice.h"
|
||||
#endif
|
||||
#if ENABLE_USB_AUDIO
|
||||
#include "audio_usb.h"
|
||||
#endif
|
||||
#include "keybind.h"
|
||||
#include "clipboard.h"
|
||||
#include "clipboard_spice.h"
|
||||
#include "kb.h"
|
||||
#include "egl_dynprocs.h"
|
||||
#include "gl_dynprocs.h"
|
||||
@@ -92,9 +85,7 @@ _Static_assert((int)LG_CAPTURE_RGBA32F == (int)LG_TEST_CAPTURE_RGBA32F,
|
||||
static int renderThread(void * unused);
|
||||
|
||||
static LGEvent *e_startup = NULL;
|
||||
static LGEvent *e_spice = NULL;
|
||||
static LGEvent *e_cursorRepaint = NULL;
|
||||
static LGThread *t_spice = NULL;
|
||||
static LGThread *t_render = NULL;
|
||||
static LGThread *t_cursorRepaint = NULL;
|
||||
|
||||
@@ -1500,416 +1491,231 @@ int main_frameThread(void * unused)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void checkUUID(void)
|
||||
static void fallbackSurfaceConfigure(void * opaque,
|
||||
unsigned int width, unsigned int height)
|
||||
{
|
||||
if (!atomic_load_explicit(&g_state.spiceReady, memory_order_acquire) ||
|
||||
!g_state.guestUUIDValid)
|
||||
(void)opaque;
|
||||
g_state.fallbackSurfaceValid = true;
|
||||
g_state.srcSize.x = width;
|
||||
g_state.srcSize.y = height;
|
||||
g_state.haveSrcSize = true;
|
||||
core_updatePositionInfo();
|
||||
|
||||
renderQueue_swSurfaceConfigure(width, height);
|
||||
}
|
||||
|
||||
static void fallbackSurfaceDestroy(void * opaque)
|
||||
{
|
||||
(void)opaque;
|
||||
g_state.fallbackSurfaceValid = false;
|
||||
atomic_store_explicit(&g_state.fallbackDisplayRequested, false,
|
||||
memory_order_release);
|
||||
if (atomic_exchange_explicit(&g_state.fallbackDisplayActive, false,
|
||||
memory_order_acq_rel))
|
||||
{
|
||||
renderQueue_swSurfaceShow(false);
|
||||
lgInput_useTransport(true);
|
||||
overlayStatus_set(LG_USER_STATUS_SPICE, false);
|
||||
}
|
||||
}
|
||||
|
||||
static void fallbackSurfaceDrawFill(void * opaque, int x, int y,
|
||||
int width, int height, uint32_t color)
|
||||
{
|
||||
(void)opaque;
|
||||
renderQueue_swSurfaceDrawFill(x, y, width, height, color);
|
||||
}
|
||||
|
||||
static void fallbackSurfaceDrawBitmap(void * opaque, bool topDown,
|
||||
int x, int y, int width, int height, int stride, const void * data)
|
||||
{
|
||||
(void)opaque;
|
||||
renderQueue_swSurfaceDrawBitmap(
|
||||
x, y, width, height, stride, data, topDown);
|
||||
}
|
||||
|
||||
static void fallbackSurfacePointer(void * opaque,
|
||||
const LG_TransportPointer * pointer)
|
||||
{
|
||||
(void)opaque;
|
||||
|
||||
if (pointer->flags & LG_TRANSPORT_POINTER_SHAPE)
|
||||
{
|
||||
LG_RendererCursor type;
|
||||
switch(pointer->type)
|
||||
{
|
||||
case CURSOR_TYPE_COLOR:
|
||||
type = LG_CURSOR_COLOR;
|
||||
break;
|
||||
|
||||
case CURSOR_TYPE_MONOCHROME:
|
||||
type = LG_CURSOR_MONOCHROME;
|
||||
break;
|
||||
|
||||
case CURSOR_TYPE_MASKED_COLOR:
|
||||
type = LG_CURSOR_MASKED_COLOR;
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t size = (size_t)pointer->pitch * pointer->height;
|
||||
uint8_t * shape = malloc(size);
|
||||
if (!shape)
|
||||
return;
|
||||
memcpy(shape, pointer->shape, size);
|
||||
renderQueue_cursorImage(type, pointer->width, pointer->height,
|
||||
pointer->pitch, shape);
|
||||
}
|
||||
|
||||
if ((pointer->flags & LG_TRANSPORT_POINTER_POSITION) &&
|
||||
(pointer->flags & LG_TRANSPORT_POINTER_VISIBLE_VALID))
|
||||
renderQueue_cursorState(
|
||||
pointer->flags & LG_TRANSPORT_POINTER_VISIBLE,
|
||||
pointer->x, pointer->y, pointer->hx, pointer->hy);
|
||||
}
|
||||
|
||||
static const LG_SwSurfaceEventOps fallbackSurfaceEvents =
|
||||
{
|
||||
.configure = fallbackSurfaceConfigure,
|
||||
.destroy = fallbackSurfaceDestroy,
|
||||
.drawFill = fallbackSurfaceDrawFill,
|
||||
.drawBitmap = fallbackSurfaceDrawBitmap,
|
||||
.pointer = fallbackSurfacePointer,
|
||||
};
|
||||
|
||||
static void fallbackDisconnect(void)
|
||||
{
|
||||
if (!g_state.fallbackTransport.ops)
|
||||
return;
|
||||
|
||||
if (memcmp(g_state.spiceUUID, g_state.guestUUID,
|
||||
sizeof(g_state.spiceUUID)) == 0)
|
||||
const bool connected = g_state.fallbackTransport.handle &&
|
||||
g_state.fallbackTransport.ops->sessionValid(
|
||||
g_state.fallbackTransport.handle);
|
||||
|
||||
atomic_store_explicit(
|
||||
&g_state.fallbackReady, false, memory_order_release);
|
||||
|
||||
if (connected)
|
||||
{
|
||||
lgInput_setFallback(NULL, NULL);
|
||||
lgAudio_setFallback(NULL, NULL);
|
||||
lgClipboard_setFallback(NULL, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
lgInput_dropFallback();
|
||||
lgAudio_dropFallback();
|
||||
lgClipboard_dropFallback();
|
||||
}
|
||||
|
||||
if (g_state.fallbackVideoOps &&
|
||||
g_state.fallbackVideoOps->type == LG_VIDEO_TYPE_SW_SURFACE &&
|
||||
g_state.fallbackVideoOps->swSurface)
|
||||
{
|
||||
if (connected)
|
||||
g_state.fallbackVideoOps->swSurface->setActive(
|
||||
g_state.fallbackTransport.handle, false);
|
||||
g_state.fallbackVideoOps->swSurface->detach(
|
||||
g_state.fallbackTransport.handle);
|
||||
}
|
||||
|
||||
if (atomic_exchange_explicit(&g_state.fallbackDisplayActive, false,
|
||||
memory_order_acq_rel))
|
||||
{
|
||||
renderQueue_swSurfaceShow(false);
|
||||
lgInput_useTransport(true);
|
||||
overlayStatus_set(LG_USER_STATUS_SPICE, false);
|
||||
}
|
||||
|
||||
g_state.fallbackTransport.ops->disconnect(
|
||||
g_state.fallbackTransport.handle);
|
||||
lgTransport_destroy(&g_state.fallbackTransport);
|
||||
g_state.fallbackVideoOps = NULL;
|
||||
g_state.fallbackSurfaceValid = false;
|
||||
}
|
||||
|
||||
static bool fallbackConnect(void)
|
||||
{
|
||||
if (!g_params.useSpice || strcmp(g_params.transport, "spice") == 0)
|
||||
return true;
|
||||
|
||||
if (!lgTransport_create("spice", &g_state.fallbackTransport))
|
||||
{
|
||||
DEBUG_ERROR("Failed to create the SPICE fallback transport");
|
||||
return false;
|
||||
}
|
||||
|
||||
g_state.fallbackVideoOps =
|
||||
g_state.fallbackTransport.ops->getVideoOps(
|
||||
g_state.fallbackTransport.handle);
|
||||
if (!g_state.fallbackVideoOps ||
|
||||
g_state.fallbackVideoOps->type != LG_VIDEO_TYPE_SW_SURFACE ||
|
||||
!g_state.fallbackVideoOps->swSurface ||
|
||||
!g_state.fallbackVideoOps->swSurface->attach(
|
||||
g_state.fallbackTransport.handle, &fallbackSurfaceEvents, NULL))
|
||||
{
|
||||
DEBUG_ERROR("SPICE does not provide a software surface");
|
||||
fallbackDisconnect();
|
||||
return false;
|
||||
}
|
||||
|
||||
const LG_TransportStatus status =
|
||||
g_state.fallbackTransport.ops->connect(
|
||||
g_state.fallbackTransport.handle, &g_state.fallbackSession);
|
||||
if (status != LG_TRANSPORT_OK)
|
||||
{
|
||||
DEBUG_ERROR("Failed to connect the SPICE fallback transport: %d", status);
|
||||
fallbackDisconnect();
|
||||
return false;
|
||||
}
|
||||
|
||||
void * inputOpaque = NULL;
|
||||
const LG_InputOps * inputOps =
|
||||
g_state.fallbackTransport.ops->getInputOps ?
|
||||
g_state.fallbackTransport.ops->getInputOps(
|
||||
g_state.fallbackTransport.handle, &inputOpaque) : NULL;
|
||||
lgInput_setFallback(inputOps, inputOpaque);
|
||||
|
||||
void * audioOpaque = NULL;
|
||||
const LG_AudioOps * audioOps =
|
||||
g_state.fallbackTransport.ops->getAudioOps ?
|
||||
g_state.fallbackTransport.ops->getAudioOps(
|
||||
g_state.fallbackTransport.handle, &audioOpaque) : NULL;
|
||||
lgAudio_setFallback(audioOps, audioOpaque);
|
||||
|
||||
void * clipboardOpaque = NULL;
|
||||
const LG_ClipboardOps * clipboardOps =
|
||||
g_state.fallbackTransport.ops->getClipboardOps ?
|
||||
g_state.fallbackTransport.ops->getClipboardOps(
|
||||
g_state.fallbackTransport.handle, &clipboardOpaque) : NULL;
|
||||
lgClipboard_setFallback(clipboardOps, clipboardOpaque);
|
||||
|
||||
atomic_store_explicit(
|
||||
&g_state.fallbackReady, true, memory_order_release);
|
||||
if (g_state.fallbackSession.name[0])
|
||||
core_setTitle(g_state.fallbackSession.name);
|
||||
if (inputOps)
|
||||
keybind_inputRegister();
|
||||
return true;
|
||||
}
|
||||
|
||||
static void checkUUID(void)
|
||||
{
|
||||
if (!atomic_load_explicit(&g_state.fallbackReady, memory_order_acquire) ||
|
||||
!g_state.fallbackSession.uuidValid || !g_state.guestUUIDValid)
|
||||
return;
|
||||
|
||||
if (memcmp(g_state.fallbackSession.uuid, g_state.guestUUID,
|
||||
sizeof(g_state.guestUUID)) == 0)
|
||||
return;
|
||||
|
||||
app_msgBox(
|
||||
"SPICE Configuration Error",
|
||||
"You have connected SPICE to the wrong guest.\n"
|
||||
"SPICE input will not function until this is corrected.");
|
||||
|
||||
g_params.useSpiceInput = false;
|
||||
lgInput_setFallback(NULL, NULL);
|
||||
lgcSpice_setAvailable(false);
|
||||
lgClipboard_setFallback(NULL, NULL);
|
||||
atomic_store_explicit(&g_state.spiceClose, true, memory_order_release);
|
||||
}
|
||||
|
||||
void spiceReady(void)
|
||||
{
|
||||
atomic_store_explicit(&g_state.spiceReady, true, memory_order_release);
|
||||
if (g_params.useSpiceInput)
|
||||
lgInput_setFallback(&LGI_Spice, NULL);
|
||||
#if ENABLE_AUDIO
|
||||
if (g_params.useSpiceAudio && !g_params.useSpiceUSBAudio)
|
||||
{
|
||||
lgaSpice_setAvailable(true);
|
||||
lgAudio_setFallback(&LGA_Spice, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (atomic_load_explicit(&g_state.spiceDisplayRequested,
|
||||
memory_order_acquire))
|
||||
app_useSpiceDisplay(true);
|
||||
|
||||
// set the intial mouse mode
|
||||
purespice_mouseMode(true);
|
||||
|
||||
PSServerInfo info;
|
||||
if (purespice_getServerInfo(&info))
|
||||
{
|
||||
core_setTitle(info.name);
|
||||
|
||||
bool uuidValid = false;
|
||||
for(int i = 0; i < sizeof(info.uuid); ++i)
|
||||
if (info.uuid[i])
|
||||
{
|
||||
uuidValid = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (uuidValid)
|
||||
{
|
||||
memcpy(g_state.spiceUUID, info.uuid, sizeof(g_state.spiceUUID));
|
||||
checkUUID();
|
||||
}
|
||||
purespice_freeServerInfo(&info);
|
||||
}
|
||||
else
|
||||
DEBUG_WARN("Failed to obtain SPICE server information");
|
||||
|
||||
if (g_params.useSpiceClipboard &&
|
||||
!atomic_load_explicit(&g_state.spiceClose, memory_order_acquire))
|
||||
lgClipboard_setFallback(&LGC_Spice, NULL);
|
||||
|
||||
if (g_params.useSpiceInput)
|
||||
keybind_inputRegister();
|
||||
|
||||
lgSignalEvent(e_spice);
|
||||
}
|
||||
|
||||
static void spice_surfaceCreate(unsigned int surfaceId, PSSurfaceFormat format,
|
||||
unsigned int width, unsigned int height)
|
||||
{
|
||||
if (surfaceId != 0)
|
||||
{
|
||||
DEBUG_INFO("Ignoring secondary SPICE surface: id: %u, size: %ux%u",
|
||||
surfaceId, width, height);
|
||||
return;
|
||||
}
|
||||
|
||||
switch(format)
|
||||
{
|
||||
case PS_SURFACE_FMT_32_xRGB:
|
||||
case PS_SURFACE_FMT_32_ARGB:
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unsupported primary SPICE surface format: %d", format);
|
||||
g_state.spicePrimarySurfaceValid = false;
|
||||
app_useSpiceDisplay(false);
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_INFO("Create primary SPICE surface: id: %u, size: %ux%u",
|
||||
surfaceId, width, height);
|
||||
|
||||
g_state.spicePrimarySurfaceValid = true;
|
||||
|
||||
g_state.srcSize.x = width;
|
||||
g_state.srcSize.y = height;
|
||||
g_state.haveSrcSize = true;
|
||||
core_updatePositionInfo();
|
||||
|
||||
renderQueue_swSurfaceConfigure(width, height);
|
||||
renderQueue_swSurfaceDrawFill(0, 0, width, height, 0x0);
|
||||
}
|
||||
|
||||
static void spice_surfaceDestroy(unsigned int surfaceId)
|
||||
{
|
||||
if (!g_state.spicePrimarySurfaceValid || surfaceId != 0)
|
||||
{
|
||||
DEBUG_INFO("Ignoring destruction of inactive or secondary SPICE surface %u",
|
||||
surfaceId);
|
||||
return;
|
||||
}
|
||||
|
||||
DEBUG_INFO("Destroy primary SPICE surface %u", surfaceId);
|
||||
g_state.spicePrimarySurfaceValid = false;
|
||||
app_useSpiceDisplay(false);
|
||||
}
|
||||
|
||||
static void spice_drawFill(unsigned int surfaceId, int x, int y, int width,
|
||||
int height, uint32_t color)
|
||||
{
|
||||
if (!g_state.spicePrimarySurfaceValid || surfaceId != 0)
|
||||
return;
|
||||
|
||||
renderQueue_swSurfaceDrawFill(x, y, width, height, color);
|
||||
}
|
||||
|
||||
static void spice_drawBitmap(unsigned int surfaceId, PSBitmapFormat format,
|
||||
bool topDown, int x, int y, int width, int height, int stride, void * data)
|
||||
{
|
||||
if (!g_state.spicePrimarySurfaceValid || surfaceId != 0)
|
||||
return;
|
||||
|
||||
switch(format)
|
||||
{
|
||||
case PS_BITMAP_FMT_32BIT:
|
||||
case PS_BITMAP_FMT_RGBA:
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unsupported SPICE bitmap format: %d", format);
|
||||
return;
|
||||
}
|
||||
|
||||
renderQueue_swSurfaceDrawBitmap(x, y, width, height, stride, data, topDown);
|
||||
}
|
||||
|
||||
static void spice_setCursorRGBAImage(int width, int height, int hx, int hy,
|
||||
const void * data)
|
||||
{
|
||||
g_state.spiceHotX = hx;
|
||||
g_state.spiceHotY = hy;
|
||||
|
||||
const uint8_t * rgba = data;
|
||||
uint8_t * bgra = malloc(width * height * 4);
|
||||
for (int i = 0; i < width * height; ++i)
|
||||
{
|
||||
bgra[i * 4 + 0] = rgba[i * 4 + 2];
|
||||
bgra[i * 4 + 1] = rgba[i * 4 + 1];
|
||||
bgra[i * 4 + 2] = rgba[i * 4 + 0];
|
||||
bgra[i * 4 + 3] = rgba[i * 4 + 3];
|
||||
}
|
||||
renderQueue_cursorImage(
|
||||
LG_CURSOR_COLOR, width, height, width * 4, bgra);
|
||||
}
|
||||
|
||||
static void spice_setCursorMonoImage(int width, int height, int hx, int hy,
|
||||
const void * xorMask, const void * andMask)
|
||||
{
|
||||
g_state.spiceHotX = hx;
|
||||
g_state.spiceHotY = hy;
|
||||
|
||||
int stride = (width + 7) / 8;
|
||||
uint8_t * buffer = malloc(stride * height * 2);
|
||||
memcpy(buffer, andMask, stride * height);
|
||||
memcpy(buffer + stride * height, xorMask, stride * height);
|
||||
renderQueue_cursorImage(
|
||||
LG_CURSOR_MONOCHROME, width, height * 2, stride, buffer);
|
||||
}
|
||||
|
||||
static void spice_setCursorColorImage(int width, int height, int hx, int hy,
|
||||
const void * data, const void * maskData)
|
||||
{
|
||||
g_state.spiceHotX = hx;
|
||||
g_state.spiceHotY = hy;
|
||||
|
||||
const uint8_t * rgba = data;
|
||||
const uint8_t * andMask = maskData;
|
||||
const int maskStride = (width + 7) / 8;
|
||||
uint8_t * bgra = malloc(width * height * 4);
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
for (int x = 0; x < width; ++x)
|
||||
{
|
||||
const int i = y * width + x;
|
||||
bgra[i * 4 + 0] = rgba[i * 4 + 2];
|
||||
bgra[i * 4 + 1] = rgba[i * 4 + 1];
|
||||
bgra[i * 4 + 2] = rgba[i * 4 + 0];
|
||||
bgra[i * 4 + 3] =
|
||||
andMask[y * maskStride + x / 8] & (0x80U >> (x % 8)) ? 255 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
renderQueue_cursorImage(
|
||||
LG_CURSOR_MASKED_COLOR, width, height, width * 4, bgra);
|
||||
}
|
||||
|
||||
static void spice_setCursorState(bool visible, int x, int y)
|
||||
{
|
||||
renderQueue_cursorState(visible, x, y, g_state.spiceHotX, g_state.spiceHotY);
|
||||
}
|
||||
|
||||
int spiceThread(void * arg)
|
||||
{
|
||||
#if ENABLE_USB_AUDIO
|
||||
LGA_USBState * usbAudio = NULL;
|
||||
LG_USBRedir * usbRedir = NULL;
|
||||
|
||||
if (g_params.useSpiceAudio && g_params.useSpiceUSBAudio)
|
||||
{
|
||||
if (!lgAudio_supportsPlayback())
|
||||
{
|
||||
DEBUG_WARN("USB audio requires a playback backend, using SPICE audio");
|
||||
g_params.useSpiceUSBAudio = false;
|
||||
}
|
||||
else if (!(usbAudio = lgaUsb_create(g_params.audioDebug)))
|
||||
{
|
||||
DEBUG_WARN("Failed to initialize USB audio, using SPICE audio");
|
||||
g_params.useSpiceUSBAudio = false;
|
||||
}
|
||||
else
|
||||
usbRedir = lgaUsb_redir(usbAudio);
|
||||
}
|
||||
#endif
|
||||
|
||||
const struct PSConfig config =
|
||||
{
|
||||
.host = g_params.spiceHost,
|
||||
.port = g_params.spicePort,
|
||||
.password = "",
|
||||
.ready = spiceReady,
|
||||
.inputs =
|
||||
{
|
||||
.enable = g_params.useSpiceInput,
|
||||
.autoConnect = true
|
||||
},
|
||||
.clipboard =
|
||||
{
|
||||
.enable = g_params.useSpiceClipboard,
|
||||
.notice = lgcSpice_notice,
|
||||
.data = lgcSpice_data,
|
||||
.release = lgcSpice_release,
|
||||
.request = lgcSpice_request,
|
||||
.status = lgcSpice_setAvailable,
|
||||
},
|
||||
.display =
|
||||
{
|
||||
.enable = true,
|
||||
.autoConnect = false,
|
||||
.surfaceCreate = spice_surfaceCreate,
|
||||
.surfaceDestroy = spice_surfaceDestroy,
|
||||
.drawFill = spice_drawFill,
|
||||
.drawBitmap = spice_drawBitmap
|
||||
},
|
||||
.cursor =
|
||||
{
|
||||
.enable = true,
|
||||
.autoConnect = false,
|
||||
.setRGBAImage = spice_setCursorRGBAImage,
|
||||
.setMonoImage = spice_setCursorMonoImage,
|
||||
.setColorImage = spice_setCursorColorImage,
|
||||
.setState = spice_setCursorState,
|
||||
},
|
||||
#if ENABLE_AUDIO
|
||||
.playback =
|
||||
{
|
||||
.enable = g_params.useSpiceAudio &&
|
||||
!g_params.useSpiceUSBAudio && lgAudio_supportsPlayback(),
|
||||
.autoConnect = true,
|
||||
.start = lgaSpice_playbackStart,
|
||||
.volume = lgaSpice_playbackVolume,
|
||||
.mute = lgaSpice_playbackMute,
|
||||
.stop = lgaSpice_playbackStop,
|
||||
.data = lgaSpice_playbackData
|
||||
},
|
||||
.record =
|
||||
{
|
||||
.enable = g_params.useSpiceAudio &&
|
||||
!g_params.useSpiceUSBAudio && lgAudio_supportsRecord(),
|
||||
.autoConnect = true,
|
||||
.start = lgaSpice_recordStart,
|
||||
.volume = lgaSpice_recordVolume,
|
||||
.mute = lgaSpice_recordMute,
|
||||
.stop = lgaSpice_recordStop
|
||||
},
|
||||
#endif
|
||||
#if ENABLE_USB_AUDIO
|
||||
.usbRedir =
|
||||
{
|
||||
.enable = usbAudio != NULL,
|
||||
.autoConnect = false,
|
||||
.opaque = usbRedir,
|
||||
.state = lgUsbRedir_state,
|
||||
.data = lgUsbRedir_data,
|
||||
},
|
||||
#endif
|
||||
};
|
||||
|
||||
bool connected = false;
|
||||
PSStatus status = PS_STATUS_SHUTDOWN;
|
||||
if (!purespice_connect(&config))
|
||||
{
|
||||
DEBUG_ERROR("Failed to connect to spice server");
|
||||
lgSignalEvent(e_spice);
|
||||
goto end;
|
||||
}
|
||||
connected = true;
|
||||
status = PS_STATUS_RUN;
|
||||
|
||||
int processTimeout = 100;
|
||||
#if ENABLE_USB_AUDIO
|
||||
if (usbAudio)
|
||||
{
|
||||
lgAudio_setFallback(&LGA_USB, usbAudio);
|
||||
processTimeout = 10;
|
||||
}
|
||||
#endif
|
||||
|
||||
// process all spice messages
|
||||
while(app_getState() != APP_STATE_SHUTDOWN &&
|
||||
!atomic_load_explicit(&g_state.spiceClose, memory_order_acquire))
|
||||
{
|
||||
#if ENABLE_USB_AUDIO
|
||||
if (usbRedir && !lgUsbRedir_process(usbRedir))
|
||||
DEBUG_WARN("Failed to process USB audio redirection");
|
||||
if (usbAudio)
|
||||
{
|
||||
const uint64_t delay = lgaUsb_processDelayNs(usbAudio);
|
||||
if (delay == UINT64_MAX)
|
||||
processTimeout = 10;
|
||||
else
|
||||
{
|
||||
const uint64_t timeout = delay / UINT64_C(1000000) +
|
||||
(delay % UINT64_C(1000000) != 0);
|
||||
processTimeout = (int)min(timeout, UINT64_C(10));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((status = purespice_process(processTimeout)) != PS_STATUS_RUN)
|
||||
{
|
||||
if (status != PS_STATUS_SHUTDOWN)
|
||||
DEBUG_ERROR("failed to process spice messages");
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
|
||||
lgInput_setFallback(NULL, NULL);
|
||||
lgAudio_setFallback(NULL, NULL);
|
||||
lgcSpice_setAvailable(false);
|
||||
lgClipboard_setFallback(NULL, NULL);
|
||||
#if ENABLE_AUDIO
|
||||
lgaSpice_setAvailable(false);
|
||||
#endif
|
||||
#if ENABLE_USB_AUDIO
|
||||
if (connected && status == PS_STATUS_RUN && usbRedir)
|
||||
{
|
||||
if (!lgUsbRedir_process(usbRedir))
|
||||
DEBUG_WARN("Failed to disconnect USB audio device");
|
||||
else
|
||||
{
|
||||
const uint64_t deadline =
|
||||
microtime() + USB_REDIR_DISCONNECT_TIMEOUT_US;
|
||||
while (lgUsbRedir_disconnectPending(usbRedir) &&
|
||||
microtime() < deadline)
|
||||
{
|
||||
status = purespice_process(10);
|
||||
if (status != PS_STATUS_RUN)
|
||||
break;
|
||||
}
|
||||
|
||||
if (status == PS_STATUS_RUN &&
|
||||
lgUsbRedir_disconnectPending(usbRedir))
|
||||
DEBUG_WARN("Timed out disconnecting USB audio device");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (connected)
|
||||
purespice_disconnect();
|
||||
#if ENABLE_USB_AUDIO
|
||||
lgaUsb_destroy(usbAudio);
|
||||
#endif
|
||||
|
||||
// if the connection was disconnected intentionally we don't want to shutdown
|
||||
// so that the user can see the message box and take action
|
||||
if (!atomic_load_explicit(&g_state.spiceClose, memory_order_acquire))
|
||||
app_setState(APP_STATE_SHUTDOWN);
|
||||
|
||||
lgSignalEvent(e_spice);
|
||||
return 0;
|
||||
"SPICE fallback services will not function until this is corrected.");
|
||||
fallbackDisconnect();
|
||||
}
|
||||
|
||||
void intHandler(int sig)
|
||||
@@ -2139,13 +1945,6 @@ static int lg_run(void)
|
||||
}
|
||||
DEBUG_INFO("Using Video: %s", g_state.videoOps->name);
|
||||
|
||||
// setup the spice startup condition
|
||||
if (!(e_spice = lgCreateEvent(false, 0)))
|
||||
{
|
||||
DEBUG_ERROR("failed to create the spice startup event");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// setup the startup condition
|
||||
if (!(e_startup = lgCreateEvent(false, 0)))
|
||||
{
|
||||
@@ -2164,31 +1963,10 @@ static int lg_run(void)
|
||||
renderQueue_init();
|
||||
frameScheduler_init();
|
||||
|
||||
const PSInit psInit =
|
||||
{
|
||||
.log =
|
||||
{
|
||||
.info = debug_info,
|
||||
.warn = debug_warn,
|
||||
.error = debug_error,
|
||||
}
|
||||
};
|
||||
purespice_init(&psInit);
|
||||
|
||||
g_state.micDefaultState = g_params.micDefaultState;
|
||||
|
||||
if (g_params.useSpice)
|
||||
{
|
||||
if (!lgCreateThread("spiceThread", spiceThread, NULL, &t_spice))
|
||||
{
|
||||
DEBUG_ERROR("spice create thread failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
lgWaitEvent(e_spice, TIMEOUT_INFINITE);
|
||||
if (!atomic_load_explicit(&g_state.spiceReady, memory_order_acquire))
|
||||
return -1;
|
||||
}
|
||||
if (!fallbackConnect())
|
||||
return -1;
|
||||
|
||||
// select and init a renderer
|
||||
bool needsOpenGL = false;
|
||||
@@ -2349,6 +2127,16 @@ restart:
|
||||
|
||||
while(app_getState() == APP_STATE_RUNNING)
|
||||
{
|
||||
if (atomic_load_explicit(
|
||||
&g_state.fallbackReady, memory_order_acquire) &&
|
||||
!g_state.fallbackTransport.ops->sessionValid(
|
||||
g_state.fallbackTransport.handle))
|
||||
{
|
||||
DEBUG_ERROR("SPICE fallback transport disconnected");
|
||||
app_setState(APP_STATE_SHUTDOWN);
|
||||
break;
|
||||
}
|
||||
|
||||
if (initialSpiceEnable && microtime() > initialSpiceEnable)
|
||||
{
|
||||
app_useSpiceDisplay(true);
|
||||
@@ -2523,6 +2311,16 @@ restart:
|
||||
|
||||
while(likely(app_getState() == APP_STATE_RUNNING))
|
||||
{
|
||||
if (atomic_load_explicit(
|
||||
&g_state.fallbackReady, memory_order_acquire) &&
|
||||
!g_state.fallbackTransport.ops->sessionValid(
|
||||
g_state.fallbackTransport.handle))
|
||||
{
|
||||
DEBUG_ERROR("SPICE fallback transport disconnected");
|
||||
app_setState(APP_STATE_SHUTDOWN);
|
||||
break;
|
||||
}
|
||||
|
||||
if (unlikely(!g_state.transport.ops->sessionValid(
|
||||
g_state.transport.handle)))
|
||||
{
|
||||
@@ -2570,8 +2368,7 @@ static void lg_shutdown(void)
|
||||
if (e_cursorRepaint)
|
||||
lgSignalEvent(e_cursorRepaint);
|
||||
|
||||
if (t_spice)
|
||||
lgJoinThread(t_spice, NULL);
|
||||
fallbackDisconnect();
|
||||
|
||||
if (t_render)
|
||||
{
|
||||
@@ -2628,12 +2425,6 @@ static void lg_shutdown(void)
|
||||
e_startup = NULL;
|
||||
}
|
||||
|
||||
if (e_spice)
|
||||
{
|
||||
lgFreeEvent(e_spice);
|
||||
e_startup = NULL;
|
||||
}
|
||||
|
||||
if (g_state.ds)
|
||||
g_state.ds->shutdown();
|
||||
lgClipboard_free();
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
#include "common/event.h"
|
||||
#include "common/ll.h"
|
||||
|
||||
#include <purespice.h>
|
||||
#include "cimgui.h"
|
||||
#include "interface/transport.h"
|
||||
|
||||
@@ -79,12 +78,11 @@ struct AppState
|
||||
bool dsInitialized;
|
||||
bool jitRender;
|
||||
|
||||
uint8_t spiceUUID[16];
|
||||
atomic_bool spiceReady;
|
||||
atomic_bool spiceDisplayRequested;
|
||||
atomic_bool spiceDisplayActive;
|
||||
atomic_bool spiceDisplayTransition;
|
||||
bool spicePrimarySurfaceValid;
|
||||
atomic_bool fallbackReady;
|
||||
atomic_bool fallbackDisplayRequested;
|
||||
atomic_bool fallbackDisplayActive;
|
||||
atomic_bool fallbackDisplayTransition;
|
||||
bool fallbackSurfaceValid;
|
||||
|
||||
uint8_t guestUUID[16];
|
||||
bool guestUUIDValid;
|
||||
@@ -112,8 +110,6 @@ struct AppState
|
||||
LG_RendererRect dstRect;
|
||||
bool posInfoValid;
|
||||
bool alignToGuest;
|
||||
atomic_bool spiceClose;
|
||||
|
||||
atomic_uint_least64_t shaderMousePosition;
|
||||
atomic_uint_least32_t shaderMouseState;
|
||||
|
||||
@@ -127,6 +123,10 @@ struct AppState
|
||||
const LG_VideoOps * videoOps;
|
||||
LG_TransportFeatureFlags transportFeatures;
|
||||
|
||||
LG_TransportInstance fallbackTransport;
|
||||
LG_TransportSession fallbackSession;
|
||||
const LG_VideoOps * fallbackVideoOps;
|
||||
|
||||
LGThread * cursorThread;
|
||||
LGThread * frameThread;
|
||||
LGEvent * frameEvent;
|
||||
@@ -152,8 +152,6 @@ struct AppState
|
||||
|
||||
enum MicDefaultState micDefaultState;
|
||||
|
||||
int spiceHotX, spiceHotY;
|
||||
|
||||
// Set to true when setHDRImageDesc() returns false, indicating
|
||||
// the display server could not apply the HDR image description.
|
||||
// Checked by the renderer to fall back to software tone-mapping.
|
||||
|
||||
@@ -131,7 +131,7 @@ void renderQueue_swSurfaceDrawFill(int x, int y, int width, int height,
|
||||
}
|
||||
|
||||
void renderQueue_swSurfaceDrawBitmap(int x, int y, int width, int height,
|
||||
int stride, void * data, bool topDown)
|
||||
int stride, const void * data, bool topDown)
|
||||
{
|
||||
if (width <= 0 || height <= 0 || stride <= 0)
|
||||
{
|
||||
|
||||
@@ -108,7 +108,7 @@ void renderQueue_swSurfaceDrawFill(int x, int y, int width, int height,
|
||||
uint32_t color);
|
||||
|
||||
void renderQueue_swSurfaceDrawBitmap(int x, int y, int width, int height,
|
||||
int stride, void * data, bool topDown);
|
||||
int stride, const void * data, bool topDown);
|
||||
|
||||
void renderQueue_swSurfaceShow(bool show);
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#define _H_LG_CLIENT_USB_AUDIO_
|
||||
|
||||
#include "interface/audio.h"
|
||||
#include "usbredir.h"
|
||||
#include "interface/usbredir.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -1,466 +0,0 @@
|
||||
/**
|
||||
* 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 "usbredir.h"
|
||||
|
||||
#include "common/debug.h"
|
||||
#include "common/time.h"
|
||||
|
||||
#include <usbredirparser.h>
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define USB_REDIR_CHANNEL_COUNT 256
|
||||
#define USB_REDIR_DISCONNECT_TIMEOUT_NS INT64_C(500000000)
|
||||
#define USB_REDIR_RECONNECT_DELAY_NS INT64_C(250000000)
|
||||
#define USB_REDIR_MAX_OUTPUT_BYTES UINT64_C(1048576)
|
||||
|
||||
struct LG_USBRedir
|
||||
{
|
||||
const LG_USBRedirDeviceOps * deviceOps;
|
||||
void * deviceOpaque;
|
||||
LG_USBRedirStatusFn status;
|
||||
void * statusOpaque;
|
||||
|
||||
PSUSBRedirChannel * channels[USB_REDIR_CHANNEL_COUNT];
|
||||
PSUSBRedirChannel * channel;
|
||||
struct usbredirparser * parser;
|
||||
|
||||
const uint8_t * readData;
|
||||
size_t readSize;
|
||||
size_t readOffset;
|
||||
|
||||
atomic_bool desiredPlugged;
|
||||
atomic_bool available;
|
||||
bool plugged;
|
||||
bool disconnectPending;
|
||||
int64_t disconnectDeadline;
|
||||
int64_t reconnectDeadline;
|
||||
};
|
||||
|
||||
static void setAvailable(LG_USBRedir * usbredir, bool available)
|
||||
{
|
||||
const bool previous = atomic_exchange_explicit(
|
||||
&usbredir->available, available, memory_order_acq_rel);
|
||||
if (previous != available && usbredir->status)
|
||||
usbredir->status(usbredir->statusOpaque, available);
|
||||
}
|
||||
|
||||
static int readUSBRedir(void * opaque, uint8_t * data, int count)
|
||||
{
|
||||
LG_USBRedir * usbredir = opaque;
|
||||
const size_t remaining = usbredir->readSize - usbredir->readOffset;
|
||||
if (remaining < (size_t)count)
|
||||
count = (int)remaining;
|
||||
|
||||
if (!count)
|
||||
return 0;
|
||||
|
||||
memcpy(data, usbredir->readData + usbredir->readOffset, count);
|
||||
usbredir->readOffset += count;
|
||||
return count;
|
||||
}
|
||||
|
||||
static int writeUSBRedir(void * opaque, uint8_t * data, int count)
|
||||
{
|
||||
LG_USBRedir * usbredir = opaque;
|
||||
if (!usbredir->channel ||
|
||||
!purespice_usbRedirWrite(usbredir->channel, data, count))
|
||||
return -1;
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static void logUSBRedir(void * opaque, int level, const char * message)
|
||||
{
|
||||
(void)opaque;
|
||||
switch (level)
|
||||
{
|
||||
case usbredirparser_error:
|
||||
DEBUG_ERROR("USB redirection: %s", message);
|
||||
break;
|
||||
|
||||
case usbredirparser_warning:
|
||||
DEBUG_WARN("USB redirection: %s", message);
|
||||
break;
|
||||
|
||||
case usbredirparser_info:
|
||||
DEBUG_INFO("USB redirection: %s", message);
|
||||
break;
|
||||
|
||||
case usbredirparser_debug:
|
||||
case usbredirparser_debug_data:
|
||||
DEBUG_TRACE("USB redirection: %s", message);
|
||||
break;
|
||||
|
||||
case usbredirparser_none:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void helloUSBRedir(void * opaque,
|
||||
struct usb_redir_hello_header * hello)
|
||||
{
|
||||
(void)hello;
|
||||
LG_USBRedir * usbredir = opaque;
|
||||
setAvailable(usbredir, true);
|
||||
}
|
||||
|
||||
static void deviceDisconnectAck(void * opaque)
|
||||
{
|
||||
LG_USBRedir * usbredir = opaque;
|
||||
usbredir->disconnectPending = false;
|
||||
usbredir->disconnectDeadline = 0;
|
||||
}
|
||||
|
||||
static void unplugDevice(LG_USBRedir * usbredir)
|
||||
{
|
||||
if (!usbredir->plugged)
|
||||
return;
|
||||
|
||||
usbredir->deviceOps->unplug(usbredir->deviceOpaque);
|
||||
usbredir->plugged = false;
|
||||
}
|
||||
|
||||
static void destroyParser(LG_USBRedir * usbredir)
|
||||
{
|
||||
setAvailable(usbredir, false);
|
||||
unplugDevice(usbredir);
|
||||
usbredir->disconnectPending = false;
|
||||
usbredir->disconnectDeadline = 0;
|
||||
|
||||
if (!usbredir->parser)
|
||||
return;
|
||||
|
||||
usbredirparser_destroy(usbredir->parser);
|
||||
usbredir->parser = NULL;
|
||||
}
|
||||
|
||||
static bool flushUSBRedir(LG_USBRedir * usbredir)
|
||||
{
|
||||
if (!usbredir->parser)
|
||||
return true;
|
||||
|
||||
if (usbredirparser_do_write(usbredir->parser) != 0)
|
||||
return false;
|
||||
|
||||
const uint64_t buffered =
|
||||
usbredirparser_get_bufferered_output_size(usbredir->parser);
|
||||
if (buffered <= USB_REDIR_MAX_OUTPUT_BYTES)
|
||||
return true;
|
||||
|
||||
DEBUG_ERROR("USB redirection output queue exceeded %u bytes",
|
||||
(unsigned int)USB_REDIR_MAX_OUTPUT_BYTES);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool connectChannel(LG_USBRedir * usbredir,
|
||||
PSUSBRedirChannel * channel)
|
||||
{
|
||||
usbredir->channel = channel;
|
||||
if (purespice_usbRedirConnect(channel))
|
||||
return true;
|
||||
|
||||
DEBUG_WARN("Failed to connect USB redirection channel %u",
|
||||
purespice_usbRedirChannelId(channel));
|
||||
usbredir->channel = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool selectChannel(LG_USBRedir * usbredir)
|
||||
{
|
||||
if (usbredir->channel)
|
||||
return true;
|
||||
|
||||
for (unsigned int i = 0; i < USB_REDIR_CHANNEL_COUNT; ++i)
|
||||
{
|
||||
PSUSBRedirChannel * channel = usbredir->channels[i];
|
||||
if (!channel || !purespice_usbRedirAvailable(channel))
|
||||
continue;
|
||||
|
||||
if (connectChannel(usbredir, channel))
|
||||
{
|
||||
usbredir->reconnectDeadline = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
usbredir->reconnectDeadline =
|
||||
(int64_t)nanotime() + USB_REDIR_RECONNECT_DELAY_NS;
|
||||
return false;
|
||||
}
|
||||
|
||||
static void resetChannel(LG_USBRedir * usbredir)
|
||||
{
|
||||
PSUSBRedirChannel * channel = usbredir->channel;
|
||||
usbredir->channel = NULL;
|
||||
destroyParser(usbredir);
|
||||
|
||||
if (channel && purespice_usbRedirConnected(channel))
|
||||
purespice_usbRedirDisconnect(channel);
|
||||
|
||||
usbredir->reconnectDeadline =
|
||||
(int64_t)nanotime() + USB_REDIR_RECONNECT_DELAY_NS;
|
||||
}
|
||||
|
||||
static bool createParser(LG_USBRedir * usbredir)
|
||||
{
|
||||
if (usbredir->parser)
|
||||
return true;
|
||||
|
||||
usbredir->parser = usbredirparser_create();
|
||||
if (!usbredir->parser)
|
||||
{
|
||||
DEBUG_ERROR("Failed to create the USB redirection parser");
|
||||
return false;
|
||||
}
|
||||
|
||||
usbredir->deviceOps->setup(usbredir->deviceOpaque, usbredir->parser);
|
||||
usbredir->parser->priv = usbredir;
|
||||
usbredir->parser->log_func = logUSBRedir;
|
||||
usbredir->parser->read_func = readUSBRedir;
|
||||
usbredir->parser->write_func = writeUSBRedir;
|
||||
usbredir->parser->hello_func = helloUSBRedir;
|
||||
usbredir->parser->device_disconnect_ack_func = deviceDisconnectAck;
|
||||
|
||||
uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0 };
|
||||
usbredirparser_caps_set_cap(caps,
|
||||
usb_redir_cap_device_disconnect_ack);
|
||||
usbredirparser_caps_set_cap(caps,
|
||||
usb_redir_cap_connect_device_version);
|
||||
usbredirparser_caps_set_cap(caps,
|
||||
usb_redir_cap_ep_info_max_packet_size);
|
||||
usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids);
|
||||
usbredirparser_caps_set_cap(caps, usb_redir_cap_32bits_bulk_length);
|
||||
usbredirparser_init(usbredir->parser, "Looking Glass", caps,
|
||||
USB_REDIR_CAPS_SIZE, usbredirparser_fl_usb_host);
|
||||
|
||||
if (flushUSBRedir(usbredir))
|
||||
return true;
|
||||
|
||||
DEBUG_ERROR("Failed to send the USB redirection hello packet");
|
||||
destroyParser(usbredir);
|
||||
return false;
|
||||
}
|
||||
|
||||
LG_USBRedir * lgUsbRedir_create(
|
||||
const LG_USBRedirDeviceOps * deviceOps, void * deviceOpaque,
|
||||
LG_USBRedirStatusFn status, void * statusOpaque)
|
||||
{
|
||||
if (!deviceOps || !deviceOps->setup || !deviceOps->plug ||
|
||||
!deviceOps->unplug)
|
||||
return NULL;
|
||||
|
||||
LG_USBRedir * usbredir = calloc(1, sizeof(*usbredir));
|
||||
if (!usbredir)
|
||||
return NULL;
|
||||
|
||||
usbredir->deviceOps = deviceOps;
|
||||
usbredir->deviceOpaque = deviceOpaque;
|
||||
usbredir->status = status;
|
||||
usbredir->statusOpaque = statusOpaque;
|
||||
atomic_init(&usbredir->desiredPlugged, false);
|
||||
atomic_init(&usbredir->available, false);
|
||||
return usbredir;
|
||||
}
|
||||
|
||||
void lgUsbRedir_destroy(LG_USBRedir * usbredir)
|
||||
{
|
||||
if (!usbredir)
|
||||
return;
|
||||
|
||||
destroyParser(usbredir);
|
||||
free(usbredir);
|
||||
}
|
||||
|
||||
void lgUsbRedir_setPlugged(LG_USBRedir * usbredir, bool plugged)
|
||||
{
|
||||
atomic_store_explicit(&usbredir->desiredPlugged, plugged,
|
||||
memory_order_release);
|
||||
}
|
||||
|
||||
bool lgUsbRedir_available(const LG_USBRedir * usbredir)
|
||||
{
|
||||
return atomic_load_explicit(&usbredir->available, memory_order_acquire);
|
||||
}
|
||||
|
||||
bool lgUsbRedir_disconnectPending(const LG_USBRedir * usbredir)
|
||||
{
|
||||
return usbredir->disconnectPending;
|
||||
}
|
||||
|
||||
static bool processUSBRedir(LG_USBRedir * usbredir, bool recover)
|
||||
{
|
||||
const int64_t now = (int64_t)nanotime();
|
||||
if (!usbredir->channel &&
|
||||
now >= usbredir->reconnectDeadline)
|
||||
selectChannel(usbredir);
|
||||
|
||||
if (!usbredir->parser ||
|
||||
!atomic_load_explicit(&usbredir->available, memory_order_acquire))
|
||||
{
|
||||
const bool result = flushUSBRedir(usbredir);
|
||||
if (!result && recover)
|
||||
resetChannel(usbredir);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (usbredir->disconnectPending)
|
||||
{
|
||||
if (usbredir->disconnectDeadline &&
|
||||
now >= usbredir->disconnectDeadline)
|
||||
{
|
||||
DEBUG_WARN("USB redirection disconnect acknowledgement timed out");
|
||||
resetChannel(usbredir);
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool result = flushUSBRedir(usbredir);
|
||||
if (!result && recover)
|
||||
resetChannel(usbredir);
|
||||
return result;
|
||||
}
|
||||
|
||||
const bool desired = atomic_load_explicit(&usbredir->desiredPlugged,
|
||||
memory_order_acquire);
|
||||
if (desired != usbredir->plugged)
|
||||
{
|
||||
if (desired)
|
||||
{
|
||||
usbredir->deviceOps->plug(
|
||||
usbredir->deviceOpaque, usbredir->parser);
|
||||
usbredir->plugged = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
unplugDevice(usbredir);
|
||||
usbredir->disconnectPending = usbredirparser_peer_has_cap(
|
||||
usbredir->parser, usb_redir_cap_device_disconnect_ack);
|
||||
usbredir->disconnectDeadline = usbredir->disconnectPending ?
|
||||
now + USB_REDIR_DISCONNECT_TIMEOUT_NS : 0;
|
||||
usbredirparser_send_device_disconnect(usbredir->parser);
|
||||
}
|
||||
}
|
||||
|
||||
if (usbredir->plugged && usbredir->deviceOps->process)
|
||||
usbredir->deviceOps->process(usbredir->deviceOpaque);
|
||||
|
||||
const bool result = flushUSBRedir(usbredir);
|
||||
if (!result && recover)
|
||||
resetChannel(usbredir);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool lgUsbRedir_process(LG_USBRedir * usbredir)
|
||||
{
|
||||
return processUSBRedir(usbredir, true);
|
||||
}
|
||||
|
||||
void lgUsbRedir_state(PSUSBRedirChannel * channel,
|
||||
PSUSBRedirState state, void * opaque)
|
||||
{
|
||||
LG_USBRedir * usbredir = opaque;
|
||||
const uint8_t id = purespice_usbRedirChannelId(channel);
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case PS_USB_REDIR_AVAILABLE:
|
||||
usbredir->channels[id] = channel;
|
||||
selectChannel(usbredir);
|
||||
break;
|
||||
|
||||
case PS_USB_REDIR_CONNECTED:
|
||||
if (channel != usbredir->channel)
|
||||
{
|
||||
purespice_usbRedirDisconnect(channel);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!createParser(usbredir))
|
||||
purespice_usbRedirDisconnect(channel);
|
||||
break;
|
||||
|
||||
case PS_USB_REDIR_DISCONNECTED:
|
||||
if (channel == usbredir->channel)
|
||||
{
|
||||
destroyParser(usbredir);
|
||||
usbredir->channel = NULL;
|
||||
if (purespice_usbRedirAvailable(channel))
|
||||
usbredir->reconnectDeadline =
|
||||
(int64_t)nanotime() + USB_REDIR_RECONNECT_DELAY_NS;
|
||||
}
|
||||
break;
|
||||
|
||||
case PS_USB_REDIR_UNAVAILABLE:
|
||||
if (usbredir->channels[id] == channel)
|
||||
usbredir->channels[id] = NULL;
|
||||
|
||||
if (channel == usbredir->channel)
|
||||
{
|
||||
destroyParser(usbredir);
|
||||
usbredir->channel = NULL;
|
||||
selectChannel(usbredir);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool lgUsbRedir_data(PSUSBRedirChannel * channel,
|
||||
const uint8_t * data, size_t size, void * opaque)
|
||||
{
|
||||
LG_USBRedir * usbredir = opaque;
|
||||
if (channel != usbredir->channel || !usbredir->parser ||
|
||||
usbredir->readData)
|
||||
return false;
|
||||
|
||||
usbredir->readData = data;
|
||||
usbredir->readSize = size;
|
||||
usbredir->readOffset = 0;
|
||||
|
||||
const int result = usbredirparser_do_read(usbredir->parser);
|
||||
const bool consumed = usbredir->readOffset == usbredir->readSize;
|
||||
usbredir->readData = NULL;
|
||||
usbredir->readSize = 0;
|
||||
usbredir->readOffset = 0;
|
||||
|
||||
if (result == usbredirparser_read_parse_error)
|
||||
{
|
||||
DEBUG_ERROR("Invalid USB redirection packet");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!consumed)
|
||||
{
|
||||
DEBUG_ERROR("USB redirection parser did not consume its input");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Returning false lets PureSpice disconnect after this callback unwinds;
|
||||
* resetting it here would destroy the channel during its own dispatch. */
|
||||
return processUSBRedir(usbredir, false);
|
||||
}
|
||||
|
||||
void * lgUsbRedir_device(void * parserOpaque)
|
||||
{
|
||||
LG_USBRedir * usbredir = parserOpaque;
|
||||
return usbredir->deviceOpaque;
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef _H_LG_CLIENT_USBREDIR_
|
||||
#define _H_LG_CLIENT_USBREDIR_
|
||||
|
||||
#include <purespice.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct usbredirparser;
|
||||
|
||||
typedef struct LG_USBRedir LG_USBRedir;
|
||||
|
||||
/* Availability transitions are delivered on the PureSpice process thread. */
|
||||
typedef void (*LG_USBRedirStatusFn)(void * opaque, bool available);
|
||||
|
||||
typedef struct LG_USBRedirDeviceOps
|
||||
{
|
||||
/* Install the device packet callbacks on a newly-created parser. */
|
||||
void (*setup)(void * opaque, struct usbredirparser * parser);
|
||||
|
||||
/* Queue the device descriptors and connection announcement. */
|
||||
void (*plug)(void * opaque, struct usbredirparser * parser);
|
||||
|
||||
/* Queue time-sensitive device data. This runs on the PureSpice processing
|
||||
* thread immediately before parser output is flushed. */
|
||||
void (*process)(void * opaque);
|
||||
|
||||
/* Stop all device activity. The bridge sends the disconnect packet. */
|
||||
void (*unplug)(void * opaque);
|
||||
}
|
||||
LG_USBRedirDeviceOps;
|
||||
|
||||
LG_USBRedir * lgUsbRedir_create(
|
||||
const LG_USBRedirDeviceOps * deviceOps, void * deviceOpaque,
|
||||
LG_USBRedirStatusFn status, void * statusOpaque);
|
||||
/* The PureSpice session must be stopped before destroying the bridge. */
|
||||
void lgUsbRedir_destroy(LG_USBRedir * usbredir);
|
||||
|
||||
/* This may be called from another thread. The request is applied by
|
||||
* lgUsbRedir_process on the PureSpice processing thread. */
|
||||
void lgUsbRedir_setPlugged(LG_USBRedir * usbredir, bool plugged);
|
||||
bool lgUsbRedir_available(const LG_USBRedir * usbredir);
|
||||
/* True after a device disconnect has been sent and until the guest has
|
||||
* completed the detach. This must be queried on the processing thread. */
|
||||
bool lgUsbRedir_disconnectPending(const LG_USBRedir * usbredir);
|
||||
|
||||
/* Apply pending device state and flush parser output. This must be called on
|
||||
* the PureSpice processing thread. */
|
||||
bool lgUsbRedir_process(LG_USBRedir * usbredir);
|
||||
|
||||
void lgUsbRedir_state(PSUSBRedirChannel * channel,
|
||||
PSUSBRedirState state, void * opaque);
|
||||
bool lgUsbRedir_data(PSUSBRedirChannel * channel,
|
||||
const uint8_t * data, size_t size, void * opaque);
|
||||
|
||||
/* Parser callbacks receive the bridge as their opaque value. */
|
||||
void * lgUsbRedir_device(void * parserOpaque);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user