2

How can I capture the screen with Haskell on Mac OS X?

I've read Screen capture in Haskell?. But I'm working on a Mac Mini. So, the Windows solution is not applicable and the GTK solution does not work because it only captures a black screen. GTK in Macs only captures black screens.

Community
  • 1
  • 1

3 Answers3

2

How can I capture the screen with … and OpenGL?

Only with some luck. OpenGL is primarily a drawing API and the contents of the main framebuffer are undefined unless it's drawn to by OpenGL functions themself. That OpenGL could be abused was due to the way graphics system did manage their on-screen windows' framebuffers: After a window without predefined background color/brush was created, its initial framebuffer content was simply everything that was on the screen right before the window's creation. If a OpenGL context is created on top of this, the framebuffer could be read out using glReadPixels, that way creating a screenshot.

Today window compositing has become the norm which makes abusing OpenGL for taking screenshots almost impossible. With compositing each window has its own off-screen framebuffer and the screen's contents are composited only at the end. If you used that method outlined above, which relies on uninitialized memory containing the desired content, on a compositing window system, the results will vary wildly, between solid clear color, over wildly distorted junk fragments, to data noise.

Since taking a screenshot reliably must take into account a lot of idiosyncrasy of the system this is to happen on, it's virtually impossible to write a truly portable screenshot program.

And OpenGL is definitely the wrong tool for it, no matter that people (including myself) were able to abuse it for such in the past.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • So, will OpenGL return the same black screen as GTK does? Have you tested some code on Macs? – Juan Carlos Kuri Pinto Mar 21 '13 at 15:44
  • @JuanCarlosKuriPinto: Macs are tricky beasts, because OpenGL is so deeply ingrained into the OS, that relying on undefined behavior of OpenGL means relying on undefined behavior of the OS. Also that the GTK+ example returns a black screen is for the same reason as it's undefined behavior for OpenGL: With a compositing WM the contents of a window's DC, and be it the root DC, will not contain the image visible on the screen. – datenwolf Mar 21 '13 at 23:53
0

I programmed this C code to capture the screen of Macs and to show it in an OpenGL window through the function glDrawPixels:

opengl-capture.c

http://pastebin.com/pMH2rDNH

Coding the FFI for Haskell is quite trivial. I'll do it soon.

-2

This might be useful to find the solution in C:

NeHe Productions - Using gluUnProject
http://nehe.gamedev.net/article/using_gluunproject/16013/

Apple Mailing Lists - Re: Screen snapshot example code posted
http://lists.apple.com/archives/cocoa-dev/2005/Aug/msg00901.html

Compiling OpenGL programs on Windows, Linux and OS X
http://goanna.cs.rmit.edu.au/~gl/teaching/Interactive3D/2012/compiling.html

Grab Mac OS Screen using GL_RGB format

Community
  • 1
  • 1
  • What exactly does gluUnProject help with grabbing a *screenshot*. This not a good answer, and even if it were, for the wrong problem. – datenwolf Mar 21 '13 at 17:37
  • The link explains how to use glReadPixels in the correct way to capture the color of a single pixel at a specific location. For example: the mouse's location. – Juan Carlos Kuri Pinto Mar 21 '13 at 22:24
  • But this is not what OP asked for. OP wants to use OpenGL to make screenshots, i.e. not just read back what was rendered using OpenGL, but what's visible on the whole display. No need to unproject some location and it also doesn't solve OP's problem. Namely that OpenGL is not the right tool for this. And the last link you posted, it describes a technique that relies on undefined behavior that will break the instant its running on a composited window manager system. – datenwolf Mar 21 '13 at 23:49