/[sido]/callbacks.c
ViewVC logotype

Contents of /callbacks.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Sun Oct 11 19:01:42 2009 UTC (11 years, 8 months ago) by ben
File MIME type: text/plain
File size: 7486 byte(s)
use persistent connection
1 /*
2 * callbacks.c Copyright © 2009 by Benoît Rouits <brouits@free.fr>
3 * Published under the terms of the GNU General Public License v2 (GPLv2).
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 * see the COPYING file included in the jackie package or
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt for the full licence
22 *
23 */
24
25 #include <gtk/gtk.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include "controller.h"
29 #include "menu.h"
30 #include "about.h"
31 #include "version.h"
32 #define _(string) gettext (string)
33
34 /* refresh the tooltip */
35 gboolean tooltip_display (gpointer app_data) {
36 AppData* d = (AppData*) app_data;
37 gchar* tooltip;
38
39 tooltip = mpd_get_song(d->sock);
40 gtk_status_icon_set_tooltip(d->status_icon, tooltip);
41 g_free(tooltip);
42 return TRUE;
43 }
44
45 /* handler for the volume button drag */
46 void volume_on_event (GtkWidget* instance, gdouble value, gpointer app_data) {
47 gchar* cmd = NULL;
48 AppData* d = (AppData*) app_data;
49
50 cmd = g_strdup_printf ("setvol %.0f", value*100);
51 mpd_control (d->sock, cmd);
52 g_free (cmd);
53 instance = NULL;
54 }
55
56 /* handler for left-button click */
57 void status_icon_on_left_click(GtkStatusIcon* instance, gpointer app_data) {
58 AppData* d = (AppData*) app_data;
59 GtkVolumeButton* volume;
60 GtkWidget* popup;
61 double curvol;
62
63 volume = GTK_VOLUME_BUTTON(gtk_volume_button_new());
64 curvol = mpd_get_vol(d->sock) / 100.0;
65 gtk_scale_button_set_value(GTK_SCALE_BUTTON(volume), curvol);
66 popup = gtk_scale_button_get_popup(GTK_SCALE_BUTTON(volume));
67 gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_MOUSE);
68 g_signal_connect(G_OBJECT(volume), "value-changed", G_CALLBACK(volume_on_event), app_data);
69 g_signal_connect_swapped(popup, "leave-notify-event", G_CALLBACK(gtk_widget_destroy), popup);
70 gtk_widget_show_all(popup);
71 instance = app_data = NULL;
72 }
73
74 /* handler for right-button click */
75 void status_icon_on_right_click(GtkStatusIcon* instance, guint button, guint activate_time, gpointer app_data) {
76 AppData* d = (AppData*) app_data;
77
78 if (app_data)
79 menu_show (GTK_MENU(d->right_menu), button, activate_time);
80 instance = NULL;
81 }
82
83 /* handler for the "Play" menu item */
84 void menu_item_on_play(GtkMenuItem* instance, gpointer app_data) {
85 AppData* d = (AppData*) app_data;
86
87 mpd_control(d->sock, "play");
88 instance = NULL;
89 }
90
91 /* handler for the "Pause" menu item */
92 void menu_item_on_pause(GtkMenuItem* instance, gpointer app_data) {
93 AppData* d = (AppData*) app_data;
94
95 mpd_control(d->sock, "pause");
96 instance = NULL;
97 }
98
99 void menu_item_on_next(GtkMenuItem* instance, gpointer app_data) {
100 AppData* d = (AppData*) app_data;
101
102 mpd_control(d->sock, "next");
103 instance = NULL;
104 }
105
106 void menu_item_on_prev(GtkMenuItem* instance, gpointer app_data) {
107 AppData* d = (AppData*) app_data;
108
109 mpd_control(d->sock, "previous");
110 instance = NULL;
111 }
112
113 void menu_item_on_stop(GtkMenuItem* instance, gpointer app_data) {
114 AppData* d = (AppData*) app_data;
115
116 mpd_control(d->sock, "stop");
117 instance = NULL;
118 }
119
120 void menu_item_on_shuffle(GtkMenuItem* instance, gpointer app_data) {
121 AppData* d = (AppData*) app_data;
122
123 mpd_control(d->sock, "shuffle");
124 instance = NULL;
125 }
126
127 /* handler for the "About" menu item (see version.h) */
128 void menu_item_on_about(GtkMenuItem* instance, gpointer unused) {
129 GtkAboutDialog* about;
130 const gchar* authors [] = {
131 PROG_AUTHOR0,
132 NULL
133 };
134
135 about = about_create (PROG_NAME, PROG_VERSION, PROG_COPYRIGHT,
136 PROG_COMMENT, PROG_LICENSE, PROG_WEBSITE,
137 authors);
138 about_show (about);
139 unused = NULL; /* avoid compiler warnings */
140 instance = NULL; /* _ */
141 return;
142 }
143
144 /* callback on Preferences window closed */
145 void pref_window_on_close (gpointer app_data) {
146 AppData* d = (AppData*) app_data;
147
148 g_free(d->ip);
149 d->ip = g_strdup (gtk_entry_get_text(d->ip_entry));
150
151 d->port = gtk_spin_button_get_value_as_int (d->port_spin);
152
153 /* write it to $HOME/.fala */
154 write_config (d);
155 gtk_widget_hide (GTK_WIDGET(d->pref_window));
156 gtk_widget_destroy (GTK_WIDGET(d->pref_window));
157 gtk_widget_set_sensitive (GTK_WIDGET(d->pref_item), TRUE);
158 d->pref_window = NULL;
159 mpd_disconnect (d->sock);
160 d->sock = -1;
161 d->sock = mpd_connect (d->ip, d->port);
162 }
163
164 /* handler for the "Preference" menu item */
165 void menu_item_on_pref(GtkMenuItem* instance, gpointer app_data) {
166 AppData* d = (AppData*) app_data;
167 GtkVBox* vbox;
168 GtkHBox* hbox1;
169 GtkHBox* hbox2;
170 GtkHBox* hbox3;
171 GtkLabel* ip_label;
172 GtkLabel* port_label;
173 GtkLabel* empty_label;
174 GtkButton* close_button;
175
176 d->pref_window = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
177 gtk_window_set_title (GTK_WINDOW(d->pref_window), _("Preferences"));
178 gtk_window_set_default_size (GTK_WINDOW(d->pref_window), 300, 200);
179 gtk_window_set_resizable (GTK_WINDOW(d->pref_window), TRUE);
180
181 vbox = GTK_VBOX(gtk_vbox_new (TRUE, 0));
182 gtk_container_add (GTK_CONTAINER(d->pref_window), GTK_WIDGET(vbox));
183
184 hbox1 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
185 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox1), TRUE, FALSE, 10);
186
187 hbox2 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
188 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox2), TRUE, FALSE, 10);
189
190 hbox3 = GTK_HBOX(gtk_hbox_new (TRUE, 0));
191 gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox3), TRUE, FALSE, 10);
192
193 ip_label = GTK_LABEL(gtk_label_new (_("MPD IP Address:")));
194 gtk_box_pack_start (GTK_BOX(hbox1), GTK_WIDGET(ip_label), TRUE, FALSE, 10);
195 port_label = GTK_LABEL(gtk_label_new (_("MPD TCP Port:")));
196 gtk_box_pack_start (GTK_BOX(hbox2), GTK_WIDGET(port_label), TRUE, FALSE, 10);
197
198 d->ip_entry = GTK_ENTRY(gtk_entry_new ());
199 gtk_entry_set_max_length (d->ip_entry, 15);
200 gtk_entry_set_width_chars (d->ip_entry, 15);
201 gtk_entry_set_text (d->ip_entry, d->ip); /* MPD ip address */
202 gtk_box_pack_start( GTK_BOX(hbox1), GTK_WIDGET(d->ip_entry), TRUE, FALSE, 10);
203 d->port_spin = GTK_SPIN_BUTTON(gtk_spin_button_new_with_range (1, 65535, 1));
204 gtk_spin_button_set_value (d->port_spin, (gdouble)d->port); /* MPD tcp port */
205 gtk_box_pack_start (GTK_BOX(hbox2), GTK_WIDGET(d->port_spin), TRUE, FALSE, 10);
206
207 empty_label = GTK_LABEL(gtk_label_new (""));
208 gtk_box_pack_start (GTK_BOX(hbox3), GTK_WIDGET(empty_label), TRUE, FALSE, 10);
209
210 close_button = GTK_BUTTON(gtk_button_new_from_stock (GTK_STOCK_CLOSE));
211 gtk_box_pack_start (GTK_BOX(hbox3), GTK_WIDGET(close_button), FALSE, FALSE, 10);
212
213 g_signal_connect_swapped (G_OBJECT(close_button), "clicked", G_CALLBACK(pref_window_on_close), (gpointer)d);
214 g_signal_connect_swapped (G_OBJECT(d->pref_window), "delete-event", G_CALLBACK(pref_window_on_close), (gpointer)d);
215
216 gtk_widget_show_all (GTK_WIDGET(d->pref_window));
217 gtk_widget_set_sensitive (GTK_WIDGET(instance), FALSE);
218 }
219
220 /* handler for the "Quit" menu item */
221 void menu_item_on_quit(GtkMenuItem* instance, gpointer app_data) {
222 AppData* d = (AppData*) app_data;
223 quit (d);
224 instance = NULL; /* useless but does not warn at compile time */
225 }
226

  ViewVC Help
Powered by ViewVC 1.1.26