Skip to content

Commit f1816bb

Browse files
committed
Mouse button support
1 parent 15dd3dd commit f1816bb

2 files changed

Lines changed: 79 additions & 26 deletions

File tree

pc/include/pc_keybindings.h

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,49 @@
22
#define PC_KEYBINDINGS_H
33

44
#include <SDL2/SDL_scancode.h>
5+
#include <SDL2/SDL_mouse.h>
56

67
#ifdef __cplusplus
78
extern "C" {
89
#endif
910

11+
/* PCInputCode: values 0..SDL_NUM_SCANCODES-1 are keyboard scancodes.
12+
Values with PC_INPUT_MOUSE_BIT set are mouse buttons (low bits = SDL button index). */
13+
typedef int PCInputCode;
14+
15+
#define PC_INPUT_MOUSE_BIT 0x10000
16+
#define PC_INPUT_MOUSE1 (PC_INPUT_MOUSE_BIT | SDL_BUTTON_LEFT) /* left click */
17+
#define PC_INPUT_MOUSE2 (PC_INPUT_MOUSE_BIT | SDL_BUTTON_RIGHT) /* right click */
18+
#define PC_INPUT_MOUSE3 (PC_INPUT_MOUSE_BIT | SDL_BUTTON_MIDDLE) /* middle click */
19+
1020
typedef struct {
1121
/* buttons */
12-
SDL_Scancode a;
13-
SDL_Scancode b;
14-
SDL_Scancode x;
15-
SDL_Scancode y;
16-
SDL_Scancode start;
17-
SDL_Scancode z;
18-
SDL_Scancode l;
19-
SDL_Scancode r;
22+
PCInputCode a;
23+
PCInputCode b;
24+
PCInputCode x;
25+
PCInputCode y;
26+
PCInputCode start;
27+
PCInputCode z;
28+
PCInputCode l;
29+
PCInputCode r;
2030

2131
/* main stick */
22-
SDL_Scancode stick_up;
23-
SDL_Scancode stick_down;
24-
SDL_Scancode stick_left;
25-
SDL_Scancode stick_right;
32+
PCInputCode stick_up;
33+
PCInputCode stick_down;
34+
PCInputCode stick_left;
35+
PCInputCode stick_right;
2636

2737
/* C-stick */
28-
SDL_Scancode cstick_up;
29-
SDL_Scancode cstick_down;
30-
SDL_Scancode cstick_left;
31-
SDL_Scancode cstick_right;
38+
PCInputCode cstick_up;
39+
PCInputCode cstick_down;
40+
PCInputCode cstick_left;
41+
PCInputCode cstick_right;
3242

3343
/* D-pad */
34-
SDL_Scancode dpad_up;
35-
SDL_Scancode dpad_down;
36-
SDL_Scancode dpad_left;
37-
SDL_Scancode dpad_right;
44+
PCInputCode dpad_up;
45+
PCInputCode dpad_down;
46+
PCInputCode dpad_left;
47+
PCInputCode dpad_right;
3848
} PCKeybindings;
3949

4050
extern PCKeybindings g_pc_keybindings;

pc/src/pc_keybindings.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
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+
7082
static 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

Comments
 (0)