clock.c 10.4 KB
Newer Older
1
/*
Jonny Lamb's avatar
Jonny Lamb committed
2
 * Copyright (C) 2014 Collabora Ltd.
3
 *
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * 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 St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
Jonny Lamb's avatar
Jonny Lamb committed
19
 * Author: Jonny Lamb <jonny.lamb@collabora.co.uk>
20
21
22
23
 */

#include "config.h"

Jonny Lamb's avatar
Jonny Lamb committed
24
#include <alsa/asoundlib.h>
25
26
27
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-wall-clock.h>

Jonny Lamb's avatar
Jonny Lamb committed
28
29
#include "clock.h"

30
31
32
33
34
35
enum {
  VOLUME_CHANGED,
  N_SIGNALS
};
static guint signals[N_SIGNALS] = { 0 };

36
struct MaynardClockPrivate {
37
  GtkWidget *revealer_clock;
38
  GtkWidget *revealer_system;
39
40
  GtkWidget *revealer_volume;

Jonny Lamb's avatar
Jonny Lamb committed
41
  GtkWidget *label;
42

43
44
45
  GtkWidget *volume_scale;
  GtkWidget *volume_image;

46
  GnomeWallClock *wall_clock;
Jonny Lamb's avatar
Jonny Lamb committed
47
48
49
50

  snd_mixer_t *mixer_handle;
  snd_mixer_elem_t *mixer;
  glong min_volume, max_volume;
51
52
};

53
G_DEFINE_TYPE(MaynardClock, maynard_clock, GTK_TYPE_WINDOW)
54
55

static void
56
maynard_clock_init (MaynardClock *self)
57
{
Jonny Lamb's avatar
Jonny Lamb committed
58
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
59
60
      MAYNARD_CLOCK_TYPE,
      MaynardClockPrivate);
61
62
}

Jonny Lamb's avatar
Jonny Lamb committed
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
static gdouble
alsa_volume_to_percentage (MaynardClock *self,
    glong value)
{
  glong range;

  /* min volume isn't always zero unfortunately */
  range = self->priv->max_volume - self->priv->min_volume;

  value -= self->priv->min_volume;

  return (value / (gdouble) range) * 100;
}

static glong
percentage_to_alsa_volume (MaynardClock *self,
    gdouble value)
{
  glong range;

  /* min volume isn't always zero unfortunately */
  range = self->priv->max_volume - self->priv->min_volume;

  return (range * value / 100) + self->priv->min_volume;
}

89
90
91
92
93
94
95
96
97
98
static void
volume_changed_cb (GtkRange *range,
    MaynardClock *self)
{
  gdouble value;
  const gchar *icon_name;
  GtkWidget *box;

  value = gtk_range_get_value (range);

Jonny Lamb's avatar
Jonny Lamb committed
99
  if (self->priv->mixer != NULL)
100
    {
Jonny Lamb's avatar
Jonny Lamb committed
101
102
      snd_mixer_selem_set_playback_volume_all (self->priv->mixer,
          percentage_to_alsa_volume (self, value));
103
104
105
106
107
    }

  /* update the icon */
  if (value > 75)
    icon_name = "audio-volume-high-symbolic";
108
  else if (value > 30)
109
    icon_name = "audio-volume-medium-symbolic";
110
  else if (value > 0)
111
    icon_name = "audio-volume-low-symbolic";
112
  else
113
114
115
116
117
118
119
120
121
122
123
124
125
    icon_name = "audio-volume-muted-symbolic";

  box = gtk_widget_get_parent (self->priv->volume_image);
  gtk_widget_destroy (self->priv->volume_image);
  self->priv->volume_image = gtk_image_new_from_icon_name (
      icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
  gtk_box_pack_start (GTK_BOX (box), self->priv->volume_image,
      FALSE, FALSE, 0);
  gtk_widget_show (self->priv->volume_image);

  g_signal_emit (self, signals[VOLUME_CHANGED], 0, value, icon_name);
}

126
127
128
static gboolean
volume_idle_cb (gpointer data)
{
Jonny Lamb's avatar
Jonny Lamb committed
129
130
  MaynardClock *self = MAYNARD_CLOCK (data);
  glong volume;
131

Jonny Lamb's avatar
Jonny Lamb committed
132
133
134
135
136
137
138
139
  if (self->priv->mixer != NULL)
    {
      snd_mixer_selem_get_playback_volume (self->priv->mixer,
          SND_MIXER_SCHN_MONO, &volume);

      gtk_range_set_value (GTK_RANGE (self->priv->volume_scale),
          alsa_volume_to_percentage (self, volume));
    }
140
141
142
143

  return G_SOURCE_REMOVE;
}

144
145
146
static GtkWidget *
create_system_box (MaynardClock *self)
{
147
  return gtk_label_new ("Not implemented");
148
149
}

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
static GtkWidget *
create_volume_box (MaynardClock *self)
{
  GtkWidget *box;

  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);

  self->priv->volume_image = gtk_image_new_from_icon_name (
      "audio-volume-high-symbolic",
      GTK_ICON_SIZE_LARGE_TOOLBAR);
  gtk_box_pack_start (GTK_BOX (box), self->priv->volume_image,
      FALSE, FALSE, 0);

  self->priv->volume_scale = gtk_scale_new_with_range (
      GTK_ORIENTATION_HORIZONTAL, 0, 100, 1);
  gtk_scale_set_draw_value (GTK_SCALE (self->priv->volume_scale), FALSE);
  gtk_widget_set_size_request (self->priv->volume_scale, 100, -1);
  gtk_box_pack_end (GTK_BOX (box), self->priv->volume_scale, TRUE, TRUE, 0);

  g_signal_connect (self->priv->volume_scale, "value-changed",
      G_CALLBACK (volume_changed_cb), self);

172
173
174
  /* set the initial value in an idle so ::volume-changed is emitted
   * when other widgets are connected to the signal and can react
   * accordingly. */
Jonny Lamb's avatar
Jonny Lamb committed
175
  g_idle_add (volume_idle_cb, self);
176

177
178
179
  return box;
}

