Joystick/pad detection

All,

Just looking at some code that @killer.jo has posted and it occurred to me that it would be useful to detect if a joypad was attached/connected and ready to use so that the programmer could code accordingly i.e. giving the user the option of using a WiFi or usb connected pad, the inbuilt gyroscope or whether to provide screen input graphics etc.

Can anyone point me in the right direction for this?