summaryrefslogtreecommitdiffstats
path: root/panels/wacom/calibrator/cc-clock.c
blob: 26afd813643a6bf0198928c013b69c0c9d464df2 (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
286
287
288
289
/*
 * Copyright © 2018 Red Hat, Inc.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Joaquim Rocha <jrocha@redhat.com>
 *          Carlos Garnacho <carlosg@gnome.org>
 */
#include "config.h"
#include "cc-clock.h"

#include <math.h>

#define CLOCK_RADIUS       50
#define CLOCK_LINE_WIDTH   10
#define CLOCK_LINE_PADDING 10
#define EXTRA_SPACE        2

typedef struct _CcClock CcClock;

struct _CcClock
{
  GtkWidget parent_instance;
  guint duration;
  gint64 start_time;
  gboolean running;
};

enum
{
  PROP_DURATION = 1,
  N_PROPS
};

static GParamSpec *props[N_PROPS] = { 0, };

enum {
  FINISHED,
  N_SIGNALS
};

static guint signals[N_SIGNALS] = { 0, };

G_DEFINE_TYPE (CcClock, cc_clock, GTK_TYPE_WIDGET)

static gint64
cc_clock_get_time_diff (CcClock *clock)
{
  GdkFrameClock *frame_clock;
  gint64 current_time;

  frame_clock = gtk_widget_get_frame_clock (GTK_WIDGET (clock));
  current_time = gdk_frame_clock_get_frame_time (frame_clock);

  return current_time - clock->start_time;
}

static gdouble
cc_clock_get_angle (CcClock *clock)
{
  gint64 time_diff;

  time_diff = cc_clock_get_time_diff (clock);

  if (time_diff > clock->duration * 1000)
    return 360;

  return ((gdouble) time_diff / (clock->duration * 1000)) * 360;
}

static void
cc_clock_snapshot (GtkWidget   *widget,
                   GtkSnapshot *snapshot)
{
  GtkAllocation allocation;
  cairo_t *cr;
  gdouble angle;

  gtk_widget_get_allocation (widget, &allocation);
  angle = cc_clock_get_angle (CC_CLOCK (widget));

  cr = gtk_snapshot_append_cairo (snapshot,
                                  &GRAPHENE_RECT_INIT (0, 0, allocation.width, allocation.height));

  /* Draw the clock background */
  cairo_arc (cr, allocation.width / 2, allocation.height / 2, CLOCK_RADIUS / 2, 0.0, 2.0 * M_PI);
  cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
  cairo_fill_preserve (cr);
  cairo_stroke (cr);

  cairo_set_line_width (cr, CLOCK_LINE_WIDTH);

  cairo_arc (cr,
             allocation.width / 2,
             allocation.height / 2,
             (CLOCK_RADIUS - CLOCK_LINE_WIDTH - CLOCK_LINE_PADDING) / 2,
             3 * M_PI_2,
             3 * M_PI_2 + angle * M_PI / 180.0);
  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_stroke (cr);
}

static void
cc_clock_stop (CcClock *clock)
{
  GdkFrameClock *frame_clock;

  if (!clock->running)
    return;

  frame_clock = gtk_widget_get_frame_clock (GTK_WIDGET (clock));

  gdk_frame_clock_end_updating (frame_clock);
  clock->running = FALSE;
}

static void
on_frame_clock_update (CcClock *clock)
{
  gint64 time_diff;

  if (!clock->running)
    return;

  time_diff = cc_clock_get_time_diff (clock);

  if (time_diff > clock->duration * 1000)
    {
      g_signal_emit (clock, signals[FINISHED], 0);
      cc_clock_stop (clock);
    }

  gtk_widget_queue_draw (GTK_WIDGET (clock));
}

static void
cc_clock_map (GtkWidget *widget)
{
  GdkFrameClock *frame_clock;

  GTK_WIDGET_CLASS (cc_clock_parent_class)->map (widget);

  frame_clock = gtk_widget_get_frame_clock (widget);
  g_signal_connect_object (frame_clock, "update",
                           G_CALLBACK (on_frame_clock_update),
                           widget, G_CONNECT_SWAPPED);
  cc_clock_reset (CC_CLOCK (widget));
}

static void
cc_clock_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  CcClock *clock = CC_CLOCK (object);

  switch (prop_id)
    {
    case PROP_DURATION:
      clock->duration = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
cc_clock_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
                       GParamSpec *pspec)
{
  CcClock *clock = CC_CLOCK (object);

  switch (prop_id)
    {
    case PROP_DURATION:
      g_value_set_uint (value, clock->duration);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
cc_clock_measure (GtkWidget      *widget,
                  GtkOrientation  orientation,
                  gint            for_size,
                  gint           *minimum,
                  gint           *natural,
                  gint           *minimum_baseline,
                  gint           *natural_baseline)
{
  if (minimum)
    *minimum = CLOCK_RADIUS + EXTRA_SPACE;
  if (natural)
    *natural = CLOCK_RADIUS + EXTRA_SPACE;
}

static void
cc_clock_class_init (CcClockClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = cc_clock_set_property;
  object_class->get_property = cc_clock_get_property;

  widget_class->map = cc_clock_map;
  widget_class->snapshot = cc_clock_snapshot;
  widget_class->measure = cc_clock_measure;

  signals[FINISHED] =
    g_signal_new ("finished",
                  CC_TYPE_CLOCK,
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  props[PROP_DURATION] =
    g_param_spec_uint ("duration",
                       "Duration",
                       "Duration",
                       0, G_MAXUINT, 0,
                       G_PARAM_READWRITE |
                       G_PARAM_STATIC_STRINGS |
                       G_PARAM_CONSTRUCT_ONLY);

  g_object_class_install_properties (object_class, N_PROPS, props);
}

static void
cc_clock_init (CcClock *clock)
{
}

GtkWidget *
cc_clock_new (guint duration)
{
  return g_object_new (CC_TYPE_CLOCK,
                       "duration", duration,
                       NULL);
}

void
cc_clock_reset (CcClock *clock)
{
  GdkFrameClock *frame_clock;

  if (!gtk_widget_get_mapped (GTK_WIDGET (clock)))
    return;

  frame_clock = gtk_widget_get_frame_clock (GTK_WIDGET (clock));

  cc_clock_stop (clock);

  clock->running = TRUE;
  clock->start_time = g_get_monotonic_time ();
  gdk_frame_clock_begin_updating (frame_clock);
}

void
cc_clock_set_duration (CcClock *clock,
                       guint    duration)
{
  clock->duration = duration;
  g_object_notify (G_OBJECT (clock), "duration");
  cc_clock_reset (clock);
}

guint
cc_clock_get_duration (CcClock *clock)
{
  return clock->duration;
}