572114856502dd6c6b5226f43854bf0a3423c729
[debian/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 {
318 /* Could not find any predefined, let choose any available */
319 for (vol->master_element = snd_mixer_first_elem(vol->mixer);
320 vol->master_element != NULL;
321 vol->master_element = snd_mixer_elem_next(vol->master_element))
322 {
323 if (snd_mixer_selem_is_active(vol->master_element) &&
324 snd_mixer_selem_has_playback_volume(vol->master_element) &&
325 !snd_mixer_selem_has_capture_volume(vol->master_element) &&
326 !snd_mixer_selem_has_capture_switch(vol->master_element))
327 break;
328 }
329 if (vol->master_element == NULL)
330 return FALSE;
331 }
332 }
333
334 /* Set the playback volume range as we wish it. */
335 if ( ! vol->alsamixer_mapping)
336 snd_mixer_selem_set_playback_volume_range(vol->master_element, 0, 100);
337
338 /* Listen to events from ALSA. */
339 int n_fds = snd_mixer_poll_descriptors_count(vol->mixer);
340 struct pollfd * fds = g_new0(struct pollfd, n_fds);
341
342 vol->channels = g_new0(GIOChannel *, n_fds);
343 vol->watches = g_new0(guint, n_fds);
344 vol->num_channels = n_fds;
345
346 snd_mixer_poll_descriptors(vol->mixer, fds, n_fds);
347 int i;
348 for (i = 0; i < n_fds; ++i)
349 {
350 GIOChannel* channel = g_io_channel_unix_new(fds[i].fd);
351 vol->watches[i] = g_io_add_watch(channel, G_IO_IN | G_IO_HUP, asound_mixer_event, vol);
352 vol->channels[i] = channel;
353 }
354 g_free(fds);
355 #endif
356 return TRUE;
357 }
358
359 static void asound_deinitialize(VolumeALSAPlugin * vol)
360 {
361 #ifdef DISABLE_ALSA
362 if (vol->mixer_fd >= 0)
363 close(vol->mixer_fd);
364 vol->mixer_fd = -1;
365 #else
366 guint i;
367
368 if (vol->mixer_evt_idle != 0) {
369 g_source_remove(vol->mixer_evt_idle);
370 vol->mixer_evt_idle = 0;
371 }
372
373 for (i = 0; i < vol->num_channels; i++) {
374 g_source_remove(vol->watches[i]);
375 g_io_channel_shutdown(vol->channels[i], FALSE, NULL);
376 g_io_channel_unref(vol->channels[i]);
377 }
378 g_free(vol->channels);
379 g_free(vol->watches);
380 vol->channels = NULL;
381 vol->watches = NULL;
382 vol->num_channels = 0;
383
384 if (vol->mixer)
385 snd_mixer_close(vol->mixer);
386 vol->mixer = NULL;
387 vol->master_element = NULL;
388 #endif
389 }
390
391 /* Get the presence of the mute control from the sound system. */
392 static gboolean asound_has_mute(VolumeALSAPlugin * vol)
393 {
394 #ifdef DISABLE_ALSA
395 /* it's emulated with OSS */
396 return TRUE;
397 #else
398 return ((vol->master_element != NULL) ? snd_mixer_selem_has_playback_switch(vol->master_element) : FALSE);
399 #endif
400 }
401
402 /* Get the condition of the mute control from the sound system. */
403 static gboolean asound_is_muted(VolumeALSAPlugin * vol)
404 {
405 /* The switch is on if sound is not muted, and off if the sound is muted.
406 * Initialize so that the sound appears unmuted if the control does not exist. */
407 int value = 1;
408 #ifdef DISABLE_ALSA
409 StereoVolume levels;
410
411 ioctl(vol->mixer_fd, MIXER_READ(vol->master_channel), &levels.value);
412 value = (levels.left + levels.right) >> 1;
413 #else
414 if (vol->master_element != NULL)
415 snd_mixer_selem_get_playback_switch(vol->master_element, 0, &value);
416 #endif
417 return (value == 0);
418 }
419
420 #ifndef DISABLE_ALSA
421 static long lrint_dir(double x, int dir)
422 {
423 if (dir > 0)
424 return lrint(ceil(x));
425 else if (dir < 0)
426 return lrint(floor(x));
427 else
428 return lrint(x);
429 }
430
431 static inline gboolean use_linear_dB_scale(long dBmin, long dBmax)
432 {
433 return dBmax - dBmin <= MAX_LINEAR_DB_SCALE * 100;
434 }
435
436 static long get_normalized_volume(snd_mixer_elem_t *elem,
437 snd_mixer_selem_channel_id_t channel)
438 {
439 long min, max, value;
440 double normalized, min_norm;
441 int err;
442
443 err = snd_mixer_selem_get_playback_dB_range(elem, &min, &max);
444 if (err < 0 || min >= max) {
445 err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
446 if (err < 0 || min == max)
447 return 0;
448
449 err = snd_mixer_selem_get_playback_volume(elem, channel, &value);
450 if (err < 0)
451 return 0;
452
453 return lrint(100.0 * (value - min) / (double)(max - min));
454 }
455
456 err = snd_mixer_selem_get_playback_dB(elem, channel, &value);
457 if (err < 0)
458 return 0;
459
460 if (use_linear_dB_scale(min, max))
461 return lrint(100.0 * (value - min) / (double)(max - min));
462
463 normalized = exp10((value - max) / 6000.0);
464 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
465 min_norm = exp10((min - max) / 6000.0);
466 normalized = (normalized - min_norm) / (1 - min_norm);
467 }
468
469 return lrint(100.0 * normalized);
470 }
471 #endif
472
473 /* Get the volume from the sound system.
474 * This implementation returns the average of the Front Left and Front Right channels. */
475 static int asound_get_volume(VolumeALSAPlugin * vol)
476 {
477 #ifdef DISABLE_ALSA
478 StereoVolume levels;
479
480 ioctl(vol->mixer_fd, MIXER_READ(vol->master_channel), &levels.value);
481 return (levels.left + levels.right) >> 1;
482 #else
483 long aleft = 0;
484 long aright = 0;
485
486 if (vol->master_element != NULL)
487 {
488 if ( ! vol->alsamixer_mapping)
489 {
490 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, &aleft);
491 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, &aright);
492 }
493 else
494 {
495 aleft = get_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT);
496 aright = get_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT);
497 }
498 }
499 return (aleft + aright) >> 1;
500 #endif
501 }
502
503 #ifndef DISABLE_ALSA
504 static int set_normalized_volume(snd_mixer_elem_t *elem,
505 snd_mixer_selem_channel_id_t channel,
506 int vol,
507 int dir)
508 {
509 long min, max, value;
510 double min_norm, volume;
511 int err;
512
513 volume = vol / 100.0;
514
515 err = snd_mixer_selem_get_playback_dB_range(elem, &min, &max);
516 if (err < 0 || min >= max) {
517 err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
518 if (err < 0)
519 return err;
520
521 value = lrint_dir(volume * (max - min), dir) + min;
522 return snd_mixer_selem_set_playback_volume(elem, channel, value);
523 }
524
525 if (use_linear_dB_scale(min, max)) {
526 value = lrint_dir(volume * (max - min), dir) + min;
527 return snd_mixer_selem_set_playback_dB(elem, channel, value, dir);
528 }
529
530 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
531 min_norm = exp10((min - max) / 6000.0);
532 volume = volume * (1 - min_norm) + min_norm;
533 }
534 value = lrint_dir(6000.0 * log10(volume), dir) + max;
535
536 return snd_mixer_selem_set_playback_dB(elem, channel, value, dir);
537 }
538 #endif
539
540 /* Set the volume to the sound system.
541 * This implementation sets the Front Left and Front Right channels to the specified value. */
542 static void asound_set_volume(VolumeALSAPlugin * vol, int volume)
543 {
544 int dir = volume - asound_get_volume(vol);
545
546 /* Volume is set to the correct value already */
547 if (dir == 0)
548 return;
549
550 #ifdef DISABLE_ALSA
551 StereoVolume levels;
552
553 levels.left = levels.right = volume;
554 ioctl(vol->mixer_fd, MIXER_WRITE(vol->master_channel), &levels.value);
555 #else
556 if (vol->master_element != NULL)
557 {
558 if ( ! vol->alsamixer_mapping)
559 {
560 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, volume);
561 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, volume);
562 }
563 else
564 {
565 set_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, volume, dir);
566 set_normalized_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, volume, dir);
567 }
568 }
569 #endif
570 }
571
572 /*** Graphics ***/
573
574 static void volumealsa_lookup_current_icon(VolumeALSAPlugin * vol, gboolean mute, int level)
575 {
576 /* Change icon according to mute / volume */
577 const char* icon_panel="audio-volume-muted-panel";
578 const char* icon_fallback=ICONS_MUTE;
579 if (mute)
580 {
581 icon_panel = "audio-volume-muted-panel";
582 icon_fallback=ICONS_MUTE;
583 }
584 else if (level >= 66)
585 {
586 icon_panel = "audio-volume-high-panel";
587 icon_fallback=ICONS_VOLUME_HIGH;
588 }
589 else if (level >= 33)
590 {
591 icon_panel = "audio-volume-medium-panel";
592 icon_fallback=ICONS_VOLUME_MEDIUM;
593 }
594 else if (level > 0)
595 {
596 icon_panel = "audio-volume-low-panel";
597 icon_fallback=ICONS_VOLUME_LOW;
598 }
599
600 vol->icon_panel = icon_panel;
601 vol->icon_fallback = icon_fallback;
602 }
603
604 static void volumealsa_update_current_icon(VolumeALSAPlugin * vol, gboolean mute, int level)
605 {
606 /* Find suitable icon */
607 volumealsa_lookup_current_icon(vol, mute, level);
608
609 /* Change icon, fallback to default icon if theme doesn't exsit */
610 lxpanel_image_change_icon(vol->tray_icon, vol->icon_panel, vol->icon_fallback);
611
612 /* Display current level in tooltip. */
613 char * tooltip = g_strdup_printf("%s %d", _("Volume control"), level);
614 gtk_widget_set_tooltip_text(vol->plugin, tooltip);
615 g_free(tooltip);
616 }
617
618 /*
619 * Here we just update volume's vertical scale and mute check button.
620 * The rest will be updated by signal handelrs.
621 */
622 static void volumealsa_update_display(VolumeALSAPlugin * vol)
623 {
624 /* Mute. */
625 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), asound_is_muted(vol));
626 gtk_widget_set_sensitive(vol->mute_check, (asound_has_mute(vol)));
627
628 /* Volume. */
629 if (vol->volume_scale != NULL)
630 {
631 gtk_range_set_value(GTK_RANGE(vol->volume_scale), asound_get_volume(vol));
632 }
633 }
634
635 struct mixer_desc
636 {
637 char * cmd;
638 char * exec;
639 gboolean needs_pa;
640 gboolean needs_term;
641 };
642
643 const struct mixer_desc mixers[] = {
644 /* those with needs_pa should be first! */
645 { "gnome-sound-applet", "gnome-sound-applet", TRUE, FALSE },
646 { "pavucontrol", "pavucontrol", TRUE, FALSE },
647 { "gnome-alsamixer", "gnome-alsamixer", FALSE, FALSE },
648 { "alsamixergui", "alsamixergui", FALSE, FALSE },
649 { "alsamixer", "alsamixer", FALSE, TRUE },
650 { NULL }
651 };
652
653 static void volume_run_mixer(VolumeALSAPlugin * vol)
654 {
655 char *path = NULL;
656 const gchar *command_line = NULL;
657 GAppInfoCreateFlags flags = G_APP_INFO_CREATE_NONE;
658 int i;
659
660 /* check if command line was configured */
661 if (config_setting_lookup_string(vol->settings, "MixerCommand", &command_line))
662 if (config_setting_lookup_int(vol->settings, "MixerCommandTerm", &i) && i)
663 flags = G_APP_INFO_CREATE_NEEDS_TERMINAL;
664
665 /* if command isn't set in settings then let guess it */
666 if (command_line == NULL)
667 {
668 i = 0;
669 path = g_find_program_in_path("pulseaudio");
670 /* Assume that when pulseaudio is installed, it's launching every time */
671 if (path)
672 g_free(path);
673 /* Fallback to alsamixer when PA is not running, or when no PA utility is find */
674 else while (mixers[i].cmd && mixers[i].needs_pa)
675 i++;
676 for (; mixers[i].cmd; i++)
677 {
678 if ((path = g_find_program_in_path(mixers[i].exec)))
679 {
680 command_line = mixers[i].cmd;
681 if (mixers[i].needs_term)
682 flags = G_APP_INFO_CREATE_NEEDS_TERMINAL;
683 g_free(path);
684 break;
685 }
686 }
687 }
688
689 if (command_line)
690 {
691 fm_launch_command_simple(NULL, NULL, flags, command_line, NULL);
692 }
693 else
694 {
695 fm_show_error(NULL, NULL,
696 _("Error, you need to install an application to configure"
697 " the sound (pavucontrol, alsamixer ...)"));
698 }
699 }
700
701 static void _check_click(VolumeALSAPlugin * vol, int button, GdkModifierType mod)
702 {
703 if (vol->slider_click == button && vol->slider_click_mods == mod)
704 {
705 /* Left-click. Show or hide the popup window. */
706 if (vol->show_popup)
707 {
708 gtk_widget_hide(vol->popup_window);
709 vol->show_popup = FALSE;
710 }
711 else
712 {
713 gtk_widget_show_all(vol->popup_window);
714 vol->show_popup = TRUE;
715 }
716 }
717 if (vol->mute_click == button && vol->mute_click_mods == mod)
718 {
719 /* Middle-click. Toggle the mute status. */
720 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check)));
721 }
722 if (vol->mixer_click == button && vol->mixer_click_mods == mod)
723 {
724 volume_run_mixer(vol);
725 }
726 }
727
728 /* Handler for "button-press-event" signal on main widget. */
729 static gboolean volumealsa_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * panel)
730 {
731 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(widget);
732
733 if (event->button == 1)
734 {
735 _check_click(vol, 1,
736 event->state & gtk_accelerator_get_default_mod_mask());
737 }
738
739 return FALSE;
740 }
741
742 static gboolean volumealsa_button_release_event(GtkWidget * widget, GdkEventButton * event, VolumeALSAPlugin * vol)
743 {
744 if (event->button != 1)
745 {
746 _check_click(vol, event->button,
747 event->state & gtk_accelerator_get_default_mod_mask());
748 }
749 return FALSE;
750 }
751
752 /* Handler for "focus-out" signal on popup window. */
753 static gboolean volumealsa_popup_focus_out(GtkWidget * widget, GdkEvent * event, VolumeALSAPlugin * vol)
754 {
755 /* Hide the widget. */
756 gtk_widget_hide(vol->popup_window);
757 vol->show_popup = FALSE;
758 return FALSE;
759 }
760
761 /* Handler for "map" signal on popup window. */
762 static void volumealsa_popup_map(GtkWidget * widget, VolumeALSAPlugin * vol)
763 {
764 lxpanel_plugin_adjust_popup_position(widget, vol->plugin);
765 }
766
767 /* Handler for "value_changed" signal on popup window vertical scale. */
768 static void volumealsa_popup_scale_changed(GtkRange * range, VolumeALSAPlugin * vol)
769 {
770 int level = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
771 gboolean mute = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
772
773 /* Reflect the value of the control to the sound system. */
774 asound_set_volume(vol, level);
775
776 /*
777 * Redraw the controls.
778 * Scale and check button do not need to be updated, as these are always
779 * in sync with user's actions.
780 */
781 volumealsa_update_current_icon(vol, mute, level);
782 }
783
784 /* Handler for "scroll-event" signal on popup window vertical scale. */
785 static void volumealsa_popup_scale_scrolled(GtkScale * scale, GdkEventScroll * evt, VolumeALSAPlugin * vol)
786 {
787 /* Get the state of the vertical scale. */
788 gdouble val = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
789
790 /* Dispatch on scroll direction to update the value. */
791 if ((evt->direction == GDK_SCROLL_UP) || (evt->direction == GDK_SCROLL_LEFT))
792 val += 2;
793 else
794 val -= 2;
795
796 /* Reset the state of the vertical scale. This provokes a "value_changed" event. */
797 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP((int)val, 0, 100));
798 }
799
800 /* Handler for "toggled" signal on popup window mute checkbox. */
801 static void volumealsa_popup_mute_toggled(GtkWidget * widget, VolumeALSAPlugin * vol)
802 {
803 int level = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
804 gboolean mute = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
805
806 /* Reflect the mute toggle to the sound system. */
807 #ifdef DISABLE_ALSA
808 if (mute)
809 {
810 vol->vol_before_mute = level;
811 asound_set_volume(vol, 0);
812 }
813 else
814 {
815 asound_set_volume(vol, vol->vol_before_mute);
816 }
817 #else
818 if (vol->master_element != NULL)
819 {
820 int chn;
821 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++)
822 snd_mixer_selem_set_playback_switch(vol->master_element, chn, ((mute) ? 0 : 1));
823 }
824 #endif
825
826 /*
827 * Redraw the controls.
828 * Scale and check button do not need to be updated, as these are always
829 * in sync with user's actions.
830 */
831 volumealsa_update_current_icon(vol, mute, level);
832 }
833
834 /* Hotkeys handlers */
835 static void volume_up(const char *keystring, gpointer user_data)
836 {
837 VolumeALSAPlugin * vol = (VolumeALSAPlugin *)user_data;
838 int val = (int)gtk_range_get_value(GTK_RANGE(vol->volume_scale)) + 2;
839 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP(val, 0, 100));
840 }
841
842 static void volume_down(const char *keystring, gpointer user_data)
843 {
844 VolumeALSAPlugin * vol = (VolumeALSAPlugin *)user_data;
845 int val = (int)gtk_range_get_value(GTK_RANGE(vol->volume_scale)) - 2;
846 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP(val, 0, 100));
847 }
848
849 static void volume_mute(const char *keystring, gpointer user_data)
850 {
851 VolumeALSAPlugin * vol = (VolumeALSAPlugin *)user_data;
852 gboolean muted = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check));
853 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), !muted);
854 }
855
856 /* Build the window that appears when the top level widget is clicked. */
857 static void volumealsa_build_popup_window(GtkWidget *p)
858 {
859 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
860
861 /* Create a new window. */
862 vol->popup_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
863 gtk_window_set_decorated(GTK_WINDOW(vol->popup_window), FALSE);
864 gtk_container_set_border_width(GTK_CONTAINER(vol->popup_window), 5);
865 gtk_window_set_default_size(GTK_WINDOW(vol->popup_window), 80, 140);
866 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(vol->popup_window), TRUE);
867 gtk_window_set_skip_pager_hint(GTK_WINDOW(vol->popup_window), TRUE);
868 gtk_window_set_type_hint(GTK_WINDOW(vol->popup_window), GDK_WINDOW_TYPE_HINT_DIALOG);
869
870 /* Connect signals. */
871 g_signal_connect(G_OBJECT(vol->popup_window), "focus-out-event", G_CALLBACK(volumealsa_popup_focus_out), vol);
872 g_signal_connect(G_OBJECT(vol->popup_window), "map", G_CALLBACK(volumealsa_popup_map), vol);
873
874 /* Create a scrolled window as the child of the top level window. */
875 GtkWidget * scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
876 gtk_container_set_border_width (GTK_CONTAINER(scrolledwindow), 0);
877 gtk_widget_show(scrolledwindow);
878 gtk_container_add(GTK_CONTAINER(vol->popup_window), scrolledwindow);
879 gtk_widget_set_can_focus(scrolledwindow, FALSE);
880 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
881 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_NONE);
882
883 /* Create a viewport as the child of the scrolled window. */
884 GtkWidget * viewport = gtk_viewport_new(NULL, NULL);
885 gtk_container_add(GTK_CONTAINER(scrolledwindow), viewport);
886 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
887 gtk_widget_show(viewport);
888
889 /* Create a frame as the child of the viewport. */
890 GtkWidget * frame = gtk_frame_new(_("Volume"));
891 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
892 gtk_container_add(GTK_CONTAINER(viewport), frame);
893
894 /* Create a vertical box as the child of the frame. */
895 GtkWidget * box = gtk_vbox_new(FALSE, 0);
896 gtk_container_add(GTK_CONTAINER(frame), box);
897
898 /* Create a vertical scale as the child of the vertical box. */
899 vol->volume_scale = gtk_vscale_new(GTK_ADJUSTMENT(gtk_adjustment_new(100, 0, 100, 0, 0, 0)));
900 gtk_scale_set_draw_value(GTK_SCALE(vol->volume_scale), FALSE);
901 gtk_range_set_inverted(GTK_RANGE(vol->volume_scale), TRUE);
902 gtk_box_pack_start(GTK_BOX(box), vol->volume_scale, TRUE, TRUE, 0);
903
904 /* Value-changed and scroll-event signals. */
905 vol->volume_scale_handler = g_signal_connect(vol->volume_scale, "value-changed", G_CALLBACK(volumealsa_popup_scale_changed), vol);
906 g_signal_connect(vol->volume_scale, "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol);
907
908 /* Create a check button as the child of the vertical box. */
909 vol->mute_check = gtk_check_button_new_with_label(_("Mute"));
910 gtk_box_pack_end(GTK_BOX(box), vol->mute_check, FALSE, FALSE, 0);
911 vol->mute_check_handler = g_signal_connect(vol->mute_check, "toggled", G_CALLBACK(volumealsa_popup_mute_toggled), vol);
912 }
913
914 /* Plugin constructor. */
915 static GtkWidget *volumealsa_constructor(LXPanel *panel, config_setting_t *settings)
916 {
917 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
918 VolumeALSAPlugin * vol = g_new0(VolumeALSAPlugin, 1);
919 GtkWidget *p;
920 const char *tmp_str;
921
922 #ifndef DISABLE_ALSA
923 /* Read config necessary for proper initialization of ALSA. */
924 config_setting_lookup_int(settings, "UseAlsamixerVolumeMapping", &vol->alsamixer_mapping);
925 if (config_setting_lookup_string(settings, "MasterChannel", &tmp_str))
926 vol->master_channel = g_strdup(tmp_str);
927 if (!config_setting_lookup_int(settings, "CardNumber", &vol->used_device))
928 vol->used_device = -1;
929 #else
930 vol->master_channel = SOUND_MIXER_VOLUME;
931 if (config_setting_lookup_string(settings, "MasterChannel", &tmp_str))
932 {
933 if (strcmp(tmp_str, "PCM") == 0)
934 vol->master_channel = SOUND_MIXER_PCM;
935 else if (strcmp(tmp_str, "Headphone") == 0)
936 vol->master_channel = SOUND_MIXER_PHONEOUT;
937 }
938 #endif
939 if (config_setting_lookup_string(settings, "MuteButton", &tmp_str))
940 vol->mute_click = panel_config_click_parse(tmp_str, &vol->mute_click_mods);
941 else
942 vol->mute_click = 2; /* middle-click default */
943 if (config_setting_lookup_string(settings, "SliderButton", &tmp_str))
944 vol->slider_click = panel_config_click_parse(tmp_str, &vol->slider_click_mods);
945 else
946 vol->slider_click = 1; /* left-click default */
947 if (config_setting_lookup_string(settings, "MixerButton", &tmp_str))
948 vol->mixer_click = panel_config_click_parse(tmp_str, &vol->mixer_click_mods);
949 if (config_setting_lookup_string(settings, "VolumeUpKey", &tmp_str))
950 lxpanel_apply_hotkey(&vol->hotkey_up, tmp_str, volume_up, vol, FALSE);
951 if (config_setting_lookup_string(settings, "VolumeDownKey", &tmp_str))
952 lxpanel_apply_hotkey(&vol->hotkey_down, tmp_str, volume_down, vol, FALSE);
953 if (config_setting_lookup_string(settings, "VolumeMuteKey", &tmp_str))
954 lxpanel_apply_hotkey(&vol->hotkey_mute, tmp_str, volume_mute, vol, FALSE);
955
956 /* Initialize ALSA. If that fails, present nothing. */
957 if ( ! asound_initialize(vol))
958 {
959 volumealsa_destructor(vol);
960 return NULL;
961 }
962
963 /* Allocate top level widget and set into Plugin widget pointer. */
964 vol->panel = panel;
965 vol->plugin = p = gtk_event_box_new();
966 vol->settings = settings;
967 lxpanel_plugin_set_data(p, vol, volumealsa_destructor);
968 gtk_widget_set_tooltip_text(p, _("Volume control"));
969
970 /* Allocate icon as a child of top level. */
971 vol->tray_icon = lxpanel_image_new_for_icon(panel, "audio-volume-muted-panel",
972 -1, ICONS_MUTE);
973 gtk_container_add(GTK_CONTAINER(p), vol->tray_icon);
974
975 /* Initialize window to appear when icon clicked. */
976 volumealsa_build_popup_window(p);
977
978 /* Connect signals. */
979 g_signal_connect(G_OBJECT(p), "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol );
980 g_signal_connect(G_OBJECT(p), "button-release-event", G_CALLBACK(volumealsa_button_release_event), vol );
981
982 /* Update the display, show the widget, and return. */
983 volumealsa_update_display(vol);
984 volumealsa_update_current_icon(vol, asound_is_muted(vol), asound_get_volume(vol));
985 gtk_widget_show_all(p);
986 return p;
987 }
988
989 /* Plugin destructor. */
990 static void volumealsa_destructor(gpointer user_data)
991 {
992 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) user_data;
993
994 lxpanel_apply_hotkey(&vol->hotkey_up, NULL, NULL, NULL, FALSE);
995 lxpanel_apply_hotkey(&vol->hotkey_down, NULL, NULL, NULL, FALSE);
996 lxpanel_apply_hotkey(&vol->hotkey_mute, NULL, NULL, NULL, FALSE);
997
998 asound_deinitialize(vol);
999
1000 /* If the dialog box is open, dismiss it. */
1001 if (vol->popup_window != NULL)
1002 gtk_widget_destroy(vol->popup_window);
1003
1004 #ifndef DISABLE_ALSA
1005 if (vol->restart_idle)
1006 g_source_remove(vol->restart_idle);
1007
1008 g_free(vol->master_channel);
1009 #endif
1010
1011 /* Deallocate all memory. */
1012 g_free(vol);
1013 }
1014
1015 #ifndef DISABLE_ALSA
1016 static GtkListStore *alsa_make_channels_list(VolumeALSAPlugin *vol, int *active)
1017 {
1018 GtkListStore *list;
1019 GtkTreeIter iter;
1020 snd_mixer_selem_id_t *sid;
1021 snd_mixer_elem_t *elem;
1022 const char *name;
1023 int i;
1024
1025 snd_mixer_selem_id_alloca(&sid);
1026 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); /* desc, value */
1027 for (elem = snd_mixer_first_elem(vol->mixer), i = 0; elem != NULL;
1028 elem = snd_mixer_elem_next(elem), i++)
1029 {
1030 if (snd_mixer_selem_is_active(elem) &&
1031 snd_mixer_selem_has_playback_volume(elem) &&
1032 !snd_mixer_selem_has_capture_volume(elem) &&
1033 !snd_mixer_selem_has_capture_switch(elem))
1034 {
1035 snd_mixer_selem_get_id(elem, sid);
1036 name = snd_mixer_selem_id_get_name(sid);
1037 gtk_list_store_insert_with_values(list, &iter, i, 0, _(name),
1038 1, name, -1);
1039 if (elem == vol->master_element)
1040 *active = i;
1041 }
1042 }
1043 return list;
1044 }
1045
1046 static void card_selector_changed(GtkComboBox *card_selector, VolumeALSAPlugin *vol)
1047 {
1048 GtkTreeModel *model = gtk_combo_box_get_model(card_selector);
1049 GtkTreeIter iter;
1050 int old_card = vol->used_device;
1051 int i = gtk_combo_box_get_active(card_selector);
1052 char *old_channel;
1053
1054 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
1055 gtk_tree_model_get(model, &iter, 1, &vol->used_device, -1);
1056 asound_deinitialize(vol);
1057 if (!asound_initialize(vol))
1058 {
1059 /* could not change card with the same master channel, try default */
1060 old_channel = vol->master_channel;
1061 vol->master_channel = NULL;
1062 asound_deinitialize(vol);
1063 if (!asound_initialize(vol))
1064 {
1065 g_warning("could not set card to %d", vol->used_device);
1066 vol->master_channel = old_channel;
1067 vol->used_device = old_card;
1068 //FIXME: reset the selector back
1069 /* schedule to restart with old settings */
1070 if (vol->restart_idle == 0)
1071 vol->restart_idle = g_timeout_add_seconds(1, asound_restart, vol);
1072 return;
1073 }
1074 g_free(old_channel);
1075 config_group_set_string(vol->settings, "MasterChannel", NULL);
1076 }
1077 /* remember and apply selection */
1078 volumealsa_update_display(vol);
1079 config_group_set_int(vol->settings, "CardNumber", vol->used_device);
1080 /* rebuild channel selection list */
1081 i = -1;
1082 model = GTK_TREE_MODEL(alsa_make_channels_list(vol, &i));
1083 gtk_combo_box_set_model(GTK_COMBO_BOX(vol->channel_selector), model);
1084 gtk_combo_box_set_active(GTK_COMBO_BOX(vol->channel_selector), i);
1085 g_object_unref(model);
1086 }
1087 #endif
1088
1089 static void channel_selector_changed(GtkComboBox *channel_selector, VolumeALSAPlugin *vol)
1090 {
1091 GtkTreeModel *model = gtk_combo_box_get_model(channel_selector);
1092 GtkTreeIter iter;
1093 #ifdef DISABLE_ALSA
1094 int ch; /* channel index */
1095 #else
1096 char *ch; /* channel name */
1097 #endif
1098 int i = gtk_combo_box_get_active(channel_selector);
1099
1100 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
1101 gtk_tree_model_get(model, &iter, 1, &ch, -1);
1102 #ifdef DISABLE_ALSA
1103 config_group_set_int(vol->settings, "MasterChannel", ch);
1104 #else
1105 config_group_set_string(vol->settings, "MasterChannel", ch);
1106 asound_find_element(vol, (const char **)&ch, 1); //FIXME: is error possible?
1107 /* Set the playback volume range as we wish it. */
1108 if (!vol->alsamixer_mapping)
1109 snd_mixer_selem_set_playback_volume_range(vol->master_element, 0, 100);
1110 /* g_debug("MasterChannel changed: %s", ch); */
1111 g_free(vol->master_channel);
1112 #endif
1113 vol->master_channel = ch; /* just take it instead of alloc + free */
1114 volumealsa_update_display(vol);
1115 }
1116
1117 static void mixer_selector_changed(GtkComboBox *mixer_selector, VolumeALSAPlugin *vol)
1118 {
1119 GtkWidget *mixer_entry = gtk_bin_get_child(GTK_BIN(mixer_selector));
1120 const char *cmd, *set;
1121 GtkTreeModel *model;
1122 GtkTreeIter iter;
1123 int i;
1124
1125 i = gtk_combo_box_get_active(mixer_selector);
1126 if (i < 0)
1127 /* it was just editing */
1128 return;
1129 if (!config_setting_lookup_string(vol->settings, "MixerCommand", &set))
1130 set = NULL;
1131 cmd = gtk_entry_get_text((GtkEntry *)mixer_entry);
1132 if (set)
1133 {
1134 if (strcmp(set, cmd) == 0)
1135 /* not changed */
1136 return;
1137 }
1138 else if (gtk_combo_box_get_active(mixer_selector) == 0)
1139 /* it's left at default */
1140 return;
1141 model = gtk_combo_box_get_model(mixer_selector);
1142 gtk_tree_model_iter_nth_child(model, &iter, NULL, i);
1143 gtk_tree_model_get(model, &iter, 1, &i, -1);
1144 /* g_debug("new choice: %s needs_term=%d", cmd, i); */
1145 config_group_set_string(vol->settings, "MixerCommand", cmd);
1146 config_group_set_int(vol->settings, "MixerCommandTerm", i);
1147 }
1148
1149 struct mixer_selector_check_data
1150 {
1151 GtkComboBox *mixer_selector;
1152 const char *text;
1153 int needs_term;
1154 };
1155
1156 static gboolean mixer_selector_check(GtkTreeModel *model, GtkTreePath *path,
1157 GtkTreeIter *iter, gpointer user_data)
1158 {
1159 struct mixer_selector_check_data *data = user_data;
1160 char *cmd;
1161
1162 gtk_tree_model_get(model, iter, 0, &cmd, 1, &data->needs_term, -1);
1163 if (cmd && strcmp(cmd, data->text) == 0)
1164 {
1165 int *indices = gtk_tree_path_get_indices(path);
1166 gtk_combo_box_set_active(data->mixer_selector, indices[0]);
1167 g_free(cmd);
1168 return TRUE;
1169 }
1170 g_free(cmd);
1171 return FALSE;
1172 }
1173
1174 static gboolean mixer_selector_focus_out(GtkWidget *mixer_entry,
1175 GdkEvent *evt, VolumeALSAPlugin *vol)
1176 {
1177 struct mixer_selector_check_data data;
1178 GtkTreeModel *model;
1179
1180 data.mixer_selector = GTK_COMBO_BOX(gtk_widget_get_parent(mixer_entry));
1181 data.text = gtk_entry_get_text((GtkEntry *)mixer_entry);
1182 data.needs_term = 0;
1183 model = gtk_combo_box_get_model(data.mixer_selector);
1184
1185 /* check if current value is one of model choices */
1186 if (gtk_combo_box_get_active(data.mixer_selector) < 0)
1187 gtk_tree_model_foreach(model, &mixer_selector_check, &data);
1188 /* check executable and remember selection */
1189 if (gtk_combo_box_get_active(data.mixer_selector) < 0)
1190 {
1191 /* check only user input since predefined choices were tested already */
1192 char *exec, *path;
1193
1194 /* g_debug("user entered mixer: %s", data.text); */
1195 exec = strchr(data.text, ' ');
1196 if (exec)
1197 exec = g_strndup(data.text, exec - data.text);
1198 path = g_find_program_in_path(exec ? exec : data.text);
1199 g_free(exec);
1200 g_free(path);
1201 if (path == NULL)
1202 {
1203 /* invalid executable requested, ignore it then */
1204 g_warning("%s cannot be executed, ignoring it", data.text);
1205 return FALSE;
1206 }
1207 }
1208 config_group_set_string(vol->settings, "MixerCommand", data.text);
1209 config_group_set_int(vol->settings, "MixerCommandTerm", data.needs_term);
1210 return FALSE;
1211 }
1212
1213 static gboolean mixer_selector_key_press(GtkWidget *mixer_entry,
1214 GdkEventKey *evt, VolumeALSAPlugin *vol)
1215 {
1216 if (evt->keyval == GDK_KEY_Return)
1217 /* loose focus on Enter press */
1218 gtk_window_set_focus(GTK_WINDOW(gtk_widget_get_toplevel(mixer_entry)), NULL);
1219 return FALSE;
1220 }
1221
1222 static gboolean mute_button_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1223 {
1224 int n;
1225 GdkModifierType mods;
1226
1227 n = panel_config_click_parse(click, &mods);
1228 if (n == 0 || ((n != vol->mixer_click || mods != vol->mixer_click_mods) &&
1229 (n != vol->slider_click || mods != vol->slider_click_mods)))
1230 {
1231 config_group_set_string(vol->settings, "MuteButton", click);
1232 vol->mute_click = n;
1233 vol->mute_click_mods = mods;
1234 return TRUE;
1235 }
1236 //FIXME: show a message?
1237 return FALSE;
1238 }
1239
1240 static gboolean mixer_button_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1241 {
1242 int n;
1243 GdkModifierType mods;
1244
1245 n = panel_config_click_parse(click, &mods);
1246 if (n == 0 || ((n != vol->mute_click || mods != vol->mute_click_mods) &&
1247 (n != vol->slider_click || mods != vol->slider_click_mods)))
1248 {
1249 config_group_set_string(vol->settings, "MixerButton", click);
1250 vol->mixer_click = n;
1251 vol->mixer_click_mods = mods;
1252 return TRUE;
1253 }
1254 //FIXME: show a message?
1255 return FALSE;
1256 }
1257
1258 static gboolean volume_button_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1259 {
1260 int n;
1261 GdkModifierType mods;
1262
1263 n = panel_config_click_parse(click, &mods);
1264 if (n == 0 || ((n != vol->mixer_click || mods != vol->mixer_click_mods) &&
1265 (n != vol->mute_click || mods != vol->mute_click_mods)))
1266 {
1267 config_group_set_string(vol->settings, "SliderButton", click);
1268 vol->slider_click = n;
1269 vol->slider_click_mods = mods;
1270 return TRUE;
1271 }
1272 //FIXME: show a message?
1273 return FALSE;
1274 }
1275
1276 static gboolean up_key_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1277 {
1278 gboolean res;
1279
1280 res = lxpanel_apply_hotkey(&vol->hotkey_up, click, &volume_up, vol, TRUE);
1281 if (res)
1282 config_group_set_string(vol->settings, "VolumeUpKey", click);
1283 return res;
1284 }
1285
1286 static gboolean down_key_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1287 {
1288 gboolean res;
1289
1290 res = lxpanel_apply_hotkey(&vol->hotkey_down, click, &volume_down, vol, TRUE);
1291 if (res)
1292 config_group_set_string(vol->settings, "VolumeDownKey", click);
1293 return res;
1294 }
1295
1296 static gboolean mute_key_changed(GtkWidget *btn, char *click, VolumeALSAPlugin *vol)
1297 {
1298 gboolean res;
1299
1300 res = lxpanel_apply_hotkey(&vol->hotkey_mute, click, &volume_mute, vol, TRUE);
1301 if (res)
1302 config_group_set_string(vol->settings, "VolumeMuteKey", click);
1303 return res;
1304 }
1305
1306 #if THING_THAT_NEVER_HAPPEN
1307 /* Just to have these translated */
1308 N_("Line"), N_("LineOut"), N_("Front"), N_("Surround"), N_("Center"), N_("Speaker+LO");
1309 #endif
1310
1311 /* Callback when the configuration dialog is to be shown. */
1312 static GtkWidget *volumealsa_configure(LXPanel *panel, GtkWidget *p)
1313 {
1314 VolumeALSAPlugin *vol = lxpanel_plugin_get_data(p);
1315 const char *tmp_str;
1316 char *path;
1317 GtkListStore *list;
1318 GtkCellRenderer *column;
1319 #ifndef DISABLE_ALSA
1320 snd_mixer_selem_id_t *sid;
1321 snd_mixer_elem_t *elem;
1322 snd_hctl_t *hctl;
1323 GtkWidget *card_selector;
1324 #endif
1325 GtkWidget *mute_button;
1326 GtkWidget *volume_button;
1327 GtkWidget *mixer_button;
1328 GtkWidget *up_key;
1329 GtkWidget *down_key;
1330 GtkWidget *mute_key;
1331 GtkWidget *mixer_selector;
1332 GtkWidget *mixer_entry;
1333 GtkTreeIter iter;
1334 int active = 0;
1335 int i = 0;
1336 #ifndef DISABLE_ALSA
1337 int j = -1;
1338
1339 snd_mixer_selem_id_alloca(&sid);
1340 /* setup card selector */
1341 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); /* desc, num */
1342 if (snd_hctl_open(&hctl, "default", 0) == 0)
1343 {
1344 /* check if "default" isn't a HW, so include it in the list */
1345 if (snd_ctl_type(snd_hctl_ctl(hctl)) != 0)
1346 {
1347 gtk_list_store_insert_with_values(list, &iter, i++, 0, _("default"),
1348 1, j, -1);
1349 if (vol->used_device < 0)
1350 active = 0;
1351 }
1352 snd_hctl_close(hctl);
1353 }
1354 while (snd_card_next(&j) == 0 && j >= 0)
1355 {
1356 char *name = NULL;
1357 snd_mixer_t *mixer;
1358 char id[16];
1359
1360 if (snd_card_get_name(j, &name) == 0)
1361 {
1362 /* test if there any available channel */
1363 snprintf(id, sizeof(id), "hw:%d", j);
1364 snd_mixer_open(&mixer, 0);
1365 snd_mixer_attach(mixer, id);
1366 snd_mixer_selem_register(mixer, NULL, NULL);
1367 snd_mixer_load(mixer);
1368 for (elem = snd_mixer_first_elem(mixer); elem != NULL;
1369 elem = snd_mixer_elem_next(elem))
1370 {
1371 if (snd_mixer_selem_is_active(elem) &&
1372 snd_mixer_selem_has_playback_volume(elem) &&
1373 !snd_mixer_selem_has_capture_volume(elem) &&
1374 !snd_mixer_selem_has_capture_switch(elem))
1375 break;
1376 }
1377 snd_mixer_close(mixer);
1378 if (elem != NULL)
1379 {
1380 g_debug("found soundcard: %s", name);
1381 gtk_list_store_insert_with_values(list, &iter, i++, 0, name,
1382 1, j, -1);
1383 if (vol->used_device == j)
1384 active = i;
1385 }
1386 else
1387 g_debug("no elements in soundcard %s", name);
1388 free(name);
1389 }
1390 }
1391 card_selector = gtk_combo_box_new_with_model(GTK_TREE_MODEL(list));
1392 g_object_unref(list);
1393 /* gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(card_selector), 1); */
1394 column = gtk_cell_renderer_text_new();
1395 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(card_selector), column, TRUE);
1396 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(card_selector), column,
1397 "text", 0, NULL);
1398 gtk_combo_box_set_active(GTK_COMBO_BOX(card_selector), active);
1399 g_signal_connect(card_selector, "changed",
1400 G_CALLBACK(card_selector_changed), vol);
1401 g_signal_connect(card_selector, "scroll-event", G_CALLBACK(gtk_true), NULL);
1402 #endif
1403
1404 /* setup channel selector */
1405 #ifdef DISABLE_ALSA
1406 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); /* desc, index */
1407 gtk_list_store_insert_with_values(list, &iter, 0, 0, _("Master"),
1408 1, SOUND_MIXER_VOLUME, -1);
1409 if (vol->master_channel == SOUND_MIXER_VOLUME)
1410 active = 0;
1411 gtk_list_store_insert_with_values(list, &iter, 1, 0, _("PCM"),
1412 1, SOUND_MIXER_PCM, -1);
1413 if (vol->master_channel == SOUND_MIXER_VOLUME)
1414 active = 1;
1415 gtk_list_store_insert_with_values(list, &iter, 2, 0, _("Headphone"),
1416 1, SOUND_MIXER_PHONEOUT, -1);
1417 if (vol->master_channel == SOUND_MIXER_VOLUME)
1418 active = 2;
1419 #else
1420 list = alsa_make_channels_list(vol, &active);
1421 #endif
1422 vol->channel_selector = gtk_combo_box_new_with_model(GTK_TREE_MODEL(list));
1423 g_object_unref(list);
1424 /* gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(vol->channel_selector), 1); */
1425 column = gtk_cell_renderer_text_new();
1426 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(vol->channel_selector), column, TRUE);
1427 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(vol->channel_selector), column,
1428 "text", 0, NULL);
1429 gtk_combo_box_set_active(GTK_COMBO_BOX(vol->channel_selector), active);
1430 g_signal_connect(vol->channel_selector, "changed",
1431 G_CALLBACK(channel_selector_changed), vol);
1432 g_signal_connect(vol->channel_selector, "scroll-event", G_CALLBACK(gtk_true), NULL);
1433
1434 /* setup buttons */
1435 if (!config_setting_lookup_string(vol->settings, "SliderButton", &tmp_str))
1436 tmp_str = "1";
1437 volume_button = panel_config_click_button_new(_("Click for Volume Slider"), tmp_str);
1438 g_signal_connect(volume_button, "changed", G_CALLBACK(volume_button_changed), vol);
1439 if (!config_setting_lookup_string(vol->settings, "MuteButton", &tmp_str))
1440 tmp_str = "2";
1441 mute_button = panel_config_click_button_new(_("Click for Toggle Mute"), tmp_str);
1442 g_signal_connect(mute_button, "changed", G_CALLBACK(mute_button_changed), vol);
1443 if (!config_setting_lookup_string(vol->settings, "MixerButton", &tmp_str))
1444 tmp_str = NULL;
1445 mixer_button = panel_config_click_button_new(_("Click for Open Mixer"), tmp_str);
1446 g_signal_connect(mixer_button, "changed", G_CALLBACK(mixer_button_changed), vol);
1447
1448 /* setup hotkeys */
1449 up_key = panel_config_hotkey_button_new(_("Hotkey for Volume Up"), vol->hotkey_up);
1450 g_signal_connect(up_key, "changed", G_CALLBACK(up_key_changed), vol);
1451 down_key = panel_config_hotkey_button_new(_("Hotkey for Volume Down"), vol->hotkey_down);
1452 g_signal_connect(down_key, "changed", G_CALLBACK(down_key_changed), vol);
1453 mute_key = panel_config_hotkey_button_new(_("Hotkey for Volume Mute"), vol->hotkey_mute);
1454 g_signal_connect(mute_key, "changed", G_CALLBACK(mute_key_changed), vol);
1455
1456 /* setup mixer selector */
1457 if (!config_setting_lookup_string(vol->settings, "MixerCommand", &tmp_str))
1458 tmp_str = NULL;
1459 active = -1;
1460 i = j = 0;
1461 list = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT); /* line, needs_term */
1462 path = g_find_program_in_path("pulseaudio");
1463 if (path)
1464 g_free(path);
1465 else while (mixers[i].cmd && mixers[i].needs_pa)
1466 i++;
1467 for (; mixers[i].cmd; i++)
1468 {
1469 path = g_find_program_in_path(mixers[i].exec);
1470 if (path)
1471 {
1472 if (tmp_str && active < 0 && strcmp(tmp_str, mixers[i].cmd) == 0)
1473 {
1474 active = j;
1475 tmp_str = NULL;
1476 }
1477 gtk_list_store_insert_with_values(list, &iter, j++, 0, mixers[i].cmd,
1478 1, (int)mixers[i].needs_term,
1479 -1);
1480 g_free(path);
1481 }
1482 }
1483 if (tmp_str)
1484 {
1485 active = j;
1486 /* FIXME: support "needs terminal" for custom MixerCommand */
1487 gtk_list_store_insert_with_values(list, &iter, j, 0, tmp_str, 1, 0, -1);
1488 }
1489 if (active < 0)
1490 active = 0;
1491 #if GTK_CHECK_VERSION(2, 24, 0)
1492 mixer_selector = gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(list));
1493 gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(mixer_selector), 0);
1494 #else
1495 mixer_selector = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(list), 0);
1496 #endif
1497 g_object_unref(list);
1498 /* gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(mixer_selector), 1); */
1499 gtk_combo_box_set_active(GTK_COMBO_BOX(mixer_selector), active);
1500 mixer_entry = gtk_bin_get_child(GTK_BIN(mixer_selector));
1501 g_signal_connect(mixer_entry, "key-press-event",
1502 G_CALLBACK(mixer_selector_key_press), vol);
1503 g_signal_connect(mixer_selector, "changed",
1504 G_CALLBACK(mixer_selector_changed), vol);
1505 g_signal_connect(mixer_entry, "focus-out-event",
1506 G_CALLBACK(mixer_selector_focus_out), vol);
1507 g_signal_connect(mixer_selector, "scroll-event", G_CALLBACK(gtk_true), NULL);
1508
1509 return lxpanel_generic_config_dlg(_("Volume Control"), panel, NULL, p,
1510 #ifndef DISABLE_ALSA
1511 _("Audio Card"), NULL, CONF_TYPE_TRIM,
1512 "", card_selector, CONF_TYPE_EXTERNAL,
1513 #endif
1514 _("Channel to Operate"), NULL, CONF_TYPE_TRIM,
1515 "", vol->channel_selector, CONF_TYPE_EXTERNAL,
1516 "", volume_button, CONF_TYPE_EXTERNAL,
1517 "", mute_button, CONF_TYPE_EXTERNAL,
1518 "", mixer_button, CONF_TYPE_EXTERNAL,
1519 "", up_key, CONF_TYPE_EXTERNAL,
1520 "", down_key, CONF_TYPE_EXTERNAL,
1521 "", mute_key, CONF_TYPE_EXTERNAL,
1522 _("Command to Open Mixer"), NULL, CONF_TYPE_TRIM,
1523 "", mixer_selector, CONF_TYPE_EXTERNAL,
1524 NULL);
1525 }
1526
1527 /* Callback when panel configuration changes. */
1528 static void volumealsa_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
1529 {
1530 /* Do a full redraw. */
1531 volumealsa_update_display(lxpanel_plugin_get_data(p));
1532 }
1533
1534 static gboolean volumealsa_update_context_menu(GtkWidget *plugin, GtkMenu *menu)
1535 {
1536 GtkWidget *img = gtk_image_new_from_stock("gtk-directory", GTK_ICON_SIZE_MENU);
1537 GtkWidget *menu_item = gtk_image_menu_item_new_with_label(_("Launch Mixer"));
1538 //FIXME: precheck and disable if MixerCommand not set
1539 gtk_image_menu_item_set_image((GtkImageMenuItem *)menu_item, img);
1540 g_signal_connect_swapped(menu_item, "activate", G_CALLBACK(volume_run_mixer),
1541 lxpanel_plugin_get_data(plugin));
1542 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
1543 return FALSE;
1544 }
1545
1546 #ifndef DISABLE_ALSA
1547 static LXPanelPluginInit _volumealsa_init = {
1548 .name = N_("Volume Control"),
1549 .description = N_("Display and control volume"),
1550
1551 .superseded = TRUE,
1552 .new_instance = volumealsa_constructor,
1553 .config = volumealsa_configure,
1554 .reconfigure = volumealsa_panel_configuration_changed,
1555 .update_context_menu = volumealsa_update_context_menu,
1556 .button_press_event = volumealsa_button_press_event
1557 };
1558
1559 static void volumealsa_init(void)
1560 {
1561 lxpanel_register_plugin_type("volumealsa", &_volumealsa_init);
1562 }
1563 #endif
1564
1565 FM_DEFINE_MODULE(lxpanel_gtk, volume)
1566
1567 /* Plugin descriptor. */
1568 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
1569 .name = N_("Volume Control"),
1570 .description = N_("Display and control volume"),
1571 #ifndef DISABLE_ALSA
1572 .init = volumealsa_init,
1573 #endif
1574
1575 .new_instance = volumealsa_constructor,
1576 .config = volumealsa_configure,
1577 .reconfigure = volumealsa_panel_configuration_changed,
1578 .update_context_menu = volumealsa_update_context_menu,
1579 .button_press_event = volumealsa_button_press_event
1580 };
1581
1582 /* vim: set sw=4 et sts=4 : */