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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 466 - (show annotations) (download)
Sun Dec 22 07:15:30 2019 UTC (8 weeks, 4 days ago) by william
File size: 11345 byte(s)
more updates for disabling input queue
1 //#define DISABLE_PLUGINS // when defined plugin loading an their use will be disabled
2
3 #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 #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
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 #define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
12 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
13
14 using System;
15 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 using EmuXPortal.Api;
23 using System.Diagnostics;
24 using System.Reflection;
25 using System.Threading;
26 using Utilities.TransparentControls;
27 using Enterprise.Logging;
28 using System.IO;
29 using Enterprise.CrossPlatform;
30 using EmuXPortal.UI;
31 using EmuXPortal.UI.RenderControls;
32 using EmuXPortal.Plugins;
33
34 namespace EmuXPortal {
35 public partial class Form1 : Form {
36 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 private bool bRebuildCache = false;
42
43
44 static bool bUserAbort = false;
45
46
47 private CustomProgressBar progress_bar;
48 private IPlatformCache cache = null;
49
50
51 private System.Windows.Forms.Timer input_queue_handler;
52 private bool bInputQueuHandlerRunning = false;
53 private bool bInputQueueDisabled = false;
54
55 private Queue<KeyArrayArgs> event_queue;
56 private readonly object event_queue_lock = new object ();
57
58 static bool OnAbort ()
59 {
60
61 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 this.BackColor = Color.Black;
92 this.ForeColor = Color.LimeGreen;
93 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 #if DEBUG
103 LogLevel gLevel = gLog.LogLevel;
104 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
105 gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
106 #else
107 gLevel |= LogLevel.kLogLevel_VerboseDebug;
108 #endif
109 gLevel |= LogLevel.kLogLevel_Debug;
110 gLog.SetLogLevel (gLevel);
111 #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 #endregion
123
124 #region unhandled exception support
125 // AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
126 // Application.ThreadException += Application_Unhandled_ThreadException;
127 #endregion
128
129 #if !DISABLE_PLUGINS
130 event_queue = new Queue<KeyArrayArgs> ();
131 PluginLoader.LoadPlugins ();
132 PluginLoader.InitializeInputSystem ();
133
134
135 input_queue_handler = new System.Windows.Forms.Timer ();
136 input_queue_handler.Tick += Input_Queue_Handler_Tick;
137 input_queue_handler.Interval = 200;
138 input_queue_handler.Enabled = true;
139 input_queue_handler.Start ();
140 #endif
141 }
142
143 void Input_Queue_Handler_Tick (object sender, EventArgs e)
144 {
145 if (bInputQueueDisabled) {
146 return;
147 }
148 //gLog.Verbose.Debug.WriteLine ("Input_Queue_Handler_Tick -- running");
149 if (bInputQueuHandlerRunning) { return; }
150 if (event_queue.Count > 0) {
151 bInputQueuHandlerRunning = true;
152 KeyArrayArgs key_event = null;
153 lock (event_queue_lock) {
154 key_event = event_queue.Dequeue ();
155 }
156 var keys = key_event.GetKeys ();
157 string key_string = KeyConverter.getSendKeysString (keys);
158 gLog.Verbose.Debug.WriteLine ("Sending key: {0}", key_string);
159 SendKeys.Send (key_string);
160 // process key_event
161 bInputQueuHandlerRunning = false;
162 }
163 }
164
165
166 void Handle_InputPoll (object sender, KeyArrayArgs e)
167 {
168 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 lock (event_queue_lock) {
182 event_queue.Enqueue (e);
183 }
184 }
185
186
187
188 #region logging support
189 void Log_OnFlush (object sender, LoggerOnFlushEventArgs e) { OnLogFlush (e.Buffer); }
190
191
192
193
194 void OnLogFlush (string logmessage)
195 {
196 //if (this.IsDisposed) { return; }
197 ////UpdateStatus(logmessage);
198 ////UpdateLogOutput(logmessage);
199 //Application.DoEvents();
200 }
201 #endregion
202
203 private bool bCachedLoaded = false;
204 private bool OnCacheLoaded ()
205 {
206 return bCachedLoaded;
207 }
208
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
218 private void Form1_Load (object sender, EventArgs e)
219 {
220
221 HandleCommandLine ();
222 #if !DISAGLE_LOAD_SPLASH
223 SplashLoader loader = new SplashLoader (new AbortEvent (OnCacheLoaded));
224 loader.Show ();
225 #endif
226 Stopwatch t = new Stopwatch ();
227 t.Start ();
228 try {
229 cache = PlatformCache.Create (new AbortEvent (OnAbort), bRebuildCache);
230 } catch (Exception ex) {
231 gLog.Error.WriteLine (ex.ToString ());
232 }
233 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 Config.InitializePresentationForm (this);
243 bCachedLoaded = true;
244
245 /* initialize the container controls */
246 platform_flow.setCache (cache);
247 rom_flow.setCache (cache);
248
249
250
251 platform_flow.Font = this.Font;
252 rom_flow.Font = this.Font;
253
254 //platform_flow.setDefaultPlatformImage ((Bitmap)Properties.Resources.DefaultPlatformImage);
255 //rom_flow.setDefaultGameImage ((Bitmap)Properties.Resources.DefaultGameImage);
256
257 platform_flow.BackColor = this.BackColor;
258 platform_flow.ForeColor = this.ForeColor;
259
260 rom_flow.BackColor = this.BackColor;
261 rom_flow.ForeColor = this.ForeColor;
262
263 platform_flow.controlsRendered += ControlsRendered;
264 rom_flow.controlsRendered += ControlsRendered;
265
266 platform_flow.switchControls += SwitchControls;
267 rom_flow.switchControls += SwitchControls;
268
269 platform_flow.Parent = this;
270 rom_flow.Parent = this;
271
272
273 platform_flow.Width = this.Width-10;
274 rom_flow.Width = this.Width-10;
275
276 platform_flow.setupScroll ();
277 rom_flow.setupScroll ();
278
279 rom_flow.RomLaunched += Rom_Flow_RomLaunched;
280
281
282 }
283
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
295 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
308
309
310 void ControlsRendered (object sender, EventArgs e)
311 {
312
313 }
314
315 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 rom_flow.Focus ();
324 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 platform_flow.BringToFront ();
331 platform_flow.Focus ();
332 platform_flow.renderControls ();
333 }
334 }
335
336
337
338
339
340 private void Form1_Shown (object sender, EventArgs e)
341 {
342 platform_flow.Visible = true;
343 #if !DISABLE_CURSOR_HIDE
344 Cursor.Hide ();
345 #else
346 Cursor.Show();
347 #endif
348
349 #if !DISABLE_PLUGINS
350 PluginLoader.StartInputPolling (bInputQueueDisabled, Handle_InputPoll);
351 #endif
352
353 }
354
355
356 private void platform_flow_VisibleChanged (object sender, EventArgs e)
357 {
358 if (!platform_flow.Visible) return;
359 platform_flow.renderControls ();
360 }
361
362
363
364 private void rom_flow_VisibleChanged (object sender, EventArgs e)
365 {
366 if (!rom_flow.Visible) return;
367 }
368
369
370 private void Form1_FormClosed (object sender, FormClosedEventArgs e)
371 {
372 Cursor.Show ();
373 PluginLoader.EndInputPolling ();
374 }
375 }
376 }

  ViewVC Help
Powered by ViewVC 1.1.22