blob: 907b7bb7c6a85288be8151eb728208a4f37f218d [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 Jansen377df981997-04-03 14:48:04 +000049static char *errornames[] = {
50 "No Error",
51 "Rejected", /* 1 */
52 "Failed", /* 2 */
53 "Timeout", /* 3 */
54 "Not Open", /* 4 */
55 "Not Closed", /* 5 */
56 "No Request Pending", /* 6 */
57 "Not Supported", /* 7 */
58 "No Tools", /* 8 */
59 "User Cancel", /* 9 */
60 "Error 10", /* 10 */
61 "Unknown Error", /* 11 */
62#define MAX_POS_ERROR 11
63};
64
Jack Jansenf5c20571997-01-30 15:48:07 +000065extern PyObject *PyErr_Mac(PyObject *,int);
Jack Jansenee735be1994-12-14 13:31:11 +000066
Jack Jansen377df981997-04-03 14:48:04 +000067static PyObject *
68PyCtb_Error(PyObject *errobj, int errcode)
69{
70 if ( errcode > 0 && errcode <= MAX_POS_ERROR ) {
71 PyErr_SetString(errobj, errornames[errcode]);
72 return NULL;
73 } else {
74 return PyErr_Mac(errobj, errcode);
75 }
76}
77
Jack Jansenf5c20571997-01-30 15:48:07 +000078static PyObject *ErrorObject;
Jack Jansenee735be1994-12-14 13:31:11 +000079
80typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000081 PyObject_HEAD
Jack Jansenee735be1994-12-14 13:31:11 +000082 ConnHandle hdl; /* The handle to the connection */
Jack Jansenf5c20571997-01-30 15:48:07 +000083 PyObject *callback; /* Python callback routine */
Jack Jansenee735be1994-12-14 13:31:11 +000084 int has_callback; /* True if callback not None */
85 int err; /* Error to pass to the callback */
86} ctbcmobject;
87
Jack Jansenf5c20571997-01-30 15:48:07 +000088staticforward PyTypeObject ctbcmtype;
Jack Jansenee735be1994-12-14 13:31:11 +000089
90#define is_ctbcmobject(v) ((v)->ob_type == &ctbcmtype)
91
92static
93TrapAvailable(short tNumber, TrapType tType)
94{
95 short unImplemented;
96
97 if (tType == OSTrap)
98 unImplemented = _UnimplementedOSTrap;
99 else
100 unImplemented = _UnimplementedToolTrap;
101
102 return NGetTrapAddress(tNumber, tType) != NGetTrapAddress(unImplemented, tType);
103}
104
105static
106initialize_ctb()
107{
108 OSErr err;
109 static initialized = -1;
110
111 if ( initialized >= 0 )
112 return initialized;
113 initialized = 0;
114
115 if ( !TrapAvailable(_CommToolboxTrap, OSTrap) ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000116 PyErr_SetString(ErrorObject, "CTB not available");
Jack Jansenee735be1994-12-14 13:31:11 +0000117 return 0;
118 }
119 if ( (err=InitCTBUtilities()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000120 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000121 return 0;
122 }
123 if ( (err=InitCRM()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000124 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000125 return 0;
126 }
127 if ( (err=InitCM()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000128 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000129 return 0;
130 }
131 initialized = 1;
132 return 1;
133}
134
135static int
136ctbcm_pycallback(arg)
137 void *arg;
138{
139 ctbcmobject *self = (ctbcmobject *)arg;
Jack Jansenf5c20571997-01-30 15:48:07 +0000140 PyObject *args, *rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000141
142 if ( !self->has_callback ) /* It could have been removed in the meantime */
143 return 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000144 args = Py_BuildValue("(i)", self->err);
145 rv = PyEval_CallObject(self->callback, args);
146 Py_DECREF(args);
Jack Jansenee735be1994-12-14 13:31:11 +0000147 if( rv == NULL )
148 return -1;
Jack Jansenf5c20571997-01-30 15:48:07 +0000149 Py_DECREF(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000150 return 0;
151}
152
153/*DBG*/int ncallback;
154static pascal void
155ctbcm_ctbcallback(hconn)
156 ConnHandle hconn;
157{
158 ctbcmobject *self;
159
160 /* XXXX Do I have to do the A5 mumbo-jumbo? */
161 ncallback++; /*DBG*/
162 self = (ctbcmobject *)CMGetUserData(hconn);
163 self->err = (int)((*hconn)->errCode);
164 Py_AddPendingCall(ctbcm_pycallback, (void *)self);
165}
166
167static ctbcmobject *
168newctbcmobject(arg)
Jack Jansenf5c20571997-01-30 15:48:07 +0000169 PyObject *arg;
Jack Jansenee735be1994-12-14 13:31:11 +0000170{
171 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000172 self = PyObject_NEW(ctbcmobject, &ctbcmtype);
Jack Jansenee735be1994-12-14 13:31:11 +0000173 if (self == NULL)
174 return NULL;
175 self->hdl = NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000176 Py_INCREF(Py_None);
177 self->callback = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000178 self->has_callback = 0;
179 return self;
180}
181
182/* ctbcm methods */
183
184static void
185ctbcm_dealloc(self)
186 ctbcmobject *self;
187{
188 if ( self->hdl ) {
189 (void)CMClose(self->hdl, 0, (ConnectionCompletionUPP)0, 0, 1);
190 /*XXXX Is this safe? */
191 CMDispose(self->hdl);
192 self->hdl = NULL;
193 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000194 PyMem_DEL(self);
Jack Jansenee735be1994-12-14 13:31:11 +0000195}
196
Jack Jansenf5c20571997-01-30 15:48:07 +0000197static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000198ctbcm_open(self, args)
199 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000200 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000201{
202 long timeout;
203 OSErr err;
204 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
205
Jack Jansenf5c20571997-01-30 15:48:07 +0000206 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000207 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000208 if ( (err=CMOpen(self->hdl, self->has_callback, cb_upp, timeout)) != 0)
209 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000210 Py_INCREF(Py_None);
211 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000212}
213
Jack Jansenf5c20571997-01-30 15:48:07 +0000214static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000215ctbcm_listen(self, args)
216 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000217 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000218{
219 long timeout;
220 OSErr err;
221 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
222
Jack Jansenf5c20571997-01-30 15:48:07 +0000223 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000224 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000225 if ( (err=CMListen(self->hdl,self->has_callback, cb_upp, timeout)) != 0)
226 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000227 Py_INCREF(Py_None);
228 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000229}
230
Jack Jansenf5c20571997-01-30 15:48:07 +0000231static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000232ctbcm_accept(self, args)
233 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000234 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000235{
236 int accept;
237 OSErr err;
238
Jack Jansenf5c20571997-01-30 15:48:07 +0000239 if (!PyArg_Parse(args, "i", &accept))
Jack Jansenee735be1994-12-14 13:31:11 +0000240 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000241 if ( (err=CMAccept(self->hdl, accept)) != 0)
242 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000243 Py_INCREF(Py_None);
244 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000245}
246
Jack Jansenf5c20571997-01-30 15:48:07 +0000247static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000248ctbcm_close(self, args)
249 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000250 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000251{
252 int now;
253 long timeout;
254 OSErr err;
255 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
256
Jack Jansenf5c20571997-01-30 15:48:07 +0000257 if (!PyArg_Parse(args, "(li)", &timeout, &now))
Jack Jansenee735be1994-12-14 13:31:11 +0000258 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000259 if ( (err=CMClose(self->hdl, self->has_callback, cb_upp, timeout, now)) != 0)
260 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000261 Py_INCREF(Py_None);
262 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000263}
264
Jack Jansenf5c20571997-01-30 15:48:07 +0000265static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000266ctbcm_read(self, args)
267 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000268 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000269{
270 long timeout, len;
271 int chan;
272 CMFlags flags;
273 OSErr err;
Jack Jansen377df981997-04-03 14:48:04 +0000274 PyObject *rv, *rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000275 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
276
Jack Jansenf5c20571997-01-30 15:48:07 +0000277 if (!PyArg_Parse(args, "(lil)", &len, &chan, &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000278 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000279 if ((rv=PyString_FromStringAndSize(NULL, len)) == NULL)
Jack Jansenee735be1994-12-14 13:31:11 +0000280 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000281 if ((err=CMRead(self->hdl, (Ptr)PyString_AsString(rv), &len, (CMChannel)chan,
Jack Jansen377df981997-04-03 14:48:04 +0000282 self->has_callback, cb_upp, timeout, &flags)) != 0 && err != cmTimeOut)
283 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000284 _PyString_Resize(&rv, len);
Jack Jansen377df981997-04-03 14:48:04 +0000285 rrv = Py_BuildValue("(Oi)", rv, (int)flags);
286 Py_DECREF(rv);
287 return rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000288}
289
Jack Jansenf5c20571997-01-30 15:48:07 +0000290static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000291ctbcm_write(self, args)
292 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000293 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000294{
295 long timeout, len;
296 int chan, ilen, flags;
297 OSErr err;
298 char *buf;
299 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
300
Jack Janseneeccca91997-05-07 15:46:31 +0000301 if (!PyArg_Parse(args, "(s#ili)", &buf, &ilen, &chan, &timeout, &flags))
Jack Jansenee735be1994-12-14 13:31:11 +0000302 return NULL;
303 len = ilen;
304 if ((err=CMWrite(self->hdl, (Ptr)buf, &len, (CMChannel)chan,
Jack Jansen377df981997-04-03 14:48:04 +0000305 self->has_callback, cb_upp, timeout, (CMFlags)flags)) != 0 && err != cmTimeOut)
306 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000307 return PyInt_FromLong((int)len);
Jack Jansenee735be1994-12-14 13:31:11 +0000308}
309
Jack Jansenf5c20571997-01-30 15:48:07 +0000310static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000311ctbcm_status(self, args)
312 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000313 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000314{
315 CMBufferSizes sizes;
316 CMStatFlags flags;
317 OSErr err;
Jack Jansen377df981997-04-03 14:48:04 +0000318 PyObject *rv, *rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000319
Jack Jansenf5c20571997-01-30 15:48:07 +0000320 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000321 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000322 if ((err=CMStatus(self->hdl, sizes, &flags)) != 0)
323 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000324 rv = Py_BuildValue("(llllll)", sizes[0], sizes[1], sizes[2], sizes[3], sizes[4], sizes[5]);
Jack Jansenee735be1994-12-14 13:31:11 +0000325 if ( rv == NULL )
326 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000327 rrv = Py_BuildValue("(Ol)", rv, (long)flags);
328 Py_DECREF(rv);
329 return rrv;
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_getconfig(self, args)
334 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000335 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000336{
337 char *rv;
338
Jack Jansenf5c20571997-01-30 15:48:07 +0000339 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000340 return NULL;
341 if ((rv=(char *)CMGetConfig(self->hdl)) == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000342 PyErr_SetString(ErrorObject, "CMGetConfig failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000343 return NULL;
344 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000345 return PyString_FromString(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_setconfig(self, args)
350 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000351 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000352{
353 char *cfg;
Jack Jansen377df981997-04-03 14:48:04 +0000354 short rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000355
Jack Jansenf5c20571997-01-30 15:48:07 +0000356 if (!PyArg_Parse(args, "s", &cfg))
Jack Jansenee735be1994-12-14 13:31:11 +0000357 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000358 rv=CMSetConfig(self->hdl, (Ptr)cfg);
359 return PyInt_FromLong((long)rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000360}
361
Jack Jansenf5c20571997-01-30 15:48:07 +0000362static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000363ctbcm_choose(self, args)
364 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000365 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000366{
367 int rv;
368 Point pt;
369
Jack Jansenf5c20571997-01-30 15:48:07 +0000370 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000371 return NULL;
372 pt.v = 40;
373 pt.h = 40;
374 rv=CMChoose(&self->hdl, pt, (ConnectionChooseIdleUPP)0);
Jack Jansenf5c20571997-01-30 15:48:07 +0000375 return PyInt_FromLong(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000376}
377
Jack Jansenf5c20571997-01-30 15:48:07 +0000378static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000379ctbcm_idle(self, args)
380 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000381 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000382{
Jack Jansenf5c20571997-01-30 15:48:07 +0000383 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000384 return NULL;
385 CMIdle(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000386 Py_INCREF(Py_None);
387 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000388}
389
Jack Jansenf5c20571997-01-30 15:48:07 +0000390static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000391ctbcm_abort(self, args)
392 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000393 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000394{
Jack Jansenf5c20571997-01-30 15:48:07 +0000395 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000396 return NULL;
397 CMAbort(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000398 Py_INCREF(Py_None);
399 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000400}
401
Jack Jansenf5c20571997-01-30 15:48:07 +0000402static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000403ctbcm_reset(self, args)
404 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000405 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000406{
Jack Jansenf5c20571997-01-30 15:48:07 +0000407 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000408 return NULL;
409 CMReset(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000410 Py_INCREF(Py_None);
411 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000412}
413
Jack Jansenf5c20571997-01-30 15:48:07 +0000414static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000415ctbcm_break(self, args)
416 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000418{
419 long duration;
420 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
421
Jack Jansenf5c20571997-01-30 15:48:07 +0000422 if (!PyArg_Parse(args, "l", &duration))
Jack Jansenee735be1994-12-14 13:31:11 +0000423 return NULL;
424 CMBreak(self->hdl, duration,self->has_callback, cb_upp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000425 Py_INCREF(Py_None);
426 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000427}
428
Jack Jansenf5c20571997-01-30 15:48:07 +0000429static struct PyMethodDef ctbcm_methods[] = {
430 {"Open", (PyCFunction)ctbcm_open},
431 {"Close", (PyCFunction)ctbcm_close},
432 {"Read", (PyCFunction)ctbcm_read},
433 {"Write", (PyCFunction)ctbcm_write},
434 {"Status", (PyCFunction)ctbcm_status},
435 {"GetConfig", (PyCFunction)ctbcm_getconfig},
436 {"SetConfig", (PyCFunction)ctbcm_setconfig},
437 {"Choose", (PyCFunction)ctbcm_choose},
438 {"Idle", (PyCFunction)ctbcm_idle},
439 {"Listen", (PyCFunction)ctbcm_listen},
440 {"Accept", (PyCFunction)ctbcm_accept},
441 {"Abort", (PyCFunction)ctbcm_abort},
442 {"Reset", (PyCFunction)ctbcm_reset},
443 {"Break", (PyCFunction)ctbcm_break},
Jack Jansenee735be1994-12-14 13:31:11 +0000444 {NULL, NULL} /* sentinel */
445};
446
Jack Jansenf5c20571997-01-30 15:48:07 +0000447static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000448ctbcm_getattr(self, name)
449 ctbcmobject *self;
450 char *name;
451{
452 if ( strcmp(name, "callback") == 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000453 Py_INCREF(self->callback);
Jack Jansenee735be1994-12-14 13:31:11 +0000454 return self->callback;
455 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000456 return Py_FindMethod(ctbcm_methods, (PyObject *)self, name);
Jack Jansenee735be1994-12-14 13:31:11 +0000457}
458
459static int
460ctbcm_setattr(self, name, v)
461 ctbcmobject *self;
462 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000463 PyObject *v;
Jack Jansenee735be1994-12-14 13:31:11 +0000464{
465 if ( strcmp(name, "callback") != 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000466 PyErr_SetString(PyExc_AttributeError, "ctbcm objects have callback attr only");
Jack Jansenee735be1994-12-14 13:31:11 +0000467 return -1;
468 }
469 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000470 v = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000471 }
Jack Jansen377df981997-04-03 14:48:04 +0000472 Py_INCREF(v);
Jack Jansenee735be1994-12-14 13:31:11 +0000473 self->callback = v;
Jack Jansenf5c20571997-01-30 15:48:07 +0000474 self->has_callback = (v != Py_None);
Jack Jansenee735be1994-12-14 13:31:11 +0000475 return 0;
476}
477
Jack Jansenf5c20571997-01-30 15:48:07 +0000478statichere PyTypeObject ctbcmtype = {
479 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansenee735be1994-12-14 13:31:11 +0000480 0, /*ob_size*/
481 "ctbcm", /*tp_name*/
482 sizeof(ctbcmobject), /*tp_basicsize*/
483 0, /*tp_itemsize*/
484 /* methods */
485 (destructor)ctbcm_dealloc, /*tp_dealloc*/
486 0, /*tp_print*/
487 (getattrfunc)ctbcm_getattr, /*tp_getattr*/
488 (setattrfunc)ctbcm_setattr, /*tp_setattr*/
489 0, /*tp_compare*/
490 0, /*tp_repr*/
491 0, /*tp_as_number*/
492 0, /*tp_as_sequence*/
493 0, /*tp_as_mapping*/
494 0, /*tp_hash*/
495};
496/* --------------------------------------------------------------------- */
497
498/* Function of no arguments returning new ctbcm object */
499
Jack Jansenf5c20571997-01-30 15:48:07 +0000500static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000501ctb_cmnew(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000502 PyObject *self; /* Not used */
503 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000504{
505 int strlen;
Jack Jansenf5c20571997-01-30 15:48:07 +0000506 PyObject *sizes_obj;
Jack Jansenee735be1994-12-14 13:31:11 +0000507 char *c_str;
508 unsigned char p_str[255];
509 CMBufferSizes sizes;
510 short procid;
511 ConnHandle hdl;
512 ctbcmobject *rv;
513
Jack Jansenf5c20571997-01-30 15:48:07 +0000514 if (!PyArg_Parse(args, "(s#O)", &c_str, &strlen, &sizes_obj))
Jack Jansenee735be1994-12-14 13:31:11 +0000515 return NULL;
516 strncpy((char *)p_str+1, c_str, strlen);
517 p_str[0] = strlen;
518 if (!initialize_ctb())
519 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000520 if ( sizes_obj == Py_None ) {
Jack Jansenee735be1994-12-14 13:31:11 +0000521 memset(sizes, '\0', sizeof sizes);
522 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000523 if ( !PyArg_Parse(sizes_obj, "(llllll)", &sizes[0], &sizes[1], &sizes[2],
Jack Jansenee735be1994-12-14 13:31:11 +0000524 &sizes[3], &sizes[4], &sizes[5]))
525 return NULL;
526 }
527 if ( (procid=CMGetProcID(p_str)) < 0 )
Jack Jansen377df981997-04-03 14:48:04 +0000528 return PyCtb_Error(ErrorObject, procid);
Jack Jansenee735be1994-12-14 13:31:11 +0000529 hdl = CMNew(procid, cmNoMenus|cmQuiet, sizes, 0, 0);
530 if ( hdl == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000531 PyErr_SetString(ErrorObject, "CMNew failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000532 return NULL;
533 }
534 rv = newctbcmobject(args);
535 if ( rv == NULL )
536 return NULL; /* XXXX Should dispose of hdl */
537 rv->hdl = hdl;
538 CMSetUserData(hdl, (long)rv);
Jack Jansenf5c20571997-01-30 15:48:07 +0000539 return (PyObject *)rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000540}
541
Jack Jansenf5c20571997-01-30 15:48:07 +0000542static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000543ctb_available(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000544 PyObject *self;
545 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000546{
547 int ok;
548
Jack Jansenf5c20571997-01-30 15:48:07 +0000549 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000550 return NULL;
551 ok = initialize_ctb();
Jack Jansenf5c20571997-01-30 15:48:07 +0000552 PyErr_Clear();
553 return PyInt_FromLong(ok);
Jack Jansenee735be1994-12-14 13:31:11 +0000554}
555
556/* List of functions defined in the module */
557
Jack Jansenf5c20571997-01-30 15:48:07 +0000558static struct PyMethodDef ctb_methods[] = {
Jack Jansenee735be1994-12-14 13:31:11 +0000559 {"CMNew", ctb_cmnew},
560 {"available", ctb_available},
561 {NULL, NULL} /* sentinel */
562};
563
564
565/* Initialization function for the module (*must* be called initctb) */
566
567void
568initctb()
569{
Jack Jansenf5c20571997-01-30 15:48:07 +0000570 PyObject *m, *d, *o;
Jack Jansenee735be1994-12-14 13:31:11 +0000571
572 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000573 m = Py_InitModule("ctb", ctb_methods);
Jack Jansenee735be1994-12-14 13:31:11 +0000574
575 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000576 d = PyModule_GetDict(m);
Jack Jansenee735be1994-12-14 13:31:11 +0000577
Jack Jansenf5c20571997-01-30 15:48:07 +0000578#define CMCONST(name, value) o = PyInt_FromLong(value); PyDict_SetItemString(d, name, o)
Jack Jansenee735be1994-12-14 13:31:11 +0000579
580 CMCONST("cmData", 1);
581 CMCONST("cmCntl", 2);
582 CMCONST("cmAttn", 3);
583
584 CMCONST("cmFlagsEOM", 1);
585
586 CMCONST("chooseDisaster", -2);
587 CMCONST("chooseFailed", -1);
588 CMCONST("chooseAborted", 0);
589 CMCONST("chooseOKMinor", 1);
590 CMCONST("chooseOKMajor", 2);
591 CMCONST("chooseCancel", 3);
592
593 CMCONST("cmStatusOpening", 1);
594 CMCONST("cmStatusOpen", 2);
595 CMCONST("cmStatusClosing", 4);
596 CMCONST("cmStatusDataAvail", 8);
597 CMCONST("cmStatusCntlAvail", 0x10);
598 CMCONST("cmStatusAttnAvail", 0x20);
599 CMCONST("cmStatusDRPend", 0x40);
600 CMCONST("cmStatusDWPend", 0x80);
601 CMCONST("cmStatusCWPend", 0x100);
602 CMCONST("cmStatusCWPend", 0x200);
603 CMCONST("cmStatusARPend", 0x400);
604 CMCONST("cmStatusAWPend", 0x800);
605 CMCONST("cmStatusBreakPending", 0x1000);
606 CMCONST("cmStatusListenPend", 0x2000);
607 CMCONST("cmStatusIncomingCallPresent", 0x4000);
608
Jack Jansenf5c20571997-01-30 15:48:07 +0000609 ErrorObject = PyString_FromString("ctb.error");
610 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansenee735be1994-12-14 13:31:11 +0000611
612 /* Check for errors */
Jack Jansenf5c20571997-01-30 15:48:07 +0000613 if (PyErr_Occurred())
614 Py_FatalError("can't initialize module ctb");
Jack Jansenee735be1994-12-14 13:31:11 +0000615}