1 |
/* |
2 |
* clip_windows.cpp - Clipboard handling, Windows implementation |
3 |
* |
4 |
* Basilisk II (C) 1997-2005 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, 0xb0, 0xa2, 0xa3, 0xa7, 0xb7, 0xb6, 0xdf, |
49 |
0xae, 0xa9, 0x20, 0xb4, 0xa8, 0x23, 0xc6, 0xd8, |
50 |
0x20, 0xb1, 0x3c, 0x3e, 0xa5, 0xb5, 0xf0, 0x53, |
51 |
0x50, 0x70, 0x2f, 0xaa, 0xba, 0x4f, 0xe6, 0xf8, |
52 |
0xbf, 0xa1, 0xac, 0x2f, 0x66, 0x7e, 0x44, 0xab, |
53 |
0xbb, 0x2e, 0x20, 0xc0, 0xc3, 0xd5, 0x4f, 0x6f, |
54 |
0x2d, 0x2d, 0x22, 0x22, 0x60, 0x27, 0xf7, 0x20, |
55 |
0xff, 0x59, 0x2f, 0xa4, 0x3c, 0x3e, 0x66, 0x66, |
56 |
0x23, 0xb7, 0x2c, 0x22, 0x25, 0xc2, 0xca, 0xc1, |
57 |
0xcb, 0xc8, 0xcd, 0xce, 0xcf, 0xcc, 0xd3, 0xd4, |
58 |
0x20, 0xd2, 0xda, 0xdb, 0xd9, 0x69, 0x5e, 0x7e, |
59 |
0xaf, 0x20, 0xb7, 0xb0, 0xb8, 0x22, 0xb8, 0x20 |
60 |
}; |
61 |
|
62 |
static const uint8 iso2mac[0x80] = { |
63 |
0xad, 0xb0, 0xe2, 0xc4, 0xe3, 0xc9, 0xa0, 0xe0, |
64 |
0xf6, 0xe4, 0xde, 0xdc, 0xce, 0xb2, 0xb3, 0xb6, |
65 |
0xb7, 0xd4, 0xd5, 0xd2, 0xd3, 0xa5, 0xd0, 0xd1, |
66 |
0xf7, 0xaa, 0xdf, 0xdd, 0xcf, 0xba, 0xfd, 0xd9, |
67 |
0xca, 0xc1, 0xa2, 0xa3, 0xdb, 0xb4, 0xbd, 0xa4, |
68 |
0xac, 0xa9, 0xbb, 0xc7, 0xc2, 0xf0, 0xa8, 0xf8, |
69 |
0xa1, 0xb1, 0xc3, 0xc5, 0xab, 0xb5, 0xa6, 0xe1, |
70 |
0xfc, 0xc6, 0xbc, 0xc8, 0xf9, 0xda, 0xd7, 0xc0, |
71 |
0xcb, 0xe7, 0xe5, 0xcc, 0x80, 0x81, 0xae, 0x82, |
72 |
0xe9, 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 |
// The main window handle |
97 |
#ifdef USE_SDL_VIDEO |
98 |
#include <SDL_syswm.h> |
99 |
static HWND GetMainWindowHandle(void) |
100 |
{ |
101 |
SDL_SysWMinfo wmInfo; |
102 |
wmInfo.version.major = SDL_MAJOR_VERSION; |
103 |
wmInfo.version.minor = SDL_MINOR_VERSION; |
104 |
wmInfo.version.patch = SDL_PATCHLEVEL; |
105 |
return SDL_GetWMInfo(&wmInfo) ? wmInfo.window : NULL; |
106 |
} |
107 |
#endif |
108 |
|
109 |
|
110 |
/* |
111 |
* Initialization |
112 |
*/ |
113 |
|
114 |
void ClipInit(void) |
115 |
{ |
116 |
no_clip_conversion = PrefsFindBool("noclipconversion"); |
117 |
} |
118 |
|
119 |
|
120 |
/* |
121 |
* Deinitialization |
122 |
*/ |
123 |
|
124 |
void ClipExit(void) |
125 |
{ |
126 |
} |
127 |
|
128 |
|
129 |
/* |
130 |
* Mac application wrote to clipboard |
131 |
*/ |
132 |
|
133 |
void PutScrap(uint32 type, void *scrap, int32 length) |
134 |
{ |
135 |
D(bug("PutScrap type %08lx, data %p, length %ld\n", type, scrap, length)); |
136 |
if (we_put_this_data) { |
137 |
we_put_this_data = false; |
138 |
return; |
139 |
} |
140 |
if (length <= 0) |
141 |
return; |
142 |
|
143 |
do_putscrap(type, scrap, length); |
144 |
} |
145 |
|
146 |
static void do_putscrap(uint32 type, void *scrap, int32 length) |
147 |
{ |
148 |
ByteArray clip_data; |
149 |
UINT uFormat = 0; |
150 |
|
151 |
switch (type) { |
152 |
case FOURCC('T','E','X','T'): { |
153 |
D(bug(" clipping TEXT\n")); |
154 |
|
155 |
// Convert text from Mac charset to ISO-Latin1 |
156 |
uint8 *p = (uint8 *)scrap; |
157 |
for (int i=0; i<length; i++) { |
158 |
uint8 c = *p++; |
159 |
if (c < 0x80) { |
160 |
if (c == 13) { // CR -> CR/LF |
161 |
clip_data.push_back(c); |
162 |
c = 10; |
163 |
} |
164 |
} else if (!no_clip_conversion) |
165 |
c = mac2iso[c & 0x7f]; |
166 |
clip_data.push_back(c); |
167 |
} |
168 |
clip_data.push_back(0); |
169 |
uFormat = CF_TEXT; |
170 |
break; |
171 |
} |
172 |
} |
173 |
if (uFormat != CF_TEXT) // 'TEXT' only |
174 |
return; |
175 |
|
176 |
// Transfer data to the native clipboard |
177 |
HWND hMainWindow = GetMainWindowHandle(); |
178 |
if (!hMainWindow ||!OpenClipboard(hMainWindow)) |
179 |
return; |
180 |
EmptyClipboard(); |
181 |
HANDLE hData = GlobalAlloc(GMEM_DDESHARE, clip_data.size()); |
182 |
if (hData) { |
183 |
uint8 *data = (uint8 *)GlobalLock(hData); |
184 |
memcpy(data, clip_data.data(), clip_data.size()); |
185 |
GlobalUnlock(hData); |
186 |
if (!SetClipboardData(uFormat, hData)) |
187 |
GlobalFree(hData); |
188 |
} |
189 |
CloseClipboard(); |
190 |
} |
191 |
|
192 |
|
193 |
/* |
194 |
* Mac application reads clipboard |
195 |
*/ |
196 |
|
197 |
void GetScrap(void **handle, uint32 type, int32 offset) |
198 |
{ |
199 |
D(bug("GetScrap handle %p, type %08x, offset %d\n", handle, type, offset)); |
200 |
|
201 |
do_getscrap(handle, type, offset); |
202 |
} |
203 |
|
204 |
static void do_getscrap(void **handle, uint32 type, int32 offset) |
205 |
{ |
206 |
// Get appropriate format for requested data |
207 |
UINT uFormat = 0; |
208 |
switch (type) { |
209 |
case FOURCC('T','E','X','T'): |
210 |
uFormat = CF_TEXT; |
211 |
break; |
212 |
} |
213 |
if (uFormat != CF_TEXT) // 'TEXT' only |
214 |
return; |
215 |
|
216 |
// Get the native clipboard data |
217 |
HWND hMainWindow = GetMainWindowHandle(); |
218 |
if (!hMainWindow || !OpenClipboard(hMainWindow)) |
219 |
return; |
220 |
HANDLE hData = GetClipboardData(uFormat); |
221 |
if (hData) { |
222 |
uint8 *data = (uint8 *)GlobalLock(hData); |
223 |
if (data) { |
224 |
uint32 length = GlobalSize(hData); |
225 |
if (length) { |
226 |
int32 out_length = 0; |
227 |
|
228 |
// Allocate space for new scrap in MacOS side |
229 |
M68kRegisters r; |
230 |
r.d[0] = length; |
231 |
Execute68kTrap(0xa71e, &r); // NewPtrSysClear() |
232 |
uint32 scrap_area = r.a[0]; |
233 |
|
234 |
if (scrap_area) { |
235 |
switch (type) { |
236 |
case FOURCC('T','E','X','T'): |
237 |
D(bug(" clipping TEXT\n")); |
238 |
|
239 |
// Convert text from ISO-Latin1 to Mac charset |
240 |
uint8 *p = Mac2HostAddr(scrap_area); |
241 |
for (int i = 0; i < length; i++) { |
242 |
uint8 c = data[i]; |
243 |
if (c < 0x80) { |
244 |
if (c == 13 && i < length - 1 && data[i + 1] == 10) { // CR/LF -> CR |
245 |
c = 13; |
246 |
i++; |
247 |
} |
248 |
} else if (!no_clip_conversion) |
249 |
c = iso2mac[c & 0x7f]; |
250 |
*p++ = c; |
251 |
out_length++; |
252 |
} |
253 |
break; |
254 |
} |
255 |
|
256 |
// Add new data to clipboard |
257 |
static uint8 proc[] = { |
258 |
0x59, 0x8f, // subq.l #4,sp |
259 |
0xa9, 0xfc, // ZeroScrap() |
260 |
0x2f, 0x3c, 0, 0, 0, 0, // move.l #length,-(sp) |
261 |
0x2f, 0x3c, 0, 0, 0, 0, // move.l #type,-(sp) |
262 |
0x2f, 0x3c, 0, 0, 0, 0, // move.l #outbuf,-(sp) |
263 |
0xa9, 0xfe, // PutScrap() |
264 |
0x58, 0x8f, // addq.l #4,sp |
265 |
M68K_RTS >> 8, M68K_RTS |
266 |
}; |
267 |
uint32 proc_area = Host2MacAddr(proc); |
268 |
WriteMacInt32(proc_area + 6, out_length); |
269 |
WriteMacInt32(proc_area + 12, type); |
270 |
WriteMacInt32(proc_area + 18, scrap_area); |
271 |
we_put_this_data = true; |
272 |
Execute68k(proc_area, &r); |
273 |
|
274 |
// We are done with scratch memory |
275 |
r.a[0] = scrap_area; |
276 |
Execute68kTrap(0xa01f, &r); // DisposePtr |
277 |
} |
278 |
} |
279 |
GlobalUnlock(hData); |
280 |
} |
281 |
} |
282 |
CloseClipboard(); |
283 |
} |