History

input: use bstr for section name

Resolves mixed use of bstr in internal processing and char* for storage. Also makes faster comparision when searching for section and resolves comparision when name contains null chars.

Kacper Michajłow committed 2 years ago

Commit 1a27f3c1 parent aa35f2e2 files · +56 −55Browse files

Unified · Split

2 files
  1. input/cmd.h +1 −1
  2. input/input.c +55 −54
input/cmd.h+1−1
@@ -105,7 +105,7 @@ typedef struct mp_cmd { · 104 unmodified lines
105105 int flags; // mp_cmd_flags bitfield
106106 char *original;
107107 char *desc; // (usually NULL since stripped away later)
108− char *input_section;
108+ bstr input_section;
109109 bool is_up_down : 1;
110110 bool is_up : 1;
111111 bool emit_on_up : 1;
input/input.c+55−54
@@ -70,7 +70,7 @@ struct cmd_bind_section { · 69 unmodified lines
7070 char *owner;
7171 struct cmd_bind *binds;
7272 int num_binds;
73− char *section;
73+ bstr section;
7474 struct mp_rect mouse_area; // set at runtime, if at all
7575 bool mouse_area_set; // mouse_area is valid and should be tested
7676 };
@@ -78,7 +78,7 @@ struct cmd_bind_section { · 1 unmodified line
7878 #define MP_MAX_SOURCES 10
7979
8080 struct active_section {
81− char *name;
81+ bstr name;
8282 int flags;
8383 };
8484
@@ -129,7 +129,7 @@ struct input_ctx { · 44 unmodified lines
129129 // Mouse position on the consumer side (as command.c sees it)
130130 int mouse_x, mouse_y;
131131 int mouse_hover; // updated on mouse-enter/leave
132− char *mouse_section; // last section to receive mouse event
132+ bstr mouse_section; // last section to receive mouse event
133133
134134 // Mouse position on the producer side (as the VO sees it)
135135 // Unlike mouse_x/y, this can be used to resolve mouse click bindings.
@@ -168,7 +168,7 @@ struct input_ctx { · 32 unmodified lines
168168 };
169169
170170 static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
171− const char *location, const char *restrict_section);
171+ const char *location, bstr section);
172172 static void close_input_sources(struct input_ctx *ictx);
173173 static bool test_mouse(struct input_ctx *ictx, int x, int y, int rej_flags);
174174
@@ -316,9 +316,9 @@ static void append_bind_info(struct input_ctx *ictx, char **pmsg, · 141 unmodified lines
316316 msg = talloc_asprintf_append(msg, " '%s'", stripped);
317317 if (!cmd)
318318 msg = talloc_asprintf_append(msg, " (invalid)");
319− if (strcmp(bind->owner->section, "default") != 0)
320− msg = talloc_asprintf_append(msg, " in section {%s}",
321− bind->owner->section);
319+ if (bstr_equals0(bind->owner->section, "default"))
320+ msg = talloc_asprintf_append(msg, " in section {%.*s}",
321+ BSTR_P(bind->owner->section));
322322 msg = talloc_asprintf_append(msg, " in %s", bind->location);
323323 if (bind->is_builtin)
324324 msg = talloc_asprintf_append(msg, " (default)");
@@ -371,7 +371,7 @@ static struct cmd_bind_section *find_section(struct input_ctx *ictx, · 46 unmodified lines
371371 {
372372 for (int n = 0; n < ictx->num_sections; n++) {
373373 struct cmd_bind_section *bs = ictx->sections[n];
374− if (bstr_equals0(section, bs->section))
374+ if (bstr_equals(section, bs->section))
375375 return bs;
376376 }
377377 return NULL;
@@ -387,7 +387,7 @@ static struct cmd_bind_section *get_bind_section(struct input_ctx *ictx, · 9 unmodified lines
387387 return bind_section;
388388 bind_section = talloc_ptrtype(ictx, bind_section);
389389 *bind_section = (struct cmd_bind_section) {
390− .section = bstrdup0(bind_section, section),
390+ .section = bstrdup(bind_section, section),
391391 .mouse_area = {INT_MIN, INT_MIN, INT_MAX, INT_MAX},
392392 .mouse_area_set = true,
393393 };
@@ -403,9 +403,9 @@ static void key_buf_add(int *buf, int code) · 9 unmodified lines
403403 }
404404
405405 static struct cmd_bind *find_bind_for_key_section(struct input_ctx *ictx,
406− char *section, int code)
406+ bstr section, int code)
407407 {
408− struct cmd_bind_section *bs = get_bind_section(ictx, bstr0(section));
408+ struct cmd_bind_section *bs = get_bind_section(ictx, section);
409409
410410 if (!bs->num_binds)
411411 return NULL;
@@ -439,9 +439,9 @@ static struct cmd_bind *find_bind_for_key_section(struct input_ctx *ictx, · 27 unmodified lines
439439 }
440440
441441 static struct cmd_bind *find_any_bind_for_key(struct input_ctx *ictx,
442− char *force_section, int code)
442+ bstr force_section, int code)
443443 {
444− if (force_section)
444+ if (force_section.len)
445445 return find_bind_for_key_section(ictx, force_section, code);
446446
447447 bool use_mouse = MP_KEY_DEPENDS_ON_MOUSE_POS(code);
@@ -484,7 +484,7 @@ static struct cmd_bind *find_any_bind_for_key(struct input_ctx *ictx, · 36 unmodified lines
484484 return best_bind;
485485 }
486486
487−static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, char *force_section,
487+static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, bstr force_section,
488488 int code)
489489 {
490490 if (ictx->opts->test)
@@ -513,8 +513,8 @@ static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, char *force_section, · 22 unmodified lines
513513 if (ret) {
514514 ret->input_section = cmd->owner->section;
515515 ret->key_name = talloc_steal(ret, mp_input_get_key_combo_name(&code, 1));
516− MP_TRACE(ictx, "key '%s' -> '%s' in '%s'\n",
517− ret->key_name, cmd->cmd, ret->input_section);
516+ MP_TRACE(ictx, "key '%s' -> '%s' in '%.*s'\n",
517+ ret->key_name, cmd->cmd, BSTR_P(ret->input_section));
518518 if (MP_KEY_IS_UNICODE(code)) {
519519 bstr text = {0};
520520 mp_append_utf8_bstr(ret, &text, code);
@@ -533,16 +533,16 @@ static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, char *force_section, · 12 unmodified lines
533533 static void update_mouse_section(struct input_ctx *ictx)
534534 {
535535 struct cmd_bind *bind =
536− find_any_bind_for_key(ictx, NULL, MP_KEY_MOUSE_MOVE);
536+ find_any_bind_for_key(ictx, (bstr){0}, MP_KEY_MOUSE_MOVE);
537537
538− char *new_section = bind ? bind->owner->section : "default";
538+ bstr new_section = bind ? bind->owner->section : bstr0("default");
539539
540− char *old = ictx->mouse_section;
540+ bstr old = ictx->mouse_section;
541541 ictx->mouse_section = new_section;
542542
543− if (strcmp(old, ictx->mouse_section) != 0) {
544− MP_TRACE(ictx, "input: switch section %s -> %s\n",
545− old, ictx->mouse_section);
543+ if (!bstr_equals(old, ictx->mouse_section)) {
544+ MP_TRACE(ictx, "input: switch section %.*s -> %.*s\n",
545+ BSTR_P(old), BSTR_P(ictx->mouse_section));
546546 queue_cmd(ictx, get_cmd_from_keys(ictx, old, MP_KEY_MOUSE_LEAVE));
547547 }
548548 }
@@ -586,7 +586,7 @@ static bool should_drop_cmd(struct input_ctx *ictx, struct mp_cmd *cmd) · 37 unmodified lines
586586 static struct mp_cmd *resolve_key(struct input_ctx *ictx, int code)
587587 {
588588 update_mouse_section(ictx);
589− struct mp_cmd *cmd = get_cmd_from_keys(ictx, NULL, code);
589+ struct mp_cmd *cmd = get_cmd_from_keys(ictx, (bstr){0}, code);
590590 key_buf_add(ictx->key_history, code);
591591 if (cmd && !cmd->def->is_ignore && !should_drop_cmd(ictx, cmd))
592592 return cmd;
@@ -765,7 +765,7 @@ static void feed_key(struct input_ctx *ictx, int code, double scale, · 172 unmodified lines
765765 ictx->mouse_hover = unmod == MP_KEY_MOUSE_ENTER;
766766 update_mouse_section(ictx);
767767
768− mp_cmd_t *cmd = get_cmd_from_keys(ictx, NULL, code);
768+ mp_cmd_t *cmd = get_cmd_from_keys(ictx, (bstr){0}, code);
769769 if (!cmd) // queue dummy cmd so that mouse-pos can notify observers
770770 cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
771771 queue_cmd(ictx, cmd);
@@ -907,7 +907,7 @@ static void set_mouse_pos(struct input_ctx *ictx, int x, int y) · 135 unmodified lines
907907 ictx->mouse_vo_y = y;
908908
909909 update_mouse_section(ictx);
910− struct mp_cmd *cmd = get_cmd_from_keys(ictx, NULL, MP_KEY_MOUSE_MOVE);
910+ struct mp_cmd *cmd = get_cmd_from_keys(ictx, (bstr){0}, MP_KEY_MOUSE_MOVE);
911911 if (!cmd)
912912 cmd = mp_input_parse_cmd(ictx, bstr0("ignore"), "<internal>");
913913
@@ -1060,7 +1060,7 @@ static bool test_mouse(struct input_ctx *ictx, int x, int y, int rej_flags) · 146 unmodified lines
10601060 struct active_section *as = &ictx->active_sections[i];
10611061 if (as->flags & rej_flags)
10621062 continue;
1063− struct cmd_bind_section *s = get_bind_section(ictx, bstr0(as->name));
1063+ struct cmd_bind_section *s = get_bind_section(ictx, as->name);
10641064 if (s->mouse_area_set && test_rect(&s->mouse_area, x, y)) {
10651065 res = true;
10661066 break;
@@ -1197,19 +1197,19 @@ void mp_input_get_mouse_pos(struct input_ctx *ictx, int *x, int *y, int *hover) · 130 unmodified lines
11971197 // If name is NULL, return "default".
11981198 // Return a statically allocated name of the section (i.e. return value never
11991199 // gets deallocated).
1200−static char *normalize_section(struct input_ctx *ictx, char *name)
1200+static bstr normalize_section(struct input_ctx *ictx, bstr name)
12011201 {
1202− return get_bind_section(ictx, bstr0(name))->section;
1202+ return get_bind_section(ictx, name)->section;
12031203 }
12041204
1205−static void disable_section(struct input_ctx *ictx, char *name)
1205+static void disable_section(struct input_ctx *ictx, bstr name)
12061206 {
12071207 name = normalize_section(ictx, name);
12081208
12091209 // Remove old section, or make sure it's on top if re-enabled
12101210 for (int i = ictx->num_active_sections - 1; i >= 0; i--) {
12111211 struct active_section *as = &ictx->active_sections[i];
1212− if (strcmp(as->name, name) == 0) {
1212+ if (bstr_equals(as->name, name)) {
12131213 MP_TARRAY_REMOVE_AT(ictx->active_sections,
12141214 ictx->num_active_sections, i);
12151215 }
@@ -1219,18 +1219,19 @@ static void disable_section(struct input_ctx *ictx, char *name) · 3 unmodified lines
12191219 void mp_input_disable_section(struct input_ctx *ictx, char *name)
12201220 {
12211221 input_lock(ictx);
1222− disable_section(ictx, name);
1222+ disable_section(ictx, bstr0(name));
12231223 input_unlock(ictx);
12241224 }
12251225
12261226 void mp_input_enable_section(struct input_ctx *ictx, char *name, int flags)
12271227 {
1228+ bstr bname = bstr0(name);
12281229 input_lock(ictx);
1229− name = normalize_section(ictx, name);
1230+ bname = normalize_section(ictx, bname);
12301231
1231− disable_section(ictx, name);
1232+ disable_section(ictx, bname);
12321233
1233− MP_TRACE(ictx, "enable section '%s'\n", name);
1234+ MP_TRACE(ictx, "enable section '%.*s'\n", BSTR_P(bname));
12341235
12351236 int top = ictx->num_active_sections;
12361237 if (!(flags & MP_INPUT_ON_TOP)) {
@@ -1241,11 +1242,11 @@ void mp_input_enable_section(struct input_ctx *ictx, char *name, int flags) · 4 unmodified lines
12411242 }
12421243 }
12431244 MP_TARRAY_INSERT_AT(ictx, ictx->active_sections, ictx->num_active_sections,
1244− top, (struct active_section){name, flags});
1245+ top, (struct active_section){bname, flags});
12451246
12461247 MP_TRACE(ictx, "active section stack:\n");
12471248 for (int n = 0; n < ictx->num_active_sections; n++) {
1248− MP_TRACE(ictx, " %s %d\n", ictx->active_sections[n].name,
1249+ MP_TRACE(ictx, " %.*s %d\n", BSTR_P(ictx->active_sections[n].name),
12491250 ictx->active_sections[n].flags);
12501251 }
12511252
@@ -1292,13 +1293,14 @@ static void remove_binds(struct cmd_bind_section *bs, bool builtin) · 40 unmodified lines
12921293 void mp_input_define_section(struct input_ctx *ictx, char *name, char *location,
12931294 char *contents, bool builtin, char *owner)
12941295 {
1295− if (!name || !name[0])
1296+ bstr bname = bstr0(name);
1297+ if (!bname.len)
12961298 return; // parse_config() changes semantics with restrict_section==empty
12971299 input_lock(ictx);
12981300 // Delete:
1299− struct cmd_bind_section *bs = get_bind_section(ictx, bstr0(name));
1301+ struct cmd_bind_section *bs = get_bind_section(ictx, bname);
13001302 if ((!bs->owner || (owner && strcmp(bs->owner, owner) != 0)) &&
1301− strcmp(bs->section, "default") != 0)
1303+ !bstr_equals0(bs->section, "default"))
13021304 {
13031305 talloc_free(bs->owner);
13041306 bs->owner = talloc_strdup(bs, owner);
@@ -1306,10 +1308,10 @@ void mp_input_define_section(struct input_ctx *ictx, char *name, char *location, · 1 unmodified line
13061308 remove_binds(bs, builtin);
13071309 if (contents && contents[0]) {
13081310 // Redefine:
1309− parse_config(ictx, builtin, bstr0(contents), location, name);
1311+ parse_config(ictx, builtin, bstr0(contents), location, bname);
13101312 } else {
13111313 // Disable:
1312− disable_section(ictx, name);
1314+ disable_section(ictx, bname);
13131315 }
13141316 input_unlock(ictx);
13151317 }
@@ -1375,17 +1377,17 @@ static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section, · 59 unmodified lines
13751377 memcpy(bind->keys, keys, num_keys * sizeof(bind->keys[0]));
13761378 if (mp_msg_test(ictx->log, MSGL_DEBUG)) {
13771379 char *s = mp_input_get_key_combo_name(keys, num_keys);
1378− MP_TRACE(ictx, "add: section='%s' key='%s'%s cmd='%s' location='%s'\n",
1379− bind->owner->section, s, bind->is_builtin ? " builtin" : "",
1380+ MP_TRACE(ictx, "add: section='%.*s' key='%s'%s cmd='%s' location='%s'\n",
1381+ BSTR_P(bind->owner->section), s, bind->is_builtin ? " builtin" : "",
13801382 bind->cmd, bind->location);
13811383 talloc_free(s);
13821384 }
13831385 }
13841386
1385−// restrict_section: every entry is forced to this section name
1386−// if NULL, load normally and allow any sections
1387+// section: every entry is forced to this section name
1388+// if NULL, load normally and allow any sections
13871389 static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
1388− const char *location, const char *restrict_section)
1390+ const char *location, bstr section)
13891391 {
13901392 int n_binds = 0;
13911393 int line_no = 0;
@@ -1432,7 +1434,6 @@ static int parse_config(struct input_ctx *ictx, bool builtin, bstr data, · 40 unmodified lines
14321434 }
14331435 talloc_free(name);
14341436
1435− bstr section = bstr0(restrict_section);
14361437 if (!section.len) {
14371438 if (bstr_startswith0(command, "{")) {
14381439 int p = bstrchr(command, '}');
@@ -1479,7 +1480,7 @@ static bool parse_config_file(struct input_ctx *ictx, char *file) · 40 unmodified lines
14791480 bstr data = stream_read_complete(s, tmp, 1000000);
14801481 if (data.start) {
14811482 MP_VERBOSE(ictx, "Parsing input config file %s\n", file);
1482− int num = parse_config(ictx, false, data, file, NULL);
1483+ int num = parse_config(ictx, false, data, file, (bstr){0});
14831484 MP_VERBOSE(ictx, "Input config file %s parsed: %d binds\n", file, num);
14841485 r = true;
14851486 } else {
@@ -1502,7 +1503,7 @@ struct input_ctx *mp_input_init(struct mpv_global *global, · 16 unmodified lines
15021503 .global = global,
15031504 .ar_state = -1,
15041505 .log = mp_log_new(ictx, global->log, "input"),
1505− .mouse_section = "default",
1506+ .mouse_section = bstr0("default"),
15061507 .opts_cache = m_config_cache_alloc(ictx, global, &input_config),
15071508 .wakeup_cb = wakeup_cb,
15081509 .wakeup_ctx = wakeup_ctx,
@@ -1559,7 +1560,7 @@ void mp_input_load_config(struct input_ctx *ictx) · 50 unmodified lines
15591560 bstr line = bstr_getline(builtin, &builtin);
15601561 bstr_eatstart0(&line, "#");
15611562 if (!bstr_startswith0(line, " "))
1562− parse_config(ictx, true, line, "<builtin>", NULL);
1563+ parse_config(ictx, true, line, "<builtin>", (bstr){0});
15631564 }
15641565
15651566 bool config_ok = false;
@@ -1679,8 +1680,8 @@ void mp_input_bind_key(struct input_ctx *ictx, int key, bstr command) · 113 unmodified lines
16791680 memcpy(bind->keys, &key, 1 * sizeof(bind->keys[0]));
16801681 if (mp_msg_test(ictx->log, MSGL_DEBUG)) {
16811682 char *s = mp_input_get_key_combo_name(&key, 1);
1682− MP_TRACE(ictx, "add:section='%s' key='%s'%s cmd='%s' location='%s'\n",
1683− bind->owner->section, s, bind->is_builtin ? " builtin" : "",
1683+ MP_TRACE(ictx, "add:section='%.*s' key='%s'%s cmd='%s' location='%s'\n",
1684+ BSTR_P(bind->owner->section), s, bind->is_builtin ? " builtin" : "",
16841685 bind->cmd, bind->location);
16851686 talloc_free(s);
16861687 }
@@ -1699,7 +1700,7 @@ struct mpv_node mp_input_get_bindings(struct input_ctx *ictx) · 12 unmodified lines
16991700
17001701 for (int i = 0; i < ictx->num_active_sections; i++) {
17011702 struct active_section *as = &ictx->active_sections[i];
1702− if (strcmp(as->name, s->section) == 0) {
1703+ if (bstr_equals(as->name, s->section)) {
17031704 priority = i;
17041705 break;
17051706 }
@@ -1718,7 +1719,7 @@ struct mpv_node mp_input_get_bindings(struct input_ctx *ictx) · 12 unmodified lines
17181719 if (b_priority >= 0 && !b->is_builtin)
17191720 b_priority += ictx->num_active_sections;
17201721
1721− node_map_add_string(entry, "section", s->section);
1722+ node_map_add_string(entry, "section", s->section.start);
17221723 if (s->owner)
17231724 node_map_add_string(entry, "owner", s->owner);
17241725 node_map_add_string(entry, "cmd", b->cmd);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t