Advertisement
Guest User

daphne.sh

a guest
Feb 17th, 2016
303
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2.  
  3. # This file is part of The RetroPie Project
  4. #
  5. # The RetroPie Project is the legal property of its developers, whose names are
  6. # too numerous to list here. Please refer to the COPYRIGHT.md file distributed with this source.
  7. #
  8. # See the LICENSE.md file at the top-level directory of this distribution and
  9. # at https://raw.githubusercontent.com/RetroPie/RetroPie-Setup/master/LICENSE.md
  10. #
  11.  
  12. function onstart_daphne_joystick() {
  13.     local -r mapping_file="$configdir/daphne/dapinput.ini"
  14.     local -r force_joy_file="$configdir/daphne/dapinput-forcejoy.ini"
  15.     local -r force_key_file="$configdir/daphne/dapinput-forcekey.ini"
  16.  
  17.     if [[ ! -f "$mapping_file" ]]; then
  18.       cat > "$mapping_file" << _EOF_
  19. # Daphne cutom keyboard and joystick mapping
  20. #
  21. # Each input is mapped to 2 keyboard keys and one joystick button.
  22. # A joystick's first analog stick is also automatically mapped.
  23. #
  24. # The first two numbers are SDL keyboard codes (or 0 for "none")
  25. # Find keyboard codes here:
  26. # http://www.daphne-emu.com/mediawiki/index.php/KeyList
  27. #
  28. # The third number is the joystick button code (or 0 for "none")
  29. # Since 0 is reserved for special meaning, joystick button 0 is identified
  30. # as 1 here.  Button 1 is identified as 2, and so on.
  31. #
  32. # Find the button you want to map by running:
  33. # jstest /dev/input/js0
  34.  
  35. [KEYBOARD]
  36. KEY_UP = 273 114 5
  37. KEY_DOWN = 274 102 7
  38. KEY_LEFT = 276 100 8
  39. KEY_RIGHT = 275 103 6
  40. KEY_BUTTON1 = 306 97 14
  41. KEY_BUTTON2 = 308 115 15
  42. KEY_BUTTON3 = 32 113 16
  43. KEY_START1 = 49 0 4
  44. KEY_START2 = 50 0 0
  45. KEY_COIN1 = 53 0 1
  46. KEY_COIN2 = 54 0 0
  47. KEY_SKILL1 = 304 119 0
  48. KEY_SKILL2 = 122 105 0
  49. KEY_SKILL3 = 120 107 0
  50. KEY_SERVICE = 57 0 0
  51. KEY_TEST = 283 0 0
  52. KEY_RESET = 284 0 0
  53. KEY_SCREENSHOT = 293 0 0
  54. KEY_QUIT = 27 113 17
  55. END
  56. _EOF_
  57.     fi
  58.  
  59.     if [[ ! -f "$force_joy_file" ]]; then
  60.       cat > "$force_joy_file" << _EOF_
  61. # Daphne custom joystick mapping
  62. #
  63. # Any inputs defined below will map a joystick button to
  64. # Daphne input, regardless of remapping that occurs in emulationstation.
  65. #
  66. # Each input is mapped to 1 joystick button (or 0 for "none")
  67. #
  68. # Find joystick button codes by running:
  69. # $ jstest /dev/input/js0
  70. # and ADDING ONE to the button code you want.
  71. #
  72. # Example: Quit will always be js button 14
  73. # KEY_QUIT = 15
  74. #
  75. # (Place all entries after [KEYBOARD])
  76.  
  77. [KEYBOARD]
  78. END
  79. _EOF_
  80.     fi
  81.  
  82.     if [[ ! -f "$force_key_file" ]]; then
  83.       cat > "$force_key_file" << _EOF_
  84. # Daphne custom keyboard mapping
  85. #
  86. # Any inputs defined below will map keyboard keys to
  87. # Daphne input, regardless of remapping that occurs in emulationstation.
  88. #
  89. # Each input is mapped to 2 keyboard key codes (or 0 for "none")
  90. #
  91. # Find keyboard codes here:
  92. # http://www.daphne-emu.com/mediawiki/index.php/KeyList
  93. #
  94. # Example: Quit will always be key [Esc] or [Q]
  95. # KEY_QUIT = 27 113
  96. #
  97. # (Place all entries after [KEYBOARD])
  98.  
  99. [KEYBOARD]
  100. END
  101. _EOF_
  102.     fi
  103. }
  104.  
  105. function map_daphne_joystick() {
  106.     local input_name="$3"
  107.     local input_id="$5"
  108.  
  109.     local -r mapping_file="$configdir/daphne/dapinput.ini"
  110.     local -r force_joy_file="$configdir/daphne/dapinput-forcejoy.ini"
  111.     local -r force_key_file="$configdir/daphne/dapinput-forcekey.ini"
  112.  
  113.     local key
  114.     case "$input_name" in
  115.         up)
  116.             key="KEY_UP"
  117.             ;;
  118.         down)
  119.             key="KEY_DOWN"
  120.             ;;
  121.         left)
  122.             key="KEY_LEFT"
  123.             ;;
  124.         right)
  125.             key="KEY_RIGHT"
  126.             ;;
  127.         a)
  128.             key="KEY_BUTTON1"
  129.             ;;
  130.         b)
  131.             key="KEY_BUTTON2"
  132.             ;;
  133.         x)
  134.             key="KEY_BUTTON3"
  135.             ;;
  136.         y)
  137.             key="KEY_COIN1"
  138.             ;;
  139.         leftbottom)
  140.             key="KEY_SKILL1"
  141.             ;;
  142.         rightbottom)
  143.             key="KEY_SKILL2"
  144.             ;;
  145.         lefttop)
  146.             key="KEY_SKILL3"
  147.             ;;
  148.         righttop)
  149.             key="KEY_SERVICE"
  150.             ;;
  151.         start)
  152.             key="KEY_START1"
  153.             ;;
  154.         select)
  155.             key="KEY_QUIT"
  156.             ;;
  157.         *)
  158.             return
  159.             ;;
  160.     esac
  161.  
  162.     local key_regex="^$key = (.*) (.*)\$"
  163.     local button_regex="^$key = (.*)\$"
  164.     local full_regex="^$key = (.*) (.*) (.*)\$"
  165.     local line
  166.     local key1
  167.     local key2
  168.     local button
  169.  
  170.     # See if this key is specified in the override file...
  171.     while read -r line; do
  172.         if [[ $line =~ $key_regex ]]; then
  173.           key1="${BASH_REMATCH[1]}"
  174.           key2="${BASH_REMATCH[2]}"
  175.           echo "$key1 $key2"
  176.         fi
  177.     done < "$force_key_file"
  178.  
  179.     # ...otherwise, use the defaults file.
  180.     if [[ $key1 = "" || $key2 = "" ]]; then
  181.       echo "Keymap not found in $force_key_file"
  182.       while read -r line; do
  183.         if [[ $line =~ $full_regex ]]; then
  184.           key1="${BASH_REMATCH[1]}"
  185.           key2="${BASH_REMATCH[2]}"
  186.           echo "$key1 $key2"
  187.         fi
  188.       done < "$mapping_file"
  189.     fi
  190.  
  191.     # See if this button is specified in the override file...
  192.     while read -r line; do
  193.         if [[ $line =~ $button_regex ]]; then
  194.           button="${BASH_REMATCH[1]}"
  195.           echo "$button"
  196.         fi
  197.     done < "$force_joy_file"
  198.  
  199.     # ...otherwise, use the config sent to this function.
  200.     if [[ $button = "" ]]; then
  201.       echo "Buttonmap not found in $force_joy_file"
  202.       while read -r line; do
  203.         if [[ $line =~ $key_regex ]]; then
  204.           button=$(expr "$input_id" + 1)
  205.           echo "$button"
  206.         fi
  207.       done < "$mapping_file"
  208.     fi
  209.  
  210.     # Write new button config
  211.     echo "Mapping $key to $key1, $key2, $button"
  212.     local sedexpr="s/^$key = .* .* .*\$/$key = $key1 $key2 $button/g"
  213.     echo "$sedexpr"
  214.     sed -i "$sedexpr" "$mapping_file"
  215. }
Advertisement
RAW Paste Data Copied
Advertisement