aboutsummaryrefslogtreecommitdiff
path: root/src/sound-state-manager.c
blob: 1eecaf08d87bfbb6081d847763b5efdc48f825ac (plain)
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
/*
Copyright 2011 Canonical Ltd.

Authors:
    Conor Curran <conor.curran@canonical.com>

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
*/

#include <libindicator/indicator-image-helper.h>

#include "sound-state-manager.h"


typedef struct _SoundStateManagerPrivate SoundStateManagerPrivate;

// TODO ensure all the relevant below are initialized to null in init
struct _SoundStateManagerPrivate
{
  GDBusProxy* dbus_proxy;  
  GHashTable* volume_states;  
  GList* blocked_animation_list;
  SoundState current_state;
  GtkImage* speaker_image;
};

#define SOUND_STATE_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUND_TYPE_STATE_MANAGER, SoundStateManagerPrivate))

static GtkIconSize design_team_size;
static gint blocked_id;
static gint animation_id;
static GList* blocked_iter = NULL;

static void sound_state_manager_prepare_blocked_animation(SoundStateManager* self);
static gboolean sound_state_manager_start_animation (SoundStateManager* self)
static gboolean sound_state_manager_fade_back_to_mute_image (gpointer user_data)
static void sound_state_manager_reset_mute_blocking_animation (SoundStateManager* self);
static void sound_state_mananger_free_the_animation_list (SoundStateManager* self);
static void sound_state_manager_prepare_state_image_names (SoundStateManager* self);

G_DEFINE_TYPE (SoundStateManager, sound_state_manager, G_TYPE_OBJECT);

static void
sound_state_manager_init (SoundStateManager* self)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);

  sound_state_manager_prepare_state_image_names (self);
  sound_state_manager_prepare_blocked_animation (self);

  priv->current_state = UNAVAILABLE;
  priv->speaker_image = indicator_image_helper(g_hash_table_lookup (priv->volume_states,
                                                                    GINT_TO_POINTER(priv->current_state));
}

static void
sound_state_manager_finalize (GObject *object)
{
	/* TODO: Add deinitalization code here */

	G_OBJECT_CLASS (sound_state_manager_parent_class)->finalize (object);
}

static void
sound_state_manager_class_init (SoundStateManagerClass *klass)
{
	GObjectClass* object_class = G_OBJECT_CLASS (klass);
	GObjectClass* parent_class = G_OBJECT_CLASS (klass);

	object_class->finalize = sound_state_manager_finalize;
  design_team_size = gtk_icon_size_register("design-team-size", 22, 22);  
}

/*
Prepare states versus images names hash.
*/
static void
sound_state_manager_prepare_state_image_names (SoundStateManager* self)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  priv->volume_states = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free); 
  g_hash_table_insert(priv->volume_states, GINT_TO_POINTER(MUTED), g_strdup("audio-volume-muted-panel"));
  g_hash_table_insert(priv->volume_states, GINT_TO_POINTER(ZERO_LEVEL), g_strdup("audio-volume-low-zero-panel"));
  g_hash_table_insert(priv->volume_states, GINT_TO_POINTER(LOW_LEVEL), g_strdup("audio-volume-low-panel"));
  g_hash_table_insert(priv->volume_states, GINT_TO_POINTER(MEDIUM_LEVEL), g_strdup("audio-volume-medium-panel"));
  g_hash_table_insert(priv->volume_states, GINT_TO_POINTER(HIGH_LEVEL), g_strdup("audio-volume-high-panel"));
  g_hash_table_insert(priv->volume_states, GINT_TO_POINTER(BLOCKED), g_strdup("audio-volume-muted-blocking-panel"));
  g_hash_table_insert(priv->volume_states, GINT_TO_POINTER(UNAVAILABLE), g_strdup("audio-output-none-panel"));
}

/*
prepare_blocked_animation:
Prepares the array of images to be used in the blocked animation.
Only called at startup.
*/
static void
sound_state_manager_prepare_blocked_animation (SoundStateManager* self)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  
  gchar* blocked_name = g_hash_table_lookup(priv->volume_states,
                                            GINT_TO_POINTER(BLOCKED));
  gchar* muted_name = g_hash_table_lookup(priv->volume_states,
                                          GINT_TO_POINTER(MUTED));

  GtkImage* temp_image = indicator_image_helper(muted_name);
  GdkPixbuf* mute_buf = gtk_image_get_pixbuf(temp_image);

  temp_image = indicator_image_helper(blocked_name);
  GdkPixbuf* blocked_buf = gtk_image_get_pixbuf(temp_image);

  if (mute_buf == NULL || blocked_buf == NULL) {
    //g_debug("Don bother with the animation, the theme aint got the goods !");
    return;
  }

  int i;

  // sample 51 snapshots - range : 0-256
  for (i = 0; i < 51; i++) {
    gdk_pixbuf_composite(mute_buf, blocked_buf, 0, 0,
                         gdk_pixbuf_get_width(mute_buf),
                         gdk_pixbuf_get_height(mute_buf),
                         0, 0, 1, 1, GDK_INTERP_BILINEAR, MIN(255, i * 5));
    priv->blocked_animation_list = g_list_append(priv->blocked_animation_list,
                                                 gdk_pixbuf_copy(blocked_buf));
  }
  g_object_ref_sink(mute_buf);
  g_object_unref(mute_buf);
  g_object_ref_sink(blocked_buf);
  g_object_unref(blocked_buf);
}


GtkImage*
sound_state_manager_get_current_icon (SoundStateManager* self)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  return priv->speaker_image;  
}

SoundState
sound_state_manager_get_current_state (SoundStateManager* self)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  return priv->current_state;
}

static void 
sound_state_signal_cb ( GDBusProxy* proxy,
                        gchar* sender_name,
                        gchar* signal_name,
                        GVariant* parameters,
                        gpointer user_data)
{
  g_return_if_fail (SOUND_IS_STATE_MANAGER (user_data));
  SoundStateManager* self = SOUND_STATE_MANAGER (user_data);
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);

  g_variant_ref (parameters);
  GVariant *value = g_variant_get_child_value (parameters, 0);
  gint update = g_variant_get_int (value);

  g_debug ( "!!! signal_cb with value %i", update);

  priv->current_state = (SoundState)update;

  g_variant_unref (parameters);

  
  /*if (g_strcmp0(signal_name, INDICATOR_SOUND_SIGNAL_SINK_AVAILABLE_UPDATE) == 0){
    react_to_signal_sink_availability_update ( input, self );
  }
  else if (g_strcmp0(signal_name, INDICATOR_SOUND_SIGNAL_SINK_MUTE_UPDATE) == 0){
    react_to_signal_sink_mute_update ( input, self );
  }
  else if (g_strcmp0(signal_name, INDICATOR_SOUND_SIGNAL_SINK_INPUT_WHILE_MUTED) == 0){
    react_to_signal_sink_input_while_muted ( input, self );
  }*/
  
}

void
sound_state_manager_style_changed_cb(GtkWidget *widget, gpointer user_data)
{
  //g_debug("Just caught a style change event");
  g_return_if_fail (SOUND_IS_STATE_MANAGER (user_data));
  SoundStateManager* self = SOUND_STATE_MANAGER (user_data);
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  sound_state_manager_reset_mute_blocking_animation (self)
  sound_state_mananger_free_the_animation_list (self);
  sound_state_manager_prepare_blocked_animation (self);
}

static void
sound_state_manager_reset_mute_blocking_animation (SoundStateManager* self)
{
  if (animation_id != 0) {
    //g_debug("about to remove the animation_id callback from the mainloop!!**");
    g_source_remove(animation_id);
    animation_id = 0;
  }
  if (blocked_id != 0) {
    //g_debug("about to remove the blocked_id callback from the mainloop!!**");
    g_source_remove(blocked_id);
    blocked_id = 0;
  }
}

static void
sound_state_mananger_free_the_animation_list (SoundStateManager* self)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  
  if (priv->blocked_animation_list != NULL) {
    g_list_foreach (priv->blocked_animation_list, (GFunc)g_object_unref, NULL);
    g_list_free (priv->blocked_animation_list);
    blocked_animation_list = NULL;
  }
}

/*static void
update_state(const gint state)
{
  previous_state = current_state;
  current_state = state;
  gchar* image_name = g_hash_table_lookup(volume_states, GINT_TO_POINTER(current_state));
  indicator_image_helper_update(speaker_image, image_name);
}*/

static gboolean
sound_state_manager_start_animation (SoundStateManager* self)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  
  blocked_iter = priv->blocked_animation_list;
  blocked_id = 0;
  animation_id = g_timeout_add (50,
                                sound_state_manager_fade_back_to_mute_image,
                                self);
  return FALSE;
}

static gboolean
sound_state_manager_fade_back_to_mute_image (gpointer user_data)
{
  g_return_if_fail (SOUND_IS_STATE_MANAGER (user_data));
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE( SOUND_STATE_MANAGER (user_data) );

  if (blocked_iter != NULL) {
    gtk_image_set_from_pixbuf (priv->speaker_image, blocked_iter->data);
    blocked_iter = blocked_iter->next;
    return TRUE;
  } else {
    animation_id = 0;
    //g_debug("exit from animation now\n");
    return FALSE;
  }
}

/**
 * sound_state_manager_connect_to_dbus:
 * @returns: void
 * When ready the indicator-sound calls this method to enable state communication 
 * between the indicator and the service. 
 **/
void
sound_state_manager_connect_to_dbus (SoundStateManager* self, GDProxy* proxy)
{
  SoundStateManagerPrivate* priv = SOUND_STATE_MANAGER_GET_PRIVATE(self);
  priv->dbus_proxy = proxy;
  g_signal_connect (priv->dbus_proxy, "g-signal",
                    G_CALLBACK (sound_state_signal_cb), self);
  
}