~ubuntu-branches/ubuntu/trusty/indicator-sound-gtk2/trusty-proposed

« back to all changes in this revision

Viewing changes to src/voip-input-menu-item.c

  • Committer: Package Import Robot
  • Author(s): Lionel Le Folgoc
  • Date: 2012-09-10 00:09:01 UTC
  • Revision ID: package-import@ubuntu.com-20120910000901-q6469svv5d3pmn0y
Tags: upstream-12.10.0.1
ImportĀ upstreamĀ versionĀ 12.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2011 Canonical Ltd.
 
3
 
 
4
Authors:
 
5
    Conor Curran <conor.curran@canonical.com>
 
6
 
 
7
This program is free software: you can redistribute it and/or modify it
 
8
under the terms of the GNU General Public License version 3, as published
 
9
by the Free Software Foundation.
 
10
 
 
11
This program is distributed in the hope that it will be useful, but
 
12
WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
PURPOSE.  See the GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License along
 
17
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
#ifdef HAVE_CONFIG_H
 
20
#include "config.h"
 
21
#endif
 
22
 
 
23
#include <glib/gi18n.h>
 
24
#include "voip-input-menu-item.h"
 
25
#include "common-defs.h"
 
26
#include "pulseaudio-mgr.h"
 
27
 
 
28
typedef struct _VoipInputMenuItemPrivate VoipInputMenuItemPrivate;
 
29
 
 
30
struct _VoipInputMenuItemPrivate {
 
31
  Device*             a_sink;
 
32
  pa_cvolume          volume;
 
33
  gint                mute;
 
34
  guint32             volume_steps;
 
35
  pa_channel_map      channel_map;
 
36
  pa_volume_t         base_volume;
 
37
  gint                source_index;
 
38
  gint                source_output_index;
 
39
  gint                client_index;
 
40
};
 
41
 
 
42
#define VOIP_INPUT_MENU_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VOIP_INPUT_MENU_ITEM_TYPE, VoipInputMenuItemPrivate))
 
43
 
 
44
/* Prototypes */
 
45
static void voip_input_menu_item_class_init (VoipInputMenuItemClass *klass);
 
46
static void voip_input_menu_item_init       (VoipInputMenuItem *self);
 
47
static void voip_input_menu_item_dispose    (GObject *object);
 
48
static void voip_input_menu_item_finalize   (GObject *object);
 
49
static void handle_event (DbusmenuMenuitem * mi, const gchar * name,
 
50
                          GVariant * value, guint timestamp);
 
51
// TODO:
 
52
// This method should really be shared between this and the volume slider obj
 
53
// perfectly static - wait until the device mgr wrapper is properly sorted and
 
54
// then consolidate
 
55
static pa_cvolume voip_input_menu_item_construct_mono_volume (const pa_cvolume* vol);
 
56
 
 
57
G_DEFINE_TYPE (VoipInputMenuItem, voip_input_menu_item, DBUSMENU_TYPE_MENUITEM);
 
58
 
 
59
static void
 
60
voip_input_menu_item_class_init (VoipInputMenuItemClass *klass)
 
61
{
 
62
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
63
 
 
64
  g_type_class_add_private (klass, sizeof (VoipInputMenuItemPrivate));
 
65
 
 
66
  object_class->dispose = voip_input_menu_item_dispose;
 
67
  object_class->finalize = voip_input_menu_item_finalize;
 
68
 
 
69
  DbusmenuMenuitemClass * mclass = DBUSMENU_MENUITEM_CLASS(klass);
 
70
  mclass->handle_event = handle_event;
 
71
}
 
72
 
 
73
static void
 
74
voip_input_menu_item_init (VoipInputMenuItem *self)
 
75
{
 
76
  dbusmenu_menuitem_property_set( DBUSMENU_MENUITEM(self),
 
77
                                  DBUSMENU_MENUITEM_PROP_TYPE,
 
78
                                  DBUSMENU_VOIP_INPUT_MENUITEM_TYPE );
 
79
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (self);
 
80
  dbusmenu_menuitem_property_set_bool( DBUSMENU_MENUITEM(self),
 
81
                                       DBUSMENU_MENUITEM_PROP_VISIBLE,
 
82
                                       FALSE );
 
83
 
 
84
  priv->source_index     = NOT_ACTIVE;
 
85
  priv->source_output_index = NOT_ACTIVE;
 
86
  priv->client_index     = NOT_ACTIVE;
 
87
  priv->mute             = NOT_ACTIVE;
 
88
}
 
