Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement keyboard rebinding support #1301

Closed
wants to merge 27 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b2e81c9
Fabxx Nov 30, 2022
c8a8c5d
Empty previous keyboard config before remappig, store new config into…
Fabxx Dec 1, 2022
154bf73
update comments
Fabxx Dec 1, 2022
e345c02
remove highlight shader while binding, give names of buttons while bi…
Fabxx Dec 1, 2022
0880d67
don't clean array of bindings at each iteration. Don't use animations…
Fabxx Dec 1, 2022
d81bc5e
add checks for duplicated keybindings
Fabxx Dec 1, 2022
13490b0
Add notification message for duplicated warning
Fabxx Dec 1, 2022
5051911
Tell the already mapped button what is used for
Fabxx Dec 1, 2022
eb04bf5
check that auto binding is not enabled before remapping
Fabxx Dec 1, 2022
323f995
Fabxx Dec 1, 2022
eb3df6b
Remove FIXME
Fabxx Dec 1, 2022
a95cdb0
Adjust indentation, remove unnecessary checks, update auto-bind descr…
Fabxx Dec 2, 2022
b828dfd
Unbind keyboard focus from input UI while remapping
Fabxx Dec 2, 2022
1d3bb78
added more checks for mapping, using toggle style instead of buttons.…
Fabxx Dec 4, 2022
4a0c89f
make the user rebind button if input out of SDL range.
Fabxx Dec 4, 2022
0bea80b
disable rumble anim, add more checks for remapping. Distinguish warni…
Fabxx Dec 5, 2022
b18a1a5
update analog anim comment
Fabxx Dec 5, 2022
241aea6
remove redundant warning message when aborting remapping process
Fabxx Dec 5, 2022
de95d9c
update Toggle description
Fabxx Dec 5, 2022
46a0015
Update UI messages and restore window change check
Fabxx Dec 5, 2022
2668daf
remove window focus depency when remapping.
Fabxx Dec 6, 2022
ab396c7
remove boolean leftover
Fabxx Dec 6, 2022
159e571
adjust indentation
Fabxx Dec 6, 2022
a5be7a3
Fabxx Dec 6, 2022
d4199b3
Fix ESC button making exit from menu
Fabxx Dec 6, 2022
6883c4e
disable analog and trigger anims while mapping, remove useless new line
Fabxx Dec 6, 2022
c54a930
disable stick press animation while remapping
Fabxx Dec 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
No commit message
Fabxx committed Nov 30, 2022
commit b2e81c983452a718679b5978670fca6bb1964a46
25 changes: 24 additions & 1 deletion ui/xemu-input.c
Original file line number Diff line number Diff line change
@@ -298,7 +298,9 @@ void xemu_input_process_sdl_events(const SDL_Event *event)
}
} else if (event->type == SDL_CONTROLLERDEVICEREMAPPED) {
DPRINTF("Controller Remapped: %d\n", event->cdevice.which);
}
} else if (is_remapping_active){
Fabxx marked this conversation as resolved.
Show resolved Hide resolved
xemu_input_rebind(event);
}
}

void xemu_input_update_controller(ControllerState *state)
@@ -353,6 +355,27 @@ void xemu_input_update_sdl_kbd_controller_state(ControllerState *state)
if (kbd[sdl_kbd_scancode_map[24]]) state->axis[CONTROLLER_AXIS_RTRIG] = 32767;
}

void xemu_input_rebind(const SDL_Event *ev)
{
//FIXME: If we select a controller and start rebinding, it overrides the previous keyboard binding in a bad way
//TODO: Divide remapping for controller/keyboard
//TODO: Store updated mapping into config file for next boot
sdl_kbd_scancode_map[currently_remapping] = NULL;
if(ev->type == SDL_KEYDOWN){
sdl_kbd_scancode_map[currently_remapping] = ev->key.keysym.scancode;
currently_remapping++;
if(currently_remapping == 25){
is_remapping_active = false;
}
}

if( (sdl_kbd_scancode_map[currently_remapping] < SDL_SCANCODE_UNKNOWN) ||
(sdl_kbd_scancode_map[currently_remapping] >= SDL_NUM_SCANCODES) ) {
fprintf(stderr, "WARNING: Keyboard controller map scancode out of range (%d) : Disabled\n", sdl_kbd_scancode_map[currently_remapping]);
sdl_kbd_scancode_map[currently_remapping] = SDL_SCANCODE_UNKNOWN;
}
}

