Jack Jansen | 686f9c3 | 2001-06-26 21:51:18 +0000 | [diff] [blame] | 1 | |
| 2 | /* =========================== Module CF ============================ */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
| 6 | |
| 7 | |
| 8 | #include "macglue.h" |
| 9 | #include "pymactoolbox.h" |
| 10 | |
| 11 | #ifdef WITHOUT_FRAMEWORKS |
| 12 | #include <CoreFoundation.h> |
| 13 | #else |
| 14 | #include <CoreFoundation.h> |
| 15 | #endif |
| 16 | |
| 17 | /* For now we declare them forward here. They'll go to mactoolbox later */ |
| 18 | extern PyObject *CFTypeRefObj_New(CFTypeRef); |
| 19 | extern int CFTypeRefObj_Convert(PyObject *, CFTypeRef *); |
| 20 | extern PyObject *CFStringRefObj_New(CFStringRef); |
| 21 | extern int CFStringRefObj_Convert(PyObject *, CFStringRef *); |
| 22 | |
| 23 | #ifdef NOTYET_USE_TOOLBOX_OBJECT_GLUE |
| 24 | //extern PyObject *_CFTypeRefObj_New(CFTypeRef); |
| 25 | //extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *); |
| 26 | |
| 27 | //#define CFTypeRefObj_New _CFTypeRefObj_New |
| 28 | //#define CFTypeRefObj_Convert _CFTypeRefObj_Convert |
| 29 | #endif |
| 30 | |
| 31 | |
| 32 | static PyObject *CF_Error; |
| 33 | |
| 34 | /* --------------------- Object type CFTypeRef ---------------------- */ |
| 35 | |
| 36 | PyTypeObject CFTypeRef_Type; |
| 37 | |
| 38 | #define CFTypeRefObj_Check(x) ((x)->ob_type == &CFTypeRef_Type) |
| 39 | |
| 40 | typedef struct CFTypeRefObject { |
| 41 | PyObject_HEAD |
| 42 | CFTypeRef ob_itself; |
| 43 | void (*ob_freeit)(CFTypeRef ptr); |
| 44 | } CFTypeRefObject; |
| 45 | |
| 46 | PyObject *CFTypeRefObj_New(CFTypeRef itself) |
| 47 | { |
| 48 | CFTypeRefObject *it; |
| 49 | if (itself == NULL) return PyMac_Error(resNotFound); |
| 50 | CFRetain(itself); |
| 51 | it = PyObject_NEW(CFTypeRefObject, &CFTypeRef_Type); |
| 52 | if (it == NULL) return NULL; |
| 53 | it->ob_itself = itself; |
| 54 | it->ob_freeit = CFRelease; |
| 55 | return (PyObject *)it; |
| 56 | } |
| 57 | CFTypeRefObj_Convert(PyObject *v, CFTypeRef *p_itself) |
| 58 | { |
| 59 | |
| 60 | if (v == Py_None) { *p_itself = NULL; return 1; } |
| 61 | /* Check for other CF objects here */ |
| 62 | |
| 63 | if (!CFTypeRefObj_Check(v)) |
| 64 | { |
| 65 | PyErr_SetString(PyExc_TypeError, "CFTypeRef required"); |
| 66 | return 0; |
| 67 | } |
| 68 | *p_itself = ((CFTypeRefObject *)v)->ob_itself; |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | static void CFTypeRefObj_dealloc(CFTypeRefObject *self) |
| 73 | { |
| 74 | if (self->ob_freeit && self->ob_itself) |
| 75 | { |
| 76 | self->ob_freeit((CFTypeRef)self->ob_itself); |
| 77 | } |
| 78 | PyMem_DEL(self); |
| 79 | } |
| 80 | |
| 81 | static PyObject *CFTypeRefObj_CFGetTypeID(CFTypeRefObject *_self, PyObject *_args) |
| 82 | { |
| 83 | PyObject *_res = NULL; |
| 84 | CFTypeID _rv; |
| 85 | if (!PyArg_ParseTuple(_args, "")) |
| 86 | return NULL; |
| 87 | _rv = CFGetTypeID(_self->ob_itself); |
| 88 | _res = Py_BuildValue("l", |
| 89 | _rv); |
| 90 | return _res; |
| 91 | } |
| 92 | |
| 93 | static PyObject *CFTypeRefObj_CFRetain(CFTypeRefObject *_self, PyObject *_args) |
| 94 | { |
| 95 | PyObject *_res = NULL; |
| 96 | CFTypeRef _rv; |
| 97 | if (!PyArg_ParseTuple(_args, "")) |
| 98 | return NULL; |
| 99 | _rv = CFRetain(_self->ob_itself); |
| 100 | _res = Py_BuildValue("O&", |
| 101 | CFTypeRefObj_New, _rv); |
| 102 | return _res; |
| 103 | } |
| 104 | |
| 105 | static PyObject *CFTypeRefObj_CFRelease(CFTypeRefObject *_self, PyObject *_args) |
| 106 | { |
| 107 | PyObject *_res = NULL; |
| 108 | if (!PyArg_ParseTuple(_args, "")) |
| 109 | return NULL; |
| 110 | CFRelease(_self->ob_itself); |
| 111 | Py_INCREF(Py_None); |
| 112 | _res = Py_None; |
| 113 | return _res; |
| 114 | } |
| 115 | |
| 116 | static PyObject *CFTypeRefObj_CFGetRetainCount(CFTypeRefObject *_self, PyObject *_args) |
| 117 | { |
| 118 | PyObject *_res = NULL; |
| 119 | CFIndex _rv; |
| 120 | if (!PyArg_ParseTuple(_args, "")) |
| 121 | return NULL; |
| 122 | _rv = CFGetRetainCount(_self->ob_itself); |
| 123 | _res = Py_BuildValue("l", |
| 124 | _rv); |
| 125 | return _res; |
| 126 | } |
| 127 | |
| 128 | static PyObject *CFTypeRefObj_CFEqual(CFTypeRefObject *_self, PyObject *_args) |
| 129 | { |
| 130 | PyObject *_res = NULL; |
| 131 | Boolean _rv; |
| 132 | CFTypeRef cf2; |
| 133 | if (!PyArg_ParseTuple(_args, "O&", |
| 134 | CFTypeRefObj_Convert, &cf2)) |
| 135 | return NULL; |
| 136 | _rv = CFEqual(_self->ob_itself, |
| 137 | cf2); |
| 138 | _res = Py_BuildValue("l", |
| 139 | _rv); |
| 140 | return _res; |
| 141 | } |
| 142 | |
| 143 | static PyObject *CFTypeRefObj_CFHash(CFTypeRefObject *_self, PyObject *_args) |
| 144 | { |
| 145 | PyObject *_res = NULL; |
| 146 | CFHashCode _rv; |
| 147 | if (!PyArg_ParseTuple(_args, "")) |
| 148 | return NULL; |
| 149 | _rv = CFHash(_self->ob_itself); |
| 150 | _res = Py_BuildValue("l", |
| 151 | _rv); |
| 152 | return _res; |
| 153 | } |
| 154 | |
| 155 | static PyObject *CFTypeRefObj_CFCopyDescription(CFTypeRefObject *_self, PyObject *_args) |
| 156 | { |
| 157 | PyObject *_res = NULL; |
| 158 | CFStringRef _rv; |
| 159 | if (!PyArg_ParseTuple(_args, "")) |
| 160 | return NULL; |
| 161 | _rv = CFCopyDescription(_self->ob_itself); |
| 162 | _res = Py_BuildValue("O&", |
| 163 | CFStringRefObj_New, _rv); |
| 164 | return _res; |
| 165 | } |
| 166 | |
| 167 | static PyMethodDef CFTypeRefObj_methods[] = { |
| 168 | {"CFGetTypeID", (PyCFunction)CFTypeRefObj_CFGetTypeID, 1, |
| 169 | "() -> (CFTypeID _rv)"}, |
| 170 | {"CFRetain", (PyCFunction)CFTypeRefObj_CFRetain, 1, |
| 171 | "() -> (CFTypeRef _rv)"}, |
| 172 | {"CFRelease", (PyCFunction)CFTypeRefObj_CFRelease, 1, |
| 173 | "() -> None"}, |
| 174 | {"CFGetRetainCount", (PyCFunction)CFTypeRefObj_CFGetRetainCount, 1, |
| 175 | "() -> (CFIndex _rv)"}, |
| 176 | {"CFEqual", (PyCFunction)CFTypeRefObj_CFEqual, 1, |
| 177 | "(CFTypeRef cf2) -> (Boolean _rv)"}, |
| 178 | {"CFHash", (PyCFunction)CFTypeRefObj_CFHash, 1, |
| 179 | "() -> (CFHashCode _rv)"}, |
| 180 | {"CFCopyDescription", (PyCFunction)CFTypeRefObj_CFCopyDescription, 1, |
| 181 | "() -> (CFStringRef _rv)"}, |
| 182 | {NULL, NULL, 0} |
| 183 | }; |
| 184 | |
| 185 | PyMethodChain CFTypeRefObj_chain = { CFTypeRefObj_methods, NULL }; |
| 186 | |
| 187 | static PyObject *CFTypeRefObj_getattr(CFTypeRefObject *self, char *name) |
| 188 | { |
| 189 | return Py_FindMethodInChain(&CFTypeRefObj_chain, (PyObject *)self, name); |
| 190 | } |
| 191 | |
| 192 | #define CFTypeRefObj_setattr NULL |
| 193 | |
| 194 | static int CFTypeRefObj_compare(CFTypeRefObject *self, CFTypeRefObject *other) |
| 195 | { |
| 196 | /* XXXX Or should we use CFEqual?? */ |
| 197 | if ( self->ob_itself > other->ob_itself ) return 1; |
| 198 | if ( self->ob_itself < other->ob_itself ) return -1; |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static PyObject * CFTypeRefObj_repr(CFTypeRefObject *self) |
| 203 | { |
| 204 | char buf[100]; |
| 205 | sprintf(buf, "<CFTypeRef type-%d object at 0x%08.8x for 0x%08.8x>", CFGetTypeID(self->ob_itself), self, self->ob_itself); |
| 206 | return PyString_FromString(buf); |
| 207 | } |
| 208 | |
| 209 | static int CFTypeRefObj_hash(CFTypeRefObject *self) |
| 210 | { |
| 211 | /* XXXX Or should we use CFHash?? */ |
| 212 | return (int)self->ob_itself; |
| 213 | } |
| 214 | |
| 215 | PyTypeObject CFTypeRef_Type = { |
| 216 | PyObject_HEAD_INIT(&PyType_Type) |
| 217 | 0, /*ob_size*/ |
| 218 | "CFTypeRef", /*tp_name*/ |
| 219 | sizeof(CFTypeRefObject), /*tp_basicsize*/ |
| 220 | 0, /*tp_itemsize*/ |
| 221 | /* methods */ |
| 222 | (destructor) CFTypeRefObj_dealloc, /*tp_dealloc*/ |
| 223 | 0, /*tp_print*/ |
| 224 | (getattrfunc) CFTypeRefObj_getattr, /*tp_getattr*/ |
| 225 | (setattrfunc) CFTypeRefObj_setattr, /*tp_setattr*/ |
| 226 | (cmpfunc) CFTypeRefObj_compare, /*tp_compare*/ |
| 227 | (reprfunc) CFTypeRefObj_repr, /*tp_repr*/ |
| 228 | (PyNumberMethods *)0, /* tp_as_number */ |
| 229 | (PySequenceMethods *)0, /* tp_as_sequence */ |
| 230 | (PyMappingMethods *)0, /* tp_as_mapping */ |
| 231 | (hashfunc) CFTypeRefObj_hash, /*tp_hash*/ |
| 232 | }; |
| 233 | |
| 234 | /* ------------------- End object type CFTypeRef -------------------- */ |
| 235 | |
| 236 | |
| 237 | /* -------------------- Object type CFStringRef --------------------- */ |
| 238 | |
| 239 | PyTypeObject CFStringRef_Type; |
| 240 | |
| 241 | #define CFStringRefObj_Check(x) ((x)->ob_type == &CFStringRef_Type) |
| 242 | |
| 243 | typedef struct CFStringRefObject { |
| 244 | PyObject_HEAD |
| 245 | CFStringRef ob_itself; |
| 246 | void (*ob_freeit)(CFTypeRef ptr); |
| 247 | } CFStringRefObject; |
| 248 | |
| 249 | PyObject *CFStringRefObj_New(CFStringRef itself) |
| 250 | { |
| 251 | CFStringRefObject *it; |
| 252 | if (itself == NULL) return PyMac_Error(resNotFound); |
| 253 | CFRetain(itself); |
| 254 | it = PyObject_NEW(CFStringRefObject, &CFStringRef_Type); |
| 255 | if (it == NULL) return NULL; |
| 256 | it->ob_itself = itself; |
| 257 | it->ob_freeit = CFRelease; |
| 258 | return (PyObject *)it; |
| 259 | } |
| 260 | CFStringRefObj_Convert(PyObject *v, CFStringRef *p_itself) |
| 261 | { |
| 262 | |
| 263 | if (v == Py_None) { *p_itself = NULL; return 1; } |
| 264 | /* Check for other CF objects here */ |
| 265 | |
| 266 | if (!CFStringRefObj_Check(v)) |
| 267 | { |
| 268 | PyErr_SetString(PyExc_TypeError, "CFStringRef required"); |
| 269 | return 0; |
| 270 | } |
| 271 | *p_itself = ((CFStringRefObject *)v)->ob_itself; |
| 272 | return 1; |
| 273 | } |
| 274 | |
| 275 | static void CFStringRefObj_dealloc(CFStringRefObject *self) |
| 276 | { |
| 277 | if (self->ob_freeit && self->ob_itself) |
| 278 | { |
| 279 | self->ob_freeit((CFTypeRef)self->ob_itself); |
| 280 | } |
| 281 | PyMem_DEL(self); |
| 282 | } |
| 283 | |
| 284 | static PyMethodDef CFStringRefObj_methods[] = { |
| 285 | {NULL, NULL, 0} |
| 286 | }; |
| 287 | |
| 288 | PyMethodChain CFStringRefObj_chain = { CFStringRefObj_methods, NULL }; |
| 289 | |
| 290 | static PyObject *CFStringRefObj_getattr(CFStringRefObject *self, char *name) |
| 291 | { |
| 292 | return Py_FindMethodInChain(&CFStringRefObj_chain, (PyObject *)self, name); |
| 293 | } |
| 294 | |
| 295 | #define CFStringRefObj_setattr NULL |
| 296 | |
| 297 | static int CFStringRefObj_compare(CFStringRefObject *self, CFStringRefObject *other) |
| 298 | { |
| 299 | /* XXXX Or should we use CFEqual?? */ |
| 300 | if ( self->ob_itself > other->ob_itself ) return 1; |
| 301 | if ( self->ob_itself < other->ob_itself ) return -1; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static PyObject * CFStringRefObj_repr(CFStringRefObject *self) |
| 306 | { |
| 307 | char buf[100]; |
| 308 | sprintf(buf, "<CFTypeRef type-%d object at 0x%08.8x for 0x%08.8x>", CFGetTypeID(self->ob_itself), self, self->ob_itself); |
| 309 | return PyString_FromString(buf); |
| 310 | } |
| 311 | |
| 312 | static int CFStringRefObj_hash(CFStringRefObject *self) |
| 313 | { |
| 314 | /* XXXX Or should we use CFHash?? */ |
| 315 | return (int)self->ob_itself; |
| 316 | } |
| 317 | |
| 318 | PyTypeObject CFStringRef_Type = { |
| 319 | PyObject_HEAD_INIT(&PyType_Type) |
| 320 | 0, /*ob_size*/ |
| 321 | "CFStringRef", /*tp_name*/ |
| 322 | sizeof(CFStringRefObject), /*tp_basicsize*/ |
| 323 | 0, /*tp_itemsize*/ |
| 324 | /* methods */ |
| 325 | (destructor) CFStringRefObj_dealloc, /*tp_dealloc*/ |
| 326 | 0, /*tp_print*/ |
| 327 | (getattrfunc) CFStringRefObj_getattr, /*tp_getattr*/ |
| 328 | (setattrfunc) CFStringRefObj_setattr, /*tp_setattr*/ |
| 329 | (cmpfunc) CFStringRefObj_compare, /*tp_compare*/ |
| 330 | (reprfunc) CFStringRefObj_repr, /*tp_repr*/ |
| 331 | (PyNumberMethods *)0, /* tp_as_number */ |
| 332 | (PySequenceMethods *)0, /* tp_as_sequence */ |
| 333 | (PyMappingMethods *)0, /* tp_as_mapping */ |
| 334 | (hashfunc) CFStringRefObj_hash, /*tp_hash*/ |
| 335 | }; |
| 336 | |
| 337 | /* ------------------ End object type CFStringRef ------------------- */ |
| 338 | |
| 339 | |
| 340 | static PyObject *CF_CFAllocatorGetTypeID(PyObject *_self, PyObject *_args) |
| 341 | { |
| 342 | PyObject *_res = NULL; |
| 343 | CFTypeID _rv; |
| 344 | if (!PyArg_ParseTuple(_args, "")) |
| 345 | return NULL; |
| 346 | _rv = CFAllocatorGetTypeID(); |
| 347 | _res = Py_BuildValue("l", |
| 348 | _rv); |
| 349 | return _res; |
| 350 | } |
| 351 | |
| 352 | static PyObject *CF_CFAllocatorGetPreferredSizeForSize(PyObject *_self, PyObject *_args) |
| 353 | { |
| 354 | PyObject *_res = NULL; |
| 355 | CFIndex _rv; |
| 356 | CFIndex size; |
| 357 | CFOptionFlags hint; |
| 358 | if (!PyArg_ParseTuple(_args, "ll", |
| 359 | &size, |
| 360 | &hint)) |
| 361 | return NULL; |
| 362 | _rv = CFAllocatorGetPreferredSizeForSize((CFAllocatorRef)NULL, |
| 363 | size, |
| 364 | hint); |
| 365 | _res = Py_BuildValue("l", |
| 366 | _rv); |
| 367 | return _res; |
| 368 | } |
| 369 | |
| 370 | static PyObject *CF_CFCopyTypeIDDescription(PyObject *_self, PyObject *_args) |
| 371 | { |
| 372 | PyObject *_res = NULL; |
| 373 | CFStringRef _rv; |
| 374 | CFTypeID theType; |
| 375 | if (!PyArg_ParseTuple(_args, "l", |
| 376 | &theType)) |
| 377 | return NULL; |
| 378 | _rv = CFCopyTypeIDDescription(theType); |
| 379 | _res = Py_BuildValue("O&", |
| 380 | CFStringRefObj_New, _rv); |
| 381 | return _res; |
| 382 | } |
| 383 | |
| 384 | static PyMethodDef CF_methods[] = { |
| 385 | {"CFAllocatorGetTypeID", (PyCFunction)CF_CFAllocatorGetTypeID, 1, |
| 386 | "() -> (CFTypeID _rv)"}, |
| 387 | {"CFAllocatorGetPreferredSizeForSize", (PyCFunction)CF_CFAllocatorGetPreferredSizeForSize, 1, |
| 388 | "(CFIndex size, CFOptionFlags hint) -> (CFIndex _rv)"}, |
| 389 | {"CFCopyTypeIDDescription", (PyCFunction)CF_CFCopyTypeIDDescription, 1, |
| 390 | "(CFTypeID theType) -> (CFStringRef _rv)"}, |
| 391 | {NULL, NULL, 0} |
| 392 | }; |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | void initCF(void) |
| 398 | { |
| 399 | PyObject *m; |
| 400 | PyObject *d; |
| 401 | |
| 402 | |
| 403 | |
| 404 | // PyMac_INIT_TOOLBOX_OBJECT_NEW(Track, TrackObj_New); |
| 405 | // PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Track, TrackObj_Convert); |
| 406 | |
| 407 | |
| 408 | m = Py_InitModule("CF", CF_methods); |
| 409 | d = PyModule_GetDict(m); |
| 410 | CF_Error = PyMac_GetOSErrException(); |
| 411 | if (CF_Error == NULL || |
| 412 | PyDict_SetItemString(d, "Error", CF_Error) != 0) |
| 413 | return; |
| 414 | CFTypeRef_Type.ob_type = &PyType_Type; |
| 415 | Py_INCREF(&CFTypeRef_Type); |
| 416 | if (PyDict_SetItemString(d, "CFTypeRefType", (PyObject *)&CFTypeRef_Type) != 0) |
| 417 | Py_FatalError("can't initialize CFTypeRefType"); |
| 418 | CFStringRef_Type.ob_type = &PyType_Type; |
| 419 | Py_INCREF(&CFStringRef_Type); |
| 420 | if (PyDict_SetItemString(d, "CFStringRefType", (PyObject *)&CFStringRef_Type) != 0) |
| 421 | Py_FatalError("can't initialize CFStringRefType"); |
| 422 | } |
| 423 | |
| 424 | /* ========================= End module CF ========================== */ |
| 425 | |