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 06de41e..1ab4ee4 100755
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -64,6 +64,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);
 
@@ -200,6 +201,7 @@
 		ADD_ACTION(setlanguage);
 		ADD_ACTION(checkforapp);
 		ADD_ACTION(togglebacklight);
+		ADD_ACTION(changeterminal);
 
 		// remember actions that run in the caller thread
 		for (mapFunc::const_iterator it = mf.begin(); it != mf.end(); ++it)
@@ -2218,3 +2220,36 @@
 	operation_end(op_status);
 	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 24e9d24..9ef1b65 100644
--- a/gui/objects.hpp
+++ b/gui/objects.hpp
@@ -371,6 +371,7 @@
 	int uninstalltwrpsystemapp(std::string arg);
 	int repackimage(std::string arg);
 	int fixabrecoverybootloop(std::string arg);
+	int changeterminal(std::string arg);
 
 	int simulate;
 };
@@ -805,6 +806,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 366bc39..989da9a 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -63,6 +63,7 @@
 #define TW_THEME_VER_ERR -2
 
 extern int gGuiRunning;
+GUITerminal* term = NULL;
 
 std::map<std::string, PageSet*> PageManager::mPageSets;
 PageSet* PageManager::mCurrentSet;
@@ -385,6 +386,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 a4fed91..79262eb 100755
--- a/gui/terminal.cpp
+++ b/gui/terminal.cpp
@@ -46,7 +46,6 @@
 #endif
 
 extern int g_pty_fd; // in gui.cpp where the select is
-
 /*
 Pseudoterminal handler.
 */
@@ -142,7 +141,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
@@ -386,6 +385,14 @@
 				output(buffer[i]);
 	}
 
+	bool status() {
+		return pty.started();
+	}
+
+	void stop() {
+		pty.stop();
+	}
+
 	void clear()
 	{
 		cursorX = cursorY = 0;
@@ -899,6 +906,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 010e08e..17977e2 100755
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -4123,6 +4123,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>
@@ -4743,6 +4750,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">home</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -4830,6 +4838,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">home</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -4868,6 +4877,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="page">advanced</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -4963,6 +4973,7 @@
 				<actions>
 					<action function="page">main</action>
 					<action function="set">tw_hide_kb=0</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>
 
@@ -4971,6 +4982,7 @@
 				<actions>
 					<action function="page">advanced</action>
 					<action function="set">tw_hide_kb=0</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>
 
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index e29ffaf..497d088 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 b78191d..ea52af8 100755
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -4188,6 +4188,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>
@@ -4871,6 +4878,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">home</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -4906,6 +4914,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="key">back</action>
+					<action function="changeterminal"/>
 				</actions>
 			</button>
 
@@ -4914,6 +4923,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="page">main</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>
 
@@ -4922,6 +4932,7 @@
 				<actions>
 					<action function="set">tw_hide_kb=0</action>
 					<action function="page">advanced</action>
+					<action function="changeterminal"/>
 				</actions>
 			</action>