CGDisplayRegisterReconfigurationCallback: callback not called - objective-c

Why doesn't the function displayChanged get fired in the following code?
#import <Cocoa/Cocoa.h>
static void displayChanged(CGDirectDisplayID displayID, CGDisplayChangeSummaryFlags flags, void *userInfo) {
NSLog(#"%#, %#", displayID, flags);
}
int main(int argc, const char * argv[])
{
#autoreleasepool {
CGDisplayRegisterReconfigurationCallback(displayChanged, NULL);
CFRunLoopRun();
}
return 0;
}
I'm physically removing (and plugging in) my external display, but the function is never run.
Why?

Just found the solution on this other question
Before calling CFRunLoopRun you have to call NSApplicationLoad to establish a connection with the window server. This is the fixed code for the original question:
#import <Cocoa/Cocoa.h>
static void displayChanged(CGDirectDisplayID displayID, CGDisplayChangeSummaryFlags flags, void *userInfo) {
NSLog(#"%u, %u", displayID, flags);
}
int main(int argc, const char * argv[])
{
#autoreleasepool {
CGDisplayRegisterReconfigurationCallback(displayChanged, NULL);
NSApplicationLoad(); // establish a connection to the window server. In <Cocoa/Cocoa.h>
CFRunLoopRun(); // run the event loop
}
return 0;
}

I couldn't get CGDisplayRegisterReconfigurationCallback to work, so I used distributed notifications instead:
int main(int argc, const char * argv[]) {
#autoreleasepool {
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:#"com.apple.BezelServices.BMDisplayHWReconfiguredEvent" object:nil queue:nil usingBlock:^(NSNotification *notification) {
NSLog(#"Displays changed!");
}];
CFRunLoopRun();
}
return 0;
}

If you're using AppKit (and have a running NSApplication event loop), you can listen for the NSApplicationDidChangeScreenParametersNotification notification. Or, you can implement the -applicationDidChangeScreenParameters: method in your application delegate, which amounts to the same thing.

Related

How to change mouse settings programmatically in macOS using IOKit

The functions IOHIDGetAccelerationWithKey and IOHIDSetAccelerationWithKey are deprecated since macOS 10.12, therefore I am trying to implement the same using other IO*-methods.
I have never worked with IOKit, thus, all I can do is google for functions and try to get it to work.
Now I found this: Can't edit IORegistryEntry which has an example of how to change TrackpadThreeFingerSwipe property, however it is using a function which is not defined for me: getEVSHandle. Googling for it reveals only that it should be Found in the MachineSettings framework, however I can't seem to add any "MachineSettings" framework in Xcode 11.
What should I do? Current code is like:
#import <Foundation/Foundation.h>
#import <IOKit/hidsystem/IOHIDLib.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
NSInteger value = -65536;
CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberNSIntegerType, &value);
CFMutableDictionaryRef propertyDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, NULL, NULL);
CFDictionarySetValue(propertyDict, #"HIDMouseAcceleration", number);
io_connect_t connect = getEVSHandle(); // ???
if (!connect)
{
NSLog(#"Unable to get EVS handle");
}
res = IOConnectSetCFProperties(connect, propertyDict);
if (res != KERN_SUCCESS)
{
NSLog(#"Failed to set mouse acceleration (%d)", res);
}
IOObjectRelease(service);
CFRelease(propertyDict);
}
return 0;
}
The following works (tested with Xcode 11.2 / macOS 10.15)
#import <Foundation/Foundation.h>
#import <IOKit/hidsystem/IOHIDLib.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
io_service_t service = IORegistryEntryFromPath(kIOMasterPortDefault,
kIOServicePlane ":/IOResources/IOHIDSystem");
NSDictionary *parameters = (__bridge NSDictionary *)IORegistryEntryCreateCFProperty(service,
CFSTR(kIOHIDParametersKey), kCFAllocatorDefault, kNilOptions);
NSLog(#"%#", parameters);
NSMutableDictionary *newParameters = [parameters mutableCopy];
newParameters[#"HIDMouseAcceleration"] = #(12345);
kern_return_t result = IORegistryEntrySetCFProperty(service,
CFSTR(kIOHIDParametersKey), (__bridge CFDictionaryRef)newParameters);
NSLog(kIOReturnSuccess == result ? #"Updated" : #"Failed");
IOObjectRelease(service);
}
return 0;
}

Adding Blocks to Inactive Dispatch Queue

I have a situation where it would be helpful to be able to add blocks to an inactive queue which is then activated after another event happens. However, the entire thread from which I call dispatch_sync gets locked if the dispatch queue is suspended/inactive.
Is something like this possible? If not what other options do I have?
An example (not my actual use case):
#include <stdio.h>
#import <dispatch/dispatch.h>
int main(int argc, const char * argv[]) {
dispatch_queue_t my_queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_SERIAL_INACTIVE);
void (^b)(int) = ^void(int c) {
printf("%i\n", c);
};
for(int i = 1; i <= 1000; i++) {
dispatch_sync(my_queue, ^{ b(i); });
}
dispatch_activate(my_queue);
return 0;
}
I figured it out. Using dispatch_async fixed the problem.
Working solution:
#include <stdio.h>
#import <dispatch/dispatch.h>
int main(int argc, const char * argv[]) {
dispatch_queue_t my_queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_SERIAL_INACTIVE);
void (^b)(int) = ^void(int c) {
printf("%i\n", c);
};
for(int i = 1; i <= 1000; i++) {
dispatch_async(my_queue, ^{ b(i); });
}
dispatch_activate(my_queue);
return 0;
}

Why Does This Objective C/C++ Code Require main.m instead of main.mm?

I get strange code errors when I rename the following command line program from main.m to main.mm. Works just fine as main.m. Anyone know why?
https://stackoverflow.com/a/36469891/105539
SOURCE
#import <Foundation/Foundation.h>
void detectNewFile (
ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
int i;
char **paths = eventPaths;
printf("GOT AN EVENT!!!!\n");
for (i=0; i<numEvents; i++) {
printf("Change %llu in %s, flags %u\n", eventIds[i], paths[i], (unsigned int)eventFlags[i]);
}
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
short nPathCount = 2;
CFStringRef mypath[nPathCount];
mypath[0] = CFSTR("/Users/mike/Documents");
mypath[1] = CFSTR("/Users/mike/Downloads");
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&mypath, nPathCount, NULL);
void *callbackInfo = NULL;
CFAbsoluteTime latency = 1.0; // seconds
FSEventStreamRef hStream = FSEventStreamCreate(NULL,
&detectNewFile,
callbackInfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagFileEvents
);
FSEventStreamScheduleWithRunLoop(hStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(hStream);
printf("Waiting on new file creations...\n");
CFRunLoopRun(); // runs in an endless loop, only letting the callback function run
} // end autorelease pool
return 0;
}
ERRORS
FOR:
char **paths = eventPaths;
Cannot initialize a variable of type 'char **' with an lvalue of type 'void *'
FOR:
FSEventStreamRef hStream = FSEventStreamCreate(NULL,
&detectNewFile,
callbackInfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagFileEvents
);
No matching function for call to 'FSEventStreamCreate'
Thanks to #johnelemans, I found the problems. In C, it's legal to have automatic casting from void * to char **, but not in C++, which is what the .mm file would switch this into. The fix is to use casting:
char **paths = (char **)eventPaths;
Then, on the FSEventStreamCreate, it didn't like the void * instead of this:
FSEventStreamContext *callbackInfo = NULL;
...and didn't like the CFAbsoluteTime instead of:
CFTimeInterval latency = 1.0; // seconds
Then, you need to add CoreServices.framework library to the build steps.
I made those changes and it compiles now.

Overriding main.m with run loop but still getting to the NSApplicationMain call?

Apologies if this is a stupid/easy question, but still getting used to everything in Mac land.
Dave was kind enough to answer a question for me here:
Modify NSEvent to send a different key than the one that was pressed
which resulted in the following code, which works great:
#import <Cocoa/Cocoa.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//0x0b is the virtual keycode for "b"
//0x09 is the virtual keycode for "v"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x0B) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x09);
}
return event;
}
int main(int argc, char *argv[]) {
//return NSApplicationMain(argc, (const char **)argv);
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
if (!eventTap) {
NSLog(#"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
Thanks to some helpful error messages (can't have two "main" methods) I figured out that I had to put this code in the main.m file (right?). That means I'm overwriting the default method:
int main(int argc, char *argv[]) {
return NSApplicationMain(argc, (const char **)argv);
}
Which means none of my other Objective-C based code is firing. But if I uncomment that bit (or make any other attempt to call NSApplicationMain) then the main.m run loop is what doesn't run.
I imagine this is fairly simple for a seasoned Mac guy, but I'm having a hard time wrapping my head around it. Thanks.
I don't see any reason why the same idea won't work elsewhere in your code. Can you put it in the -applicationDidFinishLaunching: method of your app delegate? If you do, you will not need the CFRunLoop() call, since the run loop will already be running. Nor will you need the autorelease pool bit.

Nested functions are disabled; use f-nested functions to re-enable

I am just learning Objective C and I am having great difficulty. This is what is typed and it is giving me an error. I typed the text that is bold. What is wrong with it. It gives me the nested function error right after int main(void)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [NSAutoreleasePool alloc] init];
// **#include <stdio.h>
int main(void)
int amount = 1000000;
printf("The amount in your account is $%i\n", amount);
return 0;
}**
NSLog(#"Hello, World!");
[pool drain];
return 0;
}
Your problem is that C and it's brethren do not like functions within functions (putting aside gcc extensions for now).
What you seem to be trying to do is to declare a whole new main inside your main. That's a big no-no. What I suspect is that you've cut-and-pasted an entire C program into the middle of your existing main.
Start with:
#import <Foundation/Foundation.h>
#include <stdio.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [NSAutoreleasePool alloc] init];
int amount = 1000000;
printf("The amount in your account is $%i\n", amount);
NSLog(#"Hello, World!");
[pool drain];
return 0;
}
and work your way up from there.