Merge pull request #449 from go-vgo/bitmap-pr

Update README.md and removed type_string_delayed
This commit is contained in:
Evans 2022-01-26 10:47:33 -08:00 committed by GitHub
commit 72167ef467
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 88 additions and 259 deletions

View File

@ -251,16 +251,16 @@ func main() {
imgo.Save("test.png", img)
num := robotgo.DisplaysNum()
for i := 0; i < num; i++ {
robotgo.DisplayID = i
img1 := robotgo.CaptureImg()
for i := 0; i < num; i++ {
robotgo.DisplayID = i
img1 := robotgo.CaptureImg()
path1 := "save_" + strconv.Itoa(i)
robotgo.Save(img1, path1+".png")
robotgo.SaveJpeg(img1, path1+".jpeg", 50)
robotgo.Save(img1, path1+".png")
robotgo.SaveJpeg(img1, path1+".jpeg", 50)
img2 := robotgo.CaptureImg(10, 10, 20, 20)
robotgo.Save(img2, "test_"+strconv.Itoa(i)+".png")
}
img2 := robotgo.CaptureImg(10, 10, 20, 20)
robotgo.Save(img2, "test_"+strconv.Itoa(i)+".png")
}
}
```

View File

@ -2,14 +2,9 @@
#include <assert.h>
#include <string.h>
MMBitmapRef createMMBitmap_c(
uint8_t *buffer,
size_t width,
size_t height,
size_t bytewidth,
uint8_t bitsPerPixel,
uint8_t bytesPerPixel
){
MMBitmapRef createMMBitmap_c(uint8_t *buffer, size_t width, size_t height,
size_t bytewidth, uint8_t bitsPerPixel, uint8_t bytesPerPixel
) {
MMBitmapRef bitmap = malloc(sizeof(MMBitmap));
if (bitmap == NULL) { return NULL; }

View File

@ -82,7 +82,7 @@
#define bitswap16(i) bswap_16(i) /* Linux system function */
#else /* Default macro */
#define bitswap16(i) (((uint16_t)(i) & 0xFF00) >> 8) | \
(((uint16_t)(i) & 0x00FF) << 8)
(((uint16_t)(i) & 0x00FF) << 8)
#endif
#endif /* bitswap16 */

View File

@ -10,7 +10,6 @@
#define __bool_true_false_are_defined 1
#ifndef __cplusplus
#if defined(true) || defined(false) || defined(bool)
#error "Boolean type already defined"
#endif
@ -21,7 +20,6 @@
};
typedef unsigned char bool;
#endif /* !__cplusplus */
#endif /* MS_STDBOOL_H */

View File

@ -12,7 +12,6 @@
#include <stdint.h>
#endif
/* RGB colors in MMBitmaps are stored as BGR for convenience in converting
* to/from certain formats (mainly OpenGL).
*
@ -46,8 +45,7 @@ typedef uint32_t MMRGBHex;
#define RGB_TO_HEX(red, green, blue) (((red) << 16) | ((green) << 8) | (blue))
/* Convenience wrapper for MMRGBColors. */
H_INLINE MMRGBHex hexFromMMRGB(MMRGBColor rgb)
{
H_INLINE MMRGBHex hexFromMMRGB(MMRGBColor rgb) {
return RGB_TO_HEX(rgb.red, rgb.green, rgb.blue);
}
@ -56,8 +54,7 @@ H_INLINE MMRGBHex hexFromMMRGB(MMRGBColor rgb)
#define BLUE_FROM_HEX(hex) (hex & 0xFF)
/* Converts hexadecimal color to MMRGBColor. */
H_INLINE MMRGBColor MMRGBFromHex(MMRGBHex hex)
{
H_INLINE MMRGBColor MMRGBFromHex(MMRGBHex hex) {
MMRGBColor color;
color.red = RED_FROM_HEX(hex);
color.green = GREEN_FROM_HEX(hex);
@ -73,9 +70,7 @@ H_INLINE MMRGBColor MMRGBFromHex(MMRGBHex hex)
/* Returns whether two colors are similar within the given range, |tolerance|.
* Tolerance can be in the range 0.0f - 1.0f, where 0 denotes the exact
* color and 1 denotes any color. */
H_INLINE int MMRGBColorSimilarToColor(MMRGBColor c1, MMRGBColor c2,
float tolerance)
{
H_INLINE int MMRGBColorSimilarToColor(MMRGBColor c1, MMRGBColor c2, float tolerance) {
/* Speedy case */
if (tolerance <= 0.0f) {
return MMRGBColorEqualToColor(c1, c2);
@ -87,12 +82,10 @@ H_INLINE int MMRGBColorSimilarToColor(MMRGBColor c1, MMRGBColor c2,
(d2 * d2) +
(d3 * d3)) <= (tolerance * 442.0f);
}
}
/* Identical to MMRGBColorSimilarToColor, only for hex values. */
H_INLINE int MMRGBHexSimilarToColor(MMRGBHex h1, MMRGBHex h2, float tolerance)
{
H_INLINE int MMRGBHexSimilarToColor(MMRGBHex h1, MMRGBHex h2, float tolerance) {
if (tolerance <= 0.0f) {
return h1 == h2;
} else {

View File

@ -8,7 +8,6 @@
#include <stdint.h>
/* Some generic, cross-platform types. */
#ifdef RobotGo_64
typedef int64_t intptr;
typedef uint64_t uintptr;
@ -21,87 +20,74 @@ struct _MMPoint {
size_t x;
size_t y;
};
typedef struct _MMPoint MMPoint;
struct _MMPointInt32 {
int32_t x;
int32_t y;
};
typedef struct _MMPointInt32 MMPointInt32;
struct _MMSize {
size_t width;
size_t height;
};
typedef struct _MMSize MMSize;
struct _MMSizeInt32 {
int32_t w;
int32_t h;
};
typedef struct _MMSizeInt32 MMSizeInt32;
struct _MMRect {
MMPoint origin;
MMSize size;
};
typedef struct _MMRect MMRect;
struct _MMRectInt32 {
MMPointInt32 origin;
MMSizeInt32 size;
};
typedef struct _MMRectInt32 MMRectInt32;
H_INLINE MMPoint MMPointMake(size_t x, size_t y)
{
H_INLINE MMPoint MMPointMake(size_t x, size_t y) {
MMPoint point;
point.x = x;
point.y = y;
return point;
}
H_INLINE MMPointInt32 MMPointInt32Make(int32_t x, int32_t y)
{
H_INLINE MMPointInt32 MMPointInt32Make(int32_t x, int32_t y) {
MMPointInt32 point;
point.x = x;
point.y = y;
return point;
}
H_INLINE MMSize MMSizeMake(size_t width, size_t height)
{
H_INLINE MMSize MMSizeMake(size_t width, size_t height) {
MMSize size;
size.width = width;
size.height = height;
return size;
}
H_INLINE MMSizeInt32 MMSizeInt32Make(int32_t w, int32_t h)
{
H_INLINE MMSizeInt32 MMSizeInt32Make(int32_t w, int32_t h) {
MMSizeInt32 size;
size.w = w;
size.h = h;
return size;
}
H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
{
H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height) {
MMRect rect;
rect.origin = MMPointMake(x, y);
rect.size = MMSizeMake(width, height);
return rect;
}
H_INLINE MMRectInt32 MMRectInt32Make(int32_t x, int32_t y, int32_t w, int32_t h)
{
H_INLINE MMRectInt32 MMRectInt32Make(int32_t x, int32_t y, int32_t w, int32_t h) {
MMRectInt32 rect;
rect.origin = MMPointInt32Make(x, y);
rect.size = MMSizeInt32Make(w, h);
@ -112,18 +98,14 @@ H_INLINE MMRectInt32 MMRectInt32Make(int32_t x, int32_t y, int32_t w, int32_t h)
#define MMPointZero MMPointMake(0, 0)
#if defined(IS_MACOSX)
#define CGPointFromMMPoint(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointFromCGPoint(p) MMPointMake((size_t)(p).x, (size_t)(p).y)
#define CGPointFromMMPoint(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointFromCGPoint(p) MMPointMake((size_t)(p).x, (size_t)(p).y)
#define CGPointFromMMPointInt32(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointInt32FromCGPoint(p) MMPointInt32Make((int32_t)(p).x, (int32_t)(p).y)
#define CGPointFromMMPointInt32(p) CGPointMake((CGFloat)(p).x, (CGFloat)(p).y)
#define MMPointInt32FromCGPoint(p) MMPointInt32Make((int32_t)(p).x, (int32_t)(p).y)
#elif defined(IS_WINDOWS)
#define MMPointFromPOINT(p) MMPointMake((size_t)p.x, (size_t)p.y)
#define MMPointInt32FromPOINT(p) MMPointInt32Make((int32_t)p.x, (int32_t)p.y)
#define MMPointFromPOINT(p) MMPointMake((size_t)p.x, (size_t)p.y)
#define MMPointInt32FromPOINT(p) MMPointInt32Make((int32_t)p.x, (int32_t)p.y)
#endif
#endif /* TYPES_H */

View File

@ -30,5 +30,4 @@
// printf("%d\n", info.hWnd);
return info.hWnd;
}
#endif

View File

@ -172,23 +172,23 @@ int CheckKeyCodes(char* k, MMKeyCode *key){
int CheckKeyFlags(char* f, MMKeyFlags* flags){
if (!flags) { return -1; }
if ( strcmp(f, "alt") == 0 || strcmp(f, "ralt") == 0 ||
if (strcmp(f, "alt") == 0 || strcmp(f, "ralt") == 0 ||
strcmp(f, "lalt") == 0 ) {
*flags = MOD_ALT;
}
else if( strcmp(f, "command") == 0 || strcmp(f, "cmd") == 0 ||
else if(strcmp(f, "command") == 0 || strcmp(f, "cmd") == 0 ||
strcmp(f, "rcmd") == 0 || strcmp(f, "lcmd") == 0 ) {
*flags = MOD_META;
}
else if( strcmp(f, "control") == 0 || strcmp(f, "ctrl") == 0 ||
else if(strcmp(f, "control") == 0 || strcmp(f, "ctrl") == 0 ||
strcmp(f, "rctrl") == 0 || strcmp(f, "lctrl") == 0 ) {
*flags = MOD_CONTROL;
}
else if( strcmp(f, "shift") == 0 || strcmp(f, "right_shift") == 0 ||
else if(strcmp(f, "shift") == 0 || strcmp(f, "right_shift") == 0 ||
strcmp(f, "rshift") == 0 || strcmp(f, "lshift") == 0 ) {
*flags = MOD_SHIFT;
}
else if( strcmp(f, "none") == 0 ) {
else if(strcmp(f, "none") == 0 ) {
*flags = (MMKeyFlags) MOD_NONE;
} else {
return -2;
@ -332,6 +332,7 @@ char* key_Toggles(char* k, char* keyArr[], int num) {
return "";
}
// remove
char* key_toggle(char* k, char* d, char* akey, char* keyT){
MMKeyFlags flags = (MMKeyFlags) MOD_NONE;
MMKeyCode key;
@ -390,13 +391,6 @@ char* key_toggle(char* k, char* d, char* akey, char* keyT){
return "";
}
void type_string(char *str){
typeStringDelayed(str, 0);
}
void type_string_delayed(char *str, size_t cpm){
typeStringDelayed(str, cpm);
}
void set_keyboard_delay(size_t val){
keyboardDelay = val;

View File

@ -1,19 +1,16 @@
#include "keycode.h"
#if defined(IS_MACOSX)
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h> /* For kVK_ constants, and TIS functions. */
/* Returns string representation of key, if it is printable.
* Ownership follows the Create Rule; that is, it is the caller's
* responsibility to release the returned object. */
CFStringRef createStringForKey(CGKeyCode keyCode);
MMKeyCode keyCodeForCharFallBack(const char c);
/* Returns string representation of key, if it is printable.
* Ownership follows the Create Rule; that is, it is the caller's
* responsibility to release the returned object. */
CFStringRef createStringForKey(CGKeyCode keyCode);
MMKeyCode keyCodeForCharFallBack(const char c);
#elif defined(USE_X11)
/*
* Structs to store key mappings not handled by XStringToKeysym() on some
* Linux systems.
@ -77,11 +74,9 @@ MMKeyCode keyCodeForChar(const char c){
/* Generate table of keycodes and characters. */
if (charToCodeDict == NULL) {
size_t i;
charToCodeDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
128,
&kCFCopyStringDictionaryKeyCallBacks,
NULL);
if (charToCodeDict == NULL) return K_NOT_A_KEY;
charToCodeDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 128,
&kCFCopyStringDictionaryKeyCallBacks, NULL);
if (charToCodeDict == NULL) { return K_NOT_A_KEY; }
/* Loop through every keycode (0 - 127) to find its current mapping. */
for (i = 0; i < 128; ++i) {
@ -135,7 +130,7 @@ MMKeyCode keyCodeForChar(const char c){
* mapping table. */
struct XSpecialCharacterMapping* xs = XSpecialCharacterTable;
while (xs->name) {
if (c == xs->name ) {
if (c == xs->name) {
code = xs->code;
//
break;
@ -152,34 +147,22 @@ MMKeyCode keyCodeForChar(const char c){
#endif
}
#if defined(IS_MACOSX)
CFStringRef createStringForKey(CGKeyCode keyCode){
TISInputSourceRef currentKeyboard = TISCopyCurrentASCIICapableKeyboardInputSource();
CFDataRef layoutData =
(CFDataRef)TISGetInputSourceProperty(currentKeyboard,
kTISPropertyUnicodeKeyLayoutData);
CFDataRef layoutData = (CFDataRef) TISGetInputSourceProperty(
currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
if (layoutData == nil) { return 0; }
const UCKeyboardLayout *keyboardLayout =
(const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *) CFDataGetBytePtr(layoutData);
UInt32 keysDown = 0;
UniChar chars[4];
UniCharCount realLength;
UCKeyTranslate(keyboardLayout,
keyCode,
kUCKeyActionDisplay,
0,
LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&keysDown,
sizeof(chars) / sizeof(chars[0]),
&realLength,
chars);
UCKeyTranslate(keyboardLayout, keyCode, kUCKeyActionDisplay, 0, LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit, &keysDown,
sizeof(chars) / sizeof(chars[0]), &realLength, chars);
CFRelease(currentKeyboard);
return CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1);

View File

@ -17,7 +17,6 @@ extern "C"
#endif
#if defined(IS_MACOSX)
typedef enum {
MOD_NONE = 0,
MOD_META = kCGEventFlagMaskCommand,
@ -25,9 +24,7 @@ extern "C"
MOD_CONTROL = kCGEventFlagMaskControl,
MOD_SHIFT = kCGEventFlagMaskShift
} MMKeyFlags;
#elif defined(USE_X11)
enum _MMKeyFlags {
MOD_NONE = 0,
MOD_META = Mod4Mask,
@ -37,9 +34,7 @@ extern "C"
};
typedef unsigned int MMKeyFlags;
#elif defined(IS_WINDOWS)
enum _MMKeyFlags {
MOD_NONE = 0,
/* These are already defined by the Win32 API */
@ -50,7 +45,6 @@ extern "C"
};
typedef unsigned int MMKeyFlags;
#endif
#if defined(IS_WINDOWS)

View File

@ -3,7 +3,6 @@
#include "../base/microsleep.h"
#include <ctype.h> /* For isupper() */
#if defined(IS_MACOSX)
#include <ApplicationServices/ApplicationServices.h>
#import <IOKit/hidsystem/IOHIDLib.h>
@ -18,34 +17,29 @@
#define WIN32_KEY_EVENT_WAIT(key, flags) \
(win32KeyEvent(key, flags), Sleep(DEADBEEF_RANDRANGE(0, 1)))
#elif defined(USE_X11)
#define X_KEY_EVENT(display, key, is_press) \
(XTestFakeKeyEvent(display, \
XKeysymToKeycode(display, key), \
is_press, CurrentTime), \
XSync(display, false))
#define X_KEY_EVENT_WAIT(display, key, is_press) \
(X_KEY_EVENT(display, key, is_press), \
microsleep(DEADBEEF_UNIFORM(0.0, 0.5)))
#define X_KEY_EVENT(display, key, is_press) ( \
XTestFakeKeyEvent(display, XKeysymToKeycode(display, key), is_press, CurrentTime), \
XSync(display, false))
#define X_KEY_EVENT_WAIT(display, key, is_press) ( \
X_KEY_EVENT(display, key, is_press), microsleep(DEADBEEF_UNIFORM(0.0, 0.5)))
#endif
#if defined(IS_MACOSX)
static io_connect_t _getAuxiliaryKeyDriver(void){
static io_connect_t _getAuxiliaryKeyDriver(void) {
static mach_port_t sEventDrvrRef = 0;
mach_port_t masterPort, service, iter;
kern_return_t kr;
if (!sEventDrvrRef) {
kr = IOMasterPort( bootstrap_port, &masterPort );
kr = IOMasterPort(bootstrap_port, &masterPort);
assert(KERN_SUCCESS == kr);
kr = IOServiceGetMatchingServices(masterPort,
IOServiceMatching( kIOHIDSystemClass), &iter );
kr = IOServiceGetMatchingServices(masterPort, IOServiceMatching(kIOHIDSystemClass), &iter);
assert(KERN_SUCCESS == kr);
service = IOIteratorNext( iter );
service = IOIteratorNext(iter);
assert(service);
kr = IOServiceOpen(service,
mach_task_self(), kIOHIDParamConnectType, &sEventDrvrRef );
kr = IOServiceOpen(service, mach_task_self(), kIOHIDParamConnectType, &sEventDrvrRef);
assert(KERN_SUCCESS == kr);
IOObjectRelease(service);
@ -56,7 +50,7 @@ static io_connect_t _getAuxiliaryKeyDriver(void){
#endif
#if defined(IS_WINDOWS)
void win32KeyEvent(int key, MMKeyFlags flags){
void win32KeyEvent(int key, MMKeyFlags flags) {
int scan = MapVirtualKey(key & 0xff, MAPVK_VK_TO_VSC);
/* Set the scan code for extended keys */
@ -103,7 +97,6 @@ void win32KeyEvent(int key, MMKeyFlags flags){
// if ( flags & KEYEVENTF_KEYUP ) {
// scan |= 0x80;
// }
// keybd_event(key, scan, flags, 0);
INPUT keyInput;
@ -118,12 +111,12 @@ void win32KeyEvent(int key, MMKeyFlags flags){
}
#endif
void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags){
void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags) {
#if defined(IS_MACOSX)
/* The media keys all have 1000 added to them to help us detect them. */
if (code >= 1000) {
code = code - 1000; /* Get the real keycode. */
NXEventData event;
NXEventData event;
kern_return_t kr;
IOGPoint loc = { 0, 0 };
@ -133,12 +126,11 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags){
event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
event.compound.misc.L[0] = evtInfo;
kr = IOHIDPostEvent( _getAuxiliaryKeyDriver(),
NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
assert( KERN_SUCCESS == kr );
kr = IOHIDPostEvent(_getAuxiliaryKeyDriver(),
NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE);
assert(KERN_SUCCESS == kr);
} else {
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL,
(CGKeyCode)code, down);
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)code, down);
assert(keyEvent != NULL);
CGEventSetType(keyEvent, down ? kCGEventKeyDown : kCGEventKeyUp);
@ -269,13 +261,11 @@ void toggleUnicode(UniChar ch, const bool down){
XFlush(dpy);
KeyCode code = XKeysymToKeycode(dpy, sym);
XTestFakeKeyEvent(dpy, code, True, 1);
XTestFakeKeyEvent(dpy, code, False, 1);
XFlush(dpy);
XCloseDisplay(dpy);
return 0;
}
#endif
@ -313,52 +303,4 @@ void unicodeType(const unsigned value){
microsleep(5.0);
toggleUniKey(value, false);
#endif
}
// todo: removed
void typeStringDelayed(const char *str, const unsigned cpm){
/* Characters per second */
const double cps = (double)cpm / 60.0;
/* Average milli-seconds per character */
const double mspc = (cps == 0.0) ? 0.0 : 1000.0 / cps;
unsigned long n;
unsigned short c;
unsigned short c1;
unsigned short c2;
unsigned short c3;
while (*str != '\0') {
c = *str++;
// warning, the following utf8 decoder
// doesn't perform validation
if (c <= 0x7F) {
// 0xxxxxxx one byte
n = c;
} else if ((c & 0xE0) == 0xC0) {
// 110xxxxx two bytes
c1 = (*str++) & 0x3F;
n = ((c & 0x1F) << 6) | c1;
} else if ((c & 0xF0) == 0xE0) {
// 1110xxxx three bytes
c1 = (*str++) & 0x3F;
c2 = (*str++) & 0x3F;
n = ((c & 0x0F) << 12) | (c1 << 6) | c2;
} else if ((c & 0xF8) == 0xF0) {
// 11110xxx four bytes
c1 = (*str++) & 0x3F;
c2 = (*str++) & 0x3F;
c3 = (*str++) & 0x3F;
n = ((c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | c3;
}
unicodeType(n);
if (mspc > 0) {
microsleep(mspc + (DEADBEEF_UNIFORM(0.0, 0.5)));
}
}
}

View File

@ -30,18 +30,15 @@
#define MMMouseDownToCGEventType(button) \
((button) == (LEFT_BUTTON) ? kCGEventLeftMouseDown \
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseDown \
: kCGEventOtherMouseDown))
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseDown : kCGEventOtherMouseDown))
#define MMMouseUpToCGEventType(button) \
((button) == LEFT_BUTTON ? kCGEventLeftMouseUp \
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseUp \
: kCGEventOtherMouseUp))
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseUp : kCGEventOtherMouseUp))
#define MMMouseDragToCGEventType(button) \
((button) == LEFT_BUTTON ? kCGEventLeftMouseDragged \
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseDragged \
: kCGEventOtherMouseDragged))
: ((button) == RIGHT_BUTTON ? kCGEventRightMouseDragged : kCGEventOtherMouseDragged))
#elif defined(IS_WINDOWS)
@ -50,13 +47,11 @@
#define MMMouseUpToMEventF(button) \
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTUP \
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTUP \
: MOUSEEVENTF_MIDDLEUP))
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTUP : MOUSEEVENTF_MIDDLEUP))
#define MMMouseDownToMEventF(button) \
((button) == LEFT_BUTTON ? MOUSEEVENTF_LEFTDOWN \
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \
: MOUSEEVENTF_MIDDLEDOWN))
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_MIDDLEDOWN))
#endif
@ -158,8 +153,8 @@ MMPointInt32 getMousePos(){
unsigned int more_garbage;
Display *display = XGetMainDisplay();
XQueryPointer(display, XDefaultRootWindow(display), &garb1, &garb2,
&x, &y, &garb_x, &garb_y, &more_garbage);
XQueryPointer(display, XDefaultRootWindow(display), &garb1, &garb2, &x, &y,
&garb_x, &garb_y, &more_garbage);
return MMPointInt32Make(x, y);
#elif defined(IS_WINDOWS)
@ -179,8 +174,7 @@ void toggleMouse(bool down, MMMouseButton button){
#if defined(IS_MACOSX)
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
const CGEventType mouseType = MMMouseToCGEventType(down, button);
CGEventRef event = CGEventCreateMouseEvent(NULL,
mouseType, currentPos, (CGMouseButton)button);
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseType, currentPos, (CGMouseButton)button);
CGEventPost(kCGSessionEventTap, event);
CFRelease(event);
@ -215,32 +209,26 @@ void clickMouse(MMMouseButton button){
*/
void doubleClick(MMMouseButton button){
#if defined(IS_MACOSX)
/* Double click for Mac. */
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button);
const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button);
CGEventRef event = CGEventCreateMouseEvent(
NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);
/* Set event to double click. */
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2);
CGEventPost(kCGHIDEventTap, event);
CGEventSetType(event, mouseTypeUP);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
#else
/* Double click for everything else. */
clickMouse(button);
microsleep(200);
clickMouse(button);
#endif
}
@ -272,13 +260,9 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection){
/* Make scroll magnitude negative if we're scrolling down. */
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;
event = CGEventCreateScrollWheelEvent(NULL,
kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
CGEventPost(kCGHIDEventTap, event);
#elif defined(USE_X11)
int x;
int dir = 4; /* Button 4 is up, 5 is down. */
Display *display = XGetMainDisplay();
@ -293,9 +277,7 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection){
}
XSync(display, false);
#elif defined(IS_WINDOWS)
mouseScrollInput.type = INPUT_MOUSE;
mouseScrollInput.mi.dx = 0;
mouseScrollInput.mi.dy = 0;
@ -305,7 +287,6 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection){
mouseScrollInput.mi.mouseData = WHEEL_DELTA * scrollDirection * cleanScrollMagnitude;
SendInput(1, &mouseScrollInput, sizeof(mouseScrollInput));
#endif
}
@ -322,16 +303,12 @@ void scrollMouseXY(int x, int y) {
/* Set up the OS specific solution */
#if defined(__APPLE__)
CGEventRef event;
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
#elif defined(USE_X11)
int ydir = 4; /* Button 4 is up, 5 is down. */
int xdir = 6;
Display *display = XGetMainDisplay();
@ -355,9 +332,7 @@ void scrollMouseXY(int x, int y) {
}
XSync(display, false);
#elif defined(IS_WINDOWS)
mouseScrollInputH.type = INPUT_MOUSE;
mouseScrollInputH.mi.dx = 0;
mouseScrollInputH.mi.dy = 0;
@ -408,9 +383,7 @@ bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed)
double velo_x = 0.0, velo_y = 0.0;
double distance;
while ((distance =
crude_hypot((double)pos.x - endPoint.x, (double)pos.y - endPoint.y)
) > 1.0) {
while ((distance =crude_hypot((double)pos.x - endPoint.x, (double)pos.y - endPoint.y)) > 1.0) {
double gravity = DEADBEEF_UNIFORM(5.0, 500.0);
// double gravity = DEADBEEF_UNIFORM(lowSpeed, highSpeed);

View File

@ -1136,25 +1136,6 @@ func PasteStr(str string) string {
return KeyTap("v", "control")
}
// Deprecated: use the TypeStr(),
//
// TypeString send a string, support unicode(no linux support)
// TypeStr(string: The string to send), Wno-deprecated
//
// This function will be removed in version v1.0.0
func TypeString(str string, delay ...int) {
tt.Drop("TypeString", "TypeStr")
var cdelay C.size_t
cstr := C.CString(str)
if len(delay) > 0 {
cdelay = C.size_t(delay[0])
}
C.type_string_delayed(cstr, cdelay)
C.free(unsafe.Pointer(cstr))
}
// TypeStrDelay type string delayed
func TypeStrDelay(str string, delay int) {
TypeStr(str)

View File

@ -37,13 +37,10 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
bufferSize = CFDataGetLength(imageData);
buffer = malloc(bufferSize);
CFDataGetBytes(imageData, CFRangeMake(0, bufferSize), buffer);
bitmap = createMMBitmap_c(buffer,
CGImageGetWidth(image), CGImageGetHeight(image),
CGImageGetBytesPerRow(image), CGImageGetBitsPerPixel(image),
CGImageGetBitsPerPixel(image) / 8);
bitmap = createMMBitmap_c(buffer, CGImageGetWidth(image), CGImageGetHeight(image),
CGImageGetBytesPerRow(image), CGImageGetBitsPerPixel(image),CGImageGetBitsPerPixel(image) / 8);
CFRelease(imageData);
CGImageRelease(image);
@ -58,16 +55,15 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
display = XGetMainDisplay();
}
XImage *image = XGetImage(display, XDefaultRootWindow(display),
XImage *image = XGetImage(display, XDefaultRootWindow(display),
(int)rect.origin.x, (int)rect.origin.y,
(unsigned int)rect.size.w, (unsigned int)rect.size.h,
AllPlanes, ZPixmap);
(unsigned int)rect.size.w, (unsigned int)rect.size.h, AllPlanes, ZPixmap);
XCloseDisplay(display);
if (image == NULL) { return NULL; }
bitmap = createMMBitmap_c((uint8_t *)image->data,
rect.size.w, rect.size.h, (size_t)image->bytes_per_line,
(uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8);
rect.size.w, rect.size.h, (size_t)image->bytes_per_line,
(uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8);
image->data = NULL; /* Steal ownership of bitmap data so we don't have to copy it. */
XDestroyImage(image);
@ -103,9 +99,8 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0);
/* Copy the data into a bitmap struct. */
if ((screenMem = CreateCompatibleDC(screen)) == NULL ||
SelectObject(screenMem, dib) == NULL ||
!BitBlt(screenMem, (int)0, (int)0, (int)rect.size.w, (int)rect.size.h,
if ((screenMem = CreateCompatibleDC(screen)) == NULL || SelectObject(screenMem, dib) == NULL ||
!BitBlt(screenMem, (int)0, (int)0, (int)rect.size.w, (int)rect.size.h,
screen, rect.origin.x, rect.origin.y, SRCCOPY)
) {
@ -118,7 +113,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
}
bitmap = createMMBitmap_c(NULL, rect.size.w, rect.size.h, 4 * rect.size.w,
(uint8_t)bi.bmiHeader.biBitCount, 4);
(uint8_t)bi.bmiHeader.biBitCount, 4);
/* Copy the data to our pixel buffer. */
if (bitmap != NULL) {