1 2 /* ========================= Module _Scrap ========================== */ 3 4 #include "Python.h" 5 6 7 #ifndef __LP64__ 8 9 #include "pymactoolbox.h" 10 11 /* Macro to test whether a weak-loaded CFM function exists */ 12 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\ 13 PyErr_SetString(PyExc_NotImplementedError, \ 14 "Not available in this shared library/OS version"); \ 15 return NULL; \ 16 }} while(0) 17 18 19 #include <Carbon/Carbon.h> 20 21 static PyObject *Scrap_Error; 22 23 /* ----------------------- Object type Scrap ------------------------ */ 24 25 PyTypeObject Scrap_Type; 26 27 #define ScrapObj_Check(x) ((x)->ob_type == &Scrap_Type || PyObject_TypeCheck((x), &Scrap_Type)) 28 29 typedef struct ScrapObject { 30 PyObject_HEAD 31 ScrapRef ob_itself; 32 } ScrapObject; 33 34 PyObject *ScrapObj_New(ScrapRef itself) 35 { 36 ScrapObject *it; 37 it = PyObject_NEW(ScrapObject, &Scrap_Type); 38 if (it == NULL) return NULL; 39 it->ob_itself = itself; 40 return (PyObject *)it; 41 } 42 int ScrapObj_Convert(PyObject *v, ScrapRef *p_itself) 43 { 44 if (!ScrapObj_Check(v)) 45 { 46 PyErr_SetString(PyExc_TypeError, "Scrap required"); 47 return 0; 48 } 49 *p_itself = ((ScrapObject *)v)->ob_itself; 50 return 1; 51 } 52 53 static void ScrapObj_dealloc(ScrapObject *self) 54 { 55 /* Cleanup of self->ob_itself goes here */ 56 self->ob_type->tp_free((PyObject *)self); 57 } 58 59 static PyObject *ScrapObj_GetScrapFlavorFlags(ScrapObject *_self, PyObject *_args) 60 { 61 PyObject *_res = NULL; 62 OSStatus _err; 63 ScrapFlavorType flavorType; 64 ScrapFlavorFlags flavorFlags; 65 if (!PyArg_ParseTuple(_args, "O&", 66 PyMac_GetOSType, &flavorType)) 67 return NULL; 68 _err = GetScrapFlavorFlags(_self->ob_itself, 69 flavorType, 70 &flavorFlags); 71 if (_err != noErr) return PyMac_Error(_err); 72 _res = Py_BuildValue("l", 73 flavorFlags); 74 return _res; 75 } 76 77 static PyObject *ScrapObj_GetScrapFlavorSize(ScrapObject *_self, PyObject *_args) 78 { 79 PyObject *_res = NULL; 80 OSStatus _err; 81 ScrapFlavorType flavorType; 82 Size byteCount; 83 if (!PyArg_ParseTuple(_args, "O&", 84 PyMac_GetOSType, &flavorType)) 85 return NULL; 86 _err = GetScrapFlavorSize(_self->ob_itself, 87 flavorType, 88 &byteCount); 89 if (_err != noErr) return PyMac_Error(_err); 90 _res = Py_BuildValue("l", 91 byteCount); 92 return _res; 93 } 94 95 static PyObject *ScrapObj_GetScrapFlavorData(ScrapObject *_self, PyObject *_args) 96 { 97 PyObject *_res = NULL; 98 OSStatus _err; 99 ScrapFlavorType flavorType; 100 Size byteCount; 101 102 if (!PyArg_ParseTuple(_args, "O&", 103 PyMac_GetOSType, &flavorType)) 104 return NULL; 105 _err = GetScrapFlavorSize(_self->ob_itself, 106 flavorType, 107 &byteCount); 108 if (_err != noErr) return PyMac_Error(_err); 109 _res = PyString_FromStringAndSize(NULL, (int)byteCount); 110 if ( _res == NULL ) return NULL; 111 _err = GetScrapFlavorData(_self->ob_itself, 112 flavorType, 113 &byteCount, 114 PyString_AS_STRING(_res)); 115 if (_err != noErr) { 116 Py_XDECREF(_res); 117 return PyMac_Error(_err); 118 } 119 return _res; 120 } 121 122 static PyObject *ScrapObj_PutScrapFlavor(ScrapObject *_self, PyObject *_args) 123 { 124 PyObject *_res = NULL; 125 OSStatus _err; 126 ScrapFlavorType flavorType; 127 ScrapFlavorFlags flavorFlags; 128 char *flavorData__in__; 129 int flavorData__in_len__; 130 if (!PyArg_ParseTuple(_args, "O&Ks#", 131 PyMac_GetOSType, &flavorType, 132 &flavorFlags, 133 &flavorData__in__, &flavorData__in_len__)) 134 return NULL; 135 _err = PutScrapFlavor(_self->ob_itself, 136 flavorType, 137 flavorFlags, 138 (Size)flavorData__in_len__, 139 flavorData__in__); 140 if (_err != noErr) return PyMac_Error(_err); 141 Py_INCREF(Py_None); 142 _res = Py_None; 143 return _res; 144 } 145 146 static PyObject *ScrapObj_GetScrapFlavorCount(ScrapObject *_self, PyObject *_args) 147 { 148 PyObject *_res = NULL; 149 OSStatus _err; 150 UInt32 infoCount; 151 if (!PyArg_ParseTuple(_args, "")) 152 return NULL; 153 _err = GetScrapFlavorCount(_self->ob_itself, 154 &infoCount); 155 if (_err != noErr) return PyMac_Error(_err); 156 _res = Py_BuildValue("l", 157 infoCount); 158 return _res; 159 } 160 161 static PyObject *ScrapObj_GetScrapFlavorInfoList(ScrapObject *_self, PyObject *_args) 162 { 163 PyObject *_res = NULL; 164 PyObject *item; 165 OSStatus _err; 166 UInt32 infoCount; 167 ScrapFlavorInfo *infolist = NULL; 168 int i; 169 170 if (!PyArg_ParseTuple(_args, "")) 171 return NULL; 172 _err = GetScrapFlavorCount(_self->ob_itself, 173 &infoCount); 174 if (_err != noErr) return PyMac_Error(_err); 175 if (infoCount == 0) return Py_BuildValue("[]"); 176 177 if ((infolist = (ScrapFlavorInfo *)malloc(infoCount*sizeof(ScrapFlavorInfo))) == NULL ) 178 return PyErr_NoMemory(); 179 180 _err = GetScrapFlavorInfoList(_self->ob_itself, &infoCount, infolist); 181 if (_err != noErr) { 182 free(infolist); 183 return NULL; 184 } 185 if ((_res = PyList_New(infoCount)) == NULL ) { 186 free(infolist); 187 return NULL; 188 } 189 for(i=0; i<infoCount; i++) { 190 item = Py_BuildValue("O&l", PyMac_BuildOSType, infolist[i].flavorType, 191 infolist[i].flavorFlags); 192 if ( !item || PyList_SetItem(_res, i, item) < 0 ) { 193 Py_DECREF(_res); 194 free(infolist); 195 return NULL; 196 } 197 } 198 free(infolist); 199 return _res; 200 } 201 202 static PyMethodDef ScrapObj_methods[] = { 203 {"GetScrapFlavorFlags", (PyCFunction)ScrapObj_GetScrapFlavorFlags, 1, 204 PyDoc_STR("(ScrapFlavorType flavorType) -> (ScrapFlavorFlags flavorFlags)")}, 205 {"GetScrapFlavorSize", (PyCFunction)ScrapObj_GetScrapFlavorSize, 1, 206 PyDoc_STR("(ScrapFlavorType flavorType) -> (Size byteCount)")}, 207 {"GetScrapFlavorData", (PyCFunction)ScrapObj_GetScrapFlavorData, 1, 208 PyDoc_STR("(ScrapFlavorType flavorType, Buffer destination) -> (Size byteCount)")}, 209 {"PutScrapFlavor", (PyCFunction)ScrapObj_PutScrapFlavor, 1, 210 PyDoc_STR("(ScrapFlavorType flavorType, ScrapFlavorFlags flavorFlags, Size flavorSize, Buffer flavorData) -> None")}, 211 {"GetScrapFlavorCount", (PyCFunction)ScrapObj_GetScrapFlavorCount, 1, 212 PyDoc_STR("() -> (UInt32 infoCount)")}, 213 {"GetScrapFlavorInfoList", (PyCFunction)ScrapObj_GetScrapFlavorInfoList, 1, 214 PyDoc_STR("() -> ([(ScrapFlavorType, ScrapFlavorInfo), ...])")}, 215 {NULL, NULL, 0} 216 }; 217 218 PyMethodChain ScrapObj_chain = { ScrapObj_methods, NULL }; 219 220 static PyObject *ScrapObj_getattr(ScrapObject *self, char *name) 221 { 222 return Py_FindMethodInChain(&ScrapObj_chain, (PyObject *)self, name); 223 } 224 225 #define ScrapObj_setattr NULL 226 227 #define ScrapObj_compare NULL 228 229 #define ScrapObj_repr NULL 230 231 #define ScrapObj_hash NULL 232 233 PyTypeObject Scrap_Type = { 234 PyObject_HEAD_INIT(NULL) 235 0, /*ob_size*/ 236 "_Scrap.Scrap", /*tp_name*/ 237 sizeof(ScrapObject), /*tp_basicsize*/ 238 0, /*tp_itemsize*/ 239 /* methods */ 240 (destructor) ScrapObj_dealloc, /*tp_dealloc*/ 241 0, /*tp_print*/ 242 (getattrfunc) ScrapObj_getattr, /*tp_getattr*/ 243 (setattrfunc) ScrapObj_setattr, /*tp_setattr*/ 244 (cmpfunc) ScrapObj_compare, /*tp_compare*/ 245 (reprfunc) ScrapObj_repr, /*tp_repr*/ 246 (PyNumberMethods *)0, /* tp_as_number */ 247 (PySequenceMethods *)0, /* tp_as_sequence */ 248 (PyMappingMethods *)0, /* tp_as_mapping */ 249 (hashfunc) ScrapObj_hash, /*tp_hash*/ 250 }; 251 252 /* --------------------- End object type Scrap ---------------------- */ 253 254 static PyObject *Scrap_LoadScrap(PyObject *_self, PyObject *_args) 255 { 256 PyObject *_res = NULL; 257 OSStatus _err; 258 if (!PyArg_ParseTuple(_args, "")) 259 return NULL; 260 _err = LoadScrap(); 261 if (_err != noErr) return PyMac_Error(_err); 262 Py_INCREF(Py_None); 263 _res = Py_None; 264 return _res; 265 } 266 267 static PyObject *Scrap_UnloadScrap(PyObject *_self, PyObject *_args) 268 { 269 PyObject *_res = NULL; 270 OSStatus _err; 271 if (!PyArg_ParseTuple(_args, "")) 272 return NULL; 273 _err = UnloadScrap(); 274 if (_err != noErr) return PyMac_Error(_err); 275 Py_INCREF(Py_None); 276 _res = Py_None; 277 return _res; 278 } 279 280 static PyObject *Scrap_GetCurrentScrap(PyObject *_self, PyObject *_args) 281 { 282 PyObject *_res = NULL; 283 OSStatus _err; 284 ScrapRef scrap; 285 if (!PyArg_ParseTuple(_args, "")) 286 return NULL; 287 _err = GetCurrentScrap(&scrap); 288 if (_err != noErr) return PyMac_Error(_err); 289 _res = Py_BuildValue("O&", 290 ScrapObj_New, scrap); 291 return _res; 292 } 293 294 static PyObject *Scrap_ClearCurrentScrap(PyObject *_self, PyObject *_args) 295 { 296 PyObject *_res = NULL; 297 OSStatus _err; 298 if (!PyArg_ParseTuple(_args, "")) 299 return NULL; 300 _err = ClearCurrentScrap(); 301 if (_err != noErr) return PyMac_Error(_err); 302 Py_INCREF(Py_None); 303 _res = Py_None; 304 return _res; 305 } 306 307 static PyObject *Scrap_CallInScrapPromises(PyObject *_self, PyObject *_args) 308 { 309 PyObject *_res = NULL; 310 OSStatus _err; 311 if (!PyArg_ParseTuple(_args, "")) 312 return NULL; 313 _err = CallInScrapPromises(); 314 if (_err != noErr) return PyMac_Error(_err); 315 Py_INCREF(Py_None); 316 _res = Py_None; 317 return _res; 318 } 319 #endif /* __LP64__ */ 320 321 static PyMethodDef Scrap_methods[] = { 322 #ifndef __LP64__ 323 {"LoadScrap", (PyCFunction)Scrap_LoadScrap, 1, 324 PyDoc_STR("() -> None")}, 325 {"UnloadScrap", (PyCFunction)Scrap_UnloadScrap, 1, 326 PyDoc_STR("() -> None")}, 327 {"GetCurrentScrap", (PyCFunction)Scrap_GetCurrentScrap, 1, 328 PyDoc_STR("() -> (ScrapRef scrap)")}, 329 {"ClearCurrentScrap", (PyCFunction)Scrap_ClearCurrentScrap, 1, 330 PyDoc_STR("() -> None")}, 331 {"CallInScrapPromises", (PyCFunction)Scrap_CallInScrapPromises, 1, 332 PyDoc_STR("() -> None")}, 333 #endif /* __LP64__ */ 334 {NULL, NULL, 0} 335 }; 336 337 338 339 340 void init_Scrap(void) 341 { 342 PyObject *m; 343 #ifndef __LP64__ 344 PyObject *d; 345 #endif /* __LP64__ */ 346 347 348 349 350 m = Py_InitModule("_Scrap", Scrap_methods); 351 #ifndef __LP64__ 352 d = PyModule_GetDict(m); 353 Scrap_Error = PyMac_GetOSErrException(); 354 if (Scrap_Error == NULL || 355 PyDict_SetItemString(d, "Error", Scrap_Error) != 0) 356 return; 357 Scrap_Type.ob_type = &PyType_Type; 358 Py_INCREF(&Scrap_Type); 359 if (PyDict_SetItemString(d, "ScrapType", (PyObject *)&Scrap_Type) != 0) 360 Py_FatalError("can't initialize ScrapType"); 361 #endif /* __LP64__ */ 362 } 363 364 /* ======================= End module _Scrap ======================== */ 365 366