BBWIWARG.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Input.InputProviding;
  3. using BBIWARG.Output.DebugOutput;
  4. using BBIWARG.Output.GlassesOutput;
  5. using BBIWARG.TUIO;
  6. using System;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. namespace BBIWARG
  11. {
  12. /// <summary>
  13. /// Main class which creates and starts the input, tuio and window objects.
  14. /// </summary>
  15. internal class BBIWARG
  16. {
  17. /// <summary>
  18. /// the debug window
  19. /// </summary>
  20. private DebugWindow debugWindow;
  21. /// <summary>
  22. /// true iff the debug window is enabled
  23. /// </summary>
  24. private bool debugWindowEnabled;
  25. /// <summary>
  26. /// the thread the debug window runs in
  27. /// </summary>
  28. private Thread debugWindowThread;
  29. /// <summary>
  30. /// the glasses window
  31. /// </summary>
  32. private GlassesWindow glassesWindow;
  33. /// <summary>
  34. /// true iff the glasses window is enabled
  35. /// </summary>
  36. private bool glassesWindowEnabled;
  37. /// <summary>
  38. /// the thread the glasses window runs in
  39. /// </summary>
  40. private Thread glassesWindowThread;
  41. /// <summary>
  42. /// the input handler
  43. /// </summary>
  44. private InputHandler inputHandler;
  45. /// <summary>
  46. /// the input provider
  47. /// </summary>
  48. private InputProvider inputProvider;
  49. /// <summary>
  50. /// the tuio communicator
  51. /// </summary>
  52. private TuioCommunicator tuioCommunicator;
  53. /// <summary>
  54. /// true iff the tuio server is enabled
  55. /// </summary>
  56. private bool tuioEnabled;
  57. /// <summary>
  58. /// the ip address for the tuio server
  59. /// </summary>
  60. private String tuioIP;
  61. /// <summary>
  62. /// the port for the tuio server
  63. /// </summary>
  64. private Int16 tuioPort;
  65. /// <summary>
  66. /// Parses the command line arguments and depending on them creates the input, tuio and window objects.
  67. /// </summary>
  68. /// <param name="args">command line arguments</param>
  69. public BBIWARG(string[] args)
  70. {
  71. handleArgs(args);
  72. debugWindowEnabled = Parameters.DebugWindowEnabled;
  73. glassesWindowEnabled = Parameters.GlassesWindowEnabled && Screen.AllScreens.Count() >= 2;
  74. // inputProvider
  75. createInputProvider();
  76. inputProvider.initialize();
  77. inputProvider.DeviceStartedEvent += handleDeviceStartedEvent;
  78. // inputHandler
  79. inputHandler = new InputHandler(inputProvider);
  80. // tuioCommunicator
  81. if (tuioEnabled)
  82. {
  83. tuioCommunicator = new TuioCommunicator(tuioIP, tuioPort);
  84. inputHandler.NewProcessedFrameEvent += tuioCommunicator.handleNewFrameData;
  85. }
  86. // debug output
  87. if (debugWindowEnabled)
  88. debugWindowThread = new Thread(new ThreadStart(debugWindowThreadStart));
  89. // glasses output
  90. if (glassesWindowEnabled)
  91. glassesWindowThread = new Thread(new ThreadStart(glassesWindowThreadStart));
  92. }
  93. /// <summary>
  94. /// Runs the main program.
  95. /// </summary>
  96. public void run()
  97. {
  98. inputProvider.start();
  99. }
  100. /// <summary>
  101. /// Starts the program.
  102. /// </summary>
  103. /// <param name="args">command line arguments</param>
  104. private static void Main(string[] args)
  105. {
  106. Console.SetWindowSize(Parameters.ConsoleWidth, Parameters.ConsoleHeight);
  107. BBIWARG program = new BBIWARG(args);
  108. program.run();
  109. }
  110. /// <summary>
  111. /// Creates the input provider.
  112. /// </summary>
  113. private void createInputProvider()
  114. {
  115. if (Parameters.InputSource == InputType.Movie)
  116. inputProvider = new VideoInputProvider(Parameters.InputMoviePath);
  117. else
  118. inputProvider = new InputProvider();
  119. }
  120. /// <summary>
  121. /// Runs the debug window in its own thread.
  122. /// </summary>
  123. private void debugWindowThreadStart()
  124. {
  125. debugWindow = new DebugWindow(inputProvider, inputHandler, Parameters.DebugWindowTitle, Parameters.DebugWindowUpdateIntervall);
  126. Application.Run(debugWindow);
  127. }
  128. /// <summary>
  129. /// Runs the glasses window in its own thread.
  130. /// </summary>
  131. private void glassesWindowThreadStart()
  132. {
  133. glassesWindow = new GlassesWindow(inputProvider, inputHandler, Parameters.GlassesWindowTitle, Screen.AllScreens[1], Parameters.GlassesWindowUpdateInterval);
  134. Application.Run(glassesWindow);
  135. }
  136. /// <summary>
  137. /// Parses the command line arguments and sets parameters depending on them.
  138. /// </summary>
  139. /// <param name="args">command line arguments</param>
  140. private void handleArgs(String[] args)
  141. {
  142. if (args.Length > 0)
  143. {
  144. tuioEnabled = true;
  145. String ipString = null;
  146. String portString = null;
  147. if (args.Length == 2)
  148. {
  149. ipString = args[0];
  150. portString = args[1];
  151. }
  152. else if (args.Length == 1)
  153. {
  154. String[] parts = args[0].Split(':');
  155. ipString = parts[0];
  156. if (parts.Length == 2)
  157. portString = parts[1];
  158. }
  159. while (!TuioCommunicator.tryParseIPAddress(ipString, out tuioIP))
  160. {
  161. Console.WriteLine("Please insert the TUIO-Client's IP (Default is " + Parameters.TuioDefaultIP + "):");
  162. ipString = Console.ReadLine();
  163. }
  164. Console.WriteLine("TUIO-IP is set to:" + tuioIP);
  165. while (!TuioCommunicator.tryParsePort(portString, out tuioPort))
  166. {
  167. Console.WriteLine("Please insert the TUIO-Client's Port (Default is " + Parameters.TuioDefaultPort + "):");
  168. portString = Console.ReadLine();
  169. }
  170. Console.WriteLine("TUIO-PORT is set to:" + tuioPort);
  171. }
  172. else
  173. {
  174. tuioEnabled = Parameters.TuioEnabledByDefault;
  175. tuioIP = Parameters.TuioDefaultIP;
  176. tuioPort = Parameters.TuioDefaultPort;
  177. }
  178. }
  179. /// <summary>
  180. /// Handles the event that the device has started by starting the enabled windows.
  181. /// </summary>
  182. /// <param name="sender">event sender</param>
  183. /// <param name="e">event arguments</param>
  184. private void handleDeviceStartedEvent(object sender, EventArgs e)
  185. {
  186. if (debugWindowEnabled)
  187. debugWindowThread.Start();
  188. if (glassesWindowEnabled)
  189. glassesWindowThread.Start();
  190. }
  191. }
  192. }