1 /* Public Domain Curses */
5 RCSID("$Id: pdcclip.c,v 1.35 2008/07/14 04:24:52 wmcbrine Exp $")
9 /*man-start**************************************************************
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);
20 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
23 caller to free the memory returned, via PDC_freeclipboard().
24 The length of the clipboard contents is returned in the length
27 PDC_setclipboard copies the supplied text into the system's
28 clipboard, emptying the clipboard prior to the copy.
30 PDC_clearclipboard() clears the internal clipboard.
33 indicator of success/failure of call.
34 PDC_CLIP_SUCCESS the call was successful
35 PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for
36 the clipboard contents
37 PDC_CLIP_EMPTY the clipboard contains no text
38 PDC_CLIP_ACCESS_ERROR no clipboard support
40 Portability X/Open BSD SYS V
41 PDC_getclipboard - - -
42 PDC_setclipboard - - -
43 PDC_freeclipboard - - -
44 PDC_clearclipboard - - -
46 **man-end****************************************************************/
48 int PDC_getclipboard(char **contents
, long *length
)
56 PDC_LOG(("PDC_getclipboard() - called\n"));
58 XCursesInstructAndWait(CURSES_GET_SELECTION
);
60 if (XC_read_socket(xc_display_sock
, &result
, sizeof(int)) < 0)
61 XCursesExitCursesProcess(5, "exiting from PDC_getclipboard");
63 if (result
== PDC_CLIP_SUCCESS
)
65 if (XC_read_socket(xc_display_sock
, &len
, sizeof(int)) < 0)
66 XCursesExitCursesProcess(5, "exiting from PDC_getclipboard");
68 wcontents
= malloc((len
+ 1) * sizeof(wchar_t));
69 *contents
= malloc(len
* 3 + 1);
71 if (!wcontents
|| !*contents
)
73 *contents
= malloc(len
+ 1);
77 XCursesExitCursesProcess(6, "exiting from PDC_getclipboard - "
78 "synchronization error");
82 if (XC_read_socket(xc_display_sock
,
84 wcontents
, len
* sizeof(wchar_t)) < 0)
88 XCursesExitCursesProcess(5, "exiting from PDC_getclipboard");
93 len
= PDC_wcstombs(*contents
, wcontents
, len
* 3);
96 (*contents
)[len
] = '\0';
103 int PDC_setclipboard(const char *contents
, long length
)
110 PDC_LOG(("PDC_setclipboard() - called\n"));
113 wcontents
= malloc((length
+ 1) * sizeof(wchar_t));
115 return PDC_CLIP_MEMORY_ERROR
;
117 length
= PDC_mbstowcs(wcontents
, contents
, length
);
119 XCursesInstruct(CURSES_SET_SELECTION
);
121 /* Write, then wait for X to do its stuff; expect return code. */
123 if (XC_write_socket(xc_display_sock
, &length
, sizeof(long)) >= 0)
125 if (XC_write_socket(xc_display_sock
,
127 wcontents
, length
* sizeof(wchar_t)) >= 0)
131 contents
, length
) >= 0)
134 if (XC_read_socket(xc_display_sock
, &rc
, sizeof(int)) >= 0)
139 XCursesExitCursesProcess(5, "exiting from PDC_setclipboard");
141 return PDC_CLIP_ACCESS_ERROR
; /* not reached */
144 int PDC_freeclipboard(char *contents
)
146 PDC_LOG(("PDC_freeclipboard() - called\n"));
149 return PDC_CLIP_SUCCESS
;
152 int PDC_clearclipboard(void)
157 PDC_LOG(("PDC_clearclipboard() - called\n"));
159 XCursesInstruct(CURSES_CLEAR_SELECTION
);
161 /* Write, then wait for X to do its stuff; expect return code. */
163 if (XC_write_socket(xc_display_sock
, &len
, sizeof(long)) >= 0)
164 if (XC_read_socket(xc_display_sock
, &rc
, sizeof(int)) >= 0)
167 XCursesExitCursesProcess(5, "exiting from PDC_clearclipboard");
169 return PDC_CLIP_ACCESS_ERROR
; /* not reached */