Skip to content

Commit

Permalink
add impl code
Browse files Browse the repository at this point in the history
  • Loading branch information
leibeiyi committed Dec 20, 2021
1 parent 15ab00c commit 35c033a
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

.vscode/
build/
4 changes: 4 additions & 0 deletions C.interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <IOKit/pwr_mgt/IOPMLib.h>

bool disableScreenSleep(const char *reason, IOPMAssertionID *noSleepAssertionID);
bool enableScreenSleep(IOPMAssertionID noSleepAssertionID);
19 changes: 19 additions & 0 deletions CCAwake.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#import <Foundation/Foundation.h>
#import <IOKit/pwr_mgt/IOPMLib.h>

bool disableScreenSleep(const char *reason, IOPMAssertionID *noSleepAssertionID) {
CFStringRef reasonStr =
CFStringCreateWithCString(NULL, reason, kCFStringEncodingUTF8);
int noSleepReturn = IOPMAssertionCreateWithName(
kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, reasonStr,
noSleepAssertionID);
bool noSleepEnable = noSleepReturn == kIOReturnSuccess;
return noSleepEnable;
}

bool enableScreenSleep(IOPMAssertionID noSleepAssertionID) {
int noSleepReturn = IOPMAssertionRelease(noSleepAssertionID);
bool noSleepEnable = noSleepReturn == kIOReturnSuccess;
return noSleepEnable;
}
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
# awake_mac
# Make your mac awake

Attention! Only work for macos!!!

## example

```
import {disableScreenSleep, enableScreenSleep} from 'awake_mac'
let ok = disableScreenSleep("marklei");
console.log("disableScreenSleep", ok);
if (ok) {
setTimeout(() => {
console.log("enableScreenSleep", enableScreenSleep());
}, 1000 * 180);
} else {
console.warn("disableScreenSleep failed");
}
```

64 changes: 64 additions & 0 deletions awake.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

#include <napi.h>
#include "C.interface.h"
#include <stdlib.h>

// uint32_t *lockId = nullptr;

Napi::Value DisableScreenSleep(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 1) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}

if (!info[0].IsString()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
Napi::String reason = info[0].As<Napi::String>();
uint32_t id = 0;
bool ok = disableScreenSleep(reason.Utf8Value().c_str(), &id);
if (ok) {
return Napi::Number::New(env, id);
} else {
return env.Null();
}
}

Napi::Value EnableScreenSleep(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 1) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return Napi::Boolean::New(env, false);
}

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return Napi::Boolean::New(env, false);
}
uint32_t id = info[0].ToNumber().Uint32Value();
bool ok = enableScreenSleep(id);
return Napi::Boolean::New(env, ok);
}

// void CleanupHook (void* arg) {
// printf("cleanup(%d)\n", *static_cast<uint32_t*>(arg));
// free(arg);
// }

Napi::Object Init(Napi::Env env, Napi::Object exports) {
// lockId = (uint32_t*)malloc(sizeof(uint32_t));
exports.Set(Napi::String::New(env, "disableScreenSleep"),
Napi::Function::New(env, DisableScreenSleep));
exports.Set(Napi::String::New(env, "enableScreenSleep"),
Napi::Function::New(env, EnableScreenSleep));
// napi_add_env_cleanup_hook(env, CleanupHook, lockId);
return exports;
}

NODE_API_MODULE(awake, Init)
17 changes: 17 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"targets": [
{
"target_name": "awake",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [ "awake.cc", "CCAwake.mm" ],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
"link_settings": {
"libraries": ["/System/Library/Frameworks/Foundation.framework", "/System/Library/Frameworks/IOKit.framework"]
}
}
]
}
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export function disableScreenSleep(reason: string): boolean;
export function enableScreenSleep(): boolean;
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const awake = require('bindings')('awake');
let lockId = null;
const disableScreenSleep = (reason) => {
if (lockId == null) {
lockId = awake.disableScreenSleep(reason)
return lockId != null
} else {
return true
}
}

const enableScreenSleep = () => {
if (lockId == null) {
return true
} else {
let ok = awake.enableScreenSleep(lockId)
lockId = null
return ok
}
}
function cleanup(callback) {

// attach user callback to the process event emitter
// if no callback, it will still exit gracefully on Ctrl-C
callback = callback || noOp;
process.on('cleanup', callback);

// do app specific cleaning before exiting
process.on('exit', function () {
process.emit('cleanup');
});

// catch ctrl+c event and exit normally
process.on('SIGINT', function () {
process.exit(2);
});

//catch uncaught exceptions, trace, then exit normally
process.on('uncaughtException', function (e) {
process.exit(99);
});
};
cleanup(() => {
enableScreenSleep()
})

module.exports = {
disableScreenSleep,
enableScreenSleep
}



Loading

0 comments on commit 35c033a

Please sign in to comment.