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