Skip to content

Commit

Permalink
Added macOS-only api calls
Browse files Browse the repository at this point in the history
At the current state, this objective-c++ file is used only to enable-disable AppNap.
In the future it can be used for more macOS-only related activities

Co-authored-by: Claudio Fantacci <claudio.fantacci@iit.it>
  • Loading branch information
francesco-romano and claudiofantacci committed Mar 30, 2017
1 parent 4d17762 commit 4edc999
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/libYARP_OS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ set(YARP_OS_SRCS src/AbstractCarrier.cpp
src/hmac_sha2.c
src/sha2.c
src/QosStyle.cpp)


if(APPLE)
list(APPEND YARP_OS_IMPL_HDRS include/yarp/os/impl/MacOSAPI.h)
list(APPEND YARP_OS_SRCS src/MacOSAPI.mm)
endif()

if(NOT YARP_NO_DEPRECATED)
set(YARP_OS_DEPRECATED_SRCS src/Module.cpp) # since YARP 2.3.65
Expand Down Expand Up @@ -404,6 +410,10 @@ add_library(YARP::YARP_OS ALIAS YARP_OS)

target_link_libraries(YARP_OS PRIVATE ${ACE_LIBRARIES})

if(APPLE)
target_link_libraries(YARP_OS PRIVATE "-framework Foundation")
endif()

set_property(TARGET YARP_OS PROPERTY PUBLIC_HEADER ${YARP_OS_HDRS})
set_property(TARGET YARP_OS PROPERTY PRIVATE_HEADER ${YARP_OS_IMPL_HDRS})

Expand Down
2 changes: 2 additions & 0 deletions src/libYARP_OS/include/yarp/os/SystemInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class YARP_OS_API yarp::os::SystemInfo
static ProcessInfo getProcessInfo(int pid);

//static NetworkInfo getNetworkInfo();

static void toggleEnergySavingMode(bool enable);
};

#endif // YARP_OS_SYSTEMINFO_H
Expand Down
8 changes: 8 additions & 0 deletions src/libYARP_OS/include/yarp/os/impl/MacOSAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef YARP_OS_MACOSAPI_H
#define YARP_OS_MACOSAPI_H

void* enableAppNap();
void disableAppNap(void *activityInfo);


#endif /* end of include guard: YARP_OS_MACOSAPI_H */
19 changes: 19 additions & 0 deletions src/libYARP_OS/src/MacOSAPI.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#import "yarp/os/impl/MacOSAPI.h"

#import <Foundation/Foundation.h>

void* enableAppNap()
{
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
id activity = [processInfo beginActivityWithOptions:NSActivityLatencyCritical | NSActivityUserInitiated
reason:@"YARP requires AppNap off"];
return activity;
}

void disableAppNap(void *activityInfo)
{
id activity = (id)activityInfo;
if (!activity) return;
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
[processInfo endActivity:activity];
}
19 changes: 19 additions & 0 deletions src/libYARP_OS/src/SystemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,22 @@ SystemInfo::ProcessInfo SystemInfo::getProcessInfo(int pid) {
#endif
return info;
}

#ifdef __APPLE__
#include "yarp/os/impl/MacOSAPI.h"
#endif


void SystemInfo::toggleEnergySavingMode(bool enable)
{
#ifdef __APPLE__
static void* handle = 0;
if (!enable && !handle) {
handle = enableAppNap();
} else {
disableAppNap(handle);
}

#endif
}

0 comments on commit 4edc999

Please sign in to comment.