From ef5299ee92af603ac27a56eb79017b2c04f8765d Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Mon, 3 Oct 2005 18:13:45 +0000 Subject: [PATCH] Truncate ridiculously long titles to avoid crashing or letting the pager 2005-10-03 Elijah Newren Truncate ridiculously long titles to avoid crashing or letting the pager crash. Based on patch from Ray, incorporating suggestions from Havoc and some extensions of my own. Fixes #315070. * src/display.c (set_utf8_string_hint, meta_display_open): * src/xprops.[ch] (meta_prop_set_utf8_string_hint): Move set_utf8_string_hint() to props.[ch], namespace it ("meta_prop_"), and make it public * src/tabpopup.c (utf8_strndup, meta_ui_tab_popup_new): * src/util.[ch] (meta_g_utf8_strndup): Move utf8_strndup() to util.[ch], namespace it ("meta_g_"), and make it public * src/display.c (meta_display_open): * src/display.h (struct _MetaDisplay): add net_wm_visible_name and net_wm_visible_icon_name atoms to the list of atoms we work with * src/window-props.c (set_window_title, set_icon_title): If title length is greater than 512, truncate it and set _NET_WM_VISIBLE_NAME or _NET_WM_VISIBLE_ICON_NAME accordingly --- ChangeLog | 25 +++++++++++++++++++++++++ src/display.c | 36 +++++++++++++----------------------- src/display.h | 2 ++ src/tabpopup.c | 16 +--------------- src/util.c | 14 ++++++++++++++ src/util.h | 1 + src/window-props.c | 23 +++++++++++++++++++++-- src/xprops.c | 14 ++++++++++++++ src/xprops.h | 5 +++++ 9 files changed, 96 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1bd13cf29..d81c9c9fe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,28 @@ +2005-10-03 Elijah Newren + + Truncate ridiculously long titles to avoid crashing or letting the + pager crash. Based on patch from Ray, incorporating suggestions + from Havoc and some extensions of my own. Fixes #315070. + + * src/display.c (set_utf8_string_hint, meta_display_open): + * src/xprops.[ch] (meta_prop_set_utf8_string_hint): + Move set_utf8_string_hint() to props.[ch], namespace it + ("meta_prop_"), and make it public + + * src/tabpopup.c (utf8_strndup, meta_ui_tab_popup_new): + * src/util.[ch] (meta_g_utf8_strndup): + Move utf8_strndup() to util.[ch], namespace it ("meta_g_"), and + make it public + + * src/display.c (meta_display_open): + * src/display.h (struct _MetaDisplay): + add net_wm_visible_name and net_wm_visible_icon_name atoms to the + list of atoms we work with + + * src/window-props.c (set_window_title, set_icon_title): If title + length is greater than 512, truncate it and set + _NET_WM_VISIBLE_NAME or _NET_WM_VISIBLE_ICON_NAME accordingly + 2005-10-03 Elijah Newren Get the tabbing window outline to work with gtk+ 2.8.4 again. diff --git a/src/display.c b/src/display.c index 99f0d72e7..38e224741 100644 --- a/src/display.c +++ b/src/display.c @@ -121,20 +121,6 @@ static void prefs_changed_callback (MetaPreference pref, static void sanity_check_timestamps (MetaDisplay *display, Time known_good_timestamp); -static void -set_utf8_string_hint (MetaDisplay *display, - Window xwindow, - Atom atom, - const char *val) -{ - meta_error_trap_push (display); - XChangeProperty (display->xdisplay, - xwindow, atom, - display->atom_utf8_string, - 8, PropModeReplace, (guchar*) val, strlen (val)); - meta_error_trap_pop (display, FALSE); -} - static void ping_data_free (MetaPingData *ping_data) { @@ -302,7 +288,9 @@ meta_display_open (const char *name) "_NET_MOVERESIZE_WINDOW", "_NET_DESKTOP_GEOMETRY", "_NET_DESKTOP_VIEWPORT", - "_METACITY_VERSION" + "_METACITY_VERSION", + "_NET_WM_VISIBLE_NAME", + "_NET_WM_VISIBLE_ICON_NAME" }; Atom atoms[G_N_ELEMENTS(atom_names)]; @@ -460,6 +448,8 @@ meta_display_open (const char *name) display->atom_net_desktop_geometry = atoms[89]; display->atom_net_desktop_viewport = atoms[90]; display->atom_metacity_version = atoms[91]; + display->atom_net_wm_visible_name = atoms[92]; + display->atom_net_wm_visible_icon_name = atoms[93]; display->prop_hooks = NULL; meta_display_init_window_prop_hooks (display); @@ -637,15 +627,15 @@ meta_display_open (const char *name) display->leader_window = meta_create_offscreen_window (display->xdisplay, DefaultRootWindow (display->xdisplay)); - set_utf8_string_hint (display, - display->leader_window, - display->atom_net_wm_name, - "Metacity"); + meta_prop_set_utf8_string_hint (display, + display->leader_window, + display->atom_net_wm_name, + "Metacity"); - set_utf8_string_hint (display, - display->leader_window, - display->atom_metacity_version, - VERSION); + meta_prop_set_utf8_string_hint (display, + display->leader_window, + display->atom_metacity_version, + VERSION); data[0] = display->leader_window; XChangeProperty (display->xdisplay, diff --git a/src/display.h b/src/display.h index 9c8c2f5e6..fef38d699 100644 --- a/src/display.h +++ b/src/display.h @@ -183,6 +183,8 @@ struct _MetaDisplay Atom atom_net_desktop_geometry; Atom atom_net_desktop_viewport; Atom atom_metacity_version; + Atom atom_net_wm_visible_name; + Atom atom_net_wm_visible_icon_name; /* This is the actual window from focus events, * not the one we last set diff --git a/src/tabpopup.c b/src/tabpopup.c index 6d2660dfd..d3936b1f4 100644 --- a/src/tabpopup.c +++ b/src/tabpopup.c @@ -98,20 +98,6 @@ outline_window_expose (GtkWidget *widget, return FALSE; } -static char* -utf8_strndup (const char *src, - int n) -{ - const gchar *s = src; - while (n && *s) - { - s = g_utf8_next_char (s); - n--; - } - - return g_strndup (src, s - src); -} - static GdkPixbuf* dimm_icon (GdkPixbuf *pixbuf) { @@ -238,7 +224,7 @@ meta_ui_tab_popup_new (const MetaTabEntry *entries, tmp = markup; } - te->title = utf8_strndup (tmp, max_chars_per_title); + te->title = meta_g_utf8_strndup (tmp, max_chars_per_title); g_free (tmp); } te->widget = NULL; diff --git a/src/util.c b/src/util.c index c4d59f267..2d76065a2 100644 --- a/src/util.c +++ b/src/util.c @@ -161,6 +161,20 @@ meta_set_replace_current_wm (gboolean setting) replace_current = setting; } +char * +meta_g_utf8_strndup (const gchar *src, + gsize n) +{ + const gchar *s = src; + while (n && *s) + { + s = g_utf8_next_char (s); + n--; + } + + return g_strndup (src, s - src); +} + static int utf8_fputs (const char *str, FILE *f) diff --git a/src/util.h b/src/util.h index 9488ce566..6253bbaeb 100644 --- a/src/util.h +++ b/src/util.h @@ -88,6 +88,7 @@ void meta_print_backtrace (void); #define _(x) dgettext (GETTEXT_PACKAGE, x) #define N_(x) x +char* meta_g_utf8_strndup (const gchar *src, gsize n); /* To disable verbose mode, we make these functions into no-ops */ #ifdef WITH_VERBOSE_MODE diff --git a/src/window-props.c b/src/window-props.c index 34881ff19..31efd276a 100644 --- a/src/window-props.c +++ b/src/window-props.c @@ -190,6 +190,8 @@ reload_net_wm_user_time (MetaWindow *window, } } +#define MAX_TITLE_LENGTH 512 + static void set_window_title (MetaWindow *window, const char *title) @@ -200,8 +202,16 @@ set_window_title (MetaWindow *window, if (title == NULL) window->title = g_strdup (""); - else + else if (g_utf8_strlen (title, MAX_TITLE_LENGTH + 1) <= MAX_TITLE_LENGTH) window->title = g_strdup (title); + else + { + window->title = meta_g_utf8_strndup (title, MAX_TITLE_LENGTH); + meta_prop_set_utf8_string_hint (window->display, + window->xwindow, + window->display->atom_net_wm_visible_name, + window->title); + } /* strndup is a hack since GNU libc has broken %.10s */ str = g_strndup (window->title, 10); @@ -285,8 +295,17 @@ set_icon_title (MetaWindow *window, if (title == NULL) window->icon_name = g_strdup (""); - else + else if (g_utf8_strlen (title, MAX_TITLE_LENGTH + 1) <= MAX_TITLE_LENGTH) window->icon_name = g_strdup (title); + else + { + window->icon_name = meta_g_utf8_strndup (title, MAX_TITLE_LENGTH); + meta_prop_set_utf8_string_hint (window->display, + window->xwindow, + window->display->atom_net_wm_visible_icon_name, + window->icon_name); + } + } static void diff --git a/src/xprops.c b/src/xprops.c index e5465b9b4..7f1ba70c7 100644 --- a/src/xprops.c +++ b/src/xprops.c @@ -524,6 +524,20 @@ meta_prop_get_utf8_list (MetaDisplay *display, return utf8_list_from_results (&results, str_p, n_str_p); } +void +meta_prop_set_utf8_string_hint (MetaDisplay *display, + Window xwindow, + Atom atom, + const char *val) +{ + meta_error_trap_push (display); + XChangeProperty (display->xdisplay, + xwindow, atom, + display->atom_utf8_string, + 8, PropModeReplace, (guchar*) val, strlen (val)); + meta_error_trap_pop (display, FALSE); +} + static gboolean window_from_results (GetPropertyResults *results, Window *window_p) diff --git a/src/xprops.h b/src/xprops.h index cb93737b5..646f087f7 100644 --- a/src/xprops.h +++ b/src/xprops.h @@ -95,6 +95,11 @@ gboolean meta_prop_get_utf8_list (MetaDisplay *display, Atom xatom, char ***str_p, int *n_str_p); +void meta_prop_set_utf8_string_hint + (MetaDisplay *display, + Window xwindow, + Atom atom, + const char *val); gboolean meta_prop_get_window (MetaDisplay *display, Window xwindow, Atom xatom,