forked from raduprv/Eternal-Lands
/
paste.c
353 lines (301 loc) · 8.49 KB
/
paste.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <stdlib.h>
#include <string.h>
#include <SDL_syswm.h>
#include "gl_init.h"
#include "paste.h"
#include "chat.h"
#include "translate.h"
void do_paste(const Uint8* buffer)
{
paste_in_input_field(buffer);
}
void do_paste_to_text_field(widget_list *widget, const char* text)
{
text_field *tf;
int bytes = strlen(text);
text_message* msg;
int p;
if ((widget == NULL) || (widget->widget_info == NULL))
return;
// if not editable, don't allow paste
if (!(widget->Flags & TEXT_FIELD_EDITABLE))
return;
tf = (text_field *) widget->widget_info;
msg = &tf->buffer[tf->msg];
// if can't grow and would over fill, just use what we can
if ((msg->len + bytes >= msg->size) && !(widget->Flags & TEXT_FIELD_CAN_GROW))
bytes = msg->size - msg->len - 1;
resize_text_message_data (msg, msg->len + bytes);
p = tf->cursor;
memmove (&msg->data[p + bytes], &msg->data[p], msg->len - p + 1);
memcpy (&msg->data[p], text, bytes);
msg->len += bytes;
tf->cursor += bytes;
text_field_find_cursor_line (tf);
}
#if defined OSX
void start_paste(widget_list *widget)
{
OSStatus err = noErr;
PasteboardRef gClipboard;
PasteboardItemID itemID;
CFDataRef flavorData;
char* flavorText;
err = PasteboardCreate( kPasteboardClipboard, &gClipboard );
//require_noerr( err, CantCreateClipboard );
err = PasteboardGetItemIdentifier( gClipboard, 1, &itemID );
err = PasteboardCopyItemFlavorData( gClipboard, itemID, CFSTR("com.apple.traditional-mac-plain-text"), &flavorData );
int flavorDataSize = CFDataGetLength(flavorData);
flavorText=(char*)malloc(flavorDataSize+1);
short dataIndex;
for(dataIndex = 0; dataIndex <= flavorDataSize; dataIndex++ )
{
char byte = *(CFDataGetBytePtr( flavorData ) + dataIndex);
flavorText[dataIndex] = (byte>32) ? byte : ' ';
}
flavorText[flavorDataSize] = '\0';
CFRelease(flavorData);
if (widget == NULL)
{
do_paste (flavorText);
}
else
{
do_paste_to_text_field(widget, flavorText);
}
free(flavorText);
CFRelease( gClipboard );
}
void copy_to_clipboard(const char* text)
{
OSStatus err = noErr;
PasteboardRef gClipboard;
CFDataRef textData;
err = PasteboardCreate( kPasteboardClipboard, &gClipboard );
err = PasteboardClear( gClipboard );
// allocate data based on the size of the selection
textData = CFDataCreate( kCFAllocatorSystemDefault, (UInt8*)text, strlen(text));
// add text data to the pasteboard
err = PasteboardPutItemFlavor( gClipboard, (PasteboardItemID)1,
CFSTR("com.apple.traditional-mac-plain-text"), textData, 0 );
CFRelease(textData);
CFRelease( gClipboard );
}
#elif defined WINDOWS
void start_paste(widget_list *widget)
{
if (OpenClipboard(NULL))
{
HANDLE hText = GetClipboardData (CF_TEXT);
char* text = GlobalLock (hText);
if (widget == NULL)
do_paste(text);
else
do_paste_to_text_field(widget, text);
GlobalUnlock (hText);
CloseClipboard ();
}
}
void copy_to_clipboard(const char* text)
{
SDL_SysWMinfo info;
if (text == NULL)
return;
SDL_VERSION (&info.version);
if (SDL_GetWindowWMInfo (el_gl_window, &info))
{
if (OpenClipboard (info.info.win.window))
{
HGLOBAL hCopy = GlobalAlloc (GMEM_MOVEABLE, 1+strlen (text));
char* copy = GlobalLock (hCopy);
strcpy (copy, text);
GlobalUnlock (hCopy);
EmptyClipboard ();
SetClipboardData (CF_TEXT, hCopy);
CloseClipboard ();
}
}
}
#else
static widget_list *paste_to_widget = NULL;
static char* cur_text_primary = NULL;
static char* cur_text_clipboard = NULL;
int use_clipboard = 1;
Atom targets_atom = None;
void processpaste(Display *dpy, Window window, Atom atom)
{
Atom type;
int actualformat;
unsigned long items;
unsigned long bytes, tmp;
unsigned char * value = NULL;
XGetWindowProperty(dpy, window, atom, 0, 0, 0, XA_STRING, &type, &actualformat, &items, &bytes, &value);
XFree(value);
// From the XGetWindowProperty man page:
// *) The length parameter is in 32 bit units, so we can divide
// bytes by four (rounding up
// *) It always allocates one extra byte and sets it to zero, so
// using value as a zero-terminated string should be safe
XGetWindowProperty(dpy, window, atom, 0, (bytes+3)/4, 1, XA_STRING, &type, &actualformat, &items, &tmp, &value);
if(type == XA_STRING)
{
if (paste_to_widget == NULL)
{
do_paste(value); // copy to input line
}
else
{
do_paste_to_text_field(paste_to_widget, (const char*) value);
paste_to_widget = NULL;
}
}
/* XGetWindowProperty allocated this, so we have to free it */
if(value)
{
XFree(value);
}
}
static void start_paste_from_target(widget_list *widget, int clipboard)
{
Display *dpy;
Window window;
SDL_SysWMinfo wminfo;
Atom selection;
Atom property;
SDL_VERSION(&wminfo.version);
if (SDL_GetWMInfo(&wminfo) && wminfo.subsystem == SDL_SYSWM_X11)
{
wminfo.info.x11.lock_func();
dpy = wminfo.info.x11.display;
window = wminfo.info.x11.window;
paste_to_widget = widget;
/* Set selection to XA_PRIMARY to use xterm-style select,
* or CLIPBOARD to use Gnome-style cut'n'paste.
* KDE3 should use CLIPBOARD also, I'm not sure about older
* KDE versions */
if (clipboard)
selection = XInternAtom(dpy, "CLIPBOARD", 0);
else
selection = XA_PRIMARY;
property = XInternAtom(dpy, "PASTE", 0);
XConvertSelection(dpy, selection, XA_STRING, property, window, CurrentTime);
/* - If we used the CLIPBOARD, we don't get a SelectionNotify
* event, so we have to call processpaste immediately.
* - If we used XA_PRIMARY, the selection-holder will send us a
* SelectionNotify-event, as soon as the selection is
* available for us. Then finishpaste calls processpaste
*/
// Alia: we should receive SelectionNotify event, property is NULL until it comes.
// Let's try to comment it :)
// //if(clipboard) {
// processpaste(dpy, window, property);
//}
wminfo.info.x11.unlock_func();
}
}
void start_paste(widget_list *widget)
{
start_paste_from_target(widget, use_clipboard);
}
void start_paste_from_primary(widget_list *widget)
{
start_paste_from_target(widget, 0);
}
static void copy_to_clipboard_target(const char* text, int clipboard)
{
Display* dpy;
Window window;
SDL_SysWMinfo wminfo;
Atom selection;
SDL_VERSION(&wminfo.version);
if (SDL_GetWMInfo(&wminfo) && (wminfo.subsystem == SDL_SYSWM_X11))
{
wminfo.info.x11.lock_func();
dpy = wminfo.info.x11.display;
window = wminfo.info.x11.window;
if (targets_atom == None)
targets_atom = XInternAtom(dpy, "TARGETS", False);
if (clipboard)
{
selection = XInternAtom(dpy, "CLIPBOARD", False);
if (cur_text_clipboard) free(cur_text_clipboard);
cur_text_clipboard = strdup(text);
}
else
{
selection = XA_PRIMARY;
if (cur_text_primary) free(cur_text_primary);
cur_text_primary = strdup(text);
}
//property = XInternAtom(dpy, "PASTE", 0);
XSetSelectionOwner(dpy, selection, window, CurrentTime);
wminfo.info.x11.unlock_func();
}
}
void copy_to_clipboard(const char* text)
{
copy_to_clipboard_target(text, use_clipboard);
}
void copy_to_primary(const char* text)
{
copy_to_clipboard_target(text, 0);
}
void process_copy(XSelectionRequestEvent* e)
{
XEvent r;
Atom targets[] = {
targets_atom,
XA_STRING
};
if (e->target == XA_STRING)
{
// Copy the string
char *buf = e->selection == XA_PRIMARY
? cur_text_primary : cur_text_clipboard;
XChangeProperty(e->display, e->requestor, e->property,
XA_STRING, 8, PropModeReplace,
(unsigned char *)buf, strlen(buf));
r.xselection.property = e->property;
}
else if (targets_atom != None && e->target == targets_atom)
{
// Tell X we have a string available
XChangeProperty(e->display, e->requestor, e->property, XA_ATOM,
32, PropModeReplace, (unsigned char*)targets,
sizeof(targets) / sizeof(targets[0]));
r.xselection.property = e->property;
}
else
{
// No idea what X is requesting
r.xselection.property = None;
}
r.xselection.type = SelectionNotify;
r.xselection.display = e->display;
r.xselection.requestor = e->requestor;
r.xselection.selection =e->selection;
r.xselection.target= e->target;
r.xselection.time = e->time;
XSendEvent (e->display, e->requestor, 0, 0, &r);
}
void finishpaste(XSelectionEvent event)
{
Display * dpy;
Window window;
SDL_SysWMinfo wminfo;
SDL_VERSION(&wminfo.version);
if (SDL_GetWMInfo(&wminfo) && wminfo.subsystem==SDL_SYSWM_X11)
{
wminfo.info.x11.lock_func();
dpy=wminfo.info.x11.display;
window=wminfo.info.x11.window;
if (event.property == None)
{
fprintf(stderr,"%s\n",not_ascii);
return;
}
processpaste(dpy, window, event.property);
wminfo.info.x11.unlock_func();
}
}
#endif // def OSX / def WINDOWS / other