Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 1 | /* Public Domain Curses */ |
| 2 | |
| 3 | #include "pdcsdl.h" |
| 4 | |
| 5 | RCSID("$Id: pdcclip.c,v 1.6 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: |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 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 |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 23 | caller to free the memory returned, via PDC_freeclipboard(). |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 24 | The length of the clipboard contents is returned in the length |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 25 | argument. |
| 26 | |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 27 | PDC_setclipboard copies the supplied text into the system's |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 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 |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 35 | PDC_CLIP_MEMORY_ERROR unable to allocate sufficient memory for |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 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 | /* global clipboard contents, should be NULL if none set */ |
| 49 | |
| 50 | static char *pdc_SDL_clipboard = NULL; |
| 51 | |
| 52 | int PDC_getclipboard(char **contents, long *length) |
| 53 | { |
| 54 | int len; |
| 55 | |
| 56 | PDC_LOG(("PDC_getclipboard() - called\n")); |
| 57 | |
| 58 | if (!pdc_SDL_clipboard) |
| 59 | return PDC_CLIP_EMPTY; |
| 60 | |
| 61 | len = strlen(pdc_SDL_clipboard); |
| 62 | if ((*contents = malloc(len + 1)) == NULL) |
| 63 | return PDC_CLIP_MEMORY_ERROR; |
| 64 | |
| 65 | strcpy(*contents, pdc_SDL_clipboard); |
| 66 | *length = len; |
| 67 | |
| 68 | return PDC_CLIP_SUCCESS; |
| 69 | } |
| 70 | |
| 71 | int PDC_setclipboard(const char *contents, long length) |
| 72 | { |
| 73 | PDC_LOG(("PDC_setclipboard() - called\n")); |
| 74 | |
| 75 | if (pdc_SDL_clipboard) |
| 76 | { |
| 77 | free(pdc_SDL_clipboard); |
| 78 | pdc_SDL_clipboard = NULL; |
| 79 | } |
| 80 | |
| 81 | if (contents) |
| 82 | { |
| 83 | if ((pdc_SDL_clipboard = malloc(length + 1)) == NULL) |
| 84 | return PDC_CLIP_MEMORY_ERROR; |
| 85 | |
| 86 | strcpy(pdc_SDL_clipboard, contents); |
| 87 | } |
| 88 | |
| 89 | return PDC_CLIP_SUCCESS; |
| 90 | } |
| 91 | |
| 92 | int PDC_freeclipboard(char *contents) |
| 93 | { |
| 94 | PDC_LOG(("PDC_freeclipboard() - called\n")); |
| 95 | |
| 96 | /* should we also free empty the system clipboard? probably not */ |
| 97 | |
| 98 | if (contents) |
| 99 | { |
| 100 | /* NOTE: We free the memory, but we can not set caller's pointer |
| 101 | to NULL, so if caller calls again then will try to access |
| 102 | free'd memory. We 1st overwrite memory with a string so if |
| 103 | caller tries to use free memory they won't get what they |
| 104 | expect & hopefully notice. */ |
| 105 | |
| 106 | /* memset(contents, 0xFD, strlen(contents)); */ |
| 107 | |
| 108 | if (strlen(contents) >= strlen("PDCURSES")) |
| 109 | strcpy(contents, "PDCURSES"); |
| 110 | |
| 111 | free(contents); |
| 112 | } |
| 113 | |
| 114 | return PDC_CLIP_SUCCESS; |
| 115 | } |
| 116 | |
| 117 | int PDC_clearclipboard(void) |
| 118 | { |
| 119 | PDC_LOG(("PDC_clearclipboard() - called\n")); |
| 120 | |
Stefan Reinauer | e11835e | 2011-10-31 12:54:00 -0700 | [diff] [blame] | 121 | if (pdc_SDL_clipboard) |
Patrick Georgi | 3b77b72 | 2011-07-07 15:41:53 +0200 | [diff] [blame] | 122 | { |
| 123 | free(pdc_SDL_clipboard); |
| 124 | pdc_SDL_clipboard = NULL; |
| 125 | } |
| 126 | |
| 127 | return PDC_CLIP_SUCCESS; |
| 128 | } |