1
1
#include < napi.h>
2
- #include < vector>
3
- #include < sstream>
4
- #include < iostream>
5
- #include < cstdint>
6
2
#include " mouse.h"
7
3
#include " buffer_finalizer.h"
8
4
#include " deadbeef_rand.h"
9
5
#include " keypress.h"
10
6
#include " screen.h"
11
7
#include " screengrab.h"
12
8
#include " MMBitmap.h"
13
- #include " snprintf.h"
14
9
#include " microsleep.h"
15
10
#if defined(USE_X11)
16
11
#include " xdisplay.h"
@@ -109,26 +104,6 @@ Napi::Number _moveMouse(const Napi::CallbackInfo &info)
109
104
return Napi::Number::New (env, 1 );
110
105
}
111
106
112
- Napi::Number _moveMouseSmooth (const Napi::CallbackInfo &info)
113
- {
114
- Napi::Env env = info.Env ();
115
-
116
- if (info.Length () != 2 )
117
- {
118
- throw Napi::Error::New (env, " Invalid number of arguments." );
119
- }
120
-
121
- size_t x = info[0 ].As <Napi::Number>().Int32Value ();
122
- size_t y = info[1 ].As <Napi::Number>().Int32Value ();
123
-
124
- MMPoint point;
125
- point = MMPointMake (x, y);
126
- smoothlyMoveMouse (point);
127
- microsleep (mouseDelay);
128
-
129
- return Napi::Number::New (env, 1 );
130
- }
131
-
132
107
Napi::Object _getMousePos (const Napi::CallbackInfo &info)
133
108
{
134
109
Napi::Env env = info.Env ();
@@ -626,51 +601,6 @@ Napi::Number _setKeyboardDelay(const Napi::CallbackInfo &info)
626
601
627
602
*/
628
603
629
- /* *
630
- * Pad hex color code with leading zeros.
631
- * @param color Hex value to pad.
632
- * @param hex Hex value to output.
633
- */
634
- void padHex (MMRGBHex color, char *hex)
635
- {
636
- // Length needs to be 7 because snprintf includes a terminating null.
637
- // Use %06x to pad hex value with leading 0s.
638
- snprintf (hex, 7 , " %06x" , color);
639
- }
640
-
641
- Napi::String _getPixelColor (const Napi::CallbackInfo &info)
642
- {
643
- Napi::Env env = info.Env ();
644
-
645
- if (info.Length () != 2 )
646
- {
647
- throw Napi::Error::New (env, " Invalid number of arguments." );
648
- }
649
-
650
- MMBitmapRef bitmap;
651
- MMRGBHex color;
652
-
653
- size_t x = info[0 ].As <Napi::Number>().Int32Value ();
654
- size_t y = info[1 ].As <Napi::Number>().Int32Value ();
655
-
656
- if (!pointVisibleOnMainDisplay (MMPointMake (x, y)))
657
- {
658
- throw Napi::Error::New (env, " Requested coordinates are outside the main screen's dimensions." );
659
- }
660
-
661
- bitmap = copyMMBitmapFromDisplayInRect (MMRectMake (x, y, 1 , 1 ));
662
-
663
- color = MMRGBHexAtPoint (bitmap, 0 , 0 );
664
-
665
- char hex[7 ];
666
-
667
- padHex (color, hex);
668
-
669
- destroyMMBitmap (bitmap);
670
-
671
- return Napi::String::New (env, hex);
672
- }
673
-
674
604
Napi::Object _getScreenSize (const Napi::CallbackInfo &info)
675
605
{
676
606
Napi::Env env = info.Env ();
@@ -756,27 +686,19 @@ Napi::Object _captureScreen(const Napi::CallbackInfo &info)
756
686
757
687
if (!(x >= 0 && x < displaySize.width ))
758
688
{
759
- std::stringstream s;
760
- s << " x coordinate is out of bounds. Should be within 0 and " << displaySize.width << " but is: " << x;
761
- throw Napi::Error::New (env, s.str ());
689
+ throw Napi::Error::New (env, " Error: x coordinate outside of display" );
762
690
}
763
691
if (!(y >= 0 && y < displaySize.height ))
764
692
{
765
- std::stringstream s;
766
- s << " y coordinate is out of bounds. Should be within 0 and " << displaySize.height << " but is: " << y;
767
- throw Napi::Error::New (env, s.str ());
693
+ throw Napi::Error::New (env, " Error: y coordinate outside of display" );
768
694
}
769
695
if (!((x + w) >= 0 && (x + w) < displaySize.width ))
770
696
{
771
- std::stringstream s;
772
- s << " Rect is out of bounds. Should be within 0 and " << displaySize.width << " but is: (" << x << " ," << x + w << " )" ;
773
- throw Napi::Error::New (env, s.str ());
697
+ throw Napi::Error::New (env, " Error: Given width exceeds display dimensions" );
774
698
}
775
699
if (!((y + h) >= 0 && (y + h) < displaySize.height ))
776
700
{
777
- std::stringstream s;
778
- s << " Rect is out of bounds. Should be within 0 and " << displaySize.height << " but is: (" << y << " ," << y + h << " )" ;
779
- throw Napi::Error::New (env, s.str ());
701
+ throw Napi::Error::New (env, " Error: Given height exceeds display dimensions" );
780
702
}
781
703
}
782
704
else
@@ -806,85 +728,10 @@ Napi::Object _captureScreen(const Napi::CallbackInfo &info)
806
728
return obj;
807
729
}
808
730
809
- /*
810
- ____ _ _
811
- | __ )(_) |_ _ __ ___ __ _ _ __
812
- | _ \| | __| '_ ` _ \ / _` | '_ \
813
- | |_) | | |_| | | | | | (_| | |_) |
814
- |____/|_|\__|_| |_| |_|\__,_| .__/
815
- |_|
816
- */
817
-
818
- class BMP
819
- {
820
- public:
821
- size_t width;
822
- size_t height;
823
- size_t byteWidth;
824
- uint8_t bitsPerPixel;
825
- uint8_t bytesPerPixel;
826
- uint8_t *image;
827
- };
828
-
829
- // Convert object from Javascript to a C++ class (BMP).
830
- BMP buildBMP (const Napi::Object &info)
831
- {
832
- BMP img;
833
-
834
- img.width = info.Get (" width" ).As <Napi::Number>().Uint32Value ();
835
- img.height = info.Get (" height" ).As <Napi::Number>().Uint32Value ();
836
- img.byteWidth = info.Get (" byteWidth" ).As <Napi::Number>().Uint32Value ();
837
- img.bitsPerPixel = info.Get (" bitsPerPixel" ).As <Napi::Number>().Uint32Value ();
838
- img.bytesPerPixel = info.Get (" bytesPerPixel" ).As <Napi::Number>().Uint32Value ();
839
-
840
- Napi::Buffer<char > imageBuffer = info.Get (" image" ).As <Napi::Buffer<char >>();
841
- char *buf = imageBuffer.Data ();
842
-
843
- // Convert the buffer to a uint8_t which createMMBitmap requires.
844
- img.image = (uint8_t *)malloc (img.byteWidth * img.height );
845
- memcpy (img.image , buf, img.byteWidth * img.height );
846
-
847
- return img;
848
- }
849
-
850
- Napi::String _getColor (const Napi::CallbackInfo &info)
851
- {
852
- Napi::Env env = info.Env ();
853
-
854
- MMBitmapRef bitmap;
855
- MMRGBHex color;
856
-
857
- size_t x = info[1 ].As <Napi::Number>().Int32Value ();
858
- size_t y = info[2 ].As <Napi::Number>().Int32Value ();
859
-
860
- // Get our image object from JavaScript.
861
- BMP img = buildBMP (info[0 ].As <Napi::Object>());
862
-
863
- // Create the bitmap.
864
- bitmap = createMMBitmap (img.image , img.width , img.height , img.byteWidth , img.bitsPerPixel , img.bytesPerPixel );
865
-
866
- // Make sure the requested pixel is inside the bitmap.
867
- if (!MMBitmapPointInBounds (bitmap, MMPointMake (x, y)))
868
- {
869
- throw Napi::Error::New (env, " Requested coordinates are outside the bitmap's dimensions." );
870
- }
871
-
872
- color = MMRGBHexAtPoint (bitmap, x, y);
873
-
874
- char hex[7 ];
875
-
876
- padHex (color, hex);
877
-
878
- destroyMMBitmap (bitmap);
879
-
880
- return Napi::String::New (env, hex);
881
- }
882
-
883
731
Napi::Object Init (Napi::Env env, Napi::Object exports)
884
732
{
885
733
exports.Set (Napi::String::New (env, " dragMouse" ), Napi::Function::New (env, _dragMouse));
886
734
exports.Set (Napi::String::New (env, " moveMouse" ), Napi::Function::New (env, _moveMouse));
887
- exports.Set (Napi::String::New (env, " moveMouseSmooth" ), Napi::Function::New (env, _moveMouseSmooth));
888
735
exports.Set (Napi::String::New (env, " getMousePos" ), Napi::Function::New (env, _getMousePos));
889
736
exports.Set (Napi::String::New (env, " mouseClick" ), Napi::Function::New (env, _mouseClick));
890
737
exports.Set (Napi::String::New (env, " mouseToggle" ), Napi::Function::New (env, _mouseToggle));
@@ -897,11 +744,9 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
897
744
exports.Set (Napi::String::New (env, " typeStringDelayed" ), Napi::Function::New (env, _typeStringDelayed));
898
745
exports.Set (Napi::String::New (env, " setKeyboardDelay" ), Napi::Function::New (env, _setKeyboardDelay));
899
746
900
- exports.Set (Napi::String::New (env, " getPixelColor" ), Napi::Function::New (env, _getPixelColor));
901
747
exports.Set (Napi::String::New (env, " getScreenSize" ), Napi::Function::New (env, _getScreenSize));
902
748
exports.Set (Napi::String::New (env, " highlight" ), Napi::Function::New (env, _highlight));
903
749
exports.Set (Napi::String::New (env, " captureScreen" ), Napi::Function::New (env, _captureScreen));
904
- exports.Set (Napi::String::New (env, " getColor" ), Napi::Function::New (env, _getColor));
905
750
exports.Set (Napi::String::New (env, " getXDisplayName" ), Napi::Function::New (env, _getXDisplayName));
906
751
exports.Set (Napi::String::New (env, " setXDisplayName" ), Napi::Function::New (env, _setXDisplayName));
907
752
0 commit comments