Adapt to gmpc-vala-build change (fix missing header)
[gmpc-fullscreeninfo.git] / src / plugin.c
blob5b18f9f5d107e6118c324020faa77a6abdeb7830
1 /* gmpc-fullscreen_info (GMPC plugin)
2 * Copyright (C) 2006-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-metaimage.h>
27 #include <gmpc-extras.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <config.h>
31 typedef struct _FullscreenInfoPlugin
33 GmpcPluginBase parent_instance;
34 } FullscreenInfoPlugin ;
36 typedef struct _FullscreenInfoPluginClass
38 GmpcPluginBaseClass parent_class;
39 } FullscreenInfoPluginClass;
41 extern GmpcConnection *gmpcconn;
43 gulong signal_handler = 0;
45 GtkWidget *buttons[4] = {NULL, };
46 GtkWidget *label = NULL;
47 /**
48 * Get/Set enabled functions.
50 static gboolean fullscreen_info_get_enabled(GmpcPluginBase *plug)
52 return cfg_get_single_value_as_int_with_default(config, "fullscreen_info-plugin", "enable", TRUE);
54 static void fullscreen_info_set_enabled(GmpcPluginBase *plug, int enabled)
56 cfg_set_single_value_as_int(config, "fullscreen_info-plugin", "enable", enabled);
58 pl3_tool_menu_update();
62 static void gfi_button_press_event(GtkWidget *widevent, GdkEventButton *event, GtkWidget *window)
64 gtk_widget_destroy(window);
65 if(signal_handler)
66 g_signal_handler_disconnect(G_OBJECT(gmpcconn), signal_handler);
67 signal_handler = 0;
69 static gboolean gfi_window_fullscreen(GtkWidget *window)
71 gtk_window_fullscreen(GTK_WINDOW(window));
72 return FALSE;
74 static void gfi_size_allocate(GtkWidget *window, GtkAllocation *alloc, GtkWidget *image)
76 g_idle_add((GSourceFunc)gfi_window_fullscreen, window);
79 static void gfi_size_allocate_mi(GtkWidget *window, GtkAllocation *alloc, GtkWidget *image){
81 gmpc_metaimage_set_size(GMPC_METAIMAGE(image), (alloc->width < alloc->height)?alloc->width:alloc->height);
82 gmpc_metaimage_reload_image(GMPC_METAIMAGE(image));
84 static void play_pause(MpdObj *mi)
86 if(mpd_player_get_state(mi) == MPD_PLAYER_PLAY){
87 mpd_player_pause(mi);
88 }else{
89 mpd_player_play(mi);
92 static void status_changed(GmpcConnection *gmpcconn, MpdObj *mi, ChangedStatusType what, gpointer data)
94 if(what&MPD_CST_STATE){
95 GtkWidget *child = gtk_bin_get_child(GTK_BIN(buttons[2]));
97 if(mpd_player_get_state(connection) == MPD_PLAYER_PLAY){
98 gtk_image_set_from_stock(GTK_IMAGE(child),GTK_STOCK_MEDIA_PAUSE,GTK_ICON_SIZE_DIALOG);
99 }else{
100 gtk_image_set_from_stock(GTK_IMAGE(child),GTK_STOCK_MEDIA_PLAY,GTK_ICON_SIZE_DIALOG);
103 if(what&(MPD_CST_SONGID|MPD_CST_SONGPOS|MPD_CST_PLAYLIST|MPD_CST_STATE))
105 char buffer[512];
106 mpd_Song *song = mpd_playlist_get_current_song(mi);
107 if(song && mpd_player_get_state(mi) != MPD_PLAYER_STOP)
109 mpd_song_markup_escaped(buffer, 512, C_("header title markup","<span size='xx-large'>[[%title% - &[%artist%]]|%shortfile%]</span>"),song);
111 else{
112 snprintf(buffer, 512, "<span size='xx-large'>%s</span>", _("Gnome Music Player Client"));
114 gtk_label_set_markup(GTK_LABEL(label), buffer);
118 static void gfi_fullscreen(GmpcPluginBase *plu)
120 if(fullscreen_info_get_enabled(plu) == FALSE) return;
121 gchar *temp;
122 GtkWidget *popup = gtk_window_new(GTK_WINDOW_TOPLEVEL);
123 GtkWidget *metaimage = gmpc_metaimage_new(META_ALBUM_ART);
124 GtkWidget *vbox = gtk_vbox_new(FALSE, 6);
125 GtkWidget *hbox = gtk_hbox_new(TRUE, 6);
126 gtk_widget_set_has_tooltip(metaimage, FALSE);
128 gtk_window_set_keep_above(GTK_WINDOW(popup), TRUE);
129 /* Setup the meta image */
130 gmpc_metaimage_set_no_cover_icon(GMPC_METAIMAGE(metaimage),(char *)"gmpc");
131 gmpc_metaimage_set_squared(GMPC_METAIMAGE(metaimage), TRUE);
132 gmpc_metaimage_set_connection(GMPC_METAIMAGE(metaimage), connection);
133 gmpc_metaimage_update_cover(GMPC_METAIMAGE(metaimage), connection,MPD_CST_SONGID, NULL);
134 g_signal_connect(G_OBJECT(metaimage), "button-press-event", G_CALLBACK(gfi_button_press_event), popup);
137 gtk_container_add(GTK_CONTAINER(popup), vbox);
138 gtk_window_set_resizable(GTK_WINDOW(popup), TRUE);
140 g_signal_connect(G_OBJECT(popup), "size-allocate", G_CALLBACK(gfi_size_allocate), metaimage);
142 g_signal_connect(G_OBJECT(metaimage), "size-allocate", G_CALLBACK(gfi_size_allocate_mi), metaimage);
146 * Main title label
148 label = gtk_label_new("");
149 temp = g_markup_printf_escaped("<span size='xx-large'>%s</span>", _("Gnome Music Player Client"));
150 gtk_label_set_markup(GTK_LABEL(label), temp);
151 g_free(temp);
152 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
153 gtk_widget_ensure_style(label);
154 gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &(label->style->white));
156 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE,0);
158 gtk_box_pack_start(GTK_BOX(vbox), metaimage, TRUE, TRUE, 0);
160 * Control buttons */
161 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE,0);
165 /* Prev */
166 buttons[0] = gtk_button_new();
167 gtk_container_add(GTK_CONTAINER(buttons[0]),
168 gtk_image_new_from_stock(GTK_STOCK_MEDIA_PREVIOUS, GTK_ICON_SIZE_DIALOG));
169 gtk_button_set_relief(GTK_BUTTON(buttons[0]), GTK_RELIEF_NONE);
170 gtk_box_pack_start(GTK_BOX(hbox), buttons[0], FALSE, TRUE, 0);
171 g_signal_connect_swapped(G_OBJECT(buttons[0]), "clicked", G_CALLBACK(mpd_player_prev), connection);
172 /* STOP */
173 buttons[1] = gtk_button_new();
174 gtk_container_add(GTK_CONTAINER(buttons[1]),
175 gtk_image_new_from_stock(GTK_STOCK_MEDIA_STOP, GTK_ICON_SIZE_DIALOG));
176 gtk_button_set_relief(GTK_BUTTON(buttons[1]), GTK_RELIEF_NONE);
177 gtk_box_pack_start(GTK_BOX(hbox), buttons[1], FALSE, TRUE, 0);
179 g_signal_connect_swapped(G_OBJECT(buttons[1]), "clicked", G_CALLBACK(mpd_player_stop), connection);
181 /* Play/Pause */
182 buttons[2] = gtk_button_new();
183 gtk_container_add(GTK_CONTAINER(buttons[2]),
184 gtk_image_new_from_stock(GTK_STOCK_MEDIA_PLAY, GTK_ICON_SIZE_DIALOG));
185 gtk_button_set_relief(GTK_BUTTON(buttons[2]), GTK_RELIEF_NONE);
186 gtk_box_pack_start(GTK_BOX(hbox), buttons[2], FALSE, TRUE, 0);
188 g_signal_connect_swapped(G_OBJECT(buttons[2]), "clicked", G_CALLBACK(play_pause), connection);
190 /* Next */
191 buttons[3] = gtk_button_new();
192 gtk_container_add(GTK_CONTAINER(buttons[3]),
193 gtk_image_new_from_stock(GTK_STOCK_MEDIA_NEXT, GTK_ICON_SIZE_DIALOG));
194 gtk_button_set_relief(GTK_BUTTON(buttons[3]), GTK_RELIEF_NONE);
195 gtk_box_pack_start(GTK_BOX(hbox), buttons[3], FALSE, TRUE, 0);
197 g_signal_connect_swapped(G_OBJECT(buttons[3]), "clicked", G_CALLBACK(mpd_player_next), connection);
199 gtk_widget_ensure_style(popup);
200 gtk_widget_modify_bg(popup, GTK_STATE_NORMAL, &(popup->style->black));
203 status_changed(gmpcconn, connection, MPD_CST_STATE|MPD_CST_SONGPOS, NULL);
204 signal_handler = g_signal_connect(G_OBJECT(gmpcconn), "status-changed", G_CALLBACK(status_changed), NULL);
205 gtk_widget_show_all(popup);
208 static int gfi_add_tool_menu(GmpcPluginToolMenuIface *plug, GtkMenu *menu)
210 GtkWidget *item = NULL;
211 if(fullscreen_info_get_enabled(GMPC_PLUGIN_BASE(plug)) == FALSE) return 0;
213 item = gtk_image_menu_item_new_with_label(_("Fullscreen Info"));
214 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
215 gtk_image_new_from_icon_name("playlist-browser", GTK_ICON_SIZE_MENU));
216 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
217 gtk_widget_add_accelerator(GTK_WIDGET(item), "activate", gtk_menu_get_accel_group(GTK_MENU(menu)), GDK_F11, 0, GTK_ACCEL_VISIBLE);
218 g_signal_connect_swapped(G_OBJECT(item), "activate",
219 G_CALLBACK(gfi_fullscreen), plug);
220 return 1;
224 * Gobject plugin
226 static void fullscreen_info_plugin_class_init (FullscreenInfoPluginClass *klass);
227 GType fullscreen_info_plugin_get_type(void);
229 static int *fullscreen_info_plugin_get_version(GmpcPluginBase *plug, int *length)
231 static int version[3] = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION};
232 if(length) *length = 3;
233 return (int *)version;
236 static const char *fullscreen_info_plugin_get_name(GmpcPluginBase *plug)
238 return N_("Fullscreen Info");
240 static GObject *fullscreen_info_plugin_constructor(GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
241 FullscreenInfoPluginClass * klass;
242 FullscreenInfoPlugin *self;
243 GObjectClass * parent_class;
244 klass = (g_type_class_peek (fullscreen_info_plugin_get_type()));
245 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
246 self = (FullscreenInfoPlugin *) parent_class->constructor (type, n_construct_properties, construct_properties);
248 /* Setup textdomain */
249 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
250 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
252 /* Make it an internal plugin */
253 GMPC_PLUGIN_BASE(self)->translation_domain = GETTEXT_PACKAGE;
254 GMPC_PLUGIN_BASE(self)->plugin_type = GMPC_PLUGIN_NO_GUI;
256 return G_OBJECT(self);
260 static void fullscreen_info_plugin_class_init (FullscreenInfoPluginClass *klass)
262 G_OBJECT_CLASS(klass)->constructor = fullscreen_info_plugin_constructor;
263 /* Connect plugin functions */
264 GMPC_PLUGIN_BASE_CLASS(klass)->get_version = fullscreen_info_plugin_get_version;
265 GMPC_PLUGIN_BASE_CLASS(klass)->get_name = fullscreen_info_plugin_get_name;
267 GMPC_PLUGIN_BASE_CLASS(klass)->get_enabled = fullscreen_info_get_enabled;
268 GMPC_PLUGIN_BASE_CLASS(klass)->set_enabled = fullscreen_info_set_enabled;
271 static void fullscreen_info_plugin_gmpc_plugin_tool_menu_iface_interface_init (GmpcPluginToolMenuIfaceIface * iface) {
272 iface->tool_menu_integration = gfi_add_tool_menu;
275 GType fullscreen_info_plugin_get_type(void) {
276 static GType fullscreen_info_plugin_type_id = 0;
277 if(fullscreen_info_plugin_type_id == 0) {
278 static const GTypeInfo info = {
279 .class_size = sizeof(FullscreenInfoPluginClass),
280 .class_init = (GClassInitFunc)fullscreen_info_plugin_class_init,
281 .instance_size = sizeof(FullscreenInfoPlugin),
282 .n_preallocs = 0
285 static const GInterfaceInfo iface_info = { (GInterfaceInitFunc) fullscreen_info_plugin_gmpc_plugin_tool_menu_iface_interface_init,
286 (GInterfaceFinalizeFunc) NULL, NULL};
288 fullscreen_info_plugin_type_id = g_type_register_static(GMPC_PLUGIN_TYPE_BASE, "FullscreenInfosPlugin", &info, 0);
290 g_type_add_interface_static (fullscreen_info_plugin_type_id, GMPC_PLUGIN_TYPE_TOOL_MENU_IFACE, &iface_info);
292 return fullscreen_info_plugin_type_id;
295 G_MODULE_EXPORT GType plugin_get_type(void)
297 return fullscreen_info_plugin_get_type();