diff --git a/src/widgets/now_playing_mpris.c b/src/widgets/now_playing_mpris.c index 0001648..e3148f8 100644 --- a/src/widgets/now_playing_mpris.c +++ b/src/widgets/now_playing_mpris.c @@ -1,12 +1,33 @@ #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); } @@ -14,9 +35,12 @@ 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); + } else { + hide_widget(ctx, this); + } return JSValueMakeUndefined(ctx); } @@ -24,17 +48,40 @@ 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); + 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 }, }; @@ -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; @@ -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); }