suggested patch to add --get (query)

"Rick C. Petty" <[email protected]> Mon, 20 Mar 2006 17:29:29 -0600
Newsgroups gmane.comp.multimedia.xmms.devel
Message-ID <[email protected]>
--MfFXiAuoTsnnDAfZ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

One thing missing from XMMS's command-line options is the ability to query
attributes from a session.  Being able to get these attributes would make
xmms even more powerful, from the perspective of scripting languages, etc.

I quickly ran through the list of remote "get" commands and threw together
some command-line processing to query these attributes.  I've attached a
patch (applied to 1.2.10) which adds this feature to the stock xmms.  To
test its operation, try the following:
	xmms -g help
	xmms -g all
	xmms -g all,no-is-main-win,no-is-pl-win,no-is-eq-win
	xmms -g all,none

Please consider applying this patch to the upcoming version!

Contact me directly; I am not on the mailing list.  Thank you,

-- Rick C. Petty

--MfFXiAuoTsnnDAfZ
Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="patch-xmms::main.c"

--- xmms/main.c.orig	Mon Feb 23 14:31:43 2004
+++ xmms/main.c	Mon Mar 20 17:23:01 2006
@@ -3212,6 +3212,7 @@
 	{"show-main-window", 0, NULL, 'm'},
 	{"version", 0, NULL, 'v'},
 	{"sm-client-id", 1, NULL, 'i'},
+	{"get", 1, NULL, 'g'},
 	{0, 0, 0, 0}
 };
 
@@ -3256,9 +3257,137 @@
 	fprintf(stderr, "\n-v, --version		");
 	/* -v, --version switch */
 	fprintf(stderr, _("Print version number and exit."));
+	fprintf(stderr, "\n-g, --get		");
+	/* -g, --get switch */
+	fprintf(stderr, _("Remote query tool (see \"--get help\")."));
 	fprintf(stderr, "\n\n");
 	exit(0);
 }
