Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 1 | /* Public Domain Curses */ |
| 2 | |
| 3 | #include "pdcos2.h" |
| 4 | |
| 5 | RCSID("$Id: pdcclip.c,v 1.33 2008/07/14 04:24:51 wmcbrine Exp $") |
| 6 | |
| 7 | /*man-start************************************************************** |
| 8 | |
| 9 | Name: clipboard |
| 10 | |
| 11 | Synopsis: |
| 12 | int PDC_getclipboard(char **contents, long *length); |
| 13 | int PDC_setclipboard(const char *contents, long length); |
| 14 | int PDC_freeclipboard(char *contents); |
| 15 | int PDC_clearclipboard(void); |
| 16 | |
| 17 | Description: |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 18 | PDC_getclipboard() gets the textual contents of the system's |
| 19 | clipboard. This function returns the contents of the clipboard |
| 20 | in the contents argument. It is the responsibilitiy of the |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 21 | caller to free the memory returned, via PDC_freeclipboard(). |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 22 | The length of the clipboard contents is returned in the length |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 23 | argument. |
| 24 | |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 25 | PDC_setclipboard copies the supplied text into the system's |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 26 | clipboard, emptying the clipboard prior to the copy. |
| 27 | |
| 28 | PDC_clearclipboard() clears the internal clipboard. |
| 29 | |
| 30 | Return Values: |
| 31 | indicator of success/failure of call. |
| 32 | PDC_CLIP_SUCCESS the call was successful |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 33 | PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 34 | the clipboard contents |
| 35 | PDC_CLIP_EMPTY the clipboard contains no text |
| 36 | PDC_CLIP_ACCESS_ERROR no clipboard support |
| 37 | |
| 38 | Portability X/Open BSD SYS V |
| 39 | PDC_getclipboard - - - |
| 40 | PDC_setclipboard - - - |
| 41 | PDC_freeclipboard - - - |
| 42 | PDC_clearclipboard - - - |
| 43 | |
| 44 | **man-end****************************************************************/ |
| 45 | |
| 46 | int PDC_getclipboard(char **contents, long *length) |
| 47 | { |
| 48 | #ifndef EMXVIDEO |
| 49 | HMQ hmq; |
| 50 | HAB hab; |
| 51 | PTIB ptib; |
| 52 | PPIB ppib; |
| 53 | ULONG ulRet; |
| 54 | long len; |
| 55 | int rc; |
| 56 | #endif |
| 57 | PDC_LOG(("PDC_getclipboard() - called\n")); |
| 58 | |
| 59 | #ifndef EMXVIDEO |
| 60 | DosGetInfoBlocks(&ptib, &ppib); |
| 61 | ppib->pib_ultype = 3; |
| 62 | hab = WinInitialize(0); |
| 63 | hmq = WinCreateMsgQueue(hab, 0); |
| 64 | |
| 65 | if (!WinOpenClipbrd(hab)) |
| 66 | { |
| 67 | WinDestroyMsgQueue(hmq); |
| 68 | WinTerminate(hab); |
| 69 | return PDC_CLIP_ACCESS_ERROR; |
| 70 | } |
| 71 | |
| 72 | rc = PDC_CLIP_EMPTY; |
| 73 | |
| 74 | ulRet = WinQueryClipbrdData(hab, CF_TEXT); |
| 75 | |
| 76 | if (ulRet) |
| 77 | { |
| 78 | len = strlen((char *)ulRet); |
| 79 | *contents = malloc(len + 1); |
| 80 | |
| 81 | if (!*contents) |
| 82 | rc = PDC_CLIP_MEMORY_ERROR; |
| 83 | else |
| 84 | { |
| 85 | strcpy((char *)*contents, (char *)ulRet); |
| 86 | *length = len; |
| 87 | rc = PDC_CLIP_SUCCESS; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | WinCloseClipbrd(hab); |
| 92 | WinDestroyMsgQueue(hmq); |
| 93 | WinTerminate(hab); |
| 94 | |
| 95 | return rc; |
| 96 | #else |
| 97 | return PDC_CLIP_ACCESS_ERROR; |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | int PDC_setclipboard(const char *contents, long length) |
| 102 | { |
| 103 | #ifndef EMXVIDEO |
| 104 | HAB hab; |
| 105 | PTIB ptib; |
| 106 | PPIB ppib; |
| 107 | ULONG ulRC; |
| 108 | PSZ szTextOut = NULL; |
| 109 | int rc; |
| 110 | #endif |
| 111 | PDC_LOG(("PDC_setclipboard() - called\n")); |
| 112 | |
| 113 | #ifndef EMXVIDEO |
| 114 | DosGetInfoBlocks(&ptib, &ppib); |
| 115 | ppib->pib_ultype = 3; |
| 116 | hab = WinInitialize(0); |
| 117 | |
| 118 | if (!WinOpenClipbrd(hab)) |
| 119 | { |
| 120 | WinTerminate(hab); |
| 121 | return PDC_CLIP_ACCESS_ERROR; |
| 122 | } |
| 123 | |
| 124 | rc = PDC_CLIP_MEMORY_ERROR; |
| 125 | |
| 126 | ulRC = DosAllocSharedMem((PVOID)&szTextOut, NULL, length + 1, |
| 127 | PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE); |
| 128 | |
| 129 | if (ulRC == 0) |
| 130 | { |
| 131 | strcpy(szTextOut, contents); |
| 132 | WinEmptyClipbrd(hab); |
| 133 | |
| 134 | if (WinSetClipbrdData(hab, (ULONG)szTextOut, CF_TEXT, CFI_POINTER)) |
| 135 | rc = PDC_CLIP_SUCCESS; |
| 136 | else |
| 137 | { |
| 138 | DosFreeMem(szTextOut); |
| 139 | rc = PDC_CLIP_ACCESS_ERROR; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | WinCloseClipbrd(hab); |
| 144 | WinTerminate(hab); |
| 145 | |
| 146 | return rc; |
| 147 | #else |
| 148 | return PDC_CLIP_ACCESS_ERROR; |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | int PDC_freeclipboard(char *contents) |
| 153 | { |
| 154 | PDC_LOG(("PDC_freeclipboard() - called\n")); |
| 155 | |
| 156 | if (contents) |
| 157 | free(contents); |
| 158 | |
| 159 | return PDC_CLIP_SUCCESS; |
| 160 | } |
| 161 | |
| 162 | int PDC_clearclipboard(void) |
| 163 | { |
| 164 | #ifndef EMXVIDEO |
| 165 | HAB hab; |
| 166 | PTIB ptib; |
| 167 | PPIB ppib; |
| 168 | #endif |
| 169 | PDC_LOG(("PDC_clearclipboard() - called\n")); |
| 170 | |
| 171 | #ifndef EMXVIDEO |
| 172 | DosGetInfoBlocks(&ptib, &ppib); |
| 173 | ppib->pib_ultype = 3; |
| 174 | hab = WinInitialize(0); |
| 175 | |
| 176 | WinEmptyClipbrd(hab); |
| 177 | |
| 178 | WinCloseClipbrd(hab); |
| 179 | WinTerminate(hab); |
| 180 | |
| 181 | return PDC_CLIP_SUCCESS; |
| 182 | #else |
| 183 | return PDC_CLIP_ACCESS_ERROR; |
| 184 | #endif |
| 185 | } |