Adding status icon
[gmpc-shout.git] / src / plugin.c
bloba7ba7c4863f13338fe674400a1b0bb9bd6b7d210
1 /* gmpc-shout (GMPC plugin)
2 * Copyright (C) 2007-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdio.h>
21 #include <string.h>
22 #include <config.h>
23 #include <glib.h>
24 #include <glib/gi18n-lib.h>
25 #include <gmpc/plugin.h>
26 #include <gmpc/playlist3-messages.h>
27 #ifndef __WIN32__
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #else
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
34 #endif
36 #define SHOUT_COMMAND "mplayer -ao pulse -nocache http://192.150.0.120:8000/mpd.ogg"
37 #include <config.h>
38 GPid ogg123_pid = -1;
41 static GtkWidget *si_shout = NULL;
42 static GtkWidget *shout_vbox = NULL;
43 static void start_ogg123(void);
44 static void stop_ogg123(void);
45 void shout_mpd_status_changed(MpdObj *mi, ChangedStatusType what, void *data);
46 void shout_construct(GtkWidget *container);
47 void shout_destroy(GtkWidget *container);
48 static int shout_get_enabled(void);
49 static void shout_set_enabled(int enabled);
51 gmpcPrefPlugin shout_gpp = {
52 shout_construct,
53 shout_destroy
56 int plugin_api_version = PLUGIN_API_VERSION;
58 static void shout_plugin_destroy(void);
60 static const char * shout_get_translation_domain(void)
62 return GETTEXT_PACKAGE;
64 static void shout_add_si_icon(void)
66 if(shout_get_enabled() && si_shout == NULL) {
67 GtkWidget *image = gtk_image_new_from_icon_name("media-artist", GTK_ICON_SIZE_MENU);
68 si_shout = gtk_event_box_new();
69 gtk_container_add(GTK_CONTAINER(si_shout), image);
70 main_window_add_status_icon(si_shout);
71 gtk_widget_show_all(si_shout);
72 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(si_shout)), FALSE);
75 static void shout_init(void)
77 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
78 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
80 gtk_init_add((GtkFunction)shout_add_si_icon, NULL);
83 /* main plugin_osd info */
84 gmpcPlugin plugin = {
85 .name = N_("Shout plugin"),
86 .version = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
87 .plugin_type = GMPC_PLUGIN_NO_GUI,
88 .init = shout_init,
89 .destroy = shout_plugin_destroy, /* Destroy */
90 .mpd_status_changed = &shout_mpd_status_changed,
91 .pref = &shout_gpp,
92 .get_enabled = shout_get_enabled,
93 .set_enabled = shout_set_enabled,
94 .get_translation_domain = shout_get_translation_domain
98 static int shout_get_enabled(void)
100 return cfg_get_single_value_as_int_with_default(config, "shout-plugin", "enable", FALSE);
102 static void shout_set_enabled(int enabled)
104 cfg_set_single_value_as_int(config, "shout-plugin", "enable", enabled);
105 if(enabled )
107 shout_add_si_icon();
108 if(mpd_player_get_state(connection) == MPD_STATUS_STATE_PLAY)
110 start_ogg123();
113 } else {
114 /* stop */
115 stop_ogg123();
117 gtk_widget_destroy(si_shout);
118 si_shout = NULL;
122 void shout_destroy(GtkWidget *container)
124 gtk_container_remove(GTK_CONTAINER(container), shout_vbox);
126 static void shout_entry_edited(GtkWidget *entry)
128 const char *str = gtk_entry_get_text(GTK_ENTRY(entry));
129 if(str)
131 cfg_set_single_value_as_string(config, "shout-plugin", "command",(char *)str);
139 void shout_construct(GtkWidget *container)
141 GtkWidget *entry = NULL, *label;
142 char *entry_str = cfg_get_single_value_as_string_with_default(config, "shout-plugin", "command", SHOUT_COMMAND);
143 shout_vbox = gtk_vbox_new(FALSE,6);
145 gtk_container_add(GTK_CONTAINER(container), shout_vbox);
147 entry = gtk_entry_new();
148 if(entry_str)
150 gtk_entry_set_text(GTK_ENTRY(entry), entry_str);
151 cfg_free_string(entry_str);
153 label = gtk_label_new(_("Playback Command:"));
154 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
155 gtk_box_pack_start(GTK_BOX(shout_vbox), label, FALSE, FALSE,0);
156 gtk_box_pack_start(GTK_BOX(shout_vbox), entry, FALSE, FALSE,0);
158 g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(shout_entry_edited), NULL);
160 gtk_widget_show_all(container);
162 static void shout_pid_callback (GPid pid,
163 gint status,
164 gpointer data)
166 g_spawn_close_pid(ogg123_pid);
167 printf("client died: %i\n", ogg123_pid);
168 ogg123_pid = -1;
171 if(si_shout) gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(si_shout)),FALSE);
173 static void start_ogg123(void)
175 if(ogg123_pid == -1)
177 gchar *uri = cfg_get_single_value_as_string_with_default(config, "shout-plugin", "command", SHOUT_COMMAND);
178 gchar **argv = g_strsplit(uri, " ", 0);
179 GError *error = NULL;
181 if(!g_spawn_async(NULL,argv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,NULL, NULL, &ogg123_pid, &error))
183 if(error)
185 gchar *message = g_strdup_printf("%s: %s", _("Shout plugin: Failed to spawn client. Error"), error->message);
186 playlist3_show_error_message(message, ERROR_WARNING);
187 g_free(message);
188 g_error_free(error);
189 error = NULL;
192 else
194 g_child_watch_add(ogg123_pid, shout_pid_callback, NULL);
196 if(si_shout) gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(si_shout)), TRUE);
198 printf("spawned pid: %i\n", ogg123_pid);
199 g_strfreev(argv);
200 g_free(uri);
204 static void stop_ogg123(void)
206 if(ogg123_pid >= 0)
208 #ifndef __WIN32__
209 printf("killing: %i\n", ogg123_pid);
210 kill (ogg123_pid, SIGHUP);
211 #else
212 TerminateProcess (ogg123_pid, 1);
213 #endif
215 if(si_shout) gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(si_shout)), FALSE);
217 /* ogg123_pid = -1;*/
220 /* mpd changed */
222 void shout_mpd_status_changed(MpdObj *mi, ChangedStatusType what, void *data)
224 if(!shout_get_enabled())
225 return;
226 if(what&(MPD_CST_SONGID|MPD_CST_STATE))
228 if(mpd_player_get_state(mi) != MPD_STATUS_STATE_PLAY)
230 stop_ogg123();
232 else
234 start_ogg123();
236 return;
239 static void shout_plugin_destroy(void)
241 stop_ogg123();