@@ -61,6 +61,13 @@ _scrap_lost_scrap(PyObject *self, PyObject *args);
6161static PyObject *
6262_scrap_set_mode (PyObject * self , PyObject * args );
6363
64+ static PyObject *
65+ _scrap_get_text (PyObject * self , PyObject * args );
66+ static PyObject *
67+ _scrap_put_text (PyObject * self , PyObject * args );
68+ static PyObject *
69+ _scrap_has_text (PyObject * self , PyObject * args );
70+
6471/* Determine what type of clipboard we are using */
6572#if !defined(__WIN32__ )
6673#define SDL2_SCRAP
@@ -93,6 +100,11 @@ _scrap_init(PyObject *self, PyObject *args)
93100{
94101 VIDEO_INIT_CHECK ();
95102
103+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
104+ "pygame.scrap.init deprecated since 2.2.0" , 1 ) == -1 ) {
105+ return NULL ;
106+ }
107+
96108 if (!pygame_scrap_initialized ()) {
97109 Py_XDECREF (_clipdata );
98110 Py_XDECREF (_selectiondata );
@@ -120,6 +132,12 @@ _scrap_init(PyObject *self, PyObject *args)
120132static PyObject *
121133_scrap_get_init (PyObject * self , PyObject * _null )
122134{
135+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
136+ "pygame.scrap.get_init deprecated since 2.2.0" ,
137+ 1 ) == -1 ) {
138+ return NULL ;
139+ }
140+
123141 return PyBool_FromLong (pygame_scrap_initialized ());
124142}
125143
@@ -135,6 +153,12 @@ _scrap_get_types(PyObject *self, PyObject *_null)
135153 PyObject * list ;
136154 PyObject * tmp ;
137155
156+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
157+ "pygame.scrap.get_types deprecated since 2.2.0" ,
158+ 1 ) == -1 ) {
159+ return NULL ;
160+ }
161+
138162 PYGAME_SCRAP_INIT_CHECK ();
139163 if (!pygame_scrap_lost ()) {
140164 switch (_currentmode ) {
@@ -176,6 +200,12 @@ _scrap_contains(PyObject *self, PyObject *args)
176200{
177201 char * type = NULL ;
178202
203+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
204+ "pygame.scrap.contains deprecated since 2.2.0" ,
205+ 1 ) == -1 ) {
206+ return NULL ;
207+ }
208+
179209 if (!PyArg_ParseTuple (args , "s" , & type ))
180210 return NULL ;
181211 if (pygame_scrap_contains (type ))
@@ -194,6 +224,13 @@ _scrap_get_scrap(PyObject *self, PyObject *args)
194224 char * scrap_type ;
195225 size_t count ;
196226
227+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
228+ "pygame.scrap.get deprecated since 2.2.0. Consider using"
229+ " pygame.scrap.get_text instead." ,
230+ 1 ) == -1 ) {
231+ return NULL ;
232+ }
233+
197234 PYGAME_SCRAP_INIT_CHECK ();
198235
199236 if (!PyArg_ParseTuple (args , "s" , & scrap_type ))
@@ -266,6 +303,13 @@ _scrap_put_scrap(PyObject *self, PyObject *args)
266303 PyObject * tmp ;
267304 static const char argfmt [] = "sy#" ;
268305
306+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
307+ "pygame.scrap.put deprecated since 2.2.0. Consider using"
308+ " pygame.scrap.put_text instead." ,
309+ 1 ) == -1 ) {
310+ return NULL ;
311+ }
312+
269313 PYGAME_SCRAP_INIT_CHECK ();
270314
271315 if (!PyArg_ParseTuple (args , argfmt , & scrap_type , & scrap , & scraplen )) {
@@ -318,6 +362,13 @@ static PyObject *
318362_scrap_set_mode (PyObject * self , PyObject * args )
319363{
320364 PYGAME_SCRAP_INIT_CHECK ();
365+
366+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
367+ "pygame.scrap.set_mode deprecated since 2.2.0" ,
368+ 1 ) == -1 ) {
369+ return NULL ;
370+ }
371+
321372 if (!PyArg_ParseTuple (args , "i" , & _currentmode ))
322373 return NULL ;
323374
@@ -329,6 +380,75 @@ _scrap_set_mode(PyObject *self, PyObject *args)
329380 Py_RETURN_NONE ;
330381}
331382
383+ /**
384+ * @brief Fetches a python string from the SDL clipboard. If
385+ * there is nothing in the clipboard, it will return empty
386+ *
387+ * @return PyObject*
388+ */
389+ static PyObject *
390+ _scrap_get_text (PyObject * self , PyObject * args )
391+ {
392+ const SDL_bool hasText = SDL_HasClipboardText ();
393+
394+ char * text = SDL_GetClipboardText ();
395+
396+ // if SDL_GetClipboardText fails, it returns an empty string
397+ // hasText helps determine if an actual error occurred
398+ // vs just an empty string in the clipboard
399+ if (* text == '\0' && hasText == SDL_TRUE ) {
400+ SDL_free (text );
401+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
402+ return NULL ;
403+ }
404+
405+ PyObject * returnValue = PyUnicode_FromString (text );
406+ SDL_free (text );
407+
408+ return returnValue ;
409+ }
410+
411+ /**
412+ * @brief Puts a python string into the SDL clipboard
413+ *
414+ * @param args A python string to be put into the clipboard
415+ *
416+ * @return PyObject*
417+ */
418+ static PyObject *
419+ _scrap_put_text (PyObject * self , PyObject * args )
420+ {
421+ char * text ;
422+
423+ if (!PyArg_ParseTuple (args , "s" , & text )) {
424+ return NULL ;
425+ }
426+
427+ if (SDL_SetClipboardText (text )) {
428+ return RAISE (pgExc_SDLError , SDL_GetError ());
429+ }
430+
431+ Py_RETURN_NONE ;
432+ }
433+
434+ /**
435+ * @brief If the SDL clipboard has something in it, will return True.
436+ * Else it returns False.
437+ *
438+ * @return PyObject*
439+ */
440+ static PyObject *
441+ _scrap_has_text (PyObject * self , PyObject * args )
442+ {
443+ const SDL_bool hasText = SDL_HasClipboardText ();
444+
445+ if (hasText ) {
446+ Py_RETURN_TRUE ;
447+ }
448+
449+ Py_RETURN_FALSE ;
450+ }
451+
332452static PyMethodDef scrap_builtins [] = {
333453/*
334454 * Only initialise these functions for ones we know about.
@@ -347,6 +467,9 @@ static PyMethodDef scrap_builtins[] = {
347467 {"set_mode" , _scrap_set_mode , METH_VARARGS , DOC_SCRAP_SETMODE },
348468
349469#endif
470+ {"get_text" , _scrap_get_text , METH_NOARGS , DOC_SCRAP_GETTEXT },
471+ {"has_text" , _scrap_has_text , METH_NOARGS , DOC_SCRAP_HASTEXT },
472+ {"put_text" , _scrap_put_text , METH_VARARGS , DOC_SCRAP_PUTTEXT },
350473 {NULL , NULL , 0 , NULL }};
351474
352475MODINIT_DEFINE (scrap )
0 commit comments