Skip to content

Commit

Permalink
Merge pull request #431 from go-vgo/bitmap-pr
Browse files Browse the repository at this point in the history
add PadHexs and ToMMBitmap support, add move mouse windows multi screen support
  • Loading branch information
vcaesar authored Jan 4, 2022
2 parents 0b6cc48 + 7685ce8 commit adc7bd4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
31 changes: 15 additions & 16 deletions mouse/mouse_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,16 @@ void calculateDeltas(CGEventRef *event, MMPointInt32 point){
*/
void moveMouse(MMPointInt32 point){
#if defined(IS_MACOSX)
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
CGPointFromMMPointInt32(point),
kCGMouseButtonLeft);
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
CGPointFromMMPointInt32(point), kCGMouseButtonLeft);

calculateDeltas(&move, point);

CGEventPost(kCGSessionEventTap, move);
CFRelease(move);
#elif defined(USE_X11)
Display *display = XGetMainDisplay();
XWarpPointer(display, None, DefaultRootWindow(display),
0, 0, 0, 0, point.x, point.y);
XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, point.x, point.y);

XSync(display, false);
#elif defined(IS_WINDOWS)
Expand All @@ -113,15 +111,17 @@ void moveMouse(MMPointInt32 point){
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \
((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1))

point.x = MOUSE_COORD_TO_ABS(point.x, GetSystemMetrics(SM_CXSCREEN));
point.y = MOUSE_COORD_TO_ABS(point.y, GetSystemMetrics(SM_CYSCREEN));
MMRectInt32 rect = getScreenRect(-1);
int32_t x = MOUSE_COORD_TO_ABS(point.x - rect.origin.x, rect.size.w);
int32_t y = MOUSE_COORD_TO_ABS(point.y - rect.origin.y, rect.size.h);

INPUT mouseInput;
mouseInput.type = INPUT_MOUSE;
mouseInput.mi.dx = point.x;
mouseInput.mi.dy = point.y;
mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
mouseInput.mi.dx = x;
mouseInput.mi.dy = y;
mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK;
mouseInput.mi.time = 0; // System will provide the timestamp

mouseInput.mi.dwExtraInfo = 0;
mouseInput.mi.mouseData = 0;
SendInput(1, &mouseInput, sizeof(mouseInput));
Expand All @@ -132,8 +132,8 @@ void moveMouse(MMPointInt32 point){
void dragMouse(MMPointInt32 point, const MMMouseButton button){
#if defined(IS_MACOSX)
const CGEventType dragType = MMMouseDragToCGEventType(button);
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
CGPointFromMMPoint(point), (CGMouseButton)button);
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
CGPointFromMMPoint(point), (CGMouseButton)button);

calculateDeltas(&drag, point);

Expand Down Expand Up @@ -180,7 +180,7 @@ void toggleMouse(bool down, MMMouseButton button){
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
const CGEventType mouseType = MMMouseToCGEventType(down, button);
CGEventRef event = CGEventCreateMouseEvent(NULL,
mouseType, currentPos, (CGMouseButton)button);
mouseType, currentPos, (CGMouseButton)button);

CGEventPost(kCGSessionEventTap, event);
CFRelease(event);
Expand All @@ -190,7 +190,6 @@ void toggleMouse(bool down, MMMouseButton button){
XSync(display, false);
#elif defined(IS_WINDOWS)
// mouse_event(MMMouseToMEventF(down, button), 0, 0, 0, 0);

INPUT mouseInput;

mouseInput.type = INPUT_MOUSE;
Expand Down Expand Up @@ -223,7 +222,7 @@ void doubleClick(MMMouseButton button){
const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button);

CGEventRef event = CGEventCreateMouseEvent(
NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);
NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);

/* Set event to double click. */
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2);
Expand Down Expand Up @@ -274,7 +273,7 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection){
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;

event = CGEventCreateScrollWheelEvent(NULL,
kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);

CGEventPost(kCGHIDEventTap, event);

Expand Down
10 changes: 10 additions & 0 deletions robotgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ func PadHex(hex C.MMRGBHex) string {
return gcolor
}

// PadHexs trans CHex to string
func PadHexs(hex CHex) string {
return PadHex(C.MMRGBHex(hex))
}

// HexToRgb trans hex to rgb
func HexToRgb(hex uint32) *C.uint8_t {
return C.color_hex_to_rgb(C.uint32_t(hex))
Expand Down Expand Up @@ -343,6 +348,11 @@ func FreeBitmap(bitmap CBitmap) {
C.bitmap_dealloc(C.MMBitmapRef(bitmap))
}

// ToMMBitmapRef trans CBitmap to C.MMBitmapRef
func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef {
return C.MMBitmapRef(bit)
}

// ToBitmap trans C.MMBitmapRef to Bitmap
func ToBitmap(bit CBitmap) Bitmap {
bitmap := Bitmap{
Expand Down

0 comments on commit adc7bd4

Please sign in to comment.