drivers/wifi: Remove unnecessary data structure copy
[coreboot2.git] / payloads / libpayload / curses / PDCurses / x11 / pdcclip.c
blobbba080f3a2680b766072fb71c31a8f153825002c
1 /* Public Domain Curses */
3 #include "pdcx11.h"
5 RCSID("$Id: pdcclip.c,v 1.35 2008/07/14 04:24:52 wmcbrine Exp $")
7 #include <stdlib.h>
9 /*man-start**************************************************************
11 Name: clipboard
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);
19 Description:
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
25 argument.
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.
32 Return Values:
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)
50 #ifdef PDC_WIDE
51 wchar_t *wcontents;
52 #endif
53 int result = 0;
54 int len;
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");
67 #ifdef PDC_WIDE
68 wcontents = malloc((len + 1) * sizeof(wchar_t));
69 *contents = malloc(len * 3 + 1);
71 if (!wcontents || !*contents)
72 #else
73 *contents = malloc(len + 1);
75 if (!*contents)
76 #endif
77 XCursesExitCursesProcess(6, "exiting from PDC_getclipboard - "
78 "synchronization error");
80 if (len)
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");
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;
100 return result;
103 int PDC_setclipboard(const char *contents, long length)
105 #ifdef PDC_WIDE
106 wchar_t *wcontents;
107 #endif
108 int rc;
110 PDC_LOG(("PDC_setclipboard() - called\n"));
112 #ifdef PDC_WIDE
113 wcontents = malloc((length + 1) * sizeof(wchar_t));
114 if (!wcontents)
115 return PDC_CLIP_MEMORY_ERROR;
117 length = PDC_mbstowcs(wcontents, contents, length);
118 #endif
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,
126 #ifdef PDC_WIDE
127 wcontents, length * sizeof(wchar_t)) >= 0)
129 free(wcontents);
130 #else
131 contents, length) >= 0)
133 #endif
134 if (XC_read_socket(xc_display_sock, &rc, sizeof(int)) >= 0)
135 return rc;
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"));
148 free(contents);
149 return PDC_CLIP_SUCCESS;
152 int PDC_clearclipboard(void)
154 int rc;
155 long len = 0;
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)
165 return rc;
167 XCursesExitCursesProcess(5, "exiting from PDC_clearclipboard");
169 return PDC_CLIP_ACCESS_ERROR; /* not reached */