Skip to content

Commit

Permalink
Merge pull request #44 from octalmage/windows-2
Browse files Browse the repository at this point in the history
Full Windows Support!
  • Loading branch information
octalmage committed Jul 25, 2015
2 parents 491f675 + 056b6b1 commit 2376e8a
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 68 deletions.
9 changes: 7 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'targets': [{
'target_name': 'robotjs',
'include_dirs': [
'<!(node -e \'require("nan")\')'
'node_modules/nan/'
],

'cflags': [
Expand Down Expand Up @@ -44,6 +44,10 @@
'sources': [
'src/xdisplay.c'
]
}],

["OS=='win'", {
'defines': ['IS_WINDOWS']
}]
],

Expand All @@ -55,7 +59,8 @@
'src/keycode.c',
'src/screen.c',
'src/screengrab.c',
'src/snprintf.c',
'src/MMBitmap.c'
]
}]
}
}
7 changes: 1 addition & 6 deletions src/MMBitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
#include "types.h"
#include "rgb.h"
#include <assert.h>

#if defined(_MSC_VER)
#include "ms_stdint.h"
#else
#include <stdint.h>
#endif
#include <stdint.h>

#ifdef __cplusplus
extern "C"
Expand Down
6 changes: 1 addition & 5 deletions src/deadbeef_rand.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#ifndef DEADBEEF_RAND_H
#define DEADBEEF_RAND_H

#if defined(_MSC_VER)
#include "ms_stdint.h"
#else
#include <stdint.h>
#endif
#include <stdint.h>

#define DEADBEEF_MAX UINT32_MAX

Expand Down
6 changes: 1 addition & 5 deletions src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

#include "MMBitmap.h"
#include <stddef.h>
#include <stdint.h>

#if defined(_MSC_VER)
#include "ms_stdint.h"
#else
#include <stdint.h>
#endif

enum _MMImageType {
kInvalidImageType = 0,
Expand Down
10 changes: 3 additions & 7 deletions src/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
#include <stdlib.h> /* For abs() */
#include <math.h>
#include "inline_keywords.h" /* For H_INLINE */
#include <stdint.h>

#if defined(_MSC_VER)
#include "ms_stdint.h"
#else
#include <stdint.h>
#endif

/* RGB colors in MMBitmaps are stored as BGR for convenience in converting
* to/from certain formats (mainly OpenGL).
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
18 changes: 4 additions & 14 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "screen.h"
#include "screengrab.h"
#include "MMBitmap.h"
#include "snprintf.h"
#include "microsleep.h"

using namespace v8;

Expand Down Expand Up @@ -287,18 +289,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();
Expand Down Expand Up @@ -348,7 +338,7 @@ NAN_METHOD(keyTap)
break;
default:
tapKeyCode(key, flags);
mssleep(10);
microsleep(10);
}

NanReturnValue(NanNew("1"));
Expand Down Expand Up @@ -406,7 +396,7 @@ NAN_METHOD(keyToggle)
break;
default:
toggleKeyCode(key, down, flags);
mssleep(10);
microsleep(10);
}

NanReturnValue(NanNew("1"));
Expand Down
29 changes: 17 additions & 12 deletions src/screengrab.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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. */
Expand Down
7 changes: 0 additions & 7 deletions src/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, <mark.martinec@ijs.si>\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;
Expand Down Expand Up @@ -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 == '+' ||
Expand Down
9 changes: 9 additions & 0 deletions src/snprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#include <stddef.h>
#include <stdarg.h>

#ifdef __cplusplus
extern "C"
{
#endif

#ifdef HAVE_SNPRINTF
#include <stdio.h>
#else
Expand All @@ -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
6 changes: 1 addition & 5 deletions src/str_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

#include "MMBitmap.h"
#include "io.h"
#include <stdint.h>

#if defined(_MSC_VER)
#include "ms_stdint.h"
#else
#include <stdint.h>
#endif

enum _MMBMPStringError {
kMMBMPStringGenericError = 0,
Expand Down
6 changes: 1 addition & 5 deletions src/uthash.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@

#include <string.h> /* memcmp, strlen */
#include <stddef.h> /* ptrdiff_t */
#include <stdint.h>

#if defined(_MSC_VER)
#include "ms_stdint.h"
#else
#include <stdint.h>
#endif

#define UTHASH_VERSION 1.8

Expand Down

0 comments on commit 2376e8a

Please sign in to comment.