1 /*
2     clipbrd_cygwin.cpp - Windows clipbrd interaction.
3 
4     Copyright (C) 2006 Standa Opichal of ARAnyM Team
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #include "sysdeps.h"
22 #if defined(OS_cygwin) || defined(OS_mingw)
23 #include "SDL_compat.h"
24 #include "win32_supp.h"
25 #include "clipbrd.h"
26 #include "host.h"
27 #include "maptab.h"
28 
29 #define DEBUG 0
30 #include "debug.h"
31 
init_aclip()32 int init_aclip() { return 0; }
33 
filter_aclip(const SDL_Event * event)34 int filter_aclip(const SDL_Event *event) { (void) event; return 1; }
35 
write_aclip(char * src,size_t len)36 void write_aclip(char *src, size_t len)
37 {
38 	HGLOBAL clipdata;
39 	unsigned short *dst;
40 	unsigned short ch;
41 
42 	clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, sizeof(unsigned short) * (len + 1));
43 	if (!clipdata)
44 		return;
45 	dst = (unsigned short *)GlobalLock(clipdata);
46 	if (!dst)
47 		return;
48 	size_t count = len;
49 	while ( count > 0)
50 	{
51 		ch = (unsigned char)*src++;
52 		if (ch == 0)
53 			break;
54 		ch = atari_to_utf16[ch];
55 		*dst++ = ch;
56 		count--;
57 	}
58 	*dst = 0;
59 	GlobalUnlock(clipdata);
60 
61 	if (OpenClipboard(NULL)) {
62 		EmptyClipboard();
63 		SetClipboardData(CF_UNICODETEXT, clipdata);
64 		CloseClipboard();
65 	} else
66 	{
67 		GlobalFree(clipdata);
68 		D(bug("OpenClipboard failed: %s", win32_errstring(GetLastError())));
69 	}
70 }
71 
read_aclip(size_t * dstlen)72 char * read_aclip( size_t *dstlen)
73 {
74 	if (OpenClipboard(NULL)) {
75 		HANDLE hData = GetClipboardData( CF_UNICODETEXT );
76 		if (hData)
77 		{
78 			unsigned short *src = (unsigned short *)GlobalLock( hData );
79 			size_t len = GlobalSize(hData) / sizeof(unsigned short);
80 		   	char *data = new char[len + 1];
81 		   	char *dst = data;
82 		   	while (len)
83 		   	{
84 		   		unsigned short ch = *src++;
85 		   		if (ch == 0)
86 		   			break;
87 				unsigned short c = utf16_to_atari[ch];
88 				if (c >= 0x100)
89 				{
90 					charset_conv_error(ch);
91 					*dst++ = '?';
92 				} else
93 				{
94 					*dst++ = c;
95 				}
96 				len--;
97 		   	}
98 		   	*dst = '\0';
99 			*dstlen = dst - data;
100 			GlobalUnlock( hData );
101 			CloseClipboard();
102 
103 			return data;
104 		}
105 	} else
106 	{
107 		D(bug("OpenClipboard failed: %s", win32_errstring(GetLastError())));
108 	}
109 	return NULL;
110 }
111 
112 #endif
113