blob: f8bc25e7c5a686146e797799fdd0f42c7257dff1 [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
Jack Jansenee735be1994-12-14 13:31:11 +000039
40#define _UnimplementedToolTrap 0xA89F
41#define _CommToolboxTrap 0x8B
42#define _UnimplementedOSTrap 0x9F
43
Jack Jansen377df981997-04-03 14:48:04 +000044static char *errornames[] = {
45 "No Error",
46 "Rejected", /* 1 */
47 "Failed", /* 2 */
48 "Timeout", /* 3 */
49 "Not Open", /* 4 */
50 "Not Closed", /* 5 */
51 "No Request Pending", /* 6 */
52 "Not Supported", /* 7 */
53 "No Tools", /* 8 */
54 "User Cancel", /* 9 */
55 "Error 10", /* 10 */
56 "Unknown Error", /* 11 */
57#define MAX_POS_ERROR 11
58};
59
Jack Jansenf5c20571997-01-30 15:48:07 +000060extern PyObject *PyErr_Mac(PyObject *,int);
Jack Jansenee735be1994-12-14 13:31:11 +000061
Jack Jansen377df981997-04-03 14:48:04 +000062static PyObject *
63PyCtb_Error(PyObject *errobj, int errcode)
64{
65 if ( errcode > 0 && errcode <= MAX_POS_ERROR ) {
66 PyErr_SetString(errobj, errornames[errcode]);
67 return NULL;
68 } else {
69 return PyErr_Mac(errobj, errcode);
70 }
71}
72
Jack Jansenf5c20571997-01-30 15:48:07 +000073static PyObject *ErrorObject;
Jack Jansenee735be1994-12-14 13:31:11 +000074
75typedef struct {
Jack Jansenf5c20571997-01-30 15:48:07 +000076 PyObject_HEAD
Jack Jansenee735be1994-12-14 13:31:11 +000077 ConnHandle hdl; /* The handle to the connection */
Jack Jansenf5c20571997-01-30 15:48:07 +000078 PyObject *callback; /* Python callback routine */
Jack Jansenee735be1994-12-14 13:31:11 +000079 int has_callback; /* True if callback not None */
80 int err; /* Error to pass to the callback */
81} ctbcmobject;
82
Jeremy Hylton938ace62002-07-17 16:30:39 +000083static PyTypeObject ctbcmtype;
Jack Jansenee735be1994-12-14 13:31:11 +000084
85#define is_ctbcmobject(v) ((v)->ob_type == &ctbcmtype)
86
87static
88TrapAvailable(short tNumber, TrapType tType)
89{
90 short unImplemented;
91
92 if (tType == OSTrap)
93 unImplemented = _UnimplementedOSTrap;
94 else
95 unImplemented = _UnimplementedToolTrap;
96
97 return NGetTrapAddress(tNumber, tType) != NGetTrapAddress(unImplemented, tType);
98}
99
100static
101initialize_ctb()
102{
103 OSErr err;
104 static initialized = -1;
105
106 if ( initialized >= 0 )
107 return initialized;
108 initialized = 0;
109
110 if ( !TrapAvailable(_CommToolboxTrap, OSTrap) ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000111 PyErr_SetString(ErrorObject, "CTB not available");
Jack Jansenee735be1994-12-14 13:31:11 +0000112 return 0;
113 }
114 if ( (err=InitCTBUtilities()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000115 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000116 return 0;
117 }
118 if ( (err=InitCRM()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000119 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000120 return 0;
121 }
122 if ( (err=InitCM()) ) {
Jack Jansen377df981997-04-03 14:48:04 +0000123 PyCtb_Error(ErrorObject, (int)err);
Jack Jansenee735be1994-12-14 13:31:11 +0000124 return 0;
125 }
126 initialized = 1;
127 return 1;
128}
129
130static int
131ctbcm_pycallback(arg)
132 void *arg;
133{
134 ctbcmobject *self = (ctbcmobject *)arg;
Jack Jansenf5c20571997-01-30 15:48:07 +0000135 PyObject *args, *rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000136
137 if ( !self->has_callback ) /* It could have been removed in the meantime */
138 return 0;
Jack Jansenf5c20571997-01-30 15:48:07 +0000139 args = Py_BuildValue("(i)", self->err);
140 rv = PyEval_CallObject(self->callback, args);
141 Py_DECREF(args);
Jack Jansenee735be1994-12-14 13:31:11 +0000142 if( rv == NULL )
143 return -1;
Jack Jansenf5c20571997-01-30 15:48:07 +0000144 Py_DECREF(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000145 return 0;
146}
147
148/*DBG*/int ncallback;
149static pascal void
150ctbcm_ctbcallback(hconn)
151 ConnHandle hconn;
152{
153 ctbcmobject *self;
154
155 /* XXXX Do I have to do the A5 mumbo-jumbo? */
156 ncallback++; /*DBG*/
157 self = (ctbcmobject *)CMGetUserData(hconn);
158 self->err = (int)((*hconn)->errCode);
159 Py_AddPendingCall(ctbcm_pycallback, (void *)self);
160}
161
162static ctbcmobject *
163newctbcmobject(arg)
Jack Jansenf5c20571997-01-30 15:48:07 +0000164 PyObject *arg;
Jack Jansenee735be1994-12-14 13:31:11 +0000165{
166 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000167 self = PyObject_NEW(ctbcmobject, &ctbcmtype);
Jack Jansenee735be1994-12-14 13:31:11 +0000168 if (self == NULL)
169 return NULL;
170 self->hdl = NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000171 Py_INCREF(Py_None);
172 self->callback = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000173 self->has_callback = 0;
174 return self;
175}
176
177/* ctbcm methods */
178
179static void
180ctbcm_dealloc(self)
181 ctbcmobject *self;
182{
183 if ( self->hdl ) {
184 (void)CMClose(self->hdl, 0, (ConnectionCompletionUPP)0, 0, 1);
185 /*XXXX Is this safe? */
186 CMDispose(self->hdl);
187 self->hdl = NULL;
188 }
Jack Jansen0e2f7982002-05-22 14:31:48 +0000189 PyObject_DEL(self);
Jack Jansenee735be1994-12-14 13:31:11 +0000190}
191
Jack Jansenf5c20571997-01-30 15:48:07 +0000192static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000193ctbcm_open(self, args)
194 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000195 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000196{
197 long timeout;
198 OSErr err;
199 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
200
Jack Jansenf5c20571997-01-30 15:48:07 +0000201 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000202 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000203 if ( (err=CMOpen(self->hdl, self->has_callback, cb_upp, timeout)) != 0)
204 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000205 Py_INCREF(Py_None);
206 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000207}
208
Jack Jansenf5c20571997-01-30 15:48:07 +0000209static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000210ctbcm_listen(self, args)
211 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000212 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000213{
214 long timeout;
215 OSErr err;
216 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
217
Jack Jansenf5c20571997-01-30 15:48:07 +0000218 if (!PyArg_Parse(args, "l", &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000219 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000220 if ( (err=CMListen(self->hdl,self->has_callback, cb_upp, timeout)) != 0)
221 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000222 Py_INCREF(Py_None);
223 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000224}
225
Jack Jansenf5c20571997-01-30 15:48:07 +0000226static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000227ctbcm_accept(self, args)
228 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000229 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000230{
231 int accept;
232 OSErr err;
233
Jack Jansenf5c20571997-01-30 15:48:07 +0000234 if (!PyArg_Parse(args, "i", &accept))
Jack Jansenee735be1994-12-14 13:31:11 +0000235 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000236 if ( (err=CMAccept(self->hdl, accept)) != 0)
237 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000238 Py_INCREF(Py_None);
239 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000240}
241
Jack Jansenf5c20571997-01-30 15:48:07 +0000242static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000243ctbcm_close(self, args)
244 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000245 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000246{
247 int now;
248 long timeout;
249 OSErr err;
250 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
251
Jack Jansenf5c20571997-01-30 15:48:07 +0000252 if (!PyArg_Parse(args, "(li)", &timeout, &now))
Jack Jansenee735be1994-12-14 13:31:11 +0000253 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000254 if ( (err=CMClose(self->hdl, self->has_callback, cb_upp, timeout, now)) != 0)
255 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000256 Py_INCREF(Py_None);
257 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000258}
259
Jack Jansenf5c20571997-01-30 15:48:07 +0000260static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000261ctbcm_read(self, args)
262 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000263 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000264{
265 long timeout, len;
266 int chan;
267 CMFlags flags;
268 OSErr err;
Jack Jansen377df981997-04-03 14:48:04 +0000269 PyObject *rv, *rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000270 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
271
Jack Jansenf5c20571997-01-30 15:48:07 +0000272 if (!PyArg_Parse(args, "(lil)", &len, &chan, &timeout))
Jack Jansenee735be1994-12-14 13:31:11 +0000273 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000274 if ((rv=PyString_FromStringAndSize(NULL, len)) == NULL)
Jack Jansenee735be1994-12-14 13:31:11 +0000275 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000276 if ((err=CMRead(self->hdl, (Ptr)PyString_AsString(rv), &len, (CMChannel)chan,
Jack Jansen377df981997-04-03 14:48:04 +0000277 self->has_callback, cb_upp, timeout, &flags)) != 0 && err != cmTimeOut)
278 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000279 _PyString_Resize(&rv, len);
Jack Jansen377df981997-04-03 14:48:04 +0000280 rrv = Py_BuildValue("(Oi)", rv, (int)flags);
281 Py_DECREF(rv);
282 return rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000283}
284
Jack Jansenf5c20571997-01-30 15:48:07 +0000285static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000286ctbcm_write(self, args)
287 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000288 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000289{
290 long timeout, len;
291 int chan, ilen, flags;
292 OSErr err;
293 char *buf;
294 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
295
Jack Janseneeccca91997-05-07 15:46:31 +0000296 if (!PyArg_Parse(args, "(s#ili)", &buf, &ilen, &chan, &timeout, &flags))
Jack Jansenee735be1994-12-14 13:31:11 +0000297 return NULL;
298 len = ilen;
299 if ((err=CMWrite(self->hdl, (Ptr)buf, &len, (CMChannel)chan,
Jack Jansen377df981997-04-03 14:48:04 +0000300 self->has_callback, cb_upp, timeout, (CMFlags)flags)) != 0 && err != cmTimeOut)
301 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000302 return PyInt_FromLong((int)len);
Jack Jansenee735be1994-12-14 13:31:11 +0000303}
304
Jack Jansenf5c20571997-01-30 15:48:07 +0000305static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000306ctbcm_status(self, args)
307 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000308 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000309{
310 CMBufferSizes sizes;
311 CMStatFlags flags;
312 OSErr err;
Jack Jansen377df981997-04-03 14:48:04 +0000313 PyObject *rv, *rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000314
Jack Jansenf5c20571997-01-30 15:48:07 +0000315 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000316 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000317 if ((err=CMStatus(self->hdl, sizes, &flags)) != 0)
318 return PyCtb_Error(ErrorObject, (int)err);
Jack Jansenf5c20571997-01-30 15:48:07 +0000319 rv = Py_BuildValue("(llllll)", sizes[0], sizes[1], sizes[2], sizes[3], sizes[4], sizes[5]);
Jack Jansenee735be1994-12-14 13:31:11 +0000320 if ( rv == NULL )
321 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000322 rrv = Py_BuildValue("(Ol)", rv, (long)flags);
323 Py_DECREF(rv);
324 return rrv;
Jack Jansenee735be1994-12-14 13:31:11 +0000325}
326
Jack Jansenf5c20571997-01-30 15:48:07 +0000327static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000328ctbcm_getconfig(self, args)
329 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000330 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000331{
332 char *rv;
333
Jack Jansenf5c20571997-01-30 15:48:07 +0000334 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000335 return NULL;
336 if ((rv=(char *)CMGetConfig(self->hdl)) == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000337 PyErr_SetString(ErrorObject, "CMGetConfig failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000338 return NULL;
339 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000340 return PyString_FromString(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000341}
342
Jack Jansenf5c20571997-01-30 15:48:07 +0000343static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000344ctbcm_setconfig(self, args)
345 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000346 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000347{
348 char *cfg;
Jack Jansen377df981997-04-03 14:48:04 +0000349 short rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000350
Jack Jansenf5c20571997-01-30 15:48:07 +0000351 if (!PyArg_Parse(args, "s", &cfg))
Jack Jansenee735be1994-12-14 13:31:11 +0000352 return NULL;
Jack Jansen377df981997-04-03 14:48:04 +0000353 rv=CMSetConfig(self->hdl, (Ptr)cfg);
354 return PyInt_FromLong((long)rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000355}
356
Jack Jansenf5c20571997-01-30 15:48:07 +0000357static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000358ctbcm_choose(self, args)
359 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000360 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000361{
362 int rv;
363 Point pt;
364
Jack Jansenf5c20571997-01-30 15:48:07 +0000365 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000366 return NULL;
367 pt.v = 40;
368 pt.h = 40;
369 rv=CMChoose(&self->hdl, pt, (ConnectionChooseIdleUPP)0);
Jack Jansenf5c20571997-01-30 15:48:07 +0000370 return PyInt_FromLong(rv);
Jack Jansenee735be1994-12-14 13:31:11 +0000371}
372
Jack Jansenf5c20571997-01-30 15:48:07 +0000373static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000374ctbcm_idle(self, args)
375 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000376 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000377{
Jack Jansenf5c20571997-01-30 15:48:07 +0000378 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000379 return NULL;
380 CMIdle(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000381 Py_INCREF(Py_None);
382 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000383}
384
Jack Jansenf5c20571997-01-30 15:48:07 +0000385static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000386ctbcm_abort(self, args)
387 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000388 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000389{
Jack Jansenf5c20571997-01-30 15:48:07 +0000390 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000391 return NULL;
392 CMAbort(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000393 Py_INCREF(Py_None);
394 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000395}
396
Jack Jansenf5c20571997-01-30 15:48:07 +0000397static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000398ctbcm_reset(self, args)
399 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000400 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000401{
Jack Jansenf5c20571997-01-30 15:48:07 +0000402 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000403 return NULL;
404 CMReset(self->hdl);
Jack Jansenf5c20571997-01-30 15:48:07 +0000405 Py_INCREF(Py_None);
406 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000407}
408
Jack Jansenf5c20571997-01-30 15:48:07 +0000409static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000410ctbcm_break(self, args)
411 ctbcmobject *self;
Jack Jansenf5c20571997-01-30 15:48:07 +0000412 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000413{
414 long duration;
415 ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
416
Jack Jansenf5c20571997-01-30 15:48:07 +0000417 if (!PyArg_Parse(args, "l", &duration))
Jack Jansenee735be1994-12-14 13:31:11 +0000418 return NULL;
419 CMBreak(self->hdl, duration,self->has_callback, cb_upp);
Jack Jansenf5c20571997-01-30 15:48:07 +0000420 Py_INCREF(Py_None);
421 return Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000422}
423
Jack Jansenf5c20571997-01-30 15:48:07 +0000424static struct PyMethodDef ctbcm_methods[] = {
425 {"Open", (PyCFunction)ctbcm_open},
426 {"Close", (PyCFunction)ctbcm_close},
427 {"Read", (PyCFunction)ctbcm_read},
428 {"Write", (PyCFunction)ctbcm_write},
429 {"Status", (PyCFunction)ctbcm_status},
430 {"GetConfig", (PyCFunction)ctbcm_getconfig},
431 {"SetConfig", (PyCFunction)ctbcm_setconfig},
432 {"Choose", (PyCFunction)ctbcm_choose},
433 {"Idle", (PyCFunction)ctbcm_idle},
434 {"Listen", (PyCFunction)ctbcm_listen},
435 {"Accept", (PyCFunction)ctbcm_accept},
436 {"Abort", (PyCFunction)ctbcm_abort},
437 {"Reset", (PyCFunction)ctbcm_reset},
438 {"Break", (PyCFunction)ctbcm_break},
Jack Jansenee735be1994-12-14 13:31:11 +0000439 {NULL, NULL} /* sentinel */
440};
441
Jack Jansenf5c20571997-01-30 15:48:07 +0000442static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000443ctbcm_getattr(self, name)
444 ctbcmobject *self;
445 char *name;
446{
447 if ( strcmp(name, "callback") == 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000448 Py_INCREF(self->callback);
Jack Jansenee735be1994-12-14 13:31:11 +0000449 return self->callback;
450 }
Jack Jansenf5c20571997-01-30 15:48:07 +0000451 return Py_FindMethod(ctbcm_methods, (PyObject *)self, name);
Jack Jansenee735be1994-12-14 13:31:11 +0000452}
453
454static int
455ctbcm_setattr(self, name, v)
456 ctbcmobject *self;
457 char *name;
Jack Jansenf5c20571997-01-30 15:48:07 +0000458 PyObject *v;
Jack Jansenee735be1994-12-14 13:31:11 +0000459{
460 if ( strcmp(name, "callback") != 0 ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000461 PyErr_SetString(PyExc_AttributeError, "ctbcm objects have callback attr only");
Jack Jansenee735be1994-12-14 13:31:11 +0000462 return -1;
463 }
464 if ( v == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000465 v = Py_None;
Jack Jansenee735be1994-12-14 13:31:11 +0000466 }
Jack Jansen377df981997-04-03 14:48:04 +0000467 Py_INCREF(v);
Jack Jansenee735be1994-12-14 13:31:11 +0000468 self->callback = v;
Jack Jansenf5c20571997-01-30 15:48:07 +0000469 self->has_callback = (v != Py_None);
Jack Jansenee735be1994-12-14 13:31:11 +0000470 return 0;
471}
472
Jack Jansenf5c20571997-01-30 15:48:07 +0000473statichere PyTypeObject ctbcmtype = {
474 PyObject_HEAD_INIT(&PyType_Type)
Jack Jansenee735be1994-12-14 13:31:11 +0000475 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000476 "ctb.CTBConnectionMgr", /*tp_name*/
Jack Jansenee735be1994-12-14 13:31:11 +0000477 sizeof(ctbcmobject), /*tp_basicsize*/
478 0, /*tp_itemsize*/
479 /* methods */
480 (destructor)ctbcm_dealloc, /*tp_dealloc*/
481 0, /*tp_print*/
482 (getattrfunc)ctbcm_getattr, /*tp_getattr*/
483 (setattrfunc)ctbcm_setattr, /*tp_setattr*/
484 0, /*tp_compare*/
485 0, /*tp_repr*/
486 0, /*tp_as_number*/
487 0, /*tp_as_sequence*/
488 0, /*tp_as_mapping*/
489 0, /*tp_hash*/
490};
491/* --------------------------------------------------------------------- */
492
493/* Function of no arguments returning new ctbcm object */
494
Jack Jansenf5c20571997-01-30 15:48:07 +0000495static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000496ctb_cmnew(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000497 PyObject *self; /* Not used */
498 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000499{
500 int strlen;
Jack Jansenf5c20571997-01-30 15:48:07 +0000501 PyObject *sizes_obj;
Jack Jansenee735be1994-12-14 13:31:11 +0000502 char *c_str;
503 unsigned char p_str[255];
504 CMBufferSizes sizes;
505 short procid;
506 ConnHandle hdl;
507 ctbcmobject *rv;
508
Jack Jansenf5c20571997-01-30 15:48:07 +0000509 if (!PyArg_Parse(args, "(s#O)", &c_str, &strlen, &sizes_obj))
Jack Jansenee735be1994-12-14 13:31:11 +0000510 return NULL;
511 strncpy((char *)p_str+1, c_str, strlen);
512 p_str[0] = strlen;
513 if (!initialize_ctb())
514 return NULL;
Jack Jansenf5c20571997-01-30 15:48:07 +0000515 if ( sizes_obj == Py_None ) {
Jack Jansenee735be1994-12-14 13:31:11 +0000516 memset(sizes, '\0', sizeof sizes);
517 } else {
Jack Jansenf5c20571997-01-30 15:48:07 +0000518 if ( !PyArg_Parse(sizes_obj, "(llllll)", &sizes[0], &sizes[1], &sizes[2],
Jack Jansenee735be1994-12-14 13:31:11 +0000519 &sizes[3], &sizes[4], &sizes[5]))
520 return NULL;
521 }
522 if ( (procid=CMGetProcID(p_str)) < 0 )
Jack Jansen377df981997-04-03 14:48:04 +0000523 return PyCtb_Error(ErrorObject, procid);
Jack Jansenee735be1994-12-14 13:31:11 +0000524 hdl = CMNew(procid, cmNoMenus|cmQuiet, sizes, 0, 0);
525 if ( hdl == NULL ) {
Jack Jansenf5c20571997-01-30 15:48:07 +0000526 PyErr_SetString(ErrorObject, "CMNew failed");
Jack Jansenee735be1994-12-14 13:31:11 +0000527 return NULL;
528 }
529 rv = newctbcmobject(args);
530 if ( rv == NULL )
531 return NULL; /* XXXX Should dispose of hdl */
532 rv->hdl = hdl;
533 CMSetUserData(hdl, (long)rv);
Jack Jansenf5c20571997-01-30 15:48:07 +0000534 return (PyObject *)rv;
Jack Jansenee735be1994-12-14 13:31:11 +0000535}
536
Jack Jansenf5c20571997-01-30 15:48:07 +0000537static PyObject *
Jack Jansenee735be1994-12-14 13:31:11 +0000538ctb_available(self, args)
Jack Jansenf5c20571997-01-30 15:48:07 +0000539 PyObject *self;
540 PyObject *args;
Jack Jansenee735be1994-12-14 13:31:11 +0000541{
542 int ok;
543
Jack Jansenf5c20571997-01-30 15:48:07 +0000544 if (!PyArg_NoArgs(args))
Jack Jansenee735be1994-12-14 13:31:11 +0000545 return NULL;
546 ok = initialize_ctb();
Jack Jansenf5c20571997-01-30 15:48:07 +0000547 PyErr_Clear();
548 return PyInt_FromLong(ok);
Jack Jansenee735be1994-12-14 13:31:11 +0000549}
550
551/* List of functions defined in the module */
552
Jack Jansenf5c20571997-01-30 15:48:07 +0000553static struct PyMethodDef ctb_methods[] = {
Jack Jansenee735be1994-12-14 13:31:11 +0000554 {"CMNew", ctb_cmnew},
555 {"available", ctb_available},
556 {NULL, NULL} /* sentinel */
557};
558
559
560/* Initialization function for the module (*must* be called initctb) */
561
562void
563initctb()
564{
Jack Jansenf5c20571997-01-30 15:48:07 +0000565 PyObject *m, *d, *o;
Jack Jansenee735be1994-12-14 13:31:11 +0000566
567 /* Create the module and add the functions */
Jack Jansenf5c20571997-01-30 15:48:07 +0000568 m = Py_InitModule("ctb", ctb_methods);
Jack Jansenee735be1994-12-14 13:31:11 +0000569
570 /* Add some symbolic constants to the module */
Jack Jansenf5c20571997-01-30 15:48:07 +0000571 d = PyModule_GetDict(m);
Jack Jansenee735be1994-12-14 13:31:11 +0000572
Jack Jansenf5c20571997-01-30 15:48:07 +0000573#define CMCONST(name, value) o = PyInt_FromLong(value); PyDict_SetItemString(d, name, o)
Jack Jansenee735be1994-12-14 13:31:11 +0000574
575 CMCONST("cmData", 1);
576 CMCONST("cmCntl", 2);
577 CMCONST("cmAttn", 3);
578
579 CMCONST("cmFlagsEOM", 1);
580
581 CMCONST("chooseDisaster", -2);
582 CMCONST("chooseFailed", -1);
583 CMCONST("chooseAborted", 0);
584 CMCONST("chooseOKMinor", 1);
585 CMCONST("chooseOKMajor", 2);
586 CMCONST("chooseCancel", 3);
587
588 CMCONST("cmStatusOpening", 1);
589 CMCONST("cmStatusOpen", 2);
590 CMCONST("cmStatusClosing", 4);
591 CMCONST("cmStatusDataAvail", 8);
592 CMCONST("cmStatusCntlAvail", 0x10);
593 CMCONST("cmStatusAttnAvail", 0x20);
594 CMCONST("cmStatusDRPend", 0x40);
595 CMCONST("cmStatusDWPend", 0x80);
596 CMCONST("cmStatusCWPend", 0x100);
597 CMCONST("cmStatusCWPend", 0x200);
598 CMCONST("cmStatusARPend", 0x400);
599 CMCONST("cmStatusAWPend", 0x800);
600 CMCONST("cmStatusBreakPending", 0x1000);
601 CMCONST("cmStatusListenPend", 0x2000);
602 CMCONST("cmStatusIncomingCallPresent", 0x4000);
603
Jack Jansen55e39271997-10-07 21:47:25 +0000604 ErrorObject = PyErr_NewException("ctb.error", NULL, NULL);
Jack Jansenf5c20571997-01-30 15:48:07 +0000605 PyDict_SetItemString(d, "error", ErrorObject);
Jack Jansena755e681997-09-20 17:40:22 +0000606 ctbcmtype.ob_type = &PyType_Type;
607 Py_INCREF(&ctbcmtype);
608 PyDict_SetItemString(d, "CTBConnectionMgrType", (PyObject *)&ctbcmtype);
Jack Jansenee735be1994-12-14 13:31:11 +0000609}