From a9db903df3bff516b7d4245aec8a64f4a4eb1fe2 Mon Sep 17 00:00:00 2001 From: 5paceManSpiff Date: Fri, 27 Jun 2014 15:20:58 -0600 Subject: [PATCH 1/3] mpris: pause status now changes on external input --- src/widgets/now_playing_mpris.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/widgets/now_playing_mpris.c b/src/widgets/now_playing_mpris.c index 0001648..38d617c 100644 --- a/src/widgets/now_playing_mpris.c +++ b/src/widgets/now_playing_mpris.c @@ -31,10 +31,30 @@ widget_js_func_previous (JSContextRef ctx, JSObjectRef func, JSObjectRef this, s 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 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 }, }; @@ -68,6 +88,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; @@ -82,6 +108,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); } From a41d1a1874ec66ec4700052505a1190d8650659f Mon Sep 17 00:00:00 2001 From: Aaron Landis Date: Sat, 29 Nov 2014 02:47:47 -0500 Subject: [PATCH 2/3] mpris: controls now validate playerctl connection --- src/widgets/now_playing_mpris.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/widgets/now_playing_mpris.c b/src/widgets/now_playing_mpris.c index 38d617c..1ebcab7 100644 --- a/src/widgets/now_playing_mpris.c +++ b/src/widgets/now_playing_mpris.c @@ -4,9 +4,10 @@ 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); + } return JSValueMakeUndefined(ctx); } @@ -14,9 +15,10 @@ widget_js_func_toggle (JSContextRef ctx, JSObjectRef func, JSObjectRef this, siz 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); + } return JSValueMakeUndefined(ctx); } @@ -24,9 +26,10 @@ widget_js_func_next (JSContextRef ctx, JSObjectRef func, JSObjectRef this, size_ 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); - - g_object_unref(player); + if (player) { + playerctl_player_previous(player, NULL); + g_object_unref(player); + } return JSValueMakeUndefined(ctx); } From 4d689a95f011aeeaf501e7318e358becf0d78f49 Mon Sep 17 00:00:00 2001 From: Aaron Landis Date: Sat, 29 Nov 2014 03:41:51 -0500 Subject: [PATCH 3/3] mpris: widget hides if connection stops --- src/widgets/now_playing_mpris.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/widgets/now_playing_mpris.c b/src/widgets/now_playing_mpris.c index 1ebcab7..e3148f8 100644 --- a/src/widgets/now_playing_mpris.c +++ b/src/widgets/now_playing_mpris.c @@ -1,12 +1,32 @@ #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); if (player) { playerctl_player_play_pause(player, NULL); g_object_unref(player); + } else { + hide_widget(ctx, this); } return JSValueMakeUndefined(ctx); @@ -18,6 +38,8 @@ widget_js_func_next (JSContextRef ctx, JSObjectRef func, JSObjectRef this, size_ if (player) { playerctl_player_next(player, NULL); g_object_unref(player); + } else { + hide_widget(ctx, this); } return JSValueMakeUndefined(ctx); @@ -29,6 +51,8 @@ widget_js_func_previous (JSContextRef ctx, JSObjectRef func, JSObjectRef this, s if (player) { playerctl_player_previous(player, NULL); g_object_unref(player); + } else { + hide_widget(ctx, this); } return JSValueMakeUndefined(ctx);