Guest User

Untitled

a guest
Aug 4th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.63 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5.  
  6. // Controller capabilities structure
  7. typedef struct {
  8.     int  joystick_count;         // Number of analog joysticks (0-2)
  9.     bool has_abxy;               // Has A/B/X/Y buttons (East/South/North/West)
  10.     bool has_start;              // Has Start/Plus button
  11.     bool has_select;             // Has Select/Back/Minus button
  12.     bool has_guide;              // Has Guide/Home button
  13.     bool has_share;              // Has Share/Capture button
  14.     bool has_analog_triggers;
  15.     bool has_digital_triggers;   // Fixed: added missing semicolon
  16.     bool has_bumpers;
  17.     int  digital_paddle_count;   // Digital paddle buttons (0-4)
  18.     int  misc_button_count;      // Misc buttons (0-4, excluding misc2 if touchpad2 present)
  19.     int  touchpad_count;         // Touchpad buttons (0-2)
  20.     bool has_dpad;               // Has D-pad
  21. } controller_capabilities_t;
  22.  
  23. // Generate SDL mapping string based on controller capabilities
  24. char* generate_sdl_mapping(const controller_capabilities_t* caps, bool debug_mask) {
  25.     static char mapping_string[2048];
  26.     uint8_t button_mask[4] = {0};
  27.     int current_button = 0;
  28.     int current_axis = 0;
  29.    
  30.     mapping_string[0] = '\0';
  31.    
  32.     // Helper function to add button mapping
  33.     #define ADD_BUTTON_MAPPING(sdl_name, button_id) \
  34.         do { \
  35.             char temp[32]; \
  36.             snprintf(temp, sizeof(temp), "%s:b%d,", sdl_name, button_id); \
  37.             strcat(mapping_string, temp); \
  38.         } while(0)
  39.    
  40.     // Helper function to add axis mapping
  41.     #define ADD_AXIS_MAPPING(sdl_name, axis_id) \
  42.         do { \
  43.             char temp[32]; \
  44.             snprintf(temp, sizeof(temp), "%s:a%d,", sdl_name, axis_id); \
  45.             strcat(mapping_string, temp); \
  46.         } while(0)
  47.    
  48.     // Analog joysticks (always come first in axis mapping)
  49.     if (caps->joystick_count >= 1) {
  50.         ADD_AXIS_MAPPING("leftx", current_axis++);
  51.         ADD_AXIS_MAPPING("lefty", current_axis++);
  52.     }
  53.     if (caps->joystick_count >= 2) {
  54.         ADD_AXIS_MAPPING("rightx", current_axis++);
  55.         ADD_AXIS_MAPPING("righty", current_axis++);
  56.     }
  57.    
  58.     // Analog triggers
  59.     if (caps->has_analog_triggers && !caps->has_digital_triggers) {
  60.         ADD_AXIS_MAPPING("lefttrigger", current_axis++);
  61.         ADD_AXIS_MAPPING("righttrigger", current_axis++);
  62.     }
  63.    
  64.     // BAYX buttons (East, South, North, West)
  65.     if (caps->has_abxy) {
  66.         ADD_BUTTON_MAPPING("b", current_button++);      // East  (typically B on Xbox, Circle on PlayStation)
  67.         ADD_BUTTON_MAPPING("a", current_button++);      // South (typically A on Xbox, X on PlayStation)
  68.         ADD_BUTTON_MAPPING("y", current_button++);      // North (typically Y on Xbox, Triangle on PlayStation)
  69.         ADD_BUTTON_MAPPING("x", current_button++);      // West  (typically X on Xbox, Square on PlayStation)
  70.         button_mask[0] |= 0x0F;
  71.     }
  72.    
  73.     // D-pad (hat 0)
  74.     if (caps->has_dpad) {
  75.         strcat(mapping_string, "dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,");
  76.         button_mask[0] |= 0xF0;
  77.     }
  78.    
  79.     // Left and Right stick buttons
  80.     if (caps->joystick_count >= 1) {
  81.         ADD_BUTTON_MAPPING("leftstick", current_button++);
  82.         button_mask[1] |= 0x01;
  83.     }
  84.     if (caps->joystick_count >= 2) {
  85.         ADD_BUTTON_MAPPING("rightstick", current_button++);
  86.         button_mask[1] |= 0x02;
  87.     }
  88.    
  89.     // Digital shoulder buttons (L/R Shoulder)
  90.     if (caps->has_bumpers) {
  91.         ADD_BUTTON_MAPPING("leftshoulder", current_button++);
  92.         button_mask[1] |= 0x04;
  93.         ADD_BUTTON_MAPPING("rightshoulder", current_button++);
  94.         button_mask[1] |= 0x08;
  95.     }
  96.  
  97.     // Digital trigger buttons (capability overrides analog)
  98.     if(caps->has_digital_triggers) {
  99.         ADD_BUTTON_MAPPING("lefttrigger", current_button++);
  100.         button_mask[1] |= 0x10;
  101.         ADD_BUTTON_MAPPING("righttrigger", current_button++);
  102.         button_mask[1] |= 0x20;
  103.     }
  104.    
  105.     // Paddle 1/2
  106.     if(caps->digital_paddle_count >= 1) {
  107.         ADD_BUTTON_MAPPING("paddle1", current_button++);
  108.         button_mask[1] |= 0x40;
  109.     }
  110.  
  111.     if(caps->digital_paddle_count >= 2) {
  112.         ADD_BUTTON_MAPPING("paddle2", current_button++);
  113.         button_mask[1] |= 0x80;
  114.     }
  115.  
  116.     // Start/Plus button
  117.     if (caps->has_start) {
  118.         ADD_BUTTON_MAPPING("start", current_button++);
  119.         button_mask[2] |= 0x01;
  120.     }
  121.    
  122.     // Select/Back/Minus button (button 17)
  123.     if (caps->has_select) {
  124.         ADD_BUTTON_MAPPING("back", current_button++);
  125.         button_mask[2] |= 0x02;
  126.     }
  127.    
  128.     // Guide/Home button (button 18)
  129.     if (caps->has_guide) {
  130.         ADD_BUTTON_MAPPING("guide", current_button++);
  131.         button_mask[2] |= 0x04;
  132.     }
  133.    
  134.     // Share/Capture button (button 19 - Misc 1)
  135.     if (caps->has_share) {
  136.         ADD_BUTTON_MAPPING("misc1", current_button++);
  137.         button_mask[2] |= 0x08;
  138.     }
  139.  
  140.     // Paddle 3/4
  141.     if(caps->digital_paddle_count >= 3) {
  142.         ADD_BUTTON_MAPPING("paddle3", current_button++);
  143.         button_mask[2] |= 0x10;
  144.     }
  145.  
  146.     if(caps->digital_paddle_count >= 4) {
  147.         ADD_BUTTON_MAPPING("paddle4", current_button++);
  148.         button_mask[2] |= 0x20;
  149.     }
  150.    
  151.     // Touchpad buttons
  152.     if (caps->touchpad_count >= 1) {
  153.         ADD_BUTTON_MAPPING("touchpad", current_button++);
  154.         button_mask[2] |= 0x40;
  155.     }
  156.     if (caps->touchpad_count >= 2) {
  157.         ADD_BUTTON_MAPPING("misc2", current_button++); // Second touchpad button is misc2
  158.         button_mask[2] |= 0x80;
  159.     }
  160.    
  161.     // Additional misc buttons (Misc 3-6)
  162.     if(caps->misc_button_count >= 1) {
  163.         ADD_BUTTON_MAPPING("misc3", current_button++);
  164.         button_mask[3] |= 0x01;
  165.     }
  166.    
  167.     if(caps->misc_button_count >= 2) {
  168.         ADD_BUTTON_MAPPING("misc4", current_button++);
  169.         button_mask[3] |= 0x02;
  170.     }
  171.  
  172.     if(caps->misc_button_count >= 3) {
  173.         ADD_BUTTON_MAPPING("misc5", current_button++);
  174.         button_mask[3] |= 0x04;
  175.     }
  176.  
  177.     if(caps->misc_button_count >= 4) {
  178.         ADD_BUTTON_MAPPING("misc6", current_button++);
  179.         button_mask[3] |= 0x08;
  180.     }
  181.    
  182.     // Remove trailing comma
  183.     size_t len = strlen(mapping_string);
  184.     if (len > 0 && mapping_string[len-1] == ',') {
  185.         mapping_string[len-1] = '\0';
  186.     }
  187.    
  188.     // Debug: Print button mask
  189.     if (debug_mask) {
  190.         printf("Button Mask (4 bytes): 0x%02X 0x%02X 0x%02X 0x%02X\n",
  191.               button_mask[0], button_mask[1], button_mask[2], button_mask[3]);
  192.         printf("Button Mask (binary): ");
  193.         for (int i = 0; i < 4; i++) {
  194.             for (int j = 7; j >= 0; j--) {
  195.                 printf("%d", (button_mask[i] >> j) & 1);
  196.             }
  197.             printf(" ");
  198.         }
  199.         printf("\n");
  200.  
  201.         printf("Buttons used: %d", current_button);
  202.         printf("\n");
  203.     }
  204.    
  205.     return mapping_string;
  206. }
  207.  
  208.  
  209. int main() {
  210.     printf("SDL Controller Mapping Generator Test\n");
  211.     printf("=====================================\n");
  212.    
  213.     controller_capabilities_t caps = {
  214.       .joystick_count = 1,
  215.       .has_abxy = true,
  216.       .has_dpad = true,
  217.       .has_analog_triggers = true,
  218.       .has_digital_triggers = false,
  219.       .misc_button_count = 4,
  220.       .digital_paddle_count = 4,
  221.       .has_bumpers = true,
  222.       .has_guide = true,
  223.       .has_share = true,
  224.       .has_start = true,
  225.       .has_select = true,
  226.       .touchpad_count = 2
  227.     };
  228.  
  229.     // Generate mapping
  230.     char* mapping = generate_sdl_mapping(&caps, true);
  231.     printf("\nSDL Mapping:\n%s\n", mapping);
  232.    
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment