diff --git a/src/mouse.c b/src/mouse.c index b437d0be..704138e2 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -118,7 +118,7 @@ void moveMouse(MMPoint point) mouseInput.mi.dwExtraInfo = 0; mouseInput.mi.mouseData = 0; SendInput(1, &mouseInput, sizeof(mouseInput)); - + #endif } @@ -202,7 +202,7 @@ void clickMouse(MMMouseButton button) */ void doubleClick(MMMouseButton button) { - + #if defined(IS_MACOSX) /* Double click for Mac. */ @@ -210,17 +210,17 @@ void doubleClick(MMMouseButton button) const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button); const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button); - CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft); - - /* Set event to double click. */ + 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); + CGEventPost(kCGHIDEventTap, event); + + CGEventSetType(event, mouseTypeUP); + CGEventPost(kCGHIDEventTap, event); + + CFRelease(event); #else @@ -228,17 +228,17 @@ void doubleClick(MMMouseButton button) clickMouse(button); microsleep(200); clickMouse(button); - + #endif } void scrollMouse(int x, int y) { #if defined(IS_WINDOWS) - // Fix for #97 https://github.com/octalmage/robotjs/issues/97, - // C89 needs variables declared on top of functions (mouseScrollInput) - INPUT mouseScrollInputH; - INPUT mouseScrollInputV; + // Fix for #97 https://github.com/octalmage/robotjs/issues/97, + // C89 needs variables declared on top of functions (mouseScrollInput) + INPUT mouseScrollInputH; + INPUT mouseScrollInputV; #endif /* Direction should only be considered based on the scrollDirection. This @@ -247,59 +247,59 @@ void scrollMouse(int x, int y) /* Set up the OS specific solution */ #if defined(__APPLE__) - CGEventRef event; + CGEventRef event; - event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x); - CGEventPost(kCGHIDEventTap, event); + event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x); + CGEventPost(kCGHIDEventTap, event); - CFRelease(event); + CFRelease(event); #elif defined(USE_X11) - int ydir = 4; /* Button 4 is up, 5 is down. */ - int xdir = 6; - Display *display = XGetMainDisplay(); + int ydir = 4; /* Button 4 is up, 5 is down. */ + int xdir = 6; + Display *display = XGetMainDisplay(); - if (y < 0){ - ydir = 5; - } - if (x < 0){ - xdir = 7; - } - - int xi; - int yi; - for (xi = 0; xi < abs(x); xi++) { - XTestFakeButtonEvent(display, xdir, 1, CurrentTime); - XTestFakeButtonEvent(display, xdir, 0, CurrentTime); - } - for (yi = 0; yi < abs(y); yi++) { - YTestFakeButtonEvent(display, ydir, 1, CurrentTime); - YTestFakeButtonEvent(display, ydir, 0, CurrentTime); - } + if (y < 0){ + ydir = 5; + } + if (x < 0){ + xdir = 7; + } - XFlush(display); + int xi; + int yi; + for (xi = 0; xi < abs(x); xi++) { + XTestFakeButtonEvent(display, xdir, 1, CurrentTime); + XTestFakeButtonEvent(display, xdir, 0, CurrentTime); + } + for (yi = 0; yi < abs(y); yi++) { + YTestFakeButtonEvent(display, ydir, 1, CurrentTime); + YTestFakeButtonEvent(display, ydir, 0, CurrentTime); + } + + XFlush(display); #elif defined(IS_WINDOWS) - mouseScrollInputH.type = INPUT_MOUSE; - mouseScrollInputH.mi.dx = 0; - mouseScrollInputH.mi.dy = 0; - mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_WHEEL; - mouseScrollInputH.mi.time = 0; - mouseScrollInputH.mi.dwExtraInfo = 0; - mouseScrollInputH.mi.mouseData = WHEEL_DELTA * x; - - mouseScrollInputV.type = INPUT_MOUSE; - mouseScrollInputV.mi.dx = 0; - mouseScrollInputV.mi.dy = 0; - mouseScrollInputV.mi.dwFlags = MOUSEEVENTF_HWHEEL; - mouseScrollInputV.mi.time = 0; - mouseScrollInputV.mi.dwExtraInfo = 0; - mouseScrollInputV.mi.mouseData = WHEEL_DELTA * y; - - SendInput(1, &mouseScrollInputH, sizeof(mouseScrollInputH)); - SendInput(1, &mouseScrollInputV, sizeof(mouseScrollInputV)); + mouseScrollInputH.type = INPUT_MOUSE; + mouseScrollInputH.mi.dx = 0; + mouseScrollInputH.mi.dy = 0; + mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_WHEEL; + mouseScrollInputH.mi.time = 0; + mouseScrollInputH.mi.dwExtraInfo = 0; + mouseScrollInputH.mi.mouseData = WHEEL_DELTA * x; + + mouseScrollInputV.type = INPUT_MOUSE; + mouseScrollInputV.mi.dx = 0; + mouseScrollInputV.mi.dy = 0; + mouseScrollInputV.mi.dwFlags = MOUSEEVENTF_HWHEEL; + mouseScrollInputV.mi.time = 0; + mouseScrollInputV.mi.dwExtraInfo = 0; + mouseScrollInputV.mi.mouseData = WHEEL_DELTA * y; + + SendInput(1, &mouseScrollInputH, sizeof(mouseScrollInputH)); + SendInput(1, &mouseScrollInputV, sizeof(mouseScrollInputV)); #endif } diff --git a/src/robotjs.cc b/src/robotjs.cc index 18d5f6ad..2c6bb4af 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -18,8 +18,8 @@ int mouseDelay = 10; int keyboardDelay = 10; /* - __ __ -| \/ | ___ _ _ ___ ___ + __ __ +| \/ | ___ _ _ ___ ___ | |\/| |/ _ \| | | / __|/ _ \ | | | | (_) | |_| \__ \ __/ |_| |_|\___/ \__,_|___/\___| @@ -238,29 +238,29 @@ NAN_METHOD(setMouseDelay) info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(scrollMouse) +NAN_METHOD(scrollMouse) { - if (info.Length() != 2) - { - return Nan::ThrowError("Invalid number of arguments."); + if (info.Length() != 2) + { + return Nan::ThrowError("Invalid number of arguments."); } - int x = info[0]->Int32Value(); - int y = info[1]->Int32Value(); - - scrollMouse(x, y); - microsleep(mouseDelay); - - info.GetReturnValue().Set(Nan::New(1)); + int x = info[0]->Int32Value(); + int y = info[1]->Int32Value(); + + scrollMouse(x, y); + microsleep(mouseDelay); + + info.GetReturnValue().Set(Nan::New(1)); } /* - _ __ _ _ + _ __ _ _ | |/ /___ _ _| |__ ___ __ _ _ __ __| | | ' // _ \ | | | '_ \ / _ \ / _` | '__/ _` | | . \ __/ |_| | |_) | (_) | (_| | | | (_| | |_|\_\___|\__, |_.__/ \___/ \__,_|_| \__,_| - |___/ + |___/ */ -struct KeyNames +struct KeyNames { const char* name; MMKeyCode key; @@ -300,7 +300,7 @@ static KeyNames key_names[] = { "space", K_SPACE }, { "printscreen", K_PRINTSCREEN }, { "insert", K_INSERT }, - + { "audio_mute", K_AUDIO_VOLUME_MUTE }, { "audio_vol_down", K_AUDIO_VOLUME_DOWN }, { "audio_vol_up", K_AUDIO_VOLUME_UP }, @@ -313,7 +313,7 @@ static KeyNames key_names[] = { "audio_forward", K_AUDIO_FORWARD }, { "audio_repeat", K_AUDIO_REPEAT }, { "audio_random", K_AUDIO_RANDOM }, - + { "lights_mon_up", K_LIGHTS_MON_UP }, { "lights_mon_down", K_LIGHTS_MON_DOWN }, { "lights_kbd_toggle",K_LIGHTS_KBD_TOGGLE }, @@ -336,7 +336,7 @@ int CheckKeyCodes(char* k, MMKeyCode *key) *key = K_NOT_A_KEY; KeyNames* kn = key_names; - while (kn->name) + while (kn->name) { if (strcmp(k, kn->name) == 0) { @@ -346,7 +346,7 @@ int CheckKeyCodes(char* k, MMKeyCode *key) kn++; } - if (*key == K_NOT_A_KEY) + if (*key == K_NOT_A_KEY) { return -2; } @@ -554,9 +554,9 @@ NAN_METHOD(typeStringDelayed) { char *str; Nan::Utf8String string(info[0]); - + str = *string; - + size_t cpm = info[1]->Int32Value(); typeStringDelayed(str, cpm); @@ -577,12 +577,12 @@ NAN_METHOD(setKeyboardDelay) } /* - ____ - / ___| ___ _ __ ___ ___ _ __ - \___ \ / __| '__/ _ \/ _ \ '_ \ + ____ + / ___| ___ _ __ ___ ___ _ __ + \___ \ / __| '__/ _ \/ _ \ '_ \ ___) | (__| | | __/ __/ | | | |____/ \___|_| \___|\___|_| |_| - + */ /** @@ -603,7 +603,7 @@ NAN_METHOD(getPixelColor) { return Nan::ThrowError("Invalid number of arguments."); } - + MMBitmapRef bitmap; MMRGBHex color; @@ -618,9 +618,9 @@ NAN_METHOD(getPixelColor) bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, 1, 1)); color = MMRGBHexAtPoint(bitmap, 0, 0); - + char hex[7]; - + padHex(color, hex); destroyMMBitmap(bitmap); @@ -642,18 +642,18 @@ NAN_METHOD(getScreenSize) info.GetReturnValue().Set(obj); } -NAN_METHOD(captureScreen) +NAN_METHOD(captureScreen) { size_t x; size_t y; size_t w; size_t h; - - //If user has provided screen coords, use them! + + //If user has provided screen coords, use them! if (info.Length() == 4) { //TODO: Make sure requested coords are within the screen bounds, or we get a seg fault. - // An error message is much nicer! + // An error message is much nicer! x = info[0]->Int32Value(); y = info[1]->Int32Value(); @@ -665,15 +665,15 @@ NAN_METHOD(captureScreen) //We're getting the full screen. x = 0; y = 0; - + //Get screen size. MMSize displaySize = getMainDisplaySize(); w = displaySize.width; h = displaySize.height; } - + MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, w, h)); - + uint32_t bufferSize = bitmap->bytewidth * bitmap->height; Local buffer = Nan::NewBuffer((char*)bitmap->imageBuffer, bufferSize, destroyMMBitmapBuffer, NULL).ToLocalChecked(); @@ -684,17 +684,17 @@ NAN_METHOD(captureScreen) Nan::Set(obj, Nan::New("bitsPerPixel").ToLocalChecked(), Nan::New(bitmap->bitsPerPixel)); Nan::Set(obj, Nan::New("bytesPerPixel").ToLocalChecked(), Nan::New(bitmap->bytesPerPixel)); Nan::Set(obj, Nan::New("image").ToLocalChecked(), buffer); - + info.GetReturnValue().Set(obj); } /* - ____ _ _ -| __ )(_) |_ _ __ ___ __ _ _ __ -| _ \| | __| '_ ` _ \ / _` | '_ \ + ____ _ _ +| __ )(_) |_ _ __ ___ __ _ _ __ +| _ \| | __| '_ ` _ \ / _` | '_ \ | |_) | | |_| | | | | | (_| | |_) | -|____/|_|\__|_| |_| |_|\__,_| .__/ - |_| +|____/|_|\__|_| |_| |_|\__,_| .__/ + |_| */ class BMP @@ -709,7 +709,7 @@ class BMP }; //Convert object from Javascript to a C++ class (BMP). -BMP buildBMP(Local info) +BMP buildBMP(Local info) { Local obj = Nan::To(info).ToLocalChecked(); @@ -781,7 +781,7 @@ NAN_MODULE_INIT(InitAll) Nan::Set(target, Nan::New("mouseToggle").ToLocalChecked(), Nan::GetFunction(Nan::New(mouseToggle)).ToLocalChecked()); - + Nan::Set(target, Nan::New("scrollMouse").ToLocalChecked(), Nan::GetFunction(Nan::New(scrollMouse)).ToLocalChecked()); @@ -808,10 +808,10 @@ NAN_MODULE_INIT(InitAll) Nan::Set(target, Nan::New("getScreenSize").ToLocalChecked(), Nan::GetFunction(Nan::New(getScreenSize)).ToLocalChecked()); - + Nan::Set(target, Nan::New("captureScreen").ToLocalChecked(), Nan::GetFunction(Nan::New(captureScreen)).ToLocalChecked()); - + Nan::Set(target, Nan::New("getColor").ToLocalChecked(), Nan::GetFunction(Nan::New(getColor)).ToLocalChecked()); }