Skip to content

Commit

Permalink
Add an option to emulate middle mouse with Alt
Browse files Browse the repository at this point in the history
Useful for laptop with touchpad.
  • Loading branch information
guillaumechereau committed Jul 12, 2024
1 parent 489f910 commit 7040bea
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/gesture.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,30 @@
// XXX: this value should be set depending on the screen resolution.
static float g_start_dist = 8;

static int g_emulate_three_buttons_mouse = 0;

void gesture_set_emulate_three_buttons_mouse(int key)
{
g_emulate_three_buttons_mouse = key;
}

static bool test_button(const inputs_t *inputs, const touch_t *touch, int mask)
{
bool lmb = touch->down[0];
bool mmb = touch->down[1];
bool rmb = touch->down[2];

if (g_emulate_three_buttons_mouse &&
inputs->keys[g_emulate_three_buttons_mouse]) {
mmb = lmb;
lmb = false;
}

if (mask & GESTURE_SHIFT && !inputs->keys[KEY_LEFT_SHIFT]) return false;
if (mask & GESTURE_CTRL && !inputs->keys[KEY_CONTROL]) return false;
if ((mask & GESTURE_LMB) && !touch->down[0]) return false;
if ((mask & GESTURE_MMB) && !touch->down[1]) return false;
if ((mask & GESTURE_RMB) && !touch->down[2]) return false;
if ((mask & GESTURE_LMB) && !lmb) return false;
if ((mask & GESTURE_MMB) && !mmb) return false;
if ((mask & GESTURE_RMB) && !rmb) return false;
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions src/gesture.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@ struct gesture
int gesture_update(int nb, gesture_t *gestures[],
const inputs_t *inputs, const float viewport[4],
void *user);

/*
* Set the key used to emulate middle mouse buttons with left click
* Set to zero to disable.
*/
void gesture_set_emulate_three_buttons_mouse(int key);
3 changes: 3 additions & 0 deletions src/goxel.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ typedef struct goxel
// Arr array of keymap settings (different from shortcuts).
keymap_t *keymaps;

// Can be set to a key code (only KEY_LEFT_ALT is supported for now).
int emulate_three_buttons_mouse;

} goxel_t;

// the global goxel instance.
Expand Down
26 changes: 26 additions & 0 deletions src/gui/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ int gui_settings_popup(void *data)
int ret = 0;
const tr_lang_t *language;
const tr_lang_t *languages;
bool val;

if (gui_section_begin(_(LANGUAGE), GUI_SECTION_COLLAPSABLE)) {
language = tr_get_language();
Expand Down Expand Up @@ -155,6 +156,16 @@ int gui_settings_popup(void *data)
}
} gui_section_end();

if (gui_section_begin("Inputs", GUI_SECTION_COLLAPSABLE_CLOSED)) {
val = goxel.emulate_three_buttons_mouse == KEY_LEFT_ALT;
if (gui_checkbox("Emulate 3 buttons with Alt", &val,
"Emulate Middle Mouse with Alt+Left Mouse.")) {
goxel.emulate_three_buttons_mouse = val ? KEY_LEFT_ALT : 0;
gesture_set_emulate_three_buttons_mouse(
goxel.emulate_three_buttons_mouse);
settings_save();
}
} gui_section_end();

if (gui_section_begin(_(PATHS), GUI_SECTION_COLLAPSABLE_CLOSED)) {
gui_text("Palettes: %s/palettes", sys_get_user_dir());
Expand Down Expand Up @@ -238,6 +249,13 @@ static int settings_ini_handler(void *user, const char *section,
if (strcmp(section, "keymaps") == 0) {
add_keymap(name, value);
}
if (strcmp(section, "inputs") == 0) {
if (strcmp(name, "emulate_three_buttons_mouse") == 0) {
if (strcmp(value, "alt") == 0) {
goxel.emulate_three_buttons_mouse = KEY_LEFT_ALT;
}
}
}
return 0;
}

Expand All @@ -247,8 +265,10 @@ void settings_load(void)
snprintf(path, sizeof(path), "%s/settings.ini", sys_get_user_dir());
LOG_I("Read settings file: %s", path);
arrfree(goxel.keymaps);
goxel.emulate_three_buttons_mouse = 0;
ini_parse(path, settings_ini_handler, NULL);
actions_check_shortcuts();
gesture_set_emulate_three_buttons_mouse(goxel.emulate_three_buttons_mouse);
}

static int shortcut_save_callback(action_t *a, void *user)
Expand Down Expand Up @@ -318,6 +338,12 @@ void settings_save(void)
fprintf(file, "[shortcuts]\n");
actions_iter(shortcut_save_callback, file);

if (goxel.emulate_three_buttons_mouse) {
assert(goxel.emulate_three_buttons_mouse == KEY_LEFT_ALT);
fprintf(file, "[inputs]\n");
fprintf(file, "emulate_three_buttons_mouse=alt");
}

fprintf(file, "\n");
save_keymaps(file);
fprintf(file, "\n");
Expand Down
1 change: 1 addition & 0 deletions src/inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ enum {
KEY_LEFT_SHIFT = 340,
KEY_RIGHT_SHIFT = 344,
KEY_CONTROL = 341,
KEY_LEFT_ALT = 342,
};


Expand Down

0 comments on commit 7040bea

Please sign in to comment.