ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Windows/clip_windows.cpp
Revision: 1.3
Committed: 2006-03-27T22:04:36Z (18 years, 7 months ago) by gbeauche
Branch: MAIN
Changes since 1.2: +1 -3 lines
Log Message:
cosmetics

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * clip_windows.cpp - Clipboard handling, Windows implementation
3     *
4 gbeauche 1.2 * Basilisk II (C) 1997-2005 Christian Bauer
5 gbeauche 1.1 *
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 gbeauche 1.3 SDL_VERSION(&wmInfo.version);
103 gbeauche 1.1 return SDL_GetWMInfo(&wmInfo) ? wmInfo.window : NULL;
104     }
105     #endif
106    
107    
108     /*
109     * Initialization
110     */
111    
112     void ClipInit(void)
113     {
114     no_clip_conversion = PrefsFindBool("noclipconversion");
115     }
116    
117    
118     /*
119     * Deinitialization
120     */
121    
122     void ClipExit(void)
123     {
124     }
125    
126    
127     /*
128     * Mac application wrote to clipboard
129     */
130    
131     void PutScrap(uint32 type, void *scrap, int32 length)
132     {
133     D(bug("PutScrap type %08lx, data %p, length %ld\n", type, scrap, length));
134     if (we_put_this_data) {
135     we_put_this_data = false;
136     return;
137     }
138     if (length <= 0)
139     return;
140    
141     do_putscrap(type, scrap, length);
142     }
143    
144     static void do_putscrap(uint32 type, void *scrap, int32 length)
145     {
146     ByteArray clip_data;
147     UINT uFormat = 0;
148    
149     switch (type) {
150     case FOURCC('T','E','X','T'): {
151     D(bug(" clipping TEXT\n"));
152    
153     // Convert text from Mac charset to ISO-Latin1
154     uint8 *p = (uint8 *)scrap;
155     for (int i=0; i<length; i++) {
156     uint8 c = *p++;
157     if (c < 0x80) {
158     if (c == 13) { // CR -> CR/LF
159     clip_data.push_back(c);
160     c = 10;
161     }
162     } else if (!no_clip_conversion)
163     c = mac2iso[c & 0x7f];
164     clip_data.push_back(c);
165     }
166     clip_data.push_back(0);
167     uFormat = CF_TEXT;
168     break;
169     }
170     }
171     if (uFormat != CF_TEXT) // 'TEXT' only
172     return;
173    
174     // Transfer data to the native clipboard
175     HWND hMainWindow = GetMainWindowHandle();
176     if (!hMainWindow ||!OpenClipboard(hMainWindow))
177     return;
178     EmptyClipboard();
179     HANDLE hData = GlobalAlloc(GMEM_DDESHARE, clip_data.size());
180     if (hData) {
181     uint8 *data = (uint8 *)GlobalLock(hData);
182     memcpy(data, clip_data.data(), clip_data.size());
183     GlobalUnlock(hData);
184     if (!SetClipboardData(uFormat, hData))
185     GlobalFree(hData);
186     }
187     CloseClipboard();
188     }
189    
190    
191     /*
192     * Mac application reads clipboard
193     */
194    
195     void GetScrap(void **handle, uint32 type, int32 offset)
196     {
197     D(bug("GetScrap handle %p, type %08x, offset %d\n", handle, type, offset));
198    
199     do_getscrap(handle, type, offset);
200     }
201    
202     static void do_getscrap(void **handle, uint32 type, int32 offset)
203     {
204     // Get appropriate format for requested data
205     UINT uFormat = 0;
206     switch (type) {
207     case FOURCC('T','E','X','T'):
208     uFormat = CF_TEXT;
209     break;
210     }
211     if (uFormat != CF_TEXT) // 'TEXT' only
212     return;
213    
214     // Get the native clipboard data
215     HWND hMainWindow = GetMainWindowHandle();
216     if (!hMainWindow || !OpenClipboard(hMainWindow))
217     return;
218     HANDLE hData = GetClipboardData(uFormat);
219     if (hData) {
220     uint8 *data = (uint8 *)GlobalLock(hData);
221     if (data) {
222     uint32 length = GlobalSize(hData);
223     if (length) {
224     int32 out_length = 0;
225    
226     // Allocate space for new scrap in MacOS side
227     M68kRegisters r;
228     r.d[0] = length;
229     Execute68kTrap(0xa71e, &r); // NewPtrSysClear()
230     uint32 scrap_area = r.a[0];
231    
232     if (scrap_area) {
233     switch (type) {
234     case FOURCC('T','E','X','T'):
235     D(bug(" clipping TEXT\n"));
236    
237     // Convert text from ISO-Latin1 to Mac charset
238     uint8 *p = Mac2HostAddr(scrap_area);
239     for (int i = 0; i < length; i++) {
240     uint8 c = data[i];
241     if (c < 0x80) {
242     if (c == 13 && i < length - 1 && data[i + 1] == 10) { // CR/LF -> CR
243     c = 13;
244     i++;
245     }
246     } else if (!no_clip_conversion)
247     c = iso2mac[c & 0x7f];
248     *p++ = c;
249     out_length++;
250     }
251     break;
252     }
253    
254     // Add new data to clipboard
255     static uint8 proc[] = {
256     0x59, 0x8f, // subq.l #4,sp
257     0xa9, 0xfc, // ZeroScrap()
258     0x2f, 0x3c, 0, 0, 0, 0, // move.l #length,-(sp)
259     0x2f, 0x3c, 0, 0, 0, 0, // move.l #type,-(sp)
260     0x2f, 0x3c, 0, 0, 0, 0, // move.l #outbuf,-(sp)
261     0xa9, 0xfe, // PutScrap()
262     0x58, 0x8f, // addq.l #4,sp
263     M68K_RTS >> 8, M68K_RTS
264     };
265     uint32 proc_area = Host2MacAddr(proc);
266     WriteMacInt32(proc_area + 6, out_length);
267     WriteMacInt32(proc_area + 12, type);
268     WriteMacInt32(proc_area + 18, scrap_area);
269     we_put_this_data = true;
270     Execute68k(proc_area, &r);
271    
272     // We are done with scratch memory
273     r.a[0] = scrap_area;
274     Execute68kTrap(0xa01f, &r); // DisposePtr
275     }
276     }
277     GlobalUnlock(hData);
278     }
279     }
280     CloseClipboard();
281     }