Skip to content

Commit

Permalink
move prop change handler to types
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Dec 25, 2023
1 parent 29234f0 commit 8cdd830
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 71 deletions.
71 changes: 8 additions & 63 deletions src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,57 +62,13 @@ static void plugin_register(int64_t wid) {
}

static void handle_property_change(mpv_event *event) {
mp_state *state = ctx->state;
mpv_event_property *prop = event->data;
switch (prop->format) {
case MPV_FORMAT_NONE:
if (strcmp(prop->name, "vid") == 0) {
state->vid = -1;
} else if (strcmp(prop->name, "aid") == 0) {
state->aid = -1;
} else if (strcmp(prop->name, "sid") == 0) {
state->sid = -1;
} else if (strcmp(prop->name, "secondary-sid") == 0) {
state->sid2 = -1;
}
break;
case MPV_FORMAT_INT64:
if (strcmp(prop->name, "window-id") == 0) {
int64_t wid = *(int64_t *)prop->data;
if (wid > 0) plugin_register(wid);
} else if (strcmp(prop->name, "vid") == 0) {
state->vid = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "aid") == 0) {
state->aid = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "sid") == 0) {
state->sid = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "secondary-sid") == 0) {
state->sid2 = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "chapter") == 0) {
state->chapter = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "current-edition") == 0) {
state->edition = *(int64_t *)prop->data;
}
break;
case MPV_FORMAT_STRING:
if (strcmp(prop->name, "audio-device") == 0) {
if (state->audio_device != NULL)
talloc_free(state->audio_device);
char *val = *(char **)prop->data;
state->audio_device = talloc_strdup(state, val);
}
break;
case MPV_FORMAT_NODE:
if (strcmp(prop->name, "track-list") == 0) {
update_track_list(state, prop->data);
} else if (strcmp(prop->name, "chapter-list") == 0) {
update_chapter_list(state, prop->data);
} else if (strcmp(prop->name, "edition-list") == 0) {
update_edition_list(state, prop->data);
} else if (strcmp(prop->name, "audio-device-list") == 0) {
update_audio_device_list(state, prop->data);
}
break;
if (prop->format == MPV_FORMAT_INT64 &&
strcmp(prop->name, "window-id") == 0) {
int64_t wid = *(int64_t *)prop->data;
if (wid > 0) plugin_register(wid);
} else {
mp_state_update(ctx->state, event);
}
}

