blob: bba080f3a2680b766072fb71c31a8f153825002c [file] [log] [blame]
Patrick Georgi3b77b722011-07-07 15:41:53 +02001/* Public Domain Curses */
2
3#include "pdcx11.h"
4
5RCSID("$Id: pdcclip.c,v 1.35 2008/07/14 04:24:52 wmcbrine Exp $")
6
7#include <stdlib.h>
8
9/*man-start**************************************************************
10
11 Name: clipboard
12
13 Synopsis:
14 int PDC_getclipboard(char **contents, long *length);
15 int PDC_setclipboard(const char *contents, long length);
16 int PDC_freeclipboard(char *contents);
17 int PDC_clearclipboard(void);
18
19 Description:
Stefan Reinauere11835e2011-10-31 12:54:00 -070020 PDC_getclipboard() gets the textual contents of the system's
21 clipboard. This function returns the contents of the clipboard
22 in the contents argument. It is the responsibilitiy of the
Patrick Georgi3b77b722011-07-07 15:41:53 +020023 caller to free the memory returned, via PDC_freeclipboard().
Stefan Reinauere11835e2011-10-31 12:54:00 -070024 The length of the clipboard contents is returned in the length
Patrick Georgi3b77b722011-07-07 15:41:53 +020025 argument.
26
Stefan Reinauere11835e2011-10-31 12:54:00 -070027 PDC_setclipboard copies the supplied text into the system's
Patrick Georgi3b77b722011-07-07 15:41:53 +020028 clipboard, emptying the clipboard prior to the copy.
29
30 PDC_clearclipboard() clears the internal clipboard.
31
32 Return Values:
33 indicator of success/failure of call.
34 PDC_CLIP_SUCCESS the call was successful
Stefan Reinauere11835e2011-10-31 12:54:00 -070035 PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for
Patrick Georgi3b77b722011-07-07 15:41:53 +020036 the clipboard contents
37 PDC_CLIP_EMPTY the clipboard contains no text
38 PDC_CLIP_ACCESS_ERROR no clipboard support
39
40 Portability X/Open BSD SYS V
41 PDC_getclipboard - - -
42 PDC_setclipboard - - -
43 PDC_freeclipboard - - -
44 PDC_clearclipboard - - -
45
46**man-end****************************************************************/
47
48int PDC_getclipboard(char **contents, long *length)
49{
50#ifdef PDC_WIDE
51 wchar_t *wcontents;
52#endif
53 int result = 0;
54 int len;
55
56 PDC_LOG(("PDC_getclipboard() - called\n"));
57
58 XCursesInstructAndWait(CURSES_GET_SELECTION);
59
60 if (XC_read_socket(xc_display_sock, &result, sizeof(int)) < 0)
61 XCursesExitCursesProcess(5, "exiting from PDC_getclipboard");
62
63 if (result == PDC_CLIP_SUCCESS)
64 {
65 if (XC_read_socket(xc_display_sock, &len, sizeof(int)) < 0)
66 XCursesExitCursesProcess(5, "exiting from PDC_getclipboard");
67#ifdef PDC_WIDE
68 wcontents = malloc((len + 1) * sizeof(wchar_t));
69 *contents = malloc(len * 3 + 1);
70
71 if (!wcontents || !*contents)
72#else
73 *contents = malloc(len + 1);
74
75 if (!*contents)
76#endif
77 XCursesExitCursesProcess(6, "exiting from PDC_getclipboard - "
78 "synchronization error");
79
80 if (len)
81 {
82 if (XC_read_socket(xc_display_sock,
83#ifdef PDC_WIDE
84 wcontents, len * sizeof(wchar_t)) < 0)
85#else
86 *contents, len) < 0)
87#endif
88 XCursesExitCursesProcess(5, "exiting from PDC_getclipboard");
89 }
90
91#ifdef PDC_WIDE
92 wcontents[len] = 0;
93 len = PDC_wcstombs(*contents, wcontents, len * 3);
94 free(wcontents);
95#endif
96 (*contents)[len] = '\0';
97 *length = len;
98 }
99
100 return result;
101}
102
103int PDC_setclipboard(const char *contents, long length)
104{
105#ifdef PDC_WIDE
106 wchar_t *wcontents;
107#endif
108 int rc;
109
110 PDC_LOG(("PDC_setclipboard() - called\n"));
111
112#ifdef PDC_WIDE
113 wcontents = malloc((length + 1) * sizeof(wchar_t));
114 if (!wcontents)
115 return PDC_CLIP_MEMORY_ERROR;
116
117 length = PDC_mbstowcs(wcontents, contents, length);
118#endif
119 XCursesInstruct(CURSES_SET_SELECTION);
120
121 /* Write, then wait for X to do its stuff; expect return code. */
122
123 if (XC_write_socket(xc_display_sock, &length, sizeof(long)) >= 0)
124 {
125 if (XC_write_socket(xc_display_sock,
126#ifdef PDC_WIDE
127 wcontents, length * sizeof(wchar_t)) >= 0)
128 {
129 free(wcontents);
130#else
131 contents, length) >= 0)
132 {
133#endif
134 if (XC_read_socket(xc_display_sock, &rc, sizeof(int)) >= 0)
135 return rc;
136 }
137 }
138
139 XCursesExitCursesProcess(5, "exiting from PDC_setclipboard");
140
141 return PDC_CLIP_ACCESS_ERROR; /* not reached */
142}
143
144int PDC_freeclipboard(char *contents)
145{
146 PDC_LOG(("PDC_freeclipboard() - called\n"));
147
148 free(contents);
149 return PDC_CLIP_SUCCESS;
150}
151
152int PDC_clearclipboard(void)
153{
154 int rc;
155 long len = 0;
156
157 PDC_LOG(("PDC_clearclipboard() - called\n"));
158
159 XCursesInstruct(CURSES_CLEAR_SELECTION);
160
161 /* Write, then wait for X to do its stuff; expect return code. */
162
163 if (XC_write_socket(xc_display_sock, &len, sizeof(long)) >= 0)
164 if (XC_read_socket(xc_display_sock, &rc, sizeof(int)) >= 0)
165 return rc;
166
167 XCursesExitCursesProcess(5, "exiting from PDC_clearclipboard");
168
169 return PDC_CLIP_ACCESS_ERROR; /* not reached */
170}