summaryrefslogtreecommitdiffstats
path: root/lib/clipboard/src/clipboardWin32.c
blob: be48906f907310102abcceb1adcae1a4c7d5b3e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
	This file is part of Warzone 2100.
	Copyright (C) 2008  Freddie Witherden
	Copyright (C) 2008-2009  Warzone Resurrection Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include "utf.h"
// Defines most macros and types from <stdbool.h> and <stdint.h>
#include "types.h"

char *widgetGetClipboardText()
{
	uint16_t *clipboardText;
	char *ourText = NULL;

	// If there is any text on the clipboard, open it
	if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
	{
		// Get any text on the clipboard
		HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);

		// If the handle is valid, fetch the text
		if (hClipboardData)
		{
			// Get the text
			clipboardText = GlobalLock(hClipboardData);

			// So long as we got something
			if (clipboardText)
			{
				int i, j;

				// Convert it to UTF-8 (from UTF-16)
				ourText = UTF16toUTF8(clipboardText, NULL);

				// Unlock the text
				GlobalUnlock(hClipboardData);

				// Strip any '\r' from the text
				for (i = j = 0; ourText[i]; i++)
				{
					if (ourText[i] != '\r')
					{
						ourText[j++] = ourText[i];
					}
				}

				// NUL terminate
				ourText[j] = '\0';
			}
		}

		// Close the clipboard
		CloseClipboard();
	}

	return ourText;
}

bool widgetSetClipboardText(const char *text)
{
	bool ret = false;

	// Copy of text with \n => \r\n
	char *newText;

	// UTF-16 version of newText
	uint16_t *utf16NewText;

	// Number of bytes utf16NewText is in size
	size_t nbytes;

	int count, i, j;

	// Get the number of '\n' characters in the text
	for (i = count = 0; text[i]; i++)
	{
		if (text[i] == '\n')
		{
			count++;
		}
	}

	// Allocate enough space for the \r\n string
	newText = malloc(strlen(text) + count + 1);

	// Copy the string, converting \n to \r\n
	for (i = j = 0; text[i]; i++, j++)
	{
		// If the character is a newline prepend a \r
		if (text[i] == '\n')
		{
			newText[j++] = '\r';
		}

		// Copy the character (\n or otherwise)
		newText[j] = text[i];
	}

	// NUL terminate
	newText[j] = '\0';

	// Convert to UTF-16
	utf16NewText = UTF8toUTF16(newText, &nbytes);

	// Open the clipboard
	if (OpenClipboard(NULL))
	{
		HGLOBAL hGlobal;
		uint16_t *clipboardText;

		// Empty it (which also transfers ownership of it to ourself)
		EmptyClipboard();

		// Allocate global space for the text
		hGlobal = GlobalAlloc(GMEM_MOVEABLE, nbytes);

		// Lock the newly allocated memory
		clipboardText = GlobalLock(hGlobal);

		// Copy the text
		memcpy(clipboardText, utf16NewText, nbytes);

		// Unlock the memory (must come before CloseClipboard())
		GlobalUnlock(hGlobal);

		// Place the handle on the clipboard
		if (SetClipboardData(CF_UNICODETEXT, hGlobal))
		{
			// We were successful
			ret = true;
		}

		// Close the clipboard
		CloseClipboard();
	}

	// Release the malloc-ed strings
	free(newText);
	free(utf16NewText);

	return ret;
}