180
181
182
static void
wall_clock_notify_cb (GnomeWallClock *wall_clock,
    GParamSpec *pspec,
183
    MaynardClock *self)
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{
  GDateTime *datetime;
  gchar *str;

  datetime = g_date_time_new_now_local ();

  str = g_date_time_format (datetime,
      "<span font=\"Droid Sans 32\">%H:%M</span>\n"
      "<span font=\"Droid Sans 12\">%d/%m/%Y</span>");
  gtk_label_set_markup (GTK_LABEL (self->priv->label), str);

  g_free (str);
  g_date_time_unref (datetime);
}

Jonny Lamb's avatar
Jonny Lamb committed
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
static void
setup_mixer (MaynardClock *self)
{
  snd_mixer_selem_id_t *sid;
  gint ret;

  /* this is all pretty specific to the rpi */

  if ((ret = snd_mixer_open (&self->priv->mixer_handle, 0)) < 0)
    goto error;

  if ((ret = snd_mixer_attach (self->priv->mixer_handle, "default")) < 0)
    goto error;

  if ((ret = snd_mixer_selem_register (self->priv->mixer_handle, NULL, NULL)) < 0)
    goto error;

  if ((ret = snd_mixer_load (self->priv->mixer_handle)) < 0)
    goto error;

  snd_mixer_selem_id_alloca (&sid);
  snd_mixer_selem_id_set_index (sid, 0);
  snd_mixer_selem_id_set_name (sid, "PCM");
  self->priv->mixer = snd_mixer_find_selem (self->priv->mixer_handle, sid);

  /* fallback to mixer "Master" */
  if (self->priv->mixer == NULL)
    {
      snd_mixer_selem_id_set_name (sid, "Master");
      self->priv->mixer = snd_mixer_find_selem (self->priv->mixer_handle, sid);
      if (self->priv->mixer == NULL)
        goto error;
    }

  if ((ret = snd_mixer_selem_get_playback_volume_range (self->priv->mixer,
              &self->priv->min_volume, &self->priv->max_volume)) < 0)
    goto error;

  return;

error:
  g_debug ("failed to setup mixer: %s", snd_strerror (ret));

  if (self->priv->mixer_handle != NULL)
    snd_mixer_close (self->priv->mixer_handle);
  self->priv->mixer_handle = NULL;
  self->priv->mixer = NULL;
}

248
static void
249
maynard_clock_constructed (GObject *object)
250
{
251
  MaynardClock *self = MAYNARD_CLOCK (object);
252
  GtkWidget *box, *system_box, *volume_box;
253

254
  G_OBJECT_CLASS (maynard_clock_parent_class)->constructed (object);
255

256
257
258
259
  self->priv->wall_clock = g_object_new (GNOME_TYPE_WALL_CLOCK, NULL);
  g_signal_connect (self->priv->wall_clock, "notify::clock",
      G_CALLBACK (wall_clock_notify_cb), self);

260
  gtk_window_set_title (GTK_WINDOW (self), "maynard");
Jonny Lamb's avatar
Jonny Lamb committed
261
262
263
264
265
  gtk_window_set_decorated (GTK_WINDOW (self), FALSE);
  gtk_widget_realize (GTK_WIDGET (self));

  gtk_style_context_add_class (
      gtk_widget_get_style_context (GTK_WIDGET (self)),
266
      "maynard-clock");
Jonny Lamb's avatar
Jonny Lamb committed
267

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  /* the box for the revealers */
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_container_add (GTK_CONTAINER (self), box);

  /* volume */
  self->priv->revealer_volume = gtk_revealer_new ();
  gtk_revealer_set_transition_type (
      GTK_REVEALER (self->priv->revealer_volume),
      GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT);
  gtk_revealer_set_reveal_child (
      GTK_REVEALER (self->priv->revealer_volume), FALSE);
  gtk_box_pack_start (GTK_BOX (box), self->priv->revealer_volume,
      TRUE, TRUE, 0);

  volume_box = create_volume_box (self);
  gtk_container_add (GTK_CONTAINER (self->priv->revealer_volume),
      volume_box);

286
287
288
289
290
291
292
293
294
295
296
297
298
299
  /* system */
  self->priv->revealer_system = gtk_revealer_new ();
  gtk_revealer_set_transition_type (
      GTK_REVEALER (self->priv->revealer_system),
      GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT);
  gtk_revealer_set_reveal_child (
      GTK_REVEALER (self->priv->revealer_system), FALSE);
  gtk_box_pack_start (GTK_BOX (box), self->priv->revealer_system,
      TRUE, TRUE, 0);

  system_box = create_system_box (self);
  gtk_container_add (GTK_CONTAINER (self->priv->revealer_system),
      system_box);

300
301
302
303
304
305
306
307
308
309
  /* clock */
  self->priv->revealer_clock = gtk_revealer_new ();
  gtk_revealer_set_transition_type (
      GTK_REVEALER (self->priv->revealer_clock),
      GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT);
  gtk_revealer_set_reveal_child (
      GTK_REVEALER (self->priv->revealer_clock), TRUE);
  gtk_box_pack_start (GTK_BOX (box), self->priv->revealer_clock,
      TRUE, TRUE, 0);

Jonny Lamb's avatar
Jonny Lamb committed
310
311
  self->priv->label = gtk_label_new ("");
  gtk_label_set_justify (GTK_LABEL (self->priv->label), GTK_JUSTIFY_CENTER);
312
313
  gtk_container_add (GTK_CONTAINER (self->priv->revealer_clock),
      self->priv->label);
314

315
316
317
318
  /* TODO: work out how to fix the padding properly. this is added to
   * fix the broken alignment where the clock appears to the right. */
  gtk_box_pack_start (GTK_BOX (box), gtk_revealer_new (), TRUE, TRUE, 0);

Jonny Lamb's avatar
Jonny Lamb committed
319
320
  setup_mixer (self);

321
  wall_clock_notify_cb (self->priv->wall_clock, NULL, self);
322
323
324
}

static void
325
maynard_clock_dispose (GObject *object)
326
{
327
  MaynardClock *self = MAYNARD_CLOCK (object);
328

329
  g_clear_object (&self->priv->wall_clock);
330

Jonny Lamb's avatar
Jonny Lamb committed
331
332
333
334
335
  if (self->priv->mixer_handle != NULL)
    snd_mixer_close (self->priv->mixer_handle);
  self->priv->mixer_handle = NULL;
  self->priv->mixer = NULL;

336
  G_OBJECT_CLASS (maynard_clock_parent_class)->dispose (object);
337
338
339
}

static void
340
maynard_clock_class_init (MaynardClockClass *klass)
341
342
343
{
  GObjectClass *object_class = (GObjectClass *)klass;

344
345
  object_class->constructed = maynard_clock_constructed;
  object_class->dispose = maynard_clock_dispose;
346

347
348
349
350
  signals[VOLUME_CHANGED] = g_signal_new ("volume-changed",
      G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
      NULL, G_TYPE_NONE, 2, G_TYPE_DOUBLE, G_TYPE_STRING);

351
  g_type_class_add_private (object_class, sizeof (MaynardClockPrivate));
352
353
354
}

GtkWidget *
355
maynard_clock_new (void)
356
{
357
  return g_object_new (MAYNARD_CLOCK_TYPE,
Jonny Lamb's avatar
Jonny Lamb committed
358
      NULL);
359
}
360
361

void
362
363
maynard_clock_show_section (MaynardClock *self,
    MaynardClockSection section)
364
365
{
  gtk_revealer_set_reveal_child (
366
367
368
369
370
371
372
      GTK_REVEALER (self->priv->revealer_clock),
      section == MAYNARD_CLOCK_SECTION_CLOCK);

  gtk_revealer_set_reveal_child (
      GTK_REVEALER (self->priv->revealer_system),
      section == MAYNARD_CLOCK_SECTION_SYSTEM);

373
  gtk_revealer_set_reveal_child (
374
375
      GTK_REVEALER (self->priv->revealer_volume),
      section == MAYNARD_CLOCK_SECTION_VOLUME);
376
}