/[RomCheater]/trunk/RomCheater/Docking/FloatingPIDSelector.cs
ViewVC logotype

Annotation of /trunk/RomCheater/Docking/FloatingPIDSelector.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 366 - (hide annotations) (download)
Sun Jun 10 03:14:42 2012 UTC (9 years ago) by william
File size: 15973 byte(s)

1 william 88 using System;
2     using System.Collections.Generic;
3     using System.ComponentModel;
4     using System.Data;
5     using System.Drawing;
6     using System.Text;
7     using System.Windows.Forms;
8     using System.Diagnostics;
9     using RomCheater.PluginFramework.Interfaces;
10     using RomCheater.PluginFramework.Core;
11 william 109 using System.IO;
12     using RomCheater.Logging;
13 william 148 using WeifenLuo.WinFormsUI.Docking;
14 william 196 using RomCheater.PluginFramework.Events;
15 william 329 using System.Management;
16 william 363 using System.Reflection;
17 william 88
18 william 149 namespace RomCheater.Docking
19 william 88 {
20 william 366 public partial class FloatingPIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>, IAcceptsChangedProcess, IAcceptsProcessPID
21 william 88 {
22 william 365 private const int ProcessRefershInterval = 5000; // refresh interval in milliseconds
23     private bool isUserInteracting = false;
24 william 329 private bool isRefreshing = false;
25     private bool isAutoRefreshing = false;
26 william 331 private bool isAttachingToProcess = false;
27 william 152 //private IConfigPlugin plugin = null;
28 william 88 //bool isInError = false;
29 william 366 public FloatingPIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
30     public FloatingPIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
31 william 88 // unsued construtor (it's not implmented, may not be)
32     [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
33 william 366 public FloatingPIDSelector(int pid) : this() { this.ProcessPID = pid; }
34 william 88 [Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)]
35 william 366 public FloatingPIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; }
36 william 327
37 william 196 #region IAcceptsProcessPID
38     public int ProcessPID { get; set; }
39     #endregion
40 william 335 #region IAcceptsPlugin<IConfigPlugin> members
41     private IConfigPlugin _AcceptedPlugin;
42     public IConfigPlugin AcceptedPlugin {
43     get { return _AcceptedPlugin; }
44     set { _AcceptedPlugin = value; ProcessWatcherTimer.Enabled = (value != null); } }
45 william 196 #endregion
46     #region IAcceptsChangedProcess members
47 william 287 public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged;
48 william 196 #endregion
49 william 151
50    
51 william 327
52 william 88 public new void Show() { this.Show(null); }
53 william 150 //public new void Show(IWin32Window owner) { this.PreInitShow(owner); }
54     public new void Show(DockPanel panel) { this.PreInitShow(panel); }
55     //public new DialogResult ShowDialog() { return this.ShowDialog(null); }
56     //public new DialogResult ShowDialog(IWin32Window owner) { return this.PreInitDialog(owner); }
57     //private void PreInitShow(IWin32Window owner)
58     //{
59     // if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.",plugin.ToString())); }
60     // else { if (owner == null) { base.Show(); } else { base.Show(owner); } }
61     //}
62     private void PreInitShow(DockPanel panel)
63 william 88 {
64 william 150 //if (panel == null) return;
65 william 153 if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", AcceptedPlugin.ToString())); }
66 william 150 else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
67 william 88 }
68 william 150 //private DialogResult PreInitDialog(IWin32Window owner)
69     //{
70     // if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", plugin.ToString())); return DialogResult.Cancel; }
71     // else { if (owner == null) { return base.ShowDialog(); } else { return base.ShowDialog(owner); } }
72     //}
73 william 363 //private void SetControlBackColor(Control control, KnownColor color) { SetControlBackColor(control, Color.FromKnownColor(color)); }
74     //private void SetControlBackColor(Control control, Color color) { control.BackColor = color; }
75     private void SetControlBackColor<T>(T component, KnownColor color) { SetControlBackColor(component, Color.FromKnownColor(color)); }
76     private void SetControlBackColor<T>(T component, Color color)
77     {
78     Type ComponentType = component.GetType();
79     PropertyInfo pBackColor = ComponentType.GetProperty("BackColor");
80     pBackColor.SetValue(component, color, null);
81     }
82     private void SetViewForProcessListView(View view)
83     {
84 william 364 Color baseColor = Color.FromKnownColor(KnownColor.GradientActiveCaption);
85     int GB_OFFSET = 10;
86     Color c = Color.FromArgb(baseColor.A, baseColor.R, baseColor.G + GB_OFFSET, baseColor.B + GB_OFFSET);
87 william 363 ResetViewButtonControls();
88     switch (view)
89     {
90     case View.Details: SetControlBackColor(btnDetails, c); break;
91     case View.LargeIcon: SetControlBackColor(btnLargeIcon, c); break;
92     case View.List: SetControlBackColor(btnList, c); break;
93     case View.SmallIcon: SetControlBackColor(btnSmallIcon, c); break;
94     case View.Tile: SetControlBackColor(btnTile, c); break;
95     }
96     lstProcessList.View = view;
97     }
98     private void ResetViewButtonControls()
99     {
100     KnownColor DefaultColor = KnownColor.Control;
101     SetControlBackColor(btnLargeIcon, DefaultColor);
102     SetControlBackColor(btnDetails, DefaultColor);
103     SetControlBackColor(btnSmallIcon, DefaultColor);
104     SetControlBackColor(btnList, DefaultColor);
105     SetControlBackColor(btnTile, DefaultColor);
106     }
107     private void btnLargeIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.LargeIcon); }
108     private void btnDetails_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Details); }
109     private void btnSmallIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.SmallIcon); }
110     private void btnList_Click(object sender, EventArgs e) { SetViewForProcessListView(View.List); }
111     private void btnTile_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Tile); }
112 william 88
113 william 156 private void PerformListViewItemSelect(ListViewItem li)
114 william 327 {
115 william 331 if (isAutoRefreshing && !isAttachingToProcess) return;
116 william 196 this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text);
117 william 296 Process SelectedProcess = Process.GetProcessById(this.ProcessPID);
118 william 332
119     txtstatus_Pid.Text = SelectedProcess.Id.ToString();
120     txtstatus_Filename.Text = SelectedProcess.MainModule.FileName;
121    
122 william 296 logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
123 william 151 //this.Close();
124 william 287 if (OnSelectedProcessChanged != null)
125 william 327 OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID));
126 william 331 if (isAttachingToProcess)
127     isAttachingToProcess = false;
128 william 88 }
129 william 156 private void btnOK_Click(object sender, EventArgs e)
130     {
131 william 327
132 william 156 }
133 william 88
134 william 151 //private void btnCancel_Click(object sender, EventArgs e)
135     //{
136     // this.SelectedPid = -1;
137     // //this.Close();
138     //}
139 william 88
140     private void btnRefresh_Click(object sender, EventArgs e)
141 william 335 {
142     //this.RefreshList();
143 william 88 }
144    
145     private bool RefreshList()
146     {
147 william 338 lstProcessList.BeginUpdate();
148 william 335 //if (this.AcceptedPlugin == null) { return false; };
149 william 329 isRefreshing = true;
150 william 88 lstProcessList.Items.Clear();
151 william 335 //logger.VerboseDebug.WriteLine(System.Environment.StackTrace);
152 william 329 this.AcceptedPlugin.Reload(true);
153 william 88 // create a selection of PCSX2 processes
154 william 153 List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
155 william 88
156 william 327 if (!(proc_list.Count > 0))
157     { //MessageBox.Show("Could not find any PCSX2 Processes. Please start an instance of one.");
158     return false;
159     }
160 william 88
161     List<ListViewItem> items = new List<ListViewItem>();
162     ImageList small_image_list = new ImageList();
163     ImageList large_image_list = new ImageList();
164 william 109
165 william 365 int small_width = 24;
166     int large_width = 32;
167 william 109
168     small_image_list.ImageSize = new Size(small_width, small_width);
169     large_image_list.ImageSize = new Size(large_width, large_width);
170 william 327 foreach (ProcContainer p in proc_list)
171 william 109 {
172     Bitmap small_image = null;
173     Bitmap large_image = null;
174     string image_key = string.Empty;
175     this.AddProcessItem(p, small_image_list.ImageSize, large_image_list.ImageSize, out image_key, out small_image, out large_image);
176     if (image_key != string.Empty)
177     {
178     if (small_image != null) { small_image_list.Images.Add(image_key, small_image); }
179     if (large_image != null) { large_image_list.Images.Add(image_key, large_image); }
180     ListViewItem item = new ListViewItem();
181 william 327 item.Name = p.GetHashCode().ToString();
182 william 109 // process name
183     item.Text = p.Name;
184     // pid
185     item.SubItems.Add(p.ProcessInfo.Id.ToString());
186     // FullPath
187     item.SubItems.Add(p.FileName);
188     // image key
189     item.ImageKey = p.FileName;
190     if (!items.Contains(item)) items.Add(item);
191     }
192     else
193     {
194 william 156 //logger.Error.WriteLine("image_key is an empty string!");
195     //if (small_image != null) { logger.Error.WriteLine(" [however small_image is not null]"); }
196     //if (large_image != null) { logger.Error.WriteLine(" [however large_image is not null]\n"); }
197 william 109 }
198     }
199 william 88 lstProcessList.SmallImageList = small_image_list;
200     lstProcessList.LargeImageList = large_image_list;
201     this.lstProcessList.Items.AddRange(items.ToArray());
202 william 156
203 william 331 if (lstProcessList.Items.Count > 1 && !isAttachingToProcess)
204 william 156 {
205     lstProcessList.Items[0].Selected = true;
206     }
207 william 329 isRefreshing = false;
208 william 338 lstProcessList.EndUpdate();
209 william 88 return true;
210     }
211    
212 william 109 private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
213 william 88 {
214 william 109 image_key = "";
215     small_image = null;
216     large_image = null;
217     ////small_image_list = new ImageList();
218     ////large_image_list = new ImageList();
219     //ListViewItem item = new ListViewItem();
220     //// process
221     //item.Text = p.Name;
222     //if (p.ProcessIcon != null)
223     //{
224     // //small_image_list.Images.Add(
225     // //large_image_list.Images.Add(p.FileName, p.ProcessIcon);
226     // item.ImageKey = p.FileName;
227     //}
228     //// pid
229     //item.SubItems.Add(p.ProcessInfo.Id.ToString());
230     //// FullPath
231     //item.SubItems.Add(p.FileName);
232     //if (!items.Contains(item)) items.Add(item);
233     if (p != null)
234 william 88 {
235 william 109 if (p.ProcessIcon != null)
236     {
237     image_key = p.FileName;
238     small_image = new Bitmap(p.ProcessIcon, small);
239     large_image = new Bitmap(p.ProcessIcon, large);
240     }
241 william 88 }
242 william 109
243 william 88 }
244 william 152
245     private void PIDSelector_Load(object sender, EventArgs e)
246     {
247 william 153 if (AcceptedPlugin == null || this.DesignMode) return;
248 william 365 ProcessWatcherTimer.Interval = ProcessRefershInterval;
249 william 332 txtstatus_Pid.Text = "";
250     txtstatus_Filename.Text = "";
251 william 365 SetViewForProcessListView(View.LargeIcon);
252 william 335 this.RefreshList();
253 william 152 }
254 william 156
255    
256     private void lstProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
257     {
258     if (!e.IsSelected) return;
259     PerformListViewItemSelect(e.Item);
260     }
261    
262     private void lstProcessList_ItemActivate(object sender, EventArgs e)
263     {
264     ListViewItem Item = lstProcessList.SelectedItems[0];
265     PerformListViewItemSelect(Item);
266     }
267    
268 william 327 private void btnLaunchAndAttach_Click(object sender, EventArgs e)
269     {
270     DialogResult result = EXESelector.ShowDialog();
271     if (result != DialogResult.OK) return;
272     FileInfo fi = new FileInfo(EXESelector.FileName);
273     if (!fi.Exists) { logger.Warn.WriteLine("Cannot load and attach to non-existing exe file: {0}", fi.FullName); return; }
274 william 331 Process p = Process.Start(fi.FullName);
275     this.ProcessPID = p.Id;
276 william 335 isAttachingToProcess = true;
277 william 327 }
278 william 329
279     private void ProcessWatcherTimer_Tick(object sender, EventArgs e)
280     {
281 william 365 if (isRefreshing || isAutoRefreshing || isUserInteracting) return;
282 william 329 //if (lstProcessList.Items.Count == 0) return;
283     isAutoRefreshing = true;
284 william 332 int pid = this.ProcessPID;
285 william 335 this.RefreshList();
286 william 329 bool pid_still_exists = false;
287     int item_index = -1;
288     foreach (ListViewItem li2 in lstProcessList.Items)
289     {
290     int _pid = Convert.ToInt32(li2.SubItems[1].Text);
291     if (_pid == pid)
292     {
293     item_index = lstProcessList.Items.IndexOf(li2);
294     pid_still_exists = true;
295     break;
296     }
297     }
298     if (pid_still_exists)
299     {
300 william 335 //logger.Debug.WriteLine("Pid: {0} still exists", pid);
301 william 329 if (lstProcessList.SelectedItems.Count > 0)
302     {
303     if (lstProcessList.SelectedItems[0] != null)
304     if (lstProcessList.SelectedItems[0].Index != -1)
305     lstProcessList.Items[lstProcessList.SelectedItems[0].Index].Selected = false;
306     }
307     lstProcessList.Items[item_index].Selected = true;
308 william 332 isAutoRefreshing = false;
309 william 329 }
310     else
311     {
312     if (lstProcessList.SelectedItems.Count > 0)
313 william 332 {
314     if (lstProcessList.SelectedItems[0] != null)
315     {
316 william 353 int missing_pid = this.ProcessPID;
317     logger.Warn.WriteLine("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid);
318     MessageBox.Show(string.Format("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid), "Connection to selected process lost", MessageBoxButtons.OK, MessageBoxIcon.Warning);
319 william 332 }
320     isAutoRefreshing = false;
321     lstProcessList.Items[0].Selected = false;
322 william 329 lstProcessList.Items[0].Selected = true;
323 william 332 }
324 william 329 }
325     }
326 william 365
327    
328     private void lstProcessList_MouseMove(object sender, MouseEventArgs e)
329     {
330     isUserInteracting = true;
331     }
332    
333     private void lstProcessList_MouseLeave(object sender, EventArgs e)
334     {
335     isUserInteracting = false;
336     }
337 william 88 }
338     }
339    

  ViewVC Help
Powered by ViewVC 1.1.22