This commit was manufactured by cvs2svn to create tag 'r212'.
[python/dscho.git] / Mac / Modules / qdoffs / Qdoffsmodule.c
blobabe36305d368e56b567e6924df75164210bb57c5
2 /* ========================= Module Qdoffs ========================== */
4 #include "Python.h"
8 #include "macglue.h"
9 #include "pymactoolbox.h"
11 #ifdef WITHOUT_FRAMEWORKS
12 #include <QDOffscreen.h>
13 #else
14 #include <Carbon/Carbon.h>
15 #endif
17 #ifdef USE_TOOLBOX_OBJECT_GLUE
18 extern PyObject *_GWorldObj_New(GWorldPtr);
19 extern int _GWorldObj_Convert(PyObject *, GWorldPtr *);
21 #define GWorldObj_New _GWorldObj_New
22 #define GWorldObj_Convert _GWorldObj_Convert
23 #endif
25 #define as_GrafPtr(gworld) ((GrafPtr)(gworld))
28 static PyObject *Qdoffs_Error;
30 /* ----------------------- Object type GWorld ----------------------- */
32 PyTypeObject GWorld_Type;
34 #define GWorldObj_Check(x) ((x)->ob_type == &GWorld_Type)
36 typedef struct GWorldObject {
37 PyObject_HEAD
38 GWorldPtr ob_itself;
39 } GWorldObject;
41 PyObject *GWorldObj_New(GWorldPtr itself)
43 GWorldObject *it;
44 if (itself == NULL) return PyMac_Error(resNotFound);
45 it = PyObject_NEW(GWorldObject, &GWorld_Type);
46 if (it == NULL) return NULL;
47 it->ob_itself = itself;
48 return (PyObject *)it;
50 GWorldObj_Convert(PyObject *v, GWorldPtr *p_itself)
52 if (!GWorldObj_Check(v))
54 PyErr_SetString(PyExc_TypeError, "GWorld required");
55 return 0;
57 *p_itself = ((GWorldObject *)v)->ob_itself;
58 return 1;
61 static void GWorldObj_dealloc(GWorldObject *self)
63 DisposeGWorld(self->ob_itself);
64 PyMem_DEL(self);
67 static PyObject *GWorldObj_GetGWorldDevice(GWorldObject *_self, PyObject *_args)
69 PyObject *_res = NULL;
70 GDHandle _rv;
71 if (!PyArg_ParseTuple(_args, ""))
72 return NULL;
73 _rv = GetGWorldDevice(_self->ob_itself);
74 _res = Py_BuildValue("O&",
75 ResObj_New, _rv);
76 return _res;
79 static PyObject *GWorldObj_GetGWorldPixMap(GWorldObject *_self, PyObject *_args)
81 PyObject *_res = NULL;
82 PixMapHandle _rv;
83 if (!PyArg_ParseTuple(_args, ""))
84 return NULL;
85 _rv = GetGWorldPixMap(_self->ob_itself);
86 _res = Py_BuildValue("O&",
87 ResObj_New, _rv);
88 return _res;
91 static PyObject *GWorldObj_as_GrafPtr(GWorldObject *_self, PyObject *_args)
93 PyObject *_res = NULL;
94 GrafPtr _rv;
95 if (!PyArg_ParseTuple(_args, ""))
96 return NULL;
97 _rv = as_GrafPtr(_self->ob_itself);
98 _res = Py_BuildValue("O&",
99 GrafObj_New, _rv);
100 return _res;
103 static PyMethodDef GWorldObj_methods[] = {
104 {"GetGWorldDevice", (PyCFunction)GWorldObj_GetGWorldDevice, 1,
105 "() -> (GDHandle _rv)"},
106 {"GetGWorldPixMap", (PyCFunction)GWorldObj_GetGWorldPixMap, 1,
107 "() -> (PixMapHandle _rv)"},
108 {"as_GrafPtr", (PyCFunction)GWorldObj_as_GrafPtr, 1,
109 "() -> (GrafPtr _rv)"},
110 {NULL, NULL, 0}
113 PyMethodChain GWorldObj_chain = { GWorldObj_methods, NULL };
115 static PyObject *GWorldObj_getattr(GWorldObject *self, char *name)
117 return Py_FindMethodInChain(&GWorldObj_chain, (PyObject *)self, name);
120 #define GWorldObj_setattr NULL
122 #define GWorldObj_compare NULL
124 #define GWorldObj_repr NULL
126 #define GWorldObj_hash NULL
128 PyTypeObject GWorld_Type = {
129 PyObject_HEAD_INIT(&PyType_Type)
130 0, /*ob_size*/
131 "GWorld", /*tp_name*/
132 sizeof(GWorldObject), /*tp_basicsize*/
133 0, /*tp_itemsize*/
134 /* methods */
135 (destructor) GWorldObj_dealloc, /*tp_dealloc*/
136 0, /*tp_print*/
137 (getattrfunc) GWorldObj_getattr, /*tp_getattr*/
138 (setattrfunc) GWorldObj_setattr, /*tp_setattr*/
139 (cmpfunc) GWorldObj_compare, /*tp_compare*/
140 (reprfunc) GWorldObj_repr, /*tp_repr*/
141 (PyNumberMethods *)0, /* tp_as_number */
142 (PySequenceMethods *)0, /* tp_as_sequence */
143 (PyMappingMethods *)0, /* tp_as_mapping */
144 (hashfunc) GWorldObj_hash, /*tp_hash*/
147 /* --------------------- End object type GWorld --------------------- */
150 static PyObject *Qdoffs_NewGWorld(PyObject *_self, PyObject *_args)
152 PyObject *_res = NULL;
153 QDErr _err;
154 GWorldPtr offscreenGWorld;
155 short PixelDepth;
156 Rect boundsRect;
157 CTabHandle cTable;
158 GDHandle aGDevice;
159 GWorldFlags flags;
160 if (!PyArg_ParseTuple(_args, "hO&O&O&l",
161 &PixelDepth,
162 PyMac_GetRect, &boundsRect,
163 OptResObj_Convert, &cTable,
164 OptResObj_Convert, &aGDevice,
165 &flags))
166 return NULL;
167 _err = NewGWorld(&offscreenGWorld,
168 PixelDepth,
169 &boundsRect,
170 cTable,
171 aGDevice,
172 flags);
173 if (_err != noErr) return PyMac_Error(_err);
174 _res = Py_BuildValue("O&",
175 GWorldObj_New, offscreenGWorld);
176 return _res;
179 static PyObject *Qdoffs_LockPixels(PyObject *_self, PyObject *_args)
181 PyObject *_res = NULL;
182 Boolean _rv;
183 PixMapHandle pm;
184 if (!PyArg_ParseTuple(_args, "O&",
185 ResObj_Convert, &pm))
186 return NULL;
187 _rv = LockPixels(pm);
188 _res = Py_BuildValue("b",
189 _rv);
190 return _res;
193 static PyObject *Qdoffs_UnlockPixels(PyObject *_self, PyObject *_args)
195 PyObject *_res = NULL;
196 PixMapHandle pm;
197 if (!PyArg_ParseTuple(_args, "O&",
198 ResObj_Convert, &pm))
199 return NULL;
200 UnlockPixels(pm);
201 Py_INCREF(Py_None);
202 _res = Py_None;
203 return _res;
206 static PyObject *Qdoffs_UpdateGWorld(PyObject *_self, PyObject *_args)
208 PyObject *_res = NULL;
209 GWorldFlags _rv;
210 GWorldPtr offscreenGWorld;
211 short pixelDepth;
212 Rect boundsRect;
213 CTabHandle cTable;
214 GDHandle aGDevice;
215 GWorldFlags flags;
216 if (!PyArg_ParseTuple(_args, "hO&O&O&l",
217 &pixelDepth,
218 PyMac_GetRect, &boundsRect,
219 OptResObj_Convert, &cTable,
220 OptResObj_Convert, &aGDevice,
221 &flags))
222 return NULL;
223 _rv = UpdateGWorld(&offscreenGWorld,
224 pixelDepth,
225 &boundsRect,
226 cTable,
227 aGDevice,
228 flags);
229 _res = Py_BuildValue("lO&",
230 _rv,
231 GWorldObj_New, offscreenGWorld);
232 return _res;
235 static PyObject *Qdoffs_GetGWorld(PyObject *_self, PyObject *_args)
237 PyObject *_res = NULL;
238 CGrafPtr port;
239 GDHandle gdh;
240 if (!PyArg_ParseTuple(_args, ""))
241 return NULL;
242 GetGWorld(&port,
243 &gdh);
244 _res = Py_BuildValue("O&O&",
245 GrafObj_New, port,
246 ResObj_New, gdh);
247 return _res;
250 static PyObject *Qdoffs_SetGWorld(PyObject *_self, PyObject *_args)
252 PyObject *_res = NULL;
253 CGrafPtr port;
254 GDHandle gdh;
255 if (!PyArg_ParseTuple(_args, "O&O&",
256 GrafObj_Convert, &port,
257 OptResObj_Convert, &gdh))
258 return NULL;
259 SetGWorld(port,
260 gdh);
261 Py_INCREF(Py_None);
262 _res = Py_None;
263 return _res;
266 static PyObject *Qdoffs_CTabChanged(PyObject *_self, PyObject *_args)
268 PyObject *_res = NULL;
269 CTabHandle ctab;
270 if (!PyArg_ParseTuple(_args, "O&",
271 OptResObj_Convert, &ctab))
272 return NULL;
273 CTabChanged(ctab);
274 Py_INCREF(Py_None);
275 _res = Py_None;
276 return _res;
279 static PyObject *Qdoffs_PixPatChanged(PyObject *_self, PyObject *_args)
281 PyObject *_res = NULL;
282 PixPatHandle ppat;
283 if (!PyArg_ParseTuple(_args, "O&",
284 ResObj_Convert, &ppat))
285 return NULL;
286 PixPatChanged(ppat);
287 Py_INCREF(Py_None);
288 _res = Py_None;
289 return _res;
292 static PyObject *Qdoffs_PortChanged(PyObject *_self, PyObject *_args)
294 PyObject *_res = NULL;
295 GrafPtr port;
296 if (!PyArg_ParseTuple(_args, "O&",
297 GrafObj_Convert, &port))
298 return NULL;
299 PortChanged(port);
300 Py_INCREF(Py_None);
301 _res = Py_None;
302 return _res;
305 static PyObject *Qdoffs_GDeviceChanged(PyObject *_self, PyObject *_args)
307 PyObject *_res = NULL;
308 GDHandle gdh;
309 if (!PyArg_ParseTuple(_args, "O&",
310 OptResObj_Convert, &gdh))
311 return NULL;
312 GDeviceChanged(gdh);
313 Py_INCREF(Py_None);
314 _res = Py_None;
315 return _res;
318 static PyObject *Qdoffs_AllowPurgePixels(PyObject *_self, PyObject *_args)
320 PyObject *_res = NULL;
321 PixMapHandle pm;
322 if (!PyArg_ParseTuple(_args, "O&",
323 ResObj_Convert, &pm))
324 return NULL;
325 AllowPurgePixels(pm);
326 Py_INCREF(Py_None);
327 _res = Py_None;
328 return _res;
331 static PyObject *Qdoffs_NoPurgePixels(PyObject *_self, PyObject *_args)
333 PyObject *_res = NULL;
334 PixMapHandle pm;
335 if (!PyArg_ParseTuple(_args, "O&",
336 ResObj_Convert, &pm))
337 return NULL;
338 NoPurgePixels(pm);
339 Py_INCREF(Py_None);
340 _res = Py_None;
341 return _res;
344 static PyObject *Qdoffs_GetPixelsState(PyObject *_self, PyObject *_args)
346 PyObject *_res = NULL;
347 GWorldFlags _rv;
348 PixMapHandle pm;
349 if (!PyArg_ParseTuple(_args, "O&",
350 ResObj_Convert, &pm))
351 return NULL;
352 _rv = GetPixelsState(pm);
353 _res = Py_BuildValue("l",
354 _rv);
355 return _res;
358 static PyObject *Qdoffs_SetPixelsState(PyObject *_self, PyObject *_args)
360 PyObject *_res = NULL;
361 PixMapHandle pm;
362 GWorldFlags state;
363 if (!PyArg_ParseTuple(_args, "O&l",
364 ResObj_Convert, &pm,
365 &state))
366 return NULL;
367 SetPixelsState(pm,
368 state);
369 Py_INCREF(Py_None);
370 _res = Py_None;
371 return _res;
374 static PyObject *Qdoffs_GetPixRowBytes(PyObject *_self, PyObject *_args)
376 PyObject *_res = NULL;
377 long _rv;
378 PixMapHandle pm;
379 if (!PyArg_ParseTuple(_args, "O&",
380 ResObj_Convert, &pm))
381 return NULL;
382 _rv = GetPixRowBytes(pm);
383 _res = Py_BuildValue("l",
384 _rv);
385 return _res;
388 static PyObject *Qdoffs_NewScreenBuffer(PyObject *_self, PyObject *_args)
390 PyObject *_res = NULL;
391 QDErr _err;
392 Rect globalRect;
393 Boolean purgeable;
394 GDHandle gdh;
395 PixMapHandle offscreenPixMap;
396 if (!PyArg_ParseTuple(_args, "O&b",
397 PyMac_GetRect, &globalRect,
398 &purgeable))
399 return NULL;
400 _err = NewScreenBuffer(&globalRect,
401 purgeable,
402 &gdh,
403 &offscreenPixMap);
404 if (_err != noErr) return PyMac_Error(_err);
405 _res = Py_BuildValue("O&O&",
406 ResObj_New, gdh,
407 ResObj_New, offscreenPixMap);
408 return _res;
411 static PyObject *Qdoffs_DisposeScreenBuffer(PyObject *_self, PyObject *_args)
413 PyObject *_res = NULL;
414 PixMapHandle offscreenPixMap;
415 if (!PyArg_ParseTuple(_args, "O&",
416 ResObj_Convert, &offscreenPixMap))
417 return NULL;
418 DisposeScreenBuffer(offscreenPixMap);
419 Py_INCREF(Py_None);
420 _res = Py_None;
421 return _res;
424 static PyObject *Qdoffs_QDDone(PyObject *_self, PyObject *_args)
426 PyObject *_res = NULL;
427 Boolean _rv;
428 GrafPtr port;
429 if (!PyArg_ParseTuple(_args, "O&",
430 GrafObj_Convert, &port))
431 return NULL;
432 _rv = QDDone(port);
433 _res = Py_BuildValue("b",
434 _rv);
435 return _res;
438 static PyObject *Qdoffs_OffscreenVersion(PyObject *_self, PyObject *_args)
440 PyObject *_res = NULL;
441 long _rv;
442 if (!PyArg_ParseTuple(_args, ""))
443 return NULL;
444 _rv = OffscreenVersion();
445 _res = Py_BuildValue("l",
446 _rv);
447 return _res;
450 static PyObject *Qdoffs_NewTempScreenBuffer(PyObject *_self, PyObject *_args)
452 PyObject *_res = NULL;
453 QDErr _err;
454 Rect globalRect;
455 Boolean purgeable;
456 GDHandle gdh;
457 PixMapHandle offscreenPixMap;
458 if (!PyArg_ParseTuple(_args, "O&b",
459 PyMac_GetRect, &globalRect,
460 &purgeable))
461 return NULL;
462 _err = NewTempScreenBuffer(&globalRect,
463 purgeable,
464 &gdh,
465 &offscreenPixMap);
466 if (_err != noErr) return PyMac_Error(_err);
467 _res = Py_BuildValue("O&O&",
468 ResObj_New, gdh,
469 ResObj_New, offscreenPixMap);
470 return _res;
473 static PyObject *Qdoffs_PixMap32Bit(PyObject *_self, PyObject *_args)
475 PyObject *_res = NULL;
476 Boolean _rv;
477 PixMapHandle pmHandle;
478 if (!PyArg_ParseTuple(_args, "O&",
479 ResObj_Convert, &pmHandle))
480 return NULL;
481 _rv = PixMap32Bit(pmHandle);
482 _res = Py_BuildValue("b",
483 _rv);
484 return _res;
487 static PyObject *Qdoffs_GetPixMapBytes(PyObject *_self, PyObject *_args)
489 PyObject *_res = NULL;
491 PixMapHandle pm;
492 int from, length;
493 char *cp;
495 if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) )
496 return NULL;
497 cp = GetPixBaseAddr(pm)+from;
498 return PyString_FromStringAndSize(cp, length);
502 static PyObject *Qdoffs_PutPixMapBytes(PyObject *_self, PyObject *_args)
504 PyObject *_res = NULL;
506 PixMapHandle pm;
507 int from, length;
508 char *cp, *icp;
510 if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) )
511 return NULL;
512 cp = GetPixBaseAddr(pm)+from;
513 memcpy(cp, icp, length);
514 Py_INCREF(Py_None);
515 return Py_None;
519 static PyMethodDef Qdoffs_methods[] = {
520 {"NewGWorld", (PyCFunction)Qdoffs_NewGWorld, 1,
521 "(short PixelDepth, Rect boundsRect, CTabHandle cTable, GDHandle aGDevice, GWorldFlags flags) -> (GWorldPtr offscreenGWorld)"},
522 {"LockPixels", (PyCFunction)Qdoffs_LockPixels, 1,
523 "(PixMapHandle pm) -> (Boolean _rv)"},
524 {"UnlockPixels", (PyCFunction)Qdoffs_UnlockPixels, 1,
525 "(PixMapHandle pm) -> None"},
526 {"UpdateGWorld", (PyCFunction)Qdoffs_UpdateGWorld, 1,
527 "(short pixelDepth, Rect boundsRect, CTabHandle cTable, GDHandle aGDevice, GWorldFlags flags) -> (GWorldFlags _rv, GWorldPtr offscreenGWorld)"},
528 {"GetGWorld", (PyCFunction)Qdoffs_GetGWorld, 1,
529 "() -> (CGrafPtr port, GDHandle gdh)"},
530 {"SetGWorld", (PyCFunction)Qdoffs_SetGWorld, 1,
531 "(CGrafPtr port, GDHandle gdh) -> None"},
532 {"CTabChanged", (PyCFunction)Qdoffs_CTabChanged, 1,
533 "(CTabHandle ctab) -> None"},
534 {"PixPatChanged", (PyCFunction)Qdoffs_PixPatChanged, 1,
535 "(PixPatHandle ppat) -> None"},
536 {"PortChanged", (PyCFunction)Qdoffs_PortChanged, 1,
537 "(GrafPtr port) -> None"},
538 {"GDeviceChanged", (PyCFunction)Qdoffs_GDeviceChanged, 1,
539 "(GDHandle gdh) -> None"},
540 {"AllowPurgePixels", (PyCFunction)Qdoffs_AllowPurgePixels, 1,
541 "(PixMapHandle pm) -> None"},
542 {"NoPurgePixels", (PyCFunction)Qdoffs_NoPurgePixels, 1,
543 "(PixMapHandle pm) -> None"},
544 {"GetPixelsState", (PyCFunction)Qdoffs_GetPixelsState, 1,
545 "(PixMapHandle pm) -> (GWorldFlags _rv)"},
546 {"SetPixelsState", (PyCFunction)Qdoffs_SetPixelsState, 1,
547 "(PixMapHandle pm, GWorldFlags state) -> None"},
548 {"GetPixRowBytes", (PyCFunction)Qdoffs_GetPixRowBytes, 1,
549 "(PixMapHandle pm) -> (long _rv)"},
550 {"NewScreenBuffer", (PyCFunction)Qdoffs_NewScreenBuffer, 1,
551 "(Rect globalRect, Boolean purgeable) -> (GDHandle gdh, PixMapHandle offscreenPixMap)"},
552 {"DisposeScreenBuffer", (PyCFunction)Qdoffs_DisposeScreenBuffer, 1,
553 "(PixMapHandle offscreenPixMap) -> None"},
554 {"QDDone", (PyCFunction)Qdoffs_QDDone, 1,
555 "(GrafPtr port) -> (Boolean _rv)"},
556 {"OffscreenVersion", (PyCFunction)Qdoffs_OffscreenVersion, 1,
557 "() -> (long _rv)"},
558 {"NewTempScreenBuffer", (PyCFunction)Qdoffs_NewTempScreenBuffer, 1,
559 "(Rect globalRect, Boolean purgeable) -> (GDHandle gdh, PixMapHandle offscreenPixMap)"},
560 {"PixMap32Bit", (PyCFunction)Qdoffs_PixMap32Bit, 1,
561 "(PixMapHandle pmHandle) -> (Boolean _rv)"},
562 {"GetPixMapBytes", (PyCFunction)Qdoffs_GetPixMapBytes, 1,
563 "(pixmap, int start, int size) -> string. Return bytes from the pixmap"},
564 {"PutPixMapBytes", (PyCFunction)Qdoffs_PutPixMapBytes, 1,
565 "(pixmap, int start, string data). Store bytes into the pixmap"},
566 {NULL, NULL, 0}
572 void initQdoffs(void)
574 PyObject *m;
575 PyObject *d;
579 PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New);
580 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert);
583 m = Py_InitModule("Qdoffs", Qdoffs_methods);
584 d = PyModule_GetDict(m);
585 Qdoffs_Error = PyMac_GetOSErrException();
586 if (Qdoffs_Error == NULL ||
587 PyDict_SetItemString(d, "Error", Qdoffs_Error) != 0)
588 return;
589 GWorld_Type.ob_type = &PyType_Type;
590 Py_INCREF(&GWorld_Type);
591 if (PyDict_SetItemString(d, "GWorldType", (PyObject *)&GWorld_Type) != 0)
592 Py_FatalError("can't initialize GWorldType");
595 /* ======================= End module Qdoffs ======================== */