diff --git a/package.json b/package.json index 666b530c..d6ec6d21 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "homepage": "https://github.com/octalmage/robotjs", "dependencies": { - "nan": "^1.3.0" + "nan": "^2.0.9" }, "devDependencies": { "tape": "^3.5.0" diff --git a/src/robotjs.cc b/src/robotjs.cc index 29a39707..b85b116f 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -28,65 +28,59 @@ int keyboardDelay = 10; NAN_METHOD(moveMouse) { - NanScope(); - if (args.Length() < 2) + if (info.Length() < 2) { - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } - size_t x = args[0]->Int32Value(); - size_t y = args[1]->Int32Value(); + size_t x = info[0]->Int32Value(); + size_t y = info[1]->Int32Value(); MMPoint point; point = MMPointMake(x, y); moveMouse(point); microsleep(mouseDelay); - NanReturnValue(NanNew("1")); +// info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(moveMouseSmooth) +NAN_METHOD(moveMouseSmooth) { - NanScope(); - if (args.Length() < 2) + if (info.Length() < 2) { - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } - size_t x = args[0]->Int32Value(); - size_t y = args[1]->Int32Value(); + size_t x = info[0]->Int32Value(); + size_t y = info[1]->Int32Value(); MMPoint point; point = MMPointMake(x, y); smoothlyMoveMouse(point); microsleep(mouseDelay); - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(getMousePos) +NAN_METHOD(getMousePos) { - NanScope(); - MMPoint pos = getMousePos(); //Return object with .x and .y. - Local obj = NanNew(); - obj->Set(NanNew("x"), NanNew((int)pos.x)); - obj->Set(NanNew("y"), NanNew((int)pos.y)); - NanReturnValue(obj); + Local obj = Nan::New(); + Nan::Set(obj, Nan::New("x").ToLocalChecked(), Nan::New((int)pos.x)); + Nan::Set(obj, Nan::New("y").ToLocalChecked(), Nan::New((int)pos.y)); + info.GetReturnValue().Set(obj); } -NAN_METHOD(mouseClick) +NAN_METHOD(mouseClick) { - NanScope(); - MMMouseButton button = LEFT_BUTTON; bool doubleC = false; - if (args.Length() > 0) + if (info.Length() > 0) { char *b; - v8::String::Utf8Value bstr(args[0]->ToString()); + v8::String::Utf8Value bstr(info[0]->ToString()); b = *bstr; if (strcmp(b, "left") == 0) @@ -103,23 +97,22 @@ NAN_METHOD(mouseClick) } else { - return NanThrowError("Invalid mouse button specified."); + return Nan::ThrowError("Invalid mouse button specified."); } } - - if (args.Length() == 2) + + if (info.Length() == 2) { - doubleC = args[1]->BooleanValue(); + doubleC = info[1]->BooleanValue(); } - else if (args.Length() > 2) + else if (info.Length() > 2) { - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } - + if (!doubleC) { clickMouse(button); - } else { @@ -128,19 +121,17 @@ NAN_METHOD(mouseClick) microsleep(mouseDelay); - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(mouseToggle) +NAN_METHOD(mouseToggle) { - NanScope(); - MMMouseButton button = LEFT_BUTTON; bool down; - if (args.Length() > 0) + if (info.Length() > 0) { - const char *d = (*v8::String::Utf8Value(args[0]->ToString())); + const char *d = (*v8::String::Utf8Value(info[0]->ToString())); if (strcmp(d, "down") == 0) { @@ -152,13 +143,13 @@ NAN_METHOD(mouseToggle) } else { - return NanThrowError("Invalid mouse button state specified."); + return Nan::ThrowError("Invalid mouse button state specified."); } } - if (args.Length() == 2) + if (info.Length() == 2) { - char *b = (*v8::String::Utf8Value(args[1]->ToString())); + char *b = (*v8::String::Utf8Value(info[1]->ToString())); if (strcmp(b, "left") == 0) { @@ -174,32 +165,30 @@ NAN_METHOD(mouseToggle) } else { - return NanThrowError("Invalid mouse button specified."); + return Nan::ThrowError("Invalid mouse button specified."); } } - else if (args.Length() > 2) + else if (info.Length() > 2) { - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } toggleMouse(down, button); microsleep(mouseDelay); - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(setMouseDelay) +NAN_METHOD(setMouseDelay) { - NanScope(); - - if (args.Length() != 1) + if (info.Length() != 1) { - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } - mouseDelay = args[0]->Int32Value(); + mouseDelay = info[0]->Int32Value(); - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } /* @@ -211,7 +200,7 @@ NAN_METHOD(setMouseDelay) |___/ */ -int CheckKeyCodes(char* k, MMKeyCode *key) +int CheckKeyCodes(char* k, MMKeyCode *key) { if (!key) return -1; @@ -337,7 +326,7 @@ int CheckKeyCodes(char* k, MMKeyCode *key) } else if (strlen(k) == 1) { - *key = keyCodeForChar(*k); + *key = keyCodeForChar(*k); } else { @@ -347,31 +336,31 @@ int CheckKeyCodes(char* k, MMKeyCode *key) return 0; } -int CheckKeyFlags(char* f, MMKeyFlags* flags) +int CheckKeyFlags(char* f, MMKeyFlags* flags) { if (!flags) return -1; - if (strcmp(f, "alt") == 0) + if (strcmp(f, "alt") == 0) { *flags = MOD_ALT; } - else if(strcmp(f, "command") == 0) + else if(strcmp(f, "command") == 0) { *flags = MOD_META; } - else if(strcmp(f, "control") == 0) + else if(strcmp(f, "control") == 0) { *flags = MOD_CONTROL; } - else if(strcmp(f, "shift") == 0) + else if(strcmp(f, "shift") == 0) { *flags = MOD_SHIFT; } - else if(strcmp(f, "none") == 0) + else if(strcmp(f, "none") == 0) { *flags = MOD_NONE; } - else + else { return -2; } @@ -379,22 +368,20 @@ int CheckKeyFlags(char* f, MMKeyFlags* flags) return 0; } -NAN_METHOD(keyTap) +NAN_METHOD(keyTap) { - NanScope(); - MMKeyFlags flags = MOD_NONE; MMKeyCode key; char *k; char *f; - v8::String::Utf8Value fstr(args[1]->ToString()); - v8::String::Utf8Value kstr(args[0]->ToString()); + v8::String::Utf8Value fstr(info[1]->ToString()); + v8::String::Utf8Value kstr(info[0]->ToString()); k = *kstr; f = *fstr; - switch (args.Length()) + switch (info.Length()) { case 2: break; @@ -402,57 +389,55 @@ NAN_METHOD(keyTap) f = NULL; break; default: - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } - if (f) + if (f) { - switch(CheckKeyFlags(f, &flags)) + switch(CheckKeyFlags(f, &flags)) { case -1: - return NanThrowError("Null pointer in key flag."); + return Nan::ThrowError("Null pointer in key flag."); break; case -2: - return NanThrowError("Invalid key flag specified."); + return Nan::ThrowError("Invalid key flag specified."); break; } } - switch(CheckKeyCodes(k, &key)) + switch(CheckKeyCodes(k, &key)) { case -1: - return NanThrowError("Null pointer in key code."); + return Nan::ThrowError("Null pointer in key code."); break; case -2: - return NanThrowError("Invalid key code specified."); + return Nan::ThrowError("Invalid key code specified."); break; default: tapKeyCode(key, flags); microsleep(keyboardDelay); } - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(keyToggle) +NAN_METHOD(keyToggle) { - NanScope(); - MMKeyFlags flags = MOD_NONE; MMKeyCode key; - + char *k; bool down; char *f; - v8::String::Utf8Value kstr(args[0]->ToString()); - v8::String::Utf8Value fstr(args[2]->ToString()); - down = args[1]->BooleanValue(); + v8::String::Utf8Value kstr(info[0]->ToString()); + v8::String::Utf8Value fstr(info[2]->ToString()); + down = info[1]->BooleanValue(); k = *kstr; f = *fstr; - switch (args.Length()) + switch (info.Length()) { case 3: break; @@ -460,64 +445,60 @@ NAN_METHOD(keyToggle) f = NULL; break; default: - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } - if (f) + if (f) { - switch(CheckKeyFlags(f, &flags)) + switch(CheckKeyFlags(f, &flags)) { case -1: - return NanThrowError("Null pointer in key flag."); + return Nan::ThrowError("Null pointer in key flag."); break; case -2: - return NanThrowError("Invalid key flag specified."); + return Nan::ThrowError("Invalid key flag specified."); break; } } - switch(CheckKeyCodes(k, &key)) + switch(CheckKeyCodes(k, &key)) { case -1: - return NanThrowError("Null pointer in key code."); + return Nan::ThrowError("Null pointer in key code."); break; case -2: - return NanThrowError("Invalid key code specified."); + return Nan::ThrowError("Invalid key code specified."); break; default: toggleKeyCode(key, down, flags); microsleep(keyboardDelay); } - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(typeString) +NAN_METHOD(typeString) { - NanScope(); - char *str; - NanUtf8String string(args[0]); + Nan::Utf8String string(info[0]); str = *string; typeString(str); - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } -NAN_METHOD(setKeyboardDelay) +NAN_METHOD(setKeyboardDelay) { - NanScope(); - - if (args.Length() != 1) + if (info.Length() != 1) { - return NanThrowError("Invalid number of arguments."); + return Nan::ThrowError("Invalid number of arguments."); } - keyboardDelay = args[0]->Int32Value(); + keyboardDelay = info[0]->Int32Value(); - NanReturnValue(NanNew("1")); + info.GetReturnValue().Set(Nan::New(1)); } /* @@ -529,86 +510,80 @@ NAN_METHOD(setKeyboardDelay) */ -NAN_METHOD(getPixelColor) +NAN_METHOD(getPixelColor) { - NanScope(); - MMBitmapRef bitmap; MMRGBHex color; - size_t x = args[0]->Int32Value(); - size_t y = args[1]->Int32Value(); + size_t x = info[0]->Int32Value(); + size_t y = info[1]->Int32Value(); bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, 1, 1)); color = MMRGBHexAtPoint(bitmap, 0, 0); - + char hex [7]; //Length needs to be 7 because snprintf includes a terminating null. - //Use %06x to pad hex value with leading 0s. + //Use %06x to pad hex value with leading 0s. snprintf(hex, 7, "%06x", color); destroyMMBitmap(bitmap); - NanReturnValue(NanNew(hex)); + info.GetReturnValue().Set(Nan::New(hex).ToLocalChecked()); } -NAN_METHOD(getScreenSize) +NAN_METHOD(getScreenSize) { - NanScope(); - //Get display size. MMSize displaySize = getMainDisplaySize(); //Create our return object. - Local obj = NanNew(); - obj->Set(NanNew("width"), NanNew(displaySize.width)); - obj->Set(NanNew("height"), NanNew(displaySize.height)); + Local obj = Nan::New(); + Nan::Set(obj, Nan::New("width").ToLocalChecked(), Nan::New(displaySize.width)); + Nan::Set(obj, Nan::New("height").ToLocalChecked(), Nan::New(displaySize.height)); //Return our object with .width and .height. - NanReturnValue(obj); + info.GetReturnValue().Set(obj); } -void init(Handle target) -{ +NAN_MODULE_INIT(InitAll){ - target->Set(NanNew("moveMouse"), - NanNew(moveMouse)->GetFunction()); + Nan::Set(target, Nan::New("moveMouse").ToLocalChecked(), + Nan::GetFunction(Nan::New(moveMouse)).ToLocalChecked()); - target->Set(NanNew("moveMouseSmooth"), - NanNew(moveMouseSmooth)->GetFunction()); + Nan::Set(target, Nan::New("moveMouseSmooth").ToLocalChecked(), + Nan::GetFunction(Nan::New(moveMouseSmooth)).ToLocalChecked()); - target->Set(NanNew("getMousePos"), - NanNew(getMousePos)->GetFunction()); + Nan::Set(target, Nan::New("getMousePos").ToLocalChecked(), + Nan::GetFunction(Nan::New(getMousePos)).ToLocalChecked()); - target->Set(NanNew("mouseClick"), - NanNew(mouseClick)->GetFunction()); + Nan::Set(target, Nan::New("mouseClick").ToLocalChecked(), + Nan::GetFunction(Nan::New(mouseClick)).ToLocalChecked()); - target->Set(NanNew("mouseToggle"), - NanNew(mouseToggle)->GetFunction()); + Nan::Set(target, Nan::New("mouseToggle").ToLocalChecked(), + Nan::GetFunction(Nan::New(mouseToggle)).ToLocalChecked()); - target->Set(NanNew("setMouseDelay"), - NanNew(setMouseDelay)->GetFunction()); + Nan::Set(target, Nan::New("setMouseDelay").ToLocalChecked(), + Nan::GetFunction(Nan::New(setMouseDelay)).ToLocalChecked()); - target->Set(NanNew("keyTap"), - NanNew(keyTap)->GetFunction()); - - target->Set(NanNew("keyToggle"), - NanNew(keyToggle)->GetFunction()); + Nan::Set(target, Nan::New("keyTap").ToLocalChecked(), + Nan::GetFunction(Nan::New(keyTap)).ToLocalChecked()); - target->Set(NanNew("typeString"), - NanNew(typeString)->GetFunction()); + Nan::Set(target, Nan::New("keyToggle").ToLocalChecked(), + Nan::GetFunction(Nan::New(keyToggle)).ToLocalChecked()); - target->Set(NanNew("setKeyboardDelay"), - NanNew(setKeyboardDelay)->GetFunction()); + Nan::Set(target, Nan::New("typeString").ToLocalChecked(), + Nan::GetFunction(Nan::New(typeString)).ToLocalChecked()); - target->Set(NanNew("getPixelColor"), - NanNew(getPixelColor)->GetFunction()); + Nan::Set(target, Nan::New("setKeyboardDelay").ToLocalChecked(), + Nan::GetFunction(Nan::New(setKeyboardDelay)).ToLocalChecked()); - target->Set(NanNew("getScreenSize"), - NanNew(getScreenSize)->GetFunction()); + Nan::Set(target, Nan::New("getPixelColor").ToLocalChecked(), + Nan::GetFunction(Nan::New(getPixelColor)).ToLocalChecked()); + Nan::Set(target, Nan::New("getScreenSize").ToLocalChecked(), + Nan::GetFunction(Nan::New(getScreenSize)).ToLocalChecked()); } -NODE_MODULE(robotjs, init) +NODE_MODULE(robotjs, InitAll)