[fix] fix mouse contorl error

This commit is contained in:
dijunkun
2025-04-09 15:47:24 +08:00
parent d828bd736d
commit badcd6a05c
6 changed files with 137 additions and 89 deletions

View File

@@ -57,8 +57,8 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& properties) {
: ICON_FA_COMPUTER_MOUSE; : ICON_FA_COMPUTER_MOUSE;
if (ImGui::Button(mouse.c_str(), ImVec2(25, 25))) { if (ImGui::Button(mouse.c_str(), ImVec2(25, 25))) {
if (properties->connection_established_) { if (properties->connection_established_) {
control_mouse_ = !control_mouse_;
start_keyboard_capturer_ = !start_keyboard_capturer_; start_keyboard_capturer_ = !start_keyboard_capturer_;
properties->control_mouse_ = !properties->control_mouse_;
properties->mouse_control_button_pressed_ = properties->mouse_control_button_pressed_ =
!properties->mouse_control_button_pressed_; !properties->mouse_control_button_pressed_;
properties->mouse_control_button_label_ = properties->mouse_control_button_label_ =

View File

@@ -78,9 +78,8 @@ int Render::RemoteWindow() {
ConnectTo(remote_id_, remote_password_, false); ConnectTo(remote_id_, remote_password_, false);
} }
std::string client_id = "C-" + remote_id_; if (client_properties_.find(remote_id_) != client_properties_.end()) {
if (client_properties_.find(client_id) != client_properties_.end()) { auto props = client_properties_[remote_id_];
auto props = client_properties_[client_id];
if (props->rejoin_) { if (props->rejoin_) {
ConnectTo(remote_id_, remote_password_, ConnectTo(remote_id_, remote_password_,
client_properties_[remote_id_]->remember_password_); client_properties_[remote_id_]->remember_password_);
@@ -107,41 +106,37 @@ static int InputTextCallback(ImGuiInputTextCallbackData *data) {
return 0; return 0;
} }
int Render::ConnectTo(const std::string &host_name, const char *password, int Render::ConnectTo(const std::string &remote_id, const char *password,
bool remember_password) { bool remember_password) {
LOG_INFO("Connect to [{}]", host_name); LOG_INFO("Connect to [{}]", remote_id);
std::string client_id = "C-" + host_name; if (client_properties_.find(remote_id) == client_properties_.end()) {
if (client_properties_.find(client_id) == client_properties_.end()) { client_properties_[remote_id] =
client_properties_[client_id] =
std::make_shared<SubStreamWindowProperties>(); std::make_shared<SubStreamWindowProperties>();
auto props = client_properties_[remote_id];
props->local_id_ = "C-" + remote_id;
props->remote_id_ = remote_id;
memcpy(&props->params_, &params_, sizeof(Params));
props->params_.user_id = props->local_id_.c_str();
props->peer_ = CreatePeer(&props->params_);
if (props->peer_) {
LOG_INFO("[{}] Create peer instance successful", props->local_id_);
Init(props->peer_);
LOG_INFO("[{}] Peer init finish", props->local_id_);
} else {
LOG_INFO("Create peer [{}] instance failed", props->local_id_);
} }
auto props = client_properties_[client_id];
props->connection_status_ = ConnectionStatus::Connecting; props->connection_status_ = ConnectionStatus::Connecting;
props->remember_password_ = remember_password; props->remember_password_ = remember_password;
memcpy(props->remote_password_, password, 6); memcpy(props->remote_password_, password, 6);
}
int ret = -1; int ret = -1;
auto props = client_properties_[remote_id];
if (!props->connection_established_) { if (!props->connection_established_) {
if (peer_map_.find(client_id) == peer_map_.end()) { ret = JoinConnection(props->peer_, remote_id.c_str(), password);
memcpy(&props->params_, &params_, sizeof(Params));
props->params_.user_id = client_id.c_str();
peer_map_[client_id] = CreatePeer(&props->params_);
}
if (peer_map_[client_id]) {
LOG_INFO("[{}] Create peer instance successful", client_id);
Init(peer_map_[client_id]);
LOG_INFO("[{}] Peer init finish", client_id);
} else {
LOG_INFO("Create peer [{}] instance failed", client_id);
}
ret = JoinConnection(peer_map_[client_id], host_name.c_str(), password);
if (0 == ret) { if (0 == ret) {
is_client_mode_ = true;
props->rejoin_ = false; props->rejoin_ = false;
} else { } else {
props->rejoin_ = true; props->rejoin_ = true;

View File

@@ -431,10 +431,12 @@ int Render::CreateRtcConnection() {
mouse_controller_is_started_ = false; mouse_controller_is_started_ = false;
} }
if (start_keyboard_capturer_ && !keyboard_capturer_is_started_) { if (start_keyboard_capturer_ && foucs_on_stream_window_) {
if (!keyboard_capturer_is_started_) {
StartKeyboardCapturer(); StartKeyboardCapturer();
keyboard_capturer_is_started_ = true; keyboard_capturer_is_started_ = true;
} else if (!start_keyboard_capturer_ && keyboard_capturer_is_started_) { }
} else if (keyboard_capturer_is_started_) {
StopKeyboardCapturer(); StopKeyboardCapturer();
keyboard_capturer_is_started_ = false; keyboard_capturer_is_started_ = false;
} }
@@ -858,8 +860,7 @@ int Render::Run() {
} }
std::string host_name = it.first; std::string host_name = it.first;
PeerPtr* peer_client = peer_map_[host_name]; if (props->peer_) {
if (peer_client) {
std::string client_id; std::string client_id;
if (host_name == client_id_) { if (host_name == client_id_) {
client_id = "C-" + std::string(client_id_); client_id = "C-" + std::string(client_id_);
@@ -867,9 +868,9 @@ int Render::Run() {
client_id = client_id_; client_id = client_id_;
} }
LOG_INFO("[{}] Leave connection [{}]", client_id, host_name); LOG_INFO("[{}] Leave connection [{}]", client_id, host_name);
LeaveConnection(peer_client, host_name.c_str()); LeaveConnection(props->peer_, host_name.c_str());
LOG_INFO("Destroy peer [{}]", client_id); LOG_INFO("Destroy peer [{}]", client_id);
DestroyPeer(&peer_client); DestroyPeer(&props->peer_);
} }
props->streaming_ = false; props->streaming_ = false;
@@ -1000,10 +1001,51 @@ int Render::Run() {
SDL_UpdateTexture(props->stream_texture_, NULL, props->dst_buffer_, SDL_UpdateTexture(props->stream_texture_, NULL, props->dst_buffer_,
props->texture_width_); props->texture_width_);
} else { } else if (event.type == SDL_MOUSEMOTION ||
event.type == SDL_MOUSEBUTTONDOWN ||
event.type == SDL_MOUSEBUTTONUP ||
event.type == SDL_MOUSEWHEEL) {
if (!foucs_on_stream_window_) {
continue;
}
for (auto& it : client_properties_) {
auto props = it.second;
if (props->control_mouse_) {
ProcessMouseEvent(event); ProcessMouseEvent(event);
} }
} }
} else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
if (stream_window_) {
if (SDL_GetWindowID(stream_window_) == event.window.windowID) {
foucs_on_stream_window_ = true;
LOG_INFO("Focus on stream window");
}
}
if (main_window_) {
if (SDL_GetWindowID(main_window_) == event.window.windowID) {
foucs_on_main_window_ = true;
LOG_INFO("Focus on main window");
}
}
} else if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
if (stream_window_) {
if (SDL_GetWindowID(stream_window_) == event.window.windowID) {
foucs_on_stream_window_ = false;
LOG_INFO("Lost focus on stream window");
}
}
if (main_window_) {
if (SDL_GetWindowID(main_window_) == event.window.windowID) {
foucs_on_main_window_ = false;
LOG_INFO("Lost focus on main window");
}
}
} else {
}
}
if (reload_recent_connections_ && main_renderer_) { if (reload_recent_connections_ && main_renderer_) {
// loal recent connection thumbnails after saving for 0.05 second // loal recent connection thumbnails after saving for 0.05 second
@@ -1103,7 +1145,7 @@ int Render::Run() {
} }
std::string host_name = it.first; std::string host_name = it.first;
PeerPtr* peer_client = peer_map_[host_name]; PeerPtr* peer_client = props->peer_;
if (peer_client) { if (peer_client) {
std::string client_id; std::string client_id;
if (host_name == client_id_) { if (host_name == client_id_) {

View File

@@ -31,6 +31,9 @@ class Render {
public: public:
struct SubStreamWindowProperties { struct SubStreamWindowProperties {
Params params_; Params params_;
PeerPtr *peer_ = nullptr;
std::string local_id_ = "";
std::string remote_id_ = "";
bool exit_ = false; bool exit_ = false;
bool signal_connected_ = false; bool signal_connected_ = false;
SignalStatus signal_status_ = SignalStatus::SignalClosed; SignalStatus signal_status_ = SignalStatus::SignalClosed;
@@ -40,6 +43,7 @@ class Render {
bool mouse_control_button_pressed_ = false; bool mouse_control_button_pressed_ = false;
bool mouse_controller_is_started_ = false; bool mouse_controller_is_started_ = false;
bool audio_capture_button_pressed_ = false; bool audio_capture_button_pressed_ = false;
bool control_mouse_ = false;
bool streaming_ = false; bool streaming_ = false;
bool is_control_bar_in_left_ = true; bool is_control_bar_in_left_ = true;
bool control_bar_hovered_ = false; bool control_bar_hovered_ = false;
@@ -117,7 +121,7 @@ class Render {
private: private:
int CreateRtcConnection(); int CreateRtcConnection();
int ConnectTo(const std::string &host_name, const char *password, int ConnectTo(const std::string &remote_id, const char *password,
bool remember_password); bool remember_password);
int CreateMainWindow(); int CreateMainWindow();
int DestroyMainWindow(); int DestroyMainWindow();
@@ -243,6 +247,8 @@ class Render {
bool screen_capturer_is_started_ = false; bool screen_capturer_is_started_ = false;
bool start_keyboard_capturer_ = false; bool start_keyboard_capturer_ = false;
bool keyboard_capturer_is_started_ = false; bool keyboard_capturer_is_started_ = false;
bool foucs_on_main_window_ = false;
bool foucs_on_stream_window_ = false;
bool audio_capture_ = false; bool audio_capture_ = false;
int main_window_width_real_ = 720; int main_window_width_real_ = 720;
int main_window_height_real_ = 540; int main_window_height_real_ = 540;
@@ -334,7 +340,6 @@ class Render {
double regenerate_password_start_time_ = 0; double regenerate_password_start_time_ = 0;
SignalStatus signal_status_ = SignalStatus::SignalClosed; SignalStatus signal_status_ = SignalStatus::SignalClosed;
std::string signal_status_str_ = ""; std::string signal_status_str_ = "";
std::string connection_status_str_ = "";
bool signal_connected_ = false; bool signal_connected_ = false;
PeerPtr *peer_ = nullptr; PeerPtr *peer_ = nullptr;
PeerPtr *peer_reserved_ = nullptr; PeerPtr *peer_reserved_ = nullptr;
@@ -368,7 +373,6 @@ class Render {
/* ------ sub stream window property start ------ */ /* ------ sub stream window property start ------ */
std::unordered_map<std::string, std::shared_ptr<SubStreamWindowProperties>> std::unordered_map<std::string, std::shared_ptr<SubStreamWindowProperties>>
client_properties_; client_properties_;
std::unordered_map<std::string, PeerPtr *> peer_map_;
/* ------ stream window property end ------ */ /* ------ stream window property end ------ */
std::unordered_map<std::string, std::shared_ptr<SubStreamWindowProperties>> std::unordered_map<std::string, std::shared_ptr<SubStreamWindowProperties>>

View File

@@ -43,8 +43,6 @@ int Render::ProcessMouseEvent(SDL_Event &event) {
video_height = props->video_height_; video_height = props->video_height_;
render_width = props->stream_render_rect_.w; render_width = props->stream_render_rect_.w;
render_height = props->stream_render_rect_.h; render_height = props->stream_render_rect_.h;
}
}
float ratio_x = (float)video_width / (float)render_width; float ratio_x = (float)video_width / (float)render_width;
float ratio_y = (float)video_height / (float)render_height; float ratio_y = (float)video_height / (float)render_height;
@@ -61,7 +59,8 @@ int Render::ProcessMouseEvent(SDL_Event &event) {
remote_action.m.flag = MouseFlag::right_down; remote_action.m.flag = MouseFlag::right_down;
} }
remote_action.m.flag = MouseFlag::move; remote_action.m.flag = MouseFlag::move;
SendDataFrame(peer_, (const char *)&remote_action, sizeof(remote_action)); SendDataFrame(peer_, (const char *)&remote_action,
sizeof(remote_action));
} else if (SDL_MOUSEBUTTONUP == event.type) { } else if (SDL_MOUSEBUTTONUP == event.type) {
remote_action.type = ControlType::mouse; remote_action.type = ControlType::mouse;
if (SDL_BUTTON_LEFT == event.button.button) { if (SDL_BUTTON_LEFT == event.button.button) {
@@ -70,11 +69,15 @@ int Render::ProcessMouseEvent(SDL_Event &event) {
remote_action.m.flag = MouseFlag::right_up; remote_action.m.flag = MouseFlag::right_up;
} }
remote_action.m.flag = MouseFlag::move; remote_action.m.flag = MouseFlag::move;
SendDataFrame(peer_, (const char *)&remote_action, sizeof(remote_action)); SendDataFrame(peer_, (const char *)&remote_action,
sizeof(remote_action));
} else if (SDL_MOUSEMOTION == event.type) { } else if (SDL_MOUSEMOTION == event.type) {
remote_action.type = ControlType::mouse; remote_action.type = ControlType::mouse;
remote_action.m.flag = MouseFlag::move; remote_action.m.flag = MouseFlag::move;
SendDataFrame(peer_, (const char *)&remote_action, sizeof(remote_action)); SendDataFrame(peer_, (const char *)&remote_action,
sizeof(remote_action));
}
}
} }
return 0; return 0;
@@ -87,9 +90,13 @@ void Render::SdlCaptureAudioIn(void *userdata, Uint8 *stream, int len) {
} }
if (1) { if (1) {
if ("Connected" == render->connection_status_str_) { for (auto it : render->client_properties_) {
SendAudioFrame(render->peer_, (const char *)stream, len); auto props = it.second;
if (props->connection_status_ == ConnectionStatus::Connected) {
SendAudioFrame(props->peer_, (const char *)stream, len);
} }
}
} else { } else {
memcpy(render->audio_buffer_, stream, len); memcpy(render->audio_buffer_, stream, len);
render->audio_len_ = len; render->audio_len_ = len;
@@ -102,8 +109,11 @@ void Render::SdlCaptureAudioOut([[maybe_unused]] void *userdata,
[[maybe_unused]] Uint8 *stream, [[maybe_unused]] Uint8 *stream,
[[maybe_unused]] int len) { [[maybe_unused]] int len) {
// Render *render = (Render *)userdata; // Render *render = (Render *)userdata;
// if ("Connected" == render->connection_status_str_) { // for (auto it : render->client_properties_) {
// SendAudioFrame(render->peer_, (const char *)stream, len); // auto props = it.second;
// if (props->connection_status_ == SignalStatus::SignalConnected) {
// SendAudioFrame(props->peer_, (const char *)stream, len);
// }
// } // }
// if (!render->audio_buffer_fresh_) { // if (!render->audio_buffer_fresh_) {
@@ -223,15 +233,15 @@ void Render::OnSignalStatusCb(SignalStatus status, const char *user_id,
if (!render) { if (!render) {
return; return;
} }
std::string remote_id(user_id, user_id_size);
if (remote_id == render->client_id_) { std::string client_id(user_id, user_id_size);
if (client_id == render->client_id_) {
render->signal_status_ = status; render->signal_status_ = status;
if (SignalStatus::SignalConnecting == status) { if (SignalStatus::SignalConnecting == status) {
render->signal_connected_ = false; render->signal_connected_ = false;
} else if (SignalStatus::SignalConnected == status) { } else if (SignalStatus::SignalConnected == status) {
render->signal_connected_ = true; render->signal_connected_ = true;
LOG_INFO("[{}] connected to signal server", remote_id); LOG_INFO("[{}] connected to signal server", client_id);
} else if (SignalStatus::SignalFailed == status) { } else if (SignalStatus::SignalFailed == status) {
render->signal_connected_ = false; render->signal_connected_ = false;
} else if (SignalStatus::SignalClosed == status) { } else if (SignalStatus::SignalClosed == status) {
@@ -242,6 +252,11 @@ void Render::OnSignalStatusCb(SignalStatus status, const char *user_id,
render->signal_connected_ = false; render->signal_connected_ = false;
} }
} else { } else {
if (client_id.rfind("C-", 0) != 0) {
return;
}
std::string remote_id(client_id.begin() + 2, client_id.end());
if (render->client_properties_.find(remote_id) == if (render->client_properties_.find(remote_id) ==
render->client_properties_.end()) { render->client_properties_.end()) {
return; return;
@@ -292,11 +307,8 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id,
props->connection_status_ = status; props->connection_status_ = status;
render->show_connection_status_window_ = true; render->show_connection_status_window_ = true;
if (ConnectionStatus::Connecting == status) { if (ConnectionStatus::Connecting == status) {
render->connection_status_str_ = "Connecting";
} else if (ConnectionStatus::Gathering == status) { } else if (ConnectionStatus::Gathering == status) {
render->connection_status_str_ = "Gathering";
} else if (ConnectionStatus::Connected == status) { } else if (ConnectionStatus::Connected == status) {
render->connection_status_str_ = "Connected";
if (!render->need_to_create_stream_window_) { if (!render->need_to_create_stream_window_) {
render->need_to_create_stream_window_ = true; render->need_to_create_stream_window_ = true;
} }
@@ -324,18 +336,15 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id,
(int)(render->stream_window_height_ - render->title_bar_height_); (int)(render->stream_window_height_ - render->title_bar_height_);
} }
if (render->peer_reserved_ || !render->is_client_mode_) { if (!render->is_client_mode_) {
render->start_screen_capturer_ = true; render->start_screen_capturer_ = true;
render->start_mouse_controller_ = true; render->start_mouse_controller_ = true;
} }
} else if (ConnectionStatus::Disconnected == status) { } else if (ConnectionStatus::Disconnected == status) {
render->connection_status_str_ = "Disconnected";
render->password_validating_time_ = 0; render->password_validating_time_ = 0;
} else if (ConnectionStatus::Failed == status) { } else if (ConnectionStatus::Failed == status) {
render->connection_status_str_ = "Failed";
render->password_validating_time_ = 0; render->password_validating_time_ = 0;
} else if (ConnectionStatus::Closed == status) { } else if (ConnectionStatus::Closed == status) {
render->connection_status_str_ = "Closed";
render->password_validating_time_ = 0; render->password_validating_time_ = 0;
render->start_screen_capturer_ = false; render->start_screen_capturer_ = false;
render->start_mouse_controller_ = false; render->start_mouse_controller_ = false;
@@ -357,7 +366,6 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id,
props->texture_width_); props->texture_width_);
} }
} else if (ConnectionStatus::IncorrectPassword == status) { } else if (ConnectionStatus::IncorrectPassword == status) {
render->connection_status_str_ = "Incorrect password";
render->password_validating_ = false; render->password_validating_ = false;
render->password_validating_time_++; render->password_validating_time_++;
if (render->connect_button_pressed_) { if (render->connect_button_pressed_) {
@@ -368,7 +376,6 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id,
: localization::connect[render->localization_language_index_]; : localization::connect[render->localization_language_index_];
} }
} else if (ConnectionStatus::NoSuchTransmissionId == status) { } else if (ConnectionStatus::NoSuchTransmissionId == status) {
render->connection_status_str_ = "No such transmission id";
if (render->connect_button_pressed_) { if (render->connect_button_pressed_) {
props->connection_established_ = false; props->connection_established_ = false;
render->connect_button_label_ = render->connect_button_label_ =