1- /* pc_keybindings.c - customizable keyboard bindings loaded from keybindings.ini */
21#include "pc_keybindings.h"
32#include "pc_platform.h"
43
@@ -67,6 +66,19 @@ static const KeybindEntry s_entries[] = {
6766
6867#define NUM_ENTRIES (sizeof(s_entries) / sizeof(s_entries[0]))
6968
69+ /* mouse button name table */
70+ typedef struct {
71+ const char * name ;
72+ PCInputCode code ;
73+ } MouseButtonEntry ;
74+
75+ static const MouseButtonEntry s_mouse_buttons [] = {
76+ { "Mouse1" , PC_INPUT_MOUSE1 },
77+ { "Mouse2" , PC_INPUT_MOUSE2 },
78+ { "Mouse3" , PC_INPUT_MOUSE3 },
79+ };
80+ #define NUM_MOUSE_BUTTONS (sizeof(s_mouse_buttons) / sizeof(s_mouse_buttons[0]))
81+
7082static const char * skip_ws (const char * s ) {
7183 while (* s == ' ' || * s == '\t' ) s ++ ;
7284 return s ;
@@ -80,16 +92,46 @@ static void trim_end(char* s) {
8092 }
8193}
8294
83- static void apply_keybind (const char * key , const char * value ) {
95+ /* get display name for an input code */
96+ static const char * input_code_name (PCInputCode code ) {
97+ if (code & PC_INPUT_MOUSE_BIT ) {
98+ for (int i = 0 ; i < (int )NUM_MOUSE_BUTTONS ; i ++ ) {
99+ if (s_mouse_buttons [i ].code == code )
100+ return s_mouse_buttons [i ].name ;
101+ }
102+ return "Unknown" ;
103+ }
104+ return SDL_GetScancodeName ((SDL_Scancode )code );
105+ }
106+
107+ /* parse an input value string to a PCInputCode */
108+ static PCInputCode parse_input_code (const char * value ) {
109+ /* check mouse button names first (case-insensitive) */
110+ for (int i = 0 ; i < (int )NUM_MOUSE_BUTTONS ; i ++ ) {
111+ if (SDL_strcasecmp (value , s_mouse_buttons [i ].name ) == 0 ) {
112+ return s_mouse_buttons [i ].code ;
113+ }
114+ }
115+
116+ /* fall back to SDL scancode */
84117 SDL_Scancode sc = SDL_GetScancodeFromName (value );
85- if (sc == SDL_SCANCODE_UNKNOWN ) {
118+ if (sc != SDL_SCANCODE_UNKNOWN ) {
119+ return (PCInputCode )sc ;
120+ }
121+
122+ return -1 ; /* invalid */
123+ }
124+
125+ static void apply_keybind (const char * key , const char * value ) {
126+ PCInputCode code = parse_input_code (value );
127+ if (code < 0 ) {
86128 printf ("[Keybindings] WARNING: unknown key name '%s' for '%s'\n" , value , key );
87129 return ;
88130 }
89131
90132 for (int i = 0 ; i < (int )NUM_ENTRIES ; i ++ ) {
91133 if (strcmp (key , s_entries [i ].ini_key ) == 0 ) {
92- * (SDL_Scancode * )((char * )& g_pc_keybindings + s_entries [i ].offset ) = sc ;
134+ * (PCInputCode * )((char * )& g_pc_keybindings + s_entries [i ].offset ) = code ;
93135 return ;
94136 }
95137 }
@@ -105,13 +147,14 @@ static void write_defaults(const char* path) {
105147 fprintf (f , "# Common names: Space, Left Shift, Right Shift, Left Ctrl, Right Ctrl,\n" );
106148 fprintf (f , "# Left Alt, Right Alt, Return, Escape, Tab, Backspace, Delete,\n" );
107149 fprintf (f , "# A-Z, 0-9, F1-F12, Up, Down, Left, Right, etc.\n" );
150+ fprintf (f , "# Mouse buttons: Mouse1 (left), Mouse2 (right), Mouse3 (middle)\n" );
108151 fprintf (f , "# Full list: https://wiki.libsdl.org/SDL2/SDL_Scancode\n" );
109152 fprintf (f , "\n" );
110153 fprintf (f , "# Buttons\n" );
111154
112155 for (int i = 0 ; i < (int )NUM_ENTRIES ; i ++ ) {
113- SDL_Scancode sc = * (SDL_Scancode * )((char * )& g_pc_keybindings + s_entries [i ].offset );
114- const char * name = SDL_GetScancodeName ( sc );
156+ PCInputCode code = * (PCInputCode * )((char * )& g_pc_keybindings + s_entries [i ].offset );
157+ const char * name = input_code_name ( code );
115158 fprintf (f , "%s = %s\n" , s_entries [i ].ini_key , name );
116159
117160 /* blank line separators between sections */
0 commit comments