aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Commands.cs
blob: a0a169217321a8cf67783fadedabe211b4f617ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * 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;
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;

/// <summary>
/// Coherence commands.
/// </summary>
namespace ShiftOS.WinForms
{
    [Namespace("coherence")]
    [RequiresUpgrade("kernel_coherence")]
    public static class CoherenceCommands
    {
		/// <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")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

		/// <summary>
		/// The HWN d TOPMOS.
		/// </summary>
        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);

		/// <summary>
		/// The SW p SHOWWINDO.
		/// </summary>
        const UInt32 SWP_SHOWWINDOW = 0x0040;

        
        [DllImport("user32.dll")]
		/// <summary>
		/// Gets the window rect.
		/// </summary>
		/// <returns>The window rect.</returns>
		/// <param name="hWnd">H window.</param>
		/// <param name="lpRect">Lp rect.</param>
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

		/// <summary>
		/// REC.
		/// </summary>
        [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")]
		/// <summary>
		/// Launchs the app.
		/// </summary>
		/// <returns>The app.</returns>
		/// <param name="args">Arguments.</param>
        public static bool LaunchApp(Dictionary<string, object> args)
        {
            string process = args["process"].ToString();
            var prc = Process.Start(process);
            StartCoherence(prc);
            return true;
        }

		/// <summary>
		/// Starts the coherence.
		/// </summary>
		/// <returns>The coherence.</returns>
		/// <param name="prc">Prc.</param>
        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);
                }));
            
        }

		/// <summary>
		/// The W s BORDE.
		/// </summary>
        const int WS_BORDER = 8388608;

		/// <summary>
		/// The W s DLGFRAM.
		/// </summary>
        const int WS_DLGFRAME = 4194304;

		/// <summary>
		/// The W s CAPTIO.
		/// </summary>
        const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;

		/// <summary>
		/// The W s SYSMEN.
		/// </summary>
        const int WS_SYSMENU = 524288;

		/// <summary>
		/// The W s THICKFRAM.
		/// </summary>
        const int WS_THICKFRAME = 262144;

		/// <summary>
		/// The W s MINIMIZ.
		/// </summary>
        const int WS_MINIMIZE = 536870912;

		/// <summary>
		/// The W s MAXIMIZEBO.
		/// </summary>
        const int WS_MAXIMIZEBOX = 65536;

		/// <summary>
		/// The GW l STYL.
		/// </summary>
        const int GWL_STYLE = -16;

		/// <summary>
		/// The GW l EXSTYL.
		/// </summary>
        const int GWL_EXSTYLE = -20;

		/// <summary>
		/// The W s E x DLGMODALFRAM.
		/// </summary>
        const int WS_EX_DLGMODALFRAME = 0x1;

		/// <summary>
		/// The SW p NOMOV.
		/// </summary>
        const int SWP_NOMOVE = 0x2;

		/// <summary>
		/// The SW p NOSIZ.
		/// </summary>
        const int SWP_NOSIZE = 0x1;

		/// <summary>
		/// The SW p FRAMECHANGE.
		/// </summary>
        const int SWP_FRAMECHANGED = 0x20;

		/// <summary>
		/// The M f BYPOSITIO.
		/// </summary>
        const uint MF_BYPOSITION = 0x400;

		/// <summary>
		/// The M f REMOV.
		/// </summary>
        const uint MF_REMOVE = 0x1000;

		/// <summary>
		/// Gets the window long.
		/// </summary>
		/// <returns>The window long.</returns>
		/// <param name="hWnd">H window.</param>
		/// <param name="nIndex">N index.</param>
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        
		/// <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)]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        
		/// <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)]
        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);
        }
    }
}