Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <mach/mach_port.h>
- #include <mach/mach_interface.h>
- #include <mach/mach_init.h>
- #include <IOKit/pwr_mgt/IOPMLib.h>
- #include <IOKit/IOMessage.h>
- #include <jni.h>
- struct MySleepContext {
- io_connect_t root_port; // the Root Power Domain IOService;
- jobject callback; // the Java callback
- JNIEnv * env; // the Java environment
- };
- void MySleepCallBack(void * pointer, io_service_t service, natural_t messageType, void * messageArgument) {
- struct MySleepContext * context = pointer;
- jclass callbackClass = (*context->env)->GetObjectClass(context->env, context->callback);
- jmethodID mid = (*context->env)->GetMethodID(context->env, callbackClass, "allowIdleSleep", "()Z");
- jboolean allow = (*context->env)->CallBooleanMethod(context->env, context->callback, mid);
- switch (messageType) {
- case kIOMessageCanSystemSleep:
- if (allow == JNI_TRUE) {
- IOAllowPowerChange(context->root_port, (long) messageArgument);
- } else {
- IOCancelPowerChange(context->root_port, (long) messageArgument);
- }
- break;
- case kIOMessageSystemWillSleep:
- IOAllowPowerChange(context->root_port, (long) messageArgument);
- break;
- default:
- break;
- }
- }
- JNIEXPORT void JNICALL KeepAwakeNative_keepAwakeNative (JNIEnv * env, jobject obj, jobject callback) {
- IONotificationPortRef notifyPortRef;
- io_object_t notifierObject;
- struct MySleepContext * context;
- context->callback = callback;
- context->env = env;
- context->root_port = IORegisterForSystemPower(context, ¬ifyPortRef, MySleepCallBack, ¬ifierObject);
- CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes);
- // blocks
- CFRunLoopRun();
- }
Add Comment
Please, Sign In to add comment