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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/*
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
*/
/* Handle clipboard text and data in arbitrary formats */
#include <limits.h>
#include <stdio.h>
#include "SDL.h"
#include "SDL_syswm.h"
#include "scrap.h"
#include "pygame.h"
#include "doc/scrap_doc.h"
#include "pgcompat.h"
/**
* Indicates, whether pygame.scrap was initialized or not.
*/
static int _scrapinitialized = 0;
/**
* Currently active Clipboard object.
*/
static ScrapClipType _currentmode;
static PyObject *_selectiondata;
static PyObject *_clipdata;
/* Forward declarations. */
static PyObject *
_scrap_get_types(PyObject *self, PyObject *args);
static PyObject *
_scrap_contains(PyObject *self, PyObject *args);
static PyObject *
_scrap_get_scrap(PyObject *self, PyObject *args);
static PyObject *
_scrap_put_scrap(PyObject *self, PyObject *args);
static PyObject *
_scrap_lost_scrap(PyObject *self, PyObject *args);
static PyObject *
_scrap_set_mode(PyObject *self, PyObject *args);
/* Determine what type of clipboard we are using */
#if defined(__unix__) && defined(SDL_VIDEO_DRIVER_X11)
/*!defined(__QNXNTO__) &&*/
#define X11_SCRAP
#include <time.h> /* Needed for clipboard timeouts. */
#include "scrap_x11.c"
#elif defined(__WIN32__)
#define WIN_SCRAP
#include "scrap_win.c"
/*
#elif defined(__QNXNTO__)
#define QNX_SCRAP
static uint32_t _cliptype = 0;
#include "scrap_qnx.c"
*/
#elif defined(__APPLE__)
#define MAC_SCRAP
#include "scrap_mac.c"
#else
#error Unknown window manager for clipboard handling
#endif /* scrap type */
/**
* \brief Indicates whether the scrap module is already initialized.
*
* \return 0 if the module is not initialized, 1, if it is.
*/
int
pygame_scrap_initialized(void)
{
return _scrapinitialized;
}
#if !defined(MAC_SCRAP)
/*
* Initializes the pygame scrap module.
*/
static PyObject *
_scrap_init(PyObject *self, PyObject *args)
{
VIDEO_INIT_CHECK();
_clipdata = PyDict_New();
_selectiondata = PyDict_New();
/* In case we've got not video surface, we won't initialize
* anything.
*/
if (!SDL_GetVideoSurface())
return RAISE(pgExc_SDLError, "No display mode is set");
if (!pygame_scrap_init())
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_NONE;
}
#endif
/*
* Indicates whether the scrap module is currently initialized.
*
* Note: All platforms supported here.
*/
static PyObject *
_scrap_get_init(PyObject *self, PyObject *args)
{
return PyBool_FromLong(pygame_scrap_initialized());
}
#if !defined(MAC_SCRAP)
/*
* Gets the currently available types from the active clipboard.
*/
static PyObject *
_scrap_get_types(PyObject *self, PyObject *args)
{
int i = 0;
char **types;
char *type;
PyObject *list;
PyObject *tmp;
PYGAME_SCRAP_INIT_CHECK();
if (!pygame_scrap_lost()) {
switch (_currentmode) {
case SCRAP_SELECTION:
return PyDict_Keys(_selectiondata);
case SCRAP_CLIPBOARD:
default:
return PyDict_Keys(_clipdata);
}
}
list = PyList_New(0);
types = pygame_scrap_get_types();
if (!types)
return list;
while (types[i] != NULL) {
type = types[i];
tmp = PyUnicode_DecodeASCII(type, strlen(type), 0);
if (!tmp) {
Py_DECREF(list);
return 0;
}
if (PyList_Append(list, tmp)) {
Py_DECREF(list);
Py_DECREF(tmp);
return 0;
}
Py_DECREF(tmp);
i++;
}
return list;
}
#endif
#if !defined(MAC_SCRAP)
/*
* Checks whether the active clipboard contains a certain type.
*/
static PyObject *
_scrap_contains(PyObject *self, PyObject *args)
{
char *type = NULL;
if (!PyArg_ParseTuple(args, "s", &type))
return NULL;
if (pygame_scrap_contains(type))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
#endif
#if !defined(MAC_SCRAP)
/*
* Gets the content for a certain type from the active clipboard.
*/
static PyObject *
_scrap_get_scrap(PyObject *self, PyObject *args)
{
char *scrap = NULL;
PyObject *retval;
char *scrap_type;
unsigned long count;
PYGAME_SCRAP_INIT_CHECK();
if (!PyArg_ParseTuple(args, "s", &scrap_type))
return NULL;
if (!pygame_scrap_lost()) {
/* Still own the clipboard. */
PyObject *scrap_dict = NULL;
PyObject *key = NULL;
PyObject *val = NULL;
switch (_currentmode) {
case SCRAP_SELECTION:
scrap_dict = _selectiondata;
break;
case SCRAP_CLIPBOARD:
default:
scrap_dict = _clipdata;
break;
}
#if PY3
key = PyUnicode_FromString(scrap_type);
if (NULL == key) {
return PyErr_Format(PyExc_ValueError,
"invalid scrap data type identifier (%s)",
scrap_type);
}
val = PyDict_GetItemWithError(scrap_dict, key);
Py_DECREF(key);
if (NULL == val) {
if (PyErr_Occurred()) {
return PyErr_Format(PyExc_SystemError,
"pygame.scrap internal error (key=%s)",
scrap_type);
}
Py_RETURN_NONE;
}
#else /* !PY3 */
val = PyDict_GetItemString(scrap_dict, scrap_type);
if (NULL == val) {
Py_RETURN_NONE;
}
#endif /* !PY3 */
Py_INCREF(val);
return val;
}
/* pygame_get_scrap() only returns NULL or !NULL, but won't set any
* errors. */
scrap = pygame_scrap_get(scrap_type, &count);
if (!scrap)
Py_RETURN_NONE;
retval = Bytes_FromStringAndSize(scrap, count);
return retval;
}
#endif
#if !defined(MAC_SCRAP)
/*
* This will put a python string into the clipboard.
*/
static PyObject *
_scrap_put_scrap(PyObject *self, PyObject *args)
{
int scraplen;
char *scrap = NULL;
char *scrap_type;
PyObject *tmp;
#if PY3
static const char argfmt[] = "sy#";
#else
static char argfmt[] = "st#";
#endif
PYGAME_SCRAP_INIT_CHECK();
if (!PyArg_ParseTuple(args, argfmt, &scrap_type, &scrap, &scraplen)) {
return NULL;
}
/* Set it in the clipboard. */
if (!pygame_scrap_put(scrap_type, scraplen, scrap))
return RAISE(pgExc_SDLError,
"content could not be placed in clipboard.");
/* Add or replace the set value. */
switch (_currentmode) {
case SCRAP_SELECTION: {
tmp = Bytes_FromStringAndSize(scrap, scraplen);
PyDict_SetItemString(_selectiondata, scrap_type, tmp);
Py_DECREF(tmp);
break;
}
case SCRAP_CLIPBOARD:
default: {
tmp = Bytes_FromStringAndSize(scrap, scraplen);
PyDict_SetItemString(_clipdata, scrap_type, tmp);
Py_DECREF(tmp);
break;
}
}
Py_RETURN_NONE;
}
#endif
#if !defined(MAC_SCRAP)
/*
* Checks whether the pygame window has lost the clipboard.
*/
static PyObject *
_scrap_lost_scrap(PyObject *self, PyObject *args)
{
PYGAME_SCRAP_INIT_CHECK();
if (pygame_scrap_lost())
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
#endif
#if !defined(MAC_SCRAP)
/*
* Sets the clipboard mode. This only works for the X11 environment, which
* diverses between mouse selections and the clipboard.
*/
static PyObject *
_scrap_set_mode(PyObject *self, PyObject *args)
{
PYGAME_SCRAP_INIT_CHECK();
if (!PyArg_ParseTuple(args, "i", &_currentmode))
return NULL;
if (_currentmode != SCRAP_CLIPBOARD && _currentmode != SCRAP_SELECTION)
return RAISE(PyExc_ValueError, "invalid clipboard mode");
#ifndef X11_SCRAP
/* Force the clipboard, if not in a X11 environment. */
_currentmode = SCRAP_CLIPBOARD;
#endif
Py_RETURN_NONE;
}
#endif /* !defined(MAC_SCRAP) */
static PyMethodDef scrap_builtins[] = {
/*
* Only initialise these functions for ones we know about.
*
* Note, the macosx stuff is done in sdlosx_main.m
*/
#if (defined(X11_SCRAP) || defined(WIN_SCRAP) || defined(QNX_SCRAP) || \
defined(MAC_SCRAP))
{"init", _scrap_init, 1, DOC_PYGAMESCRAPINIT},
{"get_init", _scrap_get_init, METH_NOARGS, DOC_PYGAMESCRAPGETINIT},
{"contains", _scrap_contains, METH_VARARGS, DOC_PYGAMESCRAPCONTAINS},
{"get", _scrap_get_scrap, METH_VARARGS, DOC_PYGAMESCRAPGET},
{"get_types", _scrap_get_types, METH_NOARGS, DOC_PYGAMESCRAPGETTYPES},
{"put", _scrap_put_scrap, METH_VARARGS, DOC_PYGAMESCRAPPUT},
{"lost", _scrap_lost_scrap, METH_NOARGS, DOC_PYGAMESCRAPLOST},
{"set_mode", _scrap_set_mode, METH_VARARGS, DOC_PYGAMESCRAPSETMODE},
#endif
{NULL, NULL, 0, NULL}};
MODINIT_DEFINE(scrap)
{
#if PY3
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
"scrap",
"",
-1,
scrap_builtins,
NULL,
NULL,
NULL,
NULL};
#endif
/* imported needed apis; Do this first so if there is an error
the module is not loaded.
*/
import_pygame_base();
if (PyErr_Occurred()) {
MODINIT_ERROR;
}
/* create the module */
#if PY3
return PyModule_Create(&_module);
#else
Py_InitModule3(MODPREFIX "scrap", scrap_builtins, NULL);
#endif
}
|