diff --git a/binding.gyp b/binding.gyp index fa8e5743..cda2eed6 100644 --- a/binding.gyp +++ b/binding.gyp @@ -17,7 +17,7 @@ } ] ], - "sources": ["autonode.cc", "deadbeef_rand.c", "mouse.c", "screen.c"] + "sources": ["src/autonode.cc", "src/deadbeef_rand.c", "src/mouse.c", "src/screen.c"] } ] } diff --git a/screengrab.back2 b/screengrab.back2 deleted file mode 100644 index 9545964f..00000000 --- a/screengrab.back2 +++ /dev/null @@ -1,248 +0,0 @@ -#include "screengrab.h" -#include "bmp_io.h" -#include "endian.h" -#include /* malloc() */ - -#if defined(IS_MACOSX) - #include - #include - #include -#elif defined(USE_X11) - #include - #include - #include "xdisplay.h" -#elif defined(IS_WINDOWS) - #include -#endif - -#if defined(IS_MACOSX) - -/* Helper functions (documented below). */ -static CGLContextObj createFullScreenCGLContext(CGOpenGLDisplayMask displayMask); -static void destroyFullScreenCGLContext(CGLContextObj glContext); - -static uint8_t *createBufferFromCurrentCGLContext(GLint x, - GLint y, - GLsizei width, - GLsizei height, - size_t bytewidth); - -#endif - -MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) -{ -#if defined(IS_MACOSX) - /* The following is a very modified version of the glGrab code example - * given by Apple (as are some of the convenience functions called). */ - size_t bytewidth; - uint8_t bitsPerPixel, bytesPerPixel; - uint8_t *buffer; - - /* Build OpenGL context of entire screen */ - CGDirectDisplayID displayID = CGMainDisplayID(); - CGOpenGLDisplayMask mask = CGDisplayIDToOpenGLDisplayMask(displayID); - CGLContextObj glContext = createFullScreenCGLContext(mask); - if (glContext == NULL) return NULL; - - /* TODO: CGDisplayBitsPerPixel() is deprecated in Snow Leopard; I'm not - * sure of the replacement function. */ - bitsPerPixel = (uint8_t)CGDisplayBitsPerPixel(displayID); - bytesPerPixel = bitsPerPixel / 8; - - /* Align width to padding. */ - bytewidth = ADD_PADDING(rect.size.width * bytesPerPixel); - - /* Convert Quartz point to postscript point. */ - rect.origin.y = CGDisplayPixelsHigh(displayID) - rect.origin.y - rect.size.height; - - /* Extract buffer from context */ - buffer = createBufferFromCurrentCGLContext((GLint)rect.origin.x, - (GLint)rect.origin.y, - (GLsizei)rect.size.width, - (GLsizei)rect.size.height, - bytewidth); - /* Reset and release GL context */ - destroyFullScreenCGLContext(glContext); - if (buffer == NULL) return NULL; - - /* Convert from OpenGL (origin at bottom left) to Quartz (origin at top - * left) coordinate system. */ - flipBitmapData(buffer, rect.size.width, rect.size.height, bytewidth); - - return createMMBitmap(buffer, rect.size.width, rect.size.height, bytewidth, - bitsPerPixel, bytesPerPixel); -#elif defined(USE_X11) - MMBitmapRef bitmap; - - Display *display = XOpenDisplay(NULL); - XImage *image = XGetImage(display, - XDefaultRootWindow(display), - (int)rect.origin.x, - (int)rect.origin.y, - (unsigned int)rect.size.width, - (unsigned int)rect.size.height, - AllPlanes, ZPixmap); - XCloseDisplay(display); - if (image == NULL) return NULL; - - bitmap = createMMBitmap((uint8_t *)image->data, - rect.size.width, - rect.size.height, - (size_t)image->bytes_per_line, - (uint8_t)image->bits_per_pixel, - (uint8_t)image->bits_per_pixel / 8); - image->data = NULL; /* Steal ownership of bitmap data so we don't have to - * copy it. */ - XDestroyImage(image); - - return bitmap; -#elif defined(IS_WINDOWS) - MMBitmapRef bitmap; - void *data; - HDC screen = NULL, screenMem = NULL; - HBITMAP dib; - BITMAPINFO bi; - - /* Initialize bitmap info. */ - bi.bmiHeader.biSize = sizeof(bi.bmiHeader); - bi.bmiHeader.biWidth = (long)rect.size.width; - bi.bmiHeader.biHeight = -(long)rect.size.height; /* Non-cartesian, please */ - bi.bmiHeader.biPlanes = 1; - bi.bmiHeader.biBitCount = 32; - bi.bmiHeader.biCompression = BI_RGB; - bi.bmiHeader.biSizeImage = (DWORD)(4 * rect.size.width * rect.size.height); - bi.bmiHeader.biXPelsPerMeter = 0; - bi.bmiHeader.biYPelsPerMeter = 0; - bi.bmiHeader.biClrUsed = 0; - bi.bmiHeader.biClrImportant = 0; - - screen = GetDC(NULL); /* Get entire screen */ - if (screen == NULL) return NULL; - - /* Get screen data in display device context. */ - dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0); - - /* Copy the data into a bitmap struct. */ - if ((screenMem = CreateCompatibleDC(screen)) == NULL || - SelectObject(screenMem, dib) == NULL || - !BitBlt(screenMem, - 0, - 0, - (int)rect.size.width, - (int)rect.size.height, screen, (int)rect.origin.x, (int)rect.origin.y, SRCCOPY)) { - /* Error copying data. */ - ReleaseDC(NULL, screen); - DeleteObject(dib); - if (screenMem != NULL) DeleteDC(screenMem); - - return NULL; - } - - bitmap = createMMBitmap(NULL, - rect.size.width, - rect.size.height, - 4 * rect.size.width, - (uint8_t)bi.bmiHeader.biBitCount, - 4); - - /* Copy the data to our pixel buffer. */ - if (bitmap != NULL) { - bitmap->imageBuffer = malloc(bitmap->bytewidth * bitmap->height); - memcpy(bitmap->imageBuffer, data, bitmap->bytewidth * bitmap->height); - } - - ReleaseDC(NULL, screen); - DeleteObject(dib); - DeleteDC(screenMem); - - return bitmap; -#endif -} - -#if defined(IS_MACOSX) - -/* Creates and returns a full-screen OpenGL graphics context (to be - * released/destroyed by caller). - * - * To clean up the returned context use destroyFullScreenCGLContext(); */ -static CGLContextObj createFullScreenCGLContext(CGOpenGLDisplayMask displayMask) -{ - CGLContextObj glContext = NULL; - CGLPixelFormatObj pix; - GLint npix; - CGLPixelFormatAttribute attribs[4]; - - attribs[0] = kCGLPFAFullScreen; - attribs[1] = kCGLPFADisplayMask; - attribs[2] = displayMask; - attribs[3] = (CGLPixelFormatAttribute)0; - - CGLChoosePixelFormat(attribs, &pix, &npix); - CGLCreateContext(pix, NULL, &glContext); - - /* The pixel format is no longer needed, so destroy it. */ - CGLDestroyPixelFormat(pix); - - if (glContext == NULL) return NULL; - - /* Set our context as the current OpenGL context. */ - CGLSetCurrentContext(glContext); - - /* Set full-screen mode. */ - CGLSetFullScreen(glContext); - - /* Select front buffer as our source for pixel data. */ - glReadBuffer(GL_FRONT); - - /* Finish previous OpenGL commands before continuing. */ - glFinish(); - - if (glGetError() != GL_NO_ERROR) return NULL; - - return glContext; -} - -/* Cleans up CGLContext created by createFullScreenCGLContext(); */ -static void destroyFullScreenCGLContext(CGLContextObj glContext) -{ - glPopClientAttrib(); /* Clear attributes previously set. */ - CGLSetCurrentContext(NULL); /* Reset context. */ - CGLClearDrawable(glContext); /* Disassociate from full-screen. */ - CGLDestroyContext(glContext); /* Release memory. */ -} - -/* Returns newly malloc'd bitmap (to be freed by caller). */ -static uint8_t *createBufferFromCurrentCGLContext(GLint x, - GLint y, - GLsizei width, - GLsizei height, - size_t bytewidth) -{ - uint8_t *data = NULL; - - /* For extra safety, save & restore OpenGL states that are changed. */ - glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); - - glPixelStorei(GL_PACK_ALIGNMENT, BYTE_ALIGN); /* Force alignment. */ - glPixelStorei(GL_PACK_ROW_LENGTH, 0); - glPixelStorei(GL_PACK_SKIP_ROWS, 0); - glPixelStorei(GL_PACK_SKIP_PIXELS, 0); - - /* Allocate size for bitmap */ - data = malloc(bytewidth * height); - if (data == NULL) return NULL; - - /* Read the OpenGL frame into our buffer */ - glReadPixels(x, y, width, height, - MMRGB_IS_BGR ? GL_BGRA : GL_RGBA, -#if __BYTE_ORDER == __BIG_ENDIAN - GL_UNSIGNED_INT_8_8_8_8, /* Non-native format (little-endian) */ -#elif __BYTE_ORDER == __LITTLE_ENDIAN - GL_UNSIGNED_INT_8_8_8_8_REV, /* Native format */ -#endif - data); - - return data; -} - -#endif diff --git a/MMBitmap.c b/src/MMBitmap.c similarity index 100% rename from MMBitmap.c rename to src/MMBitmap.c diff --git a/MMBitmap.h b/src/MMBitmap.h similarity index 100% rename from MMBitmap.h rename to src/MMBitmap.h diff --git a/MMPointArray.c b/src/MMPointArray.c similarity index 100% rename from MMPointArray.c rename to src/MMPointArray.c diff --git a/MMPointArray.h b/src/MMPointArray.h similarity index 100% rename from MMPointArray.h rename to src/MMPointArray.h diff --git a/UTHashTable.c b/src/UTHashTable.c similarity index 100% rename from UTHashTable.c rename to src/UTHashTable.c diff --git a/UTHashTable.h b/src/UTHashTable.h similarity index 100% rename from UTHashTable.h rename to src/UTHashTable.h diff --git a/alert.c b/src/alert.c similarity index 100% rename from alert.c rename to src/alert.c diff --git a/alert.h b/src/alert.h similarity index 100% rename from alert.h rename to src/alert.h diff --git a/autonode.cc b/src/autonode.cc similarity index 100% rename from autonode.cc rename to src/autonode.cc diff --git a/base64.c b/src/base64.c similarity index 100% rename from base64.c rename to src/base64.c diff --git a/base64.h b/src/base64.h similarity index 100% rename from base64.h rename to src/base64.h diff --git a/bitmap_find.c b/src/bitmap_find.c similarity index 100% rename from bitmap_find.c rename to src/bitmap_find.c diff --git a/bitmap_find.h b/src/bitmap_find.h similarity index 100% rename from bitmap_find.h rename to src/bitmap_find.h diff --git a/bmp_io.c b/src/bmp_io.c similarity index 100% rename from bmp_io.c rename to src/bmp_io.c diff --git a/bmp_io.h b/src/bmp_io.h similarity index 100% rename from bmp_io.h rename to src/bmp_io.h diff --git a/color_find.c b/src/color_find.c similarity index 100% rename from color_find.c rename to src/color_find.c diff --git a/color_find.h b/src/color_find.h similarity index 100% rename from color_find.h rename to src/color_find.h diff --git a/deadbeef_rand.c b/src/deadbeef_rand.c similarity index 100% rename from deadbeef_rand.c rename to src/deadbeef_rand.c diff --git a/deadbeef_rand.h b/src/deadbeef_rand.h similarity index 100% rename from deadbeef_rand.h rename to src/deadbeef_rand.h diff --git a/endian.h b/src/endian.h similarity index 100% rename from endian.h rename to src/endian.h diff --git a/inline_keywords.h b/src/inline_keywords.h similarity index 100% rename from inline_keywords.h rename to src/inline_keywords.h diff --git a/io.c b/src/io.c similarity index 100% rename from io.c rename to src/io.c diff --git a/io.h b/src/io.h similarity index 100% rename from io.h rename to src/io.h diff --git a/keycode.c b/src/keycode.c similarity index 100% rename from keycode.c rename to src/keycode.c diff --git a/keycode.h b/src/keycode.h similarity index 100% rename from keycode.h rename to src/keycode.h diff --git a/keypress.c b/src/keypress.c similarity index 100% rename from keypress.c rename to src/keypress.c diff --git a/keypress.h b/src/keypress.h similarity index 100% rename from keypress.h rename to src/keypress.h diff --git a/microsleep.h b/src/microsleep.h similarity index 100% rename from microsleep.h rename to src/microsleep.h diff --git a/mouse.c b/src/mouse.c similarity index 100% rename from mouse.c rename to src/mouse.c diff --git a/mouse.h b/src/mouse.h similarity index 100% rename from mouse.h rename to src/mouse.h diff --git a/ms_stdbool.h b/src/ms_stdbool.h similarity index 100% rename from ms_stdbool.h rename to src/ms_stdbool.h diff --git a/ms_stdint.h b/src/ms_stdint.h similarity index 100% rename from ms_stdint.h rename to src/ms_stdint.h diff --git a/os.h b/src/os.h similarity index 100% rename from os.h rename to src/os.h diff --git a/pasteboard.c b/src/pasteboard.c similarity index 100% rename from pasteboard.c rename to src/pasteboard.c diff --git a/pasteboard.h b/src/pasteboard.h similarity index 100% rename from pasteboard.h rename to src/pasteboard.h diff --git a/png_io.c b/src/png_io.c similarity index 100% rename from png_io.c rename to src/png_io.c diff --git a/png_io.h b/src/png_io.h similarity index 100% rename from png_io.h rename to src/png_io.h diff --git a/rgb.h b/src/rgb.h similarity index 100% rename from rgb.h rename to src/rgb.h diff --git a/screen.c b/src/screen.c similarity index 100% rename from screen.c rename to src/screen.c diff --git a/screen.h b/src/screen.h similarity index 100% rename from screen.h rename to src/screen.h diff --git a/screengrab.c b/src/screengrab.c similarity index 100% rename from screengrab.c rename to src/screengrab.c diff --git a/screengrab.h b/src/screengrab.h similarity index 100% rename from screengrab.h rename to src/screengrab.h diff --git a/snprintf.c b/src/snprintf.c similarity index 100% rename from snprintf.c rename to src/snprintf.c diff --git a/snprintf.h b/src/snprintf.h similarity index 100% rename from snprintf.h rename to src/snprintf.h diff --git a/str_io.c b/src/str_io.c similarity index 100% rename from str_io.c rename to src/str_io.c diff --git a/str_io.h b/src/str_io.h similarity index 100% rename from str_io.h rename to src/str_io.h diff --git a/types.h b/src/types.h similarity index 100% rename from types.h rename to src/types.h diff --git a/uthash.h b/src/uthash.h similarity index 100% rename from uthash.h rename to src/uthash.h diff --git a/xdisplay.c b/src/xdisplay.c similarity index 100% rename from xdisplay.c rename to src/xdisplay.c diff --git a/xdisplay.h b/src/xdisplay.h similarity index 100% rename from xdisplay.h rename to src/xdisplay.h diff --git a/zlib_util.c b/src/zlib_util.c similarity index 100% rename from zlib_util.c rename to src/zlib_util.c diff --git a/zlib_util.h b/src/zlib_util.h similarity index 100% rename from zlib_util.h rename to src/zlib_util.h