AutoHotKey can map the gamepad button to whatever keyboard key you need to press to exit.
You can then set that key to close the program in Mala hotkeys or in the emulator itself.
My trackball had hookups for a middle mouse button that weren't being used, so I mapped it to ESC.
It has worked for everything, even games I had trouble closing before.