mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-07-04 13:13:51 +08:00
Merge branch 'desktop-unlock-win' into file-transfer
This commit is contained in:
@@ -51,6 +51,22 @@ struct TranslationRow {
|
||||
X(release_mouse, u8"释放", "Release", u8"Освободить") \
|
||||
X(audio_capture, u8"声音", "Audio", u8"Звук") \
|
||||
X(mute, u8" 静音", " Mute", u8"Без звука") \
|
||||
X(send_sas, u8"发送SAS", "Send SAS", u8"Отправить SAS") \
|
||||
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(remote_unlock_requires_secure_desktop, \
|
||||
u8"当前仍需要安全桌面专用采集/输入", \
|
||||
"Secure desktop capture/input is still required", \
|
||||
u8"По-прежнему нужен отдельный захват/ввод для защищенного рабочего стола") \
|
||||
X(settings, u8"设置", "Settings", u8"Настройки") \
|
||||
X(language, u8"语言:", "Language:", u8"Язык:") \
|
||||
X(video_quality, u8"视频质量:", "Video Quality:", u8"Качество видео:") \
|
||||
|
||||
+237
-6
@@ -28,6 +28,10 @@
|
||||
#include "screen_capturer_factory.h"
|
||||
#include "version_checker.h"
|
||||
|
||||
#if _WIN32
|
||||
#include "interactive_state.h"
|
||||
#include "service_host.h"
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include "window_util_mac.h"
|
||||
@@ -75,6 +79,67 @@ HICON LoadTrayIcon() {
|
||||
|
||||
return LoadIconW(nullptr, IDI_APPLICATION);
|
||||
}
|
||||
|
||||
struct WindowsServiceInteractiveStatus {
|
||||
bool available = false;
|
||||
unsigned int error_code = 0;
|
||||
std::string interactive_stage;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
constexpr uint32_t kWindowsServiceStatusIntervalMs = 1000;
|
||||
constexpr DWORD kWindowsServiceQueryTimeoutMs = 100;
|
||||
constexpr DWORD kWindowsServiceSasTimeoutMs = 500;
|
||||
|
||||
RemoteAction BuildWindowsServiceStatusAction(
|
||||
const WindowsServiceInteractiveStatus& status) {
|
||||
RemoteAction action{};
|
||||
action.type = ControlType::service_status;
|
||||
action.ss.available = status.available;
|
||||
std::strncpy(action.ss.interactive_stage, status.interactive_stage.c_str(),
|
||||
sizeof(action.ss.interactive_stage) - 1);
|
||||
action.ss.interactive_stage[sizeof(action.ss.interactive_stage) - 1] = '\0';
|
||||
return action;
|
||||
}
|
||||
|
||||
bool QueryWindowsServiceInteractiveStatus(
|
||||
WindowsServiceInteractiveStatus* status) {
|
||||
if (status == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*status = WindowsServiceInteractiveStatus{};
|
||||
const std::string response =
|
||||
QueryCrossDeskService("status", kWindowsServiceQueryTimeoutMs);
|
||||
auto json = nlohmann::json::parse(response, nullptr, false);
|
||||
if (json.is_discarded() || !json.is_object()) {
|
||||
status->error = "invalid_service_status_json";
|
||||
return false;
|
||||
}
|
||||
|
||||
status->available = json.value("ok", false);
|
||||
if (!status->available) {
|
||||
status->error = json.value("error", std::string("service_unavailable"));
|
||||
status->error_code = json.value("code", 0u);
|
||||
return true;
|
||||
}
|
||||
|
||||
status->interactive_stage = json.value("interactive_stage", std::string());
|
||||
|
||||
if (ShouldNormalizeUnlockToUserDesktop(
|
||||
json.value("interactive_lock_screen_visible", false),
|
||||
status->interactive_stage, json.value("session_locked", false),
|
||||
json.value("interactive_logon_ui_visible", false),
|
||||
json.value("interactive_secure_desktop_active",
|
||||
json.value("secure_desktop_active", false)),
|
||||
json.value("credential_ui_visible", false),
|
||||
json.value("password_box_visible", false),
|
||||
json.value("unlock_ui_visible", false),
|
||||
json.value("last_session_event", std::string()))) {
|
||||
status->interactive_stage = "user-desktop";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) && !defined(__APPLE__)
|
||||
@@ -729,9 +794,21 @@ int Render::StopMouseController() {
|
||||
}
|
||||
|
||||
int Render::StartKeyboardCapturer() {
|
||||
keyboard_capturer_uses_sdl_events_ = false;
|
||||
|
||||
#if defined(__linux__) && !defined(__APPLE__)
|
||||
if (IsWaylandSession()) {
|
||||
keyboard_capturer_uses_sdl_events_ = true;
|
||||
LOG_INFO("Start keyboard capturer with SDL Wayland backend");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!keyboard_capturer_) {
|
||||
LOG_INFO("keyboard capturer is nullptr");
|
||||
return -1;
|
||||
keyboard_capturer_uses_sdl_events_ = true;
|
||||
LOG_WARN(
|
||||
"keyboard capturer is nullptr, falling back to SDL keyboard events");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int keyboard_capturer_init_ret = keyboard_capturer_->Hook(
|
||||
@@ -743,15 +820,24 @@ int Render::StartKeyboardCapturer() {
|
||||
},
|
||||
this);
|
||||
if (0 != keyboard_capturer_init_ret) {
|
||||
LOG_ERROR("Start keyboard capturer failed");
|
||||
keyboard_capturer_uses_sdl_events_ = true;
|
||||
LOG_WARN(
|
||||
"Start keyboard capturer failed, falling back to SDL keyboard "
|
||||
"events");
|
||||
} else {
|
||||
LOG_INFO("Start keyboard capturer");
|
||||
LOG_INFO("Start keyboard capturer with native hook");
|
||||
}
|
||||
|
||||
return keyboard_capturer_init_ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Render::StopKeyboardCapturer() {
|
||||
if (keyboard_capturer_uses_sdl_events_) {
|
||||
keyboard_capturer_uses_sdl_events_ = false;
|
||||
LOG_INFO("Stop keyboard capturer with SDL keyboard backend");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (keyboard_capturer_) {
|
||||
keyboard_capturer_->Unhook();
|
||||
LOG_INFO("Stop keyboard capturer");
|
||||
@@ -1746,6 +1832,7 @@ void Render::MainLoop() {
|
||||
HandlePendingPresenceProbe();
|
||||
HandleStreamWindow();
|
||||
HandleServerWindow();
|
||||
HandleWindowsServiceIntegration();
|
||||
|
||||
DrawMainWindow();
|
||||
if (stream_window_inited_) {
|
||||
@@ -1772,6 +1859,141 @@ void Render::UpdateLabels() {
|
||||
}
|
||||
}
|
||||
|
||||
void Render::ResetRemoteServiceStatus(SubStreamWindowProperties& props) {
|
||||
props.remote_service_status_received_ = false;
|
||||
props.remote_service_available_ = false;
|
||||
props.remote_interactive_stage_.clear();
|
||||
}
|
||||
|
||||
void Render::ApplyRemoteServiceStatus(SubStreamWindowProperties& props,
|
||||
const ServiceStatus& status) {
|
||||
props.remote_service_status_received_ = true;
|
||||
props.remote_service_available_ = status.available;
|
||||
props.remote_interactive_stage_ = status.interactive_stage;
|
||||
}
|
||||
|
||||
Render::RemoteUnlockState Render::GetRemoteUnlockState(
|
||||
const SubStreamWindowProperties& props) const {
|
||||
if (!props.remote_service_status_received_) {
|
||||
return RemoteUnlockState::none;
|
||||
}
|
||||
if (!props.remote_service_available_) {
|
||||
return RemoteUnlockState::service_unavailable;
|
||||
}
|
||||
if (props.remote_interactive_stage_ == "credential-ui") {
|
||||
return RemoteUnlockState::credential_ui;
|
||||
}
|
||||
if (props.remote_interactive_stage_ == "lock-screen") {
|
||||
return RemoteUnlockState::lock_screen;
|
||||
}
|
||||
if (props.remote_interactive_stage_ == "secure-desktop") {
|
||||
return RemoteUnlockState::secure_desktop;
|
||||
}
|
||||
return RemoteUnlockState::none;
|
||||
}
|
||||
|
||||
void Render::HandleWindowsServiceIntegration() {
|
||||
#if _WIN32
|
||||
static bool last_logged_service_available = true;
|
||||
static unsigned int last_logged_service_error_code = 0;
|
||||
static std::string last_logged_service_error;
|
||||
|
||||
if (!is_server_mode_ || peer_ == nullptr) {
|
||||
ResetLocalWindowsServiceState(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const bool has_connected_remote =
|
||||
std::any_of(connection_status_.begin(), connection_status_.end(),
|
||||
[](const auto& entry) {
|
||||
return entry.second == ConnectionStatus::Connected;
|
||||
});
|
||||
if (!has_connected_remote) {
|
||||
ResetLocalWindowsServiceState(false);
|
||||
return;
|
||||
}
|
||||
|
||||
bool force_broadcast = false;
|
||||
if (pending_windows_service_sas_.exchange(false, std::memory_order_relaxed)) {
|
||||
const std::string response =
|
||||
QueryCrossDeskService("sas", kWindowsServiceSasTimeoutMs);
|
||||
auto json = nlohmann::json::parse(response, nullptr, false);
|
||||
if (json.is_discarded() || !json.value("ok", false)) {
|
||||
LOG_WARN("Remote SAS request failed: {}", response);
|
||||
} else {
|
||||
LOG_INFO("Remote SAS request forwarded to local Windows service");
|
||||
}
|
||||
last_windows_service_status_tick_ = 0;
|
||||
force_broadcast = true;
|
||||
}
|
||||
|
||||
const uint32_t now = static_cast<uint32_t>(SDL_GetTicks());
|
||||
if (!force_broadcast && last_windows_service_status_tick_ != 0 &&
|
||||
now - last_windows_service_status_tick_ <
|
||||
kWindowsServiceStatusIntervalMs) {
|
||||
return;
|
||||
}
|
||||
last_windows_service_status_tick_ = now;
|
||||
|
||||
WindowsServiceInteractiveStatus status;
|
||||
const bool status_ok = QueryWindowsServiceInteractiveStatus(&status);
|
||||
local_service_status_received_ = status_ok;
|
||||
local_service_available_ = status.available;
|
||||
local_interactive_stage_ = status.available ? status.interactive_stage : "";
|
||||
|
||||
if (status_ok) {
|
||||
const bool availability_changed =
|
||||
status.available != last_logged_service_available;
|
||||
const bool error_changed =
|
||||
!status.available &&
|
||||
(status.error != last_logged_service_error ||
|
||||
status.error_code != last_logged_service_error_code);
|
||||
if (availability_changed || error_changed) {
|
||||
if (status.available) {
|
||||
LOG_INFO(
|
||||
"Local Windows service available for secure desktop integration");
|
||||
} else {
|
||||
LOG_WARN(
|
||||
"Local Windows service unavailable, secure desktop integration "
|
||||
"disabled: error={}, code={}",
|
||||
status.error, status.error_code);
|
||||
}
|
||||
last_logged_service_available = status.available;
|
||||
last_logged_service_error = status.error;
|
||||
last_logged_service_error_code = status.error_code;
|
||||
}
|
||||
} else if (last_logged_service_available ||
|
||||
last_logged_service_error != "invalid_service_status_json") {
|
||||
LOG_WARN(
|
||||
"Local Windows service status query failed, secure desktop integration "
|
||||
"disabled");
|
||||
last_logged_service_available = false;
|
||||
last_logged_service_error = "invalid_service_status_json";
|
||||
last_logged_service_error_code = 0;
|
||||
}
|
||||
|
||||
RemoteAction remote_action = BuildWindowsServiceStatusAction(status);
|
||||
std::string msg = remote_action.to_json();
|
||||
int ret = SendReliableDataFrame(peer_, msg.data(), msg.size(),
|
||||
control_data_label_.c_str());
|
||||
if (ret != 0) {
|
||||
LOG_WARN("Broadcast Windows service status failed, ret={}", ret);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
void Render::ResetLocalWindowsServiceState(bool clear_pending_sas) {
|
||||
last_windows_service_status_tick_ = 0;
|
||||
if (clear_pending_sas) {
|
||||
pending_windows_service_sas_.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
local_service_status_received_ = false;
|
||||
local_service_available_ = false;
|
||||
local_interactive_stage_.clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
void Render::HandleRecentConnections() {
|
||||
if (reload_recent_connections_ && main_renderer_) {
|
||||
uint32_t now_time = SDL_GetTicks();
|
||||
@@ -2470,7 +2692,7 @@ void Render::ProcessSdlEvent(const SDL_Event& event) {
|
||||
case SDL_EVENT_WINDOW_FOCUS_LOST:
|
||||
if (stream_window_ &&
|
||||
SDL_GetWindowID(stream_window_) == event.window.windowID) {
|
||||
ForceReleasePressedModifiers();
|
||||
ForceReleasePressedKeys();
|
||||
focus_on_stream_window_ = false;
|
||||
} else if (main_window_ &&
|
||||
SDL_GetWindowID(main_window_) == event.window.windowID) {
|
||||
@@ -2502,6 +2724,15 @@ void Render::ProcessSdlEvent(const SDL_Event& event) {
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_EVENT_KEY_DOWN:
|
||||
case SDL_EVENT_KEY_UP:
|
||||
if (keyboard_capturer_is_started_ && keyboard_capturer_uses_sdl_events_ &&
|
||||
focus_on_stream_window_ && stream_window_ &&
|
||||
SDL_GetWindowID(stream_window_) == event.key.windowID) {
|
||||
ProcessKeyboardEvent(event);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if (event.type == STREAM_REFRESH_EVENT) {
|
||||
auto* props = static_cast<SubStreamWindowProperties*>(event.user.data1);
|
||||
|
||||
+34
-4
@@ -44,6 +44,14 @@
|
||||
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;
|
||||
@@ -160,6 +168,9 @@ 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;
|
||||
@@ -272,6 +283,11 @@ class Render {
|
||||
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();
|
||||
@@ -325,8 +341,9 @@ class Render {
|
||||
private:
|
||||
int SendKeyCommand(int key_code, bool is_down);
|
||||
static bool IsModifierVkKey(int key_code);
|
||||
void UpdatePressedModifierState(int key_code, bool is_down);
|
||||
void ForceReleasePressedModifiers();
|
||||
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);
|
||||
@@ -360,6 +377,10 @@ class Render {
|
||||
|
||||
int AudioDeviceInit();
|
||||
int AudioDeviceDestroy();
|
||||
void HandleWindowsServiceIntegration();
|
||||
#if _WIN32
|
||||
void ResetLocalWindowsServiceState(bool clear_pending_sas);
|
||||
#endif
|
||||
|
||||
private:
|
||||
struct CDCache {
|
||||
@@ -456,6 +477,7 @@ class Render {
|
||||
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 focus_on_stream_window_ = false;
|
||||
bool main_window_minimized_ = false;
|
||||
@@ -511,11 +533,19 @@ class Render {
|
||||
std::string controlled_remote_id_ = "";
|
||||
std::string focused_remote_id_ = "";
|
||||
std::string remote_client_id_ = "";
|
||||
std::unordered_set<int> pressed_modifier_keys_;
|
||||
std::mutex pressed_modifier_keys_mutex_;
|
||||
std::unordered_set<int> pressed_keyboard_keys_;
|
||||
std::mutex pressed_keyboard_keys_mutex_;
|
||||
SDL_Event last_mouse_event;
|
||||
SDL_AudioStream* output_stream_;
|
||||
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;
|
||||
#endif
|
||||
|
||||
// stream window render
|
||||
SDL_Window* stream_window_ = nullptr;
|
||||
|
||||
+332
-18
@@ -17,11 +17,234 @@
|
||||
#include "platform.h"
|
||||
#include "rd_log.h"
|
||||
#include "render.h"
|
||||
#if _WIN32
|
||||
#include "interactive_state.h"
|
||||
#include "service_host.h"
|
||||
#endif
|
||||
|
||||
#define NV12_BUFFER_SIZE 1280 * 720 * 3 / 2
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
namespace {
|
||||
|
||||
int TranslateSdlKeypadScancodeToVk(SDL_Scancode scancode) {
|
||||
switch (scancode) {
|
||||
case SDL_SCANCODE_NUMLOCKCLEAR:
|
||||
return 0x90;
|
||||
case SDL_SCANCODE_KP_ENTER:
|
||||
return 0x0D;
|
||||
case SDL_SCANCODE_KP_0:
|
||||
return 0x60;
|
||||
case SDL_SCANCODE_KP_1:
|
||||
return 0x61;
|
||||
case SDL_SCANCODE_KP_2:
|
||||
return 0x62;
|
||||
case SDL_SCANCODE_KP_3:
|
||||
return 0x63;
|
||||
case SDL_SCANCODE_KP_4:
|
||||
return 0x64;
|
||||
case SDL_SCANCODE_KP_5:
|
||||
return 0x65;
|
||||
case SDL_SCANCODE_KP_6:
|
||||
return 0x66;
|
||||
case SDL_SCANCODE_KP_7:
|
||||
return 0x67;
|
||||
case SDL_SCANCODE_KP_8:
|
||||
return 0x68;
|
||||
case SDL_SCANCODE_KP_9:
|
||||
return 0x69;
|
||||
case SDL_SCANCODE_KP_PERIOD:
|
||||
case SDL_SCANCODE_KP_COMMA:
|
||||
return 0x6E;
|
||||
case SDL_SCANCODE_KP_DIVIDE:
|
||||
return 0x6F;
|
||||
case SDL_SCANCODE_KP_MULTIPLY:
|
||||
return 0x6A;
|
||||
case SDL_SCANCODE_KP_MINUS:
|
||||
return 0x6D;
|
||||
case SDL_SCANCODE_KP_PLUS:
|
||||
return 0x6B;
|
||||
case SDL_SCANCODE_KP_EQUALS:
|
||||
return 0xBB;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int TranslateSdlKeyboardEventToVk(const SDL_KeyboardEvent& event) {
|
||||
const int keypad_key_code = TranslateSdlKeypadScancodeToVk(event.scancode);
|
||||
if (keypad_key_code >= 0) {
|
||||
return keypad_key_code;
|
||||
}
|
||||
|
||||
const int key = static_cast<int>(event.key);
|
||||
if (key >= 'a' && key <= 'z') {
|
||||
return key - 'a' + 0x41;
|
||||
}
|
||||
if (key >= 'A' && key <= 'Z') {
|
||||
return key;
|
||||
}
|
||||
if (key >= '0' && key <= '9') {
|
||||
return key;
|
||||
}
|
||||
|
||||
switch (key) {
|
||||
case ';':
|
||||
return 0xBA;
|
||||
case '\'':
|
||||
return 0xDE;
|
||||
case '`':
|
||||
return 0xC0;
|
||||
case ',':
|
||||
return 0xBC;
|
||||
case '.':
|
||||
return 0xBE;
|
||||
case '/':
|
||||
return 0xBF;
|
||||
case '\\':
|
||||
return 0xDC;
|
||||
case '[':
|
||||
return 0xDB;
|
||||
case ']':
|
||||
return 0xDD;
|
||||
case '-':
|
||||
return 0xBD;
|
||||
case '=':
|
||||
return 0xBB;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
switch (event.scancode) {
|
||||
case SDL_SCANCODE_ESCAPE:
|
||||
return 0x1B;
|
||||
case SDL_SCANCODE_RETURN:
|
||||
return 0x0D;
|
||||
case SDL_SCANCODE_SPACE:
|
||||
return 0x20;
|
||||
case SDL_SCANCODE_BACKSPACE:
|
||||
return 0x08;
|
||||
case SDL_SCANCODE_TAB:
|
||||
return 0x09;
|
||||
case SDL_SCANCODE_PRINTSCREEN:
|
||||
return 0x2C;
|
||||
case SDL_SCANCODE_SCROLLLOCK:
|
||||
return 0x91;
|
||||
case SDL_SCANCODE_PAUSE:
|
||||
return 0x13;
|
||||
case SDL_SCANCODE_INSERT:
|
||||
return 0x2D;
|
||||
case SDL_SCANCODE_DELETE:
|
||||
return 0x2E;
|
||||
case SDL_SCANCODE_HOME:
|
||||
return 0x24;
|
||||
case SDL_SCANCODE_END:
|
||||
return 0x23;
|
||||
case SDL_SCANCODE_PAGEUP:
|
||||
return 0x21;
|
||||
case SDL_SCANCODE_PAGEDOWN:
|
||||
return 0x22;
|
||||
case SDL_SCANCODE_LEFT:
|
||||
return 0x25;
|
||||
case SDL_SCANCODE_RIGHT:
|
||||
return 0x27;
|
||||
case SDL_SCANCODE_UP:
|
||||
return 0x26;
|
||||
case SDL_SCANCODE_DOWN:
|
||||
return 0x28;
|
||||
case SDL_SCANCODE_F1:
|
||||
return 0x70;
|
||||
case SDL_SCANCODE_F2:
|
||||
return 0x71;
|
||||
case SDL_SCANCODE_F3:
|
||||
return 0x72;
|
||||
case SDL_SCANCODE_F4:
|
||||
return 0x73;
|
||||
case SDL_SCANCODE_F5:
|
||||
return 0x74;
|
||||
case SDL_SCANCODE_F6:
|
||||
return 0x75;
|
||||
case SDL_SCANCODE_F7:
|
||||
return 0x76;
|
||||
case SDL_SCANCODE_F8:
|
||||
return 0x77;
|
||||
case SDL_SCANCODE_F9:
|
||||
return 0x78;
|
||||
case SDL_SCANCODE_F10:
|
||||
return 0x79;
|
||||
case SDL_SCANCODE_F11:
|
||||
return 0x7A;
|
||||
case SDL_SCANCODE_F12:
|
||||
return 0x7B;
|
||||
case SDL_SCANCODE_CAPSLOCK:
|
||||
return 0x14;
|
||||
case SDL_SCANCODE_LSHIFT:
|
||||
return 0xA0;
|
||||
case SDL_SCANCODE_RSHIFT:
|
||||
return 0xA1;
|
||||
case SDL_SCANCODE_LCTRL:
|
||||
return 0xA2;
|
||||
case SDL_SCANCODE_RCTRL:
|
||||
return 0xA3;
|
||||
case SDL_SCANCODE_LALT:
|
||||
return 0xA4;
|
||||
case SDL_SCANCODE_RALT:
|
||||
return 0xA5;
|
||||
case SDL_SCANCODE_LGUI:
|
||||
return 0x5B;
|
||||
case SDL_SCANCODE_RGUI:
|
||||
return 0x5C;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
constexpr uint32_t kSecureDesktopInputLogIntervalMs = 2000;
|
||||
|
||||
bool BuildAbsoluteMousePosition(const std::vector<DisplayInfo>& displays,
|
||||
int display_index, float normalized_x,
|
||||
float normalized_y, int* absolute_x_out,
|
||||
int* absolute_y_out) {
|
||||
if (absolute_x_out == nullptr || absolute_y_out == nullptr ||
|
||||
display_index < 0 || display_index >= static_cast<int>(displays.size())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const DisplayInfo& display = displays[display_index];
|
||||
if (display.width <= 0 || display.height <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const float clamped_x = std::clamp(normalized_x, 0.0f, 1.0f);
|
||||
const float clamped_y = std::clamp(normalized_y, 0.0f, 1.0f);
|
||||
*absolute_x_out = static_cast<int>(clamped_x * display.width) + display.left;
|
||||
*absolute_y_out = static_cast<int>(clamped_y * display.height) + display.top;
|
||||
return true;
|
||||
}
|
||||
|
||||
void LogSecureDesktopInputBlocked(uint32_t* last_tick, const char* side,
|
||||
const char* stage) {
|
||||
if (last_tick == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint32_t now = static_cast<uint32_t>(SDL_GetTicks());
|
||||
if (*last_tick != 0 && now - *last_tick < kSecureDesktopInputLogIntervalMs) {
|
||||
return;
|
||||
}
|
||||
|
||||
*last_tick = now;
|
||||
LOG_WARN(
|
||||
"{} secure-desktop input blocked, stage={}, normal SendInput path "
|
||||
"cannot drive the Windows password UI",
|
||||
side != nullptr ? side : "unknown", stage != nullptr ? stage : "");
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
void Render::OnSignalMessageCb(const char* message, size_t size,
|
||||
void* user_data) {
|
||||
Render* render = (Render*)user_data;
|
||||
@@ -100,29 +323,29 @@ bool Render::IsModifierVkKey(int key_code) {
|
||||
}
|
||||
}
|
||||
|
||||
void Render::UpdatePressedModifierState(int key_code, bool is_down) {
|
||||
if (!IsModifierVkKey(key_code)) {
|
||||
void Render::TrackPressedKeyState(int key_code, bool is_down) {
|
||||
if (!IsWaylandSession() && !IsModifierVkKey(key_code)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(pressed_modifier_keys_mutex_);
|
||||
std::lock_guard<std::mutex> lock(pressed_keyboard_keys_mutex_);
|
||||
if (is_down) {
|
||||
pressed_modifier_keys_.insert(key_code);
|
||||
pressed_keyboard_keys_.insert(key_code);
|
||||
} else {
|
||||
pressed_modifier_keys_.erase(key_code);
|
||||
pressed_keyboard_keys_.erase(key_code);
|
||||
}
|
||||
}
|
||||
|
||||
void Render::ForceReleasePressedModifiers() {
|
||||
void Render::ForceReleasePressedKeys() {
|
||||
std::vector<int> pressed_keys;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(pressed_modifier_keys_mutex_);
|
||||
if (pressed_modifier_keys_.empty()) {
|
||||
std::lock_guard<std::mutex> lock(pressed_keyboard_keys_mutex_);
|
||||
if (pressed_keyboard_keys_.empty()) {
|
||||
return;
|
||||
}
|
||||
pressed_keys.assign(pressed_modifier_keys_.begin(),
|
||||
pressed_modifier_keys_.end());
|
||||
pressed_modifier_keys_.clear();
|
||||
pressed_keys.assign(pressed_keyboard_keys_.begin(),
|
||||
pressed_keyboard_keys_.end());
|
||||
pressed_keyboard_keys_.clear();
|
||||
}
|
||||
|
||||
for (int key_code : pressed_keys) {
|
||||
@@ -158,11 +381,28 @@ int Render::SendKeyCommand(int key_code, bool is_down) {
|
||||
}
|
||||
}
|
||||
|
||||
UpdatePressedModifierState(key_code, is_down);
|
||||
TrackPressedKeyState(key_code, is_down);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Render::ProcessKeyboardEvent(const SDL_Event& event) {
|
||||
if (event.type != SDL_EVENT_KEY_DOWN && event.type != SDL_EVENT_KEY_UP) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int key_code = TranslateSdlKeyboardEventToVk(event.key);
|
||||
if (key_code < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SendKeyCommand(key_code, event.type == SDL_EVENT_KEY_DOWN);
|
||||
}
|
||||
|
||||
int Render::ProcessMouseEvent(const SDL_Event& event) {
|
||||
controlled_remote_id_ = "";
|
||||
RemoteAction remote_action;
|
||||
@@ -716,16 +956,31 @@ void Render::OnReceiveDataBufferCb(const char* data, size_t size,
|
||||
}
|
||||
|
||||
std::string json_str(data, size);
|
||||
RemoteAction remote_action;
|
||||
|
||||
try {
|
||||
remote_action.from_json(json_str);
|
||||
} catch (const std::exception& e) {
|
||||
LOG_ERROR("Failed to parse RemoteAction JSON: {}", e.what());
|
||||
RemoteAction remote_action{};
|
||||
if (!remote_action.from_json(json_str)) {
|
||||
LOG_ERROR("Failed to parse RemoteAction JSON payload");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string remote_id(user_id, user_id_size);
|
||||
if (remote_action.type == ControlType::service_status) {
|
||||
auto props_it = render->client_properties_.find(remote_id);
|
||||
if (props_it != render->client_properties_.end()) {
|
||||
render->ApplyRemoteServiceStatus(*props_it->second, remote_action.ss);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (remote_action.type == ControlType::service_command) {
|
||||
#if _WIN32
|
||||
if (remote_action.c.flag == ServiceCommandFlag::send_sas) {
|
||||
render->pending_windows_service_sas_.store(true,
|
||||
std::memory_order_relaxed);
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// std::shared_lock lock(render->client_properties_mutex_);
|
||||
if (remote_action.type == ControlType::host_infomation) {
|
||||
if (render->client_properties_.find(remote_id) !=
|
||||
@@ -755,6 +1010,60 @@ void Render::OnReceiveDataBufferCb(const char* data, size_t size,
|
||||
}
|
||||
} else {
|
||||
// remote
|
||||
#if _WIN32
|
||||
if (render->local_service_status_received_ &&
|
||||
render->local_service_available_ &&
|
||||
IsSecureDesktopInteractionRequired(render->local_interactive_stage_)) {
|
||||
if (remote_action.type == ControlType::mouse) {
|
||||
int absolute_x = 0;
|
||||
int absolute_y = 0;
|
||||
if (!BuildAbsoluteMousePosition(render->display_info_list_,
|
||||
render->selected_display_,
|
||||
remote_action.m.x, remote_action.m.y,
|
||||
&absolute_x, &absolute_y)) {
|
||||
LOG_WARN(
|
||||
"Secure desktop mouse injection skipped, invalid display "
|
||||
"mapping: display_index={}, x={}, y={}",
|
||||
render->selected_display_, remote_action.m.x, remote_action.m.y);
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string response = SendCrossDeskSecureDesktopMouseInput(
|
||||
absolute_x, absolute_y, remote_action.m.s,
|
||||
static_cast<int>(remote_action.m.flag), 1000);
|
||||
auto json = nlohmann::json::parse(response, nullptr, false);
|
||||
if (json.is_discarded() || !json.value("ok", false)) {
|
||||
LogSecureDesktopInputBlocked(
|
||||
&render->last_local_secure_input_block_log_tick_, "local",
|
||||
render->local_interactive_stage_.c_str());
|
||||
LOG_WARN(
|
||||
"Secure desktop mouse injection failed, x={}, y={}, wheel={}, "
|
||||
"flag={}, response={}",
|
||||
absolute_x, absolute_y, remote_action.m.s,
|
||||
static_cast<int>(remote_action.m.flag), response);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (remote_action.type == ControlType::keyboard) {
|
||||
const int key_code = static_cast<int>(remote_action.k.key_value);
|
||||
const bool is_down = remote_action.k.flag == KeyFlag::key_down;
|
||||
const std::string response =
|
||||
SendCrossDeskSecureDesktopKeyInput(key_code, is_down, 1000);
|
||||
auto json = nlohmann::json::parse(response, nullptr, false);
|
||||
if (json.is_discarded() || !json.value("ok", false)) {
|
||||
LogSecureDesktopInputBlocked(
|
||||
&render->last_local_secure_input_block_log_tick_, "local",
|
||||
render->local_interactive_stage_.c_str());
|
||||
LOG_WARN(
|
||||
"Secure desktop keyboard injection failed, key_code={}, "
|
||||
"is_down={}, response={}",
|
||||
key_code, is_down, response);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (remote_action.type == ControlType::mouse && render->mouse_controller_) {
|
||||
render->mouse_controller_->SendMouseCommand(remote_action,
|
||||
render->selected_display_);
|
||||
@@ -848,6 +1157,7 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char* user_id,
|
||||
|
||||
switch (status) {
|
||||
case ConnectionStatus::Connected: {
|
||||
render->ResetRemoteServiceStatus(*props);
|
||||
{
|
||||
RemoteAction remote_action;
|
||||
remote_action.i.display_num = render->display_info_list_.size();
|
||||
@@ -911,6 +1221,7 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char* user_id,
|
||||
case ConnectionStatus::Closed: {
|
||||
props->connection_established_ = false;
|
||||
props->enable_mouse_control_ = false;
|
||||
render->ResetRemoteServiceStatus(*props);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(props->video_frame_mutex_);
|
||||
@@ -961,6 +1272,9 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char* user_id,
|
||||
|
||||
switch (status) {
|
||||
case ConnectionStatus::Connected: {
|
||||
#if _WIN32
|
||||
render->last_windows_service_status_tick_ = 0;
|
||||
#endif
|
||||
{
|
||||
RemoteAction remote_action;
|
||||
remote_action.i.display_num = render->display_info_list_.size();
|
||||
|
||||
Reference in New Issue
Block a user