Merge 'volume' and 'volumealsa' plugins into single plugin.
[lxde/lxpanel.git] / plugins / volumealsa / volumealsa.c
1 /*
2 * Copyright (C) 2006-2008 Jim Huang <jserv.tw@gmail.com>
3 * 2006 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4 *
5 * Copyright (C) 2008 Fred Chien <fred@lxde.org>
6 * 2008 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
7 * 2009-2010 Marty Jack <martyj19@comcast.net>
8 * 2010-2012 Julien Lavergne <julien.lavergne@gmail.com>
9 * 2012 Henry Gebhardt <hsggebhardt@gmail.com>
10 * 2014 Peter <ombalaxitabou@users.sf.net>
11 * 2014-2016 Andriy Grytsenko <andrej@rep.kiev.ua>
12 *
13 * This file is a part of LXPanel project.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software Foundation,
27 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30 #define _ISOC99_SOURCE /* lrint() */
31 #define _GNU_SOURCE /* exp10() */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <glib.h>
42 #include <glib/gi18n.h>
43 #include <gdk-pixbuf/gdk-pixbuf.h>
44 #ifdef DISABLE_ALSA
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/ioctl.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #ifdef HAVE_SYS_SOUNDCARD_H
53 #include <sys/soundcard.h>
54 #elif defined(HAVE_LINUX_SOUNDCARD_H)
55 #include <linux/soundcard.h>
56 #else
57 #error "Not supported platform"
58 #endif
59 #else
60 #include <alsa/asoundlib.h>
61 #include <poll.h>
62 #endif
63 #include <math.h>
64 #include <libfm/fm-gtk.h>
65 #include "plugin.h"
66 #include "misc.h"
67
68 #define ICONS_VOLUME_HIGH "volume-high"
69 #define ICONS_VOLUME_MEDIUM "volume-medium"
70 #define ICONS_VOLUME_LOW "volume-low"
71 #define ICONS_MUTE "mute"
72
73 #ifdef __UCLIBC__
74 /* 10^x = 10^(log e^x) = (e^x)^log10 = e^(x * log 10) */
75 # define M_LN10 2.30258509299404568402 /* log_e 10 */
76 #define exp10(x) (exp((x) * log(10)))
77 #endif /* __UCLIBC__ */
78
79 #define MAX_LINEAR_DB_SCALE 24
80
81 #ifdef DISABLE_ALSA
82 typedef struct stereovolume
83 {
84 unsigned char left;
85 unsigned char right;
86 } StereoVolume;
87 #endif
88
89 typedef struct {
90
91 /* Graphics. */
92 GtkWidget * plugin; /* Back pointer to the widget */
93 LXPanel * panel; /* Back pointer to panel */
94 config_setting_t * settings; /* Plugin settings */
95 GtkWidget * tray_icon; /* Displayed image */
96 GtkWidget * popup_window; /* Top level window for popup */
97 GtkWidget * volume_scale; /* Scale for volume */
98 GtkWidget * mute_check; /* Checkbox for mute state */
99 gboolean show_popup; /* Toggle to show and hide the popup on left click */
100 guint volume_scale_handler; /* Handler for vscale widget */
101 guint mute_check_handler; /* Handler for mute_check widget */
102
103 #ifdef DISABLE_ALSA
104 int mixer_fd; /* The mixer FD */
105 struct
106 {
107 unsigned char left;
108 unsigned char right;
109 } vol;
110 gdouble vol_before_mute; /* Save value when muted */
111 #else
112 /* ALSA interface. */
113 snd_mixer_t * mixer; /* The mixer */
114 snd_mixer_selem_id_t * sid; /* The element ID */
115 snd_mixer_elem_t * master_element; /* The Master element */
116 guint mixer_evt_idle; /* Timer to handle restarting poll */
117 guint restart_idle;
118 gint alsamixer_mapping;
119
120 /* unloading and error handling */
121 GIOChannel **channels; /* Channels that we listen to */
122 guint *watches; /* Watcher IDs for channels */
123 guint num_channels; /* Number of channels */
124 #endif
125
126 /* Icons */
127 const char* icon_panel;
128 const char* icon_fallback;
129
130 } VolumeALSAPlugin;
131
132 #ifndef DISABLE_ALSA
133 static gboolean asound_restart(gpointer vol_gpointer);
134 #endif
135 static gboolean asound_initialize(VolumeALSAPlugin * vol);
136 static void asound_deinitialize(VolumeALSAPlugin * vol);
137 static void volumealsa_update_display(VolumeALSAPlugin * vol);
138 static void volumealsa_destructor(gpointer user_data);
139
140 /*** ALSA ***/
141
142 #ifndef DISABLE_ALSA
143 static gboolean asound_find_element(VolumeALSAPlugin * vol, const char * ename)
144 {
145 for (
146 vol->master_element = snd_mixer_first_elem(vol->mixer);
147 vol->master_element != NULL;
148 vol->master_element = snd_mixer_elem_next(vol->master_element))
149 {
150 snd_mixer_selem_get_id(vol->master_element, vol->sid);
151 if ((snd_mixer_selem_is_active(vol->master_element))
152 && (strcmp(ename, snd_mixer_selem_id_get_name(vol->sid)) == 0))
153 return TRUE;
154 }
155 return FALSE;
156 }
157
158 /* NOTE by PCMan:
159 * This is magic! Since ALSA uses its own machanism to handle this part.
160 * After polling of mixer fds, it requires that we should call
161 * snd_mixer_handle_events to clear all pending mixer events.
162 * However, when using the glib IO channels approach, we don't have
163 * poll() and snd_mixer_poll_descriptors_revents(). Due to the design of
164 * glib, on_mixer_event() will be called for every fd whose status was
165 * changed. So, after each poll(), it's called for several times,
166 * not just once. Therefore, we cannot call snd_mixer_handle_events()
167 * directly in the event handler. Otherwise, it will get called for
168 * several times, which might clear unprocessed pending events in the queue.
169 * So, here we call it once in the event callback for the first fd.
170 * Then, we don't call it for the following fds. After all fds with changed
171 * status are handled, we remove this restriction in an idle handler.
172 * The next time the event callback is involked for the first fs, we can
173 * call snd_mixer_handle_events() again. Racing shouldn't happen here
174 * because the idle handler has the same priority as the io channel callback.
175 * So, io callbacks for future pending events should be in the next gmain
176 * iteration, and won't be affected.
177 */
178
179 static gboolean asound_reset_mixer_evt_idle(VolumeALSAPlugin * vol)
180 {
181 if (!g_source_is_destroyed(g_main_current_source()))
182 vol->mixer_evt_idle = 0;
183 return FALSE;
184 }
185
186 /* Handler for I/O event on ALSA channel. */
187 static gboolean asound_mixer_event(GIOChannel * channel, GIOCondition cond, gpointer vol_gpointer)
188 {
189 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) vol_gpointer;
190 int res = 0;
191
192 if (g_source_is_destroyed(g_main_current_source()))
193 return FALSE;
194
195 if (vol->mixer_evt_idle == 0)
196 {
197 vol->mixer_evt_idle = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) asound_reset_mixer_evt_idle, vol, NULL);
198 res = snd_mixer_handle_events(vol->mixer);
199 }
200
201 if (cond & G_IO_IN)
202 {
203 /* the status of mixer is changed. update of display is needed. */
204 volumealsa_update_display(vol);
205 }
206
207 if ((cond & G_IO_HUP) || (res < 0))
208 {
209 /* This means there're some problems with alsa. */
210 g_warning("volumealsa: ALSA (or pulseaudio) had a problem: "
211 "volumealsa: snd_mixer_handle_events() = %d,"
212 " cond 0x%x (IN: 0x%x, HUP: 0x%x).", res, cond,
213 G_IO_IN, G_IO_HUP);
214 gtk_widget_set_tooltip_text(vol->plugin, "ALSA (or pulseaudio) had a problem."
215 " Please check the lxpanel logs.");
216
217 if (vol->restart_idle == 0)
218 vol->restart_idle = g_timeout_add_seconds(1, asound_restart, vol);
219
220 return FALSE;
221 }
222
223 return TRUE;
224 }
225
226 static gboolean asound_restart(gpointer vol_gpointer)
227 {
228 VolumeALSAPlugin * vol = vol_gpointer;
229
230 if (g_source_is_destroyed(g_main_current_source()))
231 return FALSE;
232
233 asound_deinitialize(vol);
234
235 if (!asound_initialize(vol)) {
236 g_warning("volumealsa: Re-initialization failed.");
237 return TRUE; // try again in a second
238 }
239
240 g_warning("volumealsa: Restarted ALSA interface...");
241
242 vol->restart_idle = 0;
243 return FALSE;
244 }
245 #endif
246
247 /* Initialize the ALSA interface. */
248 static gboolean asound_initialize(VolumeALSAPlugin * vol)
249 {
250 /* Access the "default" device. */
251 #ifdef DISABLE_ALSA
252 vol->mixer_fd = open ("/dev/mixer", O_RDWR, 0);
253 if (vol->mixer_fd < 0)
254 {
255 g_warning("cannot initialize OSS mixer: %s", strerror(errno));
256 return FALSE;
257 }
258
259 //FIXME: is there a way to watch volume with OSS?
260 #else
261 snd_mixer_selem_id_alloca(&vol->sid);
262 snd_mixer_open(&vol->mixer, 0);
263 snd_mixer_attach(vol->mixer, "default");
264 snd_mixer_selem_register(vol->mixer, NULL, NULL);
265 snd_mixer_load(vol->mixer);
266
267 /* Find Master element, or Front element, or PCM element, or LineOut element.
268 * If one of these succeeds, master_element is valid. */
269 if ( ! asound_find_element(vol, "Master"))
270 if ( ! asound_find_element(vol, "Front"))
271 if ( ! asound_find_element(vol, "PCM"))
272 if ( ! asound_find_element(vol, "LineOut"))
273 return FALSE;
274
275 /* Set the playback volume range as we wish it. */
276 if ( ! vol->alsamixer_mapping)
277 snd_mixer_selem_set_playback_volume_range(vol->master_element, 0, 100);
278
279 /* Listen to events from ALSA. */
280 int n_fds = snd_mixer_poll_descriptors_count(vol->mixer);
281 struct pollfd * fds = g_new0(struct pollfd, n_fds);
282
283 vol->channels = g_new0(GIOChannel *, n_fds);
284 vol->watches = g_new0(guint, n_fds);
285 vol->num_channels = n_fds;
286
287 snd_mixer_poll_descriptors(vol->mixer, fds, n_fds);
288 int i;
289 for (i = 0; i < n_fds; ++i)
290 {
291 GIOChannel* channel = g_io_channel_unix_new(fds[i].fd);
292 vol->watches[i] = g_io_add_watch(channel, G_IO_IN | G_IO_HUP, asound_mixer_event, vol);
293 vol->channels[i] = channel;
294 }
295 g_free(fds);
296 #endif
297 return TRUE;
298 }
299
300 static void asound_deinitialize(VolumeALSAPlugin * vol)
301 {
302 #ifdef DISABLE_ALSA
303 if (vol->mixer_fd >= 0)
304 close(vol->mixer_fd);
305 vol->mixer_fd = -1;
306 #else
307 guint i;
308
309 if (vol->mixer_evt_idle != 0) {
310 g_source_remove(vol->mixer_evt_idle);
311 vol->mixer_evt_idle = 0;
312 }
313
314 for (i = 0; i < vol->num_channels; i++) {
315 g_source_remove(vol->watches[i]);
316 g_io_channel_shutdown(vol->channels[i], FALSE, NULL);
317 g_io_channel_unref(vol->channels[i]);
318 }
319 g_free(vol->channels);
320 g_free(vol->watches);
321 vol->channels = NULL;
322 vol->watches = NULL;
323 vol->num_channels = 0;
324
325 snd_mixer_close(vol->mixer);
326 vol->master_element = NULL;
327 /* FIXME: unalloc vol->sid */
328 #endif
329 }
330
331 /* Get the presence of the mute control from the sound system. */
332 static gboolean asound_has_mute(VolumeALSAPlugin * vol)
333 {
334 #ifdef DISABLE_ALSA
335 /* it's emulated with OSS */
336 return TRUE;
337 #else
338 return ((vol->master_element != NULL) ? snd_mixer_selem_has_playback_switch(vol->master_element) : FALSE);
339 #endif
340 }
341
342 /* Get the condition of the mute control from the sound system. */
343 static gboolean asound_is_muted(VolumeALSAPlugin * vol)
344 {
345 /* The switch is on if sound is not muted, and off if the sound is muted.
346 * Initialize so that the sound appears unmuted if the control does not exist. */
347 int value = 1;
348 #ifdef DISABLE_ALSA
349 StereoVolume levels;
350
351 ioctl(vol->mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &levels);
352 value = (levels.left + levels.right) >> 1;
353 #else
354 if (vol->master_element != NULL)
355 snd_mixer_selem_get_playback_switch(vol->master_element, 0, &value);
356 #endif
357 return (value == 0);
358 }
359
360 #ifndef DISABLE_ALSA
361 static long lrint_dir(double x, int dir)
362 {
363 if (dir > 0)
364 return lrint(ceil(x));
365 else if (dir < 0)
366 return lrint(floor(x));
367 else
368 return lrint(x);
369 }
370
371 static inline gboolean use_linear_dB_scale(long dBmin, long dBmax)
372 {
373 return dBmax - dBmin <= MAX_LINEAR_DB_SCALE * 100;
374 }
375
376 static long get_normalized_volume(snd_mixer_elem_t *elem,
377 snd_mixer_selem_channel_id_t channel)
378 {
379 long min, max, value;
380 double normalized, min_norm;
381 int err;
382
383 err = snd_mixer_selem_get_playback_dB_range(elem, &min, &max);
384 if (err < 0 || min >= max) {
385 err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
386 if (err < 0 || min == max)
387 return 0;
388
389 err = snd_mixer_selem_get_playback_volume(elem, channel, &value);
390 if (err < 0)
391 return 0;
392
393 return lrint(100.0 * (value - min) / (double)(max - min));
394 }
395
396 err = snd_mixer_selem_get_playback_dB(elem, channel, &value);
397 if (err < 0)
398 return 0;
399
400 if (use_linear_dB_scale(min, max))
401 return lrint(100.0 * (value - min) / (double)(max - min));
402
403 normalized = exp10((value - max) / 6000.0);
404 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
405 min_norm = exp10((min - max) / 6000.0);
406 normalized = (normalized - min_norm) / (1 - min_norm);
407 }
408
409 return lrint(100.0 * normalized);
410 }
411 #endif
412
413 /* Get the volume from the sound system.
414 * This implementation returns the average of the Front Left and Front Right channels. */
415 static int asound_get_volume(VolumeALSAPlugin * vol)
416 {
417 #ifdef DISABLE_ALSA
418 StereoVolume levels;
419
420 ioctl(vol->mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &levels);
421 return (levels.left + levels.right) >> 1;
422 #else
423 long aleft = 0;
424 long aright = 0;
425
426 if (vol->master_element != NULL)
427 {
428 if ( ! vol->alsamixer_mapping)
429 {
430 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, &aleft);
431 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, &aright);
432 }
433 else
434 {
435 aleft = get_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT);
436 aright = get_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT);
437 }
438 }
439 return (aleft + aright) >> 1;
440 #endif
441 }
442
443 #ifndef DISABLE_ALSA
444 static int set_normalized_volume(snd_mixer_elem_t *elem,
445 snd_mixer_selem_channel_id_t channel,
446 int vol,
447 int dir)
448 {
449 long min, max, value;
450 double min_norm, volume;
451 int err;
452
453 volume = vol / 100.0;
454
455 err = snd_mixer_selem_get_playback_dB_range(elem, &min, &max);
456 if (err < 0 || min >= max) {
457 err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
458 if (err < 0)
459 return err;
460
461 value = lrint_dir(volume * (max - min), dir) + min;
462 return snd_mixer_selem_set_playback_volume(elem, channel, value);
463 }
464
465 if (use_linear_dB_scale(min, max)) {
466 value = lrint_dir(volume * (max - min), dir) + min;
467 return snd_mixer_selem_set_playback_dB(elem, channel, value, dir);
468 }
469
470 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
471 min_norm = exp10((min - max) / 6000.0);
472 volume = volume * (1 - min_norm) + min_norm;
473 }
474 value = lrint_dir(6000.0 * log10(volume), dir) + max;
475
476 return snd_mixer_selem_set_playback_dB(elem, channel, value, dir);
477 }
478 #endif
479
480 /* Set the volume to the sound system.
481 * This implementation sets the Front Left and Front Right channels to the specified value. */
482 static void asound_set_volume(VolumeALSAPlugin * vol, int volume)
483 {
484 int dir = volume - asound_get_volume(vol);
485
486 /* Volume is set to the correct value already */
487 if (dir == 0)
488 return;
489
490 #ifdef DISABLE_ALSA
491 StereoVolume levels;
492
493 levels.left = levels.right = volume;
494 ioctl(vol->mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &levels);
495 #else
496 if (vol->master_element != NULL)
497 {
498 if ( ! vol->alsamixer_mapping)
499 {
500 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, volume);
501 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, volume);
502 }
503 else
504 {
505 set_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, volume, dir);
506 set_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, volume, dir);
507 }
508 }
509 #endif
510 }
511
512 /*** Graphics ***/
513
514 static void volumealsa_lookup_current_icon(VolumeALSAPlugin * vol, gboolean mute, int level)
515 {
516 /* Change icon according to mute / volume */
517 const char* icon_panel="audio-volume-muted-panel";
518 const char* icon_fallback=ICONS_MUTE;
519 if (mute)
520 {
521 icon_panel = "audio-volume-muted-panel";
522 icon_fallback=ICONS_MUTE;
523 }
524 else if (level >= 66)
525 {
526 icon_panel = "audio-volume-high-panel";
527 icon_fallback=ICONS_VOLUME_HIGH;
528 }
529 else if (level >= 33)
530 {
531 icon_panel = "audio-volume-medium-panel";
532 icon_fallback=ICONS_VOLUME_MEDIUM;
533 }
534 else if (level > 0)
535 {
536 icon_panel = "audio-volume-low-panel";
537 icon_fallback=ICONS_VOLUME_LOW;
538 }
539
540 vol->icon_panel = icon_panel;
541 vol->icon_fallback = icon_fallback;
542 }
543
544 static void volumealsa_update_current_icon(VolumeALSAPlugin * vol, gboolean mute, int level)
545 {
546 /* Find suitable icon */
547 volumealsa_lookup_current_icon(vol, mute, level);
548
549 /* Change icon, fallback to default icon if theme doesn't exsit */
550 lxpanel_image_change_icon(vol->tray_icon, vol->icon_panel, vol->icon_fallback);
551
552 /* Display current level in tooltip. */
553 char * tooltip = g_strdup_printf("%s %d", _("Volume control"), level);
554 gtk_widget_set_tooltip_text(vol->plugin, tooltip);
555 g_free(tooltip);
556 }
557
558 /*
559 * Here we just update volume's vertical scale and mute check button.
560 * The rest will be updated by signal handelrs.
561 */
562 static void volumealsa_update_display(VolumeALSAPlugin * vol)
563 {
564 /* Mute. */
565 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), asound_is_muted(vol));
566 gtk_widget_set_sensitive(vol->mute_check, (asound_has_mute(vol)));
567
568 /* Volume. */
569 if (vol->volume_scale != NULL)
570 {
571 gtk_range_set_value(GTK_RANGE(vol->volume_scale), asound_get_volume(vol));
572 }
573 }
574
575 /* Handler for "button-press-event" signal on main widget. */
576 static gboolean volumealsa_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * panel)
577 {
578 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(widget);
579
580 /* Left-click. Show or hide the popup window. */
581 if (event->button == 1)
582 {
583 if (vol->show_popup)
584 {
585 gtk_widget_hide(vol->popup_window);
586 vol->show_popup = FALSE;
587 }
588 else
589 {
590 gtk_widget_show_all(vol->popup_window);
591 vol->show_popup = TRUE;
592 }
593 return TRUE;
594 }
595
596 return FALSE;
597 }
598
599 static gboolean volumealsa_button_release_event(GtkWidget * widget, GdkEventButton * event, VolumeALSAPlugin * vol)
600 {
601 /* Middle-click. Toggle the mute status. */
602 if (event->button == 2)
603 {
604 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check)));
605 }
606 return FALSE;
607 }
608
609 /* Handler for "focus-out" signal on popup window. */
610 static gboolean volumealsa_popup_focus_out(GtkWidget * widget, GdkEvent * event, VolumeALSAPlugin * vol)
611 {
612 /* Hide the widget. */
613 gtk_widget_hide(vol->popup_window);
614 vol->show_popup = FALSE;
615 return FALSE;
616 }
617
618 /* Handler for "map" signal on popup window. */
619 static void volumealsa_popup_map(GtkWidget * widget, VolumeALSAPlugin * vol)
620 {
621 lxpanel_plugin_adjust_popup_position(widget, vol->plugin);
622 }
623
624 /* Handler for "value_changed" signal on popup window vertical scale. */
625 static void volumealsa_popup_scale_changed(GtkRange * range, VolumeALSAPlugin * vol)
626 {
627 int level = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
628 gboolean mute = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
629
630 /* Reflect the value of the control to the sound system. */
631 asound_set_volume(vol, level);
632
633 /*
634 * Redraw the controls.
635 * Scale and check button do not need to be updated, as these are always
636 * in sync with user's actions.
637 */
638 volumealsa_update_current_icon(vol, mute, level);
639 }
640
641 /* Handler for "scroll-event" signal on popup window vertical scale. */
642 static void volumealsa_popup_scale_scrolled(GtkScale * scale, GdkEventScroll * evt, VolumeALSAPlugin * vol)
643 {
644 /* Get the state of the vertical scale. */
645 gdouble val = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
646
647 /* Dispatch on scroll direction to update the value. */
648 if ((evt->direction == GDK_SCROLL_UP) || (evt->direction == GDK_SCROLL_LEFT))
649 val += 2;
650 else
651 val -= 2;
652
653 /* Reset the state of the vertical scale. This provokes a "value_changed" event. */
654 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP((int)val, 0, 100));
655 }
656
657 /* Handler for "toggled" signal on popup window mute checkbox. */
658 static void volumealsa_popup_mute_toggled(GtkWidget * widget, VolumeALSAPlugin * vol)
659 {
660 int level = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
661 gboolean mute = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
662
663 /* Reflect the mute toggle to the sound system. */
664 #ifdef DISABLE_ALSA
665 if (mute)
666 {
667 vol->vol_before_mute = level;
668 asound_set_volume(vol, 0);
669 }
670 else
671 {
672 asound_set_volume(vol, vol->vol_before_mute);
673 }
674 #else
675 if (vol->master_element != NULL)
676 {
677 int chn;
678 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++)
679 snd_mixer_selem_set_playback_switch(vol->master_element, chn, ((mute) ? 0 : 1));
680 }
681 #endif
682
683 /*
684 * Redraw the controls.
685 * Scale and check button do not need to be updated, as these are always
686 * in sync with user's actions.
687 */
688 volumealsa_update_current_icon(vol, mute, level);
689 }
690
691 /* Build the window that appears when the top level widget is clicked. */
692 static void volumealsa_build_popup_window(GtkWidget *p)
693 {
694 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
695
696 /* Create a new window. */
697 vol->popup_window = gtk_window_new(GTK_WINDOW_POPUP);
698 gtk_window_set_decorated(GTK_WINDOW(vol->popup_window), FALSE);
699 gtk_container_set_border_width(GTK_CONTAINER(vol->popup_window), 5);
700 gtk_window_set_default_size(GTK_WINDOW(vol->popup_window), 80, 140);
701 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(vol->popup_window), TRUE);
702 gtk_window_set_skip_pager_hint(GTK_WINDOW(vol->popup_window), TRUE);
703 gtk_window_set_type_hint(GTK_WINDOW(vol->popup_window), GDK_WINDOW_TYPE_HINT_UTILITY);
704
705 /* Connect signals. */
706 g_signal_connect(G_OBJECT(vol->popup_window), "focus-out-event", G_CALLBACK(volumealsa_popup_focus_out), vol);
707 g_signal_connect(G_OBJECT(vol->popup_window), "map", G_CALLBACK(volumealsa_popup_map), vol);
708
709 /* Create a scrolled window as the child of the top level window. */
710 GtkWidget * scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
711 gtk_container_set_border_width (GTK_CONTAINER(scrolledwindow), 0);
712 gtk_widget_show(scrolledwindow);
713 gtk_container_add(GTK_CONTAINER(vol->popup_window), scrolledwindow);
714 gtk_widget_set_can_focus(scrolledwindow, FALSE);
715 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
716 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_NONE);
717
718 /* Create a viewport as the child of the scrolled window. */
719 GtkWidget * viewport = gtk_viewport_new(NULL, NULL);
720 gtk_container_add(GTK_CONTAINER(scrolledwindow), viewport);
721 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
722 gtk_widget_show(viewport);
723
724 /* Create a frame as the child of the viewport. */
725 GtkWidget * frame = gtk_frame_new(_("Volume"));
726 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
727 gtk_container_add(GTK_CONTAINER(viewport), frame);
728
729 /* Create a vertical box as the child of the frame. */
730 GtkWidget * box = gtk_vbox_new(FALSE, 0);
731 gtk_container_add(GTK_CONTAINER(frame), box);
732
733 /* Create a vertical scale as the child of the vertical box. */
734 vol->volume_scale = gtk_vscale_new(GTK_ADJUSTMENT(gtk_adjustment_new(100, 0, 100, 0, 0, 0)));
735 gtk_scale_set_draw_value(GTK_SCALE(vol->volume_scale), FALSE);
736 gtk_range_set_inverted(GTK_RANGE(vol->volume_scale), TRUE);
737 gtk_box_pack_start(GTK_BOX(box), vol->volume_scale, TRUE, TRUE, 0);
738
739 /* Value-changed and scroll-event signals. */
740 vol->volume_scale_handler = g_signal_connect(vol->volume_scale, "value-changed", G_CALLBACK(volumealsa_popup_scale_changed), vol);
741 g_signal_connect(vol->volume_scale, "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol);
742
743 /* Create a check button as the child of the vertical box. */
744 vol->mute_check = gtk_check_button_new_with_label(_("Mute"));
745 gtk_box_pack_end(GTK_BOX(box), vol->mute_check, FALSE, FALSE, 0);
746 vol->mute_check_handler = g_signal_connect(vol->mute_check, "toggled", G_CALLBACK(volumealsa_popup_mute_toggled), vol);
747
748 /* Set background to default. */
749 gtk_widget_set_style(viewport, panel_get_defstyle(vol->panel));
750 }
751
752 /* Plugin constructor. */
753 static GtkWidget *volumealsa_constructor(LXPanel *panel, config_setting_t *settings)
754 {
755 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
756 VolumeALSAPlugin * vol = g_new0(VolumeALSAPlugin, 1);
757 GtkWidget *p;
758
759 #ifndef DISABLE_ALSA
760 /* Read config necessary for proper initialization of ALSA. */
761 config_setting_lookup_int(settings, "UseAlsamixerVolumeMapping", &vol->alsamixer_mapping);
762 #endif
763
764 /* Initialize ALSA. If that fails, present nothing. */
765 if ( ! asound_initialize(vol))
766 {
767 volumealsa_destructor(vol);
768 return NULL;
769 }
770
771 /* Allocate top level widget and set into Plugin widget pointer. */
772 vol->panel = panel;
773 vol->plugin = p = gtk_event_box_new();
774 vol->settings = settings;
775 lxpanel_plugin_set_data(p, vol, volumealsa_destructor);
776 gtk_widget_set_tooltip_text(p, _("Volume control"));
777
778 /* Allocate icon as a child of top level. */
779 vol->tray_icon = lxpanel_image_new_for_icon(panel, "audio-volume-muted-panel",
780 -1, ICONS_MUTE);
781 gtk_container_add(GTK_CONTAINER(p), vol->tray_icon);
782
783 /* Initialize window to appear when icon clicked. */
784 volumealsa_build_popup_window(p);
785
786 /* Connect signals. */
787 g_signal_connect(G_OBJECT(p), "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol );
788 g_signal_connect(G_OBJECT(p), "button-release-event", G_CALLBACK(volumealsa_button_release_event), vol );
789
790 /* Update the display, show the widget, and return. */
791 volumealsa_update_display(vol);
792 volumealsa_update_current_icon(vol, asound_is_muted(vol), asound_get_volume(vol));
793 gtk_widget_show_all(p);
794 return p;
795 }
796
797 /* Plugin destructor. */
798 static void volumealsa_destructor(gpointer user_data)
799 {
800 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) user_data;
801
802 asound_deinitialize(vol);
803
804 /* If the dialog box is open, dismiss it. */
805 if (vol->popup_window != NULL)
806 gtk_widget_destroy(vol->popup_window);
807
808 #ifndef DISABLE_ALSA
809 if (vol->restart_idle)
810 g_source_remove(vol->restart_idle);
811 #endif
812
813 /* Deallocate all memory. */
814 g_free(vol);
815 }
816
817 /* Callback when the configuration dialog is to be shown. */
818
819 static GtkWidget *volumealsa_configure(LXPanel *panel, GtkWidget *p)
820 {
821 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
822 char *path = NULL;
823 const gchar *command_line = NULL;
824 GAppInfoCreateFlags flags = G_APP_INFO_CREATE_NONE;
825
826 /* FIXME: configure settings! */
827 /* check if command line was configured */
828 config_setting_lookup_string(vol->settings, "MixerCommand", &command_line);
829 /* FIXME: support "needs terminal" for MixerCommand */
830 /* FIXME: selection for master channel! */
831 /* FIXME: selection for the device */
832 /* FIXME: configure buttons for each action (toggle volume/mixer/mute)! */
833 /* FIXME: allow bind multimedia keys to volume using libkeybinder */
834
835 /* if command isn't set in settings then let guess it */
836 if (command_line == NULL && (path = g_find_program_in_path("pulseaudio")))
837 {
838 g_free(path);
839 /* Assume that when pulseaudio is installed, it's launching every time */
840 if ((path = g_find_program_in_path("gnome-sound-applet")))
841 {
842 command_line = "gnome-sound-applet";
843 }
844 else if ((path = g_find_program_in_path("pavucontrol")))
845 {
846 command_line = "pavucontrol";
847 }
848 }
849
850 /* Fallback to alsamixer when PA is not running, or when no PA utility is find */
851 if (command_line == NULL)
852 {
853 if ((path = g_find_program_in_path("gnome-alsamixer")))
854 {
855 command_line = "gnome-alsamixer";
856 }
857 else if ((path = g_find_program_in_path("alsamixergui")))
858 {
859 command_line = "alsamixergui";
860 }
861 else if ((path = g_find_program_in_path("alsamixer")))
862 {
863 command_line = "alsamixer";
864 flags = G_APP_INFO_CREATE_NEEDS_TERMINAL;
865 }
866 }
867 g_free(path);
868
869 if (command_line)
870 {
871 fm_launch_command_simple(NULL, NULL, flags, command_line, NULL);
872 }
873 else
874 {
875 fm_show_error(NULL, NULL,
876 _("Error, you need to install an application to configure"
877 " the sound (pavucontrol, alsamixer ...)"));
878 }
879
880 return NULL;
881 }
882
883 /* Callback when panel configuration changes. */
884 static void volumealsa_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
885 {
886 /* Do a full redraw. */
887 volumealsa_update_display(lxpanel_plugin_get_data(p));
888 }
889
890 #ifndef DISABLE_ALSA
891 static LXPanelPluginInit _volumealsa_init = {
892 .name = N_("Volume Control"),
893 .description = N_("Display and control volume"),
894
895 .superseded = TRUE,
896 .new_instance = volumealsa_constructor,
897 .config = volumealsa_configure,
898 .reconfigure = volumealsa_panel_configuration_changed,
899 .button_press_event = volumealsa_button_press_event
900 };
901
902 static void volumealsa_init(void)
903 {
904 lxpanel_register_plugin_type("volumealsa", &_volumealsa_init);
905 }
906 #endif
907
908 FM_DEFINE_MODULE(lxpanel_gtk, volume)
909
910 /* Plugin descriptor. */
911 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
912 .name = N_("Volume Control"),
913 .description = N_("Display and control volume"),
914 #ifndef DISABLE_ALSA
915 .init = volumealsa_init,
916 #endif
917
918 .new_instance = volumealsa_constructor,
919 .config = volumealsa_configure,
920 .reconfigure = volumealsa_panel_configuration_changed,
921 .button_press_event = volumealsa_button_press_event
922 };
923
924 /* vim: set sw=4 et sts=4 : */