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

feat: Add support for mouse axes #1

Open
wants to merge 2 commits into
base: mouse-buttons
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ virtual keyboard object. To help performing a more complicated sequence of key p
# Hold the Right key for 1000ms
wtype -P right -s 1000 -p right
```

```
# Scroll Verticaly
wtype -A axis_v 75
wtype -A axis_v -75

# Scroll Horizontaly
wtype -A axis_h 75
wtype -A axis_h -75
```
57 changes: 54 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
#include <wchar.h>
#include <xkbcommon/xkbcommon.h>
Expand All @@ -30,6 +31,7 @@ enum wtype_command_type {
WTYPE_COMMAND_BUTTON_PRESS = 7,
WTYPE_COMMAND_BUTTON_RELEASE = 8,
WTYPE_COMMAND_BUTTON_CLICK = 9,
WTYPE_COMMAND_AXIS = 10,
};


Expand All @@ -53,7 +55,13 @@ enum wtype_btn {
WTYPE_BTN_EXTRA = 0x114,
WTYPE_BTN_FORWARD = 0x115,
WTYPE_BTN_BACK = 0x116,
WTYPE_BTN_TASK = 0x117
WTYPE_BTN_TASK = 0x117,
};

enum wtype_axis {
WTYPE_AXIS_NONE = 0,
WTYPE_AXIS_REL_WHEEL = 0x08,
WTYPE_AXIS_REL_HWHEEL = 0x06
};


Expand All @@ -69,6 +77,11 @@ struct wtype_command {
enum wtype_mod mod;
uint32_t button;
unsigned int sleep_ms;
struct {
uint32_t axis;
wl_fixed_t value;
};

};
};

Expand Down Expand Up @@ -172,6 +185,11 @@ static const struct { const char *name; uint32_t button; } button_names[] = {
};


static const struct { const char *name; uint32_t axis; } axis_names[] = {
{"axis_v", WTYPE_AXIS_REL_WHEEL},
{"axis_h", WTYPE_AXIS_REL_HWHEEL}
};

enum wtype_mod name_to_mod(const char *name)
{
for (unsigned int i = 0; i < ARRAY_SIZE(mod_names); i++) {
Expand All @@ -196,6 +214,17 @@ uint32_t button_code_from_name(const char *name)
return WTYPE_BTN_NONE;
}

uint32_t axis_code_from_name(const char *name)
{
for (unsigned int i = 0; i < ARRAY_SIZE(axis_names); i++) {
if (!strcasecmp(axis_names[i].name, name)) {
return axis_names[i].axis;
}
}
return WTYPE_AXIS_NONE;
}



static unsigned int append_keymap_entry(struct wtype *wtype, wchar_t ch, xkb_keysym_t xkb)
{
Expand Down Expand Up @@ -332,7 +361,19 @@ static void parse_args(struct wtype *wtype, int argc, const char *argv[])
if (cmd->button == WTYPE_BTN_NONE) {
fail("Unknown button '%s'", argv[i + 1]);
}
} else {
} else if (!strcmp("-A", argv[i])) {
cmd->type = WTYPE_COMMAND_AXIS;
cmd->axis = axis_code_from_name(argv[i + 1]);
if (cmd->axis == WTYPE_AXIS_NONE) {
fail("Unknown axis '%s'", argv[i + 1]);
}
if (i + 2 >= argc) {
fail("Usage -A <axis> <value>");
}
i++;
cmd->value = wl_fixed_from_double(atof(argv[i + 1]));
}
else {
fail("Unknown parameter %s", argv[i]);
}
prefix_with_space = false;
Expand Down Expand Up @@ -480,6 +521,14 @@ static void run_text_stdin(struct wtype *wtype, struct wtype_command *cmd)
free(cmd->key_codes);
}

static void run_axis(struct wtype *wtype, struct wtype_command *cmd) {
zwlr_virtual_pointer_v1_axis_discrete(
wtype->pointer, 0, cmd->axis == WTYPE_AXIS_REL_WHEEL ? WL_POINTER_AXIS_VERTICAL_SCROLL : WL_POINTER_AXIS_HORIZONTAL_SCROLL,
cmd->value, cmd->value >= 0 ? 1 : -1);
zwlr_virtual_pointer_v1_frame(wtype->pointer);
wl_display_roundtrip(wtype->display);
}

static void run_button(struct wtype *wtype, struct wtype_command *cmd) {
zwlr_virtual_pointer_v1_button(
wtype->pointer, 0, cmd->single_key_code,
Expand All @@ -488,6 +537,7 @@ static void run_button(struct wtype *wtype, struct wtype_command *cmd) {
zwlr_virtual_pointer_v1_frame(wtype->pointer);
wl_display_roundtrip(wtype->display);
}

static void run_click(struct wtype *wtype, struct wtype_command *cmd) {
zwlr_virtual_pointer_v1_button(
wtype->pointer, 0, cmd->single_key_code,
Expand All @@ -501,8 +551,8 @@ static void run_click(struct wtype *wtype, struct wtype_command *cmd) {
wl_display_roundtrip(wtype->display);
usleep(2000);
}
static void run_commands(struct wtype *wtype)

static void run_commands(struct wtype *wtype)
{
void (*handlers[])(struct wtype *, struct wtype_command *) = {
[WTYPE_COMMAND_SLEEP] = run_sleep,
Expand All @@ -515,6 +565,7 @@ static void run_commands(struct wtype *wtype)
[WTYPE_COMMAND_BUTTON_PRESS] = run_button,
[WTYPE_COMMAND_BUTTON_RELEASE] = run_button,
[WTYPE_COMMAND_BUTTON_CLICK] = run_click,
[WTYPE_COMMAND_AXIS] = run_axis
};
for (unsigned int i = 0; i < wtype->command_count; i++) {
handlers[wtype->commands[i].type](wtype, &wtype->commands[i]);
Expand Down