Use <USER_CONFIG_DIR>/lxpanel/<profile>/gtkrc for panel custom style setup.
[lxde/lxpanel.git] / src / plugins / volume / volume.c
CommitLineData
4542c20d 1/**
e5035ac1
JH
2 * Copyright (c) 2006, 2008 LxDE Developers,
3 * see the file AUTHORS for details.
a99ee9e1
JH
4 *
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.
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 Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
31b80004
JH
20#include <gtk/gtk.h>
21#include <stdlib.h>
22#include <fcntl.h>
23#include <unistd.h>
24
f277dbb7 25#include <glib/gi18n.h>
31b80004
JH
26#include <gdk-pixbuf/gdk-pixbuf.h>
27
6958db5d 28#include "plugin.h"
31b80004
JH
29
30#include "dbg.h"
31
32#include "volume-impl.h"
33
34#include "volume_xpm.h"
35
36int mixer_fd;
37
e5035ac1
JH
38GtkSpinButton *vol_spin;
39static gdouble vol_before_mute;
40static gdouble curr_volume;
41static GtkWidget *curr_image;
42static gboolean skip_botton1_event;
43
31b80004 44typedef struct {
6958db5d 45 Panel *panel;
4542c20d 46 GtkWidget *dlg;
31b80004
JH
47} volume_t;
48
49static void
6958db5d 50volume_destructor(gpointer user_data)
31b80004 51{
6958db5d 52 volume_t *vol = (volume_t *) user_data;
4542c20d
HJYP
53
54 ENTER;
55 if (vol->dlg)
56 gtk_widget_destroy(vol->dlg);
4542c20d
HJYP
57 if (mixer_fd)
58 close(mixer_fd);
59 g_free(vol);
60 RET();
31b80004
JH
61}
62
6958db5d 63static void update_icon (GtkWidget *p, volume_t *vol)
31b80004 64{
e5035ac1
JH
65 GdkPixbuf *icon;
66 GtkWidget *image;
67 GtkIconTheme* theme;
68 GtkIconInfo* info;
6958db5d
AG
69 int icon_size;
70
71 theme = panel_get_icon_theme(vol->panel);
72 icon_size = panel_get_icon_size(vol->panel);
73
e5035ac1 74 if (curr_volume <= 0) {
6958db5d 75 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-mute", icon_size, 0 );
e5035ac1
JH
76 }
77 else if (curr_volume > 0 && curr_volume <= 50) {
6958db5d 78 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-min", icon_size, 0 );
e5035ac1
JH
79 }
80 else if (curr_volume > 50 && curr_volume <= 75) {
6958db5d 81 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-med", icon_size, 0 );
e5035ac1 82 }
6958db5d
AG
83 else /* curr_volume > 75 */ {
84 info = gtk_icon_theme_lookup_icon( theme, "stock_volume-max", icon_size, 0 );
e5035ac1
JH
85 }
86
87 if (info ) {
88 icon = gdk_pixbuf_new_from_file_at_size(
89 gtk_icon_info_get_filename( info ),
6958db5d 90 icon_size, icon_size, NULL );
e5035ac1
JH
91 gtk_icon_info_free( info );
92 }
93 else {
94 icon = gdk_pixbuf_new_from_xpm_data((const char **) volume_xpm);
95 }
0bcf9045 96
e5035ac1 97 if (icon) {
0bcf9045 98 if (curr_image) {
5b397638 99 gtk_widget_destroy(curr_image);
e5035ac1
JH
100 curr_image = NULL;
101 }
102 image = gtk_image_new_from_pixbuf(icon);
6958db5d 103 gtk_container_add(GTK_CONTAINER(p), image);
0bcf9045 104
e5035ac1
JH
105 curr_image = image;
106 }
6958db5d 107 gtk_widget_show_all(p);
e5035ac1 108 return;
31b80004
JH
109}
110
6958db5d 111static void on_volume_focus (GtkWidget* dlg, GdkEventFocus *event, GtkWidget *p)
31b80004 112{
6958db5d
AG
113 volume_t *vol = lxpanel_plugin_get_data(p);
114
e5035ac1
JH
115 if (! vol_spin) return;
116 GtkAdjustment *vol_adjustment = gtk_spin_button_get_adjustment (vol_spin);
117 if (! vol_adjustment) return;
118 curr_volume = gtk_adjustment_get_value (vol_adjustment);
0bcf9045 119
6958db5d 120 update_icon(p, vol);
0bcf9045 121
e5035ac1
JH
122 /* FIXME: use smarter method */
123 gtk_widget_destroy( dlg );
124 vol->dlg = NULL;
125}
31b80004 126
6958db5d 127static void on_mouse_scroll (GtkWidget* widget, GdkEventScroll* evt, volume_t *vol)
e5035ac1 128{
e5035ac1 129 if ( ! vol->dlg ) {
cecb0eaa 130
e5035ac1 131 vol->dlg = create_volume_window();
6958db5d
AG
132 g_signal_connect( vol->dlg, "delete-event",
133 G_CALLBACK(on_volume_focus), widget );
cecb0eaa 134
e5035ac1
JH
135 }
136 else {
137 if (! vol_spin) return;
0bcf9045 138 GtkAdjustment *vol_adjustment =
e5035ac1
JH
139 gtk_spin_button_get_adjustment (vol_spin);
140 if (! vol_adjustment) return;
141
142 curr_volume = gtk_adjustment_get_value (vol_adjustment);
143
144 if (evt->direction == GDK_SCROLL_UP) {
145 curr_volume += 2;
146 }
147 else /*if (evt->direction == GDK_SCROLL_DOWN)*/ {
148 curr_volume -= 2;
149 }
6958db5d 150 update_icon(widget, vol);
e5035ac1
JH
151 gtk_adjustment_set_value (vol_adjustment, curr_volume);
152 gtk_spin_button_set_adjustment(vol_spin, vol_adjustment);
153 skip_botton1_event = TRUE;
154 }
155}
156
6958db5d 157static gboolean on_button_press (GtkWidget* widget, GdkEventButton* evt, Panel* p)
e5035ac1 158{
6958db5d 159 volume_t *vol = lxpanel_plugin_get_data(widget);
e5035ac1
JH
160
161 /* for scroll correction */
162 if (skip_botton1_event) {
163 gtk_widget_destroy(vol->dlg);
164 vol->dlg = NULL;
165 skip_botton1_event = FALSE;
166 }
167
0bcf9045
AG
168 switch ( evt->button ) {
169 case 1: { /* Left click */
e5035ac1
JH
170 if ( ! vol->dlg ) {
171 vol->dlg = create_volume_window();
172
173 /* setting background to default */
6958db5d 174 gtk_widget_set_style(vol->dlg, panel_get_defstyle(p));
e5035ac1
JH
175
176 g_signal_connect( vol->dlg, "focus-out-event",
6958db5d 177 G_CALLBACK(on_volume_focus), widget );
e5035ac1
JH
178
179 gtk_window_present( GTK_WINDOW(vol->dlg) );
180 }
181 else {
182 /* update icon */
68f80334 183 if (! vol_spin) return FALSE;
e5035ac1
JH
184 GtkAdjustment *vol_adjustment =
185 gtk_spin_button_get_adjustment (vol_spin);
68f80334 186 if (! vol_adjustment) return FALSE;
e5035ac1 187 curr_volume = gtk_adjustment_get_value (vol_adjustment);
6958db5d 188 update_icon(widget, vol);
e5035ac1
JH
189
190 gtk_widget_destroy(vol->dlg);
191 vol->dlg = NULL;
192 }
193 break;
194 }
195
196 case 3: { /* right button */
6958db5d 197 lxpanel_plugin_button_press_event(widget, evt, p);
e5035ac1
JH
198 return TRUE;
199 }
200
201 case 2: { /* middle mouse button */
202 if ( ! vol->dlg ) {
203 vol->dlg = create_volume_window();
204 }
205
68f80334 206 if (! vol_spin) return FALSE;
e5035ac1
JH
207 GtkAdjustment *vol_adjustment =
208 gtk_spin_button_get_adjustment (vol_spin);
68f80334 209 if (! vol_adjustment) return FALSE;
e5035ac1
JH
210
211 curr_volume = gtk_adjustment_get_value (vol_adjustment);
212
213 if (curr_volume > 0) {
214 /* turning to mute */
215 vol_before_mute = curr_volume;
216 curr_volume = 0;
217 }
218 else {
219 curr_volume = vol_before_mute;
220 }
221
222 gtk_adjustment_set_value (vol_adjustment, curr_volume);
223 gtk_spin_button_set_adjustment(vol_spin, vol_adjustment);
224
6958db5d 225 update_icon(widget, vol);
e5035ac1
JH
226
227 gtk_widget_destroy( vol->dlg );
228 vol->dlg = NULL;
229 break;
230 }
231 default: /* never here */
232 break;
233 }
234 return FALSE;
31b80004
JH
235}
236
6958db5d 237static GtkWidget *volume_constructor(Panel *panel, config_setting_t *settings)
31b80004 238{
4542c20d 239 volume_t *vol;
6958db5d
AG
240 GtkWidget *p;
241 GtkAdjustment *vol_adjustment;
242
e5035ac1
JH
243 vol_before_mute = 1;
244 curr_volume = 0;
245 curr_image = NULL;
246 skip_botton1_event = FALSE;
4542c20d
HJYP
247
248 ENTER;
4542c20d
HJYP
249 /* check if OSS mixer device could be open */
250 mixer_fd = open ("/dev/mixer", O_RDWR, 0);
251 if (mixer_fd < 0) {
6958db5d
AG
252 RET(NULL);
253 }
254 /* try to obtain current volume */
255 p = create_volume_window(); /* use pointer */
256 if (! vol_spin)
257 goto _error;
258 vol_adjustment = gtk_spin_button_get_adjustment (vol_spin);
259 if (! vol_adjustment)
260 {
261_error:
262 gtk_widget_destroy(p);
263 RET(NULL);
4542c20d 264 }
e5035ac1 265 curr_volume = gtk_adjustment_get_value (vol_adjustment);
6958db5d
AG
266
267 vol = g_new0(volume_t, 1);
268 vol->dlg = p; /* it was reused */
269
270 p = gtk_event_box_new();
271 lxpanel_plugin_set_data(p, vol, volume_destructor);
272 vol->panel = panel;
273
274 gtk_widget_add_events(p, GDK_BUTTON_PRESS_MASK);
275 g_signal_connect(p, "scroll-event", G_CALLBACK(on_mouse_scroll), vol);
276 gtk_widget_set_size_request(p, panel_get_icon_size(panel), panel_get_icon_size(panel));
277
278 update_icon(p, vol);
279 gtk_widget_destroy( vol->dlg );
280 vol->dlg = NULL;
4542c20d 281
4542c20d 282 /* FIXME: display current level in tooltip. ex: "Volume Control: 80%" */
09fa171b 283 gtk_widget_set_tooltip_text(p, _("Volume control"));
31b80004 284
6958db5d 285 RET(p);
31b80004
JH
286}
287
6958db5d 288FM_DEFINE_MODULE(lxpanel_gtk, volume)
31b80004 289
6958db5d 290LXPanelPluginInit fm_module_init_lxpanel_gtk = {
3c3e9c9e 291 .name = N_("Volume Control"),
3c3e9c9e
HG
292 .description = "Display and control volume",
293
6958db5d
AG
294 .new_instance = volume_constructor,
295 .button_press_event = on_button_press
31b80004 296};