ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Windows/clip_windows.cpp
Revision: 1.6
Committed: 2009-04-14T15:17:03Z (15 years, 1 month ago) by asvitkine
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +2 -0 lines
Log Message:
[Patch by Kelvin Delbarre]
Fixes copy/paste errors in the Windows version of SheepShaver, wherein pasted
text would have a trailing null character or extra garbage after the end.

File Contents

# Content
1 /*
2 * clip_windows.cpp - Clipboard handling, Windows implementation
3 *
4 * Basilisk II (C) 1997-2008 Christian Bauer
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "sysdeps.h"
22
23 #include <vector>
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26
27 #include "macos_util.h"
28 #include "clip.h"
29 #include "prefs.h"
30 #include "cpu_emulation.h"
31 #include "main.h"
32 #include "emul_op.h"
33
34 #define DEBUG 0
35 #include "debug.h"
36
37 #ifndef NO_STD_NAMESPACE
38 using std::vector;
39 #endif
40
41
42 // Conversion tables
43 static const uint8 mac2iso[0x80] = {
44 0xc4, 0xc5, 0xc7, 0xc9, 0xd1, 0xd6, 0xdc, 0xe1,
45 0xe0, 0xe2, 0xe4, 0xe3, 0xe5, 0xe7, 0xe9, 0xe8,
46 0xea, 0xeb, 0xed, 0xec, 0xee, 0xef, 0xf1, 0xf3,
47 0xf2, 0xf4, 0xf6, 0xf5, 0xfa, 0xf9, 0xfb, 0xfc,
48 0x2b, 0x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xe9, 0x83, 0xe6, 0xe8, 0xed, 0xea, 0xeb, 0xec,
73 0xf5, 0x84, 0xf1, 0xee, 0xef, 0xcd, 0x85, 0xfb,
74 0xaf, 0xf4, 0xf2, 0xf3, 0x86, 0xfa, 0xb8, 0xa7,
75 0x88, 0x87, 0x89, 0x8b, 0x8a, 0x8c, 0xbe, 0x8d,
76 0x8f, 0x8e, 0x90, 0x91, 0x93, 0x92, 0x94, 0x95,
77 0xfe, 0x96, 0x98, 0x97, 0x99, 0x9b, 0x9a, 0xd6,
78 0xbf, 0x9d, 0x9c, 0x9e, 0x9f, 0xff, 0xb9, 0xd8
79 };
80
81 // Flag: Don't convert clipboard text
82 static bool no_clip_conversion;
83
84 // Flag for PutScrap(): the data was put by GetScrap(), don't bounce it back to the Windows side
85 static bool we_put_this_data = false;
86
87 // Define a byte array (rewrite if it's a bottleneck)
88 struct ByteArray : public vector<uint8> {
89 uint8 *data() { return &(*this)[0]; }
90 };
91
92 // Prototypes
93 static void do_putscrap(uint32 type, void *scrap, int32 length);
94 static void do_getscrap(void **handle, uint32 type, int32 offset);
95
96 // From main_windows.cpp
97 extern HWND GetMainWindowHandle(void);
98
99
100 /*
101 * Initialization
102 */
103
104 void ClipInit(void)
105 {
106 no_clip_conversion = PrefsFindBool("noclipconversion");
107 }
108
109
110 /*
111 * Deinitialization
112 */
113
114 void ClipExit(void)
115 {
116 }
117
118
119 /*
120 * Mac application wrote to clipboard
121 */
122
123 void PutScrap(uint32 type, void *scrap, int32 length)
124 {
125 D(bug("PutScrap type %08lx, data %p, length %ld\n", type, scrap, length));
126 if (we_put_this_data) {
127 we_put_this_data = false;
128 return;
129 }
130 if (length <= 0)
131 return;
132
133 do_putscrap(type, scrap, length);
134 }
135
136 static void do_putscrap(uint32 type, void *scrap, int32 length)
137 {
138 ByteArray clip_data;
139 UINT uFormat = 0;
140
141 switch (type) {
142 case FOURCC('T','E','X','T'): {
143 D(bug(" clipping TEXT\n"));
144
145 // Convert text from Mac charset to ISO-Latin1
146 uint8 *p = (uint8 *)scrap;
147 for (int i=0; i<length; i++) {
148 uint8 c = *p++;
149 if (c < 0x80) {
150 if (c == 13) { // CR -> CR/LF
151 clip_data.push_back(c);
152 c = 10;
153 }
154 } else if (!no_clip_conversion)
155 c = mac2iso[c & 0x7f];
156 clip_data.push_back(c);
157 }
158 clip_data.push_back(0);
159 uFormat = CF_TEXT;
160 break;
161 }
162 }
163 if (uFormat != CF_TEXT) // 'TEXT' only
164 return;
165
166 // Transfer data to the native clipboard
167 HWND hMainWindow = GetMainWindowHandle();
168 if (!hMainWindow ||!OpenClipboard(hMainWindow))
169 return;
170 EmptyClipboard();
171 HANDLE hData = GlobalAlloc(GMEM_DDESHARE, clip_data.size());
172 if (hData) {
173 uint8 *data = (uint8 *)GlobalLock(hData);
174 memcpy(data, clip_data.data(), clip_data.size());
175 GlobalUnlock(hData);
176 if (!SetClipboardData(uFormat, hData))
177 GlobalFree(hData);
178 }
179 CloseClipboard();
180 }
181
182
183 /*
184 * Mac application reads clipboard
185 */
186
187 void GetScrap(void **handle, uint32 type, int32 offset)
188 {
189 D(bug("GetScrap handle %p, type %08x, offset %d\n", handle, type, offset));
190
191 do_getscrap(handle, type, offset);
192 }
193
194 static void do_getscrap(void **handle, uint32 type, int32 offset)
195 {
196 // Get appropriate format for requested data
197 UINT uFormat = 0;
198 switch (type) {
199 case FOURCC('T','E','X','T'):
200 uFormat = CF_TEXT;
201 break;
202 }
203 if (uFormat != CF_TEXT) // 'TEXT' only
204 return;
205
206 // Get the native clipboard data
207 HWND hMainWindow = GetMainWindowHandle();
208 if (!hMainWindow || !OpenClipboard(hMainWindow))
209 return;
210 HANDLE hData = GetClipboardData(uFormat);
211 if (hData) {
212 uint8 *data = (uint8 *)GlobalLock(hData);
213 if (data) {
214 uint32 length = GlobalSize(hData);
215 if (length) {
216 int32 out_length = 0;
217
218 // Allocate space for new scrap in MacOS side
219 M68kRegisters r;
220 r.d[0] = length;
221 Execute68kTrap(0xa71e, &r); // NewPtrSysClear()
222 uint32 scrap_area = r.a[0];
223
224 if (scrap_area) {
225 switch (type) {
226 case FOURCC('T','E','X','T'):
227 D(bug(" clipping TEXT\n"));
228
229 // Convert text from ISO-Latin1 to Mac charset
230 uint8 *p = Mac2HostAddr(scrap_area);
231 for (int i = 0; i < length; i++) {
232 uint8 c = data[i];
233 if (c < 0x80) {
234 if (c == 0)
235 break;
236 if (c == 13 && i < length - 1 && data[i + 1] == 10) { // CR/LF -> CR
237 c = 13;
238 i++;
239 }
240 } else if (!no_clip_conversion)
241 c = iso2mac[c & 0x7f];
242 *p++ = c;
243 out_length++;
244 }
245 break;
246 }
247
248 // Add new data to clipboard
249 static uint8 proc[] = {
250 0x59, 0x8f, // subq.l #4,sp
251 0xa9, 0xfc, // ZeroScrap()
252 0x2f, 0x3c, 0, 0, 0, 0, // move.l #length,-(sp)
253 0x2f, 0x3c, 0, 0, 0, 0, // move.l #type,-(sp)
254 0x2f, 0x3c, 0, 0, 0, 0, // move.l #outbuf,-(sp)
255 0xa9, 0xfe, // PutScrap()
256 0x58, 0x8f, // addq.l #4,sp
257 M68K_RTS >> 8, M68K_RTS
258 };
259 uint32 proc_area = Host2MacAddr(proc);
260 WriteMacInt32(proc_area + 6, out_length);
261 WriteMacInt32(proc_area + 12, type);
262 WriteMacInt32(proc_area + 18, scrap_area);
263 we_put_this_data = true;
264 Execute68k(proc_area, &r);
265
266 // We are done with scratch memory
267 r.a[0] = scrap_area;
268 Execute68kTrap(0xa01f, &r); // DisposePtr
269 }
270 }
271 GlobalUnlock(hData);
272 }
273 }
274 CloseClipboard();
275 }