Implement "take a screenshot" feature

* Like in android - press power+volume down, screenshots are saved
  in /sdcard/Pictures/Screenshots (if /sdcard is mounted) or /tmp

Change-Id: Iaefa15b11a1d5fdfac57d77388db1621f378a8d4
Signed-off-by: Vojtech Bocek <vbocek@gmail.com>
diff --git a/gui/action.cpp b/gui/action.cpp
index 3c5584d..b77e7e7 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <dirent.h>
+#include <pwd.h>
 
 #include <string>
 #include <sstream>
@@ -775,6 +776,54 @@
 		return 0;
 	}
 
+	if (function == "screenshot")
+	{
+		time_t tm;
+		char path[256];
+		int path_len;
+		uid_t uid = -1;
+		gid_t gid = -1;
+
+		struct passwd *pwd = getpwnam("media_rw");
+		if(pwd) {
+			uid = pwd->pw_uid;
+			gid = pwd->pw_gid;
+		}
+
+		const std::string storage = DataManager::GetCurrentStoragePath();
+		if(PartitionManager.Is_Mounted_By_Path(storage)) {
+			snprintf(path, sizeof(path), "%s/Pictures/Screenshots/", storage.c_str());
+		} else {
+			strcpy(path, "/tmp/");
+		}
+
+		if(!TWFunc::Create_Dir_Recursive(path, 0666, uid, gid))
+			return 0;
+
+		tm = time(NULL);
+		path_len = strlen(path);
+
+		// Screenshot_2014-01-01-18-21-38.png
+		strftime(path+path_len, sizeof(path)-path_len, "Screenshot_%Y-%m-%d-%H-%M-%S.png", localtime(&tm));
+
+		int res = gr_save_screenshot(path);
+		if(res == 0) {
+			chmod(path, 0666);
+			chown(path, uid, gid);
+
+			gui_print("Screenshot was saved to %s\n", path);
+
+			// blink to notify that the screenshow was taken
+			gr_color(255, 255, 255, 255);
+			gr_fill(0, 0, gr_fb_width(), gr_fb_height());
+			gr_flip();
+			gui_forceRender();
+		} else {
+			LOGERR("Failed to take a screenshot!\n");
+		}
+		return 0;
+	}
+
 	if (isThreaded)
 	{
 		if (function == "fileexists")