summaryrefslogtreecommitdiff
path: root/payloads/libpayload/curses/PDCurses-3.4/os2/pdcclip.c
blob: 1922bb83f050752f319a443561e65d79a9c53259 (plain)
1
2
3
4
5
6
7
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Public Domain Curses */

#include "pdcos2.h"

RCSID("$Id: pdcclip.c,v 1.33 2008/07/14 04:24:51 wmcbrine Exp $")

/*man-start**************************************************************

  Name:                                                         clipboard

  Synopsis:
        int PDC_getclipboard(char **contents, long *length);
        int PDC_setclipboard(const char *contents, long length);
        int PDC_freeclipboard(char *contents);
        int PDC_clearclipboard(void);

  Description:
        PDC_getclipboard() gets the textual contents of the system's
        clipboard. This function returns the contents of the clipboard
        in the contents argument. It is the responsibilitiy of the
        caller to free the memory returned, via PDC_freeclipboard().
        The length of the clipboard contents is returned in the length
        argument.

        PDC_setclipboard copies the supplied text into the system's
        clipboard, emptying the clipboard prior to the copy.

        PDC_clearclipboard() clears the internal clipboard.

  Return Values:
        indicator of success/failure of call.
        PDC_CLIP_SUCCESS        the call was successful
        PDC_CLIP_MEMORY_ERROR   unable to allocate sufficient memory for
                                the clipboard contents
        PDC_CLIP_EMPTY          the clipboard contains no text
        PDC_CLIP_ACCESS_ERROR   no clipboard support

  Portability                                X/Open    BSD    SYS V
        PDC_getclipboard                        -       -       -
        PDC_setclipboard                        -       -       -
        PDC_freeclipboard                       -       -       -
        PDC_clearclipboard                      -       -       -

**man-end****************************************************************/

int PDC_getclipboard(char **contents, long *length)
{
#ifndef EMXVIDEO
    HMQ hmq;
    HAB hab;
    PTIB ptib;
    PPIB ppib;
    ULONG ulRet;
    long len;
    int rc;
#endif
    PDC_LOG(("PDC_getclipboard() - called\n"));

#ifndef EMXVIDEO
    DosGetInfoBlocks(&ptib, &ppib);
    ppib->pib_ultype = 3;
    hab = WinInitialize(0);
    hmq = WinCreateMsgQueue(hab, 0);

    if (!WinOpenClipbrd(hab))
    {
        WinDestroyMsgQueue(hmq);
        WinTerminate(hab);
        return PDC_CLIP_ACCESS_ERROR;
    }

    rc = PDC_CLIP_EMPTY;

    ulRet = WinQueryClipbrdData(hab, CF_TEXT);

    if (ulRet)
    {
        len = strlen((char *)ulRet);
        *contents = malloc(len + 1);

        if (!*contents)
            rc = PDC_CLIP_MEMORY_ERROR;
        else
        {
            strcpy((char *)*contents, (char *)ulRet);
            *length = len;
            rc = PDC_CLIP_SUCCESS;
        }
    }

    WinCloseClipbrd(hab);
    WinDestroyMsgQueue(hmq);
    WinTerminate(hab);

    return rc;
#else
    return PDC_CLIP_ACCESS_ERROR;
#endif
}

int PDC_setclipboard(const char *contents, long length)
{
#ifndef EMXVIDEO
    HAB hab;
    PTIB ptib;
    PPIB ppib;
    ULONG ulRC;
    PSZ szTextOut = NULL;
    int rc;
#endif
    PDC_LOG(("PDC_setclipboard() - called\n"));

#ifndef EMXVIDEO
    DosGetInfoBlocks(&ptib, &ppib);
    ppib->pib_ultype = 3;
    hab = WinInitialize(0);

    if (!WinOpenClipbrd(hab))
    {
        WinTerminate(hab);
        return PDC_CLIP_ACCESS_ERROR;
    }

    rc = PDC_CLIP_MEMORY_ERROR;

    ulRC = DosAllocSharedMem((PVOID)&szTextOut, NULL, length + 1,
                             PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE);

    if (ulRC == 0)
    {
        strcpy(szTextOut, contents);
        WinEmptyClipbrd(hab);

        if (WinSetClipbrdData(hab, (ULONG)szTextOut, CF_TEXT, CFI_POINTER))
            rc = PDC_CLIP_SUCCESS;
        else
        {
            DosFreeMem(szTextOut);
            rc = PDC_CLIP_ACCESS_ERROR;
        }
    }

    WinCloseClipbrd(hab);
    WinTerminate(hab);

    return rc;
#else
    return PDC_CLIP_ACCESS_ERROR;
#endif
}

int PDC_freeclipboard(char *contents)
{
    PDC_LOG(("PDC_freeclipboard() - called\n"));

    if (contents)
        free(contents);

    return PDC_CLIP_SUCCESS;
}

int PDC_clearclipboard(void)
{
#ifndef EMXVIDEO
    HAB hab;
    PTIB ptib;
    PPIB ppib;
#endif
    PDC_LOG(("PDC_clearclipboard() - called\n"));

#ifndef EMXVIDEO
    DosGetInfoBlocks(&ptib, &ppib);
    ppib->pib_ultype = 3;
    hab = WinInitialize(0);

    WinEmptyClipbrd(hab);

    WinCloseClipbrd(hab);
    WinTerminate(hab);

    return PDC_CLIP_SUCCESS;
#else
    return PDC_CLIP_ACCESS_ERROR;
#endif
}