1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-15 07:43:50 +00:00

Added power-monitor.wakeDisplay() and sleepDisplay()

This commit is contained in:
Bryan Roe
2020-06-10 13:49:24 -07:00
parent f1a85296c5
commit 5aff4c172e
4 changed files with 138 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@@ -956,9 +956,20 @@ BOOL ILibDuktape_server_ipc_ReadSink(void *chain, HANDLE h, ILibWaitHandle_Error
else else
{ {
// I/O Errors // I/O Errors
if (winIPC->mServer != NULL) { winIPC->clientConnected = 0; } if (winIPC->mServer != NULL) { winIPC->clientConnected = 0; }
if (winIPC->reservedState != NULL) { ILibChain_WaitHandle_DestroySavedState(chain, winIPC->reservedState); winIPC->reservedState = NULL; } if (winIPC->reservedState != NULL) { ILibChain_WaitHandle_DestroySavedState(chain, winIPC->reservedState); winIPC->reservedState = NULL; }
ILibDuktape_DuplexStream_Closed(winIPC->ds); ILibDuktape_DuplexStream_Closed(winIPC->ds);
duk_push_heapptr(winIPC->ctx, winIPC->mServer); // [server]
if (duk_has_prop_string(winIPC->ctx, -1, ILibDuktape_net_server_closed_needEmit))
{
ILibDuktape_EventEmitter_SetupEmit(winIPC->ctx, winIPC->mServer, "close"); // [server][emit][this][close]
if (duk_pcall_method(winIPC->ctx, 1) != 0) { ILibDuktape_Process_UncaughtExceptionEx(winIPC->ctx, "net.ipcServer.onClose() Error: "); }
duk_pop(winIPC->ctx); // [server]
}
duk_pop(winIPC->ctx); // ...
return(FALSE); return(FALSE);
} }
} }

View File

@@ -20,7 +20,7 @@ function childContainer()
this._ObjectID = 'child-container'; this._ObjectID = 'child-container';
this.create = function create(options) this.create = function create(options)
{ {
if (!options || !options.launch || !options.launch.module || !options.launch.method || !options.launch.args) { throw ('Invalid Parameters'); } if (!options || (!options.user && !options.uid) || !options.launch || !options.launch.module || !options.launch.method || !options.launch.args) { throw ('Invalid Parameters'); }
var ipcInteger; var ipcInteger;
@@ -60,7 +60,7 @@ function childContainer()
} }
}); });
ret._ipc = require('net').createServer(); ret._ipc.parent = ret; ret._ipc = require('net').createServer(); ret._ipc.parent = ret;
ret._ipc.on('close', function () { console.log('Child Container Process Closed'); }); ret._ipc.on('close', function () { this.parent.emit('exit'); });
while (true) while (true)
{ {
@@ -89,6 +89,7 @@ function childContainer()
var script = Buffer.from("console.log('CHILD/START');require('child-container').connect('" + ipcInteger + "');").toString('base64'); var script = Buffer.from("console.log('CHILD/START');require('child-container').connect('" + ipcInteger + "');").toString('base64');
ret._ipc.once('connection', function onConnect(s) ret._ipc.once('connection', function onConnect(s)
{ {
this.close();
s.descriptorMetadata = 'child-container'; s.descriptorMetadata = 'child-container';
this.parent._client = s; this.parent._client = s;
this.parent._client._parent = this; this.parent._client._parent = this;
@@ -179,7 +180,27 @@ function childContainer()
this.connect = function (ipcNumber) this.connect = function (ipcNumber)
{ {
var ipcPath = '\\\\.\\pipe\\taskRedirection-' + ipcNumber; var ipcPath = '\\\\.\\pipe\\taskRedirection-' + ipcNumber;
this._ipcClient = require('net').createConnection({ path: ipcPath }, function () this._ipcClient = require('net').createConnection({ path: ipcPath });
this._ipcClient._parent = this;
require('events').EventEmitter.call(this, true)
.createEvent('message')
.createEvent('exit')
.addMethod('message', function (msg)
{
this.send({ command: 'message', value: msg });
})
.addMethod('send', function (data)
{
if (!this._ipcClient) { throw ('Not Connected'); }
var d, h = Buffer.alloc(4);
d = Buffer.from(JSON.stringify(data));
h.writeUInt32LE(d.length + 4);
this._ipcClient.write(h);
this._ipcClient.write(d);
});
Object.defineProperty(this, 'child', { value: true });
this._ipcClient.on('connect', function ()
{ {
this.on('close', function () { process._exit(0); }); this.on('close', function () { process._exit(0); });
this.on('data', function (c) this.on('data', function (c)
@@ -219,25 +240,6 @@ function childContainer()
if (cLen < c.length) { this.unshift(c.slice(cLen)); } if (cLen < c.length) { this.unshift(c.slice(cLen)); }
}); });
}); });
this._ipcClient._parent = this;
require('events').EventEmitter.call(this, true)
.createEvent('message')
.createEvent('exit')
.addMethod('message', function (msg)
{
this.send({ command: 'message', value: msg });
})
.addMethod('send', function (data)
{
if (!this._ipcClient) { throw ('Not Connected'); }
var d, h = Buffer.alloc(4);
d = Buffer.from(JSON.stringify(data));
h.writeUInt32LE(d.length + 4);
this._ipcClient.write(h);
this._ipcClient.write(d);
});
}; };
} }

View File

@@ -14,6 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
var WM_SYSCOMMAND = 0x0112;
var SC_MONITORPOWER = 0xF170;
var HWND_BROADCAST = 0xffff;
var ES_DISPLAY_REQUIRED = 0x00000002;
function powerMonitor() function powerMonitor()
{ {
this._ObjectID = 'power-monitor'; this._ObjectID = 'power-monitor';
@@ -115,6 +120,16 @@ function powerMonitor()
} }
if (process.platform == 'darwin') if (process.platform == 'darwin')
{ {
Object.defineProperty(this, "_caffeinate", {
value: (function ()
{
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
child.stdin.write('whereis caffeinate\nexit\n');
child.waitExit();
return (child.stdout.str.trim());
})()
});
this._getBatteryLevel = function _getBatteryLevel() this._getBatteryLevel = function _getBatteryLevel()
{ {
var child = require('child_process').execFile('/bin/sh', ['sh']); var child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -165,6 +180,92 @@ function powerMonitor()
}, 300000, this); }, 300000, this);
} }
} }
this.sleepDisplay = function sleepDispay(force)
{
var promise = require('promise');
p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
if (process.platform != 'win32') { p._rej('Not supported'); return (p); }
if (require('user-sessions').getProcessOwnerName(process.pid).tsid == 0)
{
// We are running as LocalSystem, so we have to find a user session for this to work
var options = { launch: { module: 'power-monitor', method: 'sleepDisplay', args: [] } };
try
{
options.user = require('user-sessions').getUsername(require('user-sessions').consoleUid());
}
catch(ee)
{
p._rej('No users logged in');
return (p);
}
p.child = require('child-container').create(options);
p.child.promise = p;
p.child.on('exit', function () { this.promise._res(); });
}
else
{
if (require('child-container').child) { require('win-console').hide(); }
var GM = require('_GenericMarshal');
var user32 = GM.CreateNativeProxy('User32.dll');
user32.CreateMethod('SendMessageA');
user32.SendMessageA(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
p._res();
if (require('child-container').child) { process._exit(); }
}
return (p);
};
this.wakeDisplay = function wakeDisplay()
{
var promise = require('promise');
p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
switch(process.platform)
{
case 'darwin':
if (this._caffeinate)
{
p.child = require('child_process').execFile(this._caffeinate, ['caffeinate', '-u', '-t 2']);
p.child.stdout.on('data', function () { });
p.child.stderr.on('data', function () { });
p.child.on('exit', function (code) { this.promise._res(); });
p.child.promise = p;
}
break;
case 'win32':
if (require('user-sessions').getProcessOwnerName(process.pid).tsid == 0)
{
// We are running as LocalSystem, so we have to find a user session for this to work
var options = { launch: { module: 'power-monitor', method: 'wakeDisplay', args: [] } };
try
{
options.user = require('user-sessions').getUsername(require('user-sessions').consoleUid());
}
catch (ee)
{
p._rej('No users logged in');
return (p);
}
p.child = require('child-container').create(options);
p.child.promise = p;
p.child.on('exit', function () { this.promise._res(); });
}
else
{
if (require('child-container').child) { require('win-console').hide(); }
var GM = require('_GenericMarshal');
var kernel32 = GM.CreateNativeProxy('Kernel32.dll');
kernel32.CreateMethod('SetThreadExecutionState');
kernel32.SetThreadExecutionState(ES_DISPLAY_REQUIRED);
p._res();
if (require('child-container').child) { process._exit(); }
}
break;
default:
p._res();
break;
}
return (p);
};
} }
module.exports = new powerMonitor(); module.exports = new powerMonitor();