Clarify portability and main program.
[python/dscho.git] / Modules / cstubs
blobbd2af589e04682d497619c9b19615e4bc028fa82
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5                         All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
33 Input used to generate the Python module "glmodule.c".
34 The stub generator is a Python script called "cgen.py".
36 Each definition must be contained on one line:
38 <returntype> <name> <type> <arg> <type> <arg>
40 <returntype> can be: void, short, long (XXX maybe others?)
42 <type> can be: char, string, short, float, long, or double
43         string indicates a null terminated string;
44         if <type> is char and <arg> begins with a *, the * is stripped
45         and <type> is changed into string
47 <arg> has the form <mode> or <mode>[<subscript>]
48         where <mode> can be
49                 s: arg is sent
50                 r: arg is received              (arg is a pointer)
51         and <subscript> can be (N and I are numbers):
52                 N
53                 argI
54                 retval
55                 N*argI
56                 N*I
57                 N*retval
58         In the case where the subscript consists of two parts
59         separated by *, the first part is the width of the matrix, and
60         the second part is the length of the matrix.  This order is
61         opposite from the order used in C to declare a two-dimensional
62         matrix.
66  * An attempt has been made to make this module switch threads on qread
67  * calls. It is far from safe, though.
68  */
70 #include <gl.h>
71 #include <device.h>
73 #ifdef __sgi
74 extern int devport();
75 extern int textwritemask();
76 extern int pagewritemask();
77 extern int gewrite();
78 extern int gettp();
79 #endif
81 #include "Python.h"
82 #include "cgensupport.h"
85 Some stubs are too complicated for the stub generator.
86 We can include manually written versions of them here.
87 A line starting with '%' gives the name of the function so the stub
88 generator can include it in the table of functions.
91 % qread
93 static PyObject *
94 gl_qread(self, args)
95         PyObject *self;
96         PyObject *args;
98         long retval;
99         short arg1 ;
100         Py_BEGIN_ALLOW_THREADS
101         retval = qread( & arg1 );
102         Py_END_ALLOW_THREADS
103         { PyObject *v = PyTuple_New( 2 );
104           if (v == NULL) return NULL;
105           PyTuple_SetItem(v, 0, mknewlongobject(retval));
106           PyTuple_SetItem(v, 1, mknewshortobject(arg1));
107           return v;
108         }
113 varray -- an array of v.. calls.
114 The argument is an array (maybe list or tuple) of points.
115 Each point must be a tuple or list of coordinates (x, y, z).
116 The points may be 2- or 3-dimensional but must all have the
117 same dimension.  Float and int values may be mixed however.
118 The points are always converted to 3D double precision points
119 by assuming z=0.0 if necessary (as indicated in the man page),
120 and for each point v3d() is called.
123 % varray
125 static PyObject *
126 gl_varray(self, args)
127         PyObject *self;
128         PyObject *args;
130         PyObject *v, *w=NULL;
131         int i, n, width;
132         double vec[3];
133         PyObject * (*getitem) Py_FPROTO((PyObject *, int));
134         
135         if (!PyArg_GetObject(args, 1, 0, &v))
136                 return NULL;
137         
138         if (PyList_Check(v)) {
139                 n = PyList_Size(v);
140                 getitem = PyList_GetItem;
141         }
142         else if (PyTuple_Check(v)) {
143                 n = PyTuple_Size(v);
144                 getitem = PyTuple_GetItem;
145         }
146         else {
147                 PyErr_BadArgument();
148                 return NULL;
149         }
150         
151         if (n == 0) {
152                 Py_INCREF(Py_None);
153                 return Py_None;
154         }
155         if (n > 0)
156                 w = (*getitem)(v, 0);
157         
158         width = 0;
159         if (w == NULL) {
160         }
161         else if (PyList_Check(w)) {
162                 width = PyList_Size(w);
163         }
164         else if (PyTuple_Check(w)) {
165                 width = PyTuple_Size(w);
166         }
167         
168         switch (width) {
169         case 2:
170                 vec[2] = 0.0;
171                 /* Fall through */
172         case 3:
173                 break;
174         default:
175                 PyErr_BadArgument();
176                 return NULL;
177         }
178         
179         for (i = 0; i < n; i++) {
180                 w = (*getitem)(v, i);
181                 if (!PyArg_GetDoubleArray(w, 1, 0, width, vec))
182                         return NULL;
183                 v3d(vec);
184         }
185         
186         Py_INCREF(Py_None);
187         return Py_None;
191 vnarray, nvarray -- an array of n3f and v3f calls.
192 The argument is an array (list or tuple) of pairs of points and normals.
193 Each pair is a tuple (NOT a list) of a point and a normal for that point.
194 Each point or normal must be a tuple (NOT a list) of coordinates (x, y, z).
195 Three coordinates must be given.  Float and int values may be mixed.
196 For each pair, n3f() is called for the normal, and then v3f() is called
197 for the vector.
199 vnarray and nvarray differ only in the order of the vector and normal in
200 the pair: vnarray expects (v, n) while nvarray expects (n, v).
203 static PyObject *gen_nvarray(); /* Forward */
205 % nvarray
207 static PyObject *
208 gl_nvarray(self, args)
209         PyObject *self;
210         PyObject *args;
212         return gen_nvarray(args, 0);
215 % vnarray
217 static PyObject *
218 gl_vnarray(self, args)
219         PyObject *self;
220         PyObject *args;
222         return gen_nvarray(args, 1);
225 /* Generic, internal version of {nv,nv}array: inorm indicates the
226    argument order, 0: normal first, 1: vector first. */
228 static PyObject *
229 gen_nvarray(args, inorm)
230         PyObject *args;
231         int inorm;
233         PyObject *v, *w, *wnorm, *wvec;
234         int i, n;
235         float norm[3], vec[3];
236         PyObject * (*getitem) Py_FPROTO((PyObject *, int));
237         
238         if (!PyArg_GetObject(args, 1, 0, &v))
239                 return NULL;
240         
241         if (PyList_Check(v)) {
242                 n = PyList_Size(v);
243                 getitem = PyList_GetItem;
244         }
245         else if (PyTuple_Check(v)) {
246                 n = PyTuple_Size(v);
247                 getitem = PyTuple_GetItem;
248         }
249         else {
250                 PyErr_BadArgument();
251                 return NULL;
252         }
253         
254         for (i = 0; i < n; i++) {
255                 w = (*getitem)(v, i);
256                 if (!PyTuple_Check(w) || PyTuple_Size(w) != 2) {
257                         PyErr_BadArgument();
258                         return NULL;
259                 }
260                 wnorm = PyTuple_GetItem(w, inorm);
261                 wvec = PyTuple_GetItem(w, 1 - inorm);
262                 if (!PyArg_GetFloatArray(wnorm, 1, 0, 3, norm) ||
263                         !PyArg_GetFloatArray(wvec, 1, 0, 3, vec))
264                         return NULL;
265                 n3f(norm);
266                 v3f(vec);
267         }
268         
269         Py_INCREF(Py_None);
270         return Py_None;
273 /* nurbssurface(s_knots[], t_knots[], ctl[][], s_order, t_order, type).
274    The dimensions of ctl[] are computed as follows:
275    [len(s_knots) - s_order], [len(t_knots) - t_order]
278 % nurbssurface
280 static PyObject *
281 gl_nurbssurface(self, args)
282         PyObject *self;
283         PyObject *args;
285         long arg1 ;
286         double * arg2 ;
287         long arg3 ;
288         double * arg4 ;
289         double *arg5 ;
290         long arg6 ;
291         long arg7 ;
292         long arg8 ;
293         long ncoords;
294         long s_byte_stride, t_byte_stride;
295         long s_nctl, t_nctl;
296         long s, t;
297         PyObject *v, *w, *pt;
298         double *pnext;
299         if (!PyArg_GetLongArraySize(args, 6, 0, &arg1))
300                 return NULL;
301         if ((arg2 = PyMem_NEW(double, arg1 )) == NULL) {
302                 return PyErr_NoMemory();
303         }
304         if (!PyArg_GetDoubleArray(args, 6, 0, arg1 , arg2))
305                 return NULL;
306         if (!PyArg_GetLongArraySize(args, 6, 1, &arg3))
307                 return NULL;
308         if ((arg4 = PyMem_NEW(double, arg3 )) == NULL) {
309                 return PyErr_NoMemory();
310         }
311         if (!PyArg_GetDoubleArray(args, 6, 1, arg3 , arg4))
312                 return NULL;
313         if (!PyArg_GetLong(args, 6, 3, &arg6))
314                 return NULL;
315         if (!PyArg_GetLong(args, 6, 4, &arg7))
316                 return NULL;
317         if (!PyArg_GetLong(args, 6, 5, &arg8))
318                 return NULL;
319         if (arg8 == N_XYZ)
320                 ncoords = 3;
321         else if (arg8 == N_XYZW)
322                 ncoords = 4;
323         else {
324                 PyErr_BadArgument();
325                 return NULL;
326         }
327         s_nctl = arg1 - arg6;
328         t_nctl = arg3 - arg7;
329         if (!PyArg_GetObject(args, 6, 2, &v))
330                 return NULL;
331         if (!PyList_Check(v) || PyList_Size(v) != s_nctl) {
332                 PyErr_BadArgument();
333                 return NULL;
334         }
335         if ((arg5 = PyMem_NEW(double, s_nctl*t_nctl*ncoords )) == NULL) {
336                 return PyErr_NoMemory();
337         }
338         pnext = arg5;
339         for (s = 0; s < s_nctl; s++) {
340                 w = PyList_GetItem(v, s);
341                 if (w == NULL || !PyList_Check(w) ||
342                                         PyList_Size(w) != t_nctl) {
343                         PyErr_BadArgument();
344                         return NULL;
345                 }
346                 for (t = 0; t < t_nctl; t++) {
347                         pt = PyList_GetItem(w, t);
348                         if (!PyArg_GetDoubleArray(pt, 1, 0, ncoords, pnext))
349                                 return NULL;
350                         pnext += ncoords;
351                 }
352         }
353         s_byte_stride = sizeof(double) * ncoords;
354         t_byte_stride = s_byte_stride * s_nctl;
355         nurbssurface( arg1 , arg2 , arg3 , arg4 ,
356                 s_byte_stride , t_byte_stride , arg5 , arg6 , arg7 , arg8 );
357         PyMem_DEL(arg2);
358         PyMem_DEL(arg4);
359         PyMem_DEL(arg5);
360         Py_INCREF(Py_None);
361         return Py_None;
364 /* nurbscurve(knots, ctlpoints, order, type).
365    The length of ctlpoints is len(knots)-order. */
367 %nurbscurve
369 static PyObject *
370 gl_nurbscurve(self, args)
371         PyObject *self;
372         PyObject *args;
374         long arg1 ;
375         double * arg2 ;
376         long arg3 ;
377         double * arg4 ;
378         long arg5 ;
379         long arg6 ;
380         int ncoords, npoints;
381         int i;
382         PyObject *v;
383         double *pnext;
384         if (!PyArg_GetLongArraySize(args, 4, 0, &arg1))
385                 return NULL;
386         if ((arg2 = PyMem_NEW(double, arg1 )) == NULL) {
387                 return PyErr_NoMemory();
388         }
389         if (!PyArg_GetDoubleArray(args, 4, 0, arg1 , arg2))
390                 return NULL;
391         if (!PyArg_GetLong(args, 4, 2, &arg5))
392                 return NULL;
393         if (!PyArg_GetLong(args, 4, 3, &arg6))
394                 return NULL;
395         if (arg6 == N_ST)
396                 ncoords = 2;
397         else if (arg6 == N_STW)
398                 ncoords = 3;
399         else {
400                 PyErr_BadArgument();
401                 return NULL;
402         }
403         npoints = arg1 - arg5;
404         if (!PyArg_GetObject(args, 4, 1, &v))
405                 return NULL;
406         if (!PyList_Check(v) || PyList_Size(v) != npoints) {
407                 PyErr_BadArgument();
408                 return NULL;
409         }
410         if ((arg4 = PyMem_NEW(double, npoints*ncoords )) == NULL) {
411                 return PyErr_NoMemory();
412         }
413         pnext = arg4;
414         for (i = 0; i < npoints; i++) {
415                 if (!PyArg_GetDoubleArray(PyList_GetItem(v, i), 1, 0, ncoords, pnext))
416                         return NULL;
417                 pnext += ncoords;
418         }
419         arg3 = (sizeof(double)) * ncoords;
420         nurbscurve( arg1 , arg2 , arg3 , arg4 , arg5 , arg6 );
421         PyMem_DEL(arg2);
422         PyMem_DEL(arg4);
423         Py_INCREF(Py_None);
424         return Py_None;
427 /* pwlcurve(points, type).
428    Points is a list of points. Type must be N_ST. */
430 %pwlcurve
432 static PyObject *
433 gl_pwlcurve(self, args)
434         PyObject *self;
435         PyObject *args;
437         PyObject *v;
438         long type;
439         double *data, *pnext;
440         long npoints, ncoords;
441         int i;
442         if (!PyArg_GetObject(args, 2, 0, &v))
443                 return NULL;
444         if (!PyArg_GetLong(args, 2, 1, &type))
445                 return NULL;
446         if (!PyList_Check(v)) {
447                 PyErr_BadArgument();
448                 return NULL;
449         }
450         npoints = PyList_Size(v);
451         if (type == N_ST)
452                 ncoords = 2;
453         else {
454                 PyErr_BadArgument();
455                 return NULL;
456         }
457         if ((data = PyMem_NEW(double, npoints*ncoords)) == NULL) {
458                 return PyErr_NoMemory();
459         }
460         pnext = data;
461         for (i = 0; i < npoints; i++) {
462                 if (!PyArg_GetDoubleArray(PyList_GetItem(v, i), 1, 0, ncoords, pnext))
463                         return NULL;
464                 pnext += ncoords;
465         }
466         pwlcurve(npoints, data, sizeof(double)*ncoords, type);
467         PyMem_DEL(data);
468         Py_INCREF(Py_None);
469         return Py_None;
473 /* Picking and Selecting */
475 static short *pickbuffer = NULL;
476 static long pickbuffersize;
478 static PyObject *
479 pick_select(args, func)
480         PyObject *args;
481         void (*func)();
483         if (!PyArg_GetLong(args, 1, 0, &pickbuffersize))
484                 return NULL;
485         if (pickbuffer != NULL) {
486                 PyErr_SetString(PyExc_RuntimeError,
487                         "pick/gselect: already picking/selecting");
488                 return NULL;
489         }
490         if ((pickbuffer = PyMem_NEW(short, pickbuffersize)) == NULL) {
491                 return PyErr_NoMemory();
492         }
493         (*func)(pickbuffer, pickbuffersize);
494         Py_INCREF(Py_None);
495         return Py_None;
498 static PyObject *
499 endpick_select(args, func)
500         PyObject *args;
501         long (*func)();
503         PyObject *v, *w;
504         int i, nhits, n;
505         if (!PyArg_NoArgs(args))
506                 return NULL;
507         if (pickbuffer == NULL) {
508                 PyErr_SetString(PyExc_RuntimeError,
509                         "endpick/endselect: not in pick/select mode");
510                 return NULL;
511         }
512         nhits = (*func)(pickbuffer);
513         if (nhits < 0) {
514                 nhits = -nhits; /* How to report buffer overflow otherwise? */
515         }
516         /* Scan the buffer to see how many integers */
517         n = 0;
518         for (; nhits > 0; nhits--) {
519                 n += 1 + pickbuffer[n];
520         }
521         v = PyList_New(n);
522         if (v == NULL)
523                 return NULL;
524         /* XXX Could do it nicer and interpret the data structure here,
525            returning a list of lists. But this can be done in Python... */
526         for (i = 0; i < n; i++) {
527                 w = PyInt_FromLong((long)pickbuffer[i]);
528                 if (w == NULL) {
529                         Py_DECREF(v);
530                         return NULL;
531                 }
532                 PyList_SetItem(v, i, w);
533         }
534         PyMem_DEL(pickbuffer);
535         pickbuffer = NULL;
536         return v;
539 extern void pick(), gselect();
540 extern long endpick(), endselect();
542 %pick
543 static PyObject *gl_pick(self, args) PyObject *self, *args; {
544         return pick_select(args, pick);
547 %endpick
548 static PyObject *gl_endpick(self, args) PyObject *self, *args; {
549         return endpick_select(args, endpick);
552 %gselect
553 static PyObject *gl_gselect(self, args) PyObject *self, *args; {
554         return pick_select(args, gselect);
557 %endselect
558 static PyObject *gl_endselect(self, args) PyObject *self, *args; {
559         return endpick_select(args, endselect);
563 /* XXX The generator botches this one.  Here's a quick hack to fix it. */
565 /* XXX The generator botches this one.  Here's a quick hack to fix it. */
567 % getmatrix float r[16]
569 static PyObject *
570 gl_getmatrix(self, args)
571         PyObject *self;
572         PyObject *args;
574         Matrix arg1;
575         PyObject *v, *w;
576         int i, j;
577         getmatrix( arg1 );
578         v = PyList_New(16);
579         if (v == NULL) {
580                 return PyErr_NoMemory();
581         }
582         for (i = 0; i < 4; i++) for (j = 0; j < 4; j++) {
583                 w = mknewfloatobject(arg1[i][j]);
584                 if (w == NULL) {
585                         Py_DECREF(v);
586                         return NULL;
587                 }
588                 PyList_SetItem(v, i*4+j, w);
589         }
590         return v;
593 /* Here's an alternate version that returns a 4x4 matrix instead of
594    a vector.  Unfortunately it is incompatible with loadmatrix and
595    multmatrix... */
597 % altgetmatrix float r[4][4]
599 static PyObject *
600 gl_altgetmatrix(self, args)
601         PyObject *self;
602         PyObject *args;
604         Matrix arg1;
605         PyObject *v, *w;
606         int i, j;
607         getmatrix( arg1 );
608         v = PyList_New(4);
609         if (v == NULL) {
610                 return NULL;
611         }
612         for (i = 0; i < 4; i++) {
613                 w = PyList_New(4);
614                 if (w == NULL) {
615                         Py_DECREF(v);
616                         return NULL;
617                 }
618                 PyList_SetItem(v, i, w);
619         }
620         for (i = 0; i < 4; i++) {
621                 for (j = 0; j < 4; j++) {
622                         w = mknewfloatobject(arg1[i][j]);
623                         if (w == NULL) {
624                                 Py_DECREF(v);
625                                 return NULL;
626                         }
627                         PyList_SetItem(PyList_GetItem(v, i), j, w);
628                 }
629         }
630         return v;
633 % lrectwrite
635 static PyObject *
636 gl_lrectwrite(self, args)
637         PyObject *self;
638         PyObject *args;
640         short x1 ;
641         short y1 ;
642         short x2 ;
643         short y2 ;
644         string parray ;
645         PyObject *s;
646 #if 0
647         int pixcount;
648 #endif
649         if (!PyArg_GetShort(args, 5, 0, &x1))
650                 return NULL;
651         if (!PyArg_GetShort(args, 5, 1, &y1))
652                 return NULL;
653         if (!PyArg_GetShort(args, 5, 2, &x2))
654                 return NULL;
655         if (!PyArg_GetShort(args, 5, 3, &y2))
656                 return NULL;
657         if (!PyArg_GetString(args, 5, 4, &parray))
658                 return NULL;
659         if (!PyArg_GetObject(args, 5, 4, &s))
660                 return NULL;
661 #if 0
662 /* Don't check this, it breaks experiments with pixmode(PM_SIZE, ...) */
663         pixcount = (long)(x2+1-x1) * (long)(y2+1-y1);
664         if (!PyString_Check(s) || PyString_Size(s) != pixcount*sizeof(long)) {
665                 PyErr_SetString(PyExc_RuntimeError,
666                            "string arg to lrectwrite has wrong size");
667                 return NULL;
668         }
669 #endif
670         lrectwrite( x1 , y1 , x2 , y2 , (unsigned long *) parray );
671         Py_INCREF(Py_None);
672         return Py_None;
675 % lrectread
677 static PyObject *
678 gl_lrectread(self, args)
679         PyObject *self;
680         PyObject *args;
682         short x1 ;
683         short y1 ;
684         short x2 ;
685         short y2 ;
686         PyObject *parray;
687         int pixcount;
688         if (!PyArg_GetShort(args, 4, 0, &x1))
689                 return NULL;
690         if (!PyArg_GetShort(args, 4, 1, &y1))
691                 return NULL;
692         if (!PyArg_GetShort(args, 4, 2, &x2))
693                 return NULL;
694         if (!PyArg_GetShort(args, 4, 3, &y2))
695                 return NULL;
696         pixcount = (long)(x2+1-x1) * (long)(y2+1-y1);
697         parray = PyString_FromStringAndSize((char *)NULL, pixcount*sizeof(long));
698         if (parray == NULL)
699                 return NULL; /* No memory */
700         lrectread(x1, y1, x2, y2, (unsigned long *) PyString_AsString(parray));
701         return parray;
704 % readdisplay
706 static PyObject *
707 gl_readdisplay(self, args)
708         PyObject *self;
709         PyObject *args;
711         short x1, y1, x2, y2;
712         unsigned long *parray, hints;
713         long size, size_ret;
714         PyObject *rv;
716         if ( !PyArg_Parse(args, "hhhhl", &x1, &y1, &x2, &y2, &hints) )
717           return 0;
718         size = (long)(x2+1-x1) * (long)(y2+1-y1);
719         rv = PyString_FromStringAndSize((char *)NULL, size*sizeof(long));
720         if ( rv == NULL )
721           return NULL;
722         parray = (unsigned long *)PyString_AsString(rv);
723         size_ret = readdisplay(x1, y1, x2, y2, parray, hints);
724         if ( size_ret != size ) {
725             printf("gl_readdisplay: got %ld pixels, expected %ld\n",
726                    size_ret, size);
727             PyErr_SetString(PyExc_RuntimeError, "readdisplay returned unexpected length");
728             return NULL;
729         }
730         return rv;
733 /* Desperately needed, here are tools to compress and decompress
734    the data manipulated by lrectread/lrectwrite.
736    gl.packrect(width, height, packfactor, bigdata) --> smalldata
737                 makes 'bigdata' 4*(packfactor**2) times smaller by:
738                 - turning it into B/W (a factor 4)
739                 - replacing squares of size pacfactor by one
740                   representative
742    gl.unpackrect(width, height, packfactor, smalldata) --> bigdata
743                 is the inverse; the numeric arguments must be *the same*.
745    Both work best if width and height are multiples of packfactor
746    (in fact unpackrect will leave garbage bytes).
749 % packrect
751 static PyObject *
752 gl_packrect(self, args)
753         PyObject *self;
754         PyObject *args;
756         long width, height, packfactor;
757         char *s;
758         PyObject *unpacked, *packed;
759         int pixcount, packedcount, x, y, r, g, b;
760         unsigned long pixel;
761         unsigned char *p;
762         unsigned long *parray;
763         if (!PyArg_GetLong(args, 4, 0, &width))
764                 return NULL;
765         if (!PyArg_GetLong(args, 4, 1, &height))
766                 return NULL;
767         if (!PyArg_GetLong(args, 4, 2, &packfactor))
768                 return NULL;
769         if (!PyArg_GetString(args, 4, 3, &s)) /* For type checking only */
770                 return NULL;
771         if (!PyArg_GetObject(args, 4, 3, &unpacked))
772                 return NULL;
773         if (width <= 0 || height <= 0 || packfactor <= 0) {
774                 PyErr_SetString(PyExc_RuntimeError, "packrect args must be > 0");
775                 return NULL;
776         }
777         pixcount = width*height;
778         packedcount = ((width+packfactor-1)/packfactor) *
779                 ((height+packfactor-1)/packfactor);
780         if (PyString_Size(unpacked) != pixcount*sizeof(long)) {
781                 PyErr_SetString(PyExc_RuntimeError,
782                            "string arg to packrect has wrong size");
783                 return NULL;
784         }
785         packed = PyString_FromStringAndSize((char *)NULL, packedcount);
786         if (packed == NULL)
787                 return NULL;
788         parray = (unsigned long *) PyString_AsString(unpacked);
789         p = (unsigned char *) PyString_AsString(packed);
790         for (y = 0; y < height; y += packfactor, parray += packfactor*width) {
791                 for (x = 0; x < width; x += packfactor) {
792                         pixel = parray[x];
793                         r = pixel & 0xff;
794                         g = (pixel >> 8) & 0xff;
795                         b = (pixel >> 16) & 0xff;
796                         *p++ = (30*r+59*g+11*b) / 100;
797                 }
798         }
799         return packed;
802 % unpackrect
804 static unsigned long unpacktab[256];
805 static int unpacktab_inited = 0;
807 static PyObject *
808 gl_unpackrect(self, args)
809         PyObject *self;
810         PyObject *args;
812         long width, height, packfactor;
813         char *s;
814         PyObject *unpacked, *packed;
815         int pixcount, packedcount;
816         register unsigned char *p;
817         register unsigned long *parray;
818         if (!unpacktab_inited) {
819                 register int white;
820                 for (white = 256; --white >= 0; )
821                         unpacktab[white] = white * 0x010101L;
822                 unpacktab_inited++;
823         }
824         if (!PyArg_GetLong(args, 4, 0, &width))
825                 return NULL;
826         if (!PyArg_GetLong(args, 4, 1, &height))
827                 return NULL;
828         if (!PyArg_GetLong(args, 4, 2, &packfactor))
829                 return NULL;
830         if (!PyArg_GetString(args, 4, 3, &s)) /* For type checking only */
831                 return NULL;
832         if (!PyArg_GetObject(args, 4, 3, &packed))
833                 return NULL;
834         if (width <= 0 || height <= 0 || packfactor <= 0) {
835                 PyErr_SetString(PyExc_RuntimeError, "packrect args must be > 0");
836                 return NULL;
837         }
838         pixcount = width*height;
839         packedcount = ((width+packfactor-1)/packfactor) *
840                 ((height+packfactor-1)/packfactor);
841         if (PyString_Size(packed) != packedcount) {
842                 PyErr_SetString(PyExc_RuntimeError,
843                            "string arg to unpackrect has wrong size");
844                 return NULL;
845         }
846         unpacked = PyString_FromStringAndSize((char *)NULL, pixcount*sizeof(long));
847         if (unpacked == NULL)
848                 return NULL;
849         parray = (unsigned long *) PyString_AsString(unpacked);
850         p = (unsigned char *) PyString_AsString(packed);
851         if (packfactor == 1 && width*height > 0) {
852                 /* Just expand bytes to longs */
853                 register int x = width * height;
854                 do {
855                         *parray++ = unpacktab[*p++];
856                 } while (--x >= 0);
857         }
858         else {
859                 register int y;
860                 for (y = 0; y < height-packfactor+1;
861                      y += packfactor, parray += packfactor*width) {
862                         register int x;
863                         for (x = 0; x < width-packfactor+1; x += packfactor) {
864                                 register unsigned long pixel = unpacktab[*p++];
865                                 register int i;
866                                 for (i = packfactor*width; (i-=width) >= 0;) {
867                                         register int j;
868                                         for (j = packfactor; --j >= 0; )
869                                                 parray[i+x+j] = pixel;
870                                 }
871                         }
872                 }
873         }
874         return unpacked;
877 % gversion
878 static PyObject *
879 gl_gversion(self, args)
880         PyObject *self;
881         PyObject *args;
883         char buf[20];
884         gversion(buf);
885         return PyString_FromString(buf);
889 /* void clear - Manual because of clash with termcap */
890 %clear
891 static PyObject *
892 gl_clear(self, args)
893         PyObject *self;
894         PyObject *args;
896         __GLclear( );
897         Py_INCREF(Py_None);
898         return Py_None;
901 /* End of manually written stubs */
905 long    getshade
906 if !solaris     void    devport         short s long s
907 void    rdr2i           long s long s
908 void    rectfs          short s short s short s short s
909 void    rects           short s short s short s short s
910 void    rmv2i           long s long s
911 void    noport
912 void    popviewport
913 void    clearhitcode
914 void    closeobj
915 void    cursoff
916 void    curson
917 void    doublebuffer
918 void    finish
919 void    gconfig
920 void    ginit
921 void    greset
922 void    multimap
923 void    onemap
924 void    popattributes
925 void    popmatrix
926 void    pushattributes
927 void    pushmatrix
928 void    pushviewport
929 void    qreset
930 void    RGBmode
931 void    singlebuffer
932 void    swapbuffers
933 void    gsync
934 void    gflush
935 void    tpon
936 void    tpoff
937 void    clkon
938 void    clkoff
939 void    ringbell
940 #void   callfunc
941 void    gbegin
942 void    textinit
943 void    initnames
944 void    pclos
945 void    popname
946 if !solaris     void    spclos
947 void    zclear
948 void    screenspace
949 void    reshapeviewport
950 void    winpush
951 void    winpop
952 void    foreground
953 void    endfullscrn
954 if !solaris     void    endpupmode
955 void    fullscrn
956 if !solaris     void    pupmode
957 void    winconstraints
958 void    pagecolor       short s
959 void    textcolor       short s
960 void    color           short s
961 void    curveit         short s
962 void    font            short s
963 void    linewidth       short s
964 void    setlinestyle    short s
965 void    setmap          short s
966 void    swapinterval    short s
967 void    writemask       short s
968 if !solaris     void    textwritemask   short s
969 void    qdevice         short s
970 void    unqdevice       short s
971 void    curvebasis      short s
972 void    curveprecision  short s
973 void    loadname        short s
974 void    passthrough     short s
975 void    pushname        short s
976 void    setmonitor      short s
977 if !solaris     void    setshade        short s
978 void    setpattern      short s
979 if !solaris     void    pagewritemask   short s
981 void    callobj         long s
982 void    delobj          long s
983 void    editobj         long s
984 void    makeobj         long s
985 void    maketag         long s
986 void    chunksize       long s
987 void    compactify      long s
988 void    deltag          long s
989 void    lsrepeat        long s
990 void    objinsert       long s
991 void    objreplace      long s
992 void    winclose        long s
993 void    blanktime       long s
994 void    freepup         long s
995 # This is not in the library!?
996 ###void pupcolor        long s
998 void    backbuffer      long s
999 void    frontbuffer     long s
1000 if !solaris     void    lsbackup        long s
1001 void    resetls         long s
1002 void    lampon          long s
1003 void    lampoff         long s
1004 void    setbell         long s
1005 void    blankscreen     long s
1006 void    depthcue        long s
1007 void    zbuffer         long s
1008 void    backface        long s
1010 void    cmov2i          long s long s
1011 void    draw2i          long s long s
1012 void    move2i          long s long s
1013 void    pnt2i           long s long s
1014 void    patchbasis      long s long s
1015 void    patchprecision  long s long s
1016 void    pdr2i           long s long s
1017 void    pmv2i           long s long s
1018 void    rpdr2i          long s long s
1019 void    rpmv2i          long s long s
1020 void    xfpt2i          long s long s
1021 void    objdelete       long s long s
1022 void    patchcurves     long s long s
1023 void    minsize         long s long s
1024 void    maxsize         long s long s
1025 void    keepaspect      long s long s
1026 void    prefsize        long s long s
1027 void    stepunit        long s long s
1028 void    fudge           long s long s
1029 void    winmove         long s long s
1031 void    attachcursor    short s short s
1032 void    deflinestyle    short s short s
1033 void    noise           short s short s
1034 void    picksize        short s short s
1035 void    qenter          short s short s
1036 void    setdepth        short s short s
1037 void    cmov2s          short s short s
1038 void    draw2s          short s short s
1039 void    move2s          short s short s
1040 void    pdr2s           short s short s
1041 void    pmv2s           short s short s
1042 void    pnt2s           short s short s
1043 void    rdr2s           short s short s
1044 void    rmv2s           short s short s
1045 void    rpdr2s          short s short s
1046 void    rpmv2s          short s short s
1047 void    xfpt2s          short s short s
1049 void cmov2              float s float s
1050 void draw2              float s float s
1051 void move2              float s float s
1052 void pnt2               float s float s
1053 void pdr2               float s float s
1054 void pmv2               float s float s
1055 void rdr2               float s float s
1056 void rmv2               float s float s
1057 void rpdr2              float s float s
1058 void rpmv2              float s float s
1059 void xfpt2              float s float s
1061 void loadmatrix         float s[4*4]
1062 # Really [4][4]
1063 void multmatrix         float s[4*4]
1064 # Really [4][4]
1065 void crv                        float s[3*4]
1066 # Really [4][3]
1067 void rcrv                       float s[4*4]
1068 # Really [4][4]
1070 # Methods that have strings.  
1072 void addtopup           long s char *s long s
1073 void charstr            char *s
1074 void getport            char *s
1075 long strwidth           char *s
1076 long winopen            char *s
1077 void wintitle           char *s
1079 # Methods that have 1 long (# of elements) and an array 
1081 void polf               long s float s[3*arg1]
1082 void polf2              long s float s[2*arg1]
1083 void poly               long s float s[3*arg1]
1084 void poly2              long s float s[2*arg1]
1085 void crvn               long s float s[3*arg1]
1086 void rcrvn              long s float s[4*arg1]
1088 void polf2i             long s long s[2*arg1]
1089 void polfi              long s long s[3*arg1]
1090 void poly2i             long s long s[2*arg1]
1091 void polyi              long s long s[3*arg1]
1093 void polf2s             long s short s[2*arg1]
1094 void polfs              long s short s[3*arg1]
1095 void polys              long s short s[3*arg1]
1096 void poly2s             long s short s[2*arg1]
1098 void defcursor          short s u_short s[128]
1099 # Is this useful?
1100 void writepixels        short s u_short s[arg1]
1101 # Should be unsigned short...
1102 void defbasis           long s float s[4*4]
1103 if !solaris     void gewrite            short s short s[arg1]
1105 void rotate             short s char s
1106 # This is not in the library!?
1107 ###void setbutton               short s char s
1108 void rot                float s char s
1110 void circfi             long s long s long s
1111 void circi              long s long s long s
1112 void cmovi              long s long s long s
1113 void drawi              long s long s long s
1114 void movei              long s long s long s
1115 void pnti               long s long s long s
1116 void newtag             long s long s long s
1117 void pdri               long s long s long s
1118 void pmvi               long s long s long s
1119 void rdri               long s long s long s
1120 void rmvi               long s long s long s
1121 void rpdri              long s long s long s
1122 void rpmvi              long s long s long s
1123 void xfpti              long s long s long s
1125 void circ               float s float s float s
1126 void circf              float s float s float s
1127 void cmov               float s float s float s
1128 void draw               float s float s float s
1129 void move               float s float s float s
1130 void pnt                float s float s float s
1131 void scale              float s float s float s
1132 void translate          float s float s float s
1133 void pdr                float s float s float s
1134 void pmv                float s float s float s
1135 void rdr                float s float s float s
1136 void rmv                float s float s float s
1137 void rpdr               float s float s float s
1138 void rpmv               float s float s float s
1139 void xfpt               float s float s float s
1141 void RGBcolor           short s short s short s
1142 void RGBwritemask       short s short s short s
1143 void setcursor          short s short s short s
1144 void tie                short s short s short s
1145 void circfs             short s short s short s
1146 void circs              short s short s short s
1147 void cmovs              short s short s short s
1148 void draws              short s short s short s
1149 void moves              short s short s short s
1150 void pdrs               short s short s short s
1151 void pmvs               short s short s short s
1152 void pnts               short s short s short s
1153 void rdrs               short s short s short s
1154 void rmvs               short s short s short s
1155 void rpdrs              short s short s short s
1156 void rpmvs              short s short s short s
1157 void xfpts              short s short s short s
1158 void curorigin          short s short s short s
1159 void cyclemap           short s short s short s
1161 void patch              float s[4*4] float s[4*4] float s[4*4]
1162 void splf               long s float s[3*arg1] u_short s[arg1]
1163 void splf2              long s float s[2*arg1] u_short s[arg1]
1164 void splfi              long s long s[3*arg1] u_short s[arg1]
1165 void splf2i             long s long s[2*arg1] u_short s[arg1]
1166 void splfs              long s short s[3*arg1] u_short s[arg1]
1167 void splf2s             long s short s[2*arg1] u_short s[arg1]
1168 ###void defpattern              short s short s u_short s[arg2*arg2/16]
1170 void rpatch             float s[4*4] float s[4*4] float s[4*4] float s[4*4]
1172 # routines that send 4 floats
1174 void ortho2             float s float s float s float s
1175 void rect               float s float s float s float s
1176 void rectf              float s float s float s float s
1177 void xfpt4              float s float s float s float s
1179 void textport           short s short s short s short s
1180 void mapcolor           short s short s short s short s
1181 void scrmask            short s short s short s short s
1182 void setvaluator        short s short s short s short s
1183 void viewport           short s short s short s short s
1184 void shaderange         short s short s short s short s
1185 void xfpt4s             short s short s short s short s
1186 void rectfi             long s long s long s long s
1187 void recti              long s long s long s long s
1188 void xfpt4i             long s long s long s long s
1189 void prefposition       long s long s long s long s
1191 void arc                float s float s float s short s short s
1192 void arcf               float s float s float s short s short s
1193 void arcfi              long s long s long s short s short s
1194 void arci               long s long s long s short s short s
1196 void bbox2              short s short s float s float s float s float s
1197 void bbox2i             short s short s long s long s long s long s
1198 void bbox2s             short s short s short s short s short s short s
1199 void blink              short s short s short s short s short s
1200 void ortho              float s float s float s float s float s float s
1201 void window             float s float s float s float s float s float s
1202 void lookat             float s float s float s float s float s float s short s
1204 void perspective        short s float s float s float s
1205 void polarview          float s short s short s short s
1206 # XXX getichararray not supported
1207 #void writeRGB          short s char s[arg1] char s[arg1] char s[arg1]
1209 void arcfs              short s short s short s short s short s
1210 void arcs               short s short s short s short s short s
1211 void rectcopy           short s short s short s short s short s short s
1212 if !solaris     void RGBcursor          short s short s short s short s short s short s short s
1214 long getbutton          short s
1215 long getcmmode
1216 long getlsbackup
1217 long getresetls
1218 long getdcm
1219 long getzbuffer
1220 long ismex
1221 long isobj              long s
1222 long isqueued           short s
1223 long istag              long s
1225 long genobj
1226 long gentag
1227 long getbuffer
1228 long getcolor
1229 long getdisplaymode
1230 long getfont
1231 long getheight
1232 long gethitcode
1233 long getlstyle
1234 long getlwidth
1235 long getmap
1236 long getplanes
1237 long getwritemask
1238 long qtest
1239 long getlsrepeat
1240 long getmonitor
1241 long getopenobj
1242 long getpattern
1243 long winget
1244 long winattach
1245 long getothermonitor
1246 long newpup
1248 long getvaluator        short s
1249 void winset             long s
1250 long dopup              long s
1251 void getdepth           short r short r
1252 void getcpos            short r short r
1253 void getsize            long r long r
1254 void getorigin          long r long r
1255 void getviewport        short r short r short r short r
1256 if !solaris     void gettp              short r short r short r short r
1257 void getgpos            float r float r float r float r
1258 void winposition        long s long s long s long s
1259 void gRGBcolor          short r short r short r
1260 void gRGBmask           short r short r short r
1261 void getscrmask short r short r short r short r
1262 ###void gRGBcursor      short r short r short r short r short r short r short r short r
1263 void getmcolor          short s short r short r short r
1264 void mapw               long s short s short s float r float r float r float r float r float r
1265 void mapw2              long s short s short s float r float r
1266 ###void defrasterfont   short s short s short s Fontchar s[arg3] short s short s[4*arg5]
1267 ###long qread           short r
1268 void getcursor          short r u_short r u_short r long r
1270 #   For these we receive arrays of stuff
1272 ###void getdev          long s short s[arg1] short r[arg1]
1273 #XXX not generated correctly yet
1274 #void getmatrix         float r[16]
1275 ###long readpixels              short s short r[retval]
1276 ###long readRGB         short s char r[retval] char r[retval] char r[retval]
1277 ###long blkqread                short s short r[arg1]
1279 #   New 4D routines
1281 void cmode
1282 void concave            long s
1283 void curstype           long s
1284 void drawmode           long s
1285 void gammaramp          short s[256] short s[256] short s[256]
1286 long getbackface
1287 long getdescender
1288 long getdrawmode
1289 long getmmode
1290 long getsm
1291 long getvideo           long s
1292 void imakebackground
1293 void lmbind             short s short s
1294 void lmdef              long s long s long s float s[arg3]
1295 void mmode              long s
1296 void normal             float s[3]
1297 void overlay            long s
1298 void RGBrange           short s short s short s short s short s short s short s short s
1299 if !solaris     void setvideo           long s long s
1300 void shademodel         long s
1301 void underlay           long s
1303 # New Personal Iris/GT Routines
1305 void bgnclosedline
1306 void bgnline
1307 void bgnpoint
1308 void bgnpolygon
1309 void bgnsurface
1310 void bgntmesh
1311 void bgntrim
1312 void endclosedline
1313 void endline
1314 void endpoint
1315 void endpolygon
1316 void endsurface
1317 void endtmesh
1318 void endtrim
1319 void blendfunction      long s long s
1320 void c3f                float s[3]
1321 void c3i                long  s[3]
1322 void c3s                short s[3]
1323 void c4f                float s[4]
1324 void c4i                long  s[4]
1325 void c4s                short s[4]
1326 void colorf             float s
1327 void cpack              long s
1328 void czclear            long s long s
1329 void dglclose           long s
1330 long dglopen            char *s long s
1331 long getgdesc           long s
1332 void getnurbsproperty   long s float r
1333 void glcompat           long s long s
1334 void iconsize           long s long s
1335 void icontitle          char *s
1336 void lRGBrange          short s short s short s short s short s short s long s long s
1337 void linesmooth         long s
1338 void lmcolor            long s
1339 void logicop            long s
1340 ###long lrectread               short s short s short s short s long r[retval]
1341 ###void lrectwrite              short s short s short s short s long s[(arg2-arg1+1)*(arg4-arg3+1)]
1342 ### Now manual, with string last arg
1343 ###long rectread                short s short s short s short s short r[retval]
1344 ###void rectwrite               short s short s short s short s short s[(arg2-arg1+1)*(arg4-arg3+1)]
1345 void lsetdepth          long s long s
1346 void lshaderange        short s short s long s long s
1347 void n3f                float s[3]
1348 void noborder
1349 void pntsmooth          long s
1350 void readsource         long s
1351 void rectzoom           float s float s
1352 void sbox               float s float s float s float s
1353 void sboxi              long s long s long s long s
1354 void sboxs              short s short s short s short s
1355 void sboxf              float s float s float s float s
1356 void sboxfi             long s long s long s long s
1357 void sboxfs             short s short s short s short s
1358 void setnurbsproperty   long s float s
1359 void setpup             long s long s long s
1360 void smoothline         long s
1361 void subpixel           long s
1362 void swaptmesh
1363 long swinopen           long s
1364 void v2f                float s[2]
1365 void v2i                long  s[2]
1366 void v2s                short s[2]
1367 void v3f                float s[3]
1368 void v3i                long  s[3]
1369 void v3s                short s[3]
1370 void v4f                float s[4]
1371 void v4i                long  s[4]
1372 void v4s                short s[4]
1373 void videocmd           long s
1374 long windepth           long s
1375 void wmpack             long s
1376 void zdraw              long s
1377 void zfunction          long s
1378 void zsource            long s
1379 void zwritemask         long s
1381 #   uses doubles
1383 void v2d                double s[2]
1384 void v3d                double s[3]
1385 void v4d                double s[4]
1387 # Why isn't this here?
1389 void pixmode            long s long s
1391 # New in IRIX 4.0
1393 long qgetfd
1394 void dither             long s