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

add --ignore-buttons option #41

Merged
merged 1 commit into from
Apr 7, 2019
Merged
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
6 changes: 6 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ typedef struct match_t {
int len;
} match_t;

typedef struct ignore_buttons_t {
unsigned char count;
unsigned int *buttons;
} ignore_buttons_t;

typedef struct Config {
long timeout;
long jitter;
bool exclude_root;
bool ignore_scrolling;
ignore_buttons_t ignore_buttons;
bool fork;
bool debug;
bool onescreen;
Expand Down
2 changes: 2 additions & 0 deletions include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@
void bail(char *message);

long int parse_int(char *str);

void parse_buttons_numbers(char *str, ignore_buttons_t *ignore_buttons);
8 changes: 6 additions & 2 deletions man/unclutter-xfixes.man
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ unclutter-xfixes - rewrite of unclutter using the X11-Xfixes extension

== SYNOPSIS

unclutter [*--timeout* _seconds_] [*--jitter* _radius_] [*--exclude-root*] [*--ignore-scrolling*] [*--fork*|*-b*] [*--help*|*-h*] [*--version*|*-v*]
unclutter [*--timeout* _seconds_] [*--jitter* _radius_] [*--exclude-root*] [*--ignore-scrolling*] [*--ignore-buttons* _buttons_] [*--fork*|*-b*] [*--help*|*-h*] [*--version*|*-v*]

Compatibility arguments:

Expand All @@ -30,7 +30,11 @@ rather just the desktop background.

*--ignore-scrolling*::
Ignore mouse scroll events (buttons 4 and 5) so that scrolling doesn't unhide
the cursor.
the cursor. This is a shortcut for *--ignore-buttons* '4,5'.

*--ignore-buttons*::
Defines the mouse buttons which do not unhide the cursor when clicked. You can
pass multiple button numbers by separating them with ','.

*--fork*|*-b*::
Fork unclutter to the background.
Expand Down
18 changes: 16 additions & 2 deletions src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ static void x_cb(EV_P_ ev_io *w, int revents) {
/* Deliberately empty. */
}

static bool is_button_ignored(const XIRawEvent *data) {
if (config.ignore_scrolling && (data->detail == 4 || data->detail == 5)) {
return true;
}

for (int i = 0; i < config.ignore_buttons.count; ++i) {
if (data->detail == config.ignore_buttons.buttons[i]) {
return true;
}
}

return false;
}

static void x_check_cb(EV_P_ ev_check *w, int revents) {
XEvent ev;
while (XPending(display) > 0) {
Expand All @@ -57,9 +71,9 @@ static void x_check_cb(EV_P_ ev_check *w, int revents) {
continue;
}

if (config.ignore_scrolling && cookie->evtype == XI_RawButtonPress) {
if (cookie->evtype == XI_RawButtonPress) {
const XIRawEvent *data = (const XIRawEvent *) cookie->data;
if (data->detail == 4 || data->detail == 5) {
if (is_button_ignored(data)) {
XFreeEventData(display, cookie);
continue;
}
Expand Down
8 changes: 7 additions & 1 deletion src/unclutter.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Config config = {
.jitter = 0,
.exclude_root = false,
.ignore_scrolling = false,
.ignore_buttons.count = 0,
.ignore_buttons.buttons = NULL,
.fork = false,
.debug = false,
.onescreen = false,
Expand Down Expand Up @@ -93,6 +95,7 @@ static void parse_args(int argc, char *argv[]) {
{ "jitter", required_argument, 0, 0 },
{ "exclude-root", no_argument, 0, 0 },
{ "ignore-scrolling", no_argument, 0, 0 },
{ "ignore-buttons", required_argument, 0, 0 },
{ "fork", no_argument, 0, 'b' },
{ "version", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' },
Expand Down Expand Up @@ -142,6 +145,9 @@ static void parse_args(int argc, char *argv[]) {
} else if (OPT_NAME_IS("ignore-scrolling")) {
config.ignore_scrolling = true;
break;
} else if (OPT_NAME_IS("ignore-buttons")) {
parse_buttons_numbers(optarg, &config.ignore_buttons);
break;
} else if (OPT_NAME_IS("debug")) {
config.debug = true;
break;
Expand Down Expand Up @@ -185,7 +191,7 @@ static void parse_args(int argc, char *argv[]) {
}

static void print_usage(char *argv[]) {
fprintf(stderr, "Usage: %s [--timeout <n>] [--jitter <radius>] [--exclude-root] [--ignore-scrolling] [-b|--fork] [-v|--version] [-h|--help]", argv[0]);
fprintf(stderr, "Usage: %s [--timeout <n>] [--jitter <radius>] [--exclude-root] [--ignore-scrolling] [--ignore-buttons <buttons>] [-b|--fork] [-v|--version] [-h|--help]", argv[0]);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
Expand Down
29 changes: 29 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,32 @@ long parse_int(char *str) {

return parsed;
}

void parse_buttons_numbers(char *str, ignore_buttons_t *ignore_buttons) {
char *button = strtok(str, ",");
while (button != NULL) {
long number = atol(button);
button = strtok(NULL, ",");
if (number < 0 || number > UINT_MAX) {
continue;
}

ignore_buttons->count++;
if (ignore_buttons->count == UCHAR_MAX) {
bail("Too much buttons numbers");
}
unsigned int *buttons = (unsigned int *)realloc(ignore_buttons->buttons,
ignore_buttons->count * sizeof(unsigned int));
if (buttons == NULL) {
free(ignore_buttons->buttons);
bail("Cannot reallocate memory for ignore-buttons");
} else {
ignore_buttons->buttons = buttons;
}
ignore_buttons->buttons[ignore_buttons->count - 1] = number;
}

if (!ignore_buttons->count) {
bail("Amount of buttons to ignore = 0");
}
}