[SF#679]X11 library uses unsigned long as cardinal-32 data, not uint32_t.
[lxde/lxpanel.git] / plugins / volumealsa / volumealsa.c
CommitLineData
c044bccf 1/**
b840f7cc 2 * Copyright (c) 2008-2014 LxDE Developers, see the file AUTHORS for details.
49db0a37
FC
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>
55ccbddf 27#include <poll.h>
b285684e
AG
28#include <libfm/fm-gtk.h>
29#include "plugin.h"
49db0a37 30
0b806437
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"
49db0a37
FC
35
36typedef struct {
f0861dee 37
38 /* Graphics. */
b285684e 39 GtkWidget * plugin; /* Back pointer to the widget */
a7bd16a4 40 LXPanel * panel; /* Back pointer to panel */
72312227 41 config_setting_t * settings; /* Plugin settings */
f0861dee 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 */
b285684e 55 guint restart_idle;
ad987a0c
HG
56
57 /* unloading and error handling */
58 GIOChannel **channels; /* Channels that we listen to */
9973857e 59 guint *watches; /* Watcher IDs for channels */
ad987a0c 60 guint num_channels; /* Number of channels */
7192ab49
JL
61
62 /* Icons */
63 const char* icon;
64 const char* icon_panel;
65 const char* icon_fallback;
66
f0861dee 67} VolumeALSAPlugin;
68
ad987a0c 69static gboolean asound_restart(gpointer vol_gpointer);
f0861dee 70static gboolean asound_initialize(VolumeALSAPlugin * vol);
ad987a0c 71static void asound_deinitialize(VolumeALSAPlugin * vol);
f0861dee 72static void volumealsa_update_display(VolumeALSAPlugin * vol);
b285684e 73static void volumealsa_destructor(gpointer user_data);
f0861dee 74
75/*** ALSA ***/
76
77static gboolean asound_find_element(VolumeALSAPlugin * vol, const char * ename)
49db0a37 78{
f0861dee 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 {
4542c20d 84 snd_mixer_selem_get_id(vol->master_element, vol->sid);
f0861dee 85 if ((snd_mixer_selem_is_active(vol->master_element))
86 && (strcmp(ename, snd_mixer_selem_id_get_name(vol->sid)) == 0))
4542c20d 87 return TRUE;
4542c20d 88 }
4542c20d 89 return FALSE;
49db0a37
FC
90}
91
55ccbddf
HJYP
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 */
f0861dee 112
113static gboolean asound_reset_mixer_evt_idle(VolumeALSAPlugin * vol)
55ccbddf 114{
251cfd3e
AG
115 if (!g_source_is_destroyed(g_main_current_source()))
116 vol->mixer_evt_idle = 0;
55ccbddf
HJYP
117 return FALSE;
118}
119
f0861dee 120/* Handler for I/O event on ALSA channel. */
121static gboolean asound_mixer_event(GIOChannel * channel, GIOCondition cond, gpointer vol_gpointer)
55ccbddf 122{
f0861dee 123 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) vol_gpointer;
e0eb2f82 124 int res = 0;
f0861dee 125
9973857e
AG
126 if (g_source_is_destroyed(g_main_current_source()))
127 return FALSE;
128
f0861dee 129 if (vol->mixer_evt_idle == 0)
0fdd5153 130 {
f0861dee 131 vol->mixer_evt_idle = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) asound_reset_mixer_evt_idle, vol, NULL);
e0eb2f82 132 res = snd_mixer_handle_events(vol->mixer);
0fdd5153
HJYP
133 }
134
f0861dee 135 if (cond & G_IO_IN)
55ccbddf
HJYP
136 {
137 /* the status of mixer is changed. update of display is needed. */
f0861dee 138 volumealsa_update_display(vol);
55ccbddf 139 }
55ccbddf 140
e0eb2f82 141 if ((cond & G_IO_HUP) || (res < 0))
f0861dee 142 {
143 /* This means there're some problems with alsa. */
06e29ce1 144 g_warning("volumealsa: ALSA (or pulseaudio) had a problem: "
e0eb2f82 145 "volumealsa: snd_mixer_handle_events() = %d,"
06e29ce1 146 " cond 0x%x (IN: 0x%x, HUP: 0x%x).", res, cond,
e0eb2f82 147 G_IO_IN, G_IO_HUP);
b285684e 148 gtk_widget_set_tooltip_text(vol->plugin, "ALSA (or pulseaudio) had a problem."
e0eb2f82 149 " Please check the lxpanel logs.");
ad987a0c 150
b285684e
AG
151 if (vol->restart_idle == 0)
152 vol->restart_idle = g_timeout_add_seconds(1, asound_restart, vol);
ad987a0c 153
55ccbddf
HJYP
154 return FALSE;
155 }
156
55ccbddf
HJYP
157 return TRUE;
158}
159
ad987a0c
HG
160static gboolean asound_restart(gpointer vol_gpointer)
161{
162 VolumeALSAPlugin * vol = vol_gpointer;
163
b285684e
AG
164 if (g_source_is_destroyed(g_main_current_source()))
165 return FALSE;
166
ad987a0c
HG
167 asound_deinitialize(vol);
168
169 if (!asound_initialize(vol)) {
06e29ce1 170 g_warning("volumealsa: Re-initialization failed.");
ad987a0c
HG
171 return TRUE; // try again in a second
172 }
173
06e29ce1 174 g_warning("volumealsa: Restarted ALSA interface...");
ad987a0c 175
b285684e 176 vol->restart_idle = 0;
ad987a0c
HG
177 return FALSE;
178}
179
f0861dee 180/* Initialize the ALSA interface. */
181static gboolean asound_initialize(VolumeALSAPlugin * vol)
49db0a37 182{
f0861dee 183 /* Access the "default" device. */
4542c20d
HJYP
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
60d8abbb
MJ
190 /* Find Master element, or Front element, or PCM element, or LineOut element.
191 * If one of these succeeds, master_element is valid. */
f0861dee 192 if ( ! asound_find_element(vol, "Master"))
193 if ( ! asound_find_element(vol, "Front"))
194 if ( ! asound_find_element(vol, "PCM"))
b285684e 195 if ( ! asound_find_element(vol, "LineOut"))
e5f6c697 196 return FALSE;
4542c20d 197
f0861dee 198 /* Set the playback volume range as we wish it. */
4542c20d 199 snd_mixer_selem_set_playback_volume_range(vol->master_element, 0, 100);
9864f98f 200
f0861dee 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);
55ccbddf 204
ad987a0c 205 vol->channels = g_new0(GIOChannel *, n_fds);
9973857e 206 vol->watches = g_new0(guint, n_fds);
ad987a0c
HG
207 vol->num_channels = n_fds;
208
f0861dee 209 snd_mixer_poll_descriptors(vol->mixer, fds, n_fds);
210 int i;
211 for (i = 0; i < n_fds; ++i)
55ccbddf 212 {
f0861dee 213 GIOChannel* channel = g_io_channel_unix_new(fds[i].fd);
9973857e 214 vol->watches[i] = g_io_add_watch(channel, G_IO_IN | G_IO_HUP, asound_mixer_event, vol);
ad987a0c 215 vol->channels[i] = channel;
55ccbddf 216 }
f0861dee 217 g_free(fds);
9864f98f 218 return TRUE;
49db0a37
FC
219}
220
ad987a0c
HG
221static void asound_deinitialize(VolumeALSAPlugin * vol)
222{
fbe0300a 223 guint i;
ad987a0c 224
08a8293e 225 if (vol->mixer_evt_idle != 0) {
ad987a0c 226 g_source_remove(vol->mixer_evt_idle);
08a8293e
HG
227 vol->mixer_evt_idle = 0;
228 }
ad987a0c
HG
229
230 for (i = 0; i < vol->num_channels; i++) {
9973857e 231 g_source_remove(vol->watches[i]);
ad987a0c
HG
232 g_io_channel_shutdown(vol->channels[i], FALSE, NULL);
233 g_io_channel_unref(vol->channels[i]);
234 }
235 g_free(vol->channels);
9973857e 236 g_free(vol->watches);
ad987a0c 237 vol->channels = NULL;
9973857e 238 vol->watches = NULL;
ad987a0c 239 vol->num_channels = 0;
08a8293e
HG
240
241 snd_mixer_close(vol->mixer);
242 vol->master_element = NULL;
b285684e 243 /* FIXME: unalloc vol->sid */
ad987a0c
HG
244}
245
f0861dee 246/* Get the presence of the mute control from the sound system. */
247static gboolean asound_has_mute(VolumeALSAPlugin * vol)
248{
60d8abbb 249 return ((vol->master_element != NULL) ? snd_mixer_selem_has_playback_switch(vol->master_element) : FALSE);
f0861dee 250}
251
252/* Get the condition of the mute control from the sound system. */
253static gboolean asound_is_muted(VolumeALSAPlugin * vol)
254{
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;
60d8abbb
MJ
258 if (vol->master_element != NULL)
259 snd_mixer_selem_get_playback_switch(vol->master_element, 0, &value);
f0861dee 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)
49db0a37 266{
60d8abbb
MJ
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 }
4542c20d 274 return (aleft + aright) >> 1;
49db0a37
FC
275}
276
f0861dee 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)
49db0a37 280{
60d8abbb
MJ
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 }
49db0a37
FC
286}
287
f0861dee 288/*** Graphics ***/
289
7192ab49 290static void volumealsa_update_current_icon(VolumeALSAPlugin * vol)
49db0a37 291{
f0861dee 292 /* Mute status. */
293 gboolean mute = asound_is_muted(vol);
4896be7f 294 int level = asound_get_volume(vol);
bfc11b5e 295
4896be7f
JL
296 /* Change icon according to mute / volume */
297 const char* icon="audio-volume-muted";
9a82da9a 298 const char* icon_panel="audio-volume-muted-panel";
4896be7f
JL
299 const char* icon_fallback=ICONS_MUTE;
300 if (mute)
301 {
9a82da9a 302 icon_panel = "audio-volume-muted-panel";
4896be7f
JL
303 icon="audio-volume-muted";
304 icon_fallback=ICONS_MUTE;
305 }
306 else if (level >= 75)
c14620f2 307 {
9a82da9a 308 icon_panel = "audio-volume-high-panel";
4896be7f
JL
309 icon="audio-volume-high";
310 icon_fallback=ICONS_VOLUME_HIGH;
311 }
312 else if (level >= 50)
313 {
9a82da9a 314 icon_panel = "audio-volume-medium-panel";
4896be7f
JL
315 icon="audio-volume-medium";
316 icon_fallback=ICONS_VOLUME_MEDIUM;
317 }
318 else if (level > 0)
319 {
9a82da9a 320 icon_panel = "audio-volume-low-panel";
4896be7f
JL
321 icon="audio-volume-low";
322 icon_fallback=ICONS_VOLUME_LOW;
323 }
324
7192ab49
JL
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
4896be7f 339 /* Change icon, fallback to default icon if theme doesn't exsit */
a7bd16a4 340 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon_panel))
4896be7f 341 {
a7bd16a4 342 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon))
9a82da9a 343 {
a7bd16a4 344 lxpanel_image_set_from_file(vol->panel, vol->tray_icon, vol->icon_fallback);
9a82da9a 345 }
c14620f2 346 }
f0861dee 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. */
f0861dee 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);
b285684e 363 gtk_widget_set_tooltip_text(vol->plugin, tooltip);
f0861dee 364 g_free(tooltip);
49db0a37
FC
365}
366
7192ab49 367
f0861dee 368/* Handler for "button-press-event" signal on main widget. */
a7bd16a4 369static gboolean volumealsa_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * panel)
49db0a37 370{
1639d6b3 371 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(widget);
b285684e 372
f0861dee 373 /* Left-click. Show or hide the popup window. */
d220e22a 374 if (event->button == 1)
375 {
f0861dee 376 if (vol->show_popup)
d220e22a 377 {
f0861dee 378 gtk_widget_hide(vol->popup_window);
379 vol->show_popup = FALSE;
d220e22a 380 }
381 else
382 {
f0861dee 383 gtk_widget_show_all(vol->popup_window);
384 vol->show_popup = TRUE;
d220e22a 385 }
386 }
f0861dee 387
388 /* Middle-click. Toggle the mute status. */
d220e22a 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)));
4542c20d 392 }
3e7b8eb7 393 return TRUE;
49db0a37
FC
394}
395
f0861dee 396/* Handler for "focus-out" signal on popup window. */
397static gboolean volumealsa_popup_focus_out(GtkWidget * widget, GdkEvent * event, VolumeALSAPlugin * vol)
49db0a37 398{
f0861dee 399 /* Hide the widget. */
400 gtk_widget_hide(vol->popup_window);
401 vol->show_popup = FALSE;
402 return FALSE;
49db0a37
FC
403}
404
4ec924bc
MJ
405/* Handler for "map" signal on popup window. */
406static void volumealsa_popup_map(GtkWidget * widget, VolumeALSAPlugin * vol)
407{
b285684e 408 lxpanel_plugin_adjust_popup_position(widget, vol->plugin);
4ec924bc
MJ
409}
410
7192ab49
JL
411static void volumealsa_theme_change(GtkWidget * widget, VolumeALSAPlugin * vol)
412{
a7bd16a4 413 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon_panel))
7192ab49 414 {
a7bd16a4 415 if ( ! lxpanel_image_set_icon_theme(vol->panel, vol->tray_icon, vol->icon))
7192ab49 416 {
a7bd16a4 417 lxpanel_image_set_from_file(vol->panel, vol->tray_icon, vol->icon_fallback);
7192ab49
JL
418 }
419 }
420}
421
f0861dee 422/* Handler for "value_changed" signal on popup window vertical scale. */
423static void volumealsa_popup_scale_changed(GtkRange * range, VolumeALSAPlugin * vol)
0fdd5153 424{
f0861dee 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))
0fdd5153 440 val += 2;
f0861dee 441 else
0fdd5153 442 val -= 2;
f0861dee 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));
0fdd5153
HJYP
446}
447
f0861dee 448/* Handler for "toggled" signal on popup window mute checkbox. */
449static void volumealsa_popup_mute_toggled(GtkWidget * widget, VolumeALSAPlugin * vol)
49db0a37 450{
f0861dee 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. */
60d8abbb
MJ
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 }
4542c20d 461
f0861dee 462 /* Redraw the controls. */
463 volumealsa_update_display(vol);
49db0a37
FC
464}
465
f0861dee 466/* Build the window that appears when the top level widget is clicked. */
b285684e 467static void volumealsa_build_popup_window(GtkWidget *p)
49db0a37 468{
b285684e 469 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
f0861dee 470
471 /* Create a new window. */
1918164d 472 vol->popup_window = gtk_window_new(GTK_WINDOW_POPUP);
f0861dee 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);
91f2b215 478 gtk_window_set_type_hint(GTK_WINDOW(vol->popup_window), GDK_WINDOW_TYPE_HINT_UTILITY);
f0861dee 479
4ec924bc 480 /* Connect signals. */
93a217b3 481 g_signal_connect(G_OBJECT(vol->popup_window), "focus-out-event", G_CALLBACK(volumealsa_popup_focus_out), vol);
4ec924bc 482 g_signal_connect(G_OBJECT(vol->popup_window), "map", G_CALLBACK(volumealsa_popup_map), vol);
f0861dee 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);
09fa171b 489 gtk_widget_set_can_focus(scrolledwindow, FALSE);
4542c20d 490 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
63de7afd 491 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_NONE);
4542c20d 492
f0861dee 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);
4542c20d
HJYP
497 gtk_widget_show(viewport);
498
f0861dee 499 /* Create a frame as the child of the viewport. */
500 GtkWidget * frame = gtk_frame_new(_("Volume"));
4542c20d
HJYP
501 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
502 gtk_container_add(GTK_CONTAINER(viewport), frame);
503
f0861dee 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);
4542c20d 507
f0861dee 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);
4542c20d 513
f0861dee 514 /* Value-changed and scroll-event signals. */
93a217b3 515 vol->volume_scale_handler = g_signal_connect(vol->volume_scale, "value-changed", G_CALLBACK(volumealsa_popup_scale_changed), vol);
f0861dee 516 g_signal_connect(vol->volume_scale, "scroll-event", G_CALLBACK(volumealsa_popup_scale_scrolled), vol);
49db0a37 517
f0861dee 518 /* Create a check button as the child of the vertical box. */
0fdd5153 519 vol->mute_check = gtk_check_button_new_with_label(_("Mute"));
0fdd5153 520 gtk_box_pack_end(GTK_BOX(box), vol->mute_check, FALSE, FALSE, 0);
f0861dee 521 vol->mute_check_handler = g_signal_connect(vol->mute_check, "toggled", G_CALLBACK(volumealsa_popup_mute_toggled), vol);
49db0a37 522
f0861dee 523 /* Set background to default. */
b285684e 524 gtk_widget_set_style(viewport, panel_get_defstyle(vol->panel));
49db0a37
FC
525}
526
f0861dee 527/* Plugin constructor. */
a7bd16a4 528static GtkWidget *volumealsa_constructor(LXPanel *panel, config_setting_t *settings)
49db0a37 529{
f0861dee 530 /* Allocate and initialize plugin context and set into Plugin private data pointer. */
531 VolumeALSAPlugin * vol = g_new0(VolumeALSAPlugin, 1);
b285684e 532 GtkWidget *p;
4542c20d 533
f0861dee 534 /* Initialize ALSA. If that fails, present nothing. */
535 if ( ! asound_initialize(vol))
b285684e
AG
536 {
537 volumealsa_destructor(vol);
538 return NULL;
539 }
4542c20d 540
f0861dee 541 /* Allocate top level widget and set into Plugin widget pointer. */
b285684e
AG
542 vol->panel = panel;
543 vol->plugin = p = gtk_event_box_new();
72312227 544 vol->settings = settings;
b285684e
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"));
4542c20d 548
f0861dee 549 /* Allocate icon as a child of top level. */
550 vol->tray_icon = gtk_image_new();
b285684e 551 gtk_container_add(GTK_CONTAINER(p), vol->tray_icon);
4542c20d 552
f0861dee 553 /* Initialize window to appear when icon clicked. */
554 volumealsa_build_popup_window(p);
49db0a37 555
f0861dee 556 /* Connect signals. */
b285684e
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 );
49db0a37 559
f0861dee 560 /* Update the display, show the widget, and return. */
561 volumealsa_update_display(vol);
b285684e
AG
562 gtk_widget_show_all(p);
563 return p;
f0861dee 564}
1abc8a6a 565
f0861dee 566/* Plugin destructor. */
b285684e 567static void volumealsa_destructor(gpointer user_data)
f0861dee 568{
b285684e 569 VolumeALSAPlugin * vol = (VolumeALSAPlugin *) user_data;
49db0a37 570
ad987a0c 571 asound_deinitialize(vol);
4542c20d 572
f0861dee 573 /* If the dialog box is open, dismiss it. */
574 if (vol->popup_window != NULL)
575 gtk_widget_destroy(vol->popup_window);
49db0a37 576
b285684e
AG
577 if (vol->restart_idle)
578 g_source_remove(vol->restart_idle);
579
4b9bf724
AG
580 g_signal_handlers_disconnect_by_func(panel_get_icon_theme(vol->panel),
581 volumealsa_theme_change, vol);
582
f0861dee 583 /* Deallocate all memory. */
584 g_free(vol);
49db0a37
FC
585}
586
552475ec
JL
587/* Callback when the configuration dialog is to be shown. */
588
752ee4e2 589static GtkWidget *volumealsa_configure(LXPanel *panel, GtkWidget *p)
552475ec 590{
72312227
AG
591 VolumeALSAPlugin * vol = lxpanel_plugin_get_data(p);
592 char *path = NULL;
552475ec
JL
593 const gchar *command_line = NULL;
594
72312227
AG
595 /* FIXME: configure settings! */
596 /* check if command line was configured */
597 config_setting_lookup_string(vol->settings, "MixerCommand", &command_line);
598
599 /* if command isn't set in settings then let guess it */
600 if (command_line == NULL && (path = g_find_program_in_path("pulseaudio")))
552475ec 601 {
72312227 602 g_free(path);
552475ec 603 /* Assume that when pulseaudio is installed, it's launching every time */
72312227 604 if ((path = g_find_program_in_path("gnome-sound-applet")))
552475ec
JL
605 {
606 command_line = "gnome-sound-applet";
607 }
72312227 608 else if ((path = g_find_program_in_path("pavucontrol")))
552475ec 609 {
72312227 610 command_line = "pavucontrol";
552475ec
JL
611 }
612 }
613
614 /* Fallback to alsamixer when PA is not running, or when no PA utility is find */
615 if (command_line == NULL)
616 {
72312227 617 if ((path = g_find_program_in_path("gnome-alsamixer")))
552475ec
JL
618 {
619 command_line = "gnome-alsamixer";
620 }
72312227
AG
621 else if ((path = g_find_program_in_path("alsamixergui")))
622 {
623 command_line = "alsamixergui";
624 }
625 else if ((path = g_find_program_in_path("alsamixer")))
552475ec 626 {
72312227
AG
627 g_free(path);
628 if ((path = g_find_program_in_path("xterm")))
552475ec 629 {
72312227 630 command_line = "xterm -e alsamixer";
552475ec
JL
631 }
632 }
633 }
72312227 634 g_free(path);
552475ec
JL
635
636 if (command_line)
637 {
09fa171b
AG
638 fm_launch_command_simple(NULL, NULL, G_APP_INFO_CREATE_NONE,
639 command_line, NULL);
552475ec
JL
640 }
641 else
642 {
c065f0b3
AG
643 fm_show_error(NULL, NULL,
644 _("Error, you need to install an application to configure"
645 " the sound (pavucontrol, alsamixer ...)"));
552475ec
JL
646 }
647
b285684e 648 return NULL;
552475ec
JL
649}
650
8f9e6256 651/* Callback when panel configuration changes. */
a7bd16a4 652static void volumealsa_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
8f9e6256 653{
654 /* Do a full redraw. */
b285684e 655 volumealsa_update_display(lxpanel_plugin_get_data(p));
8f9e6256 656}
49db0a37 657
b285684e 658FM_DEFINE_MODULE(lxpanel_gtk, volumealsa)
2918994e 659
b285684e
AG
660/* Plugin descriptor. */
661LXPanelPluginInit fm_module_init_lxpanel_gtk = {
3c3e9c9e 662 .name = N_("Volume Control"),
a04979c2 663 .description = N_("Display and control volume for ALSA"),
3c3e9c9e 664
b285684e 665 .new_instance = volumealsa_constructor,
a04979c2 666 .config = volumealsa_configure,
b285684e
AG
667 .reconfigure = volumealsa_panel_configuration_changed,
668 .button_press_event = volumealsa_button_press_event
49db0a37 669};
e0eb2f82
HG
670
671/* vim: set sw=4 et sts=4 : */