Skip to content

Commit

Permalink
optimize display track menu
Browse files Browse the repository at this point in the history
  • Loading branch information
dyphire committed Dec 24, 2023
1 parent fb42945 commit e2317c6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ static bool update_track_menu(plugin_ctx *ctx, dyn_menu *item, const char *type,
if (list == NULL || list->num_entries == 0) return false;

void *tmp = talloc_new(NULL);
bool is_video = strcmp(type, "video") == 0;
bool is_audio = strcmp(type, "audio") == 0;
bool is_sub = strcmp(type, "sub") == 0;

for (int i = 0; i < list->num_entries; i++) {
Expand All @@ -176,10 +178,43 @@ static bool update_track_menu(plugin_ctx *ctx, dyn_menu *item, const char *type,
UINT fState = entry->selected ? MFS_CHECKED : MFS_UNCHECKED;
if (is_sub && entry->selected && pos != entry->id)
fState |= MFS_DISABLED;

const char *key = talloc_asprintf(tmp, "");
if (entry->lang != NULL) {
key = talloc_asprintf_append(key, "%s", entry->lang);
if (entry->codec != NULL)
key = talloc_asprintf_append(key, ", %s", entry->codec);
} else if (entry->codec != NULL) {
key = talloc_asprintf_append(key, "%s", entry->codec);
}
if (is_video) {
if (entry->demux_w && entry->demux_h) {
key = talloc_asprintf_append(key, ", %dx%d",
entry->demux_w, entry->demux_h);
} else if (entry->demux_h) {
key = talloc_asprintf_append(key, ", %dp", entry->demux_h);
}
if (entry->demux_fps > 1)
key = talloc_asprintf_append(key, ", %.3f fps", entry->demux_fps);
}
if (is_audio) {
if (entry->channels)
key = talloc_asprintf_append(key, ", %d ch", entry->channels);
if (entry->samplerate)
key = talloc_asprintf_append(key, ", %d kHz", entry->samplerate / 1000);
if (entry->bitrate)
key = talloc_asprintf_append(key, ", %d kbps", entry->bitrate / 1000);
}
if (entry->forced)
key = talloc_asprintf_append(key, ", Forced");
if (entry->Default)
key = talloc_asprintf_append(key, ", Default");
if (entry->external)
key = talloc_asprintf_append(key, ", External");
append_menu(
item->hmenu, MIIM_STRING | MIIM_DATA | MIIM_STATE, 0, fState,
format_title(item->talloc_ctx, bstr0(entry->title),
bstr0(entry->lang)),
bstr0(key)),
NULL,
talloc_asprintf(item->talloc_ctx, "set %s %d", prop, entry->id));
}
Expand Down
36 changes: 36 additions & 0 deletions src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,24 @@ char *mp_get_prop_string(void *talloc_ctx, const char *prop) {
return ret;
}

void replace_string(char *str, const char *old, const char *new) {
char *ptr = strstr(str, old);
while (ptr != NULL) {
memmove(ptr + strlen(new), ptr + strlen(old), strlen(ptr + strlen(old)) + 1);
memcpy(ptr, new, strlen(new));
ptr = strstr(ptr + strlen(new), old);
}
}

static void update_track_list(mp_state *state, mpv_node *node) {
if (state->track_list != NULL) talloc_free(state->track_list);
state->track_list = talloc_ptrtype(state, state->track_list);
memset(state->track_list, 0, sizeof(*state->track_list));
mp_track_list *list = state->track_list;

void *tmp = talloc_new(NULL);
char *name = mp_get_prop_string(tmp, "filename/no-ext");

for (int i = 0; i < node->u.list->num; i++) {
mpv_node track = node->u.list->values[i];

Expand All @@ -241,13 +253,37 @@ static void update_track_list(mp_state *state, mpv_node *node) {
item.title = talloc_strdup(list, value.u.string);
} else if (strcmp(key, "lang") == 0) {
item.lang = talloc_strdup(list, value.u.string);
} else if (strcmp(key, "codec") == 0) {
item.codec = talloc_strdup(list, value.u.string);
} else if (strcmp(key, "forced") == 0) {
item.forced = value.u.flag;
} else if (strcmp(key, "default") == 0) {
item.Default = value.u.flag;
} else if (strcmp(key, "external") == 0) {
item.external = value.u.flag;
} else if (strcmp(key, "demux-w") == 0) {
item.demux_w = value.u.int64;
} else if (strcmp(key, "demux-h") == 0) {
item.demux_h = value.u.int64;
} else if (strcmp(key, "demux-fps") == 0) {
item.demux_fps = value.u.double_;
} else if (strcmp(key, "demux-channel-count") == 0) {
item.channels = value.u.int64;
} else if (strcmp(key, "demux-samplerate") == 0) {
item.samplerate = value.u.int64;
} else if (strcmp(key, "demux-bitrate") == 0) {
item.bitrate = value.u.int64;
} else if (strcmp(key, "type") == 0) {
item.type = talloc_strdup(list, value.u.string);
} else if (strcmp(key, "selected") == 0) {
item.selected = value.u.flag;
}
}

// replace playback filename for track title
if (item.title)
replace_string(item.title, name, "");

// set default title if not set
if (item.title == NULL)
item.title = talloc_asprintf(list, "%s %d", item.type, item.id);
Expand Down
10 changes: 10 additions & 0 deletions src/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ typedef struct track_item {
char *type; // string describing the media type
char *title; // track title as it is stored in the file
char *lang; // track language as identified by the file
char *codec; // track codec as identified by the file
bool forced;
bool Default;
bool external;
int demux_h;
int demux_w;
double demux_fps;
int channels;
int samplerate;
int bitrate;
bool selected; // if the track is currently decoded
} track_item;

Expand Down

0 comments on commit e2317c6

Please sign in to comment.