5353
5454#define PG_GET_LIST_LEN 128
5555
56- // Map joystick instance IDs to device ids for partial backwards compatibility
57- static PyObject * joy_instance_map = NULL ;
58-
5956/* _custom_event stores the next custom user event type that will be
6057 * returned by pygame.event.custom_type() */
6158#define _PGE_CUSTOM_EVENT_INIT PGE_USEREVENT + 1
@@ -851,51 +848,11 @@ get_joy_guid(int device_index)
851848 return PyUnicode_FromString (strguid );
852849}
853850
854- /** Try to insert the instance ID for a new device into the joystick mapping.
855- */
856- void
857- _joy_map_add (int device_index )
858- {
859- int instance_id = (int )SDL_JoystickGetDeviceInstanceID (device_index );
860- PyObject * k , * v ;
861- if (instance_id != -1 ) {
862- k = PyLong_FromLong (instance_id );
863- v = PyLong_FromLong (device_index );
864- if (k != NULL && v != NULL ) {
865- PyDict_SetItem (joy_instance_map , k , v );
866- }
867- Py_XDECREF (k );
868- Py_XDECREF (v );
869- }
870- }
871-
872- /** Look up a device ID for an instance ID. */
873- PyObject *
874- _joy_map_instance (int instance_id )
875- {
876- PyObject * v , * k = PyLong_FromLong (instance_id );
877- if (!k ) {
878- Py_RETURN_NONE ;
879- }
880- v = PyDict_GetItem (joy_instance_map , k );
881- if (v ) {
882- Py_DECREF (k );
883- Py_INCREF (v );
884- return v ;
885- }
886- return k ;
887- }
888-
889- /** Discard a joystick from the joystick instance -> device mapping. */
890- void
891- _joy_map_discard (int instance_id )
851+ static PyObject *
852+ get_joy_device_index (int instance_id )
892853{
893- PyObject * k = PyLong_FromLong (instance_id );
894-
895- if (k ) {
896- PyDict_DelItem (joy_instance_map , k );
897- Py_DECREF (k );
898- }
854+ int device_index = pgJoystick_GetDeviceIndexByInstanceID (instance_id );
855+ return PyLong_FromLong (device_index );
899856}
900857
901858static PyObject *
@@ -995,23 +952,23 @@ dict_from_event(SDL_Event *event)
995952 PyBool_FromLong ((event -> button .which == SDL_TOUCH_MOUSEID )));
996953 break ;
997954 case SDL_JOYAXISMOTION :
998- _pg_insobj (dict , "joy" , _joy_map_instance (event -> jaxis .which ));
955+ _pg_insobj (dict , "joy" , get_joy_device_index (event -> jaxis .which ));
999956 _pg_insobj (dict , "instance_id" ,
1000957 PyLong_FromLong (event -> jaxis .which ));
1001958 _pg_insobj (dict , "axis" , PyLong_FromLong (event -> jaxis .axis ));
1002959 _pg_insobj (dict , "value" ,
1003960 PyFloat_FromDouble (event -> jaxis .value / 32767.0 ));
1004961 break ;
1005962 case SDL_JOYBALLMOTION :
1006- _pg_insobj (dict , "joy" , _joy_map_instance (event -> jaxis .which ));
963+ _pg_insobj (dict , "joy" , get_joy_device_index (event -> jaxis .which ));
1007964 _pg_insobj (dict , "instance_id" ,
1008965 PyLong_FromLong (event -> jball .which ));
1009966 _pg_insobj (dict , "ball" , PyLong_FromLong (event -> jball .ball ));
1010967 obj = Py_BuildValue ("(ii)" , event -> jball .xrel , event -> jball .yrel );
1011968 _pg_insobj (dict , "rel" , obj );
1012969 break ;
1013970 case SDL_JOYHATMOTION :
1014- _pg_insobj (dict , "joy" , _joy_map_instance (event -> jaxis .which ));
971+ _pg_insobj (dict , "joy" , get_joy_device_index (event -> jaxis .which ));
1015972 _pg_insobj (dict , "instance_id" ,
1016973 PyLong_FromLong (event -> jhat .which ));
1017974 _pg_insobj (dict , "hat" , PyLong_FromLong (event -> jhat .hat ));
@@ -1028,7 +985,7 @@ dict_from_event(SDL_Event *event)
1028985 break ;
1029986 case SDL_JOYBUTTONUP :
1030987 case SDL_JOYBUTTONDOWN :
1031- _pg_insobj (dict , "joy" , _joy_map_instance (event -> jaxis .which ));
988+ _pg_insobj (dict , "joy" , get_joy_device_index (event -> jaxis .which ));
1032989 _pg_insobj (dict , "instance_id" ,
1033990 PyLong_FromLong (event -> jbutton .which ));
1034991 _pg_insobj (dict , "button" , PyLong_FromLong (event -> jbutton .button ));
@@ -1158,7 +1115,6 @@ dict_from_event(SDL_Event *event)
11581115 _pg_insobj (dict , "guid" , get_joy_guid (event -> jdevice .which ));
11591116 break ;
11601117 case SDL_JOYDEVICEADDED :
1161- _joy_map_add (event -> jdevice .which );
11621118 _pg_insobj (dict , "device_index" ,
11631119 PyLong_FromLong (event -> jdevice .which ));
11641120 _pg_insobj (dict , "guid" , get_joy_guid (event -> jdevice .which ));
@@ -1170,7 +1126,6 @@ dict_from_event(SDL_Event *event)
11701126 PyLong_FromLong (event -> cdevice .which ));
11711127 break ;
11721128 case SDL_JOYDEVICEREMOVED :
1173- _joy_map_discard (event -> jdevice .which );
11741129 _pg_insobj (dict , "instance_id" ,
11751130 PyLong_FromLong (event -> jdevice .which ));
11761131 break ;
@@ -2263,6 +2218,11 @@ MODINIT_DEFINE(event)
22632218 return NULL ;
22642219 }
22652220
2221+ import_pygame_joystick ();
2222+ if (PyErr_Occurred ()) {
2223+ return NULL ;
2224+ }
2225+
22662226 /* type preparation */
22672227 if (PyType_Ready (& pgEvent_Type ) < 0 ) {
22682228 return NULL ;
@@ -2274,15 +2234,6 @@ MODINIT_DEFINE(event)
22742234 return NULL ;
22752235 }
22762236
2277- joy_instance_map = PyDict_New ();
2278- /* need to keep a reference for use in the module */
2279- Py_XINCREF (joy_instance_map );
2280- if (PyModule_AddObject (module , "_joy_instance_map" , joy_instance_map )) {
2281- Py_XDECREF (joy_instance_map );
2282- Py_DECREF (module );
2283- return NULL ;
2284- }
2285-
22862237 Py_INCREF (& pgEvent_Type );
22872238 if (PyModule_AddObject (module , "EventType" , (PyObject * )& pgEvent_Type )) {
22882239 Py_DECREF (& pgEvent_Type );
0 commit comments