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