"Fossies" - the Fresh Open Source Software Archive

Member "PDCurses-3.9/os2/pdcclip.c" (4 Sep 2019, 3965 Bytes) of package /linux/misc/PDCurses-3.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "pdcclip.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.8_vs_3.9.

    1 /* PDCurses */
    2 
    3 #include "pdcos2.h"
    4 
    5 /*man-start**************************************************************
    6 
    7 clipboard
    8 ---------
    9 
   10 ### Synopsis
   11 
   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
   18 
   19    PDC_getclipboard() gets the textual contents of the system's
   20    clipboard. This function returns the contents of the clipboard in the
   21    contents argument. It is the responsibility of the caller to free the
   22    memory returned, via PDC_freeclipboard(). The length of the clipboard
   23    contents is returned in the length argument.
   24 
   25    PDC_setclipboard copies the supplied text into the system's
   26    clipboard, emptying the clipboard prior to the copy.
   27 
   28    PDC_clearclipboard() clears the internal clipboard.
   29 
   30 ### Return Values
   31 
   32     indicator of success/failure of call.
   33     PDC_CLIP_SUCCESS        the call was successful
   34     PDC_CLIP_MEMORY_ERROR   unable to allocate sufficient memory for
   35                             the clipboard contents
   36     PDC_CLIP_EMPTY          the clipboard contains no text
   37     PDC_CLIP_ACCESS_ERROR   no clipboard support
   38 
   39 ### Portability
   40                              X/Open  ncurses  NetBSD
   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     HMQ hmq;
   51     HAB hab;
   52     PTIB ptib;
   53     PPIB ppib;
   54     ULONG ulRet;
   55     long len;
   56     int rc;
   57 
   58     PDC_LOG(("PDC_getclipboard() - called\n"));
   59 
   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 }
   97 
   98 int PDC_setclipboard(const char *contents, long length)
   99 {
  100     HAB hab;
  101     PTIB ptib;
  102     PPIB ppib;
  103     ULONG ulRC;
  104     PSZ szTextOut = NULL;
  105     int rc;
  106 
  107     PDC_LOG(("PDC_setclipboard() - called\n"));
  108 
  109     DosGetInfoBlocks(&ptib, &ppib);
  110     ppib->pib_ultype = 3;
  111     hab = WinInitialize(0);
  112 
  113     if (!WinOpenClipbrd(hab))
  114     {
  115         WinTerminate(hab);
  116         return PDC_CLIP_ACCESS_ERROR;
  117     }
  118 
  119     rc = PDC_CLIP_MEMORY_ERROR;
  120 
  121     ulRC = DosAllocSharedMem((PVOID)&szTextOut, NULL, length + 1,
  122                              PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE);
  123 
  124     if (ulRC == 0)
  125     {
  126         strcpy(szTextOut, contents);
  127         WinEmptyClipbrd(hab);
  128 
  129         if (WinSetClipbrdData(hab, (ULONG)szTextOut, CF_TEXT, CFI_POINTER))
  130             rc = PDC_CLIP_SUCCESS;
  131         else
  132         {
  133             DosFreeMem(szTextOut);
  134             rc = PDC_CLIP_ACCESS_ERROR;
  135         }
  136     }
  137 
  138     WinCloseClipbrd(hab);
  139     WinTerminate(hab);
  140 
  141     return rc;
  142 }
  143 
  144 int PDC_freeclipboard(char *contents)
  145 {
  146     PDC_LOG(("PDC_freeclipboard() - called\n"));
  147 
  148     if (contents)
  149         free(contents);
  150 
  151     return PDC_CLIP_SUCCESS;
  152 }
  153 
  154 int PDC_clearclipboard(void)
  155 {
  156     HAB hab;
  157     PTIB ptib;
  158     PPIB ppib;
  159 
  160     PDC_LOG(("PDC_clearclipboard() - called\n"));
  161 
  162     DosGetInfoBlocks(&ptib, &ppib);
  163     ppib->pib_ultype = 3;
  164     hab = WinInitialize(0);
  165 
  166     WinEmptyClipbrd(hab);
  167 
  168     WinCloseClipbrd(hab);
  169     WinTerminate(hab);
  170 
  171     return PDC_CLIP_SUCCESS;
  172 }