summaryrefslogtreecommitdiff
path: root/plugins/mouse/gsd-timeline.c
blob: 9bcfd2fd604df25532fe05d01617f72367e4f806 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/* msd-timeline.c
 *
 * Copyright (C) 2008 Carlos Garnacho  <carlos@imendio.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <gtk/gtk.h>
#include <math.h>
#include "msd-timeline.h"

#define MSD_TIMELINE_GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MSD_TYPE_TIMELINE, MsdTimelinePriv))
#define MSECS_PER_SEC 1000
#define FRAME_INTERVAL(nframes) (MSECS_PER_SEC / nframes)
#define DEFAULT_FPS 30

typedef struct MsdTimelinePriv MsdTimelinePriv;

struct MsdTimelinePriv
{
  guint duration;
  guint fps;
  guint source_id;

  GTimer *timer;

  GdkScreen *screen;
  MsdTimelineProgressType progress_type;
  MsdTimelineProgressFunc progress_func;

  guint loop      : 1;
  guint direction : 1;
};

enum {
  PROP_0,
  PROP_FPS,
  PROP_DURATION,
  PROP_LOOP,
  PROP_DIRECTION,
  PROP_SCREEN,
  PROP_PROGRESS_TYPE,
};

enum {
  STARTED,
  PAUSED,
  FINISHED,
  FRAME,
  LAST_SIGNAL
};

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


static void  msd_timeline_set_property  (GObject         *object,
					 guint            prop_id,
					 const GValue    *value,
					 GParamSpec      *pspec);
static void  msd_timeline_get_property  (GObject         *object,
					 guint            prop_id,
					 GValue          *value,
					 GParamSpec      *pspec);
static void  msd_timeline_finalize      (GObject *object);


G_DEFINE_TYPE (MsdTimeline, msd_timeline, G_TYPE_OBJECT)


GType
msd_timeline_direction_get_type (void)
{
  static GType type = 0;

  if (G_UNLIKELY (type == 0))
    {
      static const GEnumValue values[] = {
	{ MSD_TIMELINE_DIRECTION_FORWARD,  "MSD_TIMELINE_DIRECTION_FORWARD",  "forward" },
	{ MSD_TIMELINE_DIRECTION_BACKWARD, "MSD_TIMELINE_DIRECTION_BACKWARD", "backward" },
	{ 0, NULL, NULL }
      };

      type = g_enum_register_static (g_intern_static_string ("MsdTimelineDirection"), values);
    }

  return type;
}

GType
msd_timeline_progress_type_get_type (void)
{
  static GType type = 0;

  if (G_UNLIKELY (type == 0))
    {
      static const GEnumValue values[] = {
	{ MSD_TIMELINE_PROGRESS_LINEAR,      "MSD_TIMELINE_PROGRESS_LINEAR",      "linear" },
	{ MSD_TIMELINE_PROGRESS_SINUSOIDAL,  "MSD_TIMELINE_PROGRESS_SINUSOIDAL",  "sinusoidal" },
	{ MSD_TIMELINE_PROGRESS_EXPONENTIAL, "MSD_TIMELINE_PROGRESS_EXPONENTIAL", "exponential" },
	{ 0, NULL, NULL }
      };

      type = g_enum_register_static (g_intern_static_string ("MsdTimelineProgressType"), values);
    }

  return type;
}

static void
msd_timeline_class_init (MsdTimelineClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->set_property = msd_timeline_set_property;
  object_class->get_property = msd_timeline_get_property;
  object_class->finalize = msd_timeline_finalize;

  g_object_class_install_property (object_class,
				   PROP_FPS,
				   g_param_spec_uint ("fps",
						      "FPS",
						      "Frames per second for the timeline",
						      1,
						      G_MAXUINT,
						      DEFAULT_FPS,
						      G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_DURATION,
				   g_param_spec_uint ("duration",
						      "Animation Duration",
						      "Animation Duration",
						      0,
						      G_MAXUINT,
						      0,
						      G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_LOOP,
				   g_param_spec_boolean ("loop",
							 "Loop",
							 "Whether the timeline loops or not",
							 FALSE,
							 G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_DIRECTION,
				   g_param_spec_enum ("direction",
						      "Direction",
						      "Whether the timeline moves forward or backward in time",
						      MSD_TYPE_TIMELINE_DIRECTION,
						      MSD_TIMELINE_DIRECTION_FORWARD,
						      G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_DIRECTION,
				   g_param_spec_enum ("progress-type",
						      "Progress type",
						      "Type of progress through the timeline",
						      MSD_TYPE_TIMELINE_PROGRESS_TYPE,
						      MSD_TIMELINE_PROGRESS_LINEAR,
						      G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_SCREEN,
				   g_param_spec_object ("screen",
							"Screen",
							"Screen to get the settings from",
							GDK_TYPE_SCREEN,
							G_PARAM_READWRITE));

  signals[STARTED] =
    g_signal_new ("started",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (MsdTimelineClass, started),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  signals[PAUSED] =
    g_signal_new ("paused",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (MsdTimelineClass, paused),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  signals[FINISHED] =
    g_signal_new ("finished",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (MsdTimelineClass, finished),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  signals[FRAME] =
    g_signal_new ("frame",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (MsdTimelineClass, frame),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__DOUBLE,
		  G_TYPE_NONE, 1,
		  G_TYPE_DOUBLE);

  g_type_class_add_private (class, sizeof (MsdTimelinePriv));
}

static void
msd_timeline_init (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  priv->fps = DEFAULT_FPS;
  priv->duration = 0;
  priv->direction = MSD_TIMELINE_DIRECTION_FORWARD;
  priv->screen = gdk_screen_get_default ();
}

static void
msd_timeline_set_property (GObject      *object,
			   guint         prop_id,
			   const GValue *value,
			   GParamSpec   *pspec)
{
  MsdTimeline *timeline;
  MsdTimelinePriv *priv;

  timeline = MSD_TIMELINE (object);
  priv = MSD_TIMELINE_GET_PRIV (timeline);

  switch (prop_id)
    {
    case PROP_FPS:
      msd_timeline_set_fps (timeline, g_value_get_uint (value));
      break;
    case PROP_DURATION:
      msd_timeline_set_duration (timeline, g_value_get_uint (value));
      break;
    case PROP_LOOP:
      msd_timeline_set_loop (timeline, g_value_get_boolean (value));
      break;
    case PROP_DIRECTION:
      msd_timeline_set_direction (timeline, g_value_get_enum (value));
      break;
    case PROP_SCREEN:
      msd_timeline_set_screen (timeline,
			       GDK_SCREEN (g_value_get_object (value)));
      break;
    case PROP_PROGRESS_TYPE:
      msd_timeline_set_progress_type (timeline, g_value_get_enum (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
msd_timeline_get_property (GObject    *object,
			   guint       prop_id,
			   GValue     *value,
			   GParamSpec *pspec)
{
  MsdTimeline *timeline;
  MsdTimelinePriv *priv;

  timeline = MSD_TIMELINE (object);
  priv = MSD_TIMELINE_GET_PRIV (timeline);

  switch (prop_id)
    {
    case PROP_FPS:
      g_value_set_uint (value, priv->fps);
      break;
    case PROP_DURATION:
      g_value_set_uint (value, priv->duration);
      break;
    case PROP_LOOP:
      g_value_set_boolean (value, priv->loop);
      break;
    case PROP_DIRECTION:
      g_value_set_enum (value, priv->direction);
      break;
    case PROP_SCREEN:
      g_value_set_object (value, priv->screen);
      break;
    case PROP_PROGRESS_TYPE:
      g_value_set_enum (value, priv->progress_type);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
msd_timeline_finalize (GObject *object)
{
  MsdTimelinePriv *priv;

  priv = MSD_TIMELINE_GET_PRIV (object);

  if (priv->source_id)
    {
      g_source_remove (priv->source_id);
      priv->source_id = 0;
    }

  if (priv->timer)
      g_timer_destroy (priv->timer);

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

/* Sinusoidal progress */
static gdouble
sinusoidal_progress (gdouble progress)
{
  return (sinf ((progress * G_PI) / 2));
}

