From 51211fa6265673668d28a9285daba96cd2748a33 Mon Sep 17 00:00:00 2001 From: o9000 Date: Sat, 4 Mar 2017 15:50:09 +0100 Subject: [PATCH] Systray: new config option systray_name_filter --- doc/tint2.md | 2 ++ src/config.c | 6 +++-- src/systray/systraybar.c | 49 ++++++++++++++++++++++++----------- src/systray/systraybar.h | 2 +- src/tint2conf/properties.c | 15 ++++++++++- src/tint2conf/properties.h | 2 +- src/tint2conf/properties_rw.c | 4 +++ 7 files changed, 60 insertions(+), 20 deletions(-) diff --git a/doc/tint2.md b/doc/tint2.md index 1da8212..86800ff 100644 --- a/doc/tint2.md +++ b/doc/tint2.md @@ -463,6 +463,8 @@ The action semantics: * `systray_monitor = integer (1, 2, ...)` : On which monitor to draw the systray. The first monitor is `1`. *(since 0.12)* + * `systray_name_filter = string` : Regular expression to identify icon names to be hidden. For example, `^audacious$` will hide icons with the exact name `audacious`, while `aud` will hide any icons having `aud` in the name. *(since 0.14)* + ### Clock * `time1_format = %H:%M` : The format used by the first line of the clock. diff --git a/src/config.c b/src/config.c index 13e3a49..f752a22 100644 --- a/src/config.c +++ b/src/config.c @@ -1009,8 +1009,10 @@ void add_entry(char *key, char *value) systray.brightness = atoi(value3); } else if (strcmp(key, "systray_monitor") == 0) { systray_monitor = atoi(value) - 1; - } else if (strcmp(key, "systray_hide_by_icon_name") == 0) { - strcpy(systray_hide_icons, value); + } else if (strcmp(key, "systray_name_filter") == 0) { + if (systray_hide_name_filter) + free(systray_hide_name_filter); + systray_hide_name_filter = strdup(value); } /* Launcher */ diff --git a/src/systray/systraybar.c b/src/systray/systraybar.c index 3ff26a1..34af6ee 100644 --- a/src/systray/systraybar.c +++ b/src/systray/systraybar.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -54,7 +55,8 @@ int systray_monitor; int chrono; int systray_composited; int systray_profile; -char systray_hide_icons[100]; +char *systray_hide_name_filter; +regex_t *systray_hide_name_regex; // background pixmap if we render ourselves the icons static Pixmap render_background; @@ -82,6 +84,8 @@ void default_systray() systray.area.size_mode = LAYOUT_FIXED; systray.area._resize = resize_systray; systray_profile = getenv("SYSTRAY_PROFILING") != NULL; + systray_hide_name_filter = NULL; + systray_hide_name_regex = NULL; } void cleanup_systray() @@ -96,6 +100,12 @@ void cleanup_systray() XFreePixmap(server.display, render_background); render_background = 0; } + if (systray_hide_name_regex) { + regfree(systray_hide_name_regex); + free_and_null(systray_hide_name_regex); + } + if (systray_hide_name_filter) + free_and_null(systray_hide_name_filter); } void init_systray() @@ -574,6 +584,26 @@ void print_icons() } } +gboolean reject_icon(Window win) +{ + if (systray_hide_name_filter) { + if (!systray_hide_name_regex) { + systray_hide_name_regex = (regex_t *) calloc(1, sizeof(*systray_hide_name_regex)); + if (regcomp(systray_hide_name_regex, systray_hide_name_filter, 0) != 0) { + fprintf(stderr, RED "Could not compile regex %s" RESET "\n", systray_hide_name_filter); + free_and_null(systray_hide_name_regex); + return FALSE; + } + } + char *name = get_window_name(win); + if (regexec(systray_hide_name_regex, name, 0, NULL, 0) == 0) { + fprintf(stderr, GREEN "Filtering out systray icon '%s'" RESET "\n", name); + return TRUE; + } + } + return FALSE; +} + gboolean add_icon(Window win) { // Avoid duplicates @@ -584,21 +614,9 @@ gboolean add_icon(Window win) } } - char *name = get_window_name(win); - // Filter out systray_hide_by_icon_name - char *token; - char *string; - string = strdup(systray_hide_icons); - if (string != NULL && string[0] != '0') { - while ((token = strsep(&string, ",")) != NULL) { - if (strcmp(token,name) == 0) { - if (strcmp(token,"") == 0) token = "empty name"; - fprintf(stderr, GREEN "filtering out '%s'\n", token); - return FALSE; - } - } - } + if (reject_icon(win)) + return FALSE; // Dangerous actions begin XSync(server.display, False); @@ -607,6 +625,7 @@ gboolean add_icon(Window win) XSelectInput(server.display, win, StructureNotifyMask | PropertyChangeMask | ResizeRedirectMask); + char *name = get_window_name(win); if (systray_profile) fprintf(stderr, "[%f] %s:%d win = %lu (%s)\n", profiling_get_time(), __FUNCTION__, __LINE__, win, name); Panel *panel = systray.area.panel; diff --git a/src/systray/systraybar.h b/src/systray/systraybar.h index 413040f..1c3f197 100644 --- a/src/systray/systraybar.h +++ b/src/systray/systraybar.h @@ -75,7 +75,7 @@ extern gboolean systray_enabled; extern int systray_max_icon_size; extern int systray_monitor; extern gboolean systray_profile; -extern char systray_hide_icons[100]; +extern char *systray_hide_name_filter; // default global data void default_systray(); diff --git a/src/tint2conf/properties.c b/src/tint2conf/properties.c index f71c5ec..d92fdbe 100644 --- a/src/tint2conf/properties.c +++ b/src/tint2conf/properties.c @@ -101,7 +101,7 @@ GtkWidget *ac_connected_cmd, *ac_disconnected_cmd; // systray GtkWidget *systray_icon_order, *systray_padding_x, *systray_padding_y, *systray_spacing; GtkWidget *systray_icon_size, *systray_icon_opacity, *systray_icon_saturation, *systray_icon_brightness; -GtkWidget *systray_background, *systray_monitor; +GtkWidget *systray_background, *systray_monitor, *systray_name_filter; // tooltip GtkWidget *tooltip_padding_x, *tooltip_padding_y, *tooltip_font, *tooltip_font_set, *tooltip_font_color; @@ -4484,6 +4484,19 @@ void create_systemtray(GtkWidget *parent) gtk_table_attach(GTK_TABLE(table), systray_icon_brightness, col, col+1, row, row+1, GTK_FILL, 0, 0, 0); col++; gtk_tooltips_set_tip(tooltips, systray_icon_brightness, _("Specifies the brightness adjustment of the system tray icons, in percent."), NULL); + + row++, col = 2; + label = gtk_label_new(_("Name filter")); + gtk_misc_set_alignment(GTK_MISC(label), 0, 0); + gtk_widget_show(label); + gtk_table_attach(GTK_TABLE(table), label, col, col+1, row, row+1, GTK_FILL, 0, 0, 0); + col++; + + systray_name_filter = gtk_entry_new(); + gtk_widget_show(systray_name_filter); + gtk_entry_set_width_chars(GTK_ENTRY(systray_name_filter), 50); + gtk_table_attach(GTK_TABLE(table), systray_name_filter, col, col+1, row, row+1, GTK_FILL, 0, 0, 0); + col++; } void create_battery(GtkWidget *parent) diff --git a/src/tint2conf/properties.h b/src/tint2conf/properties.h index 2274b30..d85c90e 100644 --- a/src/tint2conf/properties.h +++ b/src/tint2conf/properties.h @@ -111,7 +111,7 @@ extern GtkWidget *ac_connected_cmd, *ac_disconnected_cmd; // systray extern GtkWidget *systray_icon_order, *systray_padding_x, *systray_padding_y, *systray_spacing; extern GtkWidget *systray_icon_size, *systray_icon_opacity, *systray_icon_saturation, *systray_icon_brightness; -extern GtkWidget *systray_background, *systray_monitor; +extern GtkWidget *systray_background, *systray_monitor, *systray_name_filter; // tooltip extern GtkWidget *tooltip_padding_x, *tooltip_padding_y, *tooltip_font, *tooltip_font_set, *tooltip_font_color; diff --git a/src/tint2conf/properties_rw.c b/src/tint2conf/properties_rw.c index 470b6a0..1b1902a 100644 --- a/src/tint2conf/properties_rw.c +++ b/src/tint2conf/properties_rw.c @@ -622,6 +622,8 @@ void config_write_systray(FILE *fp) fprintf(fp, "%d", MAX(1, 1 + gtk_combo_box_get_active(GTK_COMBO_BOX(systray_monitor)))); fprintf(fp, "\n"); + fprintf(fp, "systray_name_filter = %s\n", gtk_entry_get_text(GTK_ENTRY(systray_name_filter))); + fprintf(fp, "\n"); } @@ -1705,6 +1707,8 @@ void add_entry(char *key, char *value) gtk_spin_button_set_value(GTK_SPIN_BUTTON(systray_icon_opacity), atoi(value1)); gtk_spin_button_set_value(GTK_SPIN_BUTTON(systray_icon_saturation), atoi(value2)); gtk_spin_button_set_value(GTK_SPIN_BUTTON(systray_icon_brightness), atoi(value3)); + } else if (strcmp(key, "systray_name_filter") == 0) { + gtk_entry_set_text(GTK_ENTRY(systray_name_filter), value); } /* Launcher */