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