Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdbool.h>
- // Controller capabilities structure
- typedef struct {
- int joystick_count; // Number of analog joysticks (0-2)
- bool has_abxy; // Has A/B/X/Y buttons (East/South/North/West)
- bool has_start; // Has Start/Plus button
- bool has_select; // Has Select/Back/Minus button
- bool has_guide; // Has Guide/Home button
- bool has_share; // Has Share/Capture button
- bool has_analog_triggers;
- bool has_digital_triggers; // Fixed: added missing semicolon
- bool has_bumpers;
- int digital_paddle_count; // Digital paddle buttons (0-4)
- int misc_button_count; // Misc buttons (0-4, excluding misc2 if touchpad2 present)
- int touchpad_count; // Touchpad buttons (0-2)
- bool has_dpad; // Has D-pad
- } controller_capabilities_t;
- // Generate SDL mapping string based on controller capabilities
- char* generate_sdl_mapping(const controller_capabilities_t* caps, bool debug_mask) {
- static char mapping_string[2048];
- uint8_t button_mask[4] = {0};
- int current_button = 0;
- int current_axis = 0;
- mapping_string[0] = '\0';
- // Helper function to add button mapping
- #define ADD_BUTTON_MAPPING(sdl_name, button_id) \
- do { \
- char temp[32]; \
- snprintf(temp, sizeof(temp), "%s:b%d,", sdl_name, button_id); \
- strcat(mapping_string, temp); \
- } while(0)
- // Helper function to add axis mapping
- #define ADD_AXIS_MAPPING(sdl_name, axis_id) \
- do { \
- char temp[32]; \
- snprintf(temp, sizeof(temp), "%s:a%d,", sdl_name, axis_id); \
- strcat(mapping_string, temp); \
- } while(0)
- // Analog joysticks (always come first in axis mapping)
- if (caps->joystick_count >= 1) {
- ADD_AXIS_MAPPING("leftx", current_axis++);
- ADD_AXIS_MAPPING("lefty", current_axis++);
- }
- if (caps->joystick_count >= 2) {
- ADD_AXIS_MAPPING("rightx", current_axis++);
- ADD_AXIS_MAPPING("righty", current_axis++);
- }
- // Analog triggers
- if (caps->has_analog_triggers && !caps->has_digital_triggers) {
- ADD_AXIS_MAPPING("lefttrigger", current_axis++);
- ADD_AXIS_MAPPING("righttrigger", current_axis++);
- }
- // BAYX buttons (East, South, North, West)
- if (caps->has_abxy) {
- ADD_BUTTON_MAPPING("b", current_button++); // East (typically B on Xbox, Circle on PlayStation)
- ADD_BUTTON_MAPPING("a", current_button++); // South (typically A on Xbox, X on PlayStation)
- ADD_BUTTON_MAPPING("y", current_button++); // North (typically Y on Xbox, Triangle on PlayStation)
- ADD_BUTTON_MAPPING("x", current_button++); // West (typically X on Xbox, Square on PlayStation)
- button_mask[0] |= 0x0F;
- }
- // D-pad (hat 0)
- if (caps->has_dpad) {
- strcat(mapping_string, "dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,");
- button_mask[0] |= 0xF0;
- }
- // Left and Right stick buttons
- if (caps->joystick_count >= 1) {
- ADD_BUTTON_MAPPING("leftstick", current_button++);
- button_mask[1] |= 0x01;
- }
- if (caps->joystick_count >= 2) {
- ADD_BUTTON_MAPPING("rightstick", current_button++);
- button_mask[1] |= 0x02;
- }
- // Digital shoulder buttons (L/R Shoulder)
- if (caps->has_bumpers) {
- ADD_BUTTON_MAPPING("leftshoulder", current_button++);
- button_mask[1] |= 0x04;
- ADD_BUTTON_MAPPING("rightshoulder", current_button++);
- button_mask[1] |= 0x08;
- }
- // Digital trigger buttons (capability overrides analog)
- if(caps->has_digital_triggers) {
- ADD_BUTTON_MAPPING("lefttrigger", current_button++);
- button_mask[1] |= 0x10;
- ADD_BUTTON_MAPPING("righttrigger", current_button++);
- button_mask[1] |= 0x20;
- }
- // Paddle 1/2
- if(caps->digital_paddle_count >= 1) {
- ADD_BUTTON_MAPPING("paddle1", current_button++);
- button_mask[1] |= 0x40;
- }
- if(caps->digital_paddle_count >= 2) {
- ADD_BUTTON_MAPPING("paddle2", current_button++);
- button_mask[1] |= 0x80;
- }
- // Start/Plus button
- if (caps->has_start) {
- ADD_BUTTON_MAPPING("start", current_button++);
- button_mask[2] |= 0x01;
- }
- // Select/Back/Minus button (button 17)
- if (caps->has_select) {
- ADD_BUTTON_MAPPING("back", current_button++);
- button_mask[2] |= 0x02;
- }
- // Guide/Home button (button 18)
- if (caps->has_guide) {
- ADD_BUTTON_MAPPING("guide", current_button++);
- button_mask[2] |= 0x04;
- }
- // Share/Capture button (button 19 - Misc 1)
- if (caps->has_share) {
- ADD_BUTTON_MAPPING("misc1", current_button++);
- button_mask[2] |= 0x08;
- }
- // Paddle 3/4
- if(caps->digital_paddle_count >= 3) {
- ADD_BUTTON_MAPPING("paddle3", current_button++);
- button_mask[2] |= 0x10;
- }
- if(caps->digital_paddle_count >= 4) {
- ADD_BUTTON_MAPPING("paddle4", current_button++);
- button_mask[2] |= 0x20;
- }
- // Touchpad buttons
- if (caps->touchpad_count >= 1) {
- ADD_BUTTON_MAPPING("touchpad", current_button++);
- button_mask[2] |= 0x40;
- }
- if (caps->touchpad_count >= 2) {
- ADD_BUTTON_MAPPING("misc2", current_button++); // Second touchpad button is misc2
- button_mask[2] |= 0x80;
- }
- // Additional misc buttons (Misc 3-6)
- if(caps->misc_button_count >= 1) {
- ADD_BUTTON_MAPPING("misc3", current_button++);
- button_mask[3] |= 0x01;
- }
- if(caps->misc_button_count >= 2) {
- ADD_BUTTON_MAPPING("misc4", current_button++);
- button_mask[3] |= 0x02;
- }
- if(caps->misc_button_count >= 3) {
- ADD_BUTTON_MAPPING("misc5", current_button++);
- button_mask[3] |= 0x04;
- }
- if(caps->misc_button_count >= 4) {
- ADD_BUTTON_MAPPING("misc6", current_button++);
- button_mask[3] |= 0x08;
- }
- // Remove trailing comma
- size_t len = strlen(mapping_string);
- if (len > 0 && mapping_string[len-1] == ',') {
- mapping_string[len-1] = '\0';
- }
- // Debug: Print button mask
- if (debug_mask) {
- printf("Button Mask (4 bytes): 0x%02X 0x%02X 0x%02X 0x%02X\n",
- button_mask[0], button_mask[1], button_mask[2], button_mask[3]);
- printf("Button Mask (binary): ");
- for (int i = 0; i < 4; i++) {
- for (int j = 7; j >= 0; j--) {
- printf("%d", (button_mask[i] >> j) & 1);
- }
- printf(" ");
- }
- printf("\n");
- printf("Buttons used: %d", current_button);
- printf("\n");
- }
- return mapping_string;
- }
- int main() {
- printf("SDL Controller Mapping Generator Test\n");
- printf("=====================================\n");
- controller_capabilities_t caps = {
- .joystick_count = 1,
- .has_abxy = true,
- .has_dpad = true,
- .has_analog_triggers = true,
- .has_digital_triggers = false,
- .misc_button_count = 4,
- .digital_paddle_count = 4,
- .has_bumpers = true,
- .has_guide = true,
- .has_share = true,
- .has_start = true,
- .has_select = true,
- .touchpad_count = 2
- };
- // Generate mapping
- char* mapping = generate_sdl_mapping(&caps, true);
- printf("\nSDL Mapping:\n%s\n", mapping);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment