mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 12:15:34 +08:00
Fix crash caused by screen capturer or mouse controller init failed on Linux and MacOS
This commit is contained in:
@@ -13,6 +13,7 @@ int MouseController::Init(int screen_width, int screen_height) {
|
||||
uinput_fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
|
||||
if (uinput_fd_ < 0) {
|
||||
LOG_ERROR("Cannot open device: /dev/uinput");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ioctl(uinput_fd_, UI_SET_EVBIT, EV_KEY);
|
||||
@@ -35,15 +36,18 @@ int MouseController::Init(int screen_width, int screen_height) {
|
||||
uidev.absmin[ABS_Y] = 0;
|
||||
uidev.absmax[ABS_Y] = screen_height_;
|
||||
|
||||
write(uinput_fd_, &uidev, sizeof(uidev));
|
||||
int res_uidev = write(uinput_fd_, &uidev, sizeof(uidev));
|
||||
ioctl(uinput_fd_, UI_DEV_CREATE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MouseController::Destroy() {
|
||||
if (uinput_fd_) {
|
||||
ioctl(uinput_fd_, UI_DEV_DESTROY);
|
||||
close(uinput_fd_);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MouseController::SendCommand(RemoteAction remote_action) {
|
||||
int mouse_pos_x = remote_action.m.x * screen_width_ / 1280;
|
||||
@@ -71,6 +75,7 @@ int MouseController::SendCommand(RemoteAction remote_action) {
|
||||
}
|
||||
|
||||
void MouseController::SimulateKeyDown(int fd, int kval) {
|
||||
int res_ev = 0;
|
||||
struct input_event event;
|
||||
memset(&event, 0, sizeof(event));
|
||||
gettimeofday(&event.time, 0);
|
||||
@@ -78,15 +83,16 @@ void MouseController::SimulateKeyDown(int fd, int kval) {
|
||||
event.type = EV_KEY;
|
||||
event.value = 1;
|
||||
event.code = kval;
|
||||
write(fd, &event, sizeof(event));
|
||||
res_ev = write(fd, &event, sizeof(event));
|
||||
|
||||
event.type = EV_SYN;
|
||||
event.value = 0;
|
||||
event.code = SYN_REPORT;
|
||||
write(fd, &event, sizeof(event));
|
||||
res_ev = write(fd, &event, sizeof(event));
|
||||
}
|
||||
|
||||
void MouseController::SimulateKeyUp(int fd, int kval) {
|
||||
int res_ev = 0;
|
||||
struct input_event event;
|
||||
memset(&event, 0, sizeof(event));
|
||||
gettimeofday(&event.time, 0);
|
||||
@@ -94,12 +100,12 @@ void MouseController::SimulateKeyUp(int fd, int kval) {
|
||||
event.type = EV_KEY;
|
||||
event.value = 0;
|
||||
event.code = kval;
|
||||
write(fd, &event, sizeof(event));
|
||||
res_ev = write(fd, &event, sizeof(event));
|
||||
|
||||
event.type = EV_SYN;
|
||||
event.value = 0;
|
||||
event.code = SYN_REPORT;
|
||||
write(fd, &event, sizeof(event));
|
||||
res_ev = write(fd, &event, sizeof(event));
|
||||
}
|
||||
|
||||
void MouseController::SetMousePosition(int fd, int x, int y) {
|
||||
|
||||
@@ -253,7 +253,9 @@ void ServerReceiveDataBuffer(const char *data, size_t size, const char *user_id,
|
||||
// << remote_action.m.flag << " " << remote_action.m.x << " "
|
||||
// << remote_action.m.y << std::endl;
|
||||
#if MOUSE_CONTROL
|
||||
if (mouse_controller) {
|
||||
mouse_controller->SendCommand(remote_action);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -521,7 +523,7 @@ int main(int argc, char *argv[]) {
|
||||
rect.right = screen_w;
|
||||
rect.bottom = screen_h;
|
||||
|
||||
screen_capturer->Init(
|
||||
int screen_capturer_init_ret = screen_capturer->Init(
|
||||
rect, 60,
|
||||
[](unsigned char *data, int size, int width, int height) -> void {
|
||||
auto now_time = std::chrono::high_resolution_clock::now();
|
||||
@@ -536,14 +538,24 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
});
|
||||
|
||||
if (0 == screen_capturer_init_ret) {
|
||||
screen_capturer->Start();
|
||||
} else {
|
||||
screen_capturer->Destroy();
|
||||
screen_capturer = nullptr;
|
||||
}
|
||||
|
||||
// Mouse control
|
||||
device_controller_factory = new DeviceControllerFactory();
|
||||
mouse_controller =
|
||||
(MouseController *)device_controller_factory->Create(
|
||||
DeviceControllerFactory::Device::Mouse);
|
||||
int mouse_controller_init_ret =
|
||||
mouse_controller->Init(screen_w, screen_h);
|
||||
if (0 != mouse_controller_init_ret) {
|
||||
mouse_controller->Destroy();
|
||||
mouse_controller = nullptr;
|
||||
}
|
||||
}
|
||||
},
|
||||
screen_w, screen_h);
|
||||
@@ -754,7 +766,13 @@ int main(int argc, char *argv[]) {
|
||||
SDL_CloseAudioDevice(output_dev);
|
||||
SDL_CloseAudioDevice(input_dev);
|
||||
|
||||
if (screen_capturer) {
|
||||
screen_capturer->Destroy();
|
||||
}
|
||||
|
||||
if (mouse_controller) {
|
||||
mouse_controller->Destroy();
|
||||
}
|
||||
|
||||
ImGui_ImplSDLRenderer2_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
|
||||
@@ -91,12 +91,15 @@ int ScreenCapturerX11::Init(const RECORD_DESKTOP_RECT &rect, const int fps,
|
||||
pFrame_->width, pFrame_->height, pCodecCtx_->pix_fmt, pFrameNv12_->width,
|
||||
pFrameNv12_->height, AV_PIX_FMT_NV12, SWS_BICUBIC, NULL, NULL, NULL);
|
||||
|
||||
inited_ = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerX11::Destroy() {
|
||||
if (capture_thread_->joinable()) {
|
||||
if (inited_ && capture_thread_->joinable()) {
|
||||
capture_thread_->join();
|
||||
inited_ = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -61,6 +61,8 @@ class ScreenCapturerX11 : public ScreenCapturer {
|
||||
int videoindex_ = 0;
|
||||
int got_picture_ = 0;
|
||||
int fps_ = 0;
|
||||
bool inited_ = false;
|
||||
|
||||
// ffmpeg
|
||||
AVFormatContext *pFormatCtx_ = nullptr;
|
||||
AVCodecContext *pCodecCtx_ = nullptr;
|
||||
|
||||
@@ -92,12 +92,15 @@ int ScreenCapturerAvf::Init(const RECORD_DESKTOP_RECT &rect, const int fps,
|
||||
pFrame_->width, pFrame_->height, pCodecCtx_->pix_fmt, pFrameNv12_->width,
|
||||
pFrameNv12_->height, AV_PIX_FMT_NV12, SWS_BICUBIC, NULL, NULL, NULL);
|
||||
|
||||
inited_ = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ScreenCapturerAvf::Destroy() {
|
||||
if (capture_thread_->joinable()) {
|
||||
if (inited_ && capture_thread_->joinable()) {
|
||||
capture_thread_->join();
|
||||
inited_ = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -66,6 +66,8 @@ class ScreenCapturerAvf : public ScreenCapturer {
|
||||
int i_ = 0;
|
||||
int videoindex_ = 0;
|
||||
int got_picture_ = 0;
|
||||
bool inited_ = false;
|
||||
|
||||
// ffmpeg
|
||||
AVFormatContext *pFormatCtx_ = nullptr;
|
||||
AVCodecContext *pCodecCtx_ = nullptr;
|
||||
|
||||
2
thirdparty/projectx
vendored
2
thirdparty/projectx
vendored
Submodule thirdparty/projectx updated: 5decc4b007...3fc8f9f616
Reference in New Issue
Block a user