mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-06-10 01:14:53 +08:00
Compare commits
153 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fea238722d | |||
| 178d958c08 | |||
| f9633f366b | |||
| 7a81f3e767 | |||
| bbbbbf7927 | |||
| 5735f84008 | |||
| fe0cf42e5d | |||
| 04100584ce | |||
| 9d3a422916 | |||
| 65d8284fb8 | |||
| eea107db66 | |||
| 67812957db | |||
| 69d77e59cc | |||
| efcebfd82c | |||
| 32345f93bf | |||
| 193e4bd5bf | |||
| 53edf3d57e | |||
| 895e297771 | |||
| 8f3959e6c6 | |||
| 5ff6b601c7 | |||
| 4895ac9c23 | |||
| f121aa47f7 | |||
| 00a8d59284 | |||
| a30489e05b | |||
| dfbeb3ed20 | |||
| 2eed1c974e | |||
| 63a79a90ac | |||
| a61e74d97b | |||
| 54438a4aa1 | |||
| 7682ad63e4 | |||
| 06c53fdc9c | |||
| 665f4e684c | |||
| 52b894fe0e | |||
| 82c0cbbad4 | |||
| ce004af379 | |||
| 15bd9e9fdc | |||
| 37aabeaf72 | |||
| 473737ac9b | |||
| 1e29ec708f | |||
| 515d517a99 | |||
| a3aedcb624 | |||
| 98b7c6c966 | |||
| b1d956af2c | |||
| b7a031bb7f | |||
| 15cce07b6e | |||
| 1d5d6f5121 | |||
| 5f541f5c8b | |||
| 71bce08549 | |||
| 37b9badb2a | |||
| 4089e80fe8 | |||
| 2be6e727ce | |||
| d3b886c3f6 | |||
| 97e48bfe71 | |||
| a8769dee06 | |||
| ffa94986d5 | |||
| e4dfb61509 | |||
| d42b6e3261 | |||
| 855b15025c | |||
| 3701b2c0d9 | |||
| ecbec4d301 | |||
| 21425c7132 | |||
| 3e95a7ba29 | |||
| c1394db285 | |||
| eee6c588bd | |||
| eca68f6c7a | |||
| f4e28d8774 | |||
| 21b179e01c | |||
| 83cacf6f51 | |||
| 13c37f01b1 | |||
| 511831ced3 | |||
| 518e1afa58 | |||
| 43d03ac081 | |||
| f7f62c5fe0 | |||
| 2bbddbca6b | |||
| f0f8f27f4c | |||
| 262af263f2 | |||
| 38b7775b1b | |||
| 56c0bca62f | |||
| 4b1b09fd5b | |||
| 1d6425bbf4 | |||
| 5ec6552d25 | |||
| 79e4a0790a | |||
| 1d3cac54ab | |||
| 2f26334775 | |||
| 9270d528e3 | |||
| 91db3a7e34 | |||
| d017561e54 | |||
| 8e8a85bae3 | |||
| bea89e9111 | |||
| 499ce0190a | |||
| 91bde91238 | |||
| 3e31ba102d | |||
| 263c5eefd3 | |||
| b230b851e4 | |||
| ff32477ffe | |||
| c6c60decdb | |||
| 7505adeca8 | |||
| 754f1fba88 | |||
| 8be46b870a | |||
| 81cb1d6c0b | |||
| 319416f1b7 | |||
| d679c6251b | |||
| a14baafda7 | |||
| cfdc7d3106 | |||
| 33d51b8ce5 | |||
| b13dac2093 | |||
| a605c95e5a | |||
| 9a5553a636 | |||
| ef02403da6 | |||
| adfab363c1 | |||
| 123d4cf595 | |||
| 19feb8ff49 | |||
| 9223bf9d2d | |||
| 11b5f87841 | |||
| cea59fb453 | |||
| 3a179bf480 | |||
| b9c53024f1 | |||
| 62b37ad698 | |||
| de56cd5d3b | |||
| 8d9d78185a | |||
| b10a6512fe | |||
| a94da8802f | |||
| 4e6f82d00c | |||
| 5e2ad99ec0 | |||
| 8ab50ea362 | |||
| 25e9958a69 | |||
| 410ea8b96b | |||
| e656664cad | |||
| 0e6cee0961 | |||
| 42506b8c1d | |||
| e35365d162 | |||
| bf1c0f796d | |||
| 547532b28c | |||
| a91e23abf6 | |||
| 2b324f636b | |||
| 103b8372e4 | |||
| f7f1724bf1 | |||
| 5d70e11f17 | |||
| fb7ae90d46 | |||
| 957792a7a0 | |||
| 2e8ce6a2f0 | |||
| 9927a56b78 | |||
| db3da52f83 | |||
| 19a7c6978a | |||
| b5e9ba03a1 | |||
| cb5f8b91ad | |||
| f627f60f1a | |||
| e9fce5b8b8 | |||
| a7820a79db | |||
| b6a52dbcd4 | |||
| 7bbd10a50c | |||
| ee08b231db | |||
| 619e54dc0e |
+221
-120
@@ -7,6 +7,11 @@ on:
|
||||
tags:
|
||||
- "*"
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
patch:
|
||||
description: "Hotfix patch number, for example 1 or 2. Use 0 for a normal build."
|
||||
required: false
|
||||
default: "0"
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
@@ -15,103 +20,82 @@ env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
jobs:
|
||||
# Linux amd64
|
||||
build-linux-amd64:
|
||||
name: Build on Ubuntu 22.04 amd64
|
||||
runs-on: ubuntu-22.04
|
||||
container:
|
||||
image: crossdesk/ubuntu20.04:latest
|
||||
options: --user root
|
||||
steps:
|
||||
- name: Extract version number
|
||||
id: version
|
||||
run: |
|
||||
VERSION="${GITHUB_REF##*/}"
|
||||
VERSION_NUM="${VERSION#v}"
|
||||
echo "VERSION_NUM=${VERSION_NUM}" >> $GITHUB_ENV
|
||||
|
||||
- name: Set legal Debian version
|
||||
shell: bash
|
||||
id: set_deb_version
|
||||
run: |
|
||||
SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7)
|
||||
BUILD_DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
|
||||
if [[ ! "${VERSION_NUM}" =~ ^[0-9] ]]; then
|
||||
LEGAL_VERSION="v0.0.0-${VERSION_NUM}-${BUILD_DATE}-${SHORT_SHA}"
|
||||
else
|
||||
LEGAL_VERSION="v${VERSION_NUM}-${BUILD_DATE}-${SHORT_SHA}"
|
||||
fi
|
||||
echo "LEGAL_VERSION=${LEGAL_VERSION}" >> $GITHUB_ENV
|
||||
echo "BUILD_DATE=${BUILD_DATE}" >> $GITHUB_ENV
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Build CrossDesk
|
||||
env:
|
||||
CUDA_PATH: /usr/local/cuda
|
||||
XMAKE_GLOBALDIR: /data
|
||||
run: |
|
||||
ls -la $XMAKE_GLOBALDIR
|
||||
xmake f --CROSSDESK_VERSION=${LEGAL_VERSION} --USE_CUDA=true --root -y
|
||||
xmake b -vy --root crossdesk
|
||||
|
||||
- name: Decode and save certificate
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir -p certs
|
||||
echo "${{ secrets.CROSSDESK_CERT_BASE64 }}" | base64 --decode > certs/crossdesk.cn_root.crt
|
||||
|
||||
- name: Package
|
||||
run: |
|
||||
chmod +x ./scripts/linux/pkg_amd64.sh
|
||||
./scripts/linux/pkg_amd64.sh ${LEGAL_VERSION}
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: crossdesk-linux-amd64-${{ env.LEGAL_VERSION }}
|
||||
path: ${{ github.workspace }}/crossdesk-linux-amd64-${{ env.LEGAL_VERSION }}.deb
|
||||
|
||||
# Linux arm64
|
||||
build-linux-arm64:
|
||||
name: Build on Ubuntu 22.04 arm64
|
||||
runs-on: ubuntu-22.04-arm
|
||||
build-linux:
|
||||
name: Build Linux (${{ matrix.arch }})
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- arch: amd64
|
||||
runner: ubuntu-22.04
|
||||
image: crossdesk/ubuntu20.04:latest
|
||||
package_script: ./scripts/linux/pkg_amd64.sh
|
||||
|
||||
- arch: arm64
|
||||
runner: ubuntu-22.04-arm
|
||||
image: crossdesk/ubuntu20.04-arm64v8:latest
|
||||
package_script: ./scripts/linux/pkg_arm64.sh
|
||||
|
||||
container:
|
||||
image: ${{ matrix.image }}
|
||||
options: --user root
|
||||
|
||||
steps:
|
||||
- name: Extract version number
|
||||
id: version
|
||||
shell: bash
|
||||
run: |
|
||||
VERSION="${GITHUB_REF##*/}"
|
||||
VERSION_NUM="${VERSION#v}"
|
||||
echo "VERSION_NUM=${VERSION_NUM}" >> $GITHUB_ENV
|
||||
VERSION_REF="${GITHUB_REF##*/}"
|
||||
VERSION_BASE="${VERSION_REF#v}"
|
||||
PATCH_NUMBER="${{ github.event.inputs.patch }}"
|
||||
BUILD_DATE_OVERRIDE=""
|
||||
|
||||
if [[ ! "${PATCH_NUMBER}" =~ ^[0-9]+$ ]]; then
|
||||
PATCH_NUMBER=0
|
||||
fi
|
||||
|
||||
if [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]+)-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[3]}"
|
||||
BUILD_DATE_OVERRIDE="${BASH_REMATCH[4]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
BUILD_DATE_OVERRIDE="${BASH_REMATCH[3]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[4]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
BUILD_DATE_OVERRIDE="${BASH_REMATCH[3]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]+)$ && "${PATCH_NUMBER}" == "0" ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[3]}"
|
||||
fi
|
||||
|
||||
echo "VERSION_BASE=${VERSION_BASE}" >> $GITHUB_ENV
|
||||
echo "PATCH_NUMBER=${PATCH_NUMBER}" >> $GITHUB_ENV
|
||||
echo "BUILD_DATE_OVERRIDE=${BUILD_DATE_OVERRIDE}" >> $GITHUB_ENV
|
||||
|
||||
- name: Set legal Debian version
|
||||
shell: bash
|
||||
id: set_deb_version
|
||||
run: |
|
||||
SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7)
|
||||
BUILD_DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
|
||||
if [[ ! "${VERSION_NUM}" =~ ^[0-9] ]]; then
|
||||
LEGAL_VERSION="v0.0.0-${VERSION_NUM}-${BUILD_DATE}-${SHORT_SHA}"
|
||||
else
|
||||
LEGAL_VERSION="v${VERSION_NUM}-${BUILD_DATE}-${SHORT_SHA}"
|
||||
BUILD_DATE="${BUILD_DATE_OVERRIDE}"
|
||||
if [[ -z "${BUILD_DATE}" ]]; then
|
||||
BUILD_DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
|
||||
fi
|
||||
|
||||
if [[ ! "${VERSION_BASE}" =~ ^[0-9] ]]; then
|
||||
VERSION_BASE="0.0.0-${VERSION_BASE}"
|
||||
fi
|
||||
|
||||
if [[ "${PATCH_NUMBER}" != "0" ]]; then
|
||||
LEGAL_VERSION="v${VERSION_BASE}-${PATCH_NUMBER}-${BUILD_DATE}"
|
||||
else
|
||||
LEGAL_VERSION="v${VERSION_BASE}-${BUILD_DATE}"
|
||||
fi
|
||||
|
||||
echo "LEGAL_VERSION=${LEGAL_VERSION}" >> $GITHUB_ENV
|
||||
echo "BUILD_DATE=${BUILD_DATE}" >> $GITHUB_ENV
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
@@ -123,19 +107,13 @@ jobs:
|
||||
xmake f --CROSSDESK_VERSION=${LEGAL_VERSION} --USE_CUDA=true --root -y
|
||||
xmake b -vy --root crossdesk
|
||||
|
||||
- name: Decode and save certificate
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir -p certs
|
||||
echo "${{ secrets.CROSSDESK_CERT_BASE64 }}" | base64 --decode > certs/crossdesk.cn_root.crt
|
||||
|
||||
- name: Package
|
||||
run: |
|
||||
chmod +x ${{ matrix.package_script }}
|
||||
${{ matrix.package_script }} ${LEGAL_VERSION}
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: crossdesk-linux-${{ matrix.arch }}-${{ env.LEGAL_VERSION }}
|
||||
path: ${{ github.workspace }}/crossdesk-linux-${{ matrix.arch }}-${{ env.LEGAL_VERSION }}.deb
|
||||
@@ -161,20 +139,49 @@ jobs:
|
||||
steps:
|
||||
- name: Extract version number
|
||||
id: version
|
||||
shell: bash
|
||||
run: |
|
||||
VERSION="${GITHUB_REF##*/}"
|
||||
SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7)
|
||||
VERSION_REF="${GITHUB_REF##*/}"
|
||||
VERSION_BASE="${VERSION_REF#v}"
|
||||
BUILD_DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
|
||||
VERSION_NUM="v${VERSION#v}-${BUILD_DATE}-${SHORT_SHA}"
|
||||
PATCH_NUMBER="${{ github.event.inputs.patch }}"
|
||||
|
||||
if [[ ! "${PATCH_NUMBER}" =~ ^[0-9]+$ ]]; then
|
||||
PATCH_NUMBER=0
|
||||
fi
|
||||
|
||||
if [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]+)-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[3]}"
|
||||
BUILD_DATE="${BASH_REMATCH[4]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
BUILD_DATE="${BASH_REMATCH[3]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[4]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
BUILD_DATE="${BASH_REMATCH[3]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]+)$ && "${PATCH_NUMBER}" == "0" ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[3]}"
|
||||
fi
|
||||
|
||||
if [[ "${PATCH_NUMBER}" != "0" ]]; then
|
||||
VERSION_NUM="v${VERSION_BASE}-${PATCH_NUMBER}-${BUILD_DATE}"
|
||||
else
|
||||
VERSION_NUM="v${VERSION_BASE}-${BUILD_DATE}"
|
||||
fi
|
||||
|
||||
echo "VERSION_NUM=${VERSION_NUM}" >> $GITHUB_ENV
|
||||
echo "VERSION_NUM=${VERSION_NUM}"
|
||||
echo "BUILD_DATE=${BUILD_DATE}" >> $GITHUB_ENV
|
||||
echo "PATCH_NUMBER=${PATCH_NUMBER}" >> $GITHUB_ENV
|
||||
|
||||
- name: Cache xmake dependencies
|
||||
uses: actions/cache@v4
|
||||
uses: actions/cache@v5
|
||||
with:
|
||||
path: ~/.xmake/packages
|
||||
key: ${{ runner.os }}-xmake-deps-${{ matrix.cache-key }}-${{ github.sha }}
|
||||
key: "${{ runner.os }}-xmake-deps-${{ matrix.cache-key }}-${{ github.run_id }}"
|
||||
restore-keys: |
|
||||
${{ runner.os }}-xmake-deps-${{ matrix.cache-key }}-
|
||||
|
||||
@@ -182,7 +189,7 @@ jobs:
|
||||
run: brew install xmake
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Initialize submodules
|
||||
run: git submodule update --init --recursive
|
||||
@@ -192,19 +199,13 @@ jobs:
|
||||
xmake f --CROSSDESK_VERSION=${VERSION_NUM} --USE_CUDA=true -y
|
||||
xmake b -vy crossdesk
|
||||
|
||||
- name: Decode and save certificate
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir -p certs
|
||||
echo "${{ secrets.CROSSDESK_CERT_BASE64 }}" | base64 --decode > certs/crossdesk.cn_root.crt
|
||||
|
||||
- name: Package CrossDesk app
|
||||
run: |
|
||||
chmod +x ${{ matrix.package_script }}
|
||||
${{ matrix.package_script }} ${VERSION_NUM}
|
||||
|
||||
- name: Upload build artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: crossdesk-macos-${{ matrix.arch }}-${{ env.VERSION_NUM }}
|
||||
path: crossdesk-macos-${{ matrix.arch }}-${{ env.VERSION_NUM }}.pkg
|
||||
@@ -228,16 +229,44 @@ jobs:
|
||||
$version = $ref -replace '^refs/(tags|heads)/', ''
|
||||
$version = $version -replace '^v', ''
|
||||
$version = $version -replace '/', '-'
|
||||
$SHORT_SHA = $env:GITHUB_SHA.Substring(0,7)
|
||||
$BUILD_DATE = ([System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), "China Standard Time")).ToString("yyyyMMdd")
|
||||
echo "VERSION_NUM=v$version-$BUILD_DATE-$SHORT_SHA" >> $env:GITHUB_ENV
|
||||
$PATCH_NUMBER = "${{ github.event.inputs.patch }}"
|
||||
|
||||
if ($PATCH_NUMBER -notmatch '^[0-9]+$') {
|
||||
$PATCH_NUMBER = "0"
|
||||
}
|
||||
|
||||
if ($version -match '^([0-9]+(\.[0-9]+){1,3})-([0-9]+)-([0-9]{8})$') {
|
||||
$version = $Matches[1]
|
||||
$PATCH_NUMBER = $Matches[3]
|
||||
$BUILD_DATE = $Matches[4]
|
||||
} elseif ($version -match '^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$') {
|
||||
$version = $Matches[1]
|
||||
$BUILD_DATE = $Matches[3]
|
||||
$PATCH_NUMBER = $Matches[4]
|
||||
} elseif ($version -match '^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})$') {
|
||||
$version = $Matches[1]
|
||||
$BUILD_DATE = $Matches[3]
|
||||
} elseif ($version -match '^([0-9]+(\.[0-9]+){1,3})-([0-9]+)$' -and $PATCH_NUMBER -eq "0") {
|
||||
$version = $Matches[1]
|
||||
$PATCH_NUMBER = $Matches[3]
|
||||
}
|
||||
|
||||
if ($PATCH_NUMBER -ne "0") {
|
||||
$VERSION_NUM = "v$version-$PATCH_NUMBER-$BUILD_DATE"
|
||||
} else {
|
||||
$VERSION_NUM = "v$version-$BUILD_DATE"
|
||||
}
|
||||
|
||||
echo "VERSION_NUM=$VERSION_NUM" >> $env:GITHUB_ENV
|
||||
echo "BUILD_DATE=$BUILD_DATE" >> $env:GITHUB_ENV
|
||||
echo "PATCH_NUMBER=$PATCH_NUMBER" >> $env:GITHUB_ENV
|
||||
|
||||
- name: Cache xmake dependencies
|
||||
uses: actions/cache@v4
|
||||
uses: actions/cache@v5
|
||||
with:
|
||||
path: D:\xmake_global\.xmake\packages
|
||||
key: ${{ runner.os }}-xmake-deps-intel-${{ github.sha }}
|
||||
key: "${{ runner.os }}-xmake-deps-intel-${{ github.run_id }}"
|
||||
restore-keys: |
|
||||
${{ runner.os }}-xmake-deps-intel-
|
||||
|
||||
@@ -286,7 +315,7 @@ jobs:
|
||||
Copy-Item $source $target -Force
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Initialize submodules
|
||||
run: git submodule update --init --recursive
|
||||
@@ -301,55 +330,120 @@ jobs:
|
||||
xmake f --CROSSDESK_VERSION=${{ env.VERSION_NUM }} --USE_CUDA=true -y
|
||||
xmake b -vy crossdesk
|
||||
|
||||
- name: Decode and save certificate
|
||||
shell: powershell
|
||||
run: |
|
||||
New-Item -ItemType Directory -Force -Path certs
|
||||
[System.IO.File]::WriteAllBytes('certs\crossdesk.cn_root.crt', [Convert]::FromBase64String('${{ secrets.CROSSDESK_CERT_BASE64 }}'))
|
||||
|
||||
- name: Package
|
||||
shell: pwsh
|
||||
run: |
|
||||
cd "${{ github.workspace }}\scripts\windows"
|
||||
makensis /DVERSION=$env:VERSION_NUM nsis_script.nsi
|
||||
& "${{ github.workspace }}\scripts\windows\pkg_x64.ps1" $env:VERSION_NUM
|
||||
|
||||
- name: Build Portable CrossDesk
|
||||
run: |
|
||||
xmake f --CROSSDESK_VERSION=${{ env.VERSION_NUM }} --USE_CUDA=true --CROSSDESK_PORTABLE=true -y
|
||||
xmake b -vy crossdesk
|
||||
|
||||
- name: Package Portable
|
||||
shell: pwsh
|
||||
run: |
|
||||
$buildDir = "${{ github.workspace }}\build\windows\x64\release"
|
||||
$portableDir = "${{ github.workspace }}\portable"
|
||||
New-Item -ItemType Directory -Force -Path $portableDir
|
||||
|
||||
$portableFiles = @(
|
||||
@("crossdesk.exe", "CrossDesk.exe"),
|
||||
@("crossdesk_service.exe", "crossdesk_service.exe"),
|
||||
@("crossdesk_session_helper.exe", "crossdesk_session_helper.exe")
|
||||
)
|
||||
|
||||
foreach ($file in $portableFiles) {
|
||||
$source = Join-Path $buildDir $file[0]
|
||||
$destination = Join-Path $portableDir $file[1]
|
||||
if (!(Test-Path $source)) {
|
||||
throw "Missing portable package file: $source"
|
||||
}
|
||||
Copy-Item $source $destination -Force
|
||||
}
|
||||
|
||||
Copy-Item (Join-Path $buildDir "*.dll") $portableDir -Force
|
||||
|
||||
foreach ($file in $portableFiles) {
|
||||
$packagedFile = Join-Path $portableDir $file[1]
|
||||
if (!(Test-Path $packagedFile)) {
|
||||
throw "Portable package is missing: $packagedFile"
|
||||
}
|
||||
}
|
||||
|
||||
Compress-Archive -Path "$portableDir\*" -DestinationPath "${{ github.workspace }}\crossdesk-win-x64-portable-${{ env.VERSION_NUM }}.zip"
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: crossdesk-win-x64-${{ env.VERSION_NUM }}
|
||||
path: ${{ github.workspace }}/scripts/windows/crossdesk-win-x64-${{ env.VERSION_NUM }}.exe
|
||||
|
||||
- name: Upload portable artifact
|
||||
uses: actions/upload-artifact@v6
|
||||
with:
|
||||
name: crossdesk-win-x64-portable-${{ env.VERSION_NUM }}
|
||||
path: ${{ github.workspace }}/crossdesk-win-x64-portable-${{ env.VERSION_NUM }}.zip
|
||||
|
||||
release:
|
||||
name: Publish Release
|
||||
if: startsWith(github.ref, 'refs/tags/v')
|
||||
needs:
|
||||
[build-linux-amd64, build-linux-arm64, build-macos, build-windows-x64]
|
||||
[build-linux, build-macos, build-windows-x64]
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Download all artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@v8
|
||||
with:
|
||||
path: artifacts
|
||||
|
||||
- name: Extract version number
|
||||
id: version
|
||||
shell: bash
|
||||
run: |
|
||||
VERSION="${GITHUB_REF##*/}"
|
||||
SHORT_SHA=$(echo "${GITHUB_SHA}" | cut -c1-7)
|
||||
VERSION_REF="${GITHUB_REF##*/}"
|
||||
VERSION_BASE="${VERSION_REF#v}"
|
||||
BUILD_DATE=$(TZ=Asia/Shanghai date +%Y%m%d)
|
||||
BUILD_DATE_ISO=$(TZ=Asia/Shanghai date +%Y-%m-%d)
|
||||
VERSION_NUM="${VERSION#v}-${BUILD_DATE}-${SHORT_SHA}"
|
||||
PATCH_NUMBER="${{ github.event.inputs.patch }}"
|
||||
|
||||
if [[ ! "${PATCH_NUMBER}" =~ ^[0-9]+$ ]]; then
|
||||
PATCH_NUMBER=0
|
||||
fi
|
||||
|
||||
if [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]+)-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[3]}"
|
||||
BUILD_DATE="${BASH_REMATCH[4]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
BUILD_DATE="${BASH_REMATCH[3]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[4]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
BUILD_DATE="${BASH_REMATCH[3]}"
|
||||
elif [[ "${VERSION_BASE}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]+)$ && "${PATCH_NUMBER}" == "0" ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[3]}"
|
||||
fi
|
||||
|
||||
BUILD_DATE_ISO="${BUILD_DATE:0:4}-${BUILD_DATE:4:2}-${BUILD_DATE:6:2}"
|
||||
if [[ "${PATCH_NUMBER}" != "0" ]]; then
|
||||
VERSION_NUM="${VERSION_BASE}-${PATCH_NUMBER}-${BUILD_DATE}"
|
||||
else
|
||||
VERSION_NUM="${VERSION_BASE}-${BUILD_DATE}"
|
||||
fi
|
||||
|
||||
VERSION_WITH_V="v${VERSION_NUM}"
|
||||
VERSION_ONLY="${VERSION#v}"
|
||||
echo "VERSION_NUM=${VERSION_NUM}" >> $GITHUB_OUTPUT
|
||||
echo "VERSION_WITH_V=${VERSION_WITH_V}" >> $GITHUB_OUTPUT
|
||||
echo "VERSION_ONLY=${VERSION_ONLY}" >> $GITHUB_OUTPUT
|
||||
echo "VERSION_BASE=${VERSION_BASE}" >> $GITHUB_OUTPUT
|
||||
echo "BUILD_DATE=${BUILD_DATE}" >> $GITHUB_OUTPUT
|
||||
echo "BUILD_DATE_ISO=${BUILD_DATE_ISO}" >> $GITHUB_OUTPUT
|
||||
echo "PATCH_NUMBER=${PATCH_NUMBER}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Rename artifacts
|
||||
run: |
|
||||
@@ -359,6 +453,7 @@ jobs:
|
||||
cp artifacts/crossdesk-linux-amd64-${{ steps.version.outputs.VERSION_WITH_V }}/* release/crossdesk-linux-amd64-${{ steps.version.outputs.VERSION_WITH_V }}.deb
|
||||
cp artifacts/crossdesk-linux-arm64-${{ steps.version.outputs.VERSION_WITH_V }}/* release/crossdesk-linux-arm64-${{ steps.version.outputs.VERSION_WITH_V }}.deb
|
||||
cp artifacts/crossdesk-win-x64-${{ steps.version.outputs.VERSION_WITH_V }}/* release/crossdesk-win-x64-${{ steps.version.outputs.VERSION_WITH_V }}.exe
|
||||
cp artifacts/crossdesk-win-x64-portable-${{ steps.version.outputs.VERSION_WITH_V }}/* release/crossdesk-win-x64-portable-${{ steps.version.outputs.VERSION_WITH_V }}.zip
|
||||
|
||||
- name: List release files
|
||||
run: ls -lh release/
|
||||
@@ -406,8 +501,10 @@ jobs:
|
||||
run: |
|
||||
cat > version.json << EOF
|
||||
{
|
||||
"version": "${{ steps.version.outputs.VERSION_ONLY }}",
|
||||
"latest_version": "${{ steps.version.outputs.VERSION_NUM }}",
|
||||
"version": "${{ steps.version.outputs.VERSION_NUM }}",
|
||||
"releaseDate": "${{ steps.version.outputs.BUILD_DATE_ISO }}",
|
||||
"patch": ${{ steps.version.outputs.PATCH_NUMBER }},
|
||||
"releaseName": "",
|
||||
"releaseNotes": "",
|
||||
"tagName": "${{ steps.version.outputs.VERSION_WITH_V }}",
|
||||
@@ -416,6 +513,10 @@ jobs:
|
||||
"url": "https://downloads.crossdesk.cn/crossdesk-win-x64-${{ steps.version.outputs.VERSION_WITH_V }}.exe",
|
||||
"filename": "crossdesk-win-x64-${{ steps.version.outputs.VERSION_WITH_V }}.exe"
|
||||
},
|
||||
"windows-x64-portable": {
|
||||
"url": "https://downloads.crossdesk.cn/crossdesk-win-x64-portable-${{ steps.version.outputs.VERSION_WITH_V }}.zip",
|
||||
"filename": "crossdesk-win-x64-portable-${{ steps.version.outputs.VERSION_WITH_V }}.zip"
|
||||
},
|
||||
"macos-x64": {
|
||||
"url": "https://downloads.crossdesk.cn/crossdesk-macos-x64-${{ steps.version.outputs.VERSION_WITH_V }}.pkg",
|
||||
"filename": "crossdesk-macos-x64-${{ steps.version.outputs.VERSION_WITH_V }}.pkg"
|
||||
|
||||
@@ -11,7 +11,7 @@ jobs:
|
||||
update-version-json:
|
||||
name: Update version.json with release information
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
@@ -20,21 +20,39 @@ jobs:
|
||||
|
||||
- name: Extract version from tag
|
||||
id: version
|
||||
shell: bash
|
||||
run: |
|
||||
TAG_NAME="${{ github.event.release.tag_name }}"
|
||||
VERSION_ONLY="${TAG_NAME#v}"
|
||||
echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_OUTPUT
|
||||
echo "VERSION_ONLY=${VERSION_ONLY}" >> $GITHUB_OUTPUT
|
||||
|
||||
# Extract date from tag if available (format: v1.2.3-20251113-abc)
|
||||
if [[ "${TAG_NAME}" =~ -([0-9]{8})- ]]; then
|
||||
DATE_STR="${BASH_REMATCH[1]}"
|
||||
TAG_VERSION="${TAG_NAME#v}"
|
||||
VERSION_FULL="${TAG_VERSION}"
|
||||
VERSION_BASE="${TAG_VERSION}"
|
||||
PATCH_NUMBER=0
|
||||
|
||||
# Extract date and patch from tags such as v1.2.3-1-20251113.
|
||||
if [[ "${TAG_VERSION}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]+)-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[3]}"
|
||||
DATE_STR="${BASH_REMATCH[4]}"
|
||||
BUILD_DATE_ISO="${DATE_STR:0:4}-${DATE_STR:4:2}-${DATE_STR:6:2}"
|
||||
elif [[ "${TAG_VERSION}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
DATE_STR="${BASH_REMATCH[3]}"
|
||||
PATCH_NUMBER="${BASH_REMATCH[4]}"
|
||||
BUILD_DATE_ISO="${DATE_STR:0:4}-${DATE_STR:4:2}-${DATE_STR:6:2}"
|
||||
elif [[ "${TAG_VERSION}" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})$ ]]; then
|
||||
VERSION_BASE="${BASH_REMATCH[1]}"
|
||||
DATE_STR="${BASH_REMATCH[3]}"
|
||||
BUILD_DATE_ISO="${DATE_STR:0:4}-${DATE_STR:4:2}-${DATE_STR:6:2}"
|
||||
else
|
||||
# Use release published date
|
||||
BUILD_DATE_ISO=$(echo "${{ github.event.release.published_at }}" | cut -d'T' -f1)
|
||||
fi
|
||||
|
||||
echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_OUTPUT
|
||||
echo "VERSION_FULL=${VERSION_FULL}" >> $GITHUB_OUTPUT
|
||||
echo "VERSION_BASE=${VERSION_BASE}" >> $GITHUB_OUTPUT
|
||||
echo "BUILD_DATE_ISO=${BUILD_DATE_ISO}" >> $GITHUB_OUTPUT
|
||||
echo "PATCH_NUMBER=${PATCH_NUMBER}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Install jq
|
||||
run: sudo apt-get update && sudo apt-get install -y jq
|
||||
@@ -45,7 +63,7 @@ jobs:
|
||||
# Use jq to properly escape JSON
|
||||
RELEASE_BODY="${{ github.event.release.body }}"
|
||||
RELEASE_NAME="${{ github.event.release.name }}"
|
||||
|
||||
|
||||
# Handle empty values
|
||||
if [ -z "$RELEASE_BODY" ]; then
|
||||
RELEASE_BODY=""
|
||||
@@ -53,15 +71,15 @@ jobs:
|
||||
if [ -z "$RELEASE_NAME" ]; then
|
||||
RELEASE_NAME=""
|
||||
fi
|
||||
|
||||
|
||||
# Save to temporary files for proper handling
|
||||
echo -n "$RELEASE_BODY" > /tmp/release_body.txt
|
||||
echo -n "$RELEASE_NAME" > /tmp/release_name.txt
|
||||
|
||||
|
||||
# Use jq to escape JSON strings
|
||||
RELEASE_BODY_JSON=$(jq -Rs . < /tmp/release_body.txt)
|
||||
RELEASE_NAME_JSON=$(jq -Rs . < /tmp/release_name.txt)
|
||||
|
||||
|
||||
echo "RELEASE_BODY=${RELEASE_BODY_JSON}" >> $GITHUB_OUTPUT
|
||||
echo "RELEASE_NAME=${RELEASE_NAME_JSON}" >> $GITHUB_OUTPUT
|
||||
|
||||
@@ -85,7 +103,7 @@ jobs:
|
||||
else
|
||||
DOWNLOADS_JSON=""
|
||||
fi
|
||||
|
||||
|
||||
# If downloads is empty, use default structure
|
||||
if [ -z "$DOWNLOADS_JSON" ]; then
|
||||
DOWNLOADS_JSON=$(cat << DOWNLOADS_EOF
|
||||
@@ -94,6 +112,10 @@ jobs:
|
||||
"url": "https://downloads.crossdesk.cn/crossdesk-win-x64-${{ steps.version.outputs.TAG_NAME }}.exe",
|
||||
"filename": "crossdesk-win-x64-${{ steps.version.outputs.TAG_NAME }}.exe"
|
||||
},
|
||||
"windows-x64-portable": {
|
||||
"url": "https://downloads.crossdesk.cn/crossdesk-win-x64-portable-${{ steps.version.outputs.TAG_NAME }}.zip",
|
||||
"filename": "crossdesk-win-x64-portable-${{ steps.version.outputs.TAG_NAME }}.zip"
|
||||
},
|
||||
"macos-x64": {
|
||||
"url": "https://downloads.crossdesk.cn/crossdesk-macos-x64-${{ steps.version.outputs.TAG_NAME }}.pkg",
|
||||
"filename": "crossdesk-macos-x64-${{ steps.version.outputs.TAG_NAME }}.pkg"
|
||||
@@ -114,19 +136,21 @@ jobs:
|
||||
DOWNLOADS_EOF
|
||||
)
|
||||
fi
|
||||
|
||||
|
||||
# Generate version.json using cat and heredoc
|
||||
cat > version.json << EOF
|
||||
{
|
||||
"version": "${{ steps.version.outputs.VERSION_ONLY }}",
|
||||
"latest_version": "${{ steps.version.outputs.VERSION_FULL }}",
|
||||
"version": "${{ steps.version.outputs.VERSION_FULL }}",
|
||||
"releaseDate": "${{ steps.version.outputs.BUILD_DATE_ISO }}",
|
||||
"patch": ${{ steps.version.outputs.PATCH_NUMBER }},
|
||||
"releaseName": ${{ steps.release_info.outputs.RELEASE_NAME }},
|
||||
"releaseNotes": ${{ steps.release_info.outputs.RELEASE_BODY }},
|
||||
"tagName": "${{ steps.version.outputs.TAG_NAME }}",
|
||||
"downloads": ${DOWNLOADS_JSON}
|
||||
}
|
||||
EOF
|
||||
|
||||
|
||||
cat version.json
|
||||
|
||||
- name: Upload version.json to server
|
||||
@@ -137,4 +161,4 @@ jobs:
|
||||
remote_path: /var/www/html/version/
|
||||
remote_host: ${{ secrets.SERVER_HOST }}
|
||||
remote_user: ${{ secrets.SERVER_USER }}
|
||||
remote_key: ${{ secrets.SERVER_KEY }}
|
||||
remote_key: ${{ secrets.SERVER_KEY }}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# Xmake cache
|
||||
.xmake/
|
||||
build/
|
||||
certs/
|
||||
|
||||
# MacOS Cache
|
||||
.DS_Store
|
||||
|
||||
@@ -53,6 +53,29 @@ CrossDesk 是 [MiniRTC](https://github.com/kunkundi/minirtc.git) 实时音视频
|
||||
|
||||
<img width="645" height="300" alt="_cgi-bin_mmwebwx-bin_webwxgetmsgimg__ MsgID=932911462648581698 skey=@crypt_1f5153b1_b550ca7462b5009ce03c991cca2a92a7 mmweb_appid=wx_webfilehelper" src="https://github.com/user-attachments/assets/a5109e6f-752c-4654-9f4e-7e161bddf43e" />
|
||||
|
||||
### Windows 服务(CrossDesk Service)
|
||||
CrossDesk 在 Windows 平台提供本地辅助服务 **CrossDesk Service**,服务名为 `CrossDeskService`。该服务用于锁屏、登录界面和安全桌面等受保护场景下的远程控制增强能力,包括:
|
||||
- 上报远端当前是否处于锁屏、登录、凭据输入或安全桌面状态;
|
||||
- 支持从控制端发送 `Ctrl+Alt+Del`(SAS);
|
||||
- 在锁屏、登录和安全桌面阶段转发键盘、鼠标输入。
|
||||
|
||||
Windows 安装包会自动打包 `crossdesk_service.exe` 和 `crossdesk_session_helper.exe`,并在安装时注册为按需启动的 Windows 服务。CrossDesk 客户端启动时会尝试启动已安装的服务;当本机没有 CrossDesk 客户端进程运行时,服务会自动退出。卸载客户端时会同步停止并移除该服务。
|
||||
|
||||
如果是手动编译或手动部署 Windows 版本,请确保 `CrossDesk.exe`、`crossdesk_service.exe` 和 `crossdesk_session_helper.exe` 位于同一目录。安装或卸载服务需要使用管理员权限打开 PowerShell:
|
||||
```
|
||||
# 安装
|
||||
.\CrossDesk.exe --service-install
|
||||
# 启动
|
||||
.\CrossDesk.exe --service-start
|
||||
# 查看状态
|
||||
.\CrossDesk.exe --service-status
|
||||
.\CrossDesk.exe --service-stop
|
||||
# 卸载
|
||||
.\CrossDesk.exe --service-uninstall
|
||||
```
|
||||
|
||||
如果远端 Windows 服务未安装、未启动或暂时不可用,基础远程桌面连接仍可使用,但锁屏、登录界面和安全桌面阶段的控制能力会受限,客户端会提示“远端Windows服务不可用”。
|
||||
|
||||
## 如何编译
|
||||
|
||||
依赖:
|
||||
@@ -181,7 +204,7 @@ sudo docker run -d \
|
||||
-e MAX_PORT=xxxxx \
|
||||
-v /var/lib/crossdesk:/var/lib/crossdesk \
|
||||
-v /var/log/crossdesk:/var/log/crossdesk \
|
||||
crossdesk/crossdesk-server:v1.1.3
|
||||
crossdesk/crossdesk-server:v1.1.6
|
||||
```
|
||||
|
||||
上述命令中,用户需注意的参数如下:
|
||||
@@ -208,13 +231,13 @@ sudo docker run -d \
|
||||
-e MAX_PORT=60000 \
|
||||
-v /var/lib/crossdesk:/var/lib/crossdesk \
|
||||
-v /var/log/crossdesk:/var/log/crossdesk \
|
||||
crossdesk/crossdesk-server:v1.1.3
|
||||
crossdesk/crossdesk-server:v1.1.6
|
||||
```
|
||||
|
||||
**注意**:
|
||||
- **服务器需开放端口:COTURN_PORT/udp,COTURN_PORT/tcp,MIN_PORT-MAX_PORT/udp,CROSSDESK_SERVER_PORT/tcp。**
|
||||
- 如果不挂载 volume,容器删除后数据会丢失
|
||||
- 证书文件会在首次启动时自动生成并持久化到宿主机的 `/var/lib/crossdesk/certs` 路径下
|
||||
- 证书文件会在首次启动时自动生成并持久化到宿主机的 `/var/lib/crossdesk/certs` 路径下。由于默认使用的是自签证书,无法保障安全性,建议在云服务商申请正式证书放到该目录下并重启服务。
|
||||
- 数据库文件会自动创建并持久化到宿主机的 `/var/lib/crossdesk/db/crossdesk-server.db` 路径下
|
||||
- 日志文件会自动创建并持久化到宿主机的 `/var/log/crossdesk/` 路径下
|
||||
|
||||
@@ -232,19 +255,38 @@ sudo chown -R $(id -u):$(id -g) /var/lib/crossdesk /var/log/crossdesk
|
||||
|
||||
### 客户端
|
||||
1. 点击右上角设置进入设置页面。<br><br>
|
||||
<img width="600" height="210" alt="image" src="https://github.com/user-attachments/assets/6431131d-b32a-4726-8783-6788f47baa3b" /><br><br>
|
||||
<img width="600" height="210" alt="image" src="https://github.com/user-attachments/assets/6431131d-b32a-4726-8783-6788f47baa3b" /><br>
|
||||
|
||||
2. 点击点击`自托管服务器配置`按钮。<br><br>
|
||||
<img width="600" height="140" alt="image" src="https://github.com/user-attachments/assets/24c761a3-1985-4d7e-84be-787383c2afb8" /><br><br>
|
||||
2. 点击`自托管服务器配置`按钮。<br><br>
|
||||
<img width="600" height="160" alt="image" src="https://github.com/user-attachments/assets/24c761a3-1985-4d7e-84be-787383c2afb8" /><br>
|
||||
|
||||
3. 输入`服务器地址`(**EXTERNAL_IP**)、`信令服务端口`(**CROSSDESK_SERVER_PORT**)、`中继服务端口`(**COTURN_PORT**)。<br><br>
|
||||
<img width="600" height="200" alt="image" src="https://github.com/user-attachments/assets/9a32ddd5-37f8-4bee-9a51-eae295820f9a" /><br><br>
|
||||
3. 输入`服务器地址`(**EXTERNAL_IP**)、`信令服务端口`(**CROSSDESK_SERVER_PORT**)、`中继服务端口`(**COTURN_PORT**),点击确认按钮。
|
||||
|
||||
4. 勾选`自托管服务器配置`选项,点击确认按钮保存设置。如果服务端使用的是正式证书,则到此步骤为止,客户端即可显示已连接服务器。
|
||||
|
||||
4. 后续如果自托管服务器被重置或因其他原因导致证书更换,可以点击`重置证书指纹`按钮重置客户端保存的证书指纹。<br><br>
|
||||
<img width="600" height="200" alt="image" src="https://github.com/user-attachments/assets/d9e423ab-0c2b-4fab-b132-4dc27462d704" /><br><br>
|
||||
5. 如果使用默认证书(正式证书忽略此步骤),则需要将服务端`/var/lib/crossdesk/certs/`目录下的`api.crossdesk.cn_root.crt`自签根证书下载到运行客户端的机器,并执行下述命令安装证书:
|
||||
|
||||
Windows 平台使用**管理员权限**打开 PowerShell 执行
|
||||
```
|
||||
certutil -addstore "Root" "C:\path\to\api.crossdesk.cn_root.crt"
|
||||
```
|
||||
Linux
|
||||
```
|
||||
sudo cp /path/to/api.crossdesk.cn_root.crt /usr/local/share/ca-certificates/api.crossdesk.cn_root.crt
|
||||
sudo update-ca-certificates
|
||||
```
|
||||
macOS
|
||||
```
|
||||
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain path/to/api.crossdesk.cn_root.crt
|
||||
```
|
||||
|
||||
### Web 客户端
|
||||
详情见项目 [CrossDesk Web Client](https://github.com/kunkundi/crossdesk-web-client)。
|
||||
|
||||
# 常见问题
|
||||
见 [常见问题](https://github.com/kunkundi/crossdesk/blob/self-hosted-server/docs/FAQ.md) 。
|
||||
|
||||
# 致谢
|
||||
- 感谢 [HelloGitHub](https://hellogithub.com/) 的推荐与关注。
|
||||
- 感谢 [阮一峰的科技爱好者周刊](https://github.com/ruanyf/weekly) 的收录与推荐。
|
||||
- 感谢 [LinuxDo](https://linux.do) 社区的关注、交流与支持,为 CrossDesk 项目的完善提供了帮助。
|
||||
|
||||
+54
-9
@@ -56,6 +56,31 @@ Enter the **Remote Device ID** and **Password**, then click Connect to access th
|
||||
|
||||
<img width="645" height="300" alt="_cgi-bin_mmwebwx-bin_webwxgetmsgimg__ MsgID=932911462648581698 skey=@crypt_1f5153b1_b550ca7462b5009ce03c991cca2a92a7 mmweb_appid=wx_webfilehelper" src="https://github.com/user-attachments/assets/a5109e6f-752c-4654-9f4e-7e161bddf43e" />
|
||||
|
||||
### Windows Service (CrossDesk Service)
|
||||
CrossDesk provides a local helper service on Windows named **CrossDesk Service**. Its service name is `CrossDeskService`. The service improves remote control in protected Windows states such as the lock screen, sign-in UI, credential UI, and secure desktop. It provides:
|
||||
- Remote status reporting for lock screen, sign-in, credential, and secure desktop states.
|
||||
- Remote `Ctrl+Alt+Del` (SAS) delivery.
|
||||
- Keyboard and mouse input forwarding while the remote Windows device is on the lock screen, sign-in UI, or secure desktop.
|
||||
|
||||
The Windows installer bundles `crossdesk_service.exe` and `crossdesk_session_helper.exe`, then registers the service as an on-demand Windows service during installation. When the CrossDesk client starts, it tries to start the installed service automatically. When no CrossDesk client process is running on the machine, the service exits automatically. Uninstalling the client also stops and removes the service.
|
||||
|
||||
For manual Windows builds or deployments, make sure `CrossDesk.exe`, `crossdesk_service.exe`, and `crossdesk_session_helper.exe` are placed in the same directory. Open PowerShell with administrator privileges to install or uninstall the service:
|
||||
```
|
||||
# install
|
||||
.\CrossDesk.exe --service-install
|
||||
# start
|
||||
.\CrossDesk.exe --service-start
|
||||
# check status
|
||||
.\CrossDesk.exe --service-status
|
||||
.\CrossDesk.exe --service-ping
|
||||
# stop
|
||||
.\CrossDesk.exe --service-stop
|
||||
# uninstall
|
||||
.\CrossDesk.exe --service-uninstall
|
||||
```
|
||||
|
||||
If the remote Windows service is not installed, not running, or temporarily unavailable, the basic remote desktop connection still works, but remote control on the lock screen, sign-in UI, and secure desktop is limited. The client will show “Remote Windows service unavailable”.
|
||||
|
||||
## How to build
|
||||
|
||||
Requirements:
|
||||
@@ -189,7 +214,7 @@ sudo docker run -d \
|
||||
-e MAX_PORT=xxxxx \
|
||||
-v /var/lib/crossdesk:/var/lib/crossdesk \
|
||||
-v /var/log/crossdesk:/var/log/crossdesk \
|
||||
crossdesk/crossdesk-server:v1.1.3
|
||||
crossdesk/crossdesk-server:v1.1.6
|
||||
```
|
||||
|
||||
The parameters you need to pay attention to are as follows:
|
||||
@@ -216,13 +241,13 @@ sudo docker run -d \
|
||||
-e MAX_PORT=60000 \
|
||||
-v /var/lib/crossdesk:/var/lib/crossdesk \
|
||||
-v /var/log/crossdesk:/var/log/crossdesk \
|
||||
crossdesk/crossdesk-server:v1.1.3
|
||||
crossdesk/crossdesk-server:v1.1.6
|
||||
```
|
||||
|
||||
**Notes**
|
||||
- **The server must open the following ports: COTURN_PORT/udp, COTURN_PORT/tcp, MIN_PORT–MAX_PORT/udp, and CROSSDESK_SERVER_PORT/tcp.**
|
||||
- If you don’t mount volumes, all data will be lost when the container is removed.
|
||||
- Certificate files will be automatically generated on first startup and persisted to the host at `/var/lib/crossdesk/certs`.
|
||||
- Certificate files will be automatically generated on first startup and persisted to the host at `/var/lib/crossdesk/certs`.As the default certificates are self-signed and cannot guarantee security, it is strongly recommended to apply for a trusted certificate from a cloud provider, deploy it to this directory, and restart the service.
|
||||
- The database file will be automatically created and stored at `/var/lib/crossdesk/db/crossdesk-server.db`.
|
||||
- Log files will be created and stored at `/var/log/crossdesk/`.
|
||||
|
||||
@@ -243,19 +268,39 @@ Place **crossdesk.cn.key** and **crossdesk.cn_bundle.crt** into the **/path/to/y
|
||||
|
||||
### Client Side
|
||||
1. Click the settings icon in the top-right corner to enter the settings page.<br><br>
|
||||
<img width="600" height="210" alt="image" src="https://github.com/user-attachments/assets/6431131d-b32a-4726-8783-6788f47baa3b" /><br><br>
|
||||
<img width="600" height="210" alt="image" src="https://github.com/user-attachments/assets/6431131d-b32a-4726-8783-6788f47baa3b" /><br>
|
||||
|
||||
2. Click `Self-Hosted Server Configuration` button.<br><br>
|
||||
<img width="600" height="160" alt="image" src="https://github.com/user-attachments/assets/24c761a3-1985-4d7e-84be-787383c2afb8" /><br><br>
|
||||
<img width="600" height="160" alt="image" src="https://github.com/user-attachments/assets/24c761a3-1985-4d7e-84be-787383c2afb8" /><br>
|
||||
|
||||
3. Enter the `Server Address` (**EXTERNAL_IP**), `Signaling Service Port` (**CROSSDESK_SERVER_PORT**), and `Relay Service Port` (**COTURN_PORT**).<br><br>
|
||||
<img width="600" height="200" alt="image" src="https://github.com/user-attachments/assets/9a32ddd5-37f8-4bee-9a51-eae295820f9a" /><br><br>
|
||||
3. Enter the `Server Address` (**EXTERNAL_IP**), `Signaling Service Port` (**CROSSDESK_SERVER_PORT**), and `Relay Service Port` (**COTURN_PORT**) and click OK button.
|
||||
|
||||
4. Check the `Self-hosted server configuration` option and click the OK button to save the settings. If the server is using a valid (official) certificate, the process ends here and the client will show that it is connected to the server.
|
||||
|
||||
5. If the default certificate is used (skip this step if an official certificate is used), download the self-signed root certificate `api.crossdesk.cn_root.crt` from the server directory /var/lib/crossdesk/certs/ to the machine running the client, and install the certificate by executing the following command:
|
||||
|
||||
On Windows, open PowerShell with **administrator privileges** and execute:
|
||||
```
|
||||
certutil -addstore "Root" "C:\path\to\api.crossdesk.cn_root.crt"
|
||||
```
|
||||
Linux
|
||||
```
|
||||
sudo cp /path/to/api.crossdesk.cn_root.crt /usr/local/share/ca-certificates/api.crossdesk.cn_root.crt
|
||||
sudo update-ca-certificates
|
||||
```
|
||||
macOS
|
||||
```
|
||||
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain path/to/api.crossdesk.cn_root.crt
|
||||
```
|
||||
|
||||
4. If the self-hosted server is later reset or the certificate is replaced for any reason, you can click the `Reset Certificate Fingerprint` button to clear the certificate fingerprint saved on the client.<br><br>
|
||||
<img width="600" height="200" alt="image" src="https://github.com/user-attachments/assets/d9e423ab-0c2b-4fab-b132-4dc27462d704" /><br><br>
|
||||
|
||||
### Web Client
|
||||
See [CrossDesk Web Client](https://github.com/kunkundi/crossdesk-web-client)。
|
||||
|
||||
# FAQ
|
||||
See [FAQ](https://github.com/kunkundi/crosssesk/blob/self-hosted-server/docs/FAQ.md) .
|
||||
|
||||
# Acknowledgements
|
||||
- Thanks to [HelloGitHub](https://hellogithub.com/) for the recommendation and exposure.
|
||||
- Thanks to [Ruanyf Weekly](https://github.com/ruanyf/weekly) for featuring CrossDesk.
|
||||
- Thanks to the [LinuxDo](https://linux.do) community for the attention, discussions, and support that helped improve CrossDesk.
|
||||
|
||||
+27
-34
@@ -4,10 +4,30 @@ set -e
|
||||
PKG_NAME="crossdesk"
|
||||
APP_NAME="CrossDesk"
|
||||
|
||||
APP_VERSION="$1"
|
||||
ARCHITECTURE="amd64"
|
||||
MAINTAINER="Junkun Di <junkun.di@hotmail.com>"
|
||||
DESCRIPTION="A simple cross-platform remote desktop client."
|
||||
ALSA_RUNTIME_DEP="libasound2 | libasound2t64"
|
||||
PORTAL_RUNTIME_RECOMMENDS="xdg-desktop-portal, xdg-desktop-portal-gtk | xdg-desktop-portal-kde | xdg-desktop-portal-wlr"
|
||||
|
||||
normalize_app_version() {
|
||||
local input="$1"
|
||||
local prefix=""
|
||||
local body="$input"
|
||||
|
||||
if [[ "$body" == v* ]]; then
|
||||
prefix="v"
|
||||
body="${body#v}"
|
||||
fi
|
||||
|
||||
if [[ "$body" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
echo "${prefix}${BASH_REMATCH[1]}-${BASH_REMATCH[4]}-${BASH_REMATCH[3]}"
|
||||
else
|
||||
echo "$input"
|
||||
fi
|
||||
}
|
||||
|
||||
APP_VERSION="$(normalize_app_version "$1")"
|
||||
|
||||
# Remove 'v' prefix from version for Debian package (Debian version must start with digit)
|
||||
DEB_VERSION="${APP_VERSION#v}"
|
||||
@@ -15,21 +35,18 @@ DEB_VERSION="${APP_VERSION#v}"
|
||||
DEB_DIR="${PKG_NAME}-${DEB_VERSION}"
|
||||
DEBIAN_DIR="$DEB_DIR/DEBIAN"
|
||||
BIN_DIR="$DEB_DIR/usr/bin"
|
||||
CERT_SRC_DIR="$DEB_DIR/opt/$PKG_NAME/certs"
|
||||
ICON_BASE_DIR="$DEB_DIR/usr/share/icons/hicolor"
|
||||
DESKTOP_DIR="$DEB_DIR/usr/share/applications"
|
||||
|
||||
rm -rf "$DEB_DIR"
|
||||
|
||||
mkdir -p "$DEBIAN_DIR" "$BIN_DIR" "$CERT_SRC_DIR" "$DESKTOP_DIR"
|
||||
mkdir -p "$DEBIAN_DIR" "$BIN_DIR" "$DESKTOP_DIR"
|
||||
|
||||
cp build/linux/x86_64/release/crossdesk "$BIN_DIR/$PKG_NAME"
|
||||
chmod +x "$BIN_DIR/$PKG_NAME"
|
||||
|
||||
ln -s "$PKG_NAME" "$BIN_DIR/$APP_NAME"
|
||||
|
||||
cp certs/crossdesk.cn_root.crt "$CERT_SRC_DIR/crossdesk.cn_root.crt"
|
||||
|
||||
for size in 16 24 32 48 64 96 128 256; do
|
||||
mkdir -p "$ICON_BASE_DIR/${size}x${size}/apps"
|
||||
cp "icons/linux/crossdesk_${size}x${size}.png" \
|
||||
@@ -44,9 +61,9 @@ Maintainer: $MAINTAINER
|
||||
Description: $DESCRIPTION
|
||||
Depends: libc6 (>= 2.29), libstdc++6 (>= 9), libx11-6, libxcb1,
|
||||
libxcb-randr0, libxcb-xtest0, libxcb-xinerama0, libxcb-shape0,
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, libasound2,
|
||||
libsndio7.0, libxcb-shm0, libpulse0
|
||||
Recommends: nvidia-cuda-toolkit
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, $ALSA_RUNTIME_DEP,
|
||||
libsndio7.0, libxcb-shm0, libpulse0, libdrm2, libdbus-1-3
|
||||
Recommends: $PORTAL_RUNTIME_RECOMMENDS, nvidia-cuda-toolkit
|
||||
Priority: optional
|
||||
Section: utils
|
||||
EOF
|
||||
@@ -71,7 +88,6 @@ if [ "\$1" = "remove" ] || [ "\$1" = "purge" ]; then
|
||||
rm -f /usr/bin/$PKG_NAME || true
|
||||
rm -f /usr/bin/$APP_NAME || true
|
||||
rm -f /usr/share/applications/$PKG_NAME.desktop || true
|
||||
rm -rf /opt/$PKG_NAME/certs || true
|
||||
for size in 16 24 32 48 64 96 128 256; do
|
||||
rm -f /usr/share/icons/hicolor/\${size}x\${size}/apps/$PKG_NAME.png || true
|
||||
done
|
||||
@@ -85,32 +101,9 @@ cat > "$DEBIAN_DIR/postinst" << 'EOF'
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
CERT_SRC="/opt/crossdesk/certs"
|
||||
CERT_FILE="crossdesk.cn_root.crt"
|
||||
|
||||
for user_home in /home/*; do
|
||||
[ -d "$user_home" ] || continue
|
||||
username=$(basename "$user_home")
|
||||
config_dir="$user_home/.config/CrossDesk/certs"
|
||||
target="$config_dir/$CERT_FILE"
|
||||
|
||||
if [ ! -f "$target" ]; then
|
||||
mkdir -p "$config_dir" || true
|
||||
cp "$CERT_SRC/$CERT_FILE" "$target" || true
|
||||
chown -R "$username:$username" "$user_home/.config/CrossDesk" || true
|
||||
echo "✔ Installed cert for $username at $target"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -d "/root" ]; then
|
||||
config_dir="/root/.config/CrossDesk/certs"
|
||||
mkdir -p "$config_dir" || true
|
||||
cp "$CERT_SRC/$CERT_FILE" "$config_dir/$CERT_FILE" || true
|
||||
chown -R root:root /root/.config/CrossDesk || true
|
||||
fi
|
||||
|
||||
exit 0
|
||||
EOF
|
||||
|
||||
chmod +x "$DEBIAN_DIR/postinst"
|
||||
|
||||
dpkg-deb --build "$DEB_DIR"
|
||||
@@ -120,4 +113,4 @@ mv "$DEB_DIR.deb" "$OUTPUT_FILE"
|
||||
|
||||
rm -rf "$DEB_DIR"
|
||||
|
||||
echo "✅ Deb package created: $OUTPUT_FILE"
|
||||
echo "✅ Deb package created: $OUTPUT_FILE"
|
||||
|
||||
+26
-33
@@ -4,10 +4,30 @@ set -e
|
||||
PKG_NAME="crossdesk"
|
||||
APP_NAME="CrossDesk"
|
||||
|
||||
APP_VERSION="$1"
|
||||
ARCHITECTURE="arm64"
|
||||
MAINTAINER="Junkun Di <junkun.di@hotmail.com>"
|
||||
DESCRIPTION="A simple cross-platform remote desktop client."
|
||||
ALSA_RUNTIME_DEP="libasound2 | libasound2t64"
|
||||
PORTAL_RUNTIME_RECOMMENDS="xdg-desktop-portal, xdg-desktop-portal-gtk | xdg-desktop-portal-kde | xdg-desktop-portal-wlr"
|
||||
|
||||
normalize_app_version() {
|
||||
local input="$1"
|
||||
local prefix=""
|
||||
local body="$input"
|
||||
|
||||
if [[ "$body" == v* ]]; then
|
||||
prefix="v"
|
||||
body="${body#v}"
|
||||
fi
|
||||
|
||||
if [[ "$body" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
echo "${prefix}${BASH_REMATCH[1]}-${BASH_REMATCH[4]}-${BASH_REMATCH[3]}"
|
||||
else
|
||||
echo "$input"
|
||||
fi
|
||||
}
|
||||
|
||||
APP_VERSION="$(normalize_app_version "$1")"
|
||||
|
||||
# Remove 'v' prefix from version for Debian package (Debian version must start with digit)
|
||||
DEB_VERSION="${APP_VERSION#v}"
|
||||
@@ -15,21 +35,18 @@ DEB_VERSION="${APP_VERSION#v}"
|
||||
DEB_DIR="${PKG_NAME}-${DEB_VERSION}"
|
||||
DEBIAN_DIR="$DEB_DIR/DEBIAN"
|
||||
BIN_DIR="$DEB_DIR/usr/bin"
|
||||
CERT_SRC_DIR="$DEB_DIR/opt/$PKG_NAME/certs"
|
||||
ICON_BASE_DIR="$DEB_DIR/usr/share/icons/hicolor"
|
||||
DESKTOP_DIR="$DEB_DIR/usr/share/applications"
|
||||
|
||||
rm -rf "$DEB_DIR"
|
||||
|
||||
mkdir -p "$DEBIAN_DIR" "$BIN_DIR" "$CERT_SRC_DIR" "$DESKTOP_DIR"
|
||||
mkdir -p "$DEBIAN_DIR" "$BIN_DIR" "$DESKTOP_DIR"
|
||||
|
||||
cp build/linux/arm64/release/crossdesk "$BIN_DIR"
|
||||
chmod +x "$BIN_DIR/$PKG_NAME"
|
||||
|
||||
ln -s "$PKG_NAME" "$BIN_DIR/$APP_NAME"
|
||||
|
||||
cp certs/crossdesk.cn_root.crt "$CERT_SRC_DIR/crossdesk.cn_root.crt"
|
||||
|
||||
for size in 16 24 32 48 64 96 128 256; do
|
||||
mkdir -p "$ICON_BASE_DIR/${size}x${size}/apps"
|
||||
cp "icons/linux/crossdesk_${size}x${size}.png" \
|
||||
@@ -44,8 +61,9 @@ Maintainer: $MAINTAINER
|
||||
Description: $DESCRIPTION
|
||||
Depends: libc6 (>= 2.29), libstdc++6 (>= 9), libx11-6, libxcb1,
|
||||
libxcb-randr0, libxcb-xtest0, libxcb-xinerama0, libxcb-shape0,
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, libasound2,
|
||||
libsndio7.0, libxcb-shm0, libpulse0
|
||||
libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, $ALSA_RUNTIME_DEP,
|
||||
libsndio7.0, libxcb-shm0, libpulse0, libdrm2, libdbus-1-3
|
||||
Recommends: $PORTAL_RUNTIME_RECOMMENDS
|
||||
Priority: optional
|
||||
Section: utils
|
||||
EOF
|
||||
@@ -70,7 +88,6 @@ if [ "\$1" = "remove" ] || [ "\$1" = "purge" ]; then
|
||||
rm -f /usr/bin/$PKG_NAME || true
|
||||
rm -f /usr/bin/$APP_NAME || true
|
||||
rm -f /usr/share/applications/$PKG_NAME.desktop || true
|
||||
rm -rf /opt/$PKG_NAME/certs || true
|
||||
for size in 16 24 32 48 64 96 128 256; do
|
||||
rm -f /usr/share/icons/hicolor/\${size}x\${size}/apps/$PKG_NAME.png || true
|
||||
done
|
||||
@@ -84,30 +101,6 @@ cat > "$DEBIAN_DIR/postinst" << 'EOF'
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
CERT_SRC="/opt/crossdesk/certs"
|
||||
CERT_FILE="crossdesk.cn_root.crt"
|
||||
|
||||
for user_home in /home/*; do
|
||||
[ -d "$user_home" ] || continue
|
||||
username=$(basename "$user_home")
|
||||
config_dir="$user_home/.config/CrossDesk/certs"
|
||||
target="$config_dir/$CERT_FILE"
|
||||
|
||||
if [ ! -f "$target" ]; then
|
||||
mkdir -p "$config_dir" || true
|
||||
cp "$CERT_SRC/$CERT_FILE" "$target" || true
|
||||
chown -R "$username:$username" "$user_home/.config/CrossDesk" || true
|
||||
echo "✔ Installed cert for $username at $target"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -d "/root" ]; then
|
||||
config_dir="/root/.config/CrossDesk/certs"
|
||||
mkdir -p "$config_dir" || true
|
||||
cp "$CERT_SRC/$CERT_FILE" "$config_dir/$CERT_FILE" || true
|
||||
chown -R root:root /root/.config/CrossDesk || true
|
||||
fi
|
||||
|
||||
exit 0
|
||||
EOF
|
||||
|
||||
@@ -120,4 +113,4 @@ mv "$DEB_DIR.deb" "$OUTPUT_FILE"
|
||||
|
||||
rm -rf "$DEB_DIR"
|
||||
|
||||
echo "✅ Deb package created: $OUTPUT_FILE"
|
||||
echo "✅ Deb package created: $OUTPUT_FILE"
|
||||
|
||||
+117
-67
@@ -4,20 +4,36 @@ set -e
|
||||
APP_NAME="crossdesk"
|
||||
APP_NAME_UPPER="CrossDesk"
|
||||
EXECUTABLE_PATH="./build/macosx/arm64/release/crossdesk"
|
||||
APP_VERSION="$1"
|
||||
PLATFORM="macos"
|
||||
ARCH="arm64"
|
||||
IDENTIFIER="cn.crossdesk.app"
|
||||
ICON_PATH="icons/macos/crossdesk.icns"
|
||||
MACOS_MIN_VERSION="10.12"
|
||||
|
||||
CERTS_SOURCE="certs"
|
||||
CERT_NAME="crossdesk.cn_root.crt"
|
||||
normalize_app_version() {
|
||||
local input="$1"
|
||||
local prefix=""
|
||||
local body="$input"
|
||||
|
||||
if [[ "$body" == v* ]]; then
|
||||
prefix="v"
|
||||
body="${body#v}"
|
||||
fi
|
||||
|
||||
if [[ "$body" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
echo "${prefix}${BASH_REMATCH[1]}-${BASH_REMATCH[4]}-${BASH_REMATCH[3]}"
|
||||
else
|
||||
echo "$input"
|
||||
fi
|
||||
}
|
||||
|
||||
APP_VERSION="$(normalize_app_version "$1")"
|
||||
|
||||
APP_BUNDLE="${APP_NAME_UPPER}.app"
|
||||
CONTENTS_DIR="${APP_BUNDLE}/Contents"
|
||||
MACOS_DIR="${CONTENTS_DIR}/MacOS"
|
||||
RESOURCES_DIR="${CONTENTS_DIR}/Resources"
|
||||
INSTALLER_TITLE="${APP_NAME_UPPER} ${APP_VERSION}"
|
||||
|
||||
PKG_NAME="${APP_NAME}-${PLATFORM}-${ARCH}-${APP_VERSION}.pkg"
|
||||
DMG_NAME="${APP_NAME}-${PLATFORM}-${ARCH}-${APP_VERSION}.dmg"
|
||||
@@ -76,87 +92,121 @@ cat > "${CONTENTS_DIR}/Info.plist" <<EOF
|
||||
</plist>
|
||||
EOF
|
||||
|
||||
xattr -cr "${APP_BUNDLE}" 2>/dev/null || true
|
||||
find "${APP_BUNDLE}" -name '._*' -delete
|
||||
|
||||
echo ".app created successfully."
|
||||
|
||||
mkdir -p build_pkg_scripts
|
||||
cp scripts/macosx/tcc_postinstall.sh build_pkg_scripts/postinstall
|
||||
chmod +x build_pkg_scripts/postinstall
|
||||
|
||||
mkdir -p build_pkg_resources
|
||||
cat > build_pkg_resources/welcome.html <<EOF
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
color: #222;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
h1 {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
p {
|
||||
margin: 0 0 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>欢迎安装 ${INSTALLER_TITLE}</h1>
|
||||
<p>CrossDesk 将安装到“应用程序”文件夹。</p>
|
||||
<p>首次启动时,CrossDesk 会引导你在系统设置中授予必要权限,包括辅助功能、录屏与系统录音等。</p>
|
||||
<p>为避免旧版本授权残留造成状态误判,安装后可能需要重新授权。</p>
|
||||
<p>安装完成后,请从“应用程序”文件夹启动 CrossDesk。</p>
|
||||
</body>
|
||||
</html>
|
||||
EOF
|
||||
|
||||
echo "building pkg..."
|
||||
pkgbuild \
|
||||
--identifier "${IDENTIFIER}" \
|
||||
--version "${APP_VERSION}" \
|
||||
--install-location "/Applications" \
|
||||
--component "${APP_BUNDLE}" \
|
||||
--scripts build_pkg_scripts \
|
||||
build_pkg_temp/${APP_NAME}-component.pkg
|
||||
|
||||
mkdir -p build_pkg_scripts
|
||||
|
||||
cat > build_pkg_scripts/postinstall <<'EOF'
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
IDENTIFIER="cn.crossdesk.app"
|
||||
|
||||
# 获取当前登录用户
|
||||
USER_HOME=$( /usr/bin/stat -f "%Su" /dev/console )
|
||||
HOME_DIR=$( /usr/bin/dscl . -read /Users/$USER_HOME NFSHomeDirectory | awk '{print $2}' )
|
||||
|
||||
# 复制证书文件
|
||||
DEST="$HOME_DIR/Library/Application Support/CrossDesk/certs"
|
||||
mkdir -p "$DEST"
|
||||
cp -R "/Library/Application Support/CrossDesk/certs/"* "$DEST/"
|
||||
|
||||
# 清除应用的权限授权,以便重新授权
|
||||
# 使用 tccutil 重置录屏权限和辅助功能权限
|
||||
if command -v tccutil >/dev/null 2>&1; then
|
||||
# 重置录屏权限
|
||||
tccutil reset ScreenCapture "$IDENTIFIER" 2>/dev/null || true
|
||||
# 重置辅助功能权限
|
||||
tccutil reset Accessibility "$IDENTIFIER" 2>/dev/null || true
|
||||
# 重置摄像头权限(如果需要)
|
||||
tccutil reset Camera "$IDENTIFIER" 2>/dev/null || true
|
||||
# 重置麦克风权限(如果需要)
|
||||
tccutil reset Microphone "$IDENTIFIER" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# 为所有用户清除权限(可选,如果需要)
|
||||
# 遍历所有用户目录并清除权限
|
||||
for USER_DIR in /Users/*; do
|
||||
if [ -d "$USER_DIR" ] && [ "$USER_DIR" != "/Users/Shared" ]; then
|
||||
USER_NAME=$(basename "$USER_DIR")
|
||||
# 跳过系统用户
|
||||
if [ "$USER_NAME" != "Shared" ] && [ -d "$USER_DIR/Library" ]; then
|
||||
# 删除 TCC 数据库中的相关条目(需要管理员权限)
|
||||
TCC_DB="$USER_DIR/Library/Application Support/com.apple.TCC/TCC.db"
|
||||
if [ -f "$TCC_DB" ]; then
|
||||
# 使用 sqlite3 删除相关权限记录(如果可用)
|
||||
if command -v sqlite3 >/dev/null 2>&1; then
|
||||
sqlite3 "$TCC_DB" "DELETE FROM access WHERE client='$IDENTIFIER' AND service IN ('kTCCServiceScreenCapture', 'kTCCServiceAccessibility');" 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
exit 0
|
||||
cat > build_pkg_temp/Distribution <<EOF
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<installer-gui-script minSpecVersion="1">
|
||||
<title>${INSTALLER_TITLE}</title>
|
||||
<welcome file="welcome.html" mime-type="text/html"/>
|
||||
<options customize="never" require-scripts="false" hostArchitectures="arm64"/>
|
||||
<choices-outline>
|
||||
<line choice="default">
|
||||
<line choice="${IDENTIFIER}"/>
|
||||
</line>
|
||||
</choices-outline>
|
||||
<choice id="default" title="${INSTALLER_TITLE}"/>
|
||||
<choice id="${IDENTIFIER}" title="${INSTALLER_TITLE}" visible="false">
|
||||
<pkg-ref id="${IDENTIFIER}"/>
|
||||
</choice>
|
||||
<pkg-ref id="${IDENTIFIER}" version="${APP_VERSION}" onConclusion="none">crossdesk-component.pkg</pkg-ref>
|
||||
</installer-gui-script>
|
||||
EOF
|
||||
|
||||
chmod +x build_pkg_scripts/postinstall
|
||||
|
||||
pkgbuild \
|
||||
--root "${CERTS_SOURCE}" \
|
||||
--identifier "${IDENTIFIER}.certs" \
|
||||
--version "${APP_VERSION}" \
|
||||
--install-location "/Library/Application Support/CrossDesk/certs" \
|
||||
--scripts build_pkg_scripts \
|
||||
build_pkg_temp/${APP_NAME}-certs.pkg
|
||||
|
||||
productbuild \
|
||||
--package build_pkg_temp/${APP_NAME}-component.pkg \
|
||||
--package build_pkg_temp/${APP_NAME}-certs.pkg \
|
||||
--distribution build_pkg_temp/Distribution \
|
||||
--package-path build_pkg_temp \
|
||||
--resources build_pkg_resources \
|
||||
"${PKG_NAME}"
|
||||
|
||||
echo "PKG package created: ${PKG_NAME}"
|
||||
|
||||
rm -rf build_pkg_temp build_pkg_scripts ${APP_BUNDLE}
|
||||
# Set custom icon for PKG file
|
||||
if [ -f "${ICON_PATH}" ]; then
|
||||
echo "Setting custom icon for PKG file..."
|
||||
# Create a temporary iconset from icns
|
||||
TEMP_ICON_DIR=$(mktemp -d)
|
||||
cp "${ICON_PATH}" "${TEMP_ICON_DIR}/icon.icns"
|
||||
|
||||
# Use sips to create a png from icns for the icon
|
||||
sips -s format png "${TEMP_ICON_DIR}/icon.icns" --out "${TEMP_ICON_DIR}/icon.png" 2>/dev/null || true
|
||||
|
||||
# Method: Use osascript to set file icon (works on macOS)
|
||||
osascript <<APPLESCRIPT
|
||||
use framework "Foundation"
|
||||
use framework "AppKit"
|
||||
|
||||
set iconPath to POSIX file "${TEMP_ICON_DIR}/icon.icns"
|
||||
set targetPath to POSIX file "$(pwd)/${PKG_NAME}"
|
||||
|
||||
set iconImage to current application's NSImage's alloc()'s initWithContentsOfFile:(POSIX path of iconPath)
|
||||
set workspace to current application's NSWorkspace's sharedWorkspace()
|
||||
workspace's setIcon:iconImage forFile:(POSIX path of targetPath) options:0
|
||||
APPLESCRIPT
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Custom icon set successfully for ${PKG_NAME}"
|
||||
else
|
||||
echo "Warning: Failed to set custom icon (this is optional)"
|
||||
fi
|
||||
|
||||
rm -rf "${TEMP_ICON_DIR}"
|
||||
fi
|
||||
echo "Set icon finished"
|
||||
|
||||
rm -rf build_pkg_temp build_pkg_scripts build_pkg_resources ${APP_BUNDLE}
|
||||
|
||||
echo "PKG package created successfully."
|
||||
echo "package ${APP_BUNDLE}"
|
||||
echo "installer ${PKG_NAME}"
|
||||
echo "installer ${PKG_NAME}"
|
||||
|
||||
+117
-67
@@ -4,20 +4,36 @@ set -e
|
||||
APP_NAME="crossdesk"
|
||||
APP_NAME_UPPER="CrossDesk"
|
||||
EXECUTABLE_PATH="build/macosx/x86_64/release/crossdesk"
|
||||
APP_VERSION="$1"
|
||||
PLATFORM="macos"
|
||||
ARCH="x64"
|
||||
IDENTIFIER="cn.crossdesk.app"
|
||||
ICON_PATH="icons/macos/crossdesk.icns"
|
||||
MACOS_MIN_VERSION="10.12"
|
||||
|
||||
CERTS_SOURCE="certs"
|
||||
CERT_NAME="crossdesk.cn_root.crt"
|
||||
normalize_app_version() {
|
||||
local input="$1"
|
||||
local prefix=""
|
||||
local body="$input"
|
||||
|
||||
if [[ "$body" == v* ]]; then
|
||||
prefix="v"
|
||||
body="${body#v}"
|
||||
fi
|
||||
|
||||
if [[ "$body" =~ ^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$ ]]; then
|
||||
echo "${prefix}${BASH_REMATCH[1]}-${BASH_REMATCH[4]}-${BASH_REMATCH[3]}"
|
||||
else
|
||||
echo "$input"
|
||||
fi
|
||||
}
|
||||
|
||||
APP_VERSION="$(normalize_app_version "$1")"
|
||||
|
||||
APP_BUNDLE="${APP_NAME_UPPER}.app"
|
||||
CONTENTS_DIR="${APP_BUNDLE}/Contents"
|
||||
MACOS_DIR="${CONTENTS_DIR}/MacOS"
|
||||
RESOURCES_DIR="${CONTENTS_DIR}/Resources"
|
||||
INSTALLER_TITLE="${APP_NAME_UPPER} ${APP_VERSION}"
|
||||
|
||||
PKG_NAME="${APP_NAME}-${PLATFORM}-${ARCH}-${APP_VERSION}.pkg"
|
||||
DMG_NAME="${APP_NAME}-${PLATFORM}-${ARCH}-${APP_VERSION}.dmg"
|
||||
@@ -76,87 +92,121 @@ cat > "${CONTENTS_DIR}/Info.plist" <<EOF
|
||||
</plist>
|
||||
EOF
|
||||
|
||||
xattr -cr "${APP_BUNDLE}" 2>/dev/null || true
|
||||
find "${APP_BUNDLE}" -name '._*' -delete
|
||||
|
||||
echo ".app created successfully."
|
||||
|
||||
mkdir -p build_pkg_scripts
|
||||
cp scripts/macosx/tcc_postinstall.sh build_pkg_scripts/postinstall
|
||||
chmod +x build_pkg_scripts/postinstall
|
||||
|
||||
mkdir -p build_pkg_resources
|
||||
cat > build_pkg_resources/welcome.html <<EOF
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<style>
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
color: #222;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
h1 {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
p {
|
||||
margin: 0 0 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>欢迎安装 ${INSTALLER_TITLE}</h1>
|
||||
<p>CrossDesk 将安装到“应用程序”文件夹。</p>
|
||||
<p>首次启动时,CrossDesk 会引导你在系统设置中授予必要权限,包括辅助功能、录屏与系统录音等。</p>
|
||||
<p>为避免旧版本授权残留造成状态误判,安装后可能需要重新授权。</p>
|
||||
<p>安装完成后,请从“应用程序”文件夹启动 CrossDesk。</p>
|
||||
</body>
|
||||
</html>
|
||||
EOF
|
||||
|
||||
echo "building pkg..."
|
||||
pkgbuild \
|
||||
--identifier "${IDENTIFIER}" \
|
||||
--version "${APP_VERSION}" \
|
||||
--install-location "/Applications" \
|
||||
--component "${APP_BUNDLE}" \
|
||||
--scripts build_pkg_scripts \
|
||||
build_pkg_temp/${APP_NAME}-component.pkg
|
||||
|
||||
mkdir -p build_pkg_scripts
|
||||
|
||||
cat > build_pkg_scripts/postinstall <<'EOF'
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
IDENTIFIER="cn.crossdesk.app"
|
||||
|
||||
# 获取当前登录用户
|
||||
USER_HOME=$( /usr/bin/stat -f "%Su" /dev/console )
|
||||
HOME_DIR=$( /usr/bin/dscl . -read /Users/$USER_HOME NFSHomeDirectory | awk '{print $2}' )
|
||||
|
||||
# 复制证书文件
|
||||
DEST="$HOME_DIR/Library/Application Support/CrossDesk/certs"
|
||||
mkdir -p "$DEST"
|
||||
cp -R "/Library/Application Support/CrossDesk/certs/"* "$DEST/"
|
||||
|
||||
# 清除应用的权限授权,以便重新授权
|
||||
# 使用 tccutil 重置录屏权限和辅助功能权限
|
||||
if command -v tccutil >/dev/null 2>&1; then
|
||||
# 重置录屏权限
|
||||
tccutil reset ScreenCapture "$IDENTIFIER" 2>/dev/null || true
|
||||
# 重置辅助功能权限
|
||||
tccutil reset Accessibility "$IDENTIFIER" 2>/dev/null || true
|
||||
# 重置摄像头权限(如果需要)
|
||||
tccutil reset Camera "$IDENTIFIER" 2>/dev/null || true
|
||||
# 重置麦克风权限(如果需要)
|
||||
tccutil reset Microphone "$IDENTIFIER" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# 为所有用户清除权限(可选,如果需要)
|
||||
# 遍历所有用户目录并清除权限
|
||||
for USER_DIR in /Users/*; do
|
||||
if [ -d "$USER_DIR" ] && [ "$USER_DIR" != "/Users/Shared" ]; then
|
||||
USER_NAME=$(basename "$USER_DIR")
|
||||
# 跳过系统用户
|
||||
if [ "$USER_NAME" != "Shared" ] && [ -d "$USER_DIR/Library" ]; then
|
||||
# 删除 TCC 数据库中的相关条目(需要管理员权限)
|
||||
TCC_DB="$USER_DIR/Library/Application Support/com.apple.TCC/TCC.db"
|
||||
if [ -f "$TCC_DB" ]; then
|
||||
# 使用 sqlite3 删除相关权限记录(如果可用)
|
||||
if command -v sqlite3 >/dev/null 2>&1; then
|
||||
sqlite3 "$TCC_DB" "DELETE FROM access WHERE client='$IDENTIFIER' AND service IN ('kTCCServiceScreenCapture', 'kTCCServiceAccessibility');" 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
exit 0
|
||||
cat > build_pkg_temp/Distribution <<EOF
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<installer-gui-script minSpecVersion="1">
|
||||
<title>${INSTALLER_TITLE}</title>
|
||||
<welcome file="welcome.html" mime-type="text/html"/>
|
||||
<options customize="never" require-scripts="false" hostArchitectures="x86_64"/>
|
||||
<choices-outline>
|
||||
<line choice="default">
|
||||
<line choice="${IDENTIFIER}"/>
|
||||
</line>
|
||||
</choices-outline>
|
||||
<choice id="default" title="${INSTALLER_TITLE}"/>
|
||||
<choice id="${IDENTIFIER}" title="${INSTALLER_TITLE}" visible="false">
|
||||
<pkg-ref id="${IDENTIFIER}"/>
|
||||
</choice>
|
||||
<pkg-ref id="${IDENTIFIER}" version="${APP_VERSION}" onConclusion="none">crossdesk-component.pkg</pkg-ref>
|
||||
</installer-gui-script>
|
||||
EOF
|
||||
|
||||
chmod +x build_pkg_scripts/postinstall
|
||||
|
||||
pkgbuild \
|
||||
--root "${CERTS_SOURCE}" \
|
||||
--identifier "${IDENTIFIER}.certs" \
|
||||
--version "${APP_VERSION}" \
|
||||
--install-location "/Library/Application Support/CrossDesk/certs" \
|
||||
--scripts build_pkg_scripts \
|
||||
build_pkg_temp/${APP_NAME}-certs.pkg
|
||||
|
||||
productbuild \
|
||||
--package build_pkg_temp/${APP_NAME}-component.pkg \
|
||||
--package build_pkg_temp/${APP_NAME}-certs.pkg \
|
||||
--distribution build_pkg_temp/Distribution \
|
||||
--package-path build_pkg_temp \
|
||||
--resources build_pkg_resources \
|
||||
"${PKG_NAME}"
|
||||
|
||||
echo "PKG package created: ${PKG_NAME}"
|
||||
|
||||
rm -rf build_pkg_temp build_pkg_scripts ${APP_BUNDLE}
|
||||
# Set custom icon for PKG file
|
||||
if [ -f "${ICON_PATH}" ]; then
|
||||
echo "Setting custom icon for PKG file..."
|
||||
# Create a temporary iconset from icns
|
||||
TEMP_ICON_DIR=$(mktemp -d)
|
||||
cp "${ICON_PATH}" "${TEMP_ICON_DIR}/icon.icns"
|
||||
|
||||
# Use sips to create a png from icns for the icon
|
||||
sips -s format png "${TEMP_ICON_DIR}/icon.icns" --out "${TEMP_ICON_DIR}/icon.png" 2>/dev/null || true
|
||||
|
||||
# Method: Use osascript to set file icon (works on macOS)
|
||||
osascript <<APPLESCRIPT
|
||||
use framework "Foundation"
|
||||
use framework "AppKit"
|
||||
|
||||
set iconPath to POSIX file "${TEMP_ICON_DIR}/icon.icns"
|
||||
set targetPath to POSIX file "$(pwd)/${PKG_NAME}"
|
||||
|
||||
set iconImage to current application's NSImage's alloc()'s initWithContentsOfFile:(POSIX path of iconPath)
|
||||
set workspace to current application's NSWorkspace's sharedWorkspace()
|
||||
workspace's setIcon:iconImage forFile:(POSIX path of targetPath) options:0
|
||||
APPLESCRIPT
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Custom icon set successfully for ${PKG_NAME}"
|
||||
else
|
||||
echo "Warning: Failed to set custom icon (this is optional)"
|
||||
fi
|
||||
|
||||
rm -rf "${TEMP_ICON_DIR}"
|
||||
fi
|
||||
echo "Set icon finished"
|
||||
|
||||
rm -rf build_pkg_temp build_pkg_scripts build_pkg_resources ${APP_BUNDLE}
|
||||
|
||||
echo "PKG package created successfully."
|
||||
echo "package ${APP_BUNDLE}"
|
||||
echo "installer ${PKG_NAME}"
|
||||
echo "installer ${PKG_NAME}"
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
APP_IDENTIFIER="cn.crossdesk.app"
|
||||
|
||||
# Keep known historical identifiers here. tccutil only resets identifiers that
|
||||
# Launch Services can currently resolve, so path/db cleanup below remains a
|
||||
# best-effort fallback for stale entries from unsigned or removed builds.
|
||||
BUNDLE_IDENTIFIERS=(
|
||||
"cn.crossdesk.app"
|
||||
"cn.crossdesk.CrossDesk"
|
||||
"com.crossdesk.app"
|
||||
"com.crossdesk.CrossDesk"
|
||||
"com.kunkundi.crossdesk"
|
||||
"com.kunkundi.CrossDesk"
|
||||
)
|
||||
|
||||
TCC_SERVICES=(
|
||||
"ScreenCapture"
|
||||
"Accessibility"
|
||||
"Microphone"
|
||||
"AudioCapture"
|
||||
)
|
||||
|
||||
run_tccutil() {
|
||||
local user_name="$1"
|
||||
local user_id="$2"
|
||||
local service="$3"
|
||||
local bundle_id="$4"
|
||||
|
||||
if [ -n "$user_name" ] && [ -n "$user_id" ]; then
|
||||
/bin/launchctl asuser "$user_id" \
|
||||
/usr/bin/sudo -u "$user_name" \
|
||||
/usr/bin/tccutil reset "$service" "$bundle_id" >/dev/null 2>&1
|
||||
else
|
||||
/usr/bin/tccutil reset "$service" "$bundle_id" >/dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
reset_bundle_tcc() {
|
||||
local user_name="$1"
|
||||
local user_id="$2"
|
||||
local bundle_id
|
||||
local service
|
||||
|
||||
for bundle_id in "${BUNDLE_IDENTIFIERS[@]}"; do
|
||||
if run_tccutil "$user_name" "$user_id" "All" "$bundle_id"; then
|
||||
continue
|
||||
fi
|
||||
|
||||
for service in "${TCC_SERVICES[@]}"; do
|
||||
run_tccutil "$user_name" "$user_id" "$service" "$bundle_id" || true
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
cleanup_tcc_db() {
|
||||
local db_path="$1"
|
||||
|
||||
if [ ! -f "$db_path" ] || ! command -v sqlite3 >/dev/null 2>&1; then
|
||||
return
|
||||
fi
|
||||
|
||||
/usr/bin/sqlite3 "$db_path" <<'SQL' >/dev/null 2>&1 || true
|
||||
DELETE FROM access
|
||||
WHERE service IN (
|
||||
'kTCCServiceScreenCapture',
|
||||
'kTCCServiceAccessibility',
|
||||
'kTCCServiceMicrophone',
|
||||
'kTCCServiceAudioCapture'
|
||||
)
|
||||
AND (
|
||||
client IN (
|
||||
'cn.crossdesk.app',
|
||||
'cn.crossdesk.CrossDesk',
|
||||
'com.crossdesk.app',
|
||||
'com.crossdesk.CrossDesk',
|
||||
'com.kunkundi.crossdesk',
|
||||
'com.kunkundi.CrossDesk'
|
||||
)
|
||||
OR lower(client) LIKE '%crossdesk%'
|
||||
);
|
||||
SQL
|
||||
}
|
||||
|
||||
cleanup_user_tcc_db() {
|
||||
local user_name="$1"
|
||||
local home_dir
|
||||
|
||||
home_dir=$(/usr/bin/dscl . -read "/Users/${user_name}" NFSHomeDirectory 2>/dev/null |
|
||||
/usr/bin/awk '{print $2}')
|
||||
if [ -z "$home_dir" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
cleanup_tcc_db "${home_dir}/Library/Application Support/com.apple.TCC/TCC.db"
|
||||
}
|
||||
|
||||
CONSOLE_USER=$(/usr/bin/stat -f "%Su" /dev/console 2>/dev/null || true)
|
||||
if [ -n "$CONSOLE_USER" ] &&
|
||||
[ "$CONSOLE_USER" != "root" ] &&
|
||||
[ "$CONSOLE_USER" != "loginwindow" ]; then
|
||||
CONSOLE_UID=$(/usr/bin/id -u "$CONSOLE_USER" 2>/dev/null || true)
|
||||
reset_bundle_tcc "$CONSOLE_USER" "$CONSOLE_UID"
|
||||
cleanup_user_tcc_db "$CONSOLE_USER"
|
||||
fi
|
||||
|
||||
# Also clear any system/root-scoped decisions as a harmless fallback.
|
||||
reset_bundle_tcc "" ""
|
||||
cleanup_tcc_db "/Library/Application Support/com.apple.TCC/TCC.db"
|
||||
|
||||
exit 0
|
||||
@@ -1,17 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
|
||||
|
||||
<!-- 应用程序标识 -->
|
||||
<assemblyIdentity
|
||||
version="1.0.0.0"
|
||||
processorArchitecture="*"
|
||||
name="CrossDesk"
|
||||
type="win32" />
|
||||
|
||||
<!-- 描述信息 -->
|
||||
<description>CrossDesk Application</description>
|
||||
|
||||
<!-- 权限:要求管理员运行 -->
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<security>
|
||||
<requestedPrivileges>
|
||||
@@ -20,24 +16,17 @@
|
||||
</security>
|
||||
</trustInfo>
|
||||
|
||||
<!-- DPI 感知设置:支持高分屏 -->
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<!-- Windows Vista/7 风格 DPI 感知 -->
|
||||
<dpiAware>true/pm</dpiAware>
|
||||
<!-- Windows 10/11 高级 DPI 感知 -->
|
||||
<dpiAwareness>PerMonitorV2</dpiAwareness>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
|
||||
<!-- Windows 兼容性声明 -->
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- 支持 Windows 10 -->
|
||||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
|
||||
<!-- 支持 Windows 11(向下兼容 Win10 GUID) -->
|
||||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
</assembly>
|
||||
</assembly>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// Application icon resource; load by the resource name IDI_ICON1.
|
||||
IDI_ICON1 ICON "..\\..\\icons\\windows\\crossdesk.ico"
|
||||
|
||||
#define CREATEPROCESS_MANIFEST_RESOURCE_ID 1
|
||||
#define RT_MANIFEST 24
|
||||
|
||||
#ifdef CROSSDESK_DEBUG
|
||||
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "crossdesk_debug.manifest"
|
||||
#else
|
||||
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "crossdesk.manifest"
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
|
||||
|
||||
<assemblyIdentity
|
||||
version="1.0.0.0"
|
||||
name="CrossDesk"
|
||||
type="win32" />
|
||||
|
||||
<description>CrossDesk Application</description>
|
||||
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<security>
|
||||
<requestedPrivileges>
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
</assembly>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
|
||||
|
||||
<assemblyIdentity
|
||||
version="1.0.0.0"
|
||||
name="CrossDesk"
|
||||
type="win32" />
|
||||
|
||||
<description>CrossDesk Portable Application</description>
|
||||
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<security>
|
||||
<requestedPrivileges>
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
</assembly>
|
||||
@@ -0,0 +1,8 @@
|
||||
// Portable build resource. The app itself runs as the current user; service
|
||||
// installation raises a separate UAC prompt only when the user chooses it.
|
||||
IDI_ICON1 ICON "..\\..\\icons\\windows\\crossdesk.ico"
|
||||
|
||||
#define CREATEPROCESS_MANIFEST_RESOURCE_ID 1
|
||||
#define RT_MANIFEST 24
|
||||
|
||||
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "crossdesk_portable.manifest"
|
||||
@@ -8,13 +8,11 @@
|
||||
!define PRODUCT_WEB_SITE "https://www.crossdesk.cn/"
|
||||
!define APP_NAME "CrossDesk"
|
||||
!define UNINSTALL_REG_KEY "CrossDesk"
|
||||
!define PRODUCT_SERVICE_NAME "CrossDeskService"
|
||||
|
||||
; Installer icon path
|
||||
!define MUI_ICON "${__FILEDIR__}\..\..\icons\windows\crossdesk.ico"
|
||||
|
||||
; Certificate path
|
||||
!define CERT_FILE "${__FILEDIR__}\..\..\certs\crossdesk.cn_root.crt"
|
||||
|
||||
; Compression settings
|
||||
SetCompressor /FINAL lzma
|
||||
|
||||
@@ -49,7 +47,7 @@ ShowInstDetails show
|
||||
|
||||
Section "MainSection"
|
||||
; Check if CrossDesk is running
|
||||
StrCpy $1 "crossdesk.exe"
|
||||
StrCpy $1 "CrossDesk.exe"
|
||||
|
||||
nsProcess::_FindProcess "$1"
|
||||
Pop $R0
|
||||
@@ -71,14 +69,20 @@ cancelInstall:
|
||||
Abort
|
||||
|
||||
installApp:
|
||||
Call StopInstalledService
|
||||
|
||||
SetOutPath "$INSTDIR"
|
||||
SetOverwrite ifnewer
|
||||
|
||||
; Main application executable path
|
||||
File /oname=crossdesk.exe "..\..\build\windows\x64\release\crossdesk.exe"
|
||||
|
||||
; Copy icon file to installation directory
|
||||
File "${MUI_ICON}"
|
||||
File /oname=CrossDesk.exe "..\..\build\windows\x64\release\crossdesk.exe"
|
||||
; Bundle service-side binaries required by the Windows service flow
|
||||
File "..\..\build\windows\x64\release\crossdesk_service.exe"
|
||||
File "..\..\build\windows\x64\release\crossdesk_session_helper.exe"
|
||||
; Bundle runtime DLLs from the release output directory
|
||||
File "..\..\build\windows\x64\release\*.dll"
|
||||
|
||||
Call RegisterInstalledService
|
||||
|
||||
; Write uninstall information
|
||||
WriteUninstaller "$INSTDIR\uninstall.exe"
|
||||
@@ -88,33 +92,23 @@ installApp:
|
||||
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_REG_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
|
||||
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_REG_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
|
||||
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_REG_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
|
||||
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_REG_KEY}" "DisplayIcon" "$INSTDIR\crossdesk.ico"
|
||||
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_REG_KEY}" "DisplayIcon" "$INSTDIR\CrossDesk.exe"
|
||||
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_REG_KEY}" "NoModify" 1
|
||||
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_REG_KEY}" "NoRepair" 1
|
||||
WriteRegStr HKCU "Software\${PRODUCT_NAME}" "InstallDir" "$INSTDIR"
|
||||
SectionEnd
|
||||
|
||||
; After installation
|
||||
Section -Post
|
||||
ExecWait '"C:\Program Files (x86)\Windows Kits\10\bin\10.0.26100.0\x86\mt.exe" -manifest "$INSTDIR\crossdesk.manifest" -outputresource:"$INSTDIR\crossdesk.exe";1'
|
||||
SectionEnd
|
||||
|
||||
Section "Cert"
|
||||
SetOutPath "$APPDATA\CrossDesk\certs"
|
||||
File /r "${CERT_FILE}"
|
||||
SectionEnd
|
||||
|
||||
Section -AdditionalIcons
|
||||
; Desktop shortcut
|
||||
CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\crossdesk.exe" "" "$INSTDIR\crossdesk.ico"
|
||||
CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\CrossDesk.exe" "" "$INSTDIR\CrossDesk.exe"
|
||||
|
||||
; Start menu shortcut
|
||||
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}.lnk" "$INSTDIR\crossdesk.exe" "" "$INSTDIR\crossdesk.ico"
|
||||
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}.lnk" "$INSTDIR\CrossDesk.exe" "" "$INSTDIR\CrossDesk.exe"
|
||||
SectionEnd
|
||||
|
||||
Section "Uninstall"
|
||||
; Check if CrossDesk is running
|
||||
StrCpy $1 "crossdesk.exe"
|
||||
StrCpy $1 "CrossDesk.exe"
|
||||
|
||||
nsProcess::_FindProcess "$1"
|
||||
Pop $R0
|
||||
@@ -136,8 +130,12 @@ cancelUninstall:
|
||||
Abort
|
||||
|
||||
uninstallApp:
|
||||
Call un.UnregisterInstalledService
|
||||
|
||||
; Delete main executable and uninstaller
|
||||
Delete "$INSTDIR\crossdesk.exe"
|
||||
Delete "$INSTDIR\CrossDesk.exe"
|
||||
Delete "$INSTDIR\crossdesk_service.exe"
|
||||
Delete "$INSTDIR\crossdesk_session_helper.exe"
|
||||
Delete "$INSTDIR\uninstall.exe"
|
||||
|
||||
; Recursively delete installation directory
|
||||
@@ -160,5 +158,80 @@ SectionEnd
|
||||
|
||||
; ------ Functions ------
|
||||
Function LaunchApp
|
||||
Exec "$INSTDIR\crossdesk.exe"
|
||||
Exec "$INSTDIR\CrossDesk.exe"
|
||||
FunctionEnd
|
||||
|
||||
Function StopInstalledService
|
||||
IfFileExists "$INSTDIR\CrossDesk.exe" 0 stop_with_sc
|
||||
IfFileExists "$INSTDIR\crossdesk_service.exe" 0 stop_with_sc
|
||||
|
||||
DetailPrint "Stopping existing CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-stop' $0
|
||||
${If} $0 = 0
|
||||
Return
|
||||
${EndIf}
|
||||
|
||||
stop_with_sc:
|
||||
DetailPrint "Stopping existing CrossDesk service via Service Control Manager"
|
||||
ExecWait '"$SYSDIR\sc.exe" stop ${PRODUCT_SERVICE_NAME}' $0
|
||||
${If} $0 != 0
|
||||
${AndIf} $0 != 1060
|
||||
${AndIf} $0 != 1062
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to stop the existing CrossDesk service. The installation will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
Sleep 1500
|
||||
FunctionEnd
|
||||
|
||||
Function RegisterInstalledService
|
||||
IfFileExists "$INSTDIR\CrossDesk.exe" 0 missing_service_binary
|
||||
IfFileExists "$INSTDIR\crossdesk_service.exe" 0 missing_service_binary
|
||||
IfFileExists "$INSTDIR\crossdesk_session_helper.exe" 0 missing_service_binary
|
||||
|
||||
DetailPrint "Registering CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-install' $0
|
||||
${If} $0 != 0
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to register the CrossDesk service. The installation will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
|
||||
DetailPrint "CrossDesk service registered for on-demand start"
|
||||
|
||||
Return
|
||||
|
||||
missing_service_binary:
|
||||
MessageBox MB_ICONSTOP|MB_OK "CrossDesk service files are missing from the installer package. The installation will be aborted."
|
||||
Abort
|
||||
FunctionEnd
|
||||
|
||||
Function un.UnregisterInstalledService
|
||||
IfFileExists "$INSTDIR\CrossDesk.exe" 0 unregister_with_sc
|
||||
|
||||
DetailPrint "Stopping CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-stop' $0
|
||||
${If} $0 = 0
|
||||
DetailPrint "Removing CrossDesk service"
|
||||
ExecWait '"$INSTDIR\CrossDesk.exe" --service-uninstall' $0
|
||||
${If} $0 = 0
|
||||
Return
|
||||
${EndIf}
|
||||
${EndIf}
|
||||
|
||||
unregister_with_sc:
|
||||
DetailPrint "Removing CrossDesk service via Service Control Manager"
|
||||
ExecWait '"$SYSDIR\sc.exe" stop ${PRODUCT_SERVICE_NAME}' $0
|
||||
${If} $0 != 0
|
||||
${AndIf} $0 != 1060
|
||||
${AndIf} $0 != 1062
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to stop the CrossDesk service. Uninstall will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
Sleep 1500
|
||||
|
||||
ExecWait '"$SYSDIR\sc.exe" delete ${PRODUCT_SERVICE_NAME}' $0
|
||||
${If} $0 != 0
|
||||
${AndIf} $0 != 1060
|
||||
MessageBox MB_ICONSTOP|MB_OK "Failed to remove the CrossDesk service. Uninstall will be aborted."
|
||||
Abort
|
||||
${EndIf}
|
||||
FunctionEnd
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Version
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
function Normalize-AppVersion {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$InputVersion
|
||||
)
|
||||
|
||||
$prefix = ""
|
||||
$body = $InputVersion
|
||||
|
||||
if ($body.StartsWith("v")) {
|
||||
$prefix = "v"
|
||||
$body = $body.Substring(1)
|
||||
}
|
||||
|
||||
if ($body -match '^([0-9]+(\.[0-9]+){1,3})-([0-9]{8})-([0-9]+)$') {
|
||||
return "${prefix}$($Matches[1])-$($Matches[4])-$($Matches[3])"
|
||||
}
|
||||
|
||||
return $InputVersion
|
||||
}
|
||||
|
||||
$normalizedVersion = Normalize-AppVersion -InputVersion $Version
|
||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
|
||||
Push-Location $scriptDir
|
||||
try {
|
||||
& makensis "/DVERSION=$normalizedVersion" "nsis_script.nsi"
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
+76
-51
@@ -34,9 +34,16 @@
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
Daemon* Daemon::instance_ = nullptr;
|
||||
volatile std::sig_atomic_t Daemon::stop_requested_ = 0;
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
constexpr int kRestartDelayMs = 1000;
|
||||
#ifndef _WIN32
|
||||
constexpr int kWaitPollIntervalMs = 200;
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
// get executable file path
|
||||
static std::string GetExecutablePath() {
|
||||
#ifdef _WIN32
|
||||
@@ -66,33 +73,35 @@ static std::string GetExecutablePath() {
|
||||
return "";
|
||||
}
|
||||
|
||||
Daemon::Daemon(const std::string& name)
|
||||
: name_(name)
|
||||
#ifdef _WIN32
|
||||
,
|
||||
running_(false)
|
||||
#else
|
||||
,
|
||||
running_(true)
|
||||
Daemon::Daemon(const std::string& name) : name_(name), running_(false) {}
|
||||
|
||||
void Daemon::stop() {
|
||||
running_.store(false);
|
||||
#ifndef _WIN32
|
||||
stop_requested_ = 1;
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
void Daemon::stop() { running_ = false; }
|
||||
|
||||
bool Daemon::isRunning() const { return running_; }
|
||||
bool Daemon::isRunning() const {
|
||||
#ifndef _WIN32
|
||||
return running_.load() && (stop_requested_ == 0);
|
||||
#else
|
||||
return running_.load();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Daemon::start(MainLoopFunc loop) {
|
||||
#ifdef _WIN32
|
||||
running_ = true;
|
||||
running_.store(true);
|
||||
return runWithRestart(loop);
|
||||
#elif __APPLE__
|
||||
// macOS: Use child process monitoring (like Windows) to preserve GUI
|
||||
running_ = true;
|
||||
stop_requested_ = 0;
|
||||
running_.store(true);
|
||||
return runWithRestart(loop);
|
||||
#else
|
||||
// linux: Daemonize first, then run with restart monitoring
|
||||
instance_ = this;
|
||||
stop_requested_ = 0;
|
||||
|
||||
// check if running from terminal before fork
|
||||
bool from_terminal =
|
||||
@@ -134,29 +143,13 @@ bool Daemon::start(MainLoopFunc loop) {
|
||||
}
|
||||
|
||||
// set up signal handlers
|
||||
signal(SIGTERM, [](int) {
|
||||
if (instance_) instance_->stop();
|
||||
});
|
||||
signal(SIGINT, [](int) {
|
||||
if (instance_) instance_->stop();
|
||||
});
|
||||
signal(SIGTERM, [](int) { stop_requested_ = 1; });
|
||||
signal(SIGINT, [](int) { stop_requested_ = 1; });
|
||||
|
||||
// ignore SIGPIPE
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
// set up SIGCHLD handler to reap zombie processes
|
||||
struct sigaction sa_chld;
|
||||
sa_chld.sa_handler = [](int) {
|
||||
// reap zombie processes
|
||||
while (waitpid(-1, nullptr, WNOHANG) > 0) {
|
||||
// continue until no more zombie children
|
||||
}
|
||||
};
|
||||
sigemptyset(&sa_chld.sa_mask);
|
||||
sa_chld.sa_flags = SA_RESTART | SA_NOCLDSTOP;
|
||||
sigaction(SIGCHLD, &sa_chld, nullptr);
|
||||
|
||||
running_ = true;
|
||||
running_.store(true);
|
||||
return runWithRestart(loop);
|
||||
#endif
|
||||
}
|
||||
@@ -204,8 +197,7 @@ bool Daemon::runWithRestart(MainLoopFunc loop) {
|
||||
restart_count++;
|
||||
std::cerr << "Exception caught, restarting... (attempt "
|
||||
<< restart_count << ")" << std::endl;
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(DAEMON_DEFAULT_RESTART_DELAY_MS));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(kRestartDelayMs));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -237,27 +229,41 @@ bool Daemon::runWithRestart(MainLoopFunc loop) {
|
||||
if (!success) {
|
||||
std::cerr << "Failed to create child process, error: " << GetLastError()
|
||||
<< std::endl;
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(DAEMON_DEFAULT_RESTART_DELAY_MS));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(kRestartDelayMs));
|
||||
restart_count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
while (isRunning()) {
|
||||
DWORD wait_result = WaitForSingleObject(pi.hProcess, 200);
|
||||
if (wait_result == WAIT_OBJECT_0) {
|
||||
break;
|
||||
}
|
||||
if (wait_result == WAIT_FAILED) {
|
||||
std::cerr << "Failed waiting child process, error: " << GetLastError()
|
||||
<< std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isRunning()) {
|
||||
TerminateProcess(pi.hProcess, 1);
|
||||
WaitForSingleObject(pi.hProcess, 3000);
|
||||
}
|
||||
|
||||
DWORD exit_code = 0;
|
||||
WaitForSingleObject(pi.hProcess, INFINITE);
|
||||
GetExitCodeProcess(pi.hProcess, &exit_code);
|
||||
CloseHandle(pi.hProcess);
|
||||
CloseHandle(pi.hThread);
|
||||
|
||||
if (exit_code == 0) {
|
||||
if (!isRunning() || exit_code == 0) {
|
||||
break; // normal exit
|
||||
}
|
||||
restart_count++;
|
||||
std::cerr << "Child process exited with code " << exit_code
|
||||
<< ", restarting... (attempt " << restart_count << ")"
|
||||
<< std::endl;
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(DAEMON_DEFAULT_RESTART_DELAY_MS));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(kRestartDelayMs));
|
||||
#else
|
||||
// linux: use fork + exec to create child process
|
||||
pid_t pid = fork();
|
||||
@@ -266,21 +272,39 @@ bool Daemon::runWithRestart(MainLoopFunc loop) {
|
||||
_exit(1); // exec failed
|
||||
} else if (pid > 0) {
|
||||
int status = 0;
|
||||
pid_t waited_pid = waitpid(pid, &status, 0);
|
||||
pid_t waited_pid = -1;
|
||||
while (isRunning()) {
|
||||
waited_pid = waitpid(pid, &status, WNOHANG);
|
||||
if (waited_pid == pid) {
|
||||
break;
|
||||
}
|
||||
if (waited_pid < 0 && errno != EINTR) {
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(kWaitPollIntervalMs));
|
||||
}
|
||||
|
||||
if (!isRunning() && waited_pid != pid) {
|
||||
kill(pid, SIGTERM);
|
||||
waited_pid = waitpid(pid, &status, 0);
|
||||
}
|
||||
|
||||
if (waited_pid < 0) {
|
||||
if (!isRunning()) {
|
||||
break;
|
||||
}
|
||||
restart_count++;
|
||||
std::cerr << "waitpid failed, errno: " << errno
|
||||
<< ", restarting... (attempt " << restart_count << ")"
|
||||
<< std::endl;
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(DAEMON_DEFAULT_RESTART_DELAY_MS));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(kRestartDelayMs));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (WIFEXITED(status)) {
|
||||
int exit_code = WEXITSTATUS(status);
|
||||
if (exit_code == 0) {
|
||||
if (!isRunning() || exit_code == 0) {
|
||||
break; // normal exit
|
||||
}
|
||||
restart_count++;
|
||||
@@ -288,6 +312,9 @@ bool Daemon::runWithRestart(MainLoopFunc loop) {
|
||||
<< ", restarting... (attempt " << restart_count << ")"
|
||||
<< std::endl;
|
||||
} else if (WIFSIGNALED(status)) {
|
||||
if (!isRunning()) {
|
||||
break;
|
||||
}
|
||||
restart_count++;
|
||||
std::cerr << "Child process crashed with signal " << WTERMSIG(status)
|
||||
<< ", restarting... (attempt " << restart_count << ")"
|
||||
@@ -298,12 +325,10 @@ bool Daemon::runWithRestart(MainLoopFunc loop) {
|
||||
"(attempt "
|
||||
<< restart_count << ")" << std::endl;
|
||||
}
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(DAEMON_DEFAULT_RESTART_DELAY_MS));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(kRestartDelayMs));
|
||||
} else {
|
||||
std::cerr << "Failed to fork child process" << std::endl;
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(DAEMON_DEFAULT_RESTART_DELAY_MS));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(kRestartDelayMs));
|
||||
restart_count++;
|
||||
}
|
||||
#endif
|
||||
|
||||
+6
-8
@@ -7,11 +7,11 @@
|
||||
#ifndef _DAEMON_H_
|
||||
#define _DAEMON_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <csignal>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#define DAEMON_DEFAULT_RESTART_DELAY_MS 1000
|
||||
|
||||
class Daemon {
|
||||
public:
|
||||
using MainLoopFunc = std::function<void()>;
|
||||
@@ -28,12 +28,10 @@ class Daemon {
|
||||
std::string name_;
|
||||
bool runWithRestart(MainLoopFunc loop);
|
||||
|
||||
#ifdef _WIN32
|
||||
bool running_;
|
||||
#else
|
||||
static Daemon* instance_;
|
||||
volatile bool running_;
|
||||
#ifndef _WIN32
|
||||
static volatile std::sig_atomic_t stop_requested_;
|
||||
#endif
|
||||
std::atomic<bool> running_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
+169
-4
@@ -7,15 +7,179 @@
|
||||
#endif
|
||||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <cstdio>
|
||||
|
||||
#include "service_host.h"
|
||||
#endif
|
||||
|
||||
#include "config_center.h"
|
||||
#include "daemon.h"
|
||||
#include "path_manager.h"
|
||||
#include "render.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
namespace {
|
||||
|
||||
void EnsureConsoleForCli() {
|
||||
static bool console_ready = false;
|
||||
if (console_ready) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
|
||||
DWORD error = GetLastError();
|
||||
if (error != ERROR_ACCESS_DENIED) {
|
||||
AllocConsole();
|
||||
}
|
||||
}
|
||||
|
||||
FILE* stream = nullptr;
|
||||
freopen_s(&stream, "CONOUT$", "w", stdout);
|
||||
freopen_s(&stream, "CONOUT$", "w", stderr);
|
||||
freopen_s(&stream, "CONIN$", "r", stdin);
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
console_ready = true;
|
||||
}
|
||||
|
||||
void PrintServiceCliUsage() {
|
||||
std::cout
|
||||
<< "CrossDesk service management commands\n"
|
||||
<< " --service-install Install the sibling crossdesk_service.exe\n"
|
||||
<< " --service-uninstall Remove the installed Windows service\n"
|
||||
<< " --service-start Start the Windows service\n"
|
||||
<< " --service-stop Stop the Windows service\n"
|
||||
<< " --service-sas Ask the service to send Secure Attention "
|
||||
"Sequence\n"
|
||||
<< " --service-ping Ping the service over named pipe IPC\n"
|
||||
<< " --service-status Query service runtime status\n"
|
||||
<< " --service-help Show this help\n";
|
||||
}
|
||||
|
||||
std::wstring GetCurrentExecutablePathW() {
|
||||
wchar_t path[MAX_PATH] = {0};
|
||||
DWORD length = GetModuleFileNameW(nullptr, path, MAX_PATH);
|
||||
if (length == 0 || length >= MAX_PATH) {
|
||||
return L"";
|
||||
}
|
||||
return std::wstring(path, length);
|
||||
}
|
||||
|
||||
std::filesystem::path GetSiblingServiceExecutablePath() {
|
||||
std::wstring current_executable = GetCurrentExecutablePathW();
|
||||
if (current_executable.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return std::filesystem::path(current_executable).parent_path() /
|
||||
L"crossdesk_service.exe";
|
||||
}
|
||||
|
||||
bool IsServiceCliCommand(const char* arg) {
|
||||
if (arg == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return std::strcmp(arg, "--service-install") == 0 ||
|
||||
std::strcmp(arg, "--service-uninstall") == 0 ||
|
||||
std::strcmp(arg, "--service-start") == 0 ||
|
||||
std::strcmp(arg, "--service-stop") == 0 ||
|
||||
std::strcmp(arg, "--service-sas") == 0 ||
|
||||
std::strcmp(arg, "--service-ping") == 0 ||
|
||||
std::strcmp(arg, "--service-status") == 0 ||
|
||||
std::strcmp(arg, "--service-help") == 0;
|
||||
}
|
||||
|
||||
void TryStartManagedWindowsService() {
|
||||
std::filesystem::path service_path = GetSiblingServiceExecutablePath();
|
||||
if (service_path.empty() || !std::filesystem::exists(service_path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!crossdesk::IsCrossDeskServiceInstalled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
crossdesk::StartCrossDeskService();
|
||||
}
|
||||
|
||||
int HandleServiceCliCommand(const std::string& command) {
|
||||
EnsureConsoleForCli();
|
||||
|
||||
if (command == "--service-help") {
|
||||
PrintServiceCliUsage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (command == "--service-install") {
|
||||
std::filesystem::path service_path = GetSiblingServiceExecutablePath();
|
||||
if (service_path.empty()) {
|
||||
std::cerr << "Failed to locate crossdesk_service.exe" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (!std::filesystem::exists(service_path)) {
|
||||
std::cerr << "Service binary not found: " << service_path.string()
|
||||
<< std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool success = crossdesk::InstallCrossDeskService(service_path.wstring());
|
||||
std::cout << (success ? "install ok" : "install failed") << std::endl;
|
||||
return success ? 0 : 1;
|
||||
}
|
||||
|
||||
if (command == "--service-uninstall") {
|
||||
bool success = crossdesk::UninstallCrossDeskService();
|
||||
std::cout << (success ? "uninstall ok" : "uninstall failed") << std::endl;
|
||||
return success ? 0 : 1;
|
||||
}
|
||||
|
||||
if (command == "--service-start") {
|
||||
bool success = crossdesk::StartCrossDeskService();
|
||||
std::cout << (success ? "start ok" : "start failed") << std::endl;
|
||||
return success ? 0 : 1;
|
||||
}
|
||||
|
||||
if (command == "--service-stop") {
|
||||
bool success = crossdesk::StopCrossDeskService();
|
||||
std::cout << (success ? "stop ok" : "stop failed") << std::endl;
|
||||
return success ? 0 : 1;
|
||||
}
|
||||
|
||||
if (command == "--service-sas") {
|
||||
std::cout << crossdesk::QueryCrossDeskService("sas") << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (command == "--service-ping") {
|
||||
std::cout << crossdesk::QueryCrossDeskService("ping") << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (command == "--service-status") {
|
||||
std::cout << crossdesk::QueryCrossDeskService("status") << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
PrintServiceCliUsage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
#endif
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
#ifdef _WIN32
|
||||
if (argc > 1 && IsServiceCliCommand(argv[1])) {
|
||||
return HandleServiceCliCommand(argv[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
// check if running as child process
|
||||
bool is_child = false;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
@@ -32,14 +196,15 @@ int main(int argc, char* argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TryStartManagedWindowsService();
|
||||
#endif
|
||||
|
||||
bool enable_daemon = false;
|
||||
auto path_manager = std::make_unique<crossdesk::PathManager>("CrossDesk");
|
||||
if (path_manager) {
|
||||
std::string cert_path =
|
||||
(path_manager->GetCertPath() / "crossdesk.cn_root.crt").string();
|
||||
std::string cache_path = path_manager->GetCachePath().string();
|
||||
crossdesk::ConfigCenter config_center(cache_path + "/config.ini",
|
||||
cert_path);
|
||||
crossdesk::ConfigCenter config_center(cache_path + "/config.ini");
|
||||
enable_daemon = config_center.IsEnableDaemon();
|
||||
}
|
||||
|
||||
|
||||
+26
-2
@@ -1,5 +1,8 @@
|
||||
#include "platform.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -108,7 +111,7 @@ std::string GetHostName() {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||
std::cerr << "WSAStartup failed." << std::endl;
|
||||
LOG_ERROR("WSAStartup failed");
|
||||
return "";
|
||||
}
|
||||
if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR) {
|
||||
@@ -125,4 +128,25 @@ std::string GetHostName() {
|
||||
#endif
|
||||
return hostname;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
bool IsWaylandSession() {
|
||||
#if defined(__linux__) && !defined(__APPLE__)
|
||||
const char* session_type = std::getenv("XDG_SESSION_TYPE");
|
||||
if (session_type) {
|
||||
if (std::strcmp(session_type, "wayland") == 0 ||
|
||||
std::strcmp(session_type, "Wayland") == 0) {
|
||||
return true;
|
||||
}
|
||||
if (std::strcmp(session_type, "x11") == 0 ||
|
||||
std::strcmp(session_type, "X11") == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const char* wayland_display = std::getenv("WAYLAND_DISPLAY");
|
||||
return wayland_display && wayland_display[0] != '\0';
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace crossdesk {
|
||||
|
||||
std::string GetMac();
|
||||
std::string GetHostName();
|
||||
bool IsWaylandSession();
|
||||
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "rounded_corner_button.h"
|
||||
|
||||
namespace crossdesk {
|
||||
bool RoundedCornerButton(const char* label, const ImVec2& size, float rounding,
|
||||
ImDrawFlags round_flags, bool enabled,
|
||||
ImU32 normal_col, ImU32 hover_col, ImU32 active_col,
|
||||
ImU32 border_col) {
|
||||
ImGuiWindow* current_window = ImGui::GetCurrentWindow();
|
||||
if (current_window->SkipItems) return false;
|
||||
|
||||
const ImGuiStyle& style = ImGui::GetStyle();
|
||||
|
||||
ImGuiID button_id = current_window->GetID(label);
|
||||
ImVec2 cursor_pos = current_window->DC.CursorPos;
|
||||
ImVec2 button_size = ImGui::CalcItemSize(size, 0.0f, 0.0f);
|
||||
ImRect button_rect(cursor_pos, ImVec2(cursor_pos.x + button_size.x,
|
||||
cursor_pos.y + button_size.y));
|
||||
ImGui::ItemSize(button_rect);
|
||||
if (!ImGui::ItemAdd(button_rect, button_id)) return false;
|
||||
|
||||
bool is_hovered = false, is_held = false;
|
||||
bool is_pressed = false;
|
||||
if (enabled) {
|
||||
is_pressed =
|
||||
ImGui::ButtonBehavior(button_rect, button_id, &is_hovered, &is_held);
|
||||
}
|
||||
|
||||
if (normal_col == 0) normal_col = ImGui::GetColorU32(ImGuiCol_Button);
|
||||
if (hover_col == 0) hover_col = ImGui::GetColorU32(ImGuiCol_ButtonHovered);
|
||||
if (active_col == 0) active_col = ImGui::GetColorU32(ImGuiCol_ButtonActive);
|
||||
if (border_col == 0) border_col = ImGui::GetColorU32(ImGuiCol_Border);
|
||||
|
||||
ImU32 fill_color = normal_col;
|
||||
if (is_held && is_hovered)
|
||||
fill_color = active_col;
|
||||
else if (is_hovered)
|
||||
fill_color = hover_col;
|
||||
|
||||
if (!enabled) fill_color = IM_COL32(120, 120, 120, 180);
|
||||
|
||||
ImDrawList* window_draw_list = ImGui::GetWindowDrawList();
|
||||
|
||||
window_draw_list->AddRectFilled(button_rect.Min, button_rect.Max, fill_color,
|
||||
rounding, round_flags);
|
||||
|
||||
if (style.FrameBorderSize > 0.0f) {
|
||||
window_draw_list->AddRect(button_rect.Min, button_rect.Max, border_col,
|
||||
rounding, round_flags, style.FrameBorderSize);
|
||||
}
|
||||
|
||||
ImU32 text_color =
|
||||
ImGui::GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled);
|
||||
|
||||
const char* label_end = ImGui::FindRenderedTextEnd(label);
|
||||
ImGui::PushStyleColor(ImGuiCol_Text,
|
||||
ImGui::ColorConvertU32ToFloat4(text_color));
|
||||
ImGui::RenderTextClipped(button_rect.Min, button_rect.Max, label, label_end,
|
||||
nullptr, ImVec2(0.5f, 0.5f), &button_rect);
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
return is_pressed;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-02-26
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _ROUNDED_CORNER_BUTTON_H_
|
||||
#define _ROUNDED_CORNER_BUTTON_H_
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
|
||||
namespace crossdesk {
|
||||
bool RoundedCornerButton(const char* label, const ImVec2& size, float rounding,
|
||||
ImDrawFlags round_flags, bool enabled = true,
|
||||
ImU32 normal_col = 0, ImU32 hover_col = 0,
|
||||
ImU32 active_col = 0, ImU32 border_col = 0);
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,279 @@
|
||||
#include "wayland_portal_shared.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
#include <dbus/dbus.h>
|
||||
#endif
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
std::mutex& SharedSessionMutex() {
|
||||
static std::mutex mutex;
|
||||
return mutex;
|
||||
}
|
||||
|
||||
SharedWaylandPortalSessionInfo& SharedSessionInfo() {
|
||||
static SharedWaylandPortalSessionInfo info;
|
||||
return info;
|
||||
}
|
||||
|
||||
bool& SharedSessionActive() {
|
||||
static bool active = false;
|
||||
return active;
|
||||
}
|
||||
|
||||
int& SharedSessionRefs() {
|
||||
static int refs = 0;
|
||||
return refs;
|
||||
}
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
constexpr const char* kPortalBusName = "org.freedesktop.portal.Desktop";
|
||||
constexpr const char* kPortalSessionInterface =
|
||||
"org.freedesktop.portal.Session";
|
||||
constexpr int kPortalCloseWaitMs = 100;
|
||||
|
||||
void LogCloseDbusError(const char* action, DBusError* error) {
|
||||
if (error && dbus_error_is_set(error)) {
|
||||
LOG_ERROR("{} failed: {} ({})", action,
|
||||
error->message ? error->message : "unknown",
|
||||
error->name ? error->name : "unknown");
|
||||
} else {
|
||||
LOG_ERROR("{} failed", action);
|
||||
}
|
||||
}
|
||||
|
||||
struct SessionClosedState {
|
||||
std::string session_handle;
|
||||
bool received = false;
|
||||
};
|
||||
|
||||
DBusHandlerResult HandleSessionClosedSignal(DBusConnection* connection,
|
||||
DBusMessage* message,
|
||||
void* user_data) {
|
||||
(void)connection;
|
||||
auto* state = static_cast<SessionClosedState*>(user_data);
|
||||
if (!state || !message) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
if (!dbus_message_is_signal(message, kPortalSessionInterface, "Closed")) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
const char* path = dbus_message_get_path(message);
|
||||
if (!path || state->session_handle != path) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
state->received = true;
|
||||
return DBUS_HANDLER_RESULT_HANDLED;
|
||||
}
|
||||
|
||||
bool BeginSessionClosedWatch(DBusConnection* connection,
|
||||
const std::string& session_handle,
|
||||
SessionClosedState* state,
|
||||
std::string* match_rule_out) {
|
||||
if (!connection || session_handle.empty() || !state || !match_rule_out) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state->session_handle = session_handle;
|
||||
state->received = false;
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
const std::string match_rule =
|
||||
"type='signal',interface='" + std::string(kPortalSessionInterface) +
|
||||
"',member='Closed',path='" + session_handle + "'";
|
||||
dbus_bus_add_match(connection, match_rule.c_str(), &error);
|
||||
if (dbus_error_is_set(&error)) {
|
||||
LogCloseDbusError("dbus_bus_add_match(Session.Closed)", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_connection_add_filter(connection, HandleSessionClosedSignal, state,
|
||||
nullptr);
|
||||
*match_rule_out = match_rule;
|
||||
return true;
|
||||
}
|
||||
|
||||
void EndSessionClosedWatch(DBusConnection* connection, SessionClosedState* state,
|
||||
const std::string& match_rule) {
|
||||
if (!connection || !state || match_rule.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
dbus_connection_remove_filter(connection, HandleSessionClosedSignal, state);
|
||||
|
||||
DBusError remove_error;
|
||||
dbus_error_init(&remove_error);
|
||||
dbus_bus_remove_match(connection, match_rule.c_str(), &remove_error);
|
||||
if (dbus_error_is_set(&remove_error)) {
|
||||
dbus_error_free(&remove_error);
|
||||
}
|
||||
}
|
||||
|
||||
void WaitForSessionClosed(DBusConnection* connection, SessionClosedState* state,
|
||||
int timeout_ms = kPortalCloseWaitMs) {
|
||||
if (!connection || !state) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
while (!state->received && std::chrono::steady_clock::now() < deadline) {
|
||||
dbus_connection_read_write(connection, 100);
|
||||
while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS) {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
bool PublishSharedWaylandPortalSession(
|
||||
const SharedWaylandPortalSessionInfo& info) {
|
||||
if (!info.connection || info.session_handle.empty() || info.stream_id == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(SharedSessionMutex());
|
||||
if (SharedSessionActive()) {
|
||||
const auto& active_info = SharedSessionInfo();
|
||||
if (active_info.session_handle != info.session_handle &&
|
||||
SharedSessionRefs() > 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const bool same_session =
|
||||
SharedSessionActive() &&
|
||||
SharedSessionInfo().session_handle == info.session_handle;
|
||||
SharedSessionInfo() = info;
|
||||
SharedSessionActive() = true;
|
||||
if (!same_session || SharedSessionRefs() <= 0) {
|
||||
SharedSessionRefs() = 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AcquireSharedWaylandPortalSession(bool require_pointer,
|
||||
SharedWaylandPortalSessionInfo* out) {
|
||||
if (!out) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(SharedSessionMutex());
|
||||
if (!SharedSessionActive()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto& info = SharedSessionInfo();
|
||||
if (require_pointer && !info.pointer_granted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
++SharedSessionRefs();
|
||||
*out = info;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReleaseSharedWaylandPortalSession(DBusConnection** connection_out,
|
||||
std::string* session_handle_out) {
|
||||
if (connection_out) {
|
||||
*connection_out = nullptr;
|
||||
}
|
||||
if (session_handle_out) {
|
||||
session_handle_out->clear();
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(SharedSessionMutex());
|
||||
if (!SharedSessionActive()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (SharedSessionRefs() > 0) {
|
||||
--SharedSessionRefs();
|
||||
}
|
||||
|
||||
if (SharedSessionRefs() > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (connection_out) {
|
||||
*connection_out = SharedSessionInfo().connection;
|
||||
}
|
||||
if (session_handle_out) {
|
||||
*session_handle_out = SharedSessionInfo().session_handle;
|
||||
}
|
||||
|
||||
SharedSessionInfo() = SharedWaylandPortalSessionInfo{};
|
||||
SharedSessionActive() = false;
|
||||
SharedSessionRefs() = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void CloseWaylandPortalSessionAndConnection(DBusConnection* connection,
|
||||
const std::string& session_handle,
|
||||
const char* close_action) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
if (!connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!session_handle.empty()) {
|
||||
SessionClosedState close_state;
|
||||
std::string close_match_rule;
|
||||
const bool watching_closed = BeginSessionClosedWatch(
|
||||
connection, session_handle, &close_state, &close_match_rule);
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, session_handle.c_str(), kPortalSessionInterface,
|
||||
"Close");
|
||||
if (message) {
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
|
||||
connection, message, 1000, &error);
|
||||
if (!reply && dbus_error_is_set(&error)) {
|
||||
LogCloseDbusError(close_action, &error);
|
||||
dbus_error_free(&error);
|
||||
}
|
||||
if (reply) {
|
||||
dbus_message_unref(reply);
|
||||
}
|
||||
dbus_message_unref(message);
|
||||
}
|
||||
|
||||
if (watching_closed) {
|
||||
WaitForSessionClosed(connection, &close_state);
|
||||
if (!close_state.received) {
|
||||
LOG_WARN("Timed out waiting for portal session to close: {}",
|
||||
session_handle);
|
||||
LOG_WARN("Forcing local teardown without waiting for Session.Closed: {}",
|
||||
session_handle);
|
||||
EndSessionClosedWatch(connection, &close_state, close_match_rule);
|
||||
} else {
|
||||
EndSessionClosedWatch(connection, &close_state, close_match_rule);
|
||||
LOG_INFO("Portal session closed: {}", session_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbus_connection_close(connection);
|
||||
dbus_connection_unref(connection);
|
||||
#else
|
||||
(void)connection;
|
||||
(void)session_handle;
|
||||
(void)close_action;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Shared Wayland portal session state used by the Linux Wayland capturer and
|
||||
* mouse controller so they can reuse one RemoteDesktop session.
|
||||
*/
|
||||
|
||||
#ifndef _WAYLAND_PORTAL_SHARED_H_
|
||||
#define _WAYLAND_PORTAL_SHARED_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
struct DBusConnection;
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
struct SharedWaylandPortalSessionInfo {
|
||||
DBusConnection* connection = nullptr;
|
||||
std::string session_handle;
|
||||
uint32_t stream_id = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
bool pointer_granted = false;
|
||||
};
|
||||
|
||||
bool PublishSharedWaylandPortalSession(
|
||||
const SharedWaylandPortalSessionInfo& info);
|
||||
bool AcquireSharedWaylandPortalSession(bool require_pointer,
|
||||
SharedWaylandPortalSessionInfo* out);
|
||||
bool ReleaseSharedWaylandPortalSession(DBusConnection** connection_out,
|
||||
std::string* session_handle_out);
|
||||
void CloseWaylandPortalSessionAndConnection(DBusConnection* connection,
|
||||
const std::string& session_handle,
|
||||
const char* close_action);
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-01-20
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _WINDOW_UTIL_MAC_H_
|
||||
#define _WINDOW_UTIL_MAC_H_
|
||||
|
||||
struct SDL_Window;
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
// Best-effort: keep an SDL window above normal windows on macOS.
|
||||
// No-op on non-macOS builds.
|
||||
void MacSetWindowAlwaysOnTop(::SDL_Window* window, bool always_on_top);
|
||||
|
||||
// Best-effort: exclude an SDL window from the Window menu and window cycling.
|
||||
// Note: Cmd-Tab switches apps (not individual windows), so this primarily
|
||||
// affects the Window menu and Cmd-` window cycling.
|
||||
void MacSetWindowExcludedFromWindowMenu(::SDL_Window* window, bool excluded);
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "window_util_mac.h"
|
||||
|
||||
#if defined(__APPLE__)
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
static NSWindow* GetNSWindowFromSDL(::SDL_Window* window) {
|
||||
if (!window) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
#if !defined(SDL_PROP_WINDOW_COCOA_WINDOW_POINTER)
|
||||
return nil;
|
||||
#else
|
||||
SDL_PropertiesID props = SDL_GetWindowProperties(window);
|
||||
void* cocoa_window_ptr =
|
||||
SDL_GetPointerProperty(props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL);
|
||||
if (!cocoa_window_ptr) {
|
||||
return nil;
|
||||
}
|
||||
return (__bridge NSWindow*)cocoa_window_ptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MacSetWindowAlwaysOnTop(::SDL_Window* window, bool always_on_top) {
|
||||
NSWindow* ns_window = GetNSWindowFromSDL(window);
|
||||
if (!ns_window) {
|
||||
(void)always_on_top;
|
||||
return;
|
||||
}
|
||||
|
||||
// Keep above normal windows.
|
||||
const NSInteger level = always_on_top ? NSFloatingWindowLevel : NSNormalWindowLevel;
|
||||
[ns_window setLevel:level];
|
||||
|
||||
// Optional: keep visible across Spaces/fullscreen. Safe as best-effort.
|
||||
NSWindowCollectionBehavior behavior = [ns_window collectionBehavior];
|
||||
behavior |= NSWindowCollectionBehaviorCanJoinAllSpaces;
|
||||
behavior |= NSWindowCollectionBehaviorFullScreenAuxiliary;
|
||||
[ns_window setCollectionBehavior:behavior];
|
||||
}
|
||||
|
||||
void MacSetWindowExcludedFromWindowMenu(::SDL_Window* window, bool excluded) {
|
||||
NSWindow* ns_window = GetNSWindowFromSDL(window);
|
||||
if (!ns_window) {
|
||||
(void)excluded;
|
||||
return;
|
||||
}
|
||||
|
||||
[ns_window setExcludedFromWindowsMenu:excluded];
|
||||
|
||||
NSWindowCollectionBehavior behavior = [ns_window collectionBehavior];
|
||||
behavior |= NSWindowCollectionBehaviorIgnoresCycle;
|
||||
behavior |= NSWindowCollectionBehaviorTransient;
|
||||
[ns_window setCollectionBehavior:behavior];
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif // __APPLE__
|
||||
@@ -5,11 +5,8 @@
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
ConfigCenter::ConfigCenter(const std::string& config_path,
|
||||
const std::string& cert_file_path)
|
||||
: config_path_(config_path),
|
||||
cert_file_path_(cert_file_path),
|
||||
cert_file_path_default_(cert_file_path) {
|
||||
ConfigCenter::ConfigCenter(const std::string& config_path)
|
||||
: config_path_(config_path) {
|
||||
ini_.SetUnicode(true);
|
||||
Load();
|
||||
}
|
||||
@@ -23,8 +20,14 @@ int ConfigCenter::Load() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
language_ = static_cast<LANGUAGE>(
|
||||
ini_.GetLongValue(section_, "language", static_cast<long>(language_)));
|
||||
const long language_value =
|
||||
ini_.GetLongValue(section_, "language", static_cast<long>(language_));
|
||||
if (language_value < static_cast<long>(LANGUAGE::CHINESE) ||
|
||||
language_value > static_cast<long>(LANGUAGE::RUSSIAN)) {
|
||||
language_ = LANGUAGE::ENGLISH;
|
||||
} else {
|
||||
language_ = static_cast<LANGUAGE>(language_value);
|
||||
}
|
||||
|
||||
video_quality_ = static_cast<VIDEO_QUALITY>(ini_.GetLongValue(
|
||||
section_, "video_quality", static_cast<long>(video_quality_)));
|
||||
@@ -70,77 +73,24 @@ int ConfigCenter::Load() {
|
||||
} else {
|
||||
coturn_server_port_ = 0;
|
||||
}
|
||||
const char* cert_file_path_value =
|
||||
ini_.GetValue(section_, "cert_file_path", nullptr);
|
||||
if (cert_file_path_value != nullptr && strlen(cert_file_path_value) > 0) {
|
||||
cert_file_path_ = cert_file_path_value;
|
||||
} else {
|
||||
cert_file_path_ = "";
|
||||
}
|
||||
const char* cert_fingerprint_value =
|
||||
ini_.GetValue(section_, "cert_fingerprint", nullptr);
|
||||
if (cert_fingerprint_value != nullptr && strlen(cert_fingerprint_value) > 0) {
|
||||
cert_fingerprint_ = cert_fingerprint_value;
|
||||
} else {
|
||||
cert_fingerprint_ = "";
|
||||
}
|
||||
const char* cert_fingerprint_server_host_value =
|
||||
ini_.GetValue(section_, "cert_fingerprint_server_host", nullptr);
|
||||
if (cert_fingerprint_server_host_value != nullptr &&
|
||||
strlen(cert_fingerprint_server_host_value) > 0) {
|
||||
cert_fingerprint_server_host_ = cert_fingerprint_server_host_value;
|
||||
} else {
|
||||
cert_fingerprint_server_host_ = "";
|
||||
}
|
||||
|
||||
const char* default_cert_fingerprint_value =
|
||||
ini_.GetValue(section_, "default_cert_fingerprint", nullptr);
|
||||
if (default_cert_fingerprint_value != nullptr &&
|
||||
strlen(default_cert_fingerprint_value) > 0) {
|
||||
default_cert_fingerprint_ = default_cert_fingerprint_value;
|
||||
} else {
|
||||
default_cert_fingerprint_ = "";
|
||||
}
|
||||
const char* default_cert_fingerprint_server_host_value =
|
||||
ini_.GetValue(section_, "default_cert_fingerprint_server_host", nullptr);
|
||||
if (default_cert_fingerprint_server_host_value != nullptr &&
|
||||
strlen(default_cert_fingerprint_server_host_value) > 0) {
|
||||
default_cert_fingerprint_server_host_ =
|
||||
default_cert_fingerprint_server_host_value;
|
||||
} else {
|
||||
default_cert_fingerprint_server_host_ = "";
|
||||
}
|
||||
|
||||
if (enable_self_hosted_ && !cert_fingerprint_.empty() &&
|
||||
!cert_fingerprint_server_host_.empty() &&
|
||||
signal_server_host_ != cert_fingerprint_server_host_) {
|
||||
LOG_INFO("Server IP changed from {} to {}, clearing old fingerprint",
|
||||
cert_fingerprint_server_host_, signal_server_host_);
|
||||
cert_fingerprint_.clear();
|
||||
cert_fingerprint_server_host_.clear();
|
||||
ini_.Delete(section_, "cert_fingerprint", false);
|
||||
ini_.Delete(section_, "cert_fingerprint_server_host", false);
|
||||
ini_.SaveFile(config_path_.c_str());
|
||||
}
|
||||
|
||||
if (!enable_self_hosted_ && !default_cert_fingerprint_.empty() &&
|
||||
!default_cert_fingerprint_server_host_.empty() &&
|
||||
signal_server_host_default_ != default_cert_fingerprint_server_host_) {
|
||||
LOG_INFO(
|
||||
"Default server IP changed from {} to {}, clearing old fingerprint",
|
||||
default_cert_fingerprint_server_host_, signal_server_host_default_);
|
||||
default_cert_fingerprint_.clear();
|
||||
default_cert_fingerprint_server_host_.clear();
|
||||
ini_.Delete(section_, "default_cert_fingerprint", false);
|
||||
ini_.Delete(section_, "default_cert_fingerprint_server_host", false);
|
||||
ini_.SaveFile(config_path_.c_str());
|
||||
}
|
||||
|
||||
enable_autostart_ =
|
||||
ini_.GetBoolValue(section_, "enable_autostart", enable_autostart_);
|
||||
enable_daemon_ = ini_.GetBoolValue(section_, "enable_daemon", enable_daemon_);
|
||||
enable_minimize_to_tray_ = ini_.GetBoolValue(
|
||||
section_, "enable_minimize_to_tray", enable_minimize_to_tray_);
|
||||
portable_service_prompt_suppressed_ =
|
||||
ini_.GetBoolValue(section_, "portable_service_prompt_suppressed",
|
||||
portable_service_prompt_suppressed_);
|
||||
|
||||
const char* file_transfer_save_path_value =
|
||||
ini_.GetValue(section_, "file_transfer_save_path", nullptr);
|
||||
if (file_transfer_save_path_value != nullptr &&
|
||||
strlen(file_transfer_save_path_value) > 0) {
|
||||
file_transfer_save_path_ = file_transfer_save_path_value;
|
||||
} else {
|
||||
file_transfer_save_path_ = "";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -165,25 +115,17 @@ int ConfigCenter::Save() {
|
||||
static_cast<long>(signal_server_port_));
|
||||
ini_.SetLongValue(section_, "coturn_server_port",
|
||||
static_cast<long>(coturn_server_port_));
|
||||
ini_.SetValue(section_, "cert_file_path", cert_file_path_.c_str());
|
||||
if (!cert_fingerprint_.empty()) {
|
||||
ini_.SetValue(section_, "cert_fingerprint", cert_fingerprint_.c_str());
|
||||
ini_.SetValue(section_, "cert_fingerprint_server_host",
|
||||
cert_fingerprint_server_host_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
if (!default_cert_fingerprint_.empty()) {
|
||||
ini_.SetValue(section_, "default_cert_fingerprint",
|
||||
default_cert_fingerprint_.c_str());
|
||||
ini_.SetValue(section_, "default_cert_fingerprint_server_host",
|
||||
default_cert_fingerprint_server_host_.c_str());
|
||||
}
|
||||
|
||||
ini_.SetBoolValue(section_, "enable_autostart", enable_autostart_);
|
||||
ini_.SetBoolValue(section_, "enable_daemon", enable_daemon_);
|
||||
ini_.SetBoolValue(section_, "enable_minimize_to_tray",
|
||||
enable_minimize_to_tray_);
|
||||
ini_.SetBoolValue(section_, "portable_service_prompt_suppressed",
|
||||
portable_service_prompt_suppressed_);
|
||||
|
||||
ini_.SetValue(section_, "file_transfer_save_path",
|
||||
file_transfer_save_path_.c_str());
|
||||
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
@@ -270,15 +212,6 @@ int ConfigCenter::SetSrtp(bool enable_srtp) {
|
||||
}
|
||||
|
||||
int ConfigCenter::SetServerHost(const std::string& signal_server_host) {
|
||||
if (enable_self_hosted_ && !cert_fingerprint_.empty() &&
|
||||
signal_server_host != signal_server_host_) {
|
||||
LOG_INFO("Server IP changed from {} to {}, clearing old fingerprint",
|
||||
signal_server_host_, signal_server_host);
|
||||
cert_fingerprint_.clear();
|
||||
cert_fingerprint_server_host_.clear();
|
||||
ini_.Delete(section_, "cert_fingerprint", false);
|
||||
ini_.Delete(section_, "cert_fingerprint_server_host", false);
|
||||
}
|
||||
signal_server_host_ = signal_server_host;
|
||||
ini_.SetValue(section_, "signal_server_host", signal_server_host_.c_str());
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
@@ -310,67 +243,6 @@ int ConfigCenter::SetCoturnServerPort(int coturn_server_port) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::SetCertFilePath(const std::string& cert_file_path) {
|
||||
cert_file_path_ = cert_file_path;
|
||||
ini_.SetValue(section_, "cert_file_path", cert_file_path_.c_str());
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::SetCertFingerprint(const std::string& fingerprint) {
|
||||
cert_fingerprint_ = fingerprint;
|
||||
cert_fingerprint_server_host_ = signal_server_host_;
|
||||
ini_.SetValue(section_, "cert_fingerprint", cert_fingerprint_.c_str());
|
||||
ini_.SetValue(section_, "cert_fingerprint_server_host",
|
||||
cert_fingerprint_server_host_.c_str());
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::SetDefaultCertFingerprint(const std::string& fingerprint) {
|
||||
default_cert_fingerprint_ = fingerprint;
|
||||
default_cert_fingerprint_server_host_ = signal_server_host_default_;
|
||||
ini_.SetValue(section_, "default_cert_fingerprint",
|
||||
default_cert_fingerprint_.c_str());
|
||||
ini_.SetValue(section_, "default_cert_fingerprint_server_host",
|
||||
default_cert_fingerprint_server_host_.c_str());
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::ClearCertFingerprint() {
|
||||
cert_fingerprint_.clear();
|
||||
cert_fingerprint_server_host_.clear();
|
||||
ini_.Delete(section_, "cert_fingerprint", false);
|
||||
ini_.Delete(section_, "cert_fingerprint_server_host", false);
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::ClearDefaultCertFingerprint() {
|
||||
default_cert_fingerprint_.clear();
|
||||
default_cert_fingerprint_server_host_.clear();
|
||||
ini_.Delete(section_, "default_cert_fingerprint", false);
|
||||
ini_.Delete(section_, "default_cert_fingerprint_server_host", false);
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::SetSelfHosted(bool enable_self_hosted) {
|
||||
enable_self_hosted_ = enable_self_hosted;
|
||||
ini_.SetBoolValue(section_, "enable_self_hosted", enable_self_hosted_);
|
||||
@@ -397,45 +269,12 @@ int ConfigCenter::SetSelfHosted(bool enable_self_hosted) {
|
||||
coturn_server_port_ = static_cast<int>(
|
||||
ini_.GetLongValue(section_, "coturn_server_port", 0));
|
||||
}
|
||||
const char* cert_file_path_value =
|
||||
ini_.GetValue(section_, "cert_file_path", nullptr);
|
||||
if (cert_file_path_value != nullptr && strlen(cert_file_path_value) > 0) {
|
||||
cert_file_path_ = cert_file_path_value;
|
||||
}
|
||||
const char* cert_fingerprint_value =
|
||||
ini_.GetValue(section_, "cert_fingerprint", nullptr);
|
||||
if (cert_fingerprint_value != nullptr &&
|
||||
strlen(cert_fingerprint_value) > 0) {
|
||||
cert_fingerprint_ = cert_fingerprint_value;
|
||||
}
|
||||
const char* cert_fingerprint_server_host_value =
|
||||
ini_.GetValue(section_, "cert_fingerprint_server_host", nullptr);
|
||||
if (cert_fingerprint_server_host_value != nullptr &&
|
||||
strlen(cert_fingerprint_server_host_value) > 0) {
|
||||
cert_fingerprint_server_host_ = cert_fingerprint_server_host_value;
|
||||
}
|
||||
|
||||
if (!cert_fingerprint_.empty() && !cert_fingerprint_server_host_.empty() &&
|
||||
signal_server_host_ != cert_fingerprint_server_host_) {
|
||||
LOG_INFO("Server IP changed from {} to {}, clearing old fingerprint",
|
||||
cert_fingerprint_server_host_, signal_server_host_);
|
||||
cert_fingerprint_.clear();
|
||||
cert_fingerprint_server_host_.clear();
|
||||
ini_.Delete(section_, "cert_fingerprint", false);
|
||||
ini_.Delete(section_, "cert_fingerprint_server_host", false);
|
||||
}
|
||||
|
||||
ini_.SetValue(section_, "signal_server_host", signal_server_host_.c_str());
|
||||
ini_.SetLongValue(section_, "signal_server_port",
|
||||
static_cast<long>(signal_server_port_));
|
||||
ini_.SetLongValue(section_, "coturn_server_port",
|
||||
static_cast<long>(coturn_server_port_));
|
||||
ini_.SetValue(section_, "cert_file_path", cert_file_path_.c_str());
|
||||
if (!cert_fingerprint_.empty()) {
|
||||
ini_.SetValue(section_, "cert_fingerprint", cert_fingerprint_.c_str());
|
||||
ini_.SetValue(section_, "cert_fingerprint_server_host",
|
||||
cert_fingerprint_server_host_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
@@ -491,6 +330,18 @@ int ConfigCenter::SetDaemon(bool enable_daemon) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigCenter::SetPortableServicePromptSuppressed(bool suppressed) {
|
||||
portable_service_prompt_suppressed_ = suppressed;
|
||||
ini_.SetBoolValue(section_, "portable_service_prompt_suppressed",
|
||||
portable_service_prompt_suppressed_);
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// getters
|
||||
|
||||
ConfigCenter::LANGUAGE ConfigCenter::GetLanguage() const { return language_; }
|
||||
@@ -523,16 +374,6 @@ int ConfigCenter::GetSignalServerPort() const { return signal_server_port_; }
|
||||
|
||||
int ConfigCenter::GetCoturnServerPort() const { return coturn_server_port_; }
|
||||
|
||||
std::string ConfigCenter::GetCertFilePath() const { return cert_file_path_; }
|
||||
|
||||
std::string ConfigCenter::GetCertFingerprint() const {
|
||||
return cert_fingerprint_;
|
||||
}
|
||||
|
||||
std::string ConfigCenter::GetDefaultCertFingerprint() const {
|
||||
return default_cert_fingerprint_;
|
||||
}
|
||||
|
||||
std::string ConfigCenter::GetDefaultServerHost() const {
|
||||
return signal_server_host_default_;
|
||||
}
|
||||
@@ -545,10 +386,6 @@ int ConfigCenter::GetDefaultCoturnServerPort() const {
|
||||
return coturn_server_port_default_;
|
||||
}
|
||||
|
||||
std::string ConfigCenter::GetDefaultCertFilePath() const {
|
||||
return cert_file_path_default_;
|
||||
}
|
||||
|
||||
bool ConfigCenter::IsSelfHosted() const { return enable_self_hosted_; }
|
||||
|
||||
bool ConfigCenter::IsMinimizeToTray() const { return enable_minimize_to_tray_; }
|
||||
@@ -556,4 +393,23 @@ bool ConfigCenter::IsMinimizeToTray() const { return enable_minimize_to_tray_; }
|
||||
bool ConfigCenter::IsEnableAutostart() const { return enable_autostart_; }
|
||||
|
||||
bool ConfigCenter::IsEnableDaemon() const { return enable_daemon_; }
|
||||
} // namespace crossdesk
|
||||
|
||||
bool ConfigCenter::IsPortableServicePromptSuppressed() const {
|
||||
return portable_service_prompt_suppressed_;
|
||||
}
|
||||
|
||||
int ConfigCenter::SetFileTransferSavePath(const std::string& path) {
|
||||
file_transfer_save_path_ = path;
|
||||
ini_.SetValue(section_, "file_transfer_save_path",
|
||||
file_transfer_save_path_.c_str());
|
||||
SI_Error rc = ini_.SaveFile(config_path_.c_str());
|
||||
if (rc < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string ConfigCenter::GetFileTransferSavePath() const {
|
||||
return file_transfer_save_path_;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -15,15 +15,13 @@ namespace crossdesk {
|
||||
|
||||
class ConfigCenter {
|
||||
public:
|
||||
enum class LANGUAGE { CHINESE = 0, ENGLISH = 1 };
|
||||
enum class LANGUAGE { CHINESE = 0, ENGLISH = 1, RUSSIAN = 2 };
|
||||
enum class VIDEO_QUALITY { LOW = 0, MEDIUM = 1, HIGH = 2 };
|
||||
enum class VIDEO_FRAME_RATE { FPS_30 = 0, FPS_60 = 1 };
|
||||
enum class VIDEO_ENCODE_FORMAT { H264 = 0, AV1 = 1 };
|
||||
|
||||
public:
|
||||
explicit ConfigCenter(
|
||||
const std::string& config_path = "config.ini",
|
||||
const std::string& cert_file_path = "crossdesk.cn_root.crt");
|
||||
explicit ConfigCenter(const std::string& config_path = "config.ini");
|
||||
~ConfigCenter();
|
||||
|
||||
// write config
|
||||
@@ -37,15 +35,12 @@ class ConfigCenter {
|
||||
int SetServerHost(const std::string& signal_server_host);
|
||||
int SetServerPort(int signal_server_port);
|
||||
int SetCoturnServerPort(int coturn_server_port);
|
||||
int SetCertFilePath(const std::string& cert_file_path);
|
||||
int SetCertFingerprint(const std::string& fingerprint);
|
||||
int SetDefaultCertFingerprint(const std::string& fingerprint);
|
||||
int ClearCertFingerprint();
|
||||
int ClearDefaultCertFingerprint();
|
||||
int SetSelfHosted(bool enable_self_hosted);
|
||||
int SetMinimizeToTray(bool enable_minimize_to_tray);
|
||||
int SetAutostart(bool enable_autostart);
|
||||
int SetDaemon(bool enable_daemon);
|
||||
int SetPortableServicePromptSuppressed(bool suppressed);
|
||||
int SetFileTransferSavePath(const std::string& path);
|
||||
|
||||
// read config
|
||||
|
||||
@@ -59,17 +54,15 @@ class ConfigCenter {
|
||||
std::string GetSignalServerHost() const;
|
||||
int GetSignalServerPort() const;
|
||||
int GetCoturnServerPort() const;
|
||||
std::string GetCertFilePath() const;
|
||||
std::string GetCertFingerprint() const;
|
||||
std::string GetDefaultCertFingerprint() const;
|
||||
std::string GetDefaultServerHost() const;
|
||||
int GetDefaultSignalServerPort() const;
|
||||
int GetDefaultCoturnServerPort() const;
|
||||
std::string GetDefaultCertFilePath() const;
|
||||
bool IsSelfHosted() const;
|
||||
bool IsMinimizeToTray() const;
|
||||
bool IsEnableAutostart() const;
|
||||
bool IsEnableDaemon() const;
|
||||
bool IsPortableServicePromptSuppressed() const;
|
||||
std::string GetFileTransferSavePath() const;
|
||||
|
||||
int Load();
|
||||
int Save();
|
||||
@@ -80,7 +73,7 @@ class ConfigCenter {
|
||||
const char* section_ = "Settings";
|
||||
|
||||
LANGUAGE language_ = LANGUAGE::CHINESE;
|
||||
VIDEO_QUALITY video_quality_ = VIDEO_QUALITY::MEDIUM;
|
||||
VIDEO_QUALITY video_quality_ = VIDEO_QUALITY::HIGH;
|
||||
VIDEO_FRAME_RATE video_frame_rate_ = VIDEO_FRAME_RATE::FPS_60;
|
||||
VIDEO_ENCODE_FORMAT video_encode_format_ = VIDEO_ENCODE_FORMAT::H264;
|
||||
bool hardware_video_codec_ = false;
|
||||
@@ -92,16 +85,12 @@ class ConfigCenter {
|
||||
int server_port_default_ = 9099;
|
||||
int coturn_server_port_ = 0;
|
||||
int coturn_server_port_default_ = 3478;
|
||||
std::string cert_file_path_ = "";
|
||||
std::string cert_file_path_default_ = "";
|
||||
std::string cert_fingerprint_ = "";
|
||||
std::string cert_fingerprint_server_host_ = "";
|
||||
std::string default_cert_fingerprint_ = "";
|
||||
std::string default_cert_fingerprint_server_host_ = "";
|
||||
bool enable_self_hosted_ = false;
|
||||
bool enable_minimize_to_tray_ = false;
|
||||
bool enable_autostart_ = false;
|
||||
bool enable_daemon_ = false;
|
||||
bool portable_service_prompt_suppressed_ = false;
|
||||
std::string file_transfer_save_path_ = "";
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
@@ -23,6 +25,8 @@ typedef enum {
|
||||
audio_capture,
|
||||
host_infomation,
|
||||
display_id,
|
||||
service_status,
|
||||
service_command,
|
||||
} ControlType;
|
||||
typedef enum {
|
||||
move = 0,
|
||||
@@ -36,6 +40,7 @@ typedef enum {
|
||||
wheel_horizontal
|
||||
} MouseFlag;
|
||||
typedef enum { key_down = 0, key_up } KeyFlag;
|
||||
typedef enum { send_sas = 0, lock_workstation } ServiceCommandFlag;
|
||||
typedef struct {
|
||||
float x;
|
||||
float y;
|
||||
@@ -45,6 +50,8 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
size_t key_value;
|
||||
uint32_t scan_code;
|
||||
bool extended;
|
||||
KeyFlag flag;
|
||||
} Key;
|
||||
|
||||
@@ -59,6 +66,15 @@ typedef struct {
|
||||
int* bottom;
|
||||
} HostInfo;
|
||||
|
||||
typedef struct {
|
||||
bool available;
|
||||
char interactive_stage[32];
|
||||
} ServiceStatus;
|
||||
|
||||
typedef struct {
|
||||
ServiceCommandFlag flag;
|
||||
} ServiceCommand;
|
||||
|
||||
struct RemoteAction {
|
||||
ControlType type;
|
||||
union {
|
||||
@@ -67,6 +83,8 @@ struct RemoteAction {
|
||||
HostInfo i;
|
||||
bool a;
|
||||
int d;
|
||||
ServiceStatus ss;
|
||||
ServiceCommand c;
|
||||
};
|
||||
|
||||
// parse
|
||||
@@ -88,7 +106,10 @@ struct RemoteAction {
|
||||
{"x", a.m.x}, {"y", a.m.y}, {"s", a.m.s}, {"flag", a.m.flag}};
|
||||
break;
|
||||
case ControlType::keyboard:
|
||||
j["keyboard"] = {{"key_value", a.k.key_value}, {"flag", a.k.flag}};
|
||||
j["keyboard"] = {{"key_value", a.k.key_value},
|
||||
{"scan_code", a.k.scan_code},
|
||||
{"extended", a.k.extended},
|
||||
{"flag", a.k.flag}};
|
||||
break;
|
||||
case ControlType::audio_capture:
|
||||
j["audio_capture"] = a.a;
|
||||
@@ -96,6 +117,13 @@ struct RemoteAction {
|
||||
case ControlType::display_id:
|
||||
j["display_id"] = a.d;
|
||||
break;
|
||||
case ControlType::service_status:
|
||||
j["service_status"] = {{"available", a.ss.available},
|
||||
{"interactive_stage", a.ss.interactive_stage}};
|
||||
break;
|
||||
case ControlType::service_command:
|
||||
j["service_command"] = {{"flag", a.c.flag}};
|
||||
break;
|
||||
case ControlType::host_infomation: {
|
||||
json displays = json::array();
|
||||
for (size_t idx = 0; idx < a.i.display_num; idx++) {
|
||||
@@ -129,6 +157,9 @@ struct RemoteAction {
|
||||
break;
|
||||
case ControlType::keyboard:
|
||||
out.k.key_value = j.at("keyboard").at("key_value").get<size_t>();
|
||||
out.k.scan_code =
|
||||
j.at("keyboard").value("scan_code", static_cast<uint32_t>(0));
|
||||
out.k.extended = j.at("keyboard").value("extended", false);
|
||||
out.k.flag = (KeyFlag)j.at("keyboard").at("flag").get<int>();
|
||||
break;
|
||||
case ControlType::audio_capture:
|
||||
@@ -137,6 +168,20 @@ struct RemoteAction {
|
||||
case ControlType::display_id:
|
||||
out.d = j.at("display_id").get<int>();
|
||||
break;
|
||||
case ControlType::service_status: {
|
||||
const auto& service_status_json = j.at("service_status");
|
||||
out.ss.available = service_status_json.value("available", false);
|
||||
std::string interactive_stage =
|
||||
service_status_json.value("interactive_stage", std::string());
|
||||
std::strncpy(out.ss.interactive_stage, interactive_stage.c_str(),
|
||||
sizeof(out.ss.interactive_stage) - 1);
|
||||
out.ss.interactive_stage[sizeof(out.ss.interactive_stage) - 1] = '\0';
|
||||
break;
|
||||
}
|
||||
case ControlType::service_command:
|
||||
out.c.flag = static_cast<ServiceCommandFlag>(
|
||||
j.at("service_command").at("flag").get<int>());
|
||||
break;
|
||||
case ControlType::host_infomation: {
|
||||
std::string host_name =
|
||||
j.at("host_info").at("host_name").get<std::string>();
|
||||
@@ -174,8 +219,8 @@ struct RemoteAction {
|
||||
}
|
||||
};
|
||||
|
||||
// int key_code, bool is_down
|
||||
typedef void (*OnKeyAction)(int, bool, void*);
|
||||
// int key_code, bool is_down, uint32_t scan_code, bool extended
|
||||
typedef void (*OnKeyAction)(int, bool, uint32_t, bool, void*);
|
||||
|
||||
class DeviceController {
|
||||
public:
|
||||
@@ -190,4 +235,4 @@ class DeviceController {
|
||||
// virtual int Unhook();
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -1,27 +1,60 @@
|
||||
#include "keyboard_capturer.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include "keyboard_converter.h"
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
#include "windows_key_metadata.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
static OnKeyAction g_on_key_action = nullptr;
|
||||
static void* g_user_ptr = nullptr;
|
||||
|
||||
static KeySym NormalizeKeySym(KeySym key_sym) {
|
||||
if (key_sym >= XK_a && key_sym <= XK_z) {
|
||||
return key_sym - XK_a + XK_A;
|
||||
}
|
||||
return key_sym;
|
||||
}
|
||||
|
||||
static int KeyboardEventHandler(Display* display, XEvent* event) {
|
||||
(void)display;
|
||||
if (event->xkey.type == KeyPress || event->xkey.type == KeyRelease) {
|
||||
KeySym keySym = XKeycodeToKeysym(display, event->xkey.keycode, 0);
|
||||
int key_code = XKeysymToKeycode(display, keySym);
|
||||
KeySym key_sym = NormalizeKeySym(XLookupKeysym(&event->xkey, 0));
|
||||
auto key_it = x11KeySymToVkCode.find(static_cast<int>(key_sym));
|
||||
if (key_it == x11KeySymToVkCode.end()) {
|
||||
key_sym = NormalizeKeySym(XLookupKeysym(&event->xkey, 1));
|
||||
key_it = x11KeySymToVkCode.find(static_cast<int>(key_sym));
|
||||
}
|
||||
|
||||
if (key_it == x11KeySymToVkCode.end()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int key_code = key_it->second;
|
||||
bool is_key_down = (event->xkey.type == KeyPress);
|
||||
uint32_t scan_code = 0;
|
||||
bool extended = false;
|
||||
LookupWindowsKeyMetadataFromVk(key_code, &scan_code, &extended);
|
||||
|
||||
if (g_on_key_action) {
|
||||
g_on_key_action(key_code, is_key_down, g_user_ptr);
|
||||
g_on_key_action(key_code, is_key_down, scan_code, extended, g_user_ptr);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
KeyboardCapturer::KeyboardCapturer() : display_(nullptr), running_(true) {
|
||||
KeyboardCapturer::KeyboardCapturer()
|
||||
: display_(nullptr),
|
||||
root_(0),
|
||||
running_(false),
|
||||
use_wayland_portal_(false),
|
||||
wayland_init_attempted_(false),
|
||||
dbus_connection_(nullptr) {
|
||||
XInitThreads();
|
||||
display_ = XOpenDisplay(nullptr);
|
||||
if (!display_) {
|
||||
LOG_ERROR("Failed to open X display.");
|
||||
@@ -29,42 +62,116 @@ KeyboardCapturer::KeyboardCapturer() : display_(nullptr), running_(true) {
|
||||
}
|
||||
|
||||
KeyboardCapturer::~KeyboardCapturer() {
|
||||
Unhook();
|
||||
CleanupWaylandPortal();
|
||||
|
||||
if (display_) {
|
||||
XCloseDisplay(display_);
|
||||
display_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int KeyboardCapturer::Hook(OnKeyAction on_key_action, void* user_ptr) {
|
||||
if (!display_) {
|
||||
LOG_ERROR("Display not initialized.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_on_key_action = on_key_action;
|
||||
g_user_ptr = user_ptr;
|
||||
|
||||
XSelectInput(display_, DefaultRootWindow(display_),
|
||||
KeyPressMask | KeyReleaseMask);
|
||||
|
||||
while (running_) {
|
||||
XEvent event;
|
||||
XNextEvent(display_, &event);
|
||||
KeyboardEventHandler(display_, &event);
|
||||
if (running_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
root_ = DefaultRootWindow(display_);
|
||||
XSelectInput(display_, root_, KeyPressMask | KeyReleaseMask);
|
||||
XFlush(display_);
|
||||
|
||||
running_ = true;
|
||||
const int x11_fd = ConnectionNumber(display_);
|
||||
event_thread_ = std::thread([this, x11_fd]() {
|
||||
while (running_) {
|
||||
while (running_ && XPending(display_) > 0) {
|
||||
XEvent event;
|
||||
XNextEvent(display_, &event);
|
||||
KeyboardEventHandler(display_, &event);
|
||||
}
|
||||
|
||||
if (!running_) {
|
||||
break;
|
||||
}
|
||||
|
||||
struct pollfd pfd = {x11_fd, POLLIN, 0};
|
||||
int poll_ret = poll(&pfd, 1, 50);
|
||||
if (poll_ret < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
LOG_ERROR("poll for X11 events failed.");
|
||||
running_ = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (poll_ret == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) != 0) {
|
||||
LOG_ERROR("poll got invalid X11 event fd state.");
|
||||
running_ = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if ((pfd.revents & POLLIN) == 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int KeyboardCapturer::Unhook() {
|
||||
running_ = false;
|
||||
|
||||
if (event_thread_.joinable()) {
|
||||
event_thread_.join();
|
||||
}
|
||||
|
||||
g_on_key_action = nullptr;
|
||||
g_user_ptr = nullptr;
|
||||
|
||||
running_ = false;
|
||||
|
||||
if (display_) {
|
||||
XSelectInput(display_, DefaultRootWindow(display_), 0);
|
||||
if (display_ && root_ != 0) {
|
||||
XSelectInput(display_, root_, 0);
|
||||
XFlush(display_);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down) {
|
||||
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down,
|
||||
uint32_t scan_code, bool extended) {
|
||||
(void)scan_code;
|
||||
(void)extended;
|
||||
if (IsWaylandSession()) {
|
||||
if (!use_wayland_portal_ && !wayland_init_attempted_) {
|
||||
wayland_init_attempted_ = true;
|
||||
if (InitWaylandPortal()) {
|
||||
use_wayland_portal_ = true;
|
||||
LOG_INFO("Keyboard controller initialized with Wayland portal backend");
|
||||
} else {
|
||||
LOG_WARN(
|
||||
"Wayland keyboard control init failed, falling back to X11/XTest "
|
||||
"backend");
|
||||
}
|
||||
}
|
||||
|
||||
if (use_wayland_portal_) {
|
||||
return SendWaylandKeyboardCommand(key_code, is_down, scan_code, extended);
|
||||
}
|
||||
}
|
||||
|
||||
if (!display_) {
|
||||
LOG_ERROR("Display not initialized.");
|
||||
return -1;
|
||||
@@ -78,4 +185,4 @@ int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -11,8 +11,17 @@
|
||||
#include <X11/extensions/XTest.h>
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include "device_controller.h"
|
||||
|
||||
struct DBusConnection;
|
||||
struct DBusMessageIter;
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class KeyboardCapturer : public DeviceController {
|
||||
@@ -23,12 +32,30 @@ class KeyboardCapturer : public DeviceController {
|
||||
public:
|
||||
virtual int Hook(OnKeyAction on_key_action, void* user_ptr);
|
||||
virtual int Unhook();
|
||||
virtual int SendKeyboardCommand(int key_code, bool is_down);
|
||||
virtual int SendKeyboardCommand(int key_code, bool is_down,
|
||||
uint32_t scan_code = 0,
|
||||
bool extended = false);
|
||||
|
||||
private:
|
||||
bool InitWaylandPortal();
|
||||
void CleanupWaylandPortal();
|
||||
int SendWaylandKeyboardCommand(int key_code, bool is_down, uint32_t scan_code,
|
||||
bool extended);
|
||||
bool NotifyWaylandKeyboardKeysym(int keysym, uint32_t state);
|
||||
bool NotifyWaylandKeyboardKeycode(int keycode, uint32_t state);
|
||||
bool SendWaylandPortalVoidCall(const char* method_name,
|
||||
const std::function<void(DBusMessageIter*)>&
|
||||
append_args);
|
||||
|
||||
private:
|
||||
Display* display_;
|
||||
Window root_;
|
||||
bool running_;
|
||||
std::atomic<bool> running_;
|
||||
std::thread event_thread_;
|
||||
bool use_wayland_portal_ = false;
|
||||
bool wayland_init_attempted_ = false;
|
||||
DBusConnection* dbus_connection_ = nullptr;
|
||||
std::string wayland_session_handle_;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,717 @@
|
||||
#include "keyboard_capturer.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
#include <dbus/dbus.h>
|
||||
#endif
|
||||
|
||||
#include "rd_log.h"
|
||||
#include "wayland_portal_shared.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
extern std::map<int, int> vkCodeToX11KeySym;
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
namespace {
|
||||
|
||||
constexpr const char* kPortalBusName = "org.freedesktop.portal.Desktop";
|
||||
constexpr const char* kPortalObjectPath = "/org/freedesktop/portal/desktop";
|
||||
constexpr const char* kPortalRemoteDesktopInterface =
|
||||
"org.freedesktop.portal.RemoteDesktop";
|
||||
constexpr const char* kPortalRequestInterface =
|
||||
"org.freedesktop.portal.Request";
|
||||
constexpr const char* kPortalRequestPathPrefix =
|
||||
"/org/freedesktop/portal/desktop/request/";
|
||||
constexpr const char* kPortalSessionPathPrefix =
|
||||
"/org/freedesktop/portal/desktop/session/";
|
||||
|
||||
constexpr uint32_t kRemoteDesktopDeviceKeyboard = 1u;
|
||||
constexpr uint32_t kKeyboardReleased = 0u;
|
||||
constexpr uint32_t kKeyboardPressed = 1u;
|
||||
|
||||
int NormalizeFallbackKeysym(int keysym) {
|
||||
if (keysym >= XK_A && keysym <= XK_Z) {
|
||||
return keysym - XK_A + XK_a;
|
||||
}
|
||||
return keysym;
|
||||
}
|
||||
|
||||
std::string MakeToken(const char* prefix) {
|
||||
const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
return std::string(prefix) + "_" + std::to_string(now);
|
||||
}
|
||||
|
||||
void LogDbusError(const char* action, DBusError* error) {
|
||||
if (error && dbus_error_is_set(error)) {
|
||||
LOG_ERROR("{} failed: {} ({})", action,
|
||||
error->message ? error->message : "unknown",
|
||||
error->name ? error->name : "unknown");
|
||||
} else {
|
||||
LOG_ERROR("{} failed", action);
|
||||
}
|
||||
}
|
||||
|
||||
void AppendDictEntryString(DBusMessageIter* dict, const char* key,
|
||||
const std::string& value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
const char* value_cstr = value.c_str();
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "s", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &value_cstr);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
void AppendDictEntryUint32(DBusMessageIter* dict, const char* key,
|
||||
uint32_t value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "u", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_UINT32, &value);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
void AppendEmptyOptionsDict(DBusMessageIter* iter) {
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &options);
|
||||
dbus_message_iter_close_container(iter, &options);
|
||||
}
|
||||
|
||||
bool ReadPathLikeVariant(DBusMessageIter* variant, std::string* value) {
|
||||
if (!variant || !value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int type = dbus_message_iter_get_arg_type(variant);
|
||||
if (type == DBUS_TYPE_OBJECT_PATH || type == DBUS_TYPE_STRING) {
|
||||
const char* temp = nullptr;
|
||||
dbus_message_iter_get_basic(variant, &temp);
|
||||
if (temp && temp[0] != '\0') {
|
||||
*value = temp;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReadUint32Like(DBusMessageIter* iter, uint32_t* value) {
|
||||
if (!iter || !value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int type = dbus_message_iter_get_arg_type(iter);
|
||||
if (type == DBUS_TYPE_UINT32) {
|
||||
uint32_t temp = 0;
|
||||
dbus_message_iter_get_basic(iter, &temp);
|
||||
*value = temp;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (type == DBUS_TYPE_INT32) {
|
||||
int32_t temp = 0;
|
||||
dbus_message_iter_get_basic(iter, &temp);
|
||||
if (temp < 0) {
|
||||
return false;
|
||||
}
|
||||
*value = static_cast<uint32_t>(temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string BuildSessionHandleFromRequestPath(
|
||||
const std::string& request_path, const std::string& session_handle_token) {
|
||||
if (request_path.rfind(kPortalRequestPathPrefix, 0) != 0 ||
|
||||
session_handle_token.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const size_t sender_start = strlen(kPortalRequestPathPrefix);
|
||||
const size_t token_sep = request_path.find('/', sender_start);
|
||||
if (token_sep == std::string::npos || token_sep <= sender_start) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const std::string sender =
|
||||
request_path.substr(sender_start, token_sep - sender_start);
|
||||
if (sender.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return std::string(kPortalSessionPathPrefix) + sender + "/" +
|
||||
session_handle_token;
|
||||
}
|
||||
|
||||
struct PortalResponseState {
|
||||
std::string request_path;
|
||||
bool received = false;
|
||||
DBusMessage* message = nullptr;
|
||||
};
|
||||
|
||||
DBusHandlerResult HandlePortalResponseSignal(DBusConnection* connection,
|
||||
DBusMessage* message,
|
||||
void* user_data) {
|
||||
(void)connection;
|
||||
auto* state = static_cast<PortalResponseState*>(user_data);
|
||||
if (!state || !message) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
if (!dbus_message_is_signal(message, kPortalRequestInterface, "Response")) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
const char* path = dbus_message_get_path(message);
|
||||
if (!path || state->request_path != path) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
if (state->message) {
|
||||
dbus_message_unref(state->message);
|
||||
state->message = nullptr;
|
||||
}
|
||||
|
||||
state->message = dbus_message_ref(message);
|
||||
state->received = true;
|
||||
return DBUS_HANDLER_RESULT_HANDLED;
|
||||
}
|
||||
|
||||
DBusMessage* WaitForPortalResponse(DBusConnection* connection,
|
||||
const std::string& request_path,
|
||||
int timeout_ms = 120000) {
|
||||
if (!connection || request_path.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PortalResponseState state;
|
||||
state.request_path = request_path;
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
const std::string match_rule =
|
||||
"type='signal',interface='" + std::string(kPortalRequestInterface) +
|
||||
"',member='Response',path='" + request_path + "'";
|
||||
dbus_bus_add_match(connection, match_rule.c_str(), &error);
|
||||
if (dbus_error_is_set(&error)) {
|
||||
LogDbusError("dbus_bus_add_match", &error);
|
||||
dbus_error_free(&error);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
dbus_connection_add_filter(connection, HandlePortalResponseSignal, &state,
|
||||
nullptr);
|
||||
|
||||
auto deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
while (!state.received && std::chrono::steady_clock::now() < deadline) {
|
||||
dbus_connection_read_write(connection, 100);
|
||||
while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS) {
|
||||
}
|
||||
}
|
||||
|
||||
dbus_connection_remove_filter(connection, HandlePortalResponseSignal, &state);
|
||||
|
||||
DBusError remove_error;
|
||||
dbus_error_init(&remove_error);
|
||||
dbus_bus_remove_match(connection, match_rule.c_str(), &remove_error);
|
||||
if (dbus_error_is_set(&remove_error)) {
|
||||
dbus_error_free(&remove_error);
|
||||
}
|
||||
|
||||
return state.message;
|
||||
}
|
||||
|
||||
bool ExtractRequestPath(DBusMessage* reply, std::string* request_path) {
|
||||
if (!reply || !request_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* path = nullptr;
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
const dbus_bool_t ok = dbus_message_get_args(
|
||||
reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
|
||||
if (!ok || !path) {
|
||||
LogDbusError("dbus_message_get_args(request_path)", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
*request_path = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExtractPortalResponse(DBusMessage* message, uint32_t* response_code,
|
||||
DBusMessageIter* results_array) {
|
||||
if (!message || !response_code || !results_array) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter iter;
|
||||
if (!dbus_message_iter_init(message, &iter) ||
|
||||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_message_iter_get_basic(&iter, response_code);
|
||||
if (!dbus_message_iter_next(&iter) ||
|
||||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*results_array = iter;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SendPortalRequestAndHandleResponse(
|
||||
DBusConnection* connection, const char* interface_name,
|
||||
const char* method_name, const char* action_name,
|
||||
const std::function<bool(DBusMessage*)>& append_message_args,
|
||||
const std::function<bool(uint32_t, DBusMessageIter*)>& handle_results,
|
||||
std::string* request_path_out = nullptr) {
|
||||
if (!connection || !interface_name || interface_name[0] == '\0' ||
|
||||
!method_name || method_name[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, kPortalObjectPath, interface_name, method_name);
|
||||
if (!message) {
|
||||
LOG_ERROR("Failed to allocate {} message", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (append_message_args && !append_message_args(message)) {
|
||||
dbus_message_unref(message);
|
||||
LOG_ERROR("{} arguments are malformed", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply =
|
||||
dbus_connection_send_with_reply_and_block(connection, message, -1, &error);
|
||||
dbus_message_unref(message);
|
||||
if (!reply) {
|
||||
LogDbusError(action_name ? action_name : method_name, &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string request_path;
|
||||
const bool got_request_path = ExtractRequestPath(reply, &request_path);
|
||||
dbus_message_unref(reply);
|
||||
if (!got_request_path) {
|
||||
return false;
|
||||
}
|
||||
if (request_path_out) {
|
||||
*request_path_out = request_path;
|
||||
}
|
||||
|
||||
DBusMessage* response = WaitForPortalResponse(connection, request_path);
|
||||
if (!response) {
|
||||
LOG_ERROR("Timed out waiting for {} response", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t response_code = 1;
|
||||
DBusMessageIter results;
|
||||
const bool parsed = ExtractPortalResponse(response, &response_code, &results);
|
||||
if (!parsed) {
|
||||
dbus_message_unref(response);
|
||||
LOG_ERROR("{} response was malformed", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool ok = handle_results ? handle_results(response_code, &results)
|
||||
: (response_code == 0);
|
||||
dbus_message_unref(response);
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
#endif
|
||||
|
||||
bool KeyboardCapturer::InitWaylandPortal() {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
CleanupWaylandPortal();
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
DBusConnection* check_connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
|
||||
if (!check_connection) {
|
||||
LogDbusError("dbus_bus_get", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
const dbus_bool_t has_owner =
|
||||
dbus_bus_name_has_owner(check_connection, kPortalBusName, &error);
|
||||
if (dbus_error_is_set(&error)) {
|
||||
LogDbusError("dbus_bus_name_has_owner", &error);
|
||||
dbus_error_free(&error);
|
||||
dbus_connection_unref(check_connection);
|
||||
return false;
|
||||
}
|
||||
dbus_connection_unref(check_connection);
|
||||
|
||||
if (!has_owner) {
|
||||
LOG_ERROR("xdg-desktop-portal is not available on session bus");
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_connection_ = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
|
||||
if (!dbus_connection_) {
|
||||
LogDbusError("dbus_bus_get_private", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
dbus_connection_set_exit_on_disconnect(dbus_connection_, FALSE);
|
||||
|
||||
const std::string session_handle_token =
|
||||
MakeToken("crossdesk_keyboard_session");
|
||||
std::string request_path;
|
||||
const bool create_ok = SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "CreateSession",
|
||||
"CreateSession",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryString(&options, "session_handle_token",
|
||||
session_handle_token);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_keyboard_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
[&](uint32_t response_code, DBusMessageIter* results) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("RemoteDesktop.CreateSession denied, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter dict;
|
||||
dbus_message_iter_recurse(results, &dict);
|
||||
while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter entry;
|
||||
dbus_message_iter_recurse(&dict, &entry);
|
||||
|
||||
const char* key = nullptr;
|
||||
dbus_message_iter_get_basic(&entry, &key);
|
||||
if (key && dbus_message_iter_next(&entry) &&
|
||||
dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT &&
|
||||
strcmp(key, "session_handle") == 0) {
|
||||
DBusMessageIter variant;
|
||||
std::string parsed_handle;
|
||||
dbus_message_iter_recurse(&entry, &variant);
|
||||
if (ReadPathLikeVariant(&variant, &parsed_handle) &&
|
||||
!parsed_handle.empty()) {
|
||||
wayland_session_handle_ = parsed_handle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
dbus_message_iter_next(&dict);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
&request_path);
|
||||
|
||||
if (!create_ok) {
|
||||
CleanupWaylandPortal();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wayland_session_handle_.empty()) {
|
||||
wayland_session_handle_ =
|
||||
BuildSessionHandleFromRequestPath(request_path, session_handle_token);
|
||||
}
|
||||
|
||||
if (wayland_session_handle_.empty()) {
|
||||
LOG_ERROR("RemoteDesktop.CreateSession did not return session handle");
|
||||
CleanupWaylandPortal();
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* session_handle = wayland_session_handle_.c_str();
|
||||
const bool select_ok = SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "SelectDevices",
|
||||
"SelectDevices",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryUint32(&options, "types", kRemoteDesktopDeviceKeyboard);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_keyboard_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
[](uint32_t response_code, DBusMessageIter*) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("RemoteDesktop.SelectDevices denied, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!select_ok) {
|
||||
CleanupWaylandPortal();
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* parent_window = "";
|
||||
bool keyboard_granted = false;
|
||||
const bool start_ok = SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "Start", "Start",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &parent_window);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_keyboard_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
[&](uint32_t response_code, DBusMessageIter* results) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("RemoteDesktop.Start denied, response={}", response_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t granted_devices = 0;
|
||||
DBusMessageIter dict;
|
||||
dbus_message_iter_recurse(results, &dict);
|
||||
while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter entry;
|
||||
dbus_message_iter_recurse(&dict, &entry);
|
||||
|
||||
const char* key = nullptr;
|
||||
dbus_message_iter_get_basic(&entry, &key);
|
||||
if (key && dbus_message_iter_next(&entry) &&
|
||||
dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT) {
|
||||
DBusMessageIter variant;
|
||||
dbus_message_iter_recurse(&entry, &variant);
|
||||
if (strcmp(key, "devices") == 0) {
|
||||
ReadUint32Like(&variant, &granted_devices);
|
||||
}
|
||||
}
|
||||
}
|
||||
dbus_message_iter_next(&dict);
|
||||
}
|
||||
|
||||
keyboard_granted =
|
||||
(granted_devices & kRemoteDesktopDeviceKeyboard) != 0;
|
||||
if (!keyboard_granted) {
|
||||
LOG_ERROR(
|
||||
"RemoteDesktop.Start granted devices mask={}, keyboard not allowed",
|
||||
granted_devices);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!start_ok) {
|
||||
CleanupWaylandPortal();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!keyboard_granted) {
|
||||
LOG_ERROR("RemoteDesktop session started without keyboard permission");
|
||||
CleanupWaylandPortal();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void KeyboardCapturer::CleanupWaylandPortal() {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
if (dbus_connection_) {
|
||||
CloseWaylandPortalSessionAndConnection(dbus_connection_,
|
||||
wayland_session_handle_,
|
||||
"RemoteDesktop.Session.Close");
|
||||
dbus_connection_ = nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
use_wayland_portal_ = false;
|
||||
wayland_session_handle_.clear();
|
||||
}
|
||||
|
||||
int KeyboardCapturer::SendWaylandKeyboardCommand(int key_code, bool is_down,
|
||||
uint32_t scan_code,
|
||||
bool extended) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
(void)scan_code;
|
||||
(void)extended;
|
||||
if (!dbus_connection_ || wayland_session_handle_.empty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const auto key_it = vkCodeToX11KeySym.find(key_code);
|
||||
if (key_it == vkCodeToX11KeySym.end()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint32_t key_state = is_down ? kKeyboardPressed : kKeyboardReleased;
|
||||
const int keysym = key_it->second;
|
||||
|
||||
// Prefer keycode injection to preserve physical-key semantics and avoid
|
||||
// implicit Shift interpretation for uppercase keysyms.
|
||||
if (display_) {
|
||||
const KeyCode x11_keycode =
|
||||
XKeysymToKeycode(display_, static_cast<KeySym>(keysym));
|
||||
if (x11_keycode > 8) {
|
||||
const int evdev_keycode = static_cast<int>(x11_keycode) - 8;
|
||||
if (NotifyWaylandKeyboardKeycode(evdev_keycode, key_state)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const int fallback_keysym = NormalizeFallbackKeysym(keysym);
|
||||
if (NotifyWaylandKeyboardKeysym(fallback_keysym, key_state)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
LOG_ERROR("Failed to send Wayland keyboard event, vk_code={}, is_down={}",
|
||||
key_code, is_down);
|
||||
return -3;
|
||||
#else
|
||||
(void)key_code;
|
||||
(void)is_down;
|
||||
(void)scan_code;
|
||||
(void)extended;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool KeyboardCapturer::NotifyWaylandKeyboardKeysym(int keysym, uint32_t state) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
return SendWaylandPortalVoidCall(
|
||||
"NotifyKeyboardKeysym", [&](DBusMessageIter* iter) {
|
||||
const char* session_handle = wayland_session_handle_.c_str();
|
||||
int32_t key_sym = keysym;
|
||||
uint32_t key_state = state;
|
||||
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
AppendEmptyOptionsDict(iter);
|
||||
dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &key_sym);
|
||||
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &key_state);
|
||||
});
|
||||
#else
|
||||
(void)keysym;
|
||||
(void)state;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool KeyboardCapturer::NotifyWaylandKeyboardKeycode(int keycode,
|
||||
uint32_t state) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
return SendWaylandPortalVoidCall(
|
||||
"NotifyKeyboardKeycode", [&](DBusMessageIter* iter) {
|
||||
const char* session_handle = wayland_session_handle_.c_str();
|
||||
int32_t key_code = keycode;
|
||||
uint32_t key_state = state;
|
||||
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
AppendEmptyOptionsDict(iter);
|
||||
dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &key_code);
|
||||
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &key_state);
|
||||
});
|
||||
#else
|
||||
(void)keycode;
|
||||
(void)state;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool KeyboardCapturer::SendWaylandPortalVoidCall(
|
||||
const char* method_name,
|
||||
const std::function<void(DBusMessageIter*)>& append_args) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
if (!dbus_connection_ || !method_name || method_name[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, kPortalObjectPath, kPortalRemoteDesktopInterface,
|
||||
method_name);
|
||||
if (!message) {
|
||||
LOG_ERROR("Failed to allocate {} message", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter iter;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
if (append_args) {
|
||||
append_args(&iter);
|
||||
}
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
|
||||
dbus_connection_, message, 5000, &error);
|
||||
dbus_message_unref(message);
|
||||
if (!reply) {
|
||||
LogDbusError(method_name, &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
|
||||
const char* error_name = dbus_message_get_error_name(reply);
|
||||
LOG_ERROR("{} returned DBus error: {}", method_name,
|
||||
error_name ? error_name : "unknown");
|
||||
dbus_message_unref(reply);
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_message_unref(reply);
|
||||
return true;
|
||||
#else
|
||||
(void)method_name;
|
||||
(void)append_args;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "keyboard_capturer.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "keyboard_converter.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
@@ -7,9 +9,100 @@ namespace crossdesk {
|
||||
|
||||
static OnKeyAction g_on_key_action = nullptr;
|
||||
static void* g_user_ptr = nullptr;
|
||||
static std::unordered_map<int, int> g_unmapped_keycode_to_vk;
|
||||
|
||||
static int VkCodeFromUnicode(UniChar ch) {
|
||||
if (ch >= 'a' && ch <= 'z') {
|
||||
return static_cast<int>(ch - 'a' + 'A');
|
||||
}
|
||||
if (ch >= 'A' && ch <= 'Z') {
|
||||
return static_cast<int>(ch);
|
||||
}
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
return static_cast<int>(ch);
|
||||
}
|
||||
|
||||
switch (ch) {
|
||||
case ' ':
|
||||
return 0x20; // VK_SPACE
|
||||
case '-':
|
||||
case '_':
|
||||
return 0xBD; // VK_OEM_MINUS
|
||||
case '=':
|
||||
case '+':
|
||||
return 0xBB; // VK_OEM_PLUS
|
||||
case '[':
|
||||
case '{':
|
||||
return 0xDB; // VK_OEM_4
|
||||
case ']':
|
||||
case '}':
|
||||
return 0xDD; // VK_OEM_6
|
||||
case '\\':
|
||||
case '|':
|
||||
return 0xDC; // VK_OEM_5
|
||||
case ';':
|
||||
case ':':
|
||||
return 0xBA; // VK_OEM_1
|
||||
case '\'':
|
||||
case '"':
|
||||
return 0xDE; // VK_OEM_7
|
||||
case ',':
|
||||
case '<':
|
||||
return 0xBC; // VK_OEM_COMMA
|
||||
case '.':
|
||||
case '>':
|
||||
return 0xBE; // VK_OEM_PERIOD
|
||||
case '/':
|
||||
case '?':
|
||||
return 0xBF; // VK_OEM_2
|
||||
case '`':
|
||||
case '~':
|
||||
return 0xC0; // VK_OEM_3
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int ResolveVkCodeFromMacEvent(CGEventRef event, CGKeyCode key_code,
|
||||
bool is_key_down) {
|
||||
auto key_it = CGKeyCodeToVkCode.find(key_code);
|
||||
if (key_it != CGKeyCodeToVkCode.end()) {
|
||||
if (is_key_down) {
|
||||
g_unmapped_keycode_to_vk.erase(static_cast<int>(key_code));
|
||||
}
|
||||
return key_it->second;
|
||||
}
|
||||
|
||||
int vk_code = -1;
|
||||
UniChar chars[4] = {0};
|
||||
UniCharCount char_count = 0;
|
||||
CGEventKeyboardGetUnicodeString(event, 4, &char_count, chars);
|
||||
if (char_count > 0) {
|
||||
vk_code = VkCodeFromUnicode(chars[0]);
|
||||
}
|
||||
|
||||
if (vk_code < 0) {
|
||||
auto fallback_it =
|
||||
g_unmapped_keycode_to_vk.find(static_cast<int>(key_code));
|
||||
if (fallback_it != g_unmapped_keycode_to_vk.end()) {
|
||||
vk_code = fallback_it->second;
|
||||
}
|
||||
}
|
||||
|
||||
if (vk_code >= 0) {
|
||||
if (is_key_down) {
|
||||
g_unmapped_keycode_to_vk[static_cast<int>(key_code)] = vk_code;
|
||||
} else {
|
||||
g_unmapped_keycode_to_vk.erase(static_cast<int>(key_code));
|
||||
}
|
||||
}
|
||||
|
||||
return vk_code;
|
||||
}
|
||||
|
||||
CGEventRef eventCallback(CGEventTapProxy proxy, CGEventType type,
|
||||
CGEventRef event, void* userInfo) {
|
||||
(void)proxy;
|
||||
if (!g_on_key_action) {
|
||||
return event;
|
||||
}
|
||||
@@ -20,84 +113,79 @@ CGEventRef eventCallback(CGEventTapProxy proxy, CGEventType type,
|
||||
return event;
|
||||
}
|
||||
|
||||
int vk_code = 0;
|
||||
|
||||
if (type == kCGEventKeyDown || type == kCGEventKeyUp) {
|
||||
const bool is_key_down = (type == kCGEventKeyDown);
|
||||
CGKeyCode key_code = static_cast<CGKeyCode>(
|
||||
CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode));
|
||||
if (CGKeyCodeToVkCode.find(key_code) != CGKeyCodeToVkCode.end()) {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], type == kCGEventKeyDown,
|
||||
g_user_ptr);
|
||||
int vk_code = ResolveVkCodeFromMacEvent(event, key_code, is_key_down);
|
||||
if (vk_code >= 0) {
|
||||
g_on_key_action(vk_code, is_key_down, 0, false, g_user_ptr);
|
||||
}
|
||||
} else if (type == kCGEventFlagsChanged) {
|
||||
CGEventFlags current_flags = CGEventGetFlags(event);
|
||||
CGKeyCode key_code = static_cast<CGKeyCode>(
|
||||
CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode));
|
||||
auto key_it = CGKeyCodeToVkCode.find(key_code);
|
||||
if (key_it == CGKeyCodeToVkCode.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
const int vk_code = key_it->second;
|
||||
|
||||
// caps lock
|
||||
bool caps_lock_state = (current_flags & kCGEventFlagMaskAlphaShift) != 0;
|
||||
if (caps_lock_state != keyboard_capturer->caps_lock_flag_) {
|
||||
keyboard_capturer->caps_lock_flag_ = caps_lock_state;
|
||||
if (keyboard_capturer->caps_lock_flag_) {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], true, g_user_ptr);
|
||||
} else {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], false, g_user_ptr);
|
||||
}
|
||||
g_on_key_action(vk_code, keyboard_capturer->caps_lock_flag_, 0, false,
|
||||
g_user_ptr);
|
||||
}
|
||||
|
||||
// shift
|
||||
bool shift_state = (current_flags & kCGEventFlagMaskShift) != 0;
|
||||
if (shift_state != keyboard_capturer->shift_flag_) {
|
||||
keyboard_capturer->shift_flag_ = shift_state;
|
||||
if (keyboard_capturer->shift_flag_) {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], true, g_user_ptr);
|
||||
} else {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], false, g_user_ptr);
|
||||
}
|
||||
g_on_key_action(vk_code, keyboard_capturer->shift_flag_, 0, false,
|
||||
g_user_ptr);
|
||||
}
|
||||
|
||||
// control
|
||||
bool control_state = (current_flags & kCGEventFlagMaskControl) != 0;
|
||||
if (control_state != keyboard_capturer->control_flag_) {
|
||||
keyboard_capturer->control_flag_ = control_state;
|
||||
if (keyboard_capturer->control_flag_) {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], true, g_user_ptr);
|
||||
} else {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], false, g_user_ptr);
|
||||
}
|
||||
g_on_key_action(vk_code, keyboard_capturer->control_flag_, 0, false,
|
||||
g_user_ptr);
|
||||
}
|
||||
|
||||
// option
|
||||
bool option_state = (current_flags & kCGEventFlagMaskAlternate) != 0;
|
||||
if (option_state != keyboard_capturer->option_flag_) {
|
||||
keyboard_capturer->option_flag_ = option_state;
|
||||
if (keyboard_capturer->option_flag_) {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], true, g_user_ptr);
|
||||
} else {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], false, g_user_ptr);
|
||||
}
|
||||
g_on_key_action(vk_code, keyboard_capturer->option_flag_, 0, false,
|
||||
g_user_ptr);
|
||||
}
|
||||
|
||||
// command
|
||||
bool command_state = (current_flags & kCGEventFlagMaskCommand) != 0;
|
||||
if (command_state != keyboard_capturer->command_flag_) {
|
||||
keyboard_capturer->command_flag_ = command_state;
|
||||
if (keyboard_capturer->command_flag_) {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], true, g_user_ptr);
|
||||
} else {
|
||||
g_on_key_action(CGKeyCodeToVkCode[key_code], false, g_user_ptr);
|
||||
}
|
||||
g_on_key_action(vk_code, keyboard_capturer->command_flag_, 0, false,
|
||||
g_user_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
KeyboardCapturer::KeyboardCapturer() {}
|
||||
KeyboardCapturer::KeyboardCapturer()
|
||||
: event_tap_(nullptr), run_loop_source_(nullptr) {}
|
||||
|
||||
KeyboardCapturer::~KeyboardCapturer() {}
|
||||
KeyboardCapturer::~KeyboardCapturer() { Unhook(); }
|
||||
|
||||
int KeyboardCapturer::Hook(OnKeyAction on_key_action, void* user_ptr) {
|
||||
if (event_tap_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_unmapped_keycode_to_vk.clear();
|
||||
g_on_key_action = on_key_action;
|
||||
g_user_ptr = user_ptr;
|
||||
|
||||
@@ -115,15 +203,30 @@ int KeyboardCapturer::Hook(OnKeyAction on_key_action, void* user_ptr) {
|
||||
|
||||
run_loop_source_ =
|
||||
CFMachPortCreateRunLoopSource(kCFAllocatorDefault, event_tap_, 0);
|
||||
if (!run_loop_source_) {
|
||||
LOG_ERROR("CFMachPortCreateRunLoopSource failed");
|
||||
CFRelease(event_tap_);
|
||||
event_tap_ = nullptr;
|
||||
return -1;
|
||||
}
|
||||
|
||||
CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source_,
|
||||
kCFRunLoopCommonModes);
|
||||
|
||||
const CGEventFlags current_flags =
|
||||
CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState);
|
||||
caps_lock_flag_ = (current_flags & kCGEventFlagMaskAlphaShift) != 0;
|
||||
shift_flag_ = (current_flags & kCGEventFlagMaskShift) != 0;
|
||||
control_flag_ = (current_flags & kCGEventFlagMaskControl) != 0;
|
||||
option_flag_ = (current_flags & kCGEventFlagMaskAlternate) != 0;
|
||||
command_flag_ = (current_flags & kCGEventFlagMaskCommand) != 0;
|
||||
|
||||
CGEventTapEnable(event_tap_, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int KeyboardCapturer::Unhook() {
|
||||
g_unmapped_keycode_to_vk.clear();
|
||||
g_on_key_action = nullptr;
|
||||
g_user_ptr = nullptr;
|
||||
|
||||
@@ -166,13 +269,39 @@ inline bool IsFunctionKey(int key_code) {
|
||||
}
|
||||
}
|
||||
|
||||
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down) {
|
||||
CGEventFlags ToCGEventFlags(uint32_t injected_flags) {
|
||||
CGEventFlags flags = 0;
|
||||
if ((injected_flags & kMacInjectedModifierShift) != 0) {
|
||||
flags |= kCGEventFlagMaskShift;
|
||||
}
|
||||
if ((injected_flags & kMacInjectedModifierControl) != 0) {
|
||||
flags |= kCGEventFlagMaskControl;
|
||||
}
|
||||
if ((injected_flags & kMacInjectedModifierOption) != 0) {
|
||||
flags |= kCGEventFlagMaskAlternate;
|
||||
}
|
||||
if ((injected_flags & kMacInjectedModifierCommand) != 0) {
|
||||
flags |= kCGEventFlagMaskCommand;
|
||||
}
|
||||
return flags;
|
||||
}
|
||||
|
||||
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down,
|
||||
uint32_t scan_code, bool extended) {
|
||||
(void)scan_code;
|
||||
(void)extended;
|
||||
const uint32_t injected_flags =
|
||||
injected_modifier_state_.Update(key_code, is_down);
|
||||
|
||||
if (vkCodeToCGKeyCode.find(key_code) != vkCodeToCGKeyCode.end()) {
|
||||
CGKeyCode cg_key_code = vkCodeToCGKeyCode[key_code];
|
||||
CGEventRef event = CGEventCreateKeyboardEvent(NULL, cg_key_code, is_down);
|
||||
CGEventRef clearFlags =
|
||||
CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, true);
|
||||
CGEventSetFlags(clearFlags, 0);
|
||||
if (!event) {
|
||||
LOG_ERROR("CGEventCreateKeyboardEvent failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
CGEventSetFlags(event, ToCGEventFlags(injected_flags));
|
||||
CGEventPost(kCGHIDEventTap, event);
|
||||
CFRelease(event);
|
||||
|
||||
@@ -181,6 +310,10 @@ int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down) {
|
||||
if (IsFunctionKey(cg_key_code) && !is_down) {
|
||||
CGEventRef fn_release_event =
|
||||
CGEventCreateKeyboardEvent(NULL, fn_key_code_, false);
|
||||
if (!fn_release_event) {
|
||||
LOG_ERROR("CGEventCreateKeyboardEvent failed for fn release");
|
||||
return -1;
|
||||
}
|
||||
CGEventPost(kCGHIDEventTap, fn_release_event);
|
||||
CFRelease(fn_release_event);
|
||||
}
|
||||
@@ -188,4 +321,4 @@ int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
|
||||
#include "device_controller.h"
|
||||
#include "macos_keyboard_modifier_state.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
@@ -21,11 +22,13 @@ class KeyboardCapturer : public DeviceController {
|
||||
public:
|
||||
virtual int Hook(OnKeyAction on_key_action, void* user_ptr);
|
||||
virtual int Unhook();
|
||||
virtual int SendKeyboardCommand(int key_code, bool is_down);
|
||||
virtual int SendKeyboardCommand(int key_code, bool is_down,
|
||||
uint32_t scan_code = 0,
|
||||
bool extended = false);
|
||||
|
||||
private:
|
||||
CFMachPortRef event_tap_;
|
||||
CFRunLoopSourceRef run_loop_source_;
|
||||
CFMachPortRef event_tap_ = nullptr;
|
||||
CFRunLoopSourceRef run_loop_source_ = nullptr;
|
||||
|
||||
public:
|
||||
bool caps_lock_flag_ = false;
|
||||
@@ -34,6 +37,7 @@ class KeyboardCapturer : public DeviceController {
|
||||
bool option_flag_ = false;
|
||||
bool command_flag_ = false;
|
||||
int fn_key_code_ = 0x3F;
|
||||
MacKeyboardModifierState injected_modifier_state_;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -7,14 +7,56 @@ namespace crossdesk {
|
||||
static OnKeyAction g_on_key_action = nullptr;
|
||||
static void* g_user_ptr = nullptr;
|
||||
|
||||
static int NormalizeModifierVkCode(const KBDLLHOOKSTRUCT* kb_data) {
|
||||
if (kb_data == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (kb_data->vkCode != VK_SHIFT && kb_data->vkCode != VK_CONTROL &&
|
||||
kb_data->vkCode != VK_MENU) {
|
||||
return static_cast<int>(kb_data->vkCode);
|
||||
}
|
||||
|
||||
UINT scan_code = static_cast<UINT>(kb_data->scanCode & 0xFF);
|
||||
if ((kb_data->flags & LLKHF_EXTENDED) != 0) {
|
||||
scan_code |= 0xE000;
|
||||
}
|
||||
|
||||
const UINT normalized_vk = MapVirtualKeyW(scan_code, MAPVK_VSC_TO_VK_EX);
|
||||
if (normalized_vk != 0) {
|
||||
return static_cast<int>(normalized_vk);
|
||||
}
|
||||
|
||||
return static_cast<int>(kb_data->vkCode);
|
||||
}
|
||||
|
||||
static bool PreferSideSpecificVkInjection(int key_code) {
|
||||
switch (key_code) {
|
||||
case VK_LSHIFT:
|
||||
case VK_RSHIFT:
|
||||
case VK_LCONTROL:
|
||||
case VK_RCONTROL:
|
||||
case VK_LMENU:
|
||||
case VK_RMENU:
|
||||
case VK_LWIN:
|
||||
case VK_RWIN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
|
||||
if (nCode == HC_ACTION && g_on_key_action) {
|
||||
KBDLLHOOKSTRUCT* kbData = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
|
||||
const int key_code = NormalizeModifierVkCode(kbData);
|
||||
|
||||
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
|
||||
g_on_key_action(kbData->vkCode, true, g_user_ptr);
|
||||
g_on_key_action(key_code, true, kbData->scanCode,
|
||||
(kbData->flags & LLKHF_EXTENDED) != 0, g_user_ptr);
|
||||
} else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
|
||||
g_on_key_action(kbData->vkCode, false, g_user_ptr);
|
||||
g_on_key_action(key_code, false, kbData->scanCode,
|
||||
(kbData->flags & LLKHF_EXTENDED) != 0, g_user_ptr);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -49,16 +91,53 @@ int KeyboardCapturer::Unhook() {
|
||||
}
|
||||
|
||||
// apply remote keyboard commands to the local machine
|
||||
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down) {
|
||||
int KeyboardCapturer::SendKeyboardCommand(int key_code, bool is_down,
|
||||
uint32_t scan_code, bool extended) {
|
||||
INPUT input = {0};
|
||||
input.type = INPUT_KEYBOARD;
|
||||
input.ki.wVk = (WORD)key_code;
|
||||
|
||||
const bool prefer_vk = PreferSideSpecificVkInjection(key_code);
|
||||
const UINT resolved_scan_code =
|
||||
scan_code != 0
|
||||
? static_cast<UINT>(scan_code & 0xFF) | (extended ? 0xE000u : 0u)
|
||||
: MapVirtualKeyW(static_cast<UINT>(key_code), MAPVK_VK_TO_VSC_EX);
|
||||
|
||||
if (scan_code != 0 && !prefer_vk) {
|
||||
input.ki.wVk = 0;
|
||||
input.ki.wScan = static_cast<WORD>(scan_code & 0xFF);
|
||||
input.ki.dwFlags |= KEYEVENTF_SCANCODE;
|
||||
if (extended) {
|
||||
input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
|
||||
}
|
||||
} else {
|
||||
input.ki.wVk = (WORD)key_code;
|
||||
|
||||
if (prefer_vk && resolved_scan_code != 0) {
|
||||
input.ki.wScan = static_cast<WORD>(resolved_scan_code & 0xFF);
|
||||
if ((resolved_scan_code & 0xFF00) != 0) {
|
||||
input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
|
||||
}
|
||||
} else if (resolved_scan_code != 0) {
|
||||
input.ki.wVk = 0;
|
||||
input.ki.wScan = static_cast<WORD>(resolved_scan_code & 0xFF);
|
||||
input.ki.dwFlags |= KEYEVENTF_SCANCODE;
|
||||
if ((resolved_scan_code & 0xFF00) != 0) {
|
||||
input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_down) {
|
||||
input.ki.dwFlags = KEYEVENTF_KEYUP;
|
||||
input.ki.dwFlags |= KEYEVENTF_KEYUP;
|
||||
}
|
||||
|
||||
UINT sent = SendInput(1, &input, sizeof(INPUT));
|
||||
if (sent != 1) {
|
||||
LOG_WARN("SendInput failed for key_code={}, is_down={}, err={}", key_code,
|
||||
is_down, GetLastError());
|
||||
return -1;
|
||||
}
|
||||
SendInput(1, &input, sizeof(INPUT));
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -21,7 +21,9 @@ class KeyboardCapturer : public DeviceController {
|
||||
public:
|
||||
virtual int Hook(OnKeyAction on_key_action, void* user_ptr);
|
||||
virtual int Unhook();
|
||||
virtual int SendKeyboardCommand(int key_code, bool is_down);
|
||||
virtual int SendKeyboardCommand(int key_code, bool is_down,
|
||||
uint32_t scan_code = 0,
|
||||
bool extended = false);
|
||||
|
||||
private:
|
||||
HHOOK keyboard_hook_ = nullptr;
|
||||
|
||||
@@ -73,13 +73,13 @@ std::map<int, int> vkCodeToCGKeyCode = {
|
||||
{0x20, 0x31}, // Space
|
||||
{0x08, 0x33}, // Backspace
|
||||
{0x09, 0x30}, // Tab
|
||||
{0x2C, 0x74}, // Print Screen
|
||||
{0x2C, 0x69}, // Print Screen(F13)
|
||||
{0x2D, 0x72}, // Insert
|
||||
{0x2E, 0x75}, // Delete
|
||||
{0x24, 0x73}, // Home
|
||||
{0x23, 0x77}, // End
|
||||
{0x21, 0x79}, // Page Up
|
||||
{0x22, 0x7A}, // Page Down
|
||||
{0x21, 0x74}, // Page Up
|
||||
{0x22, 0x79}, // Page Down
|
||||
|
||||
// arrow keys
|
||||
{0x25, 0x7B}, // Left Arrow
|
||||
@@ -98,6 +98,7 @@ std::map<int, int> vkCodeToCGKeyCode = {
|
||||
{0x67, 0x59}, // Numpad 7
|
||||
{0x68, 0x5B}, // Numpad 8
|
||||
{0x69, 0x5C}, // Numpad 9
|
||||
{0x90, 0x47}, // Num Lock / Keypad Clear
|
||||
{0x6E, 0x41}, // Numpad .
|
||||
{0x6F, 0x4B}, // Numpad /
|
||||
{0x6A, 0x43}, // Numpad *
|
||||
@@ -191,13 +192,13 @@ std::map<int, int> CGKeyCodeToVkCode = {
|
||||
{0x31, 0x20}, // Space
|
||||
{0x33, 0x08}, // Backspace
|
||||
{0x30, 0x09}, // Tab
|
||||
{0x74, 0x2C}, // Print Screen
|
||||
{0x69, 0x2C}, // Print Screen(F13)
|
||||
{0x72, 0x2D}, // Insert
|
||||
{0x75, 0x2E}, // Delete
|
||||
{0x73, 0x24}, // Home
|
||||
{0x77, 0x23}, // End
|
||||
{0x79, 0x21}, // Page Up
|
||||
{0x7A, 0x22}, // Page Down
|
||||
{0x74, 0x21}, // Page Up
|
||||
{0x79, 0x22}, // Page Down
|
||||
|
||||
// arrow keys
|
||||
{0x7B, 0x25}, // Left Arrow
|
||||
@@ -216,6 +217,7 @@ std::map<int, int> CGKeyCodeToVkCode = {
|
||||
{0x59, 0x67}, // Numpad 7
|
||||
{0x5B, 0x68}, // Numpad 8
|
||||
{0x5C, 0x69}, // Numpad 9
|
||||
{0x47, 0x90}, // Num Lock / Keypad Clear
|
||||
{0x41, 0x6E}, // Numpad .
|
||||
{0x4B, 0x6F}, // Numpad /
|
||||
{0x43, 0x6A}, // Numpad *
|
||||
@@ -326,21 +328,22 @@ std::map<int, int> vkCodeToX11KeySym = {
|
||||
{0x28, 0xFF54}, // Down Arrow
|
||||
|
||||
// numpad
|
||||
{0x60, 0x0030}, // Numpad 0
|
||||
{0x61, 0x0031}, // Numpad 1
|
||||
{0x62, 0x0032}, // Numpad 2
|
||||
{0x63, 0x0033}, // Numpad 3
|
||||
{0x64, 0x0034}, // Numpad 4
|
||||
{0x65, 0x0035}, // Numpad 5
|
||||
{0x66, 0x0036}, // Numpad 6
|
||||
{0x67, 0x0037}, // Numpad 7
|
||||
{0x68, 0x0038}, // Numpad 8
|
||||
{0x69, 0x0039}, // Numpad 9
|
||||
{0x6E, 0x003A}, // Numpad .
|
||||
{0x6F, 0x002F}, // Numpad /
|
||||
{0x6A, 0x002A}, // Numpad *
|
||||
{0x6D, 0x002D}, // Numpad -
|
||||
{0x6B, 0x002B}, // Numpad +
|
||||
{0x60, 0xFFB0}, // Numpad 0
|
||||
{0x61, 0xFFB1}, // Numpad 1
|
||||
{0x62, 0xFFB2}, // Numpad 2
|
||||
{0x63, 0xFFB3}, // Numpad 3
|
||||
{0x64, 0xFFB4}, // Numpad 4
|
||||
{0x65, 0xFFB5}, // Numpad 5
|
||||
{0x66, 0xFFB6}, // Numpad 6
|
||||
{0x67, 0xFFB7}, // Numpad 7
|
||||
{0x68, 0xFFB8}, // Numpad 8
|
||||
{0x69, 0xFFB9}, // Numpad 9
|
||||
{0x90, 0xFF7F}, // Num Lock
|
||||
{0x6E, 0xFFAE}, // Numpad .
|
||||
{0x6F, 0xFFAF}, // Numpad /
|
||||
{0x6A, 0xFFAA}, // Numpad *
|
||||
{0x6D, 0xFFAD}, // Numpad -
|
||||
{0x6B, 0xFFAB}, // Numpad +
|
||||
|
||||
// symbol keys
|
||||
{0xBA, 0x003B}, // ; (Semicolon)
|
||||
@@ -454,21 +457,22 @@ std::map<int, int> x11KeySymToVkCode = {
|
||||
{0xFF54, 0x28}, // Down Arrow
|
||||
|
||||
// numpad
|
||||
{0x0030, 0x60}, // Numpad 0
|
||||
{0x0031, 0x61}, // Numpad 1
|
||||
{0x0032, 0x62}, // Numpad 2
|
||||
{0x0033, 0x63}, // Numpad 3
|
||||
{0x0034, 0x64}, // Numpad 4
|
||||
{0x0035, 0x65}, // Numpad 5
|
||||
{0x0036, 0x66}, // Numpad 6
|
||||
{0x0037, 0x67}, // Numpad 7
|
||||
{0x0038, 0x68}, // Numpad 8
|
||||
{0x0039, 0x69}, // Numpad 9
|
||||
{0x003A, 0x6E}, // Numpad .
|
||||
{0x002F, 0x6F}, // Numpad /
|
||||
{0x002A, 0x6A}, // Numpad *
|
||||
{0x002D, 0x6D}, // Numpad -
|
||||
{0x002B, 0x6B}, // Numpad +
|
||||
{0xFFB0, 0x60}, // Numpad 0
|
||||
{0xFFB1, 0x61}, // Numpad 1
|
||||
{0xFFB2, 0x62}, // Numpad 2
|
||||
{0xFFB3, 0x63}, // Numpad 3
|
||||
{0xFFB4, 0x64}, // Numpad 4
|
||||
{0xFFB5, 0x65}, // Numpad 5
|
||||
{0xFFB6, 0x66}, // Numpad 6
|
||||
{0xFFB7, 0x67}, // Numpad 7
|
||||
{0xFFB8, 0x68}, // Numpad 8
|
||||
{0xFFB9, 0x69}, // Numpad 9
|
||||
{0xFF7F, 0x90}, // Num Lock
|
||||
{0xFFAE, 0x6E}, // Numpad .
|
||||
{0xFFAF, 0x6F}, // Numpad /
|
||||
{0xFFAA, 0x6A}, // Numpad *
|
||||
{0xFFAD, 0x6D}, // Numpad -
|
||||
{0xFFAB, 0x6B}, // Numpad +
|
||||
|
||||
// symbol keys
|
||||
{0x003B, 0xBA}, // ; (Semicolon)
|
||||
@@ -557,13 +561,13 @@ std::map<int, int> cgKeyCodeToX11KeySym = {
|
||||
{0x31, 0x0020}, // Space
|
||||
{0x33, 0xFF08}, // Backspace
|
||||
{0x30, 0xFF09}, // Tab
|
||||
{0x74, 0xFF15}, // Print Screen
|
||||
{0x69, 0xFF15}, // Print Screen(F13)
|
||||
{0x72, 0xFF63}, // Insert
|
||||
{0x75, 0xFFFF}, // Delete
|
||||
{0x73, 0xFF50}, // Home
|
||||
{0x77, 0xFF57}, // End
|
||||
{0x79, 0xFF55}, // Page Up
|
||||
{0x7A, 0xFF56}, // Page Down
|
||||
{0x74, 0xFF55}, // Page Up
|
||||
{0x79, 0xFF56}, // Page Down
|
||||
|
||||
// arrow keys
|
||||
{0x7B, 0xFF51}, // Left Arrow
|
||||
@@ -572,21 +576,22 @@ std::map<int, int> cgKeyCodeToX11KeySym = {
|
||||
{0x7D, 0xFF54}, // Down Arrow
|
||||
|
||||
// numpad
|
||||
{0x52, 0x0030}, // Numpad 0
|
||||
{0x53, 0x0031}, // Numpad 1
|
||||
{0x54, 0x0032}, // Numpad 2
|
||||
{0x55, 0x0033}, // Numpad 3
|
||||
{0x56, 0x0034}, // Numpad 4
|
||||
{0x57, 0x0035}, // Numpad 5
|
||||
{0x58, 0x0036}, // Numpad 6
|
||||
{0x59, 0x0037}, // Numpad 7
|
||||
{0x5B, 0x0038}, // Numpad 8
|
||||
{0x5C, 0x0039}, // Numpad 9
|
||||
{0x41, 0x003A}, // Numpad .
|
||||
{0x4B, 0x002F}, // Numpad /
|
||||
{0x43, 0x002A}, // Numpad *
|
||||
{0x4E, 0x002D}, // Numpad -
|
||||
{0x45, 0x002B}, // Numpad +
|
||||
{0x52, 0xFFB0}, // Numpad 0
|
||||
{0x53, 0xFFB1}, // Numpad 1
|
||||
{0x54, 0xFFB2}, // Numpad 2
|
||||
{0x55, 0xFFB3}, // Numpad 3
|
||||
{0x56, 0xFFB4}, // Numpad 4
|
||||
{0x57, 0xFFB5}, // Numpad 5
|
||||
{0x58, 0xFFB6}, // Numpad 6
|
||||
{0x59, 0xFFB7}, // Numpad 7
|
||||
{0x5B, 0xFFB8}, // Numpad 8
|
||||
{0x5C, 0xFFB9}, // Numpad 9
|
||||
{0x47, 0xFF7F}, // Num Lock / Keypad Clear
|
||||
{0x41, 0xFFAE}, // Numpad .
|
||||
{0x4B, 0xFFAF}, // Numpad /
|
||||
{0x43, 0xFFAA}, // Numpad *
|
||||
{0x4E, 0xFFAD}, // Numpad -
|
||||
{0x45, 0xFFAB}, // Numpad +
|
||||
|
||||
// symbol keys
|
||||
{0x29, 0x003B}, // ; (Semicolon)
|
||||
@@ -683,13 +688,13 @@ std::map<int, int> x11KeySymToCgKeyCode = {
|
||||
{0x0020, 0x31}, // Space
|
||||
{0xFF08, 0x33}, // Backspace
|
||||
{0xFF09, 0x30}, // Tab
|
||||
{0xFF15, 0x74}, // Print Screen
|
||||
{0xFF15, 0x69}, // Print Screen(F13)
|
||||
{0xFF63, 0x72}, // Insert
|
||||
{0xFFFF, 0x75}, // Delete
|
||||
{0xFF50, 0x73}, // Home
|
||||
{0xFF57, 0x77}, // End
|
||||
{0xFF55, 0x79}, // Page Up
|
||||
{0xFF56, 0x7A}, // Page Down
|
||||
{0xFF55, 0x74}, // Page Up
|
||||
{0xFF56, 0x79}, // Page Down
|
||||
|
||||
// arrow keys
|
||||
{0xFF51, 0x7B}, // Left Arrow
|
||||
@@ -698,21 +703,22 @@ std::map<int, int> x11KeySymToCgKeyCode = {
|
||||
{0xFF54, 0x7D}, // Down Arrow
|
||||
|
||||
// numpad
|
||||
{0x0030, 0x52}, // Numpad 0
|
||||
{0x0031, 0x53}, // Numpad 1
|
||||
{0x0032, 0x54}, // Numpad 2
|
||||
{0x0033, 0x55}, // Numpad 3
|
||||
{0x0034, 0x56}, // Numpad 4
|
||||
{0x0035, 0x57}, // Numpad 5
|
||||
{0x0036, 0x58}, // Numpad 6
|
||||
{0x0037, 0x59}, // Numpad 7
|
||||
{0x0038, 0x5B}, // Numpad 8
|
||||
{0x0039, 0x5C}, // Numpad 9
|
||||
{0x003A, 0x41}, // Numpad .
|
||||
{0x002F, 0x4B}, // Numpad /
|
||||
{0x002A, 0x43}, // Numpad *
|
||||
{0x002D, 0x4E}, // Numpad -
|
||||
{0x002B, 0x45}, // Numpad +
|
||||
{0xFFB0, 0x52}, // Numpad 0
|
||||
{0xFFB1, 0x53}, // Numpad 1
|
||||
{0xFFB2, 0x54}, // Numpad 2
|
||||
{0xFFB3, 0x55}, // Numpad 3
|
||||
{0xFFB4, 0x56}, // Numpad 4
|
||||
{0xFFB5, 0x57}, // Numpad 5
|
||||
{0xFFB6, 0x58}, // Numpad 6
|
||||
{0xFFB7, 0x59}, // Numpad 7
|
||||
{0xFFB8, 0x5B}, // Numpad 8
|
||||
{0xFFB9, 0x5C}, // Numpad 9
|
||||
{0xFF7F, 0x47}, // Num Lock / Keypad Clear
|
||||
{0xFFAE, 0x41}, // Numpad .
|
||||
{0xFFAF, 0x4B}, // Numpad /
|
||||
{0xFFAA, 0x43}, // Numpad *
|
||||
{0xFFAD, 0x4E}, // Numpad -
|
||||
{0xFFAB, 0x45}, // Numpad +
|
||||
|
||||
// symbol keys
|
||||
{0x003B, 0x29}, // ; (Semicolon)
|
||||
@@ -739,4 +745,4 @@ std::map<int, int> x11KeySymToCgKeyCode = {
|
||||
{0xFFEC, 0x36}, // Right Command
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-05-21
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _MACOS_KEYBOARD_MODIFIER_STATE_H_
|
||||
#define _MACOS_KEYBOARD_MODIFIER_STATE_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
inline constexpr uint32_t kMacInjectedModifierShift = 1u << 0;
|
||||
inline constexpr uint32_t kMacInjectedModifierControl = 1u << 1;
|
||||
inline constexpr uint32_t kMacInjectedModifierOption = 1u << 2;
|
||||
inline constexpr uint32_t kMacInjectedModifierCommand = 1u << 3;
|
||||
|
||||
class MacKeyboardModifierState {
|
||||
public:
|
||||
uint32_t Update(int key_code, bool is_down) {
|
||||
bool* state = MutableStateForVk(key_code);
|
||||
if (state != nullptr) {
|
||||
*state = is_down;
|
||||
}
|
||||
return flags();
|
||||
}
|
||||
|
||||
uint32_t flags() const {
|
||||
uint32_t result = 0;
|
||||
if (left_shift_down_ || right_shift_down_) {
|
||||
result |= kMacInjectedModifierShift;
|
||||
}
|
||||
if (left_control_down_ || right_control_down_) {
|
||||
result |= kMacInjectedModifierControl;
|
||||
}
|
||||
if (left_option_down_ || right_option_down_) {
|
||||
result |= kMacInjectedModifierOption;
|
||||
}
|
||||
if (left_command_down_ || right_command_down_) {
|
||||
result |= kMacInjectedModifierCommand;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
left_shift_down_ = false;
|
||||
right_shift_down_ = false;
|
||||
left_control_down_ = false;
|
||||
right_control_down_ = false;
|
||||
left_option_down_ = false;
|
||||
right_option_down_ = false;
|
||||
left_command_down_ = false;
|
||||
right_command_down_ = false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool* MutableStateForVk(int key_code) {
|
||||
switch (key_code) {
|
||||
case 0xA0: // VK_LSHIFT
|
||||
return &left_shift_down_;
|
||||
case 0xA1: // VK_RSHIFT
|
||||
return &right_shift_down_;
|
||||
case 0xA2: // VK_LCONTROL
|
||||
return &left_control_down_;
|
||||
case 0xA3: // VK_RCONTROL
|
||||
return &right_control_down_;
|
||||
case 0xA4: // VK_LMENU / left Option
|
||||
return &left_option_down_;
|
||||
case 0xA5: // VK_RMENU / right Option
|
||||
return &right_option_down_;
|
||||
case 0x5B: // VK_LWIN / left Command
|
||||
return &left_command_down_;
|
||||
case 0x5C: // VK_RWIN / right Command
|
||||
return &right_command_down_;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool left_shift_down_ = false;
|
||||
bool right_shift_down_ = false;
|
||||
bool left_control_down_ = false;
|
||||
bool right_control_down_ = false;
|
||||
bool left_option_down_ = false;
|
||||
bool right_option_down_ = false;
|
||||
bool left_command_down_ = false;
|
||||
bool right_command_down_ = false;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <X11/extensions/XTest.h>
|
||||
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
@@ -12,6 +13,17 @@ MouseController::~MouseController() { Destroy(); }
|
||||
|
||||
int MouseController::Init(std::vector<DisplayInfo> display_info_list) {
|
||||
display_info_list_ = display_info_list;
|
||||
|
||||
if (IsWaylandSession()) {
|
||||
if (InitWaylandPortal()) {
|
||||
use_wayland_portal_ = true;
|
||||
LOG_INFO("Mouse controller initialized with Wayland portal backend");
|
||||
return 0;
|
||||
}
|
||||
LOG_WARN(
|
||||
"Wayland mouse control init failed, falling back to X11/XTest backend");
|
||||
}
|
||||
|
||||
display_ = XOpenDisplay(NULL);
|
||||
if (!display_) {
|
||||
LOG_ERROR("Cannot connect to X server");
|
||||
@@ -25,26 +37,68 @@ int MouseController::Init(std::vector<DisplayInfo> display_info_list) {
|
||||
&minor_version)) {
|
||||
LOG_ERROR("XTest extension not available");
|
||||
XCloseDisplay(display_);
|
||||
display_ = nullptr;
|
||||
return -2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MouseController::UpdateDisplayInfoList(
|
||||
const std::vector<DisplayInfo>& display_info_list) {
|
||||
if (display_info_list.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
display_info_list_ = display_info_list;
|
||||
if (use_wayland_portal_) {
|
||||
OnWaylandDisplayInfoListUpdated();
|
||||
}
|
||||
|
||||
if (last_display_index_ < 0 ||
|
||||
last_display_index_ >= static_cast<int>(display_info_list_.size())) {
|
||||
last_display_index_ = -1;
|
||||
last_norm_x_ = -1.0;
|
||||
last_norm_y_ = -1.0;
|
||||
}
|
||||
}
|
||||
|
||||
int MouseController::Destroy() {
|
||||
CleanupWaylandPortal();
|
||||
|
||||
if (display_) {
|
||||
XCloseDisplay(display_);
|
||||
display_ = nullptr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MouseController::SendMouseCommand(RemoteAction remote_action,
|
||||
int display_index) {
|
||||
if (remote_action.type != ControlType::mouse) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (use_wayland_portal_) {
|
||||
return SendWaylandMouseCommand(remote_action, display_index);
|
||||
}
|
||||
|
||||
if (!display_) {
|
||||
LOG_ERROR("X11 display not initialized");
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (remote_action.type) {
|
||||
case mouse:
|
||||
switch (remote_action.m.flag) {
|
||||
case MouseFlag::move:
|
||||
case MouseFlag::move: {
|
||||
if (display_index < 0 ||
|
||||
display_index >= static_cast<int>(display_info_list_.size())) {
|
||||
LOG_ERROR("Invalid display index: {}", display_index);
|
||||
return -2;
|
||||
}
|
||||
|
||||
SetMousePosition(
|
||||
static_cast<int>(remote_action.m.x *
|
||||
display_info_list_[display_index].width +
|
||||
@@ -53,6 +107,7 @@ int MouseController::SendMouseCommand(RemoteAction remote_action,
|
||||
display_info_list_[display_index].height +
|
||||
display_info_list_[display_index].top));
|
||||
break;
|
||||
}
|
||||
case MouseFlag::left_down:
|
||||
XTestFakeButtonEvent(display_, 1, True, CurrentTime);
|
||||
XFlush(display_);
|
||||
@@ -103,25 +158,39 @@ int MouseController::SendMouseCommand(RemoteAction remote_action,
|
||||
}
|
||||
|
||||
void MouseController::SetMousePosition(int x, int y) {
|
||||
if (!display_) {
|
||||
return;
|
||||
}
|
||||
XWarpPointer(display_, None, root_, 0, 0, 0, 0, x, y);
|
||||
XFlush(display_);
|
||||
}
|
||||
|
||||
void MouseController::SimulateKeyDown(int kval) {
|
||||
if (!display_) {
|
||||
return;
|
||||
}
|
||||
XTestFakeKeyEvent(display_, kval, True, CurrentTime);
|
||||
XFlush(display_);
|
||||
}
|
||||
|
||||
void MouseController::SimulateKeyUp(int kval) {
|
||||
if (!display_) {
|
||||
return;
|
||||
}
|
||||
XTestFakeKeyEvent(display_, kval, False, CurrentTime);
|
||||
XFlush(display_);
|
||||
}
|
||||
|
||||
void MouseController::SimulateMouseWheel(int direction_button, int count) {
|
||||
if (!display_) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
XTestFakeButtonEvent(display_, direction_button, True, CurrentTime);
|
||||
XTestFakeButtonEvent(display_, direction_button, False, CurrentTime);
|
||||
}
|
||||
XFlush(display_);
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -11,10 +11,16 @@
|
||||
#include <X11/Xutil.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "device_controller.h"
|
||||
|
||||
struct DBusConnection;
|
||||
struct DBusMessageIter;
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class MouseController : public DeviceController {
|
||||
@@ -26,18 +32,49 @@ class MouseController : public DeviceController {
|
||||
virtual int Init(std::vector<DisplayInfo> display_info_list);
|
||||
virtual int Destroy();
|
||||
virtual int SendMouseCommand(RemoteAction remote_action, int display_index);
|
||||
void UpdateDisplayInfoList(const std::vector<DisplayInfo>& display_info_list);
|
||||
|
||||
private:
|
||||
void SimulateKeyDown(int kval);
|
||||
void SimulateKeyUp(int kval);
|
||||
void SetMousePosition(int x, int y);
|
||||
void SimulateMouseWheel(int direction_button, int count);
|
||||
bool InitWaylandPortal();
|
||||
void CleanupWaylandPortal();
|
||||
int SendWaylandMouseCommand(RemoteAction remote_action, int display_index);
|
||||
void OnWaylandDisplayInfoListUpdated();
|
||||
bool NotifyWaylandPointerMotion(double dx, double dy);
|
||||
bool NotifyWaylandPointerMotionAbsolute(uint32_t stream, double x, double y);
|
||||
bool NotifyWaylandPointerButton(int button, uint32_t state);
|
||||
bool NotifyWaylandPointerAxisDiscrete(uint32_t axis, int32_t steps);
|
||||
bool SendWaylandPortalVoidCall(
|
||||
const char* method_name,
|
||||
const std::function<void(DBusMessageIter*)>& append_args);
|
||||
|
||||
enum class WaylandAbsoluteMode { kUnknown, kPixels, kNormalized, kDisabled };
|
||||
|
||||
Display* display_ = nullptr;
|
||||
Window root_ = 0;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
int screen_width_ = 0;
|
||||
int screen_height_ = 0;
|
||||
bool use_wayland_portal_ = false;
|
||||
|
||||
DBusConnection* dbus_connection_ = nullptr;
|
||||
std::string wayland_session_handle_;
|
||||
int last_display_index_ = -1;
|
||||
double last_norm_x_ = -1.0;
|
||||
double last_norm_y_ = -1.0;
|
||||
bool logged_wayland_display_info_ = false;
|
||||
uintptr_t last_logged_wayland_stream_ = 0;
|
||||
int last_logged_wayland_width_ = 0;
|
||||
int last_logged_wayland_height_ = 0;
|
||||
WaylandAbsoluteMode wayland_absolute_mode_ = WaylandAbsoluteMode::kUnknown;
|
||||
bool wayland_absolute_disabled_logged_ = false;
|
||||
uint32_t wayland_absolute_stream_id_ = 0;
|
||||
int wayland_portal_space_width_ = 0;
|
||||
int wayland_portal_space_height_ = 0;
|
||||
bool using_shared_wayland_session_ = false;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,9 +2,35 @@
|
||||
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
namespace {
|
||||
|
||||
constexpr auto kDoubleClickInterval = std::chrono::milliseconds(500);
|
||||
constexpr int kDoubleClickMaxDistance = 8;
|
||||
constexpr int kMaxClickState = 3;
|
||||
|
||||
bool IsWithinClickDistance(int x1, int y1, int x2, int y2) {
|
||||
return std::abs(x1 - x2) <= kDoubleClickMaxDistance &&
|
||||
std::abs(y1 - y2) <= kDoubleClickMaxDistance;
|
||||
}
|
||||
|
||||
void SetClickState(CGEventRef event, int click_state) {
|
||||
if (!event) {
|
||||
return;
|
||||
}
|
||||
|
||||
CGEventSetIntegerValueField(
|
||||
event, kCGMouseEventClickState,
|
||||
std::max(1, std::min(click_state, kMaxClickState)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
MouseController::MouseController() {}
|
||||
|
||||
@@ -18,87 +44,144 @@ int MouseController::Init(std::vector<DisplayInfo> display_info_list) {
|
||||
|
||||
int MouseController::Destroy() { return 0; }
|
||||
|
||||
int MouseController::BeginClick(ClickTracker& tracker, int x, int y) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
const bool continues_previous_click =
|
||||
tracker.has_last_down &&
|
||||
now - tracker.last_down_time <= kDoubleClickInterval &&
|
||||
IsWithinClickDistance(tracker.last_down_x, tracker.last_down_y, x, y);
|
||||
|
||||
tracker.click_state = continues_previous_click
|
||||
? std::min(tracker.click_state + 1, kMaxClickState)
|
||||
: 1;
|
||||
tracker.active_click_state = tracker.click_state;
|
||||
tracker.has_last_down = true;
|
||||
tracker.last_down_time = now;
|
||||
tracker.last_down_x = x;
|
||||
tracker.last_down_y = y;
|
||||
|
||||
return tracker.active_click_state;
|
||||
}
|
||||
|
||||
int MouseController::EndClick(ClickTracker& tracker, int x, int y) {
|
||||
const int click_state = tracker.active_click_state;
|
||||
if (!IsWithinClickDistance(tracker.last_down_x, tracker.last_down_y, x, y)) {
|
||||
tracker.has_last_down = false;
|
||||
tracker.click_state = 0;
|
||||
tracker.active_click_state = 1;
|
||||
}
|
||||
|
||||
return click_state;
|
||||
}
|
||||
|
||||
int MouseController::SendMouseCommand(RemoteAction remote_action,
|
||||
int display_index) {
|
||||
int mouse_pos_x =
|
||||
remote_action.m.x * display_info_list_[display_index].width +
|
||||
display_info_list_[display_index].left;
|
||||
int mouse_pos_y =
|
||||
remote_action.m.y * display_info_list_[display_index].height +
|
||||
display_info_list_[display_index].top;
|
||||
if (remote_action.type != ControlType::mouse) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (remote_action.type == ControlType::mouse) {
|
||||
CGEventRef mouse_event = nullptr;
|
||||
CGEventType mouse_type;
|
||||
CGMouseButton mouse_button;
|
||||
CGPoint mouse_point = CGPointMake(mouse_pos_x, mouse_pos_y);
|
||||
if (display_index < 0 ||
|
||||
display_index >= static_cast<int>(display_info_list_.size())) {
|
||||
LOG_WARN("Mouse command skipped, invalid display_index={}, displays={}",
|
||||
display_index, display_info_list_.size());
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (remote_action.m.flag) {
|
||||
case MouseFlag::left_down:
|
||||
mouse_type = kCGEventLeftMouseDown;
|
||||
left_dragging_ = true;
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonLeft);
|
||||
break;
|
||||
case MouseFlag::left_up:
|
||||
mouse_type = kCGEventLeftMouseUp;
|
||||
left_dragging_ = false;
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonLeft);
|
||||
break;
|
||||
case MouseFlag::right_down:
|
||||
mouse_type = kCGEventRightMouseDown;
|
||||
right_dragging_ = true;
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonRight);
|
||||
break;
|
||||
case MouseFlag::right_up:
|
||||
mouse_type = kCGEventRightMouseUp;
|
||||
right_dragging_ = false;
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonRight);
|
||||
break;
|
||||
case MouseFlag::middle_down:
|
||||
mouse_type = kCGEventOtherMouseDown;
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonCenter);
|
||||
break;
|
||||
case MouseFlag::middle_up:
|
||||
mouse_type = kCGEventOtherMouseUp;
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonCenter);
|
||||
break;
|
||||
case MouseFlag::wheel_vertical:
|
||||
mouse_event = CGEventCreateScrollWheelEvent(
|
||||
NULL, kCGScrollEventUnitLine, 2, remote_action.m.s, 0);
|
||||
break;
|
||||
case MouseFlag::wheel_horizontal:
|
||||
mouse_event = CGEventCreateScrollWheelEvent(
|
||||
NULL, kCGScrollEventUnitLine, 2, 0, remote_action.m.s);
|
||||
break;
|
||||
default:
|
||||
if (left_dragging_) {
|
||||
mouse_type = kCGEventLeftMouseDragged;
|
||||
mouse_button = kCGMouseButtonLeft;
|
||||
} else if (right_dragging_) {
|
||||
mouse_type = kCGEventRightMouseDragged;
|
||||
mouse_button = kCGMouseButtonRight;
|
||||
} else {
|
||||
mouse_type = kCGEventMouseMoved;
|
||||
mouse_button = kCGMouseButtonLeft;
|
||||
}
|
||||
const DisplayInfo& display_info = display_info_list_[display_index];
|
||||
if (display_info.width <= 0 || display_info.height <= 0) {
|
||||
LOG_WARN("Mouse command skipped, invalid display geometry: {}x{}",
|
||||
display_info.width, display_info.height);
|
||||
return -1;
|
||||
}
|
||||
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
mouse_button);
|
||||
break;
|
||||
}
|
||||
const float normalized_x = std::clamp(remote_action.m.x, 0.0f, 1.0f);
|
||||
const float normalized_y = std::clamp(remote_action.m.y, 0.0f, 1.0f);
|
||||
int mouse_pos_x = normalized_x * display_info.width + display_info.left;
|
||||
int mouse_pos_y = normalized_y * display_info.height + display_info.top;
|
||||
|
||||
if (mouse_event) {
|
||||
CGEventPost(kCGHIDEventTap, mouse_event);
|
||||
CFRelease(mouse_event);
|
||||
}
|
||||
CGEventRef mouse_event = nullptr;
|
||||
CGEventType mouse_type;
|
||||
CGMouseButton mouse_button;
|
||||
CGPoint mouse_point = CGPointMake(mouse_pos_x, mouse_pos_y);
|
||||
int click_state = 1;
|
||||
|
||||
switch (remote_action.m.flag) {
|
||||
case MouseFlag::left_down:
|
||||
mouse_type = kCGEventLeftMouseDown;
|
||||
left_dragging_ = true;
|
||||
click_state = BeginClick(left_click_tracker_, mouse_pos_x, mouse_pos_y);
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonLeft);
|
||||
SetClickState(mouse_event, click_state);
|
||||
break;
|
||||
case MouseFlag::left_up:
|
||||
mouse_type = kCGEventLeftMouseUp;
|
||||
left_dragging_ = false;
|
||||
click_state = EndClick(left_click_tracker_, mouse_pos_x, mouse_pos_y);
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonLeft);
|
||||
SetClickState(mouse_event, click_state);
|
||||
break;
|
||||
case MouseFlag::right_down:
|
||||
mouse_type = kCGEventRightMouseDown;
|
||||
right_dragging_ = true;
|
||||
click_state = BeginClick(right_click_tracker_, mouse_pos_x, mouse_pos_y);
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonRight);
|
||||
SetClickState(mouse_event, click_state);
|
||||
break;
|
||||
case MouseFlag::right_up:
|
||||
mouse_type = kCGEventRightMouseUp;
|
||||
right_dragging_ = false;
|
||||
click_state = EndClick(right_click_tracker_, mouse_pos_x, mouse_pos_y);
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonRight);
|
||||
SetClickState(mouse_event, click_state);
|
||||
break;
|
||||
case MouseFlag::middle_down:
|
||||
mouse_type = kCGEventOtherMouseDown;
|
||||
click_state = BeginClick(middle_click_tracker_, mouse_pos_x, mouse_pos_y);
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonCenter);
|
||||
SetClickState(mouse_event, click_state);
|
||||
break;
|
||||
case MouseFlag::middle_up:
|
||||
mouse_type = kCGEventOtherMouseUp;
|
||||
click_state = EndClick(middle_click_tracker_, mouse_pos_x, mouse_pos_y);
|
||||
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type, mouse_point,
|
||||
kCGMouseButtonCenter);
|
||||
SetClickState(mouse_event, click_state);
|
||||
break;
|
||||
case MouseFlag::wheel_vertical:
|
||||
mouse_event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine,
|
||||
2, remote_action.m.s, 0);
|
||||
break;
|
||||
case MouseFlag::wheel_horizontal:
|
||||
mouse_event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine,
|
||||
2, 0, remote_action.m.s);
|
||||
break;
|
||||
default:
|
||||
if (left_dragging_) {
|
||||
mouse_type = kCGEventLeftMouseDragged;
|
||||
mouse_button = kCGMouseButtonLeft;
|
||||
} else if (right_dragging_) {
|
||||
mouse_type = kCGEventRightMouseDragged;
|
||||
mouse_button = kCGMouseButtonRight;
|
||||
} else {
|
||||
mouse_type = kCGEventMouseMoved;
|
||||
mouse_button = kCGMouseButtonLeft;
|
||||
}
|
||||
|
||||
mouse_event =
|
||||
CGEventCreateMouseEvent(NULL, mouse_type, mouse_point, mouse_button);
|
||||
break;
|
||||
}
|
||||
|
||||
if (mouse_event) {
|
||||
CGEventPost(kCGHIDEventTap, mouse_event);
|
||||
CFRelease(mouse_event);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#ifndef _MOUSE_CONTROLLER_H_
|
||||
#define _MOUSE_CONTROLLER_H_
|
||||
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
#include "device_controller.h"
|
||||
@@ -24,9 +25,24 @@ class MouseController : public DeviceController {
|
||||
virtual int SendMouseCommand(RemoteAction remote_action, int display_index);
|
||||
|
||||
private:
|
||||
struct ClickTracker {
|
||||
bool has_last_down = false;
|
||||
std::chrono::steady_clock::time_point last_down_time{};
|
||||
int last_down_x = 0;
|
||||
int last_down_y = 0;
|
||||
int click_state = 0;
|
||||
int active_click_state = 1;
|
||||
};
|
||||
|
||||
int BeginClick(ClickTracker& tracker, int x, int y);
|
||||
int EndClick(ClickTracker& tracker, int x, int y);
|
||||
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
bool left_dragging_ = false;
|
||||
bool right_dragging_ = false;
|
||||
ClickTracker left_click_tracker_;
|
||||
ClickTracker right_click_tracker_;
|
||||
ClickTracker middle_click_tracker_;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "mouse_controller.h"
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
@@ -18,7 +20,14 @@ int MouseController::Destroy() { return 0; }
|
||||
|
||||
int MouseController::SendMouseCommand(RemoteAction remote_action,
|
||||
int display_index) {
|
||||
INPUT ip;
|
||||
if (display_index < 0 ||
|
||||
display_index >= static_cast<int>(display_info_list_.size())) {
|
||||
LOG_WARN("Mouse command skipped, invalid display_index={}, displays={}",
|
||||
display_index, display_info_list_.size());
|
||||
return -1;
|
||||
}
|
||||
|
||||
INPUT ip = {0};
|
||||
|
||||
if (remote_action.type == ControlType::mouse) {
|
||||
ip.type = INPUT_MOUSE;
|
||||
@@ -63,13 +72,25 @@ int MouseController::SendMouseCommand(RemoteAction remote_action,
|
||||
|
||||
ip.mi.time = 0;
|
||||
|
||||
SetCursorPos(ip.mi.dx, ip.mi.dy);
|
||||
if (!SetCursorPos(ip.mi.dx, ip.mi.dy)) {
|
||||
LOG_WARN("SetCursorPos failed for mouse x={}, y={}, flag={}, err={}",
|
||||
ip.mi.dx, ip.mi.dy, static_cast<int>(remote_action.m.flag),
|
||||
GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ip.mi.dwFlags != MOUSEEVENTF_MOVE) {
|
||||
SendInput(1, &ip, sizeof(INPUT));
|
||||
UINT sent = SendInput(1, &ip, sizeof(INPUT));
|
||||
if (sent != 1) {
|
||||
LOG_WARN(
|
||||
"SendInput failed for mouse x={}, y={}, wheel={}, flag={}, err={}",
|
||||
ip.mi.dx, ip.mi.dy, remote_action.m.s,
|
||||
static_cast<int>(remote_action.m.flag), GetLastError());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-05-07
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _WINDOWS_KEY_METADATA_H_
|
||||
#define _WINDOWS_KEY_METADATA_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
inline bool LookupWindowsKeyMetadataFromVk(int key_code,
|
||||
uint32_t* scan_code_out,
|
||||
bool* extended_out) {
|
||||
if (scan_code_out == nullptr || extended_out == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (key_code) {
|
||||
case 0x21: // Page Up
|
||||
*scan_code_out = 0x49;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x22: // Page Down
|
||||
*scan_code_out = 0x51;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x23: // End
|
||||
*scan_code_out = 0x4F;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x24: // Home
|
||||
*scan_code_out = 0x47;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x25: // Left Arrow
|
||||
*scan_code_out = 0x4B;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x26: // Up Arrow
|
||||
*scan_code_out = 0x48;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x27: // Right Arrow
|
||||
*scan_code_out = 0x4D;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x28: // Down Arrow
|
||||
*scan_code_out = 0x50;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x2D: // Insert
|
||||
*scan_code_out = 0x52;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x2E: // Delete
|
||||
*scan_code_out = 0x53;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x6F: // Numpad /
|
||||
*scan_code_out = 0x35;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0xA3: // Right Ctrl
|
||||
*scan_code_out = 0x1D;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0xA5: // Right Alt
|
||||
*scan_code_out = 0x38;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x5B: // Left Win
|
||||
*scan_code_out = 0x5B;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
case 0x5C: // Right Win
|
||||
*scan_code_out = 0x5C;
|
||||
*extended_out = true;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
+2587
-2333
File diff suppressed because it is too large
Load Diff
@@ -1,222 +1,156 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2024-05-29
|
||||
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
#ifndef _LOCALIZATION_H_
|
||||
#define _LOCALIZATION_H_
|
||||
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2024-05-29
|
||||
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
#ifndef _LOCALIZATION_H_
|
||||
#define _LOCALIZATION_H_
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "localization_data.h"
|
||||
|
||||
#if _WIN32
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
namespace crossdesk {
|
||||
namespace localization {
|
||||
|
||||
struct LanguageOption {
|
||||
std::string code;
|
||||
std::string display_name;
|
||||
};
|
||||
|
||||
namespace crossdesk {
|
||||
class LocalizedString {
|
||||
public:
|
||||
constexpr explicit LocalizedString(const char* key) : key_(key) {}
|
||||
const std::string& operator[](int language_index) const;
|
||||
|
||||
private:
|
||||
const char* key_;
|
||||
};
|
||||
|
||||
inline const std::vector<LanguageOption>& GetSupportedLanguages() {
|
||||
static const std::vector<LanguageOption> kSupportedLanguages = {
|
||||
{"zh-CN", reinterpret_cast<const char*>(u8"中文")},
|
||||
{"en-US", "English"},
|
||||
{"ru-RU", reinterpret_cast<const char*>(u8"Русский")}};
|
||||
return kSupportedLanguages;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
namespace localization {
|
||||
inline int ClampLanguageIndex(int language_index) {
|
||||
if (language_index >= 0 &&
|
||||
language_index < static_cast<int>(GetSupportedLanguages().size())) {
|
||||
return language_index;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static std::vector<std::string> local_desktop = {
|
||||
reinterpret_cast<const char*>(u8"本桌面"), "Local Desktop"};
|
||||
static std::vector<std::string> local_id = {
|
||||
reinterpret_cast<const char*>(u8"本机ID"), "Local ID"};
|
||||
static std::vector<std::string> local_id_copied_to_clipboard = {
|
||||
reinterpret_cast<const char*>(u8"已复制到剪贴板"), "Copied to clipboard"};
|
||||
static std::vector<std::string> password = {
|
||||
reinterpret_cast<const char*>(u8"密码"), "Password"};
|
||||
static std::vector<std::string> max_password_len = {
|
||||
reinterpret_cast<const char*>(u8"最大6个字符"), "Max 6 chars"};
|
||||
using TranslationTable =
|
||||
std::unordered_map<std::string,
|
||||
std::unordered_map<std::string, std::string>>;
|
||||
|
||||
static std::vector<std::string> remote_desktop = {
|
||||
reinterpret_cast<const char*>(u8"控制远程桌面"), "Control Remote Desktop"};
|
||||
static std::vector<std::string> remote_id = {
|
||||
reinterpret_cast<const char*>(u8"对端ID"), "Remote ID"};
|
||||
static std::vector<std::string> connect = {
|
||||
reinterpret_cast<const char*>(u8"连接"), "Connect"};
|
||||
static std::vector<std::string> recent_connections = {
|
||||
reinterpret_cast<const char*>(u8"近期连接"), "Recent Connections"};
|
||||
static std::vector<std::string> disconnect = {
|
||||
reinterpret_cast<const char*>(u8"断开连接"), "Disconnect"};
|
||||
static std::vector<std::string> fullscreen = {
|
||||
reinterpret_cast<const char*>(u8"全屏"), " Fullscreen"};
|
||||
static std::vector<std::string> show_net_traffic_stats = {
|
||||
reinterpret_cast<const char*>(u8"显示流量统计"), "Show Net Traffic Stats"};
|
||||
static std::vector<std::string> hide_net_traffic_stats = {
|
||||
reinterpret_cast<const char*>(u8"隐藏流量统计"), "Hide Net Traffic Stats"};
|
||||
static std::vector<std::string> video = {
|
||||
reinterpret_cast<const char*>(u8"视频"), "Video"};
|
||||
static std::vector<std::string> audio = {
|
||||
reinterpret_cast<const char*>(u8"音频"), "Audio"};
|
||||
static std::vector<std::string> data = {reinterpret_cast<const char*>(u8"数据"),
|
||||
"Data"};
|
||||
static std::vector<std::string> total = {
|
||||
reinterpret_cast<const char*>(u8"总计"), "Total"};
|
||||
static std::vector<std::string> in = {reinterpret_cast<const char*>(u8"输入"),
|
||||
"In"};
|
||||
static std::vector<std::string> out = {reinterpret_cast<const char*>(u8"输出"),
|
||||
"Out"};
|
||||
static std::vector<std::string> loss_rate = {
|
||||
reinterpret_cast<const char*>(u8"丢包率"), "Loss Rate"};
|
||||
static std::vector<std::string> exit_fullscreen = {
|
||||
reinterpret_cast<const char*>(u8"退出全屏"), "Exit fullscreen"};
|
||||
static std::vector<std::string> control_mouse = {
|
||||
reinterpret_cast<const char*>(u8"控制"), "Control"};
|
||||
static std::vector<std::string> release_mouse = {
|
||||
reinterpret_cast<const char*>(u8"释放"), "Release"};
|
||||
static std::vector<std::string> audio_capture = {
|
||||
reinterpret_cast<const char*>(u8"声音"), "Audio"};
|
||||
static std::vector<std::string> mute = {
|
||||
reinterpret_cast<const char*>(u8" 静音"), " Mute"};
|
||||
static std::vector<std::string> settings = {
|
||||
reinterpret_cast<const char*>(u8"设置"), "Settings"};
|
||||
static std::vector<std::string> language = {
|
||||
reinterpret_cast<const char*>(u8"语言:"), "Language:"};
|
||||
static std::vector<std::string> language_zh = {
|
||||
reinterpret_cast<const char*>(u8"中文"), "Chinese"};
|
||||
static std::vector<std::string> language_en = {
|
||||
reinterpret_cast<const char*>(u8"英文"), "English"};
|
||||
static std::vector<std::string> video_quality = {
|
||||
reinterpret_cast<const char*>(u8"视频质量:"), "Video Quality:"};
|
||||
static std::vector<std::string> video_frame_rate = {
|
||||
reinterpret_cast<const char*>(u8"画面采集帧率:"),
|
||||
"Video Capture Frame Rate:"};
|
||||
static std::vector<std::string> video_quality_high = {
|
||||
reinterpret_cast<const char*>(u8"高"), "High"};
|
||||
static std::vector<std::string> video_quality_medium = {
|
||||
reinterpret_cast<const char*>(u8"中"), "Medium"};
|
||||
static std::vector<std::string> video_quality_low = {
|
||||
reinterpret_cast<const char*>(u8"低"), "Low"};
|
||||
static std::vector<std::string> video_encode_format = {
|
||||
reinterpret_cast<const char*>(u8"视频编码格式:"), "Video Encode Format:"};
|
||||
static std::vector<std::string> av1 = {reinterpret_cast<const char*>(u8"AV1"),
|
||||
"AV1"};
|
||||
static std::vector<std::string> h264 = {
|
||||
reinterpret_cast<const char*>(u8"H.264"), "H.264"};
|
||||
static std::vector<std::string> enable_hardware_video_codec = {
|
||||
reinterpret_cast<const char*>(u8"启用硬件编解码器:"),
|
||||
"Enable Hardware Video Codec:"};
|
||||
static std::vector<std::string> enable_turn = {
|
||||
reinterpret_cast<const char*>(u8"启用中继服务:"), "Enable TURN Service:"};
|
||||
static std::vector<std::string> enable_srtp = {
|
||||
reinterpret_cast<const char*>(u8"启用SRTP:"), "Enable SRTP:"};
|
||||
static std::vector<std::string> self_hosted_server_config = {
|
||||
reinterpret_cast<const char*>(u8"自托管服务器配置"),
|
||||
"Self-Hosted Server Config"};
|
||||
static std::vector<std::string> self_hosted_server_settings = {
|
||||
reinterpret_cast<const char*>(u8"自托管服务器设置"),
|
||||
"Self-Hosted Server Settings"};
|
||||
static std::vector<std::string> self_hosted_server_address = {
|
||||
reinterpret_cast<const char*>(u8"服务器地址:"), "Server Address:"};
|
||||
static std::vector<std::string> self_hosted_server_port = {
|
||||
reinterpret_cast<const char*>(u8"信令服务端口:"), "Signal Service Port:"};
|
||||
static std::vector<std::string> self_hosted_server_coturn_server_port = {
|
||||
reinterpret_cast<const char*>(u8"中继服务端口:"), "Relay Service Port:"};
|
||||
static std::vector<std::string> self_hosted_server_certificate_path = {
|
||||
reinterpret_cast<const char*>(u8"证书文件路径:"), "Certificate File Path:"};
|
||||
static std::vector<std::string> select_a_file = {
|
||||
reinterpret_cast<const char*>(u8"请选择文件"), "Please select a file"};
|
||||
static std::vector<std::string> reset_cert_fingerprint = {
|
||||
reinterpret_cast<const char*>(u8"重置证书指纹"),
|
||||
"Reset Certificate Fingerprint"};
|
||||
static std::vector<std::string> ok = {reinterpret_cast<const char*>(u8"确认"),
|
||||
"OK"};
|
||||
static std::vector<std::string> cancel = {
|
||||
reinterpret_cast<const char*>(u8"取消"), "Cancel"};
|
||||
inline std::unordered_map<std::string, std::string> MakeLocalizedValues(
|
||||
const TranslationRow& row) {
|
||||
return {{"zh-CN", reinterpret_cast<const char*>(row.zh)},
|
||||
{"en-US", row.en},
|
||||
{"ru-RU", reinterpret_cast<const char*>(row.ru)}};
|
||||
}
|
||||
|
||||
static std::vector<std::string> new_password = {
|
||||
reinterpret_cast<const char*>(u8"请输入六位密码:"),
|
||||
"Please input a six-char password:"};
|
||||
inline TranslationTable BuildTranslationTable() {
|
||||
TranslationTable table;
|
||||
for (const auto& row : kTranslationRows) {
|
||||
table[row.key] = MakeLocalizedValues(row);
|
||||
}
|
||||
|
||||
static std::vector<std::string> input_password = {
|
||||
reinterpret_cast<const char*>(u8"请输入密码:"), "Please input password:"};
|
||||
static std::vector<std::string> validate_password = {
|
||||
reinterpret_cast<const char*>(u8"验证密码中..."), "Validate password ..."};
|
||||
static std::vector<std::string> reinput_password = {
|
||||
reinterpret_cast<const char*>(u8"请重新输入密码"),
|
||||
"Please input password again"};
|
||||
|
||||
static std::vector<std::string> remember_password = {
|
||||
reinterpret_cast<const char*>(u8"记住密码"), "Remember password"};
|
||||
|
||||
static std::vector<std::string> signal_connected = {
|
||||
reinterpret_cast<const char*>(u8"已连接服务器"), "Connected"};
|
||||
static std::vector<std::string> signal_disconnected = {
|
||||
reinterpret_cast<const char*>(u8"未连接服务器"), "Disconnected"};
|
||||
|
||||
static std::vector<std::string> p2p_connected = {
|
||||
reinterpret_cast<const char*>(u8"对等连接已建立"), "P2P Connected"};
|
||||
static std::vector<std::string> p2p_disconnected = {
|
||||
reinterpret_cast<const char*>(u8"对等连接已断开"), "P2P Disconnected"};
|
||||
static std::vector<std::string> p2p_connecting = {
|
||||
reinterpret_cast<const char*>(u8"正在建立对等连接..."),
|
||||
"P2P Connecting ..."};
|
||||
static std::vector<std::string> p2p_failed = {
|
||||
reinterpret_cast<const char*>(u8"对等连接失败"), "P2P Failed"};
|
||||
static std::vector<std::string> p2p_closed = {
|
||||
reinterpret_cast<const char*>(u8"对等连接已关闭"), "P2P closed"};
|
||||
|
||||
static std::vector<std::string> no_such_id = {
|
||||
reinterpret_cast<const char*>(u8"无此ID"), "No such ID"};
|
||||
|
||||
static std::vector<std::string> about = {
|
||||
reinterpret_cast<const char*>(u8"关于"), "About"};
|
||||
static std::vector<std::string> notification = {
|
||||
reinterpret_cast<const char*>(u8"通知"), "Notification"};
|
||||
static std::vector<std::string> new_version_available = {
|
||||
reinterpret_cast<const char*>(u8"新版本可用"), "New Version Available"};
|
||||
static std::vector<std::string> version = {
|
||||
reinterpret_cast<const char*>(u8"版本"), "Version"};
|
||||
static std::vector<std::string> release_date = {
|
||||
reinterpret_cast<const char*>(u8"发布日期: "), "Release Date: "};
|
||||
static std::vector<std::string> access_website = {
|
||||
reinterpret_cast<const char*>(u8"访问官网: "), "Access Website: "};
|
||||
static std::vector<std::string> update = {
|
||||
reinterpret_cast<const char*>(u8"更新"), "Update"};
|
||||
|
||||
static std::vector<std::string> confirm_delete_connection = {
|
||||
reinterpret_cast<const char*>(u8"确认删除此连接"),
|
||||
"Confirm to delete this connection"};
|
||||
|
||||
static std::vector<std::string> enable_autostart = {
|
||||
reinterpret_cast<const char*>(u8"开机自启:"), "Auto Start:"};
|
||||
static std::vector<std::string> enable_daemon = {
|
||||
reinterpret_cast<const char*>(u8"启用守护进程:"), "Enable Daemon:"};
|
||||
static std::vector<std::string> takes_effect_after_restart = {
|
||||
reinterpret_cast<const char*>(u8"重启后生效"),
|
||||
"Takes effect after restart"};
|
||||
static std::vector<std::string> select_file = {
|
||||
reinterpret_cast<const char*>(u8"选择文件"), "Select File"};
|
||||
static std::vector<std::string> file_transfer_progress = {
|
||||
reinterpret_cast<const char*>(u8"文件传输进度"), "File Transfer Progress"};
|
||||
static std::vector<std::string> queued = {
|
||||
reinterpret_cast<const char*>(u8"队列中"), "Queued"};
|
||||
static std::vector<std::string> sending = {
|
||||
reinterpret_cast<const char*>(u8"正在传输"), "Sending"};
|
||||
static std::vector<std::string> completed = {
|
||||
reinterpret_cast<const char*>(u8"已完成"), "Completed"};
|
||||
static std::vector<std::string> failed = {
|
||||
reinterpret_cast<const char*>(u8"失败"), "Failed"};
|
||||
|
||||
#if _WIN32
|
||||
static std::vector<std::string> minimize_to_tray = {
|
||||
reinterpret_cast<const char*>(u8"退出时最小化到系统托盘:"),
|
||||
"Minimize to system tray when exit:"};
|
||||
static std::vector<LPCWSTR> exit_program = {L"退出", L"Exit"};
|
||||
#endif
|
||||
#ifdef __APPLE__
|
||||
static std::vector<std::string> request_permissions = {
|
||||
reinterpret_cast<const char*>(u8"权限请求"), "Request Permissions"};
|
||||
static std::vector<std::string> screen_recording_permission = {
|
||||
reinterpret_cast<const char*>(u8"屏幕录制权限"),
|
||||
"Screen Recording Permission"};
|
||||
static std::vector<std::string> accessibility_permission = {
|
||||
reinterpret_cast<const char*>(u8"辅助功能权限"),
|
||||
"Accessibility Permission"};
|
||||
static std::vector<std::string> permission_required_message = {
|
||||
reinterpret_cast<const char*>(u8"该应用需要授权以下权限:"),
|
||||
"The application requires the following permissions:"};
|
||||
#endif
|
||||
} // namespace localization
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
return table;
|
||||
}
|
||||
|
||||
inline const TranslationTable& GetTranslationTable() {
|
||||
static const TranslationTable table = BuildTranslationTable();
|
||||
return table;
|
||||
}
|
||||
|
||||
inline const std::string& GetTranslatedText(const std::string& key,
|
||||
int language_index) {
|
||||
static const std::string kEmptyText = "";
|
||||
|
||||
const auto& table = GetTranslationTable();
|
||||
const auto key_it = table.find(key);
|
||||
if (key_it == table.end()) {
|
||||
return kEmptyText;
|
||||
}
|
||||
|
||||
const auto& localized_values = key_it->second;
|
||||
const std::string& language_code =
|
||||
GetSupportedLanguages()[ClampLanguageIndex(language_index)].code;
|
||||
|
||||
const auto exact_it = localized_values.find(language_code);
|
||||
if (exact_it != localized_values.end()) {
|
||||
return exact_it->second;
|
||||
}
|
||||
|
||||
const auto english_it = localized_values.find("en-US");
|
||||
if (english_it != localized_values.end()) {
|
||||
return english_it->second;
|
||||
}
|
||||
|
||||
const auto chinese_it = localized_values.find("zh-CN");
|
||||
if (chinese_it != localized_values.end()) {
|
||||
return chinese_it->second;
|
||||
}
|
||||
|
||||
return kEmptyText;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
inline const std::string& LocalizedString::operator[](
|
||||
int language_index) const {
|
||||
return detail::GetTranslatedText(key_, language_index);
|
||||
}
|
||||
|
||||
#define CROSSDESK_DECLARE_LOCALIZED_STRING(name, zh, en, ru) \
|
||||
inline const LocalizedString name(#name);
|
||||
CROSSDESK_LOCALIZATION_ALL(CROSSDESK_DECLARE_LOCALIZED_STRING)
|
||||
#undef CROSSDESK_DECLARE_LOCALIZED_STRING
|
||||
|
||||
#if _WIN32
|
||||
inline const wchar_t* GetExitProgramLabel(int language_index) {
|
||||
static std::vector<std::wstring> cache(GetSupportedLanguages().size());
|
||||
const int normalized_index = detail::ClampLanguageIndex(language_index);
|
||||
std::wstring& cached_text = cache[normalized_index];
|
||||
if (!cached_text.empty()) {
|
||||
return cached_text.c_str();
|
||||
}
|
||||
|
||||
const std::string& utf8_text =
|
||||
detail::GetTranslatedText("exit_program", normalized_index);
|
||||
if (utf8_text.empty()) {
|
||||
cached_text = L"Exit";
|
||||
return cached_text.c_str();
|
||||
}
|
||||
|
||||
int wide_length =
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8_text.c_str(), -1, nullptr, 0);
|
||||
if (wide_length <= 0) {
|
||||
cached_text = L"Exit";
|
||||
return cached_text.c_str();
|
||||
}
|
||||
|
||||
cached_text.resize(static_cast<size_t>(wide_length - 1));
|
||||
MultiByteToWideChar(CP_UTF8, 0, utf8_text.c_str(), -1, cached_text.data(),
|
||||
wide_length);
|
||||
return cached_text.c_str();
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace localization
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2024-05-29
|
||||
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
#ifndef _LOCALIZATION_DATA_H_
|
||||
#define _LOCALIZATION_DATA_H_
|
||||
|
||||
namespace crossdesk {
|
||||
namespace localization {
|
||||
|
||||
namespace detail {
|
||||
|
||||
struct TranslationRow {
|
||||
const char* key;
|
||||
const char* zh;
|
||||
const char* en;
|
||||
const char* ru;
|
||||
};
|
||||
|
||||
// Single source of truth for all UI strings.
|
||||
#define CROSSDESK_LOCALIZATION_ALL(X) \
|
||||
X(local_desktop, u8"本桌面", "Local Desktop", u8"Локальный рабочий стол") \
|
||||
X(local_id, u8"本机ID", "Local ID", u8"Локальный ID") \
|
||||
X(local_id_copied_to_clipboard, u8"已复制到剪贴板", "Copied to clipboard", \
|
||||
u8"Скопировано в буфер обмена") \
|
||||
X(password, u8"密码", "Password", u8"Пароль") \
|
||||
X(max_password_len, u8"最大6个字符", "Max 6 chars", u8"Макс. 6 символов") \
|
||||
X(remote_desktop, u8"远程桌面", "Remote Desktop", \
|
||||
u8"Удаленный рабочий стол") \
|
||||
X(remote_id, u8"对端ID", "Remote ID", u8"Удаленный ID") \
|
||||
X(connect, u8"连接", "Connect", u8"Подключиться") \
|
||||
X(recent_connections, u8"近期连接", "Recent Connections", \
|
||||
u8"Недавние подключения") \
|
||||
X(disconnect, u8"断开连接", "Disconnect", u8"Отключить") \
|
||||
X(select_display, u8"选择显示器", "Select Display", u8"Выбрать дисплей") \
|
||||
X(expand_control_bar, u8"展开控制栏", "Expand Control Bar", \
|
||||
u8"Развернуть панель управления") \
|
||||
X(collapse_control_bar, u8"收起控制栏", "Collapse Control Bar", \
|
||||
u8"Свернуть панель управления") \
|
||||
X(fullscreen, u8"全屏", " Fullscreen", u8"Полный экран") \
|
||||
X(show_net_traffic_stats, u8"显示网络状态", "Show Net Traffic Stats", \
|
||||
u8"Показать статистику трафика") \
|
||||
X(hide_net_traffic_stats, u8"隐藏网络状态", "Hide Net Traffic Stats", \
|
||||
u8"Скрыть статистику трафика") \
|
||||
X(video, u8"视频", "Video", u8"Видео") \
|
||||
X(audio, u8"音频", "Audio", u8"Аудио") \
|
||||
X(data, u8"数据", "Data", u8"Данные") \
|
||||
X(total, u8"总计", "Total", u8"Итого") \
|
||||
X(in, u8"输入", "In", u8"Вход") \
|
||||
X(out, u8"输出", "Out", u8"Выход") \
|
||||
X(loss_rate, u8"丢包率", "Loss Rate", u8"Потери пакетов") \
|
||||
X(exit_fullscreen, u8"退出全屏", "Exit fullscreen", \
|
||||
u8"Выйти из полноэкранного режима") \
|
||||
X(control_mouse, u8"控制鼠标", "Control Mouse", u8"Управление мышью") \
|
||||
X(release_mouse, u8"释放鼠标", "Release Mouse", u8"Освободить мышь") \
|
||||
X(audio_capture, u8"播放声音", "Audio Capture", u8"Воспроизведение звука") \
|
||||
X(mute, u8" 静音", " Mute", u8"Без звука") \
|
||||
X(send_shortcut, u8"发送组合键", "Send Shortcut", u8"Сочетания клавиш") \
|
||||
X(send_sas, u8"发送SAS", "Send SAS", u8"Отправить SAS") \
|
||||
X(lock_remote, u8"锁定远端", "Lock Remote", u8"Заблокировать") \
|
||||
X(remote_password_box_visible, u8"远端密码框已出现", \
|
||||
"Remote password box visible", u8"Окно ввода пароля видно") \
|
||||
X(remote_lock_screen_hint, u8"远端处于锁屏封面,可发送SAS", \
|
||||
"Remote lock screen visible, send SAS", \
|
||||
u8"Видна блокировка, отправьте SAS") \
|
||||
X(remote_secure_desktop_active, u8"远端已进入安全桌面", \
|
||||
"Remote secure desktop active", u8"Активен защищенный рабочий стол") \
|
||||
X(remote_service_unavailable, u8"远端Windows服务不可用", \
|
||||
"Remote Windows service unavailable", \
|
||||
u8"Служба Windows на удаленной стороне недоступна") \
|
||||
X(windows_service_setup_title, u8"安装 CrossDesk Service", \
|
||||
"Install CrossDesk Service", u8"Установить CrossDesk Service") \
|
||||
X(windows_service_setup_message, \
|
||||
u8"为支持该设备在锁屏状态下被远程控制,需要以管理员权限安装 CrossDesk " \
|
||||
u8"Service。\n未安装该服务不影响 CrossDesk " \
|
||||
u8"正常使用,仅无法在锁屏状态下控制本机。", \
|
||||
"To support remote control of this device while it is locked, CrossDesk " \
|
||||
"Service must be installed with administrator permission.\nWithout this " \
|
||||
"service, CrossDesk still works normally; only lock-screen control of " \
|
||||
"this computer is unavailable.", \
|
||||
u8"Чтобы поддерживать удаленное управление этим устройством на экране " \
|
||||
u8"блокировки, необходимо установить CrossDesk Service с правами " \
|
||||
u8"администратора.\nБез этой службы CrossDesk продолжит работать " \
|
||||
u8"нормально; будет недоступно только управление этим компьютером на " \
|
||||
u8"экране блокировки.") \
|
||||
X(install_windows_service, u8"安装", "Install", u8"Установить") \
|
||||
X(windows_service_settings_label, u8"锁屏控制服务:", \
|
||||
"Lock Screen Service:", u8"Служба блокировки экрана:") \
|
||||
X(windows_service_installed, u8"已安装", "Installed", u8"Установлена") \
|
||||
X(do_not_remind_again, u8"不再提醒", "Do not remind again", \
|
||||
u8"Больше не напоминать") \
|
||||
X(windows_service_prompt_suppressed_message, \
|
||||
u8"已不再提醒。后续如需启用锁屏状态下被远程控制,可在设置中点击“安装”。", \
|
||||
"You will not be reminded again. To enable remote control while locked " \
|
||||
"later, click Install in Settings.", \
|
||||
u8"Напоминание отключено. Чтобы позже включить удаленное управление на " \
|
||||
u8"экране блокировки, нажмите «Установить» в настройках.") \
|
||||
X(installing_windows_service, u8"正在安装服务...", "Installing service...", \
|
||||
u8"Установка службы...") \
|
||||
X(windows_service_install_success, u8"服务已安装并启动", \
|
||||
"Service installed and started", u8"Служба установлена и запущена") \
|
||||
X(windows_service_install_failed, \
|
||||
u8"服务安装失败。请确认 " \
|
||||
u8"CrossDesk.exe、crossdesk_service.exe、crossdesk_session_helper.exe " \
|
||||
u8"位于同一便携目录中,并在系统弹窗中允许管理员权限。", \
|
||||
"Service installation failed. Make sure CrossDesk.exe, " \
|
||||
"crossdesk_service.exe, and crossdesk_session_helper.exe are in the same " \
|
||||
"portable folder, then approve the administrator prompt.", \
|
||||
u8"Не удалось установить службу. Убедитесь, что CrossDesk.exe, " \
|
||||
u8"crossdesk_service.exe и crossdesk_session_helper.exe находятся в " \
|
||||
u8"одной папке портативной версии, затем подтвердите запрос прав " \
|
||||
u8"администратора.") \
|
||||
X(remote_unlock_requires_secure_desktop, \
|
||||
u8"当前仍需要安全桌面专用采集/输入", \
|
||||
"Secure desktop capture/input is still required", \
|
||||
u8"По-прежнему нужен отдельный захват/ввод для защищенного рабочего " \
|
||||
u8"стола") \
|
||||
X(settings, u8"设置", "Settings", u8"Настройки") \
|
||||
X(language, u8"语言:", "Language:", u8"Язык:") \
|
||||
X(video_quality, u8"视频质量:", "Video Quality:", u8"Качество видео:") \
|
||||
X(video_frame_rate, u8"画面采集帧率:", \
|
||||
"Video Capture Frame Rate:", u8"Частота захвата видео:") \
|
||||
X(video_quality_high, u8"高", "High", u8"Высокое") \
|
||||
X(video_quality_medium, u8"中", "Medium", u8"Среднее") \
|
||||
X(video_quality_low, u8"低", "Low", u8"Низкое") \
|
||||
X(video_encode_format, u8"视频编码格式:", \
|
||||
"Video Encode Format:", u8"Формат кодека видео:") \
|
||||
X(av1, u8"AV1", "AV1", "AV1") \
|
||||
X(h264, u8"H.264", "H.264", "H.264") \
|
||||
X(enable_hardware_video_codec, u8"启用硬件编解码器:", \
|
||||
"Enable Hardware Video Codec:", u8"Использовать аппаратный кодек:") \
|
||||
X(enable_turn, u8"启用中继服务:", \
|
||||
"Enable TURN Service:", u8"Включить TURN-сервис:") \
|
||||
X(enable_srtp, u8"启用SRTP:", "Enable SRTP:", u8"Включить SRTP:") \
|
||||
X(self_hosted_server_config, u8"自托管配置", "Self-Hosted Config", \
|
||||
u8"Конфигурация self-hosted") \
|
||||
X(self_hosted_server_settings, u8"自托管设置", "Self-Hosted Settings", \
|
||||
u8"Настройки self-hosted") \
|
||||
X(self_hosted_server_address, u8"服务器地址:", \
|
||||
"Server Address:", u8"Адрес сервера:") \
|
||||
X(self_hosted_server_port, u8"信令服务端口:", \
|
||||
"Signal Service Port:", u8"Порт сигнального сервиса:") \
|
||||
X(self_hosted_server_coturn_server_port, u8"中继服务端口:", \
|
||||
"Relay Service Port:", u8"Порт реле-сервиса:") \
|
||||
X(ok, u8"确认", "OK", u8"ОК") \
|
||||
X(cancel, u8"取消", "Cancel", u8"Отмена") \
|
||||
X(new_password, u8"请输入六位密码:", \
|
||||
"Please input a six-char password:", u8"Введите шестизначный пароль:") \
|
||||
X(input_password, u8"请输入密码:", \
|
||||
"Please input password:", u8"Введите пароль:") \
|
||||
X(validate_password, u8"验证密码中...", "Validate password ...", \
|
||||
u8"Проверка пароля...") \
|
||||
X(reinput_password, u8"请重新输入密码", "Please input password again", \
|
||||
u8"Повторно введите пароль") \
|
||||
X(remember_password, u8"记住密码", "Remember password", \
|
||||
u8"Запомнить пароль") \
|
||||
X(signal_connected, u8"已连接服务器", "Connected", u8"Подключено к серверу") \
|
||||
X(signal_disconnected, u8"未连接服务器", "Disconnected", \
|
||||
u8"Нет подключения к серверу") \
|
||||
X(signal_tls_cert_error, u8"证书验证失败,请重新安装自托管根证书", \
|
||||
"Certificate verification failed. Reinstall the self-hosted root certificate.", \
|
||||
u8"Ошибка проверки сертификата. Переустановите корневой сертификат.") \
|
||||
X(p2p_connected, u8"对等连接已建立", "P2P Connected", u8"P2P подключено") \
|
||||
X(p2p_disconnected, u8"对等连接已断开", "P2P Disconnected", \
|
||||
u8"P2P отключено") \
|
||||
X(p2p_connecting, u8"正在建立对等连接...", "P2P Connecting ...", \
|
||||
u8"Подключение P2P...") \
|
||||
X(receiving_screen, u8"画面接收中...", "Receiving screen...", \
|
||||
u8"Получение изображения...") \
|
||||
X(p2p_failed, u8"对等连接失败", "P2P Failed", u8"Сбой P2P") \
|
||||
X(p2p_closed, u8"对等连接已关闭", "P2P closed", u8"P2P закрыто") \
|
||||
X(no_such_id, u8"无此ID", "No such ID", u8"ID не найден") \
|
||||
X(about, u8"关于", "About", u8"О программе") \
|
||||
X(notification, u8"通知", "Notification", u8"Уведомление") \
|
||||
X(new_version_available, u8"新版本可用", "New Version Available", \
|
||||
u8"Доступна новая версия") \
|
||||
X(version, u8"版本", "Version", u8"Версия") \
|
||||
X(release_date, u8"发布日期: ", "Release Date: ", u8"Дата релиза: ") \
|
||||
X(access_website, u8"访问官网: ", \
|
||||
"Access Website: ", u8"Официальный сайт: ") \
|
||||
X(update, u8"更新", "Update", u8"Обновить") \
|
||||
X(confirm_delete_connection, u8"确认删除此连接", \
|
||||
"Confirm to delete this connection", u8"Удалить это подключение?") \
|
||||
X(enable_autostart, u8"开机自启:", "Auto Start:", u8"Автозапуск:") \
|
||||
X(enable_daemon, u8"启用守护进程:", "Enable Daemon:", u8"Включить демон:") \
|
||||
X(takes_effect_after_restart, u8"重启后生效", "Takes effect after restart", \
|
||||
u8"Вступит в силу после перезапуска") \
|
||||
X(select_file, u8"选择文件发送", "Select File to Send", \
|
||||
u8"Выбрать файл для отправки") \
|
||||
X(file_transfer_progress, u8"文件传输进度", "File Transfer Progress", \
|
||||
u8"Прогресс передачи файлов") \
|
||||
X(queued, u8"队列中", "Queued", u8"В очереди") \
|
||||
X(sending, u8"正在传输", "Sending", u8"Передача") \
|
||||
X(completed, u8"已完成", "Completed", u8"Завершено") \
|
||||
X(failed, u8"失败", "Failed", u8"Ошибка") \
|
||||
X(controller, u8"控制端:", "Controller:", u8"Контроллер:") \
|
||||
X(file_transfer, u8"文件传输:", "File Transfer:", u8"Передача файлов:") \
|
||||
X(connection_status, u8"连接状态:", \
|
||||
"Connection Status:", u8"Состояние соединения:") \
|
||||
X(file_transfer_save_path, u8"文件接收保存路径:", \
|
||||
"File Transfer Save Path:", u8"Путь сохранения файлов:") \
|
||||
X(default_desktop, u8"桌面", "Desktop", u8"Рабочий стол") \
|
||||
X(minimize_to_tray, u8"退出时最小化到系统托盘:", \
|
||||
"Minimize on Exit:", u8"Сворачивать в трей при выходе:") \
|
||||
X(resolution, u8"分辨率", "Res", u8"Разрешение") \
|
||||
X(connection_mode, u8"连接模式", "Mode", u8"Режим") \
|
||||
X(connection_mode_direct, u8"直连", "Direct", u8"Прямой") \
|
||||
X(connection_mode_relay, u8"中继", "Relay", u8"Релейный") \
|
||||
X(online, u8"在线", "Online", u8"Онлайн") \
|
||||
X(offline, u8"离线", "Offline", u8"Офлайн") \
|
||||
X(device_offline, u8"设备离线", "Device Offline", u8"Устройство офлайн") \
|
||||
X(request_permissions, u8"权限请求", "Request Permissions", \
|
||||
u8"Запрос разрешений") \
|
||||
X(screen_recording_permission, u8"屏幕录制权限", \
|
||||
"Screen Recording Permission", u8"Разрешение на запись экрана") \
|
||||
X(accessibility_permission, u8"辅助功能权限", "Accessibility Permission", \
|
||||
u8"Разрешение специальных возможностей") \
|
||||
X(permission_required_message, u8"该应用需要授权以下权限:", \
|
||||
"The application requires the following permissions:", \
|
||||
u8"Для работы приложения требуются следующие разрешения:") \
|
||||
X(exit_program, u8"退出", "Exit", u8"Выход")
|
||||
|
||||
inline constexpr TranslationRow kTranslationRows[] = {
|
||||
#define CROSSDESK_DECLARE_TRANSLATION_ROW(name, zh, en, ru) {#name, zh, en, ru},
|
||||
CROSSDESK_LOCALIZATION_ALL(CROSSDESK_DECLARE_TRANSLATION_ROW)
|
||||
#undef CROSSDESK_DECLARE_TRANSLATION_ROW
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace localization
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-02-28
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _DEVICE_PRESENCE_H_
|
||||
#define _DEVICE_PRESENCE_H_
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class DevicePresence {
|
||||
public:
|
||||
void SetOnline(const std::string& device_id, bool online) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
cache_[device_id] = online;
|
||||
}
|
||||
|
||||
bool IsOnline(const std::string& device_id) const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return cache_.count(device_id) > 0 && cache_.at(device_id);
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
cache_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, bool> cache_;
|
||||
mutable std::mutex mutex_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -17,7 +17,7 @@ int Render::LocalWindow() {
|
||||
|
||||
ImGui::SetNextWindowPos(ImVec2(0.0f, io.DisplaySize.y * TITLE_BAR_HEIGHT),
|
||||
ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0, 0, 0, 0));
|
||||
ImGui::BeginChild("LocalDesktopWindow",
|
||||
@@ -42,11 +42,11 @@ int Render::LocalWindow() {
|
||||
ImGuiCond_Always);
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(239.0f / 255, 240.0f / 255,
|
||||
242.0f / 255, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 10.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, window_rounding_ * 1.5f);
|
||||
ImGui::BeginChild(
|
||||
"LocalDesktopPanel",
|
||||
ImVec2(local_window_width * 0.8f, local_window_height * 0.43f),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PopStyleVar();
|
||||
@@ -101,7 +101,7 @@ int Render::LocalWindow() {
|
||||
ImGuiCol_WindowBg,
|
||||
ImVec4(1.0f, 1.0f, 1.0f, 1.0f - (float)time_duration));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::Begin("ConnectionStatusWindow", nullptr,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoSavedSettings);
|
||||
@@ -177,10 +177,11 @@ int Render::LocalWindow() {
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f));
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding,
|
||||
window_rounding_ * 0.5f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 1.0));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
|
||||
ImGui::Begin("ResetPasswordWindow", nullptr,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
@@ -299,4 +300,4 @@ int Render::LocalWindow() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -9,7 +9,7 @@ int Render::RecentConnectionsWindow() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
float recent_connection_window_width = io.DisplaySize.x;
|
||||
float recent_connection_window_height =
|
||||
io.DisplaySize.y * (0.46f - STATUS_BAR_HEIGHT);
|
||||
io.DisplaySize.y * (0.455f - STATUS_BAR_HEIGHT);
|
||||
ImGui::SetNextWindowPos(ImVec2(0, io.DisplaySize.y * 0.55f),
|
||||
ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
||||
@@ -17,7 +17,7 @@ int Render::RecentConnectionsWindow() {
|
||||
ImGui::BeginChild(
|
||||
"RecentConnectionsWindow",
|
||||
ImVec2(recent_connection_window_width, recent_connection_window_height),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PopStyleVar();
|
||||
@@ -64,7 +64,7 @@ int Render::ShowRecentConnections() {
|
||||
ImGui::BeginChild(
|
||||
"RecentConnectionsContainer",
|
||||
ImVec2(recent_connection_panel_width, recent_connection_panel_height),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus |
|
||||
ImGuiWindowFlags_AlwaysHorizontalScrollbar |
|
||||
@@ -122,6 +122,8 @@ int Render::ShowRecentConnections() {
|
||||
it.second.remote_host_name = "unknown";
|
||||
}
|
||||
|
||||
bool online = device_presence_.IsOnline(it.second.remote_id);
|
||||
|
||||
ImVec2 image_screen_pos = ImVec2(
|
||||
ImGui::GetCursorScreenPos().x + recent_connection_image_width * 0.04f,
|
||||
ImGui::GetCursorScreenPos().y + recent_connection_image_height * 0.08f);
|
||||
@@ -132,6 +134,29 @@ int Render::ShowRecentConnections() {
|
||||
ImGui::Image(
|
||||
(ImTextureID)(intptr_t)it.second.texture,
|
||||
ImVec2(recent_connection_image_width, recent_connection_image_height));
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
std::string display_host_name_with_presence =
|
||||
it.second.remote_host_name + " " +
|
||||
(online ? localization::online[localization_language_index_]
|
||||
: localization::offline[localization_language_index_]);
|
||||
ImGui::Text("%s", display_host_name_with_presence.c_str());
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
ImVec2 circle_pos =
|
||||
ImVec2(image_screen_pos.x + recent_connection_image_width * 0.07f,
|
||||
image_screen_pos.y + recent_connection_image_height * 0.12f);
|
||||
ImU32 fill_color =
|
||||
online ? IM_COL32(0, 255, 0, 255) : IM_COL32(140, 140, 140, 255);
|
||||
ImU32 border_color = IM_COL32(255, 255, 255, 255);
|
||||
float dot_radius = recent_connection_image_height * 0.06f;
|
||||
draw_list->AddCircleFilled(circle_pos, dot_radius * 1.25f, border_color,
|
||||
100);
|
||||
draw_list->AddCircleFilled(circle_pos, dot_radius, fill_color, 100);
|
||||
|
||||
// remote id display button
|
||||
{
|
||||
@@ -155,14 +180,6 @@ int Render::ShowRecentConnections() {
|
||||
ImGui::Text("%s", it.second.remote_id.c_str());
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::PopStyleColor(3);
|
||||
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::Text("%s", it.second.remote_host_name.c_str());
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0.2f));
|
||||
@@ -242,6 +259,9 @@ int Render::ShowRecentConnections() {
|
||||
if (show_confirm_delete_connection_) {
|
||||
ConfirmDeleteConnection();
|
||||
}
|
||||
if (show_offline_warning_window_) {
|
||||
OfflineWarningWindow();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -253,10 +273,10 @@ int Render::ConfirmDeleteConnection() {
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f));
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0, 1.0, 1.0, 1.0));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
|
||||
ImGui::Begin("ConfirmDeleteConnectionWindow", nullptr,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
@@ -299,4 +319,45 @@ int Render::ConfirmDeleteConnection() {
|
||||
ImGui::PopStyleVar();
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
int Render::OfflineWarningWindow() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f));
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f));
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
|
||||
ImGui::Begin("OfflineWarningWindow", nullptr,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoSavedSettings);
|
||||
ImGui::PopStyleVar(2);
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
auto window_width = ImGui::GetWindowSize().x;
|
||||
auto window_height = ImGui::GetWindowSize().y;
|
||||
|
||||
ImGui::SetCursorPosX(window_width * 0.43f);
|
||||
ImGui::SetCursorPosY(window_height * 0.67f);
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
if (ImGui::Button(localization::ok[localization_language_index_].c_str()) ||
|
||||
ImGui::IsKeyPressed(ImGuiKey_Enter) ||
|
||||
ImGui::IsKeyPressed(ImGuiKey_Escape)) {
|
||||
show_offline_warning_window_ = false;
|
||||
}
|
||||
|
||||
auto text_width = ImGui::CalcTextSize(offline_warning_text_.c_str()).x;
|
||||
ImGui::SetCursorPosX((window_width - text_width) * 0.5f);
|
||||
ImGui::SetCursorPosY(window_height * 0.2f);
|
||||
ImGui::Text("%s", offline_warning_text_.c_str());
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
ImGui::End();
|
||||
ImGui::PopStyleVar();
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -18,7 +18,7 @@ int Render::RemoteWindow() {
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * TITLE_BAR_HEIGHT),
|
||||
ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0, 0, 0, 0));
|
||||
ImGui::BeginChild("RemoteDesktopWindow",
|
||||
@@ -48,7 +48,7 @@ int Render::RemoteWindow() {
|
||||
ImGui::BeginChild(
|
||||
"RemoteDesktopWindow_1",
|
||||
ImVec2(remote_window_width * 0.8f, remote_window_height * 0.43f),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PopStyleVar();
|
||||
@@ -165,7 +165,21 @@ static int InputTextCallback(ImGuiInputTextCallbackData* data) {
|
||||
}
|
||||
|
||||
int Render::ConnectTo(const std::string& remote_id, const char* password,
|
||||
bool remember_password) {
|
||||
bool remember_password, bool bypass_presence_check) {
|
||||
if (!bypass_presence_check && !device_presence_.IsOnline(remote_id)) {
|
||||
int ret =
|
||||
RequestSingleDevicePresence(remote_id, password, remember_password);
|
||||
if (ret != 0) {
|
||||
offline_warning_text_ =
|
||||
localization::device_offline[localization_language_index_];
|
||||
show_offline_warning_window_ = true;
|
||||
LOG_WARN("Presence probe failed for [{}], ret={}", remote_id, ret);
|
||||
} else {
|
||||
LOG_INFO("Presence probe requested for [{}] before connect", remote_id);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
LOG_INFO("Connect to [{}]", remote_id);
|
||||
focused_remote_id_ = remote_id;
|
||||
|
||||
@@ -190,12 +204,15 @@ int Render::ConnectTo(const std::string& remote_id, const char* password,
|
||||
props->params_.user_id = props->local_id_.c_str();
|
||||
props->peer_ = CreatePeer(&props->params_);
|
||||
|
||||
props->control_window_width_ = title_bar_height_ * 9.0f;
|
||||
props->control_window_width_ = title_bar_height_ * 10.0f;
|
||||
props->control_window_height_ = title_bar_height_ * 1.3f;
|
||||
props->control_window_min_width_ = title_bar_height_ * 0.65f;
|
||||
props->control_window_min_height_ = title_bar_height_ * 1.3f;
|
||||
props->control_window_max_width_ = title_bar_height_ * 9.0f;
|
||||
props->control_window_max_height_ = title_bar_height_ * 6.0f;
|
||||
props->control_window_max_width_ = title_bar_height_ * 10.0f;
|
||||
props->control_window_max_height_ = title_bar_height_ * 7.0f;
|
||||
|
||||
props->connection_status_ = ConnectionStatus::Connecting;
|
||||
show_connection_status_window_ = true;
|
||||
|
||||
if (!props->peer_) {
|
||||
LOG_INFO("Create peer [{}] instance failed", props->local_id_);
|
||||
@@ -207,6 +224,8 @@ int Render::ConnectTo(const std::string& remote_id, const char* password,
|
||||
}
|
||||
AddAudioStream(props->peer_, props->audio_label_.c_str());
|
||||
AddDataStream(props->peer_, props->data_label_.c_str(), false);
|
||||
AddDataStream(props->peer_, props->mouse_label_.c_str(), false);
|
||||
AddDataStream(props->peer_, props->keyboard_label_.c_str(), true);
|
||||
AddDataStream(props->peer_, props->control_data_label_.c_str(), true);
|
||||
AddDataStream(props->peer_, props->file_label_.c_str(), true);
|
||||
AddDataStream(props->peer_, props->file_feedback_label_.c_str(), true);
|
||||
@@ -253,4 +272,4 @@ int Render::ConnectTo(const std::string& remote_id, const char* password,
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
+1297
-389
File diff suppressed because it is too large
Load Diff
+204
-72
@@ -20,11 +20,13 @@
|
||||
#include <shared_mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "IconsFontAwesome6.h"
|
||||
#include "config_center.h"
|
||||
#include "device_controller_factory.h"
|
||||
#include "device_presence.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl3.h"
|
||||
#include "imgui_impl_sdlrenderer3.h"
|
||||
@@ -42,11 +44,57 @@
|
||||
namespace crossdesk {
|
||||
class Render {
|
||||
public:
|
||||
enum class RemoteUnlockState {
|
||||
none,
|
||||
service_unavailable,
|
||||
lock_screen,
|
||||
credential_ui,
|
||||
secure_desktop,
|
||||
};
|
||||
|
||||
struct FileTransferState {
|
||||
std::atomic<bool> file_sending_ = false;
|
||||
std::atomic<uint64_t> file_sent_bytes_ = 0;
|
||||
std::atomic<uint64_t> file_total_bytes_ = 0;
|
||||
std::atomic<uint32_t> file_send_rate_bps_ = 0;
|
||||
std::mutex file_transfer_mutex_;
|
||||
std::chrono::steady_clock::time_point file_send_start_time_;
|
||||
std::chrono::steady_clock::time_point file_send_last_update_time_;
|
||||
uint64_t file_send_last_bytes_ = 0;
|
||||
bool file_transfer_window_visible_ = false;
|
||||
bool file_transfer_window_hovered_ = false;
|
||||
std::atomic<uint32_t> current_file_id_{0};
|
||||
|
||||
struct QueuedFile {
|
||||
std::filesystem::path file_path;
|
||||
std::string file_label;
|
||||
std::string remote_id;
|
||||
};
|
||||
std::queue<QueuedFile> file_send_queue_;
|
||||
std::mutex file_queue_mutex_;
|
||||
|
||||
enum class FileTransferStatus { Queued, Sending, Completed, Failed };
|
||||
|
||||
struct FileTransferInfo {
|
||||
std::string file_name;
|
||||
std::filesystem::path file_path;
|
||||
uint64_t file_size = 0;
|
||||
FileTransferStatus status = FileTransferStatus::Queued;
|
||||
uint64_t sent_bytes = 0;
|
||||
uint32_t file_id = 0;
|
||||
uint32_t rate_bps = 0;
|
||||
};
|
||||
std::vector<FileTransferInfo> file_transfer_list_;
|
||||
std::mutex file_transfer_list_mutex_;
|
||||
};
|
||||
|
||||
struct SubStreamWindowProperties {
|
||||
Params params_;
|
||||
PeerPtr* peer_ = nullptr;
|
||||
std::string audio_label_ = "control_audio";
|
||||
std::string data_label_ = "data";
|
||||
std::string mouse_label_ = "mouse";
|
||||
std::string keyboard_label_ = "keyboard";
|
||||
std::string file_label_ = "file";
|
||||
std::string control_data_label_ = "control_data";
|
||||
std::string file_feedback_label_ = "file_feedback";
|
||||
@@ -59,7 +107,7 @@ class Render {
|
||||
bool connection_established_ = false;
|
||||
bool rejoin_ = false;
|
||||
bool net_traffic_stats_button_pressed_ = false;
|
||||
bool mouse_control_button_pressed_ = true;
|
||||
bool enable_mouse_control_ = true;
|
||||
bool mouse_controller_is_started_ = false;
|
||||
bool audio_capture_button_pressed_ = true;
|
||||
bool control_mouse_ = true;
|
||||
@@ -67,6 +115,7 @@ class Render {
|
||||
bool is_control_bar_in_left_ = true;
|
||||
bool control_bar_hovered_ = false;
|
||||
bool display_selectable_hovered_ = false;
|
||||
bool shortcut_selectable_hovered_ = false;
|
||||
bool control_bar_expand_ = true;
|
||||
bool reset_control_bar_pos_ = false;
|
||||
bool control_window_width_is_changing_ = false;
|
||||
@@ -77,10 +126,10 @@ class Render {
|
||||
float sub_stream_window_width_ = 1280;
|
||||
float sub_stream_window_height_ = 720;
|
||||
float control_window_min_width_ = 20;
|
||||
float control_window_max_width_ = 230;
|
||||
float control_window_max_width_ = 300;
|
||||
float control_window_min_height_ = 38;
|
||||
float control_window_max_height_ = 180;
|
||||
float control_window_width_ = 230;
|
||||
float control_window_width_ = 300;
|
||||
float control_window_height_ = 38;
|
||||
float control_bar_pos_x_ = 0;
|
||||
float control_bar_pos_y_ = 30;
|
||||
@@ -88,8 +137,13 @@ class Render {
|
||||
float mouse_diff_control_bar_pos_y_ = 0;
|
||||
double control_bar_button_pressed_time_ = 0;
|
||||
double net_traffic_stats_button_pressed_time_ = 0;
|
||||
unsigned char* dst_buffer_ = nullptr;
|
||||
size_t dst_buffer_capacity_ = 0;
|
||||
// Double-buffered NV12 frame storage. Written by decode callback thread,
|
||||
// consumed by SDL main thread.
|
||||
std::mutex video_frame_mutex_;
|
||||
std::shared_ptr<std::vector<unsigned char>> front_frame_;
|
||||
std::shared_ptr<std::vector<unsigned char>> back_frame_;
|
||||
bool render_rect_dirty_ = false;
|
||||
bool stream_cleanup_pending_ = false;
|
||||
float mouse_pos_x_ = 0;
|
||||
float mouse_pos_y_ = 0;
|
||||
float mouse_pos_x_last_ = 0;
|
||||
@@ -115,10 +169,14 @@ class Render {
|
||||
std::string mouse_control_button_label_ = "Mouse Control";
|
||||
std::string audio_capture_button_label_ = "Audio Capture";
|
||||
std::string remote_host_name_ = "";
|
||||
bool remote_service_status_received_ = false;
|
||||
bool remote_service_available_ = false;
|
||||
std::string remote_interactive_stage_ = "";
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
SDL_Texture* stream_texture_ = nullptr;
|
||||
uint8_t* argb_buffer_ = nullptr;
|
||||
int argb_buffer_size_ = 0;
|
||||
SDL_FRect stream_render_rect_f_ = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
SDL_Rect stream_render_rect_;
|
||||
SDL_Rect stream_render_rect_last_;
|
||||
ImVec2 control_window_pos_;
|
||||
@@ -129,38 +187,10 @@ class Render {
|
||||
std::chrono::steady_clock::time_point last_time_;
|
||||
XNetTrafficStats net_traffic_stats_;
|
||||
|
||||
// File transfer progress
|
||||
std::atomic<bool> file_sending_ = false;
|
||||
std::atomic<uint64_t> file_sent_bytes_ = 0;
|
||||
std::atomic<uint64_t> file_total_bytes_ = 0;
|
||||
std::atomic<uint32_t> file_send_rate_bps_ = 0;
|
||||
std::mutex file_transfer_mutex_;
|
||||
std::chrono::steady_clock::time_point file_send_start_time_;
|
||||
std::chrono::steady_clock::time_point file_send_last_update_time_;
|
||||
uint64_t file_send_last_bytes_ = 0;
|
||||
bool file_transfer_window_visible_ = false;
|
||||
std::atomic<uint32_t> current_file_id_{0};
|
||||
|
||||
struct QueuedFile {
|
||||
std::filesystem::path file_path;
|
||||
std::string file_label;
|
||||
};
|
||||
std::queue<QueuedFile> file_send_queue_;
|
||||
std::mutex file_queue_mutex_;
|
||||
|
||||
enum class FileTransferStatus { Queued, Sending, Completed, Failed };
|
||||
|
||||
struct FileTransferInfo {
|
||||
std::string file_name;
|
||||
std::filesystem::path file_path;
|
||||
uint64_t file_size = 0;
|
||||
FileTransferStatus status = FileTransferStatus::Queued;
|
||||
uint64_t sent_bytes = 0;
|
||||
uint32_t file_id = 0;
|
||||
uint32_t rate_bps = 0;
|
||||
};
|
||||
std::vector<FileTransferInfo> file_transfer_list_;
|
||||
std::mutex file_transfer_list_mutex_;
|
||||
using QueuedFile = FileTransferState::QueuedFile;
|
||||
using FileTransferStatus = FileTransferState::FileTransferStatus;
|
||||
using FileTransferInfo = FileTransferState::FileTransferInfo;
|
||||
FileTransferState file_transfer_;
|
||||
};
|
||||
|
||||
public:
|
||||
@@ -180,6 +210,8 @@ class Render {
|
||||
void UpdateLabels();
|
||||
void UpdateInteractions();
|
||||
void HandleRecentConnections();
|
||||
void HandleConnectionStatusChange();
|
||||
void HandlePendingPresenceProbe();
|
||||
void HandleStreamWindow();
|
||||
void HandleServerWindow();
|
||||
void Cleanup();
|
||||
@@ -193,9 +225,13 @@ class Render {
|
||||
|
||||
void ProcessFileDropEvent(const SDL_Event& event);
|
||||
|
||||
void ProcessSelectedFile(const std::string& path,
|
||||
std::shared_ptr<SubStreamWindowProperties>& props,
|
||||
const std::string& file_label);
|
||||
void ProcessSelectedFile(
|
||||
const std::string& path,
|
||||
const std::shared_ptr<SubStreamWindowProperties>& props,
|
||||
const std::string& file_label, const std::string& remote_id = "");
|
||||
|
||||
std::shared_ptr<SubStreamWindowProperties>
|
||||
GetSubStreamWindowPropertiesByRemoteId(const std::string& remote_id);
|
||||
|
||||
private:
|
||||
int CreateStreamRenderWindow();
|
||||
@@ -204,12 +240,12 @@ class Render {
|
||||
int UpdateNotificationWindow();
|
||||
int StreamWindow();
|
||||
int ServerWindow();
|
||||
int RemoteClientInfoWindow();
|
||||
int LocalWindow();
|
||||
int RemoteWindow();
|
||||
int RecentConnectionsWindow();
|
||||
int SettingWindow();
|
||||
int SelfHostedServerWindow();
|
||||
int ShowSimpleFileBrowser();
|
||||
int ControlWindow(std::shared_ptr<SubStreamWindowProperties>& props);
|
||||
int ControlBar(std::shared_ptr<SubStreamWindowProperties>& props);
|
||||
int AboutWindow();
|
||||
@@ -217,13 +253,17 @@ class Render {
|
||||
bool ConnectionStatusWindow(
|
||||
std::shared_ptr<SubStreamWindowProperties>& props);
|
||||
int ShowRecentConnections();
|
||||
bool OpenUrl(const std::string& url);
|
||||
void Hyperlink(const std::string& label, const std::string& url,
|
||||
const float window_width);
|
||||
int FileTransferWindow(std::shared_ptr<SubStreamWindowProperties>& props);
|
||||
std::string OpenFileDialog(std::string title);
|
||||
|
||||
private:
|
||||
int ConnectTo(const std::string& remote_id, const char* password,
|
||||
bool remember_password);
|
||||
bool remember_password, bool bypass_presence_check = false);
|
||||
int RequestSingleDevicePresence(const std::string& remote_id,
|
||||
const char* password, bool remember_password);
|
||||
int CreateMainWindow();
|
||||
int DestroyMainWindow();
|
||||
int CreateStreamWindow();
|
||||
@@ -238,9 +278,17 @@ class Render {
|
||||
int DrawStreamWindow();
|
||||
int DrawServerWindow();
|
||||
int ConfirmDeleteConnection();
|
||||
int OfflineWarningWindow();
|
||||
int NetTrafficStats(std::shared_ptr<SubStreamWindowProperties>& props);
|
||||
void DrawConnectionStatusText(
|
||||
std::shared_ptr<SubStreamWindowProperties>& props);
|
||||
void DrawReceivingScreenText(
|
||||
std::shared_ptr<SubStreamWindowProperties>& props);
|
||||
void ResetRemoteServiceStatus(SubStreamWindowProperties& props);
|
||||
void ApplyRemoteServiceStatus(SubStreamWindowProperties& props,
|
||||
const ServiceStatus& status);
|
||||
RemoteUnlockState GetRemoteUnlockState(
|
||||
const SubStreamWindowProperties& props) const;
|
||||
#ifdef __APPLE__
|
||||
int RequestPermissionWindow();
|
||||
bool CheckScreenRecordingPermission();
|
||||
@@ -248,6 +296,9 @@ class Render {
|
||||
void OpenScreenRecordingPreferences();
|
||||
void OpenAccessibilityPreferences();
|
||||
bool DrawToggleSwitch(const char* id, bool active, bool enabled);
|
||||
void RefreshMacPermissionStatus(bool force);
|
||||
bool EnsureMacScreenRecordingPermission();
|
||||
bool EnsureMacAccessibilityPermission();
|
||||
#endif
|
||||
|
||||
public:
|
||||
@@ -269,14 +320,17 @@ class Render {
|
||||
static void OnSignalStatusCb(SignalStatus status, const char* user_id,
|
||||
size_t user_id_size, void* user_data);
|
||||
|
||||
static void OnSignalMessageCb(const char* message, size_t size,
|
||||
void* user_data);
|
||||
|
||||
static void OnConnectionStatusCb(ConnectionStatus status, const char* user_id,
|
||||
size_t user_id_size, void* user_data);
|
||||
|
||||
static void NetStatusReport(const char* client_id, size_t client_id_size,
|
||||
TraversalMode mode,
|
||||
const XNetTrafficStats* net_traffic_stats,
|
||||
const char* user_id, const size_t user_id_size,
|
||||
void* user_data);
|
||||
static void OnNetStatusReport(const char* client_id, size_t client_id_size,
|
||||
TraversalMode mode,
|
||||
const XNetTrafficStats* net_traffic_stats,
|
||||
const char* user_id, const size_t user_id_size,
|
||||
void* user_data);
|
||||
|
||||
static SDL_HitTestResult HitTestCallback(SDL_Window* window,
|
||||
const SDL_Point* area, void* data);
|
||||
@@ -289,7 +343,12 @@ class Render {
|
||||
static void FreeRemoteAction(RemoteAction& action);
|
||||
|
||||
private:
|
||||
int SendKeyCommand(int key_code, bool is_down);
|
||||
int SendKeyCommand(int key_code, bool is_down, uint32_t scan_code = 0,
|
||||
bool extended = false);
|
||||
static bool IsModifierVkKey(int key_code);
|
||||
void TrackPressedKeyState(int key_code, bool is_down);
|
||||
void ForceReleasePressedKeys();
|
||||
int ProcessKeyboardEvent(const SDL_Event& event);
|
||||
int ProcessMouseEvent(const SDL_Event& event);
|
||||
|
||||
static void SdlCaptureAudioIn(void* userdata, Uint8* stream, int len);
|
||||
@@ -317,11 +376,29 @@ class Render {
|
||||
// File transfer helper functions
|
||||
void StartFileTransfer(std::shared_ptr<SubStreamWindowProperties> props,
|
||||
const std::filesystem::path& file_path,
|
||||
const std::string& file_label);
|
||||
const std::string& file_label,
|
||||
const std::string& remote_id = "");
|
||||
void ProcessFileQueue(std::shared_ptr<SubStreamWindowProperties> props);
|
||||
|
||||
int AudioDeviceInit();
|
||||
int AudioDeviceDestroy();
|
||||
void HandleWindowsServiceIntegration();
|
||||
#if _WIN32
|
||||
void ResetLocalWindowsServiceState(bool clear_pending_sas);
|
||||
#if CROSSDESK_PORTABLE
|
||||
enum class PortableServiceInstallState {
|
||||
idle,
|
||||
installing,
|
||||
succeeded,
|
||||
failed,
|
||||
};
|
||||
|
||||
void CheckPortableWindowsService();
|
||||
int PortableServiceInstallWindow();
|
||||
void StartPortableWindowsServiceInstall();
|
||||
void JoinPortableWindowsServiceInstallThread();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
private:
|
||||
struct CDCache {
|
||||
@@ -362,7 +439,6 @@ class Render {
|
||||
ConfigCenter::LANGUAGE localization_language_ =
|
||||
ConfigCenter::LANGUAGE::CHINESE;
|
||||
std::unique_ptr<PathManager> path_manager_;
|
||||
std::string cert_path_;
|
||||
std::string exec_log_path_;
|
||||
std::string dll_log_path_;
|
||||
std::string cache_path_;
|
||||
@@ -385,9 +461,12 @@ class Render {
|
||||
// recent connections
|
||||
std::vector<std::pair<std::string, Thumbnail::RecentConnection>>
|
||||
recent_connections_;
|
||||
std::vector<std::string> recent_connection_ids_;
|
||||
int recent_connection_image_width_ = 160;
|
||||
int recent_connection_image_height_ = 90;
|
||||
uint32_t recent_connection_image_save_time_ = 0;
|
||||
DevicePresence device_presence_;
|
||||
bool need_to_send_recent_connections_ = true;
|
||||
|
||||
// main window render
|
||||
SDL_Window* main_window_ = nullptr;
|
||||
@@ -413,11 +492,15 @@ class Render {
|
||||
bool screen_capturer_is_started_ = false;
|
||||
bool start_speaker_capturer_ = false;
|
||||
bool speaker_capturer_is_started_ = false;
|
||||
bool start_keyboard_capturer_ = true;
|
||||
bool start_keyboard_capturer_ = false;
|
||||
bool show_cursor_ = false;
|
||||
bool keyboard_capturer_is_started_ = false;
|
||||
bool keyboard_capturer_uses_sdl_events_ = false;
|
||||
bool foucs_on_main_window_ = false;
|
||||
bool foucs_on_stream_window_ = false;
|
||||
bool focus_on_stream_window_ = false;
|
||||
bool main_window_minimized_ = false;
|
||||
uint32_t last_main_minimize_request_tick_ = 0;
|
||||
uint32_t last_stream_minimize_request_tick_ = 0;
|
||||
bool audio_capture_ = false;
|
||||
int main_window_width_real_ = 720;
|
||||
int main_window_height_real_ = 540;
|
||||
@@ -467,10 +550,31 @@ class Render {
|
||||
bool just_created_ = false;
|
||||
std::string controlled_remote_id_ = "";
|
||||
std::string focused_remote_id_ = "";
|
||||
bool need_to_send_host_info_ = false;
|
||||
SDL_Event last_mouse_event;
|
||||
SDL_AudioStream* output_stream_;
|
||||
std::string remote_client_id_ = "";
|
||||
std::unordered_set<int> pressed_keyboard_keys_;
|
||||
std::mutex pressed_keyboard_keys_mutex_;
|
||||
SDL_Event last_mouse_event{};
|
||||
SDL_AudioStream* output_stream_ = nullptr;
|
||||
uint32_t STREAM_REFRESH_EVENT = 0;
|
||||
#if _WIN32
|
||||
std::atomic<bool> pending_windows_service_sas_{false};
|
||||
bool local_service_status_received_ = false;
|
||||
bool local_service_available_ = false;
|
||||
std::string local_interactive_stage_;
|
||||
uint32_t last_local_secure_input_block_log_tick_ = 0;
|
||||
uint32_t last_windows_service_status_tick_ = 0;
|
||||
uint32_t optimistic_windows_secure_desktop_until_tick_ = 0;
|
||||
#if CROSSDESK_PORTABLE
|
||||
bool portable_service_prompt_checked_ = false;
|
||||
bool show_portable_service_install_window_ = false;
|
||||
bool show_portable_service_prompt_suppressed_window_ = false;
|
||||
bool portable_service_do_not_remind_ = false;
|
||||
bool portable_service_prompt_suppressed_ = false;
|
||||
std::atomic<PortableServiceInstallState> portable_service_install_state_{
|
||||
PortableServiceInstallState::idle};
|
||||
std::thread portable_service_install_thread_;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// stream window render
|
||||
SDL_Window* stream_window_ = nullptr;
|
||||
@@ -504,16 +608,18 @@ class Render {
|
||||
bool need_to_destroy_server_window_ = false;
|
||||
bool server_window_created_ = false;
|
||||
bool server_window_inited_ = false;
|
||||
int server_window_width_default_ = 300;
|
||||
int server_window_height_default_ = 450;
|
||||
float server_window_width_ = 300;
|
||||
float server_window_height_ = 450;
|
||||
float server_window_title_bar_height_ = 50.0f;
|
||||
int server_window_width_default_ = 250;
|
||||
int server_window_height_default_ = 150;
|
||||
float server_window_width_ = 250;
|
||||
float server_window_height_ = 150;
|
||||
float server_window_title_bar_height_ = 30.0f;
|
||||
SDL_PixelFormat server_pixformat_ = SDL_PIXELFORMAT_NV12;
|
||||
int server_window_normal_width_ = 300;
|
||||
int server_window_normal_height_ = 450;
|
||||
int server_window_normal_width_ = 250;
|
||||
int server_window_normal_height_ = 150;
|
||||
float server_window_dpi_scaling_w_ = 1.0f;
|
||||
float server_window_dpi_scaling_h_ = 1.0f;
|
||||
float window_rounding_ = 6.0f;
|
||||
float window_rounding_default_ = 6.0f;
|
||||
|
||||
// server window collapsed mode
|
||||
bool server_window_collapsed_ = false;
|
||||
@@ -549,9 +655,11 @@ class Render {
|
||||
bool is_server_mode_ = false;
|
||||
bool reload_recent_connections_ = true;
|
||||
bool show_confirm_delete_connection_ = false;
|
||||
bool show_offline_warning_window_ = false;
|
||||
bool delete_connection_ = false;
|
||||
bool is_tab_bar_hovered_ = false;
|
||||
std::string delete_connection_name_ = "";
|
||||
std::string offline_warning_text_ = "";
|
||||
bool re_enter_remote_id_ = false;
|
||||
double copy_start_time_ = 0;
|
||||
SignalStatus signal_status_ = SignalStatus::SignalClosed;
|
||||
@@ -563,6 +671,8 @@ class Render {
|
||||
std::string video_secondary_label_ = "secondary_display";
|
||||
std::string audio_label_ = "audio";
|
||||
std::string data_label_ = "data";
|
||||
std::string mouse_label_ = "mouse";
|
||||
std::string keyboard_label_ = "keyboard";
|
||||
std::string info_label_ = "info";
|
||||
std::string control_data_label_ = "control_data";
|
||||
std::string file_label_ = "file";
|
||||
@@ -573,8 +683,12 @@ class Render {
|
||||
std::unordered_map<uint32_t, std::weak_ptr<SubStreamWindowProperties>>
|
||||
file_id_to_props_;
|
||||
std::shared_mutex file_id_to_props_mutex_;
|
||||
SDL_AudioDeviceID input_dev_;
|
||||
SDL_AudioDeviceID output_dev_;
|
||||
|
||||
// Map file_id to FileTransferState for global file transfer (props == null)
|
||||
std::unordered_map<uint32_t, FileTransferState*> file_id_to_transfer_state_;
|
||||
std::shared_mutex file_id_to_transfer_state_mutex_;
|
||||
SDL_AudioDeviceID input_dev_ = 0;
|
||||
SDL_AudioDeviceID output_dev_ = 0;
|
||||
ScreenCapturerFactory* screen_capturer_factory_ = nullptr;
|
||||
ScreenCapturer* screen_capturer_ = nullptr;
|
||||
SpeakerCapturerFactory* speaker_capturer_factory_ = nullptr;
|
||||
@@ -583,13 +697,20 @@ class Render {
|
||||
MouseController* mouse_controller_ = nullptr;
|
||||
KeyboardCapturer* keyboard_capturer_ = nullptr;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
uint64_t last_frame_time_;
|
||||
uint64_t last_frame_time_ = 0;
|
||||
std::string last_video_frame_stream_id_;
|
||||
bool show_new_version_icon_ = false;
|
||||
bool show_new_version_icon_in_menu_ = true;
|
||||
uint64_t new_version_icon_last_trigger_time_ = 0;
|
||||
uint64_t new_version_icon_render_start_time_ = 0;
|
||||
double new_version_icon_last_trigger_time_ = 0.0;
|
||||
double new_version_icon_render_start_time_ = 0.0;
|
||||
#ifdef __APPLE__
|
||||
bool show_request_permission_window_ = true;
|
||||
bool mac_permission_status_initialized_ = false;
|
||||
uint32_t mac_permission_last_check_tick_ = 0;
|
||||
bool mac_screen_recording_permission_granted_ = false;
|
||||
bool mac_accessibility_permission_granted_ = false;
|
||||
bool mac_screen_recording_permission_requested_ = false;
|
||||
bool mac_accessibility_permission_requested_ = false;
|
||||
#endif
|
||||
char client_id_[10] = "";
|
||||
char client_id_display_[12] = "";
|
||||
@@ -598,16 +719,15 @@ class Render {
|
||||
char self_hosted_id_[17] = "";
|
||||
char self_hosted_user_id_[17] = "";
|
||||
int language_button_value_ = 0;
|
||||
int video_quality_button_value_ = 0;
|
||||
int video_quality_button_value_ = 2;
|
||||
int video_frame_rate_button_value_ = 1;
|
||||
int video_encode_format_button_value_ = 0;
|
||||
bool enable_hardware_video_codec_ = false;
|
||||
bool enable_hardware_video_codec_ = true;
|
||||
bool enable_turn_ = true;
|
||||
bool enable_srtp_ = false;
|
||||
char signal_server_ip_[256] = "api.crossdesk.cn";
|
||||
char signal_server_port_[6] = "9099";
|
||||
char coturn_server_port_[6] = "3478";
|
||||
char cert_file_path_[256] = "";
|
||||
bool enable_self_hosted_ = false;
|
||||
int language_button_value_last_ = 0;
|
||||
int video_quality_button_value_last_ = 0;
|
||||
@@ -623,10 +743,11 @@ class Render {
|
||||
bool enable_daemon_last_ = false;
|
||||
bool enable_minimize_to_tray_ = false;
|
||||
bool enable_minimize_to_tray_last_ = false;
|
||||
char file_transfer_save_path_buf_[512] = "";
|
||||
std::string file_transfer_save_path_last_ = "";
|
||||
char signal_server_ip_self_[256] = "";
|
||||
char signal_server_port_self_[6] = "";
|
||||
char coturn_server_port_self_[6] = "";
|
||||
std::string tls_cert_path_self_ = "";
|
||||
bool settings_window_pos_reset_ = true;
|
||||
bool self_hosted_server_config_window_pos_reset_ = true;
|
||||
std::string selected_current_file_path_ = "";
|
||||
@@ -647,6 +768,17 @@ class Render {
|
||||
|
||||
/* ------ server mode ------ */
|
||||
std::unordered_map<std::string, ConnectionStatus> connection_status_;
|
||||
std::unordered_map<std::string, std::string> connection_host_names_;
|
||||
std::string selected_server_remote_id_ = "";
|
||||
std::string selected_server_remote_hostname_ = "";
|
||||
std::mutex pending_presence_probe_mutex_;
|
||||
bool pending_presence_probe_ = false;
|
||||
bool pending_presence_result_ready_ = false;
|
||||
bool pending_presence_online_ = false;
|
||||
std::string pending_presence_remote_id_ = "";
|
||||
std::string pending_presence_password_ = "";
|
||||
bool pending_presence_remember_password_ = false;
|
||||
FileTransferState file_transfer_;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
+982
-147
File diff suppressed because it is too large
Load Diff
@@ -15,18 +15,22 @@
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
std::string OpenFileDialog(std::string title) {
|
||||
const char* path = tinyfd_openFileDialog(title.c_str(),
|
||||
"", // default path
|
||||
0, // number of filters
|
||||
nullptr, // filters
|
||||
nullptr, // filter description
|
||||
0 // no multiple selection
|
||||
);
|
||||
namespace {
|
||||
|
||||
return path ? path : "";
|
||||
void ShowControlBarTooltip(const std::string& text) {
|
||||
if (!ImGui::IsItemHovered() || text.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::Text("%s", text.c_str());
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int CountDigits(int number) {
|
||||
if (number == 0) return 1;
|
||||
return (int)std::floor(std::log10(std::abs(number))) + 1;
|
||||
@@ -53,13 +57,29 @@ int LossRateDisplay(float loss_rate) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Render::OpenFileDialog(std::string title) {
|
||||
const char* path = tinyfd_openFileDialog(title.c_str(),
|
||||
"", // default path
|
||||
0, // number of filters
|
||||
nullptr, // filters
|
||||
nullptr, // filter description
|
||||
0 // no multiple selection
|
||||
);
|
||||
|
||||
return path ? path : "";
|
||||
}
|
||||
|
||||
void Render::ProcessSelectedFile(
|
||||
const std::string& path, std::shared_ptr<SubStreamWindowProperties>& props,
|
||||
const std::string& file_label) {
|
||||
const std::string& path,
|
||||
const std::shared_ptr<SubStreamWindowProperties>& props,
|
||||
const std::string& file_label, const std::string& remote_id) {
|
||||
if (path.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
FileTransferState* file_transfer_state =
|
||||
props ? &props->file_transfer_ : &file_transfer_;
|
||||
|
||||
LOG_INFO("Selected file: {}", path.c_str());
|
||||
|
||||
std::filesystem::path file_path = std::filesystem::u8path(path);
|
||||
@@ -74,47 +94,51 @@ void Render::ProcessSelectedFile(
|
||||
|
||||
// Add file to transfer list
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(props->file_transfer_list_mutex_);
|
||||
SubStreamWindowProperties::FileTransferInfo info;
|
||||
std::lock_guard<std::mutex> lock(
|
||||
file_transfer_state->file_transfer_list_mutex_);
|
||||
FileTransferState::FileTransferInfo info;
|
||||
info.file_name = file_path.filename().u8string();
|
||||
info.file_path = file_path; // Store full path for precise matching
|
||||
info.file_size = file_size;
|
||||
info.status = SubStreamWindowProperties::FileTransferStatus::Queued;
|
||||
info.status = FileTransferState::FileTransferStatus::Queued;
|
||||
info.sent_bytes = 0;
|
||||
info.file_id = 0;
|
||||
info.rate_bps = 0;
|
||||
props->file_transfer_list_.push_back(info);
|
||||
file_transfer_state->file_transfer_list_.push_back(info);
|
||||
}
|
||||
props->file_transfer_window_visible_ = true;
|
||||
file_transfer_state->file_transfer_window_visible_ = true;
|
||||
|
||||
if (props->file_sending_.load()) {
|
||||
if (file_transfer_state->file_sending_.load()) {
|
||||
// Add to queue
|
||||
size_t queue_size = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(props->file_queue_mutex_);
|
||||
SubStreamWindowProperties::QueuedFile queued_file;
|
||||
std::lock_guard<std::mutex> lock(file_transfer_state->file_queue_mutex_);
|
||||
FileTransferState::QueuedFile queued_file;
|
||||
queued_file.file_path = file_path;
|
||||
queued_file.file_label = file_label;
|
||||
props->file_send_queue_.push(queued_file);
|
||||
queue_size = props->file_send_queue_.size();
|
||||
queued_file.remote_id = remote_id;
|
||||
file_transfer_state->file_send_queue_.push(queued_file);
|
||||
queue_size = file_transfer_state->file_send_queue_.size();
|
||||
}
|
||||
LOG_INFO("File added to queue: {} ({} files in queue)",
|
||||
file_path.filename().string().c_str(), queue_size);
|
||||
} else {
|
||||
StartFileTransfer(props, file_path, file_label);
|
||||
StartFileTransfer(props, file_path, file_label, remote_id);
|
||||
|
||||
if (props->file_sending_.load()) {
|
||||
if (file_transfer_state->file_sending_.load()) {
|
||||
} else {
|
||||
// Failed to start (race condition: another file started between
|
||||
// check and call) Add to queue
|
||||
size_t queue_size = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(props->file_queue_mutex_);
|
||||
SubStreamWindowProperties::QueuedFile queued_file;
|
||||
std::lock_guard<std::mutex> lock(
|
||||
file_transfer_state->file_queue_mutex_);
|
||||
FileTransferState::QueuedFile queued_file;
|
||||
queued_file.file_path = file_path;
|
||||
queued_file.file_label = file_label;
|
||||
props->file_send_queue_.push(queued_file);
|
||||
queue_size = props->file_send_queue_.size();
|
||||
queued_file.remote_id = remote_id;
|
||||
file_transfer_state->file_send_queue_.push(queued_file);
|
||||
queue_size = file_transfer_state->file_send_queue_.size();
|
||||
}
|
||||
LOG_INFO(
|
||||
"File added to queue after race condition: {} ({} files in "
|
||||
@@ -130,10 +154,10 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
float line_padding = title_bar_height_ * 0.12f;
|
||||
float line_thickness = title_bar_height_ * 0.07f;
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
if (props->control_bar_expand_) {
|
||||
ImGui::SetCursorPosX(props->is_control_bar_in_left_
|
||||
? props->control_window_width_ * 1.03f
|
||||
? props->control_window_width_ * 0.03f
|
||||
: props->control_window_width_ * 0.17f);
|
||||
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
@@ -154,7 +178,10 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
|
||||
ImVec2 btn_min = ImGui::GetItemRectMin();
|
||||
ImVec2 btn_size_actual = ImGui::GetItemRectSize();
|
||||
ShowControlBarTooltip(
|
||||
localization::select_display[localization_language_index_]);
|
||||
|
||||
props->display_selectable_hovered_ = false;
|
||||
if (ImGui::BeginPopup("display")) {
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
for (int i = 0; i < props->display_info_list_.size(); i++) {
|
||||
@@ -170,44 +197,92 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
props->control_data_label_.c_str());
|
||||
}
|
||||
}
|
||||
props->display_selectable_hovered_ = ImGui::IsWindowHovered();
|
||||
}
|
||||
props->display_selectable_hovered_ =
|
||||
ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::SetWindowFontScale(0.35f);
|
||||
ImVec2 text_size = ImGui::CalcTextSize(
|
||||
std::to_string(props->selected_display_ + 1).c_str());
|
||||
ImVec2 text_pos =
|
||||
ImVec2(btn_min.x + (btn_size_actual.x - text_size.x) * 0.5f,
|
||||
btn_min.y + (btn_size_actual.y - text_size.y) * 0.35f);
|
||||
ImVec2(btn_min.x + (btn_size_actual.x - text_size.x) * 0.55f,
|
||||
btn_min.y + (btn_size_actual.y - text_size.y) * 0.33f);
|
||||
ImGui::GetWindowDrawList()->AddText(
|
||||
text_pos, IM_COL32(0, 0, 0, 255),
|
||||
std::to_string(props->selected_display_ + 1).c_str());
|
||||
|
||||
auto send_service_command = [&](ServiceCommandFlag flag,
|
||||
const char* log_action) {
|
||||
if (props->connection_status_ == ConnectionStatus::Connected &&
|
||||
props->peer_) {
|
||||
RemoteAction remote_action;
|
||||
remote_action.type = ControlType::service_command;
|
||||
remote_action.c.flag = flag;
|
||||
std::string msg = remote_action.to_json();
|
||||
int ret = SendReliableDataFrame(props->peer_, msg.c_str(), msg.size(),
|
||||
props->control_data_label_.c_str());
|
||||
if (ret != 0) {
|
||||
LOG_WARN("Send {} command failed, remote_id={}, ret={}", log_action,
|
||||
props->remote_id_, ret);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ImGui::SameLine();
|
||||
std::string shortcut = ICON_FA_KEYBOARD;
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
if (ImGui::Button(shortcut.c_str(), ImVec2(button_width, button_height))) {
|
||||
ImGui::OpenPopup("shortcut");
|
||||
}
|
||||
ShowControlBarTooltip(
|
||||
localization::send_shortcut[localization_language_index_]);
|
||||
|
||||
props->shortcut_selectable_hovered_ = false;
|
||||
if (ImGui::BeginPopup("shortcut")) {
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
std::string sas_label = "Ctrl+Alt+Del";
|
||||
std::string lock_label = "Win+L";
|
||||
if (ImGui::Selectable(sas_label.c_str())) {
|
||||
send_service_command(ServiceCommandFlag::send_sas, "SAS");
|
||||
}
|
||||
if (ImGui::Selectable(lock_label.c_str())) {
|
||||
send_service_command(ServiceCommandFlag::lock_workstation,
|
||||
"remote lock");
|
||||
}
|
||||
props->shortcut_selectable_hovered_ =
|
||||
ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
float mouse_x = ImGui::GetCursorScreenPos().x;
|
||||
float mouse_y = ImGui::GetCursorScreenPos().y;
|
||||
float disable_mouse_x = mouse_x + line_padding;
|
||||
float disable_mouse_y = mouse_y + line_padding;
|
||||
std::string mouse = props->mouse_control_button_pressed_
|
||||
? ICON_FA_COMPUTER_MOUSE
|
||||
: ICON_FA_COMPUTER_MOUSE;
|
||||
std::string mouse = ICON_FA_COMPUTER_MOUSE;
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
if (ImGui::Button(mouse.c_str(), ImVec2(button_width, button_height))) {
|
||||
if (props->connection_established_) {
|
||||
start_keyboard_capturer_ = !start_keyboard_capturer_;
|
||||
props->control_mouse_ = !props->control_mouse_;
|
||||
props->mouse_control_button_pressed_ =
|
||||
!props->mouse_control_button_pressed_;
|
||||
props->enable_mouse_control_ = !props->enable_mouse_control_;
|
||||
props->mouse_control_button_label_ =
|
||||
props->mouse_control_button_pressed_
|
||||
props->enable_mouse_control_
|
||||
? localization::release_mouse[localization_language_index_]
|
||||
: localization::control_mouse[localization_language_index_];
|
||||
}
|
||||
}
|
||||
const bool mouse_button_hovered = ImGui::IsItemHovered();
|
||||
const std::string mouse_tooltip =
|
||||
props->enable_mouse_control_
|
||||
? localization::release_mouse[localization_language_index_]
|
||||
: localization::control_mouse[localization_language_index_];
|
||||
ShowControlBarTooltip(mouse_tooltip);
|
||||
|
||||
if (!props->mouse_control_button_pressed_) {
|
||||
if (!props->enable_mouse_control_) {
|
||||
draw_list->AddLine(ImVec2(disable_mouse_x, disable_mouse_y),
|
||||
ImVec2(mouse_x + button_width - line_padding,
|
||||
mouse_y + button_height - line_padding),
|
||||
@@ -218,8 +293,8 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
ImVec2(
|
||||
mouse_x + button_width - line_padding - line_thickness * 0.7f,
|
||||
mouse_y + button_height - line_padding + line_thickness * 0.7f),
|
||||
ImGui::IsItemHovered() ? IM_COL32(66, 150, 250, 255)
|
||||
: IM_COL32(179, 213, 253, 255),
|
||||
mouse_button_hovered ? IM_COL32(66, 150, 250, 255)
|
||||
: IM_COL32(179, 213, 253, 255),
|
||||
line_thickness);
|
||||
}
|
||||
|
||||
@@ -251,6 +326,12 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
props->control_data_label_.c_str());
|
||||
}
|
||||
}
|
||||
const bool audio_button_hovered = ImGui::IsItemHovered();
|
||||
const std::string audio_tooltip =
|
||||
props->audio_capture_button_pressed_
|
||||
? localization::mute[localization_language_index_]
|
||||
: localization::audio_capture[localization_language_index_];
|
||||
ShowControlBarTooltip(audio_tooltip);
|
||||
|
||||
if (!props->audio_capture_button_pressed_) {
|
||||
draw_list->AddLine(ImVec2(disable_audio_x, disable_audio_y),
|
||||
@@ -263,8 +344,8 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
ImVec2(
|
||||
audio_x + button_width - line_padding - line_thickness * 0.7f,
|
||||
audio_y + button_height - line_padding + line_thickness * 0.7f),
|
||||
ImGui::IsItemHovered() ? IM_COL32(66, 150, 250, 255)
|
||||
: IM_COL32(179, 213, 253, 255),
|
||||
audio_button_hovered ? IM_COL32(66, 150, 250, 255)
|
||||
: IM_COL32(179, 213, 253, 255),
|
||||
line_thickness);
|
||||
}
|
||||
|
||||
@@ -275,8 +356,10 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
std::string title =
|
||||
localization::select_file[localization_language_index_];
|
||||
std::string path = OpenFileDialog(title);
|
||||
this->ProcessSelectedFile(path, props, file_label_);
|
||||
ProcessSelectedFile(path, props, file_label_);
|
||||
}
|
||||
ShowControlBarTooltip(
|
||||
localization::select_file[localization_language_index_]);
|
||||
|
||||
ImGui::SameLine();
|
||||
// net traffic stats button
|
||||
@@ -301,6 +384,12 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
: localization::show_net_traffic_stats
|
||||
[localization_language_index_];
|
||||
}
|
||||
const std::string net_traffic_stats_tooltip =
|
||||
props->net_traffic_stats_button_pressed_
|
||||
? localization::hide_net_traffic_stats[localization_language_index_]
|
||||
: localization::show_net_traffic_stats
|
||||
[localization_language_index_];
|
||||
ShowControlBarTooltip(net_traffic_stats_tooltip);
|
||||
|
||||
if (button_color_style_pushed) {
|
||||
ImGui::PopStyleColor();
|
||||
@@ -327,6 +416,11 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
}
|
||||
props->reset_control_bar_pos_ = true;
|
||||
}
|
||||
const std::string fullscreen_tooltip =
|
||||
fullscreen_button_pressed_
|
||||
? localization::exit_fullscreen[localization_language_index_]
|
||||
: localization::fullscreen[localization_language_index_];
|
||||
ShowControlBarTooltip(fullscreen_tooltip);
|
||||
|
||||
ImGui::SameLine();
|
||||
// close button
|
||||
@@ -336,6 +430,8 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
ImVec2(button_width, button_height))) {
|
||||
CleanupPeer(props);
|
||||
}
|
||||
ShowControlBarTooltip(
|
||||
localization::disconnect[localization_language_index_]);
|
||||
|
||||
ImGui::SameLine();
|
||||
|
||||
@@ -351,13 +447,11 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
ImGui::SameLine();
|
||||
}
|
||||
|
||||
float expand_button_pos_x =
|
||||
props->control_bar_expand_ ? (props->is_control_bar_in_left_
|
||||
? props->control_window_width_ * 1.917f
|
||||
: props->control_window_width_ * 0.03f)
|
||||
: (props->is_control_bar_in_left_
|
||||
? props->control_window_width_ * 1.02f
|
||||
: props->control_window_width_ * 0.23f);
|
||||
float expand_button_pos_x = props->control_bar_expand_
|
||||
? (props->is_control_bar_in_left_
|
||||
? props->control_window_width_ * 0.917f
|
||||
: props->control_window_width_ * 0.03f)
|
||||
: props->control_window_width_ * 0.11f;
|
||||
|
||||
ImGui::SetCursorPosX(expand_button_pos_x);
|
||||
|
||||
@@ -367,6 +461,10 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
: ICON_FA_ANGLE_RIGHT)
|
||||
: (props->is_control_bar_in_left_ ? ICON_FA_ANGLE_RIGHT
|
||||
: ICON_FA_ANGLE_LEFT);
|
||||
const std::string control_bar_tooltip =
|
||||
props->control_bar_expand_
|
||||
? localization::collapse_control_bar[localization_language_index_]
|
||||
: localization::expand_control_bar[localization_language_index_];
|
||||
if (ImGui::Button(control_bar.c_str(),
|
||||
ImVec2(button_height * 0.6f, button_height))) {
|
||||
props->control_bar_expand_ = !props->control_bar_expand_;
|
||||
@@ -378,6 +476,7 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
props->net_traffic_stats_button_pressed_ = false;
|
||||
}
|
||||
}
|
||||
ShowControlBarTooltip(control_bar_tooltip);
|
||||
|
||||
if (props->net_traffic_stats_button_pressed_ && props->control_bar_expand_) {
|
||||
NetTrafficStats(props);
|
||||
@@ -389,9 +488,7 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
}
|
||||
|
||||
int Render::NetTrafficStats(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
ImGui::SetCursorPos(ImVec2(props->is_control_bar_in_left_
|
||||
? props->control_window_width_ * 1.02f
|
||||
: props->control_window_width_ * 0.02f,
|
||||
ImGui::SetCursorPos(ImVec2(props->control_window_width_ * 0.048f,
|
||||
props->control_window_min_height_));
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
if (ImGui::BeginTable("NetTrafficStats", 4, ImGuiTableFlags_BordersH,
|
||||
@@ -452,16 +549,37 @@ int Render::NetTrafficStats(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
LossRateDisplay(props->net_traffic_stats_.total_inbound_stats.loss_rate);
|
||||
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("FPS");
|
||||
ImGui::Text("FPS:");
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", props->fps_);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TableNextColumn();
|
||||
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s:",
|
||||
localization::resolution[localization_language_index_].c_str());
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%dx%d", props->video_width_, props->video_height_);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TableNextColumn();
|
||||
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text(
|
||||
"%s:",
|
||||
localization::connection_mode[localization_language_index_].c_str());
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
props->traversal_mode_ == 0
|
||||
? localization::connection_mode_direct[localization_language_index_]
|
||||
.c_str()
|
||||
: localization::connection_mode_relay[localization_language_index_]
|
||||
.c_str());
|
||||
|
||||
ImGui::EndTable();
|
||||
}
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -9,39 +9,51 @@ int Render::StatusBar() {
|
||||
float status_bar_width = io.DisplaySize.x;
|
||||
float status_bar_height = io.DisplaySize.y * STATUS_BAR_HEIGHT;
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
static bool a, b, c, d, e;
|
||||
ImGui::SetNextWindowPos(ImVec2(0, io.DisplaySize.y * (1 - STATUS_BAR_HEIGHT)),
|
||||
ImGuiCond_Always);
|
||||
|
||||
ImGui::BeginChild(
|
||||
"StatusBar", ImVec2(status_bar_width, status_bar_height),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 0.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1.0f, 1.0f, 1.0f, 0.0f));
|
||||
ImGui::BeginChild("StatusBar", ImVec2(status_bar_width, status_bar_height),
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PopStyleColor(2);
|
||||
|
||||
ImVec2 dot_pos = ImVec2(status_bar_width * 0.025f,
|
||||
io.DisplaySize.y * (1 - STATUS_BAR_HEIGHT * 0.5f));
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
draw_list->AddCircleFilled(dot_pos, status_bar_height * 0.25f,
|
||||
ImColor(1.0f, 1.0f, 1.0f), 100);
|
||||
bool tls_cert_error =
|
||||
signal_status_ == SignalStatus::SignalTlsCertError;
|
||||
draw_list->AddCircleFilled(dot_pos, status_bar_height * 0.2f,
|
||||
ImColor(signal_connected_ ? 0.0f : 1.0f,
|
||||
signal_connected_ ? 1.0f : 0.0f, 0.0f),
|
||||
tls_cert_error
|
||||
? ImColor(1.0f, 0.65f, 0.0f)
|
||||
: ImColor(signal_connected_ ? 0.0f : 1.0f,
|
||||
signal_connected_ ? 1.0f : 0.0f,
|
||||
0.0f),
|
||||
100);
|
||||
draw_list->AddCircle(dot_pos, status_bar_height * 0.25f,
|
||||
ImColor(1.0f, 1.0f, 1.0f), 100);
|
||||
|
||||
ImGui::SetWindowFontScale(0.6f);
|
||||
const char* signal_status_text =
|
||||
tls_cert_error
|
||||
? localization::signal_tls_cert_error[localization_language_index_]
|
||||
.c_str()
|
||||
: (signal_connected_
|
||||
? localization::signal_connected[localization_language_index_]
|
||||
.c_str()
|
||||
: localization::signal_disconnected
|
||||
[localization_language_index_]
|
||||
.c_str());
|
||||
draw_list->AddText(
|
||||
ImVec2(status_bar_width * 0.045f,
|
||||
io.DisplaySize.y * (1 - STATUS_BAR_HEIGHT * 0.9f)),
|
||||
ImColor(0.0f, 0.0f, 0.0f),
|
||||
signal_connected_
|
||||
? localization::signal_connected[localization_language_index_].c_str()
|
||||
: localization::signal_disconnected[localization_language_index_]
|
||||
.c_str());
|
||||
ImColor(0.0f, 0.0f, 0.0f), signal_status_text);
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
ImGui::PopStyleColor();
|
||||
ImGui::EndChild();
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
#include "localization.h"
|
||||
#include "rd_log.h"
|
||||
#include "render.h"
|
||||
#include "rounded_corner_button.h"
|
||||
|
||||
#define NEW_VERSION_ICON_RENDER_TIME_INTERVAL 2000
|
||||
constexpr double kNewVersionIconBlinkIntervalSec = 2.0;
|
||||
constexpr double kNewVersionIconBlinkOnTimeSec = 1.0;
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
@@ -15,14 +17,28 @@ int Render::TitleBar(bool main_window) {
|
||||
float title_bar_button_width = title_bar_button_width_;
|
||||
float title_bar_button_height = title_bar_button_height_;
|
||||
if (main_window) {
|
||||
title_bar_width = io.DisplaySize.x;
|
||||
title_bar_height = io.DisplaySize.y * TITLE_BAR_HEIGHT;
|
||||
title_bar_height_padding = io.DisplaySize.y * (TITLE_BAR_HEIGHT + 0.01f);
|
||||
title_bar_button_width = io.DisplaySize.x * TITLE_BAR_BUTTON_WIDTH;
|
||||
title_bar_button_height = io.DisplaySize.y * TITLE_BAR_BUTTON_HEIGHT;
|
||||
title_bar_height_ = title_bar_height;
|
||||
title_bar_button_width_ = title_bar_button_width;
|
||||
title_bar_button_height_ = title_bar_button_height;
|
||||
// When the main window is minimized, Dear ImGui may report DisplaySize as
|
||||
// (0, 0). Do not overwrite shared title-bar metrics with zeros, otherwise
|
||||
// stream/server windows (which reuse these metrics) will lose their title
|
||||
// bars and appear collapsed.
|
||||
if (io.DisplaySize.x > 0.0f && io.DisplaySize.y > 0.0f) {
|
||||
title_bar_width = io.DisplaySize.x;
|
||||
title_bar_height = io.DisplaySize.y * TITLE_BAR_HEIGHT;
|
||||
title_bar_height_padding = io.DisplaySize.y * TITLE_BAR_HEIGHT;
|
||||
title_bar_button_width = io.DisplaySize.x * TITLE_BAR_BUTTON_WIDTH;
|
||||
title_bar_button_height = io.DisplaySize.y * TITLE_BAR_BUTTON_HEIGHT;
|
||||
|
||||
title_bar_height_ = title_bar_height;
|
||||
title_bar_button_width_ = title_bar_button_width;
|
||||
title_bar_button_height_ = title_bar_button_height;
|
||||
} else {
|
||||
// Keep using last known good values.
|
||||
title_bar_width = title_bar_width_;
|
||||
title_bar_height = title_bar_height_;
|
||||
title_bar_height_padding = title_bar_height_;
|
||||
title_bar_button_width = title_bar_button_width_;
|
||||
title_bar_button_height = title_bar_button_height_;
|
||||
}
|
||||
} else {
|
||||
title_bar_width = io.DisplaySize.x;
|
||||
title_bar_height = title_bar_button_height_;
|
||||
@@ -30,12 +46,12 @@ int Render::TitleBar(bool main_window) {
|
||||
title_bar_button_height = title_bar_button_height_;
|
||||
}
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 0.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
|
||||
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
|
||||
ImGui::BeginChild(main_window ? "MainTitleBar" : "StreamTitleBar",
|
||||
ImVec2(title_bar_width, title_bar_height_padding),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PopStyleVar();
|
||||
@@ -92,13 +108,11 @@ int Render::TitleBar(bool main_window) {
|
||||
|
||||
std::string about_str = localization::about[localization_language_index_];
|
||||
if (update_available_) {
|
||||
auto now_time = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
const double now_time = ImGui::GetTime();
|
||||
|
||||
// every 2 seconds
|
||||
if (now_time - new_version_icon_last_trigger_time_ >=
|
||||
NEW_VERSION_ICON_RENDER_TIME_INTERVAL) {
|
||||
kNewVersionIconBlinkIntervalSec) {
|
||||
show_new_version_icon_ = true;
|
||||
new_version_icon_render_start_time_ = now_time;
|
||||
new_version_icon_last_trigger_time_ = now_time;
|
||||
@@ -106,9 +120,9 @@ int Render::TitleBar(bool main_window) {
|
||||
|
||||
// render for 1 second
|
||||
if (show_new_version_icon_) {
|
||||
about_str = about_str + " " + ICON_FA_TRIANGLE_EXCLAMATION;
|
||||
about_str = about_str + " " + ICON_FA_CIRCLE_ARROW_UP;
|
||||
if (now_time - new_version_icon_render_start_time_ >=
|
||||
NEW_VERSION_ICON_RENDER_TIME_INTERVAL / 2) {
|
||||
kNewVersionIconBlinkOnTimeSec) {
|
||||
show_new_version_icon_ = false;
|
||||
}
|
||||
} else {
|
||||
@@ -137,13 +151,11 @@ int Render::TitleBar(bool main_window) {
|
||||
}
|
||||
|
||||
if (update_available_ && show_new_version_icon_in_menu_) {
|
||||
auto now_time = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
const double now_time = ImGui::GetTime();
|
||||
|
||||
// every 2 seconds
|
||||
if (now_time - new_version_icon_last_trigger_time_ >=
|
||||
NEW_VERSION_ICON_RENDER_TIME_INTERVAL) {
|
||||
kNewVersionIconBlinkIntervalSec) {
|
||||
show_new_version_icon_ = true;
|
||||
new_version_icon_render_start_time_ = now_time;
|
||||
new_version_icon_last_trigger_time_ = now_time;
|
||||
@@ -152,14 +164,13 @@ int Render::TitleBar(bool main_window) {
|
||||
// render for 1 second
|
||||
if (show_new_version_icon_) {
|
||||
ImGui::SetWindowFontScale(0.6f);
|
||||
ImGui::SetCursorPos(
|
||||
ImVec2(bar_pos_x + title_bar_button_width * 0.15f,
|
||||
bar_pos_y - title_bar_button_width * 0.325f));
|
||||
ImGui::Text(ICON_FA_TRIANGLE_EXCLAMATION);
|
||||
ImGui::SetCursorPos(ImVec2(bar_pos_x + title_bar_button_width * 0.21f,
|
||||
bar_pos_y - title_bar_button_width * 0.24f));
|
||||
ImGui::Text(ICON_FA_CIRCLE_ARROW_UP);
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
if (now_time - new_version_icon_render_start_time_ >=
|
||||
NEW_VERSION_ICON_RENDER_TIME_INTERVAL / 2) {
|
||||
kNewVersionIconBlinkOnTimeSec) {
|
||||
show_new_version_icon_ = false;
|
||||
}
|
||||
}
|
||||
@@ -187,6 +198,11 @@ int Render::TitleBar(bool main_window) {
|
||||
std::string window_minimize_button = "##minimize"; // ICON_FA_MINUS;
|
||||
if (ImGui::Button(window_minimize_button.c_str(),
|
||||
ImVec2(title_bar_button_width, title_bar_button_height))) {
|
||||
if (main_window) {
|
||||
last_main_minimize_request_tick_ = SDL_GetTicks();
|
||||
} else {
|
||||
last_stream_minimize_request_tick_ = SDL_GetTicks();
|
||||
}
|
||||
SDL_MinimizeWindow(main_window ? main_window_ : stream_window_);
|
||||
}
|
||||
draw_list->AddLine(
|
||||
@@ -270,8 +286,20 @@ int Render::TitleBar(bool main_window) {
|
||||
float xmark_pos_y = title_bar_button_height * 0.5f;
|
||||
float xmark_size = title_bar_button_width * 0.33f;
|
||||
std::string close_button = "##xmark"; // ICON_FA_XMARK;
|
||||
if (ImGui::Button(close_button.c_str(),
|
||||
ImVec2(title_bar_button_width, title_bar_button_height))) {
|
||||
bool close_button_clicked = false;
|
||||
if (main_window) {
|
||||
close_button_clicked = RoundedCornerButton(
|
||||
close_button.c_str(),
|
||||
ImVec2(title_bar_button_width, title_bar_button_height), 8.5f,
|
||||
ImDrawFlags_RoundCornersTopRight, true, IM_COL32(0, 0, 0, 0),
|
||||
IM_COL32(250, 0, 0, 255), IM_COL32(255, 0, 0, 128));
|
||||
} else {
|
||||
close_button_clicked =
|
||||
ImGui::Button(close_button.c_str(),
|
||||
ImVec2(title_bar_button_width, title_bar_button_height));
|
||||
}
|
||||
|
||||
if (close_button_clicked) {
|
||||
#if _WIN32
|
||||
if (enable_minimize_to_tray_) {
|
||||
tray_->MinimizeToTray();
|
||||
@@ -284,6 +312,7 @@ int Render::TitleBar(bool main_window) {
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
draw_list->AddLine(ImVec2(xmark_pos_x - xmark_size / 2 - 0.25f,
|
||||
xmark_pos_y - xmark_size / 2 + 0.75f),
|
||||
ImVec2(xmark_pos_x + xmark_size / 2 - 1.5f,
|
||||
@@ -300,4 +329,4 @@ int Render::TitleBar(bool main_window) {
|
||||
ImGui::EndChild();
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -89,7 +89,7 @@ bool WinTray::HandleTrayMessage(MSG* msg) {
|
||||
GetCursorPos(&pt);
|
||||
HMENU menu = CreatePopupMenu();
|
||||
AppendMenuW(menu, MF_STRING, 1001,
|
||||
localization::exit_program[language_index_]);
|
||||
localization::GetExitProgramLabel(language_index_));
|
||||
|
||||
SetForegroundWindow(hwnd_message_only_);
|
||||
int cmd =
|
||||
@@ -112,4 +112,4 @@ bool WinTray::HandleTrayMessage(MSG* msg) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "layout.h"
|
||||
#include "localization.h"
|
||||
#include "rd_log.h"
|
||||
@@ -8,11 +12,44 @@
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
bool Render::OpenUrl(const std::string& url) {
|
||||
#if defined(_WIN32)
|
||||
int wide_len = MultiByteToWideChar(CP_UTF8, 0, url.c_str(), -1, nullptr, 0);
|
||||
if (wide_len <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::wstring wide_url(static_cast<size_t>(wide_len), L'\0');
|
||||
MultiByteToWideChar(CP_UTF8, 0, url.c_str(), -1, &wide_url[0], wide_len);
|
||||
if (!wide_url.empty() && wide_url.back() == L'\0') {
|
||||
wide_url.pop_back();
|
||||
}
|
||||
|
||||
std::wstring cmd = L"cmd.exe /c start \"\" \"" + wide_url + L"\"";
|
||||
STARTUPINFOW startup_info = {sizeof(startup_info)};
|
||||
PROCESS_INFORMATION process_info = {};
|
||||
if (!CreateProcessW(nullptr, &cmd[0], nullptr, nullptr, FALSE,
|
||||
CREATE_NO_WINDOW, nullptr, nullptr, &startup_info,
|
||||
&process_info)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
CloseHandle(process_info.hThread);
|
||||
CloseHandle(process_info.hProcess);
|
||||
return true;
|
||||
#elif defined(__APPLE__)
|
||||
std::string cmd = "open " + url;
|
||||
return system(cmd.c_str()) == 0;
|
||||
#else
|
||||
std::string cmd = "xdg-open " + url;
|
||||
return system(cmd.c_str()) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Render::Hyperlink(const std::string& label, const std::string& url,
|
||||
const float window_width) {
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0, 0, 255, 255));
|
||||
ImGui::SetCursorPosX(window_width * 0.1f);
|
||||
ImGui::Text("%s", label.c_str());
|
||||
ImGui::TextUnformatted(label.c_str());
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
if (ImGui::IsItemHovered()) {
|
||||
@@ -23,14 +60,7 @@ void Render::Hyperlink(const std::string& label, const std::string& url,
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::EndTooltip();
|
||||
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
|
||||
#if defined(_WIN32)
|
||||
std::string cmd = "start " + url;
|
||||
#elif defined(__APPLE__)
|
||||
std::string cmd = "open " + url;
|
||||
#else
|
||||
std::string cmd = "xdg-open " + url;
|
||||
#endif
|
||||
system(cmd.c_str()); // open browser
|
||||
OpenUrl(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,7 +70,7 @@ int Render::AboutWindow() {
|
||||
float about_window_width = title_bar_button_width_ * 7.5f;
|
||||
float about_window_height = latest_version_.empty()
|
||||
? title_bar_button_width_ * 4.0f
|
||||
: title_bar_button_width_ * 4.6f;
|
||||
: title_bar_button_width_ * 4.9f;
|
||||
|
||||
const ImGuiViewport* viewport = ImGui::GetMainViewport();
|
||||
ImGui::SetNextWindowPos(ImVec2(
|
||||
@@ -52,8 +82,8 @@ int Render::AboutWindow() {
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::Begin(
|
||||
localization::about[localization_language_index_].c_str(), nullptr,
|
||||
@@ -74,16 +104,23 @@ int Render::AboutWindow() {
|
||||
ImGui::SetCursorPosX(about_window_width * 0.1f);
|
||||
ImGui::Text("%s", text.c_str());
|
||||
|
||||
if (update_available_) {
|
||||
std::string latest_version =
|
||||
if (update_available_ && show_new_version_icon_in_menu_) {
|
||||
std::string new_version_available =
|
||||
localization::new_version_available[localization_language_index_] +
|
||||
": " + latest_version_;
|
||||
": ";
|
||||
ImGui::SetCursorPosX(about_window_width * 0.1f);
|
||||
ImGui::Text("%s", new_version_available.c_str());
|
||||
std::string access_website =
|
||||
localization::access_website[localization_language_index_];
|
||||
Hyperlink(latest_version, "https://crossdesk.cn", about_window_width);
|
||||
}
|
||||
ImGui::SetCursorPosX((about_window_width -
|
||||
ImGui::CalcTextSize(latest_version_.c_str()).x) /
|
||||
2.0f);
|
||||
Hyperlink(latest_version_, "https://crossdesk.cn", about_window_width);
|
||||
|
||||
ImGui::Text("");
|
||||
ImGui::Spacing();
|
||||
} else {
|
||||
ImGui::Text("%s", "");
|
||||
}
|
||||
|
||||
std::string copyright_text = "© 2025 by JUNKUN DI. All rights reserved.";
|
||||
std::string license_text = "Licensed under GNU LGPL v3.";
|
||||
@@ -93,7 +130,7 @@ int Render::AboutWindow() {
|
||||
ImGui::Text("%s", license_text.c_str());
|
||||
|
||||
ImGui::SetCursorPosX(about_window_width * 0.445f);
|
||||
ImGui::SetCursorPosY(about_window_height * 0.75f);
|
||||
ImGui::SetCursorPosY(about_window_height * 0.8f);
|
||||
// OK
|
||||
if (ImGui::Button(localization::ok[localization_language_index_].c_str())) {
|
||||
show_about_window_ = false;
|
||||
|
||||
@@ -15,10 +15,10 @@ bool Render::ConnectionStatusWindow(
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.33f, io.DisplaySize.y * 0.33f));
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
|
||||
ImGui::Begin("ConnectionStatusWindow", nullptr,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
@@ -36,6 +36,18 @@ bool Render::ConnectionStatusWindow(
|
||||
text = localization::p2p_connecting[localization_language_index_];
|
||||
ImGui::SetCursorPosX(connection_status_window_width * 0.43f);
|
||||
ImGui::SetCursorPosY(connection_status_window_height * 0.67f);
|
||||
// cancel
|
||||
if (ImGui::Button(
|
||||
localization::cancel[localization_language_index_].c_str()) ||
|
||||
ImGui::IsKeyPressed(ImGuiKey_Escape)) {
|
||||
show_connection_status_window_ = false;
|
||||
re_enter_remote_id_ = true;
|
||||
LOG_INFO("User cancelled connecting to [{}]", props->remote_id_);
|
||||
if (props->peer_) {
|
||||
LeaveConnection(props->peer_, props->remote_id_.c_str());
|
||||
}
|
||||
ret_flag = true;
|
||||
}
|
||||
} else if (ConnectionStatus::Connected == props->connection_status_) {
|
||||
text = localization::p2p_connected[localization_language_index_];
|
||||
ImGui::SetCursorPosX(connection_status_window_width * 0.43f);
|
||||
|
||||
@@ -42,50 +42,74 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1, 1, 1, 1));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 10.0f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_ * 1.5f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 10.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, window_rounding_ * 1.5f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
|
||||
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(props->control_window_width_, props->control_window_height_),
|
||||
ImGuiCond_Always);
|
||||
float y_boundary = fullscreen_button_pressed_ ? 0.0f : title_bar_height_;
|
||||
float container_x = 0.0f;
|
||||
float container_y = y_boundary;
|
||||
float container_w = stream_window_width_;
|
||||
float container_h = stream_window_height_ - y_boundary;
|
||||
|
||||
ImGui::SetNextWindowPos(ImVec2(0, title_bar_height_), ImGuiCond_Once);
|
||||
ImGui::SetNextWindowSize(ImVec2(container_w, container_h), ImGuiCond_Always);
|
||||
ImGui::SetNextWindowPos(ImVec2(container_x, container_y), ImGuiCond_Always);
|
||||
|
||||
float pos_x = 0;
|
||||
float pos_y = 0;
|
||||
float y_boundary = fullscreen_button_pressed_ ? 0 : title_bar_height_;
|
||||
|
||||
if (props->reset_control_bar_pos_) {
|
||||
float new_cursor_pos_x = 0;
|
||||
float new_cursor_pos_y = 0;
|
||||
std::string container_window_title =
|
||||
props->remote_id_ + "ControlContainerWindow";
|
||||
ImGui::Begin(container_window_title.c_str(), nullptr,
|
||||
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoDocking |
|
||||
ImGuiWindowFlags_NoBackground);
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
ImVec2 container_pos = ImGui::GetWindowPos();
|
||||
|
||||
if (ImGui::IsMouseDown(ImGuiMouseButton_Left) &&
|
||||
props->control_bar_hovered_) {
|
||||
float current_x_rel = props->control_window_pos_.x - container_pos.x;
|
||||
float current_y_rel = props->control_window_pos_.y - container_pos.y;
|
||||
ImVec2 delta = ImGui::GetIO().MouseDelta;
|
||||
pos_x = current_x_rel + delta.x;
|
||||
pos_y = current_y_rel + delta.y;
|
||||
if (pos_x < 0.0f) pos_x = 0.0f;
|
||||
if (pos_y < 0.0f) pos_y = 0.0f;
|
||||
if (pos_x + props->control_window_width_ > container_w)
|
||||
pos_x = container_w - props->control_window_width_;
|
||||
if (pos_y + props->control_window_height_ > container_h)
|
||||
pos_y = container_h - props->control_window_height_;
|
||||
} else if (props->reset_control_bar_pos_) {
|
||||
float new_cursor_pos_x = 0.0f;
|
||||
float new_cursor_pos_y = 0.0f;
|
||||
|
||||
// set control window pos
|
||||
if (props->control_window_pos_.y + props->control_window_height_ >
|
||||
stream_window_height_) {
|
||||
pos_y = stream_window_height_ - props->control_window_height_;
|
||||
} else if (props->control_window_pos_.y < y_boundary) {
|
||||
pos_y = y_boundary;
|
||||
float current_y_rel = props->control_window_pos_.y - container_pos.y;
|
||||
if (current_y_rel + props->control_window_height_ > container_h) {
|
||||
pos_y = container_h - props->control_window_height_;
|
||||
} else if (current_y_rel < 0.0f) {
|
||||
pos_y = 0.0f;
|
||||
} else {
|
||||
pos_y = props->control_window_pos_.y;
|
||||
pos_y = current_y_rel;
|
||||
}
|
||||
|
||||
if (props->is_control_bar_in_left_) {
|
||||
pos_x = 0;
|
||||
pos_x = 0.0f;
|
||||
} else {
|
||||
pos_x = stream_window_width_ - props->control_window_width_;
|
||||
pos_x = container_w - props->control_window_width_;
|
||||
}
|
||||
|
||||
ImGui::SetNextWindowPos(ImVec2(pos_x, pos_y), ImGuiCond_Always);
|
||||
|
||||
if (0 != props->mouse_diff_control_bar_pos_x_ &&
|
||||
0 != props->mouse_diff_control_bar_pos_y_) {
|
||||
// set cursor pos
|
||||
new_cursor_pos_x = pos_x + props->mouse_diff_control_bar_pos_x_;
|
||||
new_cursor_pos_y = pos_y + props->mouse_diff_control_bar_pos_y_;
|
||||
new_cursor_pos_x =
|
||||
container_pos.x + pos_x + props->mouse_diff_control_bar_pos_x_;
|
||||
new_cursor_pos_y =
|
||||
container_pos.y + pos_y + props->mouse_diff_control_bar_pos_y_;
|
||||
|
||||
SDL_WarpMouseInWindow(stream_window_, (int)new_cursor_pos_x,
|
||||
(int)new_cursor_pos_y);
|
||||
@@ -94,12 +118,14 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
} else if (!props->reset_control_bar_pos_ &&
|
||||
ImGui::IsMouseReleased(ImGuiMouseButton_Left) ||
|
||||
props->control_window_width_is_changing_) {
|
||||
if (props->control_window_pos_.x <= stream_window_width_ * 0.5f) {
|
||||
if (props->control_window_pos_.y + props->control_window_height_ >
|
||||
stream_window_height_) {
|
||||
pos_y = stream_window_height_ - props->control_window_height_;
|
||||
float current_x_rel = props->control_window_pos_.x - container_pos.x;
|
||||
float current_y_rel = props->control_window_pos_.y - container_pos.y;
|
||||
if (current_x_rel <= container_w * 0.5f) {
|
||||
pos_x = 0.0f;
|
||||
if (current_y_rel + props->control_window_height_ > container_h) {
|
||||
pos_y = container_h - props->control_window_height_;
|
||||
} else {
|
||||
pos_y = props->control_window_pos_.y;
|
||||
pos_y = current_y_rel;
|
||||
}
|
||||
|
||||
if (props->control_bar_expand_) {
|
||||
@@ -118,47 +144,53 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
}
|
||||
}
|
||||
props->is_control_bar_in_left_ = true;
|
||||
} else if (props->control_window_pos_.x > stream_window_width_ * 0.5f) {
|
||||
pos_x = 0;
|
||||
pos_y =
|
||||
(props->control_window_pos_.y >= y_boundary &&
|
||||
props->control_window_pos_.y <=
|
||||
stream_window_height_ - props->control_window_height_)
|
||||
? props->control_window_pos_.y
|
||||
: (props->control_window_pos_.y <
|
||||
(fullscreen_button_pressed_ ? 0 : title_bar_height_)
|
||||
? (fullscreen_button_pressed_ ? 0 : title_bar_height_)
|
||||
: (stream_window_height_ - props->control_window_height_));
|
||||
} else if (current_x_rel > container_w * 0.5f) {
|
||||
pos_x = container_w - props->control_window_width_;
|
||||
pos_y = (current_y_rel >= 0.0f &&
|
||||
current_y_rel <= container_h - props->control_window_height_)
|
||||
? current_y_rel
|
||||
: (current_y_rel < 0.0f
|
||||
? 0.0f
|
||||
: (container_h - props->control_window_height_));
|
||||
|
||||
if (props->control_bar_expand_) {
|
||||
if (props->control_window_width_ >= props->control_window_max_width_) {
|
||||
props->control_window_width_ = props->control_window_max_width_;
|
||||
props->control_window_width_is_changing_ = false;
|
||||
pos_x = stream_window_width_ - props->control_window_max_width_;
|
||||
pos_x = container_w - props->control_window_max_width_;
|
||||
} else {
|
||||
props->control_window_width_is_changing_ = true;
|
||||
pos_x = stream_window_width_ - props->control_window_width_;
|
||||
pos_x = container_w - props->control_window_width_;
|
||||
}
|
||||
} else {
|
||||
if (props->control_window_width_ <= props->control_window_min_width_) {
|
||||
props->control_window_width_ = props->control_window_min_width_;
|
||||
props->control_window_width_is_changing_ = false;
|
||||
pos_x = stream_window_width_ - props->control_window_min_width_;
|
||||
pos_x = container_w - props->control_window_min_width_;
|
||||
} else {
|
||||
props->control_window_width_is_changing_ = true;
|
||||
pos_x = stream_window_width_ - props->control_window_width_;
|
||||
pos_x = container_w - props->control_window_width_;
|
||||
}
|
||||
}
|
||||
props->is_control_bar_in_left_ = false;
|
||||
}
|
||||
|
||||
if (props->control_window_pos_.y + props->control_window_height_ >
|
||||
stream_window_height_) {
|
||||
pos_y = stream_window_height_ - props->control_window_height_;
|
||||
} else if (props->control_window_pos_.y < y_boundary) {
|
||||
pos_y = y_boundary;
|
||||
if (current_y_rel + props->control_window_height_ > container_h) {
|
||||
pos_y = container_h - props->control_window_height_;
|
||||
} else if (current_y_rel < 0.0f) {
|
||||
pos_y = 0.0f;
|
||||
}
|
||||
ImGui::SetNextWindowPos(ImVec2(pos_x, pos_y), ImGuiCond_Always);
|
||||
} else {
|
||||
float current_x_rel = props->control_window_pos_.x - container_pos.x;
|
||||
float current_y_rel = props->control_window_pos_.y - container_pos.y;
|
||||
pos_x = current_x_rel;
|
||||
pos_y = current_y_rel;
|
||||
if (pos_x < 0.0f) pos_x = 0.0f;
|
||||
if (pos_y < 0.0f) pos_y = 0.0f;
|
||||
if (pos_x + props->control_window_width_ > container_w)
|
||||
pos_x = container_w - props->control_window_width_;
|
||||
if (pos_y + props->control_window_height_ > container_h)
|
||||
pos_y = container_h - props->control_window_height_;
|
||||
}
|
||||
|
||||
if (props->control_bar_expand_ && props->control_window_height_is_changing_) {
|
||||
@@ -180,10 +212,20 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
}
|
||||
|
||||
std::string control_window_title = props->remote_id_ + "ControlWindow";
|
||||
ImGui::Begin(control_window_title.c_str(), nullptr,
|
||||
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoDocking);
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
static bool a, b, c, d, e;
|
||||
float child_cursor_x = pos_x;
|
||||
float child_cursor_y = pos_y;
|
||||
ImGui::SetCursorPos(ImVec2(child_cursor_x, child_cursor_y));
|
||||
|
||||
std::string control_child_window_title =
|
||||
props->remote_id_ + "ControlChildWindow";
|
||||
ImGui::BeginChild(
|
||||
control_child_window_title.c_str(),
|
||||
ImVec2(props->control_window_width_, props->control_window_height_),
|
||||
ImGuiChildFlags_Borders, ImGuiWindowFlags_NoDecoration);
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
props->control_window_pos_ = ImGui::GetWindowPos();
|
||||
SDL_GetMouseState(&props->mouse_pos_x_, &props->mouse_pos_y_);
|
||||
@@ -192,31 +234,26 @@ int Render::ControlWindow(std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
props->mouse_diff_control_bar_pos_y_ =
|
||||
props->mouse_pos_y_ - props->control_window_pos_.y;
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
static bool a, b, c, d, e;
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(props->is_control_bar_in_left_
|
||||
? props->control_window_pos_.x - props->control_window_width_
|
||||
: props->control_window_pos_.x,
|
||||
props->control_window_pos_.y),
|
||||
ImGuiCond_Always);
|
||||
|
||||
std::string control_child_window_title =
|
||||
props->remote_id_ + "ControlChildWindow";
|
||||
ImGui::BeginChild(control_child_window_title.c_str(),
|
||||
ImVec2(props->control_window_width_ * 2.0f,
|
||||
props->control_window_height_),
|
||||
ImGuiChildFlags_Border, ImGuiWindowFlags_NoDecoration);
|
||||
ImGui::PopStyleColor();
|
||||
if (props->control_window_pos_.y < container_pos.y ||
|
||||
props->control_window_pos_.y + props->control_window_height_ >
|
||||
(container_pos.y + container_h) ||
|
||||
props->control_window_pos_.x < container_pos.x ||
|
||||
props->control_window_pos_.x + props->control_window_width_ >
|
||||
(container_pos.x + container_w)) {
|
||||
ImGui::ClearActiveID();
|
||||
props->reset_control_bar_pos_ = true;
|
||||
props->mouse_diff_control_bar_pos_x_ = 0;
|
||||
props->mouse_diff_control_bar_pos_y_ = 0;
|
||||
}
|
||||
|
||||
ControlBar(props);
|
||||
props->control_bar_hovered_ = ImGui::IsWindowHovered();
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::End();
|
||||
ImGui::PopStyleVar(4);
|
||||
ImGui::PopStyleVar(3);
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -30,12 +30,15 @@ int BitrateDisplay(int bitrate) {
|
||||
|
||||
int Render::FileTransferWindow(
|
||||
std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
FileTransferState* state = props ? &props->file_transfer_ : &file_transfer_;
|
||||
state->file_transfer_window_hovered_ = false;
|
||||
|
||||
// Only show window if there are files in transfer list or currently
|
||||
// transferring
|
||||
std::vector<SubStreamWindowProperties::FileTransferInfo> file_list;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(props->file_transfer_list_mutex_);
|
||||
file_list = props->file_transfer_list_;
|
||||
std::lock_guard<std::mutex> lock(state->file_transfer_list_mutex_);
|
||||
file_list = state->file_transfer_list_;
|
||||
}
|
||||
|
||||
// Sort file list: Sending first, then Completed, then Queued, then Failed
|
||||
@@ -66,12 +69,10 @@ int Render::FileTransferWindow(
|
||||
// It will be reopened automatically when:
|
||||
// 1. A file transfer completes (in render_callback.cpp)
|
||||
// 2. A new file starts sending from queue (in render.cpp)
|
||||
if (!props->file_transfer_window_visible_) {
|
||||
if (!state->file_transfer_window_visible_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
// Position window at bottom-left of stream window
|
||||
// Adjust window size based on number of files
|
||||
float file_transfer_window_width = main_window_width_ * 0.6f;
|
||||
@@ -80,45 +81,58 @@ int Render::FileTransferWindow(
|
||||
float pos_x = file_transfer_window_width * 0.05f;
|
||||
float pos_y = stream_window_height_ - file_transfer_window_height -
|
||||
file_transfer_window_width * 0.05;
|
||||
float same_line_width = file_transfer_window_width * 0.1f;
|
||||
|
||||
const ImVec2 mouse_pos = ImGui::GetMousePos();
|
||||
const bool mouse_in_window_rect =
|
||||
mouse_pos.x >= pos_x &&
|
||||
mouse_pos.x <= pos_x + file_transfer_window_width &&
|
||||
mouse_pos.y >= pos_y &&
|
||||
mouse_pos.y <= pos_y + file_transfer_window_height;
|
||||
|
||||
ImGui::SetNextWindowPos(ImVec2(pos_x, pos_y), ImGuiCond_Always);
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(file_transfer_window_width, file_transfer_window_height),
|
||||
ImGuiCond_Always);
|
||||
if (mouse_in_window_rect) {
|
||||
ImGui::SetNextWindowFocus();
|
||||
}
|
||||
|
||||
// Set Chinese font for proper display
|
||||
if (stream_windows_system_chinese_font_) {
|
||||
const bool has_chinese_font = stream_windows_system_chinese_font_ != nullptr;
|
||||
if (has_chinese_font) {
|
||||
ImGui::PushFont(stream_windows_system_chinese_font_);
|
||||
}
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_ * 0.5f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 0.9f));
|
||||
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0.0f, 0.0f, 0.0f, 0.3f));
|
||||
ImGui::PushStyleColor(ImGuiCol_TitleBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_TitleBgActive, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
bool window_opened = true;
|
||||
if (ImGui::Begin(
|
||||
localization::file_transfer_progress[localization_language_index_]
|
||||
.c_str(),
|
||||
&window_opened,
|
||||
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
|
||||
ImGuiWindowFlags_NoScrollbar)) {
|
||||
const bool show_contents = ImGui::Begin(
|
||||
localization::file_transfer_progress[localization_language_index_]
|
||||
.c_str(),
|
||||
&window_opened,
|
||||
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
|
||||
ImGuiWindowFlags_NoScrollbar);
|
||||
ImGui::PopStyleColor(4);
|
||||
ImGui::PopStyleVar(2);
|
||||
|
||||
state->file_transfer_window_hovered_ =
|
||||
mouse_in_window_rect ||
|
||||
ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
|
||||
|
||||
if (!window_opened) {
|
||||
state->file_transfer_window_visible_ = false;
|
||||
}
|
||||
|
||||
if (show_contents && window_opened) {
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::PopStyleColor(4);
|
||||
ImGui::PopStyleVar(2);
|
||||
|
||||
// Close button handling
|
||||
if (!window_opened) {
|
||||
props->file_transfer_window_visible_ = false;
|
||||
ImGui::End();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Display file list
|
||||
if (file_list.empty()) {
|
||||
@@ -128,7 +142,7 @@ int Render::FileTransferWindow(
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::BeginChild(
|
||||
"FileList", ImVec2(0, file_transfer_window_height * 0.75f),
|
||||
ImGuiChildFlags_Border, ImGuiWindowFlags_HorizontalScrollbar);
|
||||
ImGuiChildFlags_Borders, ImGuiWindowFlags_HorizontalScrollbar);
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
|
||||
@@ -223,19 +237,15 @@ int Render::FileTransferWindow(
|
||||
}
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::End();
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
// Pop Chinese font if it was pushed
|
||||
if (stream_windows_system_chinese_font_) {
|
||||
ImGui::PopFont();
|
||||
}
|
||||
} else {
|
||||
ImGui::PopStyleColor(4);
|
||||
ImGui::PopStyleVar(2);
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
if (has_chinese_font) {
|
||||
ImGui::PopFont();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,21 @@
|
||||
#include "localization.h"
|
||||
#include "rd_log.h"
|
||||
#include "render.h"
|
||||
#include "tinyfiledialogs.h"
|
||||
|
||||
#if _WIN32 && CROSSDESK_PORTABLE
|
||||
#include "service_host.h"
|
||||
#endif
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
int Render::SettingWindow() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
float portable_y_padding = 0.0f;
|
||||
#if _WIN32 && CROSSDESK_PORTABLE
|
||||
portable_y_padding = 0.05f;
|
||||
#endif
|
||||
|
||||
if (show_settings_window_) {
|
||||
if (settings_window_pos_reset_) {
|
||||
const ImGuiViewport* viewport = ImGui::GetMainViewport();
|
||||
@@ -15,28 +25,32 @@ int Render::SettingWindow() {
|
||||
!defined(__arm__) && USE_CUDA) || \
|
||||
defined(__APPLE__))
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.343f, io.DisplaySize.y * 0.07f));
|
||||
ImVec2(io.DisplaySize.x * 0.343f, io.DisplaySize.y * 0.05f));
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.315f, io.DisplaySize.y * 0.85f));
|
||||
ImVec2(io.DisplaySize.x * 0.315f,
|
||||
io.DisplaySize.y * (0.9f + portable_y_padding)));
|
||||
#else
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.343f, io.DisplaySize.y * 0.1f));
|
||||
ImVec2(io.DisplaySize.x * 0.343f, io.DisplaySize.y * 0.08f));
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.315f, io.DisplaySize.y * 0.8f));
|
||||
ImVec2(io.DisplaySize.x * 0.315f,
|
||||
io.DisplaySize.y * (0.85f + portable_y_padding)));
|
||||
#endif
|
||||
} else {
|
||||
#if (((defined(_WIN32) || defined(__linux__)) && !defined(__aarch64__) && \
|
||||
!defined(__arm__) && USE_CUDA) || \
|
||||
defined(__APPLE__))
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.297f, io.DisplaySize.y * 0.07f));
|
||||
ImVec2(io.DisplaySize.x * 0.297f, io.DisplaySize.y * 0.05f));
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.407f, io.DisplaySize.y * 0.85f));
|
||||
ImVec2(io.DisplaySize.x * 0.42f,
|
||||
io.DisplaySize.y * (0.9f + portable_y_padding)));
|
||||
#else
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.297f, io.DisplaySize.y * 0.1f));
|
||||
ImVec2(io.DisplaySize.x * 0.297f, io.DisplaySize.y * 0.08f));
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.407f, io.DisplaySize.y * 0.8f));
|
||||
ImVec2(io.DisplaySize.x * 0.42f,
|
||||
io.DisplaySize.y * (0.85f + portable_y_padding)));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -49,8 +63,8 @@ int Render::SettingWindow() {
|
||||
int settings_items_offset = 0;
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
|
||||
ImGui::Begin(localization::settings[localization_language_index_].c_str(),
|
||||
nullptr,
|
||||
@@ -59,9 +73,9 @@ int Render::SettingWindow() {
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
|
||||
{
|
||||
const char* language_items[] = {
|
||||
localization::language_zh[localization_language_index_].c_str(),
|
||||
localization::language_en[localization_language_index_].c_str()};
|
||||
const auto& supported_languages = localization::GetSupportedLanguages();
|
||||
language_button_value_ =
|
||||
localization::detail::ClampLanguageIndex(language_button_value_);
|
||||
|
||||
settings_items_offset += settings_items_padding;
|
||||
ImGui::SetCursorPosY(settings_items_offset);
|
||||
@@ -72,17 +86,25 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 3.0f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.5f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.7f);
|
||||
}
|
||||
|
||||
ImGui::SetNextItemWidth(title_bar_button_width_ * 1.8f);
|
||||
if (ImGui::BeginCombo("##language",
|
||||
language_items[language_button_value_])) {
|
||||
localization::GetSupportedLanguages()
|
||||
[localization::detail::ClampLanguageIndex(
|
||||
language_button_value_)]
|
||||
.display_name.c_str())) {
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
for (int i = 0; i < IM_ARRAYSIZE(language_items); i++) {
|
||||
for (int i = 0; i < static_cast<int>(supported_languages.size());
|
||||
++i) {
|
||||
bool selected = (i == language_button_value_);
|
||||
if (ImGui::Selectable(language_items[i], selected))
|
||||
if (ImGui::Selectable(supported_languages[i].display_name.c_str(),
|
||||
selected))
|
||||
language_button_value_ = i;
|
||||
if (selected) {
|
||||
ImGui::SetItemDefaultFocus();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndCombo();
|
||||
@@ -114,7 +136,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 3.0f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.5f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.7f);
|
||||
}
|
||||
|
||||
ImGui::SetNextItemWidth(title_bar_button_width_ * 1.8f);
|
||||
@@ -147,7 +169,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 3.0f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.5f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.7f);
|
||||
}
|
||||
|
||||
ImGui::SetNextItemWidth(title_bar_button_width_ * 1.8f);
|
||||
@@ -183,7 +205,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 3.0f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.5f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.7f);
|
||||
}
|
||||
|
||||
ImGui::SetNextItemWidth(title_bar_button_width_ * 1.8f);
|
||||
@@ -217,7 +239,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.275f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.755f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.955f);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("##enable_hardware_video_codec",
|
||||
@@ -238,7 +260,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.275f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.755f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.955f);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("##enable_turn", &enable_turn_);
|
||||
@@ -257,7 +279,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.275f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.755f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.955f);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("##enable_srtp", &enable_srtp_);
|
||||
@@ -278,7 +300,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.275f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.755f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.955f);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("##enable_self_hosted", &enable_self_hosted_);
|
||||
@@ -297,7 +319,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.275f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.755f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.955f);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("##enable_autostart_", &enable_autostart_);
|
||||
@@ -316,7 +338,7 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.275f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.755f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.955f);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("##enable_daemon_", &enable_daemon_);
|
||||
@@ -330,10 +352,14 @@ int Render::SettingWindow() {
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
}
|
||||
#if _WIN32
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
{
|
||||
#ifndef _WIN32
|
||||
ImGui::BeginDisabled();
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
|
||||
#endif
|
||||
settings_items_offset += settings_items_padding;
|
||||
ImGui::SetCursorPosY(settings_items_offset);
|
||||
ImGui::AlignTextToFramePadding();
|
||||
@@ -344,17 +370,150 @@ int Render::SettingWindow() {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.275f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.755f);
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.955f);
|
||||
}
|
||||
|
||||
ImGui::Checkbox("##enable_minimize_to_tray_",
|
||||
&enable_minimize_to_tray_);
|
||||
}
|
||||
#ifndef _WIN32
|
||||
ImGui::PopStyleColor();
|
||||
ImGui::EndDisabled();
|
||||
#endif
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
{
|
||||
settings_items_offset += settings_items_padding;
|
||||
ImGui::SetCursorPosY(settings_items_offset);
|
||||
ImGui::AlignTextToFramePadding();
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::file_transfer_save_path[localization_language_index_]
|
||||
.c_str());
|
||||
ImGui::SameLine();
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 2.82f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.5f);
|
||||
}
|
||||
|
||||
std::string display_path =
|
||||
strlen(file_transfer_save_path_buf_) > 0
|
||||
? file_transfer_save_path_buf_
|
||||
: localization::default_desktop[localization_language_index_];
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered,
|
||||
ImVec4(0.95f, 0.95f, 0.95f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonActive,
|
||||
ImVec4(0.9f, 0.9f, 0.9f, 1.0f));
|
||||
ImGui::PushFont(main_windows_system_chinese_font_);
|
||||
if (ImGui::Button(display_path.c_str(),
|
||||
ImVec2(title_bar_button_width_ * 2.0f, 0))) {
|
||||
const char* folder =
|
||||
tinyfd_selectFolderDialog(localization::file_transfer_save_path
|
||||
[localization_language_index_]
|
||||
.c_str(),
|
||||
strlen(file_transfer_save_path_buf_) > 0
|
||||
? file_transfer_save_path_buf_
|
||||
: nullptr);
|
||||
if (folder) {
|
||||
strncpy(file_transfer_save_path_buf_, folder,
|
||||
sizeof(file_transfer_save_path_buf_) - 1);
|
||||
file_transfer_save_path_buf_[sizeof(file_transfer_save_path_buf_) -
|
||||
1] = '\0';
|
||||
}
|
||||
}
|
||||
if (ImGui::IsItemHovered() &&
|
||||
strlen(file_transfer_save_path_buf_) > 0) {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::Text("%s", file_transfer_save_path_buf_);
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
ImGui::PopFont();
|
||||
ImGui::PopStyleColor(3);
|
||||
}
|
||||
|
||||
if (stream_window_inited_) {
|
||||
ImGui::EndDisabled();
|
||||
}
|
||||
|
||||
#if _WIN32 && CROSSDESK_PORTABLE
|
||||
ImGui::Separator();
|
||||
|
||||
{
|
||||
settings_items_offset += settings_items_padding;
|
||||
ImGui::SetCursorPosY(settings_items_offset);
|
||||
ImGui::AlignTextToFramePadding();
|
||||
ImGui::Text("%s", localization::windows_service_settings_label
|
||||
[localization_language_index_]
|
||||
.c_str());
|
||||
ImGui::SameLine();
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.0f);
|
||||
} else if (ConfigCenter::LANGUAGE::ENGLISH == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.42f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.6f);
|
||||
}
|
||||
|
||||
const PortableServiceInstallState state =
|
||||
portable_service_install_state_.load(std::memory_order_acquire);
|
||||
const bool service_installed =
|
||||
IsCrossDeskServiceInstalled() ||
|
||||
state == PortableServiceInstallState::succeeded;
|
||||
if (service_installed) {
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 3.9f);
|
||||
} else if (ConfigCenter::LANGUAGE::ENGLISH ==
|
||||
localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 5.32f);
|
||||
} else {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 4.6f);
|
||||
}
|
||||
ImGui::Text("%s", localization::windows_service_installed
|
||||
[localization_language_index_]
|
||||
.c_str());
|
||||
} else {
|
||||
if (state == PortableServiceInstallState::installing) {
|
||||
ImGui::BeginDisabled();
|
||||
}
|
||||
if (ImGui::Button(localization::install_windows_service
|
||||
[localization_language_index_]
|
||||
.c_str())) {
|
||||
StartPortableWindowsServiceInstall();
|
||||
}
|
||||
|
||||
if (state == PortableServiceInstallState::installing) {
|
||||
ImGui::EndDisabled();
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("%s", localization::installing_windows_service
|
||||
[localization_language_index_]
|
||||
.c_str());
|
||||
} else if (state == PortableServiceInstallState::failed) {
|
||||
ImGui::SameLine();
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::failed[localization_language_index_].c_str());
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::PushTextWrapPos(title_bar_button_width_ * 10.0f);
|
||||
ImGui::TextWrapped("%s",
|
||||
localization::windows_service_install_failed
|
||||
[localization_language_index_]
|
||||
.c_str());
|
||||
ImGui::PopTextWrapPos();
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
ImGui::SetCursorPosX(title_bar_button_width_ * 1.59f);
|
||||
} else {
|
||||
@@ -362,7 +521,7 @@ int Render::SettingWindow() {
|
||||
}
|
||||
|
||||
settings_items_offset +=
|
||||
settings_items_padding + title_bar_button_width_ * 0.3f;
|
||||
settings_items_padding + title_bar_button_width_ * 0.15f;
|
||||
ImGui::SetCursorPosY(settings_items_offset);
|
||||
|
||||
ImGui::PopStyleVar();
|
||||
@@ -374,16 +533,23 @@ int Render::SettingWindow() {
|
||||
show_self_hosted_server_config_window_ = false;
|
||||
|
||||
// Language
|
||||
language_button_value_ =
|
||||
localization::detail::ClampLanguageIndex(language_button_value_);
|
||||
if (language_button_value_ == 0) {
|
||||
config_center_->SetLanguage(ConfigCenter::LANGUAGE::CHINESE);
|
||||
localization_language_ = ConfigCenter::LANGUAGE::CHINESE;
|
||||
} else if (language_button_value_ == 1) {
|
||||
localization_language_ = ConfigCenter::LANGUAGE::ENGLISH;
|
||||
} else {
|
||||
config_center_->SetLanguage(ConfigCenter::LANGUAGE::ENGLISH);
|
||||
localization_language_ = ConfigCenter::LANGUAGE::RUSSIAN;
|
||||
}
|
||||
config_center_->SetLanguage(localization_language_);
|
||||
language_button_value_last_ = language_button_value_;
|
||||
localization_language_ = (ConfigCenter::LANGUAGE)language_button_value_;
|
||||
localization_language_index_ = language_button_value_;
|
||||
LOG_INFO("Set localization language: {}",
|
||||
localization_language_index_ == 0 ? "zh" : "en");
|
||||
localization::GetSupportedLanguages()
|
||||
[localization::detail::ClampLanguageIndex(
|
||||
localization_language_index_)]
|
||||
.code.c_str());
|
||||
|
||||
// Video quality
|
||||
if (video_quality_button_value_ == 0) {
|
||||
@@ -469,6 +635,10 @@ int Render::SettingWindow() {
|
||||
enable_minimize_to_tray_last_ = enable_minimize_to_tray_;
|
||||
#endif
|
||||
|
||||
// File transfer save path
|
||||
config_center_->SetFileTransferSavePath(file_transfer_save_path_buf_);
|
||||
file_transfer_save_path_last_ = file_transfer_save_path_buf_;
|
||||
|
||||
settings_window_pos_reset_ = true;
|
||||
|
||||
// Recreate peer instance
|
||||
@@ -516,6 +686,13 @@ int Render::SettingWindow() {
|
||||
enable_turn_ = enable_turn_last_;
|
||||
}
|
||||
|
||||
// Restore file transfer save path
|
||||
strncpy(file_transfer_save_path_buf_,
|
||||
file_transfer_save_path_last_.c_str(),
|
||||
sizeof(file_transfer_save_path_buf_) - 1);
|
||||
file_transfer_save_path_buf_[sizeof(file_transfer_save_path_buf_) - 1] =
|
||||
'\0';
|
||||
|
||||
settings_window_pos_reset_ = true;
|
||||
}
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
@@ -527,4 +704,4 @@ int Render::SettingWindow() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -14,15 +14,16 @@ int Render::MainWindow() {
|
||||
ImGui::SetNextWindowPos(ImVec2(0.0f, io.DisplaySize.y * (TITLE_BAR_HEIGHT)),
|
||||
ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 0.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1.0f, 1.0f, 1.0f, 0.0f));
|
||||
ImGui::BeginChild(
|
||||
"DeskWindow",
|
||||
ImVec2(local_remote_window_width, local_remote_window_height),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::PopStyleColor();
|
||||
ImGui::PopStyleColor(2);
|
||||
|
||||
LocalWindow();
|
||||
|
||||
@@ -56,4 +57,4 @@ int Render::MainWindow() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -0,0 +1,360 @@
|
||||
#include "render.h"
|
||||
|
||||
#if _WIN32 && CROSSDESK_PORTABLE
|
||||
|
||||
#include <shellapi.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "localization.h"
|
||||
#include "rd_log.h"
|
||||
#include "service_host.h"
|
||||
|
||||
namespace crossdesk {
|
||||
namespace {
|
||||
|
||||
std::filesystem::path GetCurrentExecutablePath() {
|
||||
std::vector<wchar_t> buffer(MAX_PATH);
|
||||
while (true) {
|
||||
DWORD length = GetModuleFileNameW(nullptr, buffer.data(),
|
||||
static_cast<DWORD>(buffer.size()));
|
||||
if (length == 0) {
|
||||
return {};
|
||||
}
|
||||
if (length < buffer.size()) {
|
||||
return std::filesystem::path(buffer.data(), buffer.data() + length);
|
||||
}
|
||||
if (buffer.size() >= 32768) {
|
||||
return {};
|
||||
}
|
||||
buffer.resize(buffer.size() * 2);
|
||||
}
|
||||
}
|
||||
|
||||
bool InstallServiceWithElevation() {
|
||||
const std::filesystem::path executable_path = GetCurrentExecutablePath();
|
||||
if (executable_path.empty()) {
|
||||
LOG_ERROR("Portable service install failed: current executable not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::filesystem::path service_path =
|
||||
executable_path.parent_path() / L"crossdesk_service.exe";
|
||||
const std::filesystem::path helper_path =
|
||||
executable_path.parent_path() / L"crossdesk_session_helper.exe";
|
||||
if (!std::filesystem::exists(service_path) ||
|
||||
!std::filesystem::exists(helper_path)) {
|
||||
LOG_ERROR(
|
||||
"Portable service install failed: service binaries missing, "
|
||||
"service={}, "
|
||||
"helper={}",
|
||||
service_path.string(), helper_path.string());
|
||||
return false;
|
||||
}
|
||||
|
||||
std::wstring executable = executable_path.wstring();
|
||||
std::wstring working_dir = executable_path.parent_path().wstring();
|
||||
std::wstring parameters = L"--service-install";
|
||||
|
||||
SHELLEXECUTEINFOW execute_info{};
|
||||
execute_info.cbSize = sizeof(execute_info);
|
||||
execute_info.fMask = SEE_MASK_NOCLOSEPROCESS;
|
||||
execute_info.hwnd = nullptr;
|
||||
execute_info.lpVerb = L"runas";
|
||||
execute_info.lpFile = executable.c_str();
|
||||
execute_info.lpParameters = parameters.c_str();
|
||||
execute_info.lpDirectory = working_dir.c_str();
|
||||
execute_info.nShow = SW_HIDE;
|
||||
|
||||
if (!ShellExecuteExW(&execute_info)) {
|
||||
LOG_ERROR("Portable service install failed: ShellExecuteExW error={}",
|
||||
GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD wait_result = WaitForSingleObject(execute_info.hProcess, INFINITE);
|
||||
DWORD exit_code = 1;
|
||||
if (wait_result == WAIT_OBJECT_0) {
|
||||
GetExitCodeProcess(execute_info.hProcess, &exit_code);
|
||||
} else {
|
||||
LOG_ERROR("Portable service install wait failed, result={}", wait_result);
|
||||
}
|
||||
CloseHandle(execute_info.hProcess);
|
||||
|
||||
if (exit_code != 0) {
|
||||
LOG_ERROR("Portable service install command failed, exit_code={}",
|
||||
exit_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool started = StartCrossDeskService();
|
||||
if (!started) {
|
||||
LOG_WARN("Portable service installed but start failed");
|
||||
}
|
||||
return IsCrossDeskServiceInstalled() && started;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Render::CheckPortableWindowsService() {
|
||||
if (portable_service_prompt_checked_) {
|
||||
return;
|
||||
}
|
||||
portable_service_prompt_checked_ = true;
|
||||
|
||||
if (IsCrossDeskServiceInstalled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (portable_service_prompt_suppressed_) {
|
||||
return;
|
||||
}
|
||||
|
||||
portable_service_install_state_.store(PortableServiceInstallState::idle,
|
||||
std::memory_order_relaxed);
|
||||
show_portable_service_install_window_ = true;
|
||||
}
|
||||
|
||||
void Render::StartPortableWindowsServiceInstall() {
|
||||
portable_service_do_not_remind_ = false;
|
||||
PortableServiceInstallState expected = PortableServiceInstallState::idle;
|
||||
if (!portable_service_install_state_.compare_exchange_strong(
|
||||
expected, PortableServiceInstallState::installing,
|
||||
std::memory_order_acq_rel)) {
|
||||
if (expected != PortableServiceInstallState::failed) {
|
||||
return;
|
||||
}
|
||||
portable_service_install_state_.store(
|
||||
PortableServiceInstallState::installing, std::memory_order_release);
|
||||
}
|
||||
|
||||
JoinPortableWindowsServiceInstallThread();
|
||||
portable_service_install_thread_ = std::thread([this]() {
|
||||
const bool installed = InstallServiceWithElevation();
|
||||
portable_service_install_state_.store(
|
||||
installed ? PortableServiceInstallState::succeeded
|
||||
: PortableServiceInstallState::failed,
|
||||
std::memory_order_release);
|
||||
});
|
||||
}
|
||||
|
||||
void Render::JoinPortableWindowsServiceInstallThread() {
|
||||
if (portable_service_install_thread_.joinable()) {
|
||||
portable_service_install_thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
int Render::PortableServiceInstallWindow() {
|
||||
if (!show_portable_service_install_window_ &&
|
||||
!show_portable_service_prompt_suppressed_window_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const ImGuiViewport* viewport = ImGui::GetMainViewport();
|
||||
const float window_width =
|
||||
(std::min)(viewport->WorkSize.x * 0.6f, title_bar_button_width_ * 18.0f);
|
||||
const float window_height =
|
||||
(std::min)(viewport->WorkSize.y * 0.5f, title_bar_button_width_ * 8.0f);
|
||||
|
||||
if (show_portable_service_prompt_suppressed_window_) {
|
||||
const float notice_width = window_width;
|
||||
const float notice_height = (std::min)(viewport->WorkSize.y * 0.35f,
|
||||
title_bar_button_width_ * 4.6f);
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(
|
||||
viewport->WorkPos.x + (viewport->WorkSize.x - notice_width) / 2.0f,
|
||||
viewport->WorkPos.y +
|
||||
(viewport->WorkSize.y - notice_height) / 2.0f),
|
||||
ImGuiCond_Appearing);
|
||||
ImGui::SetNextWindowSize(ImVec2(notice_width, notice_height),
|
||||
ImGuiCond_Always);
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
|
||||
ImGui::Begin(
|
||||
localization::notification[localization_language_index_].c_str(),
|
||||
nullptr,
|
||||
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
|
||||
ImGuiWindowFlags_NoTitleBar);
|
||||
|
||||
ImGui::Spacing();
|
||||
ImGui::SetWindowFontScale(0.55f);
|
||||
ImGui::SetCursorPosX(notice_width * 0.08f);
|
||||
ImGui::Text(
|
||||
"%s", localization::notification[localization_language_index_].c_str());
|
||||
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::SetCursorPosX(notice_width * 0.06f);
|
||||
ImGui::SetCursorPosY(notice_height * 0.28f);
|
||||
ImGui::PushTextWrapPos(notice_width * 0.88f);
|
||||
ImGui::TextWrapped("%s",
|
||||
localization::windows_service_prompt_suppressed_message
|
||||
[localization_language_index_]
|
||||
.c_str());
|
||||
ImGui::PopTextWrapPos();
|
||||
|
||||
const std::string ok_label = localization::ok[localization_language_index_];
|
||||
const ImGuiStyle& style = ImGui::GetStyle();
|
||||
const float ok_width =
|
||||
ImGui::CalcTextSize(ok_label.c_str()).x + style.FramePadding.x * 2.0f;
|
||||
ImGui::SetCursorPosX((notice_width - ok_width) * 0.5f);
|
||||
ImGui::SetCursorPosY(notice_height * 0.75f);
|
||||
if (ImGui::Button(ok_label.c_str())) {
|
||||
show_portable_service_prompt_suppressed_window_ = false;
|
||||
}
|
||||
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::End();
|
||||
ImGui::PopStyleVar(3);
|
||||
ImGui::PopStyleColor();
|
||||
}
|
||||
|
||||
if (!show_portable_service_install_window_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(
|
||||
viewport->WorkPos.x + (viewport->WorkSize.x - window_width) / 2.0f,
|
||||
viewport->WorkPos.y + (viewport->WorkSize.y - window_height) / 2.0f),
|
||||
ImGuiCond_Appearing);
|
||||
ImGui::SetNextWindowSize(ImVec2(window_width, window_height),
|
||||
ImGuiCond_Always);
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
|
||||
ImGui::Begin(
|
||||
localization::windows_service_setup_title[localization_language_index_]
|
||||
.c_str(),
|
||||
nullptr,
|
||||
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
|
||||
ImGuiWindowFlags_NoTitleBar);
|
||||
|
||||
ImGui::Spacing();
|
||||
ImGui::SetWindowFontScale(0.55f);
|
||||
ImGui::SetCursorPosX(window_width * 0.08f);
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::windows_service_setup_title[localization_language_index_]
|
||||
.c_str());
|
||||
|
||||
const PortableServiceInstallState state =
|
||||
portable_service_install_state_.load(std::memory_order_acquire);
|
||||
const char* status_text = nullptr;
|
||||
if (state == PortableServiceInstallState::installing ||
|
||||
state == PortableServiceInstallState::succeeded ||
|
||||
state == PortableServiceInstallState::failed) {
|
||||
status_text =
|
||||
localization::installing_windows_service[localization_language_index_]
|
||||
.c_str();
|
||||
if (state == PortableServiceInstallState::succeeded) {
|
||||
status_text = localization::windows_service_install_success
|
||||
[localization_language_index_]
|
||||
.c_str();
|
||||
} else if (state == PortableServiceInstallState::failed) {
|
||||
status_text = localization::windows_service_install_failed
|
||||
[localization_language_index_]
|
||||
.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SetWindowFontScale(0.45f);
|
||||
ImGui::SetCursorPosX(window_width * 0.04f);
|
||||
ImGui::SetCursorPosY(window_height * 0.22f);
|
||||
ImGui::BeginChild("PortableServiceInstallContent",
|
||||
ImVec2(window_width * 0.92f, window_height * 0.45f),
|
||||
ImGuiChildFlags_Borders, ImGuiWindowFlags_None);
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
const float wrap_pos = ImGui::GetContentRegionAvail().x;
|
||||
ImGui::PushTextWrapPos(wrap_pos);
|
||||
ImGui::TextWrapped(
|
||||
"%s",
|
||||
localization::windows_service_setup_message[localization_language_index_]
|
||||
.c_str());
|
||||
if (status_text != nullptr) {
|
||||
ImGui::Spacing();
|
||||
ImGui::TextWrapped("%s", status_text);
|
||||
}
|
||||
ImGui::PopTextWrapPos();
|
||||
ImGui::EndChild();
|
||||
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::SetCursorPosX(window_width * 0.08f);
|
||||
ImGui::SetCursorPosY(window_height * 0.71f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
|
||||
ImGui::Checkbox(
|
||||
localization::do_not_remind_again[localization_language_index_].c_str(),
|
||||
&portable_service_do_not_remind_);
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
const float button_y = window_height * 0.84f;
|
||||
const ImGuiStyle& style = ImGui::GetStyle();
|
||||
const auto default_button_width = [&style](const std::string& label) {
|
||||
return ImGui::CalcTextSize(label.c_str()).x + style.FramePadding.x * 2.0f;
|
||||
};
|
||||
const std::string install_label =
|
||||
localization::install_windows_service[localization_language_index_];
|
||||
const std::string cancel_label =
|
||||
localization::cancel[localization_language_index_];
|
||||
const std::string ok_label = localization::ok[localization_language_index_];
|
||||
const float buttons_width = state == PortableServiceInstallState::succeeded
|
||||
? default_button_width(ok_label)
|
||||
: default_button_width(install_label) +
|
||||
style.ItemSpacing.x +
|
||||
default_button_width(cancel_label);
|
||||
ImGui::SetCursorPosX((window_width - buttons_width) * 0.5f);
|
||||
ImGui::SetCursorPosY(button_y);
|
||||
|
||||
if (state == PortableServiceInstallState::succeeded) {
|
||||
if (ImGui::Button(ok_label.c_str())) {
|
||||
show_portable_service_install_window_ = false;
|
||||
JoinPortableWindowsServiceInstallThread();
|
||||
}
|
||||
} else {
|
||||
if (state == PortableServiceInstallState::installing) {
|
||||
ImGui::BeginDisabled();
|
||||
}
|
||||
if (ImGui::Button(install_label.c_str())) {
|
||||
StartPortableWindowsServiceInstall();
|
||||
}
|
||||
if (state == PortableServiceInstallState::installing) {
|
||||
ImGui::EndDisabled();
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
if (state == PortableServiceInstallState::installing) {
|
||||
ImGui::BeginDisabled();
|
||||
}
|
||||
if (ImGui::Button(cancel_label.c_str())) {
|
||||
if (portable_service_do_not_remind_) {
|
||||
portable_service_prompt_suppressed_ = true;
|
||||
config_center_->SetPortableServicePromptSuppressed(true);
|
||||
show_portable_service_prompt_suppressed_window_ = true;
|
||||
}
|
||||
show_portable_service_install_window_ = false;
|
||||
}
|
||||
if (state == PortableServiceInstallState::installing) {
|
||||
ImGui::EndDisabled();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::End();
|
||||
ImGui::PopStyleVar(3);
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -6,11 +6,27 @@
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
#include <CoreGraphics/CoreGraphics.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#include <unistd.h>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
constexpr uint32_t kPermissionRefreshIntervalVisibleMs = 500;
|
||||
|
||||
void OpenPrivacyPreferences(const char* pane) {
|
||||
if (pane == nullptr || pane[0] == '\0') {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string command =
|
||||
"open \"x-apple.systempreferences:com.apple.preference.security?";
|
||||
command += pane;
|
||||
command += "\"";
|
||||
system(command.c_str());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool Render::DrawToggleSwitch(const char* id, bool active, bool enabled) {
|
||||
const float TRACK_HEIGHT = ImGui::GetFrameHeight();
|
||||
const float TRACK_WIDTH = TRACK_HEIGHT * 1.8f;
|
||||
@@ -35,16 +51,19 @@ bool Render::DrawToggleSwitch(const char* id, bool active, bool enabled) {
|
||||
bool hovered = ImGui::IsItemHovered();
|
||||
bool clicked = ImGui::IsItemClicked() && enabled;
|
||||
|
||||
ImVec4 track_color = active ? (hovered && enabled ? COLOR_ACTIVE_HOVER : COLOR_ACTIVE)
|
||||
: (hovered && enabled ? COLOR_INACTIVE_HOVER : COLOR_INACTIVE);
|
||||
ImVec4 track_color =
|
||||
active ? (hovered && enabled ? COLOR_ACTIVE_HOVER : COLOR_ACTIVE)
|
||||
: (hovered && enabled ? COLOR_INACTIVE_HOVER : COLOR_INACTIVE);
|
||||
|
||||
if (!enabled) {
|
||||
track_color.w *= DISABLED_ALPHA;
|
||||
}
|
||||
|
||||
ImVec2 track_min = ImVec2(track_pos.x, track_pos.y + 0.5f);
|
||||
ImVec2 track_max = ImVec2(track_pos.x + TRACK_WIDTH, track_pos.y + TRACK_HEIGHT - 0.5f);
|
||||
draw_list->AddRectFilled(track_min, track_max, ImGui::GetColorU32(track_color), TRACK_RADIUS);
|
||||
ImVec2 track_max = ImVec2(track_pos.x + TRACK_WIDTH,
|
||||
track_pos.y + TRACK_HEIGHT - 0.5f);
|
||||
draw_list->AddRectFilled(track_min, track_max,
|
||||
ImGui::GetColorU32(track_color), TRACK_RADIUS);
|
||||
|
||||
float knob_position = active ? 1.0f : 0.0f;
|
||||
float knob_min_x = track_pos.x + KNOB_PADDING;
|
||||
@@ -59,7 +78,8 @@ bool Render::DrawToggleSwitch(const char* id, bool active, bool enabled) {
|
||||
|
||||
ImVec2 knob_min = ImVec2(knob_x, knob_y);
|
||||
ImVec2 knob_max = ImVec2(knob_x + KNOB_WIDTH, knob_y + KNOB_HEIGHT);
|
||||
draw_list->AddRectFilled(knob_min, knob_max, ImGui::GetColorU32(knob_color), KNOB_RADIUS);
|
||||
draw_list->AddRectFilled(knob_min, knob_max,
|
||||
ImGui::GetColorU32(knob_color), KNOB_RADIUS);
|
||||
|
||||
return clicked;
|
||||
}
|
||||
@@ -81,29 +101,82 @@ bool Render::CheckAccessibilityPermission() {
|
||||
}
|
||||
|
||||
void Render::OpenAccessibilityPreferences() {
|
||||
NSDictionary* options = @{(__bridge id)kAXTrustedCheckOptionPrompt : @YES};
|
||||
AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
|
||||
|
||||
system("open "
|
||||
"\"x-apple.systempreferences:com.apple.preference.security?Privacy_"
|
||||
"Accessibility\"");
|
||||
if (!mac_accessibility_permission_requested_) {
|
||||
NSDictionary* options = @{(__bridge id)kAXTrustedCheckOptionPrompt : @YES};
|
||||
AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
|
||||
} else {
|
||||
OpenPrivacyPreferences("Privacy_Accessibility");
|
||||
}
|
||||
}
|
||||
|
||||
void Render::OpenScreenRecordingPreferences() {
|
||||
if (@available(macOS 10.15, *)) {
|
||||
CGRequestScreenCaptureAccess();
|
||||
if (!mac_screen_recording_permission_requested_) {
|
||||
CGRequestScreenCaptureAccess();
|
||||
} else {
|
||||
OpenPrivacyPreferences("Privacy_ScreenCapture");
|
||||
}
|
||||
} else {
|
||||
OpenPrivacyPreferences("Privacy_ScreenCapture");
|
||||
}
|
||||
}
|
||||
|
||||
void Render::RefreshMacPermissionStatus(bool force) {
|
||||
const uint32_t now = static_cast<uint32_t>(SDL_GetTicks());
|
||||
if (!force && mac_permission_status_initialized_ &&
|
||||
now - mac_permission_last_check_tick_ <
|
||||
kPermissionRefreshIntervalVisibleMs) {
|
||||
return;
|
||||
}
|
||||
|
||||
system("open "
|
||||
"\"x-apple.systempreferences:com.apple.preference.security?Privacy_"
|
||||
"ScreenCapture\"");
|
||||
const bool old_screen_recording_granted =
|
||||
mac_screen_recording_permission_granted_;
|
||||
const bool old_accessibility_granted = mac_accessibility_permission_granted_;
|
||||
|
||||
mac_screen_recording_permission_granted_ =
|
||||
CheckScreenRecordingPermission();
|
||||
mac_accessibility_permission_granted_ = CheckAccessibilityPermission();
|
||||
mac_permission_last_check_tick_ = now;
|
||||
mac_permission_status_initialized_ = true;
|
||||
|
||||
if (old_screen_recording_granted !=
|
||||
mac_screen_recording_permission_granted_ ||
|
||||
old_accessibility_granted != mac_accessibility_permission_granted_) {
|
||||
LOG_INFO("macOS permission status: screen_recording={}, accessibility={}",
|
||||
mac_screen_recording_permission_granted_,
|
||||
mac_accessibility_permission_granted_);
|
||||
}
|
||||
}
|
||||
|
||||
bool Render::EnsureMacScreenRecordingPermission() {
|
||||
RefreshMacPermissionStatus(false);
|
||||
if (mac_screen_recording_permission_granted_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
show_request_permission_window_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Render::EnsureMacAccessibilityPermission() {
|
||||
RefreshMacPermissionStatus(false);
|
||||
if (mac_accessibility_permission_granted_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
show_request_permission_window_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
int Render::RequestPermissionWindow() {
|
||||
bool screen_recording_granted = CheckScreenRecordingPermission();
|
||||
bool accessibility_granted = CheckAccessibilityPermission();
|
||||
RefreshMacPermissionStatus(false);
|
||||
|
||||
show_request_permission_window_ = !screen_recording_granted || !accessibility_granted;
|
||||
const bool screen_recording_granted =
|
||||
mac_screen_recording_permission_granted_;
|
||||
const bool accessibility_granted = mac_accessibility_permission_granted_;
|
||||
|
||||
show_request_permission_window_ =
|
||||
!screen_recording_granted || !accessibility_granted;
|
||||
|
||||
if (!show_request_permission_window_) {
|
||||
return 0;
|
||||
@@ -127,8 +200,8 @@ int Render::RequestPermissionWindow() {
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
||||
|
||||
ImGui::Begin(
|
||||
@@ -162,8 +235,10 @@ int Render::RequestPermissionWindow() {
|
||||
if (accessibility_granted) {
|
||||
DrawToggleSwitch("accessibility_toggle_on", true, false);
|
||||
} else {
|
||||
if (DrawToggleSwitch("accessibility_toggle", accessibility_granted, !accessibility_granted)) {
|
||||
if (DrawToggleSwitch("accessibility_toggle", false, true)) {
|
||||
OpenAccessibilityPreferences();
|
||||
mac_accessibility_permission_requested_ = true;
|
||||
RefreshMacPermissionStatus(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,12 +253,12 @@ int Render::RequestPermissionWindow() {
|
||||
ImGui::AlignTextToFramePadding();
|
||||
ImGui::SetCursorPosX(checkbox_padding);
|
||||
if (screen_recording_granted) {
|
||||
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10.0f);
|
||||
DrawToggleSwitch("screen_recording_toggle_on", true, false);
|
||||
} else {
|
||||
if (DrawToggleSwitch("screen_recording_toggle", screen_recording_granted,
|
||||
!screen_recording_granted)) {
|
||||
if (DrawToggleSwitch("screen_recording_toggle", false, true)) {
|
||||
OpenScreenRecordingPreferences();
|
||||
mac_screen_recording_permission_requested_ = true;
|
||||
RefreshMacPermissionStatus(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,4 +277,4 @@ int Render::RequestPermissionWindow() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -28,98 +28,6 @@ std::vector<std::string> GetRootEntries() {
|
||||
return roots;
|
||||
}
|
||||
|
||||
int Render::ShowSimpleFileBrowser() {
|
||||
std::string display_text;
|
||||
|
||||
if (selected_current_file_path_.empty()) {
|
||||
selected_current_file_path_ = std::filesystem::current_path().string();
|
||||
}
|
||||
|
||||
if (!tls_cert_path_self_.empty()) {
|
||||
display_text =
|
||||
std::filesystem::path(tls_cert_path_self_).filename().string();
|
||||
} else if (selected_current_file_path_ != "Root") {
|
||||
display_text =
|
||||
std::filesystem::path(selected_current_file_path_).filename().string();
|
||||
if (display_text.empty()) {
|
||||
display_text = selected_current_file_path_;
|
||||
}
|
||||
}
|
||||
|
||||
if (display_text.empty()) {
|
||||
display_text =
|
||||
localization::select_a_file[localization_language_index_].c_str();
|
||||
}
|
||||
|
||||
if (show_file_browser_) {
|
||||
ImGui::PushItemFlag(ImGuiItemFlags_AutoClosePopups, false);
|
||||
|
||||
float fixed_width = title_bar_button_width_ * 3.8f;
|
||||
ImGui::SetNextItemWidth(fixed_width);
|
||||
ImGui::SetNextWindowSizeConstraints(ImVec2(fixed_width, 0),
|
||||
ImVec2(fixed_width, 100.0f));
|
||||
|
||||
if (ImGui::BeginCombo("##select_a_file", display_text.c_str(), 0)) {
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
bool file_selected = false;
|
||||
|
||||
auto roots = GetRootEntries();
|
||||
if (selected_current_file_path_ == "Root" ||
|
||||
!std::filesystem::exists(selected_current_file_path_) ||
|
||||
!std::filesystem::is_directory(selected_current_file_path_)) {
|
||||
for (const auto& root : roots) {
|
||||
if (ImGui::Selectable(root.c_str())) {
|
||||
selected_current_file_path_ = root;
|
||||
tls_cert_path_self_.clear();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
std::filesystem::path p(selected_current_file_path_);
|
||||
|
||||
if (ImGui::Selectable("..")) {
|
||||
if (std::find(roots.begin(), roots.end(),
|
||||
selected_current_file_path_) != roots.end()) {
|
||||
selected_current_file_path_ = "Root";
|
||||
} else if (p.has_parent_path()) {
|
||||
selected_current_file_path_ = p.parent_path().string();
|
||||
} else {
|
||||
selected_current_file_path_ = "Root";
|
||||
}
|
||||
tls_cert_path_self_.clear();
|
||||
}
|
||||
|
||||
try {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(
|
||||
selected_current_file_path_)) {
|
||||
std::string name = entry.path().filename().string();
|
||||
if (entry.is_directory()) {
|
||||
if (ImGui::Selectable(name.c_str())) {
|
||||
selected_current_file_path_ = entry.path().string();
|
||||
tls_cert_path_self_.clear();
|
||||
}
|
||||
} else {
|
||||
if (ImGui::Selectable(name.c_str())) {
|
||||
tls_cert_path_self_ = entry.path().string();
|
||||
file_selected = true;
|
||||
show_file_browser_ = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
ImGui::TextColored(ImVec4(1, 0, 0, 1), "Error: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
ImGui::PopItemFlag();
|
||||
} else {
|
||||
show_file_browser_ = true;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Render::SelfHostedServerWindow() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
if (show_self_hosted_server_config_window_) {
|
||||
@@ -128,12 +36,12 @@ int Render::SelfHostedServerWindow() {
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.298f, io.DisplaySize.y * 0.25f));
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.407f, io.DisplaySize.y * 0.41f));
|
||||
ImVec2(io.DisplaySize.x * 0.407f, io.DisplaySize.y * 0.35f));
|
||||
} else {
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(io.DisplaySize.x * 0.27f, io.DisplaySize.y * 0.3f));
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(io.DisplaySize.x * 0.465f, io.DisplaySize.y * 0.41f));
|
||||
ImVec2(io.DisplaySize.x * 0.465f, io.DisplaySize.y * 0.35f));
|
||||
}
|
||||
|
||||
self_hosted_server_config_window_pos_reset_ = false;
|
||||
@@ -143,8 +51,8 @@ int Render::SelfHostedServerWindow() {
|
||||
{
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
|
||||
ImGui::Begin(localization::self_hosted_server_settings
|
||||
[localization_language_index_]
|
||||
@@ -212,35 +120,6 @@ int Render::SelfHostedServerWindow() {
|
||||
IM_ARRAYSIZE(coturn_server_port_self_));
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
// {
|
||||
// ImGui::AlignTextToFramePadding();
|
||||
// ImGui::Text(
|
||||
// "%s",
|
||||
// localization::reset_cert_fingerprint[localization_language_index_]
|
||||
// .c_str());
|
||||
// ImGui::SameLine();
|
||||
// if (ConfigCenter::LANGUAGE::CHINESE == localization_language_) {
|
||||
// ImGui::SetCursorPosX(title_bar_button_width_ * 2.5f);
|
||||
// } else {
|
||||
// ImGui::SetCursorPosX(title_bar_button_width_ * 3.43f);
|
||||
// }
|
||||
// ImGui::SetNextItemWidth(title_bar_button_width_ * 3.8f);
|
||||
|
||||
// ShowSimpleFileBrowser();
|
||||
// }
|
||||
|
||||
{
|
||||
ImGui::AlignTextToFramePadding();
|
||||
if (ImGui::Button(localization::reset_cert_fingerprint
|
||||
[localization_language_index_]
|
||||
.c_str())) {
|
||||
config_center_->ClearCertFingerprint();
|
||||
LOG_INFO("Certificate fingerprint cleared by user");
|
||||
}
|
||||
}
|
||||
|
||||
if (stream_window_inited_) {
|
||||
ImGui::EndDisabled();
|
||||
}
|
||||
@@ -263,7 +142,6 @@ int Render::SelfHostedServerWindow() {
|
||||
config_center_->SetServerHost(signal_server_ip_self_);
|
||||
config_center_->SetServerPort(atoi(signal_server_port_self_));
|
||||
config_center_->SetCoturnServerPort(atoi(coturn_server_port_self_));
|
||||
config_center_->SetCertFilePath(tls_cert_path_self_);
|
||||
strncpy(signal_server_ip_, signal_server_ip_self_,
|
||||
sizeof(signal_server_ip_) - 1);
|
||||
signal_server_ip_[sizeof(signal_server_ip_) - 1] = '\0';
|
||||
@@ -273,9 +151,6 @@ int Render::SelfHostedServerWindow() {
|
||||
strncpy(coturn_server_port_, coturn_server_port_self_,
|
||||
sizeof(coturn_server_port_) - 1);
|
||||
coturn_server_port_[sizeof(coturn_server_port_) - 1] = '\0';
|
||||
strncpy(cert_file_path_, tls_cert_path_self_.c_str(),
|
||||
sizeof(cert_file_path_) - 1);
|
||||
cert_file_path_[sizeof(cert_file_path_) - 1] = '\0';
|
||||
|
||||
self_hosted_server_config_window_pos_reset_ = true;
|
||||
}
|
||||
@@ -306,7 +181,6 @@ int Render::SelfHostedServerWindow() {
|
||||
} else {
|
||||
coturn_server_port_self_[0] = '\0';
|
||||
}
|
||||
tls_cert_path_self_ = config_center_->GetCertFilePath();
|
||||
}
|
||||
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
+329
-151
@@ -1,48 +1,47 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "layout_relative.h"
|
||||
#include "localization.h"
|
||||
#include "rd_log.h"
|
||||
#include "render.h"
|
||||
#include "rounded_corner_button.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
constexpr float kDragThresholdPx = 3.0f;
|
||||
int CountDigits(int number) {
|
||||
if (number == 0) return 1;
|
||||
return (int)std::floor(std::log10(std::abs(number))) + 1;
|
||||
}
|
||||
|
||||
// Handles dragging for the *last submitted ImGui item*.
|
||||
// `reset_on_deactivate` should be false when the caller needs to know whether a
|
||||
// deactivation was a click (no drag) vs a drag-release (dragging==true).
|
||||
inline void HandleWindowDragForLastItem(SDL_Window* window, bool& dragging,
|
||||
float& start_mouse_x,
|
||||
float& start_mouse_y, int& start_win_x,
|
||||
int& start_win_y,
|
||||
bool reset_on_deactivate = true) {
|
||||
if (!window) {
|
||||
return;
|
||||
void BitrateDisplay(uint32_t bitrate) {
|
||||
const int num_of_digits = CountDigits(static_cast<int>(bitrate));
|
||||
if (num_of_digits <= 3) {
|
||||
ImGui::Text("%u bps", bitrate);
|
||||
} else if (num_of_digits > 3 && num_of_digits <= 6) {
|
||||
ImGui::Text("%u kbps", bitrate / 1000);
|
||||
} else {
|
||||
ImGui::Text("%.1f mbps", bitrate / 1000000.0f);
|
||||
}
|
||||
}
|
||||
|
||||
if (ImGui::IsItemActivated()) {
|
||||
SDL_GetGlobalMouseState(&start_mouse_x, &start_mouse_y);
|
||||
SDL_GetWindowPosition(window, &start_win_x, &start_win_y);
|
||||
dragging = false;
|
||||
}
|
||||
|
||||
if (ImGui::IsItemActive()) {
|
||||
if (!dragging &&
|
||||
ImGui::IsMouseDragging(ImGuiMouseButton_Left, kDragThresholdPx)) {
|
||||
dragging = true;
|
||||
}
|
||||
|
||||
if (dragging) {
|
||||
float mouse_x = 0.0f;
|
||||
float mouse_y = 0.0f;
|
||||
SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
|
||||
const int dx = (int)(mouse_x - start_mouse_x);
|
||||
const int dy = (int)(mouse_y - start_mouse_y);
|
||||
SDL_SetWindowPosition(window, start_win_x + dx, start_win_y + dy);
|
||||
}
|
||||
}
|
||||
|
||||
if (reset_on_deactivate && ImGui::IsItemDeactivated()) {
|
||||
dragging = false;
|
||||
std::string FormatBytes(uint64_t bytes) {
|
||||
char buf[64];
|
||||
if (bytes < 1024ULL) {
|
||||
std::snprintf(buf, sizeof(buf), "%llu B", (unsigned long long)bytes);
|
||||
} else if (bytes < 1024ULL * 1024ULL) {
|
||||
std::snprintf(buf, sizeof(buf), "%.2f KB", bytes / 1024.0);
|
||||
} else if (bytes < 1024ULL * 1024ULL * 1024ULL) {
|
||||
std::snprintf(buf, sizeof(buf), "%.2f MB", bytes / (1024.0 * 1024.0));
|
||||
} else {
|
||||
std::snprintf(buf, sizeof(buf), "%.2f GB",
|
||||
bytes / (1024.0 * 1024.0 * 1024.0));
|
||||
}
|
||||
return std::string(buf);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -50,152 +49,331 @@ int Render::ServerWindow() {
|
||||
ImGui::SetNextWindowSize(ImVec2(server_window_width_, server_window_height_),
|
||||
ImGuiCond_Always);
|
||||
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::Begin("##server_window", nullptr,
|
||||
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse |
|
||||
ImGuiWindowFlags_NoScrollbar |
|
||||
ImGuiWindowFlags_NoScrollWithMouse);
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
// Collapsed mode: no buttons; drag to move; click to restore.
|
||||
if (server_window_collapsed_) {
|
||||
ImGui::SetCursorPos(ImVec2(0.0f, 0.0f));
|
||||
ImGui::InvisibleButton("##server_collapsed_area",
|
||||
ImVec2(server_window_width_, server_window_height_));
|
||||
server_window_title_bar_height_ = title_bar_height_;
|
||||
|
||||
HandleWindowDragForLastItem(server_window_,
|
||||
server_window_collapsed_dragging_,
|
||||
server_window_collapsed_drag_start_mouse_x_,
|
||||
server_window_collapsed_drag_start_mouse_y_,
|
||||
server_window_collapsed_drag_start_win_x_,
|
||||
server_window_collapsed_drag_start_win_y_,
|
||||
/*reset_on_deactivate=*/false);
|
||||
|
||||
const bool request_restore =
|
||||
ImGui::IsItemDeactivated() && !server_window_collapsed_dragging_;
|
||||
if (ImGui::IsItemDeactivated()) {
|
||||
server_window_collapsed_dragging_ = false;
|
||||
}
|
||||
|
||||
if (request_restore && server_window_) {
|
||||
int w = 0;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int h = 0;
|
||||
SDL_GetWindowSize(server_window_, &w, &h);
|
||||
SDL_GetWindowPosition(server_window_, &x, &y);
|
||||
|
||||
const int normal_h = server_window_normal_height_;
|
||||
SDL_SetWindowSize(server_window_, w, normal_h);
|
||||
SDL_SetWindowPosition(server_window_, x, y);
|
||||
server_window_collapsed_ = false;
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
return 0;
|
||||
}
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 0.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
|
||||
ImGui::BeginChild(
|
||||
"ServerTitleBar",
|
||||
ImVec2(server_window_width_, server_window_title_bar_height_),
|
||||
ImGuiChildFlags_Border,
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
|
||||
float server_title_bar_button_width = server_window_title_bar_height_;
|
||||
float server_title_bar_button_height = server_window_title_bar_height_;
|
||||
|
||||
// Drag area: the title bar excluding the right-side buttons.
|
||||
// Collapse/expand toggle button (FontAwesome icon).
|
||||
{
|
||||
const float drag_w =
|
||||
server_window_width_ - server_title_bar_button_width * 2;
|
||||
const float drag_h = server_title_bar_button_height;
|
||||
ImGui::SetCursorPos(ImVec2(0.0f, 0.0f));
|
||||
ImGui::InvisibleButton("##server_title_drag_area", ImVec2(drag_w, drag_h));
|
||||
|
||||
HandleWindowDragForLastItem(
|
||||
server_window_, server_window_dragging_,
|
||||
server_window_drag_start_mouse_x_, server_window_drag_start_mouse_y_,
|
||||
server_window_drag_start_win_x_, server_window_drag_start_win_y_);
|
||||
}
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0.1f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonActive,
|
||||
ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
|
||||
float minimize_button_pos_x =
|
||||
server_window_width_ - server_title_bar_button_width * 2;
|
||||
ImGui::SetCursorPos(ImVec2(minimize_button_pos_x, 0.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0.1f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
const char* icon =
|
||||
server_window_collapsed_ ? ICON_FA_ANGLE_DOWN : ICON_FA_ANGLE_UP;
|
||||
std::string toggle_label = std::string(icon) + "##server_toggle";
|
||||
|
||||
float minimize_pos_x =
|
||||
minimize_button_pos_x + server_title_bar_button_width * 0.33f;
|
||||
float minimize_pos_y = server_title_bar_button_height * 0.5f;
|
||||
std::string server_minimize_button = "##minimize"; // ICON_FA_MINUS;
|
||||
if (ImGui::Button(server_minimize_button.c_str(),
|
||||
ImVec2(server_title_bar_button_width,
|
||||
server_title_bar_button_height))) {
|
||||
if (server_window_) {
|
||||
int w = 0;
|
||||
int h = 0;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
SDL_GetWindowSize(server_window_, &w, &h);
|
||||
SDL_GetWindowPosition(server_window_, &x, &y);
|
||||
bool toggle_clicked = RoundedCornerButton(
|
||||
toggle_label.c_str(),
|
||||
ImVec2(server_title_bar_button_width, server_title_bar_button_height),
|
||||
8.5f, ImDrawFlags_RoundCornersTopLeft, true, IM_COL32(0, 0, 0, 0),
|
||||
IM_COL32(0, 0, 0, 25), IM_COL32(255, 255, 255, 255));
|
||||
if (toggle_clicked) {
|
||||
if (server_window_) {
|
||||
int w = 0;
|
||||
int h = 0;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
SDL_GetWindowSize(server_window_, &w, &h);
|
||||
SDL_GetWindowPosition(server_window_, &x, &y);
|
||||
|
||||
const int collapsed_h = (int)server_window_title_bar_height_;
|
||||
// Collapse upward: keep top edge stable.
|
||||
SDL_SetWindowSize(server_window_, w, collapsed_h);
|
||||
SDL_SetWindowPosition(server_window_, x, y);
|
||||
server_window_collapsed_ = true;
|
||||
if (server_window_collapsed_) {
|
||||
const int normal_h = server_window_normal_height_;
|
||||
SDL_SetWindowSize(server_window_, w, normal_h);
|
||||
SDL_SetWindowPosition(server_window_, x, y);
|
||||
server_window_collapsed_ = false;
|
||||
} else {
|
||||
const int collapsed_h = (int)server_window_title_bar_height_;
|
||||
// Collapse upward: keep top edge stable.
|
||||
SDL_SetWindowSize(server_window_, w, collapsed_h);
|
||||
SDL_SetWindowPosition(server_window_, x, y);
|
||||
server_window_collapsed_ = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
ImGui::PopStyleColor(3);
|
||||
}
|
||||
draw_list->AddLine(
|
||||
ImVec2(minimize_pos_x, minimize_pos_y),
|
||||
ImVec2(minimize_pos_x + server_title_bar_button_width * 0.33f,
|
||||
minimize_pos_y),
|
||||
IM_COL32(0, 0, 0, 255));
|
||||
ImGui::PopStyleColor(3);
|
||||
|
||||
float xmark_button_pos_x =
|
||||
server_window_width_ - server_title_bar_button_width;
|
||||
ImGui::SetCursorPos(ImVec2(xmark_button_pos_x, 0.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.0f, 0, 0, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0, 0, 0.5f));
|
||||
|
||||
float xmark_pos_x = xmark_button_pos_x + server_title_bar_button_width * 0.5f;
|
||||
float xmark_pos_y = server_title_bar_button_height * 0.5f;
|
||||
float xmark_size = server_title_bar_button_width * 0.33f;
|
||||
std::string server_close_button = "##xmark"; // ICON_FA_XMARK;
|
||||
if (ImGui::Button(server_close_button.c_str(),
|
||||
ImVec2(server_title_bar_button_width,
|
||||
server_title_bar_button_height))) {
|
||||
LOG_ERROR("Close button clicked");
|
||||
LeaveConnection(peer_, self_hosted_id_);
|
||||
}
|
||||
|
||||
draw_list->AddLine(ImVec2(xmark_pos_x - xmark_size / 2 - 0.25f,
|
||||
xmark_pos_y - xmark_size / 2 + 0.75f),
|
||||
ImVec2(xmark_pos_x + xmark_size / 2 - 1.5f,
|
||||
xmark_pos_y + xmark_size / 2 - 0.5f),
|
||||
IM_COL32(0, 0, 0, 255));
|
||||
draw_list->AddLine(
|
||||
ImVec2(xmark_pos_x + xmark_size / 2 - 1.75f,
|
||||
xmark_pos_y - xmark_size / 2 + 0.75f),
|
||||
ImVec2(xmark_pos_x - xmark_size / 2, xmark_pos_y + xmark_size / 2 - 1.0f),
|
||||
IM_COL32(0, 0, 0, 255));
|
||||
|
||||
ImGui::PopStyleColor(3);
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::PopStyleVar(2);
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
RemoteClientInfoWindow();
|
||||
|
||||
ImGui::End();
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
int Render::RemoteClientInfoWindow() {
|
||||
float remote_client_info_window_width = server_window_width_ * 0.8f;
|
||||
float remote_client_info_window_height =
|
||||
(server_window_height_ - server_window_title_bar_height_) * 0.9f;
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f);
|
||||
ImGui::BeginChild(
|
||||
"RemoteClientInfoWindow",
|
||||
ImVec2(remote_client_info_window_width, remote_client_info_window_height),
|
||||
ImGuiChildFlags_Borders,
|
||||
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus);
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::PopStyleColor();
|
||||
|
||||
float font_scale = localization_language_index_ == 0 ? 0.5f : 0.45f;
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> remote_entries;
|
||||
remote_entries.reserve(connection_status_.size());
|
||||
for (const auto& kv : connection_status_) {
|
||||
const auto host_it = connection_host_names_.find(kv.first);
|
||||
const std::string display_name =
|
||||
(host_it != connection_host_names_.end() && !host_it->second.empty())
|
||||
? host_it->second
|
||||
: kv.first;
|
||||
remote_entries.emplace_back(kv.first, display_name);
|
||||
}
|
||||
|
||||
auto find_display_name_by_remote_id =
|
||||
[&remote_entries](const std::string& remote_id) -> std::string {
|
||||
for (const auto& entry : remote_entries) {
|
||||
if (entry.first == remote_id) {
|
||||
return entry.second;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
if (!selected_server_remote_id_.empty() &&
|
||||
find_display_name_by_remote_id(selected_server_remote_id_).empty()) {
|
||||
selected_server_remote_id_.clear();
|
||||
selected_server_remote_hostname_.clear();
|
||||
}
|
||||
if (selected_server_remote_id_.empty() && !remote_entries.empty()) {
|
||||
selected_server_remote_id_ = remote_entries.front().first;
|
||||
}
|
||||
if (!selected_server_remote_id_.empty()) {
|
||||
selected_server_remote_hostname_ =
|
||||
find_display_name_by_remote_id(selected_server_remote_id_);
|
||||
}
|
||||
|
||||
ImGui::SetWindowFontScale(font_scale);
|
||||
ImGui::AlignTextToFramePadding();
|
||||
ImGui::Text("%s",
|
||||
localization::controller[localization_language_index_].c_str());
|
||||
ImGui::SameLine();
|
||||
|
||||
const char* selected_preview = "-";
|
||||
if (!selected_server_remote_hostname_.empty()) {
|
||||
selected_preview = selected_server_remote_hostname_.c_str();
|
||||
} else if (!remote_client_id_.empty()) {
|
||||
selected_preview = remote_client_id_.c_str();
|
||||
}
|
||||
|
||||
ImGui::SetNextItemWidth(remote_client_info_window_width *
|
||||
(localization_language_index_ == 0 ? 0.68f : 0.62f));
|
||||
ImGui::SetWindowFontScale(localization_language_index_ == 0 ? 0.45f : 0.4f);
|
||||
ImGui::AlignTextToFramePadding();
|
||||
if (ImGui::BeginCombo("##server_remote_id", selected_preview)) {
|
||||
ImGui::SetWindowFontScale(localization_language_index_ == 0 ? 0.45f : 0.4f);
|
||||
for (int i = 0; i < static_cast<int>(remote_entries.size()); i++) {
|
||||
const bool selected =
|
||||
(remote_entries[i].first == selected_server_remote_id_);
|
||||
if (ImGui::Selectable(remote_entries[i].second.c_str(), selected)) {
|
||||
selected_server_remote_id_ = remote_entries[i].first;
|
||||
selected_server_remote_hostname_ = remote_entries[i].second;
|
||||
}
|
||||
if (selected) {
|
||||
ImGui::SetItemDefaultFocus();
|
||||
}
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
ImGui::SetWindowFontScale(font_scale);
|
||||
|
||||
if (!selected_server_remote_id_.empty()) {
|
||||
auto it = connection_status_.find(selected_server_remote_id_);
|
||||
const ConnectionStatus status = (it == connection_status_.end())
|
||||
? ConnectionStatus::Closed
|
||||
: it->second;
|
||||
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::connection_status[localization_language_index_].c_str());
|
||||
ImGui::SameLine();
|
||||
|
||||
switch (status) {
|
||||
case ConnectionStatus::Connected:
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::p2p_connected[localization_language_index_].c_str());
|
||||
break;
|
||||
case ConnectionStatus::Connecting:
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::p2p_connecting[localization_language_index_].c_str());
|
||||
break;
|
||||
case ConnectionStatus::Disconnected:
|
||||
ImGui::Text("%s",
|
||||
localization::p2p_disconnected[localization_language_index_]
|
||||
.c_str());
|
||||
break;
|
||||
case ConnectionStatus::Failed:
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::p2p_failed[localization_language_index_].c_str());
|
||||
break;
|
||||
case ConnectionStatus::Closed:
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::p2p_closed[localization_language_index_].c_str());
|
||||
break;
|
||||
default:
|
||||
ImGui::Text(
|
||||
"%s",
|
||||
localization::p2p_failed[localization_language_index_].c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
ImGui::AlignTextToFramePadding();
|
||||
ImGui::Text(
|
||||
"%s", localization::file_transfer[localization_language_index_].c_str());
|
||||
|
||||
ImGui::SameLine();
|
||||
|
||||
if (ImGui::Button(
|
||||
localization::select_file[localization_language_index_].c_str())) {
|
||||
std::string title = localization::select_file[localization_language_index_];
|
||||
std::string path = OpenFileDialog(title);
|
||||
LOG_INFO("Selected file path: {}", path.c_str());
|
||||
|
||||
ProcessSelectedFile(path, nullptr, file_label_, selected_server_remote_id_);
|
||||
}
|
||||
|
||||
if (file_transfer_.file_transfer_window_visible_) {
|
||||
ImGui::SameLine();
|
||||
const bool is_sending = file_transfer_.file_sending_.load();
|
||||
|
||||
if (is_sending) {
|
||||
// Simple animation: cycle icon every 0.5s while sending.
|
||||
static const char* kFileTransferIcons[] = {ICON_FA_ANGLE_UP,
|
||||
ICON_FA_ANGLES_UP};
|
||||
const int icon_index = static_cast<int>(ImGui::GetTime() / 0.5) %
|
||||
(static_cast<int>(sizeof(kFileTransferIcons) /
|
||||
sizeof(kFileTransferIcons[0])));
|
||||
ImGui::Text("%s", kFileTransferIcons[icon_index]);
|
||||
} else {
|
||||
// Completed.
|
||||
ImGui::Text("%s", ICON_FA_CHECK);
|
||||
}
|
||||
|
||||
if (ImGui::IsItemHovered()) {
|
||||
const uint64_t sent_bytes = file_transfer_.file_sent_bytes_.load();
|
||||
const uint64_t total_bytes = file_transfer_.file_total_bytes_.load();
|
||||
const uint32_t rate_bps = file_transfer_.file_send_rate_bps_.load();
|
||||
|
||||
float progress = 0.0f;
|
||||
if (total_bytes > 0) {
|
||||
progress =
|
||||
static_cast<float>(sent_bytes) / static_cast<float>(total_bytes);
|
||||
progress = (std::max)(0.0f, (std::min)(1.0f, progress));
|
||||
}
|
||||
|
||||
std::string current_file_name;
|
||||
const uint32_t current_file_id = file_transfer_.current_file_id_.load();
|
||||
if (current_file_id != 0) {
|
||||
std::lock_guard<std::mutex> lock(
|
||||
file_transfer_.file_transfer_list_mutex_);
|
||||
for (const auto& info : file_transfer_.file_transfer_list_) {
|
||||
if (info.file_id == current_file_id) {
|
||||
current_file_name = info.file_name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::BeginTooltip();
|
||||
if (server_windows_system_chinese_font_) {
|
||||
ImGui::PushFont(server_windows_system_chinese_font_);
|
||||
}
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
if (!current_file_name.empty()) {
|
||||
ImGui::Text("%s", current_file_name.c_str());
|
||||
}
|
||||
if (total_bytes > 0) {
|
||||
const std::string sent_str = FormatBytes(sent_bytes);
|
||||
const std::string total_str = FormatBytes(total_bytes);
|
||||
ImGui::Text("%s / %s", sent_str.c_str(), total_str.c_str());
|
||||
}
|
||||
|
||||
const float text_height = ImGui::GetTextLineHeight();
|
||||
char overlay[32];
|
||||
std::snprintf(overlay, sizeof(overlay), "%.1f%%", progress * 100.0f);
|
||||
ImGui::ProgressBar(progress, ImVec2(180.0f, text_height), overlay);
|
||||
BitrateDisplay(rate_bps);
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
if (server_windows_system_chinese_font_) {
|
||||
ImGui::PopFont();
|
||||
}
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
|
||||
ImGui::EndChild();
|
||||
|
||||
ImGui::SameLine();
|
||||
|
||||
float close_connection_button_width = server_window_width_ * 0.1f;
|
||||
float close_connection_button_height = remote_client_info_window_height;
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.0f, 0.3f, 0.3f, 1.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.0f, 0.5f, 0.5f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_);
|
||||
ImGui::SetWindowFontScale(font_scale);
|
||||
if (ImGui::Button(ICON_FA_XMARK, ImVec2(close_connection_button_width,
|
||||
close_connection_button_height))) {
|
||||
if (peer_ && !selected_server_remote_id_.empty()) {
|
||||
LeaveConnection(peer_, selected_server_remote_id_.c_str());
|
||||
}
|
||||
}
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
ImGui::PopStyleColor(3);
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -31,6 +31,34 @@ void Render::DrawConnectionStatusText(
|
||||
}
|
||||
}
|
||||
|
||||
void Render::DrawReceivingScreenText(
|
||||
std::shared_ptr<SubStreamWindowProperties>& props) {
|
||||
if (!props->connection_established_ ||
|
||||
props->connection_status_ != ConnectionStatus::Connected) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool has_valid_frame = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(props->video_frame_mutex_);
|
||||
has_valid_frame = props->stream_texture_ != nullptr &&
|
||||
props->video_width_ > 0 && props->video_height_ > 0 &&
|
||||
props->front_frame_ && !props->front_frame_->empty();
|
||||
}
|
||||
|
||||
if (has_valid_frame) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string& text =
|
||||
localization::receiving_screen[localization_language_index_];
|
||||
ImVec2 size = ImGui::GetWindowSize();
|
||||
ImVec2 text_size = ImGui::CalcTextSize(text.c_str());
|
||||
ImGui::SetCursorPos(
|
||||
ImVec2((size.x - text_size.x) * 0.5f, (size.y - text_size.y) * 0.5f));
|
||||
ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 0.92f), "%s", text.c_str());
|
||||
}
|
||||
|
||||
void Render::CloseTab(decltype(client_properties_)::iterator& it) {
|
||||
// std::unique_lock lock(client_properties_mutex_);
|
||||
if (it != client_properties_.end()) {
|
||||
@@ -117,7 +145,9 @@ int Render::StreamWindow() {
|
||||
ImGui::SetWindowFontScale(0.6f);
|
||||
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(stream_window_width_, stream_window_height_),
|
||||
ImVec2(stream_window_width_,
|
||||
stream_window_height_ -
|
||||
(fullscreen_button_pressed_ ? 0 : title_bar_height_)),
|
||||
ImGuiCond_Always);
|
||||
ImGui::SetNextWindowPos(
|
||||
ImVec2(0, fullscreen_button_pressed_ ? 0 : title_bar_height_),
|
||||
@@ -138,10 +168,12 @@ int Render::StreamWindow() {
|
||||
UpdateRenderRect();
|
||||
|
||||
ControlWindow(props);
|
||||
|
||||
|
||||
// Show file transfer window if needed
|
||||
FileTransferWindow(props);
|
||||
|
||||
DrawReceivingScreenText(props);
|
||||
|
||||
focused_remote_id_ = props->remote_id_;
|
||||
|
||||
if (!props->peer_) {
|
||||
@@ -151,12 +183,12 @@ int Render::StreamWindow() {
|
||||
// std::unique_lock unique_lock(client_properties_mutex_);
|
||||
auto erase_it = client_properties_.find(remote_id_to_erase);
|
||||
if (erase_it != client_properties_.end()) {
|
||||
erase_it = client_properties_.erase(erase_it);
|
||||
if (client_properties_.empty()) {
|
||||
SDL_Event event;
|
||||
event.type = SDL_EVENT_QUIT;
|
||||
SDL_PushEvent(&event);
|
||||
}
|
||||
// Ensure we flush pending STREAM_REFRESH_EVENT events and
|
||||
// clean up peer resources before erasing the entry, otherwise
|
||||
// SDL events may still hold raw pointers to freed
|
||||
// SubStreamWindowProperties (including video_frame_mutex_),
|
||||
// leading to std::system_error when locking.
|
||||
CloseTab(erase_it);
|
||||
}
|
||||
}
|
||||
// lock.lock();
|
||||
@@ -217,7 +249,9 @@ int Render::StreamWindow() {
|
||||
|
||||
if (props->tab_selected_) {
|
||||
ImGui::SetNextWindowSize(
|
||||
ImVec2(stream_window_width_, stream_window_height_),
|
||||
ImVec2(stream_window_width_,
|
||||
stream_window_height_ -
|
||||
(fullscreen_button_pressed_ ? 0 : title_bar_height_)),
|
||||
ImGuiCond_Always);
|
||||
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
||||
@@ -236,10 +270,12 @@ int Render::StreamWindow() {
|
||||
UpdateRenderRect();
|
||||
|
||||
ControlWindow(props);
|
||||
|
||||
|
||||
// Show file transfer window if needed
|
||||
FileTransferWindow(props);
|
||||
|
||||
|
||||
DrawReceivingScreenText(props);
|
||||
|
||||
ImGui::End();
|
||||
|
||||
if (!props->peer_) {
|
||||
@@ -251,12 +287,7 @@ int Render::StreamWindow() {
|
||||
// std::unique_lock unique_lock(client_properties_mutex_);
|
||||
auto erase_it = client_properties_.find(remote_id_to_erase);
|
||||
if (erase_it != client_properties_.end()) {
|
||||
client_properties_.erase(erase_it);
|
||||
if (client_properties_.empty()) {
|
||||
SDL_Event event;
|
||||
event.type = SDL_EVENT_QUIT;
|
||||
SDL_PushEvent(&event);
|
||||
}
|
||||
CloseTab(erase_it);
|
||||
}
|
||||
}
|
||||
// lock.lock();
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#include "layout.h"
|
||||
@@ -77,8 +76,8 @@ int Render::UpdateNotificationWindow() {
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 1.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 5.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, window_rounding_);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, window_rounding_ * 0.5f);
|
||||
ImGui::Begin(
|
||||
localization::notification[localization_language_index_].c_str(),
|
||||
nullptr,
|
||||
@@ -93,7 +92,7 @@ int Render::UpdateNotificationWindow() {
|
||||
ImGui::SetWindowFontScale(0.55f);
|
||||
std::string title =
|
||||
localization::new_version_available[localization_language_index_] +
|
||||
": v" + latest_version_;
|
||||
": " + latest_version_;
|
||||
ImGui::Text("%s", title.c_str());
|
||||
ImGui::SetWindowFontScale(0.1f);
|
||||
|
||||
@@ -104,6 +103,7 @@ int Render::UpdateNotificationWindow() {
|
||||
localization::access_website[localization_language_index_] +
|
||||
"https://crossdesk.cn";
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
ImGui::SetCursorPosX(update_notification_window_width * 0.1f);
|
||||
Hyperlink(download_text, "https://crossdesk.cn",
|
||||
update_notification_window_width);
|
||||
ImGui::SetWindowFontScale(1.0f);
|
||||
@@ -121,7 +121,7 @@ int Render::UpdateNotificationWindow() {
|
||||
ImGui::BeginChild(
|
||||
"ScrollableContent",
|
||||
ImVec2(update_notification_window_width * 0.9f, scrollable_height),
|
||||
ImGuiChildFlags_Border, ImGuiWindowFlags_None);
|
||||
ImGuiChildFlags_Borders, ImGuiWindowFlags_None);
|
||||
ImGui::SetWindowFontScale(0.5f);
|
||||
// set text wrap position to current available width (accounts for
|
||||
// scrollbar)
|
||||
@@ -184,14 +184,7 @@ int Render::UpdateNotificationWindow() {
|
||||
localization::update[localization_language_index_].c_str())) {
|
||||
// open download page
|
||||
std::string url = "https://crossdesk.cn";
|
||||
#if defined(_WIN32)
|
||||
std::string cmd = "start " + url;
|
||||
#elif defined(__APPLE__)
|
||||
std::string cmd = "open " + url;
|
||||
#else
|
||||
std::string cmd = "xdg-open " + url;
|
||||
#endif
|
||||
system(cmd.c_str());
|
||||
OpenUrl(url);
|
||||
show_update_notification_window_ = false;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -62,4 +62,4 @@ std::shared_ptr<spdlog::logger> get_logger() {
|
||||
|
||||
return g_logger;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -1,12 +1,98 @@
|
||||
#include "path_manager.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
#ifndef CROSSDESK_PORTABLE
|
||||
#define CROSSDESK_PORTABLE 0
|
||||
#endif
|
||||
|
||||
#if CROSSDESK_PORTABLE
|
||||
#if defined(__APPLE__)
|
||||
#include <mach-o/dyld.h>
|
||||
#elif !defined(_WIN32)
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
#if CROSSDESK_PORTABLE
|
||||
std::filesystem::path GetExecutableDirectory() {
|
||||
#ifdef _WIN32
|
||||
std::vector<wchar_t> buffer(MAX_PATH);
|
||||
while (true) {
|
||||
DWORD length =
|
||||
GetModuleFileNameW(nullptr, buffer.data(),
|
||||
static_cast<DWORD>(buffer.size()));
|
||||
if (length == 0) {
|
||||
return {};
|
||||
}
|
||||
if (length < buffer.size()) {
|
||||
return std::filesystem::path(buffer.data(), buffer.data() + length)
|
||||
.parent_path();
|
||||
}
|
||||
if (buffer.size() >= 32768) {
|
||||
return {};
|
||||
}
|
||||
buffer.resize(buffer.size() * 2);
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
uint32_t size = 0;
|
||||
_NSGetExecutablePath(nullptr, &size);
|
||||
std::vector<char> buffer(size + 1);
|
||||
if (_NSGetExecutablePath(buffer.data(), &size) != 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::path executable =
|
||||
std::filesystem::weakly_canonical(buffer.data(), ec);
|
||||
if (ec) {
|
||||
executable = buffer.data();
|
||||
}
|
||||
return executable.parent_path();
|
||||
#else
|
||||
std::vector<char> buffer(PATH_MAX);
|
||||
while (true) {
|
||||
ssize_t length = readlink("/proc/self/exe", buffer.data(),
|
||||
buffer.size() - 1);
|
||||
if (length <= 0) {
|
||||
return {};
|
||||
}
|
||||
if (static_cast<size_t>(length) < buffer.size() - 1) {
|
||||
buffer[static_cast<size_t>(length)] = '\0';
|
||||
return std::filesystem::path(buffer.data()).parent_path();
|
||||
}
|
||||
buffer.resize(buffer.size() * 2);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
std::filesystem::path GetPortableRootPath() {
|
||||
std::filesystem::path executable_dir = GetExecutableDirectory();
|
||||
if (!executable_dir.empty()) {
|
||||
return executable_dir;
|
||||
}
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::path current = std::filesystem::current_path(ec);
|
||||
return ec ? std::filesystem::path(".") : current;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
PathManager::PathManager(const std::string& app_name) : app_name_(app_name) {}
|
||||
|
||||
std::filesystem::path PathManager::GetConfigPath() {
|
||||
#if CROSSDESK_PORTABLE
|
||||
return GetPortableRootPath() / "data";
|
||||
#else
|
||||
#ifdef _WIN32
|
||||
return GetKnownFolder(FOLDERID_RoamingAppData) / app_name_;
|
||||
#elif __APPLE__
|
||||
@@ -14,9 +100,13 @@ std::filesystem::path PathManager::GetConfigPath() {
|
||||
#else
|
||||
return GetEnvOrDefault("XDG_CONFIG_HOME", GetHome() + "/.config") / app_name_;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
std::filesystem::path PathManager::GetCachePath() {
|
||||
#if CROSSDESK_PORTABLE
|
||||
return GetPortableRootPath() / "data";
|
||||
#else
|
||||
#ifdef _WIN32
|
||||
#ifdef CROSSDESK_DEBUG
|
||||
return "cache";
|
||||
@@ -28,9 +118,13 @@ std::filesystem::path PathManager::GetCachePath() {
|
||||
#else
|
||||
return GetEnvOrDefault("XDG_CACHE_HOME", GetHome() + "/.cache") / app_name_;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
std::filesystem::path PathManager::GetLogPath() {
|
||||
#if CROSSDESK_PORTABLE
|
||||
return GetPortableRootPath() / "logs";
|
||||
#else
|
||||
#ifdef _WIN32
|
||||
return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "logs";
|
||||
#elif __APPLE__
|
||||
@@ -38,19 +132,6 @@ std::filesystem::path PathManager::GetLogPath() {
|
||||
#else
|
||||
return GetCachePath() / "logs";
|
||||
#endif
|
||||
}
|
||||
|
||||
std::filesystem::path PathManager::GetCertPath() {
|
||||
#ifdef _WIN32
|
||||
// %APPDATA%\AppName\Certs
|
||||
return GetKnownFolder(FOLDERID_RoamingAppData) / app_name_ / "certs";
|
||||
#elif __APPLE__
|
||||
// $HOME/Library/Application Support/AppName/certs
|
||||
return GetHome() + "/Library/Application Support/" + app_name_ + "/certs";
|
||||
#else
|
||||
// $XDG_CONFIG_HOME/AppName/certs
|
||||
return GetEnvOrDefault("XDG_CONFIG_HOME", GetHome() + "/.config") /
|
||||
app_name_ / "certs";
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -107,4 +188,4 @@ std::filesystem::path PathManager::GetEnvOrDefault(const char* env_var,
|
||||
|
||||
return std::filesystem::path(def);
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -26,8 +26,6 @@ class PathManager {
|
||||
|
||||
std::filesystem::path GetLogPath();
|
||||
|
||||
std::filesystem::path GetCertPath();
|
||||
|
||||
bool CreateDirectories(const std::filesystem::path& p);
|
||||
|
||||
private:
|
||||
|
||||
@@ -0,0 +1,573 @@
|
||||
#include "screen_capturer_drm.h"
|
||||
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM && \
|
||||
defined(__has_include) && __has_include(<xf86drm.h>) && \
|
||||
__has_include(<xf86drmMode.h>)
|
||||
#define CROSSDESK_DRM_BUILD_ENABLED 1
|
||||
#include <xf86drm.h>
|
||||
#include <xf86drmMode.h>
|
||||
#elif defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM && \
|
||||
defined(__has_include) && __has_include(<libdrm/xf86drm.h>) && \
|
||||
__has_include(<libdrm/xf86drmMode.h>)
|
||||
#define CROSSDESK_DRM_BUILD_ENABLED 1
|
||||
#include <libdrm/xf86drm.h>
|
||||
#include <libdrm/xf86drmMode.h>
|
||||
#else
|
||||
#define CROSSDESK_DRM_BUILD_ENABLED 0
|
||||
#endif
|
||||
|
||||
#if CROSSDESK_DRM_BUILD_ENABLED
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "libyuv.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kMaxDrmCards = 16;
|
||||
|
||||
const char* ConnectorTypeName(uint32_t type) {
|
||||
switch (type) {
|
||||
case DRM_MODE_CONNECTOR_VGA:
|
||||
return "VGA";
|
||||
case DRM_MODE_CONNECTOR_DVII:
|
||||
return "DVI-I";
|
||||
case DRM_MODE_CONNECTOR_DVID:
|
||||
return "DVI-D";
|
||||
case DRM_MODE_CONNECTOR_DVIA:
|
||||
return "DVI-A";
|
||||
case DRM_MODE_CONNECTOR_HDMIA:
|
||||
return "HDMI-A";
|
||||
case DRM_MODE_CONNECTOR_HDMIB:
|
||||
return "HDMI-B";
|
||||
case DRM_MODE_CONNECTOR_DisplayPort:
|
||||
return "DP";
|
||||
case DRM_MODE_CONNECTOR_eDP:
|
||||
return "eDP";
|
||||
case DRM_MODE_CONNECTOR_LVDS:
|
||||
return "LVDS";
|
||||
#ifdef DRM_MODE_CONNECTOR_VIRTUAL
|
||||
case DRM_MODE_CONNECTOR_VIRTUAL:
|
||||
return "Virtual";
|
||||
#endif
|
||||
default:
|
||||
return "Display";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerDrm::ScreenCapturerDrm() {}
|
||||
|
||||
ScreenCapturerDrm::~ScreenCapturerDrm() { Destroy(); }
|
||||
|
||||
int ScreenCapturerDrm::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
if (!cb) {
|
||||
LOG_ERROR("DRM screen capturer callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fps_ = std::max(1, fps);
|
||||
callback_ = cb;
|
||||
monitor_index_ = 0;
|
||||
initial_monitor_index_ = 0;
|
||||
consecutive_failures_ = 0;
|
||||
display_info_list_.clear();
|
||||
outputs_.clear();
|
||||
y_plane_.clear();
|
||||
uv_plane_.clear();
|
||||
|
||||
if (!DiscoverOutputs()) {
|
||||
LOG_ERROR("DRM screen capturer could not find active outputs");
|
||||
callback_ = nullptr;
|
||||
CloseDevices();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Destroy() {
|
||||
Stop();
|
||||
callback_ = nullptr;
|
||||
display_info_list_.clear();
|
||||
outputs_.clear();
|
||||
y_plane_.clear();
|
||||
uv_plane_.clear();
|
||||
CloseDevices();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Start(bool show_cursor) {
|
||||
if (running_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (outputs_.empty()) {
|
||||
LOG_ERROR("DRM screen capturer has no output to capture");
|
||||
return -1;
|
||||
}
|
||||
|
||||
show_cursor_ = show_cursor;
|
||||
paused_ = false;
|
||||
|
||||
int probe_index = monitor_index_.load();
|
||||
if (probe_index < 0 || probe_index >= static_cast<int>(outputs_.size())) {
|
||||
probe_index = 0;
|
||||
}
|
||||
|
||||
if (!CaptureOutputFrame(outputs_[probe_index], false)) {
|
||||
LOG_ERROR("DRM start probe failed on output {}", outputs_[probe_index].name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
running_ = true;
|
||||
thread_ = std::thread([this]() { CaptureLoop(); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Stop() {
|
||||
if (!running_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running_ = false;
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Pause([[maybe_unused]] int monitor_index) {
|
||||
paused_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Resume([[maybe_unused]] int monitor_index) {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::SwitchTo(int monitor_index) {
|
||||
if (monitor_index < 0 ||
|
||||
monitor_index >= static_cast<int>(display_info_list_.size())) {
|
||||
LOG_ERROR("Invalid DRM monitor index: {}", monitor_index);
|
||||
return -1;
|
||||
}
|
||||
|
||||
monitor_index_ = monitor_index;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::ResetToInitialMonitor() {
|
||||
monitor_index_ = initial_monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerDrm::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::DiscoverOutputs() {
|
||||
for (int card_index = 0; card_index < kMaxDrmCards; ++card_index) {
|
||||
const std::string card_path = "/dev/dri/card" + std::to_string(card_index);
|
||||
const int fd = open(card_path.c_str(), O_RDWR | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
drmModeRes* resources = drmModeGetResources(fd);
|
||||
if (!resources) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
DrmDevice device;
|
||||
device.fd = fd;
|
||||
device.path = card_path;
|
||||
devices_.push_back(device);
|
||||
const int device_slot = static_cast<int>(devices_.size()) - 1;
|
||||
const size_t output_count_before = outputs_.size();
|
||||
|
||||
for (int i = 0; i < resources->count_connectors; ++i) {
|
||||
drmModeConnector* connector =
|
||||
drmModeGetConnector(fd, resources->connectors[i]);
|
||||
if (!connector) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connector->connection != DRM_MODE_CONNECTED ||
|
||||
connector->count_modes <= 0) {
|
||||
drmModeFreeConnector(connector);
|
||||
continue;
|
||||
}
|
||||
|
||||
uint32_t crtc_id = 0;
|
||||
if (connector->encoder_id != 0) {
|
||||
drmModeEncoder* encoder = drmModeGetEncoder(fd, connector->encoder_id);
|
||||
if (encoder) {
|
||||
crtc_id = encoder->crtc_id;
|
||||
drmModeFreeEncoder(encoder);
|
||||
}
|
||||
}
|
||||
|
||||
if (crtc_id == 0) {
|
||||
for (int enc_idx = 0; enc_idx < connector->count_encoders; ++enc_idx) {
|
||||
drmModeEncoder* encoder =
|
||||
drmModeGetEncoder(fd, connector->encoders[enc_idx]);
|
||||
if (!encoder) {
|
||||
continue;
|
||||
}
|
||||
if (encoder->crtc_id != 0) {
|
||||
crtc_id = encoder->crtc_id;
|
||||
drmModeFreeEncoder(encoder);
|
||||
break;
|
||||
}
|
||||
drmModeFreeEncoder(encoder);
|
||||
}
|
||||
}
|
||||
|
||||
if (crtc_id == 0) {
|
||||
drmModeFreeConnector(connector);
|
||||
continue;
|
||||
}
|
||||
|
||||
drmModeCrtc* crtc = drmModeGetCrtc(fd, crtc_id);
|
||||
if (!crtc || !crtc->mode_valid || crtc->width <= 0 || crtc->height <= 0) {
|
||||
if (crtc) {
|
||||
drmModeFreeCrtc(crtc);
|
||||
}
|
||||
drmModeFreeConnector(connector);
|
||||
continue;
|
||||
}
|
||||
|
||||
DrmOutput output;
|
||||
output.device_index = device_slot;
|
||||
output.connector_id = connector->connector_id;
|
||||
output.crtc_id = crtc_id;
|
||||
output.left = crtc->x;
|
||||
output.top = crtc->y;
|
||||
output.width = static_cast<int>(crtc->width);
|
||||
output.height = static_cast<int>(crtc->height);
|
||||
output.name = std::string(ConnectorTypeName(connector->connector_type)) +
|
||||
std::to_string(connector->connector_type_id);
|
||||
|
||||
outputs_.push_back(output);
|
||||
display_info_list_.push_back(
|
||||
DisplayInfo(output.name, output.left, output.top,
|
||||
output.left + output.width, output.top + output.height));
|
||||
|
||||
LOG_INFO("DRM output found: {} on {}, {}x{} @ ({}, {})", output.name,
|
||||
card_path, output.width, output.height, output.left, output.top);
|
||||
|
||||
drmModeFreeCrtc(crtc);
|
||||
drmModeFreeConnector(connector);
|
||||
}
|
||||
|
||||
drmModeFreeResources(resources);
|
||||
|
||||
if (outputs_.size() == output_count_before) {
|
||||
close(fd);
|
||||
devices_.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
if (outputs_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_INFO("DRM screen capturer discovered {} output(s)", outputs_.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::CloseDevices() {
|
||||
for (auto& device : devices_) {
|
||||
if (device.fd >= 0) {
|
||||
close(device.fd);
|
||||
device.fd = -1;
|
||||
}
|
||||
}
|
||||
devices_.clear();
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::CaptureLoop() {
|
||||
using clock = std::chrono::steady_clock;
|
||||
const auto frame_interval =
|
||||
std::chrono::milliseconds(std::max(1, 1000 / std::max(1, fps_)));
|
||||
|
||||
while (running_) {
|
||||
const auto frame_start = clock::now();
|
||||
if (!paused_) {
|
||||
int index = monitor_index_.load();
|
||||
if (index >= 0 && index < static_cast<int>(outputs_.size())) {
|
||||
const bool ok = CaptureOutputFrame(outputs_[index], true);
|
||||
if (!ok) {
|
||||
++consecutive_failures_;
|
||||
if (consecutive_failures_ == 1 || consecutive_failures_ % 60 == 0) {
|
||||
LOG_WARN("DRM capture failed (consecutive={})",
|
||||
consecutive_failures_);
|
||||
}
|
||||
} else {
|
||||
consecutive_failures_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
clock::now() - frame_start);
|
||||
if (elapsed < frame_interval) {
|
||||
std::this_thread::sleep_for(frame_interval - elapsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::CaptureOutputFrame(const DrmOutput& output,
|
||||
bool emit_callback) {
|
||||
if (output.device_index < 0 ||
|
||||
output.device_index >= static_cast<int>(devices_.size())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int fd = devices_[output.device_index].fd;
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
drmModeCrtc* crtc = drmModeGetCrtc(fd, output.crtc_id);
|
||||
if (!crtc) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t fb_id = crtc->buffer_id;
|
||||
drmModeFreeCrtc(crtc);
|
||||
if (fb_id == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
drmModeFB* fb = drmModeGetFB(fd, fb_id);
|
||||
if (!fb) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t handle = fb->handle;
|
||||
const uint32_t pitch = fb->pitch;
|
||||
const int src_width = static_cast<int>(fb->width);
|
||||
const int src_height = static_cast<int>(fb->height);
|
||||
const int bpp = static_cast<int>(fb->bpp);
|
||||
drmModeFreeFB(fb);
|
||||
|
||||
if (handle == 0 || pitch == 0 || src_width <= 1 || src_height <= 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bpp != 32) {
|
||||
LOG_WARN("DRM capture unsupported bpp: {}", bpp);
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t map_size =
|
||||
static_cast<size_t>(pitch) * static_cast<size_t>(src_height);
|
||||
uint8_t* mapped_ptr = nullptr;
|
||||
size_t mapped_size = 0;
|
||||
int prime_fd = -1;
|
||||
if (!MapFramebuffer(fd, handle, map_size, &mapped_ptr, &mapped_size,
|
||||
&prime_fd)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int capture_width = std::min(src_width, output.width);
|
||||
int capture_height = std::min(src_height, output.height);
|
||||
if (capture_width <= 0 || capture_height <= 0) {
|
||||
capture_width = src_width;
|
||||
capture_height = src_height;
|
||||
}
|
||||
|
||||
capture_width &= ~1;
|
||||
capture_height &= ~1;
|
||||
if (capture_width <= 1 || capture_height <= 1) {
|
||||
UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t y_size =
|
||||
static_cast<size_t>(capture_width) * static_cast<size_t>(capture_height);
|
||||
const size_t uv_size = y_size / 2;
|
||||
if (y_plane_.size() != y_size) {
|
||||
y_plane_.resize(y_size);
|
||||
}
|
||||
if (uv_plane_.size() != uv_size) {
|
||||
uv_plane_.resize(uv_size);
|
||||
}
|
||||
|
||||
const int convert_ret =
|
||||
libyuv::ARGBToNV12(mapped_ptr, static_cast<int>(pitch), y_plane_.data(),
|
||||
capture_width, uv_plane_.data(), capture_width,
|
||||
capture_width, capture_height);
|
||||
|
||||
if (convert_ret != 0) {
|
||||
UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> nv12;
|
||||
nv12.reserve(y_plane_.size() + uv_plane_.size());
|
||||
nv12.insert(nv12.end(), y_plane_.begin(), y_plane_.end());
|
||||
nv12.insert(nv12.end(), uv_plane_.begin(), uv_plane_.end());
|
||||
|
||||
if (emit_callback && callback_) {
|
||||
callback_(nv12.data(), static_cast<int>(nv12.size()), capture_width,
|
||||
capture_height, output.name.c_str());
|
||||
}
|
||||
|
||||
UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::MapFramebuffer(int fd, uint32_t handle, size_t map_size,
|
||||
uint8_t** mapped_ptr,
|
||||
size_t* mapped_size,
|
||||
int* prime_fd) const {
|
||||
if (!mapped_ptr || !mapped_size || !prime_fd || map_size == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*mapped_ptr = nullptr;
|
||||
*mapped_size = 0;
|
||||
*prime_fd = -1;
|
||||
|
||||
drm_mode_map_dumb map_arg{};
|
||||
map_arg.handle = handle;
|
||||
if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg) == 0) {
|
||||
void* mapped = mmap(nullptr, map_size, PROT_READ, MAP_SHARED, fd,
|
||||
static_cast<off_t>(map_arg.offset));
|
||||
if (mapped != MAP_FAILED) {
|
||||
*mapped_ptr = static_cast<uint8_t*>(mapped);
|
||||
*mapped_size = map_size;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
int dma_fd = -1;
|
||||
if (drmPrimeHandleToFD(fd, handle, DRM_CLOEXEC, &dma_fd) == 0) {
|
||||
size_t dma_map_size = map_size;
|
||||
const off_t fd_size = lseek(dma_fd, 0, SEEK_END);
|
||||
if (fd_size > 0) {
|
||||
dma_map_size = std::min(map_size, static_cast<size_t>(fd_size));
|
||||
}
|
||||
|
||||
void* mapped =
|
||||
mmap(nullptr, dma_map_size, PROT_READ, MAP_SHARED, dma_fd, 0);
|
||||
if (mapped != MAP_FAILED) {
|
||||
*mapped_ptr = static_cast<uint8_t*>(mapped);
|
||||
*mapped_size = dma_map_size;
|
||||
*prime_fd = dma_fd;
|
||||
return true;
|
||||
}
|
||||
|
||||
close(dma_fd);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::UnmapFramebuffer(uint8_t* mapped_ptr, size_t mapped_size,
|
||||
int prime_fd) const {
|
||||
if (mapped_ptr && mapped_size > 0) {
|
||||
munmap(mapped_ptr, mapped_size);
|
||||
}
|
||||
|
||||
if (prime_fd >= 0) {
|
||||
close(prime_fd);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#else
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
ScreenCapturerDrm::ScreenCapturerDrm() {}
|
||||
|
||||
ScreenCapturerDrm::~ScreenCapturerDrm() { Destroy(); }
|
||||
|
||||
int ScreenCapturerDrm::Init([[maybe_unused]] const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
callback_ = cb;
|
||||
LOG_WARN("DRM screen capturer disabled: libdrm headers not available");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Destroy() {
|
||||
Stop();
|
||||
callback_ = nullptr;
|
||||
display_info_list_.clear();
|
||||
outputs_.clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Start([[maybe_unused]] bool show_cursor) { return -1; }
|
||||
|
||||
int ScreenCapturerDrm::Stop() {
|
||||
running_ = false;
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::Pause([[maybe_unused]] int monitor_index) { return 0; }
|
||||
|
||||
int ScreenCapturerDrm::Resume([[maybe_unused]] int monitor_index) { return 0; }
|
||||
|
||||
int ScreenCapturerDrm::SwitchTo([[maybe_unused]] int monitor_index) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ScreenCapturerDrm::ResetToInitialMonitor() { return 0; }
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerDrm::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::DiscoverOutputs() { return false; }
|
||||
|
||||
void ScreenCapturerDrm::CloseDevices() {}
|
||||
|
||||
void ScreenCapturerDrm::CaptureLoop() {}
|
||||
|
||||
bool ScreenCapturerDrm::CaptureOutputFrame(
|
||||
[[maybe_unused]] const DrmOutput& output,
|
||||
[[maybe_unused]] bool emit_callback) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDrm::MapFramebuffer([[maybe_unused]] int fd,
|
||||
[[maybe_unused]] uint32_t handle,
|
||||
[[maybe_unused]] size_t map_size,
|
||||
[[maybe_unused]] uint8_t** mapped_ptr,
|
||||
[[maybe_unused]] size_t* mapped_size,
|
||||
[[maybe_unused]] int* prime_fd) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScreenCapturerDrm::UnmapFramebuffer([[maybe_unused]] uint8_t* mapped_ptr,
|
||||
[[maybe_unused]] size_t mapped_size,
|
||||
[[maybe_unused]] int prime_fd) const {}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_DRM_H_
|
||||
#define _SCREEN_CAPTURER_DRM_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerDrm : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerDrm();
|
||||
~ScreenCapturerDrm();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
private:
|
||||
struct DrmDevice {
|
||||
int fd = -1;
|
||||
std::string path;
|
||||
};
|
||||
|
||||
struct DrmOutput {
|
||||
int device_index = -1;
|
||||
uint32_t connector_id = 0;
|
||||
uint32_t crtc_id = 0;
|
||||
std::string name;
|
||||
int left = 0;
|
||||
int top = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
bool DiscoverOutputs();
|
||||
void CloseDevices();
|
||||
void CaptureLoop();
|
||||
bool CaptureOutputFrame(const DrmOutput& output, bool emit_callback = true);
|
||||
bool MapFramebuffer(int fd, uint32_t handle, size_t map_size,
|
||||
uint8_t** mapped_ptr, size_t* mapped_size,
|
||||
int* prime_fd) const;
|
||||
void UnmapFramebuffer(uint8_t* mapped_ptr, size_t mapped_size,
|
||||
int prime_fd) const;
|
||||
|
||||
private:
|
||||
std::vector<DrmDevice> devices_;
|
||||
std::vector<DrmOutput> outputs_;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
std::thread thread_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_;
|
||||
int consecutive_failures_ = 0;
|
||||
|
||||
std::vector<uint8_t> y_plane_;
|
||||
std::vector<uint8_t> uv_plane_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,507 @@
|
||||
#include "screen_capturer_linux.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
#include "screen_capturer_drm.h"
|
||||
#endif
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
#include "screen_capturer_wayland.h"
|
||||
#endif
|
||||
#include "screen_capturer_x11.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
constexpr bool kDrmBuildEnabled = true;
|
||||
#else
|
||||
constexpr bool kDrmBuildEnabled = false;
|
||||
#endif
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
constexpr bool kWaylandBuildEnabled = true;
|
||||
#else
|
||||
constexpr bool kWaylandBuildEnabled = false;
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerLinux::ScreenCapturerLinux() {}
|
||||
|
||||
ScreenCapturerLinux::~ScreenCapturerLinux() { Destroy(); }
|
||||
|
||||
int ScreenCapturerLinux::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
if (!cb) {
|
||||
LOG_ERROR("Linux screen capturer callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fps_ = fps;
|
||||
callback_orig_ = std::move(cb);
|
||||
callback_ = [this](unsigned char* data, int size, int width, int height,
|
||||
const char* display_name) {
|
||||
const std::string mapped_name = MapDisplayName(display_name);
|
||||
if (callback_orig_) {
|
||||
callback_orig_(data, size, width, height, mapped_name.c_str());
|
||||
}
|
||||
};
|
||||
|
||||
const char* force_backend = getenv("CROSSDESK_SCREEN_BACKEND");
|
||||
if (force_backend && force_backend[0] != '\0') {
|
||||
if (strcmp(force_backend, "drm") == 0) {
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
LOG_INFO("Linux screen capturer forced backend: DRM");
|
||||
return InitDrm();
|
||||
#else
|
||||
LOG_ERROR(
|
||||
"Linux screen capturer forced backend DRM is disabled at build time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (strcmp(force_backend, "x11") == 0) {
|
||||
LOG_INFO("Linux screen capturer forced backend: X11");
|
||||
return InitX11();
|
||||
}
|
||||
if (strcmp(force_backend, "wayland") == 0) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
LOG_INFO("Linux screen capturer forced backend: Wayland");
|
||||
return InitWayland();
|
||||
#else
|
||||
LOG_ERROR(
|
||||
"Linux screen capturer forced backend Wayland is disabled at build "
|
||||
"time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
LOG_WARN("Unknown CROSSDESK_SCREEN_BACKEND={}, using auto strategy",
|
||||
force_backend);
|
||||
}
|
||||
|
||||
const bool wayland_session = IsWaylandSession();
|
||||
if (wayland_session) {
|
||||
if (kDrmBuildEnabled) {
|
||||
LOG_INFO("Wayland session detected, prefer DRM -> X11 -> Wayland");
|
||||
if (InitDrm() == 0) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
LOG_INFO("Wayland session detected, DRM disabled, prefer X11 -> Wayland");
|
||||
}
|
||||
|
||||
if (InitX11() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (kDrmBuildEnabled) {
|
||||
LOG_WARN(
|
||||
"DRM and X11 init failed in Wayland session, trying Wayland portal");
|
||||
} else {
|
||||
LOG_WARN("X11 init failed in Wayland session, trying Wayland portal");
|
||||
}
|
||||
if (kWaylandBuildEnabled) {
|
||||
return InitWayland();
|
||||
}
|
||||
LOG_ERROR("Wayland session detected but Wayland backend is disabled");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (InitX11() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (kDrmBuildEnabled) {
|
||||
LOG_WARN("X11 init failed, trying DRM fallback");
|
||||
return InitDrm();
|
||||
}
|
||||
|
||||
LOG_ERROR("X11 init failed and DRM backend is disabled");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Destroy() {
|
||||
if (impl_) {
|
||||
impl_->Destroy();
|
||||
impl_.reset();
|
||||
}
|
||||
|
||||
backend_ = BackendType::kNone;
|
||||
callback_ = nullptr;
|
||||
callback_orig_ = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
canonical_displays_.clear();
|
||||
label_alias_.clear();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Start(bool show_cursor) {
|
||||
if (!impl_) {
|
||||
LOG_ERROR("Linux screen capturer backend is not initialized");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
if (backend_ == BackendType::kWayland) {
|
||||
const int refresh_ret = RefreshWaylandBackend();
|
||||
if (refresh_ret != 0) {
|
||||
LOG_WARN("Linux screen capturer Wayland backend refresh failed: {}",
|
||||
refresh_ret);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
const int ret = impl_->Start(show_cursor);
|
||||
if (ret == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* backend_name = "None";
|
||||
if (backend_ == BackendType::kX11) {
|
||||
backend_name = "X11";
|
||||
} else if (backend_ == BackendType::kDrm) {
|
||||
backend_name = "DRM";
|
||||
} else if (backend_ == BackendType::kWayland) {
|
||||
backend_name = "Wayland";
|
||||
}
|
||||
|
||||
LOG_WARN("Linux screen capturer backend {} start failed: {}", backend_name,
|
||||
ret);
|
||||
|
||||
if (backend_ == BackendType::kX11 && kDrmBuildEnabled &&
|
||||
TryFallbackToDrm(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (backend_ == BackendType::kX11 && kWaylandBuildEnabled &&
|
||||
TryFallbackToWayland(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (backend_ == BackendType::kDrm && kDrmBuildEnabled) {
|
||||
if (TryFallbackToX11(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (kWaylandBuildEnabled && TryFallbackToWayland(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (backend_ == BackendType::kWayland && kWaylandBuildEnabled) {
|
||||
if (kDrmBuildEnabled && TryFallbackToDrm(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
if (TryFallbackToX11(show_cursor)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Stop() {
|
||||
if (!impl_) {
|
||||
return 0;
|
||||
}
|
||||
const int ret = impl_->Stop();
|
||||
UpdateAliasesFromBackend(impl_.get());
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Pause(int monitor_index) {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
return impl_->Pause(monitor_index);
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::Resume(int monitor_index) {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
return impl_->Resume(monitor_index);
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::SwitchTo(int monitor_index) {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
return impl_->SwitchTo(monitor_index);
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::ResetToInitialMonitor() {
|
||||
if (!impl_) {
|
||||
return -1;
|
||||
}
|
||||
return impl_->ResetToInitialMonitor();
|
||||
}
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerLinux::GetDisplayInfoList() {
|
||||
if (!impl_) {
|
||||
return std::vector<DisplayInfo>();
|
||||
}
|
||||
|
||||
// Wayland backend may update display geometry/stream handle asynchronously
|
||||
// after Start(). Refresh aliases every time to keep canonical displays fresh.
|
||||
UpdateAliasesFromBackend(impl_.get());
|
||||
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
if (!canonical_displays_.empty()) {
|
||||
return canonical_displays_;
|
||||
}
|
||||
|
||||
return impl_->GetDisplayInfoList();
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::InitX11() {
|
||||
auto backend = std::make_unique<ScreenCapturerX11>();
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
LOG_WARN("Linux screen capturer X11 init failed: {}", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
impl_ = std::move(backend);
|
||||
backend_ = BackendType::kX11;
|
||||
LOG_INFO("Linux screen capturer backend selected: X11");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::InitDrm() {
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
auto backend = std::make_unique<ScreenCapturerDrm>();
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
LOG_WARN("Linux screen capturer DRM init failed: {}", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
impl_ = std::move(backend);
|
||||
backend_ = BackendType::kDrm;
|
||||
LOG_INFO("Linux screen capturer backend selected: DRM");
|
||||
return 0;
|
||||
#else
|
||||
LOG_WARN("Linux screen capturer DRM backend is disabled at build time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::InitWayland() {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
auto backend = std::make_unique<ScreenCapturerWayland>();
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
LOG_WARN("Linux screen capturer Wayland init failed: {}", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
impl_ = std::move(backend);
|
||||
backend_ = BackendType::kWayland;
|
||||
LOG_INFO("Linux screen capturer backend selected: Wayland");
|
||||
return 0;
|
||||
#else
|
||||
LOG_WARN("Linux screen capturer Wayland backend is disabled at build time");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ScreenCapturerLinux::RefreshWaylandBackend() {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
auto backend = std::make_unique<ScreenCapturerWayland>();
|
||||
const int ret = backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
backend->Destroy();
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(backend.get());
|
||||
impl_ = std::move(backend);
|
||||
backend_ = BackendType::kWayland;
|
||||
LOG_INFO("Linux screen capturer Wayland backend refreshed before start");
|
||||
return 0;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ScreenCapturerLinux::TryFallbackToDrm(bool show_cursor) {
|
||||
#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM
|
||||
auto drm_backend = std::make_unique<ScreenCapturerDrm>();
|
||||
int ret = drm_backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Linux screen capturer fallback DRM init failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(drm_backend.get());
|
||||
ret = drm_backend->Start(show_cursor);
|
||||
if (ret != 0) {
|
||||
drm_backend->Destroy();
|
||||
LOG_ERROR("Linux screen capturer fallback DRM start failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Stop();
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
impl_ = std::move(drm_backend);
|
||||
backend_ = BackendType::kDrm;
|
||||
LOG_INFO("Linux screen capturer fallback switched to DRM");
|
||||
return true;
|
||||
#else
|
||||
(void)show_cursor;
|
||||
LOG_WARN("Linux screen capturer DRM fallback is disabled at build time");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ScreenCapturerLinux::TryFallbackToX11(bool show_cursor) {
|
||||
auto x11_backend = std::make_unique<ScreenCapturerX11>();
|
||||
int ret = x11_backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Linux screen capturer fallback X11 init failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(x11_backend.get());
|
||||
ret = x11_backend->Start(show_cursor);
|
||||
if (ret != 0) {
|
||||
x11_backend->Destroy();
|
||||
LOG_ERROR("Linux screen capturer fallback X11 start failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Stop();
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
impl_ = std::move(x11_backend);
|
||||
backend_ = BackendType::kX11;
|
||||
LOG_INFO("Linux screen capturer fallback switched to X11");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerLinux::TryFallbackToWayland(bool show_cursor) {
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
auto wayland_backend = std::make_unique<ScreenCapturerWayland>();
|
||||
int ret = wayland_backend->Init(fps_, callback_);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Linux screen capturer fallback Wayland init failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
UpdateAliasesFromBackend(wayland_backend.get());
|
||||
ret = wayland_backend->Start(show_cursor);
|
||||
if (ret != 0) {
|
||||
wayland_backend->Destroy();
|
||||
LOG_ERROR("Linux screen capturer fallback Wayland start failed: {}", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (impl_) {
|
||||
impl_->Stop();
|
||||
impl_->Destroy();
|
||||
}
|
||||
|
||||
impl_ = std::move(wayland_backend);
|
||||
backend_ = BackendType::kWayland;
|
||||
LOG_INFO("Linux screen capturer fallback switched to Wayland");
|
||||
return true;
|
||||
#else
|
||||
(void)show_cursor;
|
||||
LOG_WARN("Linux screen capturer Wayland fallback is disabled at build time");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void ScreenCapturerLinux::UpdateAliasesFromBackend(ScreenCapturer* backend) {
|
||||
if (!backend) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto backend_displays = backend->GetDisplayInfoList();
|
||||
if (backend_displays.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
label_alias_.clear();
|
||||
|
||||
if (canonical_displays_.empty()) {
|
||||
canonical_displays_ = backend_displays;
|
||||
for (const auto& display : backend_displays) {
|
||||
label_alias_[display.name] = display.name;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (canonical_displays_.size() < backend_displays.size()) {
|
||||
for (size_t i = canonical_displays_.size(); i < backend_displays.size();
|
||||
++i) {
|
||||
canonical_displays_.push_back(backend_displays[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < backend_displays.size(); ++i) {
|
||||
const std::string mapped_name = i < canonical_displays_.size()
|
||||
? canonical_displays_[i].name
|
||||
: backend_displays[i].name;
|
||||
label_alias_[backend_displays[i].name] = mapped_name;
|
||||
|
||||
if (i < canonical_displays_.size()) {
|
||||
// Keep original stable names, but refresh geometry from active backend.
|
||||
canonical_displays_[i].handle = backend_displays[i].handle;
|
||||
canonical_displays_[i].is_primary = backend_displays[i].is_primary;
|
||||
canonical_displays_[i].left = backend_displays[i].left;
|
||||
canonical_displays_[i].top = backend_displays[i].top;
|
||||
canonical_displays_[i].right = backend_displays[i].right;
|
||||
canonical_displays_[i].bottom = backend_displays[i].bottom;
|
||||
canonical_displays_[i].width = backend_displays[i].width;
|
||||
canonical_displays_[i].height = backend_displays[i].height;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string ScreenCapturerLinux::MapDisplayName(
|
||||
const char* display_name) const {
|
||||
std::string input_name = display_name ? display_name : "";
|
||||
if (input_name.empty()) {
|
||||
return input_name;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(alias_mutex_);
|
||||
auto it = label_alias_.find(input_name);
|
||||
if (it != label_alias_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
if (canonical_displays_.size() == 1) {
|
||||
return canonical_displays_[0].name;
|
||||
}
|
||||
|
||||
return input_name;
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_LINUX_H_
|
||||
#define _SCREEN_CAPTURER_LINUX_H_
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerLinux : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerLinux();
|
||||
~ScreenCapturerLinux();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
private:
|
||||
enum class BackendType { kNone, kX11, kDrm, kWayland };
|
||||
|
||||
private:
|
||||
int InitX11();
|
||||
int InitDrm();
|
||||
int InitWayland();
|
||||
int RefreshWaylandBackend();
|
||||
bool TryFallbackToDrm(bool show_cursor);
|
||||
bool TryFallbackToX11(bool show_cursor);
|
||||
bool TryFallbackToWayland(bool show_cursor);
|
||||
void UpdateAliasesFromBackend(ScreenCapturer* backend);
|
||||
std::string MapDisplayName(const char* display_name) const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<ScreenCapturer> impl_;
|
||||
BackendType backend_ = BackendType::kNone;
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_;
|
||||
cb_desktop_data callback_orig_;
|
||||
std::vector<DisplayInfo> canonical_displays_;
|
||||
mutable std::mutex alias_mutex_;
|
||||
std::unordered_map<std::string, std::string> label_alias_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,250 @@
|
||||
#include "screen_capturer_wayland.h"
|
||||
|
||||
#include "screen_capturer_wayland_build.h"
|
||||
|
||||
#if !CROSSDESK_WAYLAND_BUILD_ENABLED
|
||||
#error \
|
||||
"Wayland capturer requires USE_WAYLAND=true and Wayland development headers"
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
#include "wayland_portal_shared.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
int64_t NowMs() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
struct PipeWireRecoveryConfig {
|
||||
ScreenCapturerWayland::PipeWireConnectMode mode;
|
||||
bool relaxed_connect = false;
|
||||
};
|
||||
|
||||
constexpr auto kPipeWireCloseSettleDelay = std::chrono::milliseconds(200);
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerWayland::ScreenCapturerWayland() {}
|
||||
|
||||
ScreenCapturerWayland::~ScreenCapturerWayland() { Destroy(); }
|
||||
|
||||
int ScreenCapturerWayland::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
if (!IsWaylandSession()) {
|
||||
LOG_ERROR("Wayland screen capturer requires a Wayland session");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!cb) {
|
||||
LOG_ERROR("Wayland screen capturer callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!CheckPortalAvailability()) {
|
||||
LOG_ERROR("xdg-desktop-portal screencast service is unavailable");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!EnsurePipeWireRuntimeAvailable()) {
|
||||
LOG_ERROR("Wayland screen capturer requires PipeWire 0.3 runtime");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fps_ = fps;
|
||||
callback_ = cb;
|
||||
pointer_granted_ = false;
|
||||
shared_session_registered_ = false;
|
||||
display_info_list_.clear();
|
||||
display_info_list_.push_back(
|
||||
DisplayInfo(display_name_, 0, 0, kFallbackWidth, kFallbackHeight));
|
||||
monitor_index_ = 0;
|
||||
initial_monitor_index_ = 0;
|
||||
frame_width_ = kFallbackWidth;
|
||||
frame_height_ = kFallbackHeight;
|
||||
frame_stride_ = kFallbackWidth * 4;
|
||||
portal_has_logical_size_ = false;
|
||||
portal_stream_width_ = 0;
|
||||
portal_stream_height_ = 0;
|
||||
logical_width_ = kFallbackWidth;
|
||||
logical_height_ = kFallbackHeight;
|
||||
y_plane_.resize(kFallbackWidth * kFallbackHeight);
|
||||
uv_plane_.resize((kFallbackWidth / 2) * (kFallbackHeight / 2) * 2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Destroy() {
|
||||
Stop();
|
||||
y_plane_.clear();
|
||||
uv_plane_.clear();
|
||||
display_info_list_.clear();
|
||||
callback_ = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Start(bool show_cursor) {
|
||||
if (running_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
show_cursor_ = show_cursor;
|
||||
paused_ = false;
|
||||
pipewire_node_id_ = 0;
|
||||
UpdateDisplayGeometry(
|
||||
logical_width_ > 0 ? logical_width_ : kFallbackWidth,
|
||||
logical_height_ > 0 ? logical_height_ : kFallbackHeight);
|
||||
pipewire_format_ready_.store(false);
|
||||
pipewire_stream_start_ms_.store(0);
|
||||
pipewire_last_frame_ms_.store(0);
|
||||
running_ = true;
|
||||
thread_ = std::thread([this]() { Run(); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Stop() {
|
||||
running_ = false;
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
pipewire_node_id_ = 0;
|
||||
UpdateDisplayGeometry(
|
||||
logical_width_ > 0 ? logical_width_ : kFallbackWidth,
|
||||
logical_height_ > 0 ? logical_height_ : kFallbackHeight);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Pause([[maybe_unused]] int monitor_index) {
|
||||
paused_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::Resume([[maybe_unused]] int monitor_index) {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::SwitchTo(int monitor_index) {
|
||||
if (monitor_index != 0) {
|
||||
LOG_WARN("Wayland screencast currently supports one logical display");
|
||||
return -1;
|
||||
}
|
||||
|
||||
monitor_index_ = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWayland::ResetToInitialMonitor() {
|
||||
monitor_index_ = initial_monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerWayland::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::Run() {
|
||||
static constexpr PipeWireRecoveryConfig kRecoveryConfigs[] = {
|
||||
{PipeWireConnectMode::kTargetObject, false},
|
||||
{PipeWireConnectMode::kAny, true},
|
||||
{PipeWireConnectMode::kNodeId, false},
|
||||
{PipeWireConnectMode::kNodeId, true},
|
||||
};
|
||||
|
||||
int recovery_index = 0;
|
||||
auto setup_pipewire = [this, &recovery_index]() -> bool {
|
||||
const auto& config = kRecoveryConfigs[recovery_index];
|
||||
return OpenPipeWireRemote() &&
|
||||
SetupPipeWireStream(config.relaxed_connect, config.mode);
|
||||
};
|
||||
auto setup_pipeline = [this, &setup_pipewire]() -> bool {
|
||||
return ConnectSessionBus() && CreatePortalSession() &&
|
||||
SelectPortalDevices() && SelectPortalSource() &&
|
||||
StartPortalSession() && setup_pipewire();
|
||||
};
|
||||
|
||||
if (!setup_pipeline()) {
|
||||
running_ = false;
|
||||
CleanupPipeWire();
|
||||
ClosePortalSession();
|
||||
CleanupDbus();
|
||||
return;
|
||||
}
|
||||
while (running_) {
|
||||
if (!paused_) {
|
||||
const int64_t now = NowMs();
|
||||
const int64_t stream_start = pipewire_stream_start_ms_.load();
|
||||
const int64_t last_frame = pipewire_last_frame_ms_.load();
|
||||
const bool format_ready = pipewire_format_ready_.load();
|
||||
|
||||
const bool format_timeout =
|
||||
stream_start > 0 && !format_ready && (now - stream_start) > 1200;
|
||||
const bool first_frame_timeout = stream_start > 0 && format_ready &&
|
||||
last_frame == 0 &&
|
||||
(now - stream_start) > 4000;
|
||||
const bool frame_stall = last_frame > 0 && (now - last_frame) > 5000;
|
||||
|
||||
if (format_timeout || first_frame_timeout || frame_stall) {
|
||||
if (recovery_index + 1 >=
|
||||
static_cast<int>(sizeof(kRecoveryConfigs) /
|
||||
sizeof(kRecoveryConfigs[0]))) {
|
||||
LOG_ERROR(
|
||||
"Wayland capture stalled and recovery limit reached, "
|
||||
"format_ready={}, stream_start={}, last_frame={}, attempts={}",
|
||||
format_ready, stream_start, last_frame, recovery_index);
|
||||
running_ = false;
|
||||
break;
|
||||
}
|
||||
|
||||
++recovery_index;
|
||||
const char* reason =
|
||||
format_timeout
|
||||
? "format-timeout"
|
||||
: (first_frame_timeout ? "first-frame-timeout" : "frame-stall");
|
||||
const auto& config = kRecoveryConfigs[recovery_index];
|
||||
LOG_WARN(
|
||||
"Wayland capture stalled ({}) - retrying PipeWire only, "
|
||||
"attempt {}/{}, mode={}, relaxed_connect={}",
|
||||
reason, recovery_index,
|
||||
static_cast<int>(sizeof(kRecoveryConfigs) /
|
||||
sizeof(kRecoveryConfigs[0])) -
|
||||
1,
|
||||
config.mode == PipeWireConnectMode::kTargetObject
|
||||
? "target-object"
|
||||
: (config.mode == PipeWireConnectMode::kNodeId ? "node-id"
|
||||
: "any"),
|
||||
config.relaxed_connect);
|
||||
|
||||
CleanupPipeWire();
|
||||
if (!setup_pipewire()) {
|
||||
LOG_ERROR("Wayland PipeWire-only recovery failed at attempt {}",
|
||||
recovery_index);
|
||||
running_ = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
|
||||
CleanupPipeWire();
|
||||
if (!session_handle_.empty()) {
|
||||
std::this_thread::sleep_for(kPipeWireCloseSettleDelay);
|
||||
}
|
||||
ClosePortalSession();
|
||||
CleanupDbus();
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_WAYLAND_H_
|
||||
#define _SCREEN_CAPTURER_WAYLAND_H_
|
||||
|
||||
struct DBusConnection;
|
||||
struct pw_context;
|
||||
struct pw_core;
|
||||
struct pw_stream;
|
||||
struct pw_thread_loop;
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerWayland : public ScreenCapturer {
|
||||
public:
|
||||
enum class PipeWireConnectMode { kTargetObject, kNodeId, kAny };
|
||||
|
||||
public:
|
||||
ScreenCapturerWayland();
|
||||
~ScreenCapturerWayland();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
private:
|
||||
bool CheckPortalAvailability() const;
|
||||
bool ConnectSessionBus();
|
||||
bool CreatePortalSession();
|
||||
bool SelectPortalDevices();
|
||||
bool SelectPortalSource();
|
||||
bool StartPortalSession();
|
||||
bool EnsurePipeWireRuntimeAvailable() const;
|
||||
bool OpenPipeWireRemote();
|
||||
bool SetupPipeWireStream(bool relaxed_connect, PipeWireConnectMode mode);
|
||||
|
||||
void Run();
|
||||
void CleanupPipeWire();
|
||||
void CleanupDbus();
|
||||
void ClosePortalSession();
|
||||
void HandlePipeWireBuffer();
|
||||
void UpdateDisplayGeometry(int width, int height);
|
||||
|
||||
private:
|
||||
static constexpr int kFallbackWidth = 1920;
|
||||
static constexpr int kFallbackHeight = 1080;
|
||||
|
||||
std::thread thread_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
std::atomic<bool> pipewire_format_ready_{false};
|
||||
std::atomic<int64_t> pipewire_stream_start_ms_{0};
|
||||
std::atomic<int64_t> pipewire_last_frame_ms_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_ = nullptr;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
|
||||
DBusConnection* dbus_connection_ = nullptr;
|
||||
std::string session_handle_;
|
||||
std::string display_name_ = "WAYLAND0";
|
||||
uint32_t pipewire_node_id_ = 0;
|
||||
int pipewire_fd_ = -1;
|
||||
|
||||
pw_thread_loop* pw_thread_loop_ = nullptr;
|
||||
pw_context* pw_context_ = nullptr;
|
||||
pw_core* pw_core_ = nullptr;
|
||||
pw_stream* pw_stream_ = nullptr;
|
||||
void* stream_listener_ = nullptr;
|
||||
bool pipewire_initialized_ = false;
|
||||
bool pipewire_thread_loop_started_ = false;
|
||||
bool pointer_granted_ = false;
|
||||
bool shared_session_registered_ = false;
|
||||
bool portal_has_logical_size_ = false;
|
||||
uint32_t spa_video_format_ = 0;
|
||||
int frame_width_ = 0;
|
||||
int frame_height_ = 0;
|
||||
int frame_stride_ = 0;
|
||||
int portal_stream_width_ = 0;
|
||||
int portal_stream_height_ = 0;
|
||||
int logical_width_ = 0;
|
||||
int logical_height_ = 0;
|
||||
|
||||
std::vector<uint8_t> y_plane_;
|
||||
std::vector<uint8_t> uv_plane_;
|
||||
};
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-03-22
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_WAYLAND_BUILD_H_
|
||||
#define _SCREEN_CAPTURER_WAYLAND_BUILD_H_
|
||||
|
||||
#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER
|
||||
|
||||
#define CROSSDESK_WAYLAND_BUILD_ENABLED 1
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
#include <pipewire/keys.h>
|
||||
#include <pipewire/pipewire.h>
|
||||
#include <pipewire/stream.h>
|
||||
#include <pipewire/thread-loop.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/format-utils.h>
|
||||
#include <spa/param/video/format-utils.h>
|
||||
#include <spa/param/video/raw.h>
|
||||
#include <spa/buffer/meta.h>
|
||||
#include <spa/utils/result.h>
|
||||
|
||||
#if defined(__has_include)
|
||||
#if __has_include(<spa/param/buffers.h>)
|
||||
#include <spa/param/buffers.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_BUFFERS 1u
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_BLOCKS 2u
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_SIZE 3u
|
||||
#define CROSSDESK_SPA_PARAM_BUFFERS_STRIDE 4u
|
||||
|
||||
#define CROSSDESK_SPA_PARAM_META_TYPE 1u
|
||||
#define CROSSDESK_SPA_PARAM_META_SIZE 2u
|
||||
|
||||
#else
|
||||
|
||||
#define CROSSDESK_WAYLAND_BUILD_ENABLED 0
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,868 @@
|
||||
#include "screen_capturer_wayland.h"
|
||||
#include "screen_capturer_wayland_build.h"
|
||||
|
||||
#if CROSSDESK_WAYLAND_BUILD_ENABLED
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "libyuv.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
struct PipeWireDynamicApi {
|
||||
void* library = nullptr;
|
||||
bool available = false;
|
||||
|
||||
decltype(&::pw_init) init = nullptr;
|
||||
decltype(&::pw_deinit) deinit = nullptr;
|
||||
decltype(&::pw_thread_loop_new) thread_loop_new = nullptr;
|
||||
decltype(&::pw_thread_loop_destroy) thread_loop_destroy = nullptr;
|
||||
decltype(&::pw_thread_loop_get_loop) thread_loop_get_loop = nullptr;
|
||||
decltype(&::pw_thread_loop_start) thread_loop_start = nullptr;
|
||||
decltype(&::pw_thread_loop_stop) thread_loop_stop = nullptr;
|
||||
decltype(&::pw_thread_loop_lock) thread_loop_lock = nullptr;
|
||||
decltype(&::pw_thread_loop_unlock) thread_loop_unlock = nullptr;
|
||||
decltype(&::pw_thread_loop_wait) thread_loop_wait = nullptr;
|
||||
decltype(&::pw_thread_loop_signal) thread_loop_signal = nullptr;
|
||||
decltype(&::pw_context_new) context_new = nullptr;
|
||||
decltype(&::pw_context_destroy) context_destroy = nullptr;
|
||||
decltype(&::pw_context_connect_fd) context_connect_fd = nullptr;
|
||||
decltype(&::pw_properties_new) properties_new = nullptr;
|
||||
decltype(&::pw_properties_set) properties_set = nullptr;
|
||||
decltype(&::pw_stream_new) stream_new = nullptr;
|
||||
decltype(&::pw_stream_add_listener) stream_add_listener = nullptr;
|
||||
decltype(&::pw_stream_state_as_string) stream_state_as_string = nullptr;
|
||||
decltype(&::pw_stream_connect) stream_connect = nullptr;
|
||||
decltype(&::pw_stream_update_params) stream_update_params = nullptr;
|
||||
decltype(&::pw_stream_set_active) stream_set_active = nullptr;
|
||||
decltype(&::pw_stream_disconnect) stream_disconnect = nullptr;
|
||||
decltype(&::pw_stream_destroy) stream_destroy = nullptr;
|
||||
decltype(&::pw_stream_dequeue_buffer) stream_dequeue_buffer = nullptr;
|
||||
decltype(&::pw_stream_queue_buffer) stream_queue_buffer = nullptr;
|
||||
decltype(&::pw_core_disconnect) core_disconnect = nullptr;
|
||||
decltype(&::pw_proxy_destroy) proxy_destroy = nullptr;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool LoadPipeWireSymbol(void* library, T* function, const char* symbol_name) {
|
||||
*function = reinterpret_cast<T>(dlsym(library, symbol_name));
|
||||
if (*function != nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
LOG_ERROR("Unable to find PipeWire symbol {}", symbol_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
void UnloadPipeWireApi(PipeWireDynamicApi* api) {
|
||||
if (api->library != nullptr) {
|
||||
dlclose(api->library);
|
||||
}
|
||||
*api = PipeWireDynamicApi{};
|
||||
}
|
||||
|
||||
bool LoadPipeWireApi(PipeWireDynamicApi* api) {
|
||||
static constexpr const char* kPipeWireLibraries[] = {
|
||||
"libpipewire-0.3.so.0",
|
||||
"libpipewire-0.3.so",
|
||||
};
|
||||
|
||||
for (const char* library_name : kPipeWireLibraries) {
|
||||
api->library = dlopen(library_name, RTLD_LAZY | RTLD_LOCAL);
|
||||
if (api->library != nullptr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (api->library == nullptr) {
|
||||
LOG_WARN("PipeWire 0.3 runtime library is unavailable");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!LoadPipeWireSymbol(api->library, &api->init, "pw_init") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->deinit, "pw_deinit") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_new,
|
||||
"pw_thread_loop_new") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_destroy,
|
||||
"pw_thread_loop_destroy") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_get_loop,
|
||||
"pw_thread_loop_get_loop") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_start,
|
||||
"pw_thread_loop_start") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_stop,
|
||||
"pw_thread_loop_stop") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_lock,
|
||||
"pw_thread_loop_lock") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_unlock,
|
||||
"pw_thread_loop_unlock") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_wait,
|
||||
"pw_thread_loop_wait") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->thread_loop_signal,
|
||||
"pw_thread_loop_signal") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->context_new, "pw_context_new") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->context_destroy,
|
||||
"pw_context_destroy") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->context_connect_fd,
|
||||
"pw_context_connect_fd") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->properties_new,
|
||||
"pw_properties_new") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->properties_set,
|
||||
"pw_properties_set") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_new, "pw_stream_new") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_add_listener,
|
||||
"pw_stream_add_listener") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_state_as_string,
|
||||
"pw_stream_state_as_string") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_connect,
|
||||
"pw_stream_connect") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_update_params,
|
||||
"pw_stream_update_params") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_set_active,
|
||||
"pw_stream_set_active") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_disconnect,
|
||||
"pw_stream_disconnect") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_destroy,
|
||||
"pw_stream_destroy") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_dequeue_buffer,
|
||||
"pw_stream_dequeue_buffer") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->stream_queue_buffer,
|
||||
"pw_stream_queue_buffer") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->core_disconnect,
|
||||
"pw_core_disconnect") ||
|
||||
!LoadPipeWireSymbol(api->library, &api->proxy_destroy,
|
||||
"pw_proxy_destroy")) {
|
||||
UnloadPipeWireApi(api);
|
||||
return false;
|
||||
}
|
||||
|
||||
api->available = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
const PipeWireDynamicApi* GetPipeWireApi() {
|
||||
static PipeWireDynamicApi api;
|
||||
static std::once_flag once;
|
||||
std::call_once(once, []() { LoadPipeWireApi(&api); });
|
||||
return api.available ? &api : nullptr;
|
||||
}
|
||||
|
||||
const char* PipeWireFormatName(uint32_t spa_format) {
|
||||
switch (spa_format) {
|
||||
case SPA_VIDEO_FORMAT_BGRx:
|
||||
return "BGRx";
|
||||
case SPA_VIDEO_FORMAT_BGRA:
|
||||
return "BGRA";
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBx
|
||||
case SPA_VIDEO_FORMAT_RGBx:
|
||||
return "RGBx";
|
||||
#endif
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBA
|
||||
case SPA_VIDEO_FORMAT_RGBA:
|
||||
return "RGBA";
|
||||
#endif
|
||||
default:
|
||||
return "unsupported";
|
||||
}
|
||||
}
|
||||
|
||||
const char* PipeWireConnectModeName(
|
||||
ScreenCapturerWayland::PipeWireConnectMode mode) {
|
||||
switch (mode) {
|
||||
case ScreenCapturerWayland::PipeWireConnectMode::kTargetObject:
|
||||
return "target-object";
|
||||
case ScreenCapturerWayland::PipeWireConnectMode::kNodeId:
|
||||
return "node-id";
|
||||
case ScreenCapturerWayland::PipeWireConnectMode::kAny:
|
||||
return "any";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
int64_t NowMs() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
double SnapLikelyFractionalScale(double observed_scale) {
|
||||
static constexpr double kCandidates[] = {
|
||||
1.0, 1.25, 1.3333333333, 1.5, 1.6666666667, 1.75, 2.0, 2.25, 2.5, 3.0};
|
||||
double best = observed_scale;
|
||||
double best_error = std::numeric_limits<double>::max();
|
||||
for (double candidate : kCandidates) {
|
||||
const double error = std::abs(candidate - observed_scale);
|
||||
if (error < best_error) {
|
||||
best = candidate;
|
||||
best_error = error;
|
||||
}
|
||||
}
|
||||
|
||||
return best_error <= 0.08 ? best : observed_scale;
|
||||
}
|
||||
|
||||
struct PipeWireTargetLookupState {
|
||||
const PipeWireDynamicApi* pipewire = nullptr;
|
||||
pw_thread_loop* loop = nullptr;
|
||||
uint32_t target_node_id = 0;
|
||||
int sync_seq = -1;
|
||||
bool done = false;
|
||||
bool found = false;
|
||||
std::string object_serial;
|
||||
};
|
||||
|
||||
std::string LookupPipeWireTargetObjectSerial(pw_core* core,
|
||||
pw_thread_loop* loop,
|
||||
uint32_t node_id) {
|
||||
const PipeWireDynamicApi* pipewire = GetPipeWireApi();
|
||||
if (!pipewire || !core || !loop || node_id == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
PipeWireTargetLookupState state;
|
||||
state.pipewire = pipewire;
|
||||
state.loop = loop;
|
||||
state.target_node_id = node_id;
|
||||
|
||||
pw_registry* registry = pw_core_get_registry(core, PW_VERSION_REGISTRY, 0);
|
||||
if (!registry) {
|
||||
return "";
|
||||
}
|
||||
|
||||
spa_hook registry_listener{};
|
||||
spa_hook core_listener{};
|
||||
|
||||
pw_registry_events registry_events{};
|
||||
registry_events.version = PW_VERSION_REGISTRY_EVENTS;
|
||||
registry_events.global = [](void* userdata, uint32_t id, uint32_t permissions,
|
||||
const char* type, uint32_t version,
|
||||
const spa_dict* props) {
|
||||
(void)permissions;
|
||||
(void)version;
|
||||
auto* state = static_cast<PipeWireTargetLookupState*>(userdata);
|
||||
if (!state || !props || id != state->target_node_id || !type) {
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, PW_TYPE_INTERFACE_Node) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char* object_serial = spa_dict_lookup(props, PW_KEY_OBJECT_SERIAL);
|
||||
if (!object_serial || object_serial[0] == '\0') {
|
||||
object_serial = spa_dict_lookup(props, "object.serial");
|
||||
}
|
||||
if (!object_serial || object_serial[0] == '\0') {
|
||||
return;
|
||||
}
|
||||
|
||||
state->object_serial = object_serial;
|
||||
state->found = true;
|
||||
};
|
||||
|
||||
pw_core_events core_events{};
|
||||
core_events.version = PW_VERSION_CORE_EVENTS;
|
||||
core_events.done = [](void* userdata, uint32_t id, int seq) {
|
||||
auto* state = static_cast<PipeWireTargetLookupState*>(userdata);
|
||||
if (!state || id != PW_ID_CORE || seq != state->sync_seq) {
|
||||
return;
|
||||
}
|
||||
state->done = true;
|
||||
state->pipewire->thread_loop_signal(state->loop, false);
|
||||
};
|
||||
core_events.error = [](void* userdata, uint32_t id, int seq, int res,
|
||||
const char* message) {
|
||||
(void)id;
|
||||
(void)seq;
|
||||
(void)res;
|
||||
auto* state = static_cast<PipeWireTargetLookupState*>(userdata);
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
LOG_WARN("PipeWire registry lookup error: {}",
|
||||
message ? message : "unknown");
|
||||
state->done = true;
|
||||
state->pipewire->thread_loop_signal(state->loop, false);
|
||||
};
|
||||
|
||||
pw_registry_add_listener(registry, ®istry_listener, ®istry_events,
|
||||
&state);
|
||||
pw_core_add_listener(core, &core_listener, &core_events, &state);
|
||||
state.sync_seq = pw_core_sync(core, PW_ID_CORE, 0);
|
||||
|
||||
while (!state.done) {
|
||||
pipewire->thread_loop_wait(loop);
|
||||
}
|
||||
|
||||
spa_hook_remove(®istry_listener);
|
||||
spa_hook_remove(&core_listener);
|
||||
pipewire->proxy_destroy(reinterpret_cast<pw_proxy*>(registry));
|
||||
return state.found ? state.object_serial : "";
|
||||
}
|
||||
|
||||
int BytesPerPixel(uint32_t spa_format) {
|
||||
switch (spa_format) {
|
||||
case SPA_VIDEO_FORMAT_BGRx:
|
||||
case SPA_VIDEO_FORMAT_BGRA:
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBx
|
||||
case SPA_VIDEO_FORMAT_RGBx:
|
||||
#endif
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBA
|
||||
case SPA_VIDEO_FORMAT_RGBA:
|
||||
#endif
|
||||
return 4;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ScreenCapturerWayland::EnsurePipeWireRuntimeAvailable() const {
|
||||
return GetPipeWireApi() != nullptr;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::SetupPipeWireStream(bool relaxed_connect,
|
||||
PipeWireConnectMode mode) {
|
||||
const PipeWireDynamicApi* pipewire = GetPipeWireApi();
|
||||
if (!pipewire) {
|
||||
LOG_ERROR("PipeWire 0.3 runtime library is unavailable");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pipewire_fd_ < 0 || pipewire_node_id_ == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!pipewire_initialized_) {
|
||||
pipewire->init(nullptr, nullptr);
|
||||
pipewire_initialized_ = true;
|
||||
}
|
||||
|
||||
pw_thread_loop_ =
|
||||
pipewire->thread_loop_new("crossdesk-wayland-capture", nullptr);
|
||||
if (!pw_thread_loop_) {
|
||||
LOG_ERROR("Failed to create PipeWire thread loop");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pipewire->thread_loop_start(pw_thread_loop_) < 0) {
|
||||
LOG_ERROR("Failed to start PipeWire thread loop");
|
||||
CleanupPipeWire();
|
||||
return false;
|
||||
}
|
||||
pipewire_thread_loop_started_ = true;
|
||||
|
||||
pipewire->thread_loop_lock(pw_thread_loop_);
|
||||
|
||||
pw_context_ = pipewire->context_new(
|
||||
pipewire->thread_loop_get_loop(pw_thread_loop_), nullptr, 0);
|
||||
if (!pw_context_) {
|
||||
LOG_ERROR("Failed to create PipeWire context");
|
||||
pipewire->thread_loop_unlock(pw_thread_loop_);
|
||||
CleanupPipeWire();
|
||||
return false;
|
||||
}
|
||||
|
||||
pw_core_ =
|
||||
pipewire->context_connect_fd(pw_context_, pipewire_fd_, nullptr, 0);
|
||||
if (!pw_core_) {
|
||||
LOG_ERROR("Failed to connect to PipeWire remote");
|
||||
pipewire->thread_loop_unlock(pw_thread_loop_);
|
||||
CleanupPipeWire();
|
||||
return false;
|
||||
}
|
||||
pipewire_fd_ = -1;
|
||||
|
||||
pw_properties* stream_props = pipewire->properties_new(
|
||||
PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture",
|
||||
PW_KEY_MEDIA_ROLE, "Screen", nullptr);
|
||||
if (!stream_props) {
|
||||
LOG_ERROR("Failed to allocate PipeWire stream properties");
|
||||
pipewire->thread_loop_unlock(pw_thread_loop_);
|
||||
CleanupPipeWire();
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string target_object_serial;
|
||||
if (mode == PipeWireConnectMode::kTargetObject) {
|
||||
target_object_serial = LookupPipeWireTargetObjectSerial(
|
||||
pw_core_, pw_thread_loop_, pipewire_node_id_);
|
||||
if (!target_object_serial.empty()) {
|
||||
pipewire->properties_set(stream_props, PW_KEY_TARGET_OBJECT,
|
||||
target_object_serial.c_str());
|
||||
LOG_INFO("PipeWire target object serial for node {} is {}",
|
||||
pipewire_node_id_, target_object_serial);
|
||||
} else {
|
||||
LOG_WARN(
|
||||
"PipeWire target object serial lookup failed for node {}, "
|
||||
"falling back to direct target id in target-object mode",
|
||||
pipewire_node_id_);
|
||||
}
|
||||
}
|
||||
|
||||
pw_stream_ =
|
||||
pipewire->stream_new(pw_core_, "CrossDesk Wayland Capture", stream_props);
|
||||
if (!pw_stream_) {
|
||||
LOG_ERROR("Failed to create PipeWire stream");
|
||||
pipewire->thread_loop_unlock(pw_thread_loop_);
|
||||
CleanupPipeWire();
|
||||
return false;
|
||||
}
|
||||
|
||||
auto* listener = new spa_hook();
|
||||
stream_listener_ = listener;
|
||||
|
||||
static const pw_stream_events stream_events = [] {
|
||||
pw_stream_events events{};
|
||||
events.version = PW_VERSION_STREAM_EVENTS;
|
||||
events.state_changed = [](void* userdata, enum pw_stream_state old_state,
|
||||
enum pw_stream_state state,
|
||||
const char* error_message) {
|
||||
auto* self = static_cast<ScreenCapturerWayland*>(userdata);
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == PW_STREAM_STATE_ERROR) {
|
||||
LOG_ERROR("PipeWire stream error: {}",
|
||||
error_message ? error_message : "unknown");
|
||||
self->running_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const PipeWireDynamicApi* pipewire = GetPipeWireApi();
|
||||
LOG_INFO(
|
||||
"PipeWire stream state: {} -> {}",
|
||||
pipewire ? pipewire->stream_state_as_string(old_state) : "unknown",
|
||||
pipewire ? pipewire->stream_state_as_string(state) : "unknown");
|
||||
};
|
||||
events.param_changed = [](void* userdata, uint32_t id,
|
||||
const struct spa_pod* param) {
|
||||
auto* self = static_cast<ScreenCapturerWayland*>(userdata);
|
||||
if (!self || id != SPA_PARAM_Format || !param) {
|
||||
return;
|
||||
}
|
||||
|
||||
spa_video_info_raw info{};
|
||||
if (spa_format_video_raw_parse(param, &info) < 0) {
|
||||
LOG_ERROR("Failed to parse PipeWire video format");
|
||||
return;
|
||||
}
|
||||
|
||||
self->spa_video_format_ = info.format;
|
||||
self->frame_width_ = static_cast<int>(info.size.width);
|
||||
self->frame_height_ = static_cast<int>(info.size.height);
|
||||
self->frame_stride_ = static_cast<int>(info.size.width) * 4;
|
||||
|
||||
bool supported_format =
|
||||
(self->spa_video_format_ == SPA_VIDEO_FORMAT_BGRx) ||
|
||||
(self->spa_video_format_ == SPA_VIDEO_FORMAT_BGRA);
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBx
|
||||
supported_format = supported_format ||
|
||||
(self->spa_video_format_ == SPA_VIDEO_FORMAT_RGBx);
|
||||
#endif
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBA
|
||||
supported_format = supported_format ||
|
||||
(self->spa_video_format_ == SPA_VIDEO_FORMAT_RGBA);
|
||||
#endif
|
||||
if (!supported_format) {
|
||||
LOG_ERROR("Unsupported PipeWire pixel format: {}",
|
||||
PipeWireFormatName(self->spa_video_format_));
|
||||
self->running_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const int bytes_per_pixel = BytesPerPixel(self->spa_video_format_);
|
||||
if (bytes_per_pixel <= 0 || self->frame_width_ <= 0 ||
|
||||
self->frame_height_ <= 0) {
|
||||
LOG_ERROR("Invalid PipeWire frame layout: format={}, size={}x{}",
|
||||
PipeWireFormatName(self->spa_video_format_),
|
||||
self->frame_width_, self->frame_height_);
|
||||
self->running_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
self->frame_stride_ = self->frame_width_ * bytes_per_pixel;
|
||||
|
||||
uint8_t buffer[1024];
|
||||
spa_pod_builder builder = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
||||
const spa_pod* params[2];
|
||||
uint32_t param_count = 0;
|
||||
|
||||
params[param_count++] =
|
||||
reinterpret_cast<const spa_pod*>(spa_pod_builder_add_object(
|
||||
&builder, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
|
||||
CROSSDESK_SPA_PARAM_BUFFERS_BUFFERS,
|
||||
SPA_POD_CHOICE_RANGE_Int(8, 4, 16),
|
||||
CROSSDESK_SPA_PARAM_BUFFERS_BLOCKS, SPA_POD_Int(1),
|
||||
CROSSDESK_SPA_PARAM_BUFFERS_SIZE,
|
||||
SPA_POD_CHOICE_RANGE_Int(
|
||||
self->frame_stride_ * self->frame_height_,
|
||||
self->frame_stride_ * self->frame_height_,
|
||||
self->frame_stride_ * self->frame_height_),
|
||||
CROSSDESK_SPA_PARAM_BUFFERS_STRIDE,
|
||||
SPA_POD_CHOICE_RANGE_Int(self->frame_stride_, self->frame_stride_,
|
||||
self->frame_stride_)));
|
||||
|
||||
params[param_count++] =
|
||||
reinterpret_cast<const spa_pod*>(spa_pod_builder_add_object(
|
||||
&builder, SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
|
||||
CROSSDESK_SPA_PARAM_META_TYPE, SPA_POD_Id(SPA_META_Header),
|
||||
CROSSDESK_SPA_PARAM_META_SIZE,
|
||||
SPA_POD_Int(sizeof(struct spa_meta_header))));
|
||||
|
||||
if (self->pw_stream_) {
|
||||
const PipeWireDynamicApi* pipewire = GetPipeWireApi();
|
||||
if (pipewire) {
|
||||
pipewire->stream_update_params(self->pw_stream_, params, param_count);
|
||||
}
|
||||
}
|
||||
self->pipewire_format_ready_.store(true);
|
||||
|
||||
int pointer_width =
|
||||
self->logical_width_ > 0 ? self->logical_width_ : self->frame_width_;
|
||||
int pointer_height = self->logical_height_ > 0 ? self->logical_height_
|
||||
: self->frame_height_;
|
||||
double observed_scale_x = pointer_width > 0
|
||||
? static_cast<double>(self->frame_width_) /
|
||||
static_cast<double>(pointer_width)
|
||||
: 1.0;
|
||||
double observed_scale_y = pointer_height > 0
|
||||
? static_cast<double>(self->frame_height_) /
|
||||
static_cast<double>(pointer_height)
|
||||
: 1.0;
|
||||
double snapped_scale = 1.0;
|
||||
bool derived_pointer_space = false;
|
||||
|
||||
if (!self->portal_has_logical_size_ && self->portal_stream_width_ > 0 &&
|
||||
self->portal_stream_height_ > 0 && self->frame_width_ > 0 &&
|
||||
self->frame_height_ > 0) {
|
||||
const double raw_scale_x =
|
||||
static_cast<double>(self->frame_width_) /
|
||||
static_cast<double>(self->portal_stream_width_);
|
||||
const double raw_scale_y =
|
||||
static_cast<double>(self->frame_height_) /
|
||||
static_cast<double>(self->portal_stream_height_);
|
||||
const double average_scale = (raw_scale_x + raw_scale_y) * 0.5;
|
||||
snapped_scale = SnapLikelyFractionalScale(average_scale);
|
||||
|
||||
const bool scales_are_consistent =
|
||||
std::abs(raw_scale_x - raw_scale_y) <= 0.05;
|
||||
const bool scale_was_snapped =
|
||||
std::abs(snapped_scale - average_scale) <= 0.08;
|
||||
if (scales_are_consistent && scale_was_snapped &&
|
||||
snapped_scale > 1.05) {
|
||||
pointer_width =
|
||||
std::max(1, static_cast<int>(std::floor(
|
||||
static_cast<double>(self->portal_stream_width_) *
|
||||
snapped_scale +
|
||||
1e-6)));
|
||||
pointer_height =
|
||||
std::max(1, static_cast<int>(std::floor(
|
||||
static_cast<double>(self->portal_stream_height_) *
|
||||
snapped_scale +
|
||||
1e-6)));
|
||||
observed_scale_x = pointer_width > 0
|
||||
? static_cast<double>(self->frame_width_) /
|
||||
static_cast<double>(pointer_width)
|
||||
: 1.0;
|
||||
observed_scale_y = pointer_height > 0
|
||||
? static_cast<double>(self->frame_height_) /
|
||||
static_cast<double>(pointer_height)
|
||||
: 1.0;
|
||||
derived_pointer_space = true;
|
||||
}
|
||||
}
|
||||
|
||||
self->UpdateDisplayGeometry(pointer_width, pointer_height);
|
||||
if (derived_pointer_space) {
|
||||
LOG_INFO(
|
||||
"PipeWire video format: {}, {}x{} stride={} (pointer space {}x{}, "
|
||||
"derived from portal stream {}x{} with compositor scale {:.4f}, "
|
||||
"effective scale {:.4f}x{:.4f})",
|
||||
PipeWireFormatName(self->spa_video_format_), self->frame_width_,
|
||||
self->frame_height_, self->frame_stride_, pointer_width,
|
||||
pointer_height, self->portal_stream_width_,
|
||||
self->portal_stream_height_, snapped_scale, observed_scale_x,
|
||||
observed_scale_y);
|
||||
} else {
|
||||
LOG_INFO(
|
||||
"PipeWire video format: {}, {}x{} stride={} (pointer space {}x{}, "
|
||||
"scale {:.4f}x{:.4f})",
|
||||
PipeWireFormatName(self->spa_video_format_), self->frame_width_,
|
||||
self->frame_height_, self->frame_stride_, pointer_width,
|
||||
pointer_height, observed_scale_x, observed_scale_y);
|
||||
}
|
||||
};
|
||||
events.process = [](void* userdata) {
|
||||
auto* self = static_cast<ScreenCapturerWayland*>(userdata);
|
||||
if (self) {
|
||||
self->HandlePipeWireBuffer();
|
||||
}
|
||||
};
|
||||
return events;
|
||||
}();
|
||||
|
||||
pipewire->stream_add_listener(pw_stream_, listener, &stream_events, this);
|
||||
pipewire_format_ready_.store(false);
|
||||
pipewire_stream_start_ms_.store(NowMs());
|
||||
pipewire_last_frame_ms_.store(0);
|
||||
|
||||
uint8_t buffer[4096];
|
||||
spa_pod_builder builder = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
||||
const spa_pod* params[8];
|
||||
int param_count = 0;
|
||||
const spa_rectangle fixed_size{
|
||||
static_cast<uint32_t>(logical_width_ > 0 ? logical_width_
|
||||
: kFallbackWidth),
|
||||
static_cast<uint32_t>(logical_height_ > 0 ? logical_height_
|
||||
: kFallbackHeight)};
|
||||
const spa_rectangle min_size{1u, 1u};
|
||||
const spa_rectangle max_size{16384u, 16384u};
|
||||
|
||||
if (!relaxed_connect) {
|
||||
auto add_format_param = [&](uint32_t spa_format) {
|
||||
if (param_count >= static_cast<int>(sizeof(params) / sizeof(params[0]))) {
|
||||
return;
|
||||
}
|
||||
params[param_count++] =
|
||||
reinterpret_cast<const spa_pod*>(spa_pod_builder_add_object(
|
||||
&builder, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
|
||||
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
|
||||
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
|
||||
SPA_FORMAT_VIDEO_format, SPA_POD_Id(spa_format),
|
||||
SPA_FORMAT_VIDEO_size,
|
||||
SPA_POD_CHOICE_RANGE_Rectangle(&fixed_size, &min_size,
|
||||
&max_size)));
|
||||
};
|
||||
|
||||
add_format_param(SPA_VIDEO_FORMAT_BGRx);
|
||||
add_format_param(SPA_VIDEO_FORMAT_BGRA);
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBx
|
||||
add_format_param(SPA_VIDEO_FORMAT_RGBx);
|
||||
#endif
|
||||
#ifdef SPA_VIDEO_FORMAT_RGBA
|
||||
add_format_param(SPA_VIDEO_FORMAT_RGBA);
|
||||
#endif
|
||||
|
||||
if (param_count == 0) {
|
||||
LOG_ERROR("No valid PipeWire format params were built");
|
||||
pipewire->thread_loop_unlock(pw_thread_loop_);
|
||||
CleanupPipeWire();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
LOG_INFO("PipeWire stream using relaxed format negotiation");
|
||||
}
|
||||
|
||||
uint32_t target_id = PW_ID_ANY;
|
||||
if (mode == PipeWireConnectMode::kNodeId ||
|
||||
(mode == PipeWireConnectMode::kTargetObject &&
|
||||
target_object_serial.empty())) {
|
||||
target_id = pipewire_node_id_;
|
||||
}
|
||||
LOG_INFO(
|
||||
"PipeWire connecting stream: mode={}, node_id={}, target_id={}, "
|
||||
"target_object_serial={}, relaxed_connect={}, param_count={}, "
|
||||
"requested_size={}x{}",
|
||||
PipeWireConnectModeName(mode), pipewire_node_id_, target_id,
|
||||
target_object_serial.empty() ? "none" : target_object_serial.c_str(),
|
||||
relaxed_connect, param_count, fixed_size.width, fixed_size.height);
|
||||
const int ret = pipewire->stream_connect(
|
||||
pw_stream_, PW_DIRECTION_INPUT, target_id,
|
||||
static_cast<pw_stream_flags>(PW_STREAM_FLAG_AUTOCONNECT |
|
||||
PW_STREAM_FLAG_MAP_BUFFERS),
|
||||
param_count > 0 ? params : nullptr, static_cast<uint32_t>(param_count));
|
||||
pipewire->thread_loop_unlock(pw_thread_loop_);
|
||||
|
||||
if (ret < 0) {
|
||||
LOG_ERROR("pw_stream_connect failed: {}", spa_strerror(ret));
|
||||
CleanupPipeWire();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::CleanupPipeWire() {
|
||||
const PipeWireDynamicApi* pipewire = GetPipeWireApi();
|
||||
const bool need_lock =
|
||||
pipewire && pw_thread_loop_ &&
|
||||
(pw_stream_ != nullptr || pw_core_ != nullptr || pw_context_ != nullptr);
|
||||
if (need_lock) {
|
||||
pipewire->thread_loop_lock(pw_thread_loop_);
|
||||
}
|
||||
|
||||
if (pw_stream_ && pipewire) {
|
||||
pipewire->stream_set_active(pw_stream_, false);
|
||||
pipewire->stream_disconnect(pw_stream_);
|
||||
}
|
||||
|
||||
if (stream_listener_) {
|
||||
spa_hook_remove(static_cast<spa_hook*>(stream_listener_));
|
||||
delete static_cast<spa_hook*>(stream_listener_);
|
||||
stream_listener_ = nullptr;
|
||||
}
|
||||
|
||||
if (pw_stream_ && pipewire) {
|
||||
pipewire->stream_destroy(pw_stream_);
|
||||
}
|
||||
pw_stream_ = nullptr;
|
||||
|
||||
if (pw_core_ && pipewire) {
|
||||
pipewire->core_disconnect(pw_core_);
|
||||
}
|
||||
pw_core_ = nullptr;
|
||||
|
||||
if (pw_context_ && pipewire) {
|
||||
pipewire->context_destroy(pw_context_);
|
||||
}
|
||||
pw_context_ = nullptr;
|
||||
|
||||
if (need_lock) {
|
||||
pipewire->thread_loop_unlock(pw_thread_loop_);
|
||||
}
|
||||
|
||||
if (pw_thread_loop_ && pipewire) {
|
||||
if (pipewire_thread_loop_started_) {
|
||||
pipewire->thread_loop_stop(pw_thread_loop_);
|
||||
pipewire_thread_loop_started_ = false;
|
||||
}
|
||||
pipewire->thread_loop_destroy(pw_thread_loop_);
|
||||
}
|
||||
pw_thread_loop_ = nullptr;
|
||||
pipewire_thread_loop_started_ = false;
|
||||
|
||||
if (pipewire_fd_ >= 0) {
|
||||
close(pipewire_fd_);
|
||||
pipewire_fd_ = -1;
|
||||
}
|
||||
|
||||
pipewire_format_ready_.store(false);
|
||||
pipewire_stream_start_ms_.store(0);
|
||||
pipewire_last_frame_ms_.store(0);
|
||||
|
||||
if (pipewire_initialized_ && pipewire) {
|
||||
pipewire->deinit();
|
||||
}
|
||||
pipewire_initialized_ = false;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::HandlePipeWireBuffer() {
|
||||
const PipeWireDynamicApi* pipewire = GetPipeWireApi();
|
||||
if (!pw_stream_ || !pipewire) {
|
||||
return;
|
||||
}
|
||||
|
||||
pw_buffer* buffer = pipewire->stream_dequeue_buffer(pw_stream_);
|
||||
if (!buffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto requeue = [&]() { pipewire->stream_queue_buffer(pw_stream_, buffer); };
|
||||
|
||||
if (paused_) {
|
||||
requeue();
|
||||
return;
|
||||
}
|
||||
|
||||
spa_buffer* spa_buffer = buffer->buffer;
|
||||
if (!spa_buffer || spa_buffer->n_datas == 0 || !spa_buffer->datas[0].data) {
|
||||
requeue();
|
||||
return;
|
||||
}
|
||||
|
||||
const spa_data& data = spa_buffer->datas[0];
|
||||
if (!data.chunk) {
|
||||
requeue();
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame_width_ <= 1 || frame_height_ <= 1) {
|
||||
requeue();
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t* src = static_cast<uint8_t*>(data.data);
|
||||
src += data.chunk->offset;
|
||||
|
||||
int stride = frame_stride_;
|
||||
if (data.chunk->stride > 0) {
|
||||
stride = data.chunk->stride;
|
||||
} else if (stride <= 0) {
|
||||
stride = frame_width_ * 4;
|
||||
}
|
||||
|
||||
int even_width = frame_width_ & ~1;
|
||||
int even_height = frame_height_ & ~1;
|
||||
if (even_width <= 0 || even_height <= 0) {
|
||||
requeue();
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t y_size = static_cast<size_t>(even_width) * even_height;
|
||||
const size_t uv_size = y_size / 2;
|
||||
if (y_plane_.size() != y_size) {
|
||||
y_plane_.resize(y_size);
|
||||
}
|
||||
if (uv_plane_.size() != uv_size) {
|
||||
uv_plane_.resize(uv_size);
|
||||
}
|
||||
|
||||
libyuv::ARGBToNV12(src, stride, y_plane_.data(), even_width, uv_plane_.data(),
|
||||
even_width, even_width, even_height);
|
||||
|
||||
std::vector<uint8_t> nv12;
|
||||
nv12.reserve(y_plane_.size() + uv_plane_.size());
|
||||
nv12.insert(nv12.end(), y_plane_.begin(), y_plane_.end());
|
||||
nv12.insert(nv12.end(), uv_plane_.begin(), uv_plane_.end());
|
||||
|
||||
if (callback_) {
|
||||
callback_(nv12.data(), static_cast<int>(nv12.size()), even_width,
|
||||
even_height, display_name_.c_str());
|
||||
}
|
||||
pipewire_last_frame_ms_.store(NowMs());
|
||||
|
||||
requeue();
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::UpdateDisplayGeometry(int width, int height) {
|
||||
if (width <= 0 || height <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
void* stream_handle =
|
||||
reinterpret_cast<void*>(static_cast<uintptr_t>(pipewire_node_id_));
|
||||
|
||||
if (display_info_list_.empty()) {
|
||||
display_info_list_.push_back(
|
||||
DisplayInfo(stream_handle, display_name_, true, 0, 0, width, height));
|
||||
return;
|
||||
}
|
||||
|
||||
auto& display = display_info_list_[0];
|
||||
display.handle = stream_handle;
|
||||
display.left = 0;
|
||||
display.top = 0;
|
||||
display.right = width;
|
||||
display.bottom = height;
|
||||
display.width = width;
|
||||
display.height = height;
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,824 @@
|
||||
#include "screen_capturer_wayland.h"
|
||||
#include "screen_capturer_wayland_build.h"
|
||||
#include "wayland_portal_shared.h"
|
||||
|
||||
#if CROSSDESK_WAYLAND_BUILD_ENABLED
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* kPortalBusName = "org.freedesktop.portal.Desktop";
|
||||
constexpr const char* kPortalObjectPath = "/org/freedesktop/portal/desktop";
|
||||
constexpr const char* kPortalRemoteDesktopInterface =
|
||||
"org.freedesktop.portal.RemoteDesktop";
|
||||
constexpr const char* kPortalScreenCastInterface =
|
||||
"org.freedesktop.portal.ScreenCast";
|
||||
constexpr const char* kPortalRequestInterface =
|
||||
"org.freedesktop.portal.Request";
|
||||
constexpr const char* kPortalSessionInterface =
|
||||
"org.freedesktop.portal.Session";
|
||||
constexpr const char* kPortalRequestPathPrefix =
|
||||
"/org/freedesktop/portal/desktop/request/";
|
||||
constexpr const char* kPortalSessionPathPrefix =
|
||||
"/org/freedesktop/portal/desktop/session/";
|
||||
|
||||
constexpr uint32_t kScreenCastSourceMonitor = 1u;
|
||||
constexpr uint32_t kCursorModeHidden = 1u;
|
||||
constexpr uint32_t kCursorModeEmbedded = 2u;
|
||||
constexpr uint32_t kRemoteDesktopDevicePointer = 2u;
|
||||
|
||||
std::string MakeToken(const char* prefix) {
|
||||
const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
return std::string(prefix) + "_" + std::to_string(now);
|
||||
}
|
||||
|
||||
void LogDbusError(const char* action, DBusError* error) {
|
||||
if (error && dbus_error_is_set(error)) {
|
||||
LOG_ERROR("{} failed: {} ({})", action,
|
||||
error->message ? error->message : "unknown",
|
||||
error->name ? error->name : "unknown");
|
||||
} else {
|
||||
LOG_ERROR("{} failed", action);
|
||||
}
|
||||
}
|
||||
|
||||
void AppendDictEntryString(DBusMessageIter* dict, const char* key,
|
||||
const std::string& value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
const char* value_cstr = value.c_str();
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "s", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &value_cstr);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
void AppendDictEntryUint32(DBusMessageIter* dict, const char* key,
|
||||
uint32_t value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "u", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_UINT32, &value);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
void AppendDictEntryBool(DBusMessageIter* dict, const char* key, bool value) {
|
||||
DBusMessageIter entry;
|
||||
DBusMessageIter variant;
|
||||
const char* key_cstr = key;
|
||||
dbus_bool_t bool_value = value ? TRUE : FALSE;
|
||||
|
||||
dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry);
|
||||
dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr);
|
||||
dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "b", &variant);
|
||||
dbus_message_iter_append_basic(&variant, DBUS_TYPE_BOOLEAN, &bool_value);
|
||||
dbus_message_iter_close_container(&entry, &variant);
|
||||
dbus_message_iter_close_container(dict, &entry);
|
||||
}
|
||||
|
||||
bool ReadIntLike(DBusMessageIter* iter, int* value) {
|
||||
if (!iter || !value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int type = dbus_message_iter_get_arg_type(iter);
|
||||
if (type == DBUS_TYPE_INT32) {
|
||||
int32_t temp = 0;
|
||||
dbus_message_iter_get_basic(iter, &temp);
|
||||
*value = static_cast<int>(temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (type == DBUS_TYPE_UINT32) {
|
||||
uint32_t temp = 0;
|
||||
dbus_message_iter_get_basic(iter, &temp);
|
||||
*value = static_cast<int>(temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReadPathLikeVariant(DBusMessageIter* variant, std::string* value) {
|
||||
if (!variant || !value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const int type = dbus_message_iter_get_arg_type(variant);
|
||||
if (type == DBUS_TYPE_OBJECT_PATH || type == DBUS_TYPE_STRING) {
|
||||
const char* temp = nullptr;
|
||||
dbus_message_iter_get_basic(variant, &temp);
|
||||
if (temp && temp[0] != '\0') {
|
||||
*value = temp;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string BuildSessionHandleFromRequestPath(
|
||||
const std::string& request_path, const std::string& session_handle_token) {
|
||||
if (request_path.rfind(kPortalRequestPathPrefix, 0) != 0 ||
|
||||
session_handle_token.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const size_t sender_start = strlen(kPortalRequestPathPrefix);
|
||||
const size_t token_sep = request_path.find('/', sender_start);
|
||||
if (token_sep == std::string::npos || token_sep <= sender_start) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const std::string sender =
|
||||
request_path.substr(sender_start, token_sep - sender_start);
|
||||
if (sender.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return std::string(kPortalSessionPathPrefix) + sender + "/" +
|
||||
session_handle_token;
|
||||
}
|
||||
|
||||
struct PortalResponseState {
|
||||
std::string request_path;
|
||||
bool received = false;
|
||||
DBusMessage* message = nullptr;
|
||||
};
|
||||
|
||||
DBusHandlerResult HandlePortalResponseSignal(DBusConnection* connection,
|
||||
DBusMessage* message,
|
||||
void* user_data) {
|
||||
auto* state = static_cast<PortalResponseState*>(user_data);
|
||||
if (!state || !message) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
if (!dbus_message_is_signal(message, kPortalRequestInterface, "Response")) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
const char* path = dbus_message_get_path(message);
|
||||
if (!path || state->request_path != path) {
|
||||
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
||||
}
|
||||
|
||||
if (state->message) {
|
||||
dbus_message_unref(state->message);
|
||||
state->message = nullptr;
|
||||
}
|
||||
|
||||
state->message = dbus_message_ref(message);
|
||||
state->received = true;
|
||||
return DBUS_HANDLER_RESULT_HANDLED;
|
||||
}
|
||||
|
||||
DBusMessage* WaitForPortalResponse(DBusConnection* connection,
|
||||
const std::string& request_path,
|
||||
const std::atomic<bool>& running,
|
||||
int timeout_ms = 120000) {
|
||||
if (!connection || request_path.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PortalResponseState state;
|
||||
state.request_path = request_path;
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
const std::string match_rule =
|
||||
"type='signal',interface='" + std::string(kPortalRequestInterface) +
|
||||
"',member='Response',path='" + request_path + "'";
|
||||
dbus_bus_add_match(connection, match_rule.c_str(), &error);
|
||||
if (dbus_error_is_set(&error)) {
|
||||
LogDbusError("dbus_bus_add_match", &error);
|
||||
dbus_error_free(&error);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
dbus_connection_add_filter(connection, HandlePortalResponseSignal, &state,
|
||||
nullptr);
|
||||
|
||||
auto deadline =
|
||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
while (running.load() && !state.received &&
|
||||
std::chrono::steady_clock::now() < deadline) {
|
||||
dbus_connection_read_write(connection, 100);
|
||||
while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS) {
|
||||
}
|
||||
}
|
||||
|
||||
dbus_connection_remove_filter(connection, HandlePortalResponseSignal, &state);
|
||||
|
||||
DBusError remove_error;
|
||||
dbus_error_init(&remove_error);
|
||||
dbus_bus_remove_match(connection, match_rule.c_str(), &remove_error);
|
||||
if (dbus_error_is_set(&remove_error)) {
|
||||
dbus_error_free(&remove_error);
|
||||
}
|
||||
|
||||
return state.message;
|
||||
}
|
||||
|
||||
bool ExtractRequestPath(DBusMessage* reply, std::string* request_path) {
|
||||
if (!reply || !request_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* path = nullptr;
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
const dbus_bool_t ok = dbus_message_get_args(
|
||||
reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
|
||||
if (!ok || !path) {
|
||||
LogDbusError("dbus_message_get_args(request_path)", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
*request_path = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExtractPortalResponse(DBusMessage* message, uint32_t* response_code,
|
||||
DBusMessageIter* results_array) {
|
||||
if (!message || !response_code || !results_array) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter iter;
|
||||
if (!dbus_message_iter_init(message, &iter) ||
|
||||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_message_iter_get_basic(&iter, response_code);
|
||||
if (!dbus_message_iter_next(&iter) ||
|
||||
dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*results_array = iter;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SendPortalRequestAndHandleResponse(
|
||||
DBusConnection* connection, const char* interface_name,
|
||||
const char* method_name, const char* action_name,
|
||||
const std::function<bool(DBusMessage*)>& append_message_args,
|
||||
const std::atomic<bool>& running,
|
||||
const std::function<bool(uint32_t, DBusMessageIter*)>& handle_results,
|
||||
std::string* request_path_out = nullptr) {
|
||||
if (!connection || !interface_name || interface_name[0] == '\0' ||
|
||||
!method_name || method_name[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, kPortalObjectPath, interface_name, method_name);
|
||||
if (!message) {
|
||||
LOG_ERROR("Failed to allocate {} message", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (append_message_args && !append_message_args(message)) {
|
||||
dbus_message_unref(message);
|
||||
LOG_ERROR("{} arguments are malformed", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
|
||||
connection, message, -1, &error);
|
||||
dbus_message_unref(message);
|
||||
if (!reply) {
|
||||
LogDbusError(action_name ? action_name : method_name, &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string request_path;
|
||||
const bool got_request_path = ExtractRequestPath(reply, &request_path);
|
||||
dbus_message_unref(reply);
|
||||
if (!got_request_path) {
|
||||
return false;
|
||||
}
|
||||
if (request_path_out) {
|
||||
*request_path_out = request_path;
|
||||
}
|
||||
|
||||
DBusMessage* response =
|
||||
WaitForPortalResponse(connection, request_path, running);
|
||||
if (!response) {
|
||||
LOG_ERROR("Timed out waiting for {} response", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t response_code = 1;
|
||||
DBusMessageIter results;
|
||||
const bool parsed = ExtractPortalResponse(response, &response_code, &results);
|
||||
if (!parsed) {
|
||||
dbus_message_unref(response);
|
||||
LOG_ERROR("{} response was malformed", method_name);
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool ok = handle_results ? handle_results(response_code, &results)
|
||||
: (response_code == 0);
|
||||
dbus_message_unref(response);
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool ScreenCapturerWayland::CheckPortalAvailability() const {
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
DBusConnection* connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
|
||||
if (!connection) {
|
||||
LogDbusError("dbus_bus_get", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
const dbus_bool_t has_owner =
|
||||
dbus_bus_name_has_owner(connection, kPortalBusName, &error);
|
||||
if (dbus_error_is_set(&error)) {
|
||||
LogDbusError("dbus_bus_name_has_owner", &error);
|
||||
dbus_error_free(&error);
|
||||
dbus_connection_unref(connection);
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_connection_unref(connection);
|
||||
return has_owner == TRUE;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::ConnectSessionBus() {
|
||||
if (dbus_connection_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
|
||||
dbus_connection_ = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
|
||||
if (!dbus_connection_) {
|
||||
LogDbusError("dbus_bus_get_private", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
dbus_connection_set_exit_on_disconnect(dbus_connection_, FALSE);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::CreatePortalSession() {
|
||||
if (!dbus_connection_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string session_handle_token = MakeToken("crossdesk_session");
|
||||
std::string request_path;
|
||||
const bool ok = SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "CreateSession",
|
||||
"CreateSession",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryString(&options, "session_handle_token",
|
||||
session_handle_token);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[&](uint32_t response_code, DBusMessageIter* results) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("CreateSession was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter dict;
|
||||
dbus_message_iter_recurse(results, &dict);
|
||||
while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter entry;
|
||||
dbus_message_iter_recurse(&dict, &entry);
|
||||
|
||||
const char* key = nullptr;
|
||||
dbus_message_iter_get_basic(&entry, &key);
|
||||
if (key && dbus_message_iter_next(&entry) &&
|
||||
dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT &&
|
||||
strcmp(key, "session_handle") == 0) {
|
||||
DBusMessageIter variant;
|
||||
std::string parsed_handle;
|
||||
dbus_message_iter_recurse(&entry, &variant);
|
||||
if (ReadPathLikeVariant(&variant, &parsed_handle) &&
|
||||
!parsed_handle.empty()) {
|
||||
session_handle_ = parsed_handle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
dbus_message_iter_next(&dict);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
&request_path);
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (session_handle_.empty()) {
|
||||
const std::string fallback_handle =
|
||||
BuildSessionHandleFromRequestPath(request_path, session_handle_token);
|
||||
if (!fallback_handle.empty()) {
|
||||
LOG_WARN(
|
||||
"CreateSession response missing session_handle, using derived handle "
|
||||
"{}",
|
||||
fallback_handle);
|
||||
session_handle_ = fallback_handle;
|
||||
}
|
||||
}
|
||||
|
||||
if (session_handle_.empty()) {
|
||||
LOG_ERROR("CreateSession response did not include a session handle");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::SelectPortalSource() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
return SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalScreenCastInterface, "SelectSources",
|
||||
"SelectSources",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryUint32(&options, "types", kScreenCastSourceMonitor);
|
||||
AppendDictEntryBool(&options, "multiple", false);
|
||||
AppendDictEntryUint32(
|
||||
&options, "cursor_mode",
|
||||
show_cursor_ ? kCursorModeEmbedded : kCursorModeHidden);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[](uint32_t response_code, DBusMessageIter*) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("SelectSources was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::SelectPortalDevices() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
return SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "SelectDevices",
|
||||
"SelectDevices",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryUint32(&options, "types", kRemoteDesktopDevicePointer);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[](uint32_t response_code, DBusMessageIter*) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("SelectDevices was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::StartPortalSession() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
const char* parent_window = "";
|
||||
pointer_granted_ = false;
|
||||
const bool ok = SendPortalRequestAndHandleResponse(
|
||||
dbus_connection_, kPortalRemoteDesktopInterface, "Start", "Start",
|
||||
[&](DBusMessage* message) {
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
|
||||
&session_handle);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &parent_window);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
|
||||
&options);
|
||||
AppendDictEntryString(&options, "handle_token",
|
||||
MakeToken("crossdesk_req"));
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
return true;
|
||||
},
|
||||
running_,
|
||||
[&](uint32_t response_code, DBusMessageIter* results) {
|
||||
if (response_code != 0) {
|
||||
LOG_ERROR("Start was denied or malformed, response={}",
|
||||
response_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t granted_devices = 0;
|
||||
DBusMessageIter dict;
|
||||
dbus_message_iter_recurse(results, &dict);
|
||||
while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter entry;
|
||||
dbus_message_iter_recurse(&dict, &entry);
|
||||
|
||||
const char* key = nullptr;
|
||||
dbus_message_iter_get_basic(&entry, &key);
|
||||
if (key && dbus_message_iter_next(&entry) &&
|
||||
dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT) {
|
||||
DBusMessageIter variant;
|
||||
dbus_message_iter_recurse(&entry, &variant);
|
||||
if (strcmp(key, "devices") == 0) {
|
||||
int granted_devices_int = 0;
|
||||
if (ReadIntLike(&variant, &granted_devices_int) &&
|
||||
granted_devices_int >= 0) {
|
||||
granted_devices = static_cast<uint32_t>(granted_devices_int);
|
||||
}
|
||||
} else if (strcmp(key, "streams") == 0) {
|
||||
DBusMessageIter streams;
|
||||
dbus_message_iter_recurse(&variant, &streams);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(&streams) ==
|
||||
DBUS_TYPE_STRUCT) {
|
||||
DBusMessageIter stream;
|
||||
dbus_message_iter_recurse(&streams, &stream);
|
||||
|
||||
if (dbus_message_iter_get_arg_type(&stream) ==
|
||||
DBUS_TYPE_UINT32) {
|
||||
dbus_message_iter_get_basic(&stream, &pipewire_node_id_);
|
||||
}
|
||||
|
||||
if (dbus_message_iter_next(&stream) &&
|
||||
dbus_message_iter_get_arg_type(&stream) ==
|
||||
DBUS_TYPE_ARRAY) {
|
||||
DBusMessageIter props;
|
||||
int stream_width = 0;
|
||||
int stream_height = 0;
|
||||
int logical_width = 0;
|
||||
int logical_height = 0;
|
||||
dbus_message_iter_recurse(&stream, &props);
|
||||
while (dbus_message_iter_get_arg_type(&props) !=
|
||||
DBUS_TYPE_INVALID) {
|
||||
if (dbus_message_iter_get_arg_type(&props) ==
|
||||
DBUS_TYPE_DICT_ENTRY) {
|
||||
DBusMessageIter prop_entry;
|
||||
dbus_message_iter_recurse(&props, &prop_entry);
|
||||
|
||||
const char* prop_key = nullptr;
|
||||
dbus_message_iter_get_basic(&prop_entry, &prop_key);
|
||||
if (prop_key && dbus_message_iter_next(&prop_entry) &&
|
||||
dbus_message_iter_get_arg_type(&prop_entry) ==
|
||||
DBUS_TYPE_VARIANT) {
|
||||
DBusMessageIter prop_variant;
|
||||
dbus_message_iter_recurse(&prop_entry, &prop_variant);
|
||||
if (dbus_message_iter_get_arg_type(&prop_variant) ==
|
||||
DBUS_TYPE_STRUCT) {
|
||||
DBusMessageIter size_iter;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
dbus_message_iter_recurse(&prop_variant,
|
||||
&size_iter);
|
||||
if (ReadIntLike(&size_iter, &width) &&
|
||||
dbus_message_iter_next(&size_iter) &&
|
||||
ReadIntLike(&size_iter, &height)) {
|
||||
if (strcmp(prop_key, "logical_size") == 0) {
|
||||
logical_width = width;
|
||||
logical_height = height;
|
||||
} else if (strcmp(prop_key, "size") == 0) {
|
||||
stream_width = width;
|
||||
stream_height = height;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dbus_message_iter_next(&props);
|
||||
}
|
||||
|
||||
const int picked_width =
|
||||
logical_width > 0 ? logical_width : stream_width;
|
||||
const int picked_height =
|
||||
logical_height > 0 ? logical_height : stream_height;
|
||||
LOG_INFO(
|
||||
"Wayland portal stream geometry: stream_size={}x{}, "
|
||||
"logical_size={}x{}, pointer_space={}x{}",
|
||||
stream_width, stream_height, logical_width,
|
||||
logical_height, picked_width, picked_height);
|
||||
|
||||
portal_stream_width_ = stream_width;
|
||||
portal_stream_height_ = stream_height;
|
||||
portal_has_logical_size_ =
|
||||
logical_width > 0 && logical_height > 0;
|
||||
|
||||
if (logical_width > 0 && logical_height > 0) {
|
||||
logical_width_ = logical_width;
|
||||
logical_height_ = logical_height;
|
||||
UpdateDisplayGeometry(logical_width_, logical_height_);
|
||||
} else if (stream_width > 0 && stream_height > 0) {
|
||||
logical_width_ = stream_width;
|
||||
logical_height_ = stream_height;
|
||||
UpdateDisplayGeometry(logical_width_, logical_height_);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbus_message_iter_next(&dict);
|
||||
}
|
||||
pointer_granted_ = (granted_devices & kRemoteDesktopDevicePointer) != 0;
|
||||
return true;
|
||||
});
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pipewire_node_id_ == 0) {
|
||||
LOG_ERROR("Start response did not include a PipeWire node id");
|
||||
return false;
|
||||
}
|
||||
if (!pointer_granted_) {
|
||||
LOG_ERROR("Start response did not grant pointer control");
|
||||
return false;
|
||||
}
|
||||
|
||||
shared_session_registered_ =
|
||||
PublishSharedWaylandPortalSession(SharedWaylandPortalSessionInfo{
|
||||
dbus_connection_, session_handle_, pipewire_node_id_, logical_width_,
|
||||
logical_height_, pointer_granted_});
|
||||
if (!shared_session_registered_) {
|
||||
LOG_WARN("Failed to publish shared Wayland portal session");
|
||||
}
|
||||
|
||||
LOG_INFO("Wayland screencast ready, node_id={}", pipewire_node_id_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerWayland::OpenPipeWireRemote() {
|
||||
if (!dbus_connection_ || session_handle_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessage* message = dbus_message_new_method_call(
|
||||
kPortalBusName, kPortalObjectPath, kPortalScreenCastInterface,
|
||||
"OpenPipeWireRemote");
|
||||
if (!message) {
|
||||
LOG_ERROR("Failed to allocate OpenPipeWireRemote message");
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter iter;
|
||||
DBusMessageIter options;
|
||||
const char* session_handle = session_handle_.c_str();
|
||||
dbus_message_iter_init_append(message, &iter);
|
||||
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &session_handle);
|
||||
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &options);
|
||||
dbus_message_iter_close_container(&iter, &options);
|
||||
|
||||
DBusError error;
|
||||
dbus_error_init(&error);
|
||||
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
|
||||
dbus_connection_, message, -1, &error);
|
||||
dbus_message_unref(message);
|
||||
if (!reply) {
|
||||
LogDbusError("OpenPipeWireRemote", &error);
|
||||
dbus_error_free(&error);
|
||||
return false;
|
||||
}
|
||||
|
||||
DBusMessageIter reply_iter;
|
||||
if (!dbus_message_iter_init(reply, &reply_iter) ||
|
||||
dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_UNIX_FD) {
|
||||
LOG_ERROR("OpenPipeWireRemote returned an unexpected payload");
|
||||
dbus_message_unref(reply);
|
||||
return false;
|
||||
}
|
||||
|
||||
int received_fd = -1;
|
||||
dbus_message_iter_get_basic(&reply_iter, &received_fd);
|
||||
dbus_message_unref(reply);
|
||||
|
||||
if (received_fd < 0) {
|
||||
LOG_ERROR("OpenPipeWireRemote returned an invalid fd");
|
||||
return false;
|
||||
}
|
||||
|
||||
pipewire_fd_ = dup(received_fd);
|
||||
if (pipewire_fd_ < 0) {
|
||||
LOG_ERROR("Failed to duplicate PipeWire remote fd");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::CleanupDbus() {
|
||||
if (!dbus_connection_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (shared_session_registered_) {
|
||||
return;
|
||||
}
|
||||
|
||||
dbus_connection_close(dbus_connection_);
|
||||
dbus_connection_unref(dbus_connection_);
|
||||
dbus_connection_ = nullptr;
|
||||
}
|
||||
|
||||
void ScreenCapturerWayland::ClosePortalSession() {
|
||||
if (shared_session_registered_) {
|
||||
DBusConnection* close_connection = nullptr;
|
||||
std::string close_session_handle;
|
||||
ReleaseSharedWaylandPortalSession(&close_connection, &close_session_handle);
|
||||
shared_session_registered_ = false;
|
||||
if (close_connection) {
|
||||
CloseWaylandPortalSessionAndConnection(
|
||||
close_connection, close_session_handle, "Session.Close");
|
||||
}
|
||||
dbus_connection_ = nullptr;
|
||||
} else if (dbus_connection_ && !session_handle_.empty()) {
|
||||
CloseWaylandPortalSessionAndConnection(dbus_connection_, session_handle_,
|
||||
"Session.Close");
|
||||
dbus_connection_ = nullptr;
|
||||
}
|
||||
|
||||
session_handle_.clear();
|
||||
pipewire_node_id_ = 0;
|
||||
UpdateDisplayGeometry(
|
||||
logical_width_ > 0 ? logical_width_ : kFallbackWidth,
|
||||
logical_height_ > 0 ? logical_height_ : kFallbackHeight);
|
||||
pointer_granted_ = false;
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -5,7 +5,9 @@
|
||||
#include <X11/extensions/Xfixes.h>
|
||||
#include <X11/extensions/Xrandr.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include "libyuv.h"
|
||||
@@ -13,11 +15,58 @@
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
std::atomic<int> g_x11_last_error_code{0};
|
||||
std::mutex g_x11_error_handler_mutex;
|
||||
|
||||
int CaptureX11ErrorHandler([[maybe_unused]] Display* display,
|
||||
XErrorEvent* error_event) {
|
||||
if (error_event) {
|
||||
g_x11_last_error_code.store(error_event->error_code);
|
||||
} else {
|
||||
g_x11_last_error_code.store(-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
class ScopedX11ErrorTrap {
|
||||
public:
|
||||
explicit ScopedX11ErrorTrap(Display* display)
|
||||
: display_(display), lock_(g_x11_error_handler_mutex) {
|
||||
g_x11_last_error_code.store(0);
|
||||
previous_handler_ = XSetErrorHandler(CaptureX11ErrorHandler);
|
||||
}
|
||||
|
||||
~ScopedX11ErrorTrap() {
|
||||
if (display_) {
|
||||
XSync(display_, False);
|
||||
}
|
||||
XSetErrorHandler(previous_handler_);
|
||||
}
|
||||
|
||||
int SyncAndGetError() const {
|
||||
if (display_) {
|
||||
XSync(display_, False);
|
||||
}
|
||||
return g_x11_last_error_code.load();
|
||||
}
|
||||
|
||||
private:
|
||||
Display* display_ = nullptr;
|
||||
int (*previous_handler_)(Display*, XErrorEvent*) = nullptr;
|
||||
std::unique_lock<std::mutex> lock_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerX11::ScreenCapturerX11() {}
|
||||
|
||||
ScreenCapturerX11::~ScreenCapturerX11() { Destroy(); }
|
||||
|
||||
int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) {
|
||||
Destroy();
|
||||
|
||||
display_ = XOpenDisplay(nullptr);
|
||||
if (!display_) {
|
||||
LOG_ERROR("Cannot connect to X server");
|
||||
@@ -29,6 +78,7 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) {
|
||||
if (!screen_res_) {
|
||||
LOG_ERROR("Failed to get screen resources");
|
||||
XCloseDisplay(display_);
|
||||
display_ = nullptr;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -71,8 +121,15 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) {
|
||||
width_ = attr.width;
|
||||
height_ = attr.height;
|
||||
|
||||
if (width_ % 2 != 0 || height_ % 2 != 0) {
|
||||
LOG_ERROR("Width and height must be even numbers");
|
||||
if ((width_ & 1) != 0 || (height_ & 1) != 0) {
|
||||
LOG_WARN("X11 root size {}x{} is not even, aligning down to {}x{} for NV12",
|
||||
width_, height_, width_ & ~1, height_ & ~1);
|
||||
width_ &= ~1;
|
||||
height_ &= ~1;
|
||||
}
|
||||
|
||||
if (width_ <= 1 || height_ <= 1) {
|
||||
LOG_ERROR("Invalid capture size after alignment: {}x{}", width_, height_);
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -82,6 +139,11 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) {
|
||||
y_plane_.resize(width_ * height_);
|
||||
uv_plane_.resize((width_ / 2) * (height_ / 2) * 2);
|
||||
|
||||
if (!ProbeCapture()) {
|
||||
LOG_ERROR("X11 backend probe failed, XGetImage is not usable");
|
||||
return -3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -108,9 +170,24 @@ int ScreenCapturerX11::Start(bool show_cursor) {
|
||||
show_cursor_ = show_cursor;
|
||||
running_ = true;
|
||||
paused_ = false;
|
||||
capture_error_count_ = 0;
|
||||
thread_ = std::thread([this]() {
|
||||
using clock = std::chrono::steady_clock;
|
||||
const auto frame_interval =
|
||||
std::chrono::milliseconds(std::max(1, 1000 / std::max(1, fps_)));
|
||||
|
||||
while (running_) {
|
||||
if (!paused_) OnFrame();
|
||||
const auto frame_start = clock::now();
|
||||
if (!paused_) {
|
||||
OnFrame();
|
||||
}
|
||||
|
||||
const auto elapsed =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() -
|
||||
frame_start);
|
||||
if (elapsed < frame_interval) {
|
||||
std::this_thread::sleep_for(frame_interval - elapsed);
|
||||
}
|
||||
}
|
||||
});
|
||||
return 0;
|
||||
@@ -138,6 +215,10 @@ int ScreenCapturerX11::SwitchTo(int monitor_index) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::ResetToInitialMonitor() {
|
||||
monitor_index_ = initial_monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
std::vector<DisplayInfo> ScreenCapturerX11::GetDisplayInfoList() {
|
||||
return display_info_list_;
|
||||
}
|
||||
@@ -148,19 +229,44 @@ void ScreenCapturerX11::OnFrame() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (monitor_index_ < 0 || monitor_index_ >= display_info_list_.size()) {
|
||||
LOG_ERROR("Invalid monitor index: {}", monitor_index_.load());
|
||||
const int monitor_index = monitor_index_.load();
|
||||
if (monitor_index < 0 ||
|
||||
monitor_index >= static_cast<int>(display_info_list_.size())) {
|
||||
LOG_ERROR("Invalid monitor index: {}", monitor_index);
|
||||
return;
|
||||
}
|
||||
|
||||
left_ = display_info_list_[monitor_index_].left;
|
||||
top_ = display_info_list_[monitor_index_].top;
|
||||
width_ = display_info_list_[monitor_index_].width;
|
||||
height_ = display_info_list_[monitor_index_].height;
|
||||
left_ = display_info_list_[monitor_index].left;
|
||||
top_ = display_info_list_[monitor_index].top;
|
||||
width_ = display_info_list_[monitor_index].width & ~1;
|
||||
height_ = display_info_list_[monitor_index].height & ~1;
|
||||
|
||||
XImage* image = XGetImage(display_, root_, left_, top_, width_, height_,
|
||||
AllPlanes, ZPixmap);
|
||||
if (!image) return;
|
||||
if (width_ <= 1 || height_ <= 1) {
|
||||
LOG_ERROR("Invalid capture size: {}x{}", width_, height_);
|
||||
return;
|
||||
}
|
||||
|
||||
XImage* image = nullptr;
|
||||
int x11_error = 0;
|
||||
{
|
||||
ScopedX11ErrorTrap trap(display_);
|
||||
image = XGetImage(display_, root_, left_, top_, width_, height_, AllPlanes,
|
||||
ZPixmap);
|
||||
x11_error = trap.SyncAndGetError();
|
||||
}
|
||||
|
||||
if (x11_error != 0 || !image) {
|
||||
if (image) {
|
||||
XDestroyImage(image);
|
||||
}
|
||||
++capture_error_count_;
|
||||
if (capture_error_count_ == 1 || capture_error_count_ % 120 == 0) {
|
||||
LOG_WARN("X11 capture failed: x11_error={}, image={}, consecutive={}",
|
||||
x11_error, image ? "valid" : "null", capture_error_count_);
|
||||
}
|
||||
return;
|
||||
}
|
||||
capture_error_count_ = 0;
|
||||
|
||||
// if enable show cursor, draw cursor
|
||||
if (show_cursor_) {
|
||||
@@ -176,23 +282,41 @@ void ScreenCapturerX11::OnFrame() {
|
||||
}
|
||||
}
|
||||
|
||||
bool needs_copy = image->bytes_per_line != width_ * 4;
|
||||
std::vector<uint8_t> argb_buf;
|
||||
uint8_t* src_argb = nullptr;
|
||||
|
||||
if (needs_copy) {
|
||||
argb_buf.resize(width_ * height_ * 4);
|
||||
for (int y = 0; y < height_; ++y) {
|
||||
memcpy(&argb_buf[y * width_ * 4], image->data + y * image->bytes_per_line,
|
||||
width_ * 4);
|
||||
}
|
||||
src_argb = argb_buf.data();
|
||||
} else {
|
||||
src_argb = reinterpret_cast<uint8_t*>(image->data);
|
||||
if (image->bits_per_pixel != 32 || image->bytes_per_line <= 0) {
|
||||
LOG_WARN(
|
||||
"Unsupported X11 image layout: bits_per_pixel={}, bytes_per_line={}",
|
||||
image->bits_per_pixel, image->bytes_per_line);
|
||||
XDestroyImage(image);
|
||||
return;
|
||||
}
|
||||
|
||||
libyuv::ARGBToNV12(src_argb, width_ * 4, y_plane_.data(), width_,
|
||||
uv_plane_.data(), width_, width_, height_);
|
||||
const uint8_t* src_argb = reinterpret_cast<const uint8_t*>(image->data);
|
||||
const int src_stride_argb = image->bytes_per_line;
|
||||
|
||||
const size_t y_size =
|
||||
static_cast<size_t>(width_) * static_cast<size_t>(height_);
|
||||
const size_t uv_size = y_size / 2;
|
||||
if (y_plane_.size() != y_size) {
|
||||
y_plane_.resize(y_size);
|
||||
}
|
||||
if (uv_plane_.size() != uv_size) {
|
||||
uv_plane_.resize(uv_size);
|
||||
}
|
||||
|
||||
const int convert_ret =
|
||||
use_abgr_to_nv12_
|
||||
? libyuv::ABGRToNV12(src_argb, src_stride_argb, y_plane_.data(),
|
||||
width_, uv_plane_.data(), width_, width_,
|
||||
height_)
|
||||
: libyuv::ARGBToNV12(src_argb, src_stride_argb, y_plane_.data(),
|
||||
width_, uv_plane_.data(), width_, width_,
|
||||
height_);
|
||||
if (convert_ret != 0) {
|
||||
LOG_WARN("X11 {} failed: {}",
|
||||
use_abgr_to_nv12_ ? "ABGRToNV12" : "ARGBToNV12", convert_ret);
|
||||
XDestroyImage(image);
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> nv12;
|
||||
nv12.reserve(y_plane_.size() + uv_plane_.size());
|
||||
@@ -201,7 +325,7 @@ void ScreenCapturerX11::OnFrame() {
|
||||
|
||||
if (callback_) {
|
||||
callback_(nv12.data(), width_ * height_ * 3 / 2, width_, height_,
|
||||
display_info_list_[monitor_index_].name.c_str());
|
||||
display_info_list_[monitor_index].name.c_str());
|
||||
}
|
||||
|
||||
XDestroyImage(image);
|
||||
@@ -284,4 +408,34 @@ void ScreenCapturerX11::DrawCursor(XImage* image, int x, int y) {
|
||||
|
||||
XFree(cursor_image);
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
bool ScreenCapturerX11::ProbeCapture() {
|
||||
if (!display_ || display_info_list_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto& first_display = display_info_list_[0];
|
||||
XImage* probe_image = nullptr;
|
||||
int x11_error = 0;
|
||||
{
|
||||
ScopedX11ErrorTrap trap(display_);
|
||||
probe_image = XGetImage(display_, root_, first_display.left,
|
||||
first_display.top, 1, 1, AllPlanes, ZPixmap);
|
||||
x11_error = trap.SyncAndGetError();
|
||||
}
|
||||
|
||||
if (x11_error != 0 || !probe_image) {
|
||||
LOG_WARN("X11 probe XGetImage failed: x11_error={}, image={}", x11_error,
|
||||
probe_image ? "valid" : "null");
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool red_in_low_byte = (probe_image->red_mask & 0x000000FFu) != 0;
|
||||
const bool blue_in_low_byte = (probe_image->blue_mask & 0x000000FFu) != 0;
|
||||
use_abgr_to_nv12_ = red_in_low_byte && !blue_in_low_byte;
|
||||
|
||||
XDestroyImage(probe_image);
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -17,6 +17,7 @@ struct _XImage;
|
||||
typedef struct _XImage XImage;
|
||||
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
@@ -42,6 +43,7 @@ class ScreenCapturerX11 : public ScreenCapturer {
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
@@ -49,6 +51,7 @@ class ScreenCapturerX11 : public ScreenCapturer {
|
||||
|
||||
private:
|
||||
void DrawCursor(XImage* image, int x, int y);
|
||||
bool ProbeCapture();
|
||||
|
||||
private:
|
||||
Display* display_ = nullptr;
|
||||
@@ -62,13 +65,16 @@ class ScreenCapturerX11 : public ScreenCapturer {
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
int capture_error_count_ = 0;
|
||||
bool use_abgr_to_nv12_ = false;
|
||||
|
||||
std::vector<uint8_t> y_plane_;
|
||||
std::vector<uint8_t> uv_plane_;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -16,7 +16,11 @@ int ScreenCapturerSck::Init(const int fps, cb_desktop_data cb) {
|
||||
}
|
||||
|
||||
screen_capturer_sck_impl_ = CreateScreenCapturerSck();
|
||||
screen_capturer_sck_impl_->Init(fps, on_data_);
|
||||
const int ret = screen_capturer_sck_impl_->Init(fps, on_data_);
|
||||
if (ret != 0) {
|
||||
screen_capturer_sck_impl_.reset();
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -29,8 +33,11 @@ int ScreenCapturerSck::Destroy() {
|
||||
}
|
||||
|
||||
int ScreenCapturerSck::Start(bool show_cursor) {
|
||||
screen_capturer_sck_impl_->Start(show_cursor);
|
||||
return 0;
|
||||
if (!screen_capturer_sck_impl_) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return screen_capturer_sck_impl_->Start(show_cursor);
|
||||
}
|
||||
|
||||
int ScreenCapturerSck::Stop() {
|
||||
@@ -62,6 +69,13 @@ int ScreenCapturerSck::SwitchTo(int monitor_index) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ScreenCapturerSck::ResetToInitialMonitor() {
|
||||
if (screen_capturer_sck_impl_) {
|
||||
return screen_capturer_sck_impl_->ResetToInitialMonitor();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::vector<DisplayInfo> ScreenCapturerSck::GetDisplayInfoList() {
|
||||
if (screen_capturer_sck_impl_) {
|
||||
return screen_capturer_sck_impl_->GetDisplayInfoList();
|
||||
@@ -73,4 +87,4 @@ std::vector<DisplayInfo> ScreenCapturerSck::GetDisplayInfoList() {
|
||||
void ScreenCapturerSck::OnFrame() {}
|
||||
|
||||
void ScreenCapturerSck::CleanUp() {}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -33,6 +33,7 @@ class ScreenCapturerSck : public ScreenCapturer {
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
|
||||
@@ -16,7 +16,12 @@
|
||||
#include <IOKit/graphics/IOGraphicsLib.h>
|
||||
#include <IOSurface/IOSurface.h>
|
||||
#include <ScreenCaptureKit/ScreenCaptureKit.h>
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include "display_info.h"
|
||||
@@ -28,6 +33,15 @@ class ScreenCapturerSckImpl;
|
||||
|
||||
static const int kFullDesktopScreenId = -1;
|
||||
|
||||
static std::string NSErrorToString(NSError *error) {
|
||||
if (!error) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const char *description = [error.localizedDescription UTF8String];
|
||||
return description ? description : "";
|
||||
}
|
||||
|
||||
// The ScreenCaptureKit API was available in macOS 12.3, but full-screen capture
|
||||
// was reported to be broken before macOS 13 - see http://crbug.com/40234870.
|
||||
// Also, the `SCContentFilter` fields `contentRect` and `pointPixelScale` were
|
||||
@@ -70,6 +84,7 @@ class API_AVAILABLE(macos(14.0)) ScreenCapturerSckImpl : public ScreenCapturer {
|
||||
int Resume(int monitor_index) override { return 0; }
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override { return display_info_list_; }
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
private:
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
@@ -77,6 +92,7 @@ class API_AVAILABLE(macos(14.0)) ScreenCapturerSckImpl : public ScreenCapturer {
|
||||
std::map<CGDirectDisplayID, int> display_id_map_reverse_;
|
||||
std::map<CGDirectDisplayID, std::string> display_id_name_map_;
|
||||
unsigned char *nv12_frame_ = nullptr;
|
||||
size_t nv12_frame_size_ = 0;
|
||||
int width_ = 0;
|
||||
int height_ = 0;
|
||||
int fps_ = 60;
|
||||
@@ -99,7 +115,7 @@ class API_AVAILABLE(macos(14.0)) ScreenCapturerSckImpl : public ScreenCapturer {
|
||||
// Helper object to receive Objective-C callbacks from ScreenCaptureKit and call into this C++
|
||||
// object. The helper may outlive this C++ instance, if a completion-handler is passed to
|
||||
// ScreenCaptureKit APIs and the C++ object is deleted before the handler executes.
|
||||
SckHelper *__strong helper_;
|
||||
SckHelper *__strong helper_ = nil;
|
||||
// Callback for returning captured frames, or errors, to the caller. Only used on the caller's
|
||||
// thread.
|
||||
cb_desktop_data _on_data = nullptr;
|
||||
@@ -109,10 +125,11 @@ class API_AVAILABLE(macos(14.0)) ScreenCapturerSckImpl : public ScreenCapturer {
|
||||
// Guards some variables that may be accessed on different threads.
|
||||
std::mutex lock_;
|
||||
// Provides captured desktop frames.
|
||||
SCStream *__strong stream_;
|
||||
SCStream *__strong stream_ = nil;
|
||||
// Currently selected display, or 0 if the full desktop is selected. This capturer does not
|
||||
// support full-desktop capture, and will fall back to the first display.
|
||||
CGDirectDisplayID current_display_ = 0;
|
||||
int initial_monitor_index_ = 0;
|
||||
};
|
||||
|
||||
std::string GetDisplayName(CGDirectDisplayID display_id) {
|
||||
@@ -180,6 +197,19 @@ ScreenCapturerSckImpl::ScreenCapturerSckImpl() {
|
||||
}
|
||||
|
||||
ScreenCapturerSckImpl::~ScreenCapturerSckImpl() {
|
||||
SckHelper *helper_to_release = nil;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
if (stream_) {
|
||||
[stream_ stopCaptureWithCompletionHandler:nil];
|
||||
stream_ = nil;
|
||||
}
|
||||
_on_data = nullptr;
|
||||
helper_to_release = helper_;
|
||||
helper_ = nil;
|
||||
}
|
||||
[helper_to_release releaseCapturer];
|
||||
|
||||
display_info_list_.clear();
|
||||
display_id_map_.clear();
|
||||
display_id_map_reverse_.clear();
|
||||
@@ -188,15 +218,22 @@ ScreenCapturerSckImpl::~ScreenCapturerSckImpl() {
|
||||
if (nv12_frame_) {
|
||||
delete[] nv12_frame_;
|
||||
nv12_frame_ = nullptr;
|
||||
nv12_frame_size_ = 0;
|
||||
}
|
||||
|
||||
[stream_ stopCaptureWithCompletionHandler:nil];
|
||||
[helper_ releaseCapturer];
|
||||
}
|
||||
|
||||
int ScreenCapturerSckImpl::Init(const int fps, cb_desktop_data cb) {
|
||||
if (!cb) {
|
||||
LOG_ERROR("Screen capturer callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
_on_data = cb;
|
||||
fps_ = fps;
|
||||
fps_ = fps > 0 ? fps : 60;
|
||||
display_info_list_.clear();
|
||||
display_id_map_.clear();
|
||||
display_id_map_reverse_.clear();
|
||||
display_id_name_map_.clear();
|
||||
|
||||
if (@available(macOS 10.15, *)) {
|
||||
bool has_permission = CGPreflightScreenCaptureAccess();
|
||||
@@ -214,8 +251,7 @@ int ScreenCapturerSckImpl::Init(const int fps, cb_desktop_data cb) {
|
||||
getShareableContentWithCompletionHandler:^(SCShareableContent *result, NSError *error) {
|
||||
if (error) {
|
||||
capture_error = error;
|
||||
LOG_ERROR("Failed to get shareable content: {}",
|
||||
std::string([error.localizedDescription UTF8String]));
|
||||
LOG_ERROR("Failed to get shareable content: {}", NSErrorToString(error));
|
||||
} else {
|
||||
content = result;
|
||||
}
|
||||
@@ -225,7 +261,7 @@ int ScreenCapturerSckImpl::Init(const int fps, cb_desktop_data cb) {
|
||||
|
||||
if (capture_error || !content || content.displays.count == 0) {
|
||||
LOG_ERROR("Failed to get display info, error: {}",
|
||||
std::string([capture_error.localizedDescription UTF8String]));
|
||||
NSErrorToString(capture_error));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -261,6 +297,7 @@ int ScreenCapturerSckImpl::Init(const int fps, cb_desktop_data cb) {
|
||||
display_id_name_map_[display_id] = name;
|
||||
}
|
||||
|
||||
initial_monitor_index_ = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -281,32 +318,58 @@ int ScreenCapturerSckImpl::Start(bool show_cursor) {
|
||||
}
|
||||
|
||||
int ScreenCapturerSckImpl::SwitchTo(int monitor_index) {
|
||||
if (stream_) {
|
||||
[stream_ stopCaptureWithCompletionHandler:^(NSError *error) {
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
stream_ = nil;
|
||||
current_display_ = display_id_map_[monitor_index];
|
||||
StartOrReconfigureCapturer();
|
||||
}];
|
||||
} else {
|
||||
current_display_ = display_id_map_[monitor_index];
|
||||
StartOrReconfigureCapturer();
|
||||
auto display_it = display_id_map_.find(monitor_index);
|
||||
if (display_it == display_id_map_.end()) {
|
||||
LOG_WARN("SwitchTo skipped, invalid monitor_index={}, displays={}",
|
||||
monitor_index, display_id_map_.size());
|
||||
return -1;
|
||||
}
|
||||
|
||||
const CGDirectDisplayID target_display = display_it->second;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
current_display_ = target_display;
|
||||
}
|
||||
StartOrReconfigureCapturer();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerSckImpl::ResetToInitialMonitor() {
|
||||
int target = initial_monitor_index_;
|
||||
if (display_info_list_.empty()) return -1;
|
||||
auto display_it = display_id_map_.find(target);
|
||||
if (display_it == display_id_map_.end()) {
|
||||
LOG_WARN("ResetToInitialMonitor skipped, invalid monitor_index={}", target);
|
||||
return -1;
|
||||
}
|
||||
|
||||
CGDirectDisplayID target_display = display_it->second;
|
||||
if (current_display_ == target_display) return 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
current_display_ = target_display;
|
||||
}
|
||||
StartOrReconfigureCapturer();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerSckImpl::Destroy() {
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
if (stream_) {
|
||||
LOG_INFO("Destroying stream");
|
||||
[stream_ stopCaptureWithCompletionHandler:nil];
|
||||
stream_ = nil;
|
||||
SckHelper *helper_to_release = nil;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
if (stream_) {
|
||||
LOG_INFO("Destroying stream");
|
||||
[stream_ stopCaptureWithCompletionHandler:nil];
|
||||
stream_ = nil;
|
||||
}
|
||||
current_display_ = 0;
|
||||
permanent_error_ = false;
|
||||
_on_data = nullptr;
|
||||
helper_to_release = helper_;
|
||||
helper_ = nil;
|
||||
}
|
||||
current_display_ = 0;
|
||||
permanent_error_ = false;
|
||||
_on_data = nullptr;
|
||||
[helper_ releaseCapturer];
|
||||
helper_ = nil;
|
||||
|
||||
[helper_to_release releaseCapturer];
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -394,7 +457,7 @@ void ScreenCapturerSckImpl::OnShareableContentCreated(SCShareableContent *conten
|
||||
|
||||
// TODO: crbug.com/327458809 - Choose an appropriate sampleHandlerQueue for
|
||||
// best performance.
|
||||
NSError *add_stream_output_error;
|
||||
NSError *add_stream_output_error = nil;
|
||||
dispatch_queue_t queue = dispatch_queue_create("ScreenCaptureKit.Queue", DISPATCH_QUEUE_SERIAL);
|
||||
bool add_stream_output_result = [stream_ addStreamOutput:helper_
|
||||
type:SCStreamOutputTypeScreen
|
||||
@@ -403,7 +466,7 @@ void ScreenCapturerSckImpl::OnShareableContentCreated(SCShareableContent *conten
|
||||
|
||||
if (!add_stream_output_result) {
|
||||
stream_ = nil;
|
||||
LOG_ERROR("addStreamOutput failed");
|
||||
LOG_ERROR("addStreamOutput failed: {}", NSErrorToString(add_stream_output_error));
|
||||
permanent_error_ = true;
|
||||
return;
|
||||
}
|
||||
@@ -414,7 +477,7 @@ void ScreenCapturerSckImpl::OnShareableContentCreated(SCShareableContent *conten
|
||||
// calls stopCaptureWithCompletionHandler on the stream, which cancels
|
||||
// this handler.
|
||||
permanent_error_ = true;
|
||||
LOG_ERROR("startCaptureWithCompletionHandler failed");
|
||||
LOG_ERROR("startCaptureWithCompletionHandler failed: {}", NSErrorToString(error));
|
||||
} else {
|
||||
LOG_INFO("Capture started");
|
||||
}
|
||||
@@ -426,8 +489,18 @@ void ScreenCapturerSckImpl::OnShareableContentCreated(SCShareableContent *conten
|
||||
|
||||
void ScreenCapturerSckImpl::OnNewCVPixelBuffer(CVPixelBufferRef pixelBuffer,
|
||||
CFDictionaryRef attachment) {
|
||||
(void)attachment;
|
||||
if (!pixelBuffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t width = CVPixelBufferGetWidth(pixelBuffer);
|
||||
size_t height = CVPixelBufferGetHeight(pixelBuffer);
|
||||
if (width == 0 || height == 0 || CVPixelBufferGetPlaneCount(pixelBuffer) < 2) {
|
||||
LOG_ERROR("Invalid CVPixelBuffer: width={}, height={}, planes={}", width, height,
|
||||
CVPixelBufferGetPlaneCount(pixelBuffer));
|
||||
return;
|
||||
}
|
||||
|
||||
CVReturn status = CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
|
||||
if (status != kCVReturnSuccess) {
|
||||
@@ -436,18 +509,37 @@ void ScreenCapturerSckImpl::OnNewCVPixelBuffer(CVPixelBufferRef pixelBuffer,
|
||||
}
|
||||
|
||||
size_t required_size = width * height * 3 / 2;
|
||||
if (!nv12_frame_ || (width_ * height_ * 3 / 2 < required_size)) {
|
||||
if (required_size > static_cast<size_t>((std::numeric_limits<int>::max)())) {
|
||||
LOG_ERROR("Captured frame is too large: {} bytes", required_size);
|
||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(lock_);
|
||||
if (!_on_data) {
|
||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nv12_frame_ || nv12_frame_size_ < required_size) {
|
||||
delete[] nv12_frame_;
|
||||
nv12_frame_ = new unsigned char[required_size];
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
nv12_frame_size_ = required_size;
|
||||
}
|
||||
width_ = static_cast<int>(width);
|
||||
height_ = static_cast<int>(height);
|
||||
|
||||
void *base_y = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
|
||||
size_t stride_y = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
|
||||
|
||||
void *base_uv = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
|
||||
size_t stride_uv = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1);
|
||||
if (!base_y || !base_uv || stride_y < width || stride_uv < width) {
|
||||
LOG_ERROR("Invalid CVPixelBuffer planes: base_y={}, base_uv={}, stride_y={}, stride_uv={}",
|
||||
base_y != nullptr, base_uv != nullptr, stride_y, stride_uv);
|
||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char *dst_y = nv12_frame_;
|
||||
for (size_t row = 0; row < height; ++row) {
|
||||
@@ -459,7 +551,8 @@ void ScreenCapturerSckImpl::OnNewCVPixelBuffer(CVPixelBufferRef pixelBuffer,
|
||||
memcpy(dst_uv + row * width, static_cast<unsigned char *>(base_uv) + row * stride_uv, width);
|
||||
}
|
||||
|
||||
_on_data(nv12_frame_, width * height * 3 / 2, width, height,
|
||||
_on_data(nv12_frame_, static_cast<int>(required_size), static_cast<int>(width),
|
||||
static_cast<int>(height),
|
||||
display_id_name_map_[current_display_].c_str());
|
||||
|
||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
|
||||
@@ -481,10 +574,14 @@ void ScreenCapturerSckImpl::StartOrReconfigureCapturer() {
|
||||
}
|
||||
|
||||
SckHelper *local_helper = helper_;
|
||||
if (!local_helper) {
|
||||
LOG_ERROR("Cannot reconfigure capturer: helper is null");
|
||||
return;
|
||||
}
|
||||
|
||||
auto handler = ^(SCShareableContent *content, NSError *error) {
|
||||
if (error) {
|
||||
LOG_ERROR("getShareableContent failed: {}",
|
||||
std::string([error.localizedDescription UTF8String]));
|
||||
LOG_ERROR("getShareableContent failed: {}", NSErrorToString(error));
|
||||
[local_helper onShareableContentCreated:nil];
|
||||
return;
|
||||
}
|
||||
@@ -554,4 +651,4 @@ void ScreenCapturerSckImpl::StartOrReconfigureCapturer() {
|
||||
|
||||
std::unique_ptr<ScreenCapturer> ScreenCapturerSck::CreateScreenCapturerSck() {
|
||||
return std::make_unique<ScreenCapturerSckImpl>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ class ScreenCapturer {
|
||||
|
||||
virtual std::vector<DisplayInfo> GetDisplayInfoList() = 0;
|
||||
virtual int SwitchTo(int monitor_index) = 0;
|
||||
virtual int ResetToInitialMonitor() = 0;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
@@ -8,9 +8,9 @@
|
||||
#define _SCREEN_CAPTURER_FACTORY_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "screen_capturer_wgc.h"
|
||||
#include "screen_capturer_win.h"
|
||||
#elif __linux__
|
||||
#include "screen_capturer_x11.h"
|
||||
#include "screen_capturer_linux.h"
|
||||
#elif __APPLE__
|
||||
// #include "screen_capturer_avf.h"
|
||||
#include "screen_capturer_sck.h"
|
||||
@@ -25,9 +25,9 @@ class ScreenCapturerFactory {
|
||||
public:
|
||||
ScreenCapturer* Create() {
|
||||
#ifdef _WIN32
|
||||
return new ScreenCapturerWgc();
|
||||
return new ScreenCapturerWin();
|
||||
#elif __linux__
|
||||
return new ScreenCapturerX11();
|
||||
return new ScreenCapturerLinux();
|
||||
#elif __APPLE__
|
||||
// return new ScreenCapturerAvf();
|
||||
return new ScreenCapturerSck();
|
||||
@@ -37,4 +37,4 @@ class ScreenCapturerFactory {
|
||||
}
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,386 @@
|
||||
#include "screen_capturer_dxgi.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "libyuv.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
std::string WideToUtf8(const std::wstring& wstr) {
|
||||
if (wstr.empty()) return {};
|
||||
int size_needed = WideCharToMultiByte(
|
||||
CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
|
||||
std::string result(size_needed, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), result.data(),
|
||||
size_needed, nullptr, nullptr);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string CleanDisplayName(const std::wstring& wide_name) {
|
||||
std::string name = WideToUtf8(wide_name);
|
||||
name.erase(std::remove_if(name.begin(), name.end(),
|
||||
[](unsigned char c) { return !std::isalnum(c); }),
|
||||
name.end());
|
||||
return name;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerDxgi::ScreenCapturerDxgi() {}
|
||||
ScreenCapturerDxgi::~ScreenCapturerDxgi() {
|
||||
Stop();
|
||||
Destroy();
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::Init(const int fps, cb_desktop_data cb) {
|
||||
fps_ = fps;
|
||||
callback_ = cb;
|
||||
if (!callback_) {
|
||||
LOG_ERROR("DXGI: callback is null");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!InitializeDxgi()) {
|
||||
LOG_ERROR("DXGI: initialize DXGI failed");
|
||||
return -2;
|
||||
}
|
||||
|
||||
EnumerateDisplays();
|
||||
if (display_info_list_.empty()) {
|
||||
LOG_ERROR("DXGI: no displays found");
|
||||
return -3;
|
||||
}
|
||||
|
||||
monitor_index_ = 0;
|
||||
initial_monitor_index_ = monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::Destroy() {
|
||||
Stop();
|
||||
ReleaseDuplication();
|
||||
outputs_.clear();
|
||||
d3d_context_.Reset();
|
||||
d3d_device_.Reset();
|
||||
dxgi_factory_.Reset();
|
||||
if (nv12_frame_) {
|
||||
delete[] nv12_frame_;
|
||||
nv12_frame_ = nullptr;
|
||||
nv12_width_ = 0;
|
||||
nv12_height_ = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::Start(bool show_cursor) {
|
||||
if (running_) return 0;
|
||||
show_cursor_ = show_cursor;
|
||||
|
||||
if (!CreateDuplicationForMonitor(monitor_index_)) {
|
||||
LOG_ERROR("DXGI: create duplication failed for monitor {}",
|
||||
monitor_index_.load());
|
||||
return -1;
|
||||
}
|
||||
|
||||
paused_ = false;
|
||||
running_ = true;
|
||||
thread_ = std::thread([this]() { CaptureLoop(); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::Stop() {
|
||||
if (!running_) return 0;
|
||||
running_ = false;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
ReleaseDuplication();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::Pause(int monitor_index) {
|
||||
paused_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::Resume(int monitor_index) {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::SwitchTo(int monitor_index) {
|
||||
if (monitor_index < 0 || monitor_index >= (int)display_info_list_.size()) {
|
||||
LOG_ERROR("DXGI: invalid monitor index {}", monitor_index);
|
||||
return -1;
|
||||
}
|
||||
paused_ = true;
|
||||
monitor_index_ = monitor_index;
|
||||
ReleaseDuplication();
|
||||
if (!CreateDuplicationForMonitor(monitor_index_)) {
|
||||
LOG_ERROR("DXGI: create duplication failed for monitor {}",
|
||||
monitor_index_.load());
|
||||
return -2;
|
||||
}
|
||||
paused_ = false;
|
||||
LOG_INFO("DXGI: switched to monitor {}:{}", monitor_index_.load(),
|
||||
display_info_list_[monitor_index_].name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerDxgi::ResetToInitialMonitor() {
|
||||
if (display_info_list_.empty()) return -1;
|
||||
int target = initial_monitor_index_;
|
||||
if (target < 0 || target >= (int)display_info_list_.size()) return -1;
|
||||
if (monitor_index_ == target) return 0;
|
||||
if (running_) {
|
||||
paused_ = true;
|
||||
monitor_index_ = target;
|
||||
ReleaseDuplication();
|
||||
if (!CreateDuplicationForMonitor(monitor_index_)) {
|
||||
paused_ = false;
|
||||
return -2;
|
||||
}
|
||||
paused_ = false;
|
||||
LOG_INFO("DXGI: reset to initial monitor {}:{}", monitor_index_.load(),
|
||||
display_info_list_[monitor_index_].name);
|
||||
} else {
|
||||
monitor_index_ = target;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDxgi::InitializeDxgi() {
|
||||
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
|
||||
#ifdef _DEBUG
|
||||
flags |= D3D11_CREATE_DEVICE_DEBUG;
|
||||
#endif
|
||||
|
||||
D3D_FEATURE_LEVEL feature_levels[] = {
|
||||
D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1,
|
||||
D3D_FEATURE_LEVEL_10_0};
|
||||
|
||||
D3D_FEATURE_LEVEL out_level{};
|
||||
HRESULT hr = D3D11CreateDevice(
|
||||
nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, feature_levels,
|
||||
ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, d3d_device_.GetAddressOf(),
|
||||
&out_level, d3d_context_.GetAddressOf());
|
||||
if (FAILED(hr)) {
|
||||
hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, flags,
|
||||
feature_levels, ARRAYSIZE(feature_levels),
|
||||
D3D11_SDK_VERSION, d3d_device_.GetAddressOf(),
|
||||
&out_level, d3d_context_.GetAddressOf());
|
||||
if (FAILED(hr)) {
|
||||
LOG_ERROR("DXGI: D3D11CreateDevice failed, hr={}", (int)hr);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
hr = CreateDXGIFactory1(
|
||||
__uuidof(IDXGIFactory1),
|
||||
reinterpret_cast<void**>(dxgi_factory_.GetAddressOf()));
|
||||
if (FAILED(hr)) {
|
||||
LOG_ERROR("DXGI: CreateDXGIFactory1 failed, hr={}", (int)hr);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScreenCapturerDxgi::EnumerateDisplays() {
|
||||
display_info_list_.clear();
|
||||
outputs_.clear();
|
||||
|
||||
Microsoft::WRL::ComPtr<IDXGIAdapter> adapter;
|
||||
for (UINT a = 0;
|
||||
dxgi_factory_->EnumAdapters(a, adapter.ReleaseAndGetAddressOf()) !=
|
||||
DXGI_ERROR_NOT_FOUND;
|
||||
++a) {
|
||||
Microsoft::WRL::ComPtr<IDXGIOutput> output;
|
||||
for (UINT o = 0; adapter->EnumOutputs(o, output.ReleaseAndGetAddressOf()) !=
|
||||
DXGI_ERROR_NOT_FOUND;
|
||||
++o) {
|
||||
DXGI_OUTPUT_DESC desc{};
|
||||
if (FAILED(output->GetDesc(&desc))) {
|
||||
continue;
|
||||
}
|
||||
std::string name = CleanDisplayName(desc.DeviceName);
|
||||
MONITORINFOEX mi{};
|
||||
mi.cbSize = sizeof(MONITORINFOEX);
|
||||
if (GetMonitorInfo(desc.Monitor, &mi)) {
|
||||
bool is_primary = (mi.dwFlags & MONITORINFOF_PRIMARY) ? true : false;
|
||||
DisplayInfo info((void*)desc.Monitor, name, is_primary,
|
||||
mi.rcMonitor.left, mi.rcMonitor.top,
|
||||
mi.rcMonitor.right, mi.rcMonitor.bottom);
|
||||
// primary first
|
||||
if (is_primary)
|
||||
display_info_list_.insert(display_info_list_.begin(), info);
|
||||
else
|
||||
display_info_list_.push_back(info);
|
||||
outputs_.push_back(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ScreenCapturerDxgi::CreateDuplicationForMonitor(int monitor_index) {
|
||||
if (monitor_index < 0 || monitor_index >= (int)outputs_.size()) return false;
|
||||
Microsoft::WRL::ComPtr<IDXGIOutput1> output1;
|
||||
HRESULT hr = outputs_[monitor_index]->QueryInterface(
|
||||
IID_PPV_ARGS(output1.GetAddressOf()));
|
||||
if (FAILED(hr)) {
|
||||
LOG_ERROR("DXGI: Query IDXGIOutput1 failed, hr={}", (int)hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
duplication_.Reset();
|
||||
hr = output1->DuplicateOutput(d3d_device_.Get(), duplication_.GetAddressOf());
|
||||
if (FAILED(hr)) {
|
||||
LOG_ERROR("DXGI: DuplicateOutput failed, hr={}", (int)hr);
|
||||
return false;
|
||||
}
|
||||
|
||||
staging_.Reset();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScreenCapturerDxgi::RecreateDuplicationForCurrentMonitor() {
|
||||
ReleaseDuplication();
|
||||
int current_monitor = monitor_index_.load();
|
||||
if (CreateDuplicationForMonitor(current_monitor)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
EnumerateDisplays();
|
||||
if (display_info_list_.empty()) {
|
||||
LOG_ERROR("DXGI: no displays found while recreating duplication");
|
||||
return false;
|
||||
}
|
||||
if (current_monitor < 0 ||
|
||||
current_monitor >= static_cast<int>(display_info_list_.size())) {
|
||||
current_monitor = 0;
|
||||
monitor_index_ = 0;
|
||||
}
|
||||
if (CreateDuplicationForMonitor(current_monitor)) {
|
||||
LOG_INFO("DXGI: recreated duplication for monitor {}",
|
||||
monitor_index_.load());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScreenCapturerDxgi::ReleaseDuplication() {
|
||||
staging_.Reset();
|
||||
if (duplication_) {
|
||||
duplication_->ReleaseFrame();
|
||||
}
|
||||
duplication_.Reset();
|
||||
}
|
||||
|
||||
void ScreenCapturerDxgi::CaptureLoop() {
|
||||
const int timeout_ms = 33;
|
||||
auto last_duplication_retry =
|
||||
std::chrono::steady_clock::now() - std::chrono::milliseconds(1000);
|
||||
while (running_) {
|
||||
if (paused_) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!duplication_) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now - last_duplication_retry >= std::chrono::milliseconds(500)) {
|
||||
last_duplication_retry = now;
|
||||
RecreateDuplicationForCurrentMonitor();
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
continue;
|
||||
}
|
||||
|
||||
DXGI_OUTDUPL_FRAME_INFO frame_info{};
|
||||
Microsoft::WRL::ComPtr<IDXGIResource> desktop_resource;
|
||||
HRESULT hr = duplication_->AcquireNextFrame(
|
||||
timeout_ms, &frame_info, desktop_resource.GetAddressOf());
|
||||
if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
|
||||
continue;
|
||||
}
|
||||
if (FAILED(hr)) {
|
||||
LOG_ERROR("DXGI: AcquireNextFrame failed, hr={}", (int)hr);
|
||||
RecreateDuplicationForCurrentMonitor();
|
||||
continue;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> acquired_tex;
|
||||
if (desktop_resource) {
|
||||
hr = desktop_resource->QueryInterface(
|
||||
IID_PPV_ARGS(acquired_tex.GetAddressOf()));
|
||||
if (FAILED(hr)) {
|
||||
duplication_->ReleaseFrame();
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
duplication_->ReleaseFrame();
|
||||
continue;
|
||||
}
|
||||
|
||||
D3D11_TEXTURE2D_DESC src_desc{};
|
||||
acquired_tex->GetDesc(&src_desc);
|
||||
|
||||
if (!staging_) {
|
||||
D3D11_TEXTURE2D_DESC staging_desc = src_desc;
|
||||
staging_desc.Usage = D3D11_USAGE_STAGING;
|
||||
staging_desc.BindFlags = 0;
|
||||
staging_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
staging_desc.MiscFlags = 0;
|
||||
hr = d3d_device_->CreateTexture2D(&staging_desc, nullptr,
|
||||
staging_.GetAddressOf());
|
||||
if (FAILED(hr)) {
|
||||
LOG_ERROR("DXGI: CreateTexture2D staging failed, hr={}", (int)hr);
|
||||
duplication_->ReleaseFrame();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
d3d_context_->CopyResource(staging_.Get(), acquired_tex.Get());
|
||||
|
||||
D3D11_MAPPED_SUBRESOURCE mapped{};
|
||||
hr = d3d_context_->Map(staging_.Get(), 0, D3D11_MAP_READ, 0, &mapped);
|
||||
if (FAILED(hr)) {
|
||||
duplication_->ReleaseFrame();
|
||||
continue;
|
||||
}
|
||||
|
||||
int logical_width = static_cast<int>(src_desc.Width);
|
||||
int even_width = logical_width & ~1;
|
||||
int even_height = static_cast<int>(src_desc.Height) & ~1;
|
||||
if (even_width <= 0 || even_height <= 0) {
|
||||
d3d_context_->Unmap(staging_.Get(), 0);
|
||||
duplication_->ReleaseFrame();
|
||||
continue;
|
||||
}
|
||||
|
||||
int nv12_size = even_width * even_height * 3 / 2;
|
||||
if (!nv12_frame_ || nv12_width_ != even_width ||
|
||||
nv12_height_ != even_height) {
|
||||
delete[] nv12_frame_;
|
||||
nv12_frame_ = new unsigned char[nv12_size];
|
||||
nv12_width_ = even_width;
|
||||
nv12_height_ = even_height;
|
||||
}
|
||||
|
||||
libyuv::ARGBToNV12(static_cast<const uint8_t*>(mapped.pData),
|
||||
static_cast<int>(mapped.RowPitch), nv12_frame_,
|
||||
even_width, nv12_frame_ + even_width * even_height,
|
||||
even_width, even_width, even_height);
|
||||
|
||||
if (callback_) {
|
||||
callback_(nv12_frame_, nv12_size, even_width, even_height,
|
||||
display_info_list_[monitor_index_].name.c_str());
|
||||
}
|
||||
|
||||
d3d_context_->Unmap(staging_.Get(), 0);
|
||||
duplication_->ReleaseFrame();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-02-27
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_DXGI_H_
|
||||
#define _SCREEN_CAPTURER_DXGI_H_
|
||||
|
||||
#include <Windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "rd_log.h"
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerDxgi : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerDxgi();
|
||||
~ScreenCapturerDxgi();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
private:
|
||||
bool InitializeDxgi();
|
||||
void EnumerateDisplays();
|
||||
bool CreateDuplicationForMonitor(int monitor_index);
|
||||
bool RecreateDuplicationForCurrentMonitor();
|
||||
void CaptureLoop();
|
||||
void ReleaseDuplication();
|
||||
|
||||
private:
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
std::vector<Microsoft::WRL::ComPtr<IDXGIOutput>> outputs_;
|
||||
|
||||
Microsoft::WRL::ComPtr<IDXGIFactory1> dxgi_factory_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Device> d3d_device_;
|
||||
Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d_context_;
|
||||
Microsoft::WRL::ComPtr<IDXGIOutputDuplication> duplication_;
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> staging_;
|
||||
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
std::thread thread_;
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_ = nullptr;
|
||||
|
||||
unsigned char* nv12_frame_ = nullptr;
|
||||
int nv12_width_ = 0;
|
||||
int nv12_height_ = 0;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,217 @@
|
||||
#include "screen_capturer_gdi.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "libyuv.h"
|
||||
#include "rd_log.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
std::string WideToUtf8(const std::wstring& wstr) {
|
||||
if (wstr.empty()) return {};
|
||||
int size_needed = WideCharToMultiByte(
|
||||
CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
|
||||
std::string result(size_needed, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), result.data(),
|
||||
size_needed, nullptr, nullptr);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string CleanDisplayName(const std::wstring& wide_name) {
|
||||
std::string name = WideToUtf8(wide_name);
|
||||
name.erase(std::remove_if(name.begin(), name.end(),
|
||||
[](unsigned char c) { return !std::isalnum(c); }),
|
||||
name.end());
|
||||
return name;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ScreenCapturerGdi::ScreenCapturerGdi() {}
|
||||
ScreenCapturerGdi::~ScreenCapturerGdi() {
|
||||
Stop();
|
||||
Destroy();
|
||||
}
|
||||
|
||||
BOOL CALLBACK ScreenCapturerGdi::EnumMonitorProc(HMONITOR hMonitor, HDC, LPRECT,
|
||||
LPARAM data) {
|
||||
auto displays = reinterpret_cast<std::vector<DisplayInfo>*>(data);
|
||||
MONITORINFOEX mi{};
|
||||
mi.cbSize = sizeof(MONITORINFOEX);
|
||||
if (GetMonitorInfo(hMonitor, &mi)) {
|
||||
std::string name = CleanDisplayName(mi.szDevice);
|
||||
bool is_primary = (mi.dwFlags & MONITORINFOF_PRIMARY) ? true : false;
|
||||
DisplayInfo info((void*)hMonitor, name, is_primary, mi.rcMonitor.left,
|
||||
mi.rcMonitor.top, mi.rcMonitor.right, mi.rcMonitor.bottom);
|
||||
if (is_primary)
|
||||
displays->insert(displays->begin(), info);
|
||||
else
|
||||
displays->push_back(info);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void ScreenCapturerGdi::EnumerateDisplays() {
|
||||
display_info_list_.clear();
|
||||
EnumDisplayMonitors(nullptr, nullptr, EnumMonitorProc,
|
||||
(LPARAM)&display_info_list_);
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::Init(const int fps, cb_desktop_data cb) {
|
||||
fps_ = fps;
|
||||
callback_ = cb;
|
||||
if (!callback_) {
|
||||
LOG_ERROR("GDI: callback is null");
|
||||
return -1;
|
||||
}
|
||||
EnumerateDisplays();
|
||||
if (display_info_list_.empty()) {
|
||||
LOG_ERROR("GDI: no displays found");
|
||||
return -2;
|
||||
}
|
||||
monitor_index_ = 0;
|
||||
initial_monitor_index_ = monitor_index_;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::Destroy() {
|
||||
Stop();
|
||||
if (nv12_frame_) {
|
||||
delete[] nv12_frame_;
|
||||
nv12_frame_ = nullptr;
|
||||
nv12_width_ = 0;
|
||||
nv12_height_ = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::Start(bool show_cursor) {
|
||||
if (running_) return 0;
|
||||
show_cursor_ = show_cursor;
|
||||
paused_ = false;
|
||||
running_ = true;
|
||||
thread_ = std::thread([this]() { CaptureLoop(); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::Stop() {
|
||||
if (!running_) return 0;
|
||||
running_ = false;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::Pause(int monitor_index) {
|
||||
paused_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::Resume(int monitor_index) {
|
||||
paused_ = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::SwitchTo(int monitor_index) {
|
||||
if (monitor_index < 0 || monitor_index >= (int)display_info_list_.size()) {
|
||||
LOG_ERROR("GDI: invalid monitor index {}", monitor_index);
|
||||
return -1;
|
||||
}
|
||||
monitor_index_ = monitor_index;
|
||||
LOG_INFO("GDI: switched to monitor {}:{}", monitor_index_.load(),
|
||||
display_info_list_[monitor_index_].name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerGdi::ResetToInitialMonitor() {
|
||||
if (display_info_list_.empty()) return -1;
|
||||
int target = initial_monitor_index_;
|
||||
if (target < 0 || target >= (int)display_info_list_.size()) return -1;
|
||||
monitor_index_ = target;
|
||||
LOG_INFO("GDI: reset to initial monitor {}:{}", monitor_index_.load(),
|
||||
display_info_list_[monitor_index_].name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ScreenCapturerGdi::CaptureLoop() {
|
||||
int interval_ms = fps_ > 0 ? (1000 / fps_) : 16;
|
||||
HDC screen_dc = GetDC(nullptr);
|
||||
while (running_) {
|
||||
if (paused_) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
continue;
|
||||
}
|
||||
if (!screen_dc) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto& di = display_info_list_[monitor_index_];
|
||||
int left = di.left;
|
||||
int top = di.top;
|
||||
int width = di.width & ~1;
|
||||
int height = di.height & ~1;
|
||||
if (width <= 0 || height <= 0) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
|
||||
continue;
|
||||
}
|
||||
|
||||
BITMAPINFO bmi{};
|
||||
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bmi.bmiHeader.biWidth = width;
|
||||
bmi.bmiHeader.biHeight = -height;
|
||||
bmi.bmiHeader.biPlanes = 1;
|
||||
bmi.bmiHeader.biBitCount = 32;
|
||||
bmi.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
void* bits = nullptr;
|
||||
HDC mem_dc = CreateCompatibleDC(screen_dc);
|
||||
HBITMAP dib =
|
||||
CreateDIBSection(mem_dc, &bmi, DIB_RGB_COLORS, &bits, nullptr, 0);
|
||||
HGDIOBJ old = SelectObject(mem_dc, dib);
|
||||
|
||||
BitBlt(mem_dc, 0, 0, width, height, screen_dc, left, top,
|
||||
SRCCOPY | CAPTUREBLT);
|
||||
|
||||
if (show_cursor_) {
|
||||
CURSORINFO ci{};
|
||||
ci.cbSize = sizeof(CURSORINFO);
|
||||
if (GetCursorInfo(&ci) && ci.flags == CURSOR_SHOWING && ci.hCursor) {
|
||||
POINT pt = ci.ptScreenPos;
|
||||
int cx = pt.x - left;
|
||||
int cy = pt.y - top;
|
||||
if (cx >= -64 && cy >= -64 && cx < width + 64 && cy < height + 64) {
|
||||
DrawIconEx(mem_dc, cx, cy, ci.hCursor, 0, 0, 0, nullptr, DI_NORMAL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int stride_argb = width * 4;
|
||||
int nv12_size = width * height * 3 / 2;
|
||||
if (!nv12_frame_ || nv12_width_ != width || nv12_height_ != height) {
|
||||
delete[] nv12_frame_;
|
||||
nv12_frame_ = new unsigned char[nv12_size];
|
||||
nv12_width_ = width;
|
||||
nv12_height_ = height;
|
||||
}
|
||||
|
||||
libyuv::ARGBToNV12(static_cast<const uint8_t*>(bits), stride_argb,
|
||||
nv12_frame_, width, nv12_frame_ + width * height, width,
|
||||
width, height);
|
||||
|
||||
if (callback_) {
|
||||
callback_(nv12_frame_, nv12_size, width, height, di.name.c_str());
|
||||
}
|
||||
|
||||
SelectObject(mem_dc, old);
|
||||
DeleteObject(dib);
|
||||
DeleteDC(mem_dc);
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
|
||||
}
|
||||
ReleaseDC(nullptr, screen_dc);
|
||||
}
|
||||
|
||||
} // namespace crossdesk
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-02-27
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_GDI_H_
|
||||
#define _SCREEN_CAPTURER_GDI_H_
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "rd_log.h"
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerGdi : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerGdi();
|
||||
~ScreenCapturerGdi();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override {
|
||||
return display_info_list_;
|
||||
}
|
||||
|
||||
private:
|
||||
static BOOL CALLBACK EnumMonitorProc(HMONITOR hMonitor, HDC, LPRECT,
|
||||
LPARAM data);
|
||||
void EnumerateDisplays();
|
||||
void CaptureLoop();
|
||||
|
||||
private:
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
std::thread thread_;
|
||||
int fps_ = 60;
|
||||
cb_desktop_data callback_ = nullptr;
|
||||
|
||||
unsigned char* nv12_frame_ = nullptr;
|
||||
int nv12_width_ = 0;
|
||||
int nv12_height_ = 0;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
|
||||
#endif
|
||||
@@ -56,8 +56,6 @@ BOOL WINAPI EnumMonitorProc(HMONITOR hmonitor, [[maybe_unused]] HDC hdc,
|
||||
}
|
||||
}
|
||||
|
||||
if (monitor_info_.dwFlags == DISPLAY_DEVICE_MIRRORING_DRIVER) return true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -102,8 +100,7 @@ bool ScreenCapturerWgc::IsWgcSupported() {
|
||||
}
|
||||
|
||||
int ScreenCapturerWgc::Init(const int fps, cb_desktop_data cb) {
|
||||
int error = 0;
|
||||
if (inited_ == true) return error;
|
||||
if (inited_ == true) return 0;
|
||||
|
||||
// nv12_frame_ = new unsigned char[rect.right * rect.bottom * 3 / 2];
|
||||
// nv12_frame_scaled_ = new unsigned char[1280 * 720 * 3 / 2];
|
||||
@@ -114,8 +111,18 @@ int ScreenCapturerWgc::Init(const int fps, cb_desktop_data cb) {
|
||||
|
||||
if (!IsWgcSupported()) {
|
||||
LOG_ERROR("WGC not supported");
|
||||
error = 2;
|
||||
return error;
|
||||
return 2;
|
||||
}
|
||||
|
||||
return RebuildSessions(monitor_index_);
|
||||
}
|
||||
|
||||
int ScreenCapturerWgc::RebuildSessions(int preferred_monitor_index) {
|
||||
CleanUp();
|
||||
|
||||
if (!IsWgcSupported()) {
|
||||
LOG_ERROR("WGC not supported");
|
||||
return 2;
|
||||
}
|
||||
|
||||
monitor_ = GetPrimaryMonitor();
|
||||
@@ -127,6 +134,13 @@ int ScreenCapturerWgc::Init(const int fps, cb_desktop_data cb) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (preferred_monitor_index < 0 ||
|
||||
preferred_monitor_index >= static_cast<int>(display_info_list_.size())) {
|
||||
preferred_monitor_index = 0;
|
||||
}
|
||||
monitor_index_ = preferred_monitor_index;
|
||||
|
||||
int error = 0;
|
||||
for (int i = 0; i < display_info_list_.size(); i++) {
|
||||
const auto& display = display_info_list_[i];
|
||||
LOG_INFO(
|
||||
@@ -140,19 +154,28 @@ int ScreenCapturerWgc::Init(const int fps, cb_desktop_data cb) {
|
||||
sessions_.back().session_->RegisterObserver(this);
|
||||
error = sessions_.back().session_->Initialize((HMONITOR)display.handle);
|
||||
if (error != 0) {
|
||||
LOG_ERROR("WGC: initialize session {} failed, ret={}", i, error);
|
||||
CleanUp();
|
||||
return error;
|
||||
}
|
||||
sessions_[i].inited_ = true;
|
||||
inited_ = true;
|
||||
}
|
||||
|
||||
LOG_INFO("Default on monitor {}:{}", monitor_index_,
|
||||
display_info_list_[monitor_index_].name);
|
||||
|
||||
if (initial_monitor_index_ < 0 ||
|
||||
initial_monitor_index_ >= static_cast<int>(display_info_list_.size())) {
|
||||
initial_monitor_index_ = monitor_index_;
|
||||
}
|
||||
inited_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWgc::Destroy() { return 0; }
|
||||
int ScreenCapturerWgc::Destroy() {
|
||||
CleanUp();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWgc::Start(bool show_cursor) {
|
||||
if (running_ == true) {
|
||||
@@ -161,11 +184,37 @@ int ScreenCapturerWgc::Start(bool show_cursor) {
|
||||
}
|
||||
|
||||
if (inited_ == false) {
|
||||
LOG_ERROR("Screen capturer not inited");
|
||||
return 4;
|
||||
const int ret = RebuildSessions(monitor_index_);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Screen capturer not inited");
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < sessions_.size(); i++) {
|
||||
int ret = StartSessions(show_cursor);
|
||||
if (ret == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
LOG_WARN("WGC: start failed, rebuilding sessions");
|
||||
ret = RebuildSessions(monitor_index_);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
return StartSessions(show_cursor);
|
||||
}
|
||||
|
||||
int ScreenCapturerWgc::StartSessions(bool show_cursor) {
|
||||
bool any_started = false;
|
||||
bool active_started = false;
|
||||
int last_error = 0;
|
||||
int active_monitor = monitor_index_;
|
||||
if (active_monitor < 0 ||
|
||||
active_monitor >= static_cast<int>(sessions_.size())) {
|
||||
active_monitor = 0;
|
||||
monitor_index_ = 0;
|
||||
}
|
||||
for (int i = 0; i < static_cast<int>(sessions_.size()); i++) {
|
||||
if (sessions_[i].inited_ == false) {
|
||||
LOG_ERROR("Session {} not inited", i);
|
||||
continue;
|
||||
@@ -174,17 +223,38 @@ int ScreenCapturerWgc::Start(bool show_cursor) {
|
||||
if (sessions_[i].running_) {
|
||||
LOG_ERROR("Session {} is already running", i);
|
||||
} else {
|
||||
sessions_[i].session_->Start(show_cursor);
|
||||
int ret = sessions_[i].session_->Start(show_cursor);
|
||||
if (ret != 0) {
|
||||
LOG_ERROR("Session {} start failed, ret={}", i, ret);
|
||||
last_error = ret;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i != 0) {
|
||||
if (i != active_monitor) {
|
||||
sessions_[i].session_->Pause();
|
||||
sessions_[i].paused_ = true;
|
||||
} else {
|
||||
sessions_[i].session_->Resume();
|
||||
sessions_[i].paused_ = false;
|
||||
}
|
||||
sessions_[i].running_ = true;
|
||||
any_started = true;
|
||||
if (i == active_monitor) {
|
||||
active_started = true;
|
||||
}
|
||||
}
|
||||
running_ = true;
|
||||
}
|
||||
|
||||
running_ = active_started;
|
||||
if (!active_started) {
|
||||
LOG_ERROR("WGC: active session did not start successfully");
|
||||
Stop();
|
||||
return last_error != 0 ? last_error : -1;
|
||||
}
|
||||
if (!any_started) {
|
||||
LOG_ERROR("WGC: no session started successfully");
|
||||
return last_error != 0 ? last_error : -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -257,6 +327,26 @@ int ScreenCapturerWgc::SwitchTo(int monitor_index) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerWgc::ResetToInitialMonitor() {
|
||||
if (display_info_list_.empty()) return -1;
|
||||
if (initial_monitor_index_ < 0 ||
|
||||
initial_monitor_index_ >= static_cast<int>(display_info_list_.size())) {
|
||||
return -1;
|
||||
}
|
||||
if (monitor_index_ == initial_monitor_index_) {
|
||||
return 0;
|
||||
}
|
||||
if (running_) {
|
||||
Pause(monitor_index_);
|
||||
}
|
||||
monitor_index_ = initial_monitor_index_;
|
||||
LOG_INFO("Reset to initial monitor {}:{}", monitor_index_,
|
||||
display_info_list_[monitor_index_].name);
|
||||
if (running_) {
|
||||
Resume(monitor_index_);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void ScreenCapturerWgc::OnFrame(const WgcSession::wgc_session_frame& frame,
|
||||
int id) {
|
||||
if (!running_ || !on_data_) {
|
||||
@@ -318,13 +408,16 @@ void ScreenCapturerWgc::OnFrame(const WgcSession::wgc_session_frame& frame,
|
||||
}
|
||||
|
||||
void ScreenCapturerWgc::CleanUp() {
|
||||
if (inited_) {
|
||||
for (auto& session : sessions_) {
|
||||
if (session.session_) {
|
||||
session.session_->Stop();
|
||||
}
|
||||
running_ = false;
|
||||
for (auto& session : sessions_) {
|
||||
if (session.session_) {
|
||||
session.session_->Stop();
|
||||
}
|
||||
sessions_.clear();
|
||||
}
|
||||
sessions_.clear();
|
||||
display_info_list_.clear();
|
||||
gs_display_list.clear();
|
||||
monitor_ = nullptr;
|
||||
inited_ = false;
|
||||
}
|
||||
} // namespace crossdesk
|
||||
} // namespace crossdesk
|
||||
|
||||
@@ -34,17 +34,21 @@ class ScreenCapturerWgc : public ScreenCapturer,
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() { return display_info_list_; }
|
||||
|
||||
int SwitchTo(int monitor_index);
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
void OnFrame(const WgcSession::wgc_session_frame& frame, int id);
|
||||
|
||||
protected:
|
||||
void CleanUp();
|
||||
int RebuildSessions(int preferred_monitor_index);
|
||||
int StartSessions(bool show_cursor);
|
||||
|
||||
private:
|
||||
HMONITOR monitor_;
|
||||
MONITORINFOEX monitor_info_;
|
||||
std::vector<DisplayInfo> display_info_list_;
|
||||
int monitor_index_ = 0;
|
||||
int initial_monitor_index_ = 0;
|
||||
|
||||
private:
|
||||
class WgcSessionInfo {
|
||||
@@ -57,8 +61,8 @@ class ScreenCapturerWgc : public ScreenCapturer,
|
||||
|
||||
std::vector<WgcSessionInfo> sessions_;
|
||||
|
||||
std::atomic_bool running_;
|
||||
std::atomic_bool inited_;
|
||||
std::atomic_bool running_{false};
|
||||
std::atomic_bool inited_{false};
|
||||
|
||||
int fps_ = 60;
|
||||
|
||||
@@ -72,4 +76,4 @@ class ScreenCapturerWgc : public ScreenCapturer,
|
||||
std::mutex frame_mutex_;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* @Author: DI JUNKUN
|
||||
* @Date: 2026-02-27
|
||||
* Copyright (c) 2026 by DI JUNKUN, All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef _SCREEN_CAPTURER_WIN_H_
|
||||
#define _SCREEN_CAPTURER_WIN_H_
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "screen_capturer.h"
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class ScreenCapturerWin : public ScreenCapturer {
|
||||
public:
|
||||
ScreenCapturerWin();
|
||||
~ScreenCapturerWin();
|
||||
|
||||
public:
|
||||
int Init(const int fps, cb_desktop_data cb) override;
|
||||
int Destroy() override;
|
||||
int Start(bool show_cursor) override;
|
||||
int Stop() override;
|
||||
|
||||
int Pause(int monitor_index) override;
|
||||
int Resume(int monitor_index) override;
|
||||
|
||||
int SwitchTo(int monitor_index) override;
|
||||
int ResetToInitialMonitor() override;
|
||||
|
||||
std::vector<DisplayInfo> GetDisplayInfoList() override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<ScreenCapturer> impl_;
|
||||
bool impl_is_wgc_plugin_ = false;
|
||||
int fps_ = 60;
|
||||
cb_desktop_data cb_;
|
||||
cb_desktop_data cb_orig_;
|
||||
|
||||
std::unordered_map<void*, std::string> handle_to_canonical_;
|
||||
std::unordered_map<std::string, std::string> label_alias_;
|
||||
std::mutex alias_mutex_;
|
||||
std::vector<DisplayInfo> canonical_displays_;
|
||||
std::unordered_set<std::string> canonical_labels_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<bool> show_cursor_{true};
|
||||
std::atomic<int> monitor_index_{0};
|
||||
int initial_monitor_index_ = 0;
|
||||
std::atomic<bool> secure_desktop_capture_active_{false};
|
||||
std::atomic<bool> post_secure_desktop_waiting_for_frame_{false};
|
||||
std::atomic<bool> post_secure_desktop_drop_logged_{false};
|
||||
std::atomic<ULONGLONG> post_secure_desktop_started_tick_{0};
|
||||
std::thread secure_capture_thread_;
|
||||
HANDLE secure_frame_mapping_ = nullptr;
|
||||
HANDLE secure_frame_ready_event_ = nullptr;
|
||||
uint8_t* secure_frame_view_ = nullptr;
|
||||
size_t secure_frame_view_size_ = 0;
|
||||
DWORD secure_shared_session_id_ = 0xFFFFFFFF;
|
||||
int secure_shared_left_ = 0;
|
||||
int secure_shared_top_ = 0;
|
||||
int secure_shared_width_ = 0;
|
||||
int secure_shared_height_ = 0;
|
||||
int secure_shared_fps_ = 0;
|
||||
bool secure_shared_show_cursor_ = true;
|
||||
std::string secure_shared_stage_;
|
||||
bool secure_shared_capture_started_ = false;
|
||||
|
||||
void BuildCanonicalFromImpl();
|
||||
void RebuildAliasesFromImpl();
|
||||
void StopSecureCaptureThread();
|
||||
bool RestartCaptureBackendAfterSecureDesktop();
|
||||
void SecureDesktopCaptureLoop();
|
||||
bool GetCurrentCaptureRegion(int* left, int* top, int* width, int* height,
|
||||
std::string* display_name);
|
||||
bool StartSecureDesktopSharedCapture(DWORD session_id, int left, int top,
|
||||
int width, int height,
|
||||
const std::string& stage,
|
||||
bool show_cursor, int fps,
|
||||
std::string* error_out);
|
||||
void StopSecureDesktopSharedCapture(DWORD session_id);
|
||||
bool OpenSecureDesktopSharedFrame(DWORD session_id, size_t min_size,
|
||||
std::string* error_out);
|
||||
bool ReadSecureDesktopSharedFrame(DWORD wait_ms,
|
||||
std::vector<uint8_t>* nv12_frame_out,
|
||||
int* width_out, int* height_out,
|
||||
std::string* error_out);
|
||||
void CloseSecureDesktopSharedFrame();
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user