1 #include <glib.h>
2 
3 #include "mprisServer.h"
4 #include "logging.h"
5 
6 static GThread *mprisThread;
7 static struct MprisData mprisData;
8 
9 static int oldLoopStatus = -1;
10 static int oldShuffleStatus = -1;
11 
onStart()12 static int onStart() {
13 	oldLoopStatus = mprisData.deadbeef->conf_get_int("playback.loop", 0);
14 	oldShuffleStatus = mprisData.deadbeef->conf_get_int("playback.order", PLAYBACK_ORDER_LINEAR);
15 
16 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 32)
17 	mprisThread = g_thread_create(startServer, (void *)&mprisData, TRUE, NULL);
18 #else
19 	mprisThread = g_thread_new("mpris-listener", startServer, (void *)&mprisData);
20 #endif
21 	return 0;
22 }
23 
onStop()24 static int onStop() {
25 	stopServer();
26 
27 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 32)
28 	g_thread_join(mprisThread);
29 #else
30 	g_thread_unref(mprisThread);
31 #endif
32 
33 	return 0;
34 }
35 
onConnect()36 static int onConnect() {
37 	DB_artwork_plugin_t *artworkPlugin = (DB_artwork_plugin_t *)mprisData.deadbeef->plug_get_for_id ("artwork");
38 
39 	if (artworkPlugin != NULL) {
40 		debug("artwork plugin detected... album art support enabled");
41 		mprisData.artwork = artworkPlugin;
42 	} else {
43 		debug("artwork plugin not detected... album art support disabled");
44 	}
45 
46 	return 0;
47 }
48 
49 //***********************
50 //* Handels signals for *
51 //* - Playback status   *
52 //* - Metadata          *
53 //* - Volume            *
54 //* - Seeked            *
55 //* - Loop status       *
56 //* - Shuffle status    *
57 //***********************
handleEvent(uint32_t id,uintptr_t ctx,uint32_t p1,uint32_t p2)58 static int handleEvent (uint32_t id, uintptr_t ctx, uint32_t p1, uint32_t p2) {
59 	DB_functions_t *deadbeef = mprisData.deadbeef;
60 
61 	switch (id) {
62 		case DB_EV_SEEKED:
63 			debug("DB_EV_SEEKED event received");
64 			emitSeeked(((ddb_event_playpos_t *) ctx)->playpos);
65 			break;
66 		case DB_EV_TRACKINFOCHANGED:
67 			debug("DB_EV_TRACKINFOCHANGED event received");
68 			emitMetadataChanged(-1, &mprisData);
69 			emitCanGoChanged(&mprisData);
70 			break;
71 		case DB_EV_SELCHANGED:
72 		case DB_EV_PLAYLISTSWITCHED:
73 			emitCanGoChanged(&mprisData);
74 			break;
75 		case DB_EV_SONGSTARTED:
76 			debug("DB_EV_SONGSTARTED event received");
77 			emitMetadataChanged(-1, &mprisData);
78 			emitPlaybackStatusChanged(OUTPUT_STATE_PLAYING, &mprisData);
79 			break;
80 		case DB_EV_PAUSED:
81 			debug("DB_EV_PAUSED event received");
82 			emitPlaybackStatusChanged(p1 ? OUTPUT_STATE_PAUSED : OUTPUT_STATE_PLAYING, &mprisData);
83 			break;
84 		case DB_EV_STOP:
85 			debug("DB_EV_STOP event received");
86 			emitPlaybackStatusChanged(OUTPUT_STATE_STOPPED, &mprisData);
87 			break;
88 		case DB_EV_VOLUMECHANGED:
89 			debug("DB_EV_VOLUMECHANGED event received");
90 			emitVolumeChanged(deadbeef->volume_get_db());
91 			break;
92 		case DB_EV_CONFIGCHANGED:
93 			debug("DB_EV_CONFIGCHANGED event received");
94 			if (oldShuffleStatus != -1) {
95 				int newLoopStatus = mprisData.deadbeef->conf_get_int("playback.loop", PLAYBACK_MODE_LOOP_ALL);
96 				int newShuffleStatus = mprisData.deadbeef->conf_get_int("playback.order", PLAYBACK_ORDER_LINEAR);
97 
98 				if (newLoopStatus != oldLoopStatus) {
99 					debug("LoopStatus changed %d", newLoopStatus);
100 					emitLoopStatusChanged(oldLoopStatus = newLoopStatus);
101 				} if (newShuffleStatus != oldShuffleStatus) {
102 					debug("ShuffleStatus changed %d", newShuffleStatus);
103 					emitShuffleStatusChanged(oldShuffleStatus = newShuffleStatus);
104 				}
105 			}
106 			break;
107 		default:
108 			break;
109 	}
110 
111 	return 0;
112 }
113 
114 DB_misc_t plugin = {
115 	.plugin.api_vmajor = DB_API_VERSION_MAJOR,\
116 	.plugin.api_vminor = DB_API_VERSION_MINOR,
117 	.plugin.type = DB_PLUGIN_MISC,
118 	.plugin.version_major = PLUGIN_VERSION_MAJOR,
119 	.plugin.version_minor = PLUGIN_VERSION_MINOR,
120 	.plugin.id = "mpris",
121 	.plugin.name ="MPRISv2 plugin",
122 	.plugin.descr = "Communicate with other applications using D-Bus.",
123 	.plugin.copyright =
124 			"Copyright (C) 2014 Peter Lamby <peterlamby@web.de>\n"
125 			"\n"
126 			"This program is free software; you can redistribute it and/or\n"
127 			"modify it under the terms of the GNU General Public License\n"
128 			"as published by the Free Software Foundation; either version 2\n"
129 			"of the License, or (at your option) any later version.\n"
130 			"\n"
131 			"This program is distributed in the hope that it will be useful,\n"
132 			"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
133 			"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
134 			"GNU General Public License for more details.\n"
135 			"\n"
136 			"You should have received a copy of the GNU General Public License\n"
137 			"along with this program; if not, write to the Free Software\n"
138 			"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n"
139 	,
140 	.plugin.website = "https://github.com/Serranya/deadbeef-mpris2-plugin",
141 	.plugin.start = onStart,
142 	.plugin.stop = onStop,
143 	.plugin.connect = onConnect,
144 	.plugin.disconnect = NULL,
145 	.plugin.configdialog = NULL,
146 	.plugin.message = handleEvent,
147 };
148 
mpris_load(DB_functions_t * ddb)149 DB_plugin_t * mpris_load (DB_functions_t *ddb) {
150 	debug("Loading...");
151 	mprisData.deadbeef = ddb;
152 
153 	return DB_PLUGIN(&plugin);
154 }
155