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