blob: 2c580199f444ce907a10b98e42e933cf6bc5e680 [file] [log] [blame]
Jack Jansenee735be1994-12-14 13:31:11 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossum4410d571995-02-21 20:58:53 +00003The Netherlands.
Jack Jansenee735be1994-12-14 13:31:11 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Note: This file is partially converted to the new naming standard */
26/* ctbcm objects */
27
28
Jack Jansenf5c20571997-01-30 15:48:07 +000029#include "Python.h"
Jack Jansenee735be1994-12-14 13:31:11 +000030
Guido van Rossum4410d571995-02-21 20:58:53 +000031#include "macglue.h"
32
Jack Jansenee735be1994-12-14 13:31:11 +000033#include <CommResources.h>
34#include <Connections.h>
35#include <ToolUtils.h>
36#include <OSUtils.h>
Guido van Rossum4410d571995-02-21 20:58:53 +000037
38#ifndef HAVE_UNIVERSAL_HEADERS
Jack Jansenee735be1994-12-14 13:31:11 +000039#define ConnectionCompletionUPP ProcPtr
40#define ConnectionChooseIdleUPP ProcPtr
41#define NewConnectionCompletionProc(x) (x)
42#define NewConnectionChooseIdleProc(x) (x)
43#endif
44
45#define _UnimplementedToolTrap 0xA89F
46#define _CommToolboxTrap 0x8B
47#define _UnimplementedOSTrap 0x9F
48
Jack Jansenf5c20571997-01-30 15:48:07 +000049extern PyObject *PyErr_Mac(PyObject *,int);
Jack Jansenee735be1994-12-14 13:31:11 +000050
Jack Jansenf5c20571997-01-30 15:48:07 +000051static PyObject *ErrorObject;
Jack Jansenee735be1994-12-14 13:31:11 +000052
53typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000054 PyObject_HEAD
Jack Jansenee735be1994-12-14 13:31:11 +000055 ConnHandle hdl; /* The handle to the connection */
Jack Jansenf5c20571997-01-30 15:48:07 +000056 PyObject *callback; /* Python callback routine */
Jack Jansenee735be1994-12-14 13:31:11 +000057 int has_callback; /* True if callback not None */
58 int err; /* Error to pass to the callback */
59} ctbcmobject;
60
Jack Jansenf5c20571997-01-30 15:48:07 +000061staticforward PyTypeObject ctbcmtype;
Jack Jansenee735be1994-12-14 13:31:11 +000062
63#define is_ctbcmobject(v) ((v)->ob_type == &ctbcmtype)
64
65static
66TrapAvailable(short tNumber, TrapType tType)
67{
68 short unImplemented;
69
70 if (tType == OSTrap)
71 unImplemented = _UnimplementedOSTrap;
72 else
73 unImplemented = _UnimplementedToolTrap;
74
75 return NGetTrapAddress(tNumber, tType) != NGetTrapAddress(unImplemented, tType);
76}
77
78static
79initialize_ctb()
80{
81 OSErr err;
82 static initialized = -1;
83
84 if ( initialized >= 0 )
85 return initialized;
86 initialized = 0;
87
88 if ( !TrapAvailable(_CommToolboxTrap, OSTrap) ) {
Jack Jansenf5c20571997-01-30 15:48:07 +000089 PyErr_SetString(ErrorObject, "CTB not available");
Jack Jansenee735be1994-12-14 13:31:11 +000090 return 0;
91 }
92 if ( (err=InitCTBUtilities()) ) {
93 PyErr_Mac(ErrorObject, (int)err);
94 return 0;
95 }
96 if ( (err=InitCRM()) ) {
97 PyErr_Mac(ErrorObject, (int)err);
98 return 0;
99 }
100 if ( (err=InitCM()) ) {
101 PyErr_Mac(ErrorObject, (int)err);
102 return 0;
103 }
104 initialized = 1;
105 return 1;
106}
107
108static int
109ctbcm_pycallback(arg)
110 void *arg;
111{
112 ctbcmobject *self = (ctbcmobject *)arg;
Jack Jansenf5c20571997-01-30 15:48:07 +0000113 PyObject *args, *rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000114
115 if ( !self->has_callback ) /* It could have been removed in the meantime */
116 return 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000117 args = Py_BuildValue("(i)", self->err);
118 rv = PyEval_CallObject(self->callback, args);
119 Py_DECREF(args);
Jack Jansenee735be1994-12-14 13:31:11 +0000120 if( rv == NULL )
121 return -1;
Jack Jansenf5c20571997-01-30 15:48:07 +0000122 Py_DECREF(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000123 return 0;
124}
125
126/*DBG*/int ncallback;
127static pascal void
128ctbcm_ctbcallback(hconn)
129 ConnHandle hconn;
130{
131 ctbcmobject *self;
132
133 /* XXXX Do I have to do the A5 mumbo-jumbo? */
134 ncallback++; /*DBG*/
135 self = (ctbcmobject *)CMGetUserData(hconn);
136 self->err = (int)((*hconn)->errCode);
137 Py_AddPendingCall(ctbcm_pycallback, (void *)self);
138}
139
140static ctbcmobject *
141newctbcmobject(arg)
Jack Jansenf5c20571997-01-30 15:48:07 +0000142 PyObject *arg;
Jack Jansenee735be1994-12-14 13:31:11 +0000143{
144 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000145 self = PyObject_NEW(ctbcmobject, &ctbcmtype);
Jack Jansenee735be1994-12-14 13:31:11 +0000146 if (self == NULL)
147 return NULL;
148 self->hdl = NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000149 Py_INCREF(Py_None);
150 self->callback = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000151 self->has_callback = 0;
152 return self;
153}
154
155/* ctbcm methods */
156
157static void
158ctbcm_dealloc(self)
159 ctbcmobject *self;
160{
161 if ( self->hdl ) {
162 (void)CMClose(self->hdl, 0, (ConnectionCompletionUPP)0, 0, 1);
163 /*XXXX Is this safe? */
164 CMDispose(self->hdl);
165 self->hdl = NULL;
166 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000167 PyMem_DEL(self);
Jack Jansenee735be1994-12-14 13:31:11 +0000168}
169
Jack Jansenf5c20571997-01-30 15:48:07 +0000170static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000171ctbcm_open(self, args)
172 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000173 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000174{
175 long timeout;
176 OSErr err;
177 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
178
Jack Jansenf5c20571997-01-30 15:48:07 +0000179 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000180 return NULL;
181 if ( (err=CMOpen(self->hdl, self->has_callback, cb_upp, timeout)) < 0)
182 return PyErr_Mac(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000183 Py_INCREF(Py_None);
184 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000185}
186
Jack Jansenf5c20571997-01-30 15:48:07 +0000187static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000188ctbcm_listen(self, args)
189 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000190 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000191{
192 long timeout;
193 OSErr err;
194 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
195
Jack Jansenf5c20571997-01-30 15:48:07 +0000196 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000197 return NULL;
198 if ( (err=CMListen(self->hdl,self->has_callback, cb_upp, timeout)) < 0)
199 return PyErr_Mac(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000200 Py_INCREF(Py_None);
201 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000202}
203
Jack Jansenf5c20571997-01-30 15:48:07 +0000204static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000205ctbcm_accept(self, args)
206 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000208{
209 int accept;
210 OSErr err;
211
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 if (!PyArg_Parse(args, "i", &accept))
Jack Jansenee735be1994-12-14 13:31:11 +0000213 return NULL;
214 if ( (err=CMAccept(self->hdl, accept)) < 0)
215 return PyErr_Mac(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000216 Py_INCREF(Py_None);
217 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000218}
219
Jack Jansenf5c20571997-01-30 15:48:07 +0000220static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000221ctbcm_close(self, args)
222 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000223 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000224{
225 int now;
226 long timeout;
227 OSErr err;
228 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
229
Jack Jansenf5c20571997-01-30 15:48:07 +0000230 if (!PyArg_Parse(args, "(li)", &timeout, &now))
Jack Jansenee735be1994-12-14 13:31:11 +0000231 return NULL;
232 if ( (err=CMClose(self->hdl, self->has_callback, cb_upp, timeout, now)) < 0)
233 return PyErr_Mac(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000234 Py_INCREF(Py_None);
235 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000236}
237
Jack Jansenf5c20571997-01-30 15:48:07 +0000238static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000239ctbcm_read(self, args)
240 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000241 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000242{
243 long timeout, len;
244 int chan;
245 CMFlags flags;
246 OSErr err;
Jack Jansenf5c20571997-01-30 15:48:07 +0000247 PyObject *rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000248 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
249
Jack Jansenf5c20571997-01-30 15:48:07 +0000250 if (!PyArg_Parse(args, "(lil)", &len, &chan, &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000251 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000252 if ((rv=PyString_FromStringAndSize(NULL, len)) == NULL)
Jack Jansenee735be1994-12-14 13:31:11 +0000253 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000254 if ((err=CMRead(self->hdl, (Ptr)PyString_AsString(rv), &len, (CMChannel)chan,
Jack Jansenee735be1994-12-14 13:31:11 +0000255 self->has_callback, cb_upp, timeout, &flags)) < 0)
256 return PyErr_Mac(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000257 _PyString_Resize(&rv, len);
258 return Py_BuildValue("(Oi)", rv, (int)flags);
Jack Jansenee735be1994-12-14 13:31:11 +0000259}
260
Jack Jansenf5c20571997-01-30 15:48:07 +0000261static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000262ctbcm_write(self, args)
263 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000264 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000265{
266 long timeout, len;
267 int chan, ilen, flags;
268 OSErr err;
269 char *buf;
270 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
271
Jack Jansen4a8c54e1997-02-24 13:56:59 +0000272 if (!PyArg_Parse(args, "(m#ili)", &buf, &ilen, &chan, &timeout, &flags))
Jack Jansenee735be1994-12-14 13:31:11 +0000273 return NULL;
274 len = ilen;
275 if ((err=CMWrite(self->hdl, (Ptr)buf, &len, (CMChannel)chan,
276 self->has_callback, cb_upp, timeout, (CMFlags)flags)) < 0)
277 return PyErr_Mac(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000278 return PyInt_FromLong((int)len);
Jack Jansenee735be1994-12-14 13:31:11 +0000279}
280
Jack Jansenf5c20571997-01-30 15:48:07 +0000281static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000282ctbcm_status(self, args)
283 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000284 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000285{
286 CMBufferSizes sizes;
287 CMStatFlags flags;
288 OSErr err;
Jack Jansenf5c20571997-01-30 15:48:07 +0000289 PyObject *rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000290
Jack Jansenf5c20571997-01-30 15:48:07 +0000291 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000292 return NULL;
293 if ((err=CMStatus(self->hdl, sizes, &flags)) < 0)
294 return PyErr_Mac(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000295 rv = Py_BuildValue("(llllll)", sizes[0], sizes[1], sizes[2], sizes[3], sizes[4], sizes[5]);
Jack Jansenee735be1994-12-14 13:31:11 +0000296 if ( rv == NULL )
297 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000298 return Py_BuildValue("(Ol)", rv, (long)flags);
Jack Jansenee735be1994-12-14 13:31:11 +0000299}
300
Jack Jansenf5c20571997-01-30 15:48:07 +0000301static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000302ctbcm_getconfig(self, args)
303 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000304 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000305{
306 char *rv;
307
Jack Jansenf5c20571997-01-30 15:48:07 +0000308 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000309 return NULL;
310 if ((rv=(char *)CMGetConfig(self->hdl)) == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000311 PyErr_SetString(ErrorObject, "CMGetConfig failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000312 return NULL;
313 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000314 return PyString_FromString(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000315}
316
Jack Jansenf5c20571997-01-30 15:48:07 +0000317static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000318ctbcm_setconfig(self, args)
319 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000320 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000321{
322 char *cfg;
323 OSErr err;
324
Jack Jansenf5c20571997-01-30 15:48:07 +0000325 if (!PyArg_Parse(args, "s", &cfg))
Jack Jansenee735be1994-12-14 13:31:11 +0000326 return NULL;
327 if ((err=CMSetConfig(self->hdl, (Ptr)cfg)) < 0)
328 return PyErr_Mac(ErrorObject, err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000329 return PyInt_FromLong((int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000330}
331
Jack Jansenf5c20571997-01-30 15:48:07 +0000332static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000333ctbcm_choose(self, args)
334 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000335 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000336{
337 int rv;
338 Point pt;
339
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000341 return NULL;
342 pt.v = 40;
343 pt.h = 40;
344 rv=CMChoose(&self->hdl, pt, (ConnectionChooseIdleUPP)0);
Jack Jansenf5c20571997-01-30 15:48:07 +0000345 return PyInt_FromLong(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000346}
347
Jack Jansenf5c20571997-01-30 15:48:07 +0000348static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000349ctbcm_idle(self, args)
350 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000351 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000352{
Jack Jansenf5c20571997-01-30 15:48:07 +0000353 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000354 return NULL;
355 CMIdle(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000356 Py_INCREF(Py_None);
357 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000358}
359
Jack Jansenf5c20571997-01-30 15:48:07 +0000360static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000361ctbcm_abort(self, args)
362 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000363 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000364{
Jack Jansenf5c20571997-01-30 15:48:07 +0000365 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000366 return NULL;
367 CMAbort(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000368 Py_INCREF(Py_None);
369 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000370}
371
Jack Jansenf5c20571997-01-30 15:48:07 +0000372static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000373ctbcm_reset(self, args)
374 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000375 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000376{
Jack Jansenf5c20571997-01-30 15:48:07 +0000377 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000378 return NULL;
379 CMReset(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000380 Py_INCREF(Py_None);
381 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000382}
383
Jack Jansenf5c20571997-01-30 15:48:07 +0000384static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000385ctbcm_break(self, args)
386 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000387 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000388{
389 long duration;
390 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
391
Jack Jansenf5c20571997-01-30 15:48:07 +0000392 if (!PyArg_Parse(args, "l", &duration))
Jack Jansenee735be1994-12-14 13:31:11 +0000393 return NULL;
394 CMBreak(self->hdl, duration,self->has_callback, cb_upp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000395 Py_INCREF(Py_None);
396 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000397}
398
Jack Jansenf5c20571997-01-30 15:48:07 +0000399static struct PyMethodDef ctbcm_methods[] = {
400 {"Open", (PyCFunction)ctbcm_open},
401 {"Close", (PyCFunction)ctbcm_close},
402 {"Read", (PyCFunction)ctbcm_read},
403 {"Write", (PyCFunction)ctbcm_write},
404 {"Status", (PyCFunction)ctbcm_status},
405 {"GetConfig", (PyCFunction)ctbcm_getconfig},
406 {"SetConfig", (PyCFunction)ctbcm_setconfig},
407 {"Choose", (PyCFunction)ctbcm_choose},
408 {"Idle", (PyCFunction)ctbcm_idle},
409 {"Listen", (PyCFunction)ctbcm_listen},
410 {"Accept", (PyCFunction)ctbcm_accept},
411 {"Abort", (PyCFunction)ctbcm_abort},
412 {"Reset", (PyCFunction)ctbcm_reset},
413 {"Break", (PyCFunction)ctbcm_break},
Jack Jansenee735be1994-12-14 13:31:11 +0000414 {NULL, NULL} /* sentinel */
415};
416
Jack Jansenf5c20571997-01-30 15:48:07 +0000417static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000418ctbcm_getattr(self, name)
419 ctbcmobject *self;
420 char *name;
421{
422 if ( strcmp(name, "callback") == 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000423 Py_INCREF(self->callback);
Jack Jansenee735be1994-12-14 13:31:11 +0000424 return self->callback;
425 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000426 return Py_FindMethod(ctbcm_methods, (PyObject *)self, name);
Jack Jansenee735be1994-12-14 13:31:11 +0000427}
428
429static int
430ctbcm_setattr(self, name, v)
431 ctbcmobject *self;
432 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000433 PyObject *v;
Jack Jansenee735be1994-12-14 13:31:11 +0000434{
435 if ( strcmp(name, "callback") != 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000436 PyErr_SetString(PyExc_AttributeError, "ctbcm objects have callback attr only");
Jack Jansenee735be1994-12-14 13:31:11 +0000437 return -1;
438 }
439 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000440 v = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000441 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000442 Py_INCREF(v); /* XXXX Must I do this? */
Jack Jansenee735be1994-12-14 13:31:11 +0000443 self->callback = v;
Jack Jansenf5c20571997-01-30 15:48:07 +0000444 self->has_callback = (v != Py_None);
Jack Jansenee735be1994-12-14 13:31:11 +0000445 return 0;
446}
447
Jack Jansenf5c20571997-01-30 15:48:07 +0000448statichere PyTypeObject ctbcmtype = {
449 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansenee735be1994-12-14 13:31:11 +0000450 0, /*ob_size*/
451 "ctbcm", /*tp_name*/
452 sizeof(ctbcmobject), /*tp_basicsize*/
453 0, /*tp_itemsize*/
454 /* methods */
455 (destructor)ctbcm_dealloc, /*tp_dealloc*/
456 0, /*tp_print*/
457 (getattrfunc)ctbcm_getattr, /*tp_getattr*/
458 (setattrfunc)ctbcm_setattr, /*tp_setattr*/
459 0, /*tp_compare*/
460 0, /*tp_repr*/
461 0, /*tp_as_number*/
462 0, /*tp_as_sequence*/
463 0, /*tp_as_mapping*/
464 0, /*tp_hash*/
465};
466/* --------------------------------------------------------------------- */
467
468/* Function of no arguments returning new ctbcm object */
469
Jack Jansenf5c20571997-01-30 15:48:07 +0000470static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000471ctb_cmnew(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000472 PyObject *self; /* Not used */
473 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000474{
475 int strlen;
Jack Jansenf5c20571997-01-30 15:48:07 +0000476 PyObject *sizes_obj;
Jack Jansenee735be1994-12-14 13:31:11 +0000477 char *c_str;
478 unsigned char p_str[255];
479 CMBufferSizes sizes;
480 short procid;
481 ConnHandle hdl;
482 ctbcmobject *rv;
483
Jack Jansenf5c20571997-01-30 15:48:07 +0000484 if (!PyArg_Parse(args, "(s#O)", &c_str, &strlen, &sizes_obj))
Jack Jansenee735be1994-12-14 13:31:11 +0000485 return NULL;
486 strncpy((char *)p_str+1, c_str, strlen);
487 p_str[0] = strlen;
488 if (!initialize_ctb())
489 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000490 if ( sizes_obj == Py_None ) {
Jack Jansenee735be1994-12-14 13:31:11 +0000491 memset(sizes, '\0', sizeof sizes);
492 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000493 if ( !PyArg_Parse(sizes_obj, "(llllll)", &sizes[0], &sizes[1], &sizes[2],
Jack Jansenee735be1994-12-14 13:31:11 +0000494 &sizes[3], &sizes[4], &sizes[5]))
495 return NULL;
496 }
497 if ( (procid=CMGetProcID(p_str)) < 0 )
498 return PyErr_Mac(ErrorObject, procid);
499 hdl = CMNew(procid, cmNoMenus|cmQuiet, sizes, 0, 0);
500 if ( hdl == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000501 PyErr_SetString(ErrorObject, "CMNew failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000502 return NULL;
503 }
504 rv = newctbcmobject(args);
505 if ( rv == NULL )
506 return NULL; /* XXXX Should dispose of hdl */
507 rv->hdl = hdl;
508 CMSetUserData(hdl, (long)rv);
Jack Jansenf5c20571997-01-30 15:48:07 +0000509 return (PyObject *)rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000510}
511
Jack Jansenf5c20571997-01-30 15:48:07 +0000512static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000513ctb_available(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000514 PyObject *self;
515 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000516{
517 int ok;
518
Jack Jansenf5c20571997-01-30 15:48:07 +0000519 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000520 return NULL;
521 ok = initialize_ctb();
Jack Jansenf5c20571997-01-30 15:48:07 +0000522 PyErr_Clear();
523 return PyInt_FromLong(ok);
Jack Jansenee735be1994-12-14 13:31:11 +0000524}
525
526/* List of functions defined in the module */
527
Jack Jansenf5c20571997-01-30 15:48:07 +0000528static struct PyMethodDef ctb_methods[] = {
Jack Jansenee735be1994-12-14 13:31:11 +0000529 {"CMNew", ctb_cmnew},
530 {"available", ctb_available},
531 {NULL, NULL} /* sentinel */
532};
533
534
535/* Initialization function for the module (*must* be called initctb) */
536
537void
538initctb()
539{
Jack Jansenf5c20571997-01-30 15:48:07 +0000540 PyObject *m, *d, *o;
Jack Jansenee735be1994-12-14 13:31:11 +0000541
542 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000543 m = Py_InitModule("ctb", ctb_methods);
Jack Jansenee735be1994-12-14 13:31:11 +0000544
545 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000546 d = PyModule_GetDict(m);
Jack Jansenee735be1994-12-14 13:31:11 +0000547
Jack Jansenf5c20571997-01-30 15:48:07 +0000548#define CMCONST(name, value) o = PyInt_FromLong(value); PyDict_SetItemString(d, name, o)
Jack Jansenee735be1994-12-14 13:31:11 +0000549
550 CMCONST("cmData", 1);
551 CMCONST("cmCntl", 2);
552 CMCONST("cmAttn", 3);
553
554 CMCONST("cmFlagsEOM", 1);
555
556 CMCONST("chooseDisaster", -2);
557 CMCONST("chooseFailed", -1);
558 CMCONST("chooseAborted", 0);
559 CMCONST("chooseOKMinor", 1);
560 CMCONST("chooseOKMajor", 2);
561 CMCONST("chooseCancel", 3);
562
563 CMCONST("cmStatusOpening", 1);
564 CMCONST("cmStatusOpen", 2);
565 CMCONST("cmStatusClosing", 4);
566 CMCONST("cmStatusDataAvail", 8);
567 CMCONST("cmStatusCntlAvail", 0x10);
568 CMCONST("cmStatusAttnAvail", 0x20);
569 CMCONST("cmStatusDRPend", 0x40);
570 CMCONST("cmStatusDWPend", 0x80);
571 CMCONST("cmStatusCWPend", 0x100);
572 CMCONST("cmStatusCWPend", 0x200);
573 CMCONST("cmStatusARPend", 0x400);
574 CMCONST("cmStatusAWPend", 0x800);
575 CMCONST("cmStatusBreakPending", 0x1000);
576 CMCONST("cmStatusListenPend", 0x2000);
577 CMCONST("cmStatusIncomingCallPresent", 0x4000);
578
Jack Jansenf5c20571997-01-30 15:48:07 +0000579 ErrorObject = PyString_FromString("ctb.error");
580 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansenee735be1994-12-14 13:31:11 +0000581
582 /* Check for errors */
Jack Jansenf5c20571997-01-30 15:48:07 +0000583 if (PyErr_Occurred())
584 Py_FatalError("can't initialize module ctb");
Jack Jansenee735be1994-12-14 13:31:11 +0000585}