Tutorial :register for WindowServer CGScreenRefreshCallback CGScreenUpdateMoveCallback in C++


I'm trying to register for CGScreenRefreshCallback and CGScreenUpdateMoveCallback ( here's what apple's saying about http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/Reference/reference.html#//apple_ref/doc/uid/TP30001070-CH1g-F16970 ) using C++ only.

I wrote this simple tester for the refresh callback in order to retrieve the changing rectangles:

#include "ApplicationServices/ApplicationServices.h"  #include <iostream>    using namespace std;    /////////////HEADER  static void DHRefreshCallback (CGRectCount count,const CGRect * rectArray,void * userParameter);  ///////////////////    int main (int argc, char * const argv[]) {    CGRegisterScreenRefreshCallback(DHRefreshCallback, NULL);    while (true) {      // just hanging  }  return 0;  }      static void DHRefreshCallback (CGRectCount count,const CGRect * rectArray,void * userParameter){  cout << "something changed" << endl;  return;  }  

...but didn't work.

I know I need a connection with WindowServer (Quartz Compositor \ Quartz Extreme \ Quartz Extreme 2D...still can't figure out the difference) and a running thread in order to get these callbacks, but I really don't know how to do this in C++ only (no Objective-C at all).

any direction?

thx in advance, pigiuz


It's not about using/not using Objective-C. It's about the event loop in OS X apps in general, which is done by CFRunloop, which is a C API. See Run Loop management and CFRunLoop reference. You also need a connection to the window server, which can be established by calling

Instead of

while (true) {      // just hanging  }  

just do

extern "C" void NSApplicationLoad(void);  NSApplicationLoad();  // establish a connection to the window server. In <Cocoa/Cocoa.h>  CFRunLoopRun();       // run the event loop  

Don't forget to link against Cocoa.framework; just add -framework Cocoa in the command line of the compiler. You can #import <Cocoa/Cocoa.h> but then you need to use Objective-C++ because of the Objective-C classes declared in it.

You could use

 RunApplicationEventLoop(); //establish a connection to the window server                               //and runs the event loop. In <Carbon/Carbon.h>  

in an 32 bit app, instead of NSApplicationLoad + CFRunLoopRun, but it's not available in an 64 bit app.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »