OK, so I have nearly got this thing in order.
I had asked this question in two other forums also and the only answer I got pointed me to the SDL2 Gamepad Tool (http://generalarcade.com/gamepadtool/). This tool was supposed to register my gamepad without using the xboxdrv command. This tool did recognize my gamepad, gave a name to my gamepad as "DragonRise_Inc. Generic USB Joystick" rather than what was previously displayed as just "Generic USB Joystick" and I could use my gamepad in the game. But the mapping was wrong, i.e., the right d-pad button would accelerate my player and other anomalies. Even by manually mapping the buttons in this tool, the same behavior was found.
So, this tool, to a degree was kind of helpful (maybe as its name suggests, it would be more helpful for SDL2 based games, and maybe the game that I wanted to play with the gamepad is not SDL2 based.) The game that I want to play with my gamepad is 'Teddy Floppy Ear - The Race' on Steam Linux.
Next, I was left with the same first problem. I searched and searched and found a question in this very website that helped me. The page link is How can I execute command on startup (rc.local alternative) on Ubuntu 16.10 According to this page, Ubuntu >= 16.10 do not have rc.local file, but a kind person has given the instructions to make one. So I went ahead and created this file.
The commands given on that page, to create the rc.local file are:
printf '%s\n' '#!/bin/bash' 'exit 0' | sudo tee -a /etc/rc.local
sudo chmod +x /etc/rc.local
sudo reboot
Next, I was not happy with the additional js input devices, so I searched some more. My search brought me to these two pages: https://github.com/RetroPie/RetroPie-Setup/wiki/Universal-Controller-Calibration-&-Mapping-Using-xboxdrv and https://retropie.org.uk/forum/topic/7096/xboxdrv-guidance-needed These pages have very dedicated discussion about the xboxdrv command. From these pages I got the command 'sudo killall xboxdrv'. This command removed all the additional js input devices. And showed that calling the xboxdrv initialization command with the name of the gamepad is a better way than using the event number. I also found that the event number changes if there are more/less usb devices attached to the notebook. Here, the gamepad name registered through the SDL2 Gamepad Tool came handy.
Now, I have a working setup. I have the rc.local file with these contents:
#!/bin/bash
sudo killall xboxdrv
sudo killall xboxdrv
sudo xboxdrv --evdev /dev/input/by-id/usb-DragonRise_Inc._Generic_USB_Joystick-event-joystick --silent --mimic-xpad --evdev-absmap ABS_Z=x1,ABS_Y=y1,ABS_RX=x2,ABS_RZ=y2,ABS_HAT0X=dpad_x,ABS_HAT0Y=dpad_y --axismap -Y1=Y1,-Y2=Y2 --evdev-keymap BTN_THUMB2=a,BTN_THUMB=b,BTN_TOP=x,BTN_TRIGGER=y,BTN_TOP2=lb,BTN_PINKIE=rb,BTN_BASE=lt,BTN_BASE2=rt, BTN_BASE5=tl,BTN_BASE6=tr,BTN_BASE3=back,BTN_BASE4=start &
exit 0
The double "sudo killall xboxdrv" command is required in my setup, I do not know the exact reason, but without this double there is a problem of having an additional js input device listed.
The one catch is that, I have to attach the gamepad to the notebook before booting it. If I attach the gamepad after booting the notebook, the gamepad is not initialized. I could just run the commands manually after attaching the gamepad, but it not possible for everyone using my notebook.
My next step is to find a way to create a script file with the commands, so that I can just double click the script file after attaching the gamepad and this would initialize the gamepad. But I have not yet found a way to incorporate the sudo command in a simple script.
Edit (14 Aug 2018):
Ok, so I now have a way to call the initialization script without using the rc.local file. This is a much better solution and works anytime I want to connect the gamepad. (I got this method from another forum where I had asked this question).
I created my initialization script at /home/mark/.my-controller.sh (here, replace 'mark' with the name of your user)
This .my-controller.sh file has the following code:
#!/bin/bash
sudo killall xboxdrv
sudo killall xboxdrv
sudo xboxdrv --evdev /dev/input/by-id/usb-DragonRise_Inc._Generic_USB_Joystick-event-joystick --silent --mimic-xpad --evdev-absmap ABS_Z=x1,ABS_Y=y1,ABS_RX=x2,ABS_RZ=y2,ABS_HAT0X=dpad_x,ABS_HAT0Y=dpad_y --axismap -Y1=Y1,-Y2=Y2 --evdev-keymap BTN_THUMB2=a,BTN_THUMB=b,BTN_TOP=x,BTN_TRIGGER=y,BTN_TOP2=lb,BTN_PINKIE=rb,BTN_BASE=lt,BTN_BASE2=rt, BTN_BASE5=tl,BTN_BASE6=tr,BTN_BASE3=back,BTN_BASE4=start &
exit 0
Next, in the /etc/sudoers file add this text (replace /home/mark with the name of your home):
# Added manually
mark ALL = NOPASSWD: /home/mark/.my-controller.sh
Next, create the .desktop file that will call this script (replace "/home/mark" with the name of your home) (and, replace 'sakura' with 'gnome-terminal' if you use that):
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Name=StartMyGamepad
Comment=To initialize my gamepad
Exec=sakura -e "bash -c 'sudo /home/mark/.my-controller.sh;$SHELL'"
Icon=utilities-terminal
Terminal=false
Type=Application
Categories=Application;
Now, when you want to use the gamepad, first attach the gamepad to the PC, then open the .desktop file. this will open a terminal asking for your password. Enter your password and the script is run that initializes the gamepad.