blob: d95cc990b8885b831efce757837895c22b82e931 [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>
Jack Jansenc2952bc1998-02-20 15:55:53 +000035#include <CTBUtilities.h>
Jack Jansenee735be1994-12-14 13:31:11 +000036#include <ToolUtils.h>
37#include <OSUtils.h>
Guido van Rossum4410d571995-02-21 20:58:53 +000038
39#ifndef HAVE_UNIVERSAL_HEADERS
Jack Jansenee735be1994-12-14 13:31:11 +000040#define ConnectionCompletionUPP ProcPtr
41#define ConnectionChooseIdleUPP ProcPtr
42#define NewConnectionCompletionProc(x) (x)
43#define NewConnectionChooseIdleProc(x) (x)
44#endif
45
46#define _UnimplementedToolTrap 0xA89F
47#define _CommToolboxTrap 0x8B
48#define _UnimplementedOSTrap 0x9F
49
Jack Jansen377df981997-04-03 14:48:04 +000050static char *errornames[] = {
51 "No Error",
52 "Rejected", /* 1 */
53 "Failed", /* 2 */
54 "Timeout", /* 3 */
55 "Not Open", /* 4 */
56 "Not Closed", /* 5 */
57 "No Request Pending", /* 6 */
58 "Not Supported", /* 7 */
59 "No Tools", /* 8 */
60 "User Cancel", /* 9 */
61 "Error 10", /* 10 */
62 "Unknown Error", /* 11 */
63#define MAX_POS_ERROR 11
64};
65
Jack Jansenf5c20571997-01-30 15:48:07 +000066extern PyObject *PyErr_Mac(PyObject *,int);
Jack Jansenee735be1994-12-14 13:31:11 +000067
Jack Jansen377df981997-04-03 14:48:04 +000068static PyObject *
69PyCtb_Error(PyObject *errobj, int errcode)
70{
71 if ( errcode > 0 && errcode <= MAX_POS_ERROR ) {
72 PyErr_SetString(errobj, errornames[errcode]);
73 return NULL;
74 } else {
75 return PyErr_Mac(errobj, errcode);
76 }
77}
78
Jack Jansenf5c20571997-01-30 15:48:07 +000079static PyObject *ErrorObject;
Jack Jansenee735be1994-12-14 13:31:11 +000080
81typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000082 PyObject_HEAD
Jack Jansenee735be1994-12-14 13:31:11 +000083 ConnHandle hdl; /* The handle to the connection */
Jack Jansenf5c20571997-01-30 15:48:07 +000084 PyObject *callback; /* Python callback routine */
Jack Jansenee735be1994-12-14 13:31:11 +000085 int has_callback; /* True if callback not None */
86 int err; /* Error to pass to the callback */
87} ctbcmobject;
88
Jack Jansenf5c20571997-01-30 15:48:07 +000089staticforward PyTypeObject ctbcmtype;
Jack Jansenee735be1994-12-14 13:31:11 +000090
91#define is_ctbcmobject(v) ((v)->ob_type == &ctbcmtype)
92
93static
94TrapAvailable(short tNumber, TrapType tType)
95{
96 short unImplemented;
97
98 if (tType == OSTrap)
99 unImplemented = _UnimplementedOSTrap;
100 else
101 unImplemented = _UnimplementedToolTrap;
102
103 return NGetTrapAddress(tNumber, tType) != NGetTrapAddress(unImplemented, tType);
104}
105
106static
107initialize_ctb()
108{
109 OSErr err;
110 static initialized = -1;
111
112 if ( initialized >= 0 )
113 return initialized;
114 initialized = 0;
115
116 if ( !TrapAvailable(_CommToolboxTrap, OSTrap) ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000117 PyErr_SetString(ErrorObject, "CTB not available");
Jack Jansenee735be1994-12-14 13:31:11 +0000118 return 0;
119 }
120 if ( (err=InitCTBUtilities()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000121 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000122 return 0;
123 }
124 if ( (err=InitCRM()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000125 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000126 return 0;
127 }
128 if ( (err=InitCM()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000129 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000130 return 0;
131 }
132 initialized = 1;
133 return 1;
134}
135
136static int
137ctbcm_pycallback(arg)
138 void *arg;
139{
140 ctbcmobject *self = (ctbcmobject *)arg;
Jack Jansenf5c20571997-01-30 15:48:07 +0000141 PyObject *args, *rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000142
143 if ( !self->has_callback ) /* It could have been removed in the meantime */
144 return 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000145 args = Py_BuildValue("(i)", self->err);
146 rv = PyEval_CallObject(self->callback, args);
147 Py_DECREF(args);
Jack Jansenee735be1994-12-14 13:31:11 +0000148 if( rv == NULL )
149 return -1;
Jack Jansenf5c20571997-01-30 15:48:07 +0000150 Py_DECREF(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000151 return 0;
152}
153
154/*DBG*/int ncallback;
155static pascal void
156ctbcm_ctbcallback(hconn)
157 ConnHandle hconn;
158{
159 ctbcmobject *self;
160
161 /* XXXX Do I have to do the A5 mumbo-jumbo? */
162 ncallback++; /*DBG*/
163 self = (ctbcmobject *)CMGetUserData(hconn);
164 self->err = (int)((*hconn)->errCode);
165 Py_AddPendingCall(ctbcm_pycallback, (void *)self);
166}
167
168static ctbcmobject *
169newctbcmobject(arg)
Jack Jansenf5c20571997-01-30 15:48:07 +0000170 PyObject *arg;
Jack Jansenee735be1994-12-14 13:31:11 +0000171{
172 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000173 self = PyObject_NEW(ctbcmobject, &ctbcmtype);
Jack Jansenee735be1994-12-14 13:31:11 +0000174 if (self == NULL)
175 return NULL;
176 self->hdl = NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000177 Py_INCREF(Py_None);
178 self->callback = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000179 self->has_callback = 0;
180 return self;
181}
182
183/* ctbcm methods */
184
185static void
186ctbcm_dealloc(self)
187 ctbcmobject *self;
188{
189 if ( self->hdl ) {
190 (void)CMClose(self->hdl, 0, (ConnectionCompletionUPP)0, 0, 1);
191 /*XXXX Is this safe? */
192 CMDispose(self->hdl);
193 self->hdl = NULL;
194 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000195 PyMem_DEL(self);
Jack Jansenee735be1994-12-14 13:31:11 +0000196}
197
Jack Jansenf5c20571997-01-30 15:48:07 +0000198static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000199ctbcm_open(self, args)
200 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000201 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000202{
203 long timeout;
204 OSErr err;
205 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
206
Jack Jansenf5c20571997-01-30 15:48:07 +0000207 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000208 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000209 if ( (err=CMOpen(self->hdl, self->has_callback, cb_upp, timeout)) != 0)
210 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000211 Py_INCREF(Py_None);
212 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000213}
214
Jack Jansenf5c20571997-01-30 15:48:07 +0000215static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000216ctbcm_listen(self, args)
217 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000218 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000219{
220 long timeout;
221 OSErr err;
222 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
223
Jack Jansenf5c20571997-01-30 15:48:07 +0000224 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000225 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000226 if ( (err=CMListen(self->hdl,self->has_callback, cb_upp, timeout)) != 0)
227 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000228 Py_INCREF(Py_None);
229 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000230}
231
Jack Jansenf5c20571997-01-30 15:48:07 +0000232static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000233ctbcm_accept(self, args)
234 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000235 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000236{
237 int accept;
238 OSErr err;
239
Jack Jansenf5c20571997-01-30 15:48:07 +0000240 if (!PyArg_Parse(args, "i", &accept))
Jack Jansenee735be1994-12-14 13:31:11 +0000241 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000242 if ( (err=CMAccept(self->hdl, accept)) != 0)
243 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000244 Py_INCREF(Py_None);
245 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000246}
247
Jack Jansenf5c20571997-01-30 15:48:07 +0000248static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000249ctbcm_close(self, args)
250 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000251 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000252{
253 int now;
254 long timeout;
255 OSErr err;
256 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
257
Jack Jansenf5c20571997-01-30 15:48:07 +0000258 if (!PyArg_Parse(args, "(li)", &timeout, &now))
Jack Jansenee735be1994-12-14 13:31:11 +0000259 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000260 if ( (err=CMClose(self->hdl, self->has_callback, cb_upp, timeout, now)) != 0)
261 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000262 Py_INCREF(Py_None);
263 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000264}
265
Jack Jansenf5c20571997-01-30 15:48:07 +0000266static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000267ctbcm_read(self, args)
268 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000269 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000270{
271 long timeout, len;
272 int chan;
273 CMFlags flags;
274 OSErr err;
Jack Jansen377df981997-04-03 14:48:04 +0000275 PyObject *rv, *rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000276 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
277
Jack Jansenf5c20571997-01-30 15:48:07 +0000278 if (!PyArg_Parse(args, "(lil)", &len, &chan, &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000279 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000280 if ((rv=PyString_FromStringAndSize(NULL, len)) == NULL)
Jack Jansenee735be1994-12-14 13:31:11 +0000281 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000282 if ((err=CMRead(self->hdl, (Ptr)PyString_AsString(rv), &len, (CMChannel)chan,
Jack Jansen377df981997-04-03 14:48:04 +0000283 self->has_callback, cb_upp, timeout, &flags)) != 0 && err != cmTimeOut)
284 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000285 _PyString_Resize(&rv, len);
Jack Jansen377df981997-04-03 14:48:04 +0000286 rrv = Py_BuildValue("(Oi)", rv, (int)flags);
287 Py_DECREF(rv);
288 return rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000289}
290
Jack Jansenf5c20571997-01-30 15:48:07 +0000291static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000292ctbcm_write(self, args)
293 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000294 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000295{
296 long timeout, len;
297 int chan, ilen, flags;
298 OSErr err;
299 char *buf;
300 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
301
Jack Janseneeccca91997-05-07 15:46:31 +0000302 if (!PyArg_Parse(args, "(s#ili)", &buf, &ilen, &chan, &timeout, &flags))
Jack Jansenee735be1994-12-14 13:31:11 +0000303 return NULL;
304 len = ilen;
305 if ((err=CMWrite(self->hdl, (Ptr)buf, &len, (CMChannel)chan,
Jack Jansen377df981997-04-03 14:48:04 +0000306 self->has_callback, cb_upp, timeout, (CMFlags)flags)) != 0 && err != cmTimeOut)
307 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000308 return PyInt_FromLong((int)len);
Jack Jansenee735be1994-12-14 13:31:11 +0000309}
310
Jack Jansenf5c20571997-01-30 15:48:07 +0000311static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000312ctbcm_status(self, args)
313 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000314 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000315{
316 CMBufferSizes sizes;
317 CMStatFlags flags;
318 OSErr err;
Jack Jansen377df981997-04-03 14:48:04 +0000319 PyObject *rv, *rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000320
Jack Jansenf5c20571997-01-30 15:48:07 +0000321 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000322 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000323 if ((err=CMStatus(self->hdl, sizes, &flags)) != 0)
324 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000325 rv = Py_BuildValue("(llllll)", sizes[0], sizes[1], sizes[2], sizes[3], sizes[4], sizes[5]);
Jack Jansenee735be1994-12-14 13:31:11 +0000326 if ( rv == NULL )
327 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000328 rrv = Py_BuildValue("(Ol)", rv, (long)flags);
329 Py_DECREF(rv);
330 return rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000331}
332
Jack Jansenf5c20571997-01-30 15:48:07 +0000333static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000334ctbcm_getconfig(self, args)
335 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000336 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000337{
338 char *rv;
339
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000341 return NULL;
342 if ((rv=(char *)CMGetConfig(self->hdl)) == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000343 PyErr_SetString(ErrorObject, "CMGetConfig failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000344 return NULL;
345 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000346 return PyString_FromString(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000347}
348
Jack Jansenf5c20571997-01-30 15:48:07 +0000349static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000350ctbcm_setconfig(self, args)
351 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000352 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000353{
354 char *cfg;
Jack Jansen377df981997-04-03 14:48:04 +0000355 short rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000356
Jack Jansenf5c20571997-01-30 15:48:07 +0000357 if (!PyArg_Parse(args, "s", &cfg))
Jack Jansenee735be1994-12-14 13:31:11 +0000358 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000359 rv=CMSetConfig(self->hdl, (Ptr)cfg);
360 return PyInt_FromLong((long)rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000361}
362
Jack Jansenf5c20571997-01-30 15:48:07 +0000363static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000364ctbcm_choose(self, args)
365 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000366 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000367{
368 int rv;
369 Point pt;
370
Jack Jansenf5c20571997-01-30 15:48:07 +0000371 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000372 return NULL;
373 pt.v = 40;
374 pt.h = 40;
375 rv=CMChoose(&self->hdl, pt, (ConnectionChooseIdleUPP)0);
Jack Jansenf5c20571997-01-30 15:48:07 +0000376 return PyInt_FromLong(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000377}
378
Jack Jansenf5c20571997-01-30 15:48:07 +0000379static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000380ctbcm_idle(self, args)
381 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000382 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000383{
Jack Jansenf5c20571997-01-30 15:48:07 +0000384 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000385 return NULL;
386 CMIdle(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000387 Py_INCREF(Py_None);
388 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000389}
390
Jack Jansenf5c20571997-01-30 15:48:07 +0000391static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000392ctbcm_abort(self, args)
393 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000394 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000395{
Jack Jansenf5c20571997-01-30 15:48:07 +0000396 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000397 return NULL;
398 CMAbort(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000399 Py_INCREF(Py_None);
400 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000401}
402
Jack Jansenf5c20571997-01-30 15:48:07 +0000403static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000404ctbcm_reset(self, args)
405 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000406 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000407{
Jack Jansenf5c20571997-01-30 15:48:07 +0000408 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000409 return NULL;
410 CMReset(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000411 Py_INCREF(Py_None);
412 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000413}
414
Jack Jansenf5c20571997-01-30 15:48:07 +0000415static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000416ctbcm_break(self, args)
417 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000418 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000419{
420 long duration;
421 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
422
Jack Jansenf5c20571997-01-30 15:48:07 +0000423 if (!PyArg_Parse(args, "l", &duration))
Jack Jansenee735be1994-12-14 13:31:11 +0000424 return NULL;
425 CMBreak(self->hdl, duration,self->has_callback, cb_upp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000426 Py_INCREF(Py_None);
427 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000428}
429
Jack Jansenf5c20571997-01-30 15:48:07 +0000430static struct PyMethodDef ctbcm_methods[] = {
431 {"Open", (PyCFunction)ctbcm_open},
432 {"Close", (PyCFunction)ctbcm_close},
433 {"Read", (PyCFunction)ctbcm_read},
434 {"Write", (PyCFunction)ctbcm_write},
435 {"Status", (PyCFunction)ctbcm_status},
436 {"GetConfig", (PyCFunction)ctbcm_getconfig},
437 {"SetConfig", (PyCFunction)ctbcm_setconfig},
438 {"Choose", (PyCFunction)ctbcm_choose},
439 {"Idle", (PyCFunction)ctbcm_idle},
440 {"Listen", (PyCFunction)ctbcm_listen},
441 {"Accept", (PyCFunction)ctbcm_accept},
442 {"Abort", (PyCFunction)ctbcm_abort},
443 {"Reset", (PyCFunction)ctbcm_reset},
444 {"Break", (PyCFunction)ctbcm_break},
Jack Jansenee735be1994-12-14 13:31:11 +0000445 {NULL, NULL} /* sentinel */
446};
447
Jack Jansenf5c20571997-01-30 15:48:07 +0000448static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000449ctbcm_getattr(self, name)
450 ctbcmobject *self;
451 char *name;
452{
453 if ( strcmp(name, "callback") == 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000454 Py_INCREF(self->callback);
Jack Jansenee735be1994-12-14 13:31:11 +0000455 return self->callback;
456 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000457 return Py_FindMethod(ctbcm_methods, (PyObject *)self, name);
Jack Jansenee735be1994-12-14 13:31:11 +0000458}
459
460static int
461ctbcm_setattr(self, name, v)
462 ctbcmobject *self;
463 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000464 PyObject *v;
Jack Jansenee735be1994-12-14 13:31:11 +0000465{
466 if ( strcmp(name, "callback") != 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000467 PyErr_SetString(PyExc_AttributeError, "ctbcm objects have callback attr only");
Jack Jansenee735be1994-12-14 13:31:11 +0000468 return -1;
469 }
470 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000471 v = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000472 }
Jack Jansen377df981997-04-03 14:48:04 +0000473 Py_INCREF(v);
Jack Jansenee735be1994-12-14 13:31:11 +0000474 self->callback = v;
Jack Jansenf5c20571997-01-30 15:48:07 +0000475 self->has_callback = (v != Py_None);
Jack Jansenee735be1994-12-14 13:31:11 +0000476 return 0;
477}
478
Jack Jansenf5c20571997-01-30 15:48:07 +0000479statichere PyTypeObject ctbcmtype = {
480 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansenee735be1994-12-14 13:31:11 +0000481 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000482 "CTBConnectionMgr", /*tp_name*/
Jack Jansenee735be1994-12-14 13:31:11 +0000483 sizeof(ctbcmobject), /*tp_basicsize*/
484 0, /*tp_itemsize*/
485 /* methods */
486 (destructor)ctbcm_dealloc, /*tp_dealloc*/
487 0, /*tp_print*/
488 (getattrfunc)ctbcm_getattr, /*tp_getattr*/
489 (setattrfunc)ctbcm_setattr, /*tp_setattr*/
490 0, /*tp_compare*/
491 0, /*tp_repr*/
492 0, /*tp_as_number*/
493 0, /*tp_as_sequence*/
494 0, /*tp_as_mapping*/
495 0, /*tp_hash*/
496};
497/* --------------------------------------------------------------------- */
498
499/* Function of no arguments returning new ctbcm object */
500
Jack Jansenf5c20571997-01-30 15:48:07 +0000501static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000502ctb_cmnew(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000503 PyObject *self; /* Not used */
504 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000505{
506 int strlen;
Jack Jansenf5c20571997-01-30 15:48:07 +0000507 PyObject *sizes_obj;
Jack Jansenee735be1994-12-14 13:31:11 +0000508 char *c_str;
509 unsigned char p_str[255];
510 CMBufferSizes sizes;
511 short procid;
512 ConnHandle hdl;
513 ctbcmobject *rv;
514
Jack Jansenf5c20571997-01-30 15:48:07 +0000515 if (!PyArg_Parse(args, "(s#O)", &c_str, &strlen, &sizes_obj))
Jack Jansenee735be1994-12-14 13:31:11 +0000516 return NULL;
517 strncpy((char *)p_str+1, c_str, strlen);
518 p_str[0] = strlen;
519 if (!initialize_ctb())
520 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000521 if ( sizes_obj == Py_None ) {
Jack Jansenee735be1994-12-14 13:31:11 +0000522 memset(sizes, '\0', sizeof sizes);
523 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000524 if ( !PyArg_Parse(sizes_obj, "(llllll)", &sizes[0], &sizes[1], &sizes[2],
Jack Jansenee735be1994-12-14 13:31:11 +0000525 &sizes[3], &sizes[4], &sizes[5]))
526 return NULL;
527 }
528 if ( (procid=CMGetProcID(p_str)) < 0 )
Jack Jansen377df981997-04-03 14:48:04 +0000529 return PyCtb_Error(ErrorObject, procid);
Jack Jansenee735be1994-12-14 13:31:11 +0000530 hdl = CMNew(procid, cmNoMenus|cmQuiet, sizes, 0, 0);
531 if ( hdl == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000532 PyErr_SetString(ErrorObject, "CMNew failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000533 return NULL;
534 }
535 rv = newctbcmobject(args);
536 if ( rv == NULL )
537 return NULL; /* XXXX Should dispose of hdl */
538 rv->hdl = hdl;
539 CMSetUserData(hdl, (long)rv);
Jack Jansenf5c20571997-01-30 15:48:07 +0000540 return (PyObject *)rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000541}
542
Jack Jansenf5c20571997-01-30 15:48:07 +0000543static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000544ctb_available(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000545 PyObject *self;
546 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000547{
548 int ok;
549
Jack Jansenf5c20571997-01-30 15:48:07 +0000550 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000551 return NULL;
552 ok = initialize_ctb();
Jack Jansenf5c20571997-01-30 15:48:07 +0000553 PyErr_Clear();
554 return PyInt_FromLong(ok);
Jack Jansenee735be1994-12-14 13:31:11 +0000555}
556
557/* List of functions defined in the module */
558
Jack Jansenf5c20571997-01-30 15:48:07 +0000559static struct PyMethodDef ctb_methods[] = {
Jack Jansenee735be1994-12-14 13:31:11 +0000560 {"CMNew", ctb_cmnew},
561 {"available", ctb_available},
562 {NULL, NULL} /* sentinel */
563};
564
565
566/* Initialization function for the module (*must* be called initctb) */
567
568void
569initctb()
570{
Jack Jansenf5c20571997-01-30 15:48:07 +0000571 PyObject *m, *d, *o;
Jack Jansenee735be1994-12-14 13:31:11 +0000572
573 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000574 m = Py_InitModule("ctb", ctb_methods);
Jack Jansenee735be1994-12-14 13:31:11 +0000575
576 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000577 d = PyModule_GetDict(m);
Jack Jansenee735be1994-12-14 13:31:11 +0000578
Jack Jansenf5c20571997-01-30 15:48:07 +0000579#define CMCONST(name, value) o = PyInt_FromLong(value); PyDict_SetItemString(d, name, o)
Jack Jansenee735be1994-12-14 13:31:11 +0000580
581 CMCONST("cmData", 1);
582 CMCONST("cmCntl", 2);
583 CMCONST("cmAttn", 3);
584
585 CMCONST("cmFlagsEOM", 1);
586
587 CMCONST("chooseDisaster", -2);
588 CMCONST("chooseFailed", -1);
589 CMCONST("chooseAborted", 0);
590 CMCONST("chooseOKMinor", 1);
591 CMCONST("chooseOKMajor", 2);
592 CMCONST("chooseCancel", 3);
593
594 CMCONST("cmStatusOpening", 1);
595 CMCONST("cmStatusOpen", 2);
596 CMCONST("cmStatusClosing", 4);
597 CMCONST("cmStatusDataAvail", 8);
598 CMCONST("cmStatusCntlAvail", 0x10);
599 CMCONST("cmStatusAttnAvail", 0x20);
600 CMCONST("cmStatusDRPend", 0x40);
601 CMCONST("cmStatusDWPend", 0x80);
602 CMCONST("cmStatusCWPend", 0x100);
603 CMCONST("cmStatusCWPend", 0x200);
604 CMCONST("cmStatusARPend", 0x400);
605 CMCONST("cmStatusAWPend", 0x800);
606 CMCONST("cmStatusBreakPending", 0x1000);
607 CMCONST("cmStatusListenPend", 0x2000);
608 CMCONST("cmStatusIncomingCallPresent", 0x4000);
609
Jack Jansen55e39271997-10-07 21:47:25 +0000610 ErrorObject = PyErr_NewException("ctb.error", NULL, NULL);
Jack Jansenf5c20571997-01-30 15:48:07 +0000611 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansena755e681997-09-20 17:40:22 +0000612 ctbcmtype.ob_type = &PyType_Type;
613 Py_INCREF(&ctbcmtype);
614 PyDict_SetItemString(d, "CTBConnectionMgrType", (PyObject *)&ctbcmtype);
Jack Jansenee735be1994-12-14 13:31:11 +0000615}