static gdouble
exponential_progress (gdouble progress)
{
  return progress * progress;
}

static MsdTimelineProgressFunc
progress_type_to_func (MsdTimelineProgressType type)
{
  if (type == MSD_TIMELINE_PROGRESS_SINUSOIDAL)
    return sinusoidal_progress;
  else if (type == MSD_TIMELINE_PROGRESS_EXPONENTIAL)
    return exponential_progress;

  return NULL;
}

static gboolean
msd_timeline_run_frame (MsdTimeline *timeline,
			gboolean     enable_animations)
{
  MsdTimelinePriv *priv;
  gdouble linear_progress, progress;
  guint elapsed_time;
  MsdTimelineProgressFunc progress_func = NULL;

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  if (enable_animations)
    {
      elapsed_time = (guint) (g_timer_elapsed (priv->timer, NULL) * 1000);

      linear_progress = (gdouble) elapsed_time / priv->duration;

      if (priv->direction == MSD_TIMELINE_DIRECTION_BACKWARD)
	linear_progress = 1 - linear_progress;

      linear_progress = CLAMP (linear_progress, 0., 1.);

      if (priv->progress_func)
	progress_func = priv->progress_func;
      else if (priv->progress_type)
	progress_func = progress_type_to_func (priv->progress_type);

      if (progress_func)
	progress = (progress_func) (linear_progress);
      else
	progress = linear_progress;
    }
  else
    progress = (priv->direction == MSD_TIMELINE_DIRECTION_FORWARD) ? 1.0 : 0.0;

  g_signal_emit (timeline, signals [FRAME], 0,
		 CLAMP (progress, 0.0, 1.0));

  if ((priv->direction == MSD_TIMELINE_DIRECTION_FORWARD && progress >= 1.0) ||
      (priv->direction == MSD_TIMELINE_DIRECTION_BACKWARD && progress <= 0.0))
    {
      if (!priv->loop)
	{
	  if (priv->source_id)
	    {
	      g_source_remove (priv->source_id);
	      priv->source_id = 0;
	    }

	  g_signal_emit (timeline, signals [FINISHED], 0);
	  return FALSE;
	}
      else
	msd_timeline_rewind (timeline);
    }

  return TRUE;
}

static gboolean
msd_timeline_frame_idle_func (MsdTimeline *timeline)
{
  return msd_timeline_run_frame (timeline, TRUE);
}

/**
 * msd_timeline_new:
 * @duration: duration in milliseconds for the timeline
 *
 * Creates a new #MsdTimeline with the specified number of frames.
 *
 * Return Value: the newly created #MsdTimeline
 **/
MsdTimeline *
msd_timeline_new (guint duration)
{
  return g_object_new (MSD_TYPE_TIMELINE,
		       "duration", duration,
		       NULL);
}

MsdTimeline *
msd_timeline_new_for_screen (guint      duration,
			     GdkScreen *screen)
{
  return g_object_new (MSD_TYPE_TIMELINE,
		       "duration", duration,
		       "screen", screen,
		       NULL);
}

/**
 * msd_timeline_start:
 * @timeline: A #MsdTimeline
 *
 * Runs the timeline from the current frame.
 **/
void
msd_timeline_start (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;
  GtkSettings *settings;
  gboolean enable_animations = FALSE;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  if (priv->screen)
    {
      settings = gtk_settings_get_for_screen (priv->screen);
      g_object_get (settings, "gtk-enable-animations", &enable_animations, NULL);
    }

  if (enable_animations)
    {
      if (!priv->source_id)
	{
	  if (priv->timer)
	    g_timer_continue (priv->timer);
	  else
	    priv->timer = g_timer_new ();

	  /* sanity check */
	  g_assert (priv->fps > 0);

	  g_signal_emit (timeline, signals [STARTED], 0);

	  priv->source_id = gdk_threads_add_timeout (FRAME_INTERVAL (priv->fps),
						     (GSourceFunc) msd_timeline_frame_idle_func,
						     timeline);
	}
    }
  else
    {
      /* If animations are not enabled, only run the last frame,
       * it take us instantaneously to the last state of the animation.
       * The only potential flaw happens when people use the ::finished
       * signal to trigger another animation, or even worse, finally
       * loop into this animation again.
       */
      g_signal_emit (timeline, signals [STARTED], 0);
      msd_timeline_run_frame (timeline, FALSE);
    }
}

/**
 * msd_timeline_pause:
 * @timeline: A #MsdTimeline
 *
 * Pauses the timeline.
 **/
void
msd_timeline_pause (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  if (priv->source_id)
    {
      g_source_remove (priv->source_id);
      priv->source_id = 0;
      g_timer_stop (priv->timer);
      g_signal_emit (timeline, signals [PAUSED], 0);
    }
}

/**
 * msd_timeline_rewind:
 * @timeline: A #MsdTimeline
 *
 * Rewinds the timeline.
 **/
void
msd_timeline_rewind (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  /* destroy and re-create timer if neccesary  */
  if (priv->timer)
    {
      g_timer_destroy (priv->timer);

      if (msd_timeline_is_running (timeline))
	priv->timer = g_timer_new ();
      else
	priv->timer = NULL;
    }
}

/**
 * msd_timeline_is_running:
 * @timeline: A #MsdTimeline
 *
 * Returns whether the timeline is running or not.
 *
 * Return Value: %TRUE if the timeline is running
 **/
gboolean
msd_timeline_is_running (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), FALSE);

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  return (priv->source_id != 0);
}

/**
 * msd_timeline_get_fps:
 * @timeline: A #MsdTimeline
 *
 * Returns the number of frames per second.
 *
 * Return Value: frames per second
 **/
guint
msd_timeline_get_fps (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), 1);

  priv = MSD_TIMELINE_GET_PRIV (timeline);
  return priv->fps;
}

/**
 * msd_timeline_set_fps:
 * @timeline: A #MsdTimeline
 * @fps: frames per second
 *
 * Sets the number of frames per second that
 * the timeline will play.
 **/
void
msd_timeline_set_fps (MsdTimeline *timeline,
		      guint        fps)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));
  g_return_if_fail (fps > 0);

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  priv->fps = fps;

  if (msd_timeline_is_running (timeline))
    {
      g_source_remove (priv->source_id);
      priv->source_id = gdk_threads_add_timeout (FRAME_INTERVAL (priv->fps),
						 (GSourceFunc) msd_timeline_run_frame,
						 timeline);
    }

  g_object_notify (G_OBJECT (timeline), "fps");
}

/**
 * msd_timeline_get_loop:
 * @timeline: A #MsdTimeline
 *
 * Returns whether the timeline loops to the
 * beginning when it has reached the end.
 *
 * Return Value: %TRUE if the timeline loops
 **/
gboolean
msd_timeline_get_loop (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), FALSE);

  priv = MSD_TIMELINE_GET_PRIV (timeline);
  return priv->loop;
}

/**
 * msd_timeline_set_loop:
 * @timeline: A #MsdTimeline
 * @loop: %TRUE to make the timeline loop
 *
 * Sets whether the timeline loops to the beginning
 * when it has reached the end.
 **/
void
msd_timeline_set_loop (MsdTimeline *timeline,
		       gboolean     loop)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);
  priv->loop = loop;

  g_object_notify (G_OBJECT (timeline), "loop");
}

void
msd_timeline_set_duration (MsdTimeline *timeline,
			   guint        duration)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  priv->duration = duration;

  g_object_notify (G_OBJECT (timeline), "duration");
}

guint
msd_timeline_get_duration (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), 0);

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  return priv->duration;
}

/**
 * msd_timeline_get_direction:
 * @timeline: A #MsdTimeline
 *
 * Returns the direction of the timeline.
 *
 * Return Value: direction
 **/
MsdTimelineDirection
msd_timeline_get_direction (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), MSD_TIMELINE_DIRECTION_FORWARD);

  priv = MSD_TIMELINE_GET_PRIV (timeline);
  return priv->direction;
}

/**
 * msd_timeline_set_direction:
 * @timeline: A #MsdTimeline
 * @direction: direction
 *
 * Sets the direction of the timeline.
 **/
void
msd_timeline_set_direction (MsdTimeline          *timeline,
			    MsdTimelineDirection  direction)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);
  priv->direction = direction;

  g_object_notify (G_OBJECT (timeline), "direction");
}

GdkScreen *
msd_timeline_get_screen (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), NULL);

  priv = MSD_TIMELINE_GET_PRIV (timeline);
  return priv->screen;
}

void
msd_timeline_set_screen (MsdTimeline *timeline,
			 GdkScreen   *screen)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));
  g_return_if_fail (GDK_IS_SCREEN (screen));

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  if (priv->screen)
    g_object_unref (priv->screen);

  priv->screen = g_object_ref (screen);

  g_object_notify (G_OBJECT (timeline), "screen");
}

void
msd_timeline_set_progress_type (MsdTimeline             *timeline,
				MsdTimelineProgressType  type)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  priv->progress_type = type;

  g_object_notify (G_OBJECT (timeline), "progress-type");
}

MsdTimelineProgressType
msd_timeline_get_progress_type (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), MSD_TIMELINE_PROGRESS_LINEAR);

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  if (priv->progress_func)
    return MSD_TIMELINE_PROGRESS_LINEAR;

  return priv->progress_type;
}

/**
 * msd_timeline_set_progress_func:
 * @timeline: A #MsdTimeline
 * @progress_func: progress function
 *
 * Sets the progress function. This function will be used to calculate
 * a different progress to pass to the ::frame signal based on the
 * linear progress through the timeline. Setting progress_func
 * to %NULL will make the timeline use the default function,
 * which is just a linear progress.
 *
 * All progresses are in the [0.0, 1.0] range.
 **/
void
msd_timeline_set_progress_func (MsdTimeline             *timeline,
				MsdTimelineProgressFunc  progress_func)
{
  MsdTimelinePriv *priv;

  g_return_if_fail (MSD_IS_TIMELINE (timeline));

  priv = MSD_TIMELINE_GET_PRIV (timeline);
  priv->progress_func = progress_func;
}

gdouble
msd_timeline_get_progress (MsdTimeline *timeline)
{
  MsdTimelinePriv *priv;
  MsdTimelineProgressFunc progress_func = NULL;
  gdouble linear_progress, progress;
  guint elapsed_time;

  g_return_val_if_fail (MSD_IS_TIMELINE (timeline), 0.0);

  priv = MSD_TIMELINE_GET_PRIV (timeline);

  if (!priv->timer)
    return 0.;

  elapsed_time = (guint) (g_timer_elapsed (priv->timer, NULL) * 1000);

  linear_progress = (gdouble) elapsed_time / priv->duration;

  if (priv->direction == MSD_TIMELINE_DIRECTION_BACKWARD)
    linear_progress = 1 - linear_progress;

  linear_progress = CLAMP (linear_progress, 0., 1.);

  if (priv->progress_func)
    progress_func = priv->progress_func;
  else if (priv->progress_type)
    progress_func = progress_type_to_func (priv->progress_type);

  if (progress_func)
    progress = (progress_func) (linear_progress);
  else
    progress = linear_progress;

  return CLAMP (progress, 0., 1.);
}