Skip to content

Commit

Permalink
new additional scripting properties for mouse events -> $BUTTON and $…
Browse files Browse the repository at this point in the history
…MODIFIER are accessible now. Closes #92 and #25
  • Loading branch information
FelixKratz committed Nov 4, 2021
1 parent 4b1dec6 commit d221d0e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/bar_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/bar_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 29 additions & 2 deletions src/misc/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down

0 comments on commit d221d0e

Please sign in to comment.