Configuration dialog to the 'volume' 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 * 2008 Frank ENDRES <frank_endres@yahoo.fr>
5 *
6 * Copyright (C) 2008 Fred Chien <fred@lxde.org>
7 * 2008 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
8 * 2009-2010 Marty Jack <martyj19@comcast.net>
9 * 2010-2012 Julien Lavergne <julien.lavergne@gmail.com>
10 * 2012 Henry Gebhardt <hsggebhardt@gmail.com>
11 * 2014 Peter <ombalaxitabou@users.sf.net>
12 * 2014-2016 Andriy Grytsenko <andrej@rep.kiev.ua>
13 *
14 * This file is a part of LXPanel project.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 */
30
31 #define _ISOC99_SOURCE /* lrint() */
32 #define _GNU_SOURCE /* exp10() */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <gtk/gtk.h>
39 #include <gdk/gdkkeysyms.h>
40 #include <stdlib.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <glib.h>
44 #include <glib/gi18n.h>
45 #include <gdk-pixbuf/gdk-pixbuf.h>
46 #ifdef DISABLE_ALSA
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/ioctl.h>
50 #include <string.h>
51 #include <stdio.h>
52 #include <fcntl.h>
53 #include <errno.h>
54 #ifdef HAVE_SYS_SOUNDCARD_H
55 #include <sys/soundcard.h>
56 #elif defined(HAVE_LINUX_SOUNDCARD_H)
57 #include <linux/soundcard.h>
58 #else
59 #error "Not supported platform"
60 #endif
61 #ifndef SOUND_MIXER_PHONEOUT
62 # define SOUND_MIXER_PHONEOUT SOUND_MIXER_MONO
63 #endif
64 //TODO: support OSSv4
65 #else
66 #include <alsa/asoundlib.h>
67 #include <poll.h>
68 #endif
69 #include <math.h>
70 #include <libfm/fm-gtk.h>
71
72 #include "plugin.h"
73 #include "misc.h"
74 #include "gtk-compat.h"
75
76 #define ICONS_VOLUME_HIGH "volume-high"
77 #define ICONS_VOLUME_MEDIUM "volume-medium"
78 #define ICONS_VOLUME_LOW "volume-low"
79 #define ICONS_MUTE "mute"
80
81 #ifdef __UCLIBC__
82 /* 10^x = 10^(log e^x) = (e^x)^log10 = e^(x * log 10) */
83 # define M_LN10 2.30258509299404568402 /* log_e 10 */
84 #define exp10(x) (exp((x) * log(10)))
85 #endif /* __UCLIBC__ */
86
87 #define MAX_LINEAR_DB_SCALE 24
88
89 #ifdef DISABLE_ALSA
90 typedef union
91 {
92 struct
93 {
94 unsigned char left;
95 unsigned char right;
96 };
97 int value;
98 } StereoVolume;
99 #endif
100
101 typedef struct {
102
103 /* Graphics. */
104 GtkWidget * plugin; /* Back pointer to the widget */
105 LXPanel * panel; /* Back pointer to panel */
106 config_setting_t * settings; /* Plugin settings */
107 GtkWidget * tray_icon; /* Displayed image */
108 GtkWidget * popup_window; /* Top level window for popup */
109 GtkWidget * volume_scale; /* Scale for volume */
110 GtkWidget * mute_check; /* Checkbox for mute state */
111 gboolean show_popup; /* Toggle to show and hide the popup on left click */
112 guint volume_scale_handler; /* Handler for vscale widget */
113 guint mute_check_handler; /* Handler for mute_check widget */
114
115 #ifdef DISABLE_ALSA
116 int mixer_fd; /* The mixer FD */
117 gdouble vol_before_mute; /* Save value when muted */
118
119 guint master_channel;
120 #else
121 /* ALSA interface. */
122 snd_mixer_t * mixer; /* The mixer */
123 snd_mixer_elem_t * master_element; /* The Master element */
124 guint mixer_evt_idle; /* Timer to handle restarting poll */
125 guint restart_idle;
126 gint alsamixer_mapping;
127
128 /* unloading and error handling */
129 GIOChannel **channels; /* Channels that we listen to */
130 guint *watches; /* Watcher IDs for channels */
131 guint num_channels; /* Number of channels */
132
133 gint used_device;
134 char *master_channel;
135 #endif
136
137 /* Icons */
138 const char* icon_panel;
139 const char* icon_fallback;
140
141 /* Clicks */
142 int mute_click;
143 GdkModifierType mute_click_mods;
144 int mixer_click;
145 GdkModifierType mixer_click_mods;
146 int slider_click;
147 GdkModifierType slider_click_mods;
148
149 /* Hotkeys */
150 char * hotkey_up;
151 char * hotkey_down;
152 char * hotkey_mute;
153
154 GtkWidget *channel_selector; /* Used by configure dialog */
155 } VolumeALSAPlugin;
156
157 #ifndef DISABLE_ALSA
158 static gboolean asound_restart(gpointer vol_gpointer);
159 #endif
160 static gboolean asound_initialize(VolumeALSAPlugin * vol);
161 static void asound_deinitialize(VolumeALSAPlugin * vol);
162 static void volumealsa_update_display(VolumeALSAPlugin * vol);
163 static void volumealsa_destructor(gpointer user_data);
164
165 /*** ALSA ***/
166
167 #ifndef DISABLE_ALSA
168 static gboolean asound_find_element(VolumeALSAPlugin * vol, const char ** ename, int n)
169 {
170 int i;
171 snd_mixer_selem_id_t * sid; /* The element ID */
172
173 snd_mixer_selem_id_alloca(&sid);
174 for (i = 0; i < n; i++)
175 {
176 for (vol->master_element = snd_mixer_first_elem(vol->mixer);
177 vol->master_element != NULL;
178 vol->master_element = snd_mixer_elem_next(vol->master_element))
179 {
180 snd_mixer_selem_get_id(vol->master_element, sid);
181 if (snd_mixer_selem_is_active(vol->master_element) &&
182 strcmp(ename[i], snd_mixer_selem_id_get_name(sid)) == 0)
183 return TRUE;
184 }
185 }
186 return FALSE;
187 }
188
189 /* NOTE by PCMan:
190 * This is magic! Since ALSA uses its own machanism to handle this part.
191 * After polling of mixer fds, it requires that we should call
192 * snd_mixer_handle_events to clear all pending mixer events.
193 * However, when using the glib IO channels approach, we don't have
194 * poll() and snd_mixer_poll_descriptors_revents(). Due to the design of
195 * glib, on_mixer_event() will be called for every fd whose status was
196 * changed. So, after each poll(), it's called for several times,
197 * not just once. Therefore, we cannot call snd_mixer_handle_events()
198 * directly in the event handler. Otherwise, it will get called for
199 * several times, which might clear unprocessed pending events in the queue.
200 * So, here we call it once in the event callback for the first fd.
201 * Then, we don't call it for the following fds. After all fds with changed
202 * status are handled, we remove this restriction in an idle handler.
203 * The next time the event callback is involked for the first fs, we can
204 * call snd_mixer_handle_events() again. Racing shouldn't happen here
205 * because the idle handler has the same priority as the io channel callback.
206 * So, io callbacks for future pending events should be in the next gmain
207 * iteration, and won't be affected.
208 */
209
210 static gboolean asound_reset_mixer_evt_idle(VolumeALSAPlugin * vol)
211 {
212 if (!g_source_is_destroyed(g_main_current_source()))
213 vol->mixer_evt_idle = 0;
214 return FALSE;
215 }
216
217 /* Handler for I/O event on ALSA channel. */
218 static gboolean asound_mixer_event(GIOChannel * channel, GIOCondition cond, gpointer vol_gpointer)
219 {
220 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) vol_gpointer;
221 int res = 0;
222
223 if (g_source_is_destroyed(g_main_current_source()))
224 return FALSE;
225
226 if (vol->mixer_evt_idle == 0)
227 {
228 vol->mixer_evt_idle = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) asound_reset_mixer_evt_idle, vol, NULL);
229 res = snd_mixer_handle_events(vol->mixer);
230 }
231
232 if (cond & G_IO_IN)
233 {
234 /* the status of mixer is changed. update of display is needed. */
235 volumealsa_update_display(vol);
236 }
237
238 if ((cond & G_IO_HUP) || (res < 0))
239 {
240 /* This means there're some problems with alsa. */
241 g_warning("volumealsa: ALSA (or pulseaudio) had a problem: "
242 "volumealsa: snd_mixer_handle_events() = %d,"
243 " cond 0x%x (IN: 0x%x, HUP: 0x%x).", res, cond,
244 G_IO_IN, G_IO_HUP);
245 gtk_widget_set_tooltip_text(vol->plugin, _("ALSA (or pulseaudio) had a problem."
246 " Please check the lxpanel logs."));
247
248 if (vol->restart_idle == 0)
249 vol->restart_idle = g_timeout_add_seconds(1, asound_restart, vol);
250
251 return FALSE;
252 }
253
254 return TRUE;
255 }
256
257 static gboolean asound_restart(gpointer vol_gpointer)
258 {
259 VolumeALSAPlugin * vol = vol_gpointer;
260
261 if (g_source_is_destroyed(g_main_current_source()))
262 return FALSE;
263
264 asound_deinitialize(vol);
265
266 if (!asound_initialize(vol)) {
267 g_warning("volumealsa: Re-initialization failed.");
268 return TRUE; // try again in a second
269 }
270
271 g_warning("volumealsa: Restarted ALSA interface...");
272
273 vol->restart_idle = 0;
274 return FALSE;
275 }
276 #endif
277
278 /* Initialize the ALSA interface. */
279 static gboolean asound_initialize(VolumeALSAPlugin * vol)
280 {
281 #ifdef DISABLE_ALSA
282 /* Access the "default" device. */
283 vol->mixer_fd = open ("/dev/mixer", O_RDWR, 0);
284 if (vol->mixer_fd < 0)
285 {
286 g_warning("cannot initialize OSS mixer: %s", strerror(errno));
287 return FALSE;
288 }
289
290 //FIXME: is there a way to watch volume with OSS?
291 #else
292 snd_mixer_open(&vol->mixer, 0);
293 if (vol->used_device < 0)
294 snd_mixer_attach(vol->mixer, "default");
295 else
296 {
297 char id[16];
298
299 snprintf(id, sizeof(id), "hw:%d", vol->used_device);
300 snd_mixer_attach(vol->mixer, id);
301 }
302 snd_mixer_selem_register(vol->mixer, NULL, NULL);
303 snd_mixer_load(vol->mixer);
304
305 if (vol->master_channel)
306 {
307 /* If user defined the channel then use it */
308 if (!asound_find_element(vol, (const char **)&vol->master_channel, 1))
309 return FALSE;
310 }
311 else
312 {
313 const char * def_channels[] = { "Master", "Front", "PCM", "LineOut" };
314 /* Find Master element, or Front element, or PCM element, or LineOut element.
315 * If one of these succeeds, master_element is valid. */
316 if (!asound_find_element(vol, def_channels, G_N_ELEMENTS(def_channels)))
317 return FALSE;
318 }
319
320 /* Set the playback volume range as we wish it. */
321 if ( ! vol->alsamixer_mapping)
322 snd_mixer_selem_set_playback_volume_range(vol->master_element, 0, 100);
323
324 /* Listen to events from ALSA. */
325 int n_fds = snd_mixer_poll_descriptors_count(vol->mixer);
326 struct pollfd * fds = g_new0(struct pollfd, n_fds);
327
328 vol->channels = g_new0(GIOChannel *, n_fds);
329 vol->watches = g_new0(guint, n_fds);
330 vol->num_channels = n_fds;
331
332 snd_mixer_poll_descriptors(vol->mixer, fds, n_fds);
333 int i;
334 for (i = 0; i < n_fds; ++i)
335 {
336 GIOChannel* channel = g_io_channel_unix_new(fds[i].fd);
337 vol->watches[i] = g_io_add_watch(channel, G_IO_IN | G_IO_HUP, asound_mixer_event, vol);
338 vol->channels[i] = channel;
339 }
340 g_free(fds);
341 #endif
342 return TRUE;
343 }
344
345 static void asound_deinitialize(VolumeALSAPlugin * vol)
346 {
347 #ifdef DISABLE_ALSA
348 if (vol->mixer_fd >= 0)
349 close(vol->mixer_fd);
350 vol->mixer_fd = -1;
351 #else
352 guint i;
353
354 if (vol->mixer_evt_idle != 0) {
355 g_source_remove(vol->mixer_evt_idle);
356 vol->mixer_evt_idle = 0;
357 }
358
359 for (i = 0; i < vol->num_channels; i++) {
360 g_source_remove(vol->watches[i]);
361 g_io_channel_shutdown(vol->channels[i], FALSE, NULL);
362 g_io_channel_unref(vol->channels[i]);
363 }
364 g_free(vol->channels);
365 g_free(vol->watches);
366 vol->channels = NULL;
367 vol->watches = NULL;
368 vol->num_channels = 0;
369
370 snd_mixer_close(vol->mixer);
371 vol->master_element = NULL;
372 #endif
373 }
374
375 /* Get the presence of the mute control from the sound system. */
376 static gboolean asound_has_mute(VolumeALSAPlugin * vol)
377 {
378 #ifdef DISABLE_ALSA
379 /* it's emulated with OSS */
380 return TRUE;
381 #else
382 return ((vol->master_element != NULL) ? snd_mixer_selem_has_playback_switch(vol->master_element) : FALSE);
383 #endif
384 }
385
386 /* Get the condition of the mute control from the sound system. */
387 static gboolean asound_is_muted(VolumeALSAPlugin * vol)
388 {
389 /* The switch is on if sound is not muted, and off if the sound is muted.
390 * Initialize so that the sound appears unmuted if the control does not exist. */
391 int value = 1;
392 #ifdef DISABLE_ALSA
393 StereoVolume levels;
394
395 ioctl(vol->mixer_fd, MIXER_READ(vol->master_channel), &levels.value);
396 value = (levels.left + levels.right) >> 1;
397 #else
398 if (vol->master_element != NULL)
399 snd_mixer_selem_get_playback_switch(vol->master_element, 0, &value);
400 #endif
401 return (value == 0);
402 }
403
404 #ifndef DISABLE_ALSA
405 static long lrint_dir(double x, int dir)
406 {
407 if (dir > 0)
408 return lrint(ceil(x));
409 else if (dir < 0)
410 return lrint(floor(x));
411 else
412 return lrint(x);
413 }
414
415 static inline gboolean use_linear_dB_scale(long dBmin, long dBmax)
416 {
417 return dBmax - dBmin <= MAX_LINEAR_DB_SCALE * 100;
418 }
419
420 static long get_normalized_volume(snd_mixer_elem_t *elem,
421 snd_mixer_selem_channel_id_t channel)
422 {
423 long min, max, value;
424 double normalized, min_norm;
425 int err;
426
427 err = snd_mixer_selem_get_playback_dB_range(elem, &min, &max);
428 if (err < 0 || min >= max) {
429 err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
430 if (err < 0 || min == max)
431 return 0;
432
433 err = snd_mixer_selem_get_playback_volume(elem, channel, &value);
434 if (err < 0)
435 return 0;
436
437 return lrint(100.0 * (value - min) / (double)(max - min));
438 }
439
440 err = snd_mixer_selem_get_playback_dB(elem, channel, &value);
441 if (err < 0)
442 return 0;
443
444 if (use_linear_dB_scale(min, max))
445 return lrint(100.0 * (value - min) / (double)(max - min));
446
447 normalized = exp10((value - max) / 6000.0);
448 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
449 min_norm = exp10((min - max) / 6000.0);
450 normalized = (normalized - min_norm) / (1 - min_norm);
451 }
452
453 return lrint(100.0 * normalized);
454 }
455 #endif
456
457 /* Get the volume from the sound system.
458 * This implementation returns the average of the Front Left and Front Right channels. */
459 static int asound_get_volume(VolumeALSAPlugin * vol)
460 {
461 #ifdef DISABLE_ALSA
462 StereoVolume levels;
463
464 ioctl(vol->mixer_fd, MIXER_READ(vol->master_channel), &levels.value);
465 return (levels.left + levels.right) >> 1;
466 #else
467 long aleft = 0;
468 long aright = 0;
469
470 if (vol->master_element != NULL)
471 {
472 if ( ! vol->alsamixer_mapping)
473 {
474 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, &aleft);
475 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, &aright);
476 }
477 else
478 {
479 aleft = get_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT);
480 aright = get_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT);
481 }
482 }
483 return (aleft + aright) >> 1;
484 #endif
485 }
486
487 #ifndef DISABLE_ALSA
488 static int set_normalized_volume(snd_mixer_elem_t *elem,
489 snd_mixer_selem_channel_id_t channel,
490 int vol,
491 int dir)
492 {
493 long min, max, value;
494 double min_norm, volume;
495 int err;
496
497 volume = vol / 100.0;
498
499 err = snd_mixer_selem_get_playback_dB_range(elem, &min, &max);
500 if (err < 0 || min >= max) {
501 err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
502 if (err < 0)
503 return err;
504
505 value = lrint_dir(volume * (max - min), dir) + min;
506 return snd_mixer_selem_set_playback_volume(elem, channel, value);
507 }
508
509 if (use_linear_dB_scale(min, max)) {
510 value = lrint_dir(volume * (max - min), dir) + min;
511 return snd_mixer_selem_set_playback_dB(elem, channel, value, dir);
512 }
513
514 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
515 min_norm = exp10((min - max) / 6000.0);
516 volume = volume * (1 - min_norm) + min_norm;
517 }
518 value = lrint_dir(6000.0 * log10(volume), dir) + max;
519
520 return snd_mixer_selem_set_playback_dB(elem, channel, value, dir);
521 }
522 #endif
523
524 /* Set the volume to the sound system.
525 * This implementation sets the Front Left and Front Right channels to the specified value. */
526 static void asound_set_volume(VolumeALSAPlugin * vol, int volume)
527 {
528 int dir = volume - asound_get_volume(vol);
529
530 /* Volume is set to the correct value already */
531 if (dir == 0)
532 return;
533
534 #ifdef DISABLE_ALSA
535 StereoVolume levels;
536
537 levels.left = levels.right = volume;
538 ioctl(vol->mixer_fd, MIXER_WRITE(vol->master_channel), &levels.value);
539 #else
540 if (vol->master_element != NULL)
541 {
542 if ( ! vol->alsamixer_mapping)
543 {
544 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, volume);
545 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, volume);
546 }
547 else
548 {
549 set_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, volume, dir);
550 set_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, volume, dir);
551 }
552 }
553 #endif
554 }
555
556 /*** Graphics ***/
557
558 static void volumealsa_lookup_current_icon(VolumeALSAPlugin * vol, gboolean mute, int level)
559 {
560 /* Change icon according to mute / volume */
561 const char* icon_panel="audio-volume-muted-panel";
562 const char* icon_fallback=ICONS_MUTE;
563 if (mute)
564 {
565 icon_panel = "audio-volume-muted-panel";
566 icon_fallback=ICONS_MUTE;
567 }
568 else if (level >= 66)
569 {
570 icon_panel = "audio-volume-high-panel";
571 icon_fallback=ICONS_VOLUME_HIGH;
572 }
573 else if (level >= 33)
574 {
575 icon_panel = "audio-volume-medium-panel";
576 icon_fallback=ICONS_VOLUME_MEDIUM;
577 }
578 else if (level > 0)
579 {
580 icon_panel = "audio-volume-low-panel";
581 icon_fallback=ICONS_VOLUME_LOW;
582 }
583
584 vol->icon_panel = icon_panel;
585 vol->icon_fallback = icon_fallback;
586 }
587
588 static void volumealsa_update_current_icon(VolumeALSAPlugin * vol, gboolean mute, int level)
589 {
590 /* Find suitable icon */
591 volumealsa_lookup_current_icon(vol, mute, level);
592
593 /* Change icon, fallback to default icon if theme doesn't exsit */
594 lxpanel_image_change_icon(vol->tray_icon, vol->icon_panel, vol->icon_fallback);
595
596 /* Display current level in tooltip. */
597 char * tooltip = g_strdup_printf("%s %d", _("Volume control"), level);
598 gtk_widget_set_tooltip_text(vol->plugin, tooltip);
599 g_free(tooltip);
600 }
601
602 /*
603 * Here we just update volume's vertical scale and mute check button.
604 * The rest will be updated by signal handelrs.
605 */
606 static void volumealsa_update_display(VolumeALSAPlugin * vol)
607 {
608 /* Mute. */
609 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), asound_is_muted(vol));
610 gtk_widget_set_sensitive(vol->mute_check, (asound_has_mute(vol)));
611
612 /* Volume. */
613 if (vol->volume_scale != NULL)
614 {
615 gtk_range_set_value(GTK_RANGE(vol->volume_scale), asound_get_volume(vol));
616 }
617 }
618
619 struct mixer_desc
620 {
621 char * cmd;
622 char * exec;
623 gboolean needs_pa;
624 gboolean needs_term;
625 };
626
627 const struct mixer_desc mixers[] = {
628 /* those with needs_pa should be first! */
629 { "gnome-sound-applet", "gnome-sound-applet", TRUE, FALSE },
630 { "pavucontrol", "pavucontrol", TRUE, FALSE },
631 { "gnome-alsamixer", "gnome-alsamixer", FALSE, FALSE },
632 { "alsamixergui", "alsamixergui", FALSE, FALSE },
633 { "alsamixer", "alsamixer", FALSE, TRUE },
634 { NULL }
635 };
636
637 static void volume_run_mixer(VolumeALSAPlugin * vol)
638 {
639 char *path = NULL;
640 const gchar *command_line = NULL;
641 GAppInfoCreateFlags flags = G_APP_INFO_CREATE_NONE;
642 int i;
643
644 /* check if command line was configured */
645 if (config_setting_lookup_string(vol->settings, "MixerCommand", &command_line))
646 if (config_setting_lookup_int(vol->settings, "MixerCommandTerm", &i) && i)
647 flags = G_APP_INFO_CREATE_NEEDS_TERMINAL;
648
649 /* if command isn't set in settings then let guess it */
650 if (command_line == NULL)
651 {
652 i = 0;
653 path = g_find_program_in_path("pulseaudio");
654 /* Assume that when pulseaudio is installed, it's launching every time */
655 if (path)
656 g_free(path);
657 /* Fallback to alsamixer when PA is not running, or when no PA utility is find */
658 else while (mixers[i].cmd && mixers[i].needs_pa)
659 i++;
660 for (; mixers[i].cmd; i++)
661 {
662 if ((path = g_find_program_in_path(mixers[i].exec)))
663 {
664 command_line = mixers[i].cmd;
665 if (mixers[i].needs_term)
666 flags = G_APP_INFO_CREATE_NEEDS_TERMINAL;
667 g_free(path);
668 break;
669 }
670 }
671 }
672
673 if (command_line)
674 {
675 fm_launch_command_simple(NULL, NULL, flags, command_line, NULL);
676 }
677 else
678 {
679 fm_show_error(NULL, NULL,
680 _("Error, you need to install an application to configure"
681 " the sound (pavucontrol, alsamixer ...)"));
682 }
683 }
684
685 static void _check_click(VolumeALSAPlugin * vol, int button, GdkModifierType mod)
686 {
687 if (vol->slider_click == button && vol->slider_click_mods == mod)
688 {
689 /* Left-click. Show or hide the popup window. */
690 if (vol->show_popup)
691 {
692 gtk_widget_hide(vol->popup_window);
693 vol->show_popup = FALSE;
694 }
695 else
696 {
697 gtk_widget_show_all(vol->popup_window);
698 vol->show_popup = TRUE;
699 }
700 }
701 if (vol->mute_click == button && vol->mute_click_mods == mod)
702 {
703 /* Middle-click. Toggle the mute status. */
704 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check)));
705 }
706 if (vol->mixer_click == button && vol->mixer_click_mods == mod)
707 {
708 volume_run_mixer(vol);
709 }
710 }
711
712 /* Handler for "button-press-event" signal on main widget. */
713 static gboolean volumealsa_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * panel)
714 {
715 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(widget);
716
717 if (event->button == 1)
718 {
719 _check_click(vol, 1,
720 event->state & gtk_accelerator_get_default_mod_mask());
721 }
722
723 return FALSE;
724 }
725
726 static gboolean volumealsa_button_release_event(GtkWidget * widget, GdkEventButton * event, VolumeALSAPlugin * vol)
727 {
728 if (event->button != 1)
729 {
730 _check_click(vol, event->button,
731 event->state & gtk_accelerator_get_default_mod_mask());
732 }
733 return FALSE;
734 }
735
736 /* Handler for "focus-out" signal on popup window. */
737 static gboolean volumealsa_popup_focus_out(GtkWidget * widget, GdkEvent * event, VolumeALSAPlugin * vol)
738 {
739 /* Hide the widget. */
740 gtk_widget_hide(vol->popup_window);
741 vol->show_popup = FALSE;
742 return FALSE;
743 }
744
745 /* Handler for "map" signal on popup window. */
746 static void volumealsa_popup_map(GtkWidget * widget, VolumeALSAPlugin * vol)
747 {
748 lxpanel_plugin_adjust_popup_position(widget, vol->plugin);
749 }
750
751 /* Handler for "value_changed" signal on popup window vertical scale. */
752 static void volumealsa_popup_scale_changed(GtkRange * range, VolumeALSAPlugin * vol)
753 {
754 int level = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
755 gboolean mute = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
756
757 /* Reflect the value of the control to the sound system. */
758 asound_set_volume(vol, level);
759
760 /*
761 * Redraw the controls.
762 * Scale and check button do not need to be updated, as these are always
763 * in sync with user's actions.
764 */
765 volumealsa_update_current_icon(vol, mute, level);
766 }
767
768 /* Handler for "scroll-event" signal on popup window vertical scale. */
769 static void volumealsa_popup_scale_scrolled(GtkScale * scale, GdkEventScroll * evt, VolumeALSAPlugin * vol)
770 {
771 /* Get the state of the vertical scale. */
772 gdouble val = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
773
774 /* Dispatch on scroll direction to update the value. */
775 if ((evt->direction == GDK_SCROLL_UP) || (evt->direction == GDK_SCROLL_LEFT))
776 val += 2;
777 else
778 val -= 2;
779
780 /* Reset the state of the vertical scale. This provokes a "value_changed" event. */
781 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP((int)val, 0, 100));
782 }
783
784 /* Handler for "toggled" signal on popup window mute checkbox. */
785 static void volumealsa_popup_mute_toggled(GtkWidget * widget, VolumeALSAPlugin * vol)
786 {
787 int level = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
788 gboolean mute = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
789
790 /* Reflect the mute toggle to the sound system. */
791 #ifdef DISABLE_ALSA
792 if (mute)
793 {
794 vol->vol_before_mute = level;
795 asound_set_volume(vol, 0);
796 }
797 else
798 {
799 asound_set_volume(vol, vol->vol_before_mute);
800 }
801 #else
802 if (vol->master_element != NULL)
803 {
804 int chn;
805 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++)
806 snd_mixer_selem_set_playback_switch(vol->master_element, chn, ((mute) ? 0 : 1));
807 }
808 #endif
809
810 /*
811 * Redraw the controls.
812 * Scale and check button do not need to be updated, as these are always
813 * in sync with user's actions.
814 */
815 volumealsa_update_current_icon(vol, mute, level);
816 }
817
818 /* Hotkeys handlers */
819 static void volume_up(const char *keystring, gpointer user_data)
820 {
821 VolumeALSAPlugin * vol = (VolumeALSAPlugin *)user_data;
822 int val = (int)gtk_range_get_value(GTK_RANGE(vol->volume_scale)) + 2;
823 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP(val, 0, 100));
824 }
825
826 static void volume_down(const char *keystring, gpointer user_data)
827 {
828 VolumeALSAPlugin * vol = (VolumeALSAPlugin *)user_data;
829 int val = (int)gtk_range_get_value(GTK_RANGE(vol->volume_scale)) - 2;
830 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP(val, 0, 100));
831 }
832
833 static void volume_mute(const char *keystring, gpointer user_data)
834 {
835 VolumeALSAPlugin * vol = (VolumeALSAPlugin *)user_data;
836 gboolean muted = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
837 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), !muted);
838 }
839
840 /* Build the window that appears when the top level widget is clicked. */
841 static void volumealsa_build_popup_window(GtkWidget *p)
842 {
843 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
844
845 /* Create a new window. */
846 vol->popup_window = gtk_window_new(GTK_WINDOW_POPUP);
847 gtk_window_set_decorated(GTK_WINDOW(vol->popup_window), FALSE);
848 gtk_container_set_border_width(GTK_CONTAINER(vol->popup_window), 5);
849 gtk_window_set_default_size(GTK_WINDOW(vol->popup_window), 80, 140);
850 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(vol->popup_window), TRUE);
851 gtk_window_set_skip_pager_hint(GTK_WINDOW(vol->popup_window), TRUE);
852 gtk_window_set_type_hint(GTK_WINDOW(vol->popup_window), GDK_WINDOW_TYPE_HINT_UTILITY);
853
854 /* Connect signals. */
855 g_signal_connect(G_OBJECT(vol->popup_window), "focus-out-event", G_CALLBACK(volumealsa_popup_focus_out), vol);
856 g_signal_connect(G_OBJECT(vol->popup_window), "map", G_CALLBACK(volumealsa_popup_map), vol);
857
858 /* Create a scrolled window as the child of the top level window. */
859 GtkWidget * scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
860 gtk_container_set_border_width (GTK_CONTAINER(scrolledwindow), 0);
861 gtk_widget_show(scrolledwindow);
862 gtk_container_add(GTK_CONTAINER(vol->popup_window), scrolledwindow);
863 gtk_widget_set_can_focus(scrolledwindow, FALSE);
864 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
865 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_NONE);
866
867 /* Create a viewport as the child of the scrolled window. */
868 GtkWidget * viewport = gtk_viewport_new(NULL, NULL);
869 gtk_container_add(GTK_CONTAINER(scrolledwindow), viewport);
870 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
871 gtk_widget_show(viewport);
872
873 /* Create a frame as the child of the viewport. */
874 GtkWidget * frame = gtk_frame_new(_("Volume"));
875 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
876 gtk_container_add(GTK_CONTAINER(viewport), frame);
877
878 /* Create a vertical box as the child of the frame. */
879 GtkWidget * box = gtk_vbox_new(FALSE, 0);
880 gtk_container_add(GTK_CONTAINER(frame), box);
881
882 /* Create a vertical scale as the child of the vertical box. */
883 vol->volume_scale = gtk_vscale_new(GTK_ADJUSTMENT(gtk_adjustment_new(100, 0, 100, 0, 0, 0)));
884 gtk_scale_set_draw_value(GTK_SCALE(vol->volume_scale), FALSE);
885 gtk_range_set_inverted(GTK_RANGE(vol->volume_scale), TRUE);
886 gtk_box_pack_start(GTK_BOX(box), vol->volume_scale, TRUE, TRUE, 0);
887
888 /* Value-changed and scroll-event signals. */
889 vol->volume_scale_handler = g_signal_connect(vol->volume_scale, "value-changed", G_CALLBACK(volumealsa_popup_scale_changed), vol);
890 g_signal_connect(vol->volume_scale, "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol);
891
892 /* Create a check button as the child of the vertical box. */
893 vol->mute_check = gtk_check_button_new_with_label(_("Mute"));
894 gtk_box_pack_end(GTK_BOX(box), vol->mute_check, FALSE, FALSE, 0);
895 vol->mute_check_handler = g_signal_connect(vol->mute_check, "toggled", G_CALLBACK(volumealsa_popup_mute_toggled), vol);
896
897 /* Set background to default. */
898 gtk_widget_set_style(viewport, panel_get_defstyle(vol->panel));
899 }
900
901 /* Plugin constructor. */
902 static GtkWidget *volumealsa_constructor(LXPanel *panel, config_setting_t *settings)
903 {
904 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
905 VolumeALSAPlugin * vol = g_new0(VolumeALSAPlugin, 1);
906 GtkWidget *p;
907 const char *tmp_str;
908
909 #ifndef DISABLE_ALSA
910 /* Read config necessary for proper initialization of ALSA. */
911 config_setting_lookup_int(settings, "UseAlsamixerVolumeMapping", &vol->alsamixer_mapping);
912 if (config_setting_lookup_string(settings, "MasterChannel", &tmp_str))
913 vol->master_channel = g_strdup(tmp_str);
914 if (!config_setting_lookup_int(settings, "CardNumber", &vol->used_device))
915 vol->used_device = -1;
916 #else
917 vol->master_channel = SOUND_MIXER_VOLUME;
918 if (config_setting_lookup_string(settings, "MasterChannel", &tmp_str))
919 {
920 if (strcmp(tmp_str, "PCM") == 0)
921 vol->master_channel = SOUND_MIXER_PCM;
922 else if (strcmp(tmp_str, "Headphone") == 0)
923 vol->master_channel = SOUND_MIXER_PHONEOUT;
924 }
925 #endif
926 if (config_setting_lookup_string(settings, "MuteButton", &tmp_str))
927 vol->mute_click = panel_config_click_parse(tmp_str, &vol->mute_click_mods);
928 else
929 vol->mute_click = 2; /* middle-click default */
930 if (config_setting_lookup_string(settings, "SliderButton", &tmp_str))
931 vol->slider_click = panel_config_click_parse(tmp_str, &vol->slider_click_mods);
932 else
933 vol->slider_click = 1; /* left-click default */
934 if (config_setting_lookup_string(settings, "MixerButton", &tmp_str))
935 vol->mixer_click = panel_config_click_parse(tmp_str, &vol->mixer_click_mods);
936 if (config_setting_lookup_string(settings, "VolumeUpKey", &tmp_str))
937 lxpanel_apply_hotkey(&vol->hotkey_up, tmp_str, volume_up, vol, FALSE);
938 if (config_setting_lookup_string(settings, "VolumeDownKey", &tmp_str))
939 lxpanel_apply_hotkey(&vol->hotkey_down, tmp_str, volume_down, vol, FALSE);
940 if (config_setting_lookup_string(settings, "VolumeMuteKey", &tmp_str))
941 lxpanel_apply_hotkey(&vol->hotkey_mute, tmp_str, volume_mute, vol, FALSE);
942
943 /* Initialize ALSA. If that fails, present nothing. */
944 if ( ! asound_initialize(vol))
945 {
946 volumealsa_destructor(vol);
947 return NULL;
948 }
949
950 /* Allocate top level widget and set into Plugin widget pointer. */
951 vol->panel = panel;
952 vol->plugin = p = gtk_event_box_new();
953 vol->settings = settings;
954 lxpanel_plugin_set_data(p, vol, volumealsa_destructor);
955 gtk_widget_set_tooltip_text(p, _("Volume control"));
956
957 /* Allocate icon as a child of top level. */
958 vol->tray_icon = lxpanel_image_new_for_icon(panel, "audio-volume-muted-panel",
959 -1, ICONS_MUTE);
960 gtk_container_add(GTK_CONTAINER(p), vol->tray_icon);
961
962 /* Initialize window to appear when icon clicked. */
963 volumealsa_build_popup_window(p);
964
965 /* Connect signals. */
966 g_signal_connect(G_OBJECT(p), "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol );
967 g_signal_connect(G_OBJECT(p), "button-release-event", G_CALLBACK(volumealsa_button_release_event), vol );
968
969 /* Update the display, show the widget, and return. */
970 volumealsa_update_display(vol);
971 volumealsa_update_current_icon(vol, asound_is_muted(vol), asound_get_volume(vol));
972 gtk_widget_show_all(p);
973 return p;
974 }
975
976 /* Plugin destructor. */
977 static void volumealsa_destructor(gpointer user_data)
978 {
979 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) user_data;
980
981 lxpanel_apply_hotkey(&vol->hotkey_up, NULL, NULL, NULL, FALSE);
982 lxpanel_apply_hotkey(&vol->hotkey_down, NULL, NULL, NULL, FALSE);
983 lxpanel_apply_hotkey(&vol->hotkey_mute, NULL, NULL, NULL, FALSE);
984
985 asound_deinitialize(vol);
986
987 /* If the dialog box is open, dismiss it. */
988 if (vol->popup_window != NULL)
989 gtk_widget_destroy(vol->popup_window);
990
991 #ifndef DISABLE_ALSA
992 if (vol->restart_idle)
993 g_source_remove(vol->restart_idle);
994
995 g_free(vol->master_channel);
996 #endif
997
998 /* Deallocate all memory. */
999 g_free(vol);
1000 }
1001
1002 #ifndef DISABLE_ALSA
1003 static GtkListStore *alsa_make_channels_list(VolumeALSAPlugin *vol, int *active)
1004 {
1005 GtkListStore *list;
1006 GtkTreeIter iter;
1007 snd_mixer_selem_id_t *sid;
1008 snd_mixer_elem_t *elem;
1009 const char *name;
1010 int i;
1011
1012 snd_mixer_selem_id_alloca(&sid);
1013 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); /* desc, value */
1014 for (elem = snd_mixer_first_elem(vol->mixer), i = 0; elem != NULL;
1015 elem = snd_mixer_elem_next(elem), i++)
1016 {
1017 if (snd_mixer_selem_is_active(elem) &&
1018 snd_mixer_selem_has_playback_volume(elem) &&
1019 !snd_mixer_selem_has_capture_volume(elem) &&
1020 !snd_mixer_selem_has_capture_switch(elem))
1021 {
1022 snd_mixer_selem_get_id(elem, sid);
1023 name = snd_mixer_selem_id_get_name(sid);
1024 gtk_list_store_insert_with_values(list, &iter, i, 0, _(name),
1025 1, name, -1);
1026 if (elem == vol->master_element)
1027 *active = i;
1028 }
1029 }
1030 return list;
1031 }
1032
1033 static void card_selector_changed(GtkComboBox *card_selector, VolumeALSAPlugin *vol)
1034 {
1035 GtkTreeModel *model = gtk_combo_box_get_model(card_selector);
1036 GtkTreeIter iter;
1037 int old_card = vol->used_device;
1038 int i = gtk_combo_box_get_active(card_selector);
1039 char *old_channel;
1040
1041 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
1042 gtk_tree_model_get(model, &iter, 1, &vol->used_device, -1);
1043 asound_deinitialize(vol);
1044 if (!asound_initialize(vol))
1045 {
1046 /* could not change card with the same master channel, try default */
1047 old_channel = vol->master_channel;
1048 vol->master_channel = NULL;
1049 asound_deinitialize(vol);
1050 if (!asound_initialize(vol))
1051 {
1052 g_warning("could not set card to %d", vol->used_device);
1053 vol->master_channel = old_channel;
1054 vol->used_device = old_card;
1055 /* schedule to restart with old settings */
1056 if (vol->restart_idle == 0)
1057 vol->restart_idle = g_timeout_add_seconds(1, asound_restart, vol);
1058 return;
1059 }
1060 g_free(old_channel);
1061 config_group_set_string(vol->settings, "MasterChannel", NULL);
1062 }
1063 /* remember and apply selection */
1064 volumealsa_update_display(vol);
1065 config_group_set_int(vol->settings, "CardNumber", vol->used_device);
1066 /* rebuild channel selection list */
1067 i = -1;
1068 model = GTK_TREE_MODEL(alsa_make_channels_list(vol, &i));
1069 gtk_combo_box_set_model(GTK_COMBO_BOX(vol->channel_selector), model);
1070 gtk_combo_box_set_active(GTK_COMBO_BOX(vol->channel_selector), i);
1071 }
1072 #endif
1073
1074 static void channel_selector_changed(GtkComboBox *channel_selector, VolumeALSAPlugin *vol)
1075 {
1076 GtkTreeModel *model = gtk_combo_box_get_model(channel_selector);
1077 GtkTreeIter iter;
1078 #ifdef DISABLE_ALSA
1079 int ch; /* channel index */
1080 #else
1081 char *ch; /* channel name */
1082 #endif
1083 int i = gtk_combo_box_get_active(channel_selector);
1084
1085 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
1086 gtk_tree_model_get(model, &iter, 1, &ch, -1);
1087 #ifdef DISABLE_ALSA
1088 config_group_set_int(vol->settings, "MasterChannel", ch);
1089 #else
1090 config_group_set_string(vol->settings, "MasterChannel", ch);
1091 asound_find_element(vol, (const char **)&ch, 1); //FIXME: is error possible?
1092 /* Set the playback volume range as we wish it. */
1093 if (!vol->alsamixer_mapping)
1094 snd_mixer_selem_set_playback_volume_range(vol->master_element, 0, 100);
1095 /* g_debug("MasterChannel changed: %s", ch); */
1096 g_free(vol->master_channel);
1097 #endif
1098 vol->master_channel = ch; /* just take it instead of alloc + free */
1099 volumealsa_update_display(vol);
1100 }
1101
1102 static void mixer_selector_changed(GtkComboBox *mixer_selector, VolumeALSAPlugin *vol)
1103 {
1104 GtkWidget *mixer_entry = gtk_bin_get_child(GTK_BIN(mixer_selector));
1105 const char *cmd, *set;
1106 GtkTreeModel *model;
1107 GtkTreeIter iter;
1108 int i;
1109
1110 i = gtk_combo_box_get_active(mixer_selector);
1111 if (i < 0)
1112 /* it was just editing */
1113 return;
1114 if (!config_setting_lookup_string(vol->settings, "MixerCommand", &set))
1115 set = NULL;
1116 cmd = gtk_entry_get_text((GtkEntry *)mixer_entry);
1117 if (set)
1118 {
1119 if (strcmp(set, cmd) == 0)
1120 /* not changed */
1121 return;
1122 }
1123 else if (gtk_combo_box_get_active(mixer_selector) == 0)
1124 /* it's left at default */
1125 return;
1126 model = gtk_combo_box_get_model(mixer_selector);
1127 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
1128 gtk_tree_model_get(model, &iter, 1, &i, -1);
1129 /* g_debug("new choice: %s needs_term=%d", cmd, i); */
1130 config_group_set_string(vol->settings, "MixerCommand", cmd);
1131 config_group_set_int(vol->settings, "MixerCommandTerm", i);
1132 }
1133
1134 struct mixer_selector_check_data
1135 {
1136 GtkComboBox *mixer_selector;
1137 const char *text;
1138 int needs_term;
1139 };
1140
1141 static gboolean mixer_selector_check(GtkTreeModel *model, GtkTreePath *path,
1142 GtkTreeIter *iter, gpointer user_data)
1143 {
1144 struct mixer_selector_check_data *data = user_data;
1145 char *cmd;
1146
1147 gtk_tree_model_get(model, iter, 0, &cmd, 1, &data->needs_term, -1);
1148 if (cmd && strcmp(cmd, data->text) == 0)
1149 {
1150 int *indices = gtk_tree_path_get_indices(path);
1151 gtk_combo_box_set_active(data->mixer_selector, indices[0]);
1152 g_free(cmd);
1153 return TRUE;
1154 }
1155 g_free(cmd);
1156 return FALSE;
1157 }
1158
1159 static gboolean mixer_selector_focus_out(GtkWidget *mixer_entry,
1160 GdkEvent *evt, VolumeALSAPlugin *vol)
1161 {
1162 struct mixer_selector_check_data data;
1163 GtkTreeModel *model;
1164
1165 data.mixer_selector = GTK_COMBO_BOX(gtk_widget_get_parent(mixer_entry));
1166 data.text = gtk_entry_get_text((GtkEntry *)mixer_entry);
1167 data.needs_term = 0;
1168 model = gtk_combo_box_get_model(data.mixer_selector);
1169
1170 /* check if current value is one of model choices */
1171 if (gtk_combo_box_get_active(data.mixer_selector) < 0)
1172 gtk_tree_model_foreach(model, &mixer_selector_check, &data);
1173 /* check executable and remember selection */
1174 if (gtk_combo_box_get_active(data.mixer_selector) < 0)
1175 {
1176 /* check only user input since predefined choices were tested already */
1177 char *exec, *path;
1178
1179 /* g_debug("user entered mixer: %s", data.text); */
1180 exec = strchr(data.text, ' ');
1181 if (exec)
1182 exec = g_strndup(data.text, exec - data.text);
1183 path = g_find_program_in_path(exec ? exec : data.text);
1184 g_free(exec);
1185 g_free(path);
1186 if (path == NULL)
1187 {
1188 /* invalid executable requested, ignore it then */
1189 g_warning("%s cannot be executed, ignoring it", data.text);
1190 return FALSE;
1191 }
1192 }
1193 config_group_set_string(vol->settings, "MixerCommand", data.text);
1194 config_group_set_int(vol->settings, "MixerCommandTerm", data.needs_term);
1195 return FALSE;
1196 }
1197
1198 static gboolean mixer_selector_key_press(GtkWidget *mixer_entry,
1199 GdkEventKey *evt, VolumeALSAPlugin *vol)
1200 {
1201 if (evt->keyval == GDK_KEY_Return)
1202 /* loose focus on Enter press */
1203 gtk_window_set_focus(GTK_WINDOW(gtk_widget_get_toplevel(mixer_entry)), NULL);
1204 return FALSE;
1205 }
1206
1207 static gboolean mute_button_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1208 {
1209 int n;
1210 GdkModifierType mods;
1211
1212 n = panel_config_click_parse(click, &mods);
1213 if (n == 0 || ((n != vol->mixer_click || mods != vol->mixer_click_mods) &&
1214 (n != vol->slider_click || mods != vol->slider_click_mods)))
1215 {
1216 config_group_set_string(vol->settings, "MuteButton", click);
1217 vol->mute_click = n;
1218 vol->mute_click_mods = mods;
1219 return TRUE;
1220 }
1221 //FIXME: show a message?
1222 return FALSE;
1223 }
1224
1225 static gboolean mixer_button_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1226 {
1227 int n;
1228 GdkModifierType mods;
1229
1230 n = panel_config_click_parse(click, &mods);
1231 if (n == 0 || ((n != vol->mute_click || mods != vol->mute_click_mods) &&
1232 (n != vol->slider_click || mods != vol->slider_click_mods)))
1233 {
1234 config_group_set_string(vol->settings, "MixerButton", click);
1235 vol->mixer_click = n;
1236 vol->mixer_click_mods = mods;
1237 return TRUE;
1238 }
1239 //FIXME: show a message?
1240 return FALSE;
1241 }
1242
1243 static gboolean volume_button_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1244 {
1245 int n;
1246 GdkModifierType mods;
1247
1248 n = panel_config_click_parse(click, &mods);
1249 if (n == 0 || ((n != vol->mixer_click || mods != vol->mixer_click_mods) &&
1250 (n != vol->mute_click || mods != vol->mute_click_mods)))
1251 {
1252 config_group_set_string(vol->settings, "SliderButton", click);
1253 vol->slider_click = n;
1254 vol->slider_click_mods = mods;
1255 return TRUE;
1256 }
1257 //FIXME: show a message?
1258 return FALSE;
1259 }
1260
1261 static gboolean up_key_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1262 {
1263 gboolean res;
1264
1265 res = lxpanel_apply_hotkey(&vol->hotkey_up, click, &volume_up, vol, TRUE);
1266 if (res)
1267 config_group_set_string(vol->settings, "VolumeUpKey", click);
1268 return res;
1269 }
1270
1271 static gboolean down_key_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1272 {
1273 gboolean res;
1274
1275 res = lxpanel_apply_hotkey(&vol->hotkey_down, click, &volume_down, vol, TRUE);
1276 if (res)
1277 config_group_set_string(vol->settings, "VolumeDownKey", click);
1278 return res;
1279 }
1280
1281 static gboolean mute_key_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1282 {
1283 gboolean res;
1284
1285 res = lxpanel_apply_hotkey(&vol->hotkey_mute, click, &volume_mute, vol, TRUE);
1286 if (res)
1287 config_group_set_string(vol->settings, "VolumeMuteKey", click);
1288 return res;
1289 }
1290
1291 #if THING_THAT_NEVER_HAPPEN
1292 /* Just to have these translated */
1293 N_("Line"), N_("LineOut"), N_("Front"), N_("Surround"), N_("Center"), N_("Speaker+LO");
1294 #endif
1295
1296 /* Callback when the configuration dialog is to be shown. */
1297 static GtkWidget *volumealsa_configure(LXPanel *panel, GtkWidget *p)
1298 {
1299 VolumeALSAPlugin *vol = lxpanel_plugin_get_data(p);
1300 const char *tmp_str;
1301 char *path;
1302 GtkListStore *list;
1303 GtkCellRenderer *column;
1304 #ifndef DISABLE_ALSA
1305 snd_mixer_selem_id_t *sid;
1306 snd_mixer_elem_t *elem;
1307 snd_hctl_t *hctl;
1308 GtkWidget *card_selector;
1309 #endif
1310 GtkWidget *mute_button;
1311 GtkWidget *volume_button;
1312 GtkWidget *mixer_button;
1313 GtkWidget *up_key;
1314 GtkWidget *down_key;
1315 GtkWidget *mute_key;
1316 GtkWidget *mixer_selector;
1317 GtkWidget *mixer_entry;
1318 GtkTreeIter iter;
1319 int active = 0;
1320 int i = 0;
1321 #ifndef DISABLE_ALSA
1322 int j = -1;
1323
1324 snd_mixer_selem_id_alloca(&sid);
1325 /* setup card selector */
1326 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); /* desc, num */
1327 if (snd_hctl_open(&hctl, "default", 0) == 0)
1328 {
1329 /* check if "default" isn't a HW, so include it in the list */
1330 if (snd_ctl_type(snd_hctl_ctl(hctl)) != 0)
1331 {
1332 gtk_list_store_insert_with_values(list, &iter, i++, 0, _("default"),
1333 1, j, -1);
1334 if (vol->used_device < 0)
1335 active = 0;
1336 }
1337 snd_hctl_close(hctl);
1338 }
1339 while (snd_card_next(&j) == 0 && j >= 0)
1340 {
1341 char *name = NULL;
1342 snd_mixer_t *mixer;
1343 char id[16];
1344
1345 if (snd_card_get_name(j, &name) == 0)
1346 {
1347 /* test if there any available channel */
1348 snprintf(id, sizeof(id), "hw:%d", j);
1349 snd_mixer_open(&mixer, 0);
1350 snd_mixer_attach(mixer, id);
1351 snd_mixer_selem_register(mixer, NULL, NULL);
1352 snd_mixer_load(mixer);
1353 for (elem = snd_mixer_first_elem(mixer); elem != NULL;
1354 elem = snd_mixer_elem_next(elem))
1355 {
1356 if (snd_mixer_selem_is_active(elem) &&
1357 snd_mixer_selem_has_playback_volume(elem) &&
1358 !snd_mixer_selem_has_capture_volume(elem) &&
1359 !snd_mixer_selem_has_capture_switch(elem))
1360 break;
1361 }
1362 snd_mixer_close(mixer);
1363 if (elem != NULL)
1364 {
1365 g_debug("found soundcard: %s", name);
1366 gtk_list_store_insert_with_values(list, &iter, i++, 0, name,
1367 1, j, -1);
1368 if (vol->used_device == j)
1369 active = i;
1370 }
1371 else
1372 g_debug("no elements in soundcard %s", name);
1373 free(name);
1374 }
1375 }
1376 card_selector = gtk_combo_box_new_with_model(GTK_TREE_MODEL(list));
1377 /* gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(card_selector), 1); */
1378 column = gtk_cell_renderer_text_new();
1379 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(card_selector), column, TRUE);
1380 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(card_selector), column,
1381 "text", 0, NULL);
1382 gtk_combo_box_set_active(GTK_COMBO_BOX(card_selector), active);
1383 g_signal_connect(card_selector, "changed",
1384 G_CALLBACK(card_selector_changed), vol);
1385 g_signal_connect(card_selector, "scroll-event", G_CALLBACK(gtk_true), NULL);
1386 #endif
1387
1388 /* setup channel selector */
1389 #ifdef DISABLE_ALSA
1390 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); /* desc, index */
1391 gtk_list_store_insert_with_values(list, &iter, 0, 0, _("Master"),
1392 1, SOUND_MIXER_VOLUME, -1);
1393 if (vol->master_channel == SOUND_MIXER_VOLUME)
1394 active = 0;
1395 gtk_list_store_insert_with_values(list, &iter, 1, 0, _("PCM"),
1396 1, SOUND_MIXER_PCM, -1);
1397 if (vol->master_channel == SOUND_MIXER_VOLUME)
1398 active = 1;
1399 gtk_list_store_insert_with_values(list, &iter, 2, 0, _("Headphone"),
1400 1, SOUND_MIXER_PHONEOUT, -1);
1401 if (vol->master_channel == SOUND_MIXER_VOLUME)
1402 active = 2;
1403 #else
1404 list = alsa_make_channels_list(vol, &active);
1405 #endif
1406 vol->channel_selector = gtk_combo_box_new_with_model(GTK_TREE_MODEL(list));
1407 /* gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(vol->channel_selector), 1); */
1408 column = gtk_cell_renderer_text_new();
1409 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(vol->channel_selector), column, TRUE);
1410 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(vol->channel_selector), column,
1411 "text", 0, NULL);
1412 gtk_combo_box_set_active(GTK_COMBO_BOX(vol->channel_selector), active);
1413 g_signal_connect(vol->channel_selector, "changed",
1414 G_CALLBACK(channel_selector_changed), vol);
1415 g_signal_connect(vol->channel_selector, "scroll-event", G_CALLBACK(gtk_true), NULL);
1416
1417 /* setup buttons */
1418 if (!config_setting_lookup_string(vol->settings, "SliderButton", &tmp_str))
1419 tmp_str = "1";
1420 volume_button = panel_config_click_button_new(_("Click for Volume Slider"), tmp_str);
1421 g_signal_connect(volume_button, "changed", G_CALLBACK(volume_button_changed), vol);
1422 if (!config_setting_lookup_string(vol->settings, "MuteButton", &tmp_str))
1423 tmp_str = "2";
1424 mute_button = panel_config_click_button_new(_("Click for Toggle Mute"), tmp_str);
1425 g_signal_connect(mute_button, "changed", G_CALLBACK(mute_button_changed), vol);
1426 if (!config_setting_lookup_string(vol->settings, "MixerButton", &tmp_str))
1427 tmp_str = NULL;
1428 mixer_button = panel_config_click_button_new(_("Click for Open Mixer"), tmp_str);
1429 g_signal_connect(mixer_button, "changed", G_CALLBACK(mixer_button_changed), vol);
1430
1431 /* setup hotkeys */
1432 up_key = panel_config_hotkey_button_new(_("Hotkey for Volume Up"), vol->hotkey_up);
1433 g_signal_connect(up_key, "changed", G_CALLBACK(up_key_changed), vol);
1434 down_key = panel_config_hotkey_button_new(_("Hotkey for Volume Down"), vol->hotkey_down);
1435 g_signal_connect(down_key, "changed", G_CALLBACK(down_key_changed), vol);
1436 mute_key = panel_config_hotkey_button_new(_("Hotkey for Volume Mute"), vol->hotkey_mute);
1437 g_signal_connect(mute_key, "changed", G_CALLBACK(mute_key_changed), vol);
1438
1439 /* setup mixer selector */
1440 if (!config_setting_lookup_string(vol->settings, "MixerCommand", &tmp_str))
1441 tmp_str = NULL;
1442 active = -1;
1443 i = j = 0;
1444 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); /* line, needs_term */
1445 path = g_find_program_in_path("pulseaudio");
1446 if (path)
1447 g_free(path);
1448 else while (mixers[i].cmd && mixers[i].needs_pa)
1449 i++;
1450 for (; mixers[i].cmd; i++)
1451 {
1452 path = g_find_program_in_path(mixers[i].exec);
1453 if (path)
1454 {
1455 if (tmp_str && active < 0 && strcmp(tmp_str, mixers[i].cmd) == 0)
1456 {
1457 active = j;
1458 tmp_str = NULL;
1459 }
1460 gtk_list_store_insert_with_values(list, &iter, j++, 0, mixers[i].cmd,
1461 1, (int)mixers[i].needs_term,
1462 -1);
1463 g_free(path);
1464 }
1465 }
1466 if (tmp_str)
1467 {
1468 active = j;
1469 /* FIXME: support "needs terminal" for custom MixerCommand */
1470 gtk_list_store_insert_with_values(list, &iter, j, 0, tmp_str, 1, 0, -1);
1471 }
1472 if (active < 0)
1473 active = 0;
1474 #if GTK_CHECK_VERSION(2, 24, 0)
1475 mixer_selector = gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(list));
1476 gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(mixer_selector), 0);
1477 #else
1478 mixer_selector = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(list), 0);
1479 #endif
1480 /* gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(mixer_selector), 1); */
1481 gtk_combo_box_set_active(GTK_COMBO_BOX(mixer_selector), active);
1482 mixer_entry = gtk_bin_get_child(GTK_BIN(mixer_selector));
1483 g_signal_connect(mixer_entry, "key-press-event",
1484 G_CALLBACK(mixer_selector_key_press), vol);
1485 g_signal_connect(mixer_selector, "changed",
1486 G_CALLBACK(mixer_selector_changed), vol);
1487 g_signal_connect(mixer_entry, "focus-out-event",
1488 G_CALLBACK(mixer_selector_focus_out), vol);
1489 g_signal_connect(mixer_selector, "scroll-event", G_CALLBACK(gtk_true), NULL);
1490
1491 return lxpanel_generic_config_dlg(_("Volume Control"), panel, NULL, p,
1492 #ifndef DISABLE_ALSA
1493 _("Audio Card"), NULL, CONF_TYPE_TRIM,
1494 "", card_selector, CONF_TYPE_EXTERNAL,
1495 #endif
1496 _("Channel to Operate"), NULL, CONF_TYPE_TRIM,
1497 "", vol->channel_selector, CONF_TYPE_EXTERNAL,
1498 "", volume_button, CONF_TYPE_EXTERNAL,
1499 "", mute_button, CONF_TYPE_EXTERNAL,
1500 "", mixer_button, CONF_TYPE_EXTERNAL,
1501 "", up_key, CONF_TYPE_EXTERNAL,
1502 "", down_key, CONF_TYPE_EXTERNAL,
1503 "", mute_key, CONF_TYPE_EXTERNAL,
1504 _("Command to Open Mixer"), NULL, CONF_TYPE_TRIM,
1505 "", mixer_selector, CONF_TYPE_EXTERNAL,
1506 NULL);
1507 }
1508
1509 /* Callback when panel configuration changes. */
1510 static void volumealsa_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
1511 {
1512 /* Do a full redraw. */
1513 volumealsa_update_display(lxpanel_plugin_get_data(p));
1514 }
1515
1516 static gboolean volumealsa_update_context_menu(GtkWidget *plugin, GtkMenu *menu)
1517 {
1518 GtkWidget *img = gtk_image_new_from_stock("gtk-directory", GTK_ICON_SIZE_MENU);
1519 GtkWidget *menu_item = gtk_image_menu_item_new_with_label(_("Launch Mixer"));
1520 //FIXME: precheck and disable if MixerCommand not set
1521 gtk_image_menu_item_set_image((GtkImageMenuItem *)menu_item, img);
1522 g_signal_connect_swapped(menu_item, "activate", G_CALLBACK(volume_run_mixer),
1523 lxpanel_plugin_get_data(plugin));
1524 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
1525 return FALSE;
1526 }
1527
1528 #ifndef DISABLE_ALSA
1529 static LXPanelPluginInit _volumealsa_init = {
1530 .name = N_("Volume Control"),
1531 .description = N_("Display and control volume"),
1532
1533 .superseded = TRUE,
1534 .new_instance = volumealsa_constructor,
1535 .config = volumealsa_configure,
1536 .reconfigure = volumealsa_panel_configuration_changed,
1537 .update_context_menu = volumealsa_update_context_menu,
1538 .button_press_event = volumealsa_button_press_event
1539 };
1540
1541 static void volumealsa_init(void)
1542 {
1543 lxpanel_register_plugin_type("volumealsa", &_volumealsa_init);
1544 }
1545 #endif
1546
1547 FM_DEFINE_MODULE(lxpanel_gtk, volume)
1548
1549 /* Plugin descriptor. */
1550 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
1551 .name = N_("Volume Control"),
1552 .description = N_("Display and control volume"),
1553 #ifndef DISABLE_ALSA
1554 .init = volumealsa_init,
1555 #endif
1556
1557 .new_instance = volumealsa_constructor,
1558 .config = volumealsa_configure,
1559 .reconfigure = volumealsa_panel_configuration_changed,
1560 .update_context_menu = volumealsa_update_context_menu,
1561 .button_press_event = volumealsa_button_press_event
1562 };
1563
1564 /* vim: set sw=4 et sts=4 : */