From 8040a68fa087f4c09f60c5663fd08077830b8707 Mon Sep 17 00:00:00 2001
From: Alam Ed Arias <alam@srb2.org>
Date: Thu, 3 Nov 2016 14:29:51 -0400
Subject: [PATCH 1/2] Clipboard: remove Win32 code for SDL Clipboard API

---
 src/sdl/i_system.c | 107 ++-------------------------------------------
 1 file changed, 4 insertions(+), 103 deletions(-)

diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c
index 8c4331aa0..97746b6bf 100644
--- a/src/sdl/i_system.c
+++ b/src/sdl/i_system.c
@@ -260,18 +260,6 @@ static char returnWadPath[256];
 #include "../byteptr.h"
 #endif
 
-// FAKE_CLIPBOARD simulates system clipboard, but is just local to our app
-#ifndef FAKE_CLIPBOARD
-#include "SDL_syswm.h"
-
-#if defined (_WIN32) && !defined (_XBOX)
-#include <winuser.h>
-#endif
-
-// TODO: clipboard support for other OSes
-
-#endif
-
 /**	\brief	The JoyReset function
 
 	\param	JoySet	Joystick info to reset
@@ -2659,107 +2647,20 @@ INT32 I_PutEnv(char *variable)
 #endif
 }
 
-#ifdef FAKE_CLIPBOARD
-static char __clipboard[512] = "";
-#endif
 
 INT32 I_ClipboardCopy(const char *data, size_t size)
 {
-#ifdef FAKE_CLIPBOARD
-	if (size >= 512) size = 511;
-	memcpy(__clipboard, data, size);
-	__clipboard[size] = 0;
-	return 0;
-#else // !FAKE_CLIPBOARD
-
-#if defined(_WIN32) && !defined(_XBOX)
-	SDL_SysWMinfo syswm;
-	HGLOBAL *storage_ptr;
-	char *storage_raw;
-
-	SDL_VERSION(&syswm.version);
-
-	// Get window (HWND) information, use that to open the clipboard
-	if (!SDL_GetWindowWMInfo(window, &syswm) || !(OpenClipboard(syswm.info.win.window)))
-		return -1;
-
-	// Erase clipboard contents -- if we have nothing to copy, just leave them blank
-	EmptyClipboard();
-	if (size == 0)
-		goto clipboardend;
-
-	// Allocate movable global memory to store our text.
-	if (!(storage_ptr = GlobalAlloc(GMEM_MOVEABLE, size+1))
-	 || !(storage_raw = (char *)GlobalLock(storage_ptr))) // Lock our pointer (gives us access to its data)
-		goto clipboardfail;
-	memcpy(storage_raw, data, size);
-	storage_raw[size] = 0;
-	GlobalUnlock(storage_ptr); // Unlock it before sending it off
-	if (!SetClipboardData(CF_TEXT, storage_ptr)) // NOTE: this removes our permissions from the pointer
-		goto clipboardfail;
-
-clipboardend:
-	CloseClipboard();
-	return 0; //OpenClipboard();
-clipboardfail:
-	CloseClipboard(); // Don't leave me hanging
-	return -1;
-#else
-	(void)data;
 	(void)size;
+	if (SDL_SetClipboardText(data) == 0)
+		return 0;
 	return -1;
-#endif
-
-#endif // FAKE_CLIPBOARD
 }
 
 const char *I_ClipboardPaste(void)
 {
-#ifdef FAKE_CLIPBOARD
-	return (const char *)&__clipboard;
-#else // !FAKE_CLIPBOARD
-
-#if defined(_WIN32) && !defined(_XBOX)
-	HGLOBAL *clipboard_ptr;
-	const char *clipboard_raw;
-	static char clipboard_contents[512];
-	char *i = clipboard_contents;
-
-	if (!IsClipboardFormatAvailable(CF_TEXT))
-		return NULL; // Data either unavailable, or in wrong format
-	OpenClipboard(NULL); // NOTE: Don't need window pointer to get, only to set
-	if (!(clipboard_ptr = GetClipboardData(CF_TEXT))) // Get global pointer
-		return NULL;
-	if (!(clipboard_raw = (const char *)GlobalLock(clipboard_ptr))) // Lock access -- gives us direct ptr to data
-	{
-		CloseClipboard(); // Don't leave me hanging if we fail
-		return NULL;
-	}
-	memcpy(clipboard_contents, clipboard_raw, 511);
-	GlobalUnlock(clipboard_ptr); // Unlock for other apps
-	CloseClipboard(); // And make sure to close the clipboard for other apps too
-	clipboard_contents[511] = 0; // Done after unlock to cause as little interference as possible
-
-	while (*i)
-	{
-		if (*i == '\n' || *i == '\r')
-		{ // End on newline
-			*i = 0;
-			break;
-		}
-		else if (*i == '\t')
-			*i = ' '; // Tabs become spaces
-		else if (*i < 32 || (unsigned)*i > 127)
-			*i = '?'; // Nonprintable chars become question marks
-		++i;
-	}
-
-	return (const char *)&clipboard_contents;
-#else
+	if (SDL_HasClipboardText())
+		return SDL_GetClipboardText();
 	return NULL;
-#endif
-
-#endif // FAKE_CLIPBOARD
 }
 
 /**	\brief	The isWadPathOk function

From 1f6388a2e0089c6004825fa7ef66c54c0800de73 Mon Sep 17 00:00:00 2001
From: Alam Ed Arias <alam@srb2.org>
Date: Thu, 3 Nov 2016 14:35:32 -0400
Subject: [PATCH 2/2] Fix up Win32 interface code misdeclaration of
 I_ClipboardPaste()

---
 src/win32/win_sys.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/win32/win_sys.c b/src/win32/win_sys.c
index eec043803..80b89a6e6 100644
--- a/src/win32/win_sys.c
+++ b/src/win32/win_sys.c
@@ -3605,7 +3605,7 @@ INT32 I_ClipboardCopy(const char *data, size_t size)
 	return -1;
 }
 
-char *I_ClipboardPaste(void)
+const char *I_ClipboardPaste(void)
 {
 	return NULL;
 }