76d35e6d6151db3c584b2b0926ae43b07a059239
[blender.git] / source / gameengine / GameLogic / SCA_JoystickSensor.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27 #include "SCA_JoystickManager.h"
28 #include "SCA_JoystickSensor.h"
29
30 #include "SCA_EventManager.h"
31 #include "SCA_LogicManager.h"
32
33 #include "PyObjectPlus.h"
34
35 #include <iostream>
36
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42
43 SCA_JoystickSensor::SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
44                                                                            SCA_IObject* gameobj,
45                                                                            short int joyindex,
46                                                                            short int joymode,
47                                                                            int axis, int axisf,int prec,
48                                                                            int button,
49                                                                            int hat, int hatf, bool allevents)
50                                                                            :SCA_ISensor(gameobj,eventmgr),
51                                                                            m_axis(axis),
52                                                                            m_axisf(axisf),
53                                                                            m_button(button),
54                                                                            m_hat(hat),
55                                                                            m_hatf(hatf),
56                                                                            m_precision(prec),
57                                                                            m_joymode(joymode),
58                                                                            m_joyindex(joyindex),
59                                                                            m_bAllEvents(allevents)
60 {       
61 /*
62 std::cout << " axis "           << m_axis               << std::endl;
63 std::cout << " axis flag "      << m_axisf              << std::endl;
64 std::cout << " precision "      << m_precision  << std::endl;
65 std::cout << " button "         << m_button     << std::endl;
66 std::cout << " hat "            << m_hat                << std::endl;
67 std::cout << " hat flag "       << m_hatf               << std::endl;
68 */
69         Init();
70 }
71
72 void SCA_JoystickSensor::Init()
73 {
74         m_istrig=(m_invert)?1:0;
75         m_istrig_prev=0;
76         m_reset = true;
77 }
78
79 SCA_JoystickSensor::~SCA_JoystickSensor()
80 {
81 }
82
83
84 CValue* SCA_JoystickSensor::GetReplica()
85 {
86         SCA_JoystickSensor* replica = new SCA_JoystickSensor(*this);
87         // this will copy properties and so on...
88         replica->ProcessReplica();
89         replica->Init();
90         return replica;
91 }
92
93
94 bool SCA_JoystickSensor::IsPositiveTrigger()
95
96         bool result =   m_istrig;
97         if (m_invert)
98                 result = !result;
99         return result;
100 }
101
102
103 bool SCA_JoystickSensor::Evaluate()
104 {
105         SCA_Joystick *js = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
106         bool result = false;
107         bool reset = m_reset && m_level;
108         
109         if(js==NULL) /* no joystick - dont do anything */
110                 return false;
111         
112         m_reset = false;
113         switch(m_joymode)
114         {
115         case KX_JOYSENSORMODE_AXIS:
116                 {
117                 /* what is what!
118                         m_axisf == JOYAXIS_RIGHT, JOYAXIS_UP, JOYAXIS_DOWN, JOYAXIS_LEFT
119                         m_axisf == 1 == up
120                         m_axisf == 2 == left
121                         m_axisf == 3 == down
122                         
123                         numberof== m_axis (1-4), range is half of JOYAXIS_MAX since 
124                                 it assumes the axis joysticks are axis parirs (0,1), (2,3), etc
125                                 also note that this starts at 1 where functions its used
126                                 with expect a zero index.
127                         */
128                         
129                         if (!js->IsTrigAxis() && !reset) /* No events from SDL? - dont bother */
130                                 return false;
131                         
132                         js->cSetPrecision(m_precision);
133                         if (m_bAllEvents) {
134                                 if(js->aAxisPairIsPositive(m_axis-1)){ /* use zero based axis index internally */
135                                         m_istrig = 1;
136                                         result = true;
137                                 }else{
138                                         if(m_istrig){
139                                                 m_istrig = 0;
140                                                 result = true;
141                                         }
142                                 }
143                         }
144                         else {
145                                 if(js->aAxisPairDirectionIsPositive(m_axis-1, m_axisf)){ /* use zero based axis index internally */
146                                         m_istrig = 1;
147                                         result = true;
148                                 }else{
149                                         if(m_istrig){
150                                                 m_istrig = 0;
151                                                 result = true;
152                                         }
153                                 }
154                         }
155                         break;
156                 }
157         case KX_JOYSENSORMODE_AXIS_SINGLE:
158                 {
159                         /* Like KX_JOYSENSORMODE_AXIS but dont pair up axis */
160                         if (!js->IsTrigAxis() && !reset) /* No events from SDL? - dont bother */
161                                 return false;
162                         
163                         /* No need for 'm_bAllEvents' check here since were only checking 1 axis */
164                         js->cSetPrecision(m_precision);
165                         if(js->aAxisIsPositive(m_axis-1)){ /* use zero based axis index internally */
166                                 m_istrig = 1;
167                                 result = true;
168                         }else{
169                                 if(m_istrig){
170                                         m_istrig = 0;
171                                         result = true;
172                                 }
173                         }
174                         break;
175                 }
176                 
177         case KX_JOYSENSORMODE_BUTTON:
178                 {
179                 /* what is what!
180                         m_button = the actual button in question
181                         */
182                         if (!js->IsTrigButton() && !reset) /* No events from SDL? - dont bother */
183                                 return false;
184                         
185                         if(( m_bAllEvents && js->aAnyButtonPressIsPositive()) || (!m_bAllEvents && js->aButtonPressIsPositive(m_button))) {
186                                 m_istrig = 1;
187                                 result = true;
188                         }else {
189                                 if(m_istrig){
190                                         m_istrig = 0;
191                                         result = true;
192                                 }
193                         }
194                         break;
195                 }
196         case KX_JOYSENSORMODE_HAT:
197                 {
198                 /* what is what!
199                         numberof = m_hat  -- max 4
200                         direction= m_hatf -- max 12
201                         */
202                         
203                         if (!js->IsTrigHat() && !reset) /* No events from SDL? - dont bother */
204                                 return false;
205                         
206                         if((m_bAllEvents && js->GetHat(m_hat-1)) || js->aHatIsPositive(m_hat-1, m_hatf)) {
207                                 m_istrig = 1;
208                                 result = true;
209                         }else{
210                                 if(m_istrig){
211                                         m_istrig = 0;
212                                         result = true;
213                                 }
214                         }
215                         break;
216                 }
217                 /* test for ball anyone ?*/
218         default:
219                 printf("Error invalid switch statement\n");
220                 break;
221         }
222         
223         /* if not all events are enabled, only send a positive pulse when 
224          * the button state changes */
225         if (!m_bAllEvents) {
226                 if (m_istrig_prev == m_istrig) {
227                         result = false;
228                 } else {
229                         m_istrig_prev = m_istrig;
230                 }
231         }
232         
233         if (reset)
234                 result = true;
235         
236         return result;
237 }
238
239
240 bool SCA_JoystickSensor::isValid(SCA_JoystickSensor::KX_JOYSENSORMODE m)
241 {
242         bool res = false;
243         res = ((m > KX_JOYSENSORMODE_NODEF) && (m < KX_JOYSENSORMODE_MAX));
244         return res;
245 }
246
247
248 /* ------------------------------------------------------------------------- */
249 /* Python functions                                                                                                              */
250 /* ------------------------------------------------------------------------- */
251
252 /* Integration hooks ------------------------------------------------------- */
253 PyTypeObject SCA_JoystickSensor::Type = {
254         PyVarObject_HEAD_INIT(NULL, 0)
255         "SCA_JoystickSensor",
256         sizeof(PyObjectPlus_Proxy),
257         0,
258         py_base_dealloc,
259         0,
260         0,
261         0,
262         0,
263         py_base_repr,
264         0,0,0,0,0,0,0,0,0,
265         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
266         0,0,0,0,0,0,0,
267         Methods,
268         0,
269         0,
270         &SCA_ISensor::Type,
271         0,0,0,0,0,0,
272         py_base_new
273 };
274
275 PyMethodDef SCA_JoystickSensor::Methods[] = {
276         //Deprecated functions ------>
277         {"getIndex",     (PyCFunction) SCA_JoystickSensor::sPyGetIndex,         METH_NOARGS,    (const char *)GetIndex_doc},
278         {"setIndex",     (PyCFunction) SCA_JoystickSensor::sPySetIndex,         METH_O,                 (const char *)SetIndex_doc},
279         {"getAxis",      (PyCFunction) SCA_JoystickSensor::sPyGetAxis,          METH_NOARGS,    (const char *)GetAxis_doc},
280         {"setAxis",      (PyCFunction) SCA_JoystickSensor::sPySetAxis,          METH_VARARGS,   (const char *)SetAxis_doc},
281         {"getAxisValue", (PyCFunction) SCA_JoystickSensor::sPyGetAxisValue,     METH_NOARGS,    (const char *)GetAxisValue_doc},
282         {"getThreshold", (PyCFunction) SCA_JoystickSensor::sPyGetThreshold, METH_NOARGS,        (const char *)GetThreshold_doc},
283         {"setThreshold", (PyCFunction) SCA_JoystickSensor::sPySetThreshold, METH_VARARGS,       (const char *)SetThreshold_doc},
284         {"getButton",    (PyCFunction) SCA_JoystickSensor::sPyGetButton,        METH_NOARGS,    (const char *)GetButton_doc},
285         {"setButton",    (PyCFunction) SCA_JoystickSensor::sPySetButton,        METH_O,                 (const char *)SetButton_doc},
286         {"getHat",               (PyCFunction) SCA_JoystickSensor::sPyGetHat,           METH_NOARGS,    (const char *)GetHat_doc},
287         {"setHat",               (PyCFunction) SCA_JoystickSensor::sPySetHat,           METH_VARARGS,   (const char *)SetHat_doc},
288         {"getNumAxes",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfAxes,     METH_NOARGS,    (const char *)NumberOfAxes_doc},
289         {"getNumButtons",(PyCFunction) SCA_JoystickSensor::sPyNumberOfButtons,METH_NOARGS,      (const char *)NumberOfButtons_doc},
290         {"getNumHats",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfHats,     METH_NOARGS,    (const char *)NumberOfHats_doc},
291         {"isConnected", (PyCFunction) SCA_JoystickSensor::sPyConnected,         METH_NOARGS,    (const char *)Connected_doc},
292         {"getButtonValue",(PyCFunction) SCA_JoystickSensor::sPyGetButtonValue,  METH_NOARGS,(const char *)GetButtonValue_doc},
293         //<----- Deprecated
294         {"getButtonActiveList",(PyCFunction) SCA_JoystickSensor::sPyGetButtonActiveList,        METH_NOARGS,(const char *)GetButtonActiveList_doc},
295         {"getButtonStatus",(PyCFunction) SCA_JoystickSensor::sPyGetButtonStatus,        METH_VARARGS,(const char *)GetButtonStatus_doc},
296         {NULL,NULL} //Sentinel
297 };
298
299 PyAttributeDef SCA_JoystickSensor::Attributes[] = {
300         KX_PYATTRIBUTE_SHORT_RW("index",0,JOYINDEX_MAX-1,true,SCA_JoystickSensor,m_joyindex),
301         KX_PYATTRIBUTE_INT_RW("threshold",0,32768,true,SCA_JoystickSensor,m_precision),
302         KX_PYATTRIBUTE_INT_RW("button",0,100,false,SCA_JoystickSensor,m_button),
303         KX_PYATTRIBUTE_INT_LIST_RW_CHECK("axis",0,3,true,SCA_JoystickSensor,m_axis,2,CheckAxis),
304         KX_PYATTRIBUTE_INT_LIST_RW_CHECK("hat",0,12,true,SCA_JoystickSensor,m_hat,2,CheckHat),
305         KX_PYATTRIBUTE_RO_FUNCTION("axisValues",        SCA_JoystickSensor, pyattr_get_axis_values),
306         KX_PYATTRIBUTE_RO_FUNCTION("axisSingle", SCA_JoystickSensor, pyattr_get_axis_single),
307         KX_PYATTRIBUTE_RO_FUNCTION("hatValues", SCA_JoystickSensor, pyattr_get_hat_values),
308         KX_PYATTRIBUTE_RO_FUNCTION("hatSingle", SCA_JoystickSensor, pyattr_get_hat_single),
309         KX_PYATTRIBUTE_RO_FUNCTION("numAxis",           SCA_JoystickSensor, pyattr_get_num_axis),
310         KX_PYATTRIBUTE_RO_FUNCTION("numButtons",        SCA_JoystickSensor, pyattr_get_num_buttons),
311         KX_PYATTRIBUTE_RO_FUNCTION("numHats",           SCA_JoystickSensor, pyattr_get_num_hats),
312         KX_PYATTRIBUTE_RO_FUNCTION("connected",         SCA_JoystickSensor, pyattr_get_connected),
313         //KX_PYATTRIBUTE_TODO("events"),
314         { NULL }        //Sentinel
315 };
316
317
318 /* get index ---------------------------------------------------------- */
319 const char SCA_JoystickSensor::GetIndex_doc[] = 
320 "getIndex\n"
321 "\tReturns the joystick index to use.\n";
322 PyObject* SCA_JoystickSensor::PyGetIndex( ) {
323         ShowDeprecationWarning("getIndex()", "the index property");
324         return PyLong_FromSsize_t(m_joyindex);
325 }
326
327
328 /* set index ---------------------------------------------------------- */
329 const char SCA_JoystickSensor::SetIndex_doc[] = 
330 "setIndex\n"
331 "\tSets the joystick index to use.\n";
332 PyObject* SCA_JoystickSensor::PySetIndex( PyObject* value ) {
333         ShowDeprecationWarning("setIndex()", "the index property");
334         int index = PyLong_AsSsize_t( value ); /* -1 on error, will raise an error in this case */
335         if (index < 0 || index >= JOYINDEX_MAX) {
336                 PyErr_SetString(PyExc_ValueError, "joystick index out of range or not an int");
337                 return NULL;
338         }
339         
340         m_joyindex = index;
341         Py_RETURN_NONE;
342 }
343
344 /* get axis  ---------------------------------------------------------- */
345 const char SCA_JoystickSensor::GetAxis_doc[] = 
346 "getAxis\n"
347 "\tReturns the current axis this sensor reacts to.\n";
348 PyObject* SCA_JoystickSensor::PyGetAxis( ) {
349         ShowDeprecationWarning("getAxis()", "the axis property");
350         return Py_BuildValue("[ii]",m_axis, m_axisf);
351 }
352
353
354 /* set axis  ---------------------------------------------------------- */
355 const char SCA_JoystickSensor::SetAxis_doc[] = 
356 "setAxis\n"
357 "\tSets the current axis this sensor reacts to.\n";
358 PyObject* SCA_JoystickSensor::PySetAxis( PyObject* args ) {
359         ShowDeprecationWarning("setAxis()", "the axis property");
360         
361         int axis,axisflag;
362         if(!PyArg_ParseTuple(args, "ii:setAxis", &axis, &axisflag)){
363                 return NULL;
364         }
365         m_axis = axis;
366         m_axisf = axisflag;
367         
368         CheckAxis((void *)this, NULL); /* clamp values */
369         Py_RETURN_NONE;
370 }
371
372
373 /* get axis value ----------------------------------------------------- */
374 const char SCA_JoystickSensor::GetAxisValue_doc[] = 
375 "getAxisValue\n"
376 "\tReturns a list of the values for the current state of each axis.\n";
377 PyObject* SCA_JoystickSensor::PyGetAxisValue( ) {
378         ShowDeprecationWarning("getAxisValue()", "the axisPosition property");
379         SCA_Joystick *joy = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
380         
381         int axis_index= joy->GetNumberOfAxes();
382         PyObject *list= PyList_New(axis_index);
383         
384         while(axis_index--) {
385                 PyList_SET_ITEM(list, axis_index, PyLong_FromSsize_t(joy->GetAxisPosition(axis_index)));
386         }
387         
388         return list;
389 }
390
391
392 /* get threshold  ----------------------------------------------------- */
393 const char SCA_JoystickSensor::GetThreshold_doc[] = 
394 "getThreshold\n"
395 "\tReturns the threshold of the axis.\n";
396 PyObject* SCA_JoystickSensor::PyGetThreshold( ) {
397         ShowDeprecationWarning("getThreshold()", "the threshold property");
398         return PyLong_FromSsize_t(m_precision);
399 }
400
401
402 /* set threshold  ----------------------------------------------------- */
403 const char SCA_JoystickSensor::SetThreshold_doc[] = 
404 "setThreshold\n"
405 "\tSets the threshold of the axis.\n";
406 PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* args ) {
407         ShowDeprecationWarning("setThreshold()", "the threshold property");
408         int thresh;
409         if(!PyArg_ParseTuple(args, "i:setThreshold", &thresh)){
410                 return NULL;
411         }
412         m_precision = thresh;
413         Py_RETURN_NONE;
414 }
415
416 /* get button  -------------------------------------------------------- */
417 const char SCA_JoystickSensor::GetButton_doc[] = 
418 "getButton\n"
419 "\tReturns the current button this sensor is checking.\n";
420 PyObject* SCA_JoystickSensor::PyGetButton( ) {
421         ShowDeprecationWarning("getButton()", "the button property");
422         return PyLong_FromSsize_t(m_button);
423 }
424
425 /* set button  -------------------------------------------------------- */
426 const char SCA_JoystickSensor::SetButton_doc[] = 
427 "setButton\n"
428 "\tSets the button the sensor reacts to.\n";
429 PyObject* SCA_JoystickSensor::PySetButton( PyObject* value ) {
430         ShowDeprecationWarning("setButton()", "the button property");
431         int button = PyLong_AsSsize_t(value);
432         if(button==-1 && PyErr_Occurred()) {
433                 PyErr_SetString(PyExc_ValueError, "expected an int");
434                 return NULL;
435         }
436         m_button = button;
437         Py_RETURN_NONE; 
438 }
439
440 /* get button value  -------------------------------------------------- */
441 const char SCA_JoystickSensor::GetButtonValue_doc[] = 
442 "getButtonValue\n"
443 "\tReturns a list containing the indicies of the current pressed state of each button.\n";
444 PyObject* SCA_JoystickSensor::PyGetButtonValue( ) {
445         ShowDeprecationWarning("getButtonValue()", "getButtonActiveList");
446         return PyGetButtonActiveList( );
447 }
448
449 /* get button active list  -------------------------------------------------- */
450 const char SCA_JoystickSensor::GetButtonActiveList_doc[] = 
451 "getButtonActiveList\n"
452 "\tReturns a list containing the indicies of the button currently pressed.\n";
453 PyObject* SCA_JoystickSensor::PyGetButtonActiveList( ) {
454         SCA_Joystick *joy = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
455         PyObject *ls = PyList_New(0);
456         PyObject *value;
457         int i;
458         
459         if(joy) {
460                 for (i=0; i < joy->GetNumberOfButtons(); i++) {
461                         if (joy->aButtonPressIsPositive(i)) {
462                                 value = PyLong_FromSsize_t(i);
463                                 PyList_Append(ls, value);
464                                 Py_DECREF(value);
465                         }
466                 }
467         }
468         return ls;
469 }
470
471 /* get button status  -------------------------------------------------- */
472 const char SCA_JoystickSensor::GetButtonStatus_doc[] = 
473 "getButtonStatus(buttonIndex)\n"
474 "\tReturns a bool of the current pressed state of the specified button.\n";
475 PyObject* SCA_JoystickSensor::PyGetButtonStatus( PyObject* args ) {
476         SCA_Joystick *joy = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
477         int index;
478         
479         if(!PyArg_ParseTuple(args, "i:getButtonStatus", &index)){
480                 return NULL;
481         }
482         if(joy && index >= 0 && index < joy->GetNumberOfButtons()) {
483                 return PyBool_FromLong(joy->aButtonPressIsPositive(index) ? 1 : 0);
484         }
485         return PyBool_FromLong(0);
486 }
487
488 /* get hat      ----------------------------------------------------------- */
489 const char SCA_JoystickSensor::GetHat_doc[] = 
490 "getHat\n"
491 "\tReturns the current direction of the hat.\n";
492 PyObject* SCA_JoystickSensor::PyGetHat( ) {
493         ShowDeprecationWarning("getHat()", "the hat property");
494         return Py_BuildValue("[ii]",m_hat, m_hatf);
495 }
496
497
498 /* set hat      ----------------------------------------------------------- */
499 const char SCA_JoystickSensor::SetHat_doc[] = 
500 "setHat\n"
501 "\tSets the hat the sensor reacts to.\n";
502 PyObject* SCA_JoystickSensor::PySetHat( PyObject* args ) {
503         ShowDeprecationWarning("setHat()", "the hat property");
504         int hat,hatflag;
505         if(!PyArg_ParseTuple(args, "ii:setHat", &hat, &hatflag)){
506                 return NULL;
507         }
508         m_hat = hat;
509         m_hatf = hatflag;
510         
511         CheckHat((void *)this, NULL); /* clamp values */
512         Py_RETURN_NONE;
513 }
514
515
516 /* get # of ----------------------------------------------------- */
517 const char SCA_JoystickSensor::NumberOfAxes_doc[] = 
518 "getNumAxes\n"
519 "\tReturns the number of axes .\n";
520 PyObject* SCA_JoystickSensor::PyNumberOfAxes( ) {
521         ShowDeprecationWarning("getNumAxes()", "the numAxis property");
522         SCA_Joystick *joy = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
523         // when the joystick is null their is 0 exis still. dumb but scripters should use isConnected()
524         return PyLong_FromSsize_t( joy ? joy->GetNumberOfAxes() : 0 );
525 }
526
527
528 const char SCA_JoystickSensor::NumberOfButtons_doc[] = 
529 "getNumButtons\n"
530 "\tReturns the number of buttons .\n";
531 PyObject* SCA_JoystickSensor::PyNumberOfButtons( ) {
532         ShowDeprecationWarning("getNumButtons()", "the numButtons property");
533         SCA_Joystick *joy = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
534         return PyLong_FromSsize_t( joy ? joy->GetNumberOfButtons() : 0 );
535 }
536
537
538 const char SCA_JoystickSensor::NumberOfHats_doc[] = 
539 "getNumHats\n"
540 "\tReturns the number of hats .\n";
541 PyObject* SCA_JoystickSensor::PyNumberOfHats( ) {
542         ShowDeprecationWarning("getNumHats()", "the numHats property");
543         SCA_Joystick *joy = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
544         return PyLong_FromSsize_t( joy ? joy->GetNumberOfHats() : 0 );
545 }
546
547 const char SCA_JoystickSensor::Connected_doc[] = 
548 "getConnected\n"
549 "\tReturns True if a joystick is connected at this joysticks index.\n";
550 PyObject* SCA_JoystickSensor::PyConnected( ) {
551         ShowDeprecationWarning("getConnected()", "the connected property");
552         SCA_Joystick *joy = ((SCA_JoystickManager *)m_eventmgr)->GetJoystickDevice(m_joyindex);
553         return PyBool_FromLong( joy ? joy->Connected() : 0 );
554 }
555
556
557 PyObject* SCA_JoystickSensor::pyattr_get_axis_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
558 {
559         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
560         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
561         
562         int axis_index= joy->GetNumberOfAxes();
563         PyObject *list= PyList_New(axis_index);
564         
565         while(axis_index--) {
566                 PyList_SET_ITEM(list, axis_index, PyLong_FromSsize_t(joy->GetAxisPosition(axis_index)));
567         }
568         
569         return list;
570 }
571
572 PyObject* SCA_JoystickSensor::pyattr_get_axis_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
573 {
574         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
575         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
576         
577         if(self->m_joymode != KX_JOYSENSORMODE_AXIS_SINGLE) {
578                 PyErr_SetString(PyExc_TypeError, "val = sensor.axisSingle: Joystick Sensor, not 'Single Axis' type");
579                 return NULL;
580         }
581         
582         return PyLong_FromSsize_t(joy->GetAxisPosition(self->m_axis-1));
583 }
584
585 PyObject* SCA_JoystickSensor::pyattr_get_hat_values(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
586 {
587         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
588         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
589         
590         int hat_index= joy->GetNumberOfHats();
591         PyObject *list= PyList_New(hat_index);
592         
593         while(hat_index--) {
594                 PyList_SET_ITEM(list, hat_index, PyLong_FromSsize_t(joy->GetHat(hat_index)));
595         }
596         
597         return list;
598 }
599
600 PyObject* SCA_JoystickSensor::pyattr_get_hat_single(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
601 {
602         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
603         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
604         
605         return PyLong_FromSsize_t(joy->GetHat(self->m_hat-1));
606 }
607
608 PyObject* SCA_JoystickSensor::pyattr_get_num_axis(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
609 {
610         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
611         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
612         return PyLong_FromSsize_t( joy ? joy->GetNumberOfAxes() : 0 );
613 }
614
615 PyObject* SCA_JoystickSensor::pyattr_get_num_buttons(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
616 {
617         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
618         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
619         return PyLong_FromSsize_t( joy ? joy->GetNumberOfButtons() : 0 );
620 }
621
622 PyObject* SCA_JoystickSensor::pyattr_get_num_hats(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
623 {
624         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
625         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
626         return PyLong_FromSsize_t( joy ? joy->GetNumberOfHats() : 0 );
627 }
628
629 PyObject* SCA_JoystickSensor::pyattr_get_connected(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
630 {
631         SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
632         SCA_Joystick *joy = ((SCA_JoystickManager *)self->m_eventmgr)->GetJoystickDevice(self->m_joyindex);
633         return PyBool_FromLong( joy ? joy->Connected() : 0 );
634 }