Expand Down Expand Up @@ -146,22 +102,11 @@ static void destroy_plugin_ctx() {
MPV_EXPORT int mpv_open_cplugin(mpv_handle *handle) {
ctx = create_plugin_ctx(NULL);
ctx->mpv = handle;
plugin_read_conf(ctx->conf, mpv_client_name(handle));

ctx->dispatch = mp_dispatch_create(ctx);

plugin_read_conf(ctx->conf, mpv_client_name(handle));
mpv_observe_property(handle, 0, "window-id", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, "vid", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, "aid", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, "sid", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, "secondary-sid", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, "chapter", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, "current-edition", MPV_FORMAT_INT64);
mpv_observe_property(handle, 0, "audio-device", MPV_FORMAT_STRING);
mpv_observe_property(handle, 0, "track-list", MPV_FORMAT_NODE);
mpv_observe_property(handle, 0, "chapter-list", MPV_FORMAT_NODE);
mpv_observe_property(handle, 0, "edition-list", MPV_FORMAT_NODE);
mpv_observe_property(handle, 0, "audio-device-list", MPV_FORMAT_NODE);
mp_state_init(ctx->state, handle);

while (handle) {
mpv_event *event = mpv_wait_event(handle, -1);
Expand Down
73 changes: 69 additions & 4 deletions src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "misc/ctype.h"
#include "types.h"

void update_track_list(mp_state *state, mpv_node *node) {
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));
Expand Down Expand Up @@ -48,7 +48,7 @@ void update_track_list(mp_state *state, mpv_node *node) {
}
}

void update_chapter_list(mp_state *state, mpv_node *node) {
static void update_chapter_list(mp_state *state, mpv_node *node) {
if (state->chapter_list != NULL) talloc_free(state->chapter_list);
state->chapter_list = talloc_ptrtype(state, state->chapter_list);
memset(state->chapter_list, 0, sizeof(*state->chapter_list));
Expand Down Expand Up @@ -78,7 +78,7 @@ void update_chapter_list(mp_state *state, mpv_node *node) {
}
}

void update_edition_list(mp_state *state, mpv_node *node) {
static void update_edition_list(mp_state *state, mpv_node *node) {
if (state->edition_list != NULL) talloc_free(state->edition_list);
state->edition_list = talloc_ptrtype(state, state->edition_list);
memset(state->edition_list, 0, sizeof(*state->edition_list));
Expand Down Expand Up @@ -108,7 +108,7 @@ void update_edition_list(mp_state *state, mpv_node *node) {
}
}

void update_audio_device_list(mp_state *state, mpv_node *node) {
static void update_audio_device_list(mp_state *state, mpv_node *node) {
if (state->audio_device_list != NULL) talloc_free(state->audio_device_list);
state->audio_device_list = talloc_ptrtype(state, state->audio_device_list);
memset(state->audio_device_list, 0, sizeof(*state->audio_device_list));
Expand All @@ -132,4 +132,69 @@ void update_audio_device_list(mp_state *state, mpv_node *node) {

MP_TARRAY_APPEND(list, list->entries, list->num_entries, item);
}
}

void mp_state_init(mp_state *state, mpv_handle *mpv) {
mpv_observe_property(mpv, 0, "vid", MPV_FORMAT_INT64);
mpv_observe_property(mpv, 0, "aid", MPV_FORMAT_INT64);
mpv_observe_property(mpv, 0, "sid", MPV_FORMAT_INT64);
mpv_observe_property(mpv, 0, "secondary-sid", MPV_FORMAT_INT64);
mpv_observe_property(mpv, 0, "chapter", MPV_FORMAT_INT64);
mpv_observe_property(mpv, 0, "current-edition", MPV_FORMAT_INT64);
mpv_observe_property(mpv, 0, "audio-device", MPV_FORMAT_STRING);
mpv_observe_property(mpv, 0, "track-list", MPV_FORMAT_NODE);
mpv_observe_property(mpv, 0, "chapter-list", MPV_FORMAT_NODE);
mpv_observe_property(mpv, 0, "edition-list", MPV_FORMAT_NODE);
mpv_observe_property(mpv, 0, "audio-device-list", MPV_FORMAT_NODE);
}

void mp_state_update(mp_state *state, mpv_event *event) {
mpv_event_property *prop = event->data;
switch (prop->format) {
case MPV_FORMAT_NONE:
if (strcmp(prop->name, "vid") == 0) {
state->vid = -1;
} else if (strcmp(prop->name, "aid") == 0) {
state->aid = -1;
} else if (strcmp(prop->name, "sid") == 0) {
state->sid = -1;
} else if (strcmp(prop->name, "secondary-sid") == 0) {
state->sid2 = -1;
}
break;
case MPV_FORMAT_INT64:
if (strcmp(prop->name, "vid") == 0) {
state->vid = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "aid") == 0) {
state->aid = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "sid") == 0) {
state->sid = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "secondary-sid") == 0) {
state->sid2 = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "chapter") == 0) {
state->chapter = *(int64_t *)prop->data;
} else if (strcmp(prop->name, "current-edition") == 0) {
state->edition = *(int64_t *)prop->data;
}
break;
case MPV_FORMAT_STRING:
if (strcmp(prop->name, "audio-device") == 0) {
if (state->audio_device != NULL)
talloc_free(state->audio_device);
char *val = *(char **)prop->data;
state->audio_device = talloc_strdup(state, val);
}
break;
case MPV_FORMAT_NODE:
if (strcmp(prop->name, "track-list") == 0) {
update_track_list(state, prop->data);
} else if (strcmp(prop->name, "chapter-list") == 0) {
update_chapter_list(state, prop->data);
} else if (strcmp(prop->name, "edition-list") == 0) {
update_edition_list(state, prop->data);
} else if (strcmp(prop->name, "audio-device-list") == 0) {
update_audio_device_list(state, prop->data);
}
break;
}
}
6 changes: 2 additions & 4 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ typedef struct {
mp_audio_device_list *audio_device_list; // audio device list
} mp_state;

void update_track_list(mp_state *state, mpv_node *node);
void update_chapter_list(mp_state *state, mpv_node *node);
void update_edition_list(mp_state *state, mpv_node *node);
void update_audio_device_list(mp_state *state, mpv_node *node);
void mp_state_init(mp_state *state, mpv_handle *mpv);
void mp_state_update(mp_state *state, mpv_event *event);

#endif

0 comments on commit 8cdd830

Please sign in to comment.