This patch automatically detects, whether it is run with GNOME 2 resp.
KDE 3.2 session and sets proper output plugin (esd resp. arts). This
behavior is now default.

================================================================================
--- xmms/main.c
+++ xmms/main.c
@@ -351,6 +351,7 @@
 	cfg.use_pl_metadata = TRUE;
 
 	cfg.skin = (gchar *)(-1);
+	cfg.outputplugin = (gchar *)(-1);
 
 	cfg.playlist_x = 295;
 	cfg.playlist_y = 20;
@@ -470,6 +471,17 @@
 				}
 		}
 		xmms_cfg_read_string(cfgfile, "xmms", "output_plugin", &cfg.outputplugin);
+
+		if (cfg.outputplugin != (gchar *)(-1)) {
+			if (cfg.outputplugin && !strcmp(cfg.outputplugin, "(automatic)"))
+			{
+				if (cfg.outputplugin)
+					g_free(cfg.outputplugin);
+				cfg.outputplugin = NULL;
+			} else {
+				session_output_used = 0;
+			}
+		}
 		xmms_cfg_read_string(cfgfile, "xmms", "enabled_gplugins", &cfg.enabled_gplugins);
 		xmms_cfg_read_string(cfgfile, "xmms", "enabled_vplugins", &cfg.enabled_vplugins);
 		xmms_cfg_read_string(cfgfile, "xmms", "enabled_eplugins", &cfg.enabled_eplugins);
@@ -526,6 +538,7 @@
 		/*
 		 * FIXME: This implisitly means the output plugin that is first
 		 * in the alphabet will be used (usually the disk writer plugin)
+		 * NOTE: Now fixed in pluginenum.c.
 		 */
 		cfg.outputplugin = g_strdup("");
 #endif
@@ -650,10 +663,13 @@
 			xmms_cfg_write_string(cfgfile, "xmms", "skin", skin->path);
 		else
 			xmms_cfg_write_string(cfgfile, "xmms", "skin", "(none)");
-	if (get_current_output_plugin())
-		xmms_cfg_write_string(cfgfile, "xmms", "output_plugin", get_current_output_plugin()->filename);
+	if (session_output_used)
+		xmms_cfg_write_string(cfgfile, "xmms", "output_plugin", "(automatic)");
 	else
-		xmms_cfg_remove_key(cfgfile, "xmms", "output_plugin");
+		if (get_current_output_plugin())
+			xmms_cfg_write_string(cfgfile, "xmms", "output_plugin", get_current_output_plugin()->filename);
+		else
+			xmms_cfg_remove_key(cfgfile, "xmms", "output_plugin");
 
 	str = general_stringify_enabled_list();
 	if (str)
--- xmms/output.c
+++ xmms/output.c
@@ -18,6 +18,7 @@
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 #include "xmms.h"
+#include "pluginenum.h"
 
 struct OutputPluginData *op_data;
 
@@ -33,6 +34,7 @@
 		op_data->current_output_plugin = node->data;
 	else
 		op_data->current_output_plugin = NULL;
+	session_output_used = (op_data->current_output_plugin == &output_auto);
 }
 
 GList *get_output_list(void)
--- xmms/pluginenum.c
+++ xmms/pluginenum.c
@@ -35,6 +35,7 @@
 # define RTLD_NOW 0
 #endif
 
+int session_output_used = 1;
 
 char *plugin_dir_list[] =
 {
@@ -51,6 +52,9 @@
 void scan_plugins(char *dirname);
 void add_plugin(char * filename);
 
+/* entry for session type based output plugin */
+OutputPlugin output_auto;
+
 static int d_iplist_compare(const void *a, const void *b)
 {
 	return strcmp(((char *) a), ((char *) b));
@@ -91,6 +95,8 @@
 	char *dir, *temp, *temp2;
 	GList *node, *disabled_iplugin_names = NULL;
 	int dirsel = 0;
+	char *session_plugin = "libALSA.so";
+	OutputPlugin *ossplugin;
 
 	if (cfg.disabled_iplugins)
 	{
@@ -147,6 +153,7 @@
 	}
 
 	op_data->output_list = g_list_sort(op_data->output_list, outputlist_compare_func);
+	op_data->output_list = g_list_prepend(op_data->output_list, &output_auto);
 	if (!op_data->current_output_plugin && g_list_length(op_data->output_list))
 		op_data->current_output_plugin = op_data->output_list->data;
 	ip_data->input_list = g_list_sort(ip_data->input_list, inputlist_compare_func);
@@ -175,7 +182,19 @@
 		cfg.enabled_eplugins = NULL;
 	}
 
+	if (getenv("KDE_FULL_SESSION"))
+	    session_plugin = "libarts.so";
+
+	if (getenv("GNOME_DESKTOP_SESSION_ID"))
+	    session_plugin = "libesdout.so";
+
 	node = op_data->output_list;
+	if (session_output_used)
+	{
+		op_data->current_output_plugin = node->data;
+	}
+
+	node = node->next;
 	while (node)
 	{
 		OutputPlugin *op = node->data;
@@ -184,14 +203,34 @@
 		 * prefix.  We will only see one plugin with the same
 		 * basename, so this is usually what the user want.
 		 */
-		if (!strcmp(g_basename(cfg.outputplugin),
+		if (!session_output_used)
+			if (!strcmp(g_basename(cfg.outputplugin),
+				    g_basename(op->filename)))
+				op_data->current_output_plugin = op;
+
+		if (!strcmp("libOSS.so",
+			    g_basename(op->filename)))
+			ossplugin = op;
+
+		if (!strcmp(session_plugin,
 			    g_basename(op->filename)))
-			op_data->current_output_plugin = op;
+		{
+			memcpy(&output_auto, op, sizeof(OutputPlugin));
+		}
+
 		if (op->init)
 			op->init();
 		node = node->next;
 	}
 
+	/* FIXME: If OSS plugin does not exists, xmms will segfault. */
+	if (!output_auto.description)
+	{
+		memcpy(&output_auto, ossplugin, sizeof(OutputPlugin));
+	}
+	output_auto.description = _("Automatic detection");
+	output_auto.filename = NULL;
+
 	node = ip_data->input_list;
 	while (node)
 	{
@@ -418,8 +457,11 @@
 	while (node)
 	{
 		OutputPlugin *op = node->data;
-		g_free(op->filename);
-		close_dynamic_lib(op->handle);
+		if (op->filename)
+		{
+			g_free(op->filename);
+			close_dynamic_lib(op->handle);
+		}
 		node = node->next;
 	}
 	g_list_free(op_data->output_list);
--- xmms/pluginenum.h
+++ xmms/pluginenum.h
@@ -18,6 +18,9 @@
 #ifndef PLUGINENUM_H
 #define PLUGINENUM_H
 
+extern int session_output_used;
+extern OutputPlugin output_auto;
+
 void init_plugins(void);
 void cleanup_plugins(void);
 
