diff --git a/README.md b/README.md index 02f0c109..b5b37d26 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ RobotJS Node.js Desktop Automation. Control the mouse, keyboard, and read the screen. -This is a work in progress so the exported functions could change at any time before the first stable release (1.0.0). [Ideas?](https://github.com/octalmage/robotjs/issues/4) +RobotJS supports Mac, [Windows](https://github.com/octalmage/robotjs/issues/2), and [Linux](https://github.com/octalmage/robotjs/issues/17). -RobotJS is configured for Mac and [Linux](https://github.com/octalmage/robotjs/issues/17), but this project will support [Windows](https://github.com/octalmage/robotjs/issues/2) soon. +This is a work in progress so the exported functions could change at any time before the first stable release (1.0.0). [Ideas?](https://github.com/octalmage/robotjs/issues/4) [Check out some of the cool things people are making with RobotJS](https://github.com/octalmage/robotjs/wiki/Projects-using-RobotJS)! Have your own rad RobotJS project? Feel free to add it! diff --git a/binding.gyp b/binding.gyp index 34b495bb..19860425 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,7 +2,7 @@ 'targets': [{ 'target_name': 'robotjs', 'include_dirs': [ - ' - -#if defined(_MSC_VER) - #include "ms_stdint.h" -#else - #include -#endif +#include #ifdef __cplusplus extern "C" diff --git a/src/deadbeef_rand.h b/src/deadbeef_rand.h index d607db66..ec569a24 100644 --- a/src/deadbeef_rand.h +++ b/src/deadbeef_rand.h @@ -1,11 +1,7 @@ #ifndef DEADBEEF_RAND_H #define DEADBEEF_RAND_H -#if defined(_MSC_VER) - #include "ms_stdint.h" -#else - #include -#endif +#include #define DEADBEEF_MAX UINT32_MAX diff --git a/src/io.h b/src/io.h index b341d452..47eb974f 100644 --- a/src/io.h +++ b/src/io.h @@ -4,12 +4,8 @@ #include "MMBitmap.h" #include +#include -#if defined(_MSC_VER) - #include "ms_stdint.h" -#else - #include -#endif enum _MMImageType { kInvalidImageType = 0, diff --git a/src/rgb.h b/src/rgb.h index 2dbeea2c..7a2ef5c1 100644 --- a/src/rgb.h +++ b/src/rgb.h @@ -5,12 +5,8 @@ #include /* For abs() */ #include #include "inline_keywords.h" /* For H_INLINE */ +#include -#if defined(_MSC_VER) - #include "ms_stdint.h" -#else - #include -#endif /* RGB colors in MMBitmaps are stored as BGR for convenience in converting * to/from certain formats (mainly OpenGL). @@ -82,7 +78,7 @@ H_INLINE int MMRGBColorSimilarToColor(MMRGBColor c1, MMRGBColor c2, uint8_t d1 = c1.red - c2.red; uint8_t d2 = c1.green - c2.green; uint8_t d3 = c1.blue - c2.blue; - return sqrt((d1 * d1) + + return sqrt((double)(d1 * d1) + (d2 * d2) + (d3 * d3)) <= (tolerance * 442.0f); } @@ -98,7 +94,7 @@ H_INLINE int MMRGBHexSimilarToColor(MMRGBHex h1, MMRGBHex h2, float tolerance) uint8_t d1 = RED_FROM_HEX(h1) - RED_FROM_HEX(h2); uint8_t d2 = GREEN_FROM_HEX(h1) - GREEN_FROM_HEX(h2); uint8_t d3 = BLUE_FROM_HEX(h1) - BLUE_FROM_HEX(h2); - return sqrt((d1 * d1) + + return sqrt((double)(d1 * d1) + (d2 * d2) + (d3 * d3)) <= (tolerance * 442.0f); } diff --git a/src/robotjs.cc b/src/robotjs.cc index cfabd340..baaef123 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -8,6 +8,8 @@ #include "screen.h" #include "screengrab.h" #include "MMBitmap.h" +#include "snprintf.h" +#include "microsleep.h" using namespace v8; @@ -33,6 +35,8 @@ NAN_METHOD(moveMouse) MMPoint point; point = MMPointMake(x, y); moveMouse(point); + microsleep(10); + NanReturnValue(NanNew("1")); } @@ -49,6 +53,8 @@ NAN_METHOD(moveMouseSmooth) MMPoint point; point = MMPointMake(x, y); smoothlyMoveMouse(point); + microsleep(10); + NanReturnValue(NanNew("1")); } @@ -98,6 +104,7 @@ NAN_METHOD(mouseClick) } clickMouse(button); + microsleep(10); NanReturnValue(NanNew("1")); } @@ -154,6 +161,7 @@ NAN_METHOD(mouseToggle) } toggleMouse(down, button); + microsleep(10); NanReturnValue(NanNew("1")); } @@ -287,18 +295,6 @@ int CheckKeyFlags(char* f, MMKeyFlags* flags) return 0; } -int mssleep(unsigned long millisecond) -{ - struct timespec req; - time_t sec=(int)(millisecond/1000); - millisecond=millisecond-(sec*1000); - req.tv_sec=sec; - req.tv_nsec=millisecond*1000000L; - while(nanosleep(&req,&req)==-1) - continue; - return 1; -} - NAN_METHOD(keyTap) { NanScope(); @@ -348,7 +344,7 @@ NAN_METHOD(keyTap) break; default: tapKeyCode(key, flags); - mssleep(10); + microsleep(10); } NanReturnValue(NanNew("1")); @@ -406,7 +402,7 @@ NAN_METHOD(keyToggle) break; default: toggleKeyCode(key, down, flags); - mssleep(10); + microsleep(10); } NanReturnValue(NanNew("1")); diff --git a/src/screengrab.c b/src/screengrab.c index 59a8fd5a..8061b6c0 100644 --- a/src/screengrab.c +++ b/src/screengrab.c @@ -25,10 +25,10 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) CGDirectDisplayID displayID = CGMainDisplayID(); - //Replacement for CGDisplayBitsPerPixel. + //Replacement for CGDisplayBitsPerPixel. CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayID); size_t depth = 0; - + CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(mode); if(CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) depth = 32; @@ -37,7 +37,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) else if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) depth = 8; - bitsPerPixel = (uint8_t) depth; + bitsPerPixel = (uint8_t) depth; bytesPerPixel = bitsPerPixel / 8; /* Align width to padding. */ //bytewidth = ADD_PADDING(rect.size.width * bytesPerPixel); @@ -52,15 +52,15 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) CGDataProviderRef provider = CGImageGetDataProvider(image); CFDataRef data = CGDataProviderCopyData(provider); - size_t width, height; + size_t width, height; width = CGImageGetWidth(image); - height = CGImageGetHeight(image); + height = CGImageGetHeight(image); size_t bpp = CGImageGetBitsPerPixel(image) / 8; uint8 *pixels = malloc(width * height * bpp); memcpy(pixels, CFDataGetBytePtr(data), width * height * bpp); - CFRelease(data); - CGImageRelease(image); + CFRelease(data); + CGImageRelease(image); return createMMBitmap(pixels, rect.size.width, rect.size.height, bytewidth, bitsPerPixel, bytesPerPixel); @@ -118,11 +118,16 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) /* Copy the data into a bitmap struct. */ if ((screenMem = CreateCompatibleDC(screen)) == NULL || SelectObject(screenMem, dib) == NULL || - !BitBlt(screenMem, - (int)rect.origin.x, - (int)rect.origin.y, + !BitBlt(screenMem, + (int)0, + (int)0, (int)rect.size.width, - (int)rect.size.height, screen, 0, 0, SRCCOPY)) { + (int)rect.size.height, + screen, + rect.origin.x, + rect.origin.y, + SRCCOPY)) { + /* Error copying data. */ ReleaseDC(NULL, screen); DeleteObject(dib); @@ -135,7 +140,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) rect.size.width, rect.size.height, 4 * rect.size.width, - (uint8_t)bi.bmiHeader.biBitCount, + (uint8_t)bi.bmiHeader.biBitCount, 4); /* Copy the data to our pixel buffer. */ diff --git a/src/snprintf.c b/src/snprintf.c index ca476727..22ae3677 100644 --- a/src/snprintf.c +++ b/src/snprintf.c @@ -410,11 +410,6 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap); /* declarations */ -static char credits[] = "\n\ -@(#)snprintf.c, v2.2: Mark Martinec, \n\ -@(#)snprintf.c, v2.2: Copyright 1999, Mark Martinec. Frontier Artistic License applies.\n\ -@(#)snprintf.c, v2.2: http://www.ijs.si/software/snprintf/\n"; - #if defined(NEED_ASPRINTF) int asprintf(char **ptr, const char *fmt, /*args*/ ...) { va_list ap; @@ -593,8 +588,6 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { char fmt_spec = '\0'; /* current conversion specifier character */ - str_arg = credits;/* just to make compiler happy (defined but not used)*/ - str_arg = NULL; starting_p = p; p++; /* skip '%' */ /* parse flags */ while (*p == '0' || *p == '-' || *p == '+' || diff --git a/src/snprintf.h b/src/snprintf.h index f097afa2..60046e56 100644 --- a/src/snprintf.h +++ b/src/snprintf.h @@ -15,6 +15,11 @@ #include #include +#ifdef __cplusplus +extern "C" +{ +#endif + #ifdef HAVE_SNPRINTF #include #else @@ -35,3 +40,7 @@ extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...); extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap); #endif + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/src/str_io.h b/src/str_io.h index 1f7639c7..0a6f8ed0 100644 --- a/src/str_io.h +++ b/src/str_io.h @@ -4,12 +4,8 @@ #include "MMBitmap.h" #include "io.h" +#include -#if defined(_MSC_VER) - #include "ms_stdint.h" -#else - #include -#endif enum _MMBMPStringError { kMMBMPStringGenericError = 0, diff --git a/src/uthash.h b/src/uthash.h index 9f6c2b53..1ea0fea1 100644 --- a/src/uthash.h +++ b/src/uthash.h @@ -27,12 +27,8 @@ #include /* memcmp, strlen */ #include /* ptrdiff_t */ +#include -#if defined(_MSC_VER) - #include "ms_stdint.h" -#else - #include -#endif #define UTHASH_VERSION 1.8 diff --git a/test/mouse.js b/test/mouse.js index 90598ad4..517d3c95 100644 --- a/test/mouse.js +++ b/test/mouse.js @@ -1,6 +1,6 @@ var test = require('tape'); var robot = require('..'); -var lastKnownPos; +var lastKnownPos, currentPos; test('Get the initial mouse position.', function(t) { @@ -9,3 +9,13 @@ test('Get the initial mouse position.', function(t) t.ok(lastKnownPos.x !== undefined, 'mousepos.x is a valid value.'); t.ok(lastKnownPos.y !== undefined, 'mousepos.y is a valid value.'); }); + +test('Move the mouse.', function(t) +{ + t.plan(3); + lastKnownPos = robot.moveMouse(0, 0); + t.ok(robot.moveMouse(100, 100), 'successfully moved the mouse.'); + currentPos = robot.getMousePos(); + t.ok(currentPos.x === 100, 'mousepos.x is correct.'); + t.ok(currentPos.y === 100, 'mousepos.y is correct.'); +});