From cf38a9a334669c8937ed7786663440c055b3f6c0 Mon Sep 17 00:00:00 2001
From: Edward Rudd <urkle@outoforder.cc>
Date: Fri, 3 Jan 2014 12:33:44 -0500
Subject: [PATCH 1/3] fix screen grab crash when in fullscreen

- read the FBO size NOT the screen size.
---
 src/graphics.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/graphics.cpp b/src/graphics.cpp
index 7823614..0bf8aab 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -521,7 +521,7 @@ struct GraphicsPrivate
 
 		screen.getPP().bindLastBuffer();
 
-		glReadPixels(0, 0, scSize.x, scSize.y, GL_RGBA, GL_UNSIGNED_BYTE, screenshot->pixels);
+		glReadPixels(0, 0, scRes.x, scRes.y, GL_RGBA, GL_UNSIGNED_BYTE, screenshot->pixels);
 
 		IMG_SavePNG(screenshot, filename);
 

From f72ce6de33ca7df9b4d58c4b976d757a1d387c8f Mon Sep 17 00:00:00 2001
From: Edward Rudd <urkle@outoforder.cc>
Date: Fri, 3 Jan 2014 20:23:18 -0500
Subject: [PATCH 2/3] write screenshots to desktop directory OR home directory.

---
 CMakeLists.txt         | 15 +++++++
 src/config.cpp         |  4 +-
 src/config.h           |  4 ++
 src/graphics.cpp       |  3 +-
 src/platform_linux.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++
 src/platform_osx.mm    |  7 ++++
 6 files changed, 122 insertions(+), 2 deletions(-)
 create mode 100644 src/platform_linux.cpp
 create mode 100644 src/platform_osx.mm

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ed2c331..de68bf3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -168,6 +168,20 @@ set(MAIN_SOURCE
 
 source_group("MKXP Source" FILES ${MAIN_SOURCE} ${MAIN_HEADERS})
 
+## Platform source ##
+
+if(LINUX)
+	set(PLATFORM_SOURCE
+		src/platform_linux.cpp
+	)
+elseif(APPLE)
+	set(PLATFORM_SOURCE
+		src/platform_osx.mm
+	)
+else()
+	message(FATAL_ERROR "No platform source for this platform")
+endif()
+
 ## Setup embedded source ##
 
 set(EMBEDDED_INPUT
@@ -340,6 +354,7 @@ add_executable(${PROJECT_NAME} MACOSX_BUNDLE
 	${BINDING_HEADERS}
 	${BINDING_SOURCE}
 	${EMBEDDED_SOURCE}
+	${PLATFORM_SOURCE}
 )
 
 target_compile_definitions(${PROJECT_NAME} PRIVATE
diff --git a/src/config.cpp b/src/config.cpp
index 8f594bf..4ba2391 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -49,7 +49,9 @@ Config::Config()
       anyAltToggleFS(false),
       allowSymlinks(false),
       pathCache(true)
-{}
+{
+	setupPaths();
+}
 
 void Config::read()
 {
diff --git a/src/config.h b/src/config.h
index 3de9249..c90838c 100644
--- a/src/config.h
+++ b/src/config.h
@@ -49,6 +49,8 @@ struct Config
 	bool pathCache;
 
 	std::string iconPath;
+	
+	std::string desktopPath;
 
 	std::string customScript;
 	std::vector<std::string> rtps;
@@ -63,6 +65,8 @@ struct Config
 
 	void read();
 	void readGameINI();
+private:
+	void setupPaths();
 };
 
 #endif // CONFIG_H
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 0bf8aab..0bd2cab 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -544,7 +544,8 @@ struct GraphicsPrivate
 		snprintf(filename, sizeof(filename), "%d%02d%02d-%02d%02d%02d.png",
 		         tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
 
-		writeScreenshot(filename);
+		std::string path = threadData->config.desktopPath + filename;
+		writeScreenshot(path.c_str());
 	}
 };
 
diff --git a/src/platform_linux.cpp b/src/platform_linux.cpp
new file mode 100644
index 0000000..fb4ba82
--- /dev/null
+++ b/src/platform_linux.cpp
@@ -0,0 +1,91 @@
+#include "config.h"
+
+#include <stdlib.h>
+#include <fstream>
+#include <string>
+#include <unistd.h>
+#include <boost/algorithm/string.hpp>
+
+inline void ensure_trailing_slash(std::string& path)
+{
+	if (path.size() && path[path.size()-1] != '/') {
+		path += "/";
+	}
+}
+
+static std::string find_xdg_config_dir()
+{
+	std::string path;
+	const char* env = getenv("XDG_CONFIG_HOME");
+	if (env) {
+		path = env;
+		ensure_trailing_slash(path);
+	} else {
+		env = getenv("HOME");
+		if (!env) {
+			return "";
+		}
+		path = env;
+		ensure_trailing_slash(path);
+		path += ".config/";
+	}
+	return path;
+}
+
+static std::string get_desktop_dir()
+{
+	std::string desktopPath;
+
+	std::string cfg_dir = find_xdg_config_dir();
+	if (!cfg_dir.empty()) {
+		std::string cfg_file = cfg_dir + "user-dirs.dirs";
+		if (access(cfg_file.c_str(), F_OK) == 0) {
+			std::ifstream fin(cfg_file.c_str());
+			std::string line;
+			while (std::getline(fin, line)) {
+				/* skip blank and comment lines */
+				if (line.empty() || line[0] == '#') continue;
+
+				std::vector<std::string> parts;
+				boost::split(parts, line, boost::is_any_of("="));
+				if (parts.size() == 2) {
+					std::string key = boost::trim_copy(parts[0]);
+					if (key == "XDG_DESKTOP_DIR") {
+						std::string val = boost::trim_copy_if(parts[1], boost::is_any_of(" \n\r\t\""));
+						desktopPath = val;
+						break;
+					}
+				}
+			}
+		}
+	}
+	const char* home = getenv("HOME");
+	if (home) {
+		size_t pos = desktopPath.find("$HOME");
+		if (pos != desktopPath.npos) {
+			desktopPath.replace(pos, pos + 5, home);
+		}
+		pos = desktopPath.find("${HOME}");
+		if (pos != desktopPath.npos) {
+			desktopPath.replace(pos, pos + 7, home);
+		}
+	}
+	if (desktopPath.empty() || access(desktopPath.c_str(), F_OK) != 0) {
+		if (home) {
+			desktopPath = home;
+			ensure_trailing_slash(desktopPath);
+			if (access((desktopPath + "Desktop").c_str(), F_OK) == 0) {
+				desktopPath += "Desktop/";
+			}
+		} else {
+			desktopPath = "";
+		}
+	}
+	ensure_trailing_slash(desktopPath);
+	return desktopPath;
+}
+
+void Config::setupPaths()
+{
+	desktopPath = get_desktop_dir();
+}
\ No newline at end of file
diff --git a/src/platform_osx.mm b/src/platform_osx.mm
new file mode 100644
index 0000000..41dc7be
--- /dev/null
+++ b/src/platform_osx.mm
@@ -0,0 +1,7 @@
+#include "config.h"
+
+
+void Config::setupPaths()
+{
+	
+}
\ No newline at end of file

From bae3d1b03675a26c287866994cb62b7bcd9b6b17 Mon Sep 17 00:00:00 2001
From: Edward Rudd <urkle@outoforder.cc>
Date: Fri, 3 Jan 2014 21:52:57 -0500
Subject: [PATCH 3/3] implement mac OS X version of get desktop path

---
 src/platform_osx.mm | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/platform_osx.mm b/src/platform_osx.mm
index 41dc7be..e84f022 100644
--- a/src/platform_osx.mm
+++ b/src/platform_osx.mm
@@ -1,7 +1,35 @@
 #include "config.h"
 
+#include <string>
+
+#import <Foundation/Foundation.h>
+
+static void ensure_trailing_slash(std::string& path)
+{
+	if (path.size() && path[path.size()-1] != '/') {
+		path += "/";
+	}
+}
+
+static std::string get_desktop_dir()
+{
+	std::string ret;
+	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
+	NSString *path = [paths objectAtIndex: 0];
+
+	ret = [path fileSystemRepresentation];
+
+	[pool drain];
+
+	ensure_trailing_slash(ret);
+	return ret;
+}
+
 
 void Config::setupPaths()
 {
-	
-}
\ No newline at end of file
+	desktopPath = get_desktop_dir();
+}
+