-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
leibeiyi
committed
Dec 20, 2021
1 parent
15ab00c
commit 35c033a
Showing
11 changed files
with
421 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,6 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
.vscode/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
export function disableScreenSleep(reason: string): boolean; | ||
export function enableScreenSleep(): boolean; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
||
|
Oops, something went wrong.