1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2008 William Jon McCann
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
#include <glib/gi18n.h>
29
#include <gdk/gdkkeysyms.h>
31
#include <pulse/pulseaudio.h>
33
#include "gvc-mixer-stream.h"
34
#include "gvc-channel-bar.h"
35
#include "gvc-stream-status-icon.h"
37
#define GVC_STREAM_STATUS_ICON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_STREAM_STATUS_ICON, GvcStreamStatusIconPrivate))
39
struct GvcStreamStatusIconPrivate
42
GvcMixerStream *mixer_stream;
58
static void gvc_stream_status_icon_class_init (GvcStreamStatusIconClass *klass);
59
static void gvc_stream_status_icon_init (GvcStreamStatusIcon *stream_status_icon);
60
static void gvc_stream_status_icon_finalize (GObject *object);
62
G_DEFINE_TYPE (GvcStreamStatusIcon, gvc_stream_status_icon, GTK_TYPE_STATUS_ICON)
65
on_adjustment_value_changed (GtkAdjustment *adjustment,
66
GvcStreamStatusIcon *icon)
73
volume = gtk_adjustment_get_value (adjustment);
75
/* Only push the volume if it's actually changed */
76
if (gvc_mixer_stream_set_volume(icon->priv->mixer_stream,
77
(pa_volume_t) round (volume)) != FALSE) {
78
gvc_mixer_stream_push_volume(icon->priv->mixer_stream);
83
update_dock (GvcStreamStatusIcon *icon)
88
g_return_if_fail (icon);
90
adj = GTK_ADJUSTMENT (gvc_channel_bar_get_adjustment (GVC_CHANNEL_BAR (icon->priv->bar)));
92
icon->priv->thaw = TRUE;
93
gtk_adjustment_set_value (adj,
94
gvc_mixer_stream_get_volume (icon->priv->mixer_stream));
95
is_muted = gvc_mixer_stream_get_is_muted (icon->priv->mixer_stream);
96
gvc_channel_bar_set_is_muted (GVC_CHANNEL_BAR (icon->priv->bar), is_muted);
97
icon->priv->thaw = FALSE;
101
popup_dock (GvcStreamStatusIcon *icon,
105
GtkOrientation orientation;
112
GdkRectangle monitor;
113
GtkRequisition dock_req;
117
screen = gtk_status_icon_get_screen (GTK_STATUS_ICON (icon));
118
res = gtk_status_icon_get_geometry (GTK_STATUS_ICON (icon),
123
g_warning ("Unable to determine geometry of status icon");
127
/* position roughly */
128
gtk_window_set_screen (GTK_WINDOW (icon->priv->dock), screen);
129
gvc_channel_bar_set_orientation (GVC_CHANNEL_BAR (icon->priv->bar),
132
monitor_num = gdk_screen_get_monitor_at_point (screen, area.x, area.y);
133
gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
135
gtk_container_foreach (GTK_CONTAINER (icon->priv->dock),
136
(GtkCallback) gtk_widget_show_all, NULL);
137
gtk_widget_get_preferred_size (icon->priv->dock, &dock_req, NULL);
139
if (orientation == GTK_ORIENTATION_VERTICAL) {
140
if (area.x + area.width + dock_req.width <= monitor.x + monitor.width) {
141
x = area.x + area.width;
143
x = area.x - dock_req.width;
145
if (area.y + dock_req.height <= monitor.y + monitor.height) {
148
y = monitor.y + monitor.height - dock_req.height;
151
if (area.y + area.height + dock_req.height <= monitor.y + monitor.height) {
152
y = area.y + area.height;
154
y = area.y - dock_req.height;
156
if (area.x + dock_req.width <= monitor.x + monitor.width) {
159
x = monitor.x + monitor.width - dock_req.width;
163
gtk_window_move (GTK_WINDOW (icon->priv->dock), x, y);
165
/* FIXME: without this, the popup window appears as a square
166
* after changing the orientation
168
gtk_window_resize (GTK_WINDOW (icon->priv->dock), 1, 1);
170
gtk_widget_show_all (icon->priv->dock);
173
gtk_grab_add (icon->priv->dock);
175
if (gdk_pointer_grab (gtk_widget_get_window (icon->priv->dock), TRUE,
176
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
177
GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK, NULL, NULL,
179
!= GDK_GRAB_SUCCESS) {
180
gtk_grab_remove (icon->priv->dock);
181
gtk_widget_hide (icon->priv->dock);
185
if (gdk_keyboard_grab (gtk_widget_get_window (icon->priv->dock), TRUE, time) != GDK_GRAB_SUCCESS) {
186
display = gtk_widget_get_display (icon->priv->dock);
187
gdk_display_pointer_ungrab (display, time);
188
gtk_grab_remove (icon->priv->dock);
189
gtk_widget_hide (icon->priv->dock);
193
gtk_widget_grab_focus (icon->priv->dock);
199
on_status_icon_activate (GtkStatusIcon *status_icon,
200
GvcStreamStatusIcon *icon)
202
popup_dock (icon, GDK_CURRENT_TIME);
206
on_menu_mute_toggled (GtkMenuItem *item,
207
GvcStreamStatusIcon *icon)
210
is_muted = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (item));
211
gvc_channel_bar_set_is_muted (GVC_CHANNEL_BAR (icon->priv->bar), is_muted);
215
on_menu_activate_open_volume_control (GtkMenuItem *item,
216
GvcStreamStatusIcon *icon)
219
GdkAppLaunchContext *context;
223
context = gdk_app_launch_context_new ();
224
app = g_app_info_create_from_commandline ("gnome-control-center sound", "Sound preferences", 0, &error);
226
g_app_info_launch (app, NULL, G_APP_LAUNCH_CONTEXT (context), &error);
231
dialog = gtk_message_dialog_new (NULL,
235
_("Failed to start Sound Preferences: %s"),
237
g_signal_connect (dialog,
239
G_CALLBACK (gtk_widget_destroy),
241
gtk_widget_show (dialog);
242
g_error_free (error);
245
g_object_unref (context);
246
g_object_unref (app);
250
on_status_icon_popup_menu (GtkStatusIcon *status_icon,
253
GvcStreamStatusIcon *icon)
259
menu = gtk_menu_new ();
261
item = gtk_check_menu_item_new_with_mnemonic (_("_Mute"));
262
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
263
gvc_mixer_stream_get_is_muted (icon->priv->mixer_stream));
264
g_signal_connect (item,
266
G_CALLBACK (on_menu_mute_toggled),
268
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
270
item = gtk_image_menu_item_new_with_mnemonic (_("_Sound Preferences"));
271
image = gtk_image_new_from_icon_name ("multimedia-volume-control",
273
gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
274
g_signal_connect (item,
276
G_CALLBACK (on_menu_activate_open_volume_control),
278
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
280
gtk_widget_show_all (menu);
281
gtk_menu_popup (GTK_MENU (menu),
284
gtk_status_icon_position_menu,
291
on_status_icon_scroll_event (GtkStatusIcon *status_icon,
292
GdkEventScroll *event,
293
GvcStreamStatusIcon *icon)
295
return gvc_channel_bar_scroll (GVC_CHANNEL_BAR (icon->priv->bar), event->direction);
299
gvc_icon_release_grab (GvcStreamStatusIcon *icon,
300
GdkEventButton *event)
305
display = gtk_widget_get_display (GTK_WIDGET (icon->priv->dock));
306
gdk_display_keyboard_ungrab (display, event->time);
307
gdk_display_pointer_ungrab (display, event->time);
308
gtk_grab_remove (icon->priv->dock);
311
gtk_widget_hide (icon->priv->dock);
315
on_dock_button_press (GtkWidget *widget,
316
GdkEventButton *event,
317
GvcStreamStatusIcon *icon)
319
if (event->type == GDK_BUTTON_PRESS) {
320
gvc_icon_release_grab (icon, event);
328
popdown_dock (GvcStreamStatusIcon *icon)
333
display = gtk_widget_get_display (icon->priv->dock);
334
gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
335
gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
336
gtk_grab_remove (icon->priv->dock);
339
gtk_widget_hide (icon->priv->dock);
343
on_dock_key_release (GtkWidget *widget,
345
GvcStreamStatusIcon *icon)
347
if (event->keyval == GDK_KEY_Escape) {
353
if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event)) {
354
/* The popup hasn't managed the event, pass onto the button */
355
gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
362
on_dock_scroll_event (GtkWidget *widget,
363
GdkEventScroll *event,
364
GvcStreamStatusIcon *icon)
366
/* Forward event to the status icon */
367
on_status_icon_scroll_event (NULL, event, icon);
372
update_icon (GvcStreamStatusIcon *icon)
378
gboolean can_decibel;
381
if (icon->priv->mixer_stream == NULL) {
385
volume = gvc_mixer_stream_get_volume (icon->priv->mixer_stream);
386
is_muted = gvc_mixer_stream_get_is_muted (icon->priv->mixer_stream);
387
db = gvc_mixer_stream_get_decibel (icon->priv->mixer_stream);
388
can_decibel = gvc_mixer_stream_get_can_decibel (icon->priv->mixer_stream);
391
if (volume <= 0 || is_muted) {
394
n = 3 * volume / PA_VOLUME_NORM + 1;
402
/* apparently status icon will reset icon even if
403
* if doesn't change */
404
if (icon->priv->current_icon != n) {
405
gtk_status_icon_set_from_icon_name (GTK_STATUS_ICON (icon),
406
icon->priv->icon_names [n]);
407
icon->priv->current_icon = n;
412
markup = g_strdup_printf (
413
"<b>%s: %s</b>\n<small>%s</small>",
414
icon->priv->display_name,
416
gvc_mixer_stream_get_description (icon->priv->mixer_stream));
417
} else if (can_decibel && (db > PA_DECIBEL_MININFTY)) {
418
markup = g_strdup_printf (
419
"<b>%s: %.0f%%</b>\n<small>%0.2f dB\n%s</small>",
420
icon->priv->display_name,
421
100 * (float)volume / PA_VOLUME_NORM,
423
gvc_mixer_stream_get_description (icon->priv->mixer_stream));
424
} else if (can_decibel) {
425
markup = g_strdup_printf (
426
"<b>%s: %.0f%%</b>\n<small>-∞ dB\n%s</small>",
427
icon->priv->display_name,
428
100 * (float)volume / PA_VOLUME_NORM,
429
gvc_mixer_stream_get_description (icon->priv->mixer_stream));
431
markup = g_strdup_printf (
432
"<b>%s: %.0f%%</b>\n<small>%s</small>",
433
icon->priv->display_name,
434
100 * (float)volume / PA_VOLUME_NORM,
435
gvc_mixer_stream_get_description (icon->priv->mixer_stream));
437
gtk_status_icon_set_tooltip_markup (GTK_STATUS_ICON (icon), markup);
442
gvc_stream_status_icon_set_icon_names (GvcStreamStatusIcon *icon,
445
g_return_if_fail (GVC_IS_STREAM_STATUS_ICON (icon));
447
g_strfreev (icon->priv->icon_names);
448
icon->priv->icon_names = g_strdupv ((char **)names);
450
g_object_notify (G_OBJECT (icon), "icon-names");
454
on_stream_volume_notify (GObject *object,
456
GvcStreamStatusIcon *icon)
463
on_stream_is_muted_notify (GObject *object,
465
GvcStreamStatusIcon *icon)
472
gvc_stream_status_icon_set_display_name (GvcStreamStatusIcon *icon,
475
g_return_if_fail (GVC_STREAM_STATUS_ICON (icon));
477
g_free (icon->priv->display_name);
478
icon->priv->display_name = g_strdup (name);
480
g_object_notify (G_OBJECT (icon), "display-name");
484
gvc_stream_status_icon_set_mixer_stream (GvcStreamStatusIcon *icon,
485
GvcMixerStream *stream)
487
g_return_if_fail (GVC_STREAM_STATUS_ICON (icon));
489
if (stream != NULL) {
490
g_object_ref (stream);
493
if (icon->priv->mixer_stream != NULL) {
494
g_signal_handlers_disconnect_by_func (icon->priv->mixer_stream,
495
G_CALLBACK (on_stream_volume_notify),
497
g_signal_handlers_disconnect_by_func (icon->priv->mixer_stream,
498
G_CALLBACK (on_stream_is_muted_notify),
500
g_object_unref (icon->priv->mixer_stream);
501
icon->priv->mixer_stream = NULL;
504
icon->priv->mixer_stream = stream;
506
if (icon->priv->mixer_stream != NULL) {
509
g_object_ref (icon->priv->mixer_stream);
511
icon->priv->thaw = TRUE;
512
adj = GTK_ADJUSTMENT (gvc_channel_bar_get_adjustment (GVC_CHANNEL_BAR (icon->priv->bar)));
513
gtk_adjustment_set_value (adj,
514
gvc_mixer_stream_get_volume (icon->priv->mixer_stream));
515
icon->priv->thaw = FALSE;
517
g_signal_connect (icon->priv->mixer_stream,
519
G_CALLBACK (on_stream_volume_notify),
521
g_signal_connect (icon->priv->mixer_stream,
523
G_CALLBACK (on_stream_is_muted_notify),
529
g_object_notify (G_OBJECT (icon), "mixer-stream");
533
gvc_stream_status_icon_set_property (GObject *object,
538
GvcStreamStatusIcon *self = GVC_STREAM_STATUS_ICON (object);
541
case PROP_MIXER_STREAM:
542
gvc_stream_status_icon_set_mixer_stream (self, g_value_get_object (value));
544
case PROP_DISPLAY_NAME:
545
gvc_stream_status_icon_set_display_name (self, g_value_get_string (value));
547
case PROP_ICON_NAMES:
548
gvc_stream_status_icon_set_icon_names (self, g_value_get_boxed (value));
551
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
557
gvc_stream_status_icon_get_property (GObject *object,
562
GvcStreamStatusIcon *self = GVC_STREAM_STATUS_ICON (object);
563
GvcStreamStatusIconPrivate *priv = self->priv;
566
case PROP_MIXER_STREAM:
567
g_value_set_object (value, priv->mixer_stream);
569
case PROP_DISPLAY_NAME:
570
g_value_set_string (value, priv->display_name);
572
case PROP_ICON_NAMES:
573
g_value_set_boxed (value, priv->icon_names);
576
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
582
on_bar_is_muted_notify (GObject *object,
584
GvcStreamStatusIcon *icon)
588
is_muted = gvc_channel_bar_get_is_muted (GVC_CHANNEL_BAR (object));
590
if (gvc_mixer_stream_get_is_muted (icon->priv->mixer_stream) != is_muted) {
591
/* Update the stream before pushing the change */
592
gvc_mixer_stream_set_is_muted (icon->priv->mixer_stream, is_muted);
593
gvc_mixer_stream_change_is_muted (icon->priv->mixer_stream,
599
gvc_stream_status_icon_constructor (GType type,
600
guint n_construct_properties,
601
GObjectConstructParam *construct_params)
604
GvcStreamStatusIcon *icon;
609
object = G_OBJECT_CLASS (gvc_stream_status_icon_parent_class)->constructor (type, n_construct_properties, construct_params);
611
icon = GVC_STREAM_STATUS_ICON (object);
613
gtk_status_icon_set_from_icon_name (GTK_STATUS_ICON (icon),
614
icon->priv->icon_names[0]);
617
icon->priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
618
gtk_widget_set_name (icon->priv->dock, "gvc-stream-status-icon-popup-window");
619
g_signal_connect (icon->priv->dock,
620
"button-press-event",
621
G_CALLBACK (on_dock_button_press),
623
g_signal_connect (icon->priv->dock,
625
G_CALLBACK (on_dock_key_release),
627
g_signal_connect (icon->priv->dock,
629
G_CALLBACK (on_dock_scroll_event),
632
gtk_window_set_decorated (GTK_WINDOW (icon->priv->dock), FALSE);
634
frame = gtk_frame_new (NULL);
635
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
636
gtk_container_add (GTK_CONTAINER (icon->priv->dock), frame);
638
box = gtk_vbox_new (FALSE, 6);
639
gtk_container_set_border_width (GTK_CONTAINER (box), 2);
640
gtk_container_add (GTK_CONTAINER (frame), box);
642
icon->priv->bar = gvc_channel_bar_new ();
643
gvc_channel_bar_set_orientation (GVC_CHANNEL_BAR (icon->priv->bar),
644
GTK_ORIENTATION_VERTICAL);
646
gtk_box_pack_start (GTK_BOX (box), icon->priv->bar, TRUE, FALSE, 0);
647
g_signal_connect (icon->priv->bar,
649
G_CALLBACK (on_bar_is_muted_notify),
652
adj = GTK_ADJUSTMENT (gvc_channel_bar_get_adjustment (GVC_CHANNEL_BAR (icon->priv->bar)));
653
g_signal_connect (adj,
655
G_CALLBACK (on_adjustment_value_changed),
662
gvc_stream_status_icon_dispose (GObject *object)
664
GvcStreamStatusIcon *icon = GVC_STREAM_STATUS_ICON (object);
666
if (icon->priv->dock != NULL) {
667
gtk_widget_destroy (icon->priv->dock);
668
icon->priv->dock = NULL;
671
if (icon->priv->mixer_stream != NULL) {
672
g_object_unref (icon->priv->mixer_stream);
673
icon->priv->mixer_stream = NULL;
676
G_OBJECT_CLASS (gvc_stream_status_icon_parent_class)->dispose (object);
680
gvc_stream_status_icon_class_init (GvcStreamStatusIconClass *klass)
682
GObjectClass *object_class = G_OBJECT_CLASS (klass);
684
object_class->constructor = gvc_stream_status_icon_constructor;
685
object_class->finalize = gvc_stream_status_icon_finalize;
686
object_class->dispose = gvc_stream_status_icon_dispose;
687
object_class->set_property = gvc_stream_status_icon_set_property;
688
object_class->get_property = gvc_stream_status_icon_get_property;
690
g_object_class_install_property (object_class,
692
g_param_spec_object ("mixer-stream",
695
GVC_TYPE_MIXER_STREAM,
696
G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
697
g_object_class_install_property (object_class,
699
g_param_spec_string ("display-name",
701
"Name to display for this stream",
703
G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
704
g_object_class_install_property (object_class,
706
g_param_spec_boxed ("icon-names",
708
"Name of icon to display for this stream",
710
G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
712
g_type_class_add_private (klass, sizeof (GvcStreamStatusIconPrivate));
716
on_status_icon_visible_notify (GvcStreamStatusIcon *icon)
720
g_object_get (icon, "visible", &visible, NULL);
722
if (icon->priv->dock != NULL) {
723
gtk_widget_hide (icon->priv->dock);
729
gvc_stream_status_icon_init (GvcStreamStatusIcon *icon)
731
icon->priv = GVC_STREAM_STATUS_ICON_GET_PRIVATE (icon);
733
g_signal_connect (icon,
735
G_CALLBACK (on_status_icon_activate),
737
g_signal_connect (icon,
739
G_CALLBACK (on_status_icon_popup_menu),
741
g_signal_connect (icon,
743
G_CALLBACK (on_status_icon_scroll_event),
745
g_signal_connect (icon,
747
G_CALLBACK (on_status_icon_visible_notify),
750
icon->priv->thaw = FALSE;
754
gvc_stream_status_icon_finalize (GObject *object)
756
GvcStreamStatusIcon *stream_status_icon;
758
g_return_if_fail (object != NULL);
759
g_return_if_fail (GVC_IS_STREAM_STATUS_ICON (object));
761
stream_status_icon = GVC_STREAM_STATUS_ICON (object);
763
g_return_if_fail (stream_status_icon->priv != NULL);
765
g_strfreev (stream_status_icon->priv->icon_names);
767
G_OBJECT_CLASS (gvc_stream_status_icon_parent_class)->finalize (object);
770
GvcStreamStatusIcon *
771
gvc_stream_status_icon_new (GvcMixerStream *stream,
772
const char **icon_names)
775
icon = g_object_new (GVC_TYPE_STREAM_STATUS_ICON,
776
"mixer-stream", stream,
777
"icon-names", icon_names,
779
return GVC_STREAM_STATUS_ICON (icon);