1a3ce92a2da735359d9e7cdfb13a8cd2d7ad1248
[coreboot.git] / payloads / libpayload / curses / PDCurses-3.4 / x11 / pdcclip.c
1 /* Public Domain Curses */
2
3 #include "pdcx11.h"
4
5 RCSID("$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:
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.
26
27         PDC_setclipboard copies the supplied text into the system's 
28         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
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
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
48 int 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
103 int 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
144 int PDC_freeclipboard(char *contents)
145 {
146     PDC_LOG(("PDC_freeclipboard() - called\n"));
147
148     free(contents);
149     return PDC_CLIP_SUCCESS;
150 }
151
152 int 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 }