MainWindow.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. using System;
  2. using Gtk;
  3. using Gdk;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Text;
  8. using Newtonsoft.Json.Linq;
  9. public partial class MainWindow: Gtk.Window
  10. {
  11. private System.Net.Sockets.Socket m_sock = null;
  12. private TextTag tagInfo;
  13. private TextTag tagWarning;
  14. private TextTag tagError;
  15. private TextTag tagDebug;
  16. // Console history
  17. private const uint MAX_HISTORY_ITEMS = 256;
  18. private uint history_size = 0;
  19. private uint history_current = 0;
  20. private string[] history = new string[MAX_HISTORY_ITEMS];
  21. // Connection
  22. private string m_server_ip;
  23. private int m_server_port;
  24. private byte[] m_msg_header = new byte[4];
  25. private byte[] m_byBuff = new byte[4096]; // Recieved data buffer
  26. public MainWindow (): base (Gtk.WindowType.Toplevel)
  27. {
  28. Build ();
  29. // Connection
  30. m_server_ip = "127.0.0.1";
  31. m_server_port = 10001;
  32. // Create tags for color-formatted text
  33. tagInfo = new Gtk.TextTag ("info");
  34. tagInfo.BackgroundGdk = new Gdk.Color (255, 255, 255);
  35. tagWarning = new Gtk.TextTag ("warning");
  36. tagWarning.BackgroundGdk = new Gdk.Color (255, 255, 153);
  37. tagError = new Gtk.TextTag ("error");
  38. tagError.BackgroundGdk = new Gdk.Color (255, 153, 153);
  39. tagDebug = new Gtk.TextTag ("debug");
  40. tagDebug.BackgroundGdk = new Gdk.Color (224, 224, 224);
  41. TextBuffer textbuffer1 = textview1.Buffer;
  42. textbuffer1.TagTable.Add (tagInfo);
  43. textbuffer1.TagTable.Add (tagWarning);
  44. textbuffer1.TagTable.Add (tagError);
  45. textbuffer1.TagTable.Add (tagDebug);
  46. // Create completion dictionary
  47. ListStore lua_api = new ListStore (typeof (string));
  48. lua_api.AppendValues ("Device.frame_count");
  49. lua_api.AppendValues ("Device.last_delta_time");
  50. lua_api.AppendValues ("Device.start");
  51. lua_api.AppendValues ("Device.stop");
  52. lua_api.AppendValues ("Device.create_resource_package");
  53. lua_api.AppendValues ("Device.destroy_resource_package");
  54. lua_api.AppendValues ("Window.show");
  55. lua_api.AppendValues ("Window.hide");
  56. lua_api.AppendValues ("Window.get_size");
  57. lua_api.AppendValues ("Window.get_position");
  58. lua_api.AppendValues ("Window.resize");
  59. lua_api.AppendValues ("Window.move");
  60. lua_api.AppendValues ("Window.minimize");
  61. lua_api.AppendValues ("Window.restore");
  62. lua_api.AppendValues ("Window.is_resizable");
  63. lua_api.AppendValues ("Window.set_resizable");
  64. lua_api.AppendValues ("Window.show_cursor");
  65. lua_api.AppendValues ("Window.get_cursor_xy");
  66. lua_api.AppendValues ("Window.set_cursor_xy");
  67. lua_api.AppendValues ("Window.title");
  68. lua_api.AppendValues ("Window.set_title");
  69. lua_api.AppendValues ("Math.deg_to_rad");
  70. lua_api.AppendValues ("Math.rad_to_deg");
  71. lua_api.AppendValues ("Math.next_pow_2");
  72. lua_api.AppendValues ("Math.is_pow_2");
  73. lua_api.AppendValues ("Math.ceil");
  74. lua_api.AppendValues ("Math.floor");
  75. lua_api.AppendValues ("Math.sqrt");
  76. lua_api.AppendValues ("Math.inv_sqrt");
  77. lua_api.AppendValues ("Math.sin");
  78. lua_api.AppendValues ("Math.cos");
  79. lua_api.AppendValues ("Math.asin");
  80. lua_api.AppendValues ("Math.acos");
  81. lua_api.AppendValues ("Math.tan");
  82. lua_api.AppendValues ("Math.atan2");
  83. lua_api.AppendValues ("Math.abs");
  84. lua_api.AppendValues ("Math.fmod");
  85. lua_api.AppendValues ("Vector2.new");
  86. lua_api.AppendValues ("Vector2.val");
  87. lua_api.AppendValues ("Vector2.add");
  88. lua_api.AppendValues ("Vector2.sub");
  89. lua_api.AppendValues ("Vector2.mul");
  90. lua_api.AppendValues ("Vector2.div");
  91. lua_api.AppendValues ("Vector2.dot");
  92. lua_api.AppendValues ("Vector2.equals");
  93. lua_api.AppendValues ("Vector2.lower");
  94. lua_api.AppendValues ("Vector2.greater");
  95. lua_api.AppendValues ("Vector2.length");
  96. lua_api.AppendValues ("Vector2.squared_length");
  97. lua_api.AppendValues ("Vector2.set_length");
  98. lua_api.AppendValues ("Vector2.normalize");
  99. lua_api.AppendValues ("Vector2.negate");
  100. lua_api.AppendValues ("Vector2.get_distance_to");
  101. lua_api.AppendValues ("Vector2.get_angle_between");
  102. lua_api.AppendValues ("Vector2.zero");
  103. lua_api.AppendValues ("Vector3.new");
  104. lua_api.AppendValues ("Vector3.val");
  105. lua_api.AppendValues ("Vector3.add");
  106. lua_api.AppendValues ("Vector3.sub");
  107. lua_api.AppendValues ("Vector3.mul");
  108. lua_api.AppendValues ("Vector3.div");
  109. lua_api.AppendValues ("Vector3.dot");
  110. lua_api.AppendValues ("Vector3.cross");
  111. lua_api.AppendValues ("Vector3.equals");
  112. lua_api.AppendValues ("Vector3.lower");
  113. lua_api.AppendValues ("Vector3.greater");
  114. lua_api.AppendValues ("Vector3.length");
  115. lua_api.AppendValues ("Vector3.squared_length");
  116. lua_api.AppendValues ("Vector3.set_length");
  117. lua_api.AppendValues ("Vector3.normalize");
  118. lua_api.AppendValues ("Vector3.negate");
  119. lua_api.AppendValues ("Vector3.get_distance_to");
  120. lua_api.AppendValues ("Vector3.get_angle_between");
  121. lua_api.AppendValues ("Vector3.zero");
  122. lua_api.AppendValues ("Quaternion.new");
  123. lua_api.AppendValues ("Quaternion.negate");
  124. lua_api.AppendValues ("Quaternion.load_identity");
  125. lua_api.AppendValues ("Quaternion.length");
  126. lua_api.AppendValues ("Quaternion.conjugate");
  127. lua_api.AppendValues ("Quaternion.inverse");
  128. lua_api.AppendValues ("Quaternion.cross");
  129. lua_api.AppendValues ("Quaternion.mul");
  130. lua_api.AppendValues ("Quaternion.pow");
  131. lua_api.AppendValues ("StringSetting.value");
  132. lua_api.AppendValues ("StringSetting.synopsis");
  133. lua_api.AppendValues ("StringSetting.update");
  134. lua_api.AppendValues ("IntSetting.value");
  135. lua_api.AppendValues ("IntSetting.synopsis");
  136. lua_api.AppendValues ("IntSetting.min");
  137. lua_api.AppendValues ("IntSetting.max");
  138. lua_api.AppendValues ("IntSetting.update");
  139. lua_api.AppendValues ("FloatSetting.value");
  140. lua_api.AppendValues ("FloatSetting.synopsis");
  141. lua_api.AppendValues ("FloatSetting.min");
  142. lua_api.AppendValues ("FloatSetting.max");
  143. lua_api.AppendValues ("FloatSetting.update");
  144. lua_api.AppendValues ("Mouse.button_pressed");
  145. lua_api.AppendValues ("Mouse.button_released");
  146. lua_api.AppendValues ("Mouse.any_pressed");
  147. lua_api.AppendValues ("Mouse.any_released");
  148. lua_api.AppendValues ("Mouse.cursor_xy");
  149. lua_api.AppendValues ("Mouse.set_cursor_xy");
  150. lua_api.AppendValues ("Mouse.cursor_relative_xy");
  151. lua_api.AppendValues ("Mouse.set_cursor_relative_xy");
  152. lua_api.AppendValues ("Mouse.MB_LEFT");
  153. lua_api.AppendValues ("Mouse.KB_MIDDLE");
  154. lua_api.AppendValues ("Mouse.MB_RIGHT");
  155. lua_api.AppendValues ("Keyboard.modifier_pressed");
  156. lua_api.AppendValues ("Keyboard.button_pressed");
  157. lua_api.AppendValues ("Keyboard.button_released");
  158. lua_api.AppendValues ("Keyboard.any_pressed");
  159. lua_api.AppendValues ("Keyboard.any_released");
  160. lua_api.AppendValues ("Keyboard.TAB");
  161. lua_api.AppendValues ("Keyboard.ENTER");
  162. lua_api.AppendValues ("Keyboard.ESCAPE");
  163. lua_api.AppendValues ("Keyboard.SPACE");
  164. lua_api.AppendValues ("Keyboard.BACKSPACE");
  165. lua_api.AppendValues ("Keyboard.KP_0");
  166. lua_api.AppendValues ("Keyboard.KP_1");
  167. lua_api.AppendValues ("Keyboard.KP_2");
  168. lua_api.AppendValues ("Keyboard.KP_3");
  169. lua_api.AppendValues ("Keyboard.KP_4");
  170. lua_api.AppendValues ("Keyboard.KP_5");
  171. lua_api.AppendValues ("Keyboard.KP_6");
  172. lua_api.AppendValues ("Keyboard.KP_7");
  173. lua_api.AppendValues ("Keyboard.KP_8");
  174. lua_api.AppendValues ("Keyboard.KP_9");
  175. lua_api.AppendValues ("Keyboard.F1");
  176. lua_api.AppendValues ("Keyboard.F2");
  177. lua_api.AppendValues ("Keyboard.F3");
  178. lua_api.AppendValues ("Keyboard.F4");
  179. lua_api.AppendValues ("Keyboard.F5");
  180. lua_api.AppendValues ("Keyboard.F6");
  181. lua_api.AppendValues ("Keyboard.F7");
  182. lua_api.AppendValues ("Keyboard.F8");
  183. lua_api.AppendValues ("Keyboard.F9");
  184. lua_api.AppendValues ("Keyboard.F10");
  185. lua_api.AppendValues ("Keyboard.F11");
  186. lua_api.AppendValues ("Keyboard.F12");
  187. lua_api.AppendValues ("Keyboard.HOME");
  188. lua_api.AppendValues ("Keyboard.LEFT");
  189. lua_api.AppendValues ("Keyboard.UP");
  190. lua_api.AppendValues ("Keyboard.RIGHT");
  191. lua_api.AppendValues ("Keyboard.DOWN");
  192. lua_api.AppendValues ("Keyboard.PAGE_UP");
  193. lua_api.AppendValues ("Keyboard.PAGE_DOWN");
  194. lua_api.AppendValues ("Keyboard.LCONTROL");
  195. lua_api.AppendValues ("Keyboard.RCONTROL");
  196. lua_api.AppendValues ("Keyboard.LSHIFT");
  197. lua_api.AppendValues ("Keyboard.RSHIFT");
  198. lua_api.AppendValues ("Keyboard.CAPS_LOCK");
  199. lua_api.AppendValues ("Keyboard.LALT");
  200. lua_api.AppendValues ("Keyboard.RALT");
  201. lua_api.AppendValues ("Keyboard.LSUPER");
  202. lua_api.AppendValues ("Keyboard.RSUPER");
  203. lua_api.AppendValues ("Keyboard.NUM_0");
  204. lua_api.AppendValues ("Keyboard.NUM_1");
  205. lua_api.AppendValues ("Keyboard.NUM_2");
  206. lua_api.AppendValues ("Keyboard.NUM_3");
  207. lua_api.AppendValues ("Keyboard.NUM_4");
  208. lua_api.AppendValues ("Keyboard.NUM_5");
  209. lua_api.AppendValues ("Keyboard.NUM_6");
  210. lua_api.AppendValues ("Keyboard.NUM_7");
  211. lua_api.AppendValues ("Keyboard.NUM_8");
  212. lua_api.AppendValues ("Keyboard.NUM_9");
  213. lua_api.AppendValues ("Keyboard.A");
  214. lua_api.AppendValues ("Keyboard.B");
  215. lua_api.AppendValues ("Keyboard.C");
  216. lua_api.AppendValues ("Keyboard.D");
  217. lua_api.AppendValues ("Keyboard.E");
  218. lua_api.AppendValues ("Keyboard.F");
  219. lua_api.AppendValues ("Keyboard.G");
  220. lua_api.AppendValues ("Keyboard.H");
  221. lua_api.AppendValues ("Keyboard.I");
  222. lua_api.AppendValues ("Keyboard.J");
  223. lua_api.AppendValues ("Keyboard.K");
  224. lua_api.AppendValues ("Keyboard.L");
  225. lua_api.AppendValues ("Keyboard.M");
  226. lua_api.AppendValues ("Keyboard.N");
  227. lua_api.AppendValues ("Keyboard.O");
  228. lua_api.AppendValues ("Keyboard.P");
  229. lua_api.AppendValues ("Keyboard.Q");
  230. lua_api.AppendValues ("Keyboard.R");
  231. lua_api.AppendValues ("Keyboard.S");
  232. lua_api.AppendValues ("Keyboard.T");
  233. lua_api.AppendValues ("Keyboard.U");
  234. lua_api.AppendValues ("Keyboard.V");
  235. lua_api.AppendValues ("Keyboard.W");
  236. lua_api.AppendValues ("Keyboard.X");
  237. lua_api.AppendValues ("Keyboard.Y");
  238. lua_api.AppendValues ("Keyboard.Z");
  239. lua_api.AppendValues ("Keyboard.a");
  240. lua_api.AppendValues ("Keyboard.b");
  241. lua_api.AppendValues ("Keyboard.c");
  242. lua_api.AppendValues ("Keyboard.d");
  243. lua_api.AppendValues ("Keyboard.e");
  244. lua_api.AppendValues ("Keyboard.f");
  245. lua_api.AppendValues ("Keyboard.g");
  246. lua_api.AppendValues ("Keyboard.h");
  247. lua_api.AppendValues ("Keyboard.i");
  248. lua_api.AppendValues ("Keyboard.j");
  249. lua_api.AppendValues ("Keyboard.k");
  250. lua_api.AppendValues ("Keyboard.l");
  251. lua_api.AppendValues ("Keyboard.m");
  252. lua_api.AppendValues ("Keyboard.n");
  253. lua_api.AppendValues ("Keyboard.o");
  254. lua_api.AppendValues ("Keyboard.p");
  255. lua_api.AppendValues ("Keyboard.q");
  256. lua_api.AppendValues ("Keyboard.r");
  257. lua_api.AppendValues ("Keyboard.s");
  258. lua_api.AppendValues ("Keyboard.t");
  259. lua_api.AppendValues ("Keyboard.u");
  260. lua_api.AppendValues ("Keyboard.v");
  261. lua_api.AppendValues ("Keyboard.w");
  262. lua_api.AppendValues ("Keyboard.x");
  263. lua_api.AppendValues ("Keyboard.y");
  264. lua_api.AppendValues ("Keyboard.z");
  265. lua_api.AppendValues ("ResourcePackage.load");
  266. lua_api.AppendValues ("ResourcePackage.unload");
  267. lua_api.AppendValues ("ResourcePackage.flush");
  268. lua_api.AppendValues ("ResourcePackage.has_loaded");
  269. lua_api.AppendValues("Camera.local_position");
  270. lua_api.AppendValues("Camera.local_rotation");
  271. lua_api.AppendValues("Camera.local_pose");
  272. lua_api.AppendValues("Camera.world_position");
  273. lua_api.AppendValues("Camera.world_rotation");
  274. lua_api.AppendValues("Camera.world_pose");
  275. lua_api.AppendValues("Camera.set_local_position");
  276. lua_api.AppendValues("Camera.set_local_rotation");
  277. lua_api.AppendValues("Camera.set_local_pose");
  278. lua_api.AppendValues("Camera.set_projection_type");
  279. lua_api.AppendValues("Camera.projection_type");
  280. lua_api.AppendValues("Camera.fov");
  281. lua_api.AppendValues("Camera.set_fov");
  282. lua_api.AppendValues("Camera.aspect");
  283. lua_api.AppendValues("Camera.set_aspect");
  284. lua_api.AppendValues("Camera.near_clip_distance");
  285. lua_api.AppendValues("Camera.set_near_clip_distance");
  286. lua_api.AppendValues("Camera.far_clip_distance");
  287. lua_api.AppendValues("Camera.set_far_clip_distance");
  288. lua_api.AppendValues("Mesh.local_position");
  289. lua_api.AppendValues("Mesh.local_rotation");
  290. lua_api.AppendValues("Mesh.local_pose");
  291. lua_api.AppendValues("Mesh.world_position");
  292. lua_api.AppendValues("Mesh.world_rotation");
  293. lua_api.AppendValues("Mesh.world_pose");
  294. lua_api.AppendValues("Mesh.set_local_position");
  295. lua_api.AppendValues("Mesh.set_local_rotation");
  296. lua_api.AppendValues("Mesh.set_local_pose");
  297. lua_api.AppendValues("Unit.local_position");
  298. lua_api.AppendValues("Unit.local_rotation");
  299. lua_api.AppendValues("Unit.local_pose");
  300. lua_api.AppendValues("Unit.world_position");
  301. lua_api.AppendValues("Unit.world_rotation");
  302. lua_api.AppendValues("Unit.world_pose");
  303. lua_api.AppendValues("Unit.set_local_position");
  304. lua_api.AppendValues("Unit.set_local_rotation");
  305. lua_api.AppendValues("Unit.set_local_pose");
  306. lua_api.AppendValues("World.spawn_unit");
  307. lua_api.AppendValues("World.play_sound");
  308. lua_api.AppendValues("World.pause_sound");
  309. lua_api.AppendValues("World.link_sound");
  310. lua_api.AppendValues("World.set_listener");
  311. lua_api.AppendValues("World.set_sound_position");
  312. lua_api.AppendValues("World.set_sound_range");
  313. lua_api.AppendValues("World.set_sound_volume");
  314. entry1.Completion = new EntryCompletion ();
  315. entry1.Completion.Model = lua_api;
  316. entry1.Completion.TextColumn = 0;
  317. }
  318. public void Connect()
  319. {
  320. try
  321. {
  322. // Do nothing if connected
  323. if( m_sock != null && m_sock.Connected )
  324. {
  325. return;
  326. }
  327. // Try to connect
  328. m_sock = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  329. // Define the Server address and port
  330. IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(m_server_ip), m_server_port);
  331. // Connect to server non-Blocking method
  332. m_sock.Blocking = false;
  333. m_sock.BeginConnect(epServer, new AsyncCallback(OnConnected), m_sock);
  334. }
  335. catch(Exception e)
  336. {
  337. WriteLog("Unable to connect to " + m_server_ip + ":" + m_server_port + "\n", tagInfo);
  338. }
  339. }
  340. public void OnConnected(IAsyncResult ar)
  341. {
  342. try
  343. {
  344. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket)ar.AsyncState;
  345. sock.EndConnect(ar);
  346. WriteLog("Connected to " + sock.RemoteEndPoint.ToString() + "\n", tagInfo);
  347. // Start receiving stuff
  348. Receive(sock);
  349. }
  350. catch( Exception ex )
  351. {
  352. WriteLog("Unable to connect to " + m_server_ip + ":" + m_server_port + "\n", tagInfo);
  353. }
  354. }
  355. public void Receive(System.Net.Sockets.Socket sock)
  356. {
  357. try
  358. {
  359. sock.BeginReceive(m_msg_header, 0, 4, SocketFlags.None,
  360. new AsyncCallback(OnReceived), sock);
  361. }
  362. catch( Exception ex )
  363. {
  364. Console.Write("Receive failed!");
  365. }
  366. }
  367. public void OnReceived(IAsyncResult ar)
  368. {
  369. try
  370. {
  371. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket)ar.AsyncState;
  372. int nBytesRec = sock.EndReceive(ar);
  373. if(nBytesRec > 0)
  374. {
  375. // Read the message
  376. int bytesRead = sock.Receive(m_byBuff, BitConverter.ToInt32(m_msg_header, 0), SocketFlags.None);
  377. // Wrote the data to the List
  378. string received = Encoding.ASCII.GetString( m_byBuff, 0, bytesRead );
  379. JObject obj = JObject.Parse(received);
  380. if (obj["type"].ToString() == "message")
  381. {
  382. string severity = obj["severity"].ToString();
  383. string message = obj["message"].ToString();
  384. if (severity == "info")
  385. {
  386. WriteLog(message, tagInfo);
  387. }
  388. else if (severity == "warning")
  389. {
  390. WriteLog(message, tagWarning);
  391. }
  392. else if (severity == "error")
  393. {
  394. WriteLog(message, tagError);
  395. }
  396. else if (severity == "debug")
  397. {
  398. WriteLog(message, tagDebug);
  399. }
  400. }
  401. else
  402. {
  403. WriteLog("Unknown response from server\n", tagInfo);
  404. }
  405. // If the connection is still usable restablish the callback
  406. Receive(sock);
  407. }
  408. else
  409. {
  410. // If no data was recieved then the connection is probably dead
  411. WriteLog ("Server closed connection\n", tagInfo);
  412. sock.Shutdown( SocketShutdown.Both );
  413. sock.Close();
  414. }
  415. }
  416. catch( Exception ex )
  417. {
  418. WriteLog ("Unknown error during receive\n", tagInfo);
  419. }
  420. }
  421. protected void WriteLog(string text, TextTag tag)
  422. {
  423. Gtk.Application.Invoke (delegate
  424. {
  425. TextIter endIter = textview1.Buffer.EndIter;
  426. textview1.Buffer.Insert(ref endIter, text);
  427. endIter.BackwardChars(text.Length);
  428. textview1.Buffer.ApplyTag(tag, endIter, textview1.Buffer.EndIter);
  429. textview1.ScrollToMark(textview1.Buffer.CreateMark("bottom", textview1.Buffer.EndIter, false), 0, true, 0.0, 1.0);
  430. });
  431. }
  432. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  433. {
  434. Application.Quit ();
  435. a.RetVal = true;
  436. }
  437. private static void OnSent(IAsyncResult ar)
  438. {
  439. try
  440. {
  441. // Retrieve the socket from the state object.
  442. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket) ar.AsyncState;
  443. // Complete sending the data to the remote device.
  444. sock.EndSend(ar);
  445. }
  446. catch (Exception e)
  447. {
  448. Console.WriteLine(e.ToString());
  449. }
  450. }
  451. protected void Send(string data)
  452. {
  453. try {
  454. m_sock.Send(BitConverter.GetBytes(data.Length));
  455. m_sock.Send(Encoding.ASCII.GetBytes (data));
  456. } catch (Exception e) {
  457. Console.WriteLine (e.ToString ());
  458. }
  459. }
  460. protected void SendScript(String script)
  461. {
  462. string json = "{\"type\":\"script\",\"script\":\"" + script + "\"}";
  463. Send(json);
  464. }
  465. protected void SendCommand(String command)
  466. {
  467. char[] delimiterChars = { ' ', '\t' };
  468. string[] words = command.Split(delimiterChars);
  469. string cmd = words[0];
  470. string resource_type = words[1];
  471. string resource_name = words[2];
  472. string json = "{\"type\":\"command\",\"command\":\"" + cmd + "\","
  473. + "\"resource_type\":" + "\"" + resource_type + "\","
  474. + "\"resource_name\":" + "\"" + resource_name + "\"}";
  475. Send (json);
  476. }
  477. protected void OnConnectActivated (object sender, EventArgs e)
  478. {
  479. Connect ();
  480. }
  481. protected void OnEntryActivated (object sender, EventArgs e)
  482. {
  483. string text = entry1.Text;
  484. text = text.Trim ();
  485. // Do processing only if we have text
  486. if (text.Length > 0)
  487. {
  488. // Add command to history
  489. history [history_size % MAX_HISTORY_ITEMS] = text;
  490. history_size++;
  491. history_current = history_size;
  492. // Try to connect before sending any stuff
  493. Connect();
  494. // Sanitize entered text
  495. string safe_text = text.Replace("\"", "\\\"");
  496. if (safe_text[0] == '\\') {
  497. SendCommand (safe_text.Substring(1));
  498. } else {
  499. SendScript (safe_text);
  500. }
  501. // Log entered text
  502. WriteLog ("> " + text + "\n", tagInfo);
  503. }
  504. entry1.Text = "";
  505. }
  506. protected void OnEntryKeyPressed (object o, KeyPressEventArgs args)
  507. {
  508. switch (args.Event.Key)
  509. {
  510. case Gdk.Key.Down:
  511. {
  512. if (history_current < history_size)
  513. {
  514. history_current++;
  515. }
  516. break;
  517. }
  518. case Gdk.Key.Up:
  519. {
  520. if (history_current > 0)
  521. {
  522. history_current--;
  523. }
  524. break;
  525. }
  526. default:
  527. {
  528. return;
  529. }
  530. }
  531. if (history_size == history_current)
  532. {
  533. entry1.Text = "";
  534. }
  535. else
  536. {
  537. entry1.Text = history [history_current % MAX_HISTORY_ITEMS];
  538. entry1.Position = entry1.Text.Length;
  539. }
  540. args.RetVal = true;
  541. }
  542. }