File: scrap_qnx.c

package info (click to toggle)
pygame 1.9.6%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,060 kB
  • sloc: ansic: 59,765; python: 31,220; objc: 334; makefile: 57; cpp: 25
file content (234 lines) | stat: -rw-r--r-- 6,206 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
    pygame - Python Game Library
    Copyright (C) 2006, 2007 Rene Dudfield, Marcus von Appen

    Originally written and put in the public domain by Sam Lantinga.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

static unsigned short InputGroup;
#define MAX_CHUNK_SIZE INT_MAX

/**
 * \brief Converts the passed type into a system specific type to use
 *        for the clipboard.
 *
 * \param type The type to convert.
 * \return A system specific type.
 */
static uint32_t
_convert_format(char *type)
{
    switch (type) {
        case PYGAME_SCRAP_TEXT:
            return Ph_CL_TEXT;
        default: /* PYGAME_SCRAP_BMP et al. */
        {
            /* TODO */
            return 0;
        }
    }
}

int
pygame_scrap_init(void)
{
    SDL_SysWMinfo info;
    int retval = 0;

    /* Grab the window manager specific information */
    SDL_SetError("SDL is not running on known window manager");

    SDL_VERSION(&info.version);
    if (SDL_GetWMInfo(&info)) {
        /* Save the information for later use */
        InputGroup = PhInputGroup(NULL);
        retval = 1;
    }
    if (retval)
        _scrapinitialized = 1;

    return retval;
}

int
pygame_scrap_lost(void)
{
    if (!pygame_scrap_initialized()) {
        PyErr_SetString(pgExc_SDLError, "scrap system not initialized.");
        return 0;
    }
    return (PhInputGroup(NULL) != InputGroup);
}

int
pygame_scrap_put(char *type, int srclen, char *src)
{
    uint32_t format;
    int nulledlen = srclen + 1;

    if (!pygame_scrap_initialized()) {
        PyErr_SetString(pgExc_SDLError, "scrap system not initialized.");
        return 0;
    }

    format = _convert_format(type);

    /* Clear old buffer and copy the new content. */
    if (_clipbuffer)
        free(_clipbuffer);

    _clipbuffer = malloc(nulledlen);
    if (!_clipbuffer)
        return 0; /* Allocation failed. */
    memset(_clipbuffer, 0, nulledlen);
    memcpy(_clipbuffer, src, srclen);
    _clipsize = srclen;
    _cliptype = format;

#if (_NTO_VERSION < 620) /* Before 6.2.0 releases. */
    {
        PhClipHeader clheader = {Ph_CLIPBOARD_TYPE_TEXT, 0, NULL};
        int *cldata;
        int status;

        cldata = (int *)_clipbuffer;
        *cldata = type;
        clheader.data = _clipbuffer;
        if (dstlen > 65535)
            clheader.length = 65535; /* Maximum photon clipboard size. :( */
        else
            clheader.length = nulledlen;

        status = PhClipboardCopy(InputGroup, 1, &clheader);
        if (status == -1) {
            /* Could not access the clipboard, raise an error. */
            CLEAN_CLIP_BUFFER();
            return 0;
        }
    }

#else /* 6.2.0 and 6.2.1 and future releases. */
    {
        PhClipboardHdr clheader = {Ph_CLIPBOARD_TYPE_TEXT, 0, NULL};
        int *cldata;
        int status;

        cldata = (int *)_clipbuffer;
        *cldata = type;
        clheader.data = _clipbuffer;
        clheader.length = nulledlen;

        status = PhClipboardWrite(InputGroup, 1, &clheader);
        if (status == -1) {
            /* Could not access the clipboard, raise an error. */
            CLEAN_CLIP_BUFFER();
            return 0;
        }
    }
#endif
    return 1;
}

char *
pygame_get_scrap(char *type)
{
    uint32_t format = _convert_format(type);
    char *retval = NULL;

    if (!pygame_scrap_initialized()) {
        PyErr_SetString(pgExc_SDLError, "scrap system not initialized.");
        return 0;
    }

    /* If we are the owner, simply return the clip buffer, if it matches
     * the request type. */
    if (!pygame_scrap_lost()) {
        if (format != _cliptype)
            return NULL;

        if (_clipbuffer) {
            retval = malloc(_clipsize + 1);
            if (!retval)
                return NULL;
            memset(retval, 0, _clipsize + 1);
            memcpy(retval, _clipbuffer, _clipsize + 1);
            return retval;
        }
        return NULL;
    }

#if (_NTO_VERSION < 620) /* before 6.2.0 releases */
    {
        void *clhandle;
        PhClipHeader *clheader;
        int *cldata;

        clhandle = PhClipboardPasteStart(InputGroup);

        if (clhandle) {
            clheader = PhClipboardPasteType(clhandle, Ph_CLIPBOARD_TYPE_TEXT);
            if (clheader) {
                cldata = clheader->data;
                if (*cldata == type)
                    retval = malloc(clheader->length + 1);

                if (retval) {
                    memset(retval, 0, clheader->length + 1);
                    memcpy(retval, cldata, clheader->length + 1);
                }
            }
            PhClipboardPasteFinish(clhandle);
        }
    }
#else /* 6.2.0 and 6.2.1 and future releases */
    {
        void *clhandle;
        PhClipboardHdr *clheader;
        int *cldata;

        clheader = PhClipboardRead(InputGroup, Ph_CLIPBOARD_TYPE_TEXT);
        if (clheader) {
            cldata = clheader->data;
            if (*cldata == type)
                retval = malloc(clheader->length + 1);

            if (retval) {
                memset(retval, 0, clheader->length + 1);
                memcpy(retval, cldata, clheader->length + 1);
            }
            /* According to the QNX 6.x docs, the clheader pointer is a
             * newly created one that must be freed manually. */
            free(clheader->data);
            free(clheader);
        }
    }
#endif

    return retval;
}

char **
pygame_scrap_get_types(void)
{
    return NULL;
}

int
pygame_scrap_contains(char *type)
{
    return 0;
}