BBWIWARG.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 TuioCommunicatorHack tuioCommunicatorHack;
  53. private TuioCommunicator tuioCommunicator;
  54. /// <summary>
  55. /// true iff the tuio server is enabled
  56. /// </summary>
  57. private bool tuioEnabled;
  58. /// <summary>
  59. /// the ip address for the tuio server
  60. /// </summary>
  61. private String tuioIP;
  62. /// <summary>
  63. /// the port for the tuio server
  64. /// </summary>
  65. private Int16 tuioPort;
  66. /// <summary>
  67. /// Parses the command line arguments and depending on them creates the input, tuio and window objects.
  68. /// </summary>
  69. /// <param name="args">command line arguments</param>
  70. public BBIWARG(string[] args)
  71. {
  72. handleArgs(args);
  73. debugWindowEnabled = Parameters.DebugWindowEnabled;
  74. glassesWindowEnabled = Parameters.GlassesWindowEnabled && Screen.AllScreens.Count() >= 2;
  75. // inputProvider
  76. createInputProvider();
  77. inputProvider.initialize();
  78. inputProvider.DeviceStartedEvent += handleDeviceStartedEvent;
  79. // inputHandler
  80. inputHandler = new InputHandler(inputProvider);
  81. // tuioCommunicator
  82. if (tuioEnabled)
  83. {
  84. tuioCommunicatorHack = new TuioCommunicatorHack(tuioIP, tuioPort);
  85. inputHandler.NewProcessedFrameEvent += tuioCommunicatorHack.handleNewFrameData;
  86. tuioCommunicator = new TuioCommunicator("127.0.0.1", 3333);
  87. inputHandler.NewProcessedFrameEvent += tuioCommunicator.handleNewFrameData;
  88. }
  89. // debug output
  90. if (debugWindowEnabled)
  91. debugWindowThread = new Thread(new ThreadStart(debugWindowThreadStart));
  92. // glasses output
  93. if (glassesWindowEnabled)
  94. glassesWindowThread = new Thread(new ThreadStart(glassesWindowThreadStart));
  95. }
  96. /// <summary>
  97. /// Runs the main program.
  98. /// </summary>
  99. public void run()
  100. {
  101. inputProvider.start();
  102. }
  103. /// <summary>
  104. /// Starts the program.
  105. /// </summary>
  106. /// <param name="args">command line arguments</param>
  107. private static void Main(string[] args)
  108. {
  109. Console.SetWindowSize(Parameters.ConsoleWidth, Parameters.ConsoleHeight);
  110. System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(API.Server));
  111. host.Open();
  112. BBIWARG program = new BBIWARG(args);
  113. program.run();
  114. }
  115. /// <summary>
  116. /// Creates the input provider.
  117. /// </summary>
  118. private void createInputProvider()
  119. {
  120. if (Parameters.InputSource == InputType.Movie)
  121. inputProvider = new VideoInputProvider(Parameters.InputMoviePath);
  122. else
  123. inputProvider = new InputProvider();
  124. }
  125. /// <summary>
  126. /// Runs the debug window in its own thread.
  127. /// </summary>
  128. private void debugWindowThreadStart()
  129. {
  130. debugWindow = new DebugWindow(inputProvider, inputHandler, Parameters.DebugWindowTitle, Parameters.DebugWindowUpdateIntervall);
  131. Application.Run(debugWindow);
  132. }
  133. /// <summary>
  134. /// Runs the glasses window in its own thread.
  135. /// </summary>
  136. private void glassesWindowThreadStart()
  137. {
  138. glassesWindow = new GlassesWindow(inputProvider, inputHandler, Parameters.GlassesWindowTitle, Screen.AllScreens[1], Parameters.GlassesWindowUpdateInterval);
  139. Application.Run(glassesWindow);
  140. }
  141. /// <summary>
  142. /// Parses the command line arguments and sets parameters depending on them.
  143. /// </summary>
  144. /// <param name="args">command line arguments</param>
  145. private void handleArgs(String[] args)
  146. {
  147. if (args.Length > 0)
  148. {
  149. tuioEnabled = true;
  150. String ipString = null;
  151. String portString = null;
  152. if (args.Length == 2)
  153. {
  154. ipString = args[0];
  155. portString = args[1];
  156. }
  157. else if (args.Length == 1)
  158. {
  159. String[] parts = args[0].Split(':');
  160. ipString = parts[0];
  161. if (parts.Length == 2)
  162. portString = parts[1];
  163. }
  164. while (!TuioCommunicatorHack.tryParseIPAddress(ipString, out tuioIP))
  165. {
  166. Console.WriteLine("Please insert the TUIO-Client's IP (Default is " + Parameters.TuioDefaultIP + "):");
  167. ipString = Console.ReadLine();
  168. }
  169. Console.WriteLine("TUIO-IP is set to:" + tuioIP);
  170. while (!TuioCommunicatorHack.tryParsePort(portString, out tuioPort))
  171. {
  172. Console.WriteLine("Please insert the TUIO-Client's Port (Default is " + Parameters.TuioDefaultPort + "):");
  173. portString = Console.ReadLine();
  174. }
  175. Console.WriteLine("TUIO-PORT is set to:" + tuioPort);
  176. }
  177. else
  178. {
  179. tuioEnabled = Parameters.TuioEnabledByDefault;
  180. tuioIP = Parameters.TuioDefaultIP;
  181. tuioPort = Parameters.TuioDefaultPort;
  182. }
  183. }
  184. /// <summary>
  185. /// Handles the event that the device has started by starting the enabled windows.
  186. /// </summary>
  187. /// <param name="sender">event sender</param>
  188. /// <param name="e">event arguments</param>
  189. private void handleDeviceStartedEvent(object sender, EventArgs e)
  190. {
  191. if (debugWindowEnabled)
  192. debugWindowThread.Start();
  193. if (glassesWindowEnabled)
  194. glassesWindowThread.Start();
  195. }
  196. }
  197. }