Apply clang-format across the whole tree
Run clang-format (the repo's .clang-format: LLVM base, 120 cols, tabs, Allman functions) over every source file so the tree is formatter-clean. Whitespace only -- no behavior change; full x64 + x86 suites pass. Also set SortIncludes: false in .clang-format. Windows include order is load-bearing (windows.h must precede tlhelp32.h / mmreg.h / xinput.h / dinput.h; winsock2.h must precede windows.h), and the default alphabetical sort reorders tlhelp32.h ahead of windows.h -- a build break. Leaving order alone keeps the manual, correct grouping.
This commit is contained in:
@@ -16,4 +16,9 @@ AlwaysBreakTemplateDeclarations: true
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
PointerAlignment: Left
|
||||
|
||||
# Windows include order is load-bearing (windows.h must precede tlhelp32.h / mmreg.h /
|
||||
# xinput.h / dinput.h; winsock2.h must precede windows.h), so leave include order alone
|
||||
# rather than let an alphabetical sort break the build.
|
||||
SortIncludes: false
|
||||
...
|
||||
|
||||
@@ -24,8 +24,7 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// The standard sample rates a shared-mode WASAPI stream realistically uses. Candidates are this
|
||||
// set; a non-standard true rate is out of scope (and would show as low-confidence either way).
|
||||
@@ -35,30 +34,25 @@ inline const std::vector<unsigned>& standard_audio_rates()
|
||||
return rates;
|
||||
}
|
||||
|
||||
struct RateCorrelation
|
||||
{
|
||||
struct RateCorrelation {
|
||||
bool ok = false; // a confident pick was made (winner clears the threshold AND beats the runner-up)
|
||||
unsigned rate = 0; // best candidate rate (Hz)
|
||||
double score = 0.0; // alignment score of the winner, in [0,1] (1 = perfect)
|
||||
double runner_up = 0.0; // score of the second-best candidate (for separation)
|
||||
};
|
||||
|
||||
namespace correlate_detail
|
||||
{
|
||||
namespace correlate_detail {
|
||||
|
||||
// Average interleaved float frames down to a single mono channel.
|
||||
inline void downmix(const float* interleaved, std::size_t frames, unsigned channels, std::vector<float>& out)
|
||||
{
|
||||
out.resize(frames);
|
||||
if (channels == 0)
|
||||
{
|
||||
if (channels == 0) {
|
||||
channels = 1;
|
||||
}
|
||||
for (std::size_t i = 0; i < frames; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < frames; ++i) {
|
||||
float sum = 0.0f;
|
||||
for (unsigned c = 0; c < channels; ++c)
|
||||
{
|
||||
for (unsigned c = 0; c < channels; ++c) {
|
||||
sum += interleaved[i * channels + c];
|
||||
}
|
||||
out[i] = sum / static_cast<float>(channels);
|
||||
@@ -66,24 +60,20 @@ inline void downmix(const float* interleaved, std::size_t frames, unsigned chann
|
||||
}
|
||||
|
||||
// Linear-resample a mono signal from src_rate to dst_rate.
|
||||
inline void resample_linear(const std::vector<float>& in, unsigned src_rate, unsigned dst_rate,
|
||||
std::vector<float>& out)
|
||||
{
|
||||
if (src_rate == 0 || dst_rate == 0 || in.empty())
|
||||
inline void resample_linear(const std::vector<float>& in, unsigned src_rate, unsigned dst_rate, std::vector<float>& out)
|
||||
{
|
||||
if (src_rate == 0 || dst_rate == 0 || in.empty()) {
|
||||
out.clear();
|
||||
return;
|
||||
}
|
||||
if (src_rate == dst_rate)
|
||||
{
|
||||
if (src_rate == dst_rate) {
|
||||
out = in;
|
||||
return;
|
||||
}
|
||||
const double step = static_cast<double>(src_rate) / static_cast<double>(dst_rate);
|
||||
const std::size_t out_n = static_cast<std::size_t>(static_cast<double>(in.size()) / step);
|
||||
out.resize(out_n);
|
||||
for (std::size_t i = 0; i < out_n; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < out_n; ++i) {
|
||||
const double pos = static_cast<double>(i) * step;
|
||||
const std::size_t j = static_cast<std::size_t>(pos);
|
||||
const double frac = pos - static_cast<double>(j);
|
||||
@@ -97,29 +87,24 @@ inline void resample_linear(const std::vector<float>& in, unsigned src_rate, uns
|
||||
// alignment search (the envelope/content alignment doesn't need full bandwidth).
|
||||
inline void decimate(const std::vector<float>& in, unsigned rate, unsigned corr_rate, std::vector<float>& out)
|
||||
{
|
||||
if (rate <= corr_rate || in.empty())
|
||||
{
|
||||
if (rate <= corr_rate || in.empty()) {
|
||||
out = in;
|
||||
return;
|
||||
}
|
||||
const double factor = static_cast<double>(rate) / static_cast<double>(corr_rate);
|
||||
const std::size_t out_n = static_cast<std::size_t>(static_cast<double>(in.size()) / factor);
|
||||
out.resize(out_n);
|
||||
for (std::size_t i = 0; i < out_n; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < out_n; ++i) {
|
||||
const std::size_t lo = static_cast<std::size_t>(static_cast<double>(i) * factor);
|
||||
std::size_t hi = static_cast<std::size_t>(static_cast<double>(i + 1) * factor);
|
||||
if (hi <= lo)
|
||||
{
|
||||
if (hi <= lo) {
|
||||
hi = lo + 1;
|
||||
}
|
||||
if (hi > in.size())
|
||||
{
|
||||
if (hi > in.size()) {
|
||||
hi = in.size();
|
||||
}
|
||||
float sum = 0.0f;
|
||||
for (std::size_t k = lo; k < hi; ++k)
|
||||
{
|
||||
for (std::size_t k = lo; k < hi; ++k) {
|
||||
sum += in[k];
|
||||
}
|
||||
out[i] = sum / static_cast<float>(hi - lo);
|
||||
@@ -133,29 +118,24 @@ inline double ncc(const std::vector<float>& a, const std::vector<float>& b, long
|
||||
{
|
||||
double sa = 0.0, sb = 0.0;
|
||||
std::size_t n = 0;
|
||||
for (std::size_t i = start; i < start + len && i < a.size(); ++i)
|
||||
{
|
||||
for (std::size_t i = start; i < start + len && i < a.size(); ++i) {
|
||||
const long bi = static_cast<long>(i) + lag;
|
||||
if (bi < 0 || static_cast<std::size_t>(bi) >= b.size())
|
||||
{
|
||||
if (bi < 0 || static_cast<std::size_t>(bi) >= b.size()) {
|
||||
continue;
|
||||
}
|
||||
sa += a[i];
|
||||
sb += b[bi];
|
||||
++n;
|
||||
}
|
||||
if (n < 8)
|
||||
{
|
||||
if (n < 8) {
|
||||
return 0.0;
|
||||
}
|
||||
const double ma = sa / static_cast<double>(n);
|
||||
const double mb = sb / static_cast<double>(n);
|
||||
double num = 0.0, da = 0.0, db = 0.0;
|
||||
for (std::size_t i = start; i < start + len && i < a.size(); ++i)
|
||||
{
|
||||
for (std::size_t i = start; i < start + len && i < a.size(); ++i) {
|
||||
const long bi = static_cast<long>(i) + lag;
|
||||
if (bi < 0 || static_cast<std::size_t>(bi) >= b.size())
|
||||
{
|
||||
if (bi < 0 || static_cast<std::size_t>(bi) >= b.size()) {
|
||||
continue;
|
||||
}
|
||||
const double xa = a[i] - ma;
|
||||
@@ -164,8 +144,7 @@ inline double ncc(const std::vector<float>& a, const std::vector<float>& b, long
|
||||
da += xa * xa;
|
||||
db += xb * xb;
|
||||
}
|
||||
if (da < 1e-9 || db < 1e-9)
|
||||
{
|
||||
if (da < 1e-9 || db < 1e-9) {
|
||||
return 0.0;
|
||||
}
|
||||
return num / std::sqrt(da * db);
|
||||
@@ -188,11 +167,9 @@ inline double aligned_score(const std::vector<float>& a, const std::vector<float
|
||||
const std::size_t mid_len = n / 2;
|
||||
double best = -2.0;
|
||||
long best_lag = 0;
|
||||
for (long lag = -max_lag; lag <= max_lag; ++lag)
|
||||
{
|
||||
for (long lag = -max_lag; lag <= max_lag; ++lag) {
|
||||
const double c = ncc(a, b, lag, mid_start, mid_len);
|
||||
if (c > best)
|
||||
{
|
||||
if (c > best) {
|
||||
best = c;
|
||||
best_lag = lag;
|
||||
}
|
||||
@@ -218,8 +195,7 @@ inline RateCorrelation correlate_rate(const std::vector<float>& hook_mono, const
|
||||
{
|
||||
using namespace correlate_detail;
|
||||
RateCorrelation result;
|
||||
if (hook_mono.empty() || loop_mono.empty() || device_rate == 0)
|
||||
{
|
||||
if (hook_mono.empty() || loop_mono.empty() || device_rate == 0) {
|
||||
return result;
|
||||
}
|
||||
constexpr unsigned kCorrRate = 8000; // alignment search rate (Nyquist 4 kHz -- plenty for content)
|
||||
@@ -230,19 +206,15 @@ inline RateCorrelation correlate_rate(const std::vector<float>& hook_mono, const
|
||||
double best = -1.0, second = -1.0;
|
||||
unsigned best_rate = 0;
|
||||
std::vector<float> resampled, hook_ds;
|
||||
for (unsigned cand : candidates)
|
||||
{
|
||||
for (unsigned cand : candidates) {
|
||||
resample_linear(hook_mono, cand, device_rate, resampled); // treat hook as sampled at `cand`
|
||||
decimate(resampled, device_rate, kCorrRate, hook_ds);
|
||||
const double s = aligned_score(hook_ds, loop_ds, kCorrRate);
|
||||
if (s > best)
|
||||
{
|
||||
if (s > best) {
|
||||
second = best;
|
||||
best = s;
|
||||
best_rate = cand;
|
||||
}
|
||||
else if (s > second)
|
||||
{
|
||||
} else if (s > second) {
|
||||
second = s;
|
||||
}
|
||||
}
|
||||
@@ -250,7 +222,8 @@ inline RateCorrelation correlate_rate(const std::vector<float>& hook_mono, const
|
||||
result.rate = best_rate;
|
||||
result.score = best < 0.0 ? 0.0 : best;
|
||||
result.runner_up = second < 0.0 ? 0.0 : second;
|
||||
result.ok = result.score >= min_score && (result.runner_up <= 1e-6 || result.score >= result.runner_up * separation);
|
||||
result.ok =
|
||||
result.score >= min_score && (result.runner_up <= 1e-6 || result.score >= result.runner_up * separation);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -266,8 +239,7 @@ inline RateCorrelation correlate_rate(const std::vector<float>& hook_mono, const
|
||||
inline constexpr unsigned kWaveFormatPcm = 1; // WAVE_FORMAT_PCM
|
||||
inline constexpr unsigned kWaveFormatFloat = 3; // WAVE_FORMAT_IEEE_FLOAT
|
||||
|
||||
struct LayoutCandidate
|
||||
{
|
||||
struct LayoutCandidate {
|
||||
unsigned channels;
|
||||
unsigned bits;
|
||||
unsigned tag; // kWaveFormatPcm / kWaveFormatFloat
|
||||
@@ -278,16 +250,14 @@ struct LayoutCandidate
|
||||
inline const std::vector<LayoutCandidate>& standard_audio_layouts()
|
||||
{
|
||||
static const std::vector<LayoutCandidate> v = {
|
||||
{2, 32, kWaveFormatFloat}, {1, 32, kWaveFormatFloat}, {6, 32, kWaveFormatFloat},
|
||||
{8, 32, kWaveFormatFloat}, {4, 32, kWaveFormatFloat}, {2, 16, kWaveFormatPcm},
|
||||
{1, 16, kWaveFormatPcm}, {6, 16, kWaveFormatPcm}, {8, 16, kWaveFormatPcm},
|
||||
{4, 16, kWaveFormatPcm},
|
||||
{2, 32, kWaveFormatFloat}, {1, 32, kWaveFormatFloat}, {6, 32, kWaveFormatFloat}, {8, 32, kWaveFormatFloat},
|
||||
{4, 32, kWaveFormatFloat}, {2, 16, kWaveFormatPcm}, {1, 16, kWaveFormatPcm}, {6, 16, kWaveFormatPcm},
|
||||
{8, 16, kWaveFormatPcm}, {4, 16, kWaveFormatPcm},
|
||||
};
|
||||
return v;
|
||||
}
|
||||
|
||||
struct FormatCorrelation
|
||||
{
|
||||
struct FormatCorrelation {
|
||||
bool ok = false;
|
||||
unsigned rate = 0;
|
||||
unsigned channels = 0;
|
||||
@@ -302,15 +272,13 @@ struct FormatCorrelation
|
||||
// padding is stale staging-buffer bytes, so the host must extract the real `count*real_block` bytes
|
||||
// per buffer (and concatenate) before de-interleaving -- otherwise the padding scrambles the audio.
|
||||
// This carries that self-describing capture: `bytes` holds counts[i]*stride bytes per chunk.
|
||||
struct ChunkedCapture
|
||||
{
|
||||
struct ChunkedCapture {
|
||||
unsigned stride = 0; // bytes per frame as pushed (the guessed/device block_align)
|
||||
std::vector<std::uint32_t> counts; // real frame count of each chunk
|
||||
std::vector<std::uint8_t> bytes; // concatenated, counts[i]*stride bytes per chunk
|
||||
};
|
||||
|
||||
namespace correlate_detail
|
||||
{
|
||||
namespace correlate_detail {
|
||||
// De-interleave raw bytes under (channels/bits/tag) and average to mono float.
|
||||
inline void decode_layout(const std::uint8_t* bytes, std::size_t n, const LayoutCandidate& fmt,
|
||||
std::vector<float>& mono)
|
||||
@@ -318,33 +286,25 @@ inline void decode_layout(const std::uint8_t* bytes, std::size_t n, const Layout
|
||||
mono.clear();
|
||||
const unsigned ch = fmt.channels == 0 ? 1 : fmt.channels;
|
||||
const unsigned bps = fmt.bits / 8;
|
||||
if (bps == 0)
|
||||
{
|
||||
if (bps == 0) {
|
||||
return;
|
||||
}
|
||||
const std::size_t frame = static_cast<std::size_t>(ch) * bps;
|
||||
const std::size_t frames = n / frame;
|
||||
mono.resize(frames);
|
||||
const bool is_float = fmt.tag == kWaveFormatFloat;
|
||||
for (std::size_t i = 0; i < frames; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < frames; ++i) {
|
||||
double sum = 0.0;
|
||||
for (unsigned c = 0; c < ch; ++c)
|
||||
{
|
||||
for (unsigned c = 0; c < ch; ++c) {
|
||||
const std::uint8_t* p = bytes + i * frame + static_cast<std::size_t>(c) * bps;
|
||||
float s = 0.0f;
|
||||
if (is_float && fmt.bits == 32)
|
||||
{
|
||||
if (is_float && fmt.bits == 32) {
|
||||
std::memcpy(&s, p, 4);
|
||||
}
|
||||
else if (fmt.bits == 16)
|
||||
{
|
||||
} else if (fmt.bits == 16) {
|
||||
std::int16_t v;
|
||||
std::memcpy(&v, p, 2);
|
||||
s = v / 32768.0f;
|
||||
}
|
||||
else if (fmt.bits == 32)
|
||||
{
|
||||
} else if (fmt.bits == 32) {
|
||||
std::int32_t v;
|
||||
std::memcpy(&v, p, 4);
|
||||
s = static_cast<float>(v / 2147483648.0);
|
||||
@@ -372,58 +332,50 @@ inline FormatCorrelation correlate_format(const ChunkedCapture& hook, const std:
|
||||
double min_margin = 0.04)
|
||||
{
|
||||
FormatCorrelation result;
|
||||
if (hook.stride == 0 || hook.counts.empty() || loop_mono.empty() || device_rate == 0)
|
||||
{
|
||||
if (hook.stride == 0 || hook.counts.empty() || loop_mono.empty() || device_rate == 0) {
|
||||
return result;
|
||||
}
|
||||
double best = -1.0, second = -1.0;
|
||||
std::vector<std::uint8_t> clean;
|
||||
std::vector<float> hook_mono;
|
||||
for (const LayoutCandidate& layout : layouts)
|
||||
{
|
||||
for (const LayoutCandidate& layout : layouts) {
|
||||
const unsigned real_block = layout.channels * (layout.bits / 8);
|
||||
if (real_block == 0 || real_block > hook.stride)
|
||||
{
|
||||
if (real_block == 0 || real_block > hook.stride) {
|
||||
continue; // can't extract a frame larger than what was pushed (the guess is the max)
|
||||
}
|
||||
// Pull the real count*real_block bytes out of each padded chunk and concatenate -> contiguous
|
||||
// audio for this candidate layout (the padding, which is stale staging bytes, is dropped).
|
||||
clean.clear();
|
||||
std::size_t off = 0;
|
||||
for (std::uint32_t count : hook.counts)
|
||||
{
|
||||
for (std::uint32_t count : hook.counts) {
|
||||
const std::size_t chunk_bytes = static_cast<std::size_t>(count) * hook.stride;
|
||||
const std::size_t take = static_cast<std::size_t>(count) * real_block;
|
||||
if (off + chunk_bytes <= hook.bytes.size())
|
||||
{
|
||||
if (off + chunk_bytes <= hook.bytes.size()) {
|
||||
clean.insert(clean.end(), hook.bytes.begin() + off, hook.bytes.begin() + off + take);
|
||||
}
|
||||
off += chunk_bytes;
|
||||
}
|
||||
correlate_detail::decode_layout(clean.data(), clean.size(), layout, hook_mono);
|
||||
if (hook_mono.size() < device_rate / 5)
|
||||
{
|
||||
if (hook_mono.size() < device_rate / 5) {
|
||||
continue; // this layout yields too little audio to judge
|
||||
}
|
||||
const RateCorrelation rc = correlate_rate(hook_mono, loop_mono, device_rate, rates, /*min_score=*/0.0,
|
||||
/*separation=*/1.0);
|
||||
if (rc.score > best)
|
||||
{
|
||||
if (rc.score > best) {
|
||||
second = best;
|
||||
best = rc.score;
|
||||
result.rate = rc.rate;
|
||||
result.channels = layout.channels;
|
||||
result.bits = layout.bits;
|
||||
result.tag = layout.tag;
|
||||
}
|
||||
else if (rc.score > second)
|
||||
{
|
||||
} else if (rc.score > second) {
|
||||
second = rc.score;
|
||||
}
|
||||
}
|
||||
result.score = best < 0.0 ? 0.0 : best;
|
||||
result.runner_up = second < 0.0 ? 0.0 : second;
|
||||
result.ok = result.score >= min_score && (result.runner_up <= 1e-6 || result.score - result.runner_up >= min_margin);
|
||||
result.ok =
|
||||
result.score >= min_score && (result.runner_up <= 1e-6 || result.score - result.runner_up >= min_margin);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,7 @@
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// 'AURG' little-endian; sanity-checks the mapping before either side trusts it.
|
||||
inline constexpr std::uint32_t kAudioRingMagic = 0x47525541u;
|
||||
@@ -30,8 +29,7 @@ inline constexpr std::uint32_t kAudioRingVersion = 2;
|
||||
// fields in the header. The host writes the fields then bumps op_seq; the hook applies
|
||||
// the command once per new op_seq. Lets the Audio panel re-measure a stream's rate or
|
||||
// override its format when detection is wrong/unrecoverable.
|
||||
enum AudioRingOp : std::uint32_t
|
||||
{
|
||||
enum AudioRingOp : std::uint32_t {
|
||||
AudioRingOp_None = 0,
|
||||
AudioRingOp_Remeasure = 1, // re-run the sample-rate measurement for this stream
|
||||
AudioRingOp_Override = 2, // adopt the op_rate/channels/bits/format_tag verbatim
|
||||
@@ -48,8 +46,7 @@ inline constexpr std::uint32_t kAudioRingCapacity = 1u << 20;
|
||||
// the format fields are written once by the producer *before* it publishes
|
||||
// format_valid (release), and read by the consumer *after* it observes
|
||||
// format_valid (acquire), so they need no atomicity of their own.
|
||||
struct AudioRingHeader
|
||||
{
|
||||
struct AudioRingHeader {
|
||||
std::uint32_t magic;
|
||||
std::uint32_t version;
|
||||
|
||||
@@ -176,8 +173,7 @@ inline bool audio_ring_push(AudioRingHeader& h, const void* src, std::uint32_t b
|
||||
const std::uint64_t w = h.write_pos.load(std::memory_order_relaxed);
|
||||
const std::uint64_t r = h.read_pos.load(std::memory_order_acquire);
|
||||
const std::uint32_t used = static_cast<std::uint32_t>(w - r);
|
||||
if (bytes > h.capacity - used)
|
||||
{
|
||||
if (bytes > h.capacity - used) {
|
||||
h.overruns.fetch_add(1, std::memory_order_relaxed);
|
||||
return false;
|
||||
}
|
||||
@@ -185,8 +181,7 @@ inline bool audio_ring_push(AudioRingHeader& h, const void* src, std::uint32_t b
|
||||
const std::uint32_t off = static_cast<std::uint32_t>(w % h.capacity);
|
||||
const std::uint32_t first = std::min(bytes, h.capacity - off);
|
||||
std::memcpy(data + off, src, first);
|
||||
if (bytes > first)
|
||||
{
|
||||
if (bytes > first) {
|
||||
std::memcpy(data, static_cast<const std::uint8_t*>(src) + first, bytes - first);
|
||||
}
|
||||
h.write_pos.store(w + bytes, std::memory_order_release);
|
||||
@@ -222,8 +217,7 @@ inline std::uint32_t audio_ring_pop(AudioRingHeader& h, void* dst, std::uint32_t
|
||||
const std::uint32_t off = static_cast<std::uint32_t>(r % h.capacity);
|
||||
const std::uint32_t first = std::min(bytes, h.capacity - off);
|
||||
std::memcpy(dst, data + off, first);
|
||||
if (bytes > first)
|
||||
{
|
||||
if (bytes > first) {
|
||||
std::memcpy(static_cast<std::uint8_t*>(dst) + first, data, bytes - first);
|
||||
}
|
||||
h.read_pos.store(r + bytes, std::memory_order_release);
|
||||
@@ -234,8 +228,7 @@ inline std::uint32_t audio_ring_pop(AudioRingHeader& h, void* dst, std::uint32_t
|
||||
// bumps op_seq (release) so the hook applies it exactly once. For a re-measure the
|
||||
// rate/channels/bits are ignored.
|
||||
inline void audio_ring_post_op(AudioRingHeader& h, std::uint32_t kind, std::uint32_t rate = 0,
|
||||
std::uint32_t channels = 0, std::uint32_t bits = 0,
|
||||
std::uint32_t format_tag = 0)
|
||||
std::uint32_t channels = 0, std::uint32_t bits = 0, std::uint32_t format_tag = 0)
|
||||
{
|
||||
h.op_kind = kind;
|
||||
h.op_rate = rate;
|
||||
@@ -246,8 +239,7 @@ inline void audio_ring_post_op(AudioRingHeader& h, std::uint32_t kind, std::uint
|
||||
}
|
||||
|
||||
// One operator command read back by the hook.
|
||||
struct AudioRingOpCmd
|
||||
{
|
||||
struct AudioRingOpCmd {
|
||||
std::uint32_t kind = AudioRingOp_None;
|
||||
std::uint32_t rate = 0;
|
||||
std::uint32_t channels = 0;
|
||||
@@ -261,8 +253,7 @@ struct AudioRingOpCmd
|
||||
inline std::uint32_t audio_ring_poll_op(AudioRingHeader& h, std::uint32_t& last_seq, AudioRingOpCmd& out)
|
||||
{
|
||||
const std::uint32_t seq = h.op_seq.load(std::memory_order_acquire);
|
||||
if (seq == last_seq)
|
||||
{
|
||||
if (seq == last_seq) {
|
||||
return AudioRingOp_None;
|
||||
}
|
||||
last_seq = seq;
|
||||
@@ -280,8 +271,7 @@ inline std::uint32_t audio_ring_poll_op(AudioRingHeader& h, std::uint32_t& last_
|
||||
inline std::wstring audio_ring_name(unsigned long target_pid, unsigned index = 0)
|
||||
{
|
||||
std::wstring name = std::wstring(kAudioRingPrefix) + std::to_wstring(target_pid);
|
||||
if (index != 0)
|
||||
{
|
||||
if (index != 0) {
|
||||
name += L"_" + std::to_wstring(index);
|
||||
}
|
||||
return name;
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
|
||||
#include <windows.h> // USER_DEFAULT_SCREEN_DPI (== 96, the 100%-scale baseline)
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// ImGui's built-in default font (ProggyClean) rasterizes at this pixel size at 100% scale. Named once
|
||||
// here so the DPI math scales from a single owned constant instead of a bare 13 sprinkled around.
|
||||
@@ -26,8 +25,7 @@ inline constexpr float kMaxUiScale = 8.0f;
|
||||
// callers never derive a zero-size font.
|
||||
inline float dpi_scale_from(unsigned dpi)
|
||||
{
|
||||
if (dpi == 0)
|
||||
{
|
||||
if (dpi == 0) {
|
||||
dpi = USER_DEFAULT_SCREEN_DPI;
|
||||
}
|
||||
const float scale = static_cast<float>(dpi) / static_cast<float>(USER_DEFAULT_SCREEN_DPI);
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// 'CLOG' little-endian.
|
||||
inline constexpr std::uint32_t kLogRingMagic = 0x474F4C43u;
|
||||
@@ -30,15 +29,13 @@ inline constexpr std::uint32_t kLogCapacity = 1024; // ring records
|
||||
|
||||
// Severity of a log line; drives the host Log window's colour. Stored in
|
||||
// LogRecord::level. Info is 0 so existing/zero-filled records read as Info.
|
||||
enum LogLevel : std::uint32_t
|
||||
{
|
||||
enum LogLevel : std::uint32_t {
|
||||
LogLevel_Info = 0,
|
||||
LogLevel_Warn = 1,
|
||||
LogLevel_Error = 2,
|
||||
};
|
||||
|
||||
struct LogRecord
|
||||
{
|
||||
struct LogRecord {
|
||||
std::atomic<std::uint64_t> seq; // 0 = empty; else (global index + 1) once written
|
||||
std::uint32_t pid;
|
||||
std::uint32_t level; // LogLevel
|
||||
@@ -46,8 +43,7 @@ struct LogRecord
|
||||
char text[kLogMsgLen];
|
||||
};
|
||||
|
||||
struct LogRing
|
||||
{
|
||||
struct LogRing {
|
||||
std::uint32_t magic;
|
||||
std::uint32_t version;
|
||||
std::uint32_t capacity; // number of records
|
||||
@@ -83,13 +79,11 @@ inline void log_ring_init(LogRing& r, std::uint32_t capacity)
|
||||
|
||||
inline bool log_ring_valid(const LogRing& r)
|
||||
{
|
||||
return r.magic == kLogRingMagic && r.version == kLogRingVersion && r.capacity != 0 &&
|
||||
r.msg_len == kLogMsgLen;
|
||||
return r.magic == kLogRingMagic && r.version == kLogRingVersion && r.capacity != 0 && r.msg_len == kLogMsgLen;
|
||||
}
|
||||
|
||||
// Producer (hook): append a line at severity `level` (LogLevel). Multi-producer safe.
|
||||
inline void log_ring_push(LogRing& r, std::uint32_t pid, std::uint32_t level, std::uint64_t millis,
|
||||
const char* text)
|
||||
inline void log_ring_push(LogRing& r, std::uint32_t pid, std::uint32_t level, std::uint64_t millis, const char* text)
|
||||
{
|
||||
const std::uint64_t idx = r.write_index.fetch_add(1, std::memory_order_acq_rel);
|
||||
LogRecord& rec = log_ring_records(&r)[idx % r.capacity];
|
||||
@@ -115,23 +109,19 @@ template <typename F>
|
||||
inline void log_ring_drain(LogRing& r, std::uint64_t& cursor, F&& emit)
|
||||
{
|
||||
const std::uint64_t w = r.write_index.load(std::memory_order_acquire);
|
||||
if (w <= cursor)
|
||||
{
|
||||
if (w <= cursor) {
|
||||
return;
|
||||
}
|
||||
const std::uint64_t lo = (w > r.capacity) ? (w - r.capacity) : 0;
|
||||
std::uint64_t i = cursor < lo ? lo : cursor; // skip records already overwritten
|
||||
LogRecord* recs = log_ring_records(&r);
|
||||
for (; i < w; ++i)
|
||||
{
|
||||
for (; i < w; ++i) {
|
||||
LogRecord& rec = recs[i % r.capacity];
|
||||
const std::uint64_t s1 = rec.seq.load(std::memory_order_acquire);
|
||||
if (s1 <= i)
|
||||
{
|
||||
if (s1 <= i) {
|
||||
break; // generation i not written yet (in-flight, or being overwritten); retry next call
|
||||
}
|
||||
if (s1 != i + 1)
|
||||
{
|
||||
if (s1 != i + 1) {
|
||||
continue; // s1 > i+1: overwritten by a later generation before we got here; lost, skip
|
||||
}
|
||||
// Seqlock read: copy the record out, then re-check seq. A producer overwriting this slot stores
|
||||
@@ -142,8 +132,7 @@ inline void log_ring_drain(LogRing& r, std::uint64_t& cursor, F&& emit)
|
||||
snap.millis = rec.millis;
|
||||
std::memcpy(snap.text, rec.text, kLogMsgLen);
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
if (rec.seq.load(std::memory_order_relaxed) == i + 1)
|
||||
{
|
||||
if (rec.seq.load(std::memory_order_relaxed) == i + 1) {
|
||||
emit(snap); // consistent snapshot
|
||||
}
|
||||
// else: overwritten while we copied -> skip (lost)
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Bump whenever the layout of SharedBlock or CoopPadState changes. The hook
|
||||
// refuses to attach to a host with a mismatched version.
|
||||
@@ -27,8 +26,7 @@ inline constexpr wchar_t kSharedMemoryPrefix[] = L"Local\\coop_ipc_";
|
||||
// One controller's state, laid out to map 1:1 onto XINPUT_GAMEPAD plus the
|
||||
// metadata the hook needs. Field names/types match XINPUT_GAMEPAD so the hook
|
||||
// can memcpy the trailing region straight into an XINPUT_STATE.
|
||||
struct CoopPadState
|
||||
{
|
||||
struct CoopPadState {
|
||||
std::uint8_t connected; // 1 if a guest/host pad is mapped to this slot
|
||||
std::uint8_t reserved[3];
|
||||
std::uint32_t packet; // bumps on change -> XINPUT_STATE::dwPacketNumber
|
||||
@@ -56,8 +54,7 @@ inline constexpr std::uint32_t kMaxAudioStreams = 4;
|
||||
// we injected (the common case) was never seen at Initialize, so its format starts as a
|
||||
// guess (the device mix format) and its true sample rate is measured from the render
|
||||
// cadence; a stream we watched get created carries its exact Initialize format.
|
||||
enum AudioFormatState : std::uint32_t
|
||||
{
|
||||
enum AudioFormatState : std::uint32_t {
|
||||
AudioFormat_Unknown = 0, // no format determined yet
|
||||
AudioFormat_Exact = 1, // taken from the game's own IAudioClient::Initialize
|
||||
AudioFormat_Measuring = 2, // guessed (device mix format); true sample rate being measured
|
||||
@@ -66,8 +63,7 @@ enum AudioFormatState : std::uint32_t
|
||||
AudioFormat_Override = 5, // operator set this format manually (see the per-stream op channel)
|
||||
};
|
||||
|
||||
struct AudioStreamInfo
|
||||
{
|
||||
struct AudioStreamInfo {
|
||||
std::uint32_t is_primary; // 1 = the stream the hook captures/silences
|
||||
std::uint32_t sample_rate;
|
||||
std::uint16_t channels;
|
||||
@@ -78,8 +74,7 @@ struct AudioStreamInfo
|
||||
};
|
||||
|
||||
// Orthogonal hook subsystems the host can install/remove independently.
|
||||
enum HookSubsystem : std::uint32_t
|
||||
{
|
||||
enum HookSubsystem : std::uint32_t {
|
||||
HookSubsys_Input = 0, // XInput hooks (forward the guest pad)
|
||||
HookSubsys_Focus = 1, // focus spoof (keep the game running unfocused)
|
||||
HookSubsys_Audio = 2, // WASAPI render-hook (audio mirror without echo)
|
||||
@@ -93,8 +88,7 @@ inline constexpr std::uint32_t kMaxHookEntries = 24;
|
||||
|
||||
// One installed hook, for the Injection panel's hook list. POD diagnostics, like
|
||||
// AudioStreamInfo: the hook is the sole writer; benign cross-process races are ok.
|
||||
struct HookEntry
|
||||
{
|
||||
struct HookEntry {
|
||||
char name[40]; // e.g. "XInputGetState"
|
||||
std::uint32_t subsystem; // HookSubsystem
|
||||
std::uint32_t installed; // 1 if currently hooked
|
||||
@@ -102,8 +96,7 @@ struct HookEntry
|
||||
};
|
||||
|
||||
// Indices into HookStatus::focus_query_calls.
|
||||
enum FocusApi : std::uint32_t
|
||||
{
|
||||
enum FocusApi : std::uint32_t {
|
||||
FocusApi_Foreground = 0, // GetForegroundWindow
|
||||
FocusApi_Active = 1, // GetActiveWindow
|
||||
FocusApi_Focus = 2, // GetFocus
|
||||
@@ -115,8 +108,7 @@ enum FocusApi : std::uint32_t
|
||||
// polling, does it use the focus APIs, and does it read input through a
|
||||
// focus-gated path (Raw Input / DirectInput)? Diagnostics only, so the non-atomic
|
||||
// fields tolerate benign cross-process races.
|
||||
struct HookStatus
|
||||
{
|
||||
struct HookStatus {
|
||||
std::atomic<std::uint32_t> heartbeat; // DLL bumps ~4x/sec while alive
|
||||
std::atomic<std::uint64_t> get_state_calls[kMaxPads]; // XInputGetState/Ex per slot
|
||||
std::atomic<std::uint64_t> get_caps_calls[kMaxPads]; // XInputGetCapabilities per slot
|
||||
@@ -163,8 +155,7 @@ struct HookStatus
|
||||
// Host -> hook control channel. The host requests which hook subsystems should be
|
||||
// installed; the hook reconciles each tick. 0 = install (the zero-filled default,
|
||||
// so a fresh mapping installs everything as before), 1 = remove.
|
||||
struct HookControl
|
||||
{
|
||||
struct HookControl {
|
||||
std::atomic<std::uint32_t> subsystem_disabled[HookSubsys_Count];
|
||||
|
||||
// Cursor handling for cursor-clipping games (part of the Focus subsystem).
|
||||
@@ -181,8 +172,7 @@ struct HookControl
|
||||
// is the sole writer. `generation` bumps on every published frame (0 = nothing
|
||||
// shared yet); width/height/format describe the currently shared texture, so the
|
||||
// host reopens it whenever they change. The keyed mutex uses key 0 on both sides.
|
||||
struct VideoShare
|
||||
{
|
||||
struct VideoShare {
|
||||
std::atomic<std::uint32_t> generation; // bumps per published frame; 0 = none yet
|
||||
std::uint32_t width; // shared texture dimensions / DXGI format
|
||||
std::uint32_t height;
|
||||
@@ -203,8 +193,7 @@ struct VideoShare
|
||||
// the matching window messages to the game, and maintains a synthesized state the
|
||||
// GetAsyncKeyState/GetKeyboardState/GetCursorPos hooks report to polling games.
|
||||
|
||||
enum MkbEventType : std::uint32_t
|
||||
{
|
||||
enum MkbEventType : std::uint32_t {
|
||||
Mkb_KeyDown = 0, // code = Win32 virtual-key
|
||||
Mkb_KeyUp = 1, // code = Win32 virtual-key
|
||||
Mkb_Char = 2, // code = UTF-16 code unit (WM_CHAR)
|
||||
@@ -213,8 +202,7 @@ enum MkbEventType : std::uint32_t
|
||||
Mkb_Wheel = 5, // code = signed wheel delta (WHEEL_DELTA units); x,y = game client px
|
||||
};
|
||||
|
||||
struct MkbEvent
|
||||
{
|
||||
struct MkbEvent {
|
||||
std::uint32_t type; // MkbEventType
|
||||
std::uint32_t code; // see per-type meaning above
|
||||
std::int32_t x; // game-client x (mouse events)
|
||||
@@ -227,8 +215,7 @@ static_assert(sizeof(MkbEvent) == 16, "MkbEvent must stay byte-identical across
|
||||
inline constexpr std::uint32_t kMkbQueueSize = 128;
|
||||
|
||||
// Lock-free SPSC ring: host produces, hook consumes. Free-running 32-bit indices.
|
||||
struct MkbRing
|
||||
{
|
||||
struct MkbRing {
|
||||
std::atomic<std::uint32_t> head; // producer (host) write position
|
||||
std::atomic<std::uint32_t> tail; // consumer (hook) read position
|
||||
MkbEvent events[kMkbQueueSize];
|
||||
@@ -244,8 +231,7 @@ inline constexpr std::uint64_t kVideoMutexKey = 0;
|
||||
// Top-level shared block. The host is the sole writer of pad state; the hook is
|
||||
// the sole reader. A seqlock (even = stable, odd = write in progress) lets the
|
||||
// reader grab a torn-free snapshot without a kernel lock on the hot path.
|
||||
struct SharedBlock
|
||||
{
|
||||
struct SharedBlock {
|
||||
std::uint32_t magic;
|
||||
std::uint32_t version;
|
||||
std::uint32_t pad_count; // number of populated slots, <= kMaxPads
|
||||
@@ -297,20 +283,17 @@ static_assert(sizeof(MkbRing) == 2056, "MkbRing size changed -- wire-protocol ch
|
||||
// Writer side: publish a fresh set of pad states. Called from the host.
|
||||
inline void publish_pads(SharedBlock& block, const CoopPadState* pads, std::uint32_t count)
|
||||
{
|
||||
if (count > kMaxPads)
|
||||
{
|
||||
if (count > kMaxPads) {
|
||||
count = kMaxPads;
|
||||
}
|
||||
const std::uint32_t seq = block.sequence.load(std::memory_order_relaxed);
|
||||
block.sequence.store(seq + 1, std::memory_order_release); // -> odd: write begins
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
block.pad_count = count;
|
||||
for (std::uint32_t i = 0; i < count; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < count; ++i) {
|
||||
block.pads[i] = pads[i];
|
||||
}
|
||||
for (std::uint32_t i = count; i < kMaxPads; ++i)
|
||||
{
|
||||
for (std::uint32_t i = count; i < kMaxPads; ++i) {
|
||||
block.pads[i] = CoopPadState{};
|
||||
}
|
||||
block.sequence.store(seq + 2, std::memory_order_release); // -> even: write done
|
||||
@@ -320,26 +303,21 @@ inline void publish_pads(SharedBlock& block, const CoopPadState* pads, std::uint
|
||||
// if a write is in flight; bounded so a crashed writer can't hang the game.
|
||||
inline bool read_pads(const SharedBlock& block, CoopPadState (&out)[kMaxPads], std::uint32_t& out_count)
|
||||
{
|
||||
for (int attempt = 0; attempt < 64; ++attempt)
|
||||
{
|
||||
for (int attempt = 0; attempt < 64; ++attempt) {
|
||||
const std::uint32_t before = block.sequence.load(std::memory_order_acquire);
|
||||
if (before & 1u)
|
||||
{
|
||||
if (before & 1u) {
|
||||
continue; // writer mid-update, retry
|
||||
}
|
||||
std::uint32_t count = block.pad_count;
|
||||
if (count > kMaxPads)
|
||||
{
|
||||
if (count > kMaxPads) {
|
||||
count = kMaxPads;
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i) {
|
||||
out[i] = block.pads[i];
|
||||
}
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
const std::uint32_t after = block.sequence.load(std::memory_order_acquire);
|
||||
if (before == after)
|
||||
{
|
||||
if (before == after) {
|
||||
out_count = count;
|
||||
return true;
|
||||
}
|
||||
@@ -354,8 +332,7 @@ inline bool push_mkb_event(MkbRing& ring, const MkbEvent& ev)
|
||||
{
|
||||
const std::uint32_t head = ring.head.load(std::memory_order_relaxed);
|
||||
const std::uint32_t tail = ring.tail.load(std::memory_order_acquire);
|
||||
if (head - tail >= kMkbQueueSize)
|
||||
{
|
||||
if (head - tail >= kMkbQueueSize) {
|
||||
return false; // full -> drop (host should always drain faster than it fills)
|
||||
}
|
||||
ring.events[head & (kMkbQueueSize - 1)] = ev;
|
||||
@@ -368,8 +345,7 @@ inline bool pop_mkb_event(MkbRing& ring, MkbEvent& out)
|
||||
{
|
||||
const std::uint32_t tail = ring.tail.load(std::memory_order_relaxed);
|
||||
const std::uint32_t head = ring.head.load(std::memory_order_acquire);
|
||||
if (tail == head)
|
||||
{
|
||||
if (tail == head) {
|
||||
return false; // empty
|
||||
}
|
||||
out = ring.events[tail & (kMkbQueueSize - 1)];
|
||||
|
||||
@@ -9,26 +9,20 @@
|
||||
|
||||
#include "coop/protocol.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class SharedMemory
|
||||
{
|
||||
class SharedMemory {
|
||||
public:
|
||||
SharedMemory() = default;
|
||||
|
||||
SharedMemory(const SharedMemory&) = delete;
|
||||
SharedMemory& operator=(const SharedMemory&) = delete;
|
||||
|
||||
SharedMemory(SharedMemory&& other) noexcept
|
||||
{
|
||||
*this = std::move(other);
|
||||
}
|
||||
SharedMemory(SharedMemory&& other) noexcept { *this = std::move(other); }
|
||||
|
||||
SharedMemory& operator=(SharedMemory&& other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
if (this != &other) {
|
||||
reset();
|
||||
mapping_ = std::exchange(other.mapping_, nullptr);
|
||||
view_ = std::exchange(other.view_, nullptr);
|
||||
@@ -37,19 +31,15 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
~SharedMemory()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
~SharedMemory() { reset(); }
|
||||
|
||||
// Host side: create (or open if it already exists) the named section.
|
||||
bool create(const std::wstring& name, std::size_t size)
|
||||
{
|
||||
reset();
|
||||
mapping_ = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
|
||||
static_cast<DWORD>(size), name.c_str());
|
||||
if (mapping_ == nullptr)
|
||||
{
|
||||
mapping_ = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, static_cast<DWORD>(size),
|
||||
name.c_str());
|
||||
if (mapping_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return map(size);
|
||||
@@ -60,8 +50,7 @@ public:
|
||||
{
|
||||
reset();
|
||||
mapping_ = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, name.c_str());
|
||||
if (mapping_ == nullptr)
|
||||
{
|
||||
if (mapping_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return map(size);
|
||||
@@ -69,23 +58,18 @@ public:
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (view_ != nullptr)
|
||||
{
|
||||
if (view_ != nullptr) {
|
||||
UnmapViewOfFile(view_);
|
||||
view_ = nullptr;
|
||||
}
|
||||
if (mapping_ != nullptr)
|
||||
{
|
||||
if (mapping_ != nullptr) {
|
||||
CloseHandle(mapping_);
|
||||
mapping_ = nullptr;
|
||||
}
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool valid() const
|
||||
{
|
||||
return view_ != nullptr;
|
||||
}
|
||||
[[nodiscard]] bool valid() const { return view_ != nullptr; }
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] T* as() const
|
||||
@@ -93,22 +77,15 @@ public:
|
||||
return static_cast<T*>(view_);
|
||||
}
|
||||
|
||||
[[nodiscard]] void* data() const
|
||||
{
|
||||
return view_;
|
||||
}
|
||||
[[nodiscard]] void* data() const { return view_; }
|
||||
|
||||
[[nodiscard]] std::size_t size() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
[[nodiscard]] std::size_t size() const { return size_; }
|
||||
|
||||
private:
|
||||
bool map(std::size_t size)
|
||||
{
|
||||
view_ = MapViewOfFile(mapping_, FILE_MAP_ALL_ACCESS, 0, 0, size);
|
||||
if (view_ == nullptr)
|
||||
{
|
||||
if (view_ == nullptr) {
|
||||
CloseHandle(mapping_);
|
||||
mapping_ = nullptr;
|
||||
return false;
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// WAVE_FORMAT_* tags we decode (kept local to avoid an mmreg.h dependency, matching audio_mix.hpp).
|
||||
inline constexpr std::uint32_t kToneFormatPcm = 1;
|
||||
@@ -30,8 +29,7 @@ inline constexpr std::uint32_t kToneFormatFloat = 3;
|
||||
|
||||
// One channel's worth of measured fidelity. Fields are NaN/0 when not applicable
|
||||
// (e.g. pitch metrics need a known expected_hz > 0).
|
||||
struct ToneReport
|
||||
{
|
||||
struct ToneReport {
|
||||
bool valid = false; // enough samples to analyze
|
||||
unsigned sample_rate = 0; // the rate the samples are interpreted at (the *declared* rate)
|
||||
std::size_t frames = 0; // mono frames analyzed
|
||||
@@ -59,8 +57,7 @@ struct ToneReport
|
||||
double dropout_ms = 0.0; // total duration of those gaps
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
namespace detail {
|
||||
|
||||
inline constexpr double kPi = 3.14159265358979323846;
|
||||
|
||||
@@ -68,28 +65,22 @@ inline constexpr double kPi = 3.14159265358979323846;
|
||||
inline void fft(std::vector<std::complex<double>>& a)
|
||||
{
|
||||
const std::size_t n = a.size();
|
||||
for (std::size_t i = 1, j = 0; i < n; ++i)
|
||||
{
|
||||
for (std::size_t i = 1, j = 0; i < n; ++i) {
|
||||
std::size_t bit = n >> 1;
|
||||
for (; (j & bit) != 0; bit >>= 1)
|
||||
{
|
||||
for (; (j & bit) != 0; bit >>= 1) {
|
||||
j ^= bit;
|
||||
}
|
||||
j ^= bit;
|
||||
if (i < j)
|
||||
{
|
||||
if (i < j) {
|
||||
std::swap(a[i], a[j]);
|
||||
}
|
||||
}
|
||||
for (std::size_t len = 2; len <= n; len <<= 1)
|
||||
{
|
||||
for (std::size_t len = 2; len <= n; len <<= 1) {
|
||||
const double ang = -2.0 * kPi / static_cast<double>(len);
|
||||
const std::complex<double> wlen(std::cos(ang), std::sin(ang));
|
||||
for (std::size_t i = 0; i < n; i += len)
|
||||
{
|
||||
for (std::size_t i = 0; i < n; i += len) {
|
||||
std::complex<double> w(1.0, 0.0);
|
||||
for (std::size_t k = 0; k < len / 2; ++k)
|
||||
{
|
||||
for (std::size_t k = 0; k < len / 2; ++k) {
|
||||
const std::complex<double> u = a[i + k];
|
||||
const std::complex<double> v = a[i + k + len / 2] * w;
|
||||
a[i + k] = u + v;
|
||||
@@ -104,8 +95,7 @@ inline void fft(std::vector<std::complex<double>>& a)
|
||||
inline std::size_t floor_pow2(std::size_t n)
|
||||
{
|
||||
std::size_t p = 1;
|
||||
while ((p << 1) != 0 && (p << 1) <= n)
|
||||
{
|
||||
while ((p << 1) != 0 && (p << 1) <= n) {
|
||||
p <<= 1;
|
||||
}
|
||||
return n == 0 ? 0 : p;
|
||||
@@ -120,27 +110,21 @@ inline std::vector<float> decode_channel(const std::uint8_t* pcm, std::size_t by
|
||||
std::uint32_t bits, std::uint32_t channels, std::uint32_t channel = 0)
|
||||
{
|
||||
std::vector<float> out;
|
||||
if (pcm == nullptr || channels == 0 || channel >= channels)
|
||||
{
|
||||
if (pcm == nullptr || channels == 0 || channel >= channels) {
|
||||
return out;
|
||||
}
|
||||
if (format_tag == kToneFormatFloat && bits == 32)
|
||||
{
|
||||
if (format_tag == kToneFormatFloat && bits == 32) {
|
||||
const std::size_t frames = bytes / (channels * 4);
|
||||
out.reserve(frames);
|
||||
const auto* f = reinterpret_cast<const float*>(pcm);
|
||||
for (std::size_t i = 0; i < frames; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < frames; ++i) {
|
||||
out.push_back(f[i * channels + channel]);
|
||||
}
|
||||
}
|
||||
else if (format_tag == kToneFormatPcm && bits == 16)
|
||||
{
|
||||
} else if (format_tag == kToneFormatPcm && bits == 16) {
|
||||
const std::size_t frames = bytes / (channels * 2);
|
||||
out.reserve(frames);
|
||||
const auto* s = reinterpret_cast<const std::int16_t*>(pcm);
|
||||
for (std::size_t i = 0; i < frames; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < frames; ++i) {
|
||||
out.push_back(static_cast<float>(s[i * channels + channel]) / 32768.0f);
|
||||
}
|
||||
}
|
||||
@@ -150,15 +134,13 @@ inline std::vector<float> decode_channel(const std::uint8_t* pcm, std::size_t by
|
||||
// Analyze a single channel of normalized float samples. `expected_hz` is the known
|
||||
// input tone frequency (pass 0 to skip the pitch/SNR/THD metrics for non-tone audio;
|
||||
// the click/dropout/level metrics still apply).
|
||||
inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigned sample_rate,
|
||||
double expected_hz)
|
||||
inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigned sample_rate, double expected_hz)
|
||||
{
|
||||
ToneReport r;
|
||||
r.sample_rate = sample_rate;
|
||||
r.frames = frames;
|
||||
r.expected_hz = expected_hz;
|
||||
if (samples == nullptr || frames < 64 || sample_rate == 0)
|
||||
{
|
||||
if (samples == nullptr || frames < 64 || sample_rate == 0) {
|
||||
return r;
|
||||
}
|
||||
r.valid = true;
|
||||
@@ -168,14 +150,12 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
double sumsq = 0.0;
|
||||
double peak = 0.0;
|
||||
std::size_t clipped = 0;
|
||||
for (std::size_t i = 0; i < frames; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < frames; ++i) {
|
||||
const double x = samples[i];
|
||||
sumsq += x * x;
|
||||
const double a = std::fabs(x);
|
||||
peak = std::max(peak, a);
|
||||
if (a >= 0.999)
|
||||
{
|
||||
if (a >= 0.999) {
|
||||
++clipped;
|
||||
}
|
||||
}
|
||||
@@ -188,11 +168,9 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
// step. Use the median |first difference| as a robust scale (immune to the tone's own
|
||||
// slope and to a few outliers), and flag steps beyond 8x it. Group samples within a
|
||||
// short refractory window into one event so a single click isn't counted many times.
|
||||
if (frames >= 3)
|
||||
{
|
||||
if (frames >= 3) {
|
||||
std::vector<float> diff(frames - 1);
|
||||
for (std::size_t i = 1; i < frames; ++i)
|
||||
{
|
||||
for (std::size_t i = 1; i < frames; ++i) {
|
||||
diff[i - 1] = std::fabs(samples[i] - samples[i - 1]);
|
||||
}
|
||||
std::vector<float> sorted(diff);
|
||||
@@ -202,12 +180,9 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
const std::size_t refractory = std::max<std::size_t>(sample_rate / 1000, 8); // ~1 ms
|
||||
std::size_t last_event_end = 0;
|
||||
bool have_event = false;
|
||||
for (std::size_t i = 0; i < diff.size(); ++i)
|
||||
{
|
||||
if (diff[i] > thresh)
|
||||
{
|
||||
if (!have_event || i > last_event_end)
|
||||
{
|
||||
for (std::size_t i = 0; i < diff.size(); ++i) {
|
||||
if (diff[i] > thresh) {
|
||||
if (!have_event || i > last_event_end) {
|
||||
++r.glitch_count;
|
||||
}
|
||||
have_event = true;
|
||||
@@ -220,8 +195,7 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
// --- Dropout detection: stretches that fall near-silent in an otherwise active signal ---
|
||||
// Slide a ~5 ms window; flag windows whose RMS drops below 8% of the global RMS. Only
|
||||
// meaningful when the signal is actually present (global RMS above a small floor).
|
||||
if (r.rms > 1e-4)
|
||||
{
|
||||
if (r.rms > 1e-4) {
|
||||
const std::size_t win = std::max<std::size_t>(sample_rate * 5 / 1000, 16); // ~5 ms
|
||||
const std::size_t hop = std::max<std::size_t>(win / 2, 1);
|
||||
const double silence_thresh = 0.08 * r.rms;
|
||||
@@ -230,33 +204,26 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
std::size_t gap_last = 0; // last silent window's end sample
|
||||
std::size_t total_silent_samples = 0;
|
||||
auto close_gap = [&]() {
|
||||
if (in_gap)
|
||||
{
|
||||
if (in_gap) {
|
||||
total_silent_samples += (gap_last - gap_first);
|
||||
in_gap = false;
|
||||
}
|
||||
};
|
||||
for (std::size_t start = 0; start + win <= frames; start += hop)
|
||||
{
|
||||
for (std::size_t start = 0; start + win <= frames; start += hop) {
|
||||
double ws = 0.0;
|
||||
for (std::size_t i = 0; i < win; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < win; ++i) {
|
||||
const double x = samples[start + i];
|
||||
ws += x * x;
|
||||
}
|
||||
const double wr = std::sqrt(ws / static_cast<double>(win));
|
||||
if (wr < silence_thresh)
|
||||
{
|
||||
if (!in_gap)
|
||||
{
|
||||
if (wr < silence_thresh) {
|
||||
if (!in_gap) {
|
||||
++r.dropout_count;
|
||||
gap_first = start;
|
||||
in_gap = true;
|
||||
}
|
||||
gap_last = start + win;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
close_gap();
|
||||
}
|
||||
}
|
||||
@@ -267,23 +234,19 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
}
|
||||
|
||||
// --- Spectral analysis (pitch / SNR / THD), Hann-windowed FFT ---
|
||||
if (expected_hz > 0.0)
|
||||
{
|
||||
if (expected_hz > 0.0) {
|
||||
std::size_t n = detail::floor_pow2(frames);
|
||||
n = std::min<std::size_t>(n, std::size_t(1) << 18); // cap cost (~5 s @ 48k)
|
||||
if (n >= 1024)
|
||||
{
|
||||
if (n >= 1024) {
|
||||
std::vector<std::complex<double>> buf(n);
|
||||
for (std::size_t i = 0; i < n; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
const double w = 0.5 - 0.5 * std::cos(2.0 * detail::kPi * i / (n - 1)); // Hann
|
||||
buf[i] = std::complex<double>(samples[i] * w, 0.0);
|
||||
}
|
||||
detail::fft(buf);
|
||||
const std::size_t half = n / 2;
|
||||
std::vector<double> mag(half);
|
||||
for (std::size_t i = 0; i < half; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < half; ++i) {
|
||||
mag[i] = std::abs(buf[i]);
|
||||
}
|
||||
const double bin_hz = static_cast<double>(sample_rate) / static_cast<double>(n);
|
||||
@@ -291,31 +254,26 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
// Peak bin, ignoring DC/very low bins (skip < 20 Hz).
|
||||
std::size_t lo = std::max<std::size_t>(static_cast<std::size_t>(20.0 / bin_hz), 1);
|
||||
std::size_t peak_bin = lo;
|
||||
for (std::size_t i = lo; i < half; ++i)
|
||||
{
|
||||
if (mag[i] > mag[peak_bin])
|
||||
{
|
||||
for (std::size_t i = lo; i < half; ++i) {
|
||||
if (mag[i] > mag[peak_bin]) {
|
||||
peak_bin = i;
|
||||
}
|
||||
}
|
||||
// Quadratic (parabolic) interpolation on log-magnitude for a sub-bin estimate
|
||||
// (accurate for a Hann-windowed peak).
|
||||
double delta = 0.0;
|
||||
if (peak_bin > 0 && peak_bin + 1 < half)
|
||||
{
|
||||
if (peak_bin > 0 && peak_bin + 1 < half) {
|
||||
const double a = std::log(mag[peak_bin - 1] + 1e-30);
|
||||
const double b = std::log(mag[peak_bin] + 1e-30);
|
||||
const double c = std::log(mag[peak_bin + 1] + 1e-30);
|
||||
const double denom = (a - 2.0 * b + c);
|
||||
if (std::fabs(denom) > 1e-30)
|
||||
{
|
||||
if (std::fabs(denom) > 1e-30) {
|
||||
delta = 0.5 * (a - c) / denom;
|
||||
delta = std::max(-0.5, std::min(0.5, delta));
|
||||
}
|
||||
}
|
||||
r.dominant_hz = (static_cast<double>(peak_bin) + delta) * bin_hz;
|
||||
if (r.dominant_hz > 0.0)
|
||||
{
|
||||
if (r.dominant_hz > 0.0) {
|
||||
r.pitch_error_ratio = r.dominant_hz / expected_hz;
|
||||
r.pitch_error_cents = 1200.0 * std::log2(r.pitch_error_ratio);
|
||||
}
|
||||
@@ -328,10 +286,8 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
auto lobe_power = [&](double hz, long half_w) {
|
||||
const long center = static_cast<long>(std::lround(hz / bin_hz));
|
||||
double p = 0.0;
|
||||
for (long k = center - half_w; k <= center + half_w; ++k)
|
||||
{
|
||||
if (k >= 0 && static_cast<std::size_t>(k) < half)
|
||||
{
|
||||
for (long k = center - half_w; k <= center + half_w; ++k) {
|
||||
if (k >= 0 && static_cast<std::size_t>(k) < half) {
|
||||
p += mag[k] * mag[k];
|
||||
}
|
||||
}
|
||||
@@ -339,8 +295,7 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
};
|
||||
|
||||
double total_power = 0.0;
|
||||
for (std::size_t i = lo; i < half; ++i)
|
||||
{
|
||||
for (std::size_t i = lo; i < half; ++i) {
|
||||
total_power += mag[i] * mag[i];
|
||||
}
|
||||
const double fund_power = lobe_power(r.dominant_hz, 8);
|
||||
@@ -348,11 +303,9 @@ inline ToneReport analyze_tone(const float* samples, std::size_t frames, unsigne
|
||||
r.snr_db = 10.0 * std::log10(std::max(fund_power, 1e-30) / residual);
|
||||
|
||||
double harm_power = 0.0;
|
||||
for (int h = 2; h <= 6; ++h)
|
||||
{
|
||||
for (int h = 2; h <= 6; ++h) {
|
||||
const double hz = r.dominant_hz * h;
|
||||
if (hz < (sample_rate / 2.0))
|
||||
{
|
||||
if (hz < (sample_rate / 2.0)) {
|
||||
harm_power += lobe_power(hz, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Directory of the current executable, with a trailing separator.
|
||||
inline std::wstring exe_directory()
|
||||
@@ -28,21 +27,17 @@ inline std::wstring exe_directory()
|
||||
inline std::wstring deployed_artifact_path(const wchar_t* name)
|
||||
{
|
||||
const std::wstring here = exe_directory() + name;
|
||||
if (GetFileAttributesW(here.c_str()) != INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
if (GetFileAttributesW(here.c_str()) != INVALID_FILE_ATTRIBUTES) {
|
||||
return here;
|
||||
}
|
||||
std::wstring dir = exe_directory();
|
||||
if (!dir.empty())
|
||||
{
|
||||
if (!dir.empty()) {
|
||||
dir.pop_back(); // drop the trailing separator before going up a level
|
||||
}
|
||||
const std::size_t slash = dir.find_last_of(L"\\/");
|
||||
if (slash != std::wstring::npos)
|
||||
{
|
||||
if (slash != std::wstring::npos) {
|
||||
const std::wstring up = dir.substr(0, slash + 1) + name;
|
||||
if (GetFileAttributesW(up.c_str()) != INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
if (GetFileAttributesW(up.c_str()) != INVALID_FILE_ATTRIBUTES) {
|
||||
return up;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct WavData
|
||||
{
|
||||
struct WavData {
|
||||
std::uint32_t sample_rate = 0;
|
||||
std::uint32_t channels = 0;
|
||||
std::uint32_t bits = 0;
|
||||
@@ -24,8 +22,7 @@ struct WavData
|
||||
std::vector<std::uint8_t> pcm; // interleaved frames
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
namespace detail {
|
||||
inline void wav_put_u32(std::vector<std::uint8_t>& b, std::uint32_t v)
|
||||
{
|
||||
b.push_back(v & 0xFF);
|
||||
@@ -75,12 +72,11 @@ inline bool wav_write(const std::wstring& path, const void* pcm, std::size_t byt
|
||||
detail::wav_put_u32(hdr, static_cast<std::uint32_t>(bytes));
|
||||
|
||||
FILE* f = nullptr;
|
||||
if (_wfopen_s(&f, path.c_str(), L"wb") != 0 || f == nullptr)
|
||||
{
|
||||
if (_wfopen_s(&f, path.c_str(), L"wb") != 0 || f == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const bool ok = std::fwrite(hdr.data(), 1, hdr.size(), f) == hdr.size() &&
|
||||
(bytes == 0 || std::fwrite(pcm, 1, bytes, f) == bytes);
|
||||
const bool ok = std::fwrite(hdr.data(), 1, hdr.size(), f) == hdr.size()
|
||||
&& (bytes == 0 || std::fwrite(pcm, 1, bytes, f) == bytes);
|
||||
std::fclose(f);
|
||||
return ok;
|
||||
}
|
||||
@@ -89,44 +85,37 @@ inline bool wav_write(const std::wstring& path, const void* pcm, std::size_t byt
|
||||
inline bool wav_read(const std::wstring& path, WavData& out)
|
||||
{
|
||||
FILE* f = nullptr;
|
||||
if (_wfopen_s(&f, path.c_str(), L"rb") != 0 || f == nullptr)
|
||||
{
|
||||
if (_wfopen_s(&f, path.c_str(), L"rb") != 0 || f == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::fseek(f, 0, SEEK_END);
|
||||
const long size = std::ftell(f);
|
||||
std::fseek(f, 0, SEEK_SET);
|
||||
if (size < 44)
|
||||
{
|
||||
if (size < 44) {
|
||||
std::fclose(f);
|
||||
return false;
|
||||
}
|
||||
std::vector<std::uint8_t> all(static_cast<std::size_t>(size));
|
||||
const bool read_ok = std::fread(all.data(), 1, all.size(), f) == all.size();
|
||||
std::fclose(f);
|
||||
if (!read_ok || std::memcmp(all.data(), "RIFF", 4) != 0 || std::memcmp(all.data() + 8, "WAVE", 4) != 0)
|
||||
{
|
||||
if (!read_ok || std::memcmp(all.data(), "RIFF", 4) != 0 || std::memcmp(all.data() + 8, "WAVE", 4) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Walk chunks for "fmt " and "data".
|
||||
std::size_t pos = 12;
|
||||
bool have_fmt = false, have_data = false;
|
||||
while (pos + 8 <= all.size())
|
||||
{
|
||||
while (pos + 8 <= all.size()) {
|
||||
const std::uint8_t* p = all.data() + pos;
|
||||
const std::uint32_t chunk_size = detail::wav_get_u32(p + 4);
|
||||
const std::size_t body = pos + 8;
|
||||
if (std::memcmp(p, "fmt ", 4) == 0 && body + 16 <= all.size())
|
||||
{
|
||||
if (std::memcmp(p, "fmt ", 4) == 0 && body + 16 <= all.size()) {
|
||||
out.format_tag = detail::wav_get_u16(all.data() + body + 0);
|
||||
out.channels = detail::wav_get_u16(all.data() + body + 2);
|
||||
out.sample_rate = detail::wav_get_u32(all.data() + body + 4);
|
||||
out.bits = detail::wav_get_u16(all.data() + body + 14);
|
||||
have_fmt = true;
|
||||
}
|
||||
else if (std::memcmp(p, "data", 4) == 0)
|
||||
{
|
||||
} else if (std::memcmp(p, "data", 4) == 0) {
|
||||
const std::size_t avail = all.size() - body;
|
||||
const std::size_t n = std::min<std::size_t>(chunk_size, avail);
|
||||
out.pcm.assign(all.begin() + body, all.begin() + body + n);
|
||||
@@ -136,8 +125,7 @@ inline bool wav_read(const std::wstring& path, WavData& out)
|
||||
// wrap `pos` on a 32-bit size_t (x86) and spin the loop on garbage, and there's nothing valid
|
||||
// past a chunk that claims more than the file holds anyway.
|
||||
const std::size_t advance = static_cast<std::size_t>(chunk_size) + (chunk_size & 1);
|
||||
if (advance > all.size() - body)
|
||||
{
|
||||
if (advance > all.size() - body) {
|
||||
break;
|
||||
}
|
||||
pos = body + advance;
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
#include "rate_estimator.hpp"
|
||||
#include "vtable_hook.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
// COM vtable indices (frozen ABI). IUnknown occupies 0..2.
|
||||
// IMMDevice: Activate = 3
|
||||
@@ -50,8 +48,7 @@ using ReleaseBufferFn = HRESULT(STDMETHODCALLTYPE*)(IAudioRenderClient*, UINT32,
|
||||
// Swapping the slot leaves the original code untouched.
|
||||
|
||||
// The scalar audio format we forward; resolved from the game's WAVEFORMATEX.
|
||||
struct CapturedFormat
|
||||
{
|
||||
struct CapturedFormat {
|
||||
std::uint32_t rate = 0;
|
||||
std::uint32_t channels = 0;
|
||||
std::uint32_t bits = 0;
|
||||
@@ -126,8 +123,7 @@ CapturedFormat g_stream_formats[kMaxAudioStreams];
|
||||
std::unordered_map<IAudioClient*, CapturedFormat> g_client_formats;
|
||||
|
||||
// Streams we track (frame counting + per-stream capture). Index 0 is primary.
|
||||
struct TrackedStream
|
||||
{
|
||||
struct TrackedStream {
|
||||
std::atomic<IAudioRenderClient*> client{nullptr};
|
||||
std::atomic<std::uint64_t> frames{0};
|
||||
std::atomic<std::uint32_t> block_align{0}; // hot-path frame size for this stream
|
||||
@@ -183,20 +179,15 @@ CapturedFormat capture_format(const WAVEFORMATEX* wfx)
|
||||
cf.bits = wfx->wBitsPerSample;
|
||||
cf.block_align = wfx->nBlockAlign;
|
||||
cf.tag = wfx->wFormatTag;
|
||||
if (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE && wfx->cbSize >= 22)
|
||||
{
|
||||
if (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE && wfx->cbSize >= 22) {
|
||||
const auto* ext = reinterpret_cast<const WAVEFORMATEXTENSIBLE*>(wfx);
|
||||
if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
|
||||
{
|
||||
if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
|
||||
cf.tag = WAVE_FORMAT_IEEE_FLOAT;
|
||||
}
|
||||
else if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_PCM)
|
||||
{
|
||||
} else if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
|
||||
cf.tag = WAVE_FORMAT_PCM;
|
||||
}
|
||||
}
|
||||
if (cf.block_align == 0)
|
||||
{
|
||||
if (cf.block_align == 0) {
|
||||
cf.block_align = cf.channels * (cf.bits / 8);
|
||||
}
|
||||
return cf;
|
||||
@@ -214,13 +205,10 @@ void try_register_lazy(IAudioRenderClient* rc);
|
||||
std::uint32_t readable_bytes(const void* ptr, std::uint32_t want)
|
||||
{
|
||||
MEMORY_BASIC_INFORMATION mbi{};
|
||||
if (VirtualQuery(ptr, &mbi, sizeof(mbi)) == sizeof(mbi) && mbi.State == MEM_COMMIT)
|
||||
{
|
||||
if (VirtualQuery(ptr, &mbi, sizeof(mbi)) == sizeof(mbi) && mbi.State == MEM_COMMIT) {
|
||||
const auto* base = static_cast<const std::uint8_t*>(mbi.BaseAddress);
|
||||
const auto avail = static_cast<std::uintptr_t>((base + mbi.RegionSize) -
|
||||
static_cast<const std::uint8_t*>(ptr));
|
||||
if (avail < want)
|
||||
{
|
||||
const auto avail = static_cast<std::uintptr_t>((base + mbi.RegionSize) - static_cast<const std::uint8_t*>(ptr));
|
||||
if (avail < want) {
|
||||
return static_cast<std::uint32_t>(avail);
|
||||
}
|
||||
}
|
||||
@@ -232,8 +220,7 @@ HRESULT STDMETHODCALLTYPE hk_GetBuffer(IAudioRenderClient* self, UINT32 num_fram
|
||||
DetourGate::Guard guard(g_gate); // in-flight until return (drained before an unhook tears down)
|
||||
hook_note_call(g_id_getbuffer);
|
||||
const HRESULT hr = g_vh_getbuffer.original<GetBufferFn>()(self, num_frames, data);
|
||||
if (SUCCEEDED(hr) && data != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(hr) && data != nullptr) {
|
||||
t_gb_client = self;
|
||||
t_gb_data = *data;
|
||||
t_gb_frames = num_frames;
|
||||
@@ -249,37 +236,32 @@ HRESULT STDMETHODCALLTYPE hk_ReleaseBuffer(IAudioRenderClient* self, UINT32 num_
|
||||
// A render client we've never seen actively rendering is almost certainly one
|
||||
// the game created before we injected; adopt it now (the first becomes the
|
||||
// primary we capture). Skip our own silent probe client.
|
||||
if (self != g_self_render.load(std::memory_order_acquire) && num_frames > 0 && !stream_tracked(self))
|
||||
{
|
||||
if (self != g_self_render.load(std::memory_order_acquire) && num_frames > 0 && !stream_tracked(self)) {
|
||||
try_register_lazy(self);
|
||||
}
|
||||
|
||||
// Per tracked stream: count frames (debug view) and, into the stream's own ring,
|
||||
// capture + silence its buffer while capture is enabled. Every stream is captured
|
||||
// into its own ring; the host mixes them.
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
if (g_streams[i].client.load(std::memory_order_acquire) != self)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i) {
|
||||
if (g_streams[i].client.load(std::memory_order_acquire) != self) {
|
||||
continue;
|
||||
}
|
||||
const std::uint64_t total =
|
||||
g_streams[i].frames.fetch_add(num_frames, std::memory_order_relaxed) + num_frames;
|
||||
const std::uint64_t total = g_streams[i].frames.fetch_add(num_frames, std::memory_order_relaxed) + num_frames;
|
||||
if (IpcClient* ipc = g_ipc.load(std::memory_order_acquire)) // load once (unhook may null it)
|
||||
{
|
||||
ipc->note_audio_frames(i, total);
|
||||
}
|
||||
|
||||
if (num_frames > 0 && (flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0)
|
||||
{
|
||||
if (num_frames > 0 && (flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0) {
|
||||
AudioRingHeader* ring = g_rings[i].load(std::memory_order_acquire);
|
||||
// Only capture once the format is published -- for a guessed-rate stream that's
|
||||
// after the true rate is measured, so we never capture/silence audio we'd
|
||||
// mis-rate (and don't build a backlog while measuring; the game stays audible).
|
||||
if (ring != nullptr && ring->capture_enabled.load(std::memory_order_relaxed) != 0 &&
|
||||
audio_ring_format_ready(*ring) && t_gb_client == self && t_gb_data != nullptr &&
|
||||
t_gb_frames == num_frames &&
|
||||
t_gb_epoch == g_hook_epoch.load(std::memory_order_acquire)) // same hooked epoch as the GetBuffer
|
||||
if (ring != nullptr && ring->capture_enabled.load(std::memory_order_relaxed) != 0
|
||||
&& audio_ring_format_ready(*ring) && t_gb_client == self && t_gb_data != nullptr
|
||||
&& t_gb_frames == num_frames
|
||||
&& t_gb_epoch == g_hook_epoch.load(std::memory_order_acquire)) // same hooked epoch as the GetBuffer
|
||||
{
|
||||
const bool guessed = g_streams[i].assumed_format.load(std::memory_order_relaxed) != 0;
|
||||
const std::uint32_t block = g_streams[i].block_align.load(std::memory_order_relaxed);
|
||||
@@ -287,15 +269,13 @@ HRESULT STDMETHODCALLTYPE hk_ReleaseBuffer(IAudioRenderClient* self, UINT32 num_
|
||||
// A guessed (pre-existing client) stream's block may be larger than the real
|
||||
// per-frame size, so clamp the COPY to what's actually readable -- never over-read
|
||||
// the game's buffer (no-op when the guess is right).
|
||||
if (guessed)
|
||||
{
|
||||
if (guessed) {
|
||||
bytes = readable_bytes(t_gb_data, bytes);
|
||||
}
|
||||
// Only silence if the frames made it into the ring; if the host has
|
||||
// stalled (ring full) keep playing locally rather than going dead
|
||||
// silent — degrades to today's echo, never to silence.
|
||||
if (block != 0 && audio_ring_push(*ring, t_gb_data, bytes, num_frames))
|
||||
{
|
||||
if (block != 0 && audio_ring_push(*ring, t_gb_data, bytes, num_frames)) {
|
||||
g_frames_captured.fetch_add(num_frames, std::memory_order_relaxed);
|
||||
// Mute the game's local playback so the only audio is the host's re-render.
|
||||
// Otherwise the game plays locally AND the mirror re-renders the same audio a
|
||||
@@ -306,13 +286,12 @@ HRESULT STDMETHODCALLTYPE hk_ReleaseBuffer(IAudioRenderClient* self, UINT32 num_
|
||||
// format we additionally zero the buffer (belt-and-suspenders; `block` is the
|
||||
// real frame size there, so it stays in-bounds). Only mutes once the frames made
|
||||
// the ring (above) -- a stalled host degrades to echo, never to dead silence.
|
||||
if (!guessed)
|
||||
{
|
||||
if (!guessed) {
|
||||
std::memset(t_gb_data, 0, bytes);
|
||||
}
|
||||
g_frames_silenced.fetch_add(num_frames, std::memory_order_relaxed);
|
||||
return g_vh_releasebuffer.original<ReleaseBufferFn>()(
|
||||
self, num_frames, flags | AUDCLNT_BUFFERFLAGS_SILENT);
|
||||
return g_vh_releasebuffer.original<ReleaseBufferFn>()(self, num_frames,
|
||||
flags | AUDCLNT_BUFFERFLAGS_SILENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -323,18 +302,14 @@ HRESULT STDMETHODCALLTYPE hk_ReleaseBuffer(IAudioRenderClient* self, UINT32 num_
|
||||
// audio and cross-correlate them to recover the true format from ground truth. The game
|
||||
// stays audible (the host runs loopback during the measurement window anyway), and the
|
||||
// shipping no-echo capture/silence path above is left completely untouched.
|
||||
if (num_frames > 0 && (flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0)
|
||||
{
|
||||
if (num_frames > 0 && (flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0) {
|
||||
AudioRingHeader* vring = g_rings[i].load(std::memory_order_acquire);
|
||||
if (vring != nullptr && vring->verify_capture.load(std::memory_order_relaxed) != 0 &&
|
||||
!audio_ring_format_ready(*vring) &&
|
||||
g_streams[i].assumed_format.load(std::memory_order_relaxed) != 0 && t_gb_client == self &&
|
||||
t_gb_data != nullptr && t_gb_frames == num_frames &&
|
||||
t_gb_epoch == g_hook_epoch.load(std::memory_order_acquire))
|
||||
{
|
||||
if (vring != nullptr && vring->verify_capture.load(std::memory_order_relaxed) != 0
|
||||
&& !audio_ring_format_ready(*vring) && g_streams[i].assumed_format.load(std::memory_order_relaxed) != 0
|
||||
&& t_gb_client == self && t_gb_data != nullptr && t_gb_frames == num_frames
|
||||
&& t_gb_epoch == g_hook_epoch.load(std::memory_order_acquire)) {
|
||||
const std::uint32_t block = g_streams[i].block_align.load(std::memory_order_relaxed);
|
||||
if (block != 0)
|
||||
{
|
||||
if (block != 0) {
|
||||
// Self-describing chunk: [u32 frame-count][num_frames*block bytes]. The host can't
|
||||
// know the real frame size of a guessed stream, so it recovers the layout by
|
||||
// trying candidate de-interleavings -- but it needs the frame count to strip the
|
||||
@@ -342,9 +317,8 @@ HRESULT STDMETHODCALLTYPE hk_ReleaseBuffer(IAudioRenderClient* self, UINT32 num_
|
||||
// channels/bits). Push both parts only if both fit and the payload is fully
|
||||
// readable, so a full ring or a short buffer can never tear the framing.
|
||||
const std::uint32_t want = num_frames * block;
|
||||
if (readable_bytes(t_gb_data, want) == want &&
|
||||
audio_ring_free_space(*vring) >= static_cast<std::uint32_t>(sizeof(num_frames)) + want)
|
||||
{
|
||||
if (readable_bytes(t_gb_data, want) == want
|
||||
&& audio_ring_free_space(*vring) >= static_cast<std::uint32_t>(sizeof(num_frames)) + want) {
|
||||
audio_ring_push(*vring, &num_frames, sizeof(num_frames), 0);
|
||||
audio_ring_push(*vring, t_gb_data, want, num_frames);
|
||||
}
|
||||
@@ -358,12 +332,10 @@ HRESULT STDMETHODCALLTYPE hk_ReleaseBuffer(IAudioRenderClient* self, UINT32 num_
|
||||
|
||||
// Publish a stream's format + state to the host's per-stream debug channel. Caller holds
|
||||
// g_setup_mutex.
|
||||
void publish_stream_info_locked(std::uint32_t slot, const CapturedFormat& cf, std::uint32_t state,
|
||||
std::uint64_t frames)
|
||||
void publish_stream_info_locked(std::uint32_t slot, const CapturedFormat& cf, std::uint32_t state, std::uint64_t frames)
|
||||
{
|
||||
IpcClient* ipc = g_ipc.load(std::memory_order_acquire);
|
||||
if (ipc == nullptr)
|
||||
{
|
||||
if (ipc == nullptr) {
|
||||
return;
|
||||
}
|
||||
AudioStreamInfo info{};
|
||||
@@ -384,35 +356,28 @@ void publish_stream_info_locked(std::uint32_t slot, const CapturedFormat& cf, st
|
||||
bool publish_stream_format_locked(std::uint32_t slot)
|
||||
{
|
||||
AudioRingHeader* ring = g_rings[slot].load(std::memory_order_acquire);
|
||||
if (ring == nullptr || g_stream_formats[slot].rate == 0)
|
||||
{
|
||||
if (ring == nullptr || g_stream_formats[slot].rate == 0) {
|
||||
return false; // no ring attached yet, or no stream in this slot
|
||||
}
|
||||
if (audio_ring_format_ready(*ring))
|
||||
{
|
||||
if (audio_ring_format_ready(*ring)) {
|
||||
return true; // already published
|
||||
}
|
||||
CapturedFormat cf = g_stream_formats[slot];
|
||||
if (g_stream_rate_guess[slot])
|
||||
{
|
||||
if (g_stream_rate_guess[slot]) {
|
||||
// Feed this tick's render cadence to the estimator; it only commits on consensus
|
||||
// across standard-rate windows, or a low-confidence fallback after enough attempts.
|
||||
LARGE_INTEGER now{}, freq{};
|
||||
QueryPerformanceCounter(&now);
|
||||
QueryPerformanceFrequency(&freq);
|
||||
const RateEstimate est = g_rate_estimator[slot].feed(
|
||||
g_streams[slot].frames.load(std::memory_order_relaxed), now.QuadPart, freq.QuadPart);
|
||||
if (!est.done)
|
||||
{
|
||||
const RateEstimate est = g_rate_estimator[slot].feed(g_streams[slot].frames.load(std::memory_order_relaxed),
|
||||
now.QuadPart, freq.QuadPart);
|
||||
if (!est.done) {
|
||||
return false; // still measuring; caller retries next tick
|
||||
}
|
||||
const std::uint32_t state = est.confident ? AudioFormat_Measured : AudioFormat_LowConfidence;
|
||||
if (est.confident)
|
||||
{
|
||||
if (est.confident) {
|
||||
logf("audio stream %u: measured rate %uHz (was guessing %uHz)", slot, est.rate, cf.rate);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
logw("audio stream %u: rate %uHz is a LOW-CONFIDENCE estimate (no consensus) -- verify or "
|
||||
"override",
|
||||
slot, est.rate);
|
||||
@@ -435,12 +400,10 @@ bool publish_stream_format_locked(std::uint32_t slot)
|
||||
void apply_audio_op_locked(std::uint32_t slot, const AudioRingOpCmd& cmd)
|
||||
{
|
||||
AudioRingHeader* ring = g_rings[slot].load(std::memory_order_acquire);
|
||||
if (ring == nullptr || g_stream_formats[slot].rate == 0)
|
||||
{
|
||||
if (ring == nullptr || g_stream_formats[slot].rate == 0) {
|
||||
return; // no ring / no stream in this slot
|
||||
}
|
||||
if (cmd.kind == AudioRingOp_Remeasure)
|
||||
{
|
||||
if (cmd.kind == AudioRingOp_Remeasure) {
|
||||
logw("audio stream %u: operator requested re-measure", slot);
|
||||
g_stream_rate_guess[slot] = true;
|
||||
g_rate_estimator[slot] = RateEstimator{};
|
||||
@@ -450,23 +413,19 @@ void apply_audio_op_locked(std::uint32_t slot, const AudioRingOpCmd& cmd)
|
||||
ring->format_valid.store(0, std::memory_order_release); // force re-publish after measuring
|
||||
publish_stream_info_locked(slot, g_stream_formats[slot], AudioFormat_Measuring,
|
||||
g_streams[slot].frames.load(std::memory_order_relaxed));
|
||||
}
|
||||
else if (cmd.kind == AudioRingOp_Override)
|
||||
{
|
||||
} else if (cmd.kind == AudioRingOp_Override) {
|
||||
CapturedFormat cf;
|
||||
cf.rate = cmd.rate;
|
||||
cf.channels = cmd.channels;
|
||||
cf.bits = cmd.bits;
|
||||
cf.tag = cmd.format_tag ? cmd.format_tag : WAVE_FORMAT_PCM;
|
||||
cf.block_align = cmd.channels * (cmd.bits / 8);
|
||||
if (cf.rate == 0 || cf.channels == 0 || cf.block_align == 0)
|
||||
{
|
||||
logw("audio stream %u: ignoring invalid override %uHz/%uch/%ubit", slot, cf.rate, cf.channels,
|
||||
cf.bits);
|
||||
if (cf.rate == 0 || cf.channels == 0 || cf.block_align == 0) {
|
||||
logw("audio stream %u: ignoring invalid override %uHz/%uch/%ubit", slot, cf.rate, cf.channels, cf.bits);
|
||||
return;
|
||||
}
|
||||
logw("audio stream %u: operator override -> %uHz/%uch/%ubit tag=%u", slot, cf.rate, cf.channels,
|
||||
cf.bits, cf.tag);
|
||||
logw("audio stream %u: operator override -> %uHz/%uch/%ubit tag=%u", slot, cf.rate, cf.channels, cf.bits,
|
||||
cf.tag);
|
||||
g_stream_formats[slot] = cf;
|
||||
g_stream_rate_guess[slot] = false;
|
||||
g_stream_format_state[slot] = AudioFormat_Override;
|
||||
@@ -487,25 +446,21 @@ void apply_audio_op_locked(std::uint32_t slot, const AudioRingOpCmd& cmd)
|
||||
// the format is published). Caller holds g_setup_mutex.
|
||||
void register_render_client_locked(IAudioRenderClient* rc, const CapturedFormat& cf, bool rate_is_guess)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
if (g_streams[i].client.load(std::memory_order_relaxed) == rc)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i) {
|
||||
if (g_streams[i].client.load(std::memory_order_relaxed) == rc) {
|
||||
return; // already tracked
|
||||
}
|
||||
}
|
||||
|
||||
const std::uint32_t seen = g_streams_seen.fetch_add(1, std::memory_order_relaxed) + 1;
|
||||
if (IpcClient* ipc = g_ipc.load(std::memory_order_acquire))
|
||||
{
|
||||
if (IpcClient* ipc = g_ipc.load(std::memory_order_acquire)) {
|
||||
ipc->set_audio_streams_seen(seen);
|
||||
}
|
||||
logf("register_render_client: rc=%p seen=%u fmt=%uHz/%uch/%ubit tag=%u block=%u", rc, seen, cf.rate,
|
||||
cf.channels, cf.bits, cf.tag, cf.block_align);
|
||||
logf("register_render_client: rc=%p seen=%u fmt=%uHz/%uch/%ubit tag=%u block=%u", rc, seen, cf.rate, cf.channels,
|
||||
cf.bits, cf.tag, cf.block_align);
|
||||
|
||||
const std::uint32_t slot = g_registered;
|
||||
if (slot >= kMaxAudioStreams)
|
||||
{
|
||||
if (slot >= kMaxAudioStreams) {
|
||||
return; // more streams than debug slots; counted above, not detailed
|
||||
}
|
||||
g_registered = slot + 1;
|
||||
@@ -520,16 +475,13 @@ void register_render_client_locked(IAudioRenderClient* rc, const CapturedFormat&
|
||||
g_streams[slot].block_align.store(cf.block_align, std::memory_order_relaxed); // before client (hot path)
|
||||
g_streams[slot].client.store(rc, std::memory_order_release);
|
||||
|
||||
if (rate_is_guess)
|
||||
{
|
||||
if (rate_is_guess) {
|
||||
logf("audio stream %u: format unknown (pre-existing client) -> assuming device mix %uHz/%uch/%ubit; "
|
||||
"measuring true rate; channels/bits assumed (verified byte-compatible before capture)",
|
||||
slot, cf.rate, cf.channels, cf.bits);
|
||||
}
|
||||
else
|
||||
{
|
||||
logf("audio stream %u: exact format %uHz/%uch/%ubit from the game's Initialize", slot, cf.rate,
|
||||
cf.channels, cf.bits);
|
||||
} else {
|
||||
logf("audio stream %u: exact format %uHz/%uch/%ubit from the game's Initialize", slot, cf.rate, cf.channels,
|
||||
cf.bits);
|
||||
}
|
||||
publish_stream_info_locked(slot, cf, state, 0);
|
||||
|
||||
@@ -544,10 +496,8 @@ void register_render_client_locked(IAudioRenderClient* rc, const CapturedFormat&
|
||||
// True if `rc` already occupies a tracked debug slot (lock-free scan).
|
||||
bool stream_tracked(IAudioRenderClient* rc)
|
||||
{
|
||||
for (auto& s : g_streams)
|
||||
{
|
||||
if (s.client.load(std::memory_order_acquire) == rc)
|
||||
{
|
||||
for (auto& s : g_streams) {
|
||||
if (s.client.load(std::memory_order_acquire) == rc) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -559,17 +509,14 @@ bool stream_tracked(IAudioRenderClient* rc)
|
||||
// as a best guess. Non-blocking: if setup is momentarily busy, retry next call.
|
||||
void try_register_lazy(IAudioRenderClient* rc)
|
||||
{
|
||||
if (g_have_mix_format.load(std::memory_order_acquire) == 0)
|
||||
{
|
||||
if (g_have_mix_format.load(std::memory_order_acquire) == 0) {
|
||||
return;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(g_setup_mutex, std::try_to_lock);
|
||||
if (!lock.owns_lock())
|
||||
{
|
||||
if (!lock.owns_lock()) {
|
||||
return; // another thread is in setup; try again on the next buffer
|
||||
}
|
||||
if (stream_tracked(rc))
|
||||
{
|
||||
if (stream_tracked(rc)) {
|
||||
return; // a concurrent path registered it first
|
||||
}
|
||||
logf("try_register_lazy: discovered pre-existing render client rc=%p", rc);
|
||||
@@ -581,12 +528,11 @@ HRESULT STDMETHODCALLTYPE hk_Initialize(IAudioClient* self, AUDCLNT_SHAREMODE mo
|
||||
const WAVEFORMATEX* format, LPCGUID session)
|
||||
{
|
||||
hook_note_call(g_id_initialize);
|
||||
const HRESULT hr = g_vh_initialize.original<InitializeFn>()(self, mode, flags, buffer_duration,
|
||||
periodicity, format, session);
|
||||
const HRESULT hr =
|
||||
g_vh_initialize.original<InitializeFn>()(self, mode, flags, buffer_duration, periodicity, format, session);
|
||||
logf("hk_Initialize: client=%p mode=%d flags=0x%lX hr=0x%08lX fmt=%s", self, mode,
|
||||
static_cast<unsigned long>(flags), static_cast<unsigned long>(hr), format ? "yes" : "null");
|
||||
if (SUCCEEDED(hr) && format != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(hr) && format != nullptr) {
|
||||
std::scoped_lock lock(g_setup_mutex);
|
||||
g_client_formats[self] = capture_format(format);
|
||||
}
|
||||
@@ -600,33 +546,28 @@ HRESULT STDMETHODCALLTYPE hk_GetService(IAudioClient* self, REFIID riid, void**
|
||||
const bool is_render = (riid == __uuidof(IAudioRenderClient));
|
||||
logf("hk_GetService: client=%p hr=0x%08lX render_client=%d", self, static_cast<unsigned long>(hr),
|
||||
is_render ? 1 : 0);
|
||||
if (SUCCEEDED(hr) && ppv != nullptr && *ppv != nullptr && riid == __uuidof(IAudioRenderClient))
|
||||
{
|
||||
if (SUCCEEDED(hr) && ppv != nullptr && *ppv != nullptr && riid == __uuidof(IAudioRenderClient)) {
|
||||
CapturedFormat cf;
|
||||
bool have = false;
|
||||
{
|
||||
std::scoped_lock lock(g_setup_mutex);
|
||||
auto it = g_client_formats.find(self);
|
||||
if (it != g_client_formats.end())
|
||||
{
|
||||
if (it != g_client_formats.end()) {
|
||||
cf = it->second;
|
||||
have = true;
|
||||
}
|
||||
}
|
||||
// Fallback for IAudioClient3::InitializeSharedAudioStream (no Initialize
|
||||
// format): the shared-mode format is the device mix format.
|
||||
if (!have)
|
||||
{
|
||||
if (!have) {
|
||||
WAVEFORMATEX* mix = nullptr;
|
||||
if (SUCCEEDED(self->GetMixFormat(&mix)) && mix != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(self->GetMixFormat(&mix)) && mix != nullptr) {
|
||||
cf = capture_format(mix);
|
||||
have = true;
|
||||
CoTaskMemFree(mix);
|
||||
}
|
||||
}
|
||||
if (have)
|
||||
{
|
||||
if (have) {
|
||||
std::scoped_lock lock(g_setup_mutex);
|
||||
// We saw this client's Initialize (or its shared-mode mix format), so the rate
|
||||
// is exact, not a guess.
|
||||
@@ -639,28 +580,25 @@ HRESULT STDMETHODCALLTYPE hk_GetService(IAudioClient* self, REFIID riid, void**
|
||||
void install_audioclient_hooks(IAudioClient* ac)
|
||||
{
|
||||
std::scoped_lock lock(g_setup_mutex);
|
||||
if (g_audioclient_hooked)
|
||||
{
|
||||
if (g_audioclient_hooked) {
|
||||
return; // shared vtable: hook the first IAudioClient we see, covers all
|
||||
}
|
||||
g_vh_initialize.install(ac, kIdx_IAudioClient_Initialize, reinterpret_cast<void*>(&hk_Initialize));
|
||||
g_vh_getservice.install(ac, kIdx_IAudioClient_GetService, reinterpret_cast<void*>(&hk_GetService));
|
||||
g_audioclient_hooked = (static_cast<bool>(g_vh_initialize) && static_cast<bool>(g_vh_getservice));
|
||||
logf("install_audioclient_hooks: ac=%p initialize=%d getservice=%d", ac,
|
||||
static_cast<bool>(g_vh_initialize) ? 1 : 0, static_cast<bool>(g_vh_getservice) ? 1 : 0);
|
||||
logf("install_audioclient_hooks: ac=%p initialize=%d getservice=%d", ac, static_cast<bool>(g_vh_initialize) ? 1 : 0,
|
||||
static_cast<bool>(g_vh_getservice) ? 1 : 0);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE hk_Activate(IMMDevice* self, REFIID riid, DWORD cls_ctx, PROPVARIANT* params,
|
||||
void** ppv)
|
||||
HRESULT STDMETHODCALLTYPE hk_Activate(IMMDevice* self, REFIID riid, DWORD cls_ctx, PROPVARIANT* params, void** ppv)
|
||||
{
|
||||
hook_note_call(g_id_activate);
|
||||
const HRESULT hr = g_vh_activate.original<ActivateFn>()(self, riid, cls_ctx, params, ppv);
|
||||
const bool is_audioclient = (riid == __uuidof(IAudioClient) || riid == __uuidof(IAudioClient2) ||
|
||||
riid == __uuidof(IAudioClient3));
|
||||
const bool is_audioclient =
|
||||
(riid == __uuidof(IAudioClient) || riid == __uuidof(IAudioClient2) || riid == __uuidof(IAudioClient3));
|
||||
logf("hk_Activate: device=%p hr=0x%08lX audioclient=%d", self, static_cast<unsigned long>(hr),
|
||||
is_audioclient ? 1 : 0);
|
||||
if (SUCCEEDED(hr) && ppv != nullptr && *ppv != nullptr && is_audioclient)
|
||||
{
|
||||
if (SUCCEEDED(hr) && ppv != nullptr && *ppv != nullptr && is_audioclient) {
|
||||
install_audioclient_hooks(static_cast<IAudioClient*>(*ppv));
|
||||
}
|
||||
return hr;
|
||||
@@ -668,62 +606,51 @@ HRESULT STDMETHODCALLTYPE hk_Activate(IMMDevice* self, REFIID riid, DWORD cls_ct
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
// Build the probe COM objects (enumerator -> device -> client -> render) and capture the
|
||||
// device mix format. Created ONCE and kept for the DLL's lifetime: every instance of a
|
||||
// coclass shares one vtable, so a toggle then only re-swaps vtable slots on these kept
|
||||
// objects -- no COM create/destroy churn (which races AudioSes). Caller holds g_setup_mutex.
|
||||
bool build_probe_locked()
|
||||
{
|
||||
if (g_self_device != nullptr)
|
||||
{
|
||||
if (g_self_device != nullptr) {
|
||||
return true; // already built
|
||||
}
|
||||
IMMDeviceEnumerator* enumerator = nullptr;
|
||||
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL,
|
||||
__uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&enumerator))))
|
||||
{
|
||||
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
|
||||
reinterpret_cast<void**>(&enumerator)))) {
|
||||
return false;
|
||||
}
|
||||
IMMDevice* device = nullptr;
|
||||
const HRESULT hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
|
||||
enumerator->Release(); // only needed to reach the device
|
||||
if (FAILED(hr) || device == nullptr)
|
||||
{
|
||||
if (FAILED(hr) || device == nullptr) {
|
||||
return false;
|
||||
}
|
||||
g_self_device = device; // kept alive (Activate hook re-installs from its vtable)
|
||||
|
||||
IAudioRenderClient* self_render = nullptr;
|
||||
HRESULT ah = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr,
|
||||
reinterpret_cast<void**>(&g_self_client));
|
||||
if (SUCCEEDED(ah) && g_self_client != nullptr)
|
||||
{
|
||||
HRESULT ah =
|
||||
device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&g_self_client));
|
||||
if (SUCCEEDED(ah) && g_self_client != nullptr) {
|
||||
WAVEFORMATEX* mix = nullptr;
|
||||
if (SUCCEEDED(g_self_client->GetMixFormat(&mix)) && mix != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(g_self_client->GetMixFormat(&mix)) && mix != nullptr) {
|
||||
g_mix_format = capture_format(mix);
|
||||
g_have_mix_format.store(1, std::memory_order_release);
|
||||
constexpr REFERENCE_TIME kBuf = 10 * 10000; // 10 ms; never started
|
||||
HRESULT ih = g_self_client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, kBuf, 0, mix, nullptr);
|
||||
if (SUCCEEDED(ih))
|
||||
{
|
||||
ih = g_self_client->GetService(__uuidof(IAudioRenderClient),
|
||||
reinterpret_cast<void**>(&self_render));
|
||||
if (SUCCEEDED(ih)) {
|
||||
ih = g_self_client->GetService(__uuidof(IAudioRenderClient), reinterpret_cast<void**>(&self_render));
|
||||
}
|
||||
logf("build_probe: client init=0x%08lX render=%p mix=%uHz/%uch/%ubit tag=%u",
|
||||
static_cast<unsigned long>(ih), self_render, g_mix_format.rate, g_mix_format.channels,
|
||||
g_mix_format.bits, g_mix_format.tag);
|
||||
CoTaskMemFree(mix);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
logf("build_probe: Activate(IAudioClient) failed hr=0x%08lX", static_cast<unsigned long>(ah));
|
||||
}
|
||||
if (self_render != nullptr)
|
||||
{
|
||||
if (self_render != nullptr) {
|
||||
g_self_render.store(self_render, std::memory_order_release);
|
||||
}
|
||||
return true; // device built; render may be null on odd setups (Activate hook still works)
|
||||
@@ -733,18 +660,14 @@ bool build_probe_locked()
|
||||
// g_setup_mutex.
|
||||
void install_detours_locked()
|
||||
{
|
||||
if (g_self_device == nullptr)
|
||||
{
|
||||
if (g_self_device == nullptr) {
|
||||
return;
|
||||
}
|
||||
g_hook_epoch.fetch_add(1, std::memory_order_release); // new epoch: invalidate any straddling GetBuffer
|
||||
g_vh_activate.install(g_self_device, kIdx_IMMDevice_Activate, reinterpret_cast<void*>(&hk_Activate));
|
||||
if (IAudioRenderClient* sr = g_self_render.load(std::memory_order_acquire))
|
||||
{
|
||||
g_vh_initialize.install(g_self_client, kIdx_IAudioClient_Initialize,
|
||||
reinterpret_cast<void*>(&hk_Initialize));
|
||||
g_vh_getservice.install(g_self_client, kIdx_IAudioClient_GetService,
|
||||
reinterpret_cast<void*>(&hk_GetService));
|
||||
if (IAudioRenderClient* sr = g_self_render.load(std::memory_order_acquire)) {
|
||||
g_vh_initialize.install(g_self_client, kIdx_IAudioClient_Initialize, reinterpret_cast<void*>(&hk_Initialize));
|
||||
g_vh_getservice.install(g_self_client, kIdx_IAudioClient_GetService, reinterpret_cast<void*>(&hk_GetService));
|
||||
g_vh_getbuffer.install(sr, kIdx_IAudioRenderClient_GetBuffer, reinterpret_cast<void*>(&hk_GetBuffer));
|
||||
g_vh_releasebuffer.install(sr, kIdx_IAudioRenderClient_ReleaseBuffer,
|
||||
reinterpret_cast<void*>(&hk_ReleaseBuffer));
|
||||
@@ -755,10 +678,9 @@ void install_detours_locked()
|
||||
hook_set_installed(g_id_getservice, static_cast<bool>(g_vh_getservice));
|
||||
hook_set_installed(g_id_getbuffer, static_cast<bool>(g_vh_getbuffer));
|
||||
hook_set_installed(g_id_releasebuffer, static_cast<bool>(g_vh_releasebuffer));
|
||||
logf("install_detours: activate=%d init=%d getsvc=%d getbuf=%d relbuf=%d",
|
||||
static_cast<bool>(g_vh_activate) ? 1 : 0, static_cast<bool>(g_vh_initialize) ? 1 : 0,
|
||||
static_cast<bool>(g_vh_getservice) ? 1 : 0, static_cast<bool>(g_vh_getbuffer) ? 1 : 0,
|
||||
static_cast<bool>(g_vh_releasebuffer) ? 1 : 0);
|
||||
logf("install_detours: activate=%d init=%d getsvc=%d getbuf=%d relbuf=%d", static_cast<bool>(g_vh_activate) ? 1 : 0,
|
||||
static_cast<bool>(g_vh_initialize) ? 1 : 0, static_cast<bool>(g_vh_getservice) ? 1 : 0,
|
||||
static_cast<bool>(g_vh_getbuffer) ? 1 : 0, static_cast<bool>(g_vh_releasebuffer) ? 1 : 0);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -767,8 +689,7 @@ bool install_audio_hooks(IpcClient& ipc, AudioRingHeader* ring)
|
||||
std::scoped_lock lock(g_setup_mutex);
|
||||
g_ipc.store(&ipc, std::memory_order_release);
|
||||
g_rings[0].store(ring, std::memory_order_release);
|
||||
if (g_vh_activate)
|
||||
{
|
||||
if (g_vh_activate) {
|
||||
return true; // detours already installed
|
||||
}
|
||||
if (g_id_activate < 0) // register the hook-list ids once
|
||||
@@ -790,17 +711,14 @@ bool install_audio_hooks(IpcClient& ipc, AudioRingHeader* ring)
|
||||
void republish_audio_format()
|
||||
{
|
||||
std::scoped_lock lock(g_setup_mutex);
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i) {
|
||||
AudioRingHeader* ring = g_rings[i].load(std::memory_order_acquire);
|
||||
if (ring == nullptr)
|
||||
{
|
||||
if (ring == nullptr) {
|
||||
continue;
|
||||
}
|
||||
// Apply any operator command (re-measure / override) the host posted on this ring.
|
||||
AudioRingOpCmd cmd;
|
||||
if (audio_ring_poll_op(*ring, g_last_op_seq[i], cmd) != AudioRingOp_None)
|
||||
{
|
||||
if (audio_ring_poll_op(*ring, g_last_op_seq[i], cmd) != AudioRingOp_None) {
|
||||
apply_audio_op_locked(i, cmd);
|
||||
}
|
||||
// Publishes an exact format immediately; a guessed rate is measured first and
|
||||
@@ -811,15 +729,13 @@ void republish_audio_format()
|
||||
|
||||
void set_audio_ring(unsigned index, AudioRingHeader* ring)
|
||||
{
|
||||
if (index >= kMaxAudioStreams)
|
||||
{
|
||||
if (index >= kMaxAudioStreams) {
|
||||
return;
|
||||
}
|
||||
// The worker thread re-attaches every tick (idempotent); only log when the ring
|
||||
// pointer actually changes so the log isn't flooded with identical lines.
|
||||
AudioRingHeader* const prev = g_rings[index].exchange(ring, std::memory_order_acq_rel);
|
||||
if (prev != ring)
|
||||
{
|
||||
if (prev != ring) {
|
||||
logf("set_audio_ring: index=%u ring=%p capture_enabled=%u", index, ring,
|
||||
ring ? ring->capture_enabled.load(std::memory_order_relaxed) : 0u);
|
||||
}
|
||||
@@ -836,7 +752,8 @@ void remove_audio_hooks()
|
||||
// objects alive -- a re-enable just re-swaps the slots, no COM churn. The probe is only
|
||||
// released on detach (shutdown_audio_hooks). m_original stays valid (see VtableHook), so
|
||||
// an in-flight detour on the audio thread completes safely after the restore.
|
||||
g_hook_epoch.fetch_add(1, std::memory_order_release); // new epoch: a later capture won't trust a pre-toggle GetBuffer
|
||||
g_hook_epoch.fetch_add(1,
|
||||
std::memory_order_release); // new epoch: a later capture won't trust a pre-toggle GetBuffer
|
||||
g_vh_releasebuffer.remove();
|
||||
g_vh_getbuffer.remove();
|
||||
g_vh_getservice.remove();
|
||||
@@ -858,8 +775,7 @@ void remove_audio_hooks()
|
||||
g_streams_seen.store(0, std::memory_order_relaxed);
|
||||
g_frames_captured.store(0, std::memory_order_relaxed);
|
||||
g_frames_silenced.store(0, std::memory_order_relaxed);
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i) {
|
||||
g_streams[i].client.store(nullptr, std::memory_order_relaxed);
|
||||
g_streams[i].frames.store(0, std::memory_order_relaxed);
|
||||
g_streams[i].block_align.store(0, std::memory_order_relaxed);
|
||||
@@ -880,17 +796,14 @@ void shutdown_audio_hooks()
|
||||
remove_audio_hooks(); // restore vtables + clear state (takes the lock)
|
||||
// Now safe to release the kept probe objects (called only on DLL detach).
|
||||
std::scoped_lock lock(g_setup_mutex);
|
||||
if (IAudioRenderClient* sr = g_self_render.exchange(nullptr, std::memory_order_acq_rel))
|
||||
{
|
||||
if (IAudioRenderClient* sr = g_self_render.exchange(nullptr, std::memory_order_acq_rel)) {
|
||||
sr->Release();
|
||||
}
|
||||
if (g_self_client != nullptr)
|
||||
{
|
||||
if (g_self_client != nullptr) {
|
||||
g_self_client->Release();
|
||||
g_self_client = nullptr;
|
||||
}
|
||||
if (g_self_device != nullptr)
|
||||
{
|
||||
if (g_self_device != nullptr) {
|
||||
g_self_device->Release();
|
||||
g_self_device = nullptr;
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@
|
||||
#include "coop/audio_ring.hpp"
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Installs the render-path hooks. `ipc` must outlive the hooks (used for the
|
||||
// stream-count diagnostics in HookStatus). `ring` may be null — counting still
|
||||
|
||||
@@ -19,11 +19,9 @@
|
||||
#include "shared_video_texture.hpp"
|
||||
#include "vtable_hook.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight Present detours before remove frees the shared D3D state
|
||||
|
||||
@@ -65,14 +63,12 @@ thread_local bool t_in_present = false;
|
||||
|
||||
bool ensure_device()
|
||||
{
|
||||
if (g_device != nullptr)
|
||||
{
|
||||
if (g_device != nullptr) {
|
||||
return true;
|
||||
}
|
||||
const HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0,
|
||||
D3D11_SDK_VERSION, &g_device, nullptr, &g_ctx);
|
||||
if (FAILED(hr) || g_device == nullptr)
|
||||
{
|
||||
const HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0, D3D11_SDK_VERSION,
|
||||
&g_device, nullptr, &g_ctx);
|
||||
if (FAILED(hr) || g_device == nullptr) {
|
||||
logf("d3d9: D3D11CreateDevice failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
return false;
|
||||
}
|
||||
@@ -81,13 +77,11 @@ bool ensure_device()
|
||||
|
||||
void release_sysmem()
|
||||
{
|
||||
if (g_sysmem != nullptr)
|
||||
{
|
||||
if (g_sysmem != nullptr) {
|
||||
g_sysmem->Release();
|
||||
g_sysmem = nullptr;
|
||||
}
|
||||
if (g_sysmem_dev != nullptr)
|
||||
{
|
||||
if (g_sysmem_dev != nullptr) {
|
||||
g_sysmem_dev->Release();
|
||||
g_sysmem_dev = nullptr;
|
||||
}
|
||||
@@ -99,8 +93,7 @@ void release_sysmem()
|
||||
void capture_d3d9(IDirect3DDevice9* dev)
|
||||
{
|
||||
IDirect3DSurface9* back = nullptr;
|
||||
if (FAILED(dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back)) || back == nullptr)
|
||||
{
|
||||
if (FAILED(dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back)) || back == nullptr) {
|
||||
return;
|
||||
}
|
||||
D3DSURFACE_DESC d{};
|
||||
@@ -108,10 +101,8 @@ void capture_d3d9(IDirect3DDevice9* dev)
|
||||
const UINT w = d.Width;
|
||||
const UINT h = d.Height;
|
||||
// We only handle the standard 32-bit BGRX/BGRA back buffers (the common D3D9 case).
|
||||
if ((d.Format != D3DFMT_X8R8G8B8 && d.Format != D3DFMT_A8R8G8B8) || w == 0 || h == 0)
|
||||
{
|
||||
if (!g_unsupported_logged)
|
||||
{
|
||||
if ((d.Format != D3DFMT_X8R8G8B8 && d.Format != D3DFMT_A8R8G8B8) || w == 0 || h == 0) {
|
||||
if (!g_unsupported_logged) {
|
||||
logf("d3d9: unsupported backbuffer format=%d (only X8R8G8B8 / A8R8G8B8); idle", static_cast<int>(d.Format));
|
||||
g_unsupported_logged = true;
|
||||
}
|
||||
@@ -120,12 +111,11 @@ void capture_d3d9(IDirect3DDevice9* dev)
|
||||
}
|
||||
|
||||
// (Re)create the system-memory read-back surface on the game's device.
|
||||
if (!(g_sysmem != nullptr && g_sysmem_dev == dev && g_sysmem_w == w && g_sysmem_h == h && g_sysmem_fmt == d.Format))
|
||||
{
|
||||
if (!(g_sysmem != nullptr && g_sysmem_dev == dev && g_sysmem_w == w && g_sysmem_h == h
|
||||
&& g_sysmem_fmt == d.Format)) {
|
||||
release_sysmem();
|
||||
if (SUCCEEDED(dev->CreateOffscreenPlainSurface(w, h, d.Format, D3DPOOL_SYSTEMMEM, &g_sysmem, nullptr)) &&
|
||||
g_sysmem != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(dev->CreateOffscreenPlainSurface(w, h, d.Format, D3DPOOL_SYSTEMMEM, &g_sysmem, nullptr))
|
||||
&& g_sysmem != nullptr) {
|
||||
g_sysmem_dev = dev;
|
||||
dev->AddRef();
|
||||
g_sysmem_w = w;
|
||||
@@ -138,21 +128,18 @@ void capture_d3d9(IDirect3DDevice9* dev)
|
||||
if (g_sysmem != nullptr && SUCCEEDED(dev->GetRenderTargetData(back, g_sysmem))) // GPU->sysmem, blocks
|
||||
{
|
||||
D3DLOCKED_RECT lr{};
|
||||
if (SUCCEEDED(g_sysmem->LockRect(&lr, nullptr, D3DLOCK_READONLY)) && lr.pBits != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(g_sysmem->LockRect(&lr, nullptr, D3DLOCK_READONLY)) && lr.pBits != nullptr) {
|
||||
const size_t dst_row = static_cast<size_t>(w) * 4;
|
||||
if (g_rgba.size() != dst_row * h)
|
||||
{
|
||||
if (g_rgba.size() != dst_row * h) {
|
||||
g_rgba.resize(dst_row * h);
|
||||
}
|
||||
// X8R8G8B8 / A8R8G8B8 store as little-endian 0xAARRGGBB -> bytes B,G,R,A. Swizzle to
|
||||
// R,G,B,A and force opaque alpha so the host's RGBA decode matches the other backends.
|
||||
for (UINT y = 0; y < h; ++y)
|
||||
{
|
||||
const unsigned char* src = static_cast<const unsigned char*>(lr.pBits) + static_cast<size_t>(y) * lr.Pitch;
|
||||
for (UINT y = 0; y < h; ++y) {
|
||||
const unsigned char* src =
|
||||
static_cast<const unsigned char*>(lr.pBits) + static_cast<size_t>(y) * lr.Pitch;
|
||||
unsigned char* out = g_rgba.data() + static_cast<size_t>(y) * dst_row;
|
||||
for (UINT x = 0; x < w; ++x)
|
||||
{
|
||||
for (UINT x = 0; x < w; ++x) {
|
||||
out[x * 4 + 0] = src[x * 4 + 2]; // R
|
||||
out[x * 4 + 1] = src[x * 4 + 1]; // G
|
||||
out[x * 4 + 2] = src[x * 4 + 0]; // B
|
||||
@@ -162,10 +149,8 @@ void capture_d3d9(IDirect3DDevice9* dev)
|
||||
g_sysmem->UnlockRect();
|
||||
|
||||
// DXGI_FORMAT_R8G8B8A8_UNORM: we swizzle the D3D9 BGRA backbuffer to RGBA above.
|
||||
if (ensure_device() &&
|
||||
g_shared.ensure(g_device, w, h, DXGI_FORMAT_R8G8B8A8_UNORM, g_pid, "d3d9") &&
|
||||
g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK)
|
||||
{
|
||||
if (ensure_device() && g_shared.ensure(g_device, w, h, DXGI_FORMAT_R8G8B8A8_UNORM, g_pid, "d3d9")
|
||||
&& g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK) {
|
||||
g_ctx->UpdateSubresource(g_shared.texture(), 0, nullptr, g_rgba.data(), static_cast<UINT>(dst_row), 0);
|
||||
g_ctx->Flush();
|
||||
g_shared.mutex()->ReleaseSync(kVideoMutexKey);
|
||||
@@ -174,11 +159,9 @@ void capture_d3d9(IDirect3DDevice9* dev)
|
||||
}
|
||||
}
|
||||
|
||||
if (shared)
|
||||
{
|
||||
if (shared) {
|
||||
g_frames_shared.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->publish_video_frame(w, h, static_cast<std::uint32_t>(DXGI_FORMAT_R8G8B8A8_UNORM));
|
||||
}
|
||||
}
|
||||
@@ -191,12 +174,10 @@ HRESULT STDMETHODCALLTYPE hk_Present9(IDirect3DDevice9* dev, const RECT* src, co
|
||||
DetourGate::Guard guard(g_gate); // keep the shared D3D state alive for this whole detour
|
||||
hook_note_call(g_id_present9);
|
||||
g_presents.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->note_present();
|
||||
}
|
||||
if (!t_in_present)
|
||||
{
|
||||
if (!t_in_present) {
|
||||
t_in_present = true;
|
||||
capture_d3d9(dev);
|
||||
t_in_present = false;
|
||||
@@ -213,19 +194,16 @@ HRESULT STDMETHODCALLTYPE hk_Present9(IDirect3DDevice9* dev, const RECT* src, co
|
||||
void* grab_present9_address()
|
||||
{
|
||||
HMODULE d3d9 = GetModuleHandleW(L"d3d9.dll");
|
||||
if (d3d9 == nullptr)
|
||||
{
|
||||
if (d3d9 == nullptr) {
|
||||
return nullptr; // not a D3D9 game
|
||||
}
|
||||
using PFN_Direct3DCreate9 = IDirect3D9*(WINAPI*)(UINT);
|
||||
auto create = reinterpret_cast<PFN_Direct3DCreate9>(GetProcAddress(d3d9, "Direct3DCreate9"));
|
||||
if (create == nullptr)
|
||||
{
|
||||
if (create == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
IDirect3D9* d3d = create(D3D_SDK_VERSION);
|
||||
if (d3d == nullptr)
|
||||
{
|
||||
if (d3d == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -239,8 +217,7 @@ void* grab_present9_address()
|
||||
wc.hInstance, nullptr);
|
||||
|
||||
void* present = nullptr;
|
||||
if (hwnd != nullptr)
|
||||
{
|
||||
if (hwnd != nullptr) {
|
||||
D3DPRESENT_PARAMETERS pp{};
|
||||
pp.BackBufferWidth = 8;
|
||||
pp.BackBufferHeight = 8;
|
||||
@@ -251,9 +228,8 @@ void* grab_present9_address()
|
||||
pp.Windowed = TRUE;
|
||||
IDirect3DDevice9* dev = nullptr;
|
||||
if (SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
|
||||
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &pp, &dev)) &&
|
||||
dev != nullptr)
|
||||
{
|
||||
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &pp, &dev))
|
||||
&& dev != nullptr) {
|
||||
present = vtable_method(dev, kIdx_IDirect3DDevice9_Present);
|
||||
dev->Release();
|
||||
}
|
||||
@@ -270,16 +246,14 @@ bool install_d3d9_hooks(IpcClient& ipc)
|
||||
{
|
||||
g_ipc = &ipc;
|
||||
g_pid = GetCurrentProcessId();
|
||||
if (g_hk_present9.enabled())
|
||||
{
|
||||
if (g_hk_present9.enabled()) {
|
||||
return true; // already installed (persistent hook; re-install below re-enables it)
|
||||
}
|
||||
g_id_present9 = hook_register("IDirect3DDevice9::Present", HookSubsys_Video);
|
||||
g_unsupported_logged = false;
|
||||
|
||||
void* present = grab_present9_address();
|
||||
if (present == nullptr)
|
||||
{
|
||||
if (present == nullptr) {
|
||||
hook_set_installed(g_id_present9, false); // not a D3D9 game (or no probe device)
|
||||
return false;
|
||||
}
|
||||
@@ -301,13 +275,11 @@ void remove_d3d9_hooks()
|
||||
g_gate.drain();
|
||||
g_shared.release();
|
||||
release_sysmem();
|
||||
if (g_ctx != nullptr)
|
||||
{
|
||||
if (g_ctx != nullptr) {
|
||||
g_ctx->Release();
|
||||
g_ctx = nullptr;
|
||||
}
|
||||
if (g_device != nullptr)
|
||||
{
|
||||
if (g_device != nullptr) {
|
||||
g_device->Release();
|
||||
g_device = nullptr;
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@
|
||||
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Installs the D3D9 Present hook. `ipc` must outlive the hook. Returns true if Present was
|
||||
// hooked (i.e. d3d9.dll is present and a probe device came up). Safe to call repeatedly.
|
||||
|
||||
@@ -10,11 +10,9 @@
|
||||
|
||||
#include "coop/log_ring.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
std::mutex g_log_mutex;
|
||||
FILE* g_log_file = nullptr;
|
||||
@@ -29,14 +27,12 @@ std::atomic<coop::LogRing*> g_log_ring{nullptr};
|
||||
bool logging_enabled()
|
||||
{
|
||||
wchar_t buf[8] = {};
|
||||
if (GetEnvironmentVariableW(L"COOP_HOOK_LOG", buf, 8) > 0)
|
||||
{
|
||||
if (GetEnvironmentVariableW(L"COOP_HOOK_LOG", buf, 8) > 0) {
|
||||
return true;
|
||||
}
|
||||
wchar_t dir[MAX_PATH] = {};
|
||||
const DWORD n = GetTempPathW(MAX_PATH, dir);
|
||||
if (n != 0 && n < MAX_PATH)
|
||||
{
|
||||
if (n != 0 && n < MAX_PATH) {
|
||||
const std::wstring sentinel = std::wstring(dir) + L"coop_hook.log.on";
|
||||
return GetFileAttributesW(sentinel.c_str()) != INVALID_FILE_ATTRIBUTES;
|
||||
}
|
||||
@@ -45,15 +41,12 @@ bool logging_enabled()
|
||||
|
||||
FILE* log_file_locked()
|
||||
{
|
||||
if (!g_log_tried)
|
||||
{
|
||||
if (!g_log_tried) {
|
||||
g_log_tried = true;
|
||||
if (logging_enabled())
|
||||
{
|
||||
if (logging_enabled()) {
|
||||
wchar_t dir[MAX_PATH] = {};
|
||||
const DWORD n = GetTempPathW(MAX_PATH, dir);
|
||||
if (n != 0 && n < MAX_PATH)
|
||||
{
|
||||
if (n != 0 && n < MAX_PATH) {
|
||||
std::wstring path = std::wstring(dir) + L"coop_hook.log";
|
||||
g_log_file = _wfopen(path.c_str(), L"a");
|
||||
}
|
||||
@@ -69,12 +62,10 @@ void set_log_ring(coop::LogRing* ring)
|
||||
g_log_ring.store(ring, std::memory_order_release);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
const char* level_tag(std::uint32_t level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
switch (level) {
|
||||
case coop::LogLevel_Warn:
|
||||
return "WARN ";
|
||||
case coop::LogLevel_Error:
|
||||
@@ -91,20 +82,18 @@ void vlog(std::uint32_t level, const char* fmt, va_list args)
|
||||
std::vsnprintf(line, sizeof(line), fmt, args);
|
||||
|
||||
// Stream to the host's Log window over the shared ring (the primary sink).
|
||||
if (coop::LogRing* ring = g_log_ring.load(std::memory_order_acquire))
|
||||
{
|
||||
if (coop::LogRing* ring = g_log_ring.load(std::memory_order_acquire)) {
|
||||
coop::log_ring_push(*ring, GetCurrentProcessId(), level, GetTickCount64(), line);
|
||||
}
|
||||
|
||||
// Also mirror to the file when the opt-in trace is enabled.
|
||||
std::scoped_lock lock(g_log_mutex);
|
||||
FILE* f = log_file_locked();
|
||||
if (f != nullptr)
|
||||
{
|
||||
if (f != nullptr) {
|
||||
SYSTEMTIME st;
|
||||
GetLocalTime(&st);
|
||||
std::fprintf(f, "[%02u:%02u:%02u.%03u pid=%lu %s] %s\n", st.wHour, st.wMinute, st.wSecond,
|
||||
st.wMilliseconds, GetCurrentProcessId(), level_tag(level), line);
|
||||
std::fprintf(f, "[%02u:%02u:%02u.%03u pid=%lu %s] %s\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds,
|
||||
GetCurrentProcessId(), level_tag(level), line);
|
||||
std::fflush(f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,11 @@
|
||||
// see debug_log.cpp). Thread-safe; cheap enough to leave compiled in.
|
||||
#pragma once
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
struct LogRing;
|
||||
}
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Append a printf-style line to the log ring (if attached) and the file (if on).
|
||||
// logf = info, logw = warning, loge = error; the host colours the Log window by level.
|
||||
|
||||
@@ -27,8 +27,7 @@
|
||||
#include "vk_hook.hpp"
|
||||
#include "xinput_hook.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
coop::hook::IpcClient g_ipc;
|
||||
std::atomic<bool> g_running{true};
|
||||
@@ -40,8 +39,7 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
coop::hook::logf("worker_thread: started");
|
||||
|
||||
// The host creates the mapping around injection time; give it a few seconds.
|
||||
if (!g_ipc.connect(/*attempts=*/200, /*delay_ms=*/25))
|
||||
{
|
||||
if (!g_ipc.connect(/*attempts=*/200, /*delay_ms=*/25)) {
|
||||
coop::hook::logf("worker_thread: IPC connect FAILED (no host mapping); exiting");
|
||||
return 0;
|
||||
}
|
||||
@@ -49,15 +47,11 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
// window. The host creates it at injection time; it's normally already there.
|
||||
{
|
||||
const std::wstring log_name = coop::log_ring_name(GetCurrentProcessId());
|
||||
if (g_log_shm.open(log_name, coop::log_ring_total_size(coop::kLogCapacity)))
|
||||
{
|
||||
if (g_log_shm.open(log_name, coop::log_ring_total_size(coop::kLogCapacity))) {
|
||||
auto* lr = g_log_shm.as<coop::LogRing>();
|
||||
if (coop::log_ring_valid(*lr))
|
||||
{
|
||||
if (coop::log_ring_valid(*lr)) {
|
||||
coop::hook::set_log_ring(lr);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
g_log_shm.reset();
|
||||
}
|
||||
}
|
||||
@@ -81,28 +75,21 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
// what's wanted but missing (modules / the game window may appear lazily) and
|
||||
// remove what's no longer wanted (the host toggled it off). Beat a heartbeat so
|
||||
// the host can see the hook is alive.
|
||||
while (g_running.load(std::memory_order_relaxed))
|
||||
{
|
||||
while (g_running.load(std::memory_order_relaxed)) {
|
||||
// --- Input (XInput) ---
|
||||
const bool want_input = g_ipc.subsystem_install_requested(coop::HookSubsys_Input);
|
||||
if (want_input && !xinput_installed)
|
||||
{
|
||||
if (want_input && !xinput_installed) {
|
||||
xinput_installed = coop::hook::install_xinput_hooks(g_ipc);
|
||||
}
|
||||
else if (!want_input && xinput_installed)
|
||||
{
|
||||
} else if (!want_input && xinput_installed) {
|
||||
coop::hook::remove_xinput_hooks();
|
||||
xinput_installed = false;
|
||||
}
|
||||
|
||||
// --- Focus spoof ---
|
||||
const bool want_focus = g_ipc.subsystem_install_requested(coop::HookSubsys_Focus);
|
||||
if (want_focus && !focus_installed)
|
||||
{
|
||||
if (want_focus && !focus_installed) {
|
||||
focus_installed = coop::hook::install_focus_spoof(g_ipc);
|
||||
}
|
||||
else if (!want_focus && focus_installed)
|
||||
{
|
||||
} else if (!want_focus && focus_installed) {
|
||||
coop::hook::remove_focus_spoof();
|
||||
focus_installed = false;
|
||||
}
|
||||
@@ -111,17 +98,13 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
// Install even before the host's ring exists so render streams are counted
|
||||
// regardless; attach the ring (enabling capture+silence) once it appears.
|
||||
const bool want_audio = com_ok && g_ipc.subsystem_install_requested(coop::HookSubsys_Audio);
|
||||
if (want_audio && !audio_installed)
|
||||
{
|
||||
if (want_audio && !audio_installed) {
|
||||
audio_installed = coop::hook::install_audio_hooks(g_ipc, nullptr);
|
||||
if (audio_installed)
|
||||
{
|
||||
if (audio_installed) {
|
||||
coop::hook::logf("worker_thread: audio hooks installed");
|
||||
audio_ring_open = false; // re-attach the ring below after a reinstall
|
||||
}
|
||||
}
|
||||
else if (!want_audio && audio_installed)
|
||||
{
|
||||
} else if (!want_audio && audio_installed) {
|
||||
coop::hook::remove_audio_hooks();
|
||||
audio_installed = false;
|
||||
audio_ring_open = false;
|
||||
@@ -133,31 +116,25 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
// Install both producers: DXGI games hit the Present hook, OpenGL games hit
|
||||
// the SwapBuffers hook, whichever the game uses fills the shared texture.
|
||||
const bool want_video = g_ipc.subsystem_install_requested(coop::HookSubsys_Video);
|
||||
if (want_video && !video_installed)
|
||||
{
|
||||
if (want_video && !video_installed) {
|
||||
const bool present_ok = coop::hook::install_present_hooks(g_ipc);
|
||||
const bool gl_ok = coop::hook::install_opengl_hooks(g_ipc);
|
||||
const bool d3d9_ok = coop::hook::install_d3d9_hooks(g_ipc);
|
||||
video_installed = present_ok || gl_ok || d3d9_ok;
|
||||
if (video_installed)
|
||||
{
|
||||
if (video_installed) {
|
||||
coop::hook::logf("worker_thread: video hooks installed (present=%d opengl=%d d3d9=%d)",
|
||||
present_ok ? 1 : 0, gl_ok ? 1 : 0, d3d9_ok ? 1 : 0);
|
||||
}
|
||||
}
|
||||
// Vulkan separately: vulkan-1.dll loads lazily (volk dlopens it after start), so the DXGI/
|
||||
// GL/D3D9 hooks above may install before it exists. Keep trying each tick until it appears.
|
||||
if (want_video && !vk_installed)
|
||||
{
|
||||
if (want_video && !vk_installed) {
|
||||
vk_installed = coop::hook::install_vk_hooks(g_ipc);
|
||||
if (vk_installed)
|
||||
{
|
||||
if (vk_installed) {
|
||||
video_installed = true; // a Vulkan-only game otherwise has no video hook installed
|
||||
coop::hook::logf("worker_thread: vulkan video hook installed");
|
||||
}
|
||||
}
|
||||
else if (!want_video && video_installed)
|
||||
{
|
||||
} else if (!want_video && video_installed) {
|
||||
coop::hook::remove_present_hooks();
|
||||
coop::hook::remove_opengl_hooks();
|
||||
coop::hook::remove_d3d9_hooks();
|
||||
@@ -171,16 +148,12 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
// Opt-in. When on, the host streams MKB events into the shared ring; we post
|
||||
// them to the game and synthesize polling state. Drained at high rate below.
|
||||
const bool want_mkb = g_ipc.subsystem_install_requested(coop::HookSubsys_Mkb);
|
||||
if (want_mkb && !mkb_installed)
|
||||
{
|
||||
if (want_mkb && !mkb_installed) {
|
||||
mkb_installed = coop::hook::install_mkb_hooks(g_ipc);
|
||||
if (mkb_installed)
|
||||
{
|
||||
if (mkb_installed) {
|
||||
coop::hook::logf("worker_thread: MKB hooks installed");
|
||||
}
|
||||
}
|
||||
else if (!want_mkb && mkb_installed)
|
||||
{
|
||||
} else if (!want_mkb && mkb_installed) {
|
||||
coop::hook::remove_mkb_hooks();
|
||||
mkb_installed = false;
|
||||
coop::hook::logf("worker_thread: MKB hooks removed (host request)");
|
||||
@@ -189,29 +162,21 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
// Attach a ring per stream. The host creates up to kMaxAudioStreams rings
|
||||
// (coop_audio_<pid>[_<index>]); we open each as it appears and (re)attach it so
|
||||
// every stream is captured + silenced into its own ring for the host to mix.
|
||||
if (audio_installed)
|
||||
{
|
||||
for (unsigned i = 0; i < coop::kMaxAudioStreams; ++i)
|
||||
{
|
||||
if (!g_audio_shm[i].valid())
|
||||
{
|
||||
if (audio_installed) {
|
||||
for (unsigned i = 0; i < coop::kMaxAudioStreams; ++i) {
|
||||
if (!g_audio_shm[i].valid()) {
|
||||
g_audio_shm[i].open(coop::audio_ring_name(GetCurrentProcessId(), i),
|
||||
coop::audio_ring_total_size(coop::kAudioRingCapacity));
|
||||
}
|
||||
if (g_audio_shm[i].valid())
|
||||
{
|
||||
if (g_audio_shm[i].valid()) {
|
||||
auto* ring = g_audio_shm[i].as<coop::AudioRingHeader>();
|
||||
if (coop::audio_ring_valid(*ring))
|
||||
{
|
||||
if (coop::audio_ring_valid(*ring)) {
|
||||
coop::hook::set_audio_ring(i, ring); // idempotent re-attach
|
||||
if (i == 0 && !audio_ring_open)
|
||||
{
|
||||
if (i == 0 && !audio_ring_open) {
|
||||
audio_ring_open = true;
|
||||
coop::hook::logf("worker_thread: audio ring 0 opened");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
g_audio_shm[i].reset(); // present but not our contract; retry
|
||||
}
|
||||
}
|
||||
@@ -220,8 +185,7 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
// A stream is often registered before its ring is attached (or the host re-inits
|
||||
// a ring on a mirror re-toggle, clearing its format); keep formats published so
|
||||
// the host consumes the rings instead of falling back to loopback.
|
||||
if (audio_ring_open)
|
||||
{
|
||||
if (audio_ring_open) {
|
||||
coop::hook::republish_audio_format();
|
||||
}
|
||||
coop::hook::update_input_diagnostics(g_ipc); // refreshes each tick; registrations can change
|
||||
@@ -232,18 +196,15 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
|
||||
// Reconcile ~4x/s (50 slices x 5 ms), but drain MKB events every slice --
|
||||
// input must stay responsive at a far higher rate than the reconcile.
|
||||
for (int slice = 0; slice < 50 && g_running.load(std::memory_order_relaxed); ++slice)
|
||||
{
|
||||
if (mkb_installed)
|
||||
{
|
||||
for (int slice = 0; slice < 50 && g_running.load(std::memory_order_relaxed); ++slice) {
|
||||
if (mkb_installed) {
|
||||
coop::hook::mkb_pump(g_ipc);
|
||||
}
|
||||
Sleep(5);
|
||||
}
|
||||
}
|
||||
|
||||
if (com_ok)
|
||||
{
|
||||
if (com_ok) {
|
||||
CoUninitialize();
|
||||
}
|
||||
return 0;
|
||||
@@ -253,20 +214,17 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
switch (reason) {
|
||||
case DLL_PROCESS_ATTACH:
|
||||
DisableThreadLibraryCalls(module);
|
||||
if (HANDLE thread = CreateThread(nullptr, 0, &worker_thread, nullptr, 0, nullptr))
|
||||
{
|
||||
if (HANDLE thread = CreateThread(nullptr, 0, &worker_thread, nullptr, 0, nullptr)) {
|
||||
CloseHandle(thread);
|
||||
}
|
||||
break;
|
||||
case DLL_PROCESS_DETACH:
|
||||
// Skip cleanup when the process is tearing down (reserved != null): the
|
||||
// loader is already unwinding and touching other modules is unsafe.
|
||||
if (reserved == nullptr)
|
||||
{
|
||||
if (reserved == nullptr) {
|
||||
g_running.store(false, std::memory_order_relaxed);
|
||||
coop::hook::set_log_ring(nullptr);
|
||||
coop::hook::remove_focus_spoof();
|
||||
|
||||
@@ -5,13 +5,11 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
inline HWND find_main_window(DWORD pid)
|
||||
{
|
||||
struct Ctx
|
||||
{
|
||||
struct Ctx {
|
||||
DWORD pid;
|
||||
HWND best;
|
||||
long best_area;
|
||||
@@ -22,18 +20,15 @@ inline HWND find_main_window(DWORD pid)
|
||||
auto* c = reinterpret_cast<Ctx*>(lparam);
|
||||
DWORD pid = 0;
|
||||
GetWindowThreadProcessId(hwnd, &pid);
|
||||
if (pid != c->pid || !IsWindowVisible(hwnd) || GetWindow(hwnd, GW_OWNER) != nullptr)
|
||||
{
|
||||
if (pid != c->pid || !IsWindowVisible(hwnd) || GetWindow(hwnd, GW_OWNER) != nullptr) {
|
||||
return TRUE; // not ours, hidden, or an owned dialog -- keep looking
|
||||
}
|
||||
RECT rect = {};
|
||||
if (!GetWindowRect(hwnd, &rect))
|
||||
{
|
||||
if (!GetWindowRect(hwnd, &rect)) {
|
||||
return TRUE;
|
||||
}
|
||||
const long area = (rect.right - rect.left) * (rect.bottom - rect.top);
|
||||
if (area > c->best_area)
|
||||
{
|
||||
if (area > c->best_area) {
|
||||
c->best_area = area;
|
||||
c->best = hwnd;
|
||||
}
|
||||
|
||||
@@ -11,11 +11,9 @@
|
||||
#include "hook_install.hpp"
|
||||
#include "hook_registry.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight focus / WNDPROC detours before remove nulls their state
|
||||
|
||||
@@ -40,11 +38,9 @@ safetyhook::InlineHook g_hk_setcursorpos;
|
||||
LRESULT CALLBACK subclass_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
DetourGate::Guard guard(g_gate); // keep g_orig_proc / g_unicode valid for this whole dispatch
|
||||
switch (msg)
|
||||
{
|
||||
switch (msg) {
|
||||
case WM_ACTIVATE:
|
||||
if (LOWORD(wparam) == WA_INACTIVE)
|
||||
{
|
||||
if (LOWORD(wparam) == WA_INACTIVE) {
|
||||
wparam = MAKEWPARAM(WA_ACTIVE, HIWORD(wparam));
|
||||
hook_note_call(g_id_wndproc);
|
||||
}
|
||||
@@ -66,8 +62,7 @@ LRESULT CALLBACK subclass_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam
|
||||
// Read g_orig_proc once; if the subclass is live but the original isn't published yet (the tiny
|
||||
// install/remove window), fall back to DefWindowProc rather than call through a null pointer.
|
||||
const WNDPROC orig = g_orig_proc;
|
||||
if (orig == nullptr)
|
||||
{
|
||||
if (orig == nullptr) {
|
||||
return g_unicode ? DefWindowProcW(hwnd, msg, wparam, lparam) : DefWindowProcA(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
return g_unicode ? CallWindowProcW(orig, hwnd, msg, wparam, lparam)
|
||||
@@ -78,8 +73,7 @@ HWND WINAPI hk_GetForegroundWindow()
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_foreground);
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->note_focus_query(FocusApi_Foreground);
|
||||
}
|
||||
return g_game_hwnd;
|
||||
@@ -89,8 +83,7 @@ HWND WINAPI hk_GetActiveWindow()
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_active);
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->note_focus_query(FocusApi_Active);
|
||||
}
|
||||
return g_game_hwnd;
|
||||
@@ -100,8 +93,7 @@ HWND WINAPI hk_GetFocus()
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_focus);
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->note_focus_query(FocusApi_Focus);
|
||||
}
|
||||
return g_game_hwnd;
|
||||
@@ -124,8 +116,7 @@ BOOL WINAPI hk_SetCursorPos(int x, int y)
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_setcursorpos);
|
||||
const bool allow = g_focus_ipc != nullptr && g_focus_ipc->cursor_clip_allowed();
|
||||
if (!allow)
|
||||
{
|
||||
if (!allow) {
|
||||
return TRUE;
|
||||
}
|
||||
return g_hk_setcursorpos.stdcall<BOOL>(x, y);
|
||||
@@ -133,8 +124,7 @@ BOOL WINAPI hk_SetCursorPos(int x, int y)
|
||||
|
||||
void hook_export(HMODULE module, const char* name, void* detour, int registry_id)
|
||||
{
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name)))
|
||||
{
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name))) {
|
||||
g_focus_hooks.emplace_back();
|
||||
install_inline(g_focus_hooks.back(), target, detour); // assign-then-enable (no install race)
|
||||
hook_set_installed(registry_id, true);
|
||||
@@ -146,8 +136,7 @@ void hook_export(HMODULE module, const char* name, void* detour, int registry_id
|
||||
bool install_focus_spoof(IpcClient& ipc)
|
||||
{
|
||||
g_focus_ipc = &ipc;
|
||||
if (g_game_hwnd != nullptr)
|
||||
{
|
||||
if (g_game_hwnd != nullptr) {
|
||||
return true; // already active
|
||||
}
|
||||
|
||||
@@ -159,8 +148,7 @@ bool install_focus_spoof(IpcClient& ipc)
|
||||
g_id_setcursorpos = hook_register("SetCursorPos (cursor release)", HookSubsys_Focus);
|
||||
|
||||
HWND hwnd = find_main_window(GetCurrentProcessId());
|
||||
if (hwnd == nullptr)
|
||||
{
|
||||
if (hwnd == nullptr) {
|
||||
return false; // window not created yet; caller retries
|
||||
}
|
||||
|
||||
@@ -173,38 +161,30 @@ bool install_focus_spoof(IpcClient& ipc)
|
||||
// thread is safe (the new proc runs on the window's own thread); match A/W for CallWindowProc.
|
||||
g_orig_proc = g_unicode ? reinterpret_cast<WNDPROC>(GetWindowLongPtrW(hwnd, GWLP_WNDPROC))
|
||||
: reinterpret_cast<WNDPROC>(GetWindowLongPtrA(hwnd, GWLP_WNDPROC));
|
||||
if (g_unicode)
|
||||
{
|
||||
if (g_unicode) {
|
||||
SetWindowLongPtrW(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&subclass_proc));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SetWindowLongPtrA(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&subclass_proc));
|
||||
}
|
||||
hook_set_installed(g_id_wndproc, true);
|
||||
|
||||
if (HMODULE user32 = GetModuleHandleW(L"user32.dll"))
|
||||
{
|
||||
hook_export(user32, "GetForegroundWindow", reinterpret_cast<void*>(&hk_GetForegroundWindow),
|
||||
g_id_foreground);
|
||||
if (HMODULE user32 = GetModuleHandleW(L"user32.dll")) {
|
||||
hook_export(user32, "GetForegroundWindow", reinterpret_cast<void*>(&hk_GetForegroundWindow), g_id_foreground);
|
||||
hook_export(user32, "GetActiveWindow", reinterpret_cast<void*>(&hk_GetActiveWindow), g_id_active);
|
||||
hook_export(user32, "GetFocus", reinterpret_cast<void*>(&hk_GetFocus), g_id_focus);
|
||||
|
||||
if (void* clip = reinterpret_cast<void*>(GetProcAddress(user32, "ClipCursor")))
|
||||
{
|
||||
if (void* clip = reinterpret_cast<void*>(GetProcAddress(user32, "ClipCursor"))) {
|
||||
install_inline(g_hk_clipcursor, clip, &hk_ClipCursor);
|
||||
hook_set_installed(g_id_clipcursor, static_cast<bool>(g_hk_clipcursor));
|
||||
}
|
||||
if (void* setpos = reinterpret_cast<void*>(GetProcAddress(user32, "SetCursorPos")))
|
||||
{
|
||||
if (void* setpos = reinterpret_cast<void*>(GetProcAddress(user32, "SetCursorPos"))) {
|
||||
install_inline(g_hk_setcursorpos, setpos, &hk_SetCursorPos);
|
||||
hook_set_installed(g_id_setcursorpos, static_cast<bool>(g_hk_setcursorpos));
|
||||
}
|
||||
}
|
||||
|
||||
// Free any clip the game already set, so release takes effect immediately.
|
||||
if (!ipc.cursor_clip_allowed())
|
||||
{
|
||||
if (!ipc.cursor_clip_allowed()) {
|
||||
ClipCursor(nullptr);
|
||||
}
|
||||
|
||||
@@ -221,20 +201,16 @@ void update_input_diagnostics(IpcClient& ipc)
|
||||
bool raw_gamepad_sink = false;
|
||||
|
||||
UINT count = 0;
|
||||
if (GetRegisteredRawInputDevices(nullptr, &count, sizeof(RAWINPUTDEVICE)) == 0 && count > 0)
|
||||
{
|
||||
if (GetRegisteredRawInputDevices(nullptr, &count, sizeof(RAWINPUTDEVICE)) == 0 && count > 0) {
|
||||
std::vector<RAWINPUTDEVICE> devices(count);
|
||||
const UINT got = GetRegisteredRawInputDevices(devices.data(), &count, sizeof(RAWINPUTDEVICE));
|
||||
if (got != static_cast<UINT>(-1))
|
||||
{
|
||||
if (got != static_cast<UINT>(-1)) {
|
||||
raw_registered = got > 0;
|
||||
for (UINT i = 0; i < got; ++i)
|
||||
{
|
||||
for (UINT i = 0; i < got; ++i) {
|
||||
// Generic Desktop (0x01) joystick (0x04) / gamepad (0x05).
|
||||
const bool is_pad =
|
||||
devices[i].usUsagePage == 0x01 && (devices[i].usUsage == 0x04 || devices[i].usUsage == 0x05);
|
||||
if (is_pad)
|
||||
{
|
||||
if (is_pad) {
|
||||
raw_gamepad = true;
|
||||
raw_gamepad_sink = (devices[i].dwFlags & RIDEV_INPUTSINK) != 0;
|
||||
}
|
||||
@@ -248,22 +224,17 @@ void update_input_diagnostics(IpcClient& ipc)
|
||||
|
||||
void release_cursor_tick()
|
||||
{
|
||||
if (g_focus_ipc != nullptr && g_game_hwnd != nullptr && !g_focus_ipc->cursor_clip_allowed())
|
||||
{
|
||||
if (g_focus_ipc != nullptr && g_game_hwnd != nullptr && !g_focus_ipc->cursor_clip_allowed()) {
|
||||
ClipCursor(nullptr); // routes through hk_ClipCursor -> frees the cursor
|
||||
}
|
||||
}
|
||||
|
||||
void remove_focus_spoof()
|
||||
{
|
||||
if (g_game_hwnd != nullptr && g_orig_proc != nullptr)
|
||||
{
|
||||
if (g_unicode)
|
||||
{
|
||||
if (g_game_hwnd != nullptr && g_orig_proc != nullptr) {
|
||||
if (g_unicode) {
|
||||
SetWindowLongPtrW(g_game_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_orig_proc));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SetWindowLongPtrA(g_game_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_orig_proc));
|
||||
}
|
||||
}
|
||||
@@ -275,8 +246,7 @@ void remove_focus_spoof()
|
||||
// patched bytes. The reverse of the enable order (GFW first) keeps the invariant "GetActiveWindow
|
||||
// hooked => GetForegroundWindow hooked" across the whole install/remove cycle, so a call never
|
||||
// lands in a half-patched shared region.
|
||||
for (auto it = g_focus_hooks.rbegin(); it != g_focus_hooks.rend(); ++it)
|
||||
{
|
||||
for (auto it = g_focus_hooks.rbegin(); it != g_focus_hooks.rend(); ++it) {
|
||||
disable_for_removal(*it);
|
||||
}
|
||||
disable_for_removal(g_hk_clipcursor);
|
||||
@@ -298,8 +268,7 @@ void remove_focus_spoof()
|
||||
// DO call the trampoline, so keep them ALIVE (disabled) -- persistent, re-enabled on re-install
|
||||
// (see hook_install.hpp) -- so a stale detour never hits a freed trampoline.
|
||||
g_focus_hooks.clear();
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->mark_focus_spoof(false, 0);
|
||||
}
|
||||
g_game_hwnd = nullptr;
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Finds the game's main window, subclasses it to suppress deactivation messages,
|
||||
// and hooks the focus-query APIs to always report the game as active. Returns
|
||||
|
||||
@@ -26,24 +26,15 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
class DetourGate
|
||||
{
|
||||
class DetourGate {
|
||||
public:
|
||||
// RAII: marks a detour body as in-flight for as long as it's on the stack.
|
||||
class Guard
|
||||
{
|
||||
class Guard {
|
||||
public:
|
||||
explicit Guard(DetourGate& gate) : m_gate(gate)
|
||||
{
|
||||
m_gate.m_active.fetch_add(1, std::memory_order_acq_rel);
|
||||
}
|
||||
~Guard()
|
||||
{
|
||||
m_gate.m_active.fetch_sub(1, std::memory_order_acq_rel);
|
||||
}
|
||||
explicit Guard(DetourGate& gate) : m_gate(gate) { m_gate.m_active.fetch_add(1, std::memory_order_acq_rel); }
|
||||
~Guard() { m_gate.m_active.fetch_sub(1, std::memory_order_acq_rel); }
|
||||
Guard(const Guard&) = delete;
|
||||
Guard& operator=(const Guard&) = delete;
|
||||
|
||||
@@ -64,20 +55,15 @@ public:
|
||||
// reliably, so checking before the first sleep is not safe.
|
||||
void drain()
|
||||
{
|
||||
for (int spins = 0; spins < 400; ++spins)
|
||||
{
|
||||
for (int spins = 0; spins < 400; ++spins) {
|
||||
Sleep(1);
|
||||
if (m_active.load(std::memory_order_acquire) == 0)
|
||||
{
|
||||
if (m_active.load(std::memory_order_acquire) == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int active() const
|
||||
{
|
||||
return m_active.load(std::memory_order_acquire);
|
||||
}
|
||||
int active() const { return m_active.load(std::memory_order_acquire); }
|
||||
|
||||
private:
|
||||
std::atomic<int> m_active{0};
|
||||
@@ -92,8 +78,7 @@ private:
|
||||
template <class InlineHook>
|
||||
void disable_for_removal(InlineHook& hook)
|
||||
{
|
||||
if (!hook.disable())
|
||||
{
|
||||
if (!hook.disable()) {
|
||||
OutputDebugStringA("coop: SafetyHook InlineHook::disable() failed during removal -- unhook may be unsafe\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,7 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Arm `detour` over `target` in `dst`: create it once (StartDisabled) if empty, then enable. Calling
|
||||
// this again after a remove just re-enables the SAME hook (no recreate -> the trampoline is never
|
||||
@@ -36,8 +35,7 @@ inline void install_inline(safetyhook::InlineHook& dst, void* target, void* deto
|
||||
{
|
||||
dst = safetyhook::create_inline(target, detour, safetyhook::InlineHook::StartDisabled);
|
||||
}
|
||||
if (dst && !dst.enable())
|
||||
{
|
||||
if (dst && !dst.enable()) {
|
||||
OutputDebugStringA("coop: SafetyHook InlineHook::enable() failed during install\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,11 @@
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
struct Slot
|
||||
{
|
||||
struct Slot {
|
||||
char name[40] = {};
|
||||
std::atomic<std::uint32_t> subsystem{0};
|
||||
std::atomic<std::uint32_t> installed{0};
|
||||
@@ -29,15 +26,12 @@ int hook_register(const char* name, std::uint32_t subsystem)
|
||||
{
|
||||
std::scoped_lock lock(g_register_mutex);
|
||||
const std::uint32_t count = g_count.load(std::memory_order_relaxed);
|
||||
for (std::uint32_t i = 0; i < count; ++i)
|
||||
{
|
||||
if (g_slots[i].used.load(std::memory_order_relaxed) && std::strcmp(g_slots[i].name, name) == 0)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < count; ++i) {
|
||||
if (g_slots[i].used.load(std::memory_order_relaxed) && std::strcmp(g_slots[i].name, name) == 0) {
|
||||
return static_cast<int>(i); // already registered
|
||||
}
|
||||
}
|
||||
if (count >= kMaxHookEntries)
|
||||
{
|
||||
if (count >= kMaxHookEntries) {
|
||||
return -1; // table full
|
||||
}
|
||||
Slot& s = g_slots[count];
|
||||
@@ -53,16 +47,14 @@ int hook_register(const char* name, std::uint32_t subsystem)
|
||||
|
||||
void hook_set_installed(int id, bool installed)
|
||||
{
|
||||
if (id >= 0 && id < static_cast<int>(kMaxHookEntries))
|
||||
{
|
||||
if (id >= 0 && id < static_cast<int>(kMaxHookEntries)) {
|
||||
g_slots[id].installed.store(installed ? 1u : 0u, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
void hook_note_call(int id)
|
||||
{
|
||||
if (id >= 0 && id < static_cast<int>(kMaxHookEntries))
|
||||
{
|
||||
if (id >= 0 && id < static_cast<int>(kMaxHookEntries)) {
|
||||
g_slots[id].calls.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
@@ -72,10 +64,8 @@ void hook_publish(IpcClient& ipc)
|
||||
const std::uint32_t count = g_count.load(std::memory_order_acquire);
|
||||
HookEntry entries[kMaxHookEntries];
|
||||
std::uint32_t n = 0;
|
||||
for (std::uint32_t i = 0; i < count && i < kMaxHookEntries; ++i)
|
||||
{
|
||||
if (!g_slots[i].used.load(std::memory_order_acquire))
|
||||
{
|
||||
for (std::uint32_t i = 0; i < count && i < kMaxHookEntries; ++i) {
|
||||
if (!g_slots[i].used.load(std::memory_order_acquire)) {
|
||||
continue;
|
||||
}
|
||||
HookEntry& e = entries[n];
|
||||
@@ -91,8 +81,7 @@ void hook_publish(IpcClient& ipc)
|
||||
void hook_registry_reset()
|
||||
{
|
||||
std::scoped_lock lock(g_register_mutex);
|
||||
for (auto& s : g_slots)
|
||||
{
|
||||
for (auto& s : g_slots) {
|
||||
s.used.store(0, std::memory_order_relaxed);
|
||||
s.installed.store(0, std::memory_order_relaxed);
|
||||
s.calls.store(0, std::memory_order_relaxed);
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
#include "coop/protocol.hpp"
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Find-or-create a registry slot for `name` in `subsystem`; returns a stable id
|
||||
// (>= 0) used with the calls below, or -1 if the table is full. Idempotent: the
|
||||
|
||||
@@ -11,24 +11,19 @@
|
||||
#include "coop/protocol.hpp"
|
||||
#include "coop/shared_memory.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
class IpcClient
|
||||
{
|
||||
class IpcClient {
|
||||
public:
|
||||
// Tries to open the section a few times: the host may inject us slightly
|
||||
// before (or after) it creates the mapping. Returns true once connected.
|
||||
bool connect(int attempts, int delay_ms)
|
||||
{
|
||||
const std::wstring name = shared_memory_name(GetCurrentProcessId());
|
||||
for (int i = 0; i < attempts; ++i)
|
||||
{
|
||||
if (shm_.open(name, sizeof(SharedBlock)))
|
||||
{
|
||||
for (int i = 0; i < attempts; ++i) {
|
||||
if (shm_.open(name, sizeof(SharedBlock))) {
|
||||
auto* block = shm_.as<SharedBlock>();
|
||||
if (block->magic == kProtocolMagic && block->version == kProtocolVersion)
|
||||
{
|
||||
if (block->magic == kProtocolMagic && block->version == kProtocolVersion) {
|
||||
block_ = block;
|
||||
return true;
|
||||
}
|
||||
@@ -39,17 +34,13 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool connected() const
|
||||
{
|
||||
return block_ != nullptr;
|
||||
}
|
||||
[[nodiscard]] bool connected() const { return block_ != nullptr; }
|
||||
|
||||
// Host-requested install state for a subsystem (default = install, since the
|
||||
// mapping is zero-filled and 0 means "disabled flag clear" = install).
|
||||
[[nodiscard]] bool subsystem_install_requested(std::uint32_t subsystem) const
|
||||
{
|
||||
if (block_ == nullptr || subsystem >= HookSubsys_Count)
|
||||
{
|
||||
if (block_ == nullptr || subsystem >= HookSubsys_Count) {
|
||||
return true;
|
||||
}
|
||||
return block_->control.subsystem_disabled[subsystem].load(std::memory_order_acquire) == 0;
|
||||
@@ -66,8 +57,7 @@ public:
|
||||
// was mid-write for the whole spin window (caller should reuse its cache).
|
||||
bool snapshot(CoopPadState (&out)[kMaxPads], std::uint32_t& count) const
|
||||
{
|
||||
if (block_ == nullptr)
|
||||
{
|
||||
if (block_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return read_pads(*block_, out, count);
|
||||
@@ -78,32 +68,28 @@ public:
|
||||
// Record that the game queried a controller slot via XInputGetState/Ex.
|
||||
void note_state_query(std::uint32_t user_index)
|
||||
{
|
||||
if (block_ != nullptr && user_index < kMaxPads)
|
||||
{
|
||||
if (block_ != nullptr && user_index < kMaxPads) {
|
||||
block_->status.get_state_calls[user_index].fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
void note_caps_query(std::uint32_t user_index)
|
||||
{
|
||||
if (block_ != nullptr && user_index < kMaxPads)
|
||||
{
|
||||
if (block_ != nullptr && user_index < kMaxPads) {
|
||||
block_->status.get_caps_calls[user_index].fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
void note_focus_query(FocusApi which)
|
||||
{
|
||||
if (block_ != nullptr && which < FocusApi_Count)
|
||||
{
|
||||
if (block_ != nullptr && which < FocusApi_Count) {
|
||||
block_->status.focus_query_calls[which].fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
void mark_attached()
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->status.game_pid = GetCurrentProcessId();
|
||||
block_->status.attached = 1;
|
||||
}
|
||||
@@ -113,16 +99,14 @@ public:
|
||||
// the Controllers panel stops showing stale poll rates.
|
||||
void mark_detached()
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->status.attached = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void mark_focus_spoof(bool active, std::uint64_t game_hwnd)
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->status.focus_spoof = active ? 1u : 0u;
|
||||
block_->status.game_hwnd = game_hwnd;
|
||||
}
|
||||
@@ -130,8 +114,7 @@ public:
|
||||
|
||||
void set_input_diagnostics(bool raw_registered, bool raw_gamepad, bool raw_gamepad_sink, bool dinput)
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->status.raw_input_registered = raw_registered ? 1u : 0u;
|
||||
block_->status.raw_input_gamepad = raw_gamepad ? 1u : 0u;
|
||||
block_->status.raw_input_gamepad_sink = raw_gamepad_sink ? 1u : 0u;
|
||||
@@ -141,16 +124,14 @@ public:
|
||||
|
||||
void heartbeat()
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->status.heartbeat.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
void set_vk_too_late(bool too_late)
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->status.vk_too_late = too_late ? 1u : 0u;
|
||||
}
|
||||
}
|
||||
@@ -159,8 +140,7 @@ public:
|
||||
// the guest's controller). Plain stores; the hook is the sole writer.
|
||||
void note_rumble(std::uint32_t slot, std::uint16_t left, std::uint16_t right)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxPads)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxPads) {
|
||||
block_->status.rumble_left[slot] = left;
|
||||
block_->status.rumble_right[slot] = right;
|
||||
}
|
||||
@@ -169,8 +149,7 @@ public:
|
||||
// Record the state the hook just returned to the game for a slot (round-trip view).
|
||||
void note_read_state(std::uint32_t slot, const CoopPadState& state)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxPads)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxPads) {
|
||||
block_->status.read_state[slot] = state;
|
||||
}
|
||||
}
|
||||
@@ -180,8 +159,7 @@ public:
|
||||
// Total distinct render streams the audio hook has observed.
|
||||
void set_audio_streams_seen(std::uint32_t count)
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->status.audio_streams_seen = count;
|
||||
}
|
||||
}
|
||||
@@ -189,8 +167,7 @@ public:
|
||||
// Publish a tracked stream's format/role into its debug slot.
|
||||
void publish_audio_stream(std::uint32_t slot, const AudioStreamInfo& info)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxAudioStreams)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxAudioStreams) {
|
||||
block_->status.audio_streams[slot] = info;
|
||||
}
|
||||
}
|
||||
@@ -198,12 +175,12 @@ public:
|
||||
// Update a tracked stream's cumulative frame count (host derives live/idle).
|
||||
void note_audio_frames(std::uint32_t slot, std::uint64_t frames)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxAudioStreams)
|
||||
{
|
||||
if (block_ != nullptr && slot < kMaxAudioStreams) {
|
||||
// atomic_ref so the host's cross-process read isn't torn (notably an x86 DLL -> x64 host,
|
||||
// where a plain 64-bit store is two halves). The field stays plain POD so AudioStreamInfo
|
||||
// remains trivially copyable for the wholesale publishes elsewhere.
|
||||
std::atomic_ref(block_->status.audio_streams[slot].frames_rendered).store(frames, std::memory_order_relaxed);
|
||||
std::atomic_ref(block_->status.audio_streams[slot].frames_rendered)
|
||||
.store(frames, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,8 +189,7 @@ public:
|
||||
// Record that the game's Present() ran (diagnostic counter, hook is sole writer).
|
||||
void note_present()
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
std::atomic_ref(block_->video.present_calls).fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
@@ -222,8 +198,7 @@ public:
|
||||
// keyed mutex was held by the host (we skip rather than block the game's render thread).
|
||||
void note_video_dropped()
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
std::atomic_ref(block_->video.frames_dropped).fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
@@ -233,8 +208,7 @@ public:
|
||||
// polls. The texture itself is shared out-of-band by name, not through here.
|
||||
void publish_video_frame(std::uint32_t width, std::uint32_t height, std::uint32_t format)
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->video.width = width;
|
||||
block_->video.height = height;
|
||||
block_->video.format = format;
|
||||
@@ -249,26 +223,20 @@ public:
|
||||
|
||||
// The host's MKB event queue (nullptr if not connected). The MKB subsystem
|
||||
// drains it; the host is the sole producer.
|
||||
[[nodiscard]] MkbRing* mkb_ring()
|
||||
{
|
||||
return block_ != nullptr ? &block_->mkb : nullptr;
|
||||
}
|
||||
[[nodiscard]] MkbRing* mkb_ring() { return block_ != nullptr ? &block_->mkb : nullptr; }
|
||||
|
||||
// --- Hook registry -----------------------------------------------------
|
||||
|
||||
// Publish the installed-hooks table (name / subsystem / installed / calls).
|
||||
void publish_hook_entries(const HookEntry* entries, std::uint32_t count)
|
||||
{
|
||||
if (block_ == nullptr)
|
||||
{
|
||||
if (block_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (count > kMaxHookEntries)
|
||||
{
|
||||
if (count > kMaxHookEntries) {
|
||||
count = kMaxHookEntries;
|
||||
}
|
||||
for (std::uint32_t i = 0; i < count; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < count; ++i) {
|
||||
block_->status.hook_entries[i] = entries[i];
|
||||
}
|
||||
block_->status.hook_entry_count = count;
|
||||
|
||||
@@ -15,11 +15,9 @@
|
||||
#include "hook_registry.hpp"
|
||||
#include "vtable_hook.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight polling detours before remove tears the hooks down
|
||||
|
||||
@@ -82,9 +80,8 @@ SHORT WINAPI hk_GetAsyncKeyState(int vkey)
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
const SHORT orig = g_hk_async.stdcall<SHORT>(vkey);
|
||||
if (g_active.load(std::memory_order_relaxed) && vkey >= 0 && vkey < 256 &&
|
||||
g_key_down[vkey].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_active.load(std::memory_order_relaxed) && vkey >= 0 && vkey < 256
|
||||
&& g_key_down[vkey].load(std::memory_order_relaxed)) {
|
||||
return static_cast<SHORT>(0x8000) | (orig & 0x1);
|
||||
}
|
||||
return orig;
|
||||
@@ -94,12 +91,9 @@ BOOL WINAPI hk_GetKeyboardState(PBYTE state)
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
const BOOL r = g_hk_kbstate.stdcall<BOOL>(state);
|
||||
if (r && state != nullptr && g_active.load(std::memory_order_relaxed))
|
||||
{
|
||||
for (int vk = 0; vk < 256; ++vk)
|
||||
{
|
||||
if (g_key_down[vk].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (r && state != nullptr && g_active.load(std::memory_order_relaxed)) {
|
||||
for (int vk = 0; vk < 256; ++vk) {
|
||||
if (g_key_down[vk].load(std::memory_order_relaxed)) {
|
||||
state[vk] |= 0x80;
|
||||
}
|
||||
}
|
||||
@@ -111,11 +105,9 @@ BOOL WINAPI hk_GetCursorPos(LPPOINT pt)
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
const BOOL r = g_hk_cursor.stdcall<BOOL>(pt);
|
||||
if (g_active.load(std::memory_order_relaxed) && g_have_cursor.load(std::memory_order_relaxed) && pt != nullptr)
|
||||
{
|
||||
if (g_active.load(std::memory_order_relaxed) && g_have_cursor.load(std::memory_order_relaxed) && pt != nullptr) {
|
||||
auto* hwnd = static_cast<HWND>(g_target.load(std::memory_order_relaxed));
|
||||
if (hwnd != nullptr)
|
||||
{
|
||||
if (hwnd != nullptr) {
|
||||
POINT c{g_cursor_x.load(std::memory_order_relaxed), g_cursor_y.load(std::memory_order_relaxed)};
|
||||
ClientToScreen(hwnd, &c); // synth state is game-client; GetCursorPos is screen-space
|
||||
*pt = c;
|
||||
@@ -140,31 +132,25 @@ HRESULT STDMETHODCALLTYPE hk_DI_GetDeviceState(IDirectInputDevice8W* self, DWORD
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
const HRESULT hr = g_vh_di_getstate.original<DI_GetDeviceStateFn>()(self, cb, data);
|
||||
if (FAILED(hr) || data == nullptr || !g_active.load(std::memory_order_relaxed))
|
||||
{
|
||||
if (FAILED(hr) || data == nullptr || !g_active.load(std::memory_order_relaxed)) {
|
||||
return hr;
|
||||
}
|
||||
hook_note_call(g_id_di_getstate);
|
||||
if (cb == 256) // keyboard: BYTE[256] indexed by DIK (scan code); high bit = pressed
|
||||
{
|
||||
BYTE* keys = static_cast<BYTE*>(data);
|
||||
for (int vk = 0; vk < 256; ++vk)
|
||||
{
|
||||
if (g_key_down[vk].load(std::memory_order_relaxed))
|
||||
{
|
||||
for (int vk = 0; vk < 256; ++vk) {
|
||||
if (g_key_down[vk].load(std::memory_order_relaxed)) {
|
||||
const BYTE dik = vk_to_dik(vk);
|
||||
if (dik != 0)
|
||||
{
|
||||
if (dik != 0) {
|
||||
keys[dik] |= 0x80;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (cb == sizeof(DIMOUSESTATE) || cb == sizeof(DIMOUSESTATE2)) // mouse (DIMOUSESTATE2 is a superset)
|
||||
} else if (cb == sizeof(DIMOUSESTATE) || cb == sizeof(DIMOUSESTATE2)) // mouse (DIMOUSESTATE2 is a superset)
|
||||
{
|
||||
auto* m = static_cast<DIMOUSESTATE*>(data); // the shared lead fields (lX/lY/lZ/rgbButtons)
|
||||
if (g_have_cursor.load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_have_cursor.load(std::memory_order_relaxed)) {
|
||||
const long x = g_cursor_x.load(std::memory_order_relaxed);
|
||||
const long y = g_cursor_y.load(std::memory_order_relaxed);
|
||||
if (g_di_mouse_primed.load(std::memory_order_relaxed)) // relative delta from our cursor
|
||||
@@ -176,16 +162,13 @@ HRESULT STDMETHODCALLTYPE hk_DI_GetDeviceState(IDirectInputDevice8W* self, DWORD
|
||||
g_di_mouse_last_y.store(y, std::memory_order_relaxed);
|
||||
g_di_mouse_primed.store(true, std::memory_order_relaxed);
|
||||
}
|
||||
if (g_key_down[VK_LBUTTON].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_key_down[VK_LBUTTON].load(std::memory_order_relaxed)) {
|
||||
m->rgbButtons[0] |= 0x80;
|
||||
}
|
||||
if (g_key_down[VK_RBUTTON].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_key_down[VK_RBUTTON].load(std::memory_order_relaxed)) {
|
||||
m->rgbButtons[1] |= 0x80;
|
||||
}
|
||||
if (g_key_down[VK_MBUTTON].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_key_down[VK_MBUTTON].load(std::memory_order_relaxed)) {
|
||||
m->rgbButtons[2] |= 0x80;
|
||||
}
|
||||
}
|
||||
@@ -204,38 +187,31 @@ bool is_our_raw(HRAWINPUT h)
|
||||
UINT WINAPI hk_GetRawInputData(HRAWINPUT hri, UINT cmd, LPVOID pData, PUINT pcbSize, UINT cbHeader)
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
if (g_active.load(std::memory_order_relaxed) && is_our_raw(hri))
|
||||
{
|
||||
if (g_active.load(std::memory_order_relaxed) && is_our_raw(hri)) {
|
||||
hook_note_call(g_id_rawinput);
|
||||
const RAWINPUT* ri = reinterpret_cast<const RAWINPUT*>(hri);
|
||||
const UINT body = ri->header.dwType == RIM_TYPEMOUSE ? sizeof(RAWMOUSE) : sizeof(RAWKEYBOARD);
|
||||
const UINT full = sizeof(RAWINPUTHEADER) + body;
|
||||
if (pcbSize == nullptr)
|
||||
{
|
||||
if (pcbSize == nullptr) {
|
||||
return static_cast<UINT>(-1);
|
||||
}
|
||||
if (cmd == RID_HEADER)
|
||||
{
|
||||
if (pData == nullptr)
|
||||
{
|
||||
if (cmd == RID_HEADER) {
|
||||
if (pData == nullptr) {
|
||||
*pcbSize = sizeof(RAWINPUTHEADER);
|
||||
return 0;
|
||||
}
|
||||
if (*pcbSize < sizeof(RAWINPUTHEADER))
|
||||
{
|
||||
if (*pcbSize < sizeof(RAWINPUTHEADER)) {
|
||||
return static_cast<UINT>(-1);
|
||||
}
|
||||
memcpy(pData, &ri->header, sizeof(RAWINPUTHEADER));
|
||||
return sizeof(RAWINPUTHEADER);
|
||||
}
|
||||
// RID_INPUT: the full header + body.
|
||||
if (pData == nullptr)
|
||||
{
|
||||
if (pData == nullptr) {
|
||||
*pcbSize = full;
|
||||
return 0;
|
||||
}
|
||||
if (*pcbSize < full)
|
||||
{
|
||||
if (*pcbSize < full) {
|
||||
return static_cast<UINT>(-1);
|
||||
}
|
||||
memcpy(pData, ri, full);
|
||||
@@ -247,8 +223,7 @@ UINT WINAPI hk_GetRawInputData(HRAWINPUT hri, UINT cmd, LPVOID pData, PUINT pcbS
|
||||
// Post a synthetic Raw Input event to `hwnd` (a WM_INPUT carrying one of our g_raw_slots).
|
||||
void post_raw_key(HWND hwnd, UINT vk, bool down)
|
||||
{
|
||||
if (hwnd == nullptr || !g_hk_getrawinputdata)
|
||||
{
|
||||
if (hwnd == nullptr || !g_hk_getrawinputdata) {
|
||||
return;
|
||||
}
|
||||
RAWINPUT& ri = g_raw_slots[g_raw_head.fetch_add(1, std::memory_order_relaxed) % kRawSlots];
|
||||
@@ -264,8 +239,7 @@ void post_raw_key(HWND hwnd, UINT vk, bool down)
|
||||
|
||||
void post_raw_mouse(HWND hwnd, USHORT button_flags)
|
||||
{
|
||||
if (hwnd == nullptr || !g_hk_getrawinputdata)
|
||||
{
|
||||
if (hwnd == nullptr || !g_hk_getrawinputdata) {
|
||||
return;
|
||||
}
|
||||
RAWINPUT& ri = g_raw_slots[g_raw_head.fetch_add(1, std::memory_order_relaxed) % kRawSlots];
|
||||
@@ -283,8 +257,7 @@ LPARAM key_lparam(UINT vk, bool key_up)
|
||||
{
|
||||
const UINT scan = MapVirtualKeyW(vk, MAPVK_VK_TO_VSC);
|
||||
LPARAM lp = 1 | (static_cast<LPARAM>(scan) << 16); // repeat count 1 + scan code
|
||||
if (key_up)
|
||||
{
|
||||
if (key_up) {
|
||||
lp |= (LPARAM{1} << 30) | (LPARAM{1} << 31); // previous-down + transition (key released)
|
||||
}
|
||||
return lp;
|
||||
@@ -292,8 +265,7 @@ LPARAM key_lparam(UINT vk, bool key_up)
|
||||
|
||||
void set_key(UINT vk, bool down)
|
||||
{
|
||||
if (vk < 256)
|
||||
{
|
||||
if (vk < 256) {
|
||||
g_key_down[vk].store(down, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
@@ -301,16 +273,13 @@ void set_key(UINT vk, bool down)
|
||||
WPARAM mouse_button_wparam()
|
||||
{
|
||||
WPARAM w = 0;
|
||||
if (g_key_down[VK_LBUTTON].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_key_down[VK_LBUTTON].load(std::memory_order_relaxed)) {
|
||||
w |= MK_LBUTTON;
|
||||
}
|
||||
if (g_key_down[VK_RBUTTON].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_key_down[VK_RBUTTON].load(std::memory_order_relaxed)) {
|
||||
w |= MK_RBUTTON;
|
||||
}
|
||||
if (g_key_down[VK_MBUTTON].load(std::memory_order_relaxed))
|
||||
{
|
||||
if (g_key_down[VK_MBUTTON].load(std::memory_order_relaxed)) {
|
||||
w |= MK_MBUTTON;
|
||||
}
|
||||
return w;
|
||||
@@ -324,36 +293,25 @@ void handle_mouse(const MkbEvent& ev, bool down, HWND hwnd)
|
||||
|
||||
const UINT vk = ev.code == 0 ? VK_LBUTTON : ev.code == 1 ? VK_RBUTTON : VK_MBUTTON;
|
||||
set_key(vk, down);
|
||||
if (hwnd == nullptr)
|
||||
{
|
||||
if (hwnd == nullptr) {
|
||||
return;
|
||||
}
|
||||
UINT msg;
|
||||
if (ev.code == 0)
|
||||
{
|
||||
if (ev.code == 0) {
|
||||
msg = down ? WM_LBUTTONDOWN : WM_LBUTTONUP;
|
||||
}
|
||||
else if (ev.code == 1)
|
||||
{
|
||||
} else if (ev.code == 1) {
|
||||
msg = down ? WM_RBUTTONDOWN : WM_RBUTTONUP;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
msg = down ? WM_MBUTTONDOWN : WM_MBUTTONUP;
|
||||
}
|
||||
PostMessageW(hwnd, msg, mouse_button_wparam(), MAKELPARAM(ev.x, ev.y));
|
||||
// Also feed Raw Input games (button event; relative move isn't in the MKB event stream).
|
||||
USHORT rflags = 0;
|
||||
if (ev.code == 0)
|
||||
{
|
||||
if (ev.code == 0) {
|
||||
rflags = down ? RI_MOUSE_LEFT_BUTTON_DOWN : RI_MOUSE_LEFT_BUTTON_UP;
|
||||
}
|
||||
else if (ev.code == 1)
|
||||
{
|
||||
} else if (ev.code == 1) {
|
||||
rflags = down ? RI_MOUSE_RIGHT_BUTTON_DOWN : RI_MOUSE_RIGHT_BUTTON_UP;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
rflags = down ? RI_MOUSE_MIDDLE_BUTTON_DOWN : RI_MOUSE_MIDDLE_BUTTON_UP;
|
||||
}
|
||||
post_raw_mouse(hwnd, rflags);
|
||||
@@ -364,8 +322,7 @@ void handle_wheel(const MkbEvent& ev, HWND hwnd)
|
||||
g_cursor_x.store(ev.x, std::memory_order_relaxed);
|
||||
g_cursor_y.store(ev.y, std::memory_order_relaxed);
|
||||
g_have_cursor.store(true, std::memory_order_relaxed);
|
||||
if (hwnd == nullptr)
|
||||
{
|
||||
if (hwnd == nullptr) {
|
||||
return;
|
||||
}
|
||||
POINT pt{ev.x, ev.y};
|
||||
@@ -376,15 +333,12 @@ void handle_wheel(const MkbEvent& ev, HWND hwnd)
|
||||
|
||||
void install_user32_hook(HMODULE user32, const char* name, void* detour, safetyhook::InlineHook& slot, int id)
|
||||
{
|
||||
if (user32 == nullptr)
|
||||
{
|
||||
if (user32 == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(user32, name)))
|
||||
{
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(user32, name))) {
|
||||
install_inline(slot, target, detour); // StartDisabled -> assign -> enable (no install race)
|
||||
if (slot)
|
||||
{
|
||||
if (slot) {
|
||||
hook_set_installed(id, true);
|
||||
}
|
||||
}
|
||||
@@ -396,35 +350,27 @@ void install_user32_hook(HMODULE user32, const char* name, void* detour, safetyh
|
||||
// on the calling thread (the worker thread is).
|
||||
bool install_dinput_hook()
|
||||
{
|
||||
if (g_vh_di_getstate)
|
||||
{
|
||||
if (g_vh_di_getstate) {
|
||||
return true;
|
||||
}
|
||||
HMODULE di = GetModuleHandleW(L"dinput8.dll");
|
||||
if (di == nullptr)
|
||||
{
|
||||
if (di == nullptr) {
|
||||
return false; // not a DirectInput game (yet)
|
||||
}
|
||||
using PFN_DI8Create = HRESULT(WINAPI*)(HINSTANCE, DWORD, REFIID, LPVOID*, LPUNKNOWN);
|
||||
auto create = reinterpret_cast<PFN_DI8Create>(GetProcAddress(di, "DirectInput8Create"));
|
||||
if (create == nullptr)
|
||||
{
|
||||
if (create == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (g_di_probe == nullptr)
|
||||
{
|
||||
if (g_di_probe == nullptr) {
|
||||
if (FAILED(create(GetModuleHandleW(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8W,
|
||||
reinterpret_cast<void**>(&g_di_probe), nullptr)) ||
|
||||
g_di_probe == nullptr)
|
||||
{
|
||||
reinterpret_cast<void**>(&g_di_probe), nullptr))
|
||||
|| g_di_probe == nullptr) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (g_di_probe_kbd == nullptr)
|
||||
{
|
||||
if (FAILED(g_di_probe->CreateDevice(GUID_SysKeyboard, &g_di_probe_kbd, nullptr)) ||
|
||||
g_di_probe_kbd == nullptr)
|
||||
{
|
||||
if (g_di_probe_kbd == nullptr) {
|
||||
if (FAILED(g_di_probe->CreateDevice(GUID_SysKeyboard, &g_di_probe_kbd, nullptr)) || g_di_probe_kbd == nullptr) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -438,13 +384,11 @@ bool install_dinput_hook()
|
||||
|
||||
bool install_mkb_hooks(IpcClient& ipc)
|
||||
{
|
||||
if (g_installed)
|
||||
{
|
||||
if (g_installed) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (g_id_pump < 0)
|
||||
{
|
||||
if (g_id_pump < 0) {
|
||||
g_id_pump = hook_register("MKB pump (PostMessage)", HookSubsys_Mkb);
|
||||
g_id_async = hook_register("GetAsyncKeyState", HookSubsys_Mkb);
|
||||
g_id_kbstate = hook_register("GetKeyboardState", HookSubsys_Mkb);
|
||||
@@ -454,8 +398,7 @@ bool install_mkb_hooks(IpcClient& ipc)
|
||||
}
|
||||
|
||||
// Fresh synthesized state so a previous session leaves no stuck keys.
|
||||
for (int vk = 0; vk < 256; ++vk)
|
||||
{
|
||||
for (int vk = 0; vk < 256; ++vk) {
|
||||
g_key_down[vk].store(false, std::memory_order_relaxed);
|
||||
}
|
||||
g_have_cursor.store(false, std::memory_order_relaxed);
|
||||
@@ -469,8 +412,8 @@ bool install_mkb_hooks(IpcClient& ipc)
|
||||
install_user32_hook(user32, "GetCursorPos", reinterpret_cast<void*>(&hk_GetCursorPos), g_hk_cursor, g_id_cursor);
|
||||
// Raw Input: synthesize WM_INPUT (in mkb_pump) + serve it from this hook, for games that read
|
||||
// keyboard/mouse via GetRawInputData. GetRawInputData has a clean prologue -> inline hook is OK.
|
||||
install_user32_hook(user32, "GetRawInputData", reinterpret_cast<void*>(&hk_GetRawInputData),
|
||||
g_hk_getrawinputdata, g_id_rawinput);
|
||||
install_user32_hook(user32, "GetRawInputData", reinterpret_cast<void*>(&hk_GetRawInputData), g_hk_getrawinputdata,
|
||||
g_id_rawinput);
|
||||
// DirectInput: vtable-swap GetDeviceState (best-effort -- dinput8.dll may load later, retried
|
||||
// from mkb_pump). The probe is built once and kept alive (avoids COM churn on a re-enable).
|
||||
g_di_mouse_primed.store(false, std::memory_order_relaxed);
|
||||
@@ -485,8 +428,7 @@ bool install_mkb_hooks(IpcClient& ipc)
|
||||
|
||||
void remove_mkb_hooks()
|
||||
{
|
||||
if (!g_installed)
|
||||
{
|
||||
if (!g_installed) {
|
||||
return;
|
||||
}
|
||||
g_active.store(false, std::memory_order_release);
|
||||
@@ -503,8 +445,7 @@ void remove_mkb_hooks()
|
||||
g_gate.drain(); // wait for any in-flight polling / DI / raw detour before clearing state
|
||||
hook_set_installed(g_id_di_getstate, false);
|
||||
hook_set_installed(g_id_rawinput, false);
|
||||
for (int vk = 0; vk < 256; ++vk)
|
||||
{
|
||||
for (int vk = 0; vk < 256; ++vk) {
|
||||
g_key_down[vk].store(false, std::memory_order_relaxed); // no stuck keys
|
||||
}
|
||||
g_have_cursor.store(false, std::memory_order_relaxed);
|
||||
@@ -518,47 +459,39 @@ void remove_mkb_hooks()
|
||||
void mkb_pump(IpcClient& ipc)
|
||||
{
|
||||
MkbRing* ring = ipc.mkb_ring();
|
||||
if (ring == nullptr || !g_active.load(std::memory_order_relaxed))
|
||||
{
|
||||
if (ring == nullptr || !g_active.load(std::memory_order_relaxed)) {
|
||||
return;
|
||||
}
|
||||
if (!g_vh_di_getstate)
|
||||
{
|
||||
if (!g_vh_di_getstate) {
|
||||
install_dinput_hook(); // dinput8.dll can load after we installed; keep retrying cheaply
|
||||
}
|
||||
|
||||
HWND hwnd = static_cast<HWND>(g_target.load(std::memory_order_relaxed));
|
||||
if (hwnd == nullptr || !IsWindow(hwnd))
|
||||
{
|
||||
if (hwnd == nullptr || !IsWindow(hwnd)) {
|
||||
hwnd = find_main_window(GetCurrentProcessId());
|
||||
g_target.store(hwnd, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
MkbEvent ev{};
|
||||
while (pop_mkb_event(*ring, ev))
|
||||
{
|
||||
while (pop_mkb_event(*ring, ev)) {
|
||||
hook_note_call(g_id_pump);
|
||||
switch (ev.type)
|
||||
{
|
||||
switch (ev.type) {
|
||||
case Mkb_KeyDown:
|
||||
set_key(ev.code, true);
|
||||
if (hwnd != nullptr)
|
||||
{
|
||||
if (hwnd != nullptr) {
|
||||
PostMessageW(hwnd, WM_KEYDOWN, ev.code, key_lparam(ev.code, false));
|
||||
}
|
||||
post_raw_key(hwnd, ev.code, true); // also feed Raw Input games
|
||||
break;
|
||||
case Mkb_KeyUp:
|
||||
set_key(ev.code, false);
|
||||
if (hwnd != nullptr)
|
||||
{
|
||||
if (hwnd != nullptr) {
|
||||
PostMessageW(hwnd, WM_KEYUP, ev.code, key_lparam(ev.code, true));
|
||||
}
|
||||
post_raw_key(hwnd, ev.code, false);
|
||||
break;
|
||||
case Mkb_Char:
|
||||
if (hwnd != nullptr)
|
||||
{
|
||||
if (hwnd != nullptr) {
|
||||
PostMessageW(hwnd, WM_CHAR, ev.code, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
bool install_mkb_hooks(IpcClient& ipc);
|
||||
void remove_mkb_hooks();
|
||||
|
||||
@@ -17,11 +17,9 @@
|
||||
#include "hook_registry.hpp"
|
||||
#include "shared_video_texture.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight swap detours before remove frees the shared D3D state
|
||||
|
||||
@@ -65,13 +63,11 @@ thread_local bool t_in_swap = false;
|
||||
|
||||
void resolve_gl()
|
||||
{
|
||||
if (g_gl_resolved)
|
||||
{
|
||||
if (g_gl_resolved) {
|
||||
return;
|
||||
}
|
||||
HMODULE gl = GetModuleHandleW(L"opengl32.dll");
|
||||
if (gl == nullptr)
|
||||
{
|
||||
if (gl == nullptr) {
|
||||
return; // not an OpenGL process (yet)
|
||||
}
|
||||
g_glReadPixels = reinterpret_cast<PFN_glReadPixels>(GetProcAddress(gl, "glReadPixels"));
|
||||
@@ -82,14 +78,12 @@ void resolve_gl()
|
||||
|
||||
bool ensure_device()
|
||||
{
|
||||
if (g_device != nullptr)
|
||||
{
|
||||
if (g_device != nullptr) {
|
||||
return true;
|
||||
}
|
||||
const HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0,
|
||||
D3D11_SDK_VERSION, &g_device, nullptr, &g_ctx);
|
||||
if (FAILED(hr) || g_device == nullptr)
|
||||
{
|
||||
const HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0, D3D11_SDK_VERSION,
|
||||
&g_device, nullptr, &g_ctx);
|
||||
if (FAILED(hr) || g_device == nullptr) {
|
||||
logf("opengl: D3D11CreateDevice failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
return false;
|
||||
}
|
||||
@@ -100,10 +94,8 @@ bool ensure_device()
|
||||
void capture_gl(HDC hdc)
|
||||
{
|
||||
resolve_gl();
|
||||
if (!g_gl_resolved || g_wglGetCurrentContext() == nullptr)
|
||||
{
|
||||
if (!g_unsupported_logged)
|
||||
{
|
||||
if (!g_gl_resolved || g_wglGetCurrentContext() == nullptr) {
|
||||
if (!g_unsupported_logged) {
|
||||
logf("opengl: no current GL context / glReadPixels; capture idle");
|
||||
g_unsupported_logged = true;
|
||||
}
|
||||
@@ -112,32 +104,27 @@ void capture_gl(HDC hdc)
|
||||
|
||||
HWND hwnd = WindowFromDC(hdc);
|
||||
RECT rc{};
|
||||
if (hwnd == nullptr || !GetClientRect(hwnd, &rc))
|
||||
{
|
||||
if (hwnd == nullptr || !GetClientRect(hwnd, &rc)) {
|
||||
return;
|
||||
}
|
||||
const UINT w = static_cast<UINT>(rc.right - rc.left);
|
||||
const UINT h = static_cast<UINT>(rc.bottom - rc.top);
|
||||
if (w == 0 || h == 0)
|
||||
{
|
||||
if (w == 0 || h == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// DXGI_FORMAT_R8G8B8A8_UNORM matches glReadPixels(GL_RGBA) byte order.
|
||||
if (!ensure_device() || !g_shared.ensure(g_device, w, h, DXGI_FORMAT_R8G8B8A8_UNORM, g_pid, "opengl"))
|
||||
{
|
||||
if (!ensure_device() || !g_shared.ensure(g_device, w, h, DXGI_FORMAT_R8G8B8A8_UNORM, g_pid, "opengl")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t bytes = static_cast<size_t>(w) * h * 4;
|
||||
if (g_read_buf.size() != bytes)
|
||||
{
|
||||
if (g_read_buf.size() != bytes) {
|
||||
g_read_buf.resize(bytes);
|
||||
g_flip_buf.resize(bytes);
|
||||
}
|
||||
|
||||
if (g_glPixelStorei != nullptr)
|
||||
{
|
||||
if (g_glPixelStorei != nullptr) {
|
||||
g_glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
||||
}
|
||||
// Reads the back buffer of the current context (bottom-up, origin lower-left).
|
||||
@@ -145,19 +132,16 @@ void capture_gl(HDC hdc)
|
||||
|
||||
// Flip vertically so the image is top-down like a D3D backbuffer.
|
||||
const size_t row = static_cast<size_t>(w) * 4;
|
||||
for (UINT y = 0; y < h; ++y)
|
||||
{
|
||||
for (UINT y = 0; y < h; ++y) {
|
||||
memcpy(g_flip_buf.data() + y * row, g_read_buf.data() + (h - 1 - y) * row, row);
|
||||
}
|
||||
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK)
|
||||
{
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK) {
|
||||
g_ctx->UpdateSubresource(g_shared.texture(), 0, nullptr, g_flip_buf.data(), static_cast<UINT>(row), 0);
|
||||
g_ctx->Flush();
|
||||
g_shared.mutex()->ReleaseSync(kVideoMutexKey);
|
||||
g_frames_shared.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->publish_video_frame(w, h, static_cast<std::uint32_t>(DXGI_FORMAT_R8G8B8A8_UNORM));
|
||||
}
|
||||
}
|
||||
@@ -170,18 +154,15 @@ BOOL swap_detour(safetyhook::InlineHook& hook, int hook_id, HDC hdc)
|
||||
hook_note_call(hook_id);
|
||||
g_swaps.fetch_add(1, std::memory_order_relaxed);
|
||||
const bool outer = !t_in_swap;
|
||||
if (outer)
|
||||
{
|
||||
if (outer) {
|
||||
t_in_swap = true;
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->note_present();
|
||||
}
|
||||
capture_gl(hdc);
|
||||
}
|
||||
const BOOL r = hook.stdcall<BOOL>(hdc); // __stdcall: call() is __cdecl on x86 -> crash
|
||||
if (outer)
|
||||
{
|
||||
if (outer) {
|
||||
t_in_swap = false;
|
||||
}
|
||||
return r;
|
||||
@@ -205,8 +186,7 @@ bool install_opengl_hooks(IpcClient& ipc)
|
||||
{
|
||||
g_ipc = &ipc;
|
||||
g_pid = GetCurrentProcessId();
|
||||
if (g_hk_swapbuffers.enabled() || g_hk_wglswap.enabled())
|
||||
{
|
||||
if (g_hk_swapbuffers.enabled() || g_hk_wglswap.enabled()) {
|
||||
return true; // already installed (persistent hooks; re-install below re-enables them)
|
||||
}
|
||||
|
||||
@@ -215,18 +195,14 @@ bool install_opengl_hooks(IpcClient& ipc)
|
||||
g_unsupported_logged = false;
|
||||
|
||||
// gdi32!SwapBuffers is always available (the common GL present call).
|
||||
if (HMODULE gdi = GetModuleHandleW(L"gdi32.dll"))
|
||||
{
|
||||
if (void* fn = reinterpret_cast<void*>(GetProcAddress(gdi, "SwapBuffers")))
|
||||
{
|
||||
if (HMODULE gdi = GetModuleHandleW(L"gdi32.dll")) {
|
||||
if (void* fn = reinterpret_cast<void*>(GetProcAddress(gdi, "SwapBuffers"))) {
|
||||
install_inline(g_hk_swapbuffers, fn, &hk_SwapBuffers);
|
||||
}
|
||||
}
|
||||
// opengl32!wglSwapBuffers if OpenGL is already loaded.
|
||||
if (HMODULE gl = GetModuleHandleW(L"opengl32.dll"))
|
||||
{
|
||||
if (void* fn = reinterpret_cast<void*>(GetProcAddress(gl, "wglSwapBuffers")))
|
||||
{
|
||||
if (HMODULE gl = GetModuleHandleW(L"opengl32.dll")) {
|
||||
if (void* fn = reinterpret_cast<void*>(GetProcAddress(gl, "wglSwapBuffers"))) {
|
||||
install_inline(g_hk_wglswap, fn, &hk_wglSwapBuffers);
|
||||
}
|
||||
}
|
||||
@@ -251,13 +227,11 @@ void remove_opengl_hooks()
|
||||
hook_set_installed(g_id_wglswap, false);
|
||||
g_gate.drain();
|
||||
g_shared.release();
|
||||
if (g_ctx != nullptr)
|
||||
{
|
||||
if (g_ctx != nullptr) {
|
||||
g_ctx->Release();
|
||||
g_ctx = nullptr;
|
||||
}
|
||||
if (g_device != nullptr)
|
||||
{
|
||||
if (g_device != nullptr) {
|
||||
g_device->Release();
|
||||
g_device = nullptr;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,7 @@
|
||||
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Installs the OpenGL swap hooks. `ipc` must outlive the hooks. Returns true if at
|
||||
// least SwapBuffers was hooked. Safe to call repeatedly.
|
||||
|
||||
@@ -22,11 +22,9 @@
|
||||
#include "shared_video_texture.hpp"
|
||||
#include "vtable_hook.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight Present/ECL detours before remove frees the shared state
|
||||
|
||||
@@ -116,8 +114,7 @@ std::atomic<ID3D12CommandQueue*> g_present_queue{nullptr};
|
||||
// test present is counted but produces no frame. Render-thread only; small fixed tables.
|
||||
constexpr int kMaxLoggedPresents = 16;
|
||||
constexpr int kMaxLoggedSwapchains = 8;
|
||||
struct LoggedPresent
|
||||
{
|
||||
struct LoggedPresent {
|
||||
void* swapchain;
|
||||
UINT flags;
|
||||
};
|
||||
@@ -129,15 +126,12 @@ int g_logged_swapchains_n = 0;
|
||||
// True the first time this (swapchain, flags) pair is presented, so the caller logs once.
|
||||
bool first_present_with_flags(void* swapchain, UINT flags)
|
||||
{
|
||||
for (int i = 0; i < g_logged_presents_n; ++i)
|
||||
{
|
||||
if (g_logged_presents[i].swapchain == swapchain && g_logged_presents[i].flags == flags)
|
||||
{
|
||||
for (int i = 0; i < g_logged_presents_n; ++i) {
|
||||
if (g_logged_presents[i].swapchain == swapchain && g_logged_presents[i].flags == flags) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (g_logged_presents_n >= kMaxLoggedPresents)
|
||||
{
|
||||
if (g_logged_presents_n >= kMaxLoggedPresents) {
|
||||
return false;
|
||||
}
|
||||
g_logged_presents[g_logged_presents_n++] = {swapchain, flags};
|
||||
@@ -147,15 +141,12 @@ bool first_present_with_flags(void* swapchain, UINT flags)
|
||||
// True the first time this swapchain feeds the capture, so the caller logs it once.
|
||||
bool first_capture_from(void* swapchain)
|
||||
{
|
||||
for (int i = 0; i < g_logged_swapchains_n; ++i)
|
||||
{
|
||||
if (g_logged_swapchains[i] == swapchain)
|
||||
{
|
||||
for (int i = 0; i < g_logged_swapchains_n; ++i) {
|
||||
if (g_logged_swapchains[i] == swapchain) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (g_logged_swapchains_n >= kMaxLoggedSwapchains)
|
||||
{
|
||||
if (g_logged_swapchains_n >= kMaxLoggedSwapchains) {
|
||||
return false;
|
||||
}
|
||||
g_logged_swapchains[g_logged_swapchains_n++] = swapchain;
|
||||
@@ -165,33 +156,27 @@ bool first_capture_from(void* swapchain)
|
||||
// Drop the D3D11On12 bridge. Caller holds g_tex_mutex.
|
||||
void release_on12_locked()
|
||||
{
|
||||
if (g_on12_ctx != nullptr)
|
||||
{
|
||||
if (g_on12_ctx != nullptr) {
|
||||
g_on12_ctx->Release();
|
||||
g_on12_ctx = nullptr;
|
||||
}
|
||||
if (g_on12 != nullptr)
|
||||
{
|
||||
if (g_on12 != nullptr) {
|
||||
g_on12->Release();
|
||||
g_on12 = nullptr;
|
||||
}
|
||||
if (g_on12_d3d11 != nullptr)
|
||||
{
|
||||
if (g_on12_d3d11 != nullptr) {
|
||||
g_on12_d3d11->Release();
|
||||
g_on12_d3d11 = nullptr;
|
||||
}
|
||||
if (g_on12_queue != nullptr)
|
||||
{
|
||||
if (g_on12_queue != nullptr) {
|
||||
g_on12_queue->Release();
|
||||
g_on12_queue = nullptr;
|
||||
}
|
||||
if (g_on12_d3d12 != nullptr)
|
||||
{
|
||||
if (g_on12_d3d12 != nullptr) {
|
||||
g_on12_d3d12->Release();
|
||||
g_on12_d3d12 = nullptr;
|
||||
}
|
||||
if (g_copy_fence != nullptr)
|
||||
{
|
||||
if (g_copy_fence != nullptr) {
|
||||
g_copy_fence->Release();
|
||||
g_copy_fence = nullptr;
|
||||
}
|
||||
@@ -205,8 +190,7 @@ void release_on12_locked()
|
||||
// holds g_tex_mutex. Returns true when the bridge is ready.
|
||||
bool ensure_on12_locked(ID3D12Device* dev)
|
||||
{
|
||||
if (g_on12 != nullptr && g_on12_d3d12 == dev)
|
||||
{
|
||||
if (g_on12 != nullptr && g_on12_d3d12 == dev) {
|
||||
return true;
|
||||
}
|
||||
release_on12_locked();
|
||||
@@ -215,8 +199,7 @@ bool ensure_on12_locked(ID3D12Device* dev)
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
ID3D12CommandQueue* queue = nullptr;
|
||||
HRESULT hr = dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&queue));
|
||||
if (FAILED(hr) || queue == nullptr)
|
||||
{
|
||||
if (FAILED(hr) || queue == nullptr) {
|
||||
logf("present(d3d12): CreateCommandQueue failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
return false;
|
||||
}
|
||||
@@ -225,19 +208,16 @@ bool ensure_on12_locked(ID3D12Device* dev)
|
||||
ID3D11Device* d11 = nullptr;
|
||||
ID3D11DeviceContext* ctx = nullptr;
|
||||
hr = D3D11On12CreateDevice(dev, 0, nullptr, 0, queues, 1, 0, &d11, &ctx, nullptr);
|
||||
if (FAILED(hr) || d11 == nullptr)
|
||||
{
|
||||
if (FAILED(hr) || d11 == nullptr) {
|
||||
logf("present(d3d12): D3D11On12CreateDevice failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
queue->Release();
|
||||
return false;
|
||||
}
|
||||
ID3D11On12Device* on12 = nullptr;
|
||||
hr = d11->QueryInterface(__uuidof(ID3D11On12Device), reinterpret_cast<void**>(&on12));
|
||||
if (FAILED(hr) || on12 == nullptr)
|
||||
{
|
||||
if (FAILED(hr) || on12 == nullptr) {
|
||||
logf("present(d3d12): QI ID3D11On12Device failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
if (ctx != nullptr)
|
||||
{
|
||||
if (ctx != nullptr) {
|
||||
ctx->Release();
|
||||
}
|
||||
d11->Release();
|
||||
@@ -249,10 +229,8 @@ bool ensure_on12_locked(ID3D12Device* dev)
|
||||
// cross-queue ordering (the copy can then race the frame, the pre-fence behavior).
|
||||
ID3D12Fence* fence = nullptr;
|
||||
hr = dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), reinterpret_cast<void**>(&fence));
|
||||
if (FAILED(hr) || fence == nullptr)
|
||||
{
|
||||
logf("present(d3d12): CreateFence failed hr=0x%08lX (copy will be unordered)",
|
||||
static_cast<unsigned long>(hr));
|
||||
if (FAILED(hr) || fence == nullptr) {
|
||||
logf("present(d3d12): CreateFence failed hr=0x%08lX (copy will be unordered)", static_cast<unsigned long>(hr));
|
||||
fence = nullptr;
|
||||
}
|
||||
|
||||
@@ -283,17 +261,14 @@ void capture_backbuffer_d3d12(IDXGISwapChain* sc)
|
||||
// buffer. Query IDXGISwapChain3 for it; fall back to 0 only if unavailable.
|
||||
UINT bb_index = 0;
|
||||
IDXGISwapChain3* sc3 = nullptr;
|
||||
if (SUCCEEDED(sc->QueryInterface(__uuidof(IDXGISwapChain3), reinterpret_cast<void**>(&sc3))) && sc3 != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(sc->QueryInterface(__uuidof(IDXGISwapChain3), reinterpret_cast<void**>(&sc3))) && sc3 != nullptr) {
|
||||
bb_index = sc3->GetCurrentBackBufferIndex();
|
||||
sc3->Release();
|
||||
}
|
||||
|
||||
ID3D12Resource* bb = nullptr;
|
||||
if (FAILED(sc->GetBuffer(bb_index, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&bb))) || bb == nullptr)
|
||||
{
|
||||
if (!g_unsupported_logged)
|
||||
{
|
||||
if (FAILED(sc->GetBuffer(bb_index, __uuidof(ID3D12Resource), reinterpret_cast<void**>(&bb))) || bb == nullptr) {
|
||||
if (!g_unsupported_logged) {
|
||||
logf("present: backbuffer is neither ID3D11Texture2D nor ID3D12Resource (D3D9/Vulkan?); idle");
|
||||
g_unsupported_logged = true;
|
||||
}
|
||||
@@ -309,25 +284,21 @@ void capture_backbuffer_d3d12(IDXGISwapChain* sc)
|
||||
DXGI_FORMAT fmt = DXGI_FORMAT_UNKNOWN;
|
||||
// The game's present queue, preferring the one seen on this (the render) thread.
|
||||
ID3D12CommandQueue* game_queue = t_present_queue;
|
||||
if (game_queue == nullptr)
|
||||
{
|
||||
if (game_queue == nullptr) {
|
||||
game_queue = g_present_queue.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// Log each distinct swapchain feeding the capture once (size/format/buffer index).
|
||||
if (first_capture_from(sc))
|
||||
{
|
||||
if (first_capture_from(sc)) {
|
||||
const D3D12_RESOURCE_DESC rd = bb->GetDesc();
|
||||
logf("present: swapchain=%p capturing D3D12 backbuffer %llux%u fmt=%d samples=%u bufferindex=%u queue=%s",
|
||||
sc, static_cast<unsigned long long>(rd.Width), rd.Height, static_cast<int>(rd.Format),
|
||||
rd.SampleDesc.Count, bb_index, game_queue != nullptr ? "known" : "unknown");
|
||||
logf("present: swapchain=%p capturing D3D12 backbuffer %llux%u fmt=%d samples=%u bufferindex=%u queue=%s", sc,
|
||||
static_cast<unsigned long long>(rd.Width), rd.Height, static_cast<int>(rd.Format), rd.SampleDesc.Count,
|
||||
bb_index, game_queue != nullptr ? "known" : "unknown");
|
||||
}
|
||||
|
||||
if (dev != nullptr)
|
||||
{
|
||||
if (dev != nullptr) {
|
||||
std::scoped_lock lock(g_tex_mutex);
|
||||
if (ensure_on12_locked(dev))
|
||||
{
|
||||
if (ensure_on12_locked(dev)) {
|
||||
// DX12 capture costs more present-thread overhead than DX11/OpenGL (~0.38 ms vs
|
||||
// ~0.05/0.09 ms, measured) because it goes through the D3D11On12 bridge: the
|
||||
// CopyResource on the 11On12 immediate context (~0.13 ms) plus the mandatory Flush
|
||||
@@ -339,8 +310,7 @@ void capture_backbuffer_d3d12(IDXGISwapChain* sc)
|
||||
// Order our copy after the game's frame without burdening the game's queue: the
|
||||
// game queue signals the fence (cheap), our copy queue waits on it. Skipped if the
|
||||
// queue isn't captured yet or the fence is missing (one possibly-early frame).
|
||||
if (game_queue != nullptr && g_copy_fence != nullptr)
|
||||
{
|
||||
if (game_queue != nullptr && g_copy_fence != nullptr) {
|
||||
const UINT64 fence_val = ++g_copy_fence_val;
|
||||
game_queue->Signal(g_copy_fence, fence_val);
|
||||
g_on12_queue->Wait(g_copy_fence, fence_val);
|
||||
@@ -349,33 +319,26 @@ void capture_backbuffer_d3d12(IDXGISwapChain* sc)
|
||||
D3D11_RESOURCE_FLAGS rf{};
|
||||
rf.BindFlags = D3D11_BIND_RENDER_TARGET;
|
||||
ID3D11Resource* wrapped = nullptr;
|
||||
HRESULT hr = g_on12->CreateWrappedResource(bb, &rf, D3D12_RESOURCE_STATE_PRESENT,
|
||||
D3D12_RESOURCE_STATE_PRESENT, __uuidof(ID3D11Resource),
|
||||
reinterpret_cast<void**>(&wrapped));
|
||||
if (SUCCEEDED(hr) && wrapped != nullptr)
|
||||
{
|
||||
HRESULT hr =
|
||||
g_on12->CreateWrappedResource(bb, &rf, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_PRESENT,
|
||||
__uuidof(ID3D11Resource), reinterpret_cast<void**>(&wrapped));
|
||||
if (SUCCEEDED(hr) && wrapped != nullptr) {
|
||||
g_on12->AcquireWrappedResources(&wrapped, 1);
|
||||
ID3D11Texture2D* wtex = nullptr;
|
||||
if (SUCCEEDED(wrapped->QueryInterface(__uuidof(ID3D11Texture2D),
|
||||
reinterpret_cast<void**>(&wtex))) &&
|
||||
wtex != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(wrapped->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&wtex)))
|
||||
&& wtex != nullptr) {
|
||||
D3D11_TEXTURE2D_DESC d{};
|
||||
wtex->GetDesc(&d);
|
||||
w = d.Width;
|
||||
h = d.Height;
|
||||
fmt = d.Format;
|
||||
if (d.SampleDesc.Count == 1 &&
|
||||
g_shared.ensure(g_on12_d3d11, w, h, fmt, g_pid, "present", kShareBind))
|
||||
{
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 0) == S_OK)
|
||||
{
|
||||
if (d.SampleDesc.Count == 1
|
||||
&& g_shared.ensure(g_on12_d3d11, w, h, fmt, g_pid, "present", kShareBind)) {
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 0) == S_OK) {
|
||||
g_on12_ctx->CopyResource(g_shared.texture(), wtex);
|
||||
g_shared.mutex()->ReleaseSync(kVideoMutexKey);
|
||||
shared = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
dropped = true; // host held the mutex -> this frame never reaches the mirror
|
||||
}
|
||||
}
|
||||
@@ -384,29 +347,22 @@ void capture_backbuffer_d3d12(IDXGISwapChain* sc)
|
||||
g_on12->ReleaseWrappedResources(&wrapped, 1);
|
||||
g_on12_ctx->Flush();
|
||||
wrapped->Release();
|
||||
}
|
||||
else if (!g_unsupported_logged)
|
||||
{
|
||||
} else if (!g_unsupported_logged) {
|
||||
logf("present(d3d12): CreateWrappedResource failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
g_unsupported_logged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shared)
|
||||
{
|
||||
if (shared) {
|
||||
g_frames_shared.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->publish_video_frame(w, h, static_cast<std::uint32_t>(fmt));
|
||||
}
|
||||
}
|
||||
else if (dropped && g_ipc != nullptr)
|
||||
{
|
||||
} else if (dropped && g_ipc != nullptr) {
|
||||
g_ipc->note_video_dropped();
|
||||
}
|
||||
if (dev != nullptr)
|
||||
{
|
||||
if (dev != nullptr) {
|
||||
dev->Release();
|
||||
}
|
||||
bb->Release();
|
||||
@@ -415,26 +371,22 @@ void capture_backbuffer_d3d12(IDXGISwapChain* sc)
|
||||
// Drop the hook-owned D3D11 device and the D3D10 staging texture. Caller holds g_tex_mutex.
|
||||
void release_aux_locked()
|
||||
{
|
||||
if (g_d3d10_staging != nullptr)
|
||||
{
|
||||
if (g_d3d10_staging != nullptr) {
|
||||
g_d3d10_staging->Release();
|
||||
g_d3d10_staging = nullptr;
|
||||
}
|
||||
if (g_d3d10_dev != nullptr)
|
||||
{
|
||||
if (g_d3d10_dev != nullptr) {
|
||||
g_d3d10_dev->Release();
|
||||
g_d3d10_dev = nullptr;
|
||||
}
|
||||
g_d3d10_w = g_d3d10_h = 0;
|
||||
g_d3d10_fmt = DXGI_FORMAT_UNKNOWN;
|
||||
g_force_d3d10 = false;
|
||||
if (g_aux_ctx != nullptr)
|
||||
{
|
||||
if (g_aux_ctx != nullptr) {
|
||||
g_aux_ctx->Release();
|
||||
g_aux_ctx = nullptr;
|
||||
}
|
||||
if (g_aux_d3d11 != nullptr)
|
||||
{
|
||||
if (g_aux_d3d11 != nullptr) {
|
||||
g_aux_d3d11->Release();
|
||||
g_aux_d3d11 = nullptr;
|
||||
}
|
||||
@@ -444,16 +396,14 @@ void release_aux_locked()
|
||||
// (the game has no D3D11 device of its own). Caller holds g_tex_mutex.
|
||||
bool ensure_aux_d3d11_locked()
|
||||
{
|
||||
if (g_aux_d3d11 != nullptr)
|
||||
{
|
||||
if (g_aux_d3d11 != nullptr) {
|
||||
return true;
|
||||
}
|
||||
const D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0};
|
||||
HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, levels,
|
||||
static_cast<UINT>(std::size(levels)), D3D11_SDK_VERSION, &g_aux_d3d11, nullptr,
|
||||
&g_aux_ctx);
|
||||
if (FAILED(hr) || g_aux_d3d11 == nullptr)
|
||||
{
|
||||
HRESULT hr =
|
||||
D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, levels, static_cast<UINT>(std::size(levels)),
|
||||
D3D11_SDK_VERSION, &g_aux_d3d11, nullptr, &g_aux_ctx);
|
||||
if (FAILED(hr) || g_aux_d3d11 == nullptr) {
|
||||
logf("present(d3d10): aux D3D11CreateDevice failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
g_aux_d3d11 = nullptr;
|
||||
g_aux_ctx = nullptr;
|
||||
@@ -470,20 +420,17 @@ void capture_backbuffer_d3d10(IDXGISwapChain* sc, ID3D10Texture2D* backbuf)
|
||||
{
|
||||
D3D10_TEXTURE2D_DESC bd{};
|
||||
backbuf->GetDesc(&bd);
|
||||
if (first_capture_from(sc))
|
||||
{
|
||||
if (first_capture_from(sc)) {
|
||||
logf("present: swapchain=%p capturing D3D10 backbuffer %ux%u fmt=%d samples=%u", sc, bd.Width, bd.Height,
|
||||
static_cast<int>(bd.Format), bd.SampleDesc.Count);
|
||||
}
|
||||
if (bd.SampleDesc.Count != 1)
|
||||
{
|
||||
if (bd.SampleDesc.Count != 1) {
|
||||
return; // MSAA: would need ResolveSubresource; skip rather than mis-copy
|
||||
}
|
||||
|
||||
ID3D10Device* gdev = nullptr;
|
||||
backbuf->GetDevice(&gdev);
|
||||
if (gdev == nullptr)
|
||||
{
|
||||
if (gdev == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -491,16 +438,13 @@ void capture_backbuffer_d3d10(IDXGISwapChain* sc, ID3D10Texture2D* backbuf)
|
||||
bool dropped = false;
|
||||
{
|
||||
std::scoped_lock lock(g_tex_mutex);
|
||||
if (!(g_d3d10_staging != nullptr && g_d3d10_dev == gdev && g_d3d10_w == bd.Width &&
|
||||
g_d3d10_h == bd.Height && g_d3d10_fmt == bd.Format))
|
||||
{
|
||||
if (g_d3d10_staging != nullptr)
|
||||
{
|
||||
if (!(g_d3d10_staging != nullptr && g_d3d10_dev == gdev && g_d3d10_w == bd.Width && g_d3d10_h == bd.Height
|
||||
&& g_d3d10_fmt == bd.Format)) {
|
||||
if (g_d3d10_staging != nullptr) {
|
||||
g_d3d10_staging->Release();
|
||||
g_d3d10_staging = nullptr;
|
||||
}
|
||||
if (g_d3d10_dev != nullptr)
|
||||
{
|
||||
if (g_d3d10_dev != nullptr) {
|
||||
g_d3d10_dev->Release();
|
||||
g_d3d10_dev = nullptr;
|
||||
}
|
||||
@@ -513,8 +457,7 @@ void capture_backbuffer_d3d10(IDXGISwapChain* sc, ID3D10Texture2D* backbuf)
|
||||
sd.SampleDesc.Count = 1;
|
||||
sd.Usage = D3D10_USAGE_STAGING;
|
||||
sd.CPUAccessFlags = D3D10_CPU_ACCESS_READ;
|
||||
if (SUCCEEDED(gdev->CreateTexture2D(&sd, nullptr, &g_d3d10_staging)) && g_d3d10_staging != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(gdev->CreateTexture2D(&sd, nullptr, &g_d3d10_staging)) && g_d3d10_staging != nullptr) {
|
||||
g_d3d10_dev = gdev;
|
||||
gdev->AddRef();
|
||||
g_d3d10_w = bd.Width;
|
||||
@@ -523,21 +466,16 @@ void capture_backbuffer_d3d10(IDXGISwapChain* sc, ID3D10Texture2D* backbuf)
|
||||
}
|
||||
}
|
||||
|
||||
if (g_d3d10_staging != nullptr && ensure_aux_d3d11_locked() &&
|
||||
g_shared.ensure(g_aux_d3d11, bd.Width, bd.Height, bd.Format, g_pid, "present", kShareBind))
|
||||
{
|
||||
if (g_d3d10_staging != nullptr && ensure_aux_d3d11_locked()
|
||||
&& g_shared.ensure(g_aux_d3d11, bd.Width, bd.Height, bd.Format, g_pid, "present", kShareBind)) {
|
||||
gdev->CopyResource(g_d3d10_staging, backbuf);
|
||||
D3D10_MAPPED_TEXTURE2D m{};
|
||||
if (SUCCEEDED(g_d3d10_staging->Map(0, D3D10_MAP_READ, 0, &m)) && m.pData != nullptr)
|
||||
{
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK)
|
||||
{
|
||||
if (SUCCEEDED(g_d3d10_staging->Map(0, D3D10_MAP_READ, 0, &m)) && m.pData != nullptr) {
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK) {
|
||||
g_aux_ctx->UpdateSubresource(g_shared.texture(), 0, nullptr, m.pData, m.RowPitch, 0);
|
||||
g_shared.mutex()->ReleaseSync(kVideoMutexKey);
|
||||
shared = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
dropped = true;
|
||||
}
|
||||
g_d3d10_staging->Unmap(0);
|
||||
@@ -545,16 +483,12 @@ void capture_backbuffer_d3d10(IDXGISwapChain* sc, ID3D10Texture2D* backbuf)
|
||||
}
|
||||
}
|
||||
|
||||
if (shared)
|
||||
{
|
||||
if (shared) {
|
||||
g_frames_shared.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->publish_video_frame(bd.Width, bd.Height, static_cast<std::uint32_t>(bd.Format));
|
||||
}
|
||||
}
|
||||
else if (dropped && g_ipc != nullptr)
|
||||
{
|
||||
} else if (dropped && g_ipc != nullptr) {
|
||||
g_ipc->note_video_dropped();
|
||||
}
|
||||
gdev->Release();
|
||||
@@ -568,12 +502,10 @@ void capture_backbuffer(IDXGISwapChain* sc)
|
||||
// ID3D11Texture2D (so we can't discriminate by GetBuffer), but its feature-level-10 device
|
||||
// rejects the share flags -- so a failed shared-texture creation is the signal to switch
|
||||
// (sticky) to the D3D10 read-back path, which reads through the game's own D3D10 device.
|
||||
if (!g_force_d3d10)
|
||||
{
|
||||
if (!g_force_d3d10) {
|
||||
ID3D11Texture2D* backbuf = nullptr;
|
||||
if (FAILED(sc->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backbuf))) ||
|
||||
backbuf == nullptr)
|
||||
{
|
||||
if (FAILED(sc->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backbuf)))
|
||||
|| backbuf == nullptr) {
|
||||
capture_backbuffer_d3d12(sc); // D3D12 game: bridge via D3D11On12 (or idle if neither)
|
||||
return;
|
||||
}
|
||||
@@ -583,8 +515,7 @@ void capture_backbuffer(IDXGISwapChain* sc)
|
||||
bool shared = false;
|
||||
bool dropped = false;
|
||||
bool cant_host = false;
|
||||
if (bd.SampleDesc.Count != 1)
|
||||
{
|
||||
if (bd.SampleDesc.Count != 1) {
|
||||
backbuf->Release(); // MSAA would need ResolveSubresource; skip rather than mis-copy
|
||||
return;
|
||||
}
|
||||
@@ -592,62 +523,47 @@ void capture_backbuffer(IDXGISwapChain* sc)
|
||||
ID3D11Device* device = nullptr;
|
||||
backbuf->GetDevice(&device);
|
||||
ID3D11DeviceContext* ctx = nullptr;
|
||||
if (device != nullptr)
|
||||
{
|
||||
if (device != nullptr) {
|
||||
device->GetImmediateContext(&ctx);
|
||||
}
|
||||
if (device != nullptr && ctx != nullptr)
|
||||
{
|
||||
if (device != nullptr && ctx != nullptr) {
|
||||
std::scoped_lock lock(g_tex_mutex);
|
||||
if (g_shared.ensure(device, bd.Width, bd.Height, bd.Format, g_pid, "present", kShareBind))
|
||||
{
|
||||
if (first_capture_from(sc))
|
||||
{
|
||||
if (g_shared.ensure(device, bd.Width, bd.Height, bd.Format, g_pid, "present", kShareBind)) {
|
||||
if (first_capture_from(sc)) {
|
||||
logf("present: swapchain=%p capturing D3D11 backbuffer %ux%u fmt=%d samples=%u", sc, bd.Width,
|
||||
bd.Height, static_cast<int>(bd.Format), bd.SampleDesc.Count);
|
||||
}
|
||||
// Key 0 on both sides: a plain cross-process mutex on the texture (created
|
||||
// released at key 0). Bounded wait so a stalled host consumer can never hang
|
||||
// the game's render thread.
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK)
|
||||
{
|
||||
if (g_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK) {
|
||||
ctx->CopyResource(g_shared.texture(), backbuf);
|
||||
g_shared.mutex()->ReleaseSync(kVideoMutexKey);
|
||||
shared = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
dropped = true; // host held the mutex past the wait -> frame lost (rare on D3D11)
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cant_host = true; // device can't host the shared texture -> try the D3D10 path
|
||||
}
|
||||
}
|
||||
if (ctx != nullptr)
|
||||
{
|
||||
if (ctx != nullptr) {
|
||||
ctx->Release();
|
||||
}
|
||||
if (device != nullptr)
|
||||
{
|
||||
if (device != nullptr) {
|
||||
device->Release();
|
||||
}
|
||||
backbuf->Release();
|
||||
|
||||
if (shared)
|
||||
{
|
||||
if (shared) {
|
||||
g_frames_shared.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->publish_video_frame(bd.Width, bd.Height, static_cast<std::uint32_t>(bd.Format));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!cant_host)
|
||||
{
|
||||
if (dropped && g_ipc != nullptr)
|
||||
{
|
||||
if (!cant_host) {
|
||||
if (dropped && g_ipc != nullptr) {
|
||||
g_ipc->note_video_dropped();
|
||||
}
|
||||
return; // captured-or-dropped on the D3D11 path; nothing else to try this frame
|
||||
@@ -659,8 +575,7 @@ void capture_backbuffer(IDXGISwapChain* sc)
|
||||
|
||||
// D3D10 game: its backbuffer must be read through its own D3D10 device.
|
||||
ID3D10Texture2D* bb10 = nullptr;
|
||||
if (SUCCEEDED(sc->GetBuffer(0, __uuidof(ID3D10Texture2D), reinterpret_cast<void**>(&bb10))) && bb10 != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(sc->GetBuffer(0, __uuidof(ID3D10Texture2D), reinterpret_cast<void**>(&bb10))) && bb10 != nullptr) {
|
||||
capture_backbuffer_d3d10(sc, bb10);
|
||||
bb10->Release();
|
||||
}
|
||||
@@ -672,8 +587,7 @@ void STDMETHODCALLTYPE hk_ExecuteCommandLists(ID3D12CommandQueue* queue, UINT nu
|
||||
DetourGate::Guard guard(g_gate); // keep g_present_queue/g_hk_ecl alive for this detour
|
||||
// Record the graphics queue; compute/copy queues never present, so skip them and
|
||||
// keep the last DIRECT one (the present queue on single-graphics-queue engines).
|
||||
if (queue != nullptr && queue->GetDesc().Type == D3D12_COMMAND_LIST_TYPE_DIRECT)
|
||||
{
|
||||
if (queue != nullptr && queue->GetDesc().Type == D3D12_COMMAND_LIST_TYPE_DIRECT) {
|
||||
t_present_queue = queue;
|
||||
g_present_queue.store(queue, std::memory_order_relaxed);
|
||||
hook_note_call(g_id_ecl);
|
||||
@@ -689,29 +603,25 @@ void STDMETHODCALLTYPE hk_ExecuteCommandLists(ID3D12CommandQueue* queue, UINT nu
|
||||
void* grab_execute_command_lists_address()
|
||||
{
|
||||
HMODULE d3d12 = GetModuleHandleW(L"d3d12.dll");
|
||||
if (d3d12 == nullptr)
|
||||
{
|
||||
if (d3d12 == nullptr) {
|
||||
return nullptr; // not a D3D12 game -> nothing to capture
|
||||
}
|
||||
using PFN_D3D12_CREATE_DEVICE = HRESULT(WINAPI*)(IUnknown*, D3D_FEATURE_LEVEL, REFIID, void**);
|
||||
auto create = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(GetProcAddress(d3d12, "D3D12CreateDevice"));
|
||||
if (create == nullptr)
|
||||
{
|
||||
if (create == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
ID3D12Device* dev = nullptr;
|
||||
if (FAILED(create(nullptr, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&dev))) ||
|
||||
dev == nullptr)
|
||||
{
|
||||
if (FAILED(create(nullptr, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device), reinterpret_cast<void**>(&dev)))
|
||||
|| dev == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
D3D12_COMMAND_QUEUE_DESC qd{};
|
||||
qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
ID3D12CommandQueue* queue = nullptr;
|
||||
void* addr = nullptr;
|
||||
if (SUCCEEDED(dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&queue))) &&
|
||||
queue != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(dev->CreateCommandQueue(&qd, __uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&queue)))
|
||||
&& queue != nullptr) {
|
||||
addr = vtable_method(queue, kIdx_ID3D12CommandQueue_ExecuteCommandLists);
|
||||
queue->Release();
|
||||
}
|
||||
@@ -728,17 +638,14 @@ void on_present(IDXGISwapChain* sc, UINT flags, int hook_id, const char* method)
|
||||
{
|
||||
hook_note_call(hook_id);
|
||||
g_present_calls.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->note_present();
|
||||
}
|
||||
if (first_present_with_flags(sc, flags))
|
||||
{
|
||||
if (first_present_with_flags(sc, flags)) {
|
||||
logf("present: swapchain=%p %s flags=0x%08X%s", sc, method, flags,
|
||||
(flags & DXGI_PRESENT_TEST) ? " (DXGI_PRESENT_TEST: occlusion probe, no frame drawn)" : "");
|
||||
}
|
||||
if ((flags & DXGI_PRESENT_TEST) == 0)
|
||||
{
|
||||
if ((flags & DXGI_PRESENT_TEST) == 0) {
|
||||
capture_backbuffer(sc);
|
||||
}
|
||||
}
|
||||
@@ -779,8 +686,7 @@ void* grab_present_address(void** present1_out)
|
||||
RegisterClassExW(&wc);
|
||||
HWND hwnd = CreateWindowExW(0, wc.lpszClassName, L"", WS_OVERLAPPEDWINDOW, 0, 0, 8, 8, nullptr, nullptr,
|
||||
wc.hInstance, nullptr);
|
||||
if (hwnd == nullptr)
|
||||
{
|
||||
if (hwnd == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -801,31 +707,24 @@ void* grab_present_address(void** present1_out)
|
||||
const HRESULT hr = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0,
|
||||
D3D11_SDK_VERSION, &scd, &swapchain, &device, nullptr, &ctx);
|
||||
void* present = nullptr;
|
||||
if (SUCCEEDED(hr) && swapchain != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(hr) && swapchain != nullptr) {
|
||||
present = vtable_method(swapchain, kIdx_IDXGISwapChain_Present);
|
||||
IDXGISwapChain1* sc1 = nullptr;
|
||||
if (SUCCEEDED(swapchain->QueryInterface(__uuidof(IDXGISwapChain1), reinterpret_cast<void**>(&sc1))) &&
|
||||
sc1 != nullptr)
|
||||
{
|
||||
if (SUCCEEDED(swapchain->QueryInterface(__uuidof(IDXGISwapChain1), reinterpret_cast<void**>(&sc1)))
|
||||
&& sc1 != nullptr) {
|
||||
*present1_out = vtable_method(sc1, kIdx_IDXGISwapChain1_Present1);
|
||||
sc1->Release();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
logf("present: D3D11CreateDeviceAndSwapChain(probe) failed hr=0x%08lX", static_cast<unsigned long>(hr));
|
||||
}
|
||||
if (ctx != nullptr)
|
||||
{
|
||||
if (ctx != nullptr) {
|
||||
ctx->Release();
|
||||
}
|
||||
if (device != nullptr)
|
||||
{
|
||||
if (device != nullptr) {
|
||||
device->Release();
|
||||
}
|
||||
if (swapchain != nullptr)
|
||||
{
|
||||
if (swapchain != nullptr) {
|
||||
swapchain->Release();
|
||||
}
|
||||
DestroyWindow(hwnd);
|
||||
@@ -839,8 +738,7 @@ bool install_present_hooks(IpcClient& ipc)
|
||||
{
|
||||
g_ipc = &ipc;
|
||||
g_pid = GetCurrentProcessId();
|
||||
if (g_hk_present.enabled())
|
||||
{
|
||||
if (g_hk_present.enabled()) {
|
||||
return true; // already installed (persistent hook; the re-install path below re-enables it)
|
||||
}
|
||||
|
||||
@@ -850,15 +748,13 @@ bool install_present_hooks(IpcClient& ipc)
|
||||
|
||||
void* present1 = nullptr;
|
||||
void* present = grab_present_address(&present1);
|
||||
if (present == nullptr)
|
||||
{
|
||||
if (present == nullptr) {
|
||||
hook_set_installed(g_id_present, false);
|
||||
hook_set_installed(g_id_present1, false);
|
||||
return false;
|
||||
}
|
||||
install_inline(g_hk_present, present, &hk_Present);
|
||||
if (present1 != nullptr)
|
||||
{
|
||||
if (present1 != nullptr) {
|
||||
install_inline(g_hk_present1, present1, &hk_Present1);
|
||||
}
|
||||
g_unsupported_logged = false;
|
||||
@@ -871,15 +767,11 @@ bool install_present_hooks(IpcClient& ipc)
|
||||
// here at injection time -- d3d12.dll is already loaded in a running D3D12 game --
|
||||
// so the queue is recovered even though we attached after it was created.
|
||||
void* ecl = grab_execute_command_lists_address();
|
||||
if (ecl != nullptr)
|
||||
{
|
||||
if (ecl != nullptr) {
|
||||
install_inline(g_hk_ecl, ecl, &hk_ExecuteCommandLists);
|
||||
hook_set_installed(g_id_ecl, static_cast<bool>(g_hk_ecl));
|
||||
logf("install_present_hooks: d3d12 ExecuteCommandLists=%p hooked=%d", ecl,
|
||||
static_cast<bool>(g_hk_ecl) ? 1 : 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
logf("install_present_hooks: d3d12 ExecuteCommandLists=%p hooked=%d", ecl, static_cast<bool>(g_hk_ecl) ? 1 : 0);
|
||||
} else {
|
||||
hook_set_installed(g_id_ecl, false); // not a D3D12 game; On12 path uses its own queue
|
||||
}
|
||||
return static_cast<bool>(g_hk_present);
|
||||
|
||||
@@ -13,8 +13,7 @@
|
||||
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Installs the Present hook. Grabs IDXGISwapChain::Present from a throwaway
|
||||
// swapchain and inline-hooks it, so every swapchain in the process is caught.
|
||||
|
||||
@@ -25,8 +25,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Snap a measured rate to the nearest standard rate when within `tol` (fractional);
|
||||
// returns 0 when it doesn't land near any standard rate. The standard rates are spaced
|
||||
@@ -35,10 +34,8 @@ inline std::uint32_t snap_standard_rate(double measured, double tol = 0.02)
|
||||
{
|
||||
static constexpr std::uint32_t kStd[] = {8000, 11025, 16000, 22050, 32000, 44100,
|
||||
48000, 88200, 96000, 176400, 192000};
|
||||
for (std::uint32_t s : kStd)
|
||||
{
|
||||
if (measured >= s * (1.0 - tol) && measured <= s * (1.0 + tol))
|
||||
{
|
||||
for (std::uint32_t s : kStd) {
|
||||
if (measured >= s * (1.0 - tol) && measured <= s * (1.0 + tol)) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -46,15 +43,13 @@ inline std::uint32_t snap_standard_rate(double measured, double tol = 0.02)
|
||||
}
|
||||
|
||||
// Outcome of feeding one measurement tick.
|
||||
struct RateEstimate
|
||||
{
|
||||
struct RateEstimate {
|
||||
bool done = false; // a rate has been decided (stop feeding)
|
||||
std::uint32_t rate = 0; // the decided rate, valid when done
|
||||
bool confident = false; // true = consensus on a standard rate; false = low-confidence fallback
|
||||
};
|
||||
|
||||
class RateEstimator
|
||||
{
|
||||
class RateEstimator {
|
||||
public:
|
||||
// Window length, consensus count, and the attempt budget before giving up to a
|
||||
// low-confidence estimate. Public so a caller/test can tune them; the defaults are
|
||||
@@ -68,18 +63,15 @@ public:
|
||||
// Call repeatedly (e.g. each worker tick); returns done=false while still measuring.
|
||||
RateEstimate feed(std::uint64_t frames, std::int64_t now_qpc, std::int64_t freq)
|
||||
{
|
||||
if (freq <= 0)
|
||||
{
|
||||
if (freq <= 0) {
|
||||
return {};
|
||||
}
|
||||
if (window_qpc_ == 0)
|
||||
{
|
||||
if (window_qpc_ == 0) {
|
||||
start_window(frames, now_qpc); // begin the first window
|
||||
return {};
|
||||
}
|
||||
const std::int64_t dt = now_qpc - window_qpc_;
|
||||
if (dt < static_cast<std::int64_t>(window_seconds * static_cast<double>(freq)))
|
||||
{
|
||||
if (dt < static_cast<std::int64_t>(window_seconds * static_cast<double>(freq))) {
|
||||
return {}; // window still filling
|
||||
}
|
||||
const std::uint64_t df = frames - window_frames_;
|
||||
@@ -87,16 +79,14 @@ public:
|
||||
start_window(frames, now_qpc); // next window starts here
|
||||
const double raw = static_cast<double>(df) / secs;
|
||||
|
||||
if (raw < min_audio_rate)
|
||||
{
|
||||
if (raw < min_audio_rate) {
|
||||
// Stream went (near-)idle this window: can't trust it. Drop back to the
|
||||
// warm-up state so the next active window is discarded, not measured.
|
||||
primed_ = false;
|
||||
reset_consensus();
|
||||
return {};
|
||||
}
|
||||
if (!primed_)
|
||||
{
|
||||
if (!primed_) {
|
||||
// Discard the first full active window: a freshly-attached stream can deliver
|
||||
// its already-queued buffers in a burst, over-counting frames.
|
||||
primed_ = true;
|
||||
@@ -107,32 +97,23 @@ public:
|
||||
++attempts_;
|
||||
last_raw_ = raw;
|
||||
const std::uint32_t snapped = snap_standard_rate(raw);
|
||||
if (snapped != 0)
|
||||
{
|
||||
if (snapped == last_snapped_)
|
||||
{
|
||||
if (snapped != 0) {
|
||||
if (snapped == last_snapped_) {
|
||||
++agree_;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
last_snapped_ = snapped;
|
||||
agree_ = 1;
|
||||
}
|
||||
if (agree_ >= needed_agree)
|
||||
{
|
||||
if (agree_ >= needed_agree) {
|
||||
return {true, snapped, true}; // consensus -> confident
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
reset_consensus(); // a non-snapping window breaks the streak
|
||||
}
|
||||
|
||||
if (attempts_ >= max_attempts)
|
||||
{
|
||||
if (attempts_ >= max_attempts) {
|
||||
// Give up on consensus: a snapped value seen along the way beats a raw one.
|
||||
const std::uint32_t best =
|
||||
last_snapped_ != 0 ? last_snapped_ : static_cast<std::uint32_t>(last_raw_ + 0.5);
|
||||
const std::uint32_t best = last_snapped_ != 0 ? last_snapped_ : static_cast<std::uint32_t>(last_raw_ + 0.5);
|
||||
return {true, best, false}; // low-confidence
|
||||
}
|
||||
return {};
|
||||
|
||||
@@ -13,17 +13,12 @@
|
||||
#include "coop/shared_memory.hpp"
|
||||
#include "debug_log.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
class SharedVideoTexture
|
||||
{
|
||||
class SharedVideoTexture {
|
||||
public:
|
||||
SharedVideoTexture() = default;
|
||||
~SharedVideoTexture()
|
||||
{
|
||||
release();
|
||||
}
|
||||
~SharedVideoTexture() { release(); }
|
||||
|
||||
SharedVideoTexture(const SharedVideoTexture&) = delete;
|
||||
SharedVideoTexture& operator=(const SharedVideoTexture&) = delete;
|
||||
@@ -35,8 +30,7 @@ public:
|
||||
bool ensure(ID3D11Device* device, UINT w, UINT h, DXGI_FORMAT fmt, unsigned long pid, const char* tag,
|
||||
UINT bind = D3D11_BIND_SHADER_RESOURCE)
|
||||
{
|
||||
if (m_tex != nullptr && m_w == w && m_h == h && m_fmt == fmt)
|
||||
{
|
||||
if (m_tex != nullptr && m_w == w && m_h == h && m_fmt == fmt) {
|
||||
return true;
|
||||
}
|
||||
release();
|
||||
@@ -52,35 +46,31 @@ public:
|
||||
desc.BindFlags = bind;
|
||||
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
|
||||
HRESULT hr = device->CreateTexture2D(&desc, nullptr, &m_tex);
|
||||
if (FAILED(hr) || m_tex == nullptr)
|
||||
{
|
||||
logf("%s: CreateTexture2D(shared) failed hr=0x%08lX (%ux%u fmt=%d)", tag,
|
||||
static_cast<unsigned long>(hr), w, h, static_cast<int>(fmt));
|
||||
if (FAILED(hr) || m_tex == nullptr) {
|
||||
logf("%s: CreateTexture2D(shared) failed hr=0x%08lX (%ux%u fmt=%d)", tag, static_cast<unsigned long>(hr), w,
|
||||
h, static_cast<int>(fmt));
|
||||
release();
|
||||
return false;
|
||||
}
|
||||
|
||||
IDXGIResource1* res = nullptr;
|
||||
hr = m_tex->QueryInterface(__uuidof(IDXGIResource1), reinterpret_cast<void**>(&res));
|
||||
if (FAILED(hr) || res == nullptr)
|
||||
{
|
||||
if (FAILED(hr) || res == nullptr) {
|
||||
logf("%s: QI IDXGIResource1 failed hr=0x%08lX", tag, static_cast<unsigned long>(hr));
|
||||
release();
|
||||
return false;
|
||||
}
|
||||
const std::wstring name = video_share_name(pid);
|
||||
hr = res->CreateSharedHandle(nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
|
||||
name.c_str(), &m_handle);
|
||||
hr = res->CreateSharedHandle(nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, name.c_str(),
|
||||
&m_handle);
|
||||
res->Release();
|
||||
if (FAILED(hr) || m_handle == nullptr)
|
||||
{
|
||||
if (FAILED(hr) || m_handle == nullptr) {
|
||||
logf("%s: CreateSharedHandle failed hr=0x%08lX", tag, static_cast<unsigned long>(hr));
|
||||
release();
|
||||
return false;
|
||||
}
|
||||
hr = m_tex->QueryInterface(__uuidof(IDXGIKeyedMutex), reinterpret_cast<void**>(&m_mutex));
|
||||
if (FAILED(hr) || m_mutex == nullptr)
|
||||
{
|
||||
if (FAILED(hr) || m_mutex == nullptr) {
|
||||
logf("%s: QI IDXGIKeyedMutex failed hr=0x%08lX", tag, static_cast<unsigned long>(hr));
|
||||
release();
|
||||
return false;
|
||||
@@ -95,18 +85,15 @@ public:
|
||||
|
||||
void release()
|
||||
{
|
||||
if (m_mutex != nullptr)
|
||||
{
|
||||
if (m_mutex != nullptr) {
|
||||
m_mutex->Release();
|
||||
m_mutex = nullptr;
|
||||
}
|
||||
if (m_tex != nullptr)
|
||||
{
|
||||
if (m_tex != nullptr) {
|
||||
m_tex->Release();
|
||||
m_tex = nullptr;
|
||||
}
|
||||
if (m_handle != nullptr)
|
||||
{
|
||||
if (m_handle != nullptr) {
|
||||
CloseHandle(m_handle);
|
||||
m_handle = nullptr;
|
||||
}
|
||||
@@ -114,14 +101,8 @@ public:
|
||||
m_fmt = DXGI_FORMAT_UNKNOWN;
|
||||
}
|
||||
|
||||
[[nodiscard]] ID3D11Texture2D* texture() const
|
||||
{
|
||||
return m_tex;
|
||||
}
|
||||
[[nodiscard]] IDXGIKeyedMutex* mutex() const
|
||||
{
|
||||
return m_mutex;
|
||||
}
|
||||
[[nodiscard]] ID3D11Texture2D* texture() const { return m_tex; }
|
||||
[[nodiscard]] IDXGIKeyedMutex* mutex() const { return m_mutex; }
|
||||
|
||||
private:
|
||||
ID3D11Texture2D* m_tex = nullptr;
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
|
||||
#include "coop/protocol.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
VkCapture::~VkCapture()
|
||||
{
|
||||
@@ -27,43 +26,31 @@ bool VkCapture::find_readback_memory(std::uint32_t type_bits, std::uint32_t& out
|
||||
int best = -1;
|
||||
bool best_coherent = true;
|
||||
int best_rank = -1;
|
||||
for (std::uint32_t i = 0; i < mp.memoryTypeCount; ++i)
|
||||
{
|
||||
if ((type_bits & (1u << i)) == 0)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < mp.memoryTypeCount; ++i) {
|
||||
if ((type_bits & (1u << i)) == 0) {
|
||||
continue;
|
||||
}
|
||||
const VkMemoryPropertyFlags f = mp.memoryTypes[i].propertyFlags;
|
||||
if ((f & vis) == 0)
|
||||
{
|
||||
if ((f & vis) == 0) {
|
||||
continue;
|
||||
}
|
||||
int rank;
|
||||
if ((f & cached) && (f & coherent))
|
||||
{
|
||||
if ((f & cached) && (f & coherent)) {
|
||||
rank = 3;
|
||||
}
|
||||
else if (f & cached)
|
||||
{
|
||||
} else if (f & cached) {
|
||||
rank = 2;
|
||||
}
|
||||
else if (f & coherent)
|
||||
{
|
||||
} else if (f & coherent) {
|
||||
rank = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
continue; // host-visible but neither cached nor coherent: unusable for a CPU read-back
|
||||
}
|
||||
if (rank > best_rank)
|
||||
{
|
||||
if (rank > best_rank) {
|
||||
best_rank = rank;
|
||||
best = static_cast<int>(i);
|
||||
best_coherent = (f & coherent) != 0;
|
||||
}
|
||||
}
|
||||
if (best < 0)
|
||||
{
|
||||
if (best < 0) {
|
||||
return false;
|
||||
}
|
||||
out_index = static_cast<std::uint32_t>(best);
|
||||
@@ -73,33 +60,28 @@ bool VkCapture::find_readback_memory(std::uint32_t type_bits, std::uint32_t& out
|
||||
|
||||
bool VkCapture::ensure_slot_pool()
|
||||
{
|
||||
if (m_pool != VK_NULL_HANDLE)
|
||||
{
|
||||
if (m_pool != VK_NULL_HANDLE) {
|
||||
return true;
|
||||
}
|
||||
if (m_queue == VK_NULL_HANDLE)
|
||||
{
|
||||
if (m_queue == VK_NULL_HANDLE) {
|
||||
m_fns.GetDeviceQueue(m_device, m_qfam, 0, &m_queue);
|
||||
}
|
||||
VkCommandPoolCreateInfo pci{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
|
||||
pci.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||
pci.queueFamilyIndex = m_qfam;
|
||||
if (m_fns.CreateCommandPool(m_device, &pci, nullptr, &m_pool) != VK_SUCCESS)
|
||||
{
|
||||
if (m_fns.CreateCommandPool(m_device, &pci, nullptr, &m_pool) != VK_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
for (Slot& s : m_slots)
|
||||
{
|
||||
for (Slot& s : m_slots) {
|
||||
VkCommandBufferAllocateInfo ai{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
|
||||
ai.commandPool = m_pool;
|
||||
ai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
ai.commandBufferCount = 1;
|
||||
VkFenceCreateInfo fi{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
|
||||
VkSemaphoreCreateInfo si{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
|
||||
if (m_fns.AllocateCommandBuffers(m_device, &ai, &s.cmd) != VK_SUCCESS ||
|
||||
m_fns.CreateFence(m_device, &fi, nullptr, &s.fence) != VK_SUCCESS ||
|
||||
m_fns.CreateSemaphore(m_device, &si, nullptr, &s.present_sem) != VK_SUCCESS)
|
||||
{
|
||||
if (m_fns.AllocateCommandBuffers(m_device, &ai, &s.cmd) != VK_SUCCESS
|
||||
|| m_fns.CreateFence(m_device, &fi, nullptr, &s.fence) != VK_SUCCESS
|
||||
|| m_fns.CreateSemaphore(m_device, &si, nullptr, &s.present_sem) != VK_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -109,22 +91,18 @@ bool VkCapture::ensure_slot_pool()
|
||||
bool VkCapture::ensure_staging(Slot& s, std::uint32_t w, std::uint32_t h)
|
||||
{
|
||||
const VkDeviceSize need = static_cast<VkDeviceSize>(w) * h * 4;
|
||||
if (s.staging != VK_NULL_HANDLE && s.size == need)
|
||||
{
|
||||
if (s.staging != VK_NULL_HANDLE && s.size == need) {
|
||||
return true;
|
||||
}
|
||||
if (s.mapped != nullptr)
|
||||
{
|
||||
if (s.mapped != nullptr) {
|
||||
m_fns.UnmapMemory(m_device, s.mem);
|
||||
s.mapped = nullptr;
|
||||
}
|
||||
if (s.staging != VK_NULL_HANDLE)
|
||||
{
|
||||
if (s.staging != VK_NULL_HANDLE) {
|
||||
m_fns.DestroyBuffer(m_device, s.staging, nullptr);
|
||||
s.staging = VK_NULL_HANDLE;
|
||||
}
|
||||
if (s.mem != VK_NULL_HANDLE)
|
||||
{
|
||||
if (s.mem != VK_NULL_HANDLE) {
|
||||
m_fns.FreeMemory(m_device, s.mem, nullptr);
|
||||
s.mem = VK_NULL_HANDLE;
|
||||
}
|
||||
@@ -133,16 +111,14 @@ bool VkCapture::ensure_staging(Slot& s, std::uint32_t w, std::uint32_t h)
|
||||
bci.size = need;
|
||||
bci.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
bci.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
if (m_fns.CreateBuffer(m_device, &bci, nullptr, &s.staging) != VK_SUCCESS)
|
||||
{
|
||||
if (m_fns.CreateBuffer(m_device, &bci, nullptr, &s.staging) != VK_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
VkMemoryRequirements mr{};
|
||||
m_fns.GetBufferMemoryRequirements(m_device, s.staging, &mr);
|
||||
std::uint32_t mt = 0;
|
||||
bool coherent = true;
|
||||
if (!find_readback_memory(mr.memoryTypeBits, mt, coherent))
|
||||
{
|
||||
if (!find_readback_memory(mr.memoryTypeBits, mt, coherent)) {
|
||||
m_fns.DestroyBuffer(m_device, s.staging, nullptr);
|
||||
s.staging = VK_NULL_HANDLE;
|
||||
return false;
|
||||
@@ -150,12 +126,10 @@ bool VkCapture::ensure_staging(Slot& s, std::uint32_t w, std::uint32_t h)
|
||||
VkMemoryAllocateInfo mai{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
||||
mai.allocationSize = mr.size;
|
||||
mai.memoryTypeIndex = mt;
|
||||
if (m_fns.AllocateMemory(m_device, &mai, nullptr, &s.mem) != VK_SUCCESS ||
|
||||
m_fns.BindBufferMemory(m_device, s.staging, s.mem, 0) != VK_SUCCESS ||
|
||||
m_fns.MapMemory(m_device, s.mem, 0, VK_WHOLE_SIZE, 0, &s.mapped) != VK_SUCCESS)
|
||||
{
|
||||
if (s.mem != VK_NULL_HANDLE)
|
||||
{
|
||||
if (m_fns.AllocateMemory(m_device, &mai, nullptr, &s.mem) != VK_SUCCESS
|
||||
|| m_fns.BindBufferMemory(m_device, s.staging, s.mem, 0) != VK_SUCCESS
|
||||
|| m_fns.MapMemory(m_device, s.mem, 0, VK_WHOLE_SIZE, 0, &s.mapped) != VK_SUCCESS) {
|
||||
if (s.mem != VK_NULL_HANDLE) {
|
||||
m_fns.FreeMemory(m_device, s.mem, nullptr);
|
||||
s.mem = VK_NULL_HANDLE;
|
||||
}
|
||||
@@ -170,38 +144,31 @@ bool VkCapture::ensure_staging(Slot& s, std::uint32_t w, std::uint32_t h)
|
||||
|
||||
void VkCapture::free_slots()
|
||||
{
|
||||
for (Slot& s : m_slots)
|
||||
{
|
||||
if (s.mapped != nullptr)
|
||||
{
|
||||
for (Slot& s : m_slots) {
|
||||
if (s.mapped != nullptr) {
|
||||
m_fns.UnmapMemory(m_device, s.mem);
|
||||
s.mapped = nullptr;
|
||||
}
|
||||
if (s.staging != VK_NULL_HANDLE)
|
||||
{
|
||||
if (s.staging != VK_NULL_HANDLE) {
|
||||
m_fns.DestroyBuffer(m_device, s.staging, nullptr);
|
||||
s.staging = VK_NULL_HANDLE;
|
||||
}
|
||||
if (s.mem != VK_NULL_HANDLE)
|
||||
{
|
||||
if (s.mem != VK_NULL_HANDLE) {
|
||||
m_fns.FreeMemory(m_device, s.mem, nullptr);
|
||||
s.mem = VK_NULL_HANDLE;
|
||||
}
|
||||
if (s.present_sem != VK_NULL_HANDLE)
|
||||
{
|
||||
if (s.present_sem != VK_NULL_HANDLE) {
|
||||
m_fns.DestroySemaphore(m_device, s.present_sem, nullptr);
|
||||
s.present_sem = VK_NULL_HANDLE;
|
||||
}
|
||||
if (s.fence != VK_NULL_HANDLE)
|
||||
{
|
||||
if (s.fence != VK_NULL_HANDLE) {
|
||||
m_fns.DestroyFence(m_device, s.fence, nullptr);
|
||||
s.fence = VK_NULL_HANDLE;
|
||||
}
|
||||
s.size = 0;
|
||||
s.busy.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
if (m_pool != VK_NULL_HANDLE)
|
||||
{
|
||||
if (m_pool != VK_NULL_HANDLE) {
|
||||
m_fns.DestroyCommandPool(m_device, m_pool, nullptr); // frees the command buffers
|
||||
m_pool = VK_NULL_HANDLE;
|
||||
}
|
||||
@@ -210,25 +177,22 @@ void VkCapture::free_slots()
|
||||
// --- D3D11 shared texture (reaper thread only; shutdown releases after the reaper has joined) ------
|
||||
bool VkCapture::ensure_d3d()
|
||||
{
|
||||
if (m_d3d != nullptr)
|
||||
{
|
||||
if (m_d3d != nullptr) {
|
||||
return true;
|
||||
}
|
||||
return SUCCEEDED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0,
|
||||
D3D11_SDK_VERSION, &m_d3d, nullptr, &m_d3d_ctx)) &&
|
||||
m_d3d != nullptr;
|
||||
return SUCCEEDED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0, D3D11_SDK_VERSION,
|
||||
&m_d3d, nullptr, &m_d3d_ctx))
|
||||
&& m_d3d != nullptr;
|
||||
}
|
||||
|
||||
void VkCapture::release_d3d()
|
||||
{
|
||||
m_shared.release();
|
||||
if (m_d3d_ctx != nullptr)
|
||||
{
|
||||
if (m_d3d_ctx != nullptr) {
|
||||
m_d3d_ctx->Release();
|
||||
m_d3d_ctx = nullptr;
|
||||
}
|
||||
if (m_d3d != nullptr)
|
||||
{
|
||||
if (m_d3d != nullptr) {
|
||||
m_d3d->Release();
|
||||
m_d3d = nullptr;
|
||||
}
|
||||
@@ -238,8 +202,7 @@ void VkCapture::release_d3d()
|
||||
void VkCapture::init(VkPhysicalDevice phys, VkDevice device, std::uint32_t queue_family, const Fns& fns,
|
||||
unsigned long pid, std::function<void(std::uint32_t, std::uint32_t)> on_frame)
|
||||
{
|
||||
if (m_device != VK_NULL_HANDLE)
|
||||
{
|
||||
if (m_device != VK_NULL_HANDLE) {
|
||||
return; // already initialised
|
||||
}
|
||||
m_phys = phys;
|
||||
@@ -249,8 +212,7 @@ void VkCapture::init(VkPhysicalDevice phys, VkDevice device, std::uint32_t queue
|
||||
m_pid = pid;
|
||||
m_on_frame = std::move(on_frame);
|
||||
m_stop = false;
|
||||
if (!ensure_slot_pool())
|
||||
{
|
||||
if (!ensure_slot_pool()) {
|
||||
return; // leave m_device set but the pool empty -> present() will fail format/staging checks
|
||||
}
|
||||
m_reaper = std::thread([this] { reaper_main(); });
|
||||
@@ -261,8 +223,7 @@ bool VkCapture::present(VkImage image, VkFormat fmt, std::uint32_t w, std::uint3
|
||||
{
|
||||
const bool bgra = fmt == VK_FORMAT_B8G8R8A8_UNORM || fmt == VK_FORMAT_B8G8R8A8_SRGB;
|
||||
const bool rgba = fmt == VK_FORMAT_R8G8B8A8_UNORM || fmt == VK_FORMAT_R8G8B8A8_SRGB;
|
||||
if (m_device == VK_NULL_HANDLE || m_pool == VK_NULL_HANDLE || (!bgra && !rgba))
|
||||
{
|
||||
if (m_device == VK_NULL_HANDLE || m_pool == VK_NULL_HANDLE || (!bgra && !rgba)) {
|
||||
return false;
|
||||
}
|
||||
// No time-based throttle here: capture follows the game's present rate, which vsync paces (if the
|
||||
@@ -272,23 +233,19 @@ bool VkCapture::present(VkImage image, VkFormat fmt, std::uint32_t w, std::uint3
|
||||
// Pick a slot whose previous capture the reaper has finished. None free -> the reaper is behind,
|
||||
// so skip this frame (the game keeps its rate; the mirror just drops a frame).
|
||||
int idx = -1;
|
||||
for (int n = 0; n < kSlots; ++n)
|
||||
{
|
||||
for (int n = 0; n < kSlots; ++n) {
|
||||
const int cand = (m_next + n) % kSlots;
|
||||
if (!m_slots[cand].busy.load(std::memory_order_acquire))
|
||||
{
|
||||
if (!m_slots[cand].busy.load(std::memory_order_acquire)) {
|
||||
idx = cand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (idx < 0)
|
||||
{
|
||||
if (idx < 0) {
|
||||
return false;
|
||||
}
|
||||
m_next = (idx + 1) % kSlots;
|
||||
Slot& s = m_slots[idx];
|
||||
if (!ensure_staging(s, w, h))
|
||||
{
|
||||
if (!ensure_staging(s, w, h)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -306,17 +263,17 @@ bool VkCapture::present(VkImage image, VkFormat fmt, std::uint32_t w, std::uint3
|
||||
b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
b.image = image;
|
||||
b.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
m_fns.CmdPipelineBarrier(s.cmd, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
||||
0, 0, nullptr, 0, nullptr, 1, &b);
|
||||
m_fns.CmdPipelineBarrier(s.cmd, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0,
|
||||
nullptr, 0, nullptr, 1, &b);
|
||||
};
|
||||
image_barrier(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
||||
VK_ACCESS_MEMORY_READ_BIT, VK_ACCESS_TRANSFER_READ_BIT);
|
||||
image_barrier(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_READ_BIT,
|
||||
VK_ACCESS_TRANSFER_READ_BIT);
|
||||
VkBufferImageCopy region{};
|
||||
region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
region.imageExtent = {w, h, 1};
|
||||
m_fns.CmdCopyImageToBuffer(s.cmd, image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, s.staging, 1, ®ion);
|
||||
image_barrier(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
||||
VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_MEMORY_READ_BIT);
|
||||
image_barrier(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_ACCESS_TRANSFER_READ_BIT,
|
||||
VK_ACCESS_MEMORY_READ_BIT);
|
||||
m_fns.EndCommandBuffer(s.cmd);
|
||||
|
||||
std::vector<VkPipelineStageFlags> stages(wait_count, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
|
||||
@@ -328,8 +285,7 @@ bool VkCapture::present(VkImage image, VkFormat fmt, std::uint32_t w, std::uint3
|
||||
si.pCommandBuffers = &s.cmd;
|
||||
si.signalSemaphoreCount = 1;
|
||||
si.pSignalSemaphores = &s.present_sem;
|
||||
if (m_fns.QueueSubmit(m_queue, 1, &si, s.fence) != VK_SUCCESS)
|
||||
{
|
||||
if (m_fns.QueueSubmit(m_queue, 1, &si, s.fence) != VK_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
s.w = w;
|
||||
@@ -348,8 +304,7 @@ bool VkCapture::present(VkImage image, VkFormat fmt, std::uint32_t w, std::uint3
|
||||
void VkCapture::reap_slot(Slot& s)
|
||||
{
|
||||
m_fns.WaitForFences(m_device, 1, &s.fence, VK_TRUE, UINT64_MAX);
|
||||
if (!s.coherent)
|
||||
{
|
||||
if (!s.coherent) {
|
||||
VkMappedMemoryRange r{VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE};
|
||||
r.memory = s.mem;
|
||||
r.offset = 0;
|
||||
@@ -359,43 +314,35 @@ void VkCapture::reap_slot(Slot& s)
|
||||
|
||||
const bool bgra = s.fmt == VK_FORMAT_B8G8R8A8_UNORM || s.fmt == VK_FORMAT_B8G8R8A8_SRGB;
|
||||
const size_t row = static_cast<size_t>(s.w) * 4;
|
||||
if (m_rgba.size() != row * s.h)
|
||||
{
|
||||
if (m_rgba.size() != row * s.h) {
|
||||
m_rgba.resize(row * s.h);
|
||||
}
|
||||
const auto* src = static_cast<const unsigned char*>(s.mapped);
|
||||
for (std::uint32_t y = 0; y < s.h; ++y)
|
||||
{
|
||||
for (std::uint32_t y = 0; y < s.h; ++y) {
|
||||
const unsigned char* in = src + static_cast<size_t>(y) * row;
|
||||
unsigned char* o = m_rgba.data() + static_cast<size_t>(y) * row;
|
||||
if (bgra)
|
||||
{
|
||||
for (std::uint32_t x = 0; x < s.w; ++x)
|
||||
{
|
||||
if (bgra) {
|
||||
for (std::uint32_t x = 0; x < s.w; ++x) {
|
||||
o[x * 4 + 0] = in[x * 4 + 2];
|
||||
o[x * 4 + 1] = in[x * 4 + 1];
|
||||
o[x * 4 + 2] = in[x * 4 + 0];
|
||||
o[x * 4 + 3] = 255;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::memcpy(o, in, row);
|
||||
}
|
||||
}
|
||||
|
||||
bool published = false;
|
||||
if (ensure_d3d() && m_shared.ensure(m_d3d, s.w, s.h, DXGI_FORMAT_R8G8B8A8_UNORM, m_pid, "vk") &&
|
||||
m_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK)
|
||||
{
|
||||
if (ensure_d3d() && m_shared.ensure(m_d3d, s.w, s.h, DXGI_FORMAT_R8G8B8A8_UNORM, m_pid, "vk")
|
||||
&& m_shared.mutex()->AcquireSync(kVideoMutexKey, 8) == S_OK) {
|
||||
m_d3d_ctx->UpdateSubresource(m_shared.texture(), 0, nullptr, m_rgba.data(), static_cast<UINT>(row), 0);
|
||||
m_d3d_ctx->Flush();
|
||||
m_shared.mutex()->ReleaseSync(kVideoMutexKey);
|
||||
published = true;
|
||||
}
|
||||
|
||||
if (published)
|
||||
{
|
||||
if (published) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_last_mutex);
|
||||
m_last = m_rgba;
|
||||
@@ -403,8 +350,7 @@ void VkCapture::reap_slot(Slot& s)
|
||||
m_last_h = s.h;
|
||||
}
|
||||
m_published.fetch_add(1, std::memory_order_relaxed);
|
||||
if (m_on_frame)
|
||||
{
|
||||
if (m_on_frame) {
|
||||
m_on_frame(s.w, s.h);
|
||||
}
|
||||
}
|
||||
@@ -415,14 +361,12 @@ void VkCapture::reap_slot(Slot& s)
|
||||
|
||||
void VkCapture::reaper_main()
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
int idx;
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(m_q_mutex);
|
||||
m_q_cv.wait(lk, [this] { return m_stop || !m_pending.empty(); });
|
||||
if (m_stop && m_pending.empty())
|
||||
{
|
||||
if (m_stop && m_pending.empty()) {
|
||||
return;
|
||||
}
|
||||
idx = m_pending.front();
|
||||
@@ -434,8 +378,7 @@ void VkCapture::reaper_main()
|
||||
|
||||
void VkCapture::shutdown()
|
||||
{
|
||||
if (m_reaper.joinable())
|
||||
{
|
||||
if (m_reaper.joinable()) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_q_mutex);
|
||||
m_stop = true;
|
||||
@@ -445,10 +388,8 @@ void VkCapture::shutdown()
|
||||
}
|
||||
// The reaper is gone (no more submits/reads); drain any GPU work still referencing our
|
||||
// resources, then free.
|
||||
if (m_device != VK_NULL_HANDLE)
|
||||
{
|
||||
if (m_fns.DeviceWaitIdle != nullptr)
|
||||
{
|
||||
if (m_device != VK_NULL_HANDLE) {
|
||||
if (m_fns.DeviceWaitIdle != nullptr) {
|
||||
m_fns.DeviceWaitIdle(m_device);
|
||||
}
|
||||
free_slots();
|
||||
@@ -463,8 +404,7 @@ void VkCapture::shutdown()
|
||||
bool VkCapture::last_frame(std::vector<unsigned char>& out, std::uint32_t& w, std::uint32_t& h)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_last_mutex);
|
||||
if (m_last.empty())
|
||||
{
|
||||
if (m_last.empty()) {
|
||||
return false;
|
||||
}
|
||||
out = m_last;
|
||||
|
||||
@@ -36,16 +36,13 @@
|
||||
|
||||
#include "shared_video_texture.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
class VkCapture
|
||||
{
|
||||
class VkCapture {
|
||||
public:
|
||||
// Device entry points the read-back needs (resolved by the caller via the real
|
||||
// vkGetDeviceProcAddr; GetPhysicalDeviceMemoryProperties is instance-level).
|
||||
struct Fns
|
||||
{
|
||||
struct Fns {
|
||||
PFN_vkGetDeviceQueue GetDeviceQueue;
|
||||
PFN_vkCreateCommandPool CreateCommandPool;
|
||||
PFN_vkDestroyCommandPool DestroyCommandPool;
|
||||
@@ -84,8 +81,8 @@ public:
|
||||
// Bind to the game's device + queue family and start the reaper thread. `pid` names the shared
|
||||
// texture (video_share_name). `on_frame(w,h)` runs on the reaper thread after each frame is
|
||||
// published (the caller does its own IPC / stat bookkeeping there). Idempotent-ish: call once.
|
||||
void init(VkPhysicalDevice phys, VkDevice device, std::uint32_t queue_family, const Fns& fns,
|
||||
unsigned long pid, std::function<void(std::uint32_t, std::uint32_t)> on_frame);
|
||||
void init(VkPhysicalDevice phys, VkDevice device, std::uint32_t queue_family, const Fns& fns, unsigned long pid,
|
||||
std::function<void(std::uint32_t, std::uint32_t)> on_frame);
|
||||
|
||||
bool active() const { return m_device != VK_NULL_HANDLE; }
|
||||
|
||||
@@ -110,8 +107,7 @@ public:
|
||||
private:
|
||||
static constexpr int kSlots = 4; // in-flight copies; also the present-semaphore reuse slack
|
||||
|
||||
struct Slot
|
||||
{
|
||||
struct Slot {
|
||||
VkCommandBuffer cmd = VK_NULL_HANDLE;
|
||||
VkFence fence = VK_NULL_HANDLE;
|
||||
VkSemaphore present_sem = VK_NULL_HANDLE;
|
||||
|
||||
@@ -24,11 +24,9 @@
|
||||
#include "hook_registry.hpp"
|
||||
#include "vk_capture.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight present/create detours before remove frees the Vulkan state
|
||||
// Capture gate. Unlike the other backends, the game caches our hk_vkQueuePresentKHR pointer at
|
||||
@@ -65,8 +63,7 @@ std::uint32_t g_qfam = 0;
|
||||
VkCapture g_cap; // the shared, off-present-thread read-back (same component the layer uses)
|
||||
|
||||
// Tracked swap chains (small; engines have one or two).
|
||||
struct SwapInfo
|
||||
{
|
||||
struct SwapInfo {
|
||||
VkSwapchainKHR sc;
|
||||
VkFormat fmt;
|
||||
std::uint32_t w;
|
||||
@@ -95,10 +92,8 @@ PFN_vkVoidFunction real_gipa(VkInstance inst, const char* name)
|
||||
// (copy out what you need before unlocking, since another thread can push_back and reallocate).
|
||||
const SwapInfo* find_swap(VkSwapchainKHR sc)
|
||||
{
|
||||
for (const SwapInfo& s : g_swaps)
|
||||
{
|
||||
if (s.sc == sc)
|
||||
{
|
||||
for (const SwapInfo& s : g_swaps) {
|
||||
if (s.sc == sc) {
|
||||
return &s;
|
||||
}
|
||||
}
|
||||
@@ -110,17 +105,15 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkQueuePresentKHR(VkQueue queue, const VkPrese
|
||||
DetourGate::Guard guard(g_gate); // keep the read-back resources alive for this whole detour
|
||||
hook_note_call(g_id_present);
|
||||
g_presents.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->note_present();
|
||||
}
|
||||
|
||||
// Capture only the simple, common single-swapchain present; pass anything else through. The
|
||||
// gate lets removal stop capture (and pass through to the real present) before it frees the
|
||||
// read-back state, even though the game keeps calling this cached detour pointer.
|
||||
if (g_capture_enabled.load(std::memory_order_acquire) && g_device != VK_NULL_HANDLE &&
|
||||
pPresentInfo != nullptr && pPresentInfo->swapchainCount == 1)
|
||||
{
|
||||
if (g_capture_enabled.load(std::memory_order_acquire) && g_device != VK_NULL_HANDLE && pPresentInfo != nullptr
|
||||
&& pPresentInfo->swapchainCount == 1) {
|
||||
// Copy the matched swapchain's fields out under the lock, then capture without holding it (so
|
||||
// the GPU submit can't block a concurrent create, and the SwapInfo* can't dangle on a realloc).
|
||||
VkImage image = VK_NULL_HANDLE;
|
||||
@@ -131,8 +124,7 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkQueuePresentKHR(VkQueue queue, const VkPrese
|
||||
std::scoped_lock lock(g_swaps_mutex);
|
||||
const SwapInfo* s = find_swap(pPresentInfo->pSwapchains[0]);
|
||||
const std::uint32_t idx = pPresentInfo->pImageIndices[0];
|
||||
if (s != nullptr && idx < s->images.size())
|
||||
{
|
||||
if (s != nullptr && idx < s->images.size()) {
|
||||
image = s->images[idx];
|
||||
fmt = s->fmt;
|
||||
w = s->w;
|
||||
@@ -140,12 +132,10 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkQueuePresentKHR(VkQueue queue, const VkPrese
|
||||
matched = true;
|
||||
}
|
||||
}
|
||||
if (matched)
|
||||
{
|
||||
if (matched) {
|
||||
VkSemaphore chained = VK_NULL_HANDLE;
|
||||
if (g_cap.present(image, fmt, w, h, pPresentInfo->pWaitSemaphores,
|
||||
pPresentInfo->waitSemaphoreCount, chained))
|
||||
{
|
||||
if (g_cap.present(image, fmt, w, h, pPresentInfo->pWaitSemaphores, pPresentInfo->waitSemaphoreCount,
|
||||
chained)) {
|
||||
// Replace the present's wait with our chained semaphore (our submit consumed the
|
||||
// originals and signals this one), so the present still orders after rendering.
|
||||
VkPresentInfoKHR pi = *pPresentInfo;
|
||||
@@ -163,8 +153,7 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkCreateSwapchainKHR(VkDevice device, const Vk
|
||||
{
|
||||
DetourGate::Guard guard(g_gate); // keep g_swaps stable while remove may be clearing it
|
||||
const VkResult r = g_real_create_swapchain(device, ci, alloc, out);
|
||||
if (r == VK_SUCCESS && out != nullptr && g_get_swapchain_images != nullptr)
|
||||
{
|
||||
if (r == VK_SUCCESS && out != nullptr && g_get_swapchain_images != nullptr) {
|
||||
SwapInfo info{};
|
||||
info.sc = *out;
|
||||
info.fmt = ci->imageFormat;
|
||||
@@ -178,12 +167,11 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkCreateSwapchainKHR(VkDevice device, const Vk
|
||||
std::scoped_lock lock(g_swaps_mutex);
|
||||
// De-dup a recycled handle value, then bound growth (drop the oldest; the just-created
|
||||
// active swapchain is newest and stays).
|
||||
g_swaps.erase(std::remove_if(g_swaps.begin(), g_swaps.end(),
|
||||
[&](const SwapInfo& e) { return e.sc == info.sc; }),
|
||||
g_swaps.erase(
|
||||
std::remove_if(g_swaps.begin(), g_swaps.end(), [&](const SwapInfo& e) { return e.sc == info.sc; }),
|
||||
g_swaps.end());
|
||||
g_swaps.push_back(std::move(info));
|
||||
if (g_swaps.size() > kMaxTrackedSwaps)
|
||||
{
|
||||
if (g_swaps.size() > kMaxTrackedSwaps) {
|
||||
g_swaps.erase(g_swaps.begin());
|
||||
}
|
||||
}
|
||||
@@ -236,8 +224,7 @@ void start_capture(VkDevice device)
|
||||
// Reaper thread, after each frame is mirrored into the shared texture.
|
||||
g_present_captured.store(true, std::memory_order_relaxed);
|
||||
g_frames_shared.fetch_add(1, std::memory_order_relaxed);
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->publish_video_frame(w, h, static_cast<std::uint32_t>(DXGI_FORMAT_R8G8B8A8_UNORM));
|
||||
}
|
||||
});
|
||||
@@ -254,8 +241,7 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkCreateDevice(VkPhysicalDevice phys, const Vk
|
||||
g_device = *out;
|
||||
g_qfam = ci->queueCreateInfoCount > 0 ? ci->pQueueCreateInfos[0].queueFamilyIndex : 0;
|
||||
g_real_gdpa = reinterpret_cast<PFN_vkGetDeviceProcAddr>(real_gipa(g_instance, "vkGetDeviceProcAddr"));
|
||||
g_real_create_swapchain =
|
||||
reinterpret_cast<PFN_vkCreateSwapchainKHR>(g_real_gdpa(*out, "vkCreateSwapchainKHR"));
|
||||
g_real_create_swapchain = reinterpret_cast<PFN_vkCreateSwapchainKHR>(g_real_gdpa(*out, "vkCreateSwapchainKHR"));
|
||||
g_real_present = reinterpret_cast<PFN_vkQueuePresentKHR>(g_real_gdpa(*out, "vkQueuePresentKHR"));
|
||||
start_capture(*out);
|
||||
// Arm capture only once every real_* pointer + VkCapture is populated (release pairs with the
|
||||
@@ -266,13 +252,12 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkCreateDevice(VkPhysicalDevice phys, const Vk
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL hk_vkCreateInstance(const VkInstanceCreateInfo* ci,
|
||||
const VkAllocationCallbacks* alloc, VkInstance* out)
|
||||
VKAPI_ATTR VkResult VKAPI_CALL hk_vkCreateInstance(const VkInstanceCreateInfo* ci, const VkAllocationCallbacks* alloc,
|
||||
VkInstance* out)
|
||||
{
|
||||
auto real_create = reinterpret_cast<PFN_vkCreateInstance>(real_gipa(nullptr, "vkCreateInstance"));
|
||||
const VkResult r = real_create(ci, alloc, out);
|
||||
if (r == VK_SUCCESS && out != nullptr)
|
||||
{
|
||||
if (r == VK_SUCCESS && out != nullptr) {
|
||||
g_instance = *out;
|
||||
g_real_create_device = reinterpret_cast<PFN_vkCreateDevice>(real_gipa(*out, "vkCreateDevice"));
|
||||
logf("vk: instance created -- intercepting device/swapchain/present");
|
||||
@@ -282,14 +267,11 @@ VKAPI_ATTR VkResult VKAPI_CALL hk_vkCreateInstance(const VkInstanceCreateInfo* c
|
||||
|
||||
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL hk_vkGetDeviceProcAddr(VkDevice device, const char* name)
|
||||
{
|
||||
if (name != nullptr)
|
||||
{
|
||||
if (std::strcmp(name, "vkQueuePresentKHR") == 0)
|
||||
{
|
||||
if (name != nullptr) {
|
||||
if (std::strcmp(name, "vkQueuePresentKHR") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkQueuePresentKHR);
|
||||
}
|
||||
if (std::strcmp(name, "vkCreateSwapchainKHR") == 0)
|
||||
{
|
||||
if (std::strcmp(name, "vkCreateSwapchainKHR") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkCreateSwapchainKHR);
|
||||
}
|
||||
}
|
||||
@@ -298,22 +280,17 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL hk_vkGetDeviceProcAddr(VkDevice device,
|
||||
|
||||
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL hk_vkGetInstanceProcAddr(VkInstance instance, const char* name)
|
||||
{
|
||||
if (name != nullptr)
|
||||
{
|
||||
if (std::strcmp(name, "vkGetInstanceProcAddr") == 0)
|
||||
{
|
||||
if (name != nullptr) {
|
||||
if (std::strcmp(name, "vkGetInstanceProcAddr") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkGetInstanceProcAddr);
|
||||
}
|
||||
if (std::strcmp(name, "vkCreateInstance") == 0)
|
||||
{
|
||||
if (std::strcmp(name, "vkCreateInstance") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkCreateInstance);
|
||||
}
|
||||
if (std::strcmp(name, "vkCreateDevice") == 0)
|
||||
{
|
||||
if (std::strcmp(name, "vkCreateDevice") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkCreateDevice);
|
||||
}
|
||||
if (std::strcmp(name, "vkGetDeviceProcAddr") == 0)
|
||||
{
|
||||
if (std::strcmp(name, "vkGetDeviceProcAddr") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkGetDeviceProcAddr);
|
||||
}
|
||||
// vkGetInstanceProcAddr can also resolve device-level functions (the loader returns a
|
||||
@@ -321,12 +298,10 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL hk_vkGetInstanceProcAddr(VkInstance ins
|
||||
// swapchain entry points this way (rather than via vkGetDeviceProcAddr) would otherwise get
|
||||
// the real loader pointer and bypass our capture, so intercept them here too. (Our detours
|
||||
// gate on g_capture_enabled / g_device, so handing them out before the device exists is safe.)
|
||||
if (std::strcmp(name, "vkQueuePresentKHR") == 0)
|
||||
{
|
||||
if (std::strcmp(name, "vkQueuePresentKHR") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkQueuePresentKHR);
|
||||
}
|
||||
if (std::strcmp(name, "vkCreateSwapchainKHR") == 0)
|
||||
{
|
||||
if (std::strcmp(name, "vkCreateSwapchainKHR") == 0) {
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(&hk_vkCreateSwapchainKHR);
|
||||
}
|
||||
}
|
||||
@@ -336,8 +311,7 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL hk_vkGetInstanceProcAddr(VkInstance ins
|
||||
void* gipa_export_address()
|
||||
{
|
||||
HMODULE vk = GetModuleHandleW(L"vulkan-1.dll");
|
||||
if (vk == nullptr)
|
||||
{
|
||||
if (vk == nullptr) {
|
||||
return nullptr; // not a Vulkan process (yet)
|
||||
}
|
||||
return reinterpret_cast<void*>(GetProcAddress(vk, "vkGetInstanceProcAddr"));
|
||||
@@ -349,17 +323,14 @@ bool install_vk_hooks(IpcClient& ipc)
|
||||
{
|
||||
g_ipc = &ipc;
|
||||
g_pid = GetCurrentProcessId();
|
||||
if (g_hk_gipa.enabled())
|
||||
{
|
||||
if (g_hk_gipa.enabled()) {
|
||||
return true; // already installed (persistent hook; re-install below re-enables it)
|
||||
}
|
||||
if (g_id_present < 0)
|
||||
{
|
||||
if (g_id_present < 0) {
|
||||
g_id_present = hook_register("vkQueuePresentKHR", HookSubsys_Video);
|
||||
}
|
||||
void* gipa = gipa_export_address();
|
||||
if (gipa == nullptr)
|
||||
{
|
||||
if (gipa == nullptr) {
|
||||
hook_set_installed(g_id_present, false);
|
||||
return false; // vulkan-1.dll not loaded; caller can retry once the game loads it
|
||||
}
|
||||
@@ -420,8 +391,7 @@ bool vk_injected_too_late()
|
||||
// hook (we're not in the chain). A game we hooked early always trips hk_vkCreateDevice
|
||||
// (g_device != null) well within the grace window, even before it presents. The host shows
|
||||
// the "relaunch with Auto-attach / Vulkan layer" banner on this.
|
||||
if (GetModuleHandleW(L"vulkan-1.dll") == nullptr || g_device != VK_NULL_HANDLE || g_install_tick == 0)
|
||||
{
|
||||
if (GetModuleHandleW(L"vulkan-1.dll") == nullptr || g_device != VK_NULL_HANDLE || g_install_tick == 0) {
|
||||
return false;
|
||||
}
|
||||
return (GetTickCount64() - g_install_tick) > 4000;
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Installs the Vulkan capture hook (inline-hooks vkGetInstanceProcAddr). `ipc` must outlive the
|
||||
// hook. Returns true if vulkan-1.dll is loaded and the export was hooked; false otherwise, so
|
||||
|
||||
@@ -11,8 +11,7 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Read a COM object's vtable slot (e.g. to grab a method's address off a probe object for an
|
||||
// inline hook).
|
||||
@@ -21,19 +20,16 @@ inline void* vtable_method(void* obj, unsigned index)
|
||||
return (*reinterpret_cast<void***>(obj))[index];
|
||||
}
|
||||
|
||||
class VtableHook
|
||||
{
|
||||
class VtableHook {
|
||||
public:
|
||||
bool install(void* com_object, unsigned index, void* detour)
|
||||
{
|
||||
if (m_vtable != nullptr)
|
||||
{
|
||||
if (m_vtable != nullptr) {
|
||||
return true; // already installed (shared vtable covers every instance)
|
||||
}
|
||||
auto** vtable = *reinterpret_cast<void***>(com_object);
|
||||
DWORD old_protect = 0;
|
||||
if (!VirtualProtect(&vtable[index], sizeof(void*), PAGE_READWRITE, &old_protect))
|
||||
{
|
||||
if (!VirtualProtect(&vtable[index], sizeof(void*), PAGE_READWRITE, &old_protect)) {
|
||||
return false;
|
||||
}
|
||||
m_original = vtable[index];
|
||||
@@ -46,13 +42,11 @@ public:
|
||||
|
||||
void remove()
|
||||
{
|
||||
if (m_vtable == nullptr)
|
||||
{
|
||||
if (m_vtable == nullptr) {
|
||||
return;
|
||||
}
|
||||
DWORD old_protect = 0;
|
||||
if (VirtualProtect(&m_vtable[m_index], sizeof(void*), PAGE_READWRITE, &old_protect))
|
||||
{
|
||||
if (VirtualProtect(&m_vtable[m_index], sizeof(void*), PAGE_READWRITE, &old_protect)) {
|
||||
m_vtable[m_index] = m_original;
|
||||
VirtualProtect(&m_vtable[m_index], sizeof(void*), old_protect, &old_protect);
|
||||
}
|
||||
@@ -63,7 +57,11 @@ public:
|
||||
m_index = 0;
|
||||
}
|
||||
|
||||
template <typename Fn> Fn original() const { return reinterpret_cast<Fn>(m_original); }
|
||||
template <typename Fn>
|
||||
Fn original() const
|
||||
{
|
||||
return reinterpret_cast<Fn>(m_original);
|
||||
}
|
||||
explicit operator bool() const { return m_vtable != nullptr; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -13,11 +13,9 @@
|
||||
#include "hook_install.hpp"
|
||||
#include "hook_registry.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight XInput detours before remove nulls the IPC pointer
|
||||
|
||||
@@ -41,16 +39,13 @@ std::array<CoopPadState, kMaxPads> g_cache;
|
||||
|
||||
void refresh_cache()
|
||||
{
|
||||
if (g_ipc == nullptr)
|
||||
{
|
||||
if (g_ipc == nullptr) {
|
||||
return;
|
||||
}
|
||||
CoopPadState pads[kMaxPads];
|
||||
std::uint32_t count = 0;
|
||||
if (g_ipc->snapshot(pads, count))
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i)
|
||||
{
|
||||
if (g_ipc->snapshot(pads, count)) {
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i) {
|
||||
g_cache[i] = pads[i];
|
||||
}
|
||||
}
|
||||
@@ -71,31 +66,26 @@ void fill_gamepad(const CoopPadState& pad, XINPUT_GAMEPAD& out)
|
||||
// (documented) XInputGetState, which must not report it.
|
||||
DWORD query_state(DWORD user_index, XINPUT_STATE* state, bool keep_guide)
|
||||
{
|
||||
if (state == nullptr || user_index >= kMaxPads)
|
||||
{
|
||||
if (state == nullptr || user_index >= kMaxPads) {
|
||||
return ERROR_DEVICE_NOT_CONNECTED;
|
||||
}
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->note_state_query(user_index); // proves to the host the game is polling us
|
||||
}
|
||||
refresh_cache();
|
||||
const CoopPadState& pad = g_cache[user_index];
|
||||
if (!pad.connected)
|
||||
{
|
||||
if (!pad.connected) {
|
||||
return ERROR_DEVICE_NOT_CONNECTED;
|
||||
}
|
||||
|
||||
XINPUT_STATE result = {};
|
||||
result.dwPacketNumber = pad.packet;
|
||||
fill_gamepad(pad, result.Gamepad);
|
||||
if (!keep_guide)
|
||||
{
|
||||
if (!keep_guide) {
|
||||
result.Gamepad.wButtons &= ~kGuideButton;
|
||||
}
|
||||
*state = result;
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->note_read_state(user_index, pad); // round-trip: what the game just read
|
||||
}
|
||||
return ERROR_SUCCESS;
|
||||
@@ -119,17 +109,14 @@ DWORD WINAPI hk_XInputGetCapabilities(DWORD user_index, DWORD /*flags*/, XINPUT_
|
||||
{
|
||||
DetourGate::Guard guard(g_gate); // keep g_ipc valid for this whole detour
|
||||
hook_note_call(g_id_getcaps);
|
||||
if (caps == nullptr || user_index >= kMaxPads)
|
||||
{
|
||||
if (caps == nullptr || user_index >= kMaxPads) {
|
||||
return ERROR_DEVICE_NOT_CONNECTED;
|
||||
}
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->note_caps_query(user_index);
|
||||
}
|
||||
refresh_cache();
|
||||
if (!g_cache[user_index].connected)
|
||||
{
|
||||
if (!g_cache[user_index].connected) {
|
||||
return ERROR_DEVICE_NOT_CONNECTED;
|
||||
}
|
||||
|
||||
@@ -156,12 +143,10 @@ DWORD WINAPI hk_XInputSetState(DWORD user_index, XINPUT_VIBRATION* vibration)
|
||||
{
|
||||
DetourGate::Guard guard(g_gate); // keep g_ipc valid for this whole detour
|
||||
hook_note_call(g_id_setstate);
|
||||
if (user_index >= kMaxPads || !g_cache[user_index].connected)
|
||||
{
|
||||
if (user_index >= kMaxPads || !g_cache[user_index].connected) {
|
||||
return ERROR_DEVICE_NOT_CONNECTED;
|
||||
}
|
||||
if (g_ipc != nullptr && vibration != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr && vibration != nullptr) {
|
||||
g_ipc->note_rumble(user_index, vibration->wLeftMotorSpeed, vibration->wRightMotorSpeed);
|
||||
}
|
||||
return ERROR_SUCCESS;
|
||||
@@ -170,12 +155,10 @@ DWORD WINAPI hk_XInputSetState(DWORD user_index, XINPUT_VIBRATION* vibration)
|
||||
// `name` is a GetProcAddress LPCSTR: an export name, or MAKEINTRESOURCEA(ordinal).
|
||||
void hook_export(HMODULE module, const char* name, void* detour, int registry_id)
|
||||
{
|
||||
if (module == nullptr)
|
||||
{
|
||||
if (module == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name)))
|
||||
{
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name))) {
|
||||
g_hooks.emplace_back();
|
||||
install_inline(g_hooks.back(), target, detour); // assign-then-enable (no install race)
|
||||
hook_set_installed(registry_id, true);
|
||||
@@ -186,8 +169,7 @@ void hook_export(HMODULE module, const char* name, void* detour, int registry_id
|
||||
|
||||
bool install_xinput_hooks(IpcClient& ipc)
|
||||
{
|
||||
if (!g_hooks.empty())
|
||||
{
|
||||
if (!g_hooks.empty()) {
|
||||
return true; // already installed
|
||||
}
|
||||
g_ipc = &ipc;
|
||||
@@ -201,22 +183,18 @@ bool install_xinput_hooks(IpcClient& ipc)
|
||||
// A process generally loads exactly one of these, but hook every one that is
|
||||
// present so we don't miss the one the game actually calls.
|
||||
const wchar_t* modules[] = {L"xinput1_4.dll", L"xinput1_3.dll", L"xinput9_1_0.dll", L"xinputuap.dll"};
|
||||
for (const wchar_t* name : modules)
|
||||
{
|
||||
for (const wchar_t* name : modules) {
|
||||
HMODULE module = GetModuleHandleW(name);
|
||||
if (module == nullptr)
|
||||
{
|
||||
if (module == nullptr) {
|
||||
continue;
|
||||
}
|
||||
hook_export(module, "XInputGetState", reinterpret_cast<void*>(&hk_XInputGetState), g_id_getstate);
|
||||
hook_export(module, MAKEINTRESOURCEA(100), reinterpret_cast<void*>(&hk_XInputGetStateEx),
|
||||
g_id_getstateex); // XInputGetStateEx is exported by ordinal only
|
||||
hook_export(module, "XInputGetCapabilities", reinterpret_cast<void*>(&hk_XInputGetCapabilities),
|
||||
g_id_getcaps);
|
||||
hook_export(module, "XInputGetCapabilities", reinterpret_cast<void*>(&hk_XInputGetCapabilities), g_id_getcaps);
|
||||
hook_export(module, "XInputSetState", reinterpret_cast<void*>(&hk_XInputSetState), g_id_setstate);
|
||||
}
|
||||
if (!g_hooks.empty())
|
||||
{
|
||||
if (!g_hooks.empty()) {
|
||||
g_ipc->mark_attached();
|
||||
return true;
|
||||
}
|
||||
@@ -229,8 +207,7 @@ void remove_xinput_hooks()
|
||||
// before nulling the IPC pointer they read. The XInput detours return synthesized pad state and
|
||||
// never call the trampoline, so (unlike the present/MKB hooks) destroying the vector after the
|
||||
// drain is safe -- there's no live trampoline a stale detour could jump through.
|
||||
for (auto& h : g_hooks)
|
||||
{
|
||||
for (auto& h : g_hooks) {
|
||||
disable_for_removal(h);
|
||||
}
|
||||
hook_set_installed(g_id_getstate, false);
|
||||
@@ -239,8 +216,7 @@ void remove_xinput_hooks()
|
||||
hook_set_installed(g_id_setstate, false);
|
||||
g_gate.drain(); // wait for any in-flight detour before nulling the IPC pointer it reads
|
||||
g_hooks.clear(); // no detour in-flight or able to start now -> safe to free the trampolines
|
||||
if (g_ipc != nullptr)
|
||||
{
|
||||
if (g_ipc != nullptr) {
|
||||
g_ipc->mark_detached();
|
||||
}
|
||||
g_ipc = nullptr;
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
|
||||
#include "ipc_client.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
// Locates the loaded XInput module(s) and hooks the state/capability entry
|
||||
// points. `ipc` must outlive the hooks. Returns true if at least one module was
|
||||
|
||||
@@ -11,14 +11,11 @@
|
||||
#include "audio/process_loopback_capture.hpp"
|
||||
#include "coop/audio_correlate.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
namespace coop {
|
||||
namespace {
|
||||
|
||||
// Resolve a (possibly EXTENSIBLE) WAVEFORMATEX to scalar channels / bits / tag.
|
||||
struct ScalarFormat
|
||||
{
|
||||
struct ScalarFormat {
|
||||
unsigned rate = 0;
|
||||
unsigned channels = 0;
|
||||
unsigned bits = 0;
|
||||
@@ -32,15 +29,11 @@ ScalarFormat resolve(const WAVEFORMATEX* wfx)
|
||||
f.channels = wfx->nChannels;
|
||||
f.bits = wfx->wBitsPerSample;
|
||||
f.tag = wfx->wFormatTag;
|
||||
if (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE && wfx->cbSize >= 22)
|
||||
{
|
||||
if (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE && wfx->cbSize >= 22) {
|
||||
const auto* ext = reinterpret_cast<const WAVEFORMATEXTENSIBLE*>(wfx);
|
||||
if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
|
||||
{
|
||||
if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
|
||||
f.tag = WAVE_FORMAT_IEEE_FLOAT;
|
||||
}
|
||||
else if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_PCM)
|
||||
{
|
||||
} else if (ext->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
|
||||
f.tag = WAVE_FORMAT_PCM;
|
||||
}
|
||||
}
|
||||
@@ -58,8 +51,7 @@ std::vector<float> to_mono(const std::vector<BYTE>& bytes, const ScalarFormat& f
|
||||
|
||||
void drain_ring(AudioRingHeader& ring, std::vector<BYTE>& scratch)
|
||||
{
|
||||
while (audio_ring_pop(ring, scratch.data(), static_cast<std::uint32_t>(scratch.size())) > 0)
|
||||
{
|
||||
while (audio_ring_pop(ring, scratch.data(), static_cast<std::uint32_t>(scratch.size())) > 0) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,19 +62,16 @@ ChunkedCapture parse_chunks(const std::vector<BYTE>& raw, unsigned stride)
|
||||
{
|
||||
ChunkedCapture cap;
|
||||
cap.stride = stride;
|
||||
if (stride == 0)
|
||||
{
|
||||
if (stride == 0) {
|
||||
return cap;
|
||||
}
|
||||
std::size_t off = 0;
|
||||
while (off + sizeof(std::uint32_t) <= raw.size())
|
||||
{
|
||||
while (off + sizeof(std::uint32_t) <= raw.size()) {
|
||||
std::uint32_t count = 0;
|
||||
std::memcpy(&count, raw.data() + off, sizeof(count));
|
||||
off += sizeof(count);
|
||||
const std::size_t payload = static_cast<std::size_t>(count) * stride;
|
||||
if (count == 0 || off + payload > raw.size())
|
||||
{
|
||||
if (count == 0 || off + payload > raw.size()) {
|
||||
break; // truncated or garbled -> stop
|
||||
}
|
||||
cap.counts.push_back(count);
|
||||
@@ -97,14 +86,12 @@ ChunkedCapture parse_chunks(const std::vector<BYTE>& raw, unsigned stride)
|
||||
FormatVerification verify_stream_format(DWORD pid, AudioRingHeader* ring, unsigned window_ms, bool recover_layout)
|
||||
{
|
||||
FormatVerification result;
|
||||
if (ring == nullptr)
|
||||
{
|
||||
if (ring == nullptr) {
|
||||
return result;
|
||||
}
|
||||
|
||||
WAVEFORMATEX* dev_wfx = default_render_format();
|
||||
if (dev_wfx == nullptr)
|
||||
{
|
||||
if (dev_wfx == nullptr) {
|
||||
return result;
|
||||
}
|
||||
const ScalarFormat dev = resolve(dev_wfx);
|
||||
@@ -120,12 +107,10 @@ FormatVerification verify_stream_format(DWORD pid, AudioRingHeader* ring, unsign
|
||||
ProcessLoopbackCapture loop;
|
||||
const std::uint32_t loop_block = dev_wfx->nBlockAlign;
|
||||
if (!loop.start(pid, dev_wfx, [&](const BYTE* data, std::uint32_t frames, bool silent) {
|
||||
if (!silent && data != nullptr)
|
||||
{
|
||||
if (!silent && data != nullptr) {
|
||||
loop_bytes.insert(loop_bytes.end(), data, data + static_cast<std::size_t>(frames) * loop_block);
|
||||
}
|
||||
}))
|
||||
{
|
||||
})) {
|
||||
// Distinguish "couldn't activate process loopback" from "captured fine but didn't correlate":
|
||||
// without the ground-truth post-mix path there's nothing to correlate against, so bail now
|
||||
// (don't burn the window capturing only the hook side) and leave the diagnostic visible.
|
||||
@@ -137,18 +122,15 @@ FormatVerification verify_stream_format(DWORD pid, AudioRingHeader* ring, unsign
|
||||
// Pull the hook's pre-mix bytes out of the ring across the window.
|
||||
std::vector<BYTE> hook_bytes;
|
||||
const DWORD end = GetTickCount() + window_ms;
|
||||
while (GetTickCount() < end)
|
||||
{
|
||||
while (GetTickCount() < end) {
|
||||
std::uint32_t n = 0;
|
||||
while ((n = audio_ring_pop(*ring, scratch.data(), static_cast<std::uint32_t>(scratch.size()))) > 0)
|
||||
{
|
||||
while ((n = audio_ring_pop(*ring, scratch.data(), static_cast<std::uint32_t>(scratch.size()))) > 0) {
|
||||
hook_bytes.insert(hook_bytes.end(), scratch.data(), scratch.data() + n);
|
||||
}
|
||||
Sleep(10);
|
||||
}
|
||||
std::uint32_t n = 0;
|
||||
while ((n = audio_ring_pop(*ring, scratch.data(), static_cast<std::uint32_t>(scratch.size()))) > 0)
|
||||
{
|
||||
while ((n = audio_ring_pop(*ring, scratch.data(), static_cast<std::uint32_t>(scratch.size()))) > 0) {
|
||||
hook_bytes.insert(hook_bytes.end(), scratch.data(), scratch.data() + n);
|
||||
}
|
||||
|
||||
@@ -166,19 +148,16 @@ FormatVerification verify_stream_format(DWORD pid, AudioRingHeader* ring, unsign
|
||||
CoTaskMemFree(dev_wfx);
|
||||
|
||||
char dbg[2] = {};
|
||||
if (GetEnvironmentVariableA("COOP_VERIFY_DEBUG", dbg, sizeof(dbg)) > 0 && dbg[0] == '1')
|
||||
{
|
||||
if (GetEnvironmentVariableA("COOP_VERIFY_DEBUG", dbg, sizeof(dbg)) > 0 && dbg[0] == '1') {
|
||||
std::fprintf(stderr, "[verify] dev=%uHz/%uch/%ubit blk=%u chunks=%zu hook_frames=%zu loop=%zu layout=%d\n",
|
||||
dev.rate, dev.channels, dev.bits, dev_block, cap.counts.size(), hook_frames, loop_mono.size(),
|
||||
recover_layout ? 1 : 0);
|
||||
}
|
||||
if (loop_mono.size() < need || hook_frames < need)
|
||||
{
|
||||
if (loop_mono.size() < need || hook_frames < need) {
|
||||
return result; // not enough non-silent audio captured (game quiet, or stream wasn't a guess)
|
||||
}
|
||||
|
||||
if (recover_layout)
|
||||
{
|
||||
if (recover_layout) {
|
||||
// Recover channels + bit depth too, by trying candidate de-interleavings of the
|
||||
// (de-padded) hook bytes and keeping whichever (layout, rate) correlates with the loopback.
|
||||
const FormatCorrelation fc =
|
||||
@@ -190,9 +169,7 @@ FormatVerification verify_stream_format(DWORD pid, AudioRingHeader* ring, unsign
|
||||
result.channels = fc.channels;
|
||||
result.bits = fc.bits;
|
||||
result.format_tag = fc.tag;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Rate only, assuming the hook layout matches the device (common stereo case), so
|
||||
// the de-padded payload is already clean device-layout audio.
|
||||
const std::vector<float> hook_mono = to_mono(cap.bytes, dev);
|
||||
|
||||
@@ -20,11 +20,9 @@
|
||||
|
||||
#include "coop/audio_ring.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct FormatVerification
|
||||
{
|
||||
struct FormatVerification {
|
||||
bool ok = false; // a confident rate correlation was found
|
||||
unsigned rate = 0; // recovered true sample rate (Hz)
|
||||
double score = 0.0; // correlation score of the winning rate, [0,1]
|
||||
|
||||
@@ -14,15 +14,12 @@
|
||||
#include "audio/process_loopback_capture.hpp"
|
||||
#include "audio/render_pacer.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
namespace coop {
|
||||
namespace {
|
||||
|
||||
// Single-producer/single-consumer byte FIFO guarded by a mutex (the capture
|
||||
// thread pushes, the render thread pops). Overflow drops the oldest samples.
|
||||
struct ByteRing
|
||||
{
|
||||
struct ByteRing {
|
||||
std::mutex mutex;
|
||||
std::vector<BYTE> buf;
|
||||
size_t head = 0;
|
||||
@@ -37,8 +34,7 @@ struct ByteRing
|
||||
|
||||
void drop_for(size_t incoming)
|
||||
{
|
||||
if (count + incoming > buf.size())
|
||||
{
|
||||
if (count + incoming > buf.size()) {
|
||||
const size_t drop = count + incoming - buf.size();
|
||||
head = (head + drop) % buf.size();
|
||||
count -= drop;
|
||||
@@ -48,10 +44,8 @@ struct ByteRing
|
||||
void push(const BYTE* data, size_t bytes, bool silent)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if (bytes > buf.size())
|
||||
{
|
||||
if (data)
|
||||
{
|
||||
if (bytes > buf.size()) {
|
||||
if (data) {
|
||||
data += bytes - buf.size();
|
||||
}
|
||||
bytes = buf.size();
|
||||
@@ -59,29 +53,21 @@ struct ByteRing
|
||||
drop_for(bytes);
|
||||
const size_t tail = (head + count) % buf.size();
|
||||
const size_t first = std::min(bytes, buf.size() - tail);
|
||||
if (silent || !data)
|
||||
{
|
||||
if (silent || !data) {
|
||||
std::memset(&buf[tail], 0, first);
|
||||
if (bytes > first)
|
||||
{
|
||||
if (bytes > first) {
|
||||
std::memset(&buf[0], 0, bytes - first);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::memcpy(&buf[tail], data, first);
|
||||
if (bytes > first)
|
||||
{
|
||||
if (bytes > first) {
|
||||
std::memcpy(&buf[0], data + first, bytes - first);
|
||||
}
|
||||
}
|
||||
count += bytes;
|
||||
}
|
||||
|
||||
size_t available() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
size_t available() const { return count; }
|
||||
|
||||
// Copy up to `bytes` into `dst`; returns how many bytes were available.
|
||||
size_t pop(BYTE* dst, size_t bytes)
|
||||
@@ -90,8 +76,7 @@ struct ByteRing
|
||||
bytes = std::min(bytes, count);
|
||||
const size_t first = std::min(bytes, buf.size() - head);
|
||||
std::memcpy(dst, &buf[head], first);
|
||||
if (bytes > first)
|
||||
{
|
||||
if (bytes > first) {
|
||||
std::memcpy(dst + first, &buf[0], bytes - first);
|
||||
}
|
||||
head = (head + bytes) % buf.size();
|
||||
@@ -102,8 +87,7 @@ struct ByteRing
|
||||
|
||||
// The default render endpoint plus an event-driven render client, shared by the hooked and
|
||||
// loopback mirror paths. RAII: everything acquired is released on destruction.
|
||||
struct RenderEndpoint
|
||||
{
|
||||
struct RenderEndpoint {
|
||||
IMMDeviceEnumerator* enumerator = nullptr;
|
||||
IMMDevice* endpoint = nullptr;
|
||||
IAudioClient* client = nullptr;
|
||||
@@ -117,24 +101,19 @@ struct RenderEndpoint
|
||||
|
||||
~RenderEndpoint()
|
||||
{
|
||||
if (render)
|
||||
{
|
||||
if (render) {
|
||||
render->Release();
|
||||
}
|
||||
if (client)
|
||||
{
|
||||
if (client) {
|
||||
client->Release();
|
||||
}
|
||||
if (endpoint)
|
||||
{
|
||||
if (endpoint) {
|
||||
endpoint->Release();
|
||||
}
|
||||
if (enumerator)
|
||||
{
|
||||
if (enumerator) {
|
||||
enumerator->Release();
|
||||
}
|
||||
if (event)
|
||||
{
|
||||
if (event) {
|
||||
CloseHandle(event);
|
||||
}
|
||||
}
|
||||
@@ -144,28 +123,24 @@ struct RenderEndpoint
|
||||
template <typename Fail>
|
||||
bool activate(Fail&& fail)
|
||||
{
|
||||
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL,
|
||||
__uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&enumerator));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
|
||||
reinterpret_cast<void**>(&enumerator));
|
||||
if (FAILED(hr)) {
|
||||
fail("CoCreateInstance(MMDeviceEnumerator)", hr);
|
||||
return false;
|
||||
}
|
||||
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &endpoint);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("GetDefaultAudioEndpoint", hr);
|
||||
return false;
|
||||
}
|
||||
hr = endpoint->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&client));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("Activate render client", hr);
|
||||
return false;
|
||||
}
|
||||
event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
if (!event)
|
||||
{
|
||||
if (!event) {
|
||||
fail("CreateEvent(render)", HRESULT_FROM_WIN32(GetLastError()));
|
||||
return false;
|
||||
}
|
||||
@@ -185,20 +160,17 @@ struct RenderEndpoint
|
||||
bool wire(Fail&& fail)
|
||||
{
|
||||
HRESULT hr = client->SetEventHandle(event);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("Render SetEventHandle", hr);
|
||||
return false;
|
||||
}
|
||||
hr = client->GetService(__uuidof(IAudioRenderClient), reinterpret_cast<void**>(&render));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("GetService(RenderClient)", hr);
|
||||
return false;
|
||||
}
|
||||
hr = client->GetBufferSize(&buffer_frames);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("GetBufferSize", hr);
|
||||
return false;
|
||||
}
|
||||
@@ -246,10 +218,8 @@ void AudioMirror::set_fallback_reason(std::string s)
|
||||
|
||||
void AudioMirror::enable_capture(AudioRingHeader* const* rings, bool on)
|
||||
{
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
if (rings[i] != nullptr)
|
||||
{
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i) {
|
||||
if (rings[i] != nullptr) {
|
||||
rings[i]->capture_enabled.store(on ? 1u : 0u, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
@@ -258,8 +228,7 @@ void AudioMirror::enable_capture(AudioRingHeader* const* rings, bool on)
|
||||
void AudioMirror::request_op(unsigned slot, std::uint32_t kind, std::uint32_t rate, std::uint32_t channels,
|
||||
std::uint32_t bits, std::uint32_t format_tag)
|
||||
{
|
||||
if (slot >= kMaxAudioStreams)
|
||||
{
|
||||
if (slot >= kMaxAudioStreams) {
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(ops_mutex_);
|
||||
@@ -273,11 +242,9 @@ void AudioMirror::drain_ops()
|
||||
std::lock_guard<std::mutex> lock(ops_mutex_);
|
||||
ops.swap(pending_ops_);
|
||||
}
|
||||
for (const PendingOp& op : ops)
|
||||
{
|
||||
for (const PendingOp& op : ops) {
|
||||
AudioRingHeader* ring = (op.slot < kMaxAudioStreams) ? session_rings_[op.slot] : nullptr;
|
||||
if (ring != nullptr)
|
||||
{
|
||||
if (ring != nullptr) {
|
||||
audio_ring_post_op(*ring, op.kind, op.rate, op.channels, op.bits, op.format_tag);
|
||||
}
|
||||
}
|
||||
@@ -286,14 +253,12 @@ void AudioMirror::drain_ops()
|
||||
bool AudioMirror::start(DWORD pid)
|
||||
{
|
||||
stop();
|
||||
if (!pid)
|
||||
{
|
||||
if (!pid) {
|
||||
set_status("No target process.");
|
||||
return false;
|
||||
}
|
||||
stop_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!stop_event_)
|
||||
{
|
||||
if (!stop_event_) {
|
||||
set_status("CreateEvent failed.");
|
||||
return false;
|
||||
}
|
||||
@@ -305,21 +270,17 @@ bool AudioMirror::start(DWORD pid)
|
||||
|
||||
void AudioMirror::stop()
|
||||
{
|
||||
if (stop_event_)
|
||||
{
|
||||
if (stop_event_) {
|
||||
SetEvent(stop_event_);
|
||||
}
|
||||
if (thread_.joinable())
|
||||
{
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
if (stop_event_)
|
||||
{
|
||||
if (stop_event_) {
|
||||
CloseHandle(stop_event_);
|
||||
stop_event_ = nullptr;
|
||||
}
|
||||
for (auto& shm : audio_ring_shm_)
|
||||
{
|
||||
for (auto& shm : audio_ring_shm_) {
|
||||
shm.reset();
|
||||
}
|
||||
running_.store(false, std::memory_order_release);
|
||||
@@ -337,18 +298,14 @@ bool AudioMirror::stop_requested() const
|
||||
bool AudioMirror::wait_for_format(AudioRingHeader* ring, DWORD timeout_ms)
|
||||
{
|
||||
const DWORD end = GetTickCount() + timeout_ms;
|
||||
for (;;)
|
||||
{
|
||||
if (audio_ring_format_ready(*ring))
|
||||
{
|
||||
for (;;) {
|
||||
if (audio_ring_format_ready(*ring)) {
|
||||
return true;
|
||||
}
|
||||
if (stop_event_ && WaitForSingleObject(stop_event_, 25) == WAIT_OBJECT_0)
|
||||
{
|
||||
if (stop_event_ && WaitForSingleObject(stop_event_, 25) == WAIT_OBJECT_0) {
|
||||
return false; // stopping
|
||||
}
|
||||
if (GetTickCount() >= end)
|
||||
{
|
||||
if (GetTickCount() >= end) {
|
||||
return false; // hook never published a format -> fall back to loopback
|
||||
}
|
||||
}
|
||||
@@ -363,10 +320,8 @@ void AudioMirror::thread_main(DWORD pid)
|
||||
// live the whole session and promote loopback -> hooked the moment a format appears.
|
||||
AudioRingHeader* rings[kMaxAudioStreams] = {};
|
||||
bool created_primary = false;
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
if (audio_ring_shm_[i].create(audio_ring_name(pid, i), audio_ring_total_size(kAudioRingCapacity)))
|
||||
{
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i) {
|
||||
if (audio_ring_shm_[i].create(audio_ring_name(pid, i), audio_ring_total_size(kAudioRingCapacity))) {
|
||||
rings[i] = audio_ring_shm_[i].as<AudioRingHeader>();
|
||||
audio_ring_init(*rings[i], kAudioRingCapacity);
|
||||
created_primary = created_primary || (i == 0);
|
||||
@@ -374,17 +329,13 @@ void AudioMirror::thread_main(DWORD pid)
|
||||
session_rings_[i] = rings[i]; // visible to drain_ops on this (audio) thread
|
||||
}
|
||||
|
||||
if (!created_primary)
|
||||
{
|
||||
if (!created_primary) {
|
||||
// Couldn't create the hook's ring -> loopback only (no promote target).
|
||||
set_fallback_reason("Couldn't create the audio ring; using loopback (echo).");
|
||||
if (!stop_requested())
|
||||
{
|
||||
if (!stop_requested()) {
|
||||
run_loopback(pid, nullptr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Prefer the hooked (no-echo) path. While it isn't ready, run loopback (echo) so
|
||||
// guests still hear audio, but watch the ring and promote to hooked the instant the
|
||||
// hook publishes a format. A short wait first catches the fast cases (exact format /
|
||||
@@ -393,37 +344,29 @@ void AudioMirror::thread_main(DWORD pid)
|
||||
// that gap and the promote hands off seamlessly.
|
||||
constexpr DWORD kHookWaitMs = 1200;
|
||||
bool format_verified = false; // run the two-path correlation verify/correct at most once
|
||||
for (;;)
|
||||
{
|
||||
if (stop_requested())
|
||||
{
|
||||
for (;;) {
|
||||
if (stop_requested()) {
|
||||
break;
|
||||
}
|
||||
set_status("Waiting for render-hook…");
|
||||
bool watch_for_promote = true;
|
||||
if (wait_for_format(rings[0], kHookWaitMs))
|
||||
{
|
||||
if (wait_for_format(rings[0], kHookWaitMs)) {
|
||||
set_fallback_reason({}); // hooked path is taking over
|
||||
const HookedResult r = run_hooked(rings);
|
||||
if (r == HookedResult::Stopped)
|
||||
{
|
||||
if (r == HookedResult::Stopped) {
|
||||
break; // ran to a clean stop
|
||||
}
|
||||
if (r == HookedResult::Reinit)
|
||||
{
|
||||
if (r == HookedResult::Reinit) {
|
||||
continue; // hook re-published (re-measure / override) -> re-read the new format
|
||||
}
|
||||
if (stop_requested())
|
||||
{
|
||||
if (stop_requested()) {
|
||||
break;
|
||||
}
|
||||
// run_hooked failed to initialize (the game's format isn't renderable here).
|
||||
// That won't fix itself, so don't bounce back to it -- stay on loopback.
|
||||
set_fallback_reason("Render-hook format isn't renderable on this endpoint; using loopback (echo).");
|
||||
watch_for_promote = false;
|
||||
}
|
||||
else if (!stop_requested())
|
||||
{
|
||||
} else if (!stop_requested()) {
|
||||
set_fallback_reason(
|
||||
"Render-hook hasn't published a format yet; using loopback (echo) -- will switch to "
|
||||
"hooked automatically once it does.");
|
||||
@@ -433,8 +376,7 @@ void AudioMirror::thread_main(DWORD pid)
|
||||
// and correct it through the existing override channel -- this hardens the cadence
|
||||
// method's intermittent pitch-shift. It's hidden inside the measurement gap loopback
|
||||
// already covers, so exact streams (format published immediately) never pay for it.
|
||||
if (!format_verified)
|
||||
{
|
||||
if (!format_verified) {
|
||||
format_verified = true;
|
||||
// recover_layout: correlate the full format (rate AND channels/bit-depth), so a
|
||||
// game rendering a different layout than the device is corrected too, not just
|
||||
@@ -442,8 +384,7 @@ void AudioMirror::thread_main(DWORD pid)
|
||||
// a silent game, or a genuinely ambiguous identical-channel layout).
|
||||
const FormatVerification fv = verify_stream_format(pid, rings[0], /*window_ms=*/900,
|
||||
/*recover_layout=*/true);
|
||||
if (fv.ok)
|
||||
{
|
||||
if (fv.ok) {
|
||||
set_status("Verified render-hook format by correlation.");
|
||||
audio_ring_post_op(*rings[0], AudioRingOp_Override, fv.rate, fv.channels, fv.bits,
|
||||
fv.format_tag);
|
||||
@@ -452,8 +393,7 @@ void AudioMirror::thread_main(DWORD pid)
|
||||
}
|
||||
|
||||
enable_capture(rings, false); // game audible locally so loopback can capture it
|
||||
if (!run_loopback(pid, watch_for_promote ? rings[0] : nullptr))
|
||||
{
|
||||
if (!run_loopback(pid, watch_for_promote ? rings[0] : nullptr)) {
|
||||
break; // stopped (not a promote)
|
||||
}
|
||||
// Promoted: a format appeared -> loop and try the hooked path again.
|
||||
@@ -461,24 +401,20 @@ void AudioMirror::thread_main(DWORD pid)
|
||||
}
|
||||
|
||||
enable_capture(rings, false);
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i) {
|
||||
session_rings_[i] = nullptr; // audio thread owns this; cleared before unmapping
|
||||
}
|
||||
for (auto& shm : audio_ring_shm_)
|
||||
{
|
||||
for (auto& shm : audio_ring_shm_) {
|
||||
shm.reset();
|
||||
}
|
||||
|
||||
if (running_.load(std::memory_order_acquire))
|
||||
{
|
||||
if (running_.load(std::memory_order_acquire)) {
|
||||
running_.store(false, std::memory_order_release);
|
||||
set_status("Stopped.");
|
||||
}
|
||||
source_.store(Source::None, std::memory_order_relaxed);
|
||||
|
||||
if (com_ok)
|
||||
{
|
||||
if (com_ok) {
|
||||
CoUninitialize();
|
||||
}
|
||||
}
|
||||
@@ -501,8 +437,7 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
const unsigned bits = primary->bits;
|
||||
const unsigned tag = primary->format_tag;
|
||||
const unsigned block_align = primary->block_align ? primary->block_align : channels * (bits / 8);
|
||||
if (rate == 0 || channels == 0 || block_align == 0)
|
||||
{
|
||||
if (rate == 0 || channels == 0 || block_align == 0) {
|
||||
enable_capture(rings, false); // let the game play locally again
|
||||
return HookedResult::Failed;
|
||||
}
|
||||
@@ -515,8 +450,7 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
wfx.Format.wBitsPerSample = static_cast<WORD>(bits);
|
||||
wfx.Format.nBlockAlign = static_cast<WORD>(block_align);
|
||||
wfx.Format.nAvgBytesPerSec = block_align * rate;
|
||||
if (channels > 2 || bits > 16)
|
||||
{
|
||||
if (channels > 2 || bits > 16) {
|
||||
wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
||||
wfx.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
|
||||
wfx.Samples.wValidBitsPerSample = static_cast<WORD>(bits);
|
||||
@@ -532,11 +466,8 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
wfx.dwChannelMask = (channels >= 32) ? 0xFFFFFFFFu : ((1u << channels) - 1u);
|
||||
break;
|
||||
}
|
||||
wfx.SubFormat =
|
||||
(tag == WAVE_FORMAT_IEEE_FLOAT) ? KSDATAFORMAT_SUBTYPE_IEEE_FLOAT : KSDATAFORMAT_SUBTYPE_PCM;
|
||||
}
|
||||
else
|
||||
{
|
||||
wfx.SubFormat = (tag == WAVE_FORMAT_IEEE_FLOAT) ? KSDATAFORMAT_SUBTYPE_IEEE_FLOAT : KSDATAFORMAT_SUBTYPE_PCM;
|
||||
} else {
|
||||
wfx.Format.wFormatTag = static_cast<WORD>(tag ? tag : WAVE_FORMAT_PCM);
|
||||
wfx.Format.cbSize = 0;
|
||||
}
|
||||
@@ -547,22 +478,18 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
HookedResult result = HookedResult::Stopped;
|
||||
auto fail = [this](const char* step, HRESULT hr) { set_error(step, hr); };
|
||||
|
||||
do
|
||||
{
|
||||
if (!ep.activate(fail))
|
||||
{
|
||||
do {
|
||||
if (!ep.activate(fail)) {
|
||||
break;
|
||||
}
|
||||
const DWORD flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM |
|
||||
AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY;
|
||||
if (FAILED(ep.initialize(fmt, flags)))
|
||||
{
|
||||
const DWORD flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM
|
||||
| AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY;
|
||||
if (FAILED(ep.initialize(fmt, flags))) {
|
||||
// The game's format isn't renderable here (rare). Bail to loopback.
|
||||
break;
|
||||
}
|
||||
started = true; // past the point where falling back is clean
|
||||
if (!ep.wire(fail))
|
||||
{
|
||||
if (!ep.wire(fail)) {
|
||||
break;
|
||||
}
|
||||
IAudioClient* render_client = ep.client;
|
||||
@@ -582,8 +509,7 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
std::vector<BYTE> temp(static_cast<size_t>(render_frames) * frame_bytes);
|
||||
std::vector<float> acc(static_cast<size_t>(render_frames) * channels);
|
||||
|
||||
if (const HRESULT hr = render_client->Start(); FAILED(hr))
|
||||
{
|
||||
if (const HRESULT hr = render_client->Start(); FAILED(hr)) {
|
||||
set_error("Render Start", hr);
|
||||
break;
|
||||
}
|
||||
@@ -595,23 +521,19 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
running_.store(true, std::memory_order_release);
|
||||
|
||||
HANDLE waits[2] = {stop_event_, ep.event};
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
const DWORD w = WaitForMultipleObjects(2, waits, FALSE, 200);
|
||||
if (w == WAIT_OBJECT_0)
|
||||
{
|
||||
if (w == WAIT_OBJECT_0) {
|
||||
break; // stop requested
|
||||
}
|
||||
drain_ops(); // post any queued operator ops (re-measure / override) to the hook
|
||||
if (primary->format_generation.load(std::memory_order_acquire) != start_gen)
|
||||
{
|
||||
if (primary->format_generation.load(std::memory_order_acquire) != start_gen) {
|
||||
result = HookedResult::Reinit; // hook re-published -> re-read the new format
|
||||
break;
|
||||
}
|
||||
|
||||
UINT32 padding = 0;
|
||||
if (FAILED(render_client->GetCurrentPadding(&padding)))
|
||||
{
|
||||
if (FAILED(render_client->GetCurrentPadding(&padding))) {
|
||||
continue;
|
||||
}
|
||||
const UINT32 avail = render_frames - padding;
|
||||
@@ -622,43 +544,35 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
const UINT32 have = static_cast<UINT32>(ring_bytes / frame_bytes);
|
||||
const UINT32 to_write = pacer.pump(avail, have, padding);
|
||||
{
|
||||
if (to_write > 0)
|
||||
{
|
||||
if (to_write > 0) {
|
||||
// Active streams = same format as primary (so they can be summed).
|
||||
// Streams with a different format are still silenced by the hook (no
|
||||
// echo) but can't be mixed here without resampling -> skipped.
|
||||
unsigned active[kMaxAudioStreams];
|
||||
unsigned n_active = 0;
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
for (unsigned i = 0; i < kMaxAudioStreams; ++i) {
|
||||
AudioRingHeader* r = rings[i];
|
||||
if (r == nullptr)
|
||||
{
|
||||
if (r == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (i == 0 || (audio_ring_format_ready(*r) && r->sample_rate == rate &&
|
||||
r->channels == channels && r->bits == bits && r->format_tag == tag))
|
||||
{
|
||||
if (i == 0
|
||||
|| (audio_ring_format_ready(*r) && r->sample_rate == rate && r->channels == channels
|
||||
&& r->bits == bits && r->format_tag == tag)) {
|
||||
active[n_active++] = i;
|
||||
}
|
||||
}
|
||||
|
||||
BYTE* dst = nullptr;
|
||||
if (SUCCEEDED(render->GetBuffer(to_write, &dst)))
|
||||
{
|
||||
if (SUCCEEDED(render->GetBuffer(to_write, &dst))) {
|
||||
const std::uint32_t want_bytes = to_write * static_cast<std::uint32_t>(frame_bytes);
|
||||
if (n_active <= 1 || !mixer_ok)
|
||||
{
|
||||
if (n_active <= 1 || !mixer_ok) {
|
||||
// Single stream (the common case) or an unmixable format:
|
||||
// passthrough the primary, byte-for-byte (no mixer overhead).
|
||||
audio_ring_pop(*primary, dst, want_bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
const std::uint32_t samples = to_write * channels;
|
||||
std::fill(acc.begin(), acc.begin() + samples, 0.0f);
|
||||
for (unsigned k = 0; k < n_active; ++k)
|
||||
{
|
||||
for (unsigned k = 0; k < n_active; ++k) {
|
||||
std::memset(temp.data(), 0, want_bytes); // zero-fill short reads
|
||||
audio_ring_pop(*rings[active[k]], temp.data(), want_bytes);
|
||||
mix_add(acc.data(), temp.data(), samples, tag, bits);
|
||||
@@ -677,13 +591,11 @@ AudioMirror::HookedResult AudioMirror::run_hooked(AudioRingHeader* const* rings)
|
||||
|
||||
// On a re-init (format changed) keep capturing so the rebuilt render client picks up
|
||||
// seamlessly; otherwise free the game's local playback (stop / fall back to loopback).
|
||||
if (result != HookedResult::Reinit)
|
||||
{
|
||||
if (result != HookedResult::Reinit) {
|
||||
enable_capture(rings, false);
|
||||
}
|
||||
|
||||
if (!started)
|
||||
{
|
||||
if (!started) {
|
||||
// Never got a working render client; let the caller try loopback. Capture is
|
||||
// already disabled above so loopback hears the game.
|
||||
return HookedResult::Failed;
|
||||
@@ -701,17 +613,14 @@ bool AudioMirror::run_loopback(DWORD pid, AudioRingHeader* promote_ring)
|
||||
ProcessLoopbackCapture capture;
|
||||
auto fail = [this](const char* step, HRESULT hr) { set_error(step, hr); };
|
||||
|
||||
do
|
||||
{
|
||||
if (!ep.activate(fail))
|
||||
{
|
||||
do {
|
||||
if (!ep.activate(fail)) {
|
||||
break;
|
||||
}
|
||||
// Capture and render share one format (the output endpoint's mix format);
|
||||
// WASAPI converts the captured process audio into it.
|
||||
HRESULT hr = ep.client->GetMixFormat(&fmt);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("GetMixFormat", hr);
|
||||
break;
|
||||
}
|
||||
@@ -719,13 +628,11 @@ bool AudioMirror::run_loopback(DWORD pid, AudioRingHeader* promote_ring)
|
||||
channels_.store(fmt->nChannels, std::memory_order_relaxed);
|
||||
|
||||
hr = ep.initialize(fmt, AUDCLNT_STREAMFLAGS_EVENTCALLBACK);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("Render Initialize", hr);
|
||||
break;
|
||||
}
|
||||
if (!ep.wire(fail))
|
||||
{
|
||||
if (!ep.wire(fail)) {
|
||||
break;
|
||||
}
|
||||
IAudioClient* render_client = ep.client;
|
||||
@@ -743,14 +650,12 @@ bool AudioMirror::run_loopback(DWORD pid, AudioRingHeader* promote_ring)
|
||||
// Capture pushes packets straight into the render ring.
|
||||
if (!capture.start(pid, fmt, [&ring, frame_bytes](const BYTE* data, UINT32 frames, bool silent) {
|
||||
ring.push(data, static_cast<size_t>(frames) * frame_bytes, silent);
|
||||
}))
|
||||
{
|
||||
})) {
|
||||
fail("Capture start", E_FAIL);
|
||||
break;
|
||||
}
|
||||
|
||||
if (FAILED(hr = render_client->Start()))
|
||||
{
|
||||
if (FAILED(hr = render_client->Start())) {
|
||||
fail("Render Start", hr);
|
||||
break;
|
||||
}
|
||||
@@ -762,44 +667,36 @@ bool AudioMirror::run_loopback(DWORD pid, AudioRingHeader* promote_ring)
|
||||
running_.store(true, std::memory_order_release);
|
||||
|
||||
HANDLE waits[2] = {stop_event_, ep.event};
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
const DWORD w = WaitForMultipleObjects(2, waits, FALSE, 200);
|
||||
if (w == WAIT_OBJECT_0)
|
||||
{
|
||||
if (w == WAIT_OBJECT_0) {
|
||||
break;
|
||||
}
|
||||
if (!capture.running())
|
||||
{
|
||||
if (!capture.running()) {
|
||||
set_status(capture.status());
|
||||
break;
|
||||
}
|
||||
drain_ops(); // operator ops (re-measure / override) reach the hook even on loopback
|
||||
// Auto-promote: the hook published a format -> hand back so the caller switches
|
||||
// to the no-echo hooked path (the rings stayed live the whole time).
|
||||
if (promote_ring != nullptr && audio_ring_format_ready(*promote_ring))
|
||||
{
|
||||
if (promote_ring != nullptr && audio_ring_format_ready(*promote_ring)) {
|
||||
set_status("Render-hook ready -- switching to hooked (no echo)…");
|
||||
promote = true;
|
||||
break;
|
||||
}
|
||||
|
||||
UINT32 padding = 0;
|
||||
if (FAILED(render_client->GetCurrentPadding(&padding)))
|
||||
{
|
||||
if (FAILED(render_client->GetCurrentPadding(&padding))) {
|
||||
continue;
|
||||
}
|
||||
const UINT32 avail = render_frames - padding;
|
||||
buffered_ms_.store(
|
||||
static_cast<unsigned>(ring.available() / frame_bytes * 1000 / fmt->nSamplesPerSec),
|
||||
buffered_ms_.store(static_cast<unsigned>(ring.available() / frame_bytes * 1000 / fmt->nSamplesPerSec),
|
||||
std::memory_order_relaxed);
|
||||
const UINT32 have = static_cast<UINT32>(ring.available() / frame_bytes);
|
||||
const UINT32 to_write = pacer.pump(avail, have, padding);
|
||||
if (to_write > 0)
|
||||
{
|
||||
if (to_write > 0) {
|
||||
BYTE* dst = nullptr;
|
||||
if (SUCCEEDED(render->GetBuffer(to_write, &dst)))
|
||||
{
|
||||
if (SUCCEEDED(render->GetBuffer(to_write, &dst))) {
|
||||
ring.pop(dst, static_cast<size_t>(to_write) * frame_bytes);
|
||||
render->ReleaseBuffer(to_write, 0);
|
||||
}
|
||||
@@ -810,8 +707,7 @@ bool AudioMirror::run_loopback(DWORD pid, AudioRingHeader* promote_ring)
|
||||
} while (false);
|
||||
|
||||
capture.stop();
|
||||
if (fmt)
|
||||
{
|
||||
if (fmt) {
|
||||
CoTaskMemFree(fmt);
|
||||
}
|
||||
return promote; // true = hook caught up, caller should switch to hooked
|
||||
|
||||
@@ -22,11 +22,9 @@
|
||||
#include "coop/protocol.hpp" // kMaxAudioStreams
|
||||
#include "coop/shared_memory.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class AudioMirror
|
||||
{
|
||||
class AudioMirror {
|
||||
public:
|
||||
AudioMirror() = default;
|
||||
~AudioMirror();
|
||||
@@ -42,49 +40,29 @@ public:
|
||||
|
||||
// True once the audio thread is actively mirroring (false while starting or
|
||||
// after a failure).
|
||||
[[nodiscard]] bool running() const
|
||||
{
|
||||
return running_.load(std::memory_order_acquire);
|
||||
}
|
||||
[[nodiscard]] bool running() const { return running_.load(std::memory_order_acquire); }
|
||||
|
||||
// The process currently targeted (0 if stopped). Updated synchronously by
|
||||
// start()/stop() so the UI can detect target changes without races.
|
||||
[[nodiscard]] DWORD target_pid() const
|
||||
{
|
||||
return pid_;
|
||||
}
|
||||
[[nodiscard]] DWORD target_pid() const { return pid_; }
|
||||
|
||||
[[nodiscard]] unsigned sample_rate() const
|
||||
{
|
||||
return sample_rate_.load(std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] unsigned channels() const
|
||||
{
|
||||
return channels_.load(std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] unsigned sample_rate() const { return sample_rate_.load(std::memory_order_relaxed); }
|
||||
[[nodiscard]] unsigned channels() const { return channels_.load(std::memory_order_relaxed); }
|
||||
|
||||
// Audio currently buffered between capture and the output device, in ms — a
|
||||
// health/latency proxy (rises if the consumer can't keep up). 0 when stopped.
|
||||
[[nodiscard]] unsigned buffered_ms() const
|
||||
{
|
||||
return buffered_ms_.load(std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] unsigned buffered_ms() const { return buffered_ms_.load(std::memory_order_relaxed); }
|
||||
|
||||
// Which capture path is active, for the UI's source indicator.
|
||||
enum class Source
|
||||
{
|
||||
enum class Source {
|
||||
None,
|
||||
Hooked, // shared audio ring from the render-hook (no echo)
|
||||
Loopback, // WASAPI process loopback (echo)
|
||||
};
|
||||
[[nodiscard]] Source source() const
|
||||
{
|
||||
return source_.load(std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] Source source() const { return source_.load(std::memory_order_relaxed); }
|
||||
[[nodiscard]] const char* source_name() const
|
||||
{
|
||||
switch (source())
|
||||
{
|
||||
switch (source()) {
|
||||
case Source::Hooked:
|
||||
return "Hooked"; // echo depends on the format provenance; the panel shows it
|
||||
case Source::Loopback:
|
||||
@@ -109,8 +87,7 @@ public:
|
||||
private:
|
||||
void thread_main(DWORD pid);
|
||||
// Outcome of a hooked render session.
|
||||
enum class HookedResult
|
||||
{
|
||||
enum class HookedResult {
|
||||
Stopped, // clean stop (mirror stopping) -> done
|
||||
Failed, // setup failed (format not renderable) -> caller falls back to loopback
|
||||
Reinit, // the hook re-published the format (re-measure/override) -> re-read and retry
|
||||
@@ -140,8 +117,7 @@ private:
|
||||
|
||||
// Operator ops queued by request_op (any thread) and applied to the rings on the
|
||||
// audio thread (which owns the mappings). Guarded by ops_mutex_.
|
||||
struct PendingOp
|
||||
{
|
||||
struct PendingOp {
|
||||
unsigned slot;
|
||||
std::uint32_t kind, rate, channels, bits, format_tag;
|
||||
};
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// WAVE_FORMAT_* values used here (kept local to avoid an mmreg.h dependency).
|
||||
inline constexpr std::uint32_t kWaveFormatPcm = 1;
|
||||
@@ -31,19 +30,14 @@ inline float soft_clip(float x)
|
||||
inline void mix_add(float* acc, const std::uint8_t* src, std::uint32_t samples, std::uint32_t format_tag,
|
||||
std::uint32_t bits)
|
||||
{
|
||||
if (format_tag == kWaveFormatFloat && bits == 32)
|
||||
{
|
||||
if (format_tag == kWaveFormatFloat && bits == 32) {
|
||||
const auto* f = reinterpret_cast<const float*>(src);
|
||||
for (std::uint32_t i = 0; i < samples; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < samples; ++i) {
|
||||
acc[i] += f[i];
|
||||
}
|
||||
}
|
||||
else if (format_tag == kWaveFormatPcm && bits == 16)
|
||||
{
|
||||
} else if (format_tag == kWaveFormatPcm && bits == 16) {
|
||||
const auto* s = reinterpret_cast<const std::int16_t*>(src);
|
||||
for (std::uint32_t i = 0; i < samples; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < samples; ++i) {
|
||||
acc[i] += static_cast<float>(s[i]) / 32768.0f;
|
||||
}
|
||||
}
|
||||
@@ -54,19 +48,14 @@ inline void mix_add(float* acc, const std::uint8_t* src, std::uint32_t samples,
|
||||
inline void mix_store(std::uint8_t* dst, const float* acc, std::uint32_t samples, std::uint32_t format_tag,
|
||||
std::uint32_t bits)
|
||||
{
|
||||
if (format_tag == kWaveFormatFloat && bits == 32)
|
||||
{
|
||||
if (format_tag == kWaveFormatFloat && bits == 32) {
|
||||
auto* f = reinterpret_cast<float*>(dst);
|
||||
for (std::uint32_t i = 0; i < samples; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < samples; ++i) {
|
||||
f[i] = soft_clip(acc[i]);
|
||||
}
|
||||
}
|
||||
else if (format_tag == kWaveFormatPcm && bits == 16)
|
||||
{
|
||||
} else if (format_tag == kWaveFormatPcm && bits == 16) {
|
||||
auto* s = reinterpret_cast<std::int16_t*>(dst);
|
||||
for (std::uint32_t i = 0; i < samples; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < samples; ++i) {
|
||||
int v = static_cast<int>(soft_clip(acc[i]) * 32767.0f);
|
||||
v = v > 32767 ? 32767 : (v < -32768 ? -32768 : v);
|
||||
s[i] = static_cast<std::int16_t>(v);
|
||||
|
||||
@@ -11,14 +11,11 @@
|
||||
#include "coop/tool_paths.hpp"
|
||||
#include "util/utf8.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
namespace coop {
|
||||
namespace {
|
||||
std::wstring to_lower(std::wstring s)
|
||||
{
|
||||
for (wchar_t& c : s)
|
||||
{
|
||||
for (wchar_t& c : s) {
|
||||
c = static_cast<wchar_t>(::towlower(c));
|
||||
}
|
||||
return s;
|
||||
@@ -29,8 +26,7 @@ std::wstring to_lower(std::wstring s)
|
||||
|
||||
AudioOverrideStore::AudioOverrideStore(std::wstring path) : path_(std::move(path))
|
||||
{
|
||||
if (path_.empty())
|
||||
{
|
||||
if (path_.empty()) {
|
||||
path_ = exe_directory() + L"coop_audio_overrides.ini";
|
||||
}
|
||||
}
|
||||
@@ -45,46 +41,38 @@ void AudioOverrideStore::load()
|
||||
{
|
||||
map_.clear();
|
||||
std::ifstream f(path_.c_str());
|
||||
if (!f)
|
||||
{
|
||||
if (!f) {
|
||||
return;
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(f, line))
|
||||
{
|
||||
while (std::getline(f, line)) {
|
||||
// "<image> = <rate> <ch> <bits> <pcm|float>"; skip blank lines and # comments.
|
||||
const std::size_t hash = line.find('#');
|
||||
if (hash != std::string::npos)
|
||||
{
|
||||
if (hash != std::string::npos) {
|
||||
line.resize(hash);
|
||||
}
|
||||
const std::size_t eq = line.find('=');
|
||||
if (eq == std::string::npos)
|
||||
{
|
||||
if (eq == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
std::string name = line.substr(0, eq);
|
||||
// trim trailing/leading whitespace from the name
|
||||
while (!name.empty() && std::isspace(static_cast<unsigned char>(name.back())))
|
||||
{
|
||||
while (!name.empty() && std::isspace(static_cast<unsigned char>(name.back()))) {
|
||||
name.pop_back();
|
||||
}
|
||||
std::size_t b = 0;
|
||||
while (b < name.size() && std::isspace(static_cast<unsigned char>(name[b])))
|
||||
{
|
||||
while (b < name.size() && std::isspace(static_cast<unsigned char>(name[b]))) {
|
||||
++b;
|
||||
}
|
||||
name = name.substr(b);
|
||||
if (name.empty())
|
||||
{
|
||||
if (name.empty()) {
|
||||
continue;
|
||||
}
|
||||
std::istringstream vs(line.substr(eq + 1));
|
||||
AudioFormatOverride fmt;
|
||||
std::string tag;
|
||||
vs >> fmt.rate >> fmt.channels >> fmt.bits >> tag;
|
||||
if (!fmt.valid())
|
||||
{
|
||||
if (!fmt.valid()) {
|
||||
continue;
|
||||
}
|
||||
fmt.format_tag = (tag == "float") ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
|
||||
@@ -95,8 +83,7 @@ void AudioOverrideStore::load()
|
||||
bool AudioOverrideStore::find(const std::wstring& image_name, AudioFormatOverride& out) const
|
||||
{
|
||||
const auto it = map_.find(key_of(image_name));
|
||||
if (it == map_.end())
|
||||
{
|
||||
if (it == map_.end()) {
|
||||
return false;
|
||||
}
|
||||
out = it->second;
|
||||
@@ -106,8 +93,7 @@ bool AudioOverrideStore::find(const std::wstring& image_name, AudioFormatOverrid
|
||||
void AudioOverrideStore::set(const std::wstring& image_name, const AudioFormatOverride& fmt, bool* differed)
|
||||
{
|
||||
const std::wstring key = key_of(image_name);
|
||||
if (differed != nullptr)
|
||||
{
|
||||
if (differed != nullptr) {
|
||||
const auto it = map_.find(key);
|
||||
*differed = (it != map_.end() && it->second != fmt);
|
||||
}
|
||||
@@ -118,14 +104,12 @@ void AudioOverrideStore::set(const std::wstring& image_name, const AudioFormatOv
|
||||
void AudioOverrideStore::save() const
|
||||
{
|
||||
std::ofstream f(path_.c_str(), std::ios::trunc);
|
||||
if (!f)
|
||||
{
|
||||
if (!f) {
|
||||
return;
|
||||
}
|
||||
f << "# CoopAllTheThings per-game audio format overrides (auto-managed)\n";
|
||||
f << "# <image.exe> = <rate> <channels> <bits> <pcm|float>\n";
|
||||
for (const auto& [name, fmt] : map_)
|
||||
{
|
||||
for (const auto& [name, fmt] : map_) {
|
||||
f << narrow(name) << " = " << fmt.rate << ' ' << fmt.channels << ' ' << fmt.bits << ' '
|
||||
<< (fmt.format_tag == WAVE_FORMAT_IEEE_FLOAT ? "float" : "pcm") << '\n';
|
||||
}
|
||||
|
||||
@@ -13,32 +13,23 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct AudioFormatOverride
|
||||
{
|
||||
struct AudioFormatOverride {
|
||||
std::uint32_t rate = 0;
|
||||
std::uint32_t channels = 0;
|
||||
std::uint32_t bits = 0;
|
||||
std::uint32_t format_tag = 0; // WAVE_FORMAT_PCM (1) / WAVE_FORMAT_IEEE_FLOAT (3)
|
||||
|
||||
[[nodiscard]] bool valid() const
|
||||
{
|
||||
return rate != 0 && channels != 0 && bits != 0;
|
||||
}
|
||||
[[nodiscard]] bool valid() const { return rate != 0 && channels != 0 && bits != 0; }
|
||||
bool operator==(const AudioFormatOverride& o) const
|
||||
{
|
||||
return rate == o.rate && channels == o.channels && bits == o.bits && format_tag == o.format_tag;
|
||||
}
|
||||
bool operator!=(const AudioFormatOverride& o) const
|
||||
{
|
||||
return !(*this == o);
|
||||
}
|
||||
bool operator!=(const AudioFormatOverride& o) const { return !(*this == o); }
|
||||
};
|
||||
|
||||
class AudioOverrideStore
|
||||
{
|
||||
class AudioOverrideStore {
|
||||
public:
|
||||
// `path` empty -> default (exe_dir/coop_audio_overrides.ini). Does not load yet.
|
||||
explicit AudioOverrideStore(std::wstring path = {});
|
||||
@@ -52,10 +43,7 @@ public:
|
||||
// existing entry for that game differed from `fmt` (caller warns the operator).
|
||||
void set(const std::wstring& image_name, const AudioFormatOverride& fmt, bool* differed = nullptr);
|
||||
|
||||
[[nodiscard]] const std::wstring& path() const
|
||||
{
|
||||
return path_;
|
||||
}
|
||||
[[nodiscard]] const std::wstring& path() const { return path_; }
|
||||
|
||||
private:
|
||||
static std::wstring key_of(const std::wstring& image_name); // lowercased basename
|
||||
|
||||
@@ -6,15 +6,12 @@
|
||||
#include <audioclientactivationparams.h>
|
||||
#include <mmdeviceapi.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
namespace coop {
|
||||
namespace {
|
||||
|
||||
// Completion handler for ActivateAudioInterfaceAsync. The call is async even when
|
||||
// used synchronously: it signals `done`, and the caller waits on it.
|
||||
class ActivateHandler : public IActivateAudioInterfaceCompletionHandler
|
||||
{
|
||||
class ActivateHandler : public IActivateAudioInterfaceCompletionHandler {
|
||||
public:
|
||||
HANDLE done = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
HRESULT result = E_FAIL;
|
||||
@@ -25,16 +22,13 @@ public:
|
||||
HRESULT activate_hr = E_FAIL;
|
||||
IUnknown* punk = nullptr;
|
||||
HRESULT hr = op->GetActivateResult(&activate_hr, &punk);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
if (SUCCEEDED(hr)) {
|
||||
hr = activate_hr;
|
||||
}
|
||||
if (SUCCEEDED(hr) && punk)
|
||||
{
|
||||
if (SUCCEEDED(hr) && punk) {
|
||||
hr = punk->QueryInterface(__uuidof(IAudioClient), reinterpret_cast<void**>(&client));
|
||||
}
|
||||
if (punk)
|
||||
{
|
||||
if (punk) {
|
||||
punk->Release();
|
||||
}
|
||||
result = hr;
|
||||
@@ -44,16 +38,14 @@ public:
|
||||
|
||||
STDMETHODIMP QueryInterface(REFIID riid, void** ppv) override
|
||||
{
|
||||
if (riid == __uuidof(IUnknown) || riid == __uuidof(IActivateAudioInterfaceCompletionHandler))
|
||||
{
|
||||
if (riid == __uuidof(IUnknown) || riid == __uuidof(IActivateAudioInterfaceCompletionHandler)) {
|
||||
*ppv = static_cast<IActivateAudioInterfaceCompletionHandler*>(this);
|
||||
AddRef();
|
||||
return S_OK;
|
||||
}
|
||||
// Mark the handler agile; ActivateAudioInterfaceAsync requires an agile
|
||||
// completion handler and otherwise rejects the call (E_ILLEGAL_METHOD_CALL).
|
||||
if (riid == __uuidof(IAgileObject))
|
||||
{
|
||||
if (riid == __uuidof(IAgileObject)) {
|
||||
*ppv = static_cast<IUnknown*>(this);
|
||||
AddRef();
|
||||
return S_OK;
|
||||
@@ -61,15 +53,11 @@ public:
|
||||
*ppv = nullptr;
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
STDMETHODIMP_(ULONG) AddRef() override
|
||||
{
|
||||
return ++ref_;
|
||||
}
|
||||
STDMETHODIMP_(ULONG) AddRef() override { return ++ref_; }
|
||||
STDMETHODIMP_(ULONG) Release() override
|
||||
{
|
||||
const ULONG r = --ref_;
|
||||
if (r == 0)
|
||||
{
|
||||
if (r == 0) {
|
||||
delete this;
|
||||
}
|
||||
return r;
|
||||
@@ -78,8 +66,7 @@ public:
|
||||
private:
|
||||
~ActivateHandler()
|
||||
{
|
||||
if (done)
|
||||
{
|
||||
if (done) {
|
||||
CloseHandle(done);
|
||||
}
|
||||
}
|
||||
@@ -100,26 +87,20 @@ HRESULT activate_loopback_client(DWORD pid, IAudioClient** out)
|
||||
|
||||
auto* handler = new ActivateHandler();
|
||||
HRESULT hr = E_FAIL;
|
||||
if (handler->done)
|
||||
{
|
||||
if (handler->done) {
|
||||
IActivateAudioInterfaceAsyncOperation* op = nullptr;
|
||||
hr = ActivateAudioInterfaceAsync(VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, __uuidof(IAudioClient),
|
||||
&pv, handler, &op);
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
hr = ActivateAudioInterfaceAsync(VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, __uuidof(IAudioClient), &pv, handler,
|
||||
&op);
|
||||
if (SUCCEEDED(hr)) {
|
||||
WaitForSingleObject(handler->done, INFINITE);
|
||||
hr = handler->result;
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
if (SUCCEEDED(hr)) {
|
||||
*out = handler->client; // transfer the QueryInterface reference
|
||||
}
|
||||
else if (handler->client)
|
||||
{
|
||||
} else if (handler->client) {
|
||||
handler->client->Release();
|
||||
}
|
||||
}
|
||||
if (op)
|
||||
{
|
||||
if (op) {
|
||||
op->Release();
|
||||
}
|
||||
}
|
||||
@@ -132,19 +113,16 @@ HRESULT activate_loopback_client(DWORD pid, IAudioClient** out)
|
||||
WAVEFORMATEX* default_render_format()
|
||||
{
|
||||
IMMDeviceEnumerator* enumerator = nullptr;
|
||||
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL,
|
||||
__uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&enumerator))))
|
||||
{
|
||||
if (FAILED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
|
||||
reinterpret_cast<void**>(&enumerator)))) {
|
||||
return nullptr;
|
||||
}
|
||||
IMMDevice* endpoint = nullptr;
|
||||
WAVEFORMATEX* fmt = nullptr;
|
||||
if (SUCCEEDED(enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &endpoint)))
|
||||
{
|
||||
if (SUCCEEDED(enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &endpoint))) {
|
||||
IAudioClient* client = nullptr;
|
||||
if (SUCCEEDED(endpoint->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr,
|
||||
reinterpret_cast<void**>(&client))))
|
||||
{
|
||||
if (SUCCEEDED(
|
||||
endpoint->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&client)))) {
|
||||
client->GetMixFormat(&fmt);
|
||||
client->Release();
|
||||
}
|
||||
@@ -174,14 +152,12 @@ void ProcessLoopbackCapture::set_status(std::string s)
|
||||
bool ProcessLoopbackCapture::start(DWORD pid, const WAVEFORMATEX* format, FrameSink sink)
|
||||
{
|
||||
stop();
|
||||
if (!pid || !format)
|
||||
{
|
||||
if (!pid || !format) {
|
||||
set_status("No target/format.");
|
||||
return false;
|
||||
}
|
||||
stop_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!stop_event_)
|
||||
{
|
||||
if (!stop_event_) {
|
||||
set_status("CreateEvent failed.");
|
||||
return false;
|
||||
}
|
||||
@@ -192,23 +168,19 @@ bool ProcessLoopbackCapture::start(DWORD pid, const WAVEFORMATEX* format, FrameS
|
||||
std::memcpy(fmt_copy.data(), format, fmt_copy.size());
|
||||
|
||||
set_status("Starting…");
|
||||
thread_ = std::thread(&ProcessLoopbackCapture::thread_main, this, pid, std::move(fmt_copy),
|
||||
std::move(sink));
|
||||
thread_ = std::thread(&ProcessLoopbackCapture::thread_main, this, pid, std::move(fmt_copy), std::move(sink));
|
||||
return true;
|
||||
}
|
||||
|
||||
void ProcessLoopbackCapture::stop()
|
||||
{
|
||||
if (stop_event_)
|
||||
{
|
||||
if (stop_event_) {
|
||||
SetEvent(stop_event_);
|
||||
}
|
||||
if (thread_.joinable())
|
||||
{
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
if (stop_event_)
|
||||
{
|
||||
if (stop_event_) {
|
||||
CloseHandle(stop_event_);
|
||||
stop_event_ = nullptr;
|
||||
}
|
||||
@@ -231,18 +203,15 @@ void ProcessLoopbackCapture::thread_main(DWORD pid, std::vector<BYTE> format, Fr
|
||||
set_status(buf);
|
||||
};
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
HRESULT hr = activate_loopback_client(pid, &client);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("Process loopback activate", hr);
|
||||
break;
|
||||
}
|
||||
|
||||
capture_event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
if (!capture_event)
|
||||
{
|
||||
if (!capture_event) {
|
||||
fail("CreateEvent(capture)", HRESULT_FROM_WIN32(GetLastError()));
|
||||
break;
|
||||
}
|
||||
@@ -250,27 +219,22 @@ void ProcessLoopbackCapture::thread_main(DWORD pid, std::vector<BYTE> format, Fr
|
||||
// Process loopback requires shared mode, the LOOPBACK + EVENTCALLBACK flags,
|
||||
// and zero buffer/periodicity (there is no device period to query).
|
||||
hr = client->Initialize(AUDCLNT_SHAREMODE_SHARED,
|
||||
AUDCLNT_STREAMFLAGS_LOOPBACK | AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 0, 0,
|
||||
fmt, nullptr);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
AUDCLNT_STREAMFLAGS_LOOPBACK | AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 0, 0, fmt, nullptr);
|
||||
if (FAILED(hr)) {
|
||||
fail("Capture Initialize", hr);
|
||||
break;
|
||||
}
|
||||
hr = client->SetEventHandle(capture_event);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("Capture SetEventHandle", hr);
|
||||
break;
|
||||
}
|
||||
hr = client->GetService(__uuidof(IAudioCaptureClient), reinterpret_cast<void**>(&capture));
|
||||
if (FAILED(hr))
|
||||
{
|
||||
if (FAILED(hr)) {
|
||||
fail("GetService(CaptureClient)", hr);
|
||||
break;
|
||||
}
|
||||
if (FAILED(hr = client->Start()))
|
||||
{
|
||||
if (FAILED(hr = client->Start())) {
|
||||
fail("Capture Start", hr);
|
||||
break;
|
||||
}
|
||||
@@ -279,47 +243,38 @@ void ProcessLoopbackCapture::thread_main(DWORD pid, std::vector<BYTE> format, Fr
|
||||
running_.store(true, std::memory_order_release);
|
||||
|
||||
HANDLE waits[2] = {stop_event_, capture_event};
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
const DWORD w = WaitForMultipleObjects(2, waits, FALSE, 200);
|
||||
if (w == WAIT_OBJECT_0)
|
||||
{
|
||||
if (w == WAIT_OBJECT_0) {
|
||||
break;
|
||||
}
|
||||
|
||||
UINT32 packet = 0;
|
||||
while (SUCCEEDED(capture->GetNextPacketSize(&packet)) && packet > 0)
|
||||
{
|
||||
while (SUCCEEDED(capture->GetNextPacketSize(&packet)) && packet > 0) {
|
||||
BYTE* data = nullptr;
|
||||
UINT32 frames = 0;
|
||||
DWORD flags = 0;
|
||||
if (FAILED(capture->GetBuffer(&data, &frames, &flags, nullptr, nullptr)))
|
||||
{
|
||||
if (FAILED(capture->GetBuffer(&data, &frames, &flags, nullptr, nullptr))) {
|
||||
break;
|
||||
}
|
||||
const bool silent = (flags & AUDCLNT_BUFFERFLAGS_SILENT) != 0;
|
||||
frames_captured_.fetch_add(frames, std::memory_order_relaxed);
|
||||
if (!silent && frames > 0)
|
||||
{
|
||||
if (!silent && frames > 0) {
|
||||
// Count frames that carry any non-zero sample.
|
||||
const BYTE* p = data;
|
||||
const BYTE* end = data + static_cast<size_t>(frames) * frame_bytes;
|
||||
bool any = false;
|
||||
for (; p < end; ++p)
|
||||
{
|
||||
if (*p != 0)
|
||||
{
|
||||
for (; p < end; ++p) {
|
||||
if (*p != 0) {
|
||||
any = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (any)
|
||||
{
|
||||
if (any) {
|
||||
nonsilent_frames_.fetch_add(frames, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
if (sink)
|
||||
{
|
||||
if (sink) {
|
||||
sink(data, frames, silent);
|
||||
}
|
||||
capture->ReleaseBuffer(frames);
|
||||
@@ -329,26 +284,21 @@ void ProcessLoopbackCapture::thread_main(DWORD pid, std::vector<BYTE> format, Fr
|
||||
client->Stop();
|
||||
} while (false);
|
||||
|
||||
if (running_.load(std::memory_order_acquire))
|
||||
{
|
||||
if (running_.load(std::memory_order_acquire)) {
|
||||
running_.store(false, std::memory_order_release);
|
||||
set_status("Stopped.");
|
||||
}
|
||||
|
||||
if (capture)
|
||||
{
|
||||
if (capture) {
|
||||
capture->Release();
|
||||
}
|
||||
if (client)
|
||||
{
|
||||
if (client) {
|
||||
client->Release();
|
||||
}
|
||||
if (capture_event)
|
||||
{
|
||||
if (capture_event) {
|
||||
CloseHandle(capture_event);
|
||||
}
|
||||
if (com_ok)
|
||||
{
|
||||
if (com_ok) {
|
||||
CoUninitialize();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,15 +15,13 @@
|
||||
|
||||
#include <mmreg.h> // WAVEFORMATEX
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Default render endpoint mix format (caller owns the returned pointer; free with
|
||||
// CoTaskMemFree). Returns nullptr on failure. Requires a COM-initialized thread.
|
||||
WAVEFORMATEX* default_render_format();
|
||||
|
||||
class ProcessLoopbackCapture
|
||||
{
|
||||
class ProcessLoopbackCapture {
|
||||
public:
|
||||
// Called on the capture thread for each delivered packet. `silent` means the
|
||||
// engine flagged the packet as silence (data may be undefined).
|
||||
@@ -41,20 +39,11 @@ public:
|
||||
bool start(DWORD pid, const WAVEFORMATEX* format, FrameSink sink);
|
||||
void stop();
|
||||
|
||||
[[nodiscard]] bool running() const
|
||||
{
|
||||
return running_.load(std::memory_order_acquire);
|
||||
}
|
||||
[[nodiscard]] bool running() const { return running_.load(std::memory_order_acquire); }
|
||||
[[nodiscard]] std::string status() const;
|
||||
|
||||
[[nodiscard]] std::uint64_t frames_captured() const
|
||||
{
|
||||
return frames_captured_.load(std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] std::uint64_t nonsilent_frames() const
|
||||
{
|
||||
return nonsilent_frames_.load(std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] std::uint64_t frames_captured() const { return frames_captured_.load(std::memory_order_relaxed); }
|
||||
[[nodiscard]] std::uint64_t nonsilent_frames() const { return nonsilent_frames_.load(std::memory_order_relaxed); }
|
||||
|
||||
private:
|
||||
void thread_main(DWORD pid, std::vector<BYTE> format, FrameSink sink);
|
||||
|
||||
@@ -22,11 +22,9 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct RenderPacer
|
||||
{
|
||||
struct RenderPacer {
|
||||
std::uint32_t prime_frames = 0; // cushion to (re)build before playback resumes
|
||||
bool primed = false;
|
||||
|
||||
@@ -37,28 +35,22 @@ struct RenderPacer
|
||||
// Returns the frame count to write (0 while still priming or when the ring is empty).
|
||||
std::uint32_t pump(std::uint32_t avail, std::uint32_t have, std::uint32_t padding)
|
||||
{
|
||||
if (!primed && have >= prime_frames)
|
||||
{
|
||||
if (!primed && have >= prime_frames) {
|
||||
primed = true;
|
||||
}
|
||||
if (!primed)
|
||||
{
|
||||
if (!primed) {
|
||||
return 0; // still building the initial / post-starvation cushion
|
||||
}
|
||||
const std::uint32_t to_write = std::min(avail, have);
|
||||
// Genuine starvation only: the device emptied and the ring has nothing to give.
|
||||
// A partial fill (have < avail) is normal jitter and must NOT trigger a re-prime.
|
||||
if (padding == 0 && have == 0)
|
||||
{
|
||||
if (padding == 0 && have == 0) {
|
||||
primed = false;
|
||||
}
|
||||
return to_write;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
primed = false;
|
||||
}
|
||||
void reset() { primed = false; }
|
||||
};
|
||||
|
||||
} // namespace coop
|
||||
|
||||
@@ -10,11 +10,9 @@
|
||||
#include "ui/app_chrome.hpp"
|
||||
#include "util/utf8.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
const ImVec4 kGreen(0.4f, 1.0f, 0.4f, 1.0f);
|
||||
const ImVec4 kAmber(1.0f, 0.8f, 0.3f, 1.0f);
|
||||
@@ -22,8 +20,7 @@ const ImVec4 kRed(1.0f, 0.45f, 0.4f, 1.0f);
|
||||
|
||||
const char* format_tag_name(std::uint32_t tag)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
switch (tag) {
|
||||
case WAVE_FORMAT_PCM:
|
||||
return "PCM";
|
||||
case WAVE_FORMAT_IEEE_FLOAT:
|
||||
@@ -38,8 +35,7 @@ const char* format_tag_name(std::uint32_t tag)
|
||||
// How the hooked backend learned a stream's format (drives the pitch correctness).
|
||||
const char* audio_format_state_name(std::uint32_t state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
switch (state) {
|
||||
case AudioFormat_Exact:
|
||||
return "known (from game)";
|
||||
case AudioFormat_Measuring:
|
||||
@@ -57,8 +53,7 @@ const char* audio_format_state_name(std::uint32_t state)
|
||||
|
||||
ImVec4 audio_format_state_color(std::uint32_t state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
switch (state) {
|
||||
case AudioFormat_Exact:
|
||||
case AudioFormat_Measured:
|
||||
case AudioFormat_Override:
|
||||
@@ -81,20 +76,17 @@ std::string image_basename(const std::wstring& image_path)
|
||||
|
||||
std::wstring AudioPanel::image_name_from_pid(DWORD pid)
|
||||
{
|
||||
if (pid == 0)
|
||||
{
|
||||
if (pid == 0) {
|
||||
return {};
|
||||
}
|
||||
HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
|
||||
if (h == nullptr)
|
||||
{
|
||||
if (h == nullptr) {
|
||||
return {};
|
||||
}
|
||||
wchar_t buf[MAX_PATH] = {};
|
||||
DWORD n = MAX_PATH;
|
||||
std::wstring name;
|
||||
if (QueryFullProcessImageNameW(h, 0, buf, &n))
|
||||
{
|
||||
if (QueryFullProcessImageNameW(h, 0, buf, &n)) {
|
||||
name.assign(buf, n);
|
||||
}
|
||||
CloseHandle(h);
|
||||
@@ -110,24 +102,20 @@ void AudioPanel::manage_overrides(const HookStatusView& status, DWORD pid)
|
||||
override_applied_ = false;
|
||||
exact_saved_ = false;
|
||||
}
|
||||
if (pid == 0 || target_image_.empty() || !mirror_.running() || status.audio_streams_seen == 0)
|
||||
{
|
||||
if (pid == 0 || target_image_.empty() || !mirror_.running() || status.audio_streams_seen == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const AudioStreamInfo& s = status.audio_streams[0]; // the primary (mirrored) stream
|
||||
const std::uint32_t st = s.format_state;
|
||||
if (st == AudioFormat_Exact)
|
||||
{
|
||||
if (st == AudioFormat_Exact) {
|
||||
// Ground truth: persist it as this game's override (so a later late-attach is fixed).
|
||||
if (!exact_saved_)
|
||||
{
|
||||
if (!exact_saved_) {
|
||||
exact_saved_ = true;
|
||||
const AudioFormatOverride fmt{s.sample_rate, s.channels, s.bits, s.format_tag};
|
||||
bool differed = false;
|
||||
overrides_.set(target_image_, fmt, &differed);
|
||||
if (differed && logger_)
|
||||
{
|
||||
if (differed && logger_) {
|
||||
char msg[160];
|
||||
std::snprintf(msg, sizeof(msg),
|
||||
"%s: exact format %uHz/%uch/%ubit caught -> replaced a DIFFERING saved override",
|
||||
@@ -135,19 +123,14 @@ void AudioPanel::manage_overrides(const HookStatusView& status, DWORD pid)
|
||||
logger_(LogLevel_Warn, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (st == AudioFormat_Measuring || st == AudioFormat_Measured || st == AudioFormat_LowConfidence)
|
||||
{
|
||||
} else if (st == AudioFormat_Measuring || st == AudioFormat_Measured || st == AudioFormat_LowConfidence) {
|
||||
// A guessed stream: if we have a saved override for this game, apply it.
|
||||
if (!override_applied_)
|
||||
{
|
||||
if (!override_applied_) {
|
||||
override_applied_ = true;
|
||||
AudioFormatOverride ov;
|
||||
if (overrides_.find(target_image_, ov))
|
||||
{
|
||||
if (overrides_.find(target_image_, ov)) {
|
||||
mirror_.request_op(0, AudioRingOp_Override, ov.rate, ov.channels, ov.bits, ov.format_tag);
|
||||
if (logger_)
|
||||
{
|
||||
if (logger_) {
|
||||
char msg[160];
|
||||
std::snprintf(msg, sizeof(msg), "%s: applied saved audio override %uHz/%uch/%ubit",
|
||||
image_basename(target_image_).c_str(), ov.rate, ov.channels, ov.bits);
|
||||
@@ -161,12 +144,10 @@ void AudioPanel::manage_overrides(const HookStatusView& status, DWORD pid)
|
||||
void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
{
|
||||
DWORD pid = 0;
|
||||
if (target_ != nullptr && IsWindow(target_))
|
||||
{
|
||||
if (target_ != nullptr && IsWindow(target_)) {
|
||||
GetWindowThreadProcessId(target_, &pid);
|
||||
}
|
||||
if (dev_pid_ != 0)
|
||||
{
|
||||
if (dev_pid_ != 0) {
|
||||
pid = dev_pid_; // test harness: a windowless target (e.g. coop_tone) has no HWND
|
||||
}
|
||||
const bool have_target = pid != 0;
|
||||
@@ -175,26 +156,20 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
ImGui::Begin("Audio mirror");
|
||||
|
||||
ImGui::BeginDisabled(!have_target);
|
||||
if (ImGui::Checkbox("Mirror game audio", &enabled_))
|
||||
{
|
||||
if (!enabled_)
|
||||
{
|
||||
if (ImGui::Checkbox("Mirror game audio", &enabled_)) {
|
||||
if (!enabled_) {
|
||||
mirror_.stop();
|
||||
}
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
if (!have_target)
|
||||
{
|
||||
if (!have_target) {
|
||||
ImGui::TextDisabled("Inject into a game first (its audio is the source).");
|
||||
}
|
||||
|
||||
// Start when enabled and the target process changes; stop if it disappears.
|
||||
if (enabled_ && pid != 0 && mirror_.target_pid() != pid)
|
||||
{
|
||||
if (enabled_ && pid != 0 && mirror_.target_pid() != pid) {
|
||||
mirror_.start(pid);
|
||||
}
|
||||
else if (enabled_ && pid == 0 && mirror_.target_pid() != 0)
|
||||
{
|
||||
} else if (enabled_ && pid == 0 && mirror_.target_pid() != 0) {
|
||||
mirror_.stop();
|
||||
}
|
||||
|
||||
@@ -214,14 +189,12 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
demo_ ? std::string("render-hook did not publish a format in time; using WASAPI process loopback.")
|
||||
: mirror_.fallback_reason();
|
||||
|
||||
if (running)
|
||||
{
|
||||
if (running) {
|
||||
const bool hooked = src == AudioMirror::Source::Hooked;
|
||||
ImGui::TextColored(kGreen, "Mirroring %u Hz, %u ch", m_rate, m_ch);
|
||||
ImGui::Text("Source:");
|
||||
ImGui::SameLine();
|
||||
if (hooked)
|
||||
{
|
||||
if (hooked) {
|
||||
// The hooked path only silences (no echo) an EXACT / override format, whose frame
|
||||
// size is known. A guessed stream is captured but not silenced (silencing a guessed
|
||||
// buffer could over-write it), so the game stays audible -- an echo. Make that clear.
|
||||
@@ -229,9 +202,7 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
const bool no_echo = (st == AudioFormat_Exact || st == AudioFormat_Override);
|
||||
ImGui::TextColored(no_echo ? kGreen : kAmber, "Hooked (%s)",
|
||||
no_echo ? "no echo" : "echo -- guessed format");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextColored(kAmber, "%s", demo_ ? "Loopback (echo)" : mirror_.source_name());
|
||||
}
|
||||
|
||||
@@ -240,26 +211,21 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
// captured post-mix at the device endpoint format, so it's always known-correct.
|
||||
ImGui::Text("Format:");
|
||||
ImGui::SameLine();
|
||||
if (hooked)
|
||||
{
|
||||
if (hooked) {
|
||||
const std::uint32_t st = status.audio_streams[0].format_state; // [0] is the primary
|
||||
ImGui::TextColored(audio_format_state_color(st), "%s", audio_format_state_name(st));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextColored(kGreen, "device endpoint (known, post-mix)");
|
||||
}
|
||||
ImGui::Text("Buffered: %4u ms", m_buffered);
|
||||
}
|
||||
if (!mirror_status.empty())
|
||||
{
|
||||
if (!mirror_status.empty()) {
|
||||
ImGui::TextWrapped("%s", mirror_status.c_str());
|
||||
}
|
||||
|
||||
// Why we're on loopback instead of the no-echo hooked path (empty when hooked). Amber
|
||||
// because it's a degraded-but-working state that auto-resolves when the hook catches up.
|
||||
if (!reason.empty())
|
||||
{
|
||||
if (!reason.empty()) {
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, kAmber);
|
||||
ImGui::TextWrapped("Why loopback: %s", reason.c_str());
|
||||
ImGui::PopStyleColor();
|
||||
@@ -267,25 +233,18 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
|
||||
// Only the loopback path leaves the game audible locally (the echo); the
|
||||
// hooked path silences it, so don't warn there.
|
||||
if (src == AudioMirror::Source::Loopback)
|
||||
{
|
||||
if (src == AudioMirror::Source::Loopback) {
|
||||
bool audio_hook_on = false;
|
||||
const std::uint32_t hn =
|
||||
status.hook_entry_count < kMaxHookEntries ? status.hook_entry_count : kMaxHookEntries;
|
||||
for (std::uint32_t i = 0; i < hn; ++i)
|
||||
{
|
||||
if (status.hook_entries[i].subsystem == HookSubsys_Audio && status.hook_entries[i].installed)
|
||||
{
|
||||
const std::uint32_t hn = status.hook_entry_count < kMaxHookEntries ? status.hook_entry_count : kMaxHookEntries;
|
||||
for (std::uint32_t i = 0; i < hn; ++i) {
|
||||
if (status.hook_entries[i].subsystem == HookSubsys_Audio && status.hook_entries[i].installed) {
|
||||
audio_hook_on = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (audio_hook_on)
|
||||
{
|
||||
if (audio_hook_on) {
|
||||
ImGui::TextDisabled("Game audio also plays locally (echo).");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextDisabled("Audio render-hook is off -> loopback (echo). Enable it in the Injection panel.");
|
||||
}
|
||||
}
|
||||
@@ -296,14 +255,12 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
// (debug details) shows each stream's format/provenance/activity.
|
||||
ImGui::Separator();
|
||||
ImGui::Text("Render streams: %u", status.audio_streams_seen);
|
||||
if (status.audio_streams_seen > kMaxAudioStreams)
|
||||
{
|
||||
if (status.audio_streams_seen > kMaxAudioStreams) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("(showing first %u)", kMaxAudioStreams);
|
||||
}
|
||||
|
||||
if (!debug_details)
|
||||
{
|
||||
if (!debug_details) {
|
||||
record_panel_fit("Audio");
|
||||
ImGui::End();
|
||||
return; // the per-stream table below is diagnostic detail
|
||||
@@ -312,9 +269,7 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
const std::uint32_t rows = std::min<std::uint32_t>(status.audio_streams_seen, kMaxAudioStreams);
|
||||
const double now = ImGui::GetTime();
|
||||
const bool resample = (now - rate_base_time_) >= 0.5; // recompute frames/s ~2x a second
|
||||
if (rows > 0 &&
|
||||
ImGui::BeginTable("audio_streams", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit))
|
||||
{
|
||||
if (rows > 0 && ImGui::BeginTable("audio_streams", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit)) {
|
||||
ImGui::TableSetupColumn("#");
|
||||
ImGui::TableSetupColumn("role");
|
||||
ImGui::TableSetupColumn("format");
|
||||
@@ -322,23 +277,19 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
ImGui::TableSetupColumn("frames");
|
||||
ImGui::TableSetupColumn("live");
|
||||
ImGui::TableHeadersRow();
|
||||
for (std::uint32_t i = 0; i < rows; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < rows; ++i) {
|
||||
const AudioStreamInfo& s = status.audio_streams[i];
|
||||
|
||||
// Debounced activity: remember when this stream last advanced, and call it
|
||||
// live for a short window afterwards so bursty releases don't flicker.
|
||||
if (s.frames_rendered > prev_frames_[i])
|
||||
{
|
||||
if (s.frames_rendered > prev_frames_[i]) {
|
||||
last_active_[i] = now;
|
||||
}
|
||||
prev_frames_[i] = s.frames_rendered;
|
||||
const bool live = last_active_[i] > 0.0 && (now - last_active_[i]) < 0.4;
|
||||
if (resample)
|
||||
{
|
||||
if (resample) {
|
||||
const double dt = now - rate_base_time_;
|
||||
frames_per_s_[i] =
|
||||
dt > 0.0 ? static_cast<double>(s.frames_rendered - rate_base_frames_[i]) / dt : 0.0;
|
||||
frames_per_s_[i] = dt > 0.0 ? static_cast<double>(s.frames_rendered - rate_base_frames_[i]) / dt : 0.0;
|
||||
rate_base_frames_[i] = s.frames_rendered;
|
||||
}
|
||||
|
||||
@@ -346,43 +297,35 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%u", i);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextColored(s.is_primary ? ImVec4(0.4f, 1.0f, 0.4f, 1.0f) : ImVec4(0.7f, 0.7f, 0.7f, 1.0f),
|
||||
"%s", s.is_primary ? "primary" : "extra");
|
||||
ImGui::TextColored(s.is_primary ? ImVec4(0.4f, 1.0f, 0.4f, 1.0f) : ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "%s",
|
||||
s.is_primary ? "primary" : "extra");
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%u Hz %uch %u-bit %s", s.sample_rate, s.channels, s.bits,
|
||||
format_tag_name(s.format_tag));
|
||||
ImGui::Text("%u Hz %uch %u-bit %s", s.sample_rate, s.channels, s.bits, format_tag_name(s.format_tag));
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextColored(audio_format_state_color(s.format_state), "%s",
|
||||
audio_format_state_name(s.format_state));
|
||||
ImGui::TextColored(audio_format_state_color(s.format_state), "%s", audio_format_state_name(s.format_state));
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%llu", static_cast<unsigned long long>(s.frames_rendered));
|
||||
ImGui::TableNextColumn();
|
||||
if (live)
|
||||
{
|
||||
if (live) {
|
||||
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "live");
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("%6.0f/s", frames_per_s_[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextDisabled("idle");
|
||||
}
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
if (resample)
|
||||
{
|
||||
if (resample) {
|
||||
rate_base_time_ = now;
|
||||
}
|
||||
|
||||
// --- Operator controls: re-measure / override the primary stream's format -----
|
||||
// For when detection is wrong (re-measure) or unrecoverable (override the channels/
|
||||
// bit-depth the hook had to assume). Only meaningful while mirroring is active.
|
||||
if (running)
|
||||
{
|
||||
if (running) {
|
||||
ImGui::SeparatorText("Fix the primary stream (debug)");
|
||||
if (ImGui::Button("Re-measure rate"))
|
||||
{
|
||||
if (ImGui::Button("Re-measure rate")) {
|
||||
mirror_.request_op(0, AudioRingOp_Remeasure);
|
||||
}
|
||||
ImGui::SameLine();
|
||||
@@ -395,25 +338,26 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
|
||||
ImGui::InputInt("ch", &ov_channels_, 0, 0);
|
||||
ImGui::SameLine();
|
||||
ImGui::SetNextItemWidth(90.0f);
|
||||
ImGui::Combo("##ovbits", &ov_bits_idx_, "16-bit\0" "32-bit\0");
|
||||
ImGui::Combo("##ovbits", &ov_bits_idx_,
|
||||
"16-bit\0"
|
||||
"32-bit\0");
|
||||
ImGui::SameLine();
|
||||
ImGui::SetNextItemWidth(80.0f);
|
||||
ImGui::Combo("##ovfmt", &ov_fmt_idx_, "PCM\0" "float\0");
|
||||
ImGui::Combo("##ovfmt", &ov_fmt_idx_,
|
||||
"PCM\0"
|
||||
"float\0");
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Override"))
|
||||
{
|
||||
if (ImGui::Button("Override")) {
|
||||
ov_rate_ = std::clamp(ov_rate_, 8000, 384000);
|
||||
ov_channels_ = std::clamp(ov_channels_, 1, 8);
|
||||
const std::uint32_t bits = ov_bits_idx_ == 0 ? 16u : 32u;
|
||||
const std::uint32_t tag =
|
||||
ov_fmt_idx_ == 1 ? static_cast<std::uint32_t>(WAVE_FORMAT_IEEE_FLOAT)
|
||||
const std::uint32_t tag = ov_fmt_idx_ == 1 ? static_cast<std::uint32_t>(WAVE_FORMAT_IEEE_FLOAT)
|
||||
: static_cast<std::uint32_t>(WAVE_FORMAT_PCM);
|
||||
const AudioFormatOverride fmt{static_cast<std::uint32_t>(ov_rate_),
|
||||
static_cast<std::uint32_t>(ov_channels_), bits, tag};
|
||||
mirror_.request_op(0, AudioRingOp_Override, fmt.rate, fmt.channels, fmt.bits, fmt.format_tag);
|
||||
// Persist it for this game so the correction sticks across launches.
|
||||
if (!target_image_.empty())
|
||||
{
|
||||
if (!target_image_.empty()) {
|
||||
overrides_.set(target_image_, fmt);
|
||||
override_applied_ = true; // don't let manage_overrides re-apply an older saved value
|
||||
}
|
||||
|
||||
@@ -13,30 +13,19 @@
|
||||
#include "audio/audio_overrides.hpp"
|
||||
#include "ipc/ipc_server.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class AudioPanel
|
||||
{
|
||||
class AudioPanel {
|
||||
public:
|
||||
AudioPanel()
|
||||
{
|
||||
overrides_.load();
|
||||
}
|
||||
AudioPanel() { overrides_.load(); }
|
||||
|
||||
// The window whose process audio to mirror (0 if none); typically the
|
||||
// injected game's HWND.
|
||||
void set_target(HWND target)
|
||||
{
|
||||
target_ = target;
|
||||
}
|
||||
void set_target(HWND target) { target_ = target; }
|
||||
|
||||
// Wire a sink for host-side log lines (override-overwrite warnings etc.). main
|
||||
// connects this to the injection panel's Log-window channel.
|
||||
void set_logger(std::function<void(std::uint32_t, const char*)> logger)
|
||||
{
|
||||
logger_ = std::move(logger);
|
||||
}
|
||||
void set_logger(std::function<void(std::uint32_t, const char*)> logger) { logger_ = std::move(logger); }
|
||||
|
||||
// `status` is the hook's back-channel, for the render-stream view. With
|
||||
// `debug_details` on, the per-stream table is shown.
|
||||
@@ -46,47 +35,23 @@ public:
|
||||
// loopback mirror were running with long status/reason strings -- without a live
|
||||
// AudioMirror, so the fit test can measure the panel's worst-case size. Never set in
|
||||
// the shipping host (the render path is identical, just fed synthetic values).
|
||||
void dev_set_demo(bool on)
|
||||
{
|
||||
demo_ = on;
|
||||
}
|
||||
void dev_set_demo(bool on) { demo_ = on; }
|
||||
|
||||
#ifdef COOP_TEST_HARNESS
|
||||
// Test-harness hooks (debug builds only): drive the real audio code paths and read
|
||||
// state back, incl. targeting a windowless process by pid (coop_tone has no window).
|
||||
void dev_set_enabled(bool on)
|
||||
{
|
||||
enabled_ = on;
|
||||
}
|
||||
void dev_set_pid(DWORD pid)
|
||||
{
|
||||
dev_pid_ = pid;
|
||||
}
|
||||
void dev_request_op(unsigned slot, std::uint32_t kind, std::uint32_t rate, std::uint32_t ch,
|
||||
std::uint32_t bits, std::uint32_t tag)
|
||||
void dev_set_enabled(bool on) { enabled_ = on; }
|
||||
void dev_set_pid(DWORD pid) { dev_pid_ = pid; }
|
||||
void dev_request_op(unsigned slot, std::uint32_t kind, std::uint32_t rate, std::uint32_t ch, std::uint32_t bits,
|
||||
std::uint32_t tag)
|
||||
{
|
||||
mirror_.request_op(slot, kind, rate, ch, bits, tag);
|
||||
}
|
||||
[[nodiscard]] bool dev_running() const
|
||||
{
|
||||
return mirror_.running();
|
||||
}
|
||||
[[nodiscard]] unsigned dev_rate() const
|
||||
{
|
||||
return mirror_.sample_rate();
|
||||
}
|
||||
[[nodiscard]] unsigned dev_channels() const
|
||||
{
|
||||
return mirror_.channels();
|
||||
}
|
||||
[[nodiscard]] std::string dev_source() const
|
||||
{
|
||||
return mirror_.source_name();
|
||||
}
|
||||
[[nodiscard]] std::string dev_reason() const
|
||||
{
|
||||
return mirror_.fallback_reason();
|
||||
}
|
||||
[[nodiscard]] bool dev_running() const { return mirror_.running(); }
|
||||
[[nodiscard]] unsigned dev_rate() const { return mirror_.sample_rate(); }
|
||||
[[nodiscard]] unsigned dev_channels() const { return mirror_.channels(); }
|
||||
[[nodiscard]] std::string dev_source() const { return mirror_.source_name(); }
|
||||
[[nodiscard]] std::string dev_reason() const { return mirror_.fallback_reason(); }
|
||||
#endif
|
||||
|
||||
private:
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
|
||||
#include <dxgiformat.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Map an sRGB DXGI format to its plain UNORM sibling (same byte layout / type
|
||||
// group), leaving non-sRGB formats unchanged.
|
||||
@@ -19,8 +18,7 @@ namespace coop
|
||||
// (the producer's sRGB texture -> the host's UNORM copy) is allowed.
|
||||
inline DXGI_FORMAT srgb_to_unorm(DXGI_FORMAT format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
switch (format) {
|
||||
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
|
||||
return DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
|
||||
|
||||
@@ -6,11 +6,9 @@
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
// Fullscreen triangle generated from SV_VertexID -- no vertex/index buffers
|
||||
// needed. Samples the source texture across the [0,1] UV range.
|
||||
@@ -38,10 +36,10 @@ ComPtr<ID3DBlob> compile(const char* entry, const char* target)
|
||||
{
|
||||
ComPtr<ID3DBlob> blob;
|
||||
ComPtr<ID3DBlob> errors;
|
||||
const HRESULT hr = D3DCompile(kShaderSource, sizeof(kShaderSource) - 1, "frame_renderer", nullptr, nullptr, entry,
|
||||
target, D3DCOMPILE_OPTIMIZATION_LEVEL3, 0, blob.GetAddressOf(), errors.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
const HRESULT hr =
|
||||
D3DCompile(kShaderSource, sizeof(kShaderSource) - 1, "frame_renderer", nullptr, nullptr, entry, target,
|
||||
D3DCOMPILE_OPTIMIZATION_LEVEL3, 0, blob.GetAddressOf(), errors.GetAddressOf());
|
||||
if (FAILED(hr)) {
|
||||
return nullptr;
|
||||
}
|
||||
return blob;
|
||||
@@ -53,18 +51,15 @@ bool FrameRenderer::init(ID3D11Device* device)
|
||||
{
|
||||
ComPtr<ID3DBlob> vs_blob = compile("vs_main", "vs_5_0");
|
||||
ComPtr<ID3DBlob> ps_blob = compile("ps_main", "ps_5_0");
|
||||
if (vs_blob == nullptr || ps_blob == nullptr)
|
||||
{
|
||||
if (vs_blob == nullptr || ps_blob == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (FAILED(device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), nullptr,
|
||||
vs_.GetAddressOf())))
|
||||
{
|
||||
vs_.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
if (FAILED(device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), nullptr,
|
||||
ps_.GetAddressOf())))
|
||||
{
|
||||
ps_.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -74,8 +69,7 @@ bool FrameRenderer::init(ID3D11Device* device)
|
||||
sd.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
sd.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
sd.ComparisonFunc = D3D11_COMPARISON_NEVER;
|
||||
if (FAILED(device->CreateSamplerState(&sd, sampler_.GetAddressOf())))
|
||||
{
|
||||
if (FAILED(device->CreateSamplerState(&sd, sampler_.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -84,8 +78,7 @@ bool FrameRenderer::init(ID3D11Device* device)
|
||||
void FrameRenderer::draw(ID3D11DeviceContext* ctx, ID3D11ShaderResourceView* srv, std::uint32_t src_w,
|
||||
std::uint32_t src_h, std::uint32_t dst_w, std::uint32_t dst_h)
|
||||
{
|
||||
if (srv == nullptr || src_w == 0 || src_h == 0 || dst_w == 0 || dst_h == 0)
|
||||
{
|
||||
if (srv == nullptr || src_w == 0 || src_h == 0 || dst_w == 0 || dst_h == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,9 @@
|
||||
#include <d3d11.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class FrameRenderer
|
||||
{
|
||||
class FrameRenderer {
|
||||
public:
|
||||
bool init(ID3D11Device* device);
|
||||
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
|
||||
#include <windows.h> // HRESULT, S_OK, WAIT_ABANDONED, WAIT_TIMEOUT
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// True when an IDXGIKeyedMutex::AcquireSync result means we now HOLD the mutex and must copy + then
|
||||
// release it. S_OK is the normal case. WAIT_ABANDONED is success-with-recovery: a previous owner
|
||||
|
||||
@@ -5,13 +5,11 @@
|
||||
#include "coop/protocol.hpp"
|
||||
#include "coop/shared_memory.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
bool SharedTextureSource::init(ID3D11Device* device)
|
||||
{
|
||||
if (device == nullptr || FAILED(device->QueryInterface(IID_PPV_ARGS(&device_))))
|
||||
{
|
||||
if (device == nullptr || FAILED(device->QueryInterface(IID_PPV_ARGS(&device_)))) {
|
||||
return false;
|
||||
}
|
||||
device_->GetImmediateContext(&ctx_);
|
||||
@@ -40,21 +38,17 @@ bool SharedTextureSource::reopen(unsigned long pid, const VideoShareView& share)
|
||||
width_ = height_ = format_ = 0;
|
||||
pid_ = pid;
|
||||
|
||||
if (pid == 0 || share.width == 0 || share.height == 0)
|
||||
{
|
||||
if (pid == 0 || share.width == 0 || share.height == 0) {
|
||||
return false; // the hook hasn't shared a backbuffer yet
|
||||
}
|
||||
|
||||
const std::wstring name = video_share_name(pid);
|
||||
if (FAILED(device_->OpenSharedResourceByName(name.c_str(),
|
||||
DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
|
||||
IID_PPV_ARGS(&shared_))) ||
|
||||
shared_ == nullptr)
|
||||
{
|
||||
if (FAILED(device_->OpenSharedResourceByName(name.c_str(), DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
|
||||
IID_PPV_ARGS(&shared_)))
|
||||
|| shared_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (FAILED(shared_.As(&mutex_)) || mutex_ == nullptr)
|
||||
{
|
||||
if (FAILED(shared_.As(&mutex_)) || mutex_ == nullptr) {
|
||||
shared_.Reset();
|
||||
return false;
|
||||
}
|
||||
@@ -72,14 +66,12 @@ bool SharedTextureSource::reopen(unsigned long pid, const VideoShareView& share)
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Usage = D3D11_USAGE_DEFAULT;
|
||||
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
||||
if (FAILED(device_->CreateTexture2D(&desc, nullptr, &private_)) || private_ == nullptr)
|
||||
{
|
||||
if (FAILED(device_->CreateTexture2D(&desc, nullptr, &private_)) || private_ == nullptr) {
|
||||
mutex_.Reset();
|
||||
shared_.Reset();
|
||||
return false;
|
||||
}
|
||||
if (FAILED(device_->CreateShaderResourceView(private_.Get(), nullptr, &srv_)))
|
||||
{
|
||||
if (FAILED(device_->CreateShaderResourceView(private_.Get(), nullptr, &srv_))) {
|
||||
srv_.Reset();
|
||||
private_.Reset();
|
||||
mutex_.Reset();
|
||||
@@ -96,8 +88,7 @@ bool SharedTextureSource::reopen(unsigned long pid, const VideoShareView& share)
|
||||
bool SharedTextureSource::map_staging_copy(Microsoft::WRL::ComPtr<ID3D11Texture2D>& staging,
|
||||
D3D11_MAPPED_SUBRESOURCE& map, D3D11_TEXTURE2D_DESC& desc)
|
||||
{
|
||||
if (private_ == nullptr || ctx_ == nullptr || device_ == nullptr)
|
||||
{
|
||||
if (private_ == nullptr || ctx_ == nullptr || device_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
private_->GetDesc(&desc);
|
||||
@@ -106,8 +97,7 @@ bool SharedTextureSource::map_staging_copy(Microsoft::WRL::ComPtr<ID3D11Texture2
|
||||
staging_desc.BindFlags = 0;
|
||||
staging_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
staging_desc.MiscFlags = 0;
|
||||
if (FAILED(device_->CreateTexture2D(&staging_desc, nullptr, &staging)))
|
||||
{
|
||||
if (FAILED(device_->CreateTexture2D(&staging_desc, nullptr, &staging))) {
|
||||
return false;
|
||||
}
|
||||
ctx_->CopyResource(staging.Get(), private_.Get());
|
||||
@@ -119,15 +109,13 @@ bool SharedTextureSource::read_frame(std::vector<std::uint8_t>& out, std::uint32
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> staging;
|
||||
D3D11_MAPPED_SUBRESOURCE map{};
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
if (!map_staging_copy(staging, map, desc))
|
||||
{
|
||||
if (!map_staging_copy(staging, map, desc)) {
|
||||
return false;
|
||||
}
|
||||
w = desc.Width;
|
||||
h = desc.Height;
|
||||
out.resize(static_cast<std::size_t>(w) * h * 4);
|
||||
for (std::uint32_t y = 0; y < h; ++y)
|
||||
{
|
||||
for (std::uint32_t y = 0; y < h; ++y) {
|
||||
memcpy(out.data() + static_cast<std::size_t>(y) * w * 4,
|
||||
static_cast<const std::uint8_t*>(map.pData) + static_cast<std::size_t>(y) * map.RowPitch,
|
||||
static_cast<std::size_t>(w) * 4);
|
||||
@@ -141,17 +129,15 @@ bool SharedTextureSource::read_pixel(std::uint32_t x, std::uint32_t y, std::uint
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> staging;
|
||||
D3D11_MAPPED_SUBRESOURCE map{};
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
if (!map_staging_copy(staging, map, desc))
|
||||
{
|
||||
if (!map_staging_copy(staging, map, desc)) {
|
||||
return false;
|
||||
}
|
||||
if (x >= desc.Width || y >= desc.Height)
|
||||
{
|
||||
if (x >= desc.Width || y >= desc.Height) {
|
||||
ctx_->Unmap(staging.Get(), 0);
|
||||
return false;
|
||||
}
|
||||
const auto* px = static_cast<const std::uint8_t*>(map.pData) + static_cast<std::size_t>(y) * map.RowPitch +
|
||||
static_cast<std::size_t>(x) * 4; // R8G8B8A8_UNORM
|
||||
const auto* px = static_cast<const std::uint8_t*>(map.pData) + static_cast<std::size_t>(y) * map.RowPitch
|
||||
+ static_cast<std::size_t>(x) * 4; // R8G8B8A8_UNORM
|
||||
out[0] = px[0];
|
||||
out[1] = px[1];
|
||||
out[2] = px[2];
|
||||
@@ -162,43 +148,36 @@ bool SharedTextureSource::read_pixel(std::uint32_t x, std::uint32_t y, std::uint
|
||||
|
||||
bool SharedTextureSource::update(const VideoShareView& share, unsigned long pid)
|
||||
{
|
||||
if (device_ == nullptr || pid == 0)
|
||||
{
|
||||
if (device_ == nullptr || pid == 0) {
|
||||
reset();
|
||||
return false;
|
||||
}
|
||||
|
||||
// (Re)open whenever the target or the published backbuffer geometry changes.
|
||||
if (pid != pid_ || share.width != width_ || share.height != height_ || share.format != format_)
|
||||
{
|
||||
if (!reopen(pid, share))
|
||||
{
|
||||
if (pid != pid_ || share.width != width_ || share.height != height_ || share.format != format_) {
|
||||
if (!reopen(pid, share)) {
|
||||
return srv_ != nullptr; // couldn't open yet; keep any prior frame
|
||||
}
|
||||
last_generation_ = 0; // force a copy of the current frame
|
||||
}
|
||||
|
||||
if (shared_ == nullptr || mutex_ == nullptr)
|
||||
{
|
||||
if (shared_ == nullptr || mutex_ == nullptr) {
|
||||
return srv_ != nullptr;
|
||||
}
|
||||
if (share.generation == last_generation_)
|
||||
{
|
||||
if (share.generation == last_generation_) {
|
||||
return srv_ != nullptr; // no new frame; keep showing the last copy
|
||||
}
|
||||
|
||||
// Bounded wait so a stalled producer can't hang the host's render thread. WAIT_ABANDONED (a prior
|
||||
// owner died holding the mutex -- e.g. a host that crashed and reconnected) counts as acquired:
|
||||
// recover by copying + releasing rather than skipping, which would hold it forever and freeze.
|
||||
if (keyed_mutex_acquired(mutex_->AcquireSync(kVideoMutexKey, 8)))
|
||||
{
|
||||
if (keyed_mutex_acquired(mutex_->AcquireSync(kVideoMutexKey, 8))) {
|
||||
ctx_->CopyResource(private_.Get(), shared_.Get());
|
||||
mutex_->ReleaseSync(kVideoMutexKey);
|
||||
// Generations between the last copy and this one were published but never shown
|
||||
// (we only ever copy the newest). last_generation_ == 0 is the first copy after a
|
||||
// (re)open, where the gap to a large generation is meaningless, so skip it.
|
||||
if (last_generation_ != 0 && share.generation > last_generation_ + 1)
|
||||
{
|
||||
if (last_generation_ != 0 && share.generation > last_generation_ + 1) {
|
||||
frames_missed_ += share.generation - last_generation_ - 1;
|
||||
}
|
||||
last_generation_ = share.generation;
|
||||
|
||||
@@ -14,11 +14,9 @@
|
||||
|
||||
#include "ipc/ipc_server.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class SharedTextureSource
|
||||
{
|
||||
class SharedTextureSource {
|
||||
public:
|
||||
// Binds to the host's device (must support ID3D11Device1). Returns false if not.
|
||||
bool init(ID3D11Device* device);
|
||||
@@ -43,28 +41,13 @@ public:
|
||||
// Returns false if no frame has been copied yet or the readback failed.
|
||||
bool read_pixel(std::uint32_t x, std::uint32_t y, std::uint8_t out[4]);
|
||||
|
||||
[[nodiscard]] ID3D11ShaderResourceView* srv() const
|
||||
{
|
||||
return srv_.Get();
|
||||
}
|
||||
[[nodiscard]] std::uint32_t width() const
|
||||
{
|
||||
return width_;
|
||||
}
|
||||
[[nodiscard]] std::uint32_t height() const
|
||||
{
|
||||
return height_;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t frames_copied() const
|
||||
{
|
||||
return frames_copied_;
|
||||
}
|
||||
[[nodiscard]] ID3D11ShaderResourceView* srv() const { return srv_.Get(); }
|
||||
[[nodiscard]] std::uint32_t width() const { return width_; }
|
||||
[[nodiscard]] std::uint32_t height() const { return height_; }
|
||||
[[nodiscard]] std::uint64_t frames_copied() const { return frames_copied_; }
|
||||
// Cumulative published frames the host never displayed because the generation
|
||||
// advanced by more than one between copies (host render rate < hook publish rate).
|
||||
[[nodiscard]] std::uint64_t frames_missed() const
|
||||
{
|
||||
return frames_missed_;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t frames_missed() const { return frames_missed_; }
|
||||
|
||||
private:
|
||||
bool reopen(unsigned long pid, const VideoShareView& share);
|
||||
|
||||
@@ -12,19 +12,16 @@
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace winrt
|
||||
{
|
||||
namespace winrt {
|
||||
using namespace Windows::Graphics;
|
||||
using namespace Windows::Graphics::Capture;
|
||||
using namespace Windows::Graphics::DirectX;
|
||||
using namespace Windows::Graphics::DirectX::Direct3D11;
|
||||
} // namespace winrt
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
constexpr auto kPixelFormat = winrt::DirectXPixelFormat::B8G8R8A8UIntNormalized;
|
||||
|
||||
@@ -33,8 +30,7 @@ ComPtr<ID3D11Texture2D> texture_from_surface(winrt::IDirect3DSurface const& surf
|
||||
{
|
||||
auto access = surface.as<::Windows::Graphics::DirectX::Direct3D11::IDirect3DDxgiInterfaceAccess>();
|
||||
ComPtr<ID3D11Texture2D> texture;
|
||||
if (access)
|
||||
{
|
||||
if (access) {
|
||||
access->GetInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(texture.GetAddressOf()));
|
||||
}
|
||||
return texture;
|
||||
@@ -50,24 +46,20 @@ WindowCapture::~WindowCapture()
|
||||
bool WindowCapture::start(HWND target, ID3D11Device* device)
|
||||
{
|
||||
stop();
|
||||
if (target == nullptr || device == nullptr || !IsWindow(target))
|
||||
{
|
||||
if (target == nullptr || device == nullptr || !IsWindow(target)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
device_ = device;
|
||||
|
||||
// Wrap our D3D11 device as the WinRT device the frame pool renders on.
|
||||
ComPtr<IDXGIDevice> dxgi_device;
|
||||
if (FAILED(device->QueryInterface(IID_PPV_ARGS(dxgi_device.GetAddressOf()))))
|
||||
{
|
||||
if (FAILED(device->QueryInterface(IID_PPV_ARGS(dxgi_device.GetAddressOf())))) {
|
||||
return false;
|
||||
}
|
||||
winrt::com_ptr<::IInspectable> inspectable;
|
||||
if (FAILED(CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(), inspectable.put())))
|
||||
{
|
||||
if (FAILED(CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(), inspectable.put()))) {
|
||||
return false;
|
||||
}
|
||||
winrt_device_ = inspectable.as<winrt::IDirect3DDevice>();
|
||||
@@ -75,40 +67,30 @@ bool WindowCapture::start(HWND target, ID3D11Device* device)
|
||||
// Create a capture item for the target window via the interop factory.
|
||||
auto interop = winrt::get_activation_factory<winrt::GraphicsCaptureItem, ::IGraphicsCaptureItemInterop>();
|
||||
if (FAILED(interop->CreateForWindow(target, winrt::guid_of<winrt::GraphicsCaptureItem>(),
|
||||
winrt::put_abi(item_))))
|
||||
{
|
||||
winrt::put_abi(item_)))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pool_size_ = item_.Size();
|
||||
frame_pool_ =
|
||||
winrt::Direct3D11CaptureFramePool::CreateFreeThreaded(winrt_device_, kPixelFormat, 2, pool_size_);
|
||||
frame_pool_ = winrt::Direct3D11CaptureFramePool::CreateFreeThreaded(winrt_device_, kPixelFormat, 2, pool_size_);
|
||||
session_ = frame_pool_.CreateCaptureSession(item_);
|
||||
frame_token_ = frame_pool_.FrameArrived({this, &WindowCapture::on_frame_arrived});
|
||||
|
||||
// Best-effort: hide the cursor and the yellow capture border (the border
|
||||
// API requires a recent Windows build, hence the guard).
|
||||
try
|
||||
{
|
||||
try {
|
||||
session_.IsCursorCaptureEnabled(false);
|
||||
} catch (...) {
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
try {
|
||||
session_.IsBorderRequired(false);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
session_.StartCapture();
|
||||
target_ = target;
|
||||
return true;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
} catch (...) {
|
||||
stop();
|
||||
return false;
|
||||
}
|
||||
@@ -116,18 +98,15 @@ bool WindowCapture::start(HWND target, ID3D11Device* device)
|
||||
|
||||
void WindowCapture::stop()
|
||||
{
|
||||
if (frame_pool_ != nullptr && frame_token_)
|
||||
{
|
||||
if (frame_pool_ != nullptr && frame_token_) {
|
||||
frame_pool_.FrameArrived(frame_token_);
|
||||
frame_token_ = {};
|
||||
}
|
||||
if (session_ != nullptr)
|
||||
{
|
||||
if (session_ != nullptr) {
|
||||
session_.Close();
|
||||
session_ = nullptr;
|
||||
}
|
||||
if (frame_pool_ != nullptr)
|
||||
{
|
||||
if (frame_pool_ != nullptr) {
|
||||
frame_pool_.Close();
|
||||
frame_pool_ = nullptr;
|
||||
}
|
||||
@@ -152,8 +131,7 @@ void WindowCapture::on_frame_arrived(winrt::Direct3D11CaptureFramePool const& po
|
||||
auto frame = pool.TryGetNextFrame();
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
++frames_arrived_; // capture-rate metric (this is the WGC delivery cadence)
|
||||
if (pending_ != nullptr)
|
||||
{
|
||||
if (pending_ != nullptr) {
|
||||
pending_.Close(); // drop the un-consumed previous frame back to the pool
|
||||
}
|
||||
pending_ = frame;
|
||||
@@ -169,15 +147,12 @@ void WindowCapture::draw_latest(FrameRenderer& renderer, ID3D11DeviceContext* ct
|
||||
pending_ = nullptr;
|
||||
}
|
||||
|
||||
if (frame != nullptr)
|
||||
{
|
||||
if (ComPtr<ID3D11Texture2D> src = texture_from_surface(frame.Surface()))
|
||||
{
|
||||
if (frame != nullptr) {
|
||||
if (ComPtr<ID3D11Texture2D> src = texture_from_surface(frame.Surface())) {
|
||||
D3D11_TEXTURE2D_DESC desc = {};
|
||||
src->GetDesc(&desc);
|
||||
|
||||
if (latest_ == nullptr || desc.Width != width_ || desc.Height != height_)
|
||||
{
|
||||
if (latest_ == nullptr || desc.Width != width_ || desc.Height != height_) {
|
||||
latest_srv_.Reset();
|
||||
latest_.Reset();
|
||||
|
||||
@@ -186,16 +161,12 @@ void WindowCapture::draw_latest(FrameRenderer& renderer, ID3D11DeviceContext* ct
|
||||
dst.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
||||
dst.CPUAccessFlags = 0;
|
||||
dst.MiscFlags = 0;
|
||||
if (SUCCEEDED(device_->CreateTexture2D(&dst, nullptr, latest_.GetAddressOf())))
|
||||
{
|
||||
if (SUCCEEDED(device_->CreateShaderResourceView(latest_.Get(), nullptr,
|
||||
latest_srv_.GetAddressOf())))
|
||||
{
|
||||
if (SUCCEEDED(device_->CreateTexture2D(&dst, nullptr, latest_.GetAddressOf()))) {
|
||||
if (SUCCEEDED(
|
||||
device_->CreateShaderResourceView(latest_.Get(), nullptr, latest_srv_.GetAddressOf()))) {
|
||||
width_ = desc.Width;
|
||||
height_ = desc.Height;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Drop the texture so the (latest_ == nullptr) guard retries next frame instead
|
||||
// of leaving a null SRV (a silently black mirror) until the next resize.
|
||||
latest_.Reset();
|
||||
@@ -203,8 +174,7 @@ void WindowCapture::draw_latest(FrameRenderer& renderer, ID3D11DeviceContext* ct
|
||||
}
|
||||
}
|
||||
|
||||
if (latest_ != nullptr)
|
||||
{
|
||||
if (latest_ != nullptr) {
|
||||
ctx->CopyResource(latest_.Get(), src.Get());
|
||||
}
|
||||
}
|
||||
@@ -212,15 +182,13 @@ void WindowCapture::draw_latest(FrameRenderer& renderer, ID3D11DeviceContext* ct
|
||||
|
||||
// If the window resized, the capture item changes size; re-fit the pool.
|
||||
const winrt::SizeInt32 size = item_.Size();
|
||||
if (size.Width != pool_size_.Width || size.Height != pool_size_.Height)
|
||||
{
|
||||
if (size.Width != pool_size_.Width || size.Height != pool_size_.Height) {
|
||||
pool_size_ = size;
|
||||
frame_pool_.Recreate(winrt_device_, kPixelFormat, 2, size);
|
||||
}
|
||||
}
|
||||
|
||||
if (latest_srv_ != nullptr)
|
||||
{
|
||||
if (latest_srv_ != nullptr) {
|
||||
renderer.draw(ctx, latest_srv_.Get(), width_, height_, dst_w, dst_h);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,13 +14,11 @@
|
||||
#include <winrt/Windows.Graphics.Capture.h>
|
||||
#include <winrt/Windows.Graphics.DirectX.Direct3D11.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class FrameRenderer;
|
||||
|
||||
class WindowCapture
|
||||
{
|
||||
class WindowCapture {
|
||||
public:
|
||||
~WindowCapture();
|
||||
|
||||
@@ -29,28 +27,13 @@ public:
|
||||
bool start(HWND target, ID3D11Device* device);
|
||||
void stop();
|
||||
|
||||
[[nodiscard]] bool running() const
|
||||
{
|
||||
return session_ != nullptr;
|
||||
}
|
||||
[[nodiscard]] HWND target() const
|
||||
{
|
||||
return target_;
|
||||
}
|
||||
[[nodiscard]] std::uint32_t frame_width() const
|
||||
{
|
||||
return width_;
|
||||
}
|
||||
[[nodiscard]] std::uint32_t frame_height() const
|
||||
{
|
||||
return height_;
|
||||
}
|
||||
[[nodiscard]] bool running() const { return session_ != nullptr; }
|
||||
[[nodiscard]] HWND target() const { return target_; }
|
||||
[[nodiscard]] std::uint32_t frame_width() const { return width_; }
|
||||
[[nodiscard]] std::uint32_t frame_height() const { return height_; }
|
||||
|
||||
// Cumulative frames WGC has delivered (for the capture-rate metric).
|
||||
[[nodiscard]] std::uint64_t frames_arrived() const
|
||||
{
|
||||
return frames_arrived_;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t frames_arrived() const { return frames_arrived_; }
|
||||
|
||||
// Render thread: consume the newest frame (if any) and draw it letterboxed
|
||||
// into a dst_w x dst_h target via `renderer`.
|
||||
|
||||
@@ -4,17 +4,14 @@
|
||||
#include "injection_panel.hpp"
|
||||
#include "ui/app_chrome.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
const ImVec4 kGreen(0.4f, 1.0f, 0.4f, 1.0f);
|
||||
const ImVec4 kRed(1.0f, 0.45f, 0.4f, 1.0f);
|
||||
|
||||
// One colored line for the multi-series perf graph.
|
||||
struct GraphSeries
|
||||
{
|
||||
struct GraphSeries {
|
||||
const char* name;
|
||||
const float* values; // oldest -> newest
|
||||
int count;
|
||||
@@ -37,16 +34,13 @@ void plot_multiseries(const char* id, const GraphSeries* series, int n_series, f
|
||||
|
||||
const float range = (y_max > y_min) ? (y_max - y_min) : 1.0f;
|
||||
ImVec2 pts[256];
|
||||
for (int s = 0; s < n_series; ++s)
|
||||
{
|
||||
for (int s = 0; s < n_series; ++s) {
|
||||
const GraphSeries& g = series[s];
|
||||
if (g.count < 2)
|
||||
{
|
||||
if (g.count < 2) {
|
||||
continue;
|
||||
}
|
||||
int cnt = g.count > 256 ? 256 : g.count;
|
||||
for (int i = 0; i < cnt; ++i)
|
||||
{
|
||||
for (int i = 0; i < cnt; ++i) {
|
||||
const float t = static_cast<float>(i) / static_cast<float>(cnt - 1);
|
||||
float norm = (g.values[i] - y_min) / range;
|
||||
norm = norm < 0.0f ? 0.0f : (norm > 1.0f ? 1.0f : norm);
|
||||
@@ -87,12 +81,10 @@ void CapturePanel::draw_ui(const FrameStats& stats)
|
||||
const bool have_source = source_ == Source_Hooked ? have_hook : have_wgc_target;
|
||||
|
||||
ImGui::BeginDisabled(!have_source);
|
||||
if (ImGui::Checkbox("Mirror game window", &enabled_) && !enabled_)
|
||||
{
|
||||
if (ImGui::Checkbox("Mirror game window", &enabled_) && !enabled_) {
|
||||
capture_.stop();
|
||||
shared_.reset();
|
||||
if (injection_ != nullptr)
|
||||
{
|
||||
if (injection_ != nullptr) {
|
||||
injection_->request_video(false); // stop the in-game Present hook
|
||||
}
|
||||
}
|
||||
@@ -106,12 +98,10 @@ void CapturePanel::draw_ui(const FrameStats& stats)
|
||||
ImGui::RadioButton("WGC", &source_, Source_Wgc);
|
||||
ImGui::SameLine();
|
||||
ImGui::RadioButton("Hooked (Present)", &source_, Source_Hooked);
|
||||
if (source_ != prev_source)
|
||||
{
|
||||
if (source_ != prev_source) {
|
||||
capture_.stop();
|
||||
shared_.reset();
|
||||
if (injection_ != nullptr)
|
||||
{
|
||||
if (injection_ != nullptr) {
|
||||
injection_->request_video(enabled_ && source_ == Source_Hooked);
|
||||
}
|
||||
}
|
||||
@@ -123,61 +113,44 @@ void CapturePanel::draw_ui(const FrameStats& stats)
|
||||
ImGui::BeginDisabled(source_ != Source_Hooked || !enabled_);
|
||||
ImGui::Checkbox("Sync flip to game frames", &frame_sync_);
|
||||
ImGui::EndDisabled();
|
||||
if (source_ != Source_Hooked)
|
||||
{
|
||||
if (source_ != Source_Hooked) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("(Hooked only)");
|
||||
}
|
||||
else if (ImGui::IsItemHovered())
|
||||
{
|
||||
} else if (ImGui::IsItemHovered()) {
|
||||
ImGui::SetTooltip("Present in lockstep with the game instead of vsync.");
|
||||
}
|
||||
|
||||
if (!have_source)
|
||||
{
|
||||
ImGui::TextDisabled(source_ == Source_Hooked
|
||||
? "Inject into a game first (the Present hook is the source)."
|
||||
if (!have_source) {
|
||||
ImGui::TextDisabled(source_ == Source_Hooked ? "Inject into a game first (the Present hook is the source)."
|
||||
: "Inject into a game first (its window is the source).");
|
||||
}
|
||||
|
||||
if (source_ == Source_Wgc)
|
||||
{
|
||||
if (source_ == Source_Wgc) {
|
||||
// Start/restart WGC capture when enabled and the target window changes.
|
||||
if (enabled_ && have_wgc_target && capture_.target() != target_)
|
||||
{
|
||||
if (!capture_.start(target_, device_))
|
||||
{
|
||||
if (enabled_ && have_wgc_target && capture_.target() != target_) {
|
||||
if (!capture_.start(target_, device_)) {
|
||||
enabled_ = false;
|
||||
ImGui::TextColored(kRed, "Failed to start capture.");
|
||||
}
|
||||
}
|
||||
if (capture_.running())
|
||||
{
|
||||
if (capture_.running()) {
|
||||
ImGui::TextColored(kGreen, "Capturing %ux%u (WGC)", capture_.frame_width(), capture_.frame_height());
|
||||
}
|
||||
}
|
||||
else // Source_Hooked
|
||||
{
|
||||
if (enabled_ && injection_ != nullptr)
|
||||
} else // Source_Hooked
|
||||
{
|
||||
if (enabled_ && injection_ != nullptr) {
|
||||
// Keep the subsystem requested (a fresh inject may have reset control).
|
||||
if (!injection_->video_requested())
|
||||
{
|
||||
if (!injection_->video_requested()) {
|
||||
injection_->request_video(true);
|
||||
}
|
||||
const VideoShareView share = injection_->video_share();
|
||||
if (shared_.frames_copied() > 0 && shared_.width() > 0)
|
||||
{
|
||||
ImGui::TextColored(kGreen, "Mirroring %ux%u (hooked, %llu frames)", shared_.width(),
|
||||
shared_.height(), static_cast<unsigned long long>(shared_.frames_copied()));
|
||||
}
|
||||
else if (share.present_calls > 0)
|
||||
{
|
||||
if (shared_.frames_copied() > 0 && shared_.width() > 0) {
|
||||
ImGui::TextColored(kGreen, "Mirroring %ux%u (hooked, %llu frames)", shared_.width(), shared_.height(),
|
||||
static_cast<unsigned long long>(shared_.frames_copied()));
|
||||
} else if (share.present_calls > 0) {
|
||||
ImGui::TextColored(kGreen, "Present hooked (%llu calls); opening shared texture...",
|
||||
static_cast<unsigned long long>(share.present_calls));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextDisabled("Waiting for hooked frames (the game may not render via DXGI).");
|
||||
}
|
||||
}
|
||||
@@ -192,8 +165,7 @@ void CapturePanel::draw_ui(const FrameStats& stats)
|
||||
|
||||
void CapturePanel::draw_pipeline_metrics(const FrameStats& stats)
|
||||
{
|
||||
if (!enabled_)
|
||||
{
|
||||
if (!enabled_) {
|
||||
return;
|
||||
}
|
||||
const double now = ImGui::GetTime();
|
||||
@@ -202,8 +174,7 @@ void CapturePanel::draw_pipeline_metrics(const FrameStats& stats)
|
||||
// thresholds (e.g. 99 -> 100) frame to frame.
|
||||
ImGui::Text("Tool render: %4.0f FPS (%6.2f ms)", stats.fps(), stats.avg_ms());
|
||||
|
||||
if (source_ == Source_Hooked)
|
||||
{
|
||||
if (source_ == Source_Hooked) {
|
||||
const VideoShareView v = injection_ != nullptr ? injection_->video_share() : VideoShareView{};
|
||||
ImGui::Text("Game present: %5.0f /s", present_rate_.sample(v.present_calls, now));
|
||||
ImGui::Text("Hook publish: %5.0f /s", capture_rate_.sample(v.generation, now));
|
||||
@@ -218,8 +189,7 @@ void CapturePanel::draw_pipeline_metrics(const FrameStats& stats)
|
||||
disp_skip);
|
||||
|
||||
// On each newly published frame, measure now - present_qpc (system-wide clock).
|
||||
if (v.generation != last_video_gen_ && v.present_qpc != 0 && qpc_freq_ > 0)
|
||||
{
|
||||
if (v.generation != last_video_gen_ && v.present_qpc != 0 && qpc_freq_ > 0) {
|
||||
last_video_gen_ = v.generation;
|
||||
LARGE_INTEGER now_qpc{};
|
||||
QueryPerformanceCounter(&now_qpc);
|
||||
@@ -228,12 +198,10 @@ void CapturePanel::draw_pipeline_metrics(const FrameStats& stats)
|
||||
if (ms >= 0.0 && ms < 1000.0) // ignore clock edge cases
|
||||
{
|
||||
lat_sum_ += ms;
|
||||
if (lat_n_ == 0 || ms < lat_wmin_)
|
||||
{
|
||||
if (lat_n_ == 0 || ms < lat_wmin_) {
|
||||
lat_wmin_ = ms;
|
||||
}
|
||||
if (ms > lat_wmax_)
|
||||
{
|
||||
if (ms > lat_wmax_) {
|
||||
lat_wmax_ = ms;
|
||||
}
|
||||
++lat_n_;
|
||||
@@ -241,8 +209,7 @@ void CapturePanel::draw_pipeline_metrics(const FrameStats& stats)
|
||||
}
|
||||
if (now - lat_window_start_ >= 1.0) // publish min/avg/max once a second
|
||||
{
|
||||
if (lat_n_ > 0)
|
||||
{
|
||||
if (lat_n_ > 0) {
|
||||
lat_avg_ = static_cast<float>(lat_sum_ / lat_n_);
|
||||
lat_min_ = static_cast<float>(lat_wmin_);
|
||||
lat_max_ = static_cast<float>(lat_wmax_);
|
||||
@@ -253,17 +220,12 @@ void CapturePanel::draw_pipeline_metrics(const FrameStats& stats)
|
||||
lat_wmax_ = 0.0;
|
||||
lat_window_start_ = now;
|
||||
}
|
||||
if (lat_avg_ > 0.0f)
|
||||
{
|
||||
if (lat_avg_ > 0.0f) {
|
||||
ImGui::Text("Capture->display: avg %6.1f min %6.1f max %6.1f ms", lat_avg_, lat_min_, lat_max_);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextDisabled("Capture->display latency: measuring...");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextDisabled("Game present: n/a (WGC has no game frame timing)");
|
||||
ImGui::Text("WGC capture: %5.0f /s", capture_rate_.sample(capture_.frames_arrived(), now));
|
||||
ImGui::TextDisabled("Latency: n/a (WGC frames aren't game-timestamped)");
|
||||
@@ -277,14 +239,11 @@ void CapturePanel::sample_graph_series(double now)
|
||||
const float dt = ImGui::GetIO().DeltaTime;
|
||||
tool_fps_.push(dt > 0.0f ? 1.0f / dt : 0.0f);
|
||||
|
||||
if (source_ == Source_Hooked)
|
||||
{
|
||||
if (source_ == Source_Hooked) {
|
||||
const VideoShareView v = injection_ != nullptr ? injection_->video_share() : VideoShareView{};
|
||||
game_fps_.push(game_edge_.sample(v.present_calls, now));
|
||||
hook_fps_.push(hook_edge_.sample(v.generation, now));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
wgc_fps_.push(wgc_edge_.sample(capture_.frames_arrived(), now));
|
||||
}
|
||||
last_graph_time_ = now;
|
||||
@@ -310,8 +269,7 @@ void CapturePanel::draw_perf_graphs(const FrameStats& /*stats*/)
|
||||
|
||||
const auto add = [&](const Series& s, const char* name, const ImVec4& col) {
|
||||
const int c = s.copy(fps[n]);
|
||||
for (int i = 0; i < c; ++i)
|
||||
{
|
||||
for (int i = 0; i < c; ++i) {
|
||||
ms[n][i] = fps[n][i] > 1.0f ? 1000.0f / fps[n][i] : 0.0f;
|
||||
}
|
||||
names[n] = name;
|
||||
@@ -321,42 +279,34 @@ void CapturePanel::draw_perf_graphs(const FrameStats& /*stats*/)
|
||||
};
|
||||
|
||||
add(tool_fps_, "Tool", col_tool);
|
||||
if (source_ == Source_Hooked)
|
||||
{
|
||||
if (source_ == Source_Hooked) {
|
||||
add(game_fps_, "Game", col_game);
|
||||
add(hook_fps_, "Hook", col_hook);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
add(wgc_fps_, "WGC", col_hook);
|
||||
}
|
||||
|
||||
if (counts[0] < 2)
|
||||
{
|
||||
if (counts[0] < 2) {
|
||||
ImGui::TextDisabled("Gathering samples...");
|
||||
return;
|
||||
}
|
||||
|
||||
// Legend: a colored label + the latest value of each line, so colors map to series.
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
ImGui::TextColored(cols[i], "%-4s %4.0f", names[i], counts[i] > 0 ? fps[i][counts[i] - 1] : 0.0f);
|
||||
if (i + 1 < n)
|
||||
{
|
||||
if (i + 1 < n) {
|
||||
ImGui::SameLine();
|
||||
}
|
||||
}
|
||||
|
||||
GraphSeries gs[3];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
gs[i] = GraphSeries{names[i], fps[i], counts[i], cols[i]};
|
||||
}
|
||||
ImGui::TextDisabled("FPS (0-144)");
|
||||
plot_multiseries("##fps_multi", gs, n, 0.0f, 144.0f, 56.0f);
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int i = 0; i < n; ++i) {
|
||||
gs[i].values = ms[i];
|
||||
}
|
||||
ImGui::TextDisabled("Frametime (0-33 ms)");
|
||||
@@ -365,23 +315,17 @@ void CapturePanel::draw_perf_graphs(const FrameStats& /*stats*/)
|
||||
|
||||
void CapturePanel::render(ID3D11DeviceContext* ctx, std::uint32_t dst_w, std::uint32_t dst_h)
|
||||
{
|
||||
if (!enabled_)
|
||||
{
|
||||
if (!enabled_) {
|
||||
return;
|
||||
}
|
||||
if (source_ == Source_Hooked)
|
||||
{
|
||||
if (injection_ == nullptr)
|
||||
{
|
||||
if (source_ == Source_Hooked) {
|
||||
if (injection_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (shared_.update(injection_->video_share(), injection_->target_pid()) && shared_.srv() != nullptr)
|
||||
{
|
||||
if (shared_.update(injection_->video_share(), injection_->target_pid()) && shared_.srv() != nullptr) {
|
||||
renderer_.draw(ctx, shared_.srv(), shared_.width(), shared_.height(), dst_w, dst_h);
|
||||
}
|
||||
}
|
||||
else if (capture_.running())
|
||||
{
|
||||
} else if (capture_.running()) {
|
||||
capture_.draw_latest(renderer_, ctx, dst_w, dst_h);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,28 +14,20 @@
|
||||
#include "capture/window_capture.hpp"
|
||||
#include "ui/app_chrome.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class InjectionPanel;
|
||||
|
||||
class CapturePanel
|
||||
{
|
||||
class CapturePanel {
|
||||
public:
|
||||
bool init(ID3D11Device* device);
|
||||
|
||||
// The window to mirror via WGC (0 if none yet); typically the injected game's HWND.
|
||||
void set_target(HWND target)
|
||||
{
|
||||
target_ = target;
|
||||
}
|
||||
void set_target(HWND target) { target_ = target; }
|
||||
|
||||
// The injection panel supplies the target pid + Present-hook video channel and
|
||||
// lets this panel install/remove the video subsystem when the source is Hooked.
|
||||
void set_injection(InjectionPanel* injection)
|
||||
{
|
||||
injection_ = injection;
|
||||
}
|
||||
void set_injection(InjectionPanel* injection) { injection_ = injection; }
|
||||
|
||||
// `stats` are the host's render frame-timing, drawn as the mirror's
|
||||
// frametime / FPS graphs (this window is what the mirror renders into).
|
||||
@@ -46,17 +38,11 @@ public:
|
||||
|
||||
// Whether the video mirror is on (mouse forwarding is gated on this, since the
|
||||
// operator can't aim clicks without seeing the game).
|
||||
[[nodiscard]] bool mirroring() const
|
||||
{
|
||||
return enabled_;
|
||||
}
|
||||
[[nodiscard]] bool mirroring() const { return enabled_; }
|
||||
|
||||
// True when the active source is the injected Present-hook (client/backbuffer);
|
||||
// false for WGC (whole-window). Drives the mouse coordinate mapping.
|
||||
[[nodiscard]] bool source_hooked() const
|
||||
{
|
||||
return source_ == Source_Hooked;
|
||||
}
|
||||
[[nodiscard]] bool source_hooked() const { return source_ == Source_Hooked; }
|
||||
|
||||
// True when the operator asked to pace the tool's flip to the game's published frames
|
||||
// (only meaningful with the Hooked source while mirroring) AND the target is alive and
|
||||
@@ -67,8 +53,7 @@ public:
|
||||
[[nodiscard]] bool frame_sync_active() const;
|
||||
|
||||
private:
|
||||
enum Source : int
|
||||
{
|
||||
enum Source : int {
|
||||
Source_Wgc = 0, // Windows Graphics Capture
|
||||
Source_Hooked = 1, // injected Present-hook shared texture
|
||||
};
|
||||
@@ -77,15 +62,13 @@ private:
|
||||
void draw_pipeline_metrics(const FrameStats& stats);
|
||||
|
||||
// Turns a monotonic counter into a rate (recomputed ~2x/second).
|
||||
struct RateTracker
|
||||
{
|
||||
struct RateTracker {
|
||||
std::uint64_t last_count = 0;
|
||||
double last_time = 0.0;
|
||||
double rate = 0.0;
|
||||
double sample(std::uint64_t count, double now)
|
||||
{
|
||||
if (now - last_time >= 0.5)
|
||||
{
|
||||
if (now - last_time >= 0.5) {
|
||||
const double dt = now - last_time;
|
||||
rate = dt > 0.0 ? static_cast<double>(count - last_count) / dt : 0.0;
|
||||
last_count = count;
|
||||
@@ -98,25 +81,20 @@ private:
|
||||
// Turns a monotonic counter into an instantaneous rate the moment it advances (so a
|
||||
// per-frame graph has real resolution instead of 0.5 s stair-steps); the rate is
|
||||
// held between advances. Used for the game-present / hook-publish graph series.
|
||||
struct EdgeRate
|
||||
{
|
||||
struct EdgeRate {
|
||||
std::uint64_t last_count = 0;
|
||||
double last_time = 0.0;
|
||||
float fps = 0.0f;
|
||||
bool primed = false;
|
||||
float sample(std::uint64_t count, double now)
|
||||
{
|
||||
if (!primed)
|
||||
{
|
||||
if (!primed) {
|
||||
last_count = count;
|
||||
last_time = now;
|
||||
primed = true;
|
||||
}
|
||||
else if (count != last_count)
|
||||
{
|
||||
} else if (count != last_count) {
|
||||
const double dt = now - last_time;
|
||||
if (dt > 0.0)
|
||||
{
|
||||
if (dt > 0.0) {
|
||||
fps = static_cast<float>(static_cast<double>(count - last_count) / dt);
|
||||
}
|
||||
last_count = count;
|
||||
@@ -127,8 +105,7 @@ private:
|
||||
};
|
||||
|
||||
// Fixed-length rolling history of one FPS series, plotted in the perf graph.
|
||||
struct Series
|
||||
{
|
||||
struct Series {
|
||||
static constexpr int kCap = 240; // ~2 s at 120 FPS, matches FrameStats
|
||||
float v[kCap] = {};
|
||||
int pos = 0;
|
||||
@@ -137,8 +114,7 @@ private:
|
||||
{
|
||||
v[pos] = fps;
|
||||
pos = (pos + 1) % kCap;
|
||||
if (count < kCap)
|
||||
{
|
||||
if (count < kCap) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
@@ -146,16 +122,12 @@ private:
|
||||
int copy(float* out) const
|
||||
{
|
||||
const int start = (pos - count + kCap * 2) % kCap;
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
for (int i = 0; i < count; ++i) {
|
||||
out[i] = v[(start + i) % kCap];
|
||||
}
|
||||
return count;
|
||||
}
|
||||
float latest() const
|
||||
{
|
||||
return count > 0 ? v[(pos - 1 + kCap) % kCap] : 0.0f;
|
||||
}
|
||||
float latest() const { return count > 0 ? v[(pos - 1 + kCap) % kCap] : 0.0f; }
|
||||
};
|
||||
|
||||
// Push one sample into each graph series for the current frame/source.
|
||||
|
||||
@@ -4,17 +4,14 @@
|
||||
|
||||
#include "ui/app_chrome.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
const ImVec4 kGreen(0.4f, 1.0f, 0.4f, 1.0f);
|
||||
const ImVec4 kGrey(0.7f, 0.7f, 0.7f, 1.0f);
|
||||
|
||||
struct ButtonBit
|
||||
{
|
||||
struct ButtonBit {
|
||||
std::uint16_t mask;
|
||||
const char* label;
|
||||
};
|
||||
@@ -29,16 +26,14 @@ constexpr ButtonBit kButtons[] = {
|
||||
void draw_pad(int index, const PadInfo& pad, bool debug_details)
|
||||
{
|
||||
ImGui::PushID(index);
|
||||
if (!pad.connected)
|
||||
{
|
||||
if (!pad.connected) {
|
||||
ImGui::TextDisabled("Slot %d: disconnected", index);
|
||||
ImGui::PopID();
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui::TextColored(kGreen, "Slot %d [%s]", index, pad.source.c_str());
|
||||
if (debug_details)
|
||||
{
|
||||
if (debug_details) {
|
||||
// Triggers on the slot line (saves a row); thumbsticks below.
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("LT %3u RT %3u", pad.state.left_trigger, pad.state.right_trigger);
|
||||
@@ -46,25 +41,21 @@ void draw_pad(int index, const PadInfo& pad, bool debug_details)
|
||||
|
||||
bool first = true;
|
||||
ImGui::TextUnformatted("Buttons: ");
|
||||
for (const ButtonBit& b : kButtons)
|
||||
{
|
||||
if ((pad.state.buttons & b.mask) != 0)
|
||||
{
|
||||
for (const ButtonBit& b : kButtons) {
|
||||
if ((pad.state.buttons & b.mask) != 0) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextColored(kGreen, "%s%s", first ? "" : ", ", b.label);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
if (first)
|
||||
{
|
||||
if (first) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("(none)");
|
||||
}
|
||||
|
||||
if (debug_details)
|
||||
{
|
||||
ImGui::Text("L (%6d, %6d) R (%6d, %6d)", pad.state.thumb_lx, pad.state.thumb_ly,
|
||||
pad.state.thumb_rx, pad.state.thumb_ry);
|
||||
if (debug_details) {
|
||||
ImGui::Text("L (%6d, %6d) R (%6d, %6d)", pad.state.thumb_lx, pad.state.thumb_ly, pad.state.thumb_rx,
|
||||
pad.state.thumb_ry);
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImGui::PopID();
|
||||
@@ -81,15 +72,13 @@ void ControllersPanel::draw(const InputSnapshot& input, const HookStatusView& st
|
||||
|
||||
#ifdef COOP_WITH_STEAM
|
||||
ImGui::Checkbox("Use Steam Input (experimental)", &steam_requested_);
|
||||
if (steam_requested_)
|
||||
{
|
||||
if (steam_requested_) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextColored(steam_active_ ? kGreen : kGrey, steam_active_ ? "(active)" : "(starting...)");
|
||||
ImGui::TextDisabled("Needs a controller bound to Steam Input for this app; otherwise");
|
||||
ImGui::TextDisabled("XInput is hidden and no input arrives. Leave off for plain XInput.");
|
||||
}
|
||||
if (steam_note_[0] != '\0')
|
||||
{
|
||||
if (steam_note_[0] != '\0') {
|
||||
ImGui::TextColored(kGrey, "%s", steam_note_);
|
||||
}
|
||||
#endif
|
||||
@@ -97,13 +86,11 @@ void ControllersPanel::draw(const InputSnapshot& input, const HookStatusView& st
|
||||
// Synthetic test input (debug aid): drives the game with a non-human pattern so
|
||||
// forwarding can be proven without a real controller. Only meaningful once the
|
||||
// XInput hook is attached.
|
||||
if (debug_details)
|
||||
{
|
||||
if (debug_details) {
|
||||
ImGui::BeginDisabled(!status.attached);
|
||||
ImGui::Checkbox("Forward synthetic test input", &test_input_);
|
||||
ImGui::EndDisabled();
|
||||
if (test_input_)
|
||||
{
|
||||
if (test_input_) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("(ignores your controller)");
|
||||
}
|
||||
@@ -112,15 +99,13 @@ void ControllersPanel::draw(const InputSnapshot& input, const HookStatusView& st
|
||||
// --- Guest pads the host receives from RPT -----------------------------
|
||||
ImGui::SeparatorText("Incoming (host receives)");
|
||||
const auto& pads = input.pads;
|
||||
for (int i = 0; i < static_cast<int>(pads.size()); ++i)
|
||||
{
|
||||
for (int i = 0; i < static_cast<int>(pads.size()); ++i) {
|
||||
draw_pad(i, pads[i], debug_details);
|
||||
}
|
||||
|
||||
// --- What the injected game reads back via the XInput hook --------------
|
||||
ImGui::SeparatorText("Game polling (hook reports)");
|
||||
if (!status.attached)
|
||||
{
|
||||
if (!status.attached) {
|
||||
ImGui::TextDisabled("Not injected (no XInput hook).");
|
||||
record_panel_fit("Controllers");
|
||||
ImGui::End();
|
||||
@@ -129,11 +114,9 @@ void ControllersPanel::draw(const InputSnapshot& input, const HookStatusView& st
|
||||
|
||||
// Convert the cumulative per-slot counters into rates every half second.
|
||||
const double now = ImGui::GetTime();
|
||||
if (now - last_sample_time_ >= 0.5)
|
||||
{
|
||||
if (now - last_sample_time_ >= 0.5) {
|
||||
const double dt = now - last_sample_time_;
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i)
|
||||
{
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i) {
|
||||
const unsigned long long delta =
|
||||
status.get_state[i] >= last_state_count_[i] ? status.get_state[i] - last_state_count_[i] : 0;
|
||||
state_rate_[i] = dt > 0.0 ? static_cast<double>(delta) / dt : 0.0;
|
||||
@@ -143,16 +126,12 @@ void ControllersPanel::draw(const InputSnapshot& input, const HookStatusView& st
|
||||
}
|
||||
|
||||
double total_rate = 0.0;
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i)
|
||||
{
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i) {
|
||||
total_rate += state_rate_[i];
|
||||
}
|
||||
if (total_rate > 0.0)
|
||||
{
|
||||
if (total_rate > 0.0) {
|
||||
ImGui::TextColored(kGreen, "Game reading controller: %5.0f polls/s", total_rate);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextColored(kGrey, "Game reading controller: idle");
|
||||
}
|
||||
|
||||
@@ -160,9 +139,7 @@ void ControllersPanel::draw(const InputSnapshot& input, const HookStatusView& st
|
||||
// (what we forwarded vs what the game read back through the hook). A round-trip mismatch
|
||||
// isolates a tool->game forwarding problem from an input->tool one. Merged into a single
|
||||
// table so the (debug) controller view stays inside its panel even with every slot busy.
|
||||
if (debug_details &&
|
||||
ImGui::BeginTable("slots", 5, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp))
|
||||
{
|
||||
if (debug_details && ImGui::BeginTable("slots", 5, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp)) {
|
||||
ImGui::TableSetupColumn("Slot");
|
||||
ImGui::TableSetupColumn("Poll/s");
|
||||
ImGui::TableSetupColumn("Polls");
|
||||
@@ -170,20 +147,16 @@ void ControllersPanel::draw(const InputSnapshot& input, const HookStatusView& st
|
||||
ImGui::TableSetupColumn("Game read btn/LX,LY");
|
||||
ImGui::TableHeadersRow();
|
||||
const auto& fwd = input.pads;
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i)
|
||||
{
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i) {
|
||||
const CoopPadState& f = fwd[i].state;
|
||||
const CoopPadState& r = status.read_state[i];
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", i);
|
||||
ImGui::TableNextColumn();
|
||||
if (state_rate_[i] > 0.0)
|
||||
{
|
||||
if (state_rate_[i] > 0.0) {
|
||||
ImGui::TextColored(kGreen, "%5.0f", state_rate_[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextDisabled("0");
|
||||
}
|
||||
ImGui::TableNextColumn();
|
||||
|
||||
@@ -11,11 +11,9 @@
|
||||
#include "input/input_source.hpp"
|
||||
#include "ipc/ipc_server.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class ControllersPanel
|
||||
{
|
||||
class ControllersPanel {
|
||||
public:
|
||||
// `input` is the input worker's latest snapshot (guest pads + active backend);
|
||||
// `status` is the hook's back-channel (per-slot poll counters); `debug_details`
|
||||
@@ -25,10 +23,7 @@ public:
|
||||
|
||||
// Whether the operator enabled "Forward synthetic test input" (a controller debug
|
||||
// aid). The host feeds this to InjectionPanel, which substitutes a synthetic pad.
|
||||
[[nodiscard]] bool test_input() const
|
||||
{
|
||||
return test_input_;
|
||||
}
|
||||
[[nodiscard]] bool test_input() const { return test_input_; }
|
||||
|
||||
#ifdef COOP_WITH_STEAM
|
||||
// Whether the operator has opted into Steam Input. It's off by default: simply
|
||||
@@ -36,14 +31,8 @@ public:
|
||||
// which hides controllers from XInput unless they're bound to our action set for
|
||||
// this app -- so it can silently break the (working) XInput path. main reconciles
|
||||
// this against the actual backend each frame.
|
||||
[[nodiscard]] bool steam_input_requested() const
|
||||
{
|
||||
return steam_requested_;
|
||||
}
|
||||
void set_steam_active(bool active)
|
||||
{
|
||||
steam_active_ = active;
|
||||
}
|
||||
[[nodiscard]] bool steam_input_requested() const { return steam_requested_; }
|
||||
void set_steam_active(bool active) { steam_active_ = active; }
|
||||
void on_steam_init_failed()
|
||||
{
|
||||
steam_requested_ = false;
|
||||
|
||||
@@ -9,11 +9,9 @@ extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
constexpr wchar_t kWindowClass[] = L"CoopAllTheThingsWindow";
|
||||
|
||||
// Encode a tightly-packed/row-pitched RGBA8 image to a PNG file via WIC. `src` is the
|
||||
@@ -24,42 +22,34 @@ bool write_rgba8_png(const std::wstring& path, UINT width, UINT height, const BY
|
||||
{
|
||||
ComPtr<IWICImagingFactory> factory;
|
||||
if (FAILED(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_PPV_ARGS(factory.GetAddressOf()))))
|
||||
{
|
||||
IID_PPV_ARGS(factory.GetAddressOf())))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICBitmap> bitmap; // wrap the back-buffer bytes (RGBA, matches the swap chain)
|
||||
if (FAILED(factory->CreateBitmapFromMemory(width, height, GUID_WICPixelFormat32bppRGBA, row_pitch,
|
||||
row_pitch * height, const_cast<BYTE*>(src),
|
||||
bitmap.GetAddressOf())))
|
||||
{
|
||||
row_pitch * height, const_cast<BYTE*>(src), bitmap.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICStream> stream;
|
||||
if (FAILED(factory->CreateStream(stream.GetAddressOf())) ||
|
||||
FAILED(stream->InitializeFromFilename(path.c_str(), GENERIC_WRITE)))
|
||||
{
|
||||
if (FAILED(factory->CreateStream(stream.GetAddressOf()))
|
||||
|| FAILED(stream->InitializeFromFilename(path.c_str(), GENERIC_WRITE))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICBitmapEncoder> encoder;
|
||||
if (FAILED(factory->CreateEncoder(GUID_ContainerFormatPng, nullptr, encoder.GetAddressOf())) ||
|
||||
FAILED(encoder->Initialize(stream.Get(), WICBitmapEncoderNoCache)))
|
||||
{
|
||||
if (FAILED(factory->CreateEncoder(GUID_ContainerFormatPng, nullptr, encoder.GetAddressOf()))
|
||||
|| FAILED(encoder->Initialize(stream.Get(), WICBitmapEncoderNoCache))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IWICBitmapFrameEncode> frame;
|
||||
ComPtr<IPropertyBag2> props;
|
||||
if (FAILED(encoder->CreateNewFrame(frame.GetAddressOf(), props.GetAddressOf())) ||
|
||||
FAILED(frame->Initialize(props.Get())) || FAILED(frame->SetSize(width, height)))
|
||||
{
|
||||
if (FAILED(encoder->CreateNewFrame(frame.GetAddressOf(), props.GetAddressOf()))
|
||||
|| FAILED(frame->Initialize(props.Get())) || FAILED(frame->SetSize(width, height))) {
|
||||
return false;
|
||||
}
|
||||
// Let the encoder pick its native pixel format; WriteSource converts our RGBA to it.
|
||||
WICPixelFormatGUID fmt = GUID_WICPixelFormat32bppBGRA;
|
||||
frame->SetPixelFormat(&fmt);
|
||||
if (FAILED(frame->WriteSource(bitmap.Get(), nullptr)) || FAILED(frame->Commit()) ||
|
||||
FAILED(encoder->Commit()))
|
||||
{
|
||||
if (FAILED(frame->WriteSource(bitmap.Get(), nullptr)) || FAILED(frame->Commit()) || FAILED(encoder->Commit())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -69,8 +59,7 @@ bool write_rgba8_png(const std::wstring& path, UINT width, UINT height, const BY
|
||||
D3D11Window::~D3D11Window()
|
||||
{
|
||||
release_render_target();
|
||||
if (hwnd_ != nullptr)
|
||||
{
|
||||
if (hwnd_ != nullptr) {
|
||||
DestroyWindow(hwnd_);
|
||||
hwnd_ = nullptr;
|
||||
}
|
||||
@@ -88,8 +77,7 @@ bool D3D11Window::create(const wchar_t* title)
|
||||
wc.hInstance = instance;
|
||||
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
|
||||
wc.lpszClassName = kWindowClass;
|
||||
if (RegisterClassExW(&wc) == 0)
|
||||
{
|
||||
if (RegisterClassExW(&wc) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -100,13 +88,11 @@ bool D3D11Window::create(const wchar_t* title)
|
||||
const int height = GetSystemMetrics(SM_CYSCREEN);
|
||||
|
||||
hwnd_ = CreateWindowExW(0, kWindowClass, title, WS_POPUP, 0, 0, width, height, nullptr, nullptr, instance, this);
|
||||
if (hwnd_ == nullptr)
|
||||
{
|
||||
if (hwnd_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!create_device())
|
||||
{
|
||||
if (!create_device()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -136,30 +122,25 @@ bool D3D11Window::create_device()
|
||||
const D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0};
|
||||
|
||||
if (FAILED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, levels, _countof(levels),
|
||||
D3D11_SDK_VERSION, device_.GetAddressOf(), nullptr, context_.GetAddressOf())))
|
||||
{
|
||||
D3D11_SDK_VERSION, device_.GetAddressOf(), nullptr, context_.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ComPtr<IDXGIDevice> dxgi_device;
|
||||
if (FAILED(device_.As(&dxgi_device)))
|
||||
{
|
||||
if (FAILED(device_.As(&dxgi_device))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IDXGIAdapter> adapter;
|
||||
if (FAILED(dxgi_device->GetAdapter(adapter.GetAddressOf())))
|
||||
{
|
||||
if (FAILED(dxgi_device->GetAdapter(adapter.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
ComPtr<IDXGIFactory2> factory;
|
||||
if (FAILED(adapter->GetParent(IID_PPV_ARGS(factory.GetAddressOf()))))
|
||||
{
|
||||
if (FAILED(adapter->GetParent(IID_PPV_ARGS(factory.GetAddressOf())))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FAILED(factory->CreateSwapChainForHwnd(device_.Get(), hwnd_, &desc, nullptr, nullptr,
|
||||
swap_chain_.GetAddressOf())))
|
||||
{
|
||||
swap_chain_.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
// Don't let DXGI swallow Alt+Enter into an exclusive-fullscreen transition.
|
||||
@@ -171,8 +152,7 @@ bool D3D11Window::create_device()
|
||||
|
||||
bool D3D11Window::note_device_loss(HRESULT hr)
|
||||
{
|
||||
if (hr != DXGI_ERROR_DEVICE_REMOVED && hr != DXGI_ERROR_DEVICE_RESET)
|
||||
{
|
||||
if (hr != DXGI_ERROR_DEVICE_REMOVED && hr != DXGI_ERROR_DEVICE_RESET) {
|
||||
return false;
|
||||
}
|
||||
// GetDeviceRemovedReason gives the specific cause (HUNG / driver internal / removed); a plain
|
||||
@@ -186,10 +166,8 @@ bool D3D11Window::note_device_loss(HRESULT hr)
|
||||
void D3D11Window::create_render_target()
|
||||
{
|
||||
ComPtr<ID3D11Texture2D> back_buffer;
|
||||
if (SUCCEEDED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back_buffer.GetAddressOf()))))
|
||||
{
|
||||
const HRESULT hr =
|
||||
device_->CreateRenderTargetView(back_buffer.Get(), nullptr, rtv_.ReleaseAndGetAddressOf());
|
||||
if (SUCCEEDED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back_buffer.GetAddressOf())))) {
|
||||
const HRESULT hr = device_->CreateRenderTargetView(back_buffer.Get(), nullptr, rtv_.ReleaseAndGetAddressOf());
|
||||
note_device_loss(hr); // a removed device surfaces here too; the render loop checks device_lost()
|
||||
}
|
||||
}
|
||||
@@ -201,14 +179,12 @@ void D3D11Window::release_render_target()
|
||||
|
||||
void D3D11Window::handle_resize(UINT width, UINT height)
|
||||
{
|
||||
if (swap_chain_ == nullptr || width == 0 || height == 0)
|
||||
{
|
||||
if (swap_chain_ == nullptr || width == 0 || height == 0) {
|
||||
return;
|
||||
}
|
||||
release_render_target();
|
||||
const HRESULT hr = swap_chain_->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
|
||||
if (note_device_loss(hr))
|
||||
{
|
||||
if (note_device_loss(hr)) {
|
||||
return; // device gone; the render loop will see device_lost() and stop
|
||||
}
|
||||
create_render_target();
|
||||
@@ -217,17 +193,14 @@ void D3D11Window::handle_resize(UINT width, UINT height)
|
||||
bool D3D11Window::pump_messages()
|
||||
{
|
||||
MSG msg;
|
||||
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE))
|
||||
{
|
||||
if (msg.message == WM_QUIT)
|
||||
{
|
||||
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
|
||||
if (msg.message == WM_QUIT) {
|
||||
return false;
|
||||
}
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessageW(&msg);
|
||||
}
|
||||
if (resize_pending_)
|
||||
{
|
||||
if (resize_pending_) {
|
||||
handle_resize(resize_width_, resize_height_);
|
||||
resize_pending_ = false;
|
||||
}
|
||||
@@ -240,17 +213,14 @@ void D3D11Window::render_frame(const RenderCallback& render, UINT sync_interval)
|
||||
context_->OMSetRenderTargets(1, rtv_.GetAddressOf(), nullptr);
|
||||
context_->ClearRenderTargetView(rtv_.Get(), clear);
|
||||
|
||||
if (render)
|
||||
{
|
||||
if (render) {
|
||||
render();
|
||||
}
|
||||
|
||||
// Screenshot (F10): capture after the overlay is drawn but before Present -- the
|
||||
// flip-model back buffer is undefined once presented.
|
||||
if (!pending_screenshot_.empty())
|
||||
{
|
||||
if (save_backbuffer_png(pending_screenshot_))
|
||||
{
|
||||
if (!pending_screenshot_.empty()) {
|
||||
if (save_backbuffer_png(pending_screenshot_)) {
|
||||
saved_screenshot_ = pending_screenshot_;
|
||||
}
|
||||
pending_screenshot_.clear();
|
||||
@@ -277,8 +247,7 @@ std::wstring D3D11Window::take_screenshot_result()
|
||||
bool D3D11Window::save_backbuffer_png(const std::wstring& path)
|
||||
{
|
||||
ComPtr<ID3D11Texture2D> back;
|
||||
if (FAILED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back.GetAddressOf()))))
|
||||
{
|
||||
if (FAILED(swap_chain_->GetBuffer(0, IID_PPV_ARGS(back.GetAddressOf())))) {
|
||||
return false;
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
@@ -291,44 +260,37 @@ bool D3D11Window::save_backbuffer_png(const std::wstring& path)
|
||||
staging.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
staging.MiscFlags = 0;
|
||||
ComPtr<ID3D11Texture2D> cpu;
|
||||
if (FAILED(device_->CreateTexture2D(&staging, nullptr, cpu.GetAddressOf())))
|
||||
{
|
||||
if (FAILED(device_->CreateTexture2D(&staging, nullptr, cpu.GetAddressOf()))) {
|
||||
return false;
|
||||
}
|
||||
context_->CopyResource(cpu.Get(), back.Get());
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE map{};
|
||||
if (FAILED(context_->Map(cpu.Get(), 0, D3D11_MAP_READ, 0, &map)))
|
||||
{
|
||||
if (FAILED(context_->Map(cpu.Get(), 0, D3D11_MAP_READ, 0, &map))) {
|
||||
return false;
|
||||
}
|
||||
// The swap chain is DXGI_FORMAT_R8G8B8A8_UNORM (see create_device), i.e. RGBA bytes.
|
||||
const bool ok =
|
||||
write_rgba8_png(path, desc.Width, desc.Height, static_cast<const BYTE*>(map.pData), map.RowPitch);
|
||||
const bool ok = write_rgba8_png(path, desc.Width, desc.Height, static_cast<const BYTE*>(map.pData), map.RowPitch);
|
||||
context_->Unmap(cpu.Get(), 0);
|
||||
return ok;
|
||||
}
|
||||
|
||||
LRESULT CALLBACK D3D11Window::wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
if (msg == WM_NCCREATE)
|
||||
{
|
||||
if (msg == WM_NCCREATE) {
|
||||
auto* create = reinterpret_cast<CREATESTRUCTW*>(lparam);
|
||||
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(create->lpCreateParams));
|
||||
}
|
||||
|
||||
if (ImGui_ImplWin32_WndProcHandler(hwnd, msg, wparam, lparam))
|
||||
{
|
||||
if (ImGui_ImplWin32_WndProcHandler(hwnd, msg, wparam, lparam)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto* self = reinterpret_cast<D3D11Window*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
|
||||
|
||||
switch (msg)
|
||||
{
|
||||
switch (msg) {
|
||||
case WM_SIZE:
|
||||
if (self != nullptr && wparam != SIZE_MINIMIZED)
|
||||
{
|
||||
if (self != nullptr && wparam != SIZE_MINIMIZED) {
|
||||
self->resize_pending_ = true;
|
||||
self->resize_width_ = LOWORD(lparam);
|
||||
self->resize_height_ = HIWORD(lparam);
|
||||
@@ -338,13 +300,10 @@ LRESULT CALLBACK D3D11Window::wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARA
|
||||
// Per-monitor-v2: the DPI of the display we're on changed. Resize to the rect Windows suggests
|
||||
// in lparam (its recommended handling; the resulting WM_SIZE repaints the swap chain via the
|
||||
// deferred-resize path above), then latch the new DPI for the overlay to rescale its font/style.
|
||||
if (self != nullptr)
|
||||
{
|
||||
if (const auto* suggested = reinterpret_cast<const RECT*>(lparam); suggested != nullptr)
|
||||
{
|
||||
SetWindowPos(hwnd, nullptr, suggested->left, suggested->top,
|
||||
suggested->right - suggested->left, suggested->bottom - suggested->top,
|
||||
SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
if (self != nullptr) {
|
||||
if (const auto* suggested = reinterpret_cast<const RECT*>(lparam); suggested != nullptr) {
|
||||
SetWindowPos(hwnd, nullptr, suggested->left, suggested->top, suggested->right - suggested->left,
|
||||
suggested->bottom - suggested->top, SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
}
|
||||
self->dpi_pending_ = true;
|
||||
self->pending_dpi_ = HIWORD(wparam); // X and Y DPI are equal; HIWORD is the Y value
|
||||
@@ -354,8 +313,7 @@ LRESULT CALLBACK D3D11Window::wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARA
|
||||
// F10 is our screenshot key; ImGui already saw this message (handler runs above),
|
||||
// so swallow it here to stop DefWindowProc from flicking into Win32 menu mode.
|
||||
// Alt+F4 (VK_F4) falls through to DefWindowProc so it still closes the window.
|
||||
if (wparam == VK_F10)
|
||||
{
|
||||
if (wparam == VK_F10) {
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -10,11 +10,9 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class D3D11Window
|
||||
{
|
||||
class D3D11Window {
|
||||
public:
|
||||
using RenderCallback = std::function<void()>;
|
||||
|
||||
@@ -50,15 +48,9 @@ public:
|
||||
// True once Present/ResizeBuffers reported DXGI_ERROR_DEVICE_REMOVED/RESET (a host-side TDR,
|
||||
// driver reset, or GPU hang). The render loop is expected to stop and surface the error rather
|
||||
// than spin forever on a dead device; full device re-creation is intentionally not attempted.
|
||||
[[nodiscard]] bool device_lost() const
|
||||
{
|
||||
return device_lost_;
|
||||
}
|
||||
[[nodiscard]] bool device_lost() const { return device_lost_; }
|
||||
// The GetDeviceRemovedReason() HRESULT (or the originating error) when device_lost() is true.
|
||||
[[nodiscard]] HRESULT device_lost_reason() const
|
||||
{
|
||||
return device_lost_reason_;
|
||||
}
|
||||
[[nodiscard]] HRESULT device_lost_reason() const { return device_lost_reason_; }
|
||||
|
||||
// If a WM_DPICHANGED arrived since the last call (the window moved to a different-DPI monitor, or
|
||||
// the display scale changed at runtime), returns true and writes that monitor's DPI to `dpi`,
|
||||
@@ -66,8 +58,7 @@ public:
|
||||
// fonts/style. One-shot, mirroring the deferred-resize handling in pump_messages().
|
||||
[[nodiscard]] bool take_dpi_change(unsigned& dpi)
|
||||
{
|
||||
if (!dpi_pending_)
|
||||
{
|
||||
if (!dpi_pending_) {
|
||||
return false;
|
||||
}
|
||||
dpi = pending_dpi_;
|
||||
@@ -75,18 +66,9 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] HWND hwnd() const
|
||||
{
|
||||
return hwnd_;
|
||||
}
|
||||
[[nodiscard]] ID3D11Device* device() const
|
||||
{
|
||||
return device_.Get();
|
||||
}
|
||||
[[nodiscard]] ID3D11DeviceContext* context() const
|
||||
{
|
||||
return context_.Get();
|
||||
}
|
||||
[[nodiscard]] HWND hwnd() const { return hwnd_; }
|
||||
[[nodiscard]] ID3D11Device* device() const { return device_.Get(); }
|
||||
[[nodiscard]] ID3D11DeviceContext* context() const { return context_.Get(); }
|
||||
|
||||
private:
|
||||
static LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
|
||||
|
||||
@@ -11,13 +11,11 @@
|
||||
#include "ui/app_chrome.hpp"
|
||||
#include "util/utf8.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
ImGuiLayer::~ImGuiLayer()
|
||||
{
|
||||
if (initialized_)
|
||||
{
|
||||
if (initialized_) {
|
||||
ImGui_ImplDX11_Shutdown();
|
||||
ImGui_ImplWin32_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
@@ -42,12 +40,10 @@ bool ImGuiLayer::init(HWND hwnd, ID3D11Device* device, ID3D11DeviceContext* cont
|
||||
io.IniFilename = ini_path_.c_str();
|
||||
set_layout_persisted(had_layout);
|
||||
|
||||
if (!ImGui_ImplWin32_Init(hwnd))
|
||||
{
|
||||
if (!ImGui_ImplWin32_Init(hwnd)) {
|
||||
return false;
|
||||
}
|
||||
if (!ImGui_ImplDX11_Init(device, context))
|
||||
{
|
||||
if (!ImGui_ImplDX11_Init(device, context)) {
|
||||
return false;
|
||||
}
|
||||
initialized_ = true;
|
||||
@@ -78,8 +74,7 @@ void ImGuiLayer::apply_dpi(unsigned dpi)
|
||||
|
||||
// Drop the DX11 backend's cached font texture so it rebuilds from the new atlas next frame. Before
|
||||
// the first frame nothing is built yet, so this is a harmless no-op during init().
|
||||
if (initialized_)
|
||||
{
|
||||
if (initialized_) {
|
||||
ImGui_ImplDX11_InvalidateDeviceObjects();
|
||||
}
|
||||
dpi_scale_ = scale;
|
||||
@@ -87,8 +82,7 @@ void ImGuiLayer::apply_dpi(unsigned dpi)
|
||||
|
||||
void ImGuiLayer::set_dpi(unsigned dpi)
|
||||
{
|
||||
if (!initialized_ || dpi_scale_from(dpi) == dpi_scale_)
|
||||
{
|
||||
if (!initialized_ || dpi_scale_from(dpi) == dpi_scale_) {
|
||||
return; // not up yet, or the scale didn't actually change -- skip a needless atlas rebuild
|
||||
}
|
||||
apply_dpi(dpi);
|
||||
|
||||
@@ -6,11 +6,9 @@
|
||||
#include <d3d11.h>
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class ImGuiLayer
|
||||
{
|
||||
class ImGuiLayer {
|
||||
public:
|
||||
ImGuiLayer() = default;
|
||||
~ImGuiLayer();
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
#include "coop/protocol.hpp"
|
||||
#include "coop/shared_memory.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
bool hook_dll_alive(unsigned long pid, int timeout_ms)
|
||||
{
|
||||
@@ -18,17 +17,14 @@ bool hook_dll_alive(unsigned long pid, int timeout_ms)
|
||||
// Poll rather than sample once: the worker only beats ~4x/s, so a single short read can straddle
|
||||
// a gap and miss it; return the instant a beat lands, and give up after the timeout.
|
||||
SharedMemory shm;
|
||||
if (!shm.open(shared_memory_name(pid), sizeof(SharedBlock)))
|
||||
{
|
||||
if (!shm.open(shared_memory_name(pid), sizeof(SharedBlock))) {
|
||||
return false;
|
||||
}
|
||||
auto* block = shm.as<SharedBlock>();
|
||||
const std::uint32_t h0 = block->status.heartbeat.load(std::memory_order_acquire);
|
||||
for (int waited = 0; waited < timeout_ms; waited += 25)
|
||||
{
|
||||
for (int waited = 0; waited < timeout_ms; waited += 25) {
|
||||
Sleep(25);
|
||||
if (block->status.heartbeat.load(std::memory_order_acquire) != h0)
|
||||
{
|
||||
if (block->status.heartbeat.load(std::memory_order_acquire) != h0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
// crash, since a connected DLL keeps the per-pid IPC section alive.
|
||||
#pragma once
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// True if `pid` already hosts a live coop_hook DLL: the per-pid IPC section exists and its heartbeat
|
||||
// advances within `timeout_ms` (the DLL's worker is still beating). Returns as soon as a beat lands,
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
const char* to_string(InjectStatus status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
switch (status) {
|
||||
case InjectStatus::Ok:
|
||||
return "OK";
|
||||
case InjectStatus::OpenProcessFailed:
|
||||
@@ -33,8 +31,7 @@ const char* to_string(InjectStatus status)
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
InjectResult fail(InjectStatus status)
|
||||
{
|
||||
@@ -46,16 +43,14 @@ bool is_wow64_process(HANDLE process)
|
||||
{
|
||||
USHORT process_machine = IMAGE_FILE_MACHINE_UNKNOWN;
|
||||
USHORT native_machine = IMAGE_FILE_MACHINE_UNKNOWN;
|
||||
if (IsWow64Process2(process, &process_machine, &native_machine))
|
||||
{
|
||||
if (IsWow64Process2(process, &process_machine, &native_machine)) {
|
||||
return process_machine != IMAGE_FILE_MACHINE_UNKNOWN;
|
||||
}
|
||||
// IsWow64Process2 failed -- fall back to the legacy query rather than guessing "native", since
|
||||
// guessing wrong sends the x64 DLL into a 32-bit target (which can't load it). Only if BOTH
|
||||
// queries fail do we fall back to permissive.
|
||||
BOOL wow64 = FALSE;
|
||||
if (IsWow64Process(process, &wow64))
|
||||
{
|
||||
if (IsWow64Process(process, &wow64)) {
|
||||
return wow64 != FALSE;
|
||||
}
|
||||
return false; // both queries failed; best-effort assume native
|
||||
@@ -75,9 +70,8 @@ InjectResult inject_via_helper(unsigned long pid, const std::wstring& dll_path)
|
||||
{
|
||||
const std::wstring helper = sibling(dll_path, L"coop_inject_x86.exe");
|
||||
const std::wstring x86_dll = sibling(dll_path, L"coop_hook_x86.dll");
|
||||
if (GetFileAttributesW(helper.c_str()) == INVALID_FILE_ATTRIBUTES ||
|
||||
GetFileAttributesW(x86_dll.c_str()) == INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
if (GetFileAttributesW(helper.c_str()) == INVALID_FILE_ATTRIBUTES
|
||||
|| GetFileAttributesW(x86_dll.c_str()) == INVALID_FILE_ATTRIBUTES) {
|
||||
return InjectResult{InjectStatus::HelperNotFound, 0};
|
||||
}
|
||||
|
||||
@@ -87,9 +81,8 @@ InjectResult inject_via_helper(unsigned long pid, const std::wstring& dll_path)
|
||||
STARTUPINFOW si{};
|
||||
si.cb = sizeof(si);
|
||||
PROCESS_INFORMATION pi{};
|
||||
if (!CreateProcessW(helper.c_str(), cmd.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, nullptr,
|
||||
&si, &pi))
|
||||
{
|
||||
if (!CreateProcessW(helper.c_str(), cmd.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, nullptr, &si,
|
||||
&pi)) {
|
||||
return fail(InjectStatus::HelperFailed);
|
||||
}
|
||||
WaitForSingleObject(pi.hProcess, INFINITE);
|
||||
@@ -98,8 +91,7 @@ InjectResult inject_via_helper(unsigned long pid, const std::wstring& dll_path)
|
||||
const DWORD err = got ? exit_code : GetLastError(); // on a failed query, surface the OS error
|
||||
CloseHandle(pi.hThread);
|
||||
CloseHandle(pi.hProcess);
|
||||
if (!got || exit_code != 0)
|
||||
{
|
||||
if (!got || exit_code != 0) {
|
||||
return InjectResult{InjectStatus::HelperFailed, err};
|
||||
}
|
||||
return InjectResult{InjectStatus::Ok, 0};
|
||||
@@ -109,63 +101,51 @@ InjectResult inject_via_helper(unsigned long pid, const std::wstring& dll_path)
|
||||
|
||||
InjectResult inject_dll(unsigned long pid, const std::wstring& dll_path)
|
||||
{
|
||||
if (GetFileAttributesW(dll_path.c_str()) == INVALID_FILE_ATTRIBUTES)
|
||||
{
|
||||
if (GetFileAttributesW(dll_path.c_str()) == INVALID_FILE_ATTRIBUTES) {
|
||||
return fail(InjectStatus::DllNotFound);
|
||||
}
|
||||
|
||||
const DWORD access = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION |
|
||||
PROCESS_VM_WRITE | PROCESS_VM_READ;
|
||||
const DWORD access =
|
||||
PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ;
|
||||
HANDLE process = OpenProcess(access, FALSE, pid);
|
||||
if (process == nullptr)
|
||||
{
|
||||
if (process == nullptr) {
|
||||
return fail(InjectStatus::OpenProcessFailed);
|
||||
}
|
||||
|
||||
struct HandleGuard
|
||||
{
|
||||
struct HandleGuard {
|
||||
HANDLE h;
|
||||
~HandleGuard()
|
||||
{
|
||||
if (h != nullptr)
|
||||
{
|
||||
if (h != nullptr) {
|
||||
CloseHandle(h);
|
||||
}
|
||||
}
|
||||
} process_guard{process};
|
||||
|
||||
if (is_wow64_process(process))
|
||||
{
|
||||
if (is_wow64_process(process)) {
|
||||
// The x64 host can't inject a 32-bit target directly; delegate to the helper.
|
||||
return inject_via_helper(pid, dll_path);
|
||||
}
|
||||
|
||||
const SIZE_T bytes = (dll_path.size() + 1) * sizeof(wchar_t);
|
||||
void* remote = VirtualAllocEx(process, nullptr, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
||||
if (remote == nullptr)
|
||||
{
|
||||
if (remote == nullptr) {
|
||||
return fail(InjectStatus::AllocFailed);
|
||||
}
|
||||
|
||||
InjectResult result{InjectStatus::Ok, 0};
|
||||
|
||||
if (!WriteProcessMemory(process, remote, dll_path.c_str(), bytes, nullptr))
|
||||
{
|
||||
if (!WriteProcessMemory(process, remote, dll_path.c_str(), bytes, nullptr)) {
|
||||
result = fail(InjectStatus::WriteFailed);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// kernel32 is mapped at the same address in every process, so LoadLibraryW's
|
||||
// address in this process is valid as the remote thread's start routine.
|
||||
auto load_library =
|
||||
reinterpret_cast<LPTHREAD_START_ROUTINE>(GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "LoadLibraryW"));
|
||||
HANDLE thread = CreateRemoteThread(process, nullptr, 0, load_library, remote, 0, nullptr);
|
||||
if (thread == nullptr)
|
||||
{
|
||||
if (thread == nullptr) {
|
||||
result = fail(InjectStatus::RemoteThreadFailed);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
WaitForSingleObject(thread, INFINITE);
|
||||
DWORD exit_code = 0;
|
||||
GetExitCodeThread(thread, &exit_code);
|
||||
@@ -173,8 +153,7 @@ InjectResult inject_dll(unsigned long pid, const std::wstring& dll_path)
|
||||
// LoadLibraryW returns the module handle; 0 means it failed to load.
|
||||
// (The handle is truncated to 32 bits here, but zero vs non-zero is
|
||||
// all we need to distinguish success from failure.)
|
||||
if (exit_code == 0)
|
||||
{
|
||||
if (exit_code == 0) {
|
||||
result = InjectResult{InjectStatus::RemoteLoadFailed, 0};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,9 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
enum class InjectStatus
|
||||
{
|
||||
enum class InjectStatus {
|
||||
Ok,
|
||||
OpenProcessFailed, // insufficient rights (try running the host as admin)
|
||||
BitnessMismatch, // 32-bit target; the x86 hook/helper aren't available
|
||||
@@ -21,8 +19,7 @@ enum class InjectStatus
|
||||
HelperFailed, // the x86 injector helper ran but reported failure
|
||||
};
|
||||
|
||||
struct InjectResult
|
||||
{
|
||||
struct InjectResult {
|
||||
InjectStatus status = InjectStatus::OpenProcessFailed;
|
||||
unsigned long os_error = 0; // GetLastError at the point of failure, if any
|
||||
};
|
||||
|
||||
@@ -6,80 +6,120 @@
|
||||
#include "injection_panel.hpp"
|
||||
#include "inject/mkb_map.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
// Map an ImGui key to a Win32 virtual-key. Returns 0 for keys we don't forward.
|
||||
int imgui_key_to_vk(ImGuiKey k)
|
||||
{
|
||||
if (k >= ImGuiKey_A && k <= ImGuiKey_Z)
|
||||
{
|
||||
if (k >= ImGuiKey_A && k <= ImGuiKey_Z) {
|
||||
return 'A' + (k - ImGuiKey_A);
|
||||
}
|
||||
if (k >= ImGuiKey_0 && k <= ImGuiKey_9)
|
||||
{
|
||||
if (k >= ImGuiKey_0 && k <= ImGuiKey_9) {
|
||||
return '0' + (k - ImGuiKey_0);
|
||||
}
|
||||
if (k >= ImGuiKey_Keypad0 && k <= ImGuiKey_Keypad9)
|
||||
{
|
||||
if (k >= ImGuiKey_Keypad0 && k <= ImGuiKey_Keypad9) {
|
||||
return VK_NUMPAD0 + (k - ImGuiKey_Keypad0);
|
||||
}
|
||||
if (k >= ImGuiKey_F1 && k <= ImGuiKey_F12)
|
||||
{
|
||||
if (k >= ImGuiKey_F1 && k <= ImGuiKey_F12) {
|
||||
return VK_F1 + (k - ImGuiKey_F1);
|
||||
}
|
||||
switch (k)
|
||||
{
|
||||
case ImGuiKey_Tab: return VK_TAB;
|
||||
case ImGuiKey_LeftArrow: return VK_LEFT;
|
||||
case ImGuiKey_RightArrow: return VK_RIGHT;
|
||||
case ImGuiKey_UpArrow: return VK_UP;
|
||||
case ImGuiKey_DownArrow: return VK_DOWN;
|
||||
case ImGuiKey_PageUp: return VK_PRIOR;
|
||||
case ImGuiKey_PageDown: return VK_NEXT;
|
||||
case ImGuiKey_Home: return VK_HOME;
|
||||
case ImGuiKey_End: return VK_END;
|
||||
case ImGuiKey_Insert: return VK_INSERT;
|
||||
case ImGuiKey_Delete: return VK_DELETE;
|
||||
case ImGuiKey_Backspace: return VK_BACK;
|
||||
case ImGuiKey_Space: return VK_SPACE;
|
||||
case ImGuiKey_Enter: return VK_RETURN;
|
||||
case ImGuiKey_Escape: return VK_ESCAPE;
|
||||
case ImGuiKey_LeftCtrl: return VK_LCONTROL;
|
||||
case ImGuiKey_LeftShift: return VK_LSHIFT;
|
||||
case ImGuiKey_LeftAlt: return VK_LMENU;
|
||||
case ImGuiKey_LeftSuper: return VK_LWIN;
|
||||
case ImGuiKey_RightCtrl: return VK_RCONTROL;
|
||||
case ImGuiKey_RightShift: return VK_RSHIFT;
|
||||
case ImGuiKey_RightAlt: return VK_RMENU;
|
||||
case ImGuiKey_RightSuper: return VK_RWIN;
|
||||
case ImGuiKey_Menu: return VK_APPS;
|
||||
case ImGuiKey_Apostrophe: return VK_OEM_7;
|
||||
case ImGuiKey_Comma: return VK_OEM_COMMA;
|
||||
case ImGuiKey_Minus: return VK_OEM_MINUS;
|
||||
case ImGuiKey_Period: return VK_OEM_PERIOD;
|
||||
case ImGuiKey_Slash: return VK_OEM_2;
|
||||
case ImGuiKey_Semicolon: return VK_OEM_1;
|
||||
case ImGuiKey_Equal: return VK_OEM_PLUS;
|
||||
case ImGuiKey_LeftBracket: return VK_OEM_4;
|
||||
case ImGuiKey_Backslash: return VK_OEM_5;
|
||||
case ImGuiKey_RightBracket: return VK_OEM_6;
|
||||
case ImGuiKey_GraveAccent: return VK_OEM_3;
|
||||
case ImGuiKey_CapsLock: return VK_CAPITAL;
|
||||
case ImGuiKey_ScrollLock: return VK_SCROLL;
|
||||
case ImGuiKey_NumLock: return VK_NUMLOCK;
|
||||
case ImGuiKey_PrintScreen: return VK_SNAPSHOT;
|
||||
case ImGuiKey_Pause: return VK_PAUSE;
|
||||
case ImGuiKey_KeypadDecimal: return VK_DECIMAL;
|
||||
case ImGuiKey_KeypadDivide: return VK_DIVIDE;
|
||||
case ImGuiKey_KeypadMultiply: return VK_MULTIPLY;
|
||||
case ImGuiKey_KeypadSubtract: return VK_SUBTRACT;
|
||||
case ImGuiKey_KeypadAdd: return VK_ADD;
|
||||
case ImGuiKey_KeypadEnter: return VK_RETURN;
|
||||
default: return 0;
|
||||
switch (k) {
|
||||
case ImGuiKey_Tab:
|
||||
return VK_TAB;
|
||||
case ImGuiKey_LeftArrow:
|
||||
return VK_LEFT;
|
||||
case ImGuiKey_RightArrow:
|
||||
return VK_RIGHT;
|
||||
case ImGuiKey_UpArrow:
|
||||
return VK_UP;
|
||||
case ImGuiKey_DownArrow:
|
||||
return VK_DOWN;
|
||||
case ImGuiKey_PageUp:
|
||||
return VK_PRIOR;
|
||||
case ImGuiKey_PageDown:
|
||||
return VK_NEXT;
|
||||
case ImGuiKey_Home:
|
||||
return VK_HOME;
|
||||
case ImGuiKey_End:
|
||||
return VK_END;
|
||||
case ImGuiKey_Insert:
|
||||
return VK_INSERT;
|
||||
case ImGuiKey_Delete:
|
||||
return VK_DELETE;
|
||||
case ImGuiKey_Backspace:
|
||||
return VK_BACK;
|
||||
case ImGuiKey_Space:
|
||||
return VK_SPACE;
|
||||
case ImGuiKey_Enter:
|
||||
return VK_RETURN;
|
||||
case ImGuiKey_Escape:
|
||||
return VK_ESCAPE;
|
||||
case ImGuiKey_LeftCtrl:
|
||||
return VK_LCONTROL;
|
||||
case ImGuiKey_LeftShift:
|
||||
return VK_LSHIFT;
|
||||
case ImGuiKey_LeftAlt:
|
||||
return VK_LMENU;
|
||||
case ImGuiKey_LeftSuper:
|
||||
return VK_LWIN;
|
||||
case ImGuiKey_RightCtrl:
|
||||
return VK_RCONTROL;
|
||||
case ImGuiKey_RightShift:
|
||||
return VK_RSHIFT;
|
||||
case ImGuiKey_RightAlt:
|
||||
return VK_RMENU;
|
||||
case ImGuiKey_RightSuper:
|
||||
return VK_RWIN;
|
||||
case ImGuiKey_Menu:
|
||||
return VK_APPS;
|
||||
case ImGuiKey_Apostrophe:
|
||||
return VK_OEM_7;
|
||||
case ImGuiKey_Comma:
|
||||
return VK_OEM_COMMA;
|
||||
case ImGuiKey_Minus:
|
||||
return VK_OEM_MINUS;
|
||||
case ImGuiKey_Period:
|
||||
return VK_OEM_PERIOD;
|
||||
case ImGuiKey_Slash:
|
||||
return VK_OEM_2;
|
||||
case ImGuiKey_Semicolon:
|
||||
return VK_OEM_1;
|
||||
case ImGuiKey_Equal:
|
||||
return VK_OEM_PLUS;
|
||||
case ImGuiKey_LeftBracket:
|
||||
return VK_OEM_4;
|
||||
case ImGuiKey_Backslash:
|
||||
return VK_OEM_5;
|
||||
case ImGuiKey_RightBracket:
|
||||
return VK_OEM_6;
|
||||
case ImGuiKey_GraveAccent:
|
||||
return VK_OEM_3;
|
||||
case ImGuiKey_CapsLock:
|
||||
return VK_CAPITAL;
|
||||
case ImGuiKey_ScrollLock:
|
||||
return VK_SCROLL;
|
||||
case ImGuiKey_NumLock:
|
||||
return VK_NUMLOCK;
|
||||
case ImGuiKey_PrintScreen:
|
||||
return VK_SNAPSHOT;
|
||||
case ImGuiKey_Pause:
|
||||
return VK_PAUSE;
|
||||
case ImGuiKey_KeypadDecimal:
|
||||
return VK_DECIMAL;
|
||||
case ImGuiKey_KeypadDivide:
|
||||
return VK_DIVIDE;
|
||||
case ImGuiKey_KeypadMultiply:
|
||||
return VK_MULTIPLY;
|
||||
case ImGuiKey_KeypadSubtract:
|
||||
return VK_SUBTRACT;
|
||||
case ImGuiKey_KeypadAdd:
|
||||
return VK_ADD;
|
||||
case ImGuiKey_KeypadEnter:
|
||||
return VK_RETURN;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,10 +135,8 @@ bool g_key_down[256] = {}; // indexed by VK
|
||||
|
||||
void release_held_keys(InjectionPanel& injection)
|
||||
{
|
||||
for (int vk = 0; vk < 256; ++vk)
|
||||
{
|
||||
if (g_key_down[vk])
|
||||
{
|
||||
for (int vk = 0; vk < 256; ++vk) {
|
||||
if (g_key_down[vk]) {
|
||||
injection.push_mkb(MkbEvent{Mkb_KeyUp, static_cast<std::uint32_t>(vk), 0, 0});
|
||||
g_key_down[vk] = false;
|
||||
}
|
||||
@@ -107,10 +145,8 @@ void release_held_keys(InjectionPanel& injection)
|
||||
|
||||
void release_held_mouse(InjectionPanel& injection)
|
||||
{
|
||||
for (int b = 0; b < 3; ++b)
|
||||
{
|
||||
if (g_mouse_down[b])
|
||||
{
|
||||
for (int b = 0; b < 3; ++b) {
|
||||
if (g_mouse_down[b]) {
|
||||
injection.push_mkb(MkbEvent{Mkb_MouseUp, static_cast<std::uint32_t>(b), g_last_gx, g_last_gy});
|
||||
g_mouse_down[b] = false;
|
||||
}
|
||||
@@ -126,8 +162,7 @@ void forward_mkb_frame(InjectionPanel& injection, HWND host_hwnd, bool mirroring
|
||||
// own desktop use isn't injected. If we can't forward for ANY reason -- subsystem off, we lost
|
||||
// focus, or the game is gone -- release everything we're still holding first, so a key/button
|
||||
// held at that moment doesn't stick down in the guest.
|
||||
if (!injection.mkb_enabled() || GetForegroundWindow() != host_hwnd || game == nullptr || !IsWindow(game))
|
||||
{
|
||||
if (!injection.mkb_enabled() || GetForegroundWindow() != host_hwnd || game == nullptr || !IsWindow(game)) {
|
||||
release_held_keys(injection);
|
||||
release_held_mouse(injection);
|
||||
return;
|
||||
@@ -136,35 +171,26 @@ void forward_mkb_frame(InjectionPanel& injection, HWND host_hwnd, bool mirroring
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
// --- Keyboard (unless ImGui is using it for e.g. a text field -- then release what we hold) ---
|
||||
if (io.WantCaptureKeyboard)
|
||||
{
|
||||
if (io.WantCaptureKeyboard) {
|
||||
release_held_keys(injection);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (ImGuiKey k = ImGuiKey_NamedKey_BEGIN; k < ImGuiKey_NamedKey_END; k = static_cast<ImGuiKey>(k + 1))
|
||||
{
|
||||
} else {
|
||||
for (ImGuiKey k = ImGuiKey_NamedKey_BEGIN; k < ImGuiKey_NamedKey_END; k = static_cast<ImGuiKey>(k + 1)) {
|
||||
const int vk = imgui_key_to_vk(k);
|
||||
if (vk == 0)
|
||||
{
|
||||
if (vk == 0) {
|
||||
continue;
|
||||
}
|
||||
if (ImGui::IsKeyPressed(k, false))
|
||||
{
|
||||
if (ImGui::IsKeyPressed(k, false)) {
|
||||
injection.push_mkb(MkbEvent{Mkb_KeyDown, static_cast<std::uint32_t>(vk), 0, 0});
|
||||
g_key_down[vk & 0xFF] = true;
|
||||
}
|
||||
if (ImGui::IsKeyReleased(k))
|
||||
{
|
||||
if (ImGui::IsKeyReleased(k)) {
|
||||
injection.push_mkb(MkbEvent{Mkb_KeyUp, static_cast<std::uint32_t>(vk), 0, 0});
|
||||
g_key_down[vk & 0xFF] = false;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < io.InputQueueCharacters.Size; ++i)
|
||||
{
|
||||
for (int i = 0; i < io.InputQueueCharacters.Size; ++i) {
|
||||
const ImWchar c = io.InputQueueCharacters[i];
|
||||
if (c != 0)
|
||||
{
|
||||
if (c != 0) {
|
||||
injection.push_mkb(MkbEvent{Mkb_Char, static_cast<std::uint32_t>(c), 0, 0});
|
||||
}
|
||||
}
|
||||
@@ -173,8 +199,7 @@ void forward_mkb_frame(InjectionPanel& injection, HWND host_hwnd, bool mirroring
|
||||
// --- Mouse (clicks + wheel only, and only while mirroring and ImGui isn't using the mouse) ---
|
||||
// When we're not forwarding the mouse, still release any button we hold (below), so it can't stick.
|
||||
const bool forwarding_mouse = mirroring && !io.WantCaptureMouse;
|
||||
if (!forwarding_mouse)
|
||||
{
|
||||
if (!forwarding_mouse) {
|
||||
release_held_mouse(injection);
|
||||
return;
|
||||
}
|
||||
@@ -187,15 +212,12 @@ void forward_mkb_frame(InjectionPanel& injection, HWND host_hwnd, bool mirroring
|
||||
m.host_y = static_cast<int>(io.MousePos.y);
|
||||
m.dst_w = host_client.right;
|
||||
m.dst_h = host_client.bottom;
|
||||
if (source_hooked)
|
||||
{
|
||||
if (source_hooked) {
|
||||
// Hooked capture mirrors the backbuffer (client area), no decorations.
|
||||
const VideoShareView v = injection.video_share();
|
||||
m.src_w = m.client_w = static_cast<int>(v.width);
|
||||
m.src_h = m.client_h = static_cast<int>(v.height);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// WGC captures the whole window; the client area sits at a decoration offset.
|
||||
RECT wr{}, cr{};
|
||||
POINT client_origin{0, 0};
|
||||
@@ -212,8 +234,7 @@ void forward_mkb_frame(InjectionPanel& injection, HWND host_hwnd, bool mirroring
|
||||
|
||||
int gx = 0, gy = 0;
|
||||
const bool on_game = map_host_to_game_client(m, gx, gy);
|
||||
if (on_game)
|
||||
{
|
||||
if (on_game) {
|
||||
g_last_gx = gx;
|
||||
g_last_gy = gy;
|
||||
}
|
||||
@@ -222,8 +243,7 @@ void forward_mkb_frame(InjectionPanel& injection, HWND host_hwnd, bool mirroring
|
||||
|
||||
for (int button = 0; button < 3; ++button) // 0=left, 1=right, 2=middle
|
||||
{
|
||||
if (on_game && ImGui::IsMouseClicked(button))
|
||||
{
|
||||
if (on_game && ImGui::IsMouseClicked(button)) {
|
||||
injection.push_mkb(MkbEvent{Mkb_MouseDown, static_cast<std::uint32_t>(button), mx, my});
|
||||
g_mouse_down[button] = true;
|
||||
}
|
||||
@@ -233,8 +253,7 @@ void forward_mkb_frame(InjectionPanel& injection, HWND host_hwnd, bool mirroring
|
||||
g_mouse_down[button] = false;
|
||||
}
|
||||
}
|
||||
if (on_game && io.MouseWheel != 0.0f)
|
||||
{
|
||||
if (on_game && io.MouseWheel != 0.0f) {
|
||||
const int delta = static_cast<int>(io.MouseWheel * WHEEL_DELTA);
|
||||
injection.push_mkb(MkbEvent{Mkb_Wheel, static_cast<std::uint32_t>(delta), mx, my});
|
||||
}
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class InjectionPanel;
|
||||
|
||||
|
||||
@@ -12,11 +12,9 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct MkbMapInput
|
||||
{
|
||||
struct MkbMapInput {
|
||||
int host_x = 0, host_y = 0; // mouse in host-window client pixels
|
||||
int dst_w = 0, dst_h = 0; // host window client size
|
||||
int src_w = 0, src_h = 0; // captured frame size (WGC=window, Hooked=backbuffer)
|
||||
@@ -28,28 +26,24 @@ struct MkbMapInput
|
||||
// client area; false if it falls on a letterbox bar or the window decorations.
|
||||
inline bool map_host_to_game_client(const MkbMapInput& in, int& gx, int& gy)
|
||||
{
|
||||
if (in.src_w <= 0 || in.src_h <= 0 || in.dst_w <= 0 || in.dst_h <= 0 || in.client_w <= 0 || in.client_h <= 0)
|
||||
{
|
||||
if (in.src_w <= 0 || in.src_h <= 0 || in.dst_w <= 0 || in.dst_h <= 0 || in.client_w <= 0 || in.client_h <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Invert the letterbox: the frame is fit (aspect-preserved) and centered in dst.
|
||||
const double scale =
|
||||
std::min(static_cast<double>(in.dst_w) / in.src_w, static_cast<double>(in.dst_h) / in.src_h);
|
||||
const double scale = std::min(static_cast<double>(in.dst_w) / in.src_w, static_cast<double>(in.dst_h) / in.src_h);
|
||||
const double ox = (in.dst_w - in.src_w * scale) * 0.5;
|
||||
const double oy = (in.dst_h - in.src_h * scale) * 0.5;
|
||||
|
||||
const double fx = (in.host_x - ox) / scale; // position in captured-frame pixels
|
||||
const double fy = (in.host_y - oy) / scale;
|
||||
if (fx < 0.0 || fy < 0.0 || fx >= in.src_w || fy >= in.src_h)
|
||||
{
|
||||
if (fx < 0.0 || fy < 0.0 || fx >= in.src_w || fy >= in.src_h) {
|
||||
return false; // on a letterbox bar
|
||||
}
|
||||
|
||||
const double cx = fx - in.client_off_x; // into client space
|
||||
const double cy = fy - in.client_off_y;
|
||||
if (cx < 0.0 || cy < 0.0 || cx >= in.client_w || cy >= in.client_h)
|
||||
{
|
||||
if (cx < 0.0 || cy < 0.0 || cx >= in.client_w || cy >= in.client_h) {
|
||||
return false; // on the window decorations
|
||||
}
|
||||
|
||||
|
||||
@@ -5,27 +5,22 @@
|
||||
#include <windows.h>
|
||||
#include <tlhelp32.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
std::vector<ProcessEntry> list_processes()
|
||||
{
|
||||
std::vector<ProcessEntry> result;
|
||||
|
||||
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if (snapshot == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (snapshot == INVALID_HANDLE_VALUE) {
|
||||
return result;
|
||||
}
|
||||
|
||||
PROCESSENTRY32W entry = {};
|
||||
entry.dwSize = sizeof(entry);
|
||||
if (Process32FirstW(snapshot, &entry))
|
||||
{
|
||||
do
|
||||
{
|
||||
if (entry.th32ProcessID == 0)
|
||||
{
|
||||
if (Process32FirstW(snapshot, &entry)) {
|
||||
do {
|
||||
if (entry.th32ProcessID == 0) {
|
||||
continue;
|
||||
}
|
||||
result.push_back(ProcessEntry{entry.th32ProcessID, entry.szExeFile});
|
||||
|
||||
@@ -4,11 +4,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct ProcessEntry
|
||||
{
|
||||
struct ProcessEntry {
|
||||
unsigned long pid = 0;
|
||||
std::wstring exe_name; // image base name, e.g. "game.exe"
|
||||
};
|
||||
|
||||
@@ -7,14 +7,11 @@
|
||||
|
||||
#include "inject/process_list.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
struct EnumCtx
|
||||
{
|
||||
struct EnumCtx {
|
||||
std::vector<WindowEntry>* out;
|
||||
const std::unordered_map<unsigned long, std::wstring>* names;
|
||||
DWORD self_pid;
|
||||
@@ -25,23 +22,19 @@ BOOL CALLBACK enum_proc(HWND hwnd, LPARAM lparam)
|
||||
auto* ctx = reinterpret_cast<EnumCtx*>(lparam);
|
||||
|
||||
// Keep only "alt-tab" windows: visible, titled, root-owner, non-tool, not ours.
|
||||
if (!IsWindowVisible(hwnd) || GetAncestor(hwnd, GA_ROOTOWNER) != hwnd)
|
||||
{
|
||||
if (!IsWindowVisible(hwnd) || GetAncestor(hwnd, GA_ROOTOWNER) != hwnd) {
|
||||
return TRUE;
|
||||
}
|
||||
const int len = GetWindowTextLengthW(hwnd);
|
||||
if (len <= 0)
|
||||
{
|
||||
if (len <= 0) {
|
||||
return TRUE;
|
||||
}
|
||||
if ((GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) != 0)
|
||||
{
|
||||
if ((GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) != 0) {
|
||||
return TRUE;
|
||||
}
|
||||
DWORD pid = 0;
|
||||
GetWindowThreadProcessId(hwnd, &pid);
|
||||
if (pid == 0 || pid == ctx->self_pid)
|
||||
{
|
||||
if (pid == 0 || pid == ctx->self_pid) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -49,8 +42,7 @@ BOOL CALLBACK enum_proc(HWND hwnd, LPARAM lparam)
|
||||
GetWindowTextW(hwnd, title.data(), len + 1);
|
||||
|
||||
std::wstring exe;
|
||||
if (const auto it = ctx->names->find(pid); it != ctx->names->end())
|
||||
{
|
||||
if (const auto it = ctx->names->find(pid); it != ctx->names->end()) {
|
||||
exe = it->second;
|
||||
}
|
||||
ctx->out->push_back(WindowEntry{pid, hwnd, std::move(title), std::move(exe)});
|
||||
@@ -64,8 +56,7 @@ std::vector<WindowEntry> list_windows()
|
||||
// pid -> image name, so each window can show its owning process without a separate
|
||||
// OpenProcess per window.
|
||||
std::unordered_map<unsigned long, std::wstring> names;
|
||||
for (const ProcessEntry& p : list_processes())
|
||||
{
|
||||
for (const ProcessEntry& p : list_processes()) {
|
||||
names.emplace(p.pid, p.exe_name);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,9 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct WindowEntry
|
||||
{
|
||||
struct WindowEntry {
|
||||
unsigned long pid = 0; // owning process id
|
||||
void* hwnd = nullptr; // HWND (opaque here to keep windows.h out of the header)
|
||||
std::wstring title; // window caption
|
||||
|
||||
@@ -12,11 +12,9 @@
|
||||
#include "ui/text_match.hpp"
|
||||
#include "util/utf8.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
const ImVec4 kGreen(0.4f, 1.0f, 0.4f, 1.0f);
|
||||
const ImVec4 kRed(1.0f, 0.45f, 0.4f, 1.0f);
|
||||
@@ -40,8 +38,7 @@ std::wstring hook_dll_path()
|
||||
const DWORD len = GetModuleFileNameW(nullptr, buffer, MAX_PATH);
|
||||
std::wstring path(buffer, len);
|
||||
const std::size_t slash = path.find_last_of(L"\\/");
|
||||
if (slash != std::wstring::npos)
|
||||
{
|
||||
if (slash != std::wstring::npos) {
|
||||
path.resize(slash + 1);
|
||||
}
|
||||
path += L"coop_hook.dll";
|
||||
@@ -67,8 +64,7 @@ InjectionPanel::~InjectionPanel()
|
||||
// stays injected, dormant). Short timeout -- the flags persist in the section the DLL keeps
|
||||
// alive, so the unhook completes even if the process exits before it confirms.
|
||||
disconnect_graceful(/*timeout_ms=*/300);
|
||||
if (vk_layer_enabled_)
|
||||
{
|
||||
if (vk_layer_enabled_) {
|
||||
unregister_vk_layer(); // don't leave the implicit layer registered after the tool closes
|
||||
}
|
||||
close_target_handle();
|
||||
@@ -80,11 +76,9 @@ void InjectionPanel::disconnect_graceful(int timeout_ms)
|
||||
// (bounded) for it to confirm before we drop the channel. The flags persist in the section the
|
||||
// DLL keeps alive, so it unhooks even if we time out or exit first -- the wait just lets us
|
||||
// observe a clean game. The DLL is left injected (dormant) for a later reconnect; we never eject.
|
||||
if (server_.running())
|
||||
{
|
||||
if (server_.running()) {
|
||||
server_.request_unhook_all();
|
||||
for (int waited = 0; waited < timeout_ms && !server_.all_hooks_removed(); waited += 10)
|
||||
{
|
||||
for (int waited = 0; waited < timeout_ms && !server_.all_hooks_removed(); waited += 10) {
|
||||
Sleep(10);
|
||||
}
|
||||
}
|
||||
@@ -95,8 +89,7 @@ void InjectionPanel::disconnect_graceful(int timeout_ms)
|
||||
|
||||
void InjectionPanel::close_target_handle()
|
||||
{
|
||||
if (target_process_ != nullptr)
|
||||
{
|
||||
if (target_process_ != nullptr) {
|
||||
CloseHandle(target_process_);
|
||||
target_process_ = nullptr;
|
||||
}
|
||||
@@ -114,23 +107,19 @@ void InjectionPanel::tick()
|
||||
|
||||
void InjectionPanel::auto_reattach_tick()
|
||||
{
|
||||
if (!auto_reattach_ || target_state_ != TargetState::Terminated || selected_name_.empty())
|
||||
{
|
||||
if (!auto_reattach_ || target_state_ != TargetState::Terminated || selected_name_.empty()) {
|
||||
return;
|
||||
}
|
||||
// Poll the process list a couple of times a second (cheap, and we want to catch the
|
||||
// relaunch early to read the exact audio format before the game creates its client).
|
||||
const double now = ImGui::GetTime();
|
||||
if (now - last_auto_poll_ < 0.5)
|
||||
{
|
||||
if (now - last_auto_poll_ < 0.5) {
|
||||
return;
|
||||
}
|
||||
last_auto_poll_ = now;
|
||||
refresh_processes();
|
||||
for (const ProcessEntry& e : processes_)
|
||||
{
|
||||
if (iequals_name(e.exe_name, selected_name_))
|
||||
{
|
||||
for (const ProcessEntry& e : processes_) {
|
||||
if (iequals_name(e.exe_name, selected_name_)) {
|
||||
// The same game relaunched -> tear down the stale channel and re-attach to it.
|
||||
server_.stop();
|
||||
close_target_handle();
|
||||
@@ -144,8 +133,7 @@ void InjectionPanel::auto_reattach_tick()
|
||||
|
||||
void InjectionPanel::update_liveness()
|
||||
{
|
||||
if (!injected_)
|
||||
{
|
||||
if (!injected_) {
|
||||
target_state_ = TargetState::NotInjected;
|
||||
return;
|
||||
}
|
||||
@@ -153,8 +141,7 @@ void InjectionPanel::update_liveness()
|
||||
// Process gone? The handle was opened with SYNCHRONIZE at inject time, so a
|
||||
// signaled wait means it exited. This is authoritative even if the heartbeat
|
||||
// happened to look alive a moment ago.
|
||||
if (target_process_ != nullptr && WaitForSingleObject(target_process_, 0) == WAIT_OBJECT_0)
|
||||
{
|
||||
if (target_process_ != nullptr && WaitForSingleObject(target_process_, 0) == WAIT_OBJECT_0) {
|
||||
target_state_ = TargetState::Terminated;
|
||||
dll_alive_ = false;
|
||||
return;
|
||||
@@ -164,14 +151,11 @@ void InjectionPanel::update_liveness()
|
||||
// process whose heartbeat stalled for ~2 s is frozen, not gone -- a distinct state.
|
||||
const std::uint32_t hb = server_.hook_status().heartbeat;
|
||||
const double now = ImGui::GetTime();
|
||||
if (hb != last_heartbeat_)
|
||||
{
|
||||
if (hb != last_heartbeat_) {
|
||||
last_heartbeat_ = hb;
|
||||
last_heartbeat_time_ = now;
|
||||
dll_alive_ = true;
|
||||
}
|
||||
else if (now - last_heartbeat_time_ > 2.0)
|
||||
{
|
||||
} else if (now - last_heartbeat_time_ > 2.0) {
|
||||
dll_alive_ = false;
|
||||
}
|
||||
target_state_ = dll_alive_ ? TargetState::Alive : TargetState::Hung;
|
||||
@@ -211,23 +195,21 @@ void InjectionPanel::reconnect_selected()
|
||||
// Re-attach to a DLL that's already injected and alive (a prior session left it dormant after a
|
||||
// graceful disconnect, or the tool restarted): bring the channel back up on the SAME per-pid
|
||||
// section the DLL still holds and re-publish the desired subsystem state -- no re-injection.
|
||||
if (!server_.start(selected_pid_))
|
||||
{
|
||||
if (!server_.start(selected_pid_)) {
|
||||
status_ = "Failed to re-attach shared memory.";
|
||||
status_color_ = kRed;
|
||||
return;
|
||||
}
|
||||
publish_subsystem_state();
|
||||
begin_liveness_tracking();
|
||||
status_ = "Reconnected to " + narrow(selected_name_) + " (pid " + std::to_string(selected_pid_) +
|
||||
") -- reused the injected DLL.";
|
||||
status_ = "Reconnected to " + narrow(selected_name_) + " (pid " + std::to_string(selected_pid_)
|
||||
+ ") -- reused the injected DLL.";
|
||||
status_color_ = kGreen;
|
||||
}
|
||||
|
||||
void InjectionPanel::inject_selected()
|
||||
{
|
||||
if (selected_pid_ == 0)
|
||||
{
|
||||
if (selected_pid_ == 0) {
|
||||
status_ = "Select a target process first.";
|
||||
status_color_ = kRed;
|
||||
return;
|
||||
@@ -236,16 +218,14 @@ void InjectionPanel::inject_selected()
|
||||
// If our DLL is already injected and alive in this target (left dormant by a graceful disconnect,
|
||||
// or surviving a tool restart -- it keeps the section alive), reconnect to it instead of
|
||||
// injecting a second time.
|
||||
if (hook_dll_alive(selected_pid_))
|
||||
{
|
||||
if (hook_dll_alive(selected_pid_)) {
|
||||
reconnect_selected();
|
||||
return;
|
||||
}
|
||||
|
||||
// Bring up the shared-memory channel before injecting so the hook finds it
|
||||
// immediately on load.
|
||||
if (!server_.start(selected_pid_))
|
||||
{
|
||||
if (!server_.start(selected_pid_)) {
|
||||
status_ = "Failed to create shared memory.";
|
||||
status_color_ = kRed;
|
||||
return;
|
||||
@@ -254,18 +234,14 @@ void InjectionPanel::inject_selected()
|
||||
publish_subsystem_state();
|
||||
|
||||
const InjectResult result = inject_dll(selected_pid_, hook_dll_path());
|
||||
if (result.status == InjectStatus::Ok)
|
||||
{
|
||||
if (result.status == InjectStatus::Ok) {
|
||||
begin_liveness_tracking();
|
||||
status_ = "Injected into " + narrow(selected_name_) + " (pid " + std::to_string(selected_pid_) + ").";
|
||||
status_color_ = kGreen;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
server_.stop();
|
||||
status_ = std::string("Injection failed: ") + to_string(result.status);
|
||||
if (result.os_error != 0)
|
||||
{
|
||||
if (result.os_error != 0) {
|
||||
status_ += " [err " + std::to_string(result.os_error) + "]";
|
||||
}
|
||||
status_color_ = kRed;
|
||||
@@ -274,31 +250,26 @@ void InjectionPanel::inject_selected()
|
||||
|
||||
void InjectionPanel::reattach()
|
||||
{
|
||||
if (selected_name_.empty())
|
||||
{
|
||||
if (selected_name_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find live processes that share the original target's image name.
|
||||
refresh_processes();
|
||||
std::vector<unsigned long> matches;
|
||||
for (const ProcessEntry& e : processes_)
|
||||
{
|
||||
if (iequals_name(e.exe_name, selected_name_))
|
||||
{
|
||||
for (const ProcessEntry& e : processes_) {
|
||||
if (iequals_name(e.exe_name, selected_name_)) {
|
||||
matches.push_back(e.pid);
|
||||
}
|
||||
}
|
||||
|
||||
const std::string name = narrow(selected_name_);
|
||||
if (matches.empty())
|
||||
{
|
||||
if (matches.empty()) {
|
||||
status_ = "No running \"" + name + "\" to re-attach to.";
|
||||
status_color_ = kRed;
|
||||
return;
|
||||
}
|
||||
if (matches.size() > 1)
|
||||
{
|
||||
if (matches.size() > 1) {
|
||||
// Don't guess which instance: filter the picker to the matches so the operator
|
||||
// chooses, then injects via the normal button.
|
||||
snprintf(filter_, sizeof(filter_), "%s", name.c_str());
|
||||
@@ -320,10 +291,8 @@ void InjectionPanel::reattach()
|
||||
unsigned long InjectionPanel::dev_inject_by_name(const std::wstring& image_name)
|
||||
{
|
||||
refresh_processes();
|
||||
for (const ProcessEntry& e : processes_)
|
||||
{
|
||||
if (iequals_name(e.exe_name, image_name))
|
||||
{
|
||||
for (const ProcessEntry& e : processes_) {
|
||||
if (iequals_name(e.exe_name, image_name)) {
|
||||
selected_pid_ = e.pid;
|
||||
selected_name_ = e.exe_name;
|
||||
inject_selected();
|
||||
@@ -336,8 +305,7 @@ unsigned long InjectionPanel::dev_inject_by_name(const std::wstring& image_name)
|
||||
|
||||
void InjectionPanel::publish(const std::array<PadInfo, kMaxPads>& pads)
|
||||
{
|
||||
if (!test_input_.load(std::memory_order_relaxed))
|
||||
{
|
||||
if (!test_input_.load(std::memory_order_relaxed)) {
|
||||
server_.publish(pads);
|
||||
return;
|
||||
}
|
||||
@@ -356,8 +324,7 @@ void InjectionPanel::publish(const std::array<PadInfo, kMaxPads>& pads)
|
||||
pad.state.packet = static_cast<std::uint32_t>(ms);
|
||||
pad.state.thumb_lx = static_cast<std::int16_t>(std::cos(t) * 30000.0);
|
||||
pad.state.thumb_ly = static_cast<std::int16_t>(std::sin(t) * 30000.0);
|
||||
if ((ms / 1000) % 2 == 0)
|
||||
{
|
||||
if ((ms / 1000) % 2 == 0) {
|
||||
pad.state.buttons |= 0x1000; // XINPUT_GAMEPAD_A
|
||||
}
|
||||
server_.publish(synthetic);
|
||||
@@ -368,35 +335,28 @@ void InjectionPanel::draw_hook_list(const HookStatusView& status)
|
||||
static const char* kSubsysName[] = {"Input", "Focus", "Audio", "Video", "MKB"};
|
||||
|
||||
const std::uint32_t n = status.hook_entry_count < kMaxHookEntries ? status.hook_entry_count : kMaxHookEntries;
|
||||
if (n == 0)
|
||||
{
|
||||
if (n == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ImGui::CollapsingHeader("Installed hooks", ImGuiTreeNodeFlags_DefaultOpen))
|
||||
{
|
||||
if (!ImGui::CollapsingHeader("Installed hooks", ImGuiTreeNodeFlags_DefaultOpen)) {
|
||||
return;
|
||||
}
|
||||
if (ImGui::BeginTable("hooks", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp))
|
||||
{
|
||||
if (ImGui::BeginTable("hooks", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp)) {
|
||||
ImGui::TableSetupColumn("Hook");
|
||||
ImGui::TableSetupColumn("On", ImGuiTableColumnFlags_WidthFixed);
|
||||
ImGui::TableSetupColumn("Calls", ImGuiTableColumnFlags_WidthFixed);
|
||||
ImGui::TableHeadersRow();
|
||||
|
||||
// Group rows by subsystem so related hooks sit together.
|
||||
for (std::uint32_t sub = 0; sub < HookSubsys_Count; ++sub)
|
||||
{
|
||||
for (std::uint32_t sub = 0; sub < HookSubsys_Count; ++sub) {
|
||||
bool header_done = false;
|
||||
for (std::uint32_t i = 0; i < n; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < n; ++i) {
|
||||
const HookEntry& e = status.hook_entries[i];
|
||||
if (e.subsystem != sub)
|
||||
{
|
||||
if (e.subsystem != sub) {
|
||||
continue;
|
||||
}
|
||||
if (!header_done)
|
||||
{
|
||||
if (!header_done) {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextDisabled("%s", kSubsysName[sub < HookSubsys_Count ? sub : 0]);
|
||||
@@ -408,12 +368,9 @@ void InjectionPanel::draw_hook_list(const HookStatusView& status)
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextUnformatted(e.name);
|
||||
ImGui::TableNextColumn();
|
||||
if (e.installed)
|
||||
{
|
||||
if (e.installed) {
|
||||
ImGui::TextColored(kGreen, "yes");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextDisabled("no");
|
||||
}
|
||||
ImGui::TableNextColumn();
|
||||
@@ -428,10 +385,8 @@ void InjectionPanel::draw_hook_list(const HookStatusView& status)
|
||||
static bool subsystem_installed(const HookStatusView& status, std::uint32_t subsystem)
|
||||
{
|
||||
const std::uint32_t n = status.hook_entry_count < kMaxHookEntries ? status.hook_entry_count : kMaxHookEntries;
|
||||
for (std::uint32_t i = 0; i < n; ++i)
|
||||
{
|
||||
if (status.hook_entries[i].subsystem == subsystem && status.hook_entries[i].installed)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < n; ++i) {
|
||||
if (status.hook_entries[i].subsystem == subsystem && status.hook_entries[i].installed) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -442,8 +397,7 @@ void InjectionPanel::draw_subsystem_controls(const HookStatusView& status)
|
||||
{
|
||||
ImGui::SeparatorText("Subsystems (hook / unhook)");
|
||||
|
||||
struct Row
|
||||
{
|
||||
struct Row {
|
||||
const char* label;
|
||||
std::uint32_t subsystem;
|
||||
bool* want;
|
||||
@@ -457,25 +411,19 @@ void InjectionPanel::draw_subsystem_controls(const HookStatusView& status)
|
||||
{"Mouse + keyboard forwarding", HookSubsys_Mkb, &want_mkb_, "clicks/keys reach the game"},
|
||||
};
|
||||
|
||||
for (const Row& r : rows)
|
||||
{
|
||||
for (const Row& r : rows) {
|
||||
ImGui::PushID(r.label);
|
||||
if (ImGui::Checkbox(r.label, r.want))
|
||||
{
|
||||
if (ImGui::Checkbox(r.label, r.want)) {
|
||||
server_.set_subsystem_enabled(r.subsystem, *r.want);
|
||||
}
|
||||
ImGui::SameLine();
|
||||
const bool on = subsystem_installed(status, r.subsystem);
|
||||
if (*r.want != on)
|
||||
{
|
||||
if (*r.want != on) {
|
||||
ImGui::TextColored(kGrey, "(%s...)", *r.want ? "installing" : "removing");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextColored(on ? kGreen : kGrey, on ? "installed" : "off");
|
||||
}
|
||||
if (!*r.want)
|
||||
{
|
||||
if (!*r.want) {
|
||||
ImGui::TextDisabled(" off: %s won't work", r.depends);
|
||||
}
|
||||
ImGui::PopID();
|
||||
@@ -483,33 +431,28 @@ void InjectionPanel::draw_subsystem_controls(const HookStatusView& status)
|
||||
|
||||
// Cursor release is a Focus sub-option for games that clip/recenter the mouse,
|
||||
// which would otherwise trap the operator.
|
||||
if (ImGui::Checkbox("Release operator cursor (free the game's clip) [F2]", &release_cursor_))
|
||||
{
|
||||
if (ImGui::Checkbox("Release operator cursor (free the game's clip) [F2]", &release_cursor_)) {
|
||||
server_.set_cursor_clip_allowed(!release_cursor_);
|
||||
}
|
||||
}
|
||||
|
||||
void InjectionPanel::draw_hook_status(bool debug_details)
|
||||
{
|
||||
if (!server_.running())
|
||||
{
|
||||
if (!server_.running()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const HookStatusView status = server_.hook_status();
|
||||
|
||||
ImGui::SeparatorText("Hook status");
|
||||
if (!injected_)
|
||||
{
|
||||
if (!injected_) {
|
||||
ImGui::TextColored(kGrey, "Not injected.");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (target_state_)
|
||||
{
|
||||
switch (target_state_) {
|
||||
case TargetState::Alive:
|
||||
ImGui::TextColored(kGreen, "Hook DLL loaded in pid %lu (heartbeat %u)", server_.target_pid(),
|
||||
status.heartbeat);
|
||||
ImGui::TextColored(kGreen, "Hook DLL loaded in pid %lu (heartbeat %u)", server_.target_pid(), status.heartbeat);
|
||||
break;
|
||||
case TargetState::Hung:
|
||||
ImGui::TextColored(kRed, "Target not responding -- heartbeat stalled (frozen?).");
|
||||
@@ -525,16 +468,14 @@ void InjectionPanel::draw_hook_status(bool debug_details)
|
||||
ImGui::BeginDisabled(target_state_ != TargetState::Alive);
|
||||
draw_subsystem_controls(status);
|
||||
ImGui::EndDisabled();
|
||||
if (target_state_ != TargetState::Alive)
|
||||
{
|
||||
if (target_state_ != TargetState::Alive) {
|
||||
ImGui::TextDisabled("(connect to a live game to change these)"); // why the toggles are locked
|
||||
}
|
||||
ImGui::TextDisabled("Controller poll rates are in the Controllers panel.");
|
||||
|
||||
draw_hook_list(status);
|
||||
|
||||
if (!debug_details)
|
||||
{
|
||||
if (!debug_details) {
|
||||
return; // everything below is diagnostic detail
|
||||
}
|
||||
|
||||
@@ -548,17 +489,12 @@ void InjectionPanel::draw_hook_status(bool debug_details)
|
||||
// Input-path diagnostics: a focus-gated detection path would explain a game
|
||||
// that only accepts the controller when it has true focus.
|
||||
ImGui::SeparatorText("Input path");
|
||||
if (status.raw_input_gamepad)
|
||||
{
|
||||
if (status.raw_input_gamepad) {
|
||||
ImGui::TextColored(status.raw_input_gamepad_sink ? kGreen : kRed, "Raw Input gamepad: yes (INPUTSINK %s)",
|
||||
status.raw_input_gamepad_sink ? "set -> bg ok" : "MISSING -> focus-gated!");
|
||||
}
|
||||
else if (status.raw_input_registered)
|
||||
{
|
||||
} else if (status.raw_input_registered) {
|
||||
ImGui::TextColored(kGrey, "Raw Input: registered, but not for a gamepad usage");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextColored(kGrey, "Raw Input: not registered");
|
||||
}
|
||||
ImGui::TextColored(status.dinput_loaded ? kRed : kGrey, "DirectInput dll loaded: %s",
|
||||
@@ -570,22 +506,15 @@ void InjectionPanel::draw(bool debug_details)
|
||||
apply_panel_layout(Panel::Injection);
|
||||
ImGui::Begin("Injection");
|
||||
|
||||
if (server_.running())
|
||||
{
|
||||
if (target_state_ == TargetState::Terminated)
|
||||
{
|
||||
if (server_.running()) {
|
||||
if (target_state_ == TargetState::Terminated) {
|
||||
ImGui::TextColored(kRed, "Target (pid %lu) has terminated.", server_.target_pid());
|
||||
}
|
||||
else if (target_state_ == TargetState::Hung)
|
||||
{
|
||||
} else if (target_state_ == TargetState::Hung) {
|
||||
ImGui::TextColored(kRed, "Target (pid %lu) is not responding.", server_.target_pid());
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextColored(kGreen, "Connected to pid %lu", server_.target_pid());
|
||||
}
|
||||
if (ImGui::Button("Disconnect"))
|
||||
{
|
||||
if (ImGui::Button("Disconnect")) {
|
||||
// Leave the game vanilla: unhook everything before dropping the channel. The DLL stays
|
||||
// injected (dormant), so it can be reconnected later without re-injecting.
|
||||
disconnect_graceful(/*timeout_ms=*/700);
|
||||
@@ -594,11 +523,9 @@ void InjectionPanel::draw(bool debug_details)
|
||||
}
|
||||
// A relaunched game has a new pid; re-attach by image name without hunting for
|
||||
// it in the list. Only offered once the old target is gone.
|
||||
if (target_state_ == TargetState::Terminated && !selected_name_.empty())
|
||||
{
|
||||
if (target_state_ == TargetState::Terminated && !selected_name_.empty()) {
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Re-attach"))
|
||||
{
|
||||
if (ImGui::Button("Re-attach")) {
|
||||
reattach();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
@@ -609,32 +536,25 @@ void InjectionPanel::draw(bool debug_details)
|
||||
|
||||
// Session options for the selected game, shown whether or not we're connected -- so they can be set
|
||||
// up before launching the game, and an enabled auto re-attach is never hidden after a disconnect.
|
||||
if (!selected_name_.empty())
|
||||
{
|
||||
if (!selected_name_.empty()) {
|
||||
// Session-only auto re-attach: tick it, then kill + relaunch the game and it re-injects itself
|
||||
// early -- the kill+relaunch fix for a wrong audio format, without picking a target again.
|
||||
ImGui::Checkbox("Auto re-attach this game on relaunch", &auto_reattach_);
|
||||
if (auto_reattach_ && target_state_ == TargetState::Terminated)
|
||||
{
|
||||
if (auto_reattach_ && target_state_ == TargetState::Terminated) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextColored(kGrey, "(watching for %s...)", narrow(selected_name_).c_str());
|
||||
}
|
||||
// Opt-in Vulkan capture layer: for Vulkan games that initialize Vulkan immediately (where even
|
||||
// auto-attach injects too late -- see the red banner), register a per-user implicit layer scoped
|
||||
// to this game so the next launch is captured from the first frame. Removed when unticked / exit.
|
||||
if (ImGui::Checkbox("Set up Vulkan layer (for immediate-init Vulkan games)", &vk_layer_enabled_))
|
||||
{
|
||||
if (vk_layer_enabled_)
|
||||
{
|
||||
if (ImGui::Checkbox("Set up Vulkan layer (for immediate-init Vulkan games)", &vk_layer_enabled_)) {
|
||||
if (vk_layer_enabled_) {
|
||||
vk_layer_enabled_ = register_vk_layer(selected_name_);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
unregister_vk_layer();
|
||||
}
|
||||
}
|
||||
if (ImGui::IsItemHovered())
|
||||
{
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::SetTooltip("Registers a per-user (HKCU, no admin) implicit Vulkan layer scoped to\n"
|
||||
"this game, so a relaunch is captured before Vulkan init. Pair with\n"
|
||||
"Auto re-attach. Removed when you untick it or close the tool.");
|
||||
@@ -642,28 +562,23 @@ void InjectionPanel::draw(bool debug_details)
|
||||
}
|
||||
|
||||
ImGui::TextUnformatted("Target window");
|
||||
if (ImGui::Button("Refresh"))
|
||||
{
|
||||
if (ImGui::Button("Refresh")) {
|
||||
refresh_targets();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
ImGui::SetNextItemWidth(-1.0f);
|
||||
ImGui::InputTextWithHint("##wfilter", "filter by title or process...", window_filter_, sizeof(window_filter_));
|
||||
|
||||
if (ImGui::BeginListBox("##windows", ImVec2(-1.0f, 180.0f)))
|
||||
{
|
||||
for (const WindowEntry& w : windows_)
|
||||
{
|
||||
if (!contains_ci_w(w.title, window_filter_) && !contains_ci_w(w.exe_name, window_filter_))
|
||||
{
|
||||
if (ImGui::BeginListBox("##windows", ImVec2(-1.0f, 180.0f))) {
|
||||
for (const WindowEntry& w : windows_) {
|
||||
if (!contains_ci_w(w.title, window_filter_) && !contains_ci_w(w.exe_name, window_filter_)) {
|
||||
continue;
|
||||
}
|
||||
const bool selected = w.pid == selected_pid_;
|
||||
char label[400];
|
||||
snprintf(label, sizeof(label), "%-32s [%s %lu]", narrow(w.title).c_str(),
|
||||
narrow(w.exe_name).c_str(), w.pid);
|
||||
if (ImGui::Selectable(label, selected))
|
||||
{
|
||||
snprintf(label, sizeof(label), "%-32s [%s %lu]", narrow(w.title).c_str(), narrow(w.exe_name).c_str(),
|
||||
w.pid);
|
||||
if (ImGui::Selectable(label, selected)) {
|
||||
selected_pid_ = w.pid;
|
||||
selected_name_ = w.exe_name;
|
||||
}
|
||||
@@ -673,24 +588,19 @@ void InjectionPanel::draw(bool debug_details)
|
||||
|
||||
// The full process list is the advanced fallback (e.g. a windowless game host),
|
||||
// kept out of the way unless the operator wants it.
|
||||
if (debug_details)
|
||||
{
|
||||
if (debug_details) {
|
||||
ImGui::SeparatorText("All processes (advanced)");
|
||||
ImGui::SetNextItemWidth(-1.0f);
|
||||
ImGui::InputTextWithHint("##filter", "filter by name...", filter_, sizeof(filter_));
|
||||
if (ImGui::BeginListBox("##processes", ImVec2(-1.0f, 160.0f)))
|
||||
{
|
||||
for (const ProcessEntry& entry : processes_)
|
||||
{
|
||||
if (!contains_ci_w(entry.exe_name, filter_))
|
||||
{
|
||||
if (ImGui::BeginListBox("##processes", ImVec2(-1.0f, 160.0f))) {
|
||||
for (const ProcessEntry& entry : processes_) {
|
||||
if (!contains_ci_w(entry.exe_name, filter_)) {
|
||||
continue;
|
||||
}
|
||||
const bool selected = entry.pid == selected_pid_;
|
||||
char label[300];
|
||||
snprintf(label, sizeof(label), "%-40s %lu", narrow(entry.exe_name).c_str(), entry.pid);
|
||||
if (ImGui::Selectable(label, selected))
|
||||
{
|
||||
if (ImGui::Selectable(label, selected)) {
|
||||
selected_pid_ = entry.pid;
|
||||
selected_name_ = entry.exe_name;
|
||||
}
|
||||
@@ -701,20 +611,17 @@ void InjectionPanel::draw(bool debug_details)
|
||||
|
||||
const bool can_inject = selected_pid_ != 0;
|
||||
ImGui::BeginDisabled(!can_inject);
|
||||
if (ImGui::Button("Inject & Connect", ImVec2(-1.0f, 0.0f)))
|
||||
{
|
||||
if (ImGui::Button("Inject & Connect", ImVec2(-1.0f, 0.0f))) {
|
||||
inject_selected();
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
// Explain the disabled state on hover (AllowWhenDisabled, since the button is greyed out).
|
||||
if (!can_inject && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
|
||||
{
|
||||
if (!can_inject && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) {
|
||||
ImGui::SetTooltip("Pick a target window or process above first.\n"
|
||||
"If the game already has the DLL (e.g. after a reconnect), this reuses it.");
|
||||
}
|
||||
|
||||
if (!status_.empty())
|
||||
{
|
||||
if (!status_.empty()) {
|
||||
ImGui::TextColored(status_color_, "%s", status_.c_str());
|
||||
}
|
||||
|
||||
|
||||
@@ -15,20 +15,17 @@
|
||||
#include "inject/window_list.hpp"
|
||||
#include "ipc/ipc_server.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Liveness of the injected target, surfaced in the UI so a dead/hung game is obvious.
|
||||
enum class TargetState
|
||||
{
|
||||
enum class TargetState {
|
||||
NotInjected, // no hook loaded
|
||||
Alive, // process running and the hook heartbeat is advancing
|
||||
Hung, // process still exists but the heartbeat stalled (not responding)
|
||||
Terminated, // process has exited
|
||||
};
|
||||
|
||||
class InjectionPanel
|
||||
{
|
||||
class InjectionPanel {
|
||||
public:
|
||||
InjectionPanel();
|
||||
~InjectionPanel();
|
||||
@@ -49,10 +46,7 @@ public:
|
||||
// Test harness (debug builds only): inject into the first running process whose image
|
||||
// name matches. Returns the pid on success, 0 otherwise. Same path as the UI button.
|
||||
unsigned long dev_inject_by_name(const std::wstring& image_name);
|
||||
void dev_set_auto_reattach(bool on)
|
||||
{
|
||||
auto_reattach_ = on;
|
||||
}
|
||||
void dev_set_auto_reattach(bool on) { auto_reattach_ = on; }
|
||||
#endif
|
||||
|
||||
// Forward the latest pad snapshot to the injected hook (if connected). When
|
||||
@@ -61,35 +55,25 @@ public:
|
||||
|
||||
// Enable/disable synthetic test input. The toggle itself lives in the Controllers
|
||||
// panel (a controller-debug aid); the host feeds its state here each frame.
|
||||
void set_test_input(bool on)
|
||||
{
|
||||
test_input_.store(on, std::memory_order_relaxed);
|
||||
}
|
||||
void set_test_input(bool on) { test_input_.store(on, std::memory_order_relaxed); }
|
||||
|
||||
// The injected game's main window, as reported by the hook (null if none). A
|
||||
// terminated target's HWND is stale/invalid, so report none -- the capture and
|
||||
// audio panels then drop to idle instead of chasing a dead window.
|
||||
[[nodiscard]] HWND game_hwnd() const
|
||||
{
|
||||
if (target_state_ == TargetState::Terminated)
|
||||
{
|
||||
if (target_state_ == TargetState::Terminated) {
|
||||
return nullptr;
|
||||
}
|
||||
return reinterpret_cast<HWND>(server_.hook_status().game_hwnd);
|
||||
}
|
||||
|
||||
// Current liveness of the injected target (for other panels / status).
|
||||
[[nodiscard]] TargetState target_state() const
|
||||
{
|
||||
return target_state_;
|
||||
}
|
||||
[[nodiscard]] TargetState target_state() const { return target_state_; }
|
||||
|
||||
// The hook's full diagnostics back-channel (other panels read the audio
|
||||
// render-stream counts from here).
|
||||
[[nodiscard]] HookStatusView hook_status() const
|
||||
{
|
||||
return server_.hook_status();
|
||||
}
|
||||
[[nodiscard]] HookStatusView hook_status() const { return server_.hook_status(); }
|
||||
|
||||
// Drain log lines the hook streamed (for the Log window). No-op if not active.
|
||||
template <typename F>
|
||||
@@ -100,23 +84,14 @@ public:
|
||||
|
||||
// Emit a host-side line into the Log window (color-coded by level), e.g. an
|
||||
// override-overwrite warning. No-op if not connected.
|
||||
void host_log(std::uint32_t level, const char* text)
|
||||
{
|
||||
server_.host_log(level, text);
|
||||
}
|
||||
void host_log(std::uint32_t level, const char* text) { server_.host_log(level, text); }
|
||||
|
||||
// --- Present-hook video path (consumed by the Video mirror panel) ----------
|
||||
|
||||
[[nodiscard]] unsigned long target_pid() const
|
||||
{
|
||||
return server_.target_pid();
|
||||
}
|
||||
[[nodiscard]] unsigned long target_pid() const { return server_.target_pid(); }
|
||||
|
||||
// The hook's Present-hook video channel snapshot (shared-texture descriptor).
|
||||
[[nodiscard]] VideoShareView video_share() const
|
||||
{
|
||||
return server_.video_share();
|
||||
}
|
||||
[[nodiscard]] VideoShareView video_share() const { return server_.video_share(); }
|
||||
|
||||
// Request the Present-hook video subsystem be installed/removed. Keeps the
|
||||
// Injection panel's own checkbox in sync, so the Video panel can drive it.
|
||||
@@ -126,25 +101,16 @@ public:
|
||||
server_.set_subsystem_enabled(HookSubsys_Video, on);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool video_requested() const
|
||||
{
|
||||
return want_video_;
|
||||
}
|
||||
[[nodiscard]] bool video_requested() const { return want_video_; }
|
||||
|
||||
// --- Mouse + keyboard forwarding -------------------------------------------
|
||||
|
||||
// Whether the operator enabled the MKB-forwarding subsystem (the toggle is the
|
||||
// hook). The host's MKB forwarder only runs while this is on and a hook is alive.
|
||||
[[nodiscard]] bool mkb_enabled() const
|
||||
{
|
||||
return want_mkb_ && injected_;
|
||||
}
|
||||
[[nodiscard]] bool mkb_enabled() const { return want_mkb_ && injected_; }
|
||||
|
||||
// Enqueue an MKB event for the hook to forward into the game.
|
||||
void push_mkb(const MkbEvent& ev)
|
||||
{
|
||||
server_.push_mkb(ev);
|
||||
}
|
||||
void push_mkb(const MkbEvent& ev) { server_.push_mkb(ev); }
|
||||
|
||||
// --- Cursor release (for cursor-clipping games) ----------------------------
|
||||
|
||||
@@ -156,10 +122,7 @@ public:
|
||||
server_.set_cursor_clip_allowed(!release_cursor_);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool cursor_released() const
|
||||
{
|
||||
return release_cursor_;
|
||||
}
|
||||
[[nodiscard]] bool cursor_released() const { return release_cursor_; }
|
||||
|
||||
private:
|
||||
void refresh_targets(); // refresh both the window list and the process list
|
||||
|
||||
@@ -12,11 +12,9 @@
|
||||
|
||||
#include "coop/protocol.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
struct PadInfo
|
||||
{
|
||||
struct PadInfo {
|
||||
bool connected = false;
|
||||
CoopPadState state = {};
|
||||
std::string source; // human-readable label for the debug overlay
|
||||
@@ -25,15 +23,13 @@ struct PadInfo
|
||||
// A copy-safe snapshot of the input backend's state, published by the input worker
|
||||
// thread for the UI to display. This decouples the Controllers panel (UI thread) from
|
||||
// the worker's live polling, so the worker can own its InputSource exclusively.
|
||||
struct InputSnapshot
|
||||
{
|
||||
struct InputSnapshot {
|
||||
std::array<PadInfo, kMaxPads> pads{};
|
||||
const char* backend = "XInput"; // backend name (static string literal; thread-safe to share)
|
||||
bool steam_active = false;
|
||||
};
|
||||
|
||||
class InputSource
|
||||
{
|
||||
class InputSource {
|
||||
public:
|
||||
virtual ~InputSource() = default;
|
||||
|
||||
|
||||
@@ -11,8 +11,7 @@
|
||||
#include "input/steam_input_source.hpp"
|
||||
#endif
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
InputWorker::~InputWorker()
|
||||
{
|
||||
@@ -21,8 +20,7 @@ InputWorker::~InputWorker()
|
||||
|
||||
void InputWorker::start(InjectionPanel* injection, std::string steam_manifest)
|
||||
{
|
||||
if (running_.load(std::memory_order_acquire))
|
||||
{
|
||||
if (running_.load(std::memory_order_acquire)) {
|
||||
return;
|
||||
}
|
||||
injection_ = injection;
|
||||
@@ -34,8 +32,7 @@ void InputWorker::start(InjectionPanel* injection, std::string steam_manifest)
|
||||
void InputWorker::stop()
|
||||
{
|
||||
running_.store(false, std::memory_order_release);
|
||||
if (thread_.joinable())
|
||||
{
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
}
|
||||
@@ -68,36 +65,28 @@ void InputWorker::run()
|
||||
std::uint16_t last_rumble_l[kMaxPads] = {};
|
||||
std::uint16_t last_rumble_r[kMaxPads] = {};
|
||||
|
||||
while (running_.load(std::memory_order_relaxed))
|
||||
{
|
||||
while (running_.load(std::memory_order_relaxed)) {
|
||||
const bool want_steam = want_steam_.load(std::memory_order_relaxed);
|
||||
#ifdef COOP_WITH_STEAM
|
||||
// Reconcile the backend with the UI's request. Initializing Steam Input hijacks
|
||||
// XInput, so it's strictly opt-in; a failed init falls back to plain XInput and
|
||||
// flags steam_failed_ so the UI can reset its toggle (and a later retry is
|
||||
// possible once the request is cleared).
|
||||
if (want_steam && steam == nullptr && !steam_failed_.load(std::memory_order_relaxed))
|
||||
{
|
||||
if (want_steam && steam == nullptr && !steam_failed_.load(std::memory_order_relaxed)) {
|
||||
steam = std::make_unique<SteamInputSource>();
|
||||
if (steam->init(steam_manifest_))
|
||||
{
|
||||
if (steam->init(steam_manifest_)) {
|
||||
active = steam.get();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
steam.reset();
|
||||
active = &xinput;
|
||||
steam_failed_.store(true, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
else if (!want_steam && steam != nullptr)
|
||||
{
|
||||
} else if (!want_steam && steam != nullptr) {
|
||||
steam->shutdown();
|
||||
steam.reset();
|
||||
active = &xinput;
|
||||
}
|
||||
if (!want_steam)
|
||||
{
|
||||
if (!want_steam) {
|
||||
steam_failed_.store(false, std::memory_order_relaxed); // allow a future retry
|
||||
}
|
||||
const bool steam_active = steam != nullptr;
|
||||
@@ -108,17 +97,14 @@ void InputWorker::run()
|
||||
|
||||
active->poll();
|
||||
|
||||
if (injection_ != nullptr)
|
||||
{
|
||||
if (injection_ != nullptr) {
|
||||
// Push the latest pads to the game (publish() substitutes synthetic test input
|
||||
// itself when that mode is on), then forward any newly requested rumble.
|
||||
injection_->publish(active->pads());
|
||||
|
||||
const HookStatusView hs = injection_->hook_status();
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i)
|
||||
{
|
||||
if (hs.rumble_left[i] != last_rumble_l[i] || hs.rumble_right[i] != last_rumble_r[i])
|
||||
{
|
||||
for (int i = 0; i < static_cast<int>(kMaxPads); ++i) {
|
||||
if (hs.rumble_left[i] != last_rumble_l[i] || hs.rumble_right[i] != last_rumble_r[i]) {
|
||||
active->set_rumble(i, hs.rumble_left[i], hs.rumble_right[i]);
|
||||
last_rumble_l[i] = hs.rumble_left[i];
|
||||
last_rumble_r[i] = hs.rumble_right[i];
|
||||
@@ -134,8 +120,7 @@ void InputWorker::run()
|
||||
}
|
||||
|
||||
#ifdef COOP_WITH_STEAM
|
||||
if (steam != nullptr)
|
||||
{
|
||||
if (steam != nullptr) {
|
||||
steam->shutdown();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -13,13 +13,11 @@
|
||||
|
||||
#include "input/input_source.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class InjectionPanel;
|
||||
|
||||
class InputWorker
|
||||
{
|
||||
class InputWorker {
|
||||
public:
|
||||
InputWorker() = default;
|
||||
~InputWorker();
|
||||
@@ -35,20 +33,14 @@ public:
|
||||
void stop();
|
||||
|
||||
// UI -> worker: request the Steam Input backend (true) or plain XInput (false).
|
||||
void set_want_steam(bool on)
|
||||
{
|
||||
want_steam_.store(on, std::memory_order_relaxed);
|
||||
}
|
||||
void set_want_steam(bool on) { want_steam_.store(on, std::memory_order_relaxed); }
|
||||
|
||||
// worker -> UI: latest snapshot for the Controllers panel (thread-safe copy).
|
||||
[[nodiscard]] InputSnapshot snapshot() const;
|
||||
|
||||
// worker -> UI: Steam Input was requested but failed to start (so the panel can
|
||||
// reset its toggle and fall back to XInput). Cleared once Steam is not requested.
|
||||
[[nodiscard]] bool steam_failed() const
|
||||
{
|
||||
return steam_failed_.load(std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] bool steam_failed() const { return steam_failed_.load(std::memory_order_relaxed); }
|
||||
|
||||
private:
|
||||
void run();
|
||||
|
||||
@@ -7,16 +7,13 @@
|
||||
|
||||
#include <steam/steam_api.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
// Digital actions in the manifest, paired with the XInput button bit they map to.
|
||||
// Names must match host/assets/steam_input_actions.vdf.
|
||||
struct ButtonAction
|
||||
{
|
||||
struct ButtonAction {
|
||||
const char* action;
|
||||
std::uint16_t xinput_bit;
|
||||
};
|
||||
@@ -40,12 +37,10 @@ const ButtonAction kButtons[kSteamButtonActions] = {
|
||||
|
||||
std::int16_t to_axis(float v)
|
||||
{
|
||||
if (v > 1.0f)
|
||||
{
|
||||
if (v > 1.0f) {
|
||||
v = 1.0f;
|
||||
}
|
||||
if (v < -1.0f)
|
||||
{
|
||||
if (v < -1.0f) {
|
||||
v = -1.0f;
|
||||
}
|
||||
return static_cast<std::int16_t>(v * 32767.0f);
|
||||
@@ -53,12 +48,10 @@ std::int16_t to_axis(float v)
|
||||
|
||||
std::uint8_t to_trigger(float v)
|
||||
{
|
||||
if (v > 1.0f)
|
||||
{
|
||||
if (v > 1.0f) {
|
||||
v = 1.0f;
|
||||
}
|
||||
if (v < 0.0f)
|
||||
{
|
||||
if (v < 0.0f) {
|
||||
v = 0.0f;
|
||||
}
|
||||
return static_cast<std::uint8_t>(v * 255.0f);
|
||||
@@ -75,25 +68,21 @@ bool SteamInputSource::init(const std::string& manifest_absolute_path)
|
||||
{
|
||||
// Running standalone (not launched by Steam) without a steam_appid.txt makes
|
||||
// SteamAPI_Init fail; that's fine -- we degrade to XInput.
|
||||
if (!SteamAPI_Init())
|
||||
{
|
||||
if (!SteamAPI_Init()) {
|
||||
std::printf("SteamInput: SteamAPI_Init failed (not under Steam?); using XInput.\n");
|
||||
return false;
|
||||
}
|
||||
if (SteamInput() == nullptr)
|
||||
{
|
||||
if (SteamInput() == nullptr) {
|
||||
std::printf("SteamInput: ISteamInput unavailable; using XInput.\n");
|
||||
SteamAPI_Shutdown();
|
||||
return false;
|
||||
}
|
||||
// Point Steam Input at our bundled action manifest so we don't depend on a
|
||||
// partner-backend-registered config. Must be called before Init().
|
||||
if (!manifest_absolute_path.empty())
|
||||
{
|
||||
if (!manifest_absolute_path.empty()) {
|
||||
SteamInput()->SetInputActionManifestFilePath(manifest_absolute_path.c_str());
|
||||
}
|
||||
if (!SteamInput()->Init(/*bExplicitlyCallRunFrame=*/false))
|
||||
{
|
||||
if (!SteamInput()->Init(/*bExplicitlyCallRunFrame=*/false)) {
|
||||
std::printf("SteamInput: ISteamInput::Init failed; using XInput.\n");
|
||||
SteamAPI_Shutdown();
|
||||
return false;
|
||||
@@ -108,8 +97,7 @@ bool SteamInputSource::init(const std::string& manifest_absolute_path)
|
||||
|
||||
void SteamInputSource::shutdown()
|
||||
{
|
||||
if (steam_ready_)
|
||||
{
|
||||
if (steam_ready_) {
|
||||
SteamInput()->Shutdown();
|
||||
SteamAPI_Shutdown();
|
||||
steam_ready_ = false;
|
||||
@@ -120,8 +108,7 @@ void SteamInputSource::shutdown()
|
||||
void SteamInputSource::resolve_handles()
|
||||
{
|
||||
action_set_ = SteamInput()->GetActionSetHandle("GameControls");
|
||||
for (int i = 0; i < kSteamButtonActions; ++i)
|
||||
{
|
||||
for (int i = 0; i < kSteamButtonActions; ++i) {
|
||||
button_handles_[i] = SteamInput()->GetDigitalActionHandle(kButtons[i].action);
|
||||
}
|
||||
left_stick_ = SteamInput()->GetAnalogActionHandle("LeftStick");
|
||||
@@ -138,12 +125,10 @@ bool SteamInputSource::read_steam_pad(std::uint64_t controller, PadInfo& out) co
|
||||
st.connected = 1;
|
||||
bool any_active = false;
|
||||
|
||||
for (int i = 0; i < kSteamButtonActions; ++i)
|
||||
{
|
||||
for (int i = 0; i < kSteamButtonActions; ++i) {
|
||||
const InputDigitalActionData_t d = SteamInput()->GetDigitalActionData(controller, button_handles_[i]);
|
||||
any_active = any_active || d.bActive;
|
||||
if (d.bState)
|
||||
{
|
||||
if (d.bState) {
|
||||
st.buttons |= kButtons[i].xinput_bit;
|
||||
}
|
||||
}
|
||||
@@ -163,8 +148,7 @@ bool SteamInputSource::read_steam_pad(std::uint64_t controller, PadInfo& out) co
|
||||
|
||||
// No action is bound/active (e.g. the controller isn't using our manifest) ->
|
||||
// let the XInput fallback handle this slot instead of reporting an empty pad.
|
||||
if (!any_active)
|
||||
{
|
||||
if (!any_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -182,8 +166,7 @@ void SteamInputSource::poll()
|
||||
xinput_.poll();
|
||||
pads_ = xinput_.pads();
|
||||
|
||||
if (!steam_ready_)
|
||||
{
|
||||
if (!steam_ready_) {
|
||||
steam_count_ = 0;
|
||||
return;
|
||||
}
|
||||
@@ -192,17 +175,14 @@ void SteamInputSource::poll()
|
||||
InputHandle_t handles[STEAM_INPUT_MAX_COUNT] = {};
|
||||
steam_count_ = SteamInput()->GetConnectedControllers(handles);
|
||||
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i) {
|
||||
controllers_[i] = 0;
|
||||
steam_slot_[i] = false;
|
||||
}
|
||||
for (int i = 0; i < steam_count_ && i < static_cast<int>(kMaxPads); ++i)
|
||||
{
|
||||
for (int i = 0; i < steam_count_ && i < static_cast<int>(kMaxPads); ++i) {
|
||||
controllers_[i] = handles[i];
|
||||
PadInfo steam_pad;
|
||||
if (read_steam_pad(handles[i], steam_pad))
|
||||
{
|
||||
if (read_steam_pad(handles[i], steam_pad)) {
|
||||
pads_[i] = steam_pad; // Steam controller active on this slot -> use it
|
||||
steam_slot_[i] = true;
|
||||
}
|
||||
@@ -211,12 +191,10 @@ void SteamInputSource::poll()
|
||||
|
||||
void SteamInputSource::set_rumble(int slot, std::uint16_t left, std::uint16_t right)
|
||||
{
|
||||
if (slot < 0 || slot >= static_cast<int>(kMaxPads))
|
||||
{
|
||||
if (slot < 0 || slot >= static_cast<int>(kMaxPads)) {
|
||||
return;
|
||||
}
|
||||
if (steam_ready_ && steam_slot_[slot] && controllers_[slot] != 0)
|
||||
{
|
||||
if (steam_ready_ && steam_slot_[slot] && controllers_[slot] != 0) {
|
||||
SteamInput()->TriggerVibration(controllers_[slot], left, right);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -14,14 +14,12 @@
|
||||
#include "input/input_source.hpp"
|
||||
#include "input/xinput_source.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Number of digital (button) actions in the bundled action manifest.
|
||||
inline constexpr int kSteamButtonActions = 15;
|
||||
|
||||
class SteamInputSource final : public InputSource
|
||||
{
|
||||
class SteamInputSource final : public InputSource {
|
||||
public:
|
||||
~SteamInputSource() override;
|
||||
|
||||
@@ -31,28 +29,16 @@ public:
|
||||
bool init(const std::string& manifest_absolute_path);
|
||||
void shutdown();
|
||||
|
||||
[[nodiscard]] const char* name() const override
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
[[nodiscard]] const char* name() const override { return name_; }
|
||||
void poll() override;
|
||||
[[nodiscard]] const std::array<PadInfo, kMaxPads>& pads() const override
|
||||
{
|
||||
return pads_;
|
||||
}
|
||||
[[nodiscard]] const std::array<PadInfo, kMaxPads>& pads() const override { return pads_; }
|
||||
|
||||
// Forward rumble to the guest: SteamInput TriggerVibration on the slot's
|
||||
// controller when it's Steam-active, else the XInput fallback.
|
||||
void set_rumble(int slot, std::uint16_t left, std::uint16_t right) override;
|
||||
|
||||
[[nodiscard]] bool steam_active() const
|
||||
{
|
||||
return steam_ready_;
|
||||
}
|
||||
[[nodiscard]] int steam_controllers() const
|
||||
{
|
||||
return steam_count_;
|
||||
}
|
||||
[[nodiscard]] bool steam_active() const { return steam_ready_; }
|
||||
[[nodiscard]] int steam_controllers() const { return steam_count_; }
|
||||
|
||||
private:
|
||||
void resolve_handles();
|
||||
|
||||
@@ -3,19 +3,16 @@
|
||||
#include <windows.h>
|
||||
#include <xinput.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
void XInputSource::poll()
|
||||
{
|
||||
for (DWORD i = 0; i < kMaxPads; ++i)
|
||||
{
|
||||
for (DWORD i = 0; i < kMaxPads; ++i) {
|
||||
XINPUT_STATE state = {};
|
||||
const DWORD result = XInputGetState(i, &state);
|
||||
|
||||
PadInfo& info = pads_[i];
|
||||
if (result == ERROR_SUCCESS)
|
||||
{
|
||||
if (result == ERROR_SUCCESS) {
|
||||
info.connected = true;
|
||||
info.source = "XInput #" + std::to_string(i);
|
||||
|
||||
@@ -31,9 +28,7 @@ void XInputSource::poll()
|
||||
info.state.thumb_ly = g.sThumbLY;
|
||||
info.state.thumb_rx = g.sThumbRX;
|
||||
info.state.thumb_ry = g.sThumbRY;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
info = PadInfo{};
|
||||
}
|
||||
}
|
||||
@@ -41,8 +36,7 @@ void XInputSource::poll()
|
||||
|
||||
void XInputSource::set_rumble(int slot, std::uint16_t left, std::uint16_t right)
|
||||
{
|
||||
if (slot < 0 || slot >= static_cast<int>(kMaxPads))
|
||||
{
|
||||
if (slot < 0 || slot >= static_cast<int>(kMaxPads)) {
|
||||
return;
|
||||
}
|
||||
XINPUT_VIBRATION v{left, right};
|
||||
|
||||
@@ -2,25 +2,17 @@
|
||||
|
||||
#include "input/input_source.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Reads the four XInput slots. Remote Play Together exposes guest controllers
|
||||
// here, alongside any controllers physically attached to the host.
|
||||
class XInputSource final : public InputSource
|
||||
{
|
||||
class XInputSource final : public InputSource {
|
||||
public:
|
||||
[[nodiscard]] const char* name() const override
|
||||
{
|
||||
return "XInput";
|
||||
}
|
||||
[[nodiscard]] const char* name() const override { return "XInput"; }
|
||||
|
||||
void poll() override;
|
||||
|
||||
[[nodiscard]] const std::array<PadInfo, kMaxPads>& pads() const override
|
||||
{
|
||||
return pads_;
|
||||
}
|
||||
[[nodiscard]] const std::array<PadInfo, kMaxPads>& pads() const override { return pads_; }
|
||||
|
||||
// Forward rumble to the XInput device at `slot` (the guest's RPT virtual pad).
|
||||
void set_rumble(int slot, std::uint16_t left, std::uint16_t right) override;
|
||||
|
||||
@@ -3,11 +3,9 @@
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
// The hook writes these cumulative diagnostic counters cross-process (an x86 DLL can do a 64-bit
|
||||
// store in two halves), so read them atomically to avoid a torn value. The shared mapping is
|
||||
// genuinely mutable -- the const here is just our read-only view -- so const_cast for atomic_ref.
|
||||
@@ -22,8 +20,7 @@ bool IpcServer::start(unsigned long target_pid)
|
||||
std::scoped_lock lock(mutex_);
|
||||
stop_locked();
|
||||
|
||||
if (!shm_.create(shared_memory_name(target_pid), sizeof(SharedBlock)))
|
||||
{
|
||||
if (!shm_.create(shared_memory_name(target_pid), sizeof(SharedBlock))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -40,8 +37,7 @@ bool IpcServer::start(unsigned long target_pid)
|
||||
|
||||
// Log ring: the injected hook opens this and streams its log lines back for the
|
||||
// Log window. Best-effort -- the rest of the tool works without it.
|
||||
if (log_shm_.create(log_ring_name(target_pid), log_ring_total_size(kLogCapacity)))
|
||||
{
|
||||
if (log_shm_.create(log_ring_name(target_pid), log_ring_total_size(kLogCapacity))) {
|
||||
log_ring_ = log_shm_.as<LogRing>();
|
||||
log_ring_init(*log_ring_, kLogCapacity);
|
||||
log_cursor_ = 0;
|
||||
@@ -52,13 +48,11 @@ bool IpcServer::start(unsigned long target_pid)
|
||||
void IpcServer::publish(const std::array<PadInfo, kMaxPads>& pads)
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (block_ == nullptr)
|
||||
{
|
||||
if (block_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
CoopPadState states[kMaxPads];
|
||||
for (std::size_t i = 0; i < pads.size(); ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < pads.size(); ++i) {
|
||||
states[i] = pads[i].state;
|
||||
states[i].connected = pads[i].connected ? 1 : 0;
|
||||
}
|
||||
@@ -69,21 +63,18 @@ HookStatusView IpcServer::hook_status() const
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
HookStatusView view;
|
||||
if (block_ == nullptr)
|
||||
{
|
||||
if (block_ == nullptr) {
|
||||
return view;
|
||||
}
|
||||
const HookStatus& s = block_->status;
|
||||
view.attached = s.attached != 0;
|
||||
view.focus_spoof = s.focus_spoof != 0;
|
||||
view.heartbeat = s.heartbeat.load(std::memory_order_relaxed);
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i) {
|
||||
view.get_state[i] = s.get_state_calls[i].load(std::memory_order_relaxed);
|
||||
view.get_caps[i] = s.get_caps_calls[i].load(std::memory_order_relaxed);
|
||||
}
|
||||
for (std::uint32_t i = 0; i < FocusApi_Count; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < FocusApi_Count; ++i) {
|
||||
view.focus_calls[i] = s.focus_query_calls[i].load(std::memory_order_relaxed);
|
||||
}
|
||||
view.game_pid = s.game_pid;
|
||||
@@ -94,18 +85,15 @@ HookStatusView IpcServer::hook_status() const
|
||||
view.dinput_loaded = s.dinput_loaded != 0;
|
||||
view.vk_too_late = s.vk_too_late != 0;
|
||||
view.audio_streams_seen = s.audio_streams_seen;
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxAudioStreams; ++i) {
|
||||
view.audio_streams[i] = s.audio_streams[i];
|
||||
view.audio_streams[i].frames_rendered = atomic_load_u64(s.audio_streams[i].frames_rendered);
|
||||
}
|
||||
view.hook_entry_count = s.hook_entry_count;
|
||||
for (std::uint32_t i = 0; i < kMaxHookEntries; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxHookEntries; ++i) {
|
||||
view.hook_entries[i] = s.hook_entries[i];
|
||||
}
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < kMaxPads; ++i) {
|
||||
view.rumble_left[i] = s.rumble_left[i];
|
||||
view.rumble_right[i] = s.rumble_right[i];
|
||||
view.read_state[i] = s.read_state[i];
|
||||
@@ -117,8 +105,7 @@ VideoShareView IpcServer::video_share() const
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
VideoShareView v;
|
||||
if (block_ == nullptr)
|
||||
{
|
||||
if (block_ == nullptr) {
|
||||
return v;
|
||||
}
|
||||
const VideoShare& s = block_->video;
|
||||
@@ -135,8 +122,7 @@ VideoShareView IpcServer::video_share() const
|
||||
void IpcServer::set_subsystem_enabled(std::uint32_t subsystem, bool enabled)
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (block_ != nullptr && subsystem < HookSubsys_Count)
|
||||
{
|
||||
if (block_ != nullptr && subsystem < HookSubsys_Count) {
|
||||
// 0 = install, 1 = remove.
|
||||
block_->control.subsystem_disabled[subsystem].store(enabled ? 0u : 1u, std::memory_order_release);
|
||||
}
|
||||
@@ -145,12 +131,10 @@ void IpcServer::set_subsystem_enabled(std::uint32_t subsystem, bool enabled)
|
||||
void IpcServer::request_unhook_all()
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (block_ == nullptr)
|
||||
{
|
||||
if (block_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
for (std::uint32_t s = 0; s < HookSubsys_Count; ++s)
|
||||
{
|
||||
for (std::uint32_t s = 0; s < HookSubsys_Count; ++s) {
|
||||
block_->control.subsystem_disabled[s].store(1u, std::memory_order_release); // 1 = remove
|
||||
}
|
||||
}
|
||||
@@ -158,20 +142,16 @@ void IpcServer::request_unhook_all()
|
||||
bool IpcServer::all_hooks_removed() const
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (block_ == nullptr)
|
||||
{
|
||||
if (block_ == nullptr) {
|
||||
return true; // not connected -> nothing of ours is hooked
|
||||
}
|
||||
const HookStatus& s = block_->status;
|
||||
std::uint32_t count = s.hook_entry_count;
|
||||
if (count > kMaxHookEntries)
|
||||
{
|
||||
if (count > kMaxHookEntries) {
|
||||
count = kMaxHookEntries;
|
||||
}
|
||||
for (std::uint32_t i = 0; i < count; ++i)
|
||||
{
|
||||
if (s.hook_entries[i].installed != 0)
|
||||
{
|
||||
for (std::uint32_t i = 0; i < count; ++i) {
|
||||
if (s.hook_entries[i].installed != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -181,8 +161,7 @@ bool IpcServer::all_hooks_removed() const
|
||||
void IpcServer::host_log(std::uint32_t level, const char* text)
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (log_ring_ != nullptr)
|
||||
{
|
||||
if (log_ring_ != nullptr) {
|
||||
log_ring_push(*log_ring_, GetCurrentProcessId(), level, GetTickCount64(), text);
|
||||
}
|
||||
}
|
||||
@@ -195,8 +174,7 @@ void IpcServer::stop()
|
||||
|
||||
void IpcServer::stop_locked()
|
||||
{
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->magic = 0; // invalidate so a late hook read won't trust stale data
|
||||
block_ = nullptr;
|
||||
}
|
||||
|
||||
@@ -11,12 +11,10 @@
|
||||
#include "coop/shared_memory.hpp"
|
||||
#include "input/input_source.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Plain (non-atomic) snapshot of the hook's back-channel for the overlay.
|
||||
struct HookStatusView
|
||||
{
|
||||
struct HookStatusView {
|
||||
bool attached = false; // XInput hooks installed in the game
|
||||
bool focus_spoof = false; // focus spoofing active
|
||||
std::uint32_t heartbeat = 0; // DLL liveness counter
|
||||
@@ -48,8 +46,7 @@ struct HookStatusView
|
||||
};
|
||||
|
||||
// Plain snapshot of the Present-hook video channel for the Video mirror panel.
|
||||
struct VideoShareView
|
||||
{
|
||||
struct VideoShareView {
|
||||
std::uint32_t generation = 0; // bumps per shared frame; 0 = nothing shared yet
|
||||
std::uint32_t width = 0; // shared texture dimensions / DXGI format
|
||||
std::uint32_t height = 0;
|
||||
@@ -59,8 +56,7 @@ struct VideoShareView
|
||||
std::uint64_t frames_dropped = 0; // cumulative captures skipped (mutex busy at present)
|
||||
};
|
||||
|
||||
class IpcServer
|
||||
{
|
||||
class IpcServer {
|
||||
public:
|
||||
// Creates and initializes the section for `target_pid`. The injected hook
|
||||
// derives the same name from its own pid and opens it.
|
||||
@@ -98,8 +94,7 @@ public:
|
||||
void push_mkb(const MkbEvent& ev)
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
push_mkb_event(block_->mkb, ev);
|
||||
}
|
||||
}
|
||||
@@ -109,8 +104,7 @@ public:
|
||||
void set_cursor_clip_allowed(bool allowed)
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (block_ != nullptr)
|
||||
{
|
||||
if (block_ != nullptr) {
|
||||
block_->control.allow_cursor_clip.store(allowed ? 1u : 0u, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
@@ -120,8 +114,7 @@ public:
|
||||
template <typename F>
|
||||
void drain_logs(F&& emit)
|
||||
{
|
||||
if (log_ring_ != nullptr)
|
||||
{
|
||||
if (log_ring_ != nullptr) {
|
||||
log_ring_drain(*log_ring_, log_cursor_, emit);
|
||||
}
|
||||
}
|
||||
@@ -130,14 +123,8 @@ public:
|
||||
// shows color-coded in the Log window next to the hook's lines. No-op if not started.
|
||||
void host_log(std::uint32_t level, const char* text);
|
||||
|
||||
[[nodiscard]] bool running() const
|
||||
{
|
||||
return block_ != nullptr;
|
||||
}
|
||||
[[nodiscard]] unsigned long target_pid() const
|
||||
{
|
||||
return target_pid_;
|
||||
}
|
||||
[[nodiscard]] bool running() const { return block_ != nullptr; }
|
||||
[[nodiscard]] unsigned long target_pid() const { return target_pid_; }
|
||||
|
||||
private:
|
||||
void stop_locked(); // tear-down body shared by start()/stop(); caller holds mutex_
|
||||
|
||||
@@ -8,13 +8,11 @@
|
||||
#include "ui/app_chrome.hpp"
|
||||
#include "ui/text_match.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
void LogPanel::add_line(const LogRecord& rec)
|
||||
{
|
||||
if (first_millis_ == 0)
|
||||
{
|
||||
if (first_millis_ == 0) {
|
||||
first_millis_ = rec.millis;
|
||||
}
|
||||
const double secs = static_cast<double>(rec.millis - first_millis_) / 1000.0;
|
||||
@@ -22,8 +20,7 @@ void LogPanel::add_line(const LogRecord& rec)
|
||||
char buf[256];
|
||||
std::snprintf(buf, sizeof(buf), "[%8.3f] %s", secs, rec.text);
|
||||
lines_.push_back({buf, rec.level});
|
||||
while (lines_.size() > kMaxLines)
|
||||
{
|
||||
while (lines_.size() > kMaxLines) {
|
||||
lines_.pop_front();
|
||||
}
|
||||
}
|
||||
@@ -38,8 +35,7 @@ void LogPanel::draw()
|
||||
apply_panel_layout(Panel::Log);
|
||||
ImGui::Begin("Log");
|
||||
|
||||
if (ImGui::Button("Clear"))
|
||||
{
|
||||
if (ImGui::Button("Clear")) {
|
||||
lines_.clear();
|
||||
first_millis_ = 0;
|
||||
}
|
||||
@@ -50,17 +46,13 @@ void LogPanel::draw()
|
||||
ImGui::InputTextWithHint("##logfilter", "filter...", filter_, sizeof(filter_));
|
||||
|
||||
ImGui::Separator();
|
||||
if (ImGui::BeginChild("loglines", ImVec2(0, 0), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar))
|
||||
{
|
||||
if (ImGui::BeginChild("loglines", ImVec2(0, 0), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar)) {
|
||||
const bool has_filter = filter_[0] != '\0';
|
||||
for (const Line& line : lines_)
|
||||
{
|
||||
if (has_filter && !contains_ci(line.text, filter_))
|
||||
{
|
||||
for (const Line& line : lines_) {
|
||||
if (has_filter && !contains_ci(line.text, filter_)) {
|
||||
continue;
|
||||
}
|
||||
switch (line.level)
|
||||
{
|
||||
switch (line.level) {
|
||||
case LogLevel_Warn:
|
||||
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.3f, 1.0f), "%s", line.text.c_str()); // amber
|
||||
break;
|
||||
@@ -73,8 +65,7 @@ void LogPanel::draw()
|
||||
}
|
||||
}
|
||||
// Stick to the bottom while new lines arrive (unless the user scrolled up).
|
||||
if (autoscroll_ && ImGui::GetScrollY() >= ImGui::GetScrollMaxY() - 1.0f)
|
||||
{
|
||||
if (autoscroll_ && ImGui::GetScrollY() >= ImGui::GetScrollMaxY() - 1.0f) {
|
||||
ImGui::SetScrollHereY(1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,11 @@
|
||||
|
||||
#include "coop/log_ring.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class InjectionPanel;
|
||||
|
||||
class LogPanel
|
||||
{
|
||||
class LogPanel {
|
||||
public:
|
||||
// Pull any new lines the hook emitted (call once per frame before draw()).
|
||||
void pull(InjectionPanel& injection);
|
||||
@@ -25,8 +23,7 @@ public:
|
||||
private:
|
||||
void add_line(const LogRecord& rec);
|
||||
|
||||
struct Line
|
||||
{
|
||||
struct Line {
|
||||
std::string text;
|
||||
std::uint32_t level; // LogLevel, for colouring
|
||||
};
|
||||
|
||||
@@ -38,8 +38,7 @@
|
||||
#include "util/utf8.hpp"
|
||||
#include "vk_layer_setup.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
// Timestamped screenshot path next to the exe (e.g. coop_shot_20260622_143501.png).
|
||||
std::wstring screenshot_path()
|
||||
@@ -47,8 +46,8 @@ std::wstring screenshot_path()
|
||||
SYSTEMTIME st{};
|
||||
GetLocalTime(&st);
|
||||
wchar_t name[64];
|
||||
swprintf(name, static_cast<int>(std::size(name)), L"coop_shot_%04u%02u%02u_%02u%02u%02u.png", st.wYear,
|
||||
st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
|
||||
swprintf(name, static_cast<int>(std::size(name)), L"coop_shot_%04u%02u%02u_%02u%02u%02u.png", st.wYear, st.wMonth,
|
||||
st.wDay, st.wHour, st.wMinute, st.wSecond);
|
||||
return coop::exe_directory() + name;
|
||||
}
|
||||
|
||||
@@ -57,12 +56,11 @@ std::string screenshot_basename(const std::wstring& path)
|
||||
{
|
||||
const std::size_t slash = path.find_last_of(L"\\/");
|
||||
const std::wstring file = slash == std::wstring::npos ? path : path.substr(slash + 1);
|
||||
if (file.empty())
|
||||
{
|
||||
if (file.empty()) {
|
||||
return {};
|
||||
}
|
||||
const int n = WideCharToMultiByte(CP_UTF8, 0, file.c_str(), static_cast<int>(file.size()), nullptr, 0,
|
||||
nullptr, nullptr);
|
||||
const int n =
|
||||
WideCharToMultiByte(CP_UTF8, 0, file.c_str(), static_cast<int>(file.size()), nullptr, 0, nullptr, nullptr);
|
||||
std::string out(static_cast<std::size_t>(n), '\0');
|
||||
WideCharToMultiByte(CP_UTF8, 0, file.c_str(), static_cast<int>(file.size()), out.data(), n, nullptr, nullptr);
|
||||
return out;
|
||||
@@ -74,16 +72,15 @@ std::string screenshot_basename(const std::wstring& path)
|
||||
void draw_screenshot_toast(double seconds_since, const std::string& name)
|
||||
{
|
||||
const float fade = 1.0f - static_cast<float>(seconds_since) / 2.5f;
|
||||
if (fade <= 0.0f || name.empty())
|
||||
{
|
||||
if (fade <= 0.0f || name.empty()) {
|
||||
return;
|
||||
}
|
||||
const ImGuiViewport* vp = ImGui::GetMainViewport();
|
||||
ImGui::SetNextWindowPos(ImVec2(vp->WorkPos.x + 12.0f, vp->WorkPos.y + vp->WorkSize.y - 44.0f));
|
||||
ImGui::SetNextWindowBgAlpha(0.45f * fade);
|
||||
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs |
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings |
|
||||
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
|
||||
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs
|
||||
| ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings
|
||||
| ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
|
||||
ImGui::Begin("##shot_toast", nullptr, flags);
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.6f, 1.0f, 0.6f, fade));
|
||||
ImGui::Text("Saved screenshot: %s", name.c_str());
|
||||
@@ -103,98 +100,84 @@ std::string apply_test_command(const std::string& cmd, coop::UiState& ui, coop::
|
||||
{
|
||||
std::istringstream is(cmd);
|
||||
std::string t;
|
||||
while (is >> t)
|
||||
{
|
||||
while (is >> t) {
|
||||
tok.push_back(t);
|
||||
}
|
||||
}
|
||||
if (tok.empty())
|
||||
{
|
||||
if (tok.empty()) {
|
||||
return "empty";
|
||||
}
|
||||
const std::string& v = tok[0];
|
||||
auto arg = [&](std::size_t i) -> std::string { return i < tok.size() ? tok[i] : std::string(); };
|
||||
auto num = [&](std::size_t i) -> unsigned { return static_cast<unsigned>(std::strtoul(arg(i).c_str(), nullptr, 10)); };
|
||||
auto num = [&](std::size_t i) -> unsigned {
|
||||
return static_cast<unsigned>(std::strtoul(arg(i).c_str(), nullptr, 10));
|
||||
};
|
||||
|
||||
if (v == "inject")
|
||||
{
|
||||
if (v == "inject") {
|
||||
const unsigned long pid = injection.dev_inject_by_name(widen(arg(1)));
|
||||
return pid != 0 ? ("ok pid " + std::to_string(pid)) : "fail no-process-or-inject-failed";
|
||||
}
|
||||
if (v == "audio")
|
||||
{
|
||||
if (v == "audio") {
|
||||
const bool on = arg(1) == "on";
|
||||
if (on)
|
||||
{
|
||||
if (on) {
|
||||
audio.dev_set_pid(injection.target_pid());
|
||||
}
|
||||
audio.dev_set_enabled(on);
|
||||
return "ok";
|
||||
}
|
||||
if (v == "video")
|
||||
{
|
||||
if (v == "video") {
|
||||
// Install/remove the hooked video subsystem (Present/GL/D3D9/Vulkan capture hooks).
|
||||
injection.request_video(arg(1) == "on");
|
||||
return "ok";
|
||||
}
|
||||
if (v == "debug")
|
||||
{
|
||||
if (v == "debug") {
|
||||
ui.debug_details = (arg(1) == "on");
|
||||
return "ok";
|
||||
}
|
||||
if (v == "autoattach")
|
||||
{
|
||||
if (v == "autoattach") {
|
||||
injection.dev_set_auto_reattach(arg(1) == "on");
|
||||
return "ok";
|
||||
}
|
||||
if (v == "remeasure")
|
||||
{
|
||||
if (v == "remeasure") {
|
||||
audio.dev_request_op(num(1), coop::AudioRingOp_Remeasure, 0, 0, 0, 0);
|
||||
return "ok";
|
||||
}
|
||||
if (v == "override")
|
||||
{
|
||||
if (v == "override") {
|
||||
const std::uint32_t tag = arg(5) == "float" ? static_cast<std::uint32_t>(WAVE_FORMAT_IEEE_FLOAT)
|
||||
: static_cast<std::uint32_t>(WAVE_FORMAT_PCM);
|
||||
audio.dev_request_op(num(1), coop::AudioRingOp_Override, num(2), num(3), num(4), tag);
|
||||
return "ok";
|
||||
}
|
||||
if (v == "screenshot")
|
||||
{
|
||||
if (v == "screenshot") {
|
||||
const std::wstring p = screenshot_path();
|
||||
window.request_screenshot(p);
|
||||
return "ok";
|
||||
}
|
||||
if (v == "uisize")
|
||||
{
|
||||
if (v == "uisize") {
|
||||
// Force a reference layout size so the UI-fit check is monitor-independent.
|
||||
coop::set_layout_reference(static_cast<float>(num(1)), static_cast<float>(num(2)));
|
||||
return "ok";
|
||||
}
|
||||
if (v == "uifit")
|
||||
{
|
||||
if (v == "uifit") {
|
||||
// Report any panel whose content overflowed its assigned size last frame.
|
||||
char buf[256];
|
||||
coop::panel_fit_report(buf, sizeof(buf));
|
||||
return buf;
|
||||
}
|
||||
if (v == "quit")
|
||||
{
|
||||
if (v == "quit") {
|
||||
ui.request_quit = true;
|
||||
return "ok";
|
||||
}
|
||||
if (v == "status")
|
||||
{
|
||||
if (v == "status") {
|
||||
const coop::HookStatusView st = injection.hook_status();
|
||||
const std::string reason = audio.dev_reason();
|
||||
char buf[512];
|
||||
std::snprintf(buf, sizeof(buf),
|
||||
"audio_running=%d source=%s rate=%u ch=%u state=%u streams=%u inj_pid=%lu inj_state=%d "
|
||||
"reason=%s",
|
||||
audio.dev_running() ? 1 : 0, audio.dev_source().c_str(), audio.dev_rate(),
|
||||
audio.dev_channels(), st.audio_streams[0].format_state, st.audio_streams_seen,
|
||||
injection.target_pid(), static_cast<int>(injection.target_state()),
|
||||
reason.empty() ? "-" : reason.c_str());
|
||||
audio.dev_running() ? 1 : 0, audio.dev_source().c_str(), audio.dev_rate(), audio.dev_channels(),
|
||||
st.audio_streams[0].format_state, st.audio_streams_seen, injection.target_pid(),
|
||||
static_cast<int>(injection.target_state()), reason.empty() ? "-" : reason.c_str());
|
||||
return buf;
|
||||
}
|
||||
return "unknown-command";
|
||||
@@ -209,8 +192,7 @@ std::string steam_manifest_path()
|
||||
const DWORD len = GetModuleFileNameA(nullptr, buffer, MAX_PATH);
|
||||
std::string path(buffer, len);
|
||||
const std::size_t slash = path.find_last_of("\\/");
|
||||
if (slash != std::string::npos)
|
||||
{
|
||||
if (slash != std::string::npos) {
|
||||
path.resize(slash + 1);
|
||||
}
|
||||
return path + "steam_input_actions.vdf";
|
||||
@@ -226,16 +208,15 @@ void draw_vk_too_late_banner()
|
||||
{
|
||||
const ImGuiViewport* vp = ImGui::GetMainViewport();
|
||||
float w = vp->WorkSize.x - 40.0f;
|
||||
if (w > 760.0f)
|
||||
{
|
||||
if (w > 760.0f) {
|
||||
w = 760.0f;
|
||||
}
|
||||
ImGui::SetNextWindowPos(ImVec2(vp->WorkPos.x + vp->WorkSize.x * 0.5f, vp->WorkPos.y + 16.0f),
|
||||
ImGuiCond_Always, ImVec2(0.5f, 0.0f));
|
||||
ImGui::SetNextWindowPos(ImVec2(vp->WorkPos.x + vp->WorkSize.x * 0.5f, vp->WorkPos.y + 16.0f), ImGuiCond_Always,
|
||||
ImVec2(0.5f, 0.0f));
|
||||
ImGui::SetNextWindowSize(ImVec2(w, 0.0f));
|
||||
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs |
|
||||
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing |
|
||||
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_AlwaysAutoResize;
|
||||
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs
|
||||
| ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing
|
||||
| ImGuiWindowFlags_NoNav | ImGuiWindowFlags_AlwaysAutoResize;
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.28f, 0.03f, 0.03f, 0.92f));
|
||||
ImGui::Begin("##vk_too_late", nullptr, flags);
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.5f, 0.45f, 1.0f));
|
||||
@@ -255,15 +236,14 @@ void draw_vk_too_late_banner()
|
||||
void draw_overlay_hidden_hint(double seconds_hidden)
|
||||
{
|
||||
const float fade = 1.0f - static_cast<float>(seconds_hidden) / 4.0f;
|
||||
if (fade <= 0.0f)
|
||||
{
|
||||
if (fade <= 0.0f) {
|
||||
return; // fully faded -> truly clean window for RPT capture
|
||||
}
|
||||
ImGui::SetNextWindowPos(ImVec2(12.0f, 12.0f));
|
||||
ImGui::SetNextWindowBgAlpha(0.35f * fade);
|
||||
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs |
|
||||
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings |
|
||||
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
|
||||
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs
|
||||
| ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings
|
||||
| ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav;
|
||||
ImGui::Begin("##overlay_hint", nullptr, flags);
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 1.0f, fade));
|
||||
ImGui::TextUnformatted("F1: show overlay");
|
||||
@@ -281,11 +261,9 @@ bool wait_for_hooked_frame(coop::D3D11Window& window, coop::InjectionPanel& inje
|
||||
QueryPerformanceFrequency(&freq);
|
||||
QueryPerformanceCounter(&start);
|
||||
constexpr double kTimeoutMs = 200.0; // present anyway if the game stalls / is paused
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
const std::uint32_t gen = injection.video_share().generation;
|
||||
if (gen != last_gen)
|
||||
{
|
||||
if (gen != last_gen) {
|
||||
last_gen = gen;
|
||||
return true;
|
||||
}
|
||||
@@ -293,13 +271,11 @@ bool wait_for_hooked_frame(coop::D3D11Window& window, coop::InjectionPanel& inje
|
||||
QueryPerformanceCounter(&now);
|
||||
const double elapsed =
|
||||
static_cast<double>(now.QuadPart - start.QuadPart) * 1000.0 / static_cast<double>(freq.QuadPart);
|
||||
if (elapsed >= kTimeoutMs)
|
||||
{
|
||||
if (elapsed >= kTimeoutMs) {
|
||||
last_gen = gen;
|
||||
return true;
|
||||
}
|
||||
if (!window.pump_messages())
|
||||
{
|
||||
if (!window.pump_messages()) {
|
||||
return false; // WM_QUIT
|
||||
}
|
||||
Sleep(1); // yield ~1 ms (timeBeginPeriod(1) keeps this granular) instead of busy-spinning
|
||||
@@ -313,8 +289,7 @@ int run()
|
||||
coop::cleanup_stale_vk_layer();
|
||||
|
||||
coop::D3D11Window window;
|
||||
if (!window.create(L"CoopAllTheThings"))
|
||||
{
|
||||
if (!window.create(L"CoopAllTheThings")) {
|
||||
MessageBoxW(nullptr, L"Failed to create the D3D11 window.", L"CoopAllTheThings", MB_ICONERROR);
|
||||
return 1;
|
||||
}
|
||||
@@ -324,8 +299,7 @@ int run()
|
||||
// Declaring ui first means it's destroyed AFTER imgui, so that final save never reads freed state.
|
||||
coop::UiState ui;
|
||||
coop::ImGuiLayer imgui;
|
||||
if (!imgui.init(window.hwnd(), window.device(), window.context()))
|
||||
{
|
||||
if (!imgui.init(window.hwnd(), window.device(), window.context())) {
|
||||
MessageBoxW(nullptr, L"Failed to initialize ImGui.", L"CoopAllTheThings", MB_ICONERROR);
|
||||
return 1;
|
||||
}
|
||||
@@ -335,8 +309,7 @@ int run()
|
||||
coop::AudioPanel audio;
|
||||
coop::CapturePanel capture;
|
||||
coop::LogPanel log;
|
||||
if (!capture.init(window.device()))
|
||||
{
|
||||
if (!capture.init(window.device())) {
|
||||
MessageBoxW(nullptr, L"Failed to initialize the video mirror.", L"CoopAllTheThings", MB_ICONERROR);
|
||||
return 1;
|
||||
}
|
||||
@@ -376,22 +349,18 @@ int run()
|
||||
// Frame-sync: the hook generation we last presented (so we wait for the next one).
|
||||
std::uint32_t last_synced_gen = 0;
|
||||
|
||||
while (window.pump_messages())
|
||||
{
|
||||
while (window.pump_messages()) {
|
||||
// Rescale the overlay if the window changed DPI (moved monitors, or the display scale changed).
|
||||
// pump_messages() latches the new DPI; apply it here, outside any in-progress ImGui frame.
|
||||
if (unsigned new_dpi = 0; window.take_dpi_change(new_dpi))
|
||||
{
|
||||
if (unsigned new_dpi = 0; window.take_dpi_change(new_dpi)) {
|
||||
imgui.set_dpi(new_dpi);
|
||||
}
|
||||
|
||||
// When the operator enabled "Sync flip to game frames" (Hooked source), pace the
|
||||
// whole iteration to the game: wait for the next published frame before rendering,
|
||||
// then present without vsync so the flip lands in lockstep with the game.
|
||||
if (capture.frame_sync_active())
|
||||
{
|
||||
if (!wait_for_hooked_frame(window, injection, last_synced_gen))
|
||||
{
|
||||
if (capture.frame_sync_active()) {
|
||||
if (!wait_for_hooked_frame(window, injection, last_synced_gen)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -401,12 +370,9 @@ int run()
|
||||
const coop::InputSnapshot input_snapshot = input_worker.snapshot();
|
||||
#ifdef COOP_WITH_STEAM
|
||||
input_worker.set_want_steam(controllers.steam_input_requested());
|
||||
if (input_worker.steam_failed())
|
||||
{
|
||||
if (input_worker.steam_failed()) {
|
||||
controllers.on_steam_init_failed(); // resets the toggle; worker falls back to XInput
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
controllers.set_steam_active(input_snapshot.steam_active);
|
||||
}
|
||||
#endif
|
||||
@@ -422,58 +388,45 @@ int run()
|
||||
log.pull(injection); // drain hook log lines even while the Log window is hidden
|
||||
|
||||
#ifdef COOP_TEST_HARNESS
|
||||
if (std::string tcmd = harness.poll_command(); !tcmd.empty())
|
||||
{
|
||||
if (std::string tcmd = harness.poll_command(); !tcmd.empty()) {
|
||||
harness.write_response(apply_test_command(tcmd, ui, injection, audio, window));
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_F1, false))
|
||||
{
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_F1, false)) {
|
||||
show_overlay = !show_overlay;
|
||||
if (!show_overlay)
|
||||
{
|
||||
if (!show_overlay) {
|
||||
overlay_hidden_at = ImGui::GetTime();
|
||||
}
|
||||
}
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_F2, false))
|
||||
{
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_F2, false)) {
|
||||
injection.toggle_cursor_release(); // free/clip the operator's mouse for clipping games
|
||||
}
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_F10, false))
|
||||
{
|
||||
if (ImGui::IsKeyPressed(ImGuiKey_F10, false)) {
|
||||
window.request_screenshot(screenshot_path()); // captured at Present, overlay included
|
||||
}
|
||||
|
||||
coop::reset_panel_fit(); // panels record their overflow as they draw (UI-fit check)
|
||||
coop::set_layout_debug(ui.debug_details); // center split adapts to the debug verbosity
|
||||
if (show_overlay)
|
||||
{
|
||||
if (show_overlay) {
|
||||
coop::draw_main_menu_bar(ui, stats);
|
||||
if (ui.show_controllers)
|
||||
{
|
||||
if (ui.show_controllers) {
|
||||
controllers.draw(input_snapshot, injection.hook_status(), ui.debug_details);
|
||||
}
|
||||
if (ui.show_injection)
|
||||
{
|
||||
if (ui.show_injection) {
|
||||
injection.draw(ui.debug_details);
|
||||
}
|
||||
if (ui.show_audio)
|
||||
{
|
||||
if (ui.show_audio) {
|
||||
audio.draw_ui(injection.hook_status(), ui.debug_details);
|
||||
}
|
||||
if (ui.show_video)
|
||||
{
|
||||
if (ui.show_video) {
|
||||
capture.draw_ui(stats);
|
||||
}
|
||||
if (ui.show_log)
|
||||
{
|
||||
if (ui.show_log) {
|
||||
log.draw();
|
||||
}
|
||||
draw_screenshot_toast(ImGui::GetTime() - last_shot_at, last_shot_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
draw_overlay_hidden_hint(ImGui::GetTime() - overlay_hidden_at);
|
||||
}
|
||||
if (injection.hook_status().vk_too_late) // Vulkan game injected too late -> relaunch prompt
|
||||
@@ -510,8 +463,7 @@ int run()
|
||||
// A host-side TDR / driver reset / GPU hang surfaces as a lost device on Present. We don't
|
||||
// attempt to recreate the device (it would have to re-init ImGui + the capture pipeline);
|
||||
// surface it and stop cleanly rather than spin forever rendering nothing.
|
||||
if (window.device_lost())
|
||||
{
|
||||
if (window.device_lost()) {
|
||||
wchar_t msg[320];
|
||||
swprintf_s(msg,
|
||||
L"The graphics device was lost (0x%08lX) -- a driver reset, GPU hang, or TDR on "
|
||||
@@ -523,8 +475,7 @@ int run()
|
||||
|
||||
// render_frame saves a pending F10 screenshot just before Present; pick up the
|
||||
// result here so next frame shows the confirmation toast (kept out of the shot).
|
||||
if (std::wstring shot = window.take_screenshot_result(); !shot.empty())
|
||||
{
|
||||
if (std::wstring shot = window.take_screenshot_result(); !shot.empty()) {
|
||||
last_shot_at = ImGui::GetTime();
|
||||
last_shot_name = screenshot_basename(shot);
|
||||
}
|
||||
|
||||
@@ -6,10 +6,8 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
namespace coop {
|
||||
namespace {
|
||||
std::wstring temp_file(const wchar_t* name)
|
||||
{
|
||||
wchar_t dir[MAX_PATH] = {};
|
||||
@@ -29,8 +27,7 @@ void TestHarness::init()
|
||||
std::string TestHarness::poll_command()
|
||||
{
|
||||
std::ifstream f(cmd_path_.c_str()); // MSVC accepts a wide path
|
||||
if (!f)
|
||||
{
|
||||
if (!f) {
|
||||
return {};
|
||||
}
|
||||
std::string line;
|
||||
|
||||
@@ -12,11 +12,9 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
class TestHarness
|
||||
{
|
||||
class TestHarness {
|
||||
public:
|
||||
#ifdef COOP_TEST_HARNESS
|
||||
void init(); // resolve the %TEMP% file paths and clear any stale command
|
||||
@@ -32,10 +30,7 @@ private:
|
||||
#else
|
||||
// No-op shims so call sites don't need their own #ifdef.
|
||||
void init() {}
|
||||
std::string poll_command()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
std::string poll_command() { return {}; }
|
||||
void write_response(const std::string&) {}
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -8,11 +8,9 @@
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h" // ImGuiSettingsHandler / AddSettingsHandler (custom .ini section)
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
// --- Custom .ini persistence for the UI switches ---------------------------
|
||||
// We piggy-back on ImGui's .ini so the "Debug details" verbosity survives restarts
|
||||
// without inventing a separate settings file. The section looks like:
|
||||
@@ -33,8 +31,7 @@ void ui_settings_read_line(ImGuiContext*, ImGuiSettingsHandler*, void* entry, co
|
||||
auto* ui = static_cast<UiState*>(entry);
|
||||
// Manual parse (avoids the sscanf CRT-secure deprecation for a single int key).
|
||||
constexpr char kKey[] = "DebugDetails=";
|
||||
if (std::strncmp(line, kKey, sizeof(kKey) - 1) == 0)
|
||||
{
|
||||
if (std::strncmp(line, kKey, sizeof(kKey) - 1) == 0) {
|
||||
ui->debug_details = std::atoi(line + sizeof(kKey) - 1) != 0;
|
||||
}
|
||||
}
|
||||
@@ -66,8 +63,7 @@ float g_ref_h = 0.0f;
|
||||
bool g_layout_debug = false;
|
||||
|
||||
// Per-frame panel-overflow registry (UI-fit instrumentation).
|
||||
struct PanelFit
|
||||
{
|
||||
struct PanelFit {
|
||||
char name[24];
|
||||
float over_x;
|
||||
float over_y;
|
||||
@@ -79,8 +75,7 @@ int g_fit_count = 0;
|
||||
void register_ui_settings(UiState& ui)
|
||||
{
|
||||
// Idempotent: don't stack a second handler if this is somehow called twice.
|
||||
if (ImGui::FindSettingsHandler(kUiSettingsType) != nullptr)
|
||||
{
|
||||
if (ImGui::FindSettingsHandler(kUiSettingsType) != nullptr) {
|
||||
return;
|
||||
}
|
||||
ImGuiSettingsHandler handler;
|
||||
@@ -106,8 +101,7 @@ void set_layout_persisted(bool had_persisted_layout)
|
||||
void apply_layout_end_frame()
|
||||
{
|
||||
g_layout_reset = false;
|
||||
if (g_startup_force > 0)
|
||||
{
|
||||
if (g_startup_force > 0) {
|
||||
--g_startup_force;
|
||||
}
|
||||
}
|
||||
@@ -135,8 +129,7 @@ void record_panel_fit(const char* name)
|
||||
// so > 0 on either axis means content is cut off at the assigned size.
|
||||
const float ox = ImGui::GetScrollMaxX();
|
||||
const float oy = ImGui::GetScrollMaxY();
|
||||
if (g_fit_count >= static_cast<int>(sizeof(g_fits) / sizeof(g_fits[0])))
|
||||
{
|
||||
if (g_fit_count >= static_cast<int>(sizeof(g_fits) / sizeof(g_fits[0]))) {
|
||||
return;
|
||||
}
|
||||
PanelFit& f = g_fits[g_fit_count++];
|
||||
@@ -148,17 +141,14 @@ void record_panel_fit(const char* name)
|
||||
bool panel_fit_overflow(float* worst_x, float* worst_y)
|
||||
{
|
||||
float mx = 0.0f, my = 0.0f;
|
||||
for (int i = 0; i < g_fit_count; ++i)
|
||||
{
|
||||
for (int i = 0; i < g_fit_count; ++i) {
|
||||
mx = std::max(mx, g_fits[i].over_x);
|
||||
my = std::max(my, g_fits[i].over_y);
|
||||
}
|
||||
if (worst_x != nullptr)
|
||||
{
|
||||
if (worst_x != nullptr) {
|
||||
*worst_x = mx;
|
||||
}
|
||||
if (worst_y != nullptr)
|
||||
{
|
||||
if (worst_y != nullptr) {
|
||||
*worst_y = my;
|
||||
}
|
||||
return mx > 0.5f || my > 0.5f;
|
||||
@@ -166,24 +156,20 @@ bool panel_fit_overflow(float* worst_x, float* worst_y)
|
||||
|
||||
void panel_fit_report(char* buf, int cap)
|
||||
{
|
||||
if (buf == nullptr || cap <= 0)
|
||||
{
|
||||
if (buf == nullptr || cap <= 0) {
|
||||
return;
|
||||
}
|
||||
int n = 0;
|
||||
bool any = false;
|
||||
for (int i = 0; i < g_fit_count && n < cap - 1; ++i)
|
||||
{
|
||||
if (g_fits[i].over_x <= 0.5f && g_fits[i].over_y <= 0.5f)
|
||||
{
|
||||
for (int i = 0; i < g_fit_count && n < cap - 1; ++i) {
|
||||
if (g_fits[i].over_x <= 0.5f && g_fits[i].over_y <= 0.5f) {
|
||||
continue;
|
||||
}
|
||||
any = true;
|
||||
n += std::snprintf(buf + n, static_cast<size_t>(cap - n), "%s%s:%.0f,%.0f", n > 0 ? " " : "",
|
||||
g_fits[i].name, g_fits[i].over_x, g_fits[i].over_y);
|
||||
n += std::snprintf(buf + n, static_cast<size_t>(cap - n), "%s%s:%.0f,%.0f", n > 0 ? " " : "", g_fits[i].name,
|
||||
g_fits[i].over_x, g_fits[i].over_y);
|
||||
}
|
||||
if (!any)
|
||||
{
|
||||
if (!any) {
|
||||
std::snprintf(buf, static_cast<size_t>(cap), "fit");
|
||||
}
|
||||
}
|
||||
@@ -227,8 +213,7 @@ void apply_panel_layout(Panel panel)
|
||||
const float audio_h = stack_avail * audio_frac;
|
||||
|
||||
ImVec2 pos, size;
|
||||
switch (panel)
|
||||
{
|
||||
switch (panel) {
|
||||
case Panel::Injection:
|
||||
pos = ImVec2(left_x, top);
|
||||
size = ImVec2(left_w, full_h);
|
||||
@@ -255,8 +240,7 @@ void apply_panel_layout(Panel panel)
|
||||
// install with no saved layout to restore. Otherwise FirstUseEver lets ImGui's
|
||||
// restored .ini positions stand (and still seeds any brand-new panel). A forced
|
||||
// reference size (UI-fit check) also forces, so the assigned sizes are exact.
|
||||
const bool force =
|
||||
g_layout_reset || g_ref_w > 0.0f || (!g_had_persisted_layout && g_startup_force > 0);
|
||||
const bool force = g_layout_reset || g_ref_w > 0.0f || (!g_had_persisted_layout && g_startup_force > 0);
|
||||
const ImGuiCond cond = force ? ImGuiCond_Always : ImGuiCond_FirstUseEver;
|
||||
ImGui::SetNextWindowPos(pos, cond);
|
||||
ImGui::SetNextWindowSize(size, cond);
|
||||
@@ -265,44 +249,37 @@ void apply_panel_layout(Panel panel)
|
||||
float draw_main_menu_bar(UiState& ui, const FrameStats& stats)
|
||||
{
|
||||
float height = 0.0f;
|
||||
if (!ImGui::BeginMainMenuBar())
|
||||
{
|
||||
if (!ImGui::BeginMainMenuBar()) {
|
||||
return height;
|
||||
}
|
||||
|
||||
ImGui::TextUnformatted("CoopAllTheThings");
|
||||
ImGui::Separator();
|
||||
|
||||
if (ImGui::BeginMenu("File"))
|
||||
{
|
||||
if (ImGui::MenuItem("Exit", "Alt+F4"))
|
||||
{
|
||||
if (ImGui::BeginMenu("File")) {
|
||||
if (ImGui::MenuItem("Exit", "Alt+F4")) {
|
||||
ui.request_quit = true; // the main loop sees this and stops
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu("View"))
|
||||
{
|
||||
if (ImGui::BeginMenu("View")) {
|
||||
ImGui::MenuItem("Controllers", nullptr, &ui.show_controllers);
|
||||
ImGui::MenuItem("Injection", nullptr, &ui.show_injection);
|
||||
ImGui::MenuItem("Video mirror", nullptr, &ui.show_video);
|
||||
ImGui::MenuItem("Audio mirror", nullptr, &ui.show_audio);
|
||||
ImGui::MenuItem("Log", nullptr, &ui.show_log);
|
||||
ImGui::Separator();
|
||||
if (ImGui::MenuItem("Debug details", nullptr, &ui.debug_details))
|
||||
{
|
||||
if (ImGui::MenuItem("Debug details", nullptr, &ui.debug_details)) {
|
||||
ImGui::MarkIniSettingsDirty(); // persist the new verbosity to coop_layout.ini
|
||||
}
|
||||
if (ImGui::MenuItem("Reset layout"))
|
||||
{
|
||||
if (ImGui::MenuItem("Reset layout")) {
|
||||
request_layout_reset();
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu("Help"))
|
||||
{
|
||||
if (ImGui::BeginMenu("Help")) {
|
||||
ImGui::TextDisabled("F1 hide/show this overlay");
|
||||
ImGui::TextDisabled("F2 release/clip the operator cursor");
|
||||
ImGui::TextDisabled("F10 save a screenshot (PNG, next to the exe)");
|
||||
@@ -318,16 +295,14 @@ float draw_main_menu_bar(UiState& ui, const FrameStats& stats)
|
||||
char perf[96];
|
||||
// Fixed field widths so the readout doesn't jitter/blur as values cross digit
|
||||
// thresholds (e.g. 99 -> 100) each frame.
|
||||
std::snprintf(perf, sizeof(perf), "%4.0f FPS %6.2f ms (%6.2f-%6.2f)", stats.fps(), stats.avg_ms(),
|
||||
stats.min_ms(), stats.max_ms());
|
||||
std::snprintf(perf, sizeof(perf), "%4.0f FPS %6.2f ms (%6.2f-%6.2f)", stats.fps(), stats.avg_ms(), stats.min_ms(),
|
||||
stats.max_ms());
|
||||
const float text_w = ImGui::CalcTextSize(perf).x;
|
||||
ImGui::SameLine(ImGui::GetWindowWidth() - text_w - ImGui::GetStyle().FramePadding.x * 2.0f);
|
||||
if (stats.max_ms() > 25.0f) // ~sub-40 FPS spike in the window
|
||||
{
|
||||
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.3f, 1.0f), "%s", perf);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImGui::TextUnformatted(perf);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,14 +6,12 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// Visibility + verbosity shared by all panels. Panels read `debug_details` to
|
||||
// gate verbose diagnostics; the main loop reads the per-panel flags to decide
|
||||
// what to draw.
|
||||
struct UiState
|
||||
{
|
||||
struct UiState {
|
||||
bool show_controllers = true;
|
||||
bool show_injection = true;
|
||||
bool show_video = true;
|
||||
@@ -31,8 +29,7 @@ struct UiState
|
||||
void register_ui_settings(UiState& ui);
|
||||
|
||||
// The overlay panels, for the shared default layout below.
|
||||
enum class Panel
|
||||
{
|
||||
enum class Panel {
|
||||
Injection, // left column, full height (room for hook diagnostics)
|
||||
Controllers, // center column, top
|
||||
Video, // center column, below Controllers
|
||||
@@ -89,26 +86,22 @@ void panel_fit_report(char* buf, int cap);
|
||||
|
||||
// Rolling frame-timing over a ~1 s window, recomputed each window so the status
|
||||
// bar can show a stable FPS plus the min/max frame time (jitter) underneath it.
|
||||
class FrameStats
|
||||
{
|
||||
class FrameStats {
|
||||
public:
|
||||
// Number of frame samples kept for the graphs (~2 s at 120 FPS).
|
||||
static constexpr int kHistory = 240;
|
||||
|
||||
void tick(float dt_ms)
|
||||
{
|
||||
if (dt_ms < cur_min_)
|
||||
{
|
||||
if (dt_ms < cur_min_) {
|
||||
cur_min_ = dt_ms;
|
||||
}
|
||||
if (dt_ms > cur_max_)
|
||||
{
|
||||
if (dt_ms > cur_max_) {
|
||||
cur_max_ = dt_ms;
|
||||
}
|
||||
accum_ms_ += dt_ms;
|
||||
++frames_;
|
||||
if (accum_ms_ >= 1000.0f && frames_ > 0)
|
||||
{
|
||||
if (accum_ms_ >= 1000.0f && frames_ > 0) {
|
||||
avg_ms_ = accum_ms_ / static_cast<float>(frames_);
|
||||
min_ms_ = cur_min_;
|
||||
max_ms_ = cur_max_;
|
||||
@@ -120,43 +113,26 @@ public:
|
||||
|
||||
history_[hist_pos_] = dt_ms;
|
||||
hist_pos_ = (hist_pos_ + 1) % kHistory;
|
||||
if (hist_count_ < kHistory)
|
||||
{
|
||||
if (hist_count_ < kHistory) {
|
||||
++hist_count_;
|
||||
}
|
||||
}
|
||||
|
||||
// --- 1 s windowed aggregates (stable readout for the menu bar) ---------
|
||||
[[nodiscard]] float avg_ms() const
|
||||
{
|
||||
return avg_ms_;
|
||||
}
|
||||
[[nodiscard]] float min_ms() const
|
||||
{
|
||||
return min_ms_;
|
||||
}
|
||||
[[nodiscard]] float max_ms() const
|
||||
{
|
||||
return max_ms_;
|
||||
}
|
||||
[[nodiscard]] float fps() const
|
||||
{
|
||||
return avg_ms_ > 0.0f ? 1000.0f / avg_ms_ : 0.0f;
|
||||
}
|
||||
[[nodiscard]] float avg_ms() const { return avg_ms_; }
|
||||
[[nodiscard]] float min_ms() const { return min_ms_; }
|
||||
[[nodiscard]] float max_ms() const { return max_ms_; }
|
||||
[[nodiscard]] float fps() const { return avg_ms_ > 0.0f ? 1000.0f / avg_ms_ : 0.0f; }
|
||||
|
||||
// --- Sample history (for graphs) ---------------------------------------
|
||||
[[nodiscard]] int history_size() const
|
||||
{
|
||||
return hist_count_;
|
||||
}
|
||||
[[nodiscard]] int history_size() const { return hist_count_; }
|
||||
|
||||
// Copy the frame-time samples (ms) into `out` oldest-to-newest; `out` must
|
||||
// hold at least kHistory floats. Returns the number written.
|
||||
int copy_frame_ms(float* out) const
|
||||
{
|
||||
const int start = (hist_pos_ - hist_count_ + kHistory * 2) % kHistory;
|
||||
for (int i = 0; i < hist_count_; ++i)
|
||||
{
|
||||
for (int i = 0; i < hist_count_; ++i) {
|
||||
out[i] = history_[(start + i) % kHistory];
|
||||
}
|
||||
return hist_count_;
|
||||
@@ -165,14 +141,12 @@ public:
|
||||
// min / max / mean over the whole retained history (order-independent).
|
||||
void history_stats(float& min_ms, float& max_ms, float& avg_ms) const
|
||||
{
|
||||
if (hist_count_ == 0)
|
||||
{
|
||||
if (hist_count_ == 0) {
|
||||
min_ms = max_ms = avg_ms = 0.0f;
|
||||
return;
|
||||
}
|
||||
float mn = 1.0e9f, mx = 0.0f, sum = 0.0f;
|
||||
for (int i = 0; i < hist_count_; ++i)
|
||||
{
|
||||
for (int i = 0; i < hist_count_; ++i) {
|
||||
const float v = history_[i];
|
||||
mn = std::min(mn, v);
|
||||
mx = std::max(mx, v);
|
||||
|
||||
@@ -6,13 +6,11 @@
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
inline std::string ascii_lower(std::string s)
|
||||
{
|
||||
for (char& c : s)
|
||||
{
|
||||
for (char& c : s) {
|
||||
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
}
|
||||
return s;
|
||||
@@ -21,8 +19,7 @@ inline std::string ascii_lower(std::string s)
|
||||
// True when `needle` is empty or a case-insensitive substring of `haystack`.
|
||||
inline bool contains_ci(const std::string& haystack, const char* needle)
|
||||
{
|
||||
if (needle == nullptr || needle[0] == '\0')
|
||||
{
|
||||
if (needle == nullptr || needle[0] == '\0') {
|
||||
return true;
|
||||
}
|
||||
return ascii_lower(haystack).find(ascii_lower(needle)) != std::string::npos;
|
||||
|
||||
@@ -6,14 +6,12 @@
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// UTF-16 -> UTF-8.
|
||||
inline std::string narrow(const std::wstring& w)
|
||||
{
|
||||
if (w.empty())
|
||||
{
|
||||
if (w.empty()) {
|
||||
return {};
|
||||
}
|
||||
const int n = WideCharToMultiByte(CP_UTF8, 0, w.c_str(), static_cast<int>(w.size()), nullptr, 0, nullptr, nullptr);
|
||||
@@ -25,8 +23,7 @@ inline std::string narrow(const std::wstring& w)
|
||||
// UTF-8 -> UTF-16.
|
||||
inline std::wstring widen(const std::string& s)
|
||||
{
|
||||
if (s.empty())
|
||||
{
|
||||
if (s.empty()) {
|
||||
return {};
|
||||
}
|
||||
const int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), static_cast<int>(s.size()), nullptr, 0);
|
||||
|
||||
@@ -5,10 +5,8 @@
|
||||
#include "coop/tool_paths.hpp"
|
||||
#include "util/utf8.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
namespace coop {
|
||||
namespace {
|
||||
// The Vulkan loader's per-user implicit-layer registry list. Each value is a manifest path; its
|
||||
// DWORD data 0 = enabled.
|
||||
constexpr const wchar_t* kImplicitLayersKey = L"SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers";
|
||||
@@ -30,14 +28,12 @@ bool register_vk_layer(const std::wstring& target_image)
|
||||
{
|
||||
// Write the scoping file (target image basename, UTF-8) the layer checks against its own image.
|
||||
const std::wstring sf = scoping_file();
|
||||
if (!sf.empty())
|
||||
{
|
||||
if (!sf.empty()) {
|
||||
const std::size_t slash = target_image.find_last_of(L"\\/");
|
||||
const std::wstring base = slash == std::wstring::npos ? target_image : target_image.substr(slash + 1);
|
||||
const std::string utf8 = narrow(base);
|
||||
HANDLE f = CreateFileW(sf.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (f != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (f != INVALID_HANDLE_VALUE) {
|
||||
DWORD written = 0;
|
||||
WriteFile(f, utf8.data(), static_cast<DWORD>(utf8.size()), &written, nullptr);
|
||||
CloseHandle(f);
|
||||
@@ -45,15 +41,14 @@ bool register_vk_layer(const std::wstring& target_image)
|
||||
}
|
||||
|
||||
HKEY key = nullptr;
|
||||
if (RegCreateKeyExW(HKEY_CURRENT_USER, kImplicitLayersKey, 0, nullptr, 0, KEY_SET_VALUE, nullptr, &key,
|
||||
nullptr) != ERROR_SUCCESS)
|
||||
{
|
||||
if (RegCreateKeyExW(HKEY_CURRENT_USER, kImplicitLayersKey, 0, nullptr, 0, KEY_SET_VALUE, nullptr, &key, nullptr)
|
||||
!= ERROR_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
const std::wstring mp = manifest_path();
|
||||
DWORD enabled = 0; // 0 = enabled, per the loader's convention
|
||||
const LONG r = RegSetValueExW(key, mp.c_str(), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&enabled),
|
||||
sizeof(enabled));
|
||||
const LONG r =
|
||||
RegSetValueExW(key, mp.c_str(), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&enabled), sizeof(enabled));
|
||||
RegCloseKey(key);
|
||||
return r == ERROR_SUCCESS;
|
||||
}
|
||||
@@ -61,14 +56,12 @@ bool register_vk_layer(const std::wstring& target_image)
|
||||
void unregister_vk_layer()
|
||||
{
|
||||
HKEY key = nullptr;
|
||||
if (RegOpenKeyExW(HKEY_CURRENT_USER, kImplicitLayersKey, 0, KEY_SET_VALUE, &key) == ERROR_SUCCESS)
|
||||
{
|
||||
if (RegOpenKeyExW(HKEY_CURRENT_USER, kImplicitLayersKey, 0, KEY_SET_VALUE, &key) == ERROR_SUCCESS) {
|
||||
RegDeleteValueW(key, manifest_path().c_str());
|
||||
RegCloseKey(key);
|
||||
}
|
||||
const std::wstring sf = scoping_file();
|
||||
if (!sf.empty())
|
||||
{
|
||||
if (!sf.empty()) {
|
||||
DeleteFileW(sf.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user