void xemu_input_update_sdl_controller_state(ControllerState *state)
{
state->buttons = 0;
6 changes: 6 additions & 0 deletions ui/xemu-input.h
Original file line number Diff line number Diff line change
@@ -92,10 +92,15 @@ typedef struct ControllerState {
void *device; // DeviceState opaque
} ControllerState;

struct sdl2_console *get_scon_from_window(uint32_t window_id);

typedef QTAILQ_HEAD(, ControllerState) ControllerStateList;
extern ControllerStateList available_controllers;
extern ControllerState *bound_controllers[4];

extern bool is_remapping_active;
extern int currently_remapping;

#ifdef __cplusplus
extern "C" {
#endif
@@ -107,6 +112,7 @@ void xemu_input_update_controller(ControllerState *state);
void xemu_input_update_sdl_kbd_controller_state(ControllerState *state);
void xemu_input_update_sdl_controller_state(ControllerState *state);
void xemu_input_update_rumble(ControllerState *state);
void xemu_input_rebind(const SDL_Event *ev);
ControllerState *xemu_input_get_bound(int index);
void xemu_input_bind(int index, ControllerState *state, int save);
int xemu_input_get_controller_default_bind_port(ControllerState *state, int start);
2 changes: 1 addition & 1 deletion ui/xemu.c
Original file line number Diff line number Diff line change
@@ -132,7 +132,7 @@ void xemu_toggle_fullscreen(void)

// static void sdl_update_caption(struct sdl2_console *scon);

static struct sdl2_console *get_scon_from_window(uint32_t window_id)
struct sdl2_console *get_scon_from_window(uint32_t window_id)
{
int i;
for (i = 0; i < sdl2_num_outputs; i++) {
8 changes: 8 additions & 0 deletions ui/xui/gl-helpers.cc
Original file line number Diff line number Diff line change
@@ -542,6 +542,14 @@ void RenderController(float frame_x, float frame_y, uint32_t primary_color,
}
}

//Highlight the current button to rebind when rebinding.
if (is_remapping_active)
{
RenderDecal(g_decal_shader, frame_x + buttons[currently_remapping].x,
frame_y + buttons[currently_remapping].y, buttons[currently_remapping].w, buttons[currently_remapping].h, 0,
0, 1, 1, 0, 0, primary_color + 0xff);
}

Fabxx marked this conversation as resolved.
Show resolved Hide resolved
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Blend with controller

// Render left thumbstick
13 changes: 13 additions & 0 deletions ui/xui/main-menu.cc
Original file line number Diff line number Diff line change
@@ -38,6 +38,8 @@
#include "../xemu-xbe.h"

MainMenuScene g_main_menu;
bool is_remapping_active = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globals should be prefixed with g_

int currently_remapping = 0;

MainMenuTabView::~MainMenuTabView() {}
void MainMenuTabView::Draw() {}
@@ -263,6 +265,17 @@ void MainMenuInputView::Draw()
Toggle("Background controller input capture",
&g_config.input.background_input_capture,
"Capture even if window is unfocused (requires restart)");

if (ImGui::IsItemClicked(ImGui::Button("Rebind Controls")))
Fabxx marked this conversation as resolved.
Show resolved Hide resolved
{
currently_remapping = 0;
is_remapping_active = true;
}

if (is_remapping_active)
{
ImGui::Text("Press the key you want to bind to the highlighted button. Keys left to bind: %d", 25-currently_remapping);
}
}

void MainMenuDisplayView::Draw()