BBWIWARG.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(API.Server));
  108. host.Open();
  109. BBIWARG program = new BBIWARG(args);
  110. program.run();
  111. }
  112. /// <summary>
  113. /// Creates the input provider.
  114. /// </summary>
  115. private void createInputProvider()
  116. {
  117. if (Parameters.InputSource == InputType.Movie)
  118. inputProvider = new VideoInputProvider(Parameters.InputMoviePath);
  119. else
  120. inputProvider = new InputProvider();
  121. }
  122. /// <summary>
  123. /// Runs the debug window in its own thread.
  124. /// </summary>
  125. private void debugWindowThreadStart()
  126. {
  127. debugWindow = new DebugWindow(inputProvider, inputHandler, Parameters.DebugWindowTitle, Parameters.DebugWindowUpdateIntervall);
  128. Application.Run(debugWindow);
  129. }
  130. /// <summary>
  131. /// Runs the glasses window in its own thread.
  132. /// </summary>
  133. private void glassesWindowThreadStart()
  134. {
  135. glassesWindow = new GlassesWindow(inputProvider, inputHandler, Parameters.GlassesWindowTitle, Screen.AllScreens[1], Parameters.GlassesWindowUpdateInterval);
  136. Application.Run(glassesWindow);
  137. }
  138. /// <summary>
  139. /// Parses the command line arguments and sets parameters depending on them.
  140. /// </summary>
  141. /// <param name="args">command line arguments</param>
  142. private void handleArgs(String[] args)
  143. {
  144. if (args.Length > 0)
  145. {
  146. tuioEnabled = true;
  147. String ipString = null;
  148. String portString = null;
  149. if (args.Length == 2)
  150. {
  151. ipString = args[0];
  152. portString = args[1];
  153. }
  154. else if (args.Length == 1)
  155. {
  156. String[] parts = args[0].Split(':');
  157. ipString = parts[0];
  158. if (parts.Length == 2)
  159. portString = parts[1];
  160. }
  161. while (!TuioCommunicator.tryParseIPAddress(ipString, out tuioIP))
  162. {
  163. Console.WriteLine("Please insert the TUIO-Client's IP (Default is " + Parameters.TuioDefaultIP + "):");
  164. ipString = Console.ReadLine();
  165. }
  166. Console.WriteLine("TUIO-IP is set to:" + tuioIP);
  167. while (!TuioCommunicator.tryParsePort(portString, out tuioPort))
  168. {
  169. Console.WriteLine("Please insert the TUIO-Client's Port (Default is " + Parameters.TuioDefaultPort + "):");
  170. portString = Console.ReadLine();
  171. }
  172. Console.WriteLine("TUIO-PORT is set to:" + tuioPort);
  173. }
  174. else
  175. {
  176. tuioEnabled = Parameters.TuioEnabledByDefault;
  177. tuioIP = Parameters.TuioDefaultIP;
  178. tuioPort = Parameters.TuioDefaultPort;
  179. }
  180. }
  181. /// <summary>
  182. /// Handles the event that the device has started by starting the enabled windows.
  183. /// </summary>
  184. /// <param name="sender">event sender</param>
  185. /// <param name="e">event arguments</param>
  186. private void handleDeviceStartedEvent(object sender, EventArgs e)
  187. {
  188. if (debugWindowEnabled)
  189. debugWindowThread.Start();
  190. if (glassesWindowEnabled)
  191. glassesWindowThread.Start();
  192. }
  193. }
  194. }