forked from Bill-Gray/PDCursesMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdcclip.c
253 lines (193 loc) · 6.49 KB
/
pdcclip.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
/* PDCurses */
#include "pdcx11.h"
#include <stdlib.h>
/*man-start**************************************************************
clipboard
---------
### Synopsis
int PDC_getclipboard(char **contents, long *length);
int PDC_setclipboard(const char *contents, long length);
int PDC_freeclipboard(char *contents);
int PDC_clearclipboard(void);
### Description
PDC_getclipboard() gets the textual contents of the system's
clipboard. This function returns the contents of the clipboard in the
contents argument. It is the responsibility of the caller to free the
memory returned, via PDC_freeclipboard(). The length of the clipboard
contents is returned in the length argument.
PDC_setclipboard copies the supplied text into the system's
clipboard, emptying the clipboard prior to the copy.
PDC_clearclipboard() clears the internal clipboard.
### Return Values
indicator of success/failure of call.
PDC_CLIP_SUCCESS the call was successful
PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for
the clipboard contents
PDC_CLIP_EMPTY the clipboard contains no text
PDC_CLIP_ACCESS_ERROR no clipboard support
### Portability
X/Open ncurses NetBSD
PDC_getclipboard - - -
PDC_setclipboard - - -
PDC_freeclipboard - - -
PDC_clearclipboard - - -
**man-end****************************************************************/
#include "Xmu/StdSel.h"
#include "Xmu/Atoms.h"
static char *tmpsel = NULL;
static unsigned long tmpsel_length = 0;
static char *xc_selection = NULL;
static long xc_selection_len = 0;
#ifndef X_HAVE_UTF8_STRING
static Atom XA_UTF8_STRING(Display *dpy)
{
static AtomPtr p = NULL;
if (!p)
p = XmuMakeAtom("UTF8_STRING");
return XmuInternAtom(dpy, p);
}
#endif
static Boolean _convert_proc(Widget w, Atom *selection, Atom *target,
Atom *type_return, XtPointer *value_return,
unsigned long *length_return, int *format_return)
{
PDC_LOG(("_convert_proc() - called\n"));
if (*target == XA_TARGETS(XtDisplay(pdc_toplevel)))
{
XSelectionRequestEvent *req = XtGetSelectionRequest(w,
*selection, (XtRequestId)NULL);
Atom *targetP;
XPointer std_targets;
unsigned long std_length;
XmuConvertStandardSelection(pdc_toplevel, req->time, selection,
target, type_return, &std_targets,
&std_length, format_return);
*length_return = std_length + 2;
*value_return = XtMalloc(sizeof(Atom) * (*length_return));
targetP = *(Atom**)value_return;
*targetP++ = XA_STRING;
*targetP++ = XA_UTF8_STRING(XtDisplay(pdc_toplevel));
memmove((void *)targetP, (const void *)std_targets,
sizeof(Atom) * std_length);
XtFree((char *)std_targets);
*type_return = XA_ATOM;
*format_return = 8;
return True;
}
else if (*target == XA_UTF8_STRING(XtDisplay(pdc_toplevel)) ||
*target == XA_STRING)
{
char *data = XtMalloc(tmpsel_length + 1);
char *tmp = tmpsel;
int ret_length = 0;
while (*tmp)
data[ret_length++] = *tmp++;
data[ret_length] = '\0';
*value_return = data;
*length_return = ret_length;
*format_return = 8;
*type_return = *target;
return True;
}
else
return XmuConvertStandardSelection(pdc_toplevel, CurrentTime,
selection, target, type_return, (XPointer*)value_return,
length_return, format_return);
}
static void _lose_ownership(Widget w, Atom *type)
{
PDC_LOG(("_lose_ownership() - called\n"));
INTENTIONALLY_UNUSED_PARAMETER( w);
INTENTIONALLY_UNUSED_PARAMETER( type);
if (tmpsel)
free(tmpsel);
tmpsel = NULL;
tmpsel_length = 0;
}
static void _get_selection(Widget w, XtPointer data, Atom *selection,
Atom *type, XtPointer value,
unsigned long *length, int *format)
{
PDC_LOG(("_get_selection() - called\n"));
INTENTIONALLY_UNUSED_PARAMETER( w);
INTENTIONALLY_UNUSED_PARAMETER( data);
INTENTIONALLY_UNUSED_PARAMETER( selection);
INTENTIONALLY_UNUSED_PARAMETER( type);
INTENTIONALLY_UNUSED_PARAMETER( format);
if (value)
{
xc_selection = value;
xc_selection_len = (long)(*length);
}
else
xc_selection_len = 0;
}
int PDC_getclipboard(char **contents, long *length)
{
XEvent event;
PDC_LOG(("PDC_getclipboard() - called\n"));
xc_selection = NULL;
xc_selection_len = -1;
XtGetSelectionValue(pdc_toplevel, XA_PRIMARY,
#ifdef PDC_WIDE
XA_UTF8_STRING(XtDisplay(pdc_toplevel)),
#else
XA_STRING,
#endif
_get_selection, (XtPointer)NULL, 0);
while (-1 == xc_selection_len)
{
XtAppNextEvent(pdc_app_context, &event);
XtDispatchEvent(&event);
}
if (xc_selection && xc_selection_len)
{
*contents = malloc(xc_selection_len + 1);
if (!*contents)
return PDC_CLIP_MEMORY_ERROR;
memcpy(*contents, xc_selection, xc_selection_len);
(*contents)[xc_selection_len] = '\0';
*length = xc_selection_len;
return PDC_CLIP_SUCCESS;
}
return PDC_CLIP_EMPTY;
}
int PDC_setclipboard(const char *contents, long length)
{
long pos;
int status;
PDC_LOG(("PDC_setclipboard() - called\n"));
if (length > (long)tmpsel_length)
{
if (!tmpsel_length)
tmpsel = malloc(length + 1);
else
tmpsel = realloc(tmpsel, length + 1);
}
for (pos = 0; pos < length; pos++)
tmpsel[pos] = contents[pos];
tmpsel_length = length;
tmpsel[length] = 0;
if (XtOwnSelection(pdc_toplevel, XA_PRIMARY, CurrentTime,
_convert_proc, _lose_ownership, NULL) == False)
{
status = PDC_CLIP_ACCESS_ERROR;
free(tmpsel);
tmpsel = NULL;
tmpsel_length = 0;
}
else
status = PDC_CLIP_SUCCESS;
return status;
}
int PDC_freeclipboard(char *contents)
{
PDC_LOG(("PDC_freeclipboard() - called\n"));
free(contents);
return PDC_CLIP_SUCCESS;
}
int PDC_clearclipboard(void)
{
PDC_LOG(("PDC_clearclipboard() - called\n"));
return PDC_CLIP_SUCCESS;
}