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