89
 
 
90
static void
 
91
voip_input_menu_item_dispose (GObject *object)
 
92
{
 
93
  G_OBJECT_CLASS (voip_input_menu_item_parent_class)->dispose (object);
 
94
  return;
 
95
}
 
96
 
 
97
static void
 
98
voip_input_menu_item_finalize (GObject *object)
 
99
{
 
100
  G_OBJECT_CLASS (voip_input_menu_item_parent_class)->finalize (object);
 
101
}
 
102
 
 
103
static void
 
104
handle_event (DbusmenuMenuitem * mi,
 
105
              const gchar * name,
 
106
              GVariant * value,
 
107
              guint timestamp)
 
108
{
 
109
  GVariant* input = NULL;
 
110
  input = value;
 
111
  if (g_variant_is_of_type(value, G_VARIANT_TYPE_VARIANT) == TRUE) {
 
112
    input = g_variant_get_variant(value);
 
113
  }
 
114
 
 
115
  gdouble percent = g_variant_get_double(input);
 
116
  if (value != NULL){
 
117
    if (IS_VOIP_INPUT_MENU_ITEM (mi)) {
 
118
      VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (VOIP_INPUT_MENU_ITEM (mi));
 
119
/*
 
120
      g_debug ("Handle event in the voip input level backend instance - %f", percent);
 
121
*/
 
122
      pa_cvolume new_volume;
 
123
      pa_cvolume_init(&new_volume);
 
124
      new_volume.channels = 1;
 
125
      pa_volume_t new_volume_value = (pa_volume_t) ((percent * PA_VOLUME_NORM) / 100);
 
126
      pa_cvolume_set(&new_volume, 1, new_volume_value);
 
127
 
 
128
      pm_update_mic_gain (priv->source_index, new_volume);
 
129
      // finally unmute if needed
 
130
      if (priv->mute == 1) {
 
131
        pm_update_mic_mute (priv->source_index, 0);
 
132
      }
 
133
    }
 
134
  }
 
135
}
 
136
 
 
137
static pa_cvolume
 
138
voip_input_menu_item_construct_mono_volume (const pa_cvolume* vol)
 
139
{
 
140
  pa_cvolume new_volume;
 
141
  pa_cvolume_init(&new_volume);
 
142
  new_volume.channels = 1;
 
143
  pa_volume_t max_vol = pa_cvolume_max(vol);
 
144
  pa_cvolume_set(&new_volume, 1, max_vol);
 
145
  return new_volume;
 
146
}
 
147
 
 
148
void
 
149
voip_input_menu_item_update (VoipInputMenuItem* item,
 
150
                             const pa_source_info* source)
 
151
{
 
152
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
153
  // only overwrite the constants of each source if the device has changed
 
154
  if (priv->source_index == NOT_ACTIVE){
 
155
    priv->base_volume = source->base_volume;
 
156
    priv->volume_steps = source->n_volume_steps;
 
157
    priv->channel_map = source->channel_map;
 
158
    priv->source_index = source->index;
 
159
  }
 
160
  priv->volume = voip_input_menu_item_construct_mono_volume (&source->volume);
 
161
  pa_volume_t vol = pa_cvolume_max (&source->volume);
 
162
  gdouble update = ((gdouble) vol * 100) / PA_VOLUME_NORM;
 
163
  
 
164
  GVariant* new_volume = g_variant_new_double(update);
 
165
  dbusmenu_menuitem_property_set_variant(DBUSMENU_MENUITEM(item),
 
166
                                         DBUSMENU_VOIP_INPUT_MENUITEM_LEVEL,
 
167
                                         new_volume);
 
168
  // Only send over the mute updates if the state has changed.
 
169
  // in this order - volume first mute last!!
 
170
  if (priv->mute != source->mute){
 
171
/*
 
172
    g_debug ("voip menu item - update - mute = %i", priv->mute);
 
173
*/
 
174
    GVariant* new_mute_update = g_variant_new_int32 (source->mute);
 
175
    dbusmenu_menuitem_property_set_variant (DBUSMENU_MENUITEM(item),
 
176
                                            DBUSMENU_VOIP_INPUT_MENUITEM_MUTE,
 
177
                                            new_mute_update);
 
178
  }
 
179
 
 
180
  priv->mute = source->mute;
 
181
 
 
182
}
 
