diff options
Diffstat (limited to 'sway/tree')
-rw-r--r-- | sway/tree/view.c | 93 |
1 files changed, 42 insertions, 51 deletions
diff --git a/sway/tree/view.c b/sway/tree/view.c index 4a01f096..386144f6 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -494,9 +494,17 @@ void view_child_destroy(struct sway_view_child *child) { } } -static char *parse_title_format(struct sway_view *view) { +/** + * Calculate and return the length of the formatted title. + * If buffer is not NULL, also populate the buffer with the formatted title. + */ +static size_t parse_title_format(struct sway_view *view, char *buffer) { if (!view->title_format || strcmp(view->title_format, "%title") == 0) { - return strdup(view_get_title(view)); + const char *title = view_get_title(view); + if (buffer) { + strcpy(buffer, title); + } + return strlen(title); } const char *title = view_get_title(view); const char *class = view_get_class(view); @@ -507,74 +515,56 @@ static char *parse_title_format(struct sway_view *view) { size_t instance_len = instance ? strlen(instance) : 0; size_t shell_len = shell ? strlen(shell) : 0; - // First, determine the length size_t len = 0; char *format = view->title_format; char *next = strchr(format, '%'); while (next) { - len += next - format; - format = next; - - if (strncmp(next, "%title", 6) == 0) { - len += title_len; - format += 6; - } else if (strncmp(next, "%class", 6) == 0) { - len += class_len; - format += 6; - } else if (strncmp(next, "%instance", 9) == 0) { - len += instance_len; - format += 9; - } else if (strncmp(next, "%shell", 6) == 0) { - len += shell_len; - format += 6; - } else { - ++format; - ++len; + if (buffer) { + // Copy everything up to the % + strncat(buffer, format, next - format); } - next = strchr(format, '%'); - } - len += strlen(format); - - char *buffer = calloc(len + 1, 1); - if (!sway_assert(buffer, "Unable to allocate title string")) { - return NULL; - } - - // Now build the title - format = view->title_format; - next = strchr(format, '%'); - while (next) { - // Copy everything up to the % - strncat(buffer, format, next - format); + len += next - format; format = next; if (strncmp(next, "%title", 6) == 0) { - if (title) { + if (buffer && title) { strcat(buffer, title); } + len += title_len; format += 6; } else if (strncmp(next, "%class", 6) == 0) { - if (class) { + if (buffer && class) { strcat(buffer, class); } + len += class_len; format += 6; } else if (strncmp(next, "%instance", 9) == 0) { - if (instance) { + if (buffer && instance) { strcat(buffer, instance); } + len += instance_len; format += 9; } else if (strncmp(next, "%shell", 6) == 0) { - strcat(buffer, shell); + if (buffer) { + strcat(buffer, shell); + } + len += shell_len; format += 6; } else { - strcat(buffer, "%"); + if (buffer) { + strcat(buffer, "%"); + } ++format; + ++len; } next = strchr(format, '%'); } - strcat(buffer, format); + if (buffer) { + strcat(buffer, format); + } + len += strlen(format); - return buffer; + return len; } void view_update_title(struct sway_view *view, bool force) { @@ -592,18 +582,19 @@ void view_update_title(struct sway_view *view, bool force) { } } + free(view->swayc->name); + free(view->swayc->formatted_title); if (title) { - if (view->swayc->name) { - free(view->swayc->name); - } - if (view->swayc->formatted_title) { - free(view->swayc->formatted_title); + size_t len = parse_title_format(view, NULL); + char *buffer = calloc(len + 1, 1); + if (!sway_assert(buffer, "Unable to allocate title string")) { + return; } + parse_title_format(view, buffer); + view->swayc->name = strdup(title); - view->swayc->formatted_title = parse_title_format(view); + view->swayc->formatted_title = buffer; } else { - free(view->swayc->name); - free(view->swayc->formatted_title); view->swayc->name = NULL; view->swayc->formatted_title = NULL; } |