1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/* Copyright (c) 2009-2015 Steve Dodier-Lazaro <sidi@xfce.org>
* 2015-2017 Andrzej <andrzejr@xfce.org>
* 2017 Viktor Odintsev <zakhams@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* This file implements a wrapper for libnotify
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_MATH_H
#include <math.h>
#endif
#ifdef HAVE_LIBNOTIFY
#include <libnotify/notify.h>
#include <libxfce4util/libxfce4util.h>
#define SYNCHRONOUS "x-canonical-private-synchronous"
#define LAYOUT_ICON_ONLY "x-canonical-private-icon-only"
#include "pulseaudio-notify.h"
#define V_MUTED 0
#define V_LOW 1
#define V_MEDIUM 2
#define V_HIGH 3
/* Icons for different volume levels */
static const char *icons[] = {
"audio-volume-muted-symbolic",
"audio-volume-low-symbolic",
"audio-volume-medium-symbolic",
"audio-volume-high-symbolic",
NULL
};
/* Icons for different mic volume levels */
static const char *icons_mic[] = {
"microphone-sensitivity-muted-symbolic",
"microphone-sensitivity-low-symbolic",
"microphone-sensitivity-medium-symbolic",
"microphone-sensitivity-high-symbolic",
NULL
};
static void pulseaudio_notify_finalize (GObject *object);
struct _PulseaudioNotify
{
GObject __parent__;
PulseaudioConfig *config;
PulseaudioVolume *volume;
PulseaudioButton *button;
gboolean gauge_notifications;
NotifyNotification *notification;
NotifyNotification *notification_mic;
gulong volume_changed_id;
gulong volume_mic_changed_id;
};
struct _PulseaudioNotifyClass
{
GObjectClass __parent__;
};
G_DEFINE_TYPE (PulseaudioNotify, pulseaudio_notify, G_TYPE_OBJECT)
static void
pulseaudio_notify_class_init (PulseaudioNotifyClass *klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = pulseaudio_notify_finalize;
}
static void
pulseaudio_notify_init (PulseaudioNotify *notify)
{
GList *caps_list;
GList *node;
notify->gauge_notifications = TRUE;
notify->notification = NULL;
notify->notification_mic = NULL;
notify->volume_changed_id = 0;
notify->volume_mic_changed_id = 0;
notify_init ("Xfce volume control");
caps_list = notify_get_server_caps ();
if (caps_list)
{
node = g_list_find_custom (caps_list, LAYOUT_ICON_ONLY, (GCompareFunc) g_strcmp0);
if (!node)
notify->gauge_notifications = FALSE;
g_list_free_full (caps_list, g_free);
}
notify->notification = notify_notification_new ("xfce4-pulseaudio-plugin", NULL, NULL);
notify_notification_set_timeout (notify->notification, 2000);
notify_notification_set_hint (notify->notification, "transient", g_variant_new_boolean (TRUE));
notify->notification_mic = notify_notification_new ("xfce4-pulseaudio-plugin", NULL, NULL);
notify_notification_set_timeout (notify->notification_mic, 2000);
notify_notification_set_hint (notify->notification_mic, "transient", g_variant_new_boolean (TRUE));
}
static void
pulseaudio_notify_finalize (GObject *object)
{
PulseaudioNotify *notify = PULSEAUDIO_NOTIFY (object);
notify->config = NULL;
g_object_unref (G_OBJECT (notify->notification));
notify->notification = NULL;
g_object_unref (G_OBJECT (notify->notification_mic));
notify->notification_mic = NULL;
notify_uninit ();
(*G_OBJECT_CLASS (pulseaudio_notify_parent_class)->finalize) (object);
}
static void
pulseaudio_notify_notify (PulseaudioNotify *notify, gboolean mic)
{
GError *error = NULL;
NotifyNotification *notification;
gdouble volume;
gint volume_i;
gboolean muted;
gboolean connected;
gchar *title = NULL;
const char **icons_array;
const gchar *icon = NULL;
g_return_if_fail (IS_PULSEAUDIO_NOTIFY (notify));
g_return_if_fail (IS_PULSEAUDIO_VOLUME (notify->volume));
if (!pulseaudio_config_get_show_notifications (notify->config) ||
pulseaudio_button_get_menu (notify->button) != NULL)
return;
notification = mic ? notify->notification_mic : notify->notification;
icons_array = mic ? icons_mic : icons;
volume = (mic ? pulseaudio_volume_get_volume_mic : pulseaudio_volume_get_volume) (notify->volume);
muted = (mic ? pulseaudio_volume_get_muted_mic : pulseaudio_volume_get_muted) (notify->volume);
connected = pulseaudio_volume_get_connected (notify->volume);
volume_i = (gint) round (volume * 100);
if (!connected)
volume_i = 0;
if (!connected)
title = g_strdup_printf (_("Not connected to the PulseAudio server"));
else if (muted)
title = g_strdup_printf (_("Volume %d%c (muted)"), volume_i, '%');
else
title = g_strdup_printf (_("Volume %d%c"), volume_i, '%');
if (!connected)
icon = icons_array[V_MUTED];
else if (muted)
icon = icons_array[V_MUTED];
else if (volume <= 0.0)
icon = icons_array[V_MUTED];
else if (volume <= 0.3)
icon = icons_array[V_LOW];
else if (volume <= 0.7)
icon = icons_array[V_MEDIUM];
else
icon = icons_array[V_HIGH];
notify_notification_update (notification,
title,
NULL,
icon);
g_free (title);
if (notify->gauge_notifications)
{
notify_notification_set_hint (notification,
"value",
g_variant_new_int32 (MIN (100, volume_i)));
notify_notification_set_hint (notification,
"x-canonical-private-synchronous",
g_variant_new_string(""));
}
if (!notify_notification_show (notification, &error))
{
g_warning ("Error while sending notification : %s\n", error->message);
g_error_free (error);
}
}
void
pulseaudio_notify_volume_changed (PulseaudioNotify *notify,
gboolean should_notify,
PulseaudioVolume *volume)
{
g_return_if_fail (IS_PULSEAUDIO_NOTIFY (notify));
if (should_notify)
pulseaudio_notify_notify (notify, FALSE);
}
static void
pulseaudio_notify_volume_mic_changed (PulseaudioNotify *notify,
gboolean should_notify,
PulseaudioVolume *volume)
{
g_return_if_fail (IS_PULSEAUDIO_NOTIFY (notify));
if (should_notify)
pulseaudio_notify_notify (notify, TRUE);
}
PulseaudioNotify *
pulseaudio_notify_new (PulseaudioConfig *config,
PulseaudioVolume *volume,
PulseaudioButton *button)
{
PulseaudioNotify *notify;
g_return_val_if_fail (IS_PULSEAUDIO_CONFIG (config), NULL);
g_return_val_if_fail (IS_PULSEAUDIO_VOLUME (volume), NULL);
g_return_val_if_fail (IS_PULSEAUDIO_BUTTON (button), NULL);
notify = g_object_new (TYPE_PULSEAUDIO_NOTIFY, NULL);
notify->config = config;
notify->volume = volume;
notify->button = button;
notify->volume_changed_id =
g_signal_connect_swapped (G_OBJECT (notify->volume), "volume-changed",
G_CALLBACK (pulseaudio_notify_volume_changed), notify);
notify->volume_mic_changed_id =
g_signal_connect_swapped (G_OBJECT (notify->volume), "volume-mic-changed",
G_CALLBACK (pulseaudio_notify_volume_mic_changed), notify);
return notify;
}
#endif /* HAVE_LIBNOTIFY */
|