diff --git a/src/device_controller/mouse/linux/mouse_controller.cpp b/src/device_controller/mouse/linux/mouse_controller.cpp index 4c65a1c..b0f801a 100644 --- a/src/device_controller/mouse/linux/mouse_controller.cpp +++ b/src/device_controller/mouse/linux/mouse_controller.cpp @@ -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,14 +36,17 @@ 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() { - ioctl(uinput_fd_, UI_DEV_DESTROY); - close(uinput_fd_); + if (uinput_fd_) { + ioctl(uinput_fd_, UI_DEV_DESTROY); + close(uinput_fd_); + } + return 0; } int MouseController::SendCommand(RemoteAction remote_action) { @@ -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) { diff --git a/src/gui/main.cpp b/src/gui/main.cpp index 2c1f437..85c1e23 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -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 - mouse_controller->SendCommand(remote_action); + 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[]) { } }); - screen_capturer->Start(); + 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); - mouse_controller->Init(screen_w, screen_h); + 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); - mouse_controller->Destroy(); + if (screen_capturer) { + screen_capturer->Destroy(); + } + + if (mouse_controller) { + mouse_controller->Destroy(); + } ImGui_ImplSDLRenderer2_Shutdown(); ImGui_ImplSDL2_Shutdown(); diff --git a/src/screen_capturer/linux/screen_capturer_x11.cpp b/src/screen_capturer/linux/screen_capturer_x11.cpp index 990ebf6..fb390db 100644 --- a/src/screen_capturer/linux/screen_capturer_x11.cpp +++ b/src/screen_capturer/linux/screen_capturer_x11.cpp @@ -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; diff --git a/src/screen_capturer/linux/screen_capturer_x11.h b/src/screen_capturer/linux/screen_capturer_x11.h index b95994e..d984e8f 100644 --- a/src/screen_capturer/linux/screen_capturer_x11.h +++ b/src/screen_capturer/linux/screen_capturer_x11.h @@ -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; diff --git a/src/screen_capturer/macosx/screen_capturer_avf.cpp b/src/screen_capturer/macosx/screen_capturer_avf.cpp index 765342f..d6c4648 100644 --- a/src/screen_capturer/macosx/screen_capturer_avf.cpp +++ b/src/screen_capturer/macosx/screen_capturer_avf.cpp @@ -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; diff --git a/src/screen_capturer/macosx/screen_capturer_avf.h b/src/screen_capturer/macosx/screen_capturer_avf.h index 82a2bc0..4ecc206 100644 --- a/src/screen_capturer/macosx/screen_capturer_avf.h +++ b/src/screen_capturer/macosx/screen_capturer_avf.h @@ -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; diff --git a/thirdparty/projectx b/thirdparty/projectx index 5decc4b..3fc8f9f 160000 --- a/thirdparty/projectx +++ b/thirdparty/projectx @@ -1 +1 @@ -Subproject commit 5decc4b0074d225684592b0e32a4b5ad095d1e58 +Subproject commit 3fc8f9f616a11432e0e4176f577ec407b90206b4