diff --git a/trunk/python/Wiimote.c b/trunk/python/Wiimote.c
index f5bdfc6..8debebc 100644
|
a
|
b
|
|
| 15 | 15 | * |
| 16 | 16 | * You should have received a copy of the GNU General Public License |
| 17 | 17 | * 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, |
| 19 | 19 | * Boston, MA 02110-1301 USA |
| 20 | 20 | * |
| 21 | 21 | * ChangeLog: |
| … |
… |
|
| 59 | 59 | #include <errno.h> |
| 60 | 60 | #include <bluetooth/bluetooth.h> |
| 61 | 61 | #include "cwiid.h" |
| | 62 | #include "cwiid_internal.h" /* for send_report */ |
| 62 | 63 | |
| 63 | 64 | typedef struct { |
| 64 | 65 | PyObject_HEAD |
| … |
… |
|
| 178 | 179 | Wiimote_new, /* tp_new */ |
| 179 | 180 | }; |
| 180 | 181 | |
| | 182 | static void Wiimote_closed_ValueError() { |
| | 183 | PyErr_SetString(PyExc_ValueError, "Wiimote is closed"); |
| | 184 | } |
| | 185 | |
| 181 | 186 | /* Allocate and deallocate functions */ |
| 182 | 187 | static PyObject * |
| 183 | 188 | Wiimote_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| … |
… |
|
| 259 | 264 | |
| 260 | 265 | static PyObject *Wiimote_close(Wiimote *self) |
| 261 | 266 | { |
| | 267 | if (!self->wiimote) { |
| | 268 | Wiimote_closed_ValueError(); |
| | 269 | return NULL; |
| | 270 | } |
| 262 | 271 | if (cwiid_close(self->wiimote)) { |
| 263 | 272 | PyErr_SetString(PyExc_RuntimeError, |
| 264 | 273 | "Error closing wiimote connection"); |
| … |
… |
|
| 275 | 284 | static char *kwlist[] = {"flags", NULL}; |
| 276 | 285 | int flags; |
| 277 | 286 | |
| | 287 | if (!self->wiimote) { |
| | 288 | Wiimote_closed_ValueError(); |
| | 289 | return NULL; |
| | 290 | } |
| 278 | 291 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:cwiid.Wiimote.enable", |
| 279 | 292 | kwlist, &flags)) { |
| 280 | 293 | return NULL; |
| … |
… |
|
| 293 | 306 | static char *kwlist[] = {"flags", NULL}; |
| 294 | 307 | int flags; |
| 295 | 308 | |
| | 309 | if (!self->wiimote) { |
| | 310 | Wiimote_closed_ValueError(); |
| | 311 | return NULL; |
| | 312 | } |
| 296 | 313 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:cwiid.Wiimote.disable", |
| 297 | 314 | kwlist, &flags)) { |
| 298 | 315 | return NULL; |
| … |
… |
|
| 312 | 329 | { |
| 313 | 330 | PyObject *OldCallback; |
| 314 | 331 | |
| | 332 | if (!self->wiimote) { |
| | 333 | Wiimote_closed_ValueError(); |
| | 334 | return -1; |
| | 335 | } |
| 315 | 336 | if (!PyCallable_Check(NewCallback)) { |
| 316 | 337 | PyErr_SetString(PyExc_TypeError, "callback must be callable!"); |
| 317 | 338 | } |
| … |
… |
|
| 346 | 367 | struct timespec t; |
| 347 | 368 | PyObject *PyMesg; |
| 348 | 369 | |
| | 370 | if (!self->wiimote) { |
| | 371 | Wiimote_closed_ValueError(); |
| | 372 | return NULL; |
| | 373 | } |
| 349 | 374 | if (cwiid_get_mesg(self->wiimote, &mesg_count, &mesg, &t)) { |
| 350 | 375 | if (errno == EAGAIN) { |
| 351 | 376 | Py_RETURN_NONE; |
| … |
… |
|
| 369 | 394 | struct cwiid_state state; |
| 370 | 395 | PyObject *PyState; |
| 371 | 396 | |
| | 397 | if (!self->wiimote) { |
| | 398 | Wiimote_closed_ValueError(); |
| | 399 | return NULL; |
| | 400 | } |
| 372 | 401 | if (cwiid_get_state(self->wiimote, &state)) { |
| 373 | 402 | PyErr_SetString(PyExc_IOError, "get state error"); |
| 374 | 403 | return NULL; |
| 375 | 404 | } |
| | 405 | if (!self->wiimote) { |
| | 406 | Wiimote_closed_ValueError(); |
| | 407 | return NULL; |
| | 408 | } |
| 376 | 409 | |
| 377 | 410 | PyState = Py_BuildValue("{s:B,s:B,s:B,s:B,s:i,s:i}", |
| 378 | 411 | "rpt_mode", state.rpt_mode, |
| … |
… |
|
| 539 | 572 | struct acc_cal acc_cal; |
| 540 | 573 | PyObject *PyAccCal; |
| 541 | 574 | |
| | 575 | if (!self->wiimote) { |
| | 576 | Wiimote_closed_ValueError(); |
| | 577 | return NULL; |
| | 578 | } |
| 542 | 579 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 543 | 580 | "i:cwiid.Wiimote.get_acc_cal", kwlist, |
| 544 | 581 | &ext_type)) { |
| … |
… |
|
| 575 | 612 | { |
| 576 | 613 | long led; |
| 577 | 614 | |
| | 615 | if (!self->wiimote) { |
| | 616 | Wiimote_closed_ValueError(); |
| | 617 | return -1; |
| | 618 | } |
| 578 | 619 | if (((led = PyInt_AsLong(PyLed)) == -1) && PyErr_Occurred()) { |
| 579 | 620 | return -1; |
| 580 | 621 | } |
| … |
… |
|
| 593 | 634 | { |
| 594 | 635 | long rumble; |
| 595 | 636 | |
| | 637 | if (!self->wiimote) { |
| | 638 | Wiimote_closed_ValueError(); |
| | 639 | return -1; |
| | 640 | } |
| 596 | 641 | if (((rumble = PyInt_AsLong(PyRumble)) == -1) && PyErr_Occurred()) { |
| 597 | 642 | return -1; |
| 598 | 643 | } |
| … |
… |
|
| 611 | 656 | { |
| 612 | 657 | long rpt_mode; |
| 613 | 658 | |
| | 659 | if (!self->wiimote) { |
| | 660 | Wiimote_closed_ValueError(); |
| | 661 | return -1; |
| | 662 | } |
| 614 | 663 | if (((rpt_mode = PyInt_AsLong(PyRptMode)) == -1) && PyErr_Occurred()) { |
| 615 | 664 | return -1; |
| 616 | 665 | } |
| … |
… |
|
| 649 | 698 | void *buf; |
| 650 | 699 | PyObject *pyRetBuf; |
| 651 | 700 | |
| | 701 | if (!self->wiimote) { |
| | 702 | Wiimote_closed_ValueError(); |
| | 703 | return NULL; |
| | 704 | } |
| 652 | 705 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "BII:cwiid.Wiimote.read", |
| 653 | 706 | kwlist, &flags, &offset, &len)) { |
| 654 | 707 | return NULL; |
| … |
… |
|
| 699 | 752 | void *buf; |
| 700 | 753 | int len; |
| 701 | 754 | |
| | 755 | if (!self->wiimote) { |
| | 756 | Wiimote_closed_ValueError(); |
| | 757 | return NULL; |
| | 758 | } |
| 702 | 759 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "BIt#:cwiid.Wiimote.write", |
| 703 | 760 | kwlist, &flags, &offset, &buf, &len)) { |
| 704 | 761 | return NULL; |
| … |
… |
|
| 715 | 772 | static void CallbackBridge(cwiid_wiimote_t *wiimote, int mesg_count, |
| 716 | 773 | union cwiid_mesg mesg[], struct timespec *t) |
| 717 | 774 | { |
| 718 | | PyObject *ArgTuple, *Time; |
| | 775 | PyObject *ArgTuple; |
| 719 | 776 | PyObject *PySelf; |
| 720 | 777 | PyGILState_STATE gstate; |
| 721 | 778 | |
| 722 | 779 | gstate = PyGILState_Ensure(); |
| | 780 | if (!wiimote) { |
| | 781 | Wiimote_closed_ValueError(); |
| | 782 | PyGILState_Release(gstate); |
| | 783 | return; |
| | 784 | } |
| 723 | 785 | |
| 724 | 786 | ArgTuple = ConvertMesgArray(mesg_count, mesg); |
| 725 | 787 | |
| … |
… |
|
| 743 | 805 | * |
| 744 | 806 | * Python callback takes arg (mesgs). The mesgs is a list of |
| 745 | 807 | * mesg tuples which contain the mesg type and a dict of the arguments. |
| 746 | | * |
| | 808 | * |
| 747 | 809 | * Ex: |
| 748 | 810 | * mesgs =>[(cwiid.STATUS_MESG,{"battery":battery,"ext_type":ext_type}), |
| 749 | | * (cwiid.BTN_MESG,buttons), |
| | 811 | * (cwiid.BTN_MESG,buttons), |
| 750 | 812 | * (cwiid.ACC_MESG,(x,y,z)), |
| 751 | 813 | * (cwiid.IR_MESG,[{"pos":(x,y),"size":size}, ...]), |
| 752 | 814 | * (cwiid.NUNCHUK_MESG,{"stick":(x,y),"acc":(x,y,z), |