2017-01-08 15:17:07 +00:00
/ *
* MIT License
*
* Copyright ( c ) 2017 Michael VanOverbeek and ShiftOS devs
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the "Software" ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE .
* /
using System ;
2017-01-08 14:57:10 +00:00
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using ShiftOS.Engine ;
using System.IO ;
using System.Diagnostics ;
using System.Runtime.InteropServices ;
using System.Threading ;
2017-01-21 21:23:10 +00:00
using Newtonsoft.Json ;
2017-01-08 14:57:10 +00:00
2017-01-14 22:00:16 +00:00
/// <summary>
/// Coherence commands.
/// </summary>
2017-01-08 14:57:10 +00:00
namespace ShiftOS.WinForms
{
2017-01-21 21:23:10 +00:00
[Namespace("trm")]
public static class TerminalExtensions
{
[Command("setpass", true)]
[RequiresArgument("pass")]
public static bool setPass ( Dictionary < string , object > args )
{
SaveSystem . CurrentSave . Password = args [ "pass" ] as string ;
return true ;
}
2017-01-21 21:30:07 +00:00
[Command("remote", "username:,sysname:,password:", "Allows you to control a remote system on the multi-user domain given a username, password and system name.")]
2017-01-21 21:23:10 +00:00
[RequiresArgument("username")]
[RequiresArgument("sysname")]
2017-01-21 21:30:07 +00:00
[RequiresArgument("password")]
2017-01-21 21:23:10 +00:00
public static bool RemoteControl ( Dictionary < string , object > args )
{
ServerManager . SendMessage ( "trm_handshake_request" , JsonConvert . SerializeObject ( args ) ) ;
return true ;
}
}
2017-01-08 14:57:10 +00:00
[Namespace("coherence")]
[RequiresUpgrade("kernel_coherence")]
public static class CoherenceCommands
{
2017-01-14 22:00:16 +00:00
/// <summary>
/// Sets the window position.
/// </summary>
/// <returns>The window position.</returns>
/// <param name="hWnd">H window.</param>
/// <param name="hWndInsertAfter">H window insert after.</param>
/// <param name="X">X.</param>
/// <param name="Y">Y.</param>
/// <param name="cx">Cx.</param>
/// <param name="cy">Cy.</param>
/// <param name="uFlags">U flags.</param>
2017-01-08 14:57:10 +00:00
[DllImport("user32.dll")]
static extern bool SetWindowPos ( IntPtr hWnd , IntPtr hWndInsertAfter , int X , int Y , int cx , int cy , uint uFlags ) ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The HWN d TOPMOS.
/// </summary>
2017-01-08 14:57:10 +00:00
static readonly IntPtr HWND_TOPMOST = new IntPtr ( - 1 ) ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The SW p SHOWWINDO.
/// </summary>
2017-01-08 14:57:10 +00:00
const UInt32 SWP_SHOWWINDOW = 0x0040 ;
[DllImport("user32.dll")]
2017-01-14 22:00:16 +00:00
/// <summary>
/// Gets the window rect.
/// </summary>
/// <returns>The window rect.</returns>
/// <param name="hWnd">H window.</param>
/// <param name="lpRect">Lp rect.</param>
2017-01-08 14:57:10 +00:00
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect ( IntPtr hWnd , ref RECT lpRect ) ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// REC.
/// </summary>
2017-01-08 14:57:10 +00:00
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left ; // x position of upper-left corner
public int Top ; // y position of upper-left corner
public int Right ; // x position of lower-right corner
public int Bottom ; // y position of lower-right corner
}
[Command("launch", "process: \"C:\\path\\to\\process\" - The process path to launch.", "Launch a process inside kernel coherence.")]
[RequiresArgument("process")]
2017-01-14 22:00:16 +00:00
/// <summary>
/// Launchs the app.
/// </summary>
/// <returns>The app.</returns>
/// <param name="args">Arguments.</param>
2017-01-08 14:57:10 +00:00
public static bool LaunchApp ( Dictionary < string , object > args )
{
string process = args [ "process" ] . ToString ( ) ;
var prc = Process . Start ( process ) ;
StartCoherence ( prc ) ;
return true ;
}
2017-01-14 22:00:16 +00:00
/// <summary>
/// Starts the coherence.
/// </summary>
/// <returns>The coherence.</returns>
/// <param name="prc">Prc.</param>
2017-01-08 14:57:10 +00:00
private static void StartCoherence ( Process prc )
{
RECT rct = new RECT ( ) ;
while ( ! GetWindowRect ( prc . MainWindowHandle , ref rct ) )
{
}
AppearanceManager . Invoke ( new Action ( ( ) = >
{
IShiftOSWindow coherenceWindow = new Applications . CoherenceOverlay ( prc . MainWindowHandle , rct ) ;
AppearanceManager . SetupWindow ( coherenceWindow ) ;
SetWindowPos ( prc . MainWindowHandle , HWND_TOPMOST , 0 , 0 , 0 , 0 , SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW ) ;
//MakeExternalWindowBorderless(prc.MainWindowHandle);
} ) ) ;
}
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s BORDE.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_BORDER = 8388608 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s DLGFRAM.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_DLGFRAME = 4194304 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s CAPTIO.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_CAPTION = WS_BORDER | WS_DLGFRAME ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s SYSMEN.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_SYSMENU = 524288 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s THICKFRAM.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_THICKFRAME = 262144 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s MINIMIZ.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_MINIMIZE = 536870912 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s MAXIMIZEBO.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_MAXIMIZEBOX = 65536 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The GW l STYL.
/// </summary>
2017-01-08 14:57:10 +00:00
const int GWL_STYLE = - 16 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The GW l EXSTYL.
/// </summary>
2017-01-08 14:57:10 +00:00
const int GWL_EXSTYLE = - 20 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The W s E x DLGMODALFRAM.
/// </summary>
2017-01-08 14:57:10 +00:00
const int WS_EX_DLGMODALFRAME = 0x1 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The SW p NOMOV.
/// </summary>
2017-01-08 14:57:10 +00:00
const int SWP_NOMOVE = 0x2 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The SW p NOSIZ.
/// </summary>
2017-01-08 14:57:10 +00:00
const int SWP_NOSIZE = 0x1 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The SW p FRAMECHANGE.
/// </summary>
2017-01-08 14:57:10 +00:00
const int SWP_FRAMECHANGED = 0x20 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The M f BYPOSITIO.
/// </summary>
2017-01-08 14:57:10 +00:00
const uint MF_BYPOSITION = 0x400 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// The M f REMOV.
/// </summary>
2017-01-08 14:57:10 +00:00
const uint MF_REMOVE = 0x1000 ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// Gets the window long.
/// </summary>
/// <returns>The window long.</returns>
/// <param name="hWnd">H window.</param>
/// <param name="nIndex">N index.</param>
2017-01-08 14:57:10 +00:00
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetWindowLong ( IntPtr hWnd , int nIndex ) ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// Sets the window long.
/// </summary>
/// <returns>The window long.</returns>
/// <param name="hWnd">H window.</param>
/// <param name="nIndex">N index.</param>
/// <param name="dwNewLong">Dw new long.</param>
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
2017-01-08 14:57:10 +00:00
public static extern int SetWindowLong ( IntPtr hWnd , int nIndex , int dwNewLong ) ;
2017-01-14 22:00:16 +00:00
/// <summary>
/// Sets the window position.
/// </summary>
/// <returns>The window position.</returns>
/// <param name="hWnd">H window.</param>
/// <param name="hWndInsertAfter">H window insert after.</param>
/// <param name="X">X.</param>
/// <param name="Y">Y.</param>
/// <param name="cx">Cx.</param>
/// <param name="cy">Cy.</param>
/// <param name="uFlags">U flags.</param>
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
2017-01-08 14:57:10 +00:00
public static extern bool SetWindowPos ( IntPtr hWnd , IntPtr hWndInsertAfter , int X , int Y , int cx , int cy , int uFlags ) ;
public static void MakeExternalWindowBorderless ( IntPtr MainWindowHandle )
{
int Style = 0 ;
Style = GetWindowLong ( MainWindowHandle , GWL_STYLE ) ;
Style = Style & ~ WS_CAPTION ;
Style = Style & ~ WS_SYSMENU ;
Style = Style & ~ WS_THICKFRAME ;
Style = Style & ~ WS_MINIMIZE ;
Style = Style & ~ WS_MAXIMIZEBOX ;
SetWindowLong ( MainWindowHandle , GWL_STYLE , Style ) ;
Style = GetWindowLong ( MainWindowHandle , GWL_EXSTYLE ) ;
SetWindowLong ( MainWindowHandle , GWL_EXSTYLE , Style | WS_EX_DLGMODALFRAME ) ;
SetWindowPos ( MainWindowHandle , new IntPtr ( 0 ) , 0 , 0 , 0 , 0 , SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED ) ;
}
}
}