Keeping an application alive in C++ on Apple Mac OS-X

  • Prevents an ‘App Nap’, whatever the hell that entails.
  • Prevents the system from going automagically to some power-saving mode or idle, (display) sleep or suspend.
  • ! Find the most recent version on GitHub here: https://github.com/TimZaman/toolkit/blob/master/keepalive.h https://github.com/TimZaman/toolkit/blob/master/keepalive.mm

    keepalive.h

    /**
    --------------------------------------------------------------------------------
    -   Module      :   keepalive.h
    -   Description :   A wrapper to keep an app alive, written
    -                   in C++ and Objective-C so made for the OSX platform.
    -   Author      :   Tim Zaman, 18-FEB-2016
    --------------------------------------------------------------------------------
    */
    /*
    Copyright (c) 2016 Tim Zaman
    Permission to use, copy, modify, distribute, and sell this software
    for any purpose is hereby granted without fee, provided
    that (i) the above copyright notices and this permission notice appear in
    all copies of the software and related documentation.
    THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
    EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
    WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
    */
    #ifndef KEEPALIVE_H
    #define KEEPALIVE_H
    class KeepAlive {
     public:
        KeepAlive(){
            KeepAliveMM();
        }
        ~KeepAlive(){
            KeepAliveDestructorMM();
        }
    #ifdef __APPLE__
        void KeepAliveMM();
        void KeepAliveDestructorMM();
    #else
        // Keep other platforms alive
    #endif
    };
    #endif

    keepalive.mm

    /**
    --------------------------------------------------------------------------------
    -   Module      :   keepalive.mm
    -   Description :   A wrapper to keep an app alive, written
    -                   in C++ and Objective-C so made for the OSX platform.
    -   Author      :   Tim Zaman, 18-FEB-2016
    --------------------------------------------------------------------------------
    */
    #include "keepalive.h"
    #import <iokit/pwr_mgt/IOPMLib.h>
    // kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
    // kIOPMAssertionTypeNoIdleSleep prevents idle sleep
    //reasonForActivity is a descriptive string used by the system whenever it needs
    //  to tell the user why the system is not sleeping. For example,
    //  "Mail Compacting Mailboxes" would be a useful string.
    IOPMAssertionID assertionID;
    void KeepAlive::KeepAliveMM(){
        // NOTE: IOPMAssertionCreateWithName limits the string to 128 characters.
        CFStringRef reasonForActivity = CFSTR("Program Running.");
        IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
                                    kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
        if (success == kIOReturnSuccess){
            // @TODO?
        } else {
            // @TODO?
        }
    }
    void KeepAlive::KeepAliveDestructorMM(){
        IOReturn success = IOPMAssertionRelease(assertionID);
    }
    In your CMakelists.txt just obviously add something along the lines of
    add_executable( appname (...) <strong>keepalive.mm</strong>)
    And then just call the creator in your main and make sure to keep it allocated. Above code is free domain, or beerware, whatever.]]>

    You may also like...