Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Apple's GameController framework from a macOS Command Line Tool?

I'm trying to get the following code to work as a macOS command line tool. It is important that this not be a Cocoa app, so that is not an option.

This same code works perfectly in the same project with a Cocoa App target and detects a compatible controller, but when run as a Command Line Tool target, nothing happens and the API shows no controllers connected.

Obviously, some of it is contrived... it's just the simplest I could boil it down to and have some indication of things happening when it actually works.

#import <Cocoa/Cocoa.h>
#import <GameController/GameController.h>


int main( int argc, const char * argv[] )
{
    @autoreleasepool
    {
        NSApplication * application = [NSApplication sharedApplication];

        NSNotificationCenter * center = [NSNotificationCenter defaultCenter];

        [center addObserverForName: GCControllerDidConnectNotification
                            object: nil
                             queue: nil
                        usingBlock: ^(NSNotification * note) {
                            GCController * controller = note.object;
                            printf( "ATTACHED: %s\n", controller.vendorName.UTF8String );
                        }
         ];

        [application finishLaunching];

        bool shouldKeepRunning = true;
        while (shouldKeepRunning)
        {
            printf( "." );

            while (true)
            {
                NSEvent * event = [application
                                   nextEventMatchingMask: NSEventMaskAny
                                   untilDate: nil
                                   inMode: NSDefaultRunLoopMode
                                   dequeue: YES];
                if (event == NULL)
                {
                    break;
                }
                else
                {
                    [application sendEvent: event];
                }
            }

            usleep( 100 * 1000 );
        }
    }

    return 0;
}

I'm guessing it's got something to do with how the Cocoa application sets up or the event loops are handled. Or maybe there's some internal trigger that initializes the GameController framework. The API doesn't appear to have any explicit way to initialize it.

https://developer.apple.com/documentation/gamecontroller?language=objc

Can anyone shed some light on how I might get this working?

Ultimately, this code really needs to work inside a Core Foundation bundle, so if it could actually work with a Core Foundation runloop that would be ideal.

-- EDIT --

I have made a test project to illustrate the problem more clearly. There are two build targets. The Cocoa app build target works and receives the controller connected event. The other build target, just a simple CLI app, does not work. They both use the same source file. It also includes two code paths, one of which is the traditional [NSApp run], the second is the manual event loop above. The result is the same.

https://www.dropbox.com/s/a6fw3nuegq7bg8x/ControllerTest.zip?dl=0

like image 672
Patrick Hogan Avatar asked Mar 18 '19 16:03

Patrick Hogan


1 Answers

Although every thread creates a run loop (NSRunLoop for a Cocoa app) to process input events, the loop doesn't start automatically. The code below makes it run with the [application run] call. When the proper event is processed by the run loop, the notification is raised. I install the observer in an Application delegate just to make sure all other systems have finished initializing at that point.

#import <Cocoa/Cocoa.h>
#import <GameController/GameController.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> @end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserverForName: GCControllerDidConnectNotification
                        object: nil
                         queue: nil
                    usingBlock: ^(NSNotification * note) {
                        GCController * controller = note.object;
                        printf( "ATTACHED: %s\n", controller.vendorName.UTF8String );
                    }
     ];
}

@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSApplication * application = [NSApplication sharedApplication]; // You can get rid of the variable and just use the global NSApp below instead
        AppDelegate *delegate = [[AppDelegate alloc] init];
        [application setDelegate:delegate];
        [application run];
    }
    return 0;
}

UPDATE Sorry, I misinterpreted the question. The code above works for connecting and disconnecting controllers, but it does not properly initialize the [GCController controllers] array with devices that were already connected when the application starts.

As you point out, connected devices send notifications with the same code on a Cocoa app, but not on a command line one. The difference is that Cocoa apps get didBecomeActive notifications, and that causes the private _GCControllerManager (the object that takes care of NSXPCConnections posted by the GameControllerDaemon) to receive a CBApplicationDidBecomeActive message that populates the controllers array.

Anyway, I tried making the command line app active so it routes these messages, but that didn't work; the app needs to send the didBecomeActive message early during startup.

Then I tried creating my own _GCGameController and send the CBApplicationDidBecomeActive manually; that kind of worked, except the app ends up with 2 of these controllers, and connections get duplicated.

What I needed was access to the private _GCGameController object, but I don't know who owns it, so I could not reference it directly.

So at the end, I went with method swizzling. The code below changes the last method that gets called at initialization in a terminal app, _GCGameController startIdleWatchTimer, so it sends CBApplicationDidBecomeActive afterwards.

I know is not a great solution, using all kinds of Apple's internal code, but maybe it helps somebody get to something better. Add the following code to the previous one:

#import <objc/runtime.h>


@interface _GCControllerManager : NSObject
-(void) CBApplicationDidBecomeActive;
-(void) startIdleWatchTimer;
@end

@implementation _GCControllerManager (Extras)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        SEL originalSelector = @selector(startIdleWatchTimer);
        SEL swizzledSelector = @selector(myStartIdleWatchTimer);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL didAddMethod =
            class_addMethod(class,
                originalSelector,
                method_getImplementation(swizzledMethod),
                method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                swizzledSelector,
                method_getImplementation(originalMethod),
                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

- (void) myStartIdleWatchTimer {
    [self myStartIdleWatchTimer];
    [self CBApplicationDidBecomeActive];
}

@end
like image 82
Miguel Friginal Avatar answered Sep 21 '22 17:09

Miguel Friginal