Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

#include <OpenGL/gl.h> in mac os x to fix compile issues #27

Closed
wants to merge 1 commit into from

7 participants

@brynmathias

get an occurrence of:
src/screengrab.c:194: error: ‘GL_FRONT’ undeclared (first use in this function)
Where GL_FRONT is defined in OpenGL/gl.h

@lecheel

after apply gl.h it pass compiling in OSX 10.7 but get black screen using bitmap.capture_screen(..)

@lecheel

since CGLSetFullScreen and CGLSetFullScreenOnDisplay is deprecated in 10.6 and 10.7 how make it captured displays working on 10.7.3 :-(

@karanlyons

I'm getting this same issue (after including gl.h in screen grab.c autopy compiles, but screen captures are entirely black). This is on 10.7.3. Has anyone been able to solve this issue? I'd have guessed it had something to do with the removal of CGDisplayBitsPerPixel, but polyfilling it doesn't solve the problem.

@itsachen

/* TODO: CGDisplayBitsPerPixel() is deprecated in Snow Leopard; I'm not sure of the replacement function. */

Well shit.

I need autopy for a project thats due in a couple of months and I'm contemplating replacing the deprecated functions...not sure how difficult that will be.

@pbharrin

I think you need to replace the deprecated functions. I get nine errors on Mountain Lion, it should not be too bad only nine updates.

@itsachen

How did you log the errors?

@pbharrin

when I did >sudo python setup.py install, the errors appeared in the output.

@pbharrin

This post on SO gives the exact same errors that I was getting:
http://stackoverflow.com/questions/12993126/errors-while-installing-python-autopy

@zewt

I'm not planning on fixing this (since this project is quite dead), but if anyone's looking for the right API to use, search for CGDisplayCreateImage.

@msanders msanders closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 18, 2011
  1. @brynmathias
This page is out of date. Refresh to see the latest.
Showing with 173 additions and 172 deletions.
  1. +173 −172 src/screengrab.c
View
345 src/screengrab.c
@@ -4,14 +4,15 @@
#include <stdlib.h> /* malloc() */
#if defined(IS_MACOSX)
- #include <OpenGL/OpenGL.h>
- #include <ApplicationServices/ApplicationServices.h>
+ #include <OpenGL/OpenGL.h>
+ #include <OpenGL/gl.h>
+ #include <ApplicationServices/ApplicationServices.h>
#elif defined(USE_X11)
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include "xdisplay.h"
+ #include <X11/Xlib.h>
+ #include <X11/Xutil.h>
+ #include "xdisplay.h"
#elif defined(IS_WINDOWS)
- #include <string.h>
+ #include <string.h>
#endif
#if defined(IS_MACOSX)
@@ -31,130 +32,130 @@ static uint8_t *createBufferFromCurrentCGLContext(GLint x,
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);
+ /* 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;
+ 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,
- (int)rect.origin.x,
- (int)rect.origin.y,
- (int)rect.size.width,
- (int)rect.size.height, screen, 0, 0, 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;
+ 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,
+ (int)rect.origin.x,
+ (int)rect.origin.y,
+ (int)rect.size.width,
+ (int)rect.size.height, screen, 0, 0, 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
}
@@ -166,48 +167,48 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
* To clean up the returned context use destroyFullScreenCGLContext(); */
static CGLContextObj createFullScreenCGLContext(CGOpenGLDisplayMask displayMask)
{
- CGLContextObj glContext = NULL;
- CGLPixelFormatObj pix;
- GLint npix;
- CGLPixelFormatAttribute attribs[4];
+ CGLContextObj glContext = NULL;
+ CGLPixelFormatObj pix;
+ GLint npix;
+ CGLPixelFormatAttribute attribs[4];
- attribs[0] = kCGLPFAFullScreen;
- attribs[1] = kCGLPFADisplayMask;
- attribs[2] = displayMask;
- attribs[3] = (CGLPixelFormatAttribute)0;
+ attribs[0] = kCGLPFAFullScreen;
+ attribs[1] = kCGLPFADisplayMask;
+ attribs[2] = displayMask;
+ attribs[3] = (CGLPixelFormatAttribute)0;
- CGLChoosePixelFormat(attribs, &pix, &npix);
- CGLCreateContext(pix, NULL, &glContext);
+ CGLChoosePixelFormat(attribs, &pix, &npix);
+ CGLCreateContext(pix, NULL, &glContext);
- /* The pixel format is no longer needed, so destroy it. */
- CGLDestroyPixelFormat(pix);
+ /* The pixel format is no longer needed, so destroy it. */
+ CGLDestroyPixelFormat(pix);
- if (glContext == NULL) return NULL;
+ if (glContext == NULL) return NULL;
- /* Set our context as the current OpenGL context. */
- CGLSetCurrentContext(glContext);
+ /* Set our context as the current OpenGL context. */
+ CGLSetCurrentContext(glContext);
- /* Set full-screen mode. */
- CGLSetFullScreen(glContext);
+ /* Set full-screen mode. */
+ CGLSetFullScreen(glContext);
- /* Select front buffer as our source for pixel data. */
- glReadBuffer(GL_FRONT);
+ /* Select front buffer as our source for pixel data. */
+ glReadBuffer(GL_FRONT);
- /* Finish previous OpenGL commands before continuing. */
- glFinish();
+ /* Finish previous OpenGL commands before continuing. */
+ glFinish();
- if (glGetError() != GL_NO_ERROR) return NULL;
+ if (glGetError() != GL_NO_ERROR) return NULL;
- return glContext;
+ 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. */
+ 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). */
@@ -217,31 +218,31 @@ static uint8_t *createBufferFromCurrentCGLContext(GLint x,
GLsizei height,
size_t bytewidth)
{
- uint8_t *data = NULL;
+ uint8_t *data = NULL;
- /* For extra safety, save & restore OpenGL states that are changed. */
- glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
+ /* 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);
+ 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;
+ /* 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,
+ /* 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) */
+ 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 */
+ GL_UNSIGNED_INT_8_8_8_8_REV, /* Native format */
#endif
- data);
+ data);
- return data;
+ return data;
}
#endif
Something went wrong with that request. Please try again.