Added Support for terminal from filemanager

 * Now We can open terminal from current directory in filemanager

Signed-off-by: Mohd Faraz <androiabledroid@gmail.com>
Change-Id: I6df9ee1222c1fa24105da1002abe3c180be2ded1
Signed-off-by: AndroiableDroid <mohd.faraz.abc@gmail.com>
diff --git a/gui/action.cpp b/gui/action.cpp
index 878f980..b93188f 100755
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -67,6 +67,7 @@
 static int zip_queue_index;
 pid_t sideload_child_pid;
 extern std::vector<users_struct> Users_List;
+extern GUITerminal* term;
 
 static void *ActionThread_work_wrapper(void *data);
 
@@ -205,6 +206,7 @@
 		ADD_ACTION(togglebacklight);
 		ADD_ACTION(enableadb);
 		ADD_ACTION(enablefastboot);
+		ADD_ACTION(changeterminal);
 
 		// remember actions that run in the caller thread
 		for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
@@ -2237,3 +2239,36 @@
 	android::base::SetProperty("sys.usb.config", "fastboot");
 	return 0;
 }
+
+int GUIAction::changeterminal(std::string arg) {
+	bool res = true;
+	std::string resp, cmd = "cd " + arg;
+	DataManager::GetValue("tw_terminal_location", resp);
+	if (arg.empty() && !resp.empty()) {
+		cmd = "cd /";
+		for (uint8_t iter = 0; iter < cmd.size(); iter++)
+			term->NotifyCharInput(cmd.at(iter));
+		term->NotifyCharInput(13);
+		DataManager::SetValue("tw_terminal_location", "");
+		return 0;
+	}
+	if (term != NULL && !arg.empty()) {
+		DataManager::SetValue("tw_terminal_location", arg);
+		if (term->status()) {
+			for (uint8_t iter = 0; iter < cmd.size(); iter++)
+				term->NotifyCharInput(cmd.at(iter));
+			term->NotifyCharInput(13);
+		}
+		else if (chdir(arg.c_str()) != 0) {
+			LOGINFO("Unable to change dir to %s\n", arg.c_str());
+			res = false;
+		}
+	}
+	else {
+		res = false;
+		LOGINFO("Unable to switch to Terminal\n");
+	}
+	if (res)
+		gui_changePage("terminalcommand");
+	return 0;
+}
diff --git a/gui/objects.hpp b/gui/objects.hpp
index 02f9e54..48a2e89 100755
--- a/gui/objects.hpp
+++ b/gui/objects.hpp
@@ -373,6 +373,7 @@
 	int fixabrecoverybootloop(std::string arg);
 	int enableadb(std::string arg);
 	int enablefastboot(std::string arg);
+	int changeterminal(std::string arg);
 	int simulate;
 };
 
@@ -807,6 +808,8 @@
 	virtual size_t GetItemCount();
 	virtual void RenderItem(size_t itemindex, int yPos, bool selected);
 	virtual void NotifySelect(size_t item_selected);
+	bool status();
+	void stop();
 protected:
 	void InitAndResize();
 
diff --git a/gui/pages.cpp b/gui/pages.cpp
index c274c4b..db5ea67 100755
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -60,6 +60,7 @@
 #define TW_THEME_VER_ERR -2
 
 extern int gGuiRunning;
+GUITerminal* term = NULL;
 
 std::map<std::string, PageSet*> PageManager::mPageSets;
 PageSet* PageManager::mCurrentSet;
@@ -382,6 +383,7 @@
 			mRenders.push_back(element);
 			mActions.push_back(element);
 			mInputs.push_back(element);
+			term = element;
 		}
 		else if (type == "button")
 		{
diff --git a/gui/terminal.cpp b/gui/terminal.cpp
index 288a6c3..d7037e1 100755
--- a/gui/terminal.cpp
+++ b/gui/terminal.cpp
@@ -48,7 +48,6 @@
 #endif
 
 extern int g_pty_fd; // in gui.cpp where the select is
-
 /*
 Pseudoterminal handler.
 */
@@ -144,7 +143,7 @@
 			return -1;
 		}
 		int rc = ::write(fdMaster, buffer, size);
-		debug_printf("pty write: %d bytes -> %d\n", size, rc);
+		debug_printf("pty write: %zu bytes -> %d\n", size, rc);
 		if (rc < 0) {
 			LOGERR("pty write failed: %d\n", errno);
 			// assume child has died
@@ -388,6 +387,14 @@
 				output(buffer[i]);
 	}
 
+	bool status() {
+		return pty.started();
+	}
+
+	void stop() {
+		pty.stop();
+	}
+
 	void clear()
 	{
 		cursorX = cursorY = 0;
@@ -901,6 +908,17 @@
 	// do nothing - terminal ignores selections
 }
 
+bool GUITerminal::status()
+{
+	return engine->status();
+}
+
+void GUITerminal::stop()
+{
+	engine->stop();
+	engine->clear();
+}
+
 void GUITerminal::InitAndResize()
 {
 	// make sure the shell is started
diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml
index 65850c0..6693108 100755
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -4275,6 +4275,13 @@
 			</button>
 
 			<button style="main_button_half_width">
+				<condition var1="tw_fm_isfolder" var2="1"/>
+				<placement x="%col2_x_right%" y="%row15a_y%"/>
+				<text>{@fm_open_terminal_btn=Open Terminal Here}</text>
+				<action function="changeterminal">%tw_filename1%</action>
+			</button>
+
+			<button style="main_button_half_width">
 				<placement x="%col2_x_right%" y="%row11_y%"/>
 				<text>{@fm_delete_btn=Delete}</text>
 				<actions>
@@ -4895,6 +4902,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">home</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -4982,6 +4990,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">home</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -5020,6 +5029,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="page">advanced</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -5115,6 +5125,7 @@
 				<actions>
 					<action function="page">main</action>
 					<action function="set">tw_hide_kb=0</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>
 
@@ -5123,6 +5134,7 @@
 				<actions>
 					<action function="page">advanced</action>
 					<action function="set">tw_hide_kb=0</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>
 
@@ -5131,7 +5143,7 @@
 				<action function="screenshot" />
 			</object>
 		</page>
-	
+
 		<page name="fastboot">
 			<template name="page"/>
 
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index 846339d..5d313a3 100755
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -459,6 +459,7 @@
 		<string name="fm_set_perms_hdr">Set Permissions</string>
 		<string name="fm_perms">Permissions:</string>
 		<string name="fm_complete">File Operation Complete</string>
+		<string name="fm_open_terminal_btn">Open Terminal Here</string>
 		<string name="decrypt_data_hdr">Decrypt Data</string>
 		<string name="decrypt_data_enter_pass">Enter Password.</string>
 		<string name="decrypt_data_failed">Password failed, please try again!</string>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index b669693..cd2f3be 100755
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -4340,6 +4340,13 @@
 			</text>
 
 			<button style="main_button_half_height">
+				<condition var1="tw_fm_isfolder" var2="1"/>
+				<placement x="%center_x%" y="%row12a_y%"/>
+				<text>{@fm_open_terminal_btn=Open Terminal Here}</text>
+				<action function="changeterminal">%tw_filename1%</action>
+			</button>
+
+			<button style="main_button_half_height">
 				<condition var1="tw_fm_isfolder" var2="0"/>
 				<placement x="%indent%" y="%row15a_y%"/>
 				<text>{@fm_copy_file_btn=Copy File}</text>
@@ -5023,6 +5030,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">home</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -5058,6 +5066,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">back</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -5066,6 +5075,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="page">main</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>
 
@@ -5074,6 +5084,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="page">advanced</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>