Fix incomplete resources deallocation in 'volumealsa' plugin.
[lxde/lxpanel.git] / plugins / volumealsa / volumealsa.c
1 /**
2 * Copyright (c) 2008-2014 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <gtk/gtk.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <alsa/asoundlib.h>
27 #include <poll.h>
28 #include <libfm/fm-gtk.h>
29 #include "plugin.h"
30
31 #define ICONS_VOLUME_HIGH PACKAGE_DATA_DIR "/images/volume-high.png"
32 #define ICONS_VOLUME_MEDIUM PACKAGE_DATA_DIR "/images/volume-medium.png"
33 #define ICONS_VOLUME_LOW PACKAGE_DATA_DIR "/images/volume-low.png"
34 #define ICONS_MUTE PACKAGE_DATA_DIR "/images/mute.png"
35
36 typedef struct {
37
38 /* Graphics. */
39 GtkWidget * plugin; /* Back pointer to the widget */
40 LXPanel * panel; /* Back pointer to panel */
41 config_setting_t * settings; /* Plugin settings */
42 GtkWidget * tray_icon; /* Displayed image */
43 GtkWidget * popup_window; /* Top level window for popup */
44 GtkWidget * volume_scale; /* Scale for volume */
45 GtkWidget * mute_check; /* Checkbox for mute state */
46 gboolean show_popup; /* Toggle to show and hide the popup on left click */
47 guint volume_scale_handler; /* Handler for vscale widget */
48 guint mute_check_handler; /* Handler for mute_check widget */
49
50 /* ALSA interface. */
51 snd_mixer_t * mixer; /* The mixer */
52 snd_mixer_selem_id_t * sid; /* The element ID */
53 snd_mixer_elem_t * master_element; /* The Master element */
54 guint mixer_evt_idle; /* Timer to handle restarting poll */
55 guint restart_idle;
56
57 /* unloading and error handling */
58 GIOChannel **channels; /* Channels that we listen to */
59 guint num_channels; /* Number of channels */
60
61 /* Icons */
62 const char* icon;
63 const char* icon_panel;
64 const char* icon_fallback;
65
66 } VolumeALSAPlugin;
67
68 static gboolean asound_restart(gpointer vol_gpointer);
69 static gboolean asound_initialize(VolumeALSAPlugin * vol);
70 static void asound_deinitialize(VolumeALSAPlugin * vol);
71 static void volumealsa_update_display(VolumeALSAPlugin * vol);
72 static void volumealsa_destructor(gpointer user_data);
73
74 /*** ALSA ***/
75
76 static gboolean asound_find_element(VolumeALSAPlugin * vol, const char * ename)
77 {
78 for (
79 vol->master_element = snd_mixer_first_elem(vol->mixer);
80 vol->master_element != NULL;
81 vol->master_element = snd_mixer_elem_next(vol->master_element))
82 {
83 snd_mixer_selem_get_id(vol->master_element, vol->sid);
84 if ((snd_mixer_selem_is_active(vol->master_element))
85 && (strcmp(ename, snd_mixer_selem_id_get_name(vol->sid)) == 0))
86 return TRUE;
87 }
88 return FALSE;
89 }
90
91 /* NOTE by PCMan:
92 * This is magic! Since ALSA uses its own machanism to handle this part.
93 * After polling of mixer fds, it requires that we should call
94 * snd_mixer_handle_events to clear all pending mixer events.
95 * However, when using the glib IO channels approach, we don't have
96 * poll() and snd_mixer_poll_descriptors_revents(). Due to the design of
97 * glib, on_mixer_event() will be called for every fd whose status was
98 * changed. So, after each poll(), it's called for several times,
99 * not just once. Therefore, we cannot call snd_mixer_handle_events()
100 * directly in the event handler. Otherwise, it will get called for
101 * several times, which might clear unprocessed pending events in the queue.
102 * So, here we call it once in the event callback for the first fd.
103 * Then, we don't call it for the following fds. After all fds with changed
104 * status are handled, we remove this restriction in an idle handler.
105 * The next time the event callback is involked for the first fs, we can
106 * call snd_mixer_handle_events() again. Racing shouldn't happen here
107 * because the idle handler has the same priority as the io channel callback.
108 * So, io callbacks for future pending events should be in the next gmain
109 * iteration, and won't be affected.
110 */
111
112 static gboolean asound_reset_mixer_evt_idle(VolumeALSAPlugin * vol)
113 {
114 if (!g_source_is_destroyed(g_main_current_source()))
115 vol->mixer_evt_idle = 0;
116 return FALSE;
117 }
118
119 /* Handler for I/O event on ALSA channel. */
120 static gboolean asound_mixer_event(GIOChannel * channel, GIOCondition cond, gpointer vol_gpointer)
121 {
122 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) vol_gpointer;
123 int res = 0;
124
125 if (vol->mixer_evt_idle == 0)
126 {
127 vol->mixer_evt_idle = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) asound_reset_mixer_evt_idle, vol, NULL);
128 res = snd_mixer_handle_events(vol->mixer);
129 }
130
131 if (cond & G_IO_IN)
132 {
133 /* the status of mixer is changed. update of display is needed. */
134 volumealsa_update_display(vol);
135 }
136
137 if ((cond & G_IO_HUP) || (res < 0))
138 {
139 /* This means there're some problems with alsa. */
140 g_warning("volumealsa: ALSA (or pulseaudio) had a problem: "
141 "volumealsa: snd_mixer_handle_events() = %d,"
142 " cond 0x%x (IN: 0x%x, HUP: 0x%x).", res, cond,
143 G_IO_IN, G_IO_HUP);
144 gtk_widget_set_tooltip_text(vol->plugin, "ALSA (or pulseaudio) had a problem."
145 " Please check the lxpanel logs.");
146
147 if (vol->restart_idle == 0)
148 vol->restart_idle = g_timeout_add_seconds(1, asound_restart, vol);
149
150 return FALSE;
151 }
152
153 return TRUE;
154 }
155
156 static gboolean asound_restart(gpointer vol_gpointer)
157 {
158 VolumeALSAPlugin * vol = vol_gpointer;
159
160 if (g_source_is_destroyed(g_main_current_source()))
161 return FALSE;
162
163 asound_deinitialize(vol);
164
165 if (!asound_initialize(vol)) {
166 g_warning("volumealsa: Re-initialization failed.");
167 return TRUE; // try again in a second
168 }
169
170 g_warning("volumealsa: Restarted ALSA interface...");
171
172 vol->restart_idle = 0;
173 return FALSE;
174 }
175
176 /* Initialize the ALSA interface. */
177 static gboolean asound_initialize(VolumeALSAPlugin * vol)
178 {
179 /* Access the "default" device. */
180 snd_mixer_selem_id_alloca(&vol->sid);
181 snd_mixer_open(&vol->mixer, 0);
182 snd_mixer_attach(vol->mixer, "default");
183 snd_mixer_selem_register(vol->mixer, NULL, NULL);
184 snd_mixer_load(vol->mixer);
185
186 /* Find Master element, or Front element, or PCM element, or LineOut element.
187 * If one of these succeeds, master_element is valid. */
188 if ( ! asound_find_element(vol, "Master"))
189 if ( ! asound_find_element(vol, "Front"))
190 if ( ! asound_find_element(vol, "PCM"))
191 if ( ! asound_find_element(vol, "LineOut"))
192 return FALSE;
193
194 /* Set the playback volume range as we wish it. */
195 snd_mixer_selem_set_playback_volume_range(vol->master_element, 0, 100);
196
197 /* Listen to events from ALSA. */
198 int n_fds = snd_mixer_poll_descriptors_count(vol->mixer);
199 struct pollfd * fds = g_new0(struct pollfd, n_fds);
200
201 vol->channels = g_new0(GIOChannel *, n_fds);
202 vol->num_channels = n_fds;
203
204 snd_mixer_poll_descriptors(vol->mixer, fds, n_fds);
205 int i;
206 for (i = 0; i < n_fds; ++i)
207 {
208 GIOChannel* channel = g_io_channel_unix_new(fds[i].fd);
209 g_io_add_watch(channel, G_IO_IN | G_IO_HUP, asound_mixer_event, vol);
210 vol->channels[i] = channel;
211 }
212 g_free(fds);
213 return TRUE;
214 }
215
216 static void asound_deinitialize(VolumeALSAPlugin * vol)
217 {
218 guint i;
219
220 if (vol->mixer_evt_idle != 0) {
221 g_source_remove(vol->mixer_evt_idle);
222 vol->mixer_evt_idle = 0;
223 }
224
225 for (i = 0; i < vol->num_channels; i++) {
226 g_io_channel_shutdown(vol->channels[i], FALSE, NULL);
227 g_io_channel_unref(vol->channels[i]);
228 }
229 g_free(vol->channels);
230 vol->channels = NULL;
231 vol->num_channels = 0;
232
233 snd_mixer_close(vol->mixer);
234 vol->master_element = NULL;
235 /* FIXME: unalloc vol->sid */
236 }
237
238 /* Get the presence of the mute control from the sound system. */
239 static gboolean asound_has_mute(VolumeALSAPlugin * vol)
240 {
241 return ((vol->master_element != NULL) ? snd_mixer_selem_has_playback_switch(vol->master_element) : FALSE);
242 }
243
244 /* Get the condition of the mute control from the sound system. */
245 static gboolean asound_is_muted(VolumeALSAPlugin * vol)
246 {
247 /* The switch is on if sound is not muted, and off if the sound is muted.
248 * Initialize so that the sound appears unmuted if the control does not exist. */
249 int value = 1;
250 if (vol->master_element != NULL)
251 snd_mixer_selem_get_playback_switch(vol->master_element, 0, &value);
252 return (value == 0);
253 }
254
255 /* Get the volume from the sound system.
256 * This implementation returns the average of the Front Left and Front Right channels. */
257 static int asound_get_volume(VolumeALSAPlugin * vol)
258 {
259 long aleft = 0;
260 long aright = 0;
261 if (vol->master_element != NULL)
262 {
263 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, &aleft);
264 snd_mixer_selem_get_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, &aright);
265 }
266 return (aleft + aright) >> 1;
267 }
268
269 /* Set the volume to the sound system.
270 * This implementation sets the Front Left and Front Right channels to the specified value. */
271 static void asound_set_volume(VolumeALSAPlugin * vol, int volume)
272 {
273 if (vol->master_element != NULL)
274 {
275 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_LEFT, volume);
276 snd_mixer_selem_set_playback_volume(vol->master_element, SND_MIXER_SCHN_FRONT_RIGHT, volume);
277 }
278 }
279
280 /*** Graphics ***/
281
282 static void volumealsa_update_current_icon(VolumeALSAPlugin * vol)
283 {
284 /* Mute status. */
285 gboolean mute = asound_is_muted(vol);
286 int level = asound_get_volume(vol);
287
288 /* Change icon according to mute / volume */
289 const char* icon="audio-volume-muted";
290 const char* icon_panel="audio-volume-muted-panel";
291 const char* icon_fallback=ICONS_MUTE;
292 if (mute)
293 {
294 icon_panel = "audio-volume-muted-panel";
295 icon="audio-volume-muted";
296 icon_fallback=ICONS_MUTE;
297 }
298 else if (level >= 75)
299 {
300 icon_panel = "audio-volume-high-panel";
301 icon="audio-volume-high";
302 icon_fallback=ICONS_VOLUME_HIGH;
303 }
304 else if (level >= 50)
305 {
306 icon_panel = "audio-volume-medium-panel";
307 icon="audio-volume-medium";
308 icon_fallback=ICONS_VOLUME_MEDIUM;
309 }
310 else if (level > 0)
311 {
312 icon_panel = "audio-volume-low-panel";
313 icon="audio-volume-low";
314 icon_fallback=ICONS_VOLUME_LOW;
315 }
316
317 vol->icon_panel = icon_panel;
318 vol->icon = icon;
319 vol->icon_fallback= icon_fallback;
320 }
321
322 /* Do a full redraw of the display. */
323 static void volumealsa_update_display(VolumeALSAPlugin * vol)
324 {
325 /* Mute status. */
326 gboolean mute = asound_is_muted(vol);
327 int level = asound_get_volume(vol);
328
329 volumealsa_update_current_icon(vol);
330
331 /* Change icon, fallback to default icon if theme doesn't exsit */
332 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon_panel))
333 {
334 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon))
335 {
336 lxpanel_image_set_from_file(vol->panel, vol->tray_icon, vol->icon_fallback);
337 }
338 }
339
340 g_signal_handler_block(vol->mute_check, vol->mute_check_handler);
341 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), mute);
342 gtk_widget_set_sensitive(vol->mute_check, asound_has_mute(vol));
343 g_signal_handler_unblock(vol->mute_check, vol->mute_check_handler);
344
345 /* Volume. */
346 if (vol->volume_scale != NULL)
347 {
348 g_signal_handler_block(vol->volume_scale, vol->volume_scale_handler);
349 gtk_range_set_value(GTK_RANGE(vol->volume_scale), asound_get_volume(vol));
350 g_signal_handler_unblock(vol->volume_scale, vol->volume_scale_handler);
351 }
352
353 /* Display current level in tooltip. */
354 char * tooltip = g_strdup_printf("%s %d", _("Volume control"), level);
355 gtk_widget_set_tooltip_text(vol->plugin, tooltip);
356 g_free(tooltip);
357 }
358
359
360 /* Handler for "button-press-event" signal on main widget. */
361 static gboolean volumealsa_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * panel)
362 {
363 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(widget);
364
365 /* Left-click. Show or hide the popup window. */
366 if (event->button == 1)
367 {
368 if (vol->show_popup)
369 {
370 gtk_widget_hide(vol->popup_window);
371 vol->show_popup = FALSE;
372 }
373 else
374 {
375 gtk_widget_show_all(vol->popup_window);
376 vol->show_popup = TRUE;
377 }
378 }
379
380 /* Middle-click. Toggle the mute status. */
381 else if (event->button == 2)
382 {
383 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vol->mute_check), ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vol->mute_check)));
384 }
385 return TRUE;
386 }
387
388 /* Handler for "focus-out" signal on popup window. */
389 static gboolean volumealsa_popup_focus_out(GtkWidget * widget, GdkEvent * event, VolumeALSAPlugin * vol)
390 {
391 /* Hide the widget. */
392 gtk_widget_hide(vol->popup_window);
393 vol->show_popup = FALSE;
394 return FALSE;
395 }
396
397 /* Handler for "map" signal on popup window. */
398 static void volumealsa_popup_map(GtkWidget * widget, VolumeALSAPlugin * vol)
399 {
400 lxpanel_plugin_adjust_popup_position(widget, vol->plugin);
401 }
402
403 static void volumealsa_theme_change(GtkWidget * widget, VolumeALSAPlugin * vol)
404 {
405 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon_panel))
406 {
407 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon))
408 {
409 lxpanel_image_set_from_file(vol->panel, vol->tray_icon, vol->icon_fallback);
410 }
411 }
412 }
413
414 /* Handler for "value_changed" signal on popup window vertical scale. */
415 static void volumealsa_popup_scale_changed(GtkRange * range, VolumeALSAPlugin * vol)
416 {
417 /* Reflect the value of the control to the sound system. */
418 asound_set_volume(vol, gtk_range_get_value(range));
419
420 /* Redraw the controls. */
421 volumealsa_update_display(vol);
422 }
423
424 /* Handler for "scroll-event" signal on popup window vertical scale. */
425 static void volumealsa_popup_scale_scrolled(GtkScale * scale, GdkEventScroll * evt, VolumeALSAPlugin * vol)
426 {
427 /* Get the state of the vertical scale. */
428 gdouble val = gtk_range_get_value(GTK_RANGE(vol->volume_scale));
429
430 /* Dispatch on scroll direction to update the value. */
431 if ((evt->direction == GDK_SCROLL_UP) || (evt->direction == GDK_SCROLL_LEFT))
432 val += 2;
433 else
434 val -= 2;
435
436 /* Reset the state of the vertical scale. This provokes a "value_changed" event. */
437 gtk_range_set_value(GTK_RANGE(vol->volume_scale), CLAMP((int)val, 0, 100));
438 }
439
440 /* Handler for "toggled" signal on popup window mute checkbox. */
441 static void volumealsa_popup_mute_toggled(GtkWidget * widget, VolumeALSAPlugin * vol)
442 {
443 /* Get the state of the mute toggle. */
444 gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
445
446 /* Reflect the mute toggle to the sound system. */
447 if (vol->master_element != NULL)
448 {
449 int chn;
450 for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++)
451 snd_mixer_selem_set_playback_switch(vol->master_element, chn, ((active) ? 0 : 1));
452 }
453
454 /* Redraw the controls. */
455 volumealsa_update_display(vol);
456 }
457
458 /* Build the window that appears when the top level widget is clicked. */
459 static void volumealsa_build_popup_window(GtkWidget *p)
460 {
461 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
462
463 /* Create a new window. */
464 vol->popup_window = gtk_window_new(GTK_WINDOW_POPUP);
465 gtk_window_set_decorated(GTK_WINDOW(vol->popup_window), FALSE);
466 gtk_container_set_border_width(GTK_CONTAINER(vol->popup_window), 5);
467 gtk_window_set_default_size(GTK_WINDOW(vol->popup_window), 80, 140);
468 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(vol->popup_window), TRUE);
469 gtk_window_set_skip_pager_hint(GTK_WINDOW(vol->popup_window), TRUE);
470 gtk_window_set_type_hint(GTK_WINDOW(vol->popup_window), GDK_WINDOW_TYPE_HINT_UTILITY);
471
472 /* Connect signals. */
473 g_signal_connect(G_OBJECT(vol->popup_window), "focus-out-event", G_CALLBACK(volumealsa_popup_focus_out), vol);
474 g_signal_connect(G_OBJECT(vol->popup_window), "map", G_CALLBACK(volumealsa_popup_map), vol);
475
476 /* Create a scrolled window as the child of the top level window. */
477 GtkWidget * scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
478 gtk_container_set_border_width (GTK_CONTAINER(scrolledwindow), 0);
479 gtk_widget_show(scrolledwindow);
480 gtk_container_add(GTK_CONTAINER(vol->popup_window), scrolledwindow);
481 gtk_widget_set_can_focus(scrolledwindow, FALSE);
482 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
483 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_NONE);
484
485 /* Create a viewport as the child of the scrolled window. */
486 GtkWidget * viewport = gtk_viewport_new(NULL, NULL);
487 gtk_container_add(GTK_CONTAINER(scrolledwindow), viewport);
488 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
489 gtk_widget_show(viewport);
490
491 /* Create a frame as the child of the viewport. */
492 GtkWidget * frame = gtk_frame_new(_("Volume"));
493 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
494 gtk_container_add(GTK_CONTAINER(viewport), frame);
495
496 /* Create a vertical box as the child of the frame. */
497 GtkWidget * box = gtk_vbox_new(FALSE, 0);
498 gtk_container_add(GTK_CONTAINER(frame), box);
499
500 /* Create a vertical scale as the child of the vertical box. */
501 vol->volume_scale = gtk_vscale_new(GTK_ADJUSTMENT(gtk_adjustment_new(100, 0, 100, 0, 0, 0)));
502 gtk_scale_set_draw_value(GTK_SCALE(vol->volume_scale), FALSE);
503 gtk_range_set_inverted(GTK_RANGE(vol->volume_scale), TRUE);
504 gtk_box_pack_start(GTK_BOX(box), vol->volume_scale, TRUE, TRUE, 0);
505
506 /* Value-changed and scroll-event signals. */
507 vol->volume_scale_handler = g_signal_connect(vol->volume_scale, "value-changed", G_CALLBACK(volumealsa_popup_scale_changed), vol);
508 g_signal_connect(vol->volume_scale, "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol);
509
510 /* Create a check button as the child of the vertical box. */
511 vol->mute_check = gtk_check_button_new_with_label(_("Mute"));
512 gtk_box_pack_end(GTK_BOX(box), vol->mute_check, FALSE, FALSE, 0);
513 vol->mute_check_handler = g_signal_connect(vol->mute_check, "toggled", G_CALLBACK(volumealsa_popup_mute_toggled), vol);
514
515 /* Set background to default. */
516 gtk_widget_set_style(viewport, panel_get_defstyle(vol->panel));
517 }
518
519 /* Plugin constructor. */
520 static GtkWidget *volumealsa_constructor(LXPanel *panel, config_setting_t *settings)
521 {
522 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
523 VolumeALSAPlugin * vol = g_new0(VolumeALSAPlugin, 1);
524 GtkWidget *p;
525
526 /* Initialize ALSA. If that fails, present nothing. */
527 if ( ! asound_initialize(vol))
528 {
529 volumealsa_destructor(vol);
530 return NULL;
531 }
532
533 /* Allocate top level widget and set into Plugin widget pointer. */
534 vol->panel = panel;
535 vol->plugin = p = gtk_event_box_new();
536 vol->settings = settings;
537 lxpanel_plugin_set_data(p, vol, volumealsa_destructor);
538 gtk_widget_add_events(p, GDK_BUTTON_PRESS_MASK);
539 gtk_widget_set_tooltip_text(p, _("Volume control"));
540
541 /* Allocate icon as a child of top level. */
542 vol->tray_icon = gtk_image_new();
543 gtk_container_add(GTK_CONTAINER(p), vol->tray_icon);
544
545 /* Initialize window to appear when icon clicked. */
546 volumealsa_build_popup_window(p);
547
548 /* Connect signals. */
549 g_signal_connect(G_OBJECT(p), "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol );
550 g_signal_connect(panel_get_icon_theme(panel), "changed", G_CALLBACK(volumealsa_theme_change), vol );
551
552 /* Update the display, show the widget, and return. */
553 volumealsa_update_display(vol);
554 gtk_widget_show_all(p);
555 return p;
556 }
557
558 /* Plugin destructor. */
559 static void volumealsa_destructor(gpointer user_data)
560 {
561 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) user_data;
562
563 asound_deinitialize(vol);
564
565 /* If the dialog box is open, dismiss it. */
566 if (vol->popup_window != NULL)
567 gtk_widget_destroy(vol->popup_window);
568
569 if (vol->restart_idle)
570 g_source_remove(vol->restart_idle);
571
572 g_signal_handlers_disconnect_by_func(panel_get_icon_theme(vol->panel),
573 volumealsa_theme_change, vol);
574
575 /* Deallocate all memory. */
576 g_free(vol);
577 }
578
579 /* Callback when the configuration dialog is to be shown. */
580
581 static GtkWidget *volumealsa_configure(LXPanel *panel, GtkWidget *p)
582 {
583 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
584 char *path = NULL;
585 const gchar *command_line = NULL;
586
587 /* FIXME: configure settings! */
588 /* check if command line was configured */
589 config_setting_lookup_string(vol->settings, "MixerCommand", &command_line);
590
591 /* if command isn't set in settings then let guess it */
592 if (command_line == NULL && (path = g_find_program_in_path("pulseaudio")))
593 {
594 g_free(path);
595 /* Assume that when pulseaudio is installed, it's launching every time */
596 if ((path = g_find_program_in_path("gnome-sound-applet")))
597 {
598 command_line = "gnome-sound-applet";
599 }
600 else if ((path = g_find_program_in_path("pavucontrol")))
601 {
602 command_line = "pavucontrol";
603 }
604 }
605
606 /* Fallback to alsamixer when PA is not running, or when no PA utility is find */
607 if (command_line == NULL)
608 {
609 if ((path = g_find_program_in_path("gnome-alsamixer")))
610 {
611 command_line = "gnome-alsamixer";
612 }
613 else if ((path = g_find_program_in_path("alsamixergui")))
614 {
615 command_line = "alsamixergui";
616 }
617 else if ((path = g_find_program_in_path("alsamixer")))
618 {
619 g_free(path);
620 if ((path = g_find_program_in_path("xterm")))
621 {
622 command_line = "xterm -e alsamixer";
623 }
624 }
625 }
626 g_free(path);
627
628 if (command_line)
629 {
630 fm_launch_command_simple(NULL, NULL, G_APP_INFO_CREATE_NONE,
631 command_line, NULL);
632 }
633 else
634 {
635 fm_show_error(NULL, NULL,
636 _("Error, you need to install an application to configure"
637 " the sound (pavucontrol, alsamixer ...)"));
638 }
639
640 return NULL;
641 }
642
643 /* Callback when panel configuration changes. */
644 static void volumealsa_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
645 {
646 /* Do a full redraw. */
647 volumealsa_update_display(lxpanel_plugin_get_data(p));
648 }
649
650 FM_DEFINE_MODULE(lxpanel_gtk, volumealsa)
651
652 /* Plugin descriptor. */
653 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
654 .name = N_("Volume Control"),
655 .description = N_("Display and control volume for ALSA"),
656
657 .new_instance = volumealsa_constructor,
658 .config = volumealsa_configure,
659 .reconfigure = volumealsa_panel_configuration_changed,
660 .button_press_event = volumealsa_button_press_event
661 };
662
663 /* vim: set sw=4 et sts=4 : */