BBWIWARG.cs 7.5 KB

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