Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions src/widgets/now_playing_mpris.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,87 @@
#include "widgets.h"
#include "now_playing_mpris.h"

static int
hide_widget (JSContextRef ctx, JSObjectRef this) {
struct js_callback_arg arg = widget_data_arg_string("hide");

JSStringRef str = JSStringCreateWithUTF8CString(arg.value.string);
JSValueRef refs[2] = {JSValueMakeString(ctx, str), JSValueMakeString(ctx, str)};
JSStringRelease(str);

JSStringRef str_ondatachanged = JSStringCreateWithUTF8CString("onDataChanged");
JSValueRef func = JSObjectGetProperty(ctx, this, str_ondatachanged, NULL);
JSObjectRef function = JSValueToObject(ctx, func, NULL);
JSStringRelease(str_ondatachanged);

JSObjectCallAsFunction(ctx, function, NULL, 2, refs, NULL);

return 0;
}

static JSValueRef
widget_js_func_toggle (JSContextRef ctx, JSObjectRef func, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *exc) {
PlayerctlPlayer *player = playerctl_player_new(NULL, NULL);
playerctl_player_play_pause(player, NULL);

g_object_unref(player);
if (player) {
playerctl_player_play_pause(player, NULL);
g_object_unref(player);
} else {
hide_widget(ctx, this);
}

return JSValueMakeUndefined(ctx);
}

static JSValueRef
widget_js_func_next (JSContextRef ctx, JSObjectRef func, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *exc) {
PlayerctlPlayer *player = playerctl_player_new(NULL, NULL);
playerctl_player_next(player, NULL);

g_object_unref(player);
if (player) {
playerctl_player_next(player, NULL);
g_object_unref(player);
} else {
hide_widget(ctx, this);
}

return JSValueMakeUndefined(ctx);
}

static JSValueRef
widget_js_func_previous (JSContextRef ctx, JSObjectRef func, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *exc) {
PlayerctlPlayer *player = playerctl_player_new(NULL, NULL);
playerctl_player_previous(player, NULL);
if (player) {
playerctl_player_previous(player, NULL);
g_object_unref(player);
} else {
hide_widget(ctx, this);
}

return JSValueMakeUndefined(ctx);
}

static JSValueRef
widget_js_func_status (JSContextRef ctx, JSObjectRef func, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef *exc) {
PlayerctlPlayer *player = playerctl_player_new(NULL, NULL);
char *raw;
double status;

g_object_get(player, "status", &raw);
if (strcmp(raw, "Playing") == 0) {
status = 0;
}
else {
status = 1;
}

g_object_unref(player);

return JSValueMakeUndefined(ctx);
return JSValueMakeNumber(ctx, status);
}

const JSStaticFunction widget_js_staticfuncs[] = {
{ "toggle", widget_js_func_toggle, kJSPropertyAttributeReadOnly },
{ "next", widget_js_func_next, kJSPropertyAttributeReadOnly },
{ "previous", widget_js_func_previous, kJSPropertyAttributeReadOnly },
{ "status", widget_js_func_status, kJSPropertyAttributeReadOnly },
{ NULL, NULL, 0 },
};

Expand Down Expand Up @@ -68,6 +115,12 @@ on_metadata (PlayerctlPlayer *player, GVariant *e, gpointer user_data) {
update_widget(widget, player);
}

static void
on_status (PlayerctlPlayer *player, gpointer user_data) {
struct widget *widget = user_data;
update_widget(widget, player);
}

void*
widget_main (struct widget *widget) {
struct widget_config config = widget_config_defaults;
Expand All @@ -82,6 +135,8 @@ widget_main (struct widget *widget) {

if (player) {
g_signal_connect(player, "metadata", G_CALLBACK(on_metadata), widget);
g_signal_connect(player, "pause", G_CALLBACK(on_status), widget);
g_signal_connect(player, "play", G_CALLBACK(on_status), widget);

widget_epoll_wait_goto(widget, -1, cleanup);
}
Expand Down