mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25: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);
|
uinput_fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
|
||||||
if (uinput_fd_ < 0) {
|
if (uinput_fd_ < 0) {
|
||||||
LOG_ERROR("Cannot open device: /dev/uinput");
|
LOG_ERROR("Cannot open device: /dev/uinput");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ioctl(uinput_fd_, UI_SET_EVBIT, EV_KEY);
|
ioctl(uinput_fd_, UI_SET_EVBIT, EV_KEY);
|
||||||
@@ -35,14 +36,17 @@ int MouseController::Init(int screen_width, int screen_height) {
|
|||||||
uidev.absmin[ABS_Y] = 0;
|
uidev.absmin[ABS_Y] = 0;
|
||||||
uidev.absmax[ABS_Y] = screen_height_;
|
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);
|
ioctl(uinput_fd_, UI_DEV_CREATE);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MouseController::Destroy() {
|
int MouseController::Destroy() {
|
||||||
|
if (uinput_fd_) {
|
||||||
ioctl(uinput_fd_, UI_DEV_DESTROY);
|
ioctl(uinput_fd_, UI_DEV_DESTROY);
|
||||||
close(uinput_fd_);
|
close(uinput_fd_);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MouseController::SendCommand(RemoteAction remote_action) {
|
int MouseController::SendCommand(RemoteAction remote_action) {
|
||||||
@@ -71,6 +75,7 @@ int MouseController::SendCommand(RemoteAction remote_action) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::SimulateKeyDown(int fd, int kval) {
|
void MouseController::SimulateKeyDown(int fd, int kval) {
|
||||||
|
int res_ev = 0;
|
||||||
struct input_event event;
|
struct input_event event;
|
||||||
memset(&event, 0, sizeof(event));
|
memset(&event, 0, sizeof(event));
|
||||||
gettimeofday(&event.time, 0);
|
gettimeofday(&event.time, 0);
|
||||||
@@ -78,15 +83,16 @@ void MouseController::SimulateKeyDown(int fd, int kval) {
|
|||||||
event.type = EV_KEY;
|
event.type = EV_KEY;
|
||||||
event.value = 1;
|
event.value = 1;
|
||||||
event.code = kval;
|
event.code = kval;
|
||||||
write(fd, &event, sizeof(event));
|
res_ev = write(fd, &event, sizeof(event));
|
||||||
|
|
||||||
event.type = EV_SYN;
|
event.type = EV_SYN;
|
||||||
event.value = 0;
|
event.value = 0;
|
||||||
event.code = SYN_REPORT;
|
event.code = SYN_REPORT;
|
||||||
write(fd, &event, sizeof(event));
|
res_ev = write(fd, &event, sizeof(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseController::SimulateKeyUp(int fd, int kval) {
|
void MouseController::SimulateKeyUp(int fd, int kval) {
|
||||||
|
int res_ev = 0;
|
||||||
struct input_event event;
|
struct input_event event;
|
||||||
memset(&event, 0, sizeof(event));
|
memset(&event, 0, sizeof(event));
|
||||||
gettimeofday(&event.time, 0);
|
gettimeofday(&event.time, 0);
|
||||||
@@ -94,12 +100,12 @@ void MouseController::SimulateKeyUp(int fd, int kval) {
|
|||||||
event.type = EV_KEY;
|
event.type = EV_KEY;
|
||||||
event.value = 0;
|
event.value = 0;
|
||||||
event.code = kval;
|
event.code = kval;
|
||||||
write(fd, &event, sizeof(event));
|
res_ev = write(fd, &event, sizeof(event));
|
||||||
|
|
||||||
event.type = EV_SYN;
|
event.type = EV_SYN;
|
||||||
event.value = 0;
|
event.value = 0;
|
||||||
event.code = SYN_REPORT;
|
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) {
|
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.flag << " " << remote_action.m.x << " "
|
||||||
// << remote_action.m.y << std::endl;
|
// << remote_action.m.y << std::endl;
|
||||||
#if MOUSE_CONTROL
|
#if MOUSE_CONTROL
|
||||||
|
if (mouse_controller) {
|
||||||
mouse_controller->SendCommand(remote_action);
|
mouse_controller->SendCommand(remote_action);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,7 +523,7 @@ int main(int argc, char *argv[]) {
|
|||||||
rect.right = screen_w;
|
rect.right = screen_w;
|
||||||
rect.bottom = screen_h;
|
rect.bottom = screen_h;
|
||||||
|
|
||||||
screen_capturer->Init(
|
int screen_capturer_init_ret = screen_capturer->Init(
|
||||||
rect, 60,
|
rect, 60,
|
||||||
[](unsigned char *data, int size, int width, int height) -> void {
|
[](unsigned char *data, int size, int width, int height) -> void {
|
||||||
auto now_time = std::chrono::high_resolution_clock::now();
|
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();
|
screen_capturer->Start();
|
||||||
|
} else {
|
||||||
|
screen_capturer->Destroy();
|
||||||
|
screen_capturer = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
// Mouse control
|
// Mouse control
|
||||||
device_controller_factory = new DeviceControllerFactory();
|
device_controller_factory = new DeviceControllerFactory();
|
||||||
mouse_controller =
|
mouse_controller =
|
||||||
(MouseController *)device_controller_factory->Create(
|
(MouseController *)device_controller_factory->Create(
|
||||||
DeviceControllerFactory::Device::Mouse);
|
DeviceControllerFactory::Device::Mouse);
|
||||||
|
int mouse_controller_init_ret =
|
||||||
mouse_controller->Init(screen_w, screen_h);
|
mouse_controller->Init(screen_w, screen_h);
|
||||||
|
if (0 != mouse_controller_init_ret) {
|
||||||
|
mouse_controller->Destroy();
|
||||||
|
mouse_controller = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
screen_w, screen_h);
|
screen_w, screen_h);
|
||||||
@@ -754,7 +766,13 @@ int main(int argc, char *argv[]) {
|
|||||||
SDL_CloseAudioDevice(output_dev);
|
SDL_CloseAudioDevice(output_dev);
|
||||||
SDL_CloseAudioDevice(input_dev);
|
SDL_CloseAudioDevice(input_dev);
|
||||||
|
|
||||||
|
if (screen_capturer) {
|
||||||
|
screen_capturer->Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mouse_controller) {
|
||||||
mouse_controller->Destroy();
|
mouse_controller->Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
ImGui_ImplSDLRenderer2_Shutdown();
|
ImGui_ImplSDLRenderer2_Shutdown();
|
||||||
ImGui_ImplSDL2_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,
|
pFrame_->width, pFrame_->height, pCodecCtx_->pix_fmt, pFrameNv12_->width,
|
||||||
pFrameNv12_->height, AV_PIX_FMT_NV12, SWS_BICUBIC, NULL, NULL, NULL);
|
pFrameNv12_->height, AV_PIX_FMT_NV12, SWS_BICUBIC, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
inited_ = true;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ScreenCapturerX11::Destroy() {
|
int ScreenCapturerX11::Destroy() {
|
||||||
if (capture_thread_->joinable()) {
|
if (inited_ && capture_thread_->joinable()) {
|
||||||
capture_thread_->join();
|
capture_thread_->join();
|
||||||
|
inited_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ class ScreenCapturerX11 : public ScreenCapturer {
|
|||||||
int videoindex_ = 0;
|
int videoindex_ = 0;
|
||||||
int got_picture_ = 0;
|
int got_picture_ = 0;
|
||||||
int fps_ = 0;
|
int fps_ = 0;
|
||||||
|
bool inited_ = false;
|
||||||
|
|
||||||
// ffmpeg
|
// ffmpeg
|
||||||
AVFormatContext *pFormatCtx_ = nullptr;
|
AVFormatContext *pFormatCtx_ = nullptr;
|
||||||
AVCodecContext *pCodecCtx_ = 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,
|
pFrame_->width, pFrame_->height, pCodecCtx_->pix_fmt, pFrameNv12_->width,
|
||||||
pFrameNv12_->height, AV_PIX_FMT_NV12, SWS_BICUBIC, NULL, NULL, NULL);
|
pFrameNv12_->height, AV_PIX_FMT_NV12, SWS_BICUBIC, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
inited_ = true;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ScreenCapturerAvf::Destroy() {
|
int ScreenCapturerAvf::Destroy() {
|
||||||
if (capture_thread_->joinable()) {
|
if (inited_ && capture_thread_->joinable()) {
|
||||||
capture_thread_->join();
|
capture_thread_->join();
|
||||||
|
inited_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -66,6 +66,8 @@ class ScreenCapturerAvf : public ScreenCapturer {
|
|||||||
int i_ = 0;
|
int i_ = 0;
|
||||||
int videoindex_ = 0;
|
int videoindex_ = 0;
|
||||||
int got_picture_ = 0;
|
int got_picture_ = 0;
|
||||||
|
bool inited_ = false;
|
||||||
|
|
||||||
// ffmpeg
|
// ffmpeg
|
||||||
AVFormatContext *pFormatCtx_ = nullptr;
|
AVFormatContext *pFormatCtx_ = nullptr;
|
||||||
AVCodecContext *pCodecCtx_ = 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