183
 
 
184
gboolean
 
185
voip_input_menu_item_is_interested (VoipInputMenuItem* item,
 
186
                                    gint source_output_index,
 
187
                                    gint client_index)
 
188
{
 
189
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
190
  // Check to make sure we are not handling another voip beforehand and that we
 
191
  // have an active sink (might need to match up at start up)
 
192
  if (priv->source_output_index != NOT_ACTIVE &&
 
193
      priv->source_index != NOT_ACTIVE){
 
194
    return FALSE;
 
195
  }
 
196
  
 
197
  priv->source_output_index = source_output_index;
 
198
  priv->client_index     = client_index;
 
199
 
 
200
  return TRUE;
 
201
}
 
202
 
 
203
gboolean
 
204
voip_input_menu_item_is_active (VoipInputMenuItem* item)
 
205
{
 
206
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
207
  return (priv->source_output_index != NOT_ACTIVE && priv->client_index != NOT_ACTIVE);
 
208
}
 
209
 
 
210
 
 
211
gboolean
 
212
voip_input_menu_item_is_populated (VoipInputMenuItem* item)
 
213
{
 
214
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
215
  return priv->source_index != NOT_ACTIVE;
 
216
}
 
217
 
 
218
gint
 
219
voip_input_menu_item_get_index (VoipInputMenuItem* item)
 
220
{
 
221
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
222
  return priv->source_index;
 
223
}
 
224
 
 
225
gint
 
226
voip_input_menu_item_get_source_output_index (VoipInputMenuItem* item)
 
227
{
 
228
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
229
 
 
230
  return priv->source_output_index;
 
231
}
 
232
 
 
233
/**
 
234
 * If the pulse server informs of a default source change
 
235
 * or the source in question is removed.
 
236
 * @param item
 
237
 */
 
238
void
 
239
voip_input_menu_item_deactivate_source (VoipInputMenuItem* item, gboolean visible)
 
240
{
 
241
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
242
  priv->source_index = NOT_ACTIVE;
 
243
  dbusmenu_menuitem_property_set_bool( DBUSMENU_MENUITEM(item),
 
244
                                       DBUSMENU_MENUITEM_PROP_VISIBLE,
 
245
                                       visible );
 
246
}
 
247
 
 
248
void
 
249
voip_input_menu_item_deactivate_voip_client (VoipInputMenuItem* item)
 
250
{
 
251
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
252
  priv->client_index = NOT_ACTIVE;
 
253
  priv->source_output_index = NOT_ACTIVE;
 
254
  voip_input_menu_item_enable (item, FALSE);
 
255
}
 
256
 
 
257
void
 
258
voip_input_menu_item_enable (VoipInputMenuItem* item,
 
259
                             gboolean active)
 
260
{
 
261
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (item);
 
262
  if (priv->source_index == NOT_ACTIVE && active == TRUE) {
 
263
    g_warning ("Tried to enable the VOIP menuitem but we don't have an active source ??");
 
264
    active = FALSE;
 
265
  }
 
266
  dbusmenu_menuitem_property_set_bool( DBUSMENU_MENUITEM(item),
 
267
                                       DBUSMENU_MENUITEM_PROP_VISIBLE,
 
268
                                       active );
 
269
}
 
270
 
 
271
VoipInputMenuItem*
 
272
voip_input_menu_item_new (Device* sink)
 
273
{
 
274
  VoipInputMenuItem *self = g_object_new(VOIP_INPUT_MENU_ITEM_TYPE, NULL);
 
275
  VoipInputMenuItemPrivate* priv = VOIP_INPUT_MENU_ITEM_GET_PRIVATE (self);
 
276
  priv->a_sink = sink;
 
277
  return self;
 
278
}
 
 
b'\\ No newline at end of file'