| 1 |
|
| 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 |
|
| 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 |
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 |
|
| 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; |
| 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 |
|
| 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; |
| 170 |
} |