/[EmuXPortal]/branches/mono/EmuXPortal/Form1.cs
ViewVC logotype

Annotation of /branches/mono/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 466 - (hide annotations) (download)
Sun Dec 22 07:15:30 2019 UTC (2 months, 1 week ago) by william
File size: 11345 byte(s)
more updates for disabling input queue
1 william 460 //#define DISABLE_PLUGINS // when defined plugin loading an their use will be disabled
2 william 404
3 william 460 #define HAVE_X11_BORDERSTYLE_ERROR // indicates that there is an error with Control.set_InternalBorderStyle resulting in an X11 Error BadWindow (invalid Window parameter)
4    
5 william 404 #define DISABLE_WORKER_PROGRESS_EVENT // when defined will disable worker progress events
6     #define DISAGLE_LOAD_SPLASH // when defined will not display a splash at loading (e.g. during cache building)
7 william 377
8     //#define DISABLE_CURSOR_HIDE // when this is present, the cursor will not be hidden
9     //#define DISABLE_PROGRESS_PERCENTAGE_MESSASGE // when this is present, no progress percent message will be shown on any progressbar
10    
11 william 241 #define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
12 william 377 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
13    
14 william 181 using System;
15 william 4 using System.Collections.Generic;
16     using System.ComponentModel;
17     using System.Data;
18     using System.Drawing;
19     using System.Linq;
20     using System.Text;
21     using System.Windows.Forms;
22 william 12 using EmuXPortal.Api;
23 william 23 using System.Diagnostics;
24 william 60 using System.Reflection;
25     using System.Threading;
26 william 183 using Utilities.TransparentControls;
27 william 238 using Enterprise.Logging;
28     using System.IO;
29 william 398 using Enterprise.CrossPlatform;
30 william 408 using EmuXPortal.UI;
31 william 413 using EmuXPortal.UI.RenderControls;
32 william 445 using EmuXPortal.Plugins;
33 william 398
34 william 377 namespace EmuXPortal {
35     public partial class Form1 : Form {
36 william 355 static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR = System.Drawing.Color.Lime;
37     static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR_ERROR = System.Drawing.Color.Yellow;
38     static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR_LAUNCH = System.Drawing.Color.Blue;
39     static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR_MISSING = System.Drawing.Color.Red;
40    
41 william 406 private bool bRebuildCache = false;
42    
43 william 355
44 william 377 static bool bUserAbort = false;
45 william 415
46 william 408
47 william 327 private CustomProgressBar progress_bar;
48 william 355 private IPlatformCache cache = null;
49 william 243
50 william 455
51     private System.Windows.Forms.Timer input_queue_handler;
52     private bool bInputQueuHandlerRunning = false;
53 william 465 private bool bInputQueueDisabled = false;
54 william 455
55 william 456 private Queue<KeyArrayArgs> event_queue;
56 william 455 private readonly object event_queue_lock = new object ();
57    
58 william 377 static bool OnAbort ()
59 william 455 {
60    
61 william 377 return bUserAbort;
62     }
63     #region unhandled exception support
64     static void Application_Unhandled_ThreadException (object sender, ThreadExceptionEventArgs e)
65     {
66     UnhandledExceptionEventArgs uea = new UnhandledExceptionEventArgs (e.Exception, false);
67     UnhandledExceptionEventHandler (sender, uea);
68     }
69     static void UnhandledExceptionEventHandler (object sender, UnhandledExceptionEventArgs args)
70     {
71     Exception ex = (args.ExceptionObject as Exception);
72     if (sender == null) {
73     gLog.Error.WriteLine ("Caught an unhandled exception from an unkown source");
74     } else {
75     gLog.Error.WriteLine ("Caught an unhandled exception from type: {0}", sender.GetType ().Name);
76     }
77    
78     if (ex == null) {
79     gLog.Error.WriteLine ("The exception object was null -- it probably is not derived from System.Exception");
80     } else {
81     ex = ex.GetBaseException ();
82     gLog.Error.WriteLine ("{0}:", ex.GetType ().Name);
83     gLog.Verbose.Error.WriteLine (ex.ToString ());
84     }
85    
86     }
87     #endregion
88     public Form1 ()
89     {
90     InitializeComponent ();
91 william 409 this.BackColor = Color.Black;
92     this.ForeColor = Color.LimeGreen;
93 william 377 this.progress_bar = new CustomProgressBar ();
94     this.KeyPreview = true;
95    
96     #region logging support
97     string log_path = Application.StartupPath;
98     string log_filename = string.Format ("{0}.log", typeof (Form1).Assembly.GetName ().Name);
99     FileInfo log_file = new FileInfo (OSInfo.FormatPath (string.Format (@"{0}\{1}", log_path, log_filename)));
100    
101     gLog.CreateLog (log_file.Name, true, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs> (Log_OnFlush));
102 william 238 #if DEBUG
103 william 377 LogLevel gLevel = gLog.LogLevel;
104 william 238 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
105     gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
106     #else
107 william 377 gLevel |= LogLevel.kLogLevel_VerboseDebug;
108 william 238 #endif
109 william 377 gLevel |= LogLevel.kLogLevel_Debug;
110     gLog.SetLogLevel (gLevel);
111 william 238 #else
112     LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
113     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
114     #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
115     gLevel &= ~LogLevel.kLogLevel_Debug;
116     #else
117     gLevel |= LogLevel.kLogLevel_Debug;
118     #endif
119     gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
120     gLog.SetLogLevel(gLevel);
121     #endif
122 william 460 #endregion
123 william 377
124 william 460 #region unhandled exception support
125     // AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
126     // Application.ThreadException += Application_Unhandled_ThreadException;
127     #endregion
128 william 377
129 william 465 #if !DISABLE_PLUGINS
130 william 456 event_queue = new Queue<KeyArrayArgs> ();
131 william 454 PluginLoader.LoadPlugins ();
132 william 455 PluginLoader.InitializeInputSystem ();
133    
134    
135     input_queue_handler = new System.Windows.Forms.Timer ();
136     input_queue_handler.Tick += Input_Queue_Handler_Tick;
137 william 456 input_queue_handler.Interval = 200;
138     input_queue_handler.Enabled = true;
139 william 460 input_queue_handler.Start ();
140     #endif
141 william 455 }
142 william 453
143 william 455 void Input_Queue_Handler_Tick (object sender, EventArgs e)
144 william 466 {
145     if (bInputQueueDisabled) {
146     return;
147     }
148 william 456 //gLog.Verbose.Debug.WriteLine ("Input_Queue_Handler_Tick -- running");
149 william 455 if (bInputQueuHandlerRunning) { return; }
150     if (event_queue.Count > 0) {
151     bInputQueuHandlerRunning = true;
152 william 460 KeyArrayArgs key_event = null;
153     lock (event_queue_lock) {
154     key_event = event_queue.Dequeue ();
155     }
156 william 456 var keys = key_event.GetKeys ();
157     string key_string = KeyConverter.getSendKeysString (keys);
158 william 460 gLog.Verbose.Debug.WriteLine ("Sending key: {0}", key_string);
159 william 457 SendKeys.Send (key_string);
160 william 455 // process key_event
161     bInputQueuHandlerRunning = false;
162     }
163     }
164    
165 william 377
166 william 456 void Handle_InputPoll (object sender, KeyArrayArgs e)
167 william 455 {
168 william 466 if (bInputQueueDisabled) {
169     // drop this input event
170     var keys = e.GetKeys ();
171     StringBuilder sb = new StringBuilder ();
172     if (keys.Length > 0) {
173     sb.AppendFormat ("{0}", keys [0]);
174     }
175     for (int i = 1; i < keys.Length; i++) {
176     sb.AppendFormat (", {0}", keys [i]);
177     }
178     gLog.Verbose.Warn.WriteLine ("Input Queue temporairly disabled....dropped key event ({0})!", sb.ToString());
179     return;
180     }
181 william 465 lock (event_queue_lock) {
182     event_queue.Enqueue (e);
183 william 455 }
184     }
185 william 377
186 william 455
187    
188 william 460 #region logging support
189 william 377 void Log_OnFlush (object sender, LoggerOnFlushEventArgs e) { OnLogFlush (e.Buffer); }
190 william 455
191    
192    
193    
194 william 377 void OnLogFlush (string logmessage)
195     {
196     //if (this.IsDisposed) { return; }
197     ////UpdateStatus(logmessage);
198     ////UpdateLogOutput(logmessage);
199     //Application.DoEvents();
200     }
201 william 460 #endregion
202 william 377
203 william 355 private bool bCachedLoaded = false;
204 william 377 private bool OnCacheLoaded ()
205     {
206 william 355 return bCachedLoaded;
207 william 377 }
208 william 406
209    
210     private void HandleCommandLine ()
211     {
212     var cargs = new List<String> (System.Environment.GetCommandLineArgs ());
213     if (cargs.Contains ("--rebuild")) {
214     bRebuildCache = true;
215     }
216     }
217 william 377
218     private void Form1_Load (object sender, EventArgs e)
219 william 404 {
220    
221 william 406 HandleCommandLine ();
222 william 404 #if !DISAGLE_LOAD_SPLASH
223 william 377 SplashLoader loader = new SplashLoader (new AbortEvent (OnCacheLoaded));
224     loader.Show ();
225 william 404 #endif
226 william 429 Stopwatch t = new Stopwatch ();
227 william 436 t.Start ();
228     try {
229     cache = PlatformCache.Create (new AbortEvent (OnAbort), bRebuildCache);
230     } catch (Exception ex) {
231     gLog.Error.WriteLine (ex.ToString ());
232     }
233 william 429 t.Stop ();
234     gLog.Profiler.WriteLine ("Cache generate/load took: {0}", TimeUtils.getTimeString(t));
235    
236     t = new Stopwatch ();
237     t.Start ();
238     Config.LoadConfig (cache);
239     t.Stop ();
240     gLog.Profiler.WriteLine ("Config load took: {0}", TimeUtils.getTimeString(t));
241    
242 william 377 Config.InitializePresentationForm (this);
243 william 409 bCachedLoaded = true;
244    
245     /* initialize the container controls */
246     platform_flow.setCache (cache);
247 william 415 rom_flow.setCache (cache);
248 william 377
249 william 409
250 william 415
251     platform_flow.Font = this.Font;
252     rom_flow.Font = this.Font;
253    
254 william 460 //platform_flow.setDefaultPlatformImage ((Bitmap)Properties.Resources.DefaultPlatformImage);
255     //rom_flow.setDefaultGameImage ((Bitmap)Properties.Resources.DefaultGameImage);
256 william 415
257 william 409 platform_flow.BackColor = this.BackColor;
258     platform_flow.ForeColor = this.ForeColor;
259    
260     rom_flow.BackColor = this.BackColor;
261 william 412 rom_flow.ForeColor = this.ForeColor;
262    
263 william 413 platform_flow.controlsRendered += ControlsRendered;
264     rom_flow.controlsRendered += ControlsRendered;
265 william 412
266 william 413 platform_flow.switchControls += SwitchControls;
267     rom_flow.switchControls += SwitchControls;
268    
269     platform_flow.Parent = this;
270     rom_flow.Parent = this;
271 william 412
272 william 413
273     platform_flow.Width = this.Width-10;
274     rom_flow.Width = this.Width-10;
275 william 409
276 william 413 platform_flow.setupScroll ();
277 william 420 rom_flow.setupScroll ();
278    
279 william 465 rom_flow.RomLaunched += Rom_Flow_RomLaunched;
280    
281    
282 william 413 }
283 william 465
284     void Rom_Flow_RomLaunched (object sender, RomLaunchArgs e)
285     {
286     var flags = e.RomLaunchFlags;
287     var info = e.RomInfo;
288     string game_name = "";
289     if (info == null) {
290     gLog.Warn.WriteLine ("recieved null rom info!");
291     } else {
292     game_name = info.RomTitle;
293     }
294 william 420
295 william 465 if (flags == RomLaunchFlags.BEGIN_LAUNCH) {
296     gLog.Debug.WriteLine ("Game '{0}' has been launched, disabling input queue",game_name);
297     bInputQueueDisabled = true;
298     } else if (flags == RomLaunchFlags.END_LAUNCH) {
299     gLog.Debug.WriteLine ("Game '{0}' has exited, re-enabling input queue",game_name);
300     bInputQueueDisabled = false;
301     } else {
302     if (flags != RomLaunchFlags.NONE) {
303     gLog.Debug.WriteLine ("Unknown rom launch flag: {0}", (int)flags);
304     }
305     }
306     }
307 william 420
308 william 465
309 william 412
310 william 413 void ControlsRendered (object sender, EventArgs e)
311 william 415 {
312 william 413
313 william 377 }
314 william 412
315 william 413 void SwitchControls (object sender, EventArgs e)
316     {
317     PlatformContainer pc = (sender as PlatformContainer);
318     if (pc != null) {
319     platform_flow.Visible = false;
320     rom_flow.Visible = true;
321     rom_flow.BringToFront ();
322     rom_flow.setCurrentRomPlatform (pc.getCurrentPlatform ());
323 william 419 rom_flow.Focus ();
324 william 413 rom_flow.renderControls ();
325     }
326     GameContainer gc = (sender as GameContainer);
327     if (gc != null) {
328     rom_flow.Visible = false;
329     platform_flow.Visible = true;
330 william 419 platform_flow.BringToFront ();
331     platform_flow.Focus ();
332 william 413 platform_flow.renderControls ();
333     }
334     }
335    
336    
337    
338    
339    
340 william 377 private void Form1_Shown (object sender, EventArgs e)
341     {
342     platform_flow.Visible = true;
343 william 181 #if !DISABLE_CURSOR_HIDE
344 william 377 Cursor.Hide ();
345     #else
346 william 181 Cursor.Show();
347     #endif
348 william 460
349     #if !DISABLE_PLUGINS
350 william 466 PluginLoader.StartInputPolling (bInputQueueDisabled, Handle_InputPoll);
351 william 460 #endif
352 william 377
353 william 420 }
354 william 415
355 william 377
356     private void platform_flow_VisibleChanged (object sender, EventArgs e)
357     {
358 william 415 if (!platform_flow.Visible) return;
359 william 413 platform_flow.renderControls ();
360 william 409 }
361    
362    
363    
364 william 377 private void rom_flow_VisibleChanged (object sender, EventArgs e)
365     {
366 william 415 if (!rom_flow.Visible) return;
367     }
368 william 409
369 william 408
370 william 415 private void Form1_FormClosed (object sender, FormClosedEventArgs e)
371 william 272 {
372 william 456 Cursor.Show ();
373     PluginLoader.EndInputPolling ();
374 william 408 }
375 william 377 }
376 william 4 }

  ViewVC Help
Powered by ViewVC 1.1.22