From 9e0b07908dda249de6ce115ed32a36a7d5d13ffe Mon Sep 17 00:00:00 2001 From: B Stack Date: Thu, 6 Feb 2020 16:16:50 -0500 Subject: add middleclick, and documentation --- README.md | 6 +++++ mktrayicon.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 84697fd..fea63a6 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,12 @@ newline. The following commands are supported: - `m`: Remove the menu handler - `h`: Hide the tray icon - `s`: Show the tray icon + - `R `: Set the command to execute when the user scrolls the + mouse wheel up + - `r `: Set the command to execute when the user scrolls the + mouse wheel down + - `S `: Set the command to execute when the user middle-clicks + the tray icon By default, the `none` tooltip icon is used. To change this, pass `-i ` or `-i ` when running diff --git a/mktrayicon.c b/mktrayicon.c index 8584783..21f1432 100644 --- a/mktrayicon.c +++ b/mktrayicon.c @@ -54,6 +54,7 @@ struct item { } * onmenu; char *onscrollup = NULL; char *onscrolldown = NULL; +char *onmiddleclick = NULL; int menusize = 0; // number of menu entries GtkWidget *menu = NULL; @@ -66,6 +67,24 @@ void tray_icon_on_click(GtkStatusIcon *status_icon, gpointer user_data) { } } +void tray_icon_on_middleclick(GtkStatusIcon *status_icon, GdkEventButton *event, + gpointer user_data) { + if (2 == event->button) { + if (onmiddleclick == NULL) { +#ifdef DEBUG + printf("middleclick, but no command specified\n"); +#endif + } else { +#ifdef DEBUG + printf("middleclick\n"); +#endif + if (onmiddleclick != NULL && fork() == 0) { + execl("/bin/sh", "sh", "-c", onmiddleclick, (char *)NULL); + } + } + } +} + /* * Callback function for when an entry is selected from the menu * We loop over all entry names to find what action to execute @@ -104,12 +123,10 @@ void tray_icon_on_scroll(GtkStatusIcon *status_icon, GdkEventScroll *event, execl("/bin/sh", "sh", "-c", onscrolldown, (char *)NULL); break; } - if (i != NULL) { - #ifdef DEBUG +#ifdef DEBUG printf("scroll %s\n",i); - #endif - +#endif } } @@ -433,15 +450,52 @@ outer: if (onscrollup != NULL) { free(onscrollup); } - onscrollup = malloc(strlen(param)); - strncpy(onscrollup, param, len + 1); + if (!param || (*param == '\0')) { +#ifdef DEBUG + printf("Removing scrollup command\n"); + onscrollup = NULL; +#endif + } else { +#ifdef DEBUG + printf("Setting scrollup command\n"); +#endif + onscrollup = malloc(strlen(param)); + strncpy(onscrollup, param, len + 1); + } break; case 'r': /* mouse scroll down */ if (onscrolldown != NULL) { free(onscrolldown); } - onscrolldown = malloc(strlen(param)); - strncpy(onscrolldown, param, len + 1); + if (!param || (*param == '\0')) { +#ifdef DEBUG + printf("Removing scrolldown command\n"); + onscrolldown = NULL; +#endif + } else { +#ifdef DEBUG + printf("Setting scrolldown command\n"); +#endif + onscrolldown = malloc(strlen(param)); + strncpy(onscrolldown, param, len + 1); + } + break; + case 'S': /* mouse middle click */ + if (onmiddleclick != NULL) { + free(onmiddleclick); + } + if (!param || (*param == '\0')) { +#ifdef DEBUG + printf("Removing middle click command\n"); + onmiddleclick = NULL; +#endif + } else { +#ifdef DEBUG + printf("Setting middleclick command\n"); +#endif + onmiddleclick = malloc(strlen(param)); + strncpy(onmiddleclick, param, len + 1); + } break; default: fprintf(stderr, "Unknown command: '%c'\n", *buf); @@ -470,6 +524,8 @@ static GtkStatusIcon *create_tray_icon(char *start_icon) { G_CALLBACK(tray_icon_on_menu), NULL); g_signal_connect(G_OBJECT(tray_icon), "scroll-event", G_CALLBACK(tray_icon_on_scroll), NULL); + g_signal_connect(G_OBJECT(tray_icon), "button-release-event", + G_CALLBACK(tray_icon_on_middleclick), NULL); gtk_status_icon_set_visible(tray_icon, TRUE); return tray_icon; -- cgit