Ticket #78: j78-closed-wiimote-value-error.patch

File j78-closed-wiimote-value-error.patch, 5.9 kB (added by jonaskoelker, 3 years ago)

Improved handling of closed wiimotes

  • trunk/python/Wiimote.c

    diff --git a/trunk/python/Wiimote.c b/trunk/python/Wiimote.c
    index f5bdfc6..8debebc 100644
    a b  
    1515 * 
    1616 * You should have received a copy of the GNU General Public License 
    1717 * along with this program; if not, write to the Free Software 
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor,  
     18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1919 * Boston, MA  02110-1301  USA 
    2020 * 
    2121 * ChangeLog: 
     
    5959#include <errno.h> 
    6060#include <bluetooth/bluetooth.h> 
    6161#include "cwiid.h" 
     62#include "cwiid_internal.h" /* for send_report */ 
    6263 
    6364typedef struct { 
    6465        PyObject_HEAD 
     
    178179        Wiimote_new,                    /* tp_new */ 
    179180}; 
    180181 
     182static void Wiimote_closed_ValueError() { 
     183        PyErr_SetString(PyExc_ValueError, "Wiimote is closed"); 
     184} 
     185 
    181186/* Allocate and deallocate functions */ 
    182187static PyObject * 
    183188        Wiimote_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 
     
    259264 
    260265static PyObject *Wiimote_close(Wiimote *self) 
    261266{ 
     267        if (!self->wiimote) { 
     268                Wiimote_closed_ValueError(); 
     269                return NULL; 
     270        } 
    262271        if (cwiid_close(self->wiimote)) { 
    263272                PyErr_SetString(PyExc_RuntimeError, 
    264273                                "Error closing wiimote connection"); 
     
    275284        static char *kwlist[] = {"flags", NULL}; 
    276285        int flags; 
    277286 
     287        if (!self->wiimote) { 
     288                Wiimote_closed_ValueError(); 
     289                return NULL; 
     290        } 
    278291        if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:cwiid.Wiimote.enable", 
    279292                                         kwlist, &flags)) { 
    280293                return NULL; 
     
    293306        static char *kwlist[] = {"flags", NULL}; 
    294307        int flags; 
    295308 
     309        if (!self->wiimote) { 
     310                Wiimote_closed_ValueError(); 
     311                return NULL; 
     312        } 
    296313        if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:cwiid.Wiimote.disable", 
    297314                                         kwlist, &flags)) { 
    298315                return NULL; 
     
    312329{ 
    313330        PyObject *OldCallback; 
    314331 
     332        if (!self->wiimote) { 
     333                Wiimote_closed_ValueError(); 
     334                return -1; 
     335        } 
    315336        if (!PyCallable_Check(NewCallback)) { 
    316337                PyErr_SetString(PyExc_TypeError, "callback must be callable!"); 
    317338        } 
     
    346367        struct timespec t; 
    347368        PyObject *PyMesg; 
    348369 
     370        if (!self->wiimote) { 
     371                Wiimote_closed_ValueError(); 
     372                return NULL; 
     373        } 
    349374        if (cwiid_get_mesg(self->wiimote, &mesg_count, &mesg, &t)) { 
    350375                if (errno == EAGAIN) { 
    351376                        Py_RETURN_NONE; 
     
    369394        struct cwiid_state state; 
    370395        PyObject *PyState; 
    371396 
     397        if (!self->wiimote) { 
     398                Wiimote_closed_ValueError(); 
     399                return NULL; 
     400        } 
    372401        if (cwiid_get_state(self->wiimote, &state)) { 
    373402                PyErr_SetString(PyExc_IOError, "get state error"); 
    374403                return NULL; 
    375404        } 
     405        if (!self->wiimote) { 
     406                Wiimote_closed_ValueError(); 
     407                return NULL; 
     408        } 
    376409 
    377410        PyState = Py_BuildValue("{s:B,s:B,s:B,s:B,s:i,s:i}", 
    378411                                "rpt_mode", state.rpt_mode, 
     
    539572        struct acc_cal acc_cal; 
    540573        PyObject *PyAccCal; 
    541574 
     575        if (!self->wiimote) { 
     576                Wiimote_closed_ValueError(); 
     577                return NULL; 
     578        } 
    542579        if (!PyArg_ParseTupleAndKeywords(args, kwds, 
    543580                                         "i:cwiid.Wiimote.get_acc_cal", kwlist, 
    544581                                         &ext_type)) { 
     
    575612{ 
    576613        long led; 
    577614 
     615        if (!self->wiimote) { 
     616                Wiimote_closed_ValueError(); 
     617                return -1; 
     618        } 
    578619        if (((led = PyInt_AsLong(PyLed)) == -1) && PyErr_Occurred()) { 
    579620                return -1; 
    580621        } 
     
    593634{ 
    594635        long rumble; 
    595636 
     637        if (!self->wiimote) { 
     638                Wiimote_closed_ValueError(); 
     639                return -1; 
     640        } 
    596641        if (((rumble = PyInt_AsLong(PyRumble)) == -1) && PyErr_Occurred()) { 
    597642                return -1; 
    598643        } 
     
    611656{ 
    612657        long rpt_mode; 
    613658 
     659        if (!self->wiimote) { 
     660                Wiimote_closed_ValueError(); 
     661                return -1; 
     662        } 
    614663        if (((rpt_mode = PyInt_AsLong(PyRptMode)) == -1) && PyErr_Occurred()) { 
    615664                return -1; 
    616665        } 
     
    649698        void *buf; 
    650699        PyObject *pyRetBuf; 
    651700 
     701        if (!self->wiimote) { 
     702                Wiimote_closed_ValueError(); 
     703                return NULL; 
     704        } 
    652705        if (!PyArg_ParseTupleAndKeywords(args, kwds, "BII:cwiid.Wiimote.read", 
    653706                                         kwlist, &flags, &offset, &len)) { 
    654707                return NULL; 
     
    699752        void *buf; 
    700753        int len; 
    701754 
     755        if (!self->wiimote) { 
     756                Wiimote_closed_ValueError(); 
     757                return NULL; 
     758        } 
    702759        if (!PyArg_ParseTupleAndKeywords(args, kwds, "BIt#:cwiid.Wiimote.write", 
    703760                                         kwlist, &flags, &offset, &buf, &len)) { 
    704761                return NULL; 
     
    715772static void CallbackBridge(cwiid_wiimote_t *wiimote, int mesg_count, 
    716773                               union cwiid_mesg mesg[], struct timespec *t) 
    717774{ 
    718         PyObject *ArgTuple, *Time; 
     775        PyObject *ArgTuple; 
    719776        PyObject *PySelf; 
    720777        PyGILState_STATE gstate; 
    721778 
    722779        gstate = PyGILState_Ensure(); 
     780        if (!wiimote) { 
     781                Wiimote_closed_ValueError(); 
     782                PyGILState_Release(gstate); 
     783                return; 
     784        } 
    723785 
    724786        ArgTuple = ConvertMesgArray(mesg_count, mesg); 
    725787 
     
    743805 * 
    744806 * Python callback takes arg (mesgs). The mesgs is a list of 
    745807 * mesg tuples which contain the mesg type and a dict of the arguments. 
    746  *  
     808 * 
    747809 * Ex: 
    748810 * mesgs =>[(cwiid.STATUS_MESG,{"battery":battery,"ext_type":ext_type}), 
    749  *          (cwiid.BTN_MESG,buttons),  
     811 *          (cwiid.BTN_MESG,buttons), 
    750812 *          (cwiid.ACC_MESG,(x,y,z)), 
    751813 *          (cwiid.IR_MESG,[{"pos":(x,y),"size":size}, ...]), 
    752814 *          (cwiid.NUNCHUK_MESG,{"stick":(x,y),"acc":(x,y,z),