Skip to content
Extraits de code Groupes Projets
Vérifiée Valider ae31efcd rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Separing the mpv event handle

parent 013bb733
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!57Resolve "SDL2 module"
#pragma once #pragma once
#include <sqlite3.h>
#include <mpv/client.h> #include <mpv/client.h>
#include <lektor/net.h>
void lmpv_free(mpv_handle **ctx); void lmpv_free(mpv_handle **ctx);
mpv_handle *lmpv_new(unsigned long int wid); mpv_handle *lmpv_new(unsigned long int wid);
...@@ -11,3 +13,5 @@ int lmpv_is_paused(mpv_handle *ctx, int *ret); ...@@ -11,3 +13,5 @@ int lmpv_is_paused(mpv_handle *ctx, int *ret);
int lmpv_set_volume(mpv_handle *ctx, int vol); int lmpv_set_volume(mpv_handle *ctx, int vol);
int lmpv_load_file(mpv_handle *ctx, const char *file); int lmpv_load_file(mpv_handle *ctx, const char *file);
int lmpv_toggle_pause(mpv_handle *ctx, int *pause); int lmpv_toggle_pause(mpv_handle *ctx, int *pause);
int lmpv_handle(struct lkt_win *win, mpv_handle *ctx, sqlite3 *db, enum mpd_idle_flag *mpd_idle_flag,
int *time_pos, int *time_duration);
...@@ -212,80 +212,6 @@ lx11_handle(struct module_x11_window *win) ...@@ -212,80 +212,6 @@ lx11_handle(struct module_x11_window *win)
* *
*/ */
static inline bool
lmpv_handle(struct lkt_win *win, sqlite3 *db, enum mpd_idle_flag *mpd_idle_events)
{
int ao_volume;
struct lkt_queue_state state;
bool sta = false;
struct module_x11_window *win_x11 = win->window;
mpv_event *event = NULL;
mpv_event_property *prop;
if (!win || !win_x11 || !win_x11->mpv)
goto end;
if (!database_queue_state(db, &state))
goto end;
for (;;) {
event = mpv_wait_event(win_x11->mpv, 0);
switch (event->event_id) {
case MPV_EVENT_PAUSE:
case MPV_EVENT_UNPAUSE:
database_queue_toggle_pause(db);
break;
case MPV_EVENT_SHUTDOWN:
database_queue_stop(db);
win->close(win);
goto end;
case MPV_EVENT_NONE:
goto end;
case MPV_EVENT_IDLE:
if (state.current > 0 && win_x11->mpv_time_pos > 0)
command_next(db, win, mpd_idle_events);
break;
case MPV_EVENT_PROPERTY_CHANGE:
prop = (mpv_event_property *) event->data;
if (prop->format == MPV_FORMAT_NONE)
break;
// MPV volume (BUG: The flag is not MPV_FORMAT_NONE only at the end of the song...) //
if (!strcmp(prop->name, "ao-volume")
&& prop->format == MPV_FORMAT_INT64) {
ao_volume = *(int *) prop->data;
database_config_queue(db, "volume", ao_volume);
}
// File duration //
if (!strcmp(prop->name, "duration")
&& prop->format == MPV_FORMAT_INT64) {
win_x11->mpv_duration = *(int *) prop->data;
database_config_queue(db, "duration", *(int *) prop->data);
}
if (!strcmp(prop->name, "time-pos")
&& prop->format == MPV_FORMAT_INT64) {
win_x11->mpv_time_pos = *(int *) prop->data;
database_config_queue(db, "elapsed", *(int *) prop->data);
}
// Pause state //
if (!strcmp(prop->name, "pause")
&& prop->format == MPV_FORMAT_FLAG)
database_queue_set_paused(db, *(bool *) prop->data);
break;
default:
fprintf(stderr, " ! Unhandled mpv event: %s\n", mpv_event_name(event->event_id));
break;
}
}
sta = true;
end:
return sta;
}
/* /*
* *
...@@ -424,5 +350,8 @@ module_x11_handle_events(struct lkt_win *const win, sqlite3 *db, enum mpd_idle_f ...@@ -424,5 +350,8 @@ module_x11_handle_events(struct lkt_win *const win, sqlite3 *db, enum mpd_idle_f
{ {
if (win == NULL || win->window == NULL) if (win == NULL || win->window == NULL)
return false; return false;
return lx11_handle(win->window) && lmpv_handle(win, db, mpd_idle_events); struct module_x11_window *xwin = win->window;
return lx11_handle(xwin) &&
lmpv_handle(win, xwin->mpv, db, mpd_idle_events, (int *) &xwin->mpv_time_pos,
(int *) &xwin->mpv_duration);
} }
#include <lektor/module/mpv.h> #include <lektor/module/mpv.h>
#include <lektor/commands.h>
#include <lektor/database.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sqlite3.h>
void void
lmpv_free(mpv_handle **ctx) lmpv_free(mpv_handle **ctx)
...@@ -196,3 +199,77 @@ lmpv_toggle_pause(mpv_handle *ctx, int *pause) ...@@ -196,3 +199,77 @@ lmpv_toggle_pause(mpv_handle *ctx, int *pause)
} }
return lmpv_is_paused(ctx, pause); return lmpv_is_paused(ctx, pause);
} }
int
lmpv_handle(struct lkt_win *win, mpv_handle *ctx, sqlite3 *db, enum mpd_idle_flag *mpd_idle_events,
int *time_pos, int *time_duration)
{
int ao_volume;
struct lkt_queue_state state;
bool sta = false;
mpv_event *event = NULL;
mpv_event_property *prop;
if (!ctx || !win)
goto end;
if (!database_queue_state(db, &state))
goto end;
loop:
event = mpv_wait_event(ctx, 0);
switch (event->event_id) {
case MPV_EVENT_PAUSE:
case MPV_EVENT_UNPAUSE:
database_queue_toggle_pause(db);
break;
case MPV_EVENT_SHUTDOWN:
database_queue_stop(db);
win->close(win);
goto end;
case MPV_EVENT_NONE:
goto end;
case MPV_EVENT_IDLE:
if (state.current > 0 && *time_pos > 0)
command_next(db, win, mpd_idle_events);
break;
case MPV_EVENT_PROPERTY_CHANGE:
prop = (mpv_event_property *) event->data;
if (prop->format == MPV_FORMAT_NONE)
break;
// MPV volume (BUG: The flag is not MPV_FORMAT_NONE only at the end of the song...) //
if (!strcmp(prop->name, "ao-volume")
&& prop->format == MPV_FORMAT_INT64) {
ao_volume = *(int *) prop->data;
database_config_queue(db, "volume", ao_volume);
}
// File duration //
if (!strcmp(prop->name, "duration")
&& prop->format == MPV_FORMAT_INT64) {
*time_duration = *(int *) prop->data;
database_config_queue(db, "duration", *(int *) prop->data);
}
if (!strcmp(prop->name, "time-pos")
&& prop->format == MPV_FORMAT_INT64) {
*time_pos = *(int *) prop->data;
database_config_queue(db, "elapsed", *(int *) prop->data);
}
// Pause state //
if (!strcmp(prop->name, "pause")
&& prop->format == MPV_FORMAT_FLAG)
database_queue_set_paused(db, *(bool *) prop->data);
break;
default:
fprintf(stderr, " ! Unhandled mpv event: %s\n", mpv_event_name(event->event_id));
break;
}
goto loop; /* A loop without indentation. */
sta = true;
end:
return sta;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter