Register for display reconfiguration callbacks

I am building a Mac OS X application and I am trying to register to receive display failure notifications, but now I am really lost. I read Apple documentation and some forum posts, etc., but everyone seems to understand things better than I apparently possess. I understand that I have to request a callback inside the start loop so that it works correctly. However, I do not know how to set up the basic loop of the loop. I also feel that the example that Apple has in its documentation is missing from the materials they expect from me. To show my ignorance, I feel that everything should look.

NSRunLoop *rLoop = [NSRunLoop currentRunLoop]; codeToStartRunLoop void MyDisplayReconfigurationCallBack ( CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo); { if (flags & kCGDisplayAddFlag) { NSLog (@"Display Added"); } else if (kCGDisplayRemoveFlag) { NSLog (@"Display Removed"); } } CGDisplayRegisterReconfigurationCallback(MyDisplayReconfigurationCallBack, NULL); 

The actual code I received was from an Apple example, but it tells me that flags is an undeclared identifier at this point and will not compile. Not that it worked correctly, since I don't have it in the startup loop. I was hoping to find a tutorial somewhere that explains registering for a system callback in a run loop, but was not successful. If anyone could point me in the right direction, I would really appreciate it.

(I am sure you can say on my question that I am very green. I taught myself Objective-C from a book as my first programming language. I skipped C, so every once in a while I hit something that I can not understand.)

+4
source share
1 answer

If you are writing a Mac OS X application, AppKit has already set up a launch cycle for you, so you don’t need to worry about this part. You really need to create your own launch cycle in Cocoa when you also create your own thread.

For the "uneclared identifier" part, this looks like a typo / syntax error:

 void MyDisplayReconfigurationCallBack (CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo); // Semicolon makes this an invalid function definition^^ { // This is an anonymous block,* and flags wasn't declared in it if (flags & kCGDisplayAddFlag) { // etc. } 

In addition, unlike some other languages, you cannot declare or define functions inside other functions, methods or blocks * - they must be at the top level of the file. You cannot put this in the same place where you call CGDisplayRegisterReconfigurationCallback .

As a sample (I have no idea what the rest of your code looks like):

 // MyClassThatIsInterestedInDisplayConfiguration.m #import "MyClassThatIsInterestedInDisplayConfiguration.h" // Define callback function at top level of file void MyDisplayReconfigurationCallBack ( CGDirectDisplayID display, CGDisplayChangeSummaryFlags flags, void *userInfo) { if (flags & kCGDisplayAddFlag) { NSLog (@"Display Added"); } else if (kCGDisplayRemoveFlag) { NSLog (@"Display Removed"); } } @implementation MyClassThatIsInterestedInDisplayConfiguration - (void) comeOnBabyAndDoTheRegistrationWithMe { // Register callback function inside a method CGDisplayRegisterReconfigurationCallback(MyDisplayReconfigurationCallBack, NULL); } @end 

* The main C-brace is a separable thing, not the new cool special Obj-C function .

+5
source

All Articles