From d221d0e948ac81183dd4671940b7cd5793d0f713 Mon Sep 17 00:00:00 2001 From: FelixKratz Date: Thu, 4 Nov 2021 21:51:43 +0100 Subject: [PATCH] new additional scripting properties for mouse events -> $BUTTON and $MODIFIER are accessible now. Closes #92 and #25 --- README.md | 8 ++++++++ src/bar_item.c | 8 +++++++- src/bar_item.h | 2 +- src/event.c | 3 ++- src/misc/helpers.h | 31 +++++++++++++++++++++++++++++-- 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 940a9048..6a071e38 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,14 @@ $SENDER ``` Where *$NAME* is the name of the item that has invoked the script and *$SENDER* is the reason why the script is executed. +If an item is *clicked* the script has access to the additional variables: +```bash +$BUTTON +$MODIFIER +``` +where the $BUTTON can be *left*, *right* or *other* and specifies the mouse button that was used to click the item, while the $MODIFIER is either *shift*, *ctrl*, *alt* or *cmd* and +specifies the modifier key held down while clicking the item. + ### Creating custom events This allows to define events which are triggered by a different application (see Trigger custom events). Items can also subscribe to these events for their script execution. ```bash diff --git a/src/bar_item.c b/src/bar_item.c index 2f36b0cc..510cfac3 100644 --- a/src/bar_item.c +++ b/src/bar_item.c @@ -90,6 +90,8 @@ void bar_item_init(struct bar_item* bar_item, struct bar_item* default_item) { strncpy(&bar_item->signal_args.name[0][0], "NAME", 255); strncpy(&bar_item->signal_args.name[1][0], "SELECTED", 255); strncpy(&bar_item->signal_args.name[4][0], "SENDER", 255); + strncpy(&bar_item->signal_args.name[5][0], "BUTTON", 255); + strncpy(&bar_item->signal_args.name[6][0], "MODIFIER", 255); strncpy(&bar_item->signal_args.value[1][0], "false", 255); } @@ -235,8 +237,12 @@ void bar_item_set_drawing(struct bar_item* bar_item, bool state) { bar_item_needs_update(bar_item); } -void bar_item_on_click(struct bar_item* bar_item, uint32_t modifier) { +void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t modifier) { if (!bar_item) return; + + strncpy(&bar_item->signal_args.value[5][0], get_type_description(type), 255); + strncpy(&bar_item->signal_args.value[6][0], get_modifier_description(modifier), 255); + if (strlen(bar_item->click_script) > 0) fork_exec(bar_item->click_script, &bar_item->signal_args); if (bar_item->update_mask & UPDATE_MOUSE_CLICKED) diff --git a/src/bar_item.h b/src/bar_item.h index 6ce74d9a..01ae135d 100644 --- a/src/bar_item.h +++ b/src/bar_item.h @@ -95,7 +95,7 @@ uint32_t bar_item_get_height(struct bar_item* bar_item); void bar_item_needs_update(struct bar_item* bar_item); void bar_item_clear_needs_update(struct bar_item* bar_item); -void bar_item_on_click(struct bar_item* bar_item, uint32_t modifier); +void bar_item_on_click(struct bar_item* bar_item, uint32_t type, uint32_t modifier); void bar_item_mouse_entered(struct bar_item* bar_item); void bar_item_mouse_exited(struct bar_item* bar_item); diff --git a/src/event.c b/src/event.c index 9febd635..2966118b 100644 --- a/src/event.c +++ b/src/event.c @@ -105,12 +105,13 @@ static EVENT_CALLBACK(EVENT_HANDLER_DAEMON_MESSAGE) { static EVENT_CALLBACK(EVENT_HANDLER_MOUSE_UP) { debug("%s\n", __FUNCTION__); CGPoint point = CGEventGetLocation(context); + CGEventType type = CGEventGetType(context); uint32_t modifier_keys = CGEventGetFlags(context); uint32_t adid = display_arrangement(display_active_display_id()); debug("EVENT_HANDLER_MOUSE_UP: D#%d (x: %.0f, y: %.0f) -> ", adid, point.x, point.y); struct bar_item* bar_item = bar_manager_get_item_by_point(&g_bar_manager, point, adid); debug("item: %s\n", bar_item ? bar_item->name : "NULL"); - bar_item_on_click(bar_item, modifier_keys); + bar_item_on_click(bar_item, type, modifier_keys); CFRelease(context); return EVENT_SUCCESS; } diff --git a/src/misc/helpers.h b/src/misc/helpers.h index e184b618..8ce2b759 100644 --- a/src/misc/helpers.h +++ b/src/misc/helpers.h @@ -12,8 +12,8 @@ extern uint32_t SLSGetActiveSpace(int cid); extern int g_connection; struct signal_args { - char name[5][255]; - char value[5][255]; + char name[7][255]; + char value[7][255]; void *entity; void *param1; }; @@ -55,6 +55,31 @@ static inline bool string_equals(const char *a, const char *b) { return a && b && strcmp(a, b) == 0; } +static inline char* get_type_description(uint32_t type) { + switch (type) { + case kCGEventLeftMouseUp: + return "left"; + case kCGEventRightMouseUp: + return "right"; + default: + return "other"; + } +} + +static inline char* get_modifier_description(uint32_t modifier) { + if (modifier & kCGEventFlagMaskShift) + return "shift"; + else if (modifier & kCGEventFlagMaskControl) + return "ctrl"; + else if (modifier & kCGEventFlagMaskAlternate) + return "alt"; + else if (modifier & kCGEventFlagMaskCommand) + return "cmd"; + else + return "none"; +} + + struct token { char *text; unsigned int length; @@ -283,6 +308,8 @@ static bool sync_exec(char *command, struct signal_args *args) { if (*args->name[2]) setenv(args->name[2], args->value[2], 1); if (*args->name[3]) setenv(args->name[3], args->value[3], 1); if (*args->name[4]) setenv(args->name[4], args->value[4], 1); + if (*args->name[5]) setenv(args->name[5], args->value[5], 1); + if (*args->name[6]) setenv(args->name[6], args->value[6], 1); } char *exec[] = { "/usr/bin/env", "sh", "-c", command, NULL};