+void display_query_usage(void)
+{
+	fprintf(stderr, _("Remote Query Options:\n"
+			  "---------------------\n"));
+	fprintf(stderr, "\n  version		");
+	fprintf(stderr, _("Print remote xmms version."));
+	fprintf(stderr, "\n  is-playing		");
+	fprintf(stderr, _("Query whether input is playing."));
+	fprintf(stderr, "\n  is-paused		");
+	fprintf(stderr, _("Query whether input is paused."));
+	fprintf(stderr, "\n  playlist-pos		");
+	fprintf(stderr, _("Print current playlist position."));
+	fprintf(stderr, "\n  playlist-length	");
+	fprintf(stderr, _("Print length of playlist."));
+	fprintf(stderr, "\n  output-time		");
+	fprintf(stderr, _("Print current output time."));
+	fprintf(stderr, "\n  volume		");
+	fprintf(stderr, _("Print volume."));
+	fprintf(stderr, "\n  main-volume		");
+	fprintf(stderr, _("Print main volume."));
+	fprintf(stderr, "\n  balance		");
+	fprintf(stderr, _("Print [left/right] balance."));
+	fprintf(stderr, "\n  skin			");
+	fprintf(stderr, _("Print filename of current skin."));
+	fprintf(stderr, "\n  playlist-file		");
+	fprintf(stderr, _("Print filename of current playlist item."));
+	fprintf(stderr, "\n  playlist-title	");
+	fprintf(stderr, _("Print title of current playlist item."));
+	fprintf(stderr, "\n  playlist-time		");
+	fprintf(stderr, _("Print total time of current playlist item."));
+	fprintf(stderr, "\n  info			");
+	fprintf(stderr, _("Print current item's bitrate, freq., # channels."));
+	fprintf(stderr, "\n  is-main-win		");
+	fprintf(stderr, _("Query whether main window is enabled."));
+	fprintf(stderr, "\n  is-pl-win		");
+	fprintf(stderr, _("Query whether playlist window is enabled."));
+	fprintf(stderr, "\n  is-eq-win		");
+	fprintf(stderr, _("Query whether equilizer window is enabled."));
+	fprintf(stderr, "\n  is-running		");
+	fprintf(stderr, _("Query whether xmms is running."));
+	fprintf(stderr, "\n  is-repeat		");
+	fprintf(stderr, _("Query whether repeat is enabled."));
+	fprintf(stderr, "\n  is-shuffle		");
+	fprintf(stderr, _("Query whether shuffle is enabled."));
+	fprintf(stderr, "\n\n  no-		");
+	fprintf(stderr, _("Prefix, disable a particular query."));
+	fprintf(stderr, "\n  all		");
+	fprintf(stderr, _("Enable all queries."));
+	fprintf(stderr, "\n  none		");
+	fprintf(stderr, _("Disable all queries."));
+	fprintf(stderr, "\n  help		");
+	fprintf(stderr, _("Display this text and exit."));
+	fprintf(stderr, "\n\n");
+	exit(0);
+}
+enum
+{
+	Q_VERSION, Q_IS_PLAYING, Q_IS_PAUSED, Q_PLAYLIST_POS,
+	Q_PLAYLIST_LENGTH, Q_OUTPUT_TIME, Q_VOLUME, Q_MAIN_VOLUME,
+	Q_BALANCE, Q_SKIN, Q_PLAYLIST_FILE, Q_PLAYLIST_TITLE,
+	Q_PLAYLIST_TIME, Q_INFO, Q_IS_MAIN_WIN, Q_IS_PL_WIN, Q_IS_EQ_WIN,
+	Q_IS_RUNNING, Q_IS_REPEAT, Q_IS_SHUFFLE
+};
+const gchar*	QUERIES[] =
+{
+	"version", "is-playing", "is-paused", "playlist-pos",
+	"playlist-length", "output-time", "volume", "main-volume",
+	"balance", "skin", "playlist-file", "playlist-title",
+	"playlist-time", "info", "is-main-win", "is-pl-win", "is-eq-win",
+	"is-running", "is-repeat", "is-shuffle",
+	NULL
+};
+void	parse_query_string(gulong* query_list, const gchar* query)
+{
+	gchar**	queries;
+	gint q;
+
+	if (NULL == query_list || NULL == query)
+		return;
+	queries = g_strsplit(query, ",", 31);
+	if (NULL == queries)
+		return;
+	for (q = 0; NULL != queries[q]; q++)
+	{
+		gboolean disable = FALSE;
+		gint query_index;
+
+		g_strdown(queries[q]);
+		query = queries[q];
+		if (!strcmp("help", query))
+		{
+			display_query_usage();
+			break;
+		}
+		if (!strcmp("all", query))
+		{
+			*query_list = (gulong)-1;
+			continue;
+		}
+		if (!strcmp("none", query))
+		{
+			*query_list = 0;
+			continue;
+		}
+		if (!strncmp("no-", query, 3))
+		{
+			disable = TRUE;
+			query += 3;
+		}
+		for (query_index = 0; NULL != QUERIES[query_index]; query_index++)
+			if (!strcmp(QUERIES[query_index], query))
+			{
+				if (disable)
+					*query_list &= ~(1 << query_index);
+				else
+					*query_list |= (1 << query_index);
+				break;
+			}
+		if (NULL != QUERIES[query_index])
+			continue;
+		fprintf(stderr, "%s: %s\n", _("Unknown query option: "), query);
+//		exit(1);
+	}
+}
+
 
 struct cmdlineopt {
 	GList *filenames;
@@ -3266,8 +3395,25 @@
 	gboolean play, stop, pause, fwd, rew, play_pause;
 	gboolean enqueue, mainwin, remote;
 	char *previous_session_id;
+	gulong queries;
 };
 
+gchar*	format_time(gint t)
+{
+	gint h, m, s, ms;
+
+	ms = t % 1000;
+	s = t / 1000;
+	m = s / 60;
+	if (!m)
+		return g_strdup_printf("%u.%3.3u", s, ms);
+	s %= 60;
+	h = m / 60;
+	if (!h)
+		return g_strdup_printf("%u:%2.2u.%3.3u", m, s, ms);
+	m %= 60;
+	return g_strdup_printf("%u:%2.2u:%2.2u.%3.3u", h, m, s, ms);
+}
 void parse_cmd_line(int argc, char **argv, struct cmdlineopt *opt)
 {
 	int c, i;
@@ -3275,7 +3421,7 @@
 
 	memset(opt, 0, sizeof(struct cmdlineopt));
 	opt->session = -1;
-	while ((c = getopt_long(argc, argv, "hn:rpusfemvt", long_options, NULL)) != -1)
+	while ((c = getopt_long(argc, argv, "hn:rpusfemvtg:", long_options, NULL)) != -1)
 	{
 		switch (c)
 		{
@@ -3316,6 +3462,9 @@
 			case 'i':
 				opt->previous_session_id = g_strdup(optarg);
 				break;
+			case 'g':
+				parse_query_string(&opt->queries, optarg);
+				break;
 		}
 	}
 	for (i = optind; i < argc; i++)
@@ -3365,6 +3514,107 @@
 			xmms_remote_set_playlist_pos (opt->session, pos);
 		if (!opt->enqueue)
 			xmms_remote_play(opt->session);
+	}
+	if (opt->queries)
+	{
+		gint s, q;
+
+		s = opt->session;
+		printf(_("Query results:\n"));
+		for (q = 0; NULL != QUERIES[q]; q++)
+		{
+			gint vl, vr, pos, val, rate, freq, nch;
+			gchar* str;
+
+			if (!(opt->queries & (1 << q)))
+				continue;
+			printf("%-15s\t", QUERIES[q]);
+			switch (q)
+			{
+			case Q_VERSION:
+				printf("%d\n", xmms_remote_get_version(s));
+				break;
+			case Q_IS_PLAYING:
+				printf("%d\n", (int)xmms_remote_is_playing(s));
+				break;
+			case Q_IS_PAUSED:
+				printf("%d\n", (int)xmms_remote_is_paused(s));
+				break;
+			case Q_PLAYLIST_POS:
+				printf("%d\n", xmms_remote_get_playlist_pos(s));
+				break;
+			case Q_PLAYLIST_LENGTH:
+				printf("%d\n", xmms_remote_get_playlist_length(s));
+				break;
+			case Q_OUTPUT_TIME:
+				val = xmms_remote_get_output_time(s);
+				str = format_time(val);
+				printf("%d [%s]\n", val, str);
+				g_free(str);
+				break;
+			case Q_VOLUME:
+				xmms_remote_get_volume(s, &vl, &vr);
+				printf("%d:%-3d\n", vl, vr);
+				break;
+			case Q_MAIN_VOLUME:
+				printf("%d\n", xmms_remote_get_main_volume(s));
+				break;
+			case Q_BALANCE:
+				printf("%d\n", xmms_remote_get_balance(s));
+				break;
+			case Q_SKIN:
+				str = xmms_remote_get_skin(s);
+				if (NULL == str)
+					printf(_("(none)\n"));
+				else
+				{
+					printf("%s\n", str);
+					g_free(str);
+				}
+				break;
+			case Q_PLAYLIST_FILE:
+				pos = xmms_remote_get_playlist_pos(s);
+				str = xmms_remote_get_playlist_file(s, pos);
+				printf("%s\n", str);
+				g_free(str);
+				break;
+			case Q_PLAYLIST_TITLE:
+				pos = xmms_remote_get_playlist_pos(s);
+				str = xmms_remote_get_playlist_title(s, pos);
+				printf("%s\n", str);
+				g_free(str);
+				break;
+			case Q_PLAYLIST_TIME:
+				pos = xmms_remote_get_playlist_pos(s);
+				val = xmms_remote_get_playlist_time(s, pos);
+				str = format_time(val);
+				printf("%d [%s]\n", val, str);
+				g_free(str);
+				break;
+			case Q_INFO:
+				xmms_remote_get_info(s, &rate, &freq, &nch);
+				printf("%d bps, %d Hz, %d channels\n", rate, freq, nch);
+				break;
+			case Q_IS_MAIN_WIN:
+				printf("%d\n", (int)xmms_remote_is_main_win(s));
+				break;
+			case Q_IS_PL_WIN:
+				printf("%d\n", (int)xmms_remote_is_pl_win(s));
+				break;
+			case Q_IS_EQ_WIN:
+				printf("%d\n", (int)xmms_remote_is_eq_win(s));
+				break;
+			case Q_IS_RUNNING:
+				printf("%d\n", (int)xmms_remote_is_running(s));
+				break;
+			case Q_IS_REPEAT:
+				printf("%d\n", (int)xmms_remote_is_repeat(s));
+				break;
+			case Q_IS_SHUFFLE:
+				printf("%d\n", (int)xmms_remote_is_shuffle(s));
+				break;
+			}
+		}
 	}
 	if (opt->rew)
 		xmms_remote_playlist_prev(opt->session);

--MfFXiAuoTsnnDAfZ
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
xmms-devel mailing list
[email protected]
http://lists.xmms.org/mailman/listinfo/xmms-devel

--MfFXiAuoTsnnDAfZ--