1994-12-14 09:31:11 -04:00
|
|
|
/***********************************************************
|
1997-01-31 12:15:11 -04:00
|
|
|
Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
|
1995-02-21 16:58:53 -04:00
|
|
|
The Netherlands.
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Note: This file is partially converted to the new naming standard */
|
|
|
|
/* ctbcm objects */
|
|
|
|
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
#include "Python.h"
|
1994-12-14 09:31:11 -04:00
|
|
|
|
1995-02-21 16:58:53 -04:00
|
|
|
#include "macglue.h"
|
|
|
|
|
1994-12-14 09:31:11 -04:00
|
|
|
#include <CommResources.h>
|
|
|
|
#include <Connections.h>
|
1998-02-20 11:55:53 -04:00
|
|
|
#include <CTBUtilities.h>
|
1994-12-14 09:31:11 -04:00
|
|
|
#include <ToolUtils.h>
|
|
|
|
#include <OSUtils.h>
|
1995-02-21 16:58:53 -04:00
|
|
|
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
#define _UnimplementedToolTrap 0xA89F
|
|
|
|
#define _CommToolboxTrap 0x8B
|
|
|
|
#define _UnimplementedOSTrap 0x9F
|
|
|
|
|
1997-04-03 10:48:04 -04:00
|
|
|
static char *errornames[] = {
|
|
|
|
"No Error",
|
|
|
|
"Rejected", /* 1 */
|
|
|
|
"Failed", /* 2 */
|
|
|
|
"Timeout", /* 3 */
|
|
|
|
"Not Open", /* 4 */
|
|
|
|
"Not Closed", /* 5 */
|
|
|
|
"No Request Pending", /* 6 */
|
|
|
|
"Not Supported", /* 7 */
|
|
|
|
"No Tools", /* 8 */
|
|
|
|
"User Cancel", /* 9 */
|
|
|
|
"Error 10", /* 10 */
|
|
|
|
"Unknown Error", /* 11 */
|
|
|
|
#define MAX_POS_ERROR 11
|
|
|
|
};
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
extern PyObject *PyErr_Mac(PyObject *,int);
|
1994-12-14 09:31:11 -04:00
|
|
|
|
1997-04-03 10:48:04 -04:00
|
|
|
static PyObject *
|
|
|
|
PyCtb_Error(PyObject *errobj, int errcode)
|
|
|
|
{
|
|
|
|
if ( errcode > 0 && errcode <= MAX_POS_ERROR ) {
|
|
|
|
PyErr_SetString(errobj, errornames[errcode]);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return PyErr_Mac(errobj, errcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *ErrorObject;
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
typedef struct {
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject_HEAD
|
1994-12-14 09:31:11 -04:00
|
|
|
ConnHandle hdl; /* The handle to the connection */
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *callback; /* Python callback routine */
|
1994-12-14 09:31:11 -04:00
|
|
|
int has_callback; /* True if callback not None */
|
|
|
|
int err; /* Error to pass to the callback */
|
|
|
|
} ctbcmobject;
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
staticforward PyTypeObject ctbcmtype;
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
#define is_ctbcmobject(v) ((v)->ob_type == &ctbcmtype)
|
|
|
|
|
|
|
|
static
|
|
|
|
TrapAvailable(short tNumber, TrapType tType)
|
|
|
|
{
|
|
|
|
short unImplemented;
|
|
|
|
|
|
|
|
if (tType == OSTrap)
|
|
|
|
unImplemented = _UnimplementedOSTrap;
|
|
|
|
else
|
|
|
|
unImplemented = _UnimplementedToolTrap;
|
|
|
|
|
|
|
|
return NGetTrapAddress(tNumber, tType) != NGetTrapAddress(unImplemented, tType);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
initialize_ctb()
|
|
|
|
{
|
|
|
|
OSErr err;
|
|
|
|
static initialized = -1;
|
|
|
|
|
|
|
|
if ( initialized >= 0 )
|
|
|
|
return initialized;
|
|
|
|
initialized = 0;
|
|
|
|
|
|
|
|
if ( !TrapAvailable(_CommToolboxTrap, OSTrap) ) {
|
1997-01-30 11:48:07 -04:00
|
|
|
PyErr_SetString(ErrorObject, "CTB not available");
|
1994-12-14 09:31:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( (err=InitCTBUtilities()) ) {
|
1997-04-03 10:48:04 -04:00
|
|
|
PyCtb_Error(ErrorObject, (int)err);
|
1994-12-14 09:31:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( (err=InitCRM()) ) {
|
1997-04-03 10:48:04 -04:00
|
|
|
PyCtb_Error(ErrorObject, (int)err);
|
1994-12-14 09:31:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( (err=InitCM()) ) {
|
1997-04-03 10:48:04 -04:00
|
|
|
PyCtb_Error(ErrorObject, (int)err);
|
1994-12-14 09:31:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
initialized = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ctbcm_pycallback(arg)
|
|
|
|
void *arg;
|
|
|
|
{
|
|
|
|
ctbcmobject *self = (ctbcmobject *)arg;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args, *rv;
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
if ( !self->has_callback ) /* It could have been removed in the meantime */
|
|
|
|
return 0;
|
1997-01-30 11:48:07 -04:00
|
|
|
args = Py_BuildValue("(i)", self->err);
|
|
|
|
rv = PyEval_CallObject(self->callback, args);
|
|
|
|
Py_DECREF(args);
|
1994-12-14 09:31:11 -04:00
|
|
|
if( rv == NULL )
|
|
|
|
return -1;
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_DECREF(rv);
|
1994-12-14 09:31:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*DBG*/int ncallback;
|
|
|
|
static pascal void
|
|
|
|
ctbcm_ctbcallback(hconn)
|
|
|
|
ConnHandle hconn;
|
|
|
|
{
|
|
|
|
ctbcmobject *self;
|
|
|
|
|
|
|
|
/* XXXX Do I have to do the A5 mumbo-jumbo? */
|
|
|
|
ncallback++; /*DBG*/
|
|
|
|
self = (ctbcmobject *)CMGetUserData(hconn);
|
|
|
|
self->err = (int)((*hconn)->errCode);
|
|
|
|
Py_AddPendingCall(ctbcm_pycallback, (void *)self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ctbcmobject *
|
|
|
|
newctbcmobject(arg)
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *arg;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
self = PyObject_NEW(ctbcmobject, &ctbcmtype);
|
1994-12-14 09:31:11 -04:00
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
self->hdl = NULL;
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
self->callback = Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
self->has_callback = 0;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ctbcm methods */
|
|
|
|
|
|
|
|
static void
|
|
|
|
ctbcm_dealloc(self)
|
|
|
|
ctbcmobject *self;
|
|
|
|
{
|
|
|
|
if ( self->hdl ) {
|
|
|
|
(void)CMClose(self->hdl, 0, (ConnectionCompletionUPP)0, 0, 1);
|
|
|
|
/*XXXX Is this safe? */
|
|
|
|
CMDispose(self->hdl);
|
|
|
|
self->hdl = NULL;
|
|
|
|
}
|
2002-05-22 11:31:48 -03:00
|
|
|
PyObject_DEL(self);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_open(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
long timeout;
|
|
|
|
OSErr err;
|
|
|
|
ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "l", &timeout))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-04-03 10:48:04 -04:00
|
|
|
if ( (err=CMOpen(self->hdl, self->has_callback, cb_upp, timeout)) != 0)
|
|
|
|
return PyCtb_Error(ErrorObject, (int)err);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_listen(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
long timeout;
|
|
|
|
OSErr err;
|
|
|
|
ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "l", &timeout))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-04-03 10:48:04 -04:00
|
|
|
if ( (err=CMListen(self->hdl,self->has_callback, cb_upp, timeout)) != 0)
|
|
|
|
return PyCtb_Error(ErrorObject, (int)err);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_accept(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
int accept;
|
|
|
|
OSErr err;
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "i", &accept))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-04-03 10:48:04 -04:00
|
|
|
if ( (err=CMAccept(self->hdl, accept)) != 0)
|
|
|
|
return PyCtb_Error(ErrorObject, (int)err);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_close(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
int now;
|
|
|
|
long timeout;
|
|
|
|
OSErr err;
|
|
|
|
ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "(li)", &timeout, &now))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-04-03 10:48:04 -04:00
|
|
|
if ( (err=CMClose(self->hdl, self->has_callback, cb_upp, timeout, now)) != 0)
|
|
|
|
return PyCtb_Error(ErrorObject, (int)err);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_read(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
long timeout, len;
|
|
|
|
int chan;
|
|
|
|
CMFlags flags;
|
|
|
|
OSErr err;
|
1997-04-03 10:48:04 -04:00
|
|
|
PyObject *rv, *rrv;
|
1994-12-14 09:31:11 -04:00
|
|
|
ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "(lil)", &len, &chan, &timeout))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-01-30 11:48:07 -04:00
|
|
|
if ((rv=PyString_FromStringAndSize(NULL, len)) == NULL)
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-01-30 11:48:07 -04:00
|
|
|
if ((err=CMRead(self->hdl, (Ptr)PyString_AsString(rv), &len, (CMChannel)chan,
|
1997-04-03 10:48:04 -04:00
|
|
|
self->has_callback, cb_upp, timeout, &flags)) != 0 && err != cmTimeOut)
|
|
|
|
return PyCtb_Error(ErrorObject, (int)err);
|
1997-01-30 11:48:07 -04:00
|
|
|
_PyString_Resize(&rv, len);
|
1997-04-03 10:48:04 -04:00
|
|
|
rrv = Py_BuildValue("(Oi)", rv, (int)flags);
|
|
|
|
Py_DECREF(rv);
|
|
|
|
return rrv;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_write(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
long timeout, len;
|
|
|
|
int chan, ilen, flags;
|
|
|
|
OSErr err;
|
|
|
|
char *buf;
|
|
|
|
ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
|
|
|
|
|
1997-05-07 12:46:31 -03:00
|
|
|
if (!PyArg_Parse(args, "(s#ili)", &buf, &ilen, &chan, &timeout, &flags))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
len = ilen;
|
|
|
|
if ((err=CMWrite(self->hdl, (Ptr)buf, &len, (CMChannel)chan,
|
1997-04-03 10:48:04 -04:00
|
|
|
self->has_callback, cb_upp, timeout, (CMFlags)flags)) != 0 && err != cmTimeOut)
|
|
|
|
return PyCtb_Error(ErrorObject, (int)err);
|
1997-01-30 11:48:07 -04:00
|
|
|
return PyInt_FromLong((int)len);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_status(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
CMBufferSizes sizes;
|
|
|
|
CMStatFlags flags;
|
|
|
|
OSErr err;
|
1997-04-03 10:48:04 -04:00
|
|
|
PyObject *rv, *rrv;
|
1994-12-14 09:31:11 -04:00
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-04-03 10:48:04 -04:00
|
|
|
if ((err=CMStatus(self->hdl, sizes, &flags)) != 0)
|
|
|
|
return PyCtb_Error(ErrorObject, (int)err);
|
1997-01-30 11:48:07 -04:00
|
|
|
rv = Py_BuildValue("(llllll)", sizes[0], sizes[1], sizes[2], sizes[3], sizes[4], sizes[5]);
|
1994-12-14 09:31:11 -04:00
|
|
|
if ( rv == NULL )
|
|
|
|
return NULL;
|
1997-04-03 10:48:04 -04:00
|
|
|
rrv = Py_BuildValue("(Ol)", rv, (long)flags);
|
|
|
|
Py_DECREF(rv);
|
|
|
|
return rrv;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_getconfig(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
char *rv;
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
if ((rv=(char *)CMGetConfig(self->hdl)) == NULL ) {
|
1997-01-30 11:48:07 -04:00
|
|
|
PyErr_SetString(ErrorObject, "CMGetConfig failed");
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-30 11:48:07 -04:00
|
|
|
return PyString_FromString(rv);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_setconfig(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
char *cfg;
|
1997-04-03 10:48:04 -04:00
|
|
|
short rv;
|
1994-12-14 09:31:11 -04:00
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "s", &cfg))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
1997-04-03 10:48:04 -04:00
|
|
|
rv=CMSetConfig(self->hdl, (Ptr)cfg);
|
|
|
|
return PyInt_FromLong((long)rv);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_choose(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
Point pt;
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
pt.v = 40;
|
|
|
|
pt.h = 40;
|
|
|
|
rv=CMChoose(&self->hdl, pt, (ConnectionChooseIdleUPP)0);
|
1997-01-30 11:48:07 -04:00
|
|
|
return PyInt_FromLong(rv);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_idle(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
CMIdle(self->hdl);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_abort(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
CMAbort(self->hdl);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_reset(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
CMReset(self->hdl);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_break(self, args)
|
|
|
|
ctbcmobject *self;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
long duration;
|
|
|
|
ConnectionCompletionUPP cb_upp = NewConnectionCompletionProc(ctbcm_ctbcallback);
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "l", &duration))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
CMBreak(self->hdl, duration,self->has_callback, cb_upp);
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static struct PyMethodDef ctbcm_methods[] = {
|
|
|
|
{"Open", (PyCFunction)ctbcm_open},
|
|
|
|
{"Close", (PyCFunction)ctbcm_close},
|
|
|
|
{"Read", (PyCFunction)ctbcm_read},
|
|
|
|
{"Write", (PyCFunction)ctbcm_write},
|
|
|
|
{"Status", (PyCFunction)ctbcm_status},
|
|
|
|
{"GetConfig", (PyCFunction)ctbcm_getconfig},
|
|
|
|
{"SetConfig", (PyCFunction)ctbcm_setconfig},
|
|
|
|
{"Choose", (PyCFunction)ctbcm_choose},
|
|
|
|
{"Idle", (PyCFunction)ctbcm_idle},
|
|
|
|
{"Listen", (PyCFunction)ctbcm_listen},
|
|
|
|
{"Accept", (PyCFunction)ctbcm_accept},
|
|
|
|
{"Abort", (PyCFunction)ctbcm_abort},
|
|
|
|
{"Reset", (PyCFunction)ctbcm_reset},
|
|
|
|
{"Break", (PyCFunction)ctbcm_break},
|
1994-12-14 09:31:11 -04:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctbcm_getattr(self, name)
|
|
|
|
ctbcmobject *self;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
if ( strcmp(name, "callback") == 0 ) {
|
1997-01-30 11:48:07 -04:00
|
|
|
Py_INCREF(self->callback);
|
1994-12-14 09:31:11 -04:00
|
|
|
return self->callback;
|
|
|
|
}
|
1997-01-30 11:48:07 -04:00
|
|
|
return Py_FindMethod(ctbcm_methods, (PyObject *)self, name);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ctbcm_setattr(self, name, v)
|
|
|
|
ctbcmobject *self;
|
|
|
|
char *name;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *v;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
if ( strcmp(name, "callback") != 0 ) {
|
1997-01-30 11:48:07 -04:00
|
|
|
PyErr_SetString(PyExc_AttributeError, "ctbcm objects have callback attr only");
|
1994-12-14 09:31:11 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( v == NULL ) {
|
1997-01-30 11:48:07 -04:00
|
|
|
v = Py_None;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
1997-04-03 10:48:04 -04:00
|
|
|
Py_INCREF(v);
|
1994-12-14 09:31:11 -04:00
|
|
|
self->callback = v;
|
1997-01-30 11:48:07 -04:00
|
|
|
self->has_callback = (v != Py_None);
|
1994-12-14 09:31:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
statichere PyTypeObject ctbcmtype = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1994-12-14 09:31:11 -04:00
|
|
|
0, /*ob_size*/
|
2001-12-08 14:02:58 -04:00
|
|
|
"ctb.CTBConnectionMgr", /*tp_name*/
|
1994-12-14 09:31:11 -04:00
|
|
|
sizeof(ctbcmobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)ctbcm_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)ctbcm_getattr, /*tp_getattr*/
|
|
|
|
(setattrfunc)ctbcm_setattr, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
|
|
|
};
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Function of no arguments returning new ctbcm object */
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctb_cmnew(self, args)
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *self; /* Not used */
|
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
int strlen;
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *sizes_obj;
|
1994-12-14 09:31:11 -04:00
|
|
|
char *c_str;
|
|
|
|
unsigned char p_str[255];
|
|
|
|
CMBufferSizes sizes;
|
|
|
|
short procid;
|
|
|
|
ConnHandle hdl;
|
|
|
|
ctbcmobject *rv;
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_Parse(args, "(s#O)", &c_str, &strlen, &sizes_obj))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
strncpy((char *)p_str+1, c_str, strlen);
|
|
|
|
p_str[0] = strlen;
|
|
|
|
if (!initialize_ctb())
|
|
|
|
return NULL;
|
1997-01-30 11:48:07 -04:00
|
|
|
if ( sizes_obj == Py_None ) {
|
1994-12-14 09:31:11 -04:00
|
|
|
memset(sizes, '\0', sizeof sizes);
|
|
|
|
} else {
|
1997-01-30 11:48:07 -04:00
|
|
|
if ( !PyArg_Parse(sizes_obj, "(llllll)", &sizes[0], &sizes[1], &sizes[2],
|
1994-12-14 09:31:11 -04:00
|
|
|
&sizes[3], &sizes[4], &sizes[5]))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ( (procid=CMGetProcID(p_str)) < 0 )
|
1997-04-03 10:48:04 -04:00
|
|
|
return PyCtb_Error(ErrorObject, procid);
|
1994-12-14 09:31:11 -04:00
|
|
|
hdl = CMNew(procid, cmNoMenus|cmQuiet, sizes, 0, 0);
|
|
|
|
if ( hdl == NULL ) {
|
1997-01-30 11:48:07 -04:00
|
|
|
PyErr_SetString(ErrorObject, "CMNew failed");
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rv = newctbcmobject(args);
|
|
|
|
if ( rv == NULL )
|
|
|
|
return NULL; /* XXXX Should dispose of hdl */
|
|
|
|
rv->hdl = hdl;
|
|
|
|
CMSetUserData(hdl, (long)rv);
|
1997-01-30 11:48:07 -04:00
|
|
|
return (PyObject *)rv;
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static PyObject *
|
1994-12-14 09:31:11 -04:00
|
|
|
ctb_available(self, args)
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1994-12-14 09:31:11 -04:00
|
|
|
{
|
|
|
|
int ok;
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
if (!PyArg_NoArgs(args))
|
1994-12-14 09:31:11 -04:00
|
|
|
return NULL;
|
|
|
|
ok = initialize_ctb();
|
1997-01-30 11:48:07 -04:00
|
|
|
PyErr_Clear();
|
|
|
|
return PyInt_FromLong(ok);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* List of functions defined in the module */
|
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
static struct PyMethodDef ctb_methods[] = {
|
1994-12-14 09:31:11 -04:00
|
|
|
{"CMNew", ctb_cmnew},
|
|
|
|
{"available", ctb_available},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialization function for the module (*must* be called initctb) */
|
|
|
|
|
|
|
|
void
|
|
|
|
initctb()
|
|
|
|
{
|
1997-01-30 11:48:07 -04:00
|
|
|
PyObject *m, *d, *o;
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
1997-01-30 11:48:07 -04:00
|
|
|
m = Py_InitModule("ctb", ctb_methods);
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
1997-01-30 11:48:07 -04:00
|
|
|
d = PyModule_GetDict(m);
|
1994-12-14 09:31:11 -04:00
|
|
|
|
1997-01-30 11:48:07 -04:00
|
|
|
#define CMCONST(name, value) o = PyInt_FromLong(value); PyDict_SetItemString(d, name, o)
|
1994-12-14 09:31:11 -04:00
|
|
|
|
|
|
|
CMCONST("cmData", 1);
|
|
|
|
CMCONST("cmCntl", 2);
|
|
|
|
CMCONST("cmAttn", 3);
|
|
|
|
|
|
|
|
CMCONST("cmFlagsEOM", 1);
|
|
|
|
|
|
|
|
CMCONST("chooseDisaster", -2);
|
|
|
|
CMCONST("chooseFailed", -1);
|
|
|
|
CMCONST("chooseAborted", 0);
|
|
|
|
CMCONST("chooseOKMinor", 1);
|
|
|
|
CMCONST("chooseOKMajor", 2);
|
|
|
|
CMCONST("chooseCancel", 3);
|
|
|
|
|
|
|
|
CMCONST("cmStatusOpening", 1);
|
|
|
|
CMCONST("cmStatusOpen", 2);
|
|
|
|
CMCONST("cmStatusClosing", 4);
|
|
|
|
CMCONST("cmStatusDataAvail", 8);
|
|
|
|
CMCONST("cmStatusCntlAvail", 0x10);
|
|
|
|
CMCONST("cmStatusAttnAvail", 0x20);
|
|
|
|
CMCONST("cmStatusDRPend", 0x40);
|
|
|
|
CMCONST("cmStatusDWPend", 0x80);
|
|
|
|
CMCONST("cmStatusCWPend", 0x100);
|
|
|
|
CMCONST("cmStatusCWPend", 0x200);
|
|
|
|
CMCONST("cmStatusARPend", 0x400);
|
|
|
|
CMCONST("cmStatusAWPend", 0x800);
|
|
|
|
CMCONST("cmStatusBreakPending", 0x1000);
|
|
|
|
CMCONST("cmStatusListenPend", 0x2000);
|
|
|
|
CMCONST("cmStatusIncomingCallPresent", 0x4000);
|
|
|
|
|
1997-10-07 18:47:25 -03:00
|
|
|
ErrorObject = PyErr_NewException("ctb.error", NULL, NULL);
|
1997-01-30 11:48:07 -04:00
|
|
|
PyDict_SetItemString(d, "error", ErrorObject);
|
1997-09-20 14:40:22 -03:00
|
|
|
ctbcmtype.ob_type = &PyType_Type;
|
|
|
|
Py_INCREF(&ctbcmtype);
|
|
|
|
PyDict_SetItemString(d, "CTBConnectionMgrType", (PyObject *)&ctbcmtype);
|
1994-12-14 09:31:11 -04:00
|
|
|
}
|