1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-06 00:13:33 +00:00
Files
MeshAgent/modules/win-systray.js

225 lines
8.6 KiB
JavaScript

/*
Copyright 2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var promise = require('promise');
var TH32CS_SNAPTHREAD = 0x00000004;
var WM_QUIT = 0x0012;
var WM_CLOSE = 0x0010;
function postMessage(tid)
{
var gm = require('_GenericMarshal');
var user = gm.CreateNativeProxy('User32.dll');
user.CreateMethod('PostThreadMessageA');
user.PostThreadMessageA(tid, WM_QUIT);
}
function localsystem_createTrayIcon(trayOptions)
{
var retVal = new promise(function (res, rej) { this._res = res; this._rej = rej; });
if (trayOptions.user == null)
{
trayOptions.user = require('user-sessions').getUsername(require('user-sessions').consoleUid());
}
trayOptions.split = true;
retVal.trayOptions = trayOptions;
retVal._dispatcher = require('win-dispatcher').dispatch({ user: trayOptions.user, modules: [], launch: { module: 'win-systray', method: 'createTrayIcon', args: [trayOptions], split: true } });
retVal._dispatcher.ret = retVal;
retVal._dispatcher.on('connection', function (connection)
{
this.ret.connection = connection;
this.ret.connection.ret = this.ret;
this.ret.connection.on('data', function (c)
{
var val = c.toString();
if (val.includes('<<TID:'))
{
var t = val.split('<<TID:').pop().split('>>').shift();
this.ret.tid = parseInt(t);
}
else if (val.includes('<<menuitem:'))
{
var i = parseInt(val.split('<<menuitem:').pop().split('>>').shift());
if (this.ret.trayOptions.menuItems[i].func != null)
{
this.ret.trayOptions.menuItems[i].func.call(this.ret.trayOptions);
}
}
});
this.ret._cleanup = function _cleanup()
{
_cleanup.ret._dispatcher.invoke('remove', [_cleanup.ret.tid]);
_cleanup.ret.connection.end();
};
this.ret._cleanup.ret = this.ret;
process.on('exit', this.ret._cleanup);
});
retVal.remove = function ()
{
if (this._cleanup != null)
{
this._cleanup();
process.removeListener('exit', this._cleanup);
}
};
return (retVal);
}
function createTrayIcon(trayOptions)
{
var i;
var retVal = new promise(function (res, rej) { this._res = res; this._rej = rej; });
var cid;
var options = { env: { _title: trayOptions.title } };
retVal.options = options;
retVal.trayOptions = trayOptions;
for (i in trayOptions.menuItems)
{
options.env['_menu' + i + '_text'] = trayOptions.menuItems[i].text;
}
options.env['_tidsig'] = '[DllImport("kernel32.dll")]\r\npublic static extern uint GetCurrentThreadId();';
for (var c1e in process.env)
{
options.env[c1e] = process.env[c1e];
}
if (!trayOptions.split)
{
try
{
options.uid = trayOptions.tsid == null ? require('user-sessions').consoleUid() : trayOptions.tsid;
if (options.uid == (cid = require('user-sessions').getProcessOwnerName(process.pid).tsid))
{
delete options.uid;
}
else
{
if (trayOptions.tsid != null && cid != 0)
{
retVal._rej('Insufficient permission to set tray icon as uid: ' + trayOptions.tsid);
return (retVal);
}
retVal.options.type = require('child_process').SpawnTypes.USER;
}
}
catch (ee)
{
retVal._rej('Cannot set tray icon when a user is not logged in');
return (retVal);
}
}
retVal.child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], retVal.options);
retVal.child.ret = retVal;
retVal.child.on('exit', function () { this.ret._res(); });
retVal.child.descriptorMetadata = 'win-systray';
if (!trayOptions.split)
{
retVal.child.stdout.on('data', function (c)
{
var val = c.toString();
if (val.includes('<<TID:'))
{
var t = val.split('<<TID:').pop().split('>>').shift();
this.parent.ret.tid = parseInt(t);
}
else if (val.includes('<<menuitem:'))
{
var i = parseInt(val.split('<<menuitem:').pop().split('>>').shift());
if (this.parent.ret.trayOptions.menuItems[i].func != null)
{
this.parent.ret.trayOptions.menuItems[i].func.call(this.parent.ret.trayOptions);
}
}
});
}
else
{
retVal.out = retVal.child.stdout;
retVal.in = retVal.child.stdin;
}
retVal.child.stderr.on('data', function (c) { });
retVal.child.stdin.write('$signature_gctid = $env:_tidsig\r\n');
retVal.child.stdin.write('Add-Type -MemberDefinition $signature_gctid -Name MyName -Namespace MyNameSpace -PassThru\r\n');
retVal.child.stdin.write('$tid = [MyNameSpace.MyName]::GetCurrentThreadId();\r\n');
retVal.child.stdin.write('Write-Host "<<TID:$tid>>"\r\n');
retVal.child.stdin.write('[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")\r\n');
retVal.child.stdin.write('[System.Reflection.Assembly]::LoadWithPartialName("presentationframework")\r\n');
retVal.child.stdin.write('[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")\r\n');
retVal.child.stdin.write('[System.Reflection.Assembly]::LoadWithPartialName("WindowsFormsIntegration")\r\n');
retVal.child.stdin.write('$icon = [System.Drawing.Icon]::ExtractAssociatedIcon("' + process.execPath + '")\r\n');
retVal.child.stdin.write('$Main_Tool_Icon = New-Object System.Windows.Forms.NotifyIcon\r\n');
retVal.child.stdin.write('$Main_Tool_Icon.Text = $env:_title\r\n');
retVal.child.stdin.write('$Main_Tool_Icon.Icon = $icon\r\n');
retVal.child.stdin.write('$Main_Tool_Icon.Visible = $true\r\n');
for(i in trayOptions.menuItems)
{
retVal.child.stdin.write('$menuitem_' + i + ' = New-Object System.Windows.Forms.MenuItem\r\n');
retVal.child.stdin.write('$menuitem_' + i + '.Text = $env:_menu' + i + '_text\r\n');
retVal.child.stdin.write('$menuitem_' + i + '.Add_Click({ Write-Host "<<menuitem:' + i + '>>" })\r\n');
}
retVal.child.stdin.write('$contextmenu = New-Object System.Windows.Forms.ContextMenu\r\n');
for (i in trayOptions.menuItems)
{
retVal.child.stdin.write('$contextmenu.MenuItems.Add($menuitem_' + i + ')\r\n');
}
retVal.child.stdin.write('$Main_Tool_Icon.ContextMenu = $contextmenu\r\n');
retVal.child.stdin.write('$Main_Tool_Icon.add_MouseDown({ $Main_Tool_Icon.ContextMenu = $contextmenu; $Main_Tool_Icon.GetType().GetMethod("ShowContextMenu",[System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).Invoke($Main_Tool_Icon,$null) });\r\n');
retVal.child.stdin.write('$appContext = New-Object System.Windows.Forms.ApplicationContext\r\n');
retVal.child.stdin.write('[void][System.Windows.Forms.Application]::Run($appContext)\r\n');
retVal.child.stdin.write('$Main_Tool_Icon.dispose();\r\n');
retVal.child.stdin.write('exit\r\n');
retVal._cleanup = function _cleanup(tid)
{
if (process.exitting)
{
postMessage(_cleanup.self.tid);
}
else
{
postMessage(tid ? tid : _cleanup.self.tid);
}
};
retVal._cleanup.self = retVal;
process.on('exit', retVal._cleanup);
retVal.remove = function (tid)
{
this._cleanup(tid);
process.removeListener('exit', this._cleanup);
};
return (retVal);
}
if (require('user-sessions').getProcessOwnerName(process.pid).tsid == 0)
{
module.exports = { createTrayIcon: process.platform == 'win32' ? localsystem_createTrayIcon : function () { throw (process.platform + ' not supported') } };
}
else
{
module.exports = { createTrayIcon: process.platform == 'win32' ? createTrayIcon : function () { throw (process.platform + ' not supported') } };
}