From 82dc747396e5a6814c3bcd66f065c04e68253448 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ingo=20B=C3=BCrk?= Date: Mon, 12 Oct 2015 23:43:47 +0200 Subject: [PATCH] Make pango markup in mode names optional with a flag. This introduces the flag "--pango" on the mode config directive to explicitly enable pango markup for mode names. Not setting this will cause the mode name to be rendered as is. This fixes a regression in 4.11 where mode names containing characters such as '<' would break user's configs as they didn't escape these characters. fixes #1992 --- docs/ipc | 8 ++++-- i3bar/src/mode.c | 47 +++++++++++++++++++++++++++++---- include/bindings.h | 2 +- include/config.h | 1 + include/config_directives.h | 2 +- parser-specs/config.spec | 4 ++- src/bindings.c | 10 ++++--- src/config_directives.c | 8 +++--- testcases/t/201-config-parser.t | 4 +-- 9 files changed, 67 insertions(+), 19 deletions(-) diff --git a/docs/ipc b/docs/ipc index 5113d79b..1813e53a 100644 --- a/docs/ipc +++ b/docs/ipc @@ -717,11 +717,15 @@ This event consists of a single serialized map containing a property This event consists of a single serialized map containing a property +change (string)+ which holds the name of current mode in use. The name is the same as specified in config when creating a mode. The default -mode is simply named default. +mode is simply named default. It contains a second property, +pango_markup+, which +defines whether pango markup shall be used for displaying this mode. *Example:* --------------------------- -{ "change": "default" } +{ + "change": "default", + "pango_markup": true +} --------------------------- === window event diff --git a/i3bar/src/mode.c b/i3bar/src/mode.c index 7f7537af..4b27b110 100644 --- a/i3bar/src/mode.c +++ b/i3bar/src/mode.c @@ -20,6 +20,8 @@ struct mode_json_params { char *json; char *cur_key; + char *name; + bool pango_markup; mode *mode; }; @@ -31,17 +33,35 @@ static int mode_string_cb(void *params_, const unsigned char *val, size_t len) { struct mode_json_params *params = (struct mode_json_params *)params_; if (!strcmp(params->cur_key, "change")) { - /* Save the name */ - params->mode->name = i3string_from_markup_with_length((const char *)val, len); - /* Save its rendered width */ - params->mode->width = predict_text_width(params->mode->name); + char *copy = smalloc(sizeof(const unsigned char) * (len + 1)); + strncpy(copy, (const char *)val, len); + copy[len] = '\0'; - DLOG("Got mode change: %s\n", i3string_as_utf8(params->mode->name)); + params->name = copy; FREE(params->cur_key); + return 1; + } + + FREE(params->cur_key); + return 0; +} + +/* + * Parse a boolean. + * + */ +static int mode_boolean_cb(void *params_, int val) { + struct mode_json_params *params = (struct mode_json_params *)params_; + if (strcmp(params->cur_key, "pango_markup") == 0) { + DLOG("Setting pango_markup to %d.\n", val); + params->pango_markup = val; + + FREE(params->cur_key); return 1; } + FREE(params->cur_key); return 0; } @@ -62,10 +82,27 @@ static int mode_map_key_cb(void *params_, const unsigned char *keyVal, size_t ke return 1; } +static int mode_end_map_cb(void *params_) { + struct mode_json_params *params = (struct mode_json_params *)params_; + + /* Save the name */ + params->mode->name = i3string_from_utf8(params->name); + i3string_set_markup(params->mode->name, params->pango_markup); + /* Save its rendered width */ + params->mode->width = predict_text_width(params->mode->name); + + DLOG("Got mode change: %s\n", i3string_as_utf8(params->mode->name)); + FREE(params->cur_key); + + return 1; +} + /* A datastructure to pass all these callbacks to yajl */ static yajl_callbacks mode_callbacks = { .yajl_string = mode_string_cb, + .yajl_boolean = mode_boolean_cb, .yajl_map_key = mode_map_key_cb, + .yajl_end_map = mode_end_map_cb, }; /* diff --git a/include/bindings.h b/include/bindings.h index 88b4a6cc..e9e5dac9 100644 --- a/include/bindings.h +++ b/include/bindings.h @@ -25,7 +25,7 @@ const char *DEFAULT_BINDING_MODE; */ Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code, const char *release, const char *border, const char *whole_window, - const char *command, const char *mode); + const char *command, const char *mode, bool pango_markup); /** * Grab the bound keys (tell X to send us keypress events for those keycodes) diff --git a/include/config.h b/include/config.h index fd6fe6c0..6312d3d2 100644 --- a/include/config.h +++ b/include/config.h @@ -77,6 +77,7 @@ struct Variable { */ struct Mode { char *name; + bool pango_markup; struct bindings_head *bindings; SLIST_ENTRY(Mode) modes; diff --git a/include/config_directives.h b/include/config_directives.h index c0c70bb4..97b8b424 100644 --- a/include/config_directives.h +++ b/include/config_directives.h @@ -66,7 +66,7 @@ CFGFUN(new_window, const char *windowtype, const char *border, const long width) CFGFUN(workspace, const char *workspace, const char *output); CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *border, const char *whole_window, const char *command); -CFGFUN(enter_mode, const char *mode); +CFGFUN(enter_mode, const char *pango_markup, const char *mode); CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *border, const char *whole_window, const char *command); CFGFUN(bar_font, const char *font); diff --git a/parser-specs/config.spec b/parser-specs/config.spec index b9542c8c..2170ace3 100644 --- a/parser-specs/config.spec +++ b/parser-specs/config.spec @@ -326,8 +326,10 @@ state BINDCOMMAND: ################################################################################ state MODENAME: + pango_markup = '--pango_markup' + -> modename = word - -> call cfg_enter_mode($modename); MODEBRACE + -> call cfg_enter_mode($pango_markup, $modename); MODEBRACE state MODEBRACE: end diff --git a/src/bindings.c b/src/bindings.c index c0e01030..b5f31901 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -27,7 +27,7 @@ const char *DEFAULT_BINDING_MODE = "default"; * the list of modes. * */ -static struct Mode *mode_from_name(const char *name) { +static struct Mode *mode_from_name(const char *name, bool pango_markup) { struct Mode *mode; /* Try to find the mode in the list of modes and return it */ @@ -39,6 +39,7 @@ static struct Mode *mode_from_name(const char *name) { /* If the mode was not found, create a new one */ mode = scalloc(1, sizeof(struct Mode)); mode->name = sstrdup(name); + mode->pango_markup = pango_markup; mode->bindings = scalloc(1, sizeof(struct bindings_head)); TAILQ_INIT(mode->bindings); SLIST_INSERT_HEAD(&modes, mode, modes); @@ -54,7 +55,7 @@ static struct Mode *mode_from_name(const char *name) { */ Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code, const char *release, const char *border, const char *whole_window, - const char *command, const char *modename) { + const char *command, const char *modename, bool pango_markup) { Binding *new_binding = scalloc(1, sizeof(Binding)); DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release); new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS); @@ -91,7 +92,7 @@ Binding *configure_binding(const char *bindtype, const char *modifiers, const ch if (group_bits_set > 1) ELOG("Keybinding has more than one Group specified, but your X server is always in precisely one group. The keybinding can never trigger.\n"); - struct Mode *mode = mode_from_name(modename); + struct Mode *mode = mode_from_name(modename, pango_markup); TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings); return new_binding; @@ -437,7 +438,8 @@ void switch_mode(const char *new_mode) { grab_all_keys(conn); char *event_msg; - sasprintf(&event_msg, "{\"change\":\"%s\"}", mode->name); + sasprintf(&event_msg, "{\"change\":\"%s\", \"pango_markup\":%s}", + mode->name, (mode->pango_markup ? "true" : "false")); ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg); FREE(event_msg); diff --git a/src/config_directives.c b/src/config_directives.c index cd0432fe..99b70db8 100644 --- a/src/config_directives.c +++ b/src/config_directives.c @@ -108,7 +108,7 @@ CFGFUN(font, const char *font) { } CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *border, const char *whole_window, const char *command) { - configure_binding(bindtype, modifiers, key, release, border, whole_window, command, DEFAULT_BINDING_MODE); + configure_binding(bindtype, modifiers, key, release, border, whole_window, command, DEFAULT_BINDING_MODE, false); } /******************************************************************************* @@ -116,12 +116,13 @@ CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, co ******************************************************************************/ static char *current_mode; +static bool current_mode_pango_markup; CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *border, const char *whole_window, const char *command) { - configure_binding(bindtype, modifiers, key, release, border, whole_window, command, current_mode); + configure_binding(bindtype, modifiers, key, release, border, whole_window, command, current_mode, current_mode_pango_markup); } -CFGFUN(enter_mode, const char *modename) { +CFGFUN(enter_mode, const char *pango_markup, const char *modename) { if (strcasecmp(modename, DEFAULT_BINDING_MODE) == 0) { ELOG("You cannot use the name %s for your mode\n", DEFAULT_BINDING_MODE); exit(1); @@ -129,6 +130,7 @@ CFGFUN(enter_mode, const char *modename) { DLOG("\t now in mode %s\n", modename); FREE(current_mode); current_mode = sstrdup(modename); + current_mode_pango_markup = (pango_markup != NULL); } CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) { diff --git a/testcases/t/201-config-parser.t b/testcases/t/201-config-parser.t index a2b0a3a9..7b3a181e 100644 --- a/testcases/t/201-config-parser.t +++ b/testcases/t/201-config-parser.t @@ -53,7 +53,7 @@ mode "meh" { EOT my $expected = <<'EOT'; -cfg_enter_mode(meh) +cfg_enter_mode((null), meh) cfg_mode_binding(bindsym, Mod1,Shift, x, (null), (null), (null), resize grow) cfg_mode_binding(bindcode, Mod1, 44, (null), (null), (null), resize shrink) cfg_mode_binding(bindsym, Mod1, x, --release, (null), (null), exec foo) @@ -627,7 +627,7 @@ mode "yo" { EOT $expected = <<'EOT'; -cfg_enter_mode(yo) +cfg_enter_mode((null), yo) cfg_mode_binding(bindsym, (null), x, (null), (null), (null), resize shrink left) ERROR: CONFIG: Expected one of these tokens: , '#', 'set', 'bindsym', 'bindcode', 'bind', '}' ERROR: CONFIG: (in file ) -- 2.39.2