Use factory method to create mouse controller on Windows

This commit is contained in:
dijunkun
2023-12-14 16:40:10 +08:00
parent daecc0d1e9
commit 3ab0e0136e
4 changed files with 98 additions and 194 deletions

View File

@@ -0,0 +1,47 @@
#include "mouse_controller.h"
#include "log.h"
MouseController::MouseController() {}
MouseController::~MouseController() {}
int MouseController::Init(int screen_width, int screen_height) {
screen_width_ = screen_width;
screen_height_ = screen_height;
return 0;
}
int MouseController::Destroy() { return 0; }
int MouseController::SendCommand(RemoteAction remote_action) {
INPUT ip;
if (remote_action.type == ControlType::mouse) {
ip.type = INPUT_MOUSE;
ip.mi.dx = remote_action.m.x * screen_width_ / 1280;
ip.mi.dy = remote_action.m.y * screen_height_ / 720;
if (remote_action.m.flag == MouseFlag::left_down) {
ip.mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE;
} else if (remote_action.m.flag == MouseFlag::left_up) {
ip.mi.dwFlags = MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE;
} else if (remote_action.m.flag == MouseFlag::right_down) {
ip.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE;
} else if (remote_action.m.flag == MouseFlag::right_up) {
ip.mi.dwFlags = MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE;
} else {
ip.mi.dwFlags = MOUSEEVENTF_MOVE;
}
ip.mi.mouseData = 0;
ip.mi.time = 0;
SetCursorPos(ip.mi.dx, ip.mi.dy);
if (ip.mi.dwFlags != MOUSEEVENTF_MOVE) {
SendInput(1, &ip, sizeof(INPUT));
}
}
return 0;
}

View File

@@ -0,0 +1,27 @@
/*
* @Author: DI JUNKUN
* @Date: 2023-12-14
* Copyright (c) 2023 by DI JUNKUN, All Rights Reserved.
*/
#ifndef _MOUSE_CONTROLLER_H_
#define _MOUSE_CONTROLLER_H_
#include "device_controller.h"
class MouseController : public DeviceController {
public:
MouseController();
virtual ~MouseController();
public:
virtual int Init(int screen_width, int screen_height);
virtual int Destroy();
virtual int SendCommand(RemoteAction remote_action);
private:
int screen_width_ = 0;
int screen_height_ = 0;
};
#endif

View File

@@ -387,129 +387,6 @@ void ClientReceiveAudioBuffer(const char *data, size_t size,
SDL_QueueAudio(output_dev, data, size); SDL_QueueAudio(output_dev, data, size);
} }
#ifdef __linux__
void simulate_key_down(int fd, int kval) {
struct input_event event;
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, 0);
// <20><><EFBFBD><EFBFBD>kval<61><6C>
event.type = EV_KEY;
event.value = 1;
event.code = kval;
write(fd, &event, sizeof(event));
// ͬ<><CDAC><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ
event.type = EV_SYN;
event.value = 0;
event.code = SYN_REPORT;
write(fd, &event, sizeof(event));
}
void simulate_key_up(int fd, int kval) {
struct input_event event;
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, 0);
// <20>ɿ<EFBFBD>kval<61><6C>
event.type = EV_KEY;
event.value = 0;
event.code = kval;
write(fd, &event, sizeof(event));
// ͬ<><CDAC><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ
event.type = EV_SYN;
event.value = 0;
event.code = SYN_REPORT;
write(fd, &event, sizeof(event));
}
void mouseSetPosition(int fd, int x, int y) {
struct input_event ev[2], ev_sync;
memset(ev, 0, sizeof(ev));
memset(&ev_sync, 0, sizeof(ev_sync));
ev[0].type = EV_ABS;
ev[0].code = ABS_X;
ev[0].value = x;
ev[1].type = EV_ABS;
ev[1].code = ABS_Y;
ev[1].value = y;
int res_w = write(fd, ev, sizeof(ev));
std::cout << "res w : " << res_w << "\n";
ev_sync.type = EV_SYN;
ev_sync.value = 0;
ev_sync.code = 0;
int res_ev_sync = write(fd, &ev_sync, sizeof(ev_sync));
std::cout << "res syn : " << res_ev_sync << "\n";
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>ģ<EFBFBD><C4A3>
void simulate_mouse(int fd, int x, int y) {
struct input_event ev;
memset(&ev, 0, sizeof(struct input_event));
gettimeofday(&ev.time, NULL);
ev.type = EV_REL;
ev.code = REL_X;
ev.value = x;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write1");
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_SYN;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write4");
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_REL;
ev.code = REL_Y;
ev.value = y;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write2");
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_SYN;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write3");
// ͬ<><CDAC>
// ev.type = EV_SYN;
// ev.value = 0;
// ev.code = SYN_REPORT;
// write(fd, &ev, sizeof(ev));
}
void simulate_mouse_abs(int fd, int x, int y) {
struct input_event ev;
memset(&ev, 0, sizeof(struct input_event));
gettimeofday(&ev.time, NULL);
ev.type = EV_ABS;
ev.code = ABS_X;
ev.value = x;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write1");
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_SYN;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write4");
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_ABS;
ev.code = ABS_Y;
ev.value = y;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write2");
memset(&ev, 0, sizeof(struct input_event));
ev.type = EV_SYN;
if (write(fd, &ev, sizeof(struct input_event)) < 0)
LOG_ERROR("error: write3");
// ͬ<><CDAC>
ev.type = EV_SYN;
ev.value = 0;
ev.code = SYN_REPORT;
write(fd, &ev, sizeof(ev));
}
#endif
void ServerReceiveDataBuffer(const char *data, size_t size, const char *user_id, void ServerReceiveDataBuffer(const char *data, size_t size, const char *user_id,
size_t user_id_size) { size_t user_id_size) {
std::string user(user_id, user_id_size); std::string user(user_id, user_id_size);
@@ -528,87 +405,34 @@ void ServerReceiveDataBuffer(const char *data, size_t size, const char *user_id,
#if 1 #if 1
#ifdef _WIN32 #ifdef _WIN32
INPUT ip; mouse_controller->SendCommand(remote_action);
if (remote_action.type == ControlType::mouse) {
ip.type = INPUT_MOUSE;
ip.mi.dx = remote_action.m.x * screen_w / 1280;
ip.mi.dy = remote_action.m.y * screen_h / 720;
if (remote_action.m.flag == MouseFlag::left_down) {
ip.mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE;
} else if (remote_action.m.flag == MouseFlag::left_up) {
ip.mi.dwFlags = MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE;
} else if (remote_action.m.flag == MouseFlag::right_down) {
ip.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE;
} else if (remote_action.m.flag == MouseFlag::right_up) {
ip.mi.dwFlags = MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE;
} else {
ip.mi.dwFlags = MOUSEEVENTF_MOVE;
}
ip.mi.mouseData = 0;
ip.mi.time = 0;
// Set cursor pos
SetCursorPos(ip.mi.dx, ip.mi.dy);
// Send the press
if (ip.mi.dwFlags != MOUSEEVENTF_MOVE) {
SendInput(1, &ip, sizeof(INPUT));
}
// std::cout << "Receive data from [" << user << "], " << ip.type << " "
// << ip.mi.dwFlags << " " << ip.mi.dx << " " << ip.mi.dy
// << std::endl;
}
#elif __APPLE__ #elif __APPLE__
if (remote_action.type == ControlType::mouse) {
CGEventRef mouse_event;
CGEventType mouse_type;
if (remote_action.m.flag == MouseFlag::left_down) {
mouse_type = kCGEventLeftMouseDown;
} else if (remote_action.m.flag == MouseFlag::left_up) {
mouse_type = kCGEventLeftMouseUp;
} else if (remote_action.m.flag == MouseFlag::right_down) {
mouse_type = kCGEventRightMouseDown;
} else if (remote_action.m.flag == MouseFlag::right_up) {
mouse_type = kCGEventRightMouseUp;
} else {
mouse_type = kCGEventMouseMoved;
}
mouse_event = CGEventCreateMouseEvent(NULL, mouse_type,
CGPointMake(mouse_pos_x, mouse_pos_y),
kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, mouse_event);
CFRelease(mouse_event);
}
#elif __linux__
// if (remote_action.type == ControlType::mouse) { // if (remote_action.type == ControlType::mouse) {
// struct input_event event; // CGEventRef mouse_event;
// memset(&event, 0, sizeof(event)); // CGEventType mouse_type;
// gettimeofday(&event.time, NULL);
// if (remote_action.m.flag == MouseFlag::left_down) { // if (remote_action.m.flag == MouseFlag::left_down) {
// simulate_key_down(uinput_fd, BTN_LEFT); // mouse_type = kCGEventLeftMouseDown;
// } else if (remote_action.m.flag == MouseFlag::left_up) { // } else if (remote_action.m.flag == MouseFlag::left_up) {
// simulate_key_up(uinput_fd, BTN_LEFT); // mouse_type = kCGEventLeftMouseUp;
// } else if (remote_action.m.flag == MouseFlag::right_down) { // } else if (remote_action.m.flag == MouseFlag::right_down) {
// simulate_key_down(uinput_fd, BTN_RIGHT); // mouse_type = kCGEventRightMouseDown;
// } else if (remote_action.m.flag == MouseFlag::right_up) { // } else if (remote_action.m.flag == MouseFlag::right_up) {
// simulate_key_up(uinput_fd, BTN_RIGHT); // mouse_type = kCGEventRightMouseUp;
// } else { // } else {
// mouseSetPosition(uinput_fd, mouse_pos_x, mouse_pos_y); // mouse_type = kCGEventMouseMoved;
// // simulate_mouse(uinput_fd, rel_x, rel_y);
// // simulate_mouse_abs(uinput_fd, 65535, 65535);
// mouse_pos_x_last = mouse_pos_x;
// mouse_pos_y_last = mouse_pos_y;
// } // }
// // report_key(EV_KEY, KEY_A, 1); // Report BUTTON A CLICK - PRESS event // mouse_event = CGEventCreateMouseEvent(NULL, mouse_type,
// // report_key(EV_KEY, KEY_A, 0); // CGPointMake(mouse_pos_x,
// mouse_pos_y), kCGMouseButtonLeft);
// CGEventPost(kCGHIDEventTap, mouse_event);
// CFRelease(mouse_event);
// } // }
#elif __linux__
mouse_controller->SendCommand(remote_action); mouse_controller->SendCommand(remote_action);
#endif #endif
#endif #endif
@@ -992,6 +816,11 @@ int main() {
nv12_buffer = new char[NV12_BUFFER_SIZE]; nv12_buffer = new char[NV12_BUFFER_SIZE];
#ifdef _WIN32 #ifdef _WIN32
device_controller_factory = new DeviceControllerFactory();
mouse_controller = (MouseController *)device_controller_factory->Create(
DeviceControllerFactory::Device::Mouse);
mouse_controller->Init(screen_w, screen_h);
screen_capture = new ScreenCaptureWgc(); screen_capture = new ScreenCaptureWgc();
RECORD_DESKTOP_RECT rect; RECORD_DESKTOP_RECT rect;

View File

@@ -71,8 +71,9 @@ target("device_controller")
set_kind("object") set_kind("object")
add_deps("log") add_deps("log")
if is_os("windows") then if is_os("windows") then
-- add_files("src/screen_capture/windows/*.cpp") add_files("src/device_controller/mouse/windows/*.cpp")
-- add_includedirs("src/screen_capture/windows", {public = true}) add_includedirs("src/device_controller/mouse/windows", {public = true})
add_includedirs("src/device_controller", {public = true})
elseif is_os("macosx") then elseif is_os("macosx") then
-- add_files("src/screen_capture/macosx/*.cpp") -- add_files("src/screen_capture/macosx/*.cpp")
-- add_includedirs("src/screen_capture/macosx", {public = true}) -- add_includedirs("src/screen_capture/macosx", {public = true})