BBWIWARG.cs 7.1 KB

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