mirror of
https://github.com/kunkundi/crossdesk.git
synced 2026-06-11 10:04:18 +08:00
[fix] fix Wayland reconnect black screen by keeping capturer warm and also fix Wayland mouse control
This commit is contained in:
@@ -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 <functional>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "device_controller.h"
|
||||
|
||||
struct DBusConnection;
|
||||
struct DBusMessageIter;
|
||||
|
||||
namespace crossdesk {
|
||||
|
||||
class MouseController : public DeviceController {
|
||||
@@ -26,18 +32,47 @@ 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;
|
||||
bool using_shared_wayland_session_ = false;
|
||||
};
|
||||
} // namespace crossdesk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user