Update copyrights everywhere.
[lxde/lxpanel.git] / src / plugins / volume / volume.c
CommitLineData
4542c20d 1/**
b840f7cc 2 * Copyright (c) 2006, 2008, 2014 LxDE Developers,
e5035ac1 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 {
a7bd16a4 45 LXPanel *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
a7bd16a4 157static gboolean on_button_press (GtkWidget* widget, GdkEventButton* evt, LXPanel* 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
e5035ac1
JH
196 case 2: { /* middle mouse button */
197 if ( ! vol->dlg ) {
198 vol->dlg = create_volume_window();
199 }
200
68f80334 201 if (! vol_spin) return FALSE;
e5035ac1
JH
202 GtkAdjustment *vol_adjustment =
203 gtk_spin_button_get_adjustment (vol_spin);
68f80334 204 if (! vol_adjustment) return FALSE;
e5035ac1
JH
205
206 curr_volume = gtk_adjustment_get_value (vol_adjustment);
207
208 if (curr_volume > 0) {
209 /* turning to mute */
210 vol_before_mute = curr_volume;
211 curr_volume = 0;
212 }
213 else {
214 curr_volume = vol_before_mute;
215 }
216
217 gtk_adjustment_set_value (vol_adjustment, curr_volume);
218 gtk_spin_button_set_adjustment(vol_spin, vol_adjustment);
219
6958db5d 220 update_icon(widget, vol);
e5035ac1
JH
221
222 gtk_widget_destroy( vol->dlg );
223 vol->dlg = NULL;
224 break;
225 }
226 default: /* never here */
227 break;
228 }
229 return FALSE;
31b80004
JH
230}
231
a7bd16a4 232static GtkWidget *volume_constructor(LXPanel *panel, config_setting_t *settings)
31b80004 233{
4542c20d 234 volume_t *vol;
6958db5d
AG
235 GtkWidget *p;
236 GtkAdjustment *vol_adjustment;
237
e5035ac1
JH
238 vol_before_mute = 1;
239 curr_volume = 0;
240 curr_image = NULL;
241 skip_botton1_event = FALSE;
4542c20d
HJYP
242
243 ENTER;
4542c20d
HJYP
244 /* check if OSS mixer device could be open */
245 mixer_fd = open ("/dev/mixer", O_RDWR, 0);
246 if (mixer_fd < 0) {
6958db5d
AG
247 RET(NULL);
248 }
249 /* try to obtain current volume */
250 p = create_volume_window(); /* use pointer */
251 if (! vol_spin)
252 goto _error;
253 vol_adjustment = gtk_spin_button_get_adjustment (vol_spin);
254 if (! vol_adjustment)
255 {
256_error:
257 gtk_widget_destroy(p);
258 RET(NULL);
4542c20d 259 }
e5035ac1 260 curr_volume = gtk_adjustment_get_value (vol_adjustment);
6958db5d
AG
261
262 vol = g_new0(volume_t, 1);
263 vol->dlg = p; /* it was reused */
264
265 p = gtk_event_box_new();
266 lxpanel_plugin_set_data(p, vol, volume_destructor);
267 vol->panel = panel;
268
269 gtk_widget_add_events(p, GDK_BUTTON_PRESS_MASK);
270 g_signal_connect(p, "scroll-event", G_CALLBACK(on_mouse_scroll), vol);
271 gtk_widget_set_size_request(p, panel_get_icon_size(panel), panel_get_icon_size(panel));
272
273 update_icon(p, vol);
274 gtk_widget_destroy( vol->dlg );
275 vol->dlg = NULL;
4542c20d 276
4542c20d 277 /* FIXME: display current level in tooltip. ex: "Volume Control: 80%" */
09fa171b 278 gtk_widget_set_tooltip_text(p, _("Volume control"));
31b80004 279
6958db5d 280 RET(p);
31b80004
JH
281}
282
6958db5d 283FM_DEFINE_MODULE(lxpanel_gtk, volume)
31b80004 284
6958db5d 285LXPanelPluginInit fm_module_init_lxpanel_gtk = {
3c3e9c9e 286 .name = N_("Volume Control"),
3c3e9c9e
HG
287 .description = "Display and control volume",
288
6958db5d
AG
289 .new_instance = volume_constructor,
290 .button_press_event = on_button_press
31b80004 291};