2
0

MainWindow.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. // TODO: Input check
  330. m_server_ip = ip_entry.Text;
  331. m_server_port = Convert.ToInt16(port_entry.Text);
  332. // Define the Server address and port
  333. IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(m_server_ip), m_server_port);
  334. // Connect to server non-Blocking method
  335. m_sock.Blocking = false;
  336. m_sock.BeginConnect(epServer, new AsyncCallback(OnConnected), m_sock);
  337. }
  338. catch(Exception e)
  339. {
  340. WriteLog("Unable to connect to " + m_server_ip + ":" + m_server_port + "\n", tagInfo);
  341. }
  342. }
  343. public void OnConnected(IAsyncResult ar)
  344. {
  345. try
  346. {
  347. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket)ar.AsyncState;
  348. sock.EndConnect(ar);
  349. WriteLog("Connected to " + sock.RemoteEndPoint.ToString() + "\n", tagInfo);
  350. // Start receiving stuff
  351. Receive(sock);
  352. }
  353. catch( Exception ex )
  354. {
  355. WriteLog("Unable to connect to " + m_server_ip + ":" + m_server_port + "\n", tagInfo);
  356. }
  357. }
  358. public void Receive(System.Net.Sockets.Socket sock)
  359. {
  360. try
  361. {
  362. sock.BeginReceive(m_msg_header, 0, 4, SocketFlags.None,
  363. new AsyncCallback(OnReceived), sock);
  364. }
  365. catch( Exception ex )
  366. {
  367. Console.Write("Receive failed!");
  368. }
  369. }
  370. public void OnReceived(IAsyncResult ar)
  371. {
  372. try
  373. {
  374. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket)ar.AsyncState;
  375. int nBytesRec = sock.EndReceive(ar);
  376. if(nBytesRec > 0)
  377. {
  378. // Read the message
  379. int bytesRead = sock.Receive(m_byBuff, BitConverter.ToInt32(m_msg_header, 0), SocketFlags.None);
  380. // Wrote the data to the List
  381. string received = Encoding.ASCII.GetString( m_byBuff, 0, bytesRead );
  382. JObject obj = JObject.Parse(received);
  383. if (obj["type"].ToString() == "message")
  384. {
  385. string severity = obj["severity"].ToString();
  386. string message = obj["message"].ToString();
  387. if (severity == "info")
  388. {
  389. WriteLog(message, tagInfo);
  390. }
  391. else if (severity == "warning")
  392. {
  393. WriteLog(message, tagWarning);
  394. }
  395. else if (severity == "error")
  396. {
  397. WriteLog(message, tagError);
  398. }
  399. else if (severity == "debug")
  400. {
  401. WriteLog(message, tagDebug);
  402. }
  403. }
  404. else
  405. {
  406. WriteLog("Unknown response from server\n", tagInfo);
  407. }
  408. // If the connection is still usable restablish the callback
  409. Receive(sock);
  410. }
  411. else
  412. {
  413. // If no data was recieved then the connection is probably dead
  414. WriteLog ("Server closed connection\n", tagInfo);
  415. sock.Shutdown( SocketShutdown.Both );
  416. sock.Close();
  417. }
  418. }
  419. catch( Exception ex )
  420. {
  421. WriteLog ("Unknown error during receive\n", tagInfo);
  422. }
  423. }
  424. protected void WriteLog(string text, TextTag tag)
  425. {
  426. Gtk.Application.Invoke (delegate
  427. {
  428. TextIter endIter = textview1.Buffer.EndIter;
  429. textview1.Buffer.Insert(ref endIter, text);
  430. endIter.BackwardChars(text.Length);
  431. textview1.Buffer.ApplyTag(tag, endIter, textview1.Buffer.EndIter);
  432. textview1.ScrollToMark(textview1.Buffer.CreateMark("bottom", textview1.Buffer.EndIter, false), 0, true, 0.0, 1.0);
  433. });
  434. }
  435. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  436. {
  437. Application.Quit ();
  438. a.RetVal = true;
  439. }
  440. private static void OnSent(IAsyncResult ar)
  441. {
  442. try
  443. {
  444. // Retrieve the socket from the state object.
  445. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket) ar.AsyncState;
  446. // Complete sending the data to the remote device.
  447. sock.EndSend(ar);
  448. }
  449. catch (Exception e)
  450. {
  451. Console.WriteLine(e.ToString());
  452. }
  453. }
  454. protected void Send(string data)
  455. {
  456. try {
  457. m_sock.Send(BitConverter.GetBytes(data.Length));
  458. m_sock.Send(Encoding.ASCII.GetBytes (data));
  459. } catch (Exception e) {
  460. Console.WriteLine (e.ToString ());
  461. }
  462. }
  463. protected void SendScript(String script)
  464. {
  465. string json = "{\"type\":\"script\",\"script\":\"" + script + "\"}";
  466. Send(json);
  467. }
  468. protected void SendCommand(String command)
  469. {
  470. char[] delimiterChars = { ' ', '\t' };
  471. string[] words = command.Split(delimiterChars);
  472. string cmd = words[0];
  473. string resource_type = words[1];
  474. string resource_name = words[2];
  475. string json = "{\"type\":\"command\",\"command\":\"" + cmd + "\","
  476. + "\"resource_type\":" + "\"" + resource_type + "\","
  477. + "\"resource_name\":" + "\"" + resource_name + "\"}";
  478. Send (json);
  479. }
  480. protected void OnEntryActivated (object sender, EventArgs e)
  481. {
  482. string text = entry1.Text;
  483. text = text.Trim ();
  484. // Do processing only if we have text
  485. if (text.Length > 0)
  486. {
  487. // Add command to history
  488. history [history_size % MAX_HISTORY_ITEMS] = text;
  489. history_size++;
  490. history_current = history_size;
  491. // Try to connect before sending any stuff
  492. Connect();
  493. // Sanitize entered text
  494. string safe_text = text.Replace("\"", "\\\"");
  495. if (safe_text[0] == '\\') {
  496. SendCommand (safe_text.Substring(1));
  497. } else {
  498. SendScript (safe_text);
  499. }
  500. // Log entered text
  501. WriteLog ("> " + text + "\n", tagInfo);
  502. }
  503. entry1.Text = "";
  504. }
  505. protected void OnEntryKeyPressed (object o, KeyPressEventArgs args)
  506. {
  507. switch (args.Event.Key)
  508. {
  509. case Gdk.Key.Down:
  510. {
  511. if (history_current < history_size)
  512. {
  513. history_current++;
  514. }
  515. break;
  516. }
  517. case Gdk.Key.Up:
  518. {
  519. if (history_current > 0)
  520. {
  521. history_current--;
  522. }
  523. break;
  524. }
  525. default:
  526. {
  527. return;
  528. }
  529. }
  530. if (history_size == history_current)
  531. {
  532. entry1.Text = "";
  533. }
  534. else
  535. {
  536. entry1.Text = history [history_current % MAX_HISTORY_ITEMS];
  537. entry1.Position = entry1.Text.Length;
  538. }
  539. args.RetVal = true;
  540. }
  541. protected void OnConnectClicked (object sender, EventArgs e)
  542. {
  543. Connect ();
  544. }
  545. }