forked from ConEmu/conemu-inside
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
318 lines (281 loc) · 10.4 KB
/
Form1.cs
File metadata and controls
318 lines (281 loc) · 10.4 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
namespace ConEmuInside
{
public partial class ChildTerminal : Form
{
protected Process ConEmu;
protected GuiMacro guiMacro;
public ChildTerminal()
{
InitializeComponent();
}
private void ChildTerminal_Load(object sender, EventArgs e)
{
string lsOurDir;
argConEmuExe.Text = GetConEmu();
argDirectory.Text = Directory.GetCurrentDirectory();
lsOurDir = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
argXmlFile.Text = Path.Combine(lsOurDir, "ConEmu.xml");
argCmdLine.Text = @"{cmd}"; // Use ConEmu's default {cmd} task
RefreshControls(false);
// Force focus to ‘cmd line’ control
argCmdLine.Select();
//
termPanel.Resize += new System.EventHandler(this.termPanel_Resize);
}
private void RefreshControls(bool bTermActive)
{
if (bTermActive)
{
AcceptButton = null;
groupBox2.Enabled = true;
if (startPanel.Visible)
{
promptBox.Focus();
startPanel.Visible = false;
}
if (!termPanel.Visible)
{
termPanel.Visible = true;
}
}
else
{
if (termPanel.Visible)
{
termPanel.Visible = false;
}
if (!startPanel.Visible)
{
startPanel.Visible = true;
argCmdLine.Focus();
}
groupBox2.Enabled = false;
AcceptButton = startBtn;
}
}
private string GetConEmu()
{
string sOurDir = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string[] sSearchIn = {
Directory.GetCurrentDirectory(),
sOurDir,
Path.Combine(sOurDir, ".."),
Path.Combine(sOurDir, "ConEmu"),
"%PATH%", "%REG%"
};
string[] sNames;
sNames = new string[] { "ConEmu.exe", "ConEmu64.exe" };
foreach (string sd in sSearchIn)
{
foreach (string sn in sNames)
{
string spath;
if (sd == "%PATH%" || sd == "%REG%")
{
spath = sn; //TODO
}
else
{
spath = Path.Combine(sd, sn);
}
if (File.Exists(spath))
return spath;
}
}
// Default
return "ConEmu.exe";
}
private string GetConEmuCD()
{
// Returns Path to ConEmuC (to GuiMacro execution)
if ((ConEmu == null) || ConEmu.HasExited)
return null;
if (ConEmu.Modules.Count == 0)
return null;
string lsDll = (IntPtr.Size == 8) ? "ConEmuCD64.dll" : "ConEmuCD.dll";
String lsExeDir, ConEmuCD;
lsExeDir = Path.GetDirectoryName(ConEmu.Modules[0].FileName);
ConEmuCD = Path.Combine(lsExeDir, "ConEmu\\" + lsDll);
if (!File.Exists(ConEmuCD))
{
ConEmuCD = Path.Combine(lsExeDir, lsDll);
if (!File.Exists(ConEmuCD))
{
ConEmuCD = lsDll; // Must not get here actually
}
}
return ConEmuCD;
}
private void ExecuteGuiMacro(string asMacro)
{
// conemuc.exe -silent -guimacro:1234 print("\e","git"," --version","\n")
string ConEmuCD = GetConEmuCD();
if (ConEmuCD == null)
{
throw new GuiMacroException("ConEmuCD must not be null");
}
if (guiMacro != null && guiMacro.LibraryPath != ConEmuCD)
{
guiMacro = null;
}
try
{
if (guiMacro == null)
guiMacro = new GuiMacro(ConEmuCD);
guiMacro.Execute(ConEmu.Id.ToString(), asMacro,
(GuiMacro.GuiMacroResult code, string data) => {
Debugger.Log(0, "GuiMacroResult", "code=" + code.ToString() + "; data=" + data + "\n");
});
}
catch (GuiMacroException e)
{
MessageBox.Show(e.Message, "GuiMacroException", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void printBtn_Click(object sender, EventArgs e)
{
if (promptBox.Text == "")
return;
String lsMacro;
lsMacro = "Print(@\"" + promptBox.Text.Replace("\"", "\"\"") + "\",\"\n\")";
ExecuteGuiMacro(lsMacro);
promptBox.SelectAll();
}
private void macroBtn_Click(object sender, EventArgs e)
{
if (promptBox.Text == "")
return;
ExecuteGuiMacro(promptBox.Text);
promptBox.SelectAll();
}
private void timer1_Tick(object sender, EventArgs e)
{
if ((ConEmu != null) && ConEmu.HasExited)
{
timer1.Stop();
ConEmu = null;
RefreshControls(false);
}
}
private void promptBox_KeyDown(object sender, KeyEventArgs e)
{
//TODO: Enter and Leave events are not triggered when focus is put into ConEmu window
if (e.KeyValue == 13)
{
printBtn_Click(sender, null);
}
}
private void promptBox_Enter(object sender, EventArgs e)
{
//TODO: Enter and Leave events are not triggered when focus is put into ConEmu window
//AcceptButton = printBtn;
//promptBox.Text = "...in...";
}
private void promptBox_Leave(object sender, EventArgs e)
{
//TODO: Enter and Leave events are not triggered when focus is put into ConEmu window
//if (AcceptButton == printBtn)
//AcceptButton = null;
//promptBox.Text = "...out...";
}
private void exeBtn_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Choose ConEmu main executable";
openFileDialog1.FileName = argConEmuExe.Text;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
argConEmuExe.Text = openFileDialog1.FileName;
}
argConEmuExe.Focus();
}
private void cmdBtn_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Choose startup shell";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
argCmdLine.Text = openFileDialog1.FileName;
}
argCmdLine.Focus();
}
private void dirBtn_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = argDirectory.Text;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
argDirectory.Text = folderBrowserDialog1.SelectedPath;
}
argDirectory.Focus();
}
private void startBtn_Click(object sender, EventArgs e)
{
string sRunAs, sRunArgs;
// Show terminal panel, hide start options
//RefreshControls(true);
sRunAs = argRunAs.Checked ? " -cur_console:a" : "";
sRunArgs =
(argDebug.Checked ? " -debugw" : "") +
" -NoKeyHooks" +
" -InsideWnd 0x" + termPanel.Handle.ToString("X") +
" -LoadCfgFile \"" + argXmlFile.Text + "\"" +
" -Dir \"" + argDirectory.Text + "\"" +
(argLog.Checked ? " -Log" : "") +
" -detached"
//" -cmd " + // This one MUST be the last switch
//argCmdLine.Text + sRunAs // And the shell command line itself
;
promptBox.Text = "Shell(\"new_console\", \"\", \"" + (argCmdLine.Text + sRunAs).Replace("\"", "\\\"") + "\")";
try {
// Start ConEmu
ConEmu = Process.Start(argConEmuExe.Text, sRunArgs);
} catch (System.ComponentModel.Win32Exception ex) {
RefreshControls(false);
MessageBox.Show(ex.Message + "\r\n\r\n" +
"Command:\r\n" + argConEmuExe.Text + "\r\n\r\n" +
"Arguments:\r\n" + sRunArgs,
ex.GetType().FullName + " (" + ex.NativeErrorCode.ToString() + ")",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
RefreshControls(true);
// Start monitoring
timer1.Start();
// Execute "startup" macro
macroBtn_Click(null, null);
}
private void startArgs_Enter(object sender, EventArgs e)
{
AcceptButton = startBtn;
}
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindowEx(IntPtr hParent, IntPtr hChild, string szClass, string szWindow);
private void termPanel_Resize(object sender, EventArgs e)
{
if (ConEmu != null)
{
IntPtr hConEmu = FindWindowEx(termPanel.Handle, (IntPtr)0, null, null);
if (hConEmu != (IntPtr)0)
{
MoveWindow(hConEmu, 0, 0, termPanel.Width, termPanel.Height, true);
}
}
}
private void closeBtn_Click(object sender, EventArgs e)
{
ExecuteGuiMacro("Close(2,1)");
}
}
}