| 1 | /* |
|---|
| 2 | * Copyright (C) 2007 Justin M. Tulloss <jmtulloss@gmail.com>, L. Donnie Smith <donnie.smith@gatech.edu> |
|---|
| 3 | * |
|---|
| 4 | * Interface from Python to libcwiid |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|---|
| 19 | * Boston, MA 02110-1301 USA |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include "Python.h" |
|---|
| 24 | |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | |
|---|
| 27 | #include <cwiid.h> |
|---|
| 28 | #include "structmember.h" |
|---|
| 29 | |
|---|
| 30 | /* externally defined types */ |
|---|
| 31 | extern PyTypeObject Wiimote_Type; |
|---|
| 32 | extern PyObject *ConvertMesgArray(int, union cwiid_mesg []); |
|---|
| 33 | |
|---|
| 34 | /* cwiid module initializer */ |
|---|
| 35 | PyMODINIT_FUNC initcwiid(void); |
|---|
| 36 | |
|---|
| 37 | /* constants, enumerations */ |
|---|
| 38 | #define CWIID_CONST_MACRO(a) {#a, CWIID_##a} |
|---|
| 39 | static struct { |
|---|
| 40 | char *name; |
|---|
| 41 | int value; |
|---|
| 42 | } cwiid_constants[] = { |
|---|
| 43 | CWIID_CONST_MACRO(FLAG_MESG_IFC), |
|---|
| 44 | CWIID_CONST_MACRO(FLAG_CONTINUOUS), |
|---|
| 45 | CWIID_CONST_MACRO(FLAG_REPEAT_BTN), |
|---|
| 46 | CWIID_CONST_MACRO(FLAG_NONBLOCK), |
|---|
| 47 | CWIID_CONST_MACRO(FLAG_MOTIONPLUS), |
|---|
| 48 | CWIID_CONST_MACRO(RPT_STATUS), |
|---|
| 49 | CWIID_CONST_MACRO(RPT_BTN), |
|---|
| 50 | CWIID_CONST_MACRO(RPT_ACC), |
|---|
| 51 | CWIID_CONST_MACRO(RPT_IR), |
|---|
| 52 | CWIID_CONST_MACRO(RPT_NUNCHUK), |
|---|
| 53 | CWIID_CONST_MACRO(RPT_CLASSIC), |
|---|
| 54 | CWIID_CONST_MACRO(RPT_BALANCE), |
|---|
| 55 | CWIID_CONST_MACRO(RPT_MOTIONPLUS), |
|---|
| 56 | CWIID_CONST_MACRO(RPT_EXT), |
|---|
| 57 | CWIID_CONST_MACRO(LED1_ON), |
|---|
| 58 | CWIID_CONST_MACRO(LED2_ON), |
|---|
| 59 | CWIID_CONST_MACRO(LED3_ON), |
|---|
| 60 | CWIID_CONST_MACRO(LED4_ON), |
|---|
| 61 | CWIID_CONST_MACRO(BTN_2), |
|---|
| 62 | CWIID_CONST_MACRO(BTN_1), |
|---|
| 63 | CWIID_CONST_MACRO(BTN_B), |
|---|
| 64 | CWIID_CONST_MACRO(BTN_A), |
|---|
| 65 | CWIID_CONST_MACRO(BTN_MINUS), |
|---|
| 66 | CWIID_CONST_MACRO(BTN_HOME), |
|---|
| 67 | CWIID_CONST_MACRO(BTN_LEFT), |
|---|
| 68 | CWIID_CONST_MACRO(BTN_RIGHT), |
|---|
| 69 | CWIID_CONST_MACRO(BTN_DOWN), |
|---|
| 70 | CWIID_CONST_MACRO(BTN_UP), |
|---|
| 71 | CWIID_CONST_MACRO(BTN_PLUS), |
|---|
| 72 | CWIID_CONST_MACRO(NUNCHUK_BTN_Z), |
|---|
| 73 | CWIID_CONST_MACRO(NUNCHUK_BTN_C), |
|---|
| 74 | CWIID_CONST_MACRO(CLASSIC_BTN_UP), |
|---|
| 75 | CWIID_CONST_MACRO(CLASSIC_BTN_LEFT), |
|---|
| 76 | CWIID_CONST_MACRO(CLASSIC_BTN_ZR), |
|---|
| 77 | CWIID_CONST_MACRO(CLASSIC_BTN_X), |
|---|
| 78 | CWIID_CONST_MACRO(CLASSIC_BTN_A), |
|---|
| 79 | CWIID_CONST_MACRO(CLASSIC_BTN_Y), |
|---|
| 80 | CWIID_CONST_MACRO(CLASSIC_BTN_B), |
|---|
| 81 | CWIID_CONST_MACRO(CLASSIC_BTN_ZL), |
|---|
| 82 | CWIID_CONST_MACRO(CLASSIC_BTN_R), |
|---|
| 83 | CWIID_CONST_MACRO(CLASSIC_BTN_PLUS), |
|---|
| 84 | CWIID_CONST_MACRO(CLASSIC_BTN_HOME), |
|---|
| 85 | CWIID_CONST_MACRO(CLASSIC_BTN_MINUS), |
|---|
| 86 | CWIID_CONST_MACRO(CLASSIC_BTN_L), |
|---|
| 87 | CWIID_CONST_MACRO(CLASSIC_BTN_DOWN), |
|---|
| 88 | CWIID_CONST_MACRO(CLASSIC_BTN_RIGHT), |
|---|
| 89 | CWIID_CONST_MACRO(SEND_RPT_NO_RUMBLE), |
|---|
| 90 | CWIID_CONST_MACRO(RW_EEPROM), |
|---|
| 91 | CWIID_CONST_MACRO(RW_REG), |
|---|
| 92 | CWIID_CONST_MACRO(RW_DECODE), |
|---|
| 93 | CWIID_CONST_MACRO(MAX_READ_LEN), |
|---|
| 94 | CWIID_CONST_MACRO(X), |
|---|
| 95 | CWIID_CONST_MACRO(Y), |
|---|
| 96 | CWIID_CONST_MACRO(Z), |
|---|
| 97 | CWIID_CONST_MACRO(PHI), |
|---|
| 98 | CWIID_CONST_MACRO(THETA), |
|---|
| 99 | CWIID_CONST_MACRO(PSI), |
|---|
| 100 | CWIID_CONST_MACRO(IR_SRC_COUNT), |
|---|
| 101 | CWIID_CONST_MACRO(IR_X_MAX), |
|---|
| 102 | CWIID_CONST_MACRO(IR_Y_MAX), |
|---|
| 103 | CWIID_CONST_MACRO(BATTERY_MAX), |
|---|
| 104 | CWIID_CONST_MACRO(CLASSIC_L_STICK_MAX), |
|---|
| 105 | CWIID_CONST_MACRO(CLASSIC_R_STICK_MAX), |
|---|
| 106 | CWIID_CONST_MACRO(CLASSIC_LR_MAX), |
|---|
| 107 | CWIID_CONST_MACRO(CMD_STATUS), |
|---|
| 108 | CWIID_CONST_MACRO(CMD_LED), |
|---|
| 109 | CWIID_CONST_MACRO(CMD_RUMBLE), |
|---|
| 110 | CWIID_CONST_MACRO(CMD_RPT_MODE), |
|---|
| 111 | CWIID_CONST_MACRO(MESG_STATUS), |
|---|
| 112 | CWIID_CONST_MACRO(MESG_BTN), |
|---|
| 113 | CWIID_CONST_MACRO(MESG_ACC), |
|---|
| 114 | CWIID_CONST_MACRO(MESG_IR), |
|---|
| 115 | CWIID_CONST_MACRO(MESG_NUNCHUK), |
|---|
| 116 | CWIID_CONST_MACRO(MESG_CLASSIC), |
|---|
| 117 | CWIID_CONST_MACRO(MESG_BALANCE), |
|---|
| 118 | CWIID_CONST_MACRO(MESG_MOTIONPLUS), |
|---|
| 119 | CWIID_CONST_MACRO(MESG_ERROR), |
|---|
| 120 | CWIID_CONST_MACRO(MESG_UNKNOWN), |
|---|
| 121 | CWIID_CONST_MACRO(EXT_NONE), |
|---|
| 122 | CWIID_CONST_MACRO(EXT_NUNCHUK), |
|---|
| 123 | CWIID_CONST_MACRO(EXT_CLASSIC), |
|---|
| 124 | CWIID_CONST_MACRO(EXT_BALANCE), |
|---|
| 125 | CWIID_CONST_MACRO(EXT_MOTIONPLUS), |
|---|
| 126 | CWIID_CONST_MACRO(EXT_UNKNOWN), |
|---|
| 127 | CWIID_CONST_MACRO(ERROR_DISCONNECT), |
|---|
| 128 | CWIID_CONST_MACRO(ERROR_COMM), |
|---|
| 129 | {NULL, 0} |
|---|
| 130 | }; |
|---|
| 131 | |
|---|
| 132 | /* Associates cwiid functions with python ones */ |
|---|
| 133 | static PyMethodDef Module_Methods[] = |
|---|
| 134 | { |
|---|
| 135 | {NULL, NULL, 0, NULL} |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | PyMODINIT_FUNC initcwiid(void) |
|---|
| 139 | { |
|---|
| 140 | PyObject *Module; |
|---|
| 141 | PyObject *CObj; |
|---|
| 142 | int i; |
|---|
| 143 | |
|---|
| 144 | PyEval_InitThreads(); |
|---|
| 145 | |
|---|
| 146 | if (PyType_Ready(&Wiimote_Type) < 0) { |
|---|
| 147 | return; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | if (!(Module = Py_InitModule3("cwiid", Module_Methods, |
|---|
| 151 | "CWiid Wiimote Interface"))) { |
|---|
| 152 | return; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | Py_INCREF(&Wiimote_Type); |
|---|
| 156 | PyModule_AddObject(Module, "Wiimote", (PyObject*)&Wiimote_Type); |
|---|
| 157 | |
|---|
| 158 | for (i = 0; cwiid_constants[i].name; i++) { |
|---|
| 159 | /* No way to report errors from here, so just ignore them and hope |
|---|
| 160 | * for segfault */ |
|---|
| 161 | PyModule_AddIntConstant(Module, cwiid_constants[i].name, |
|---|
| 162 | cwiid_constants[i].value); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | if (!(CObj = PyCObject_FromVoidPtr(ConvertMesgArray, NULL))) { |
|---|
| 166 | return; |
|---|
| 167 | } |
|---|
| 168 | PyModule_AddObject(Module, "ConvertMesgArray", CObj); |
|---|
| 169 | } |
|---|