aboutsummaryrefslogtreecommitdiff
path: root/src/scrub-widget.c
blob: fa290e009b0faa0cddab2d9438c503106b49791f (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
/*
Copyright 2010 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/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib/gi18n.h>
#include "scrub-widget.h"
#include "common-defs.h"
#include <libido/idoscalemenuitem.h>
#include <libido/idotimeline.h>

typedef struct _ScrubWidgetPrivate ScrubWidgetPrivate;

struct _ScrubWidgetPrivate
{
	DbusmenuMenuitem* twin_item;	
	GtkWidget* ido_scrub_bar;
	IdoTimeline* time_line;
	gboolean scrubbing;
};

#define SCRUB_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SCRUB_WIDGET_TYPE, ScrubWidgetPrivate))

/* Prototypes */
static void scrub_widget_class_init (ScrubWidgetClass *klass);
static void scrub_widget_init       (ScrubWidget *self);
static void scrub_widget_dispose    (GObject *object);
static void scrub_widget_finalize   (GObject *object);
static void scrub_widget_property_update( DbusmenuMenuitem* item, gchar* property, 
                                       	  GValue* value, gpointer userdata);
static void scrub_widget_set_twin_item(	ScrubWidget* self,
                           							DbusmenuMenuitem* twin_item);
static gchar* scrub_widget_format_time(gint time);
static void scrub_widget_set_ido_position(ScrubWidget* self,
                                          gint position,
                                          gint duration);
static gboolean scrub_widget_change_value_cb (GtkRange     *range,
                                 							GtkScrollType scroll,
                                 							gdouble       value,
                                 							gpointer      user_data);

static void scrub_widget_timeline_frame_cb(IdoTimeline *timeline,
			      												 				gdouble      progress,
                                     				gpointer 		userdata);
static void scrub_widget_timeline_started_cb(IdoTimeline *timeline,
                                     				gpointer 		userdata);
static void scrub_widget_timeline_finished_cb(IdoTimeline *timeline,
                                     				gpointer 		userdata);
static gdouble scrub_widget_calculate_progress(ScrubWidget* widget);
static void scrub_widget_check_play_state(ScrubWidget* self);
static void scrub_widget_slider_grabbed(GtkWidget *widget, gpointer user_data);
static void scrub_widget_slider_released(GtkWidget *widget, gpointer user_data);



G_DEFINE_TYPE (ScrubWidget, scrub_widget, G_TYPE_OBJECT);

static void
scrub_widget_class_init (ScrubWidgetClass *klass)
{
	GObjectClass 			*gobject_class = G_OBJECT_CLASS (klass);
	
	g_type_class_add_private (klass, sizeof (ScrubWidgetPrivate));

	gobject_class->dispose = scrub_widget_dispose;
	gobject_class->finalize = scrub_widget_finalize;
}

static void
scrub_widget_init (ScrubWidget *self)
{
	g_debug("ScrubWidget::scrub_widget_init");
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(self);

  priv->ido_scrub_bar = ido_scale_menu_item_new_with_range ("Scrub", IDO_RANGE_STYLE_SMALL,  0, 0, 100, 1);
	priv->time_line = ido_timeline_new(0);
	
	ido_scale_menu_item_set_style (IDO_SCALE_MENU_ITEM(priv->ido_scrub_bar), IDO_SCALE_MENU_ITEM_STYLE_LABEL);	
	g_object_set(priv->ido_scrub_bar, "reverse-scroll-events", TRUE, NULL);
	priv->scrubbing = FALSE;
	
  // register slider changes listening on the range
  GtkWidget* scrub_widget = ido_scale_menu_item_get_scale((IdoScaleMenuItem*)priv->ido_scrub_bar);	
  g_signal_connect(scrub_widget, "change-value", G_CALLBACK(scrub_widget_change_value_cb), self);	
	g_signal_connect(priv->time_line, "frame", G_CALLBACK(scrub_widget_timeline_frame_cb), self);	
	g_signal_connect(priv->time_line, "started", G_CALLBACK(scrub_widget_timeline_started_cb), self);		
	g_signal_connect(priv->time_line, "finished", G_CALLBACK(scrub_widget_timeline_finished_cb), self);		
  g_signal_connect(priv->ido_scrub_bar, "slider-grabbed", G_CALLBACK(scrub_widget_slider_grabbed), self);
  g_signal_connect(priv->ido_scrub_bar, "slider-released", G_CALLBACK(scrub_widget_slider_released), self);
}

static void
scrub_widget_dispose (GObject *object)
{
	G_OBJECT_CLASS (scrub_widget_parent_class)->dispose (object);
}

static void
scrub_widget_finalize (GObject *object)
{
	G_OBJECT_CLASS (scrub_widget_parent_class)->finalize (object);
}

static void 
scrub_widget_property_update(DbusmenuMenuitem* item, gchar* property, 
                             GValue* value, gpointer userdata)
{
	g_debug("scrub-widget::property_update"); 
	
	g_return_if_fail (IS_SCRUB_WIDGET (userdata));	
	ScrubWidget* mitem = SCRUB_WIDGET(userdata);
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(mitem);
	
	if(g_ascii_strcasecmp(DBUSMENU_SCRUB_MENUITEM_DURATION, property) == 0){
		g_debug("scrub-widget::update length = %i", g_value_get_int(value)); 
		ido_scale_menu_item_set_secondary_label(IDO_SCALE_MENU_ITEM(priv->ido_scrub_bar),
		                                      	scrub_widget_format_time(g_value_get_int(value))); 			
				
		ido_timeline_set_duration(priv->time_line, g_value_get_int(value) * 1000); 	
		ido_timeline_rewind(priv->time_line);
		scrub_widget_check_play_state(mitem);
		g_debug("timeline is running: %i", (gint)ido_timeline_is_running(priv->time_line));
		g_debug("timeline duration = %i", ido_timeline_get_duration(priv->time_line));

		scrub_widget_set_ido_position(mitem, 
	                              dbusmenu_menuitem_property_get_int(priv->twin_item, DBUSMENU_SCRUB_MENUITEM_POSITION)/1000,
																dbusmenu_menuitem_property_get_int(priv->twin_item, DBUSMENU_SCRUB_MENUITEM_DURATION));	                                		
	}
	else if(g_ascii_strcasecmp(DBUSMENU_SCRUB_MENUITEM_POSITION, property) == 0){
		g_debug("scrub-widget::update position = %i", g_value_get_int(value)); 
		ido_timeline_pause(priv->time_line);
		ido_scale_menu_item_set_primary_label(IDO_SCALE_MENU_ITEM(priv->ido_scrub_bar),
		                                      scrub_widget_format_time(g_value_get_int(value)/1000)); 					
																					
		ido_timeline_set_progress(priv->time_line, scrub_widget_calculate_progress(mitem)*1000);
		scrub_widget_set_ido_position(mitem, g_value_get_int(value)/1000,
																dbusmenu_menuitem_property_get_int(priv->twin_item, DBUSMENU_SCRUB_MENUITEM_DURATION));
		ido_timeline_start(priv->time_line);		

	}
	else if(g_ascii_strcasecmp(DBUSMENU_SCRUB_MENUITEM_PLAY_STATE, property) == 0){
		scrub_widget_check_play_state(mitem);
	}
}

static void 
scrub_widget_check_play_state(ScrubWidget* self)
{
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(self);
	gint play_state = dbusmenu_menuitem_property_get_int(priv->twin_item,
	                                                     DBUSMENU_SCRUB_MENUITEM_PLAY_STATE);
	
	if(play_state == 0){
		g_debug("START TIMELINE");
		ido_timeline_start(priv->time_line);
	}
	else{
		g_debug("PAUSE TIMELINE");			
		ido_timeline_pause(priv->time_line);
	}		
}

static void
scrub_widget_set_twin_item(ScrubWidget* self,
                           DbusmenuMenuitem* twin_item)
{
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(self);
	priv->twin_item = twin_item;

	g_signal_connect(G_OBJECT(twin_item), "property-changed", 
	                 G_CALLBACK(scrub_widget_property_update), self);

	gchar* left_text = scrub_widget_format_time(dbusmenu_menuitem_property_get_int(priv->twin_item,	                                                                               DBUSMENU_SCRUB_MENUITEM_POSITION)/1000); 	
	gchar* right_text = scrub_widget_format_time(dbusmenu_menuitem_property_get_int(priv->twin_item, 
	                                                                                DBUSMENU_SCRUB_MENUITEM_DURATION)); 	
	scrub_widget_set_ido_position(self, 
	                              dbusmenu_menuitem_property_get_int(priv->twin_item, DBUSMENU_SCRUB_MENUITEM_POSITION)/1000,
																dbusmenu_menuitem_property_get_int(priv->twin_item, DBUSMENU_SCRUB_MENUITEM_DURATION));	                                
	                                
	ido_scale_menu_item_set_primary_label(IDO_SCALE_MENU_ITEM(priv->ido_scrub_bar), left_text); 	
	ido_scale_menu_item_set_secondary_label(IDO_SCALE_MENU_ITEM(priv->ido_scrub_bar), right_text);
	g_free(left_text);
	g_free(right_text);
}

static gboolean
scrub_widget_change_value_cb (GtkRange     *range,
                 							GtkScrollType scroll,
                 							gdouble       new_value,
                 							gpointer      user_data)
{
	/*g_return_val_if_fail (IS_SCRUB_WIDGET (user_data), FALSE);
	ScrubWidget* mitem = SCRUB_WIDGET(user_data);
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(mitem);

	GValue value = {0};
  g_value_init(&value, G_TYPE_DOUBLE);
	gdouble clamped = CLAMP(new_value, 0, 100);
  g_value_set_double(&value, clamped);
  //g_debug("scrub-widget-change-value callback - = %f", clamped);
	if(priv->scrubbing == FALSE){
  	dbusmenu_menuitem_handle_event (priv->twin_item, "scrubbing", &value, 0);
	}
	else{
		g_debug("blocking scrubbing because the slider is still grabbed"
	}*/
	return FALSE;
}

GtkWidget*
scrub_widget_get_ido_bar(ScrubWidget* self)
{
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(self);
	return priv->ido_scrub_bar;	
}

static gchar*
scrub_widget_format_time(gint time)
{
	// Assuming its in seconds for now ...
	gint minutes = time/60;
	gint seconds = time % 60;
	gchar* prefix="0";
	gchar* seconds_prefix="0";
	if(minutes > 9)
		prefix="";
	if(seconds > 9)
		seconds_prefix="";
	return g_strdup_printf("%s%i:%s%i", prefix, minutes, seconds_prefix, seconds);	
}

static void
scrub_widget_set_ido_position(ScrubWidget* self,
                              gint position,
                              gint duration)
{
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(self);
	gdouble ido_position = position/(gdouble)duration	* 100.0;
	g_debug("scrub_widget_set_ido_position - pos: %i, duration: %i, ido_pos: %f", position, duration, ido_position);
  GtkWidget *slider = ido_scale_menu_item_get_scale((IdoScaleMenuItem*)priv->ido_scrub_bar);
  GtkRange *range = (GtkRange*)slider;
	if(duration == 0)
		ido_position = 0.0;
 	gtk_range_set_value(range, ido_position);
}

static gdouble 
scrub_widget_calculate_progress(ScrubWidget* widget)
{
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(widget);
	gint position = dbusmenu_menuitem_property_get_int(priv->twin_item,
	                                                   DBUSMENU_SCRUB_MENUITEM_POSITION)/1000;
	gint duration = dbusmenu_menuitem_property_get_int(priv->twin_item,
	                                                   DBUSMENU_SCRUB_MENUITEM_DURATION);
	gdouble ido_position = position/(gdouble)duration;
	g_debug("scrub_widget_calculate_progress %f", ido_position);
	        
	return ido_position;
}


static void
scrub_widget_timeline_frame_cb(	IdoTimeline *timeline,
			      						 				gdouble     	progress,
                         				gpointer 		user_data)
{
	
	//g_debug("Timeline CB : %f", progress);
	g_return_if_fail (IS_SCRUB_WIDGET (user_data));
	ScrubWidget* mitem = SCRUB_WIDGET(user_data);
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(mitem);
	if(priv->scrubbing == TRUE)
	{
		//g_debug("don't update the slider or timeline, slider is being scrubbed");
		return;
	}
	gint position = progress * dbusmenu_menuitem_property_get_int(priv->twin_item, 
	                                   														DBUSMENU_SCRUB_MENUITEM_DURATION);	
	gchar* left_text = scrub_widget_format_time(position); 	
	ido_scale_menu_item_set_primary_label(IDO_SCALE_MENU_ITEM(priv->ido_scrub_bar), left_text); 	
  GtkWidget *slider = ido_scale_menu_item_get_scale((IdoScaleMenuItem*)priv->ido_scrub_bar);
  GtkRange *range = (GtkRange*)slider;
 	gtk_range_set_value(range, progress * 100);

	/*g_debug("position in seconds %i and in words %s", position, left_text);
	g_debug("timeline is running: %i", (gint)ido_timeline_is_running(priv->time_line));
	g_debug("timeline duration = %i", ido_timeline_get_duration(priv->time_line));	
	*/
	g_free(left_text);
}


static void
scrub_widget_slider_released(GtkWidget *widget, gpointer user_data)
{
	ScrubWidget* mitem = SCRUB_WIDGET(user_data);
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(mitem);
	priv->scrubbing = FALSE;
	GtkWidget *slider = ido_scale_menu_item_get_scale((IdoScaleMenuItem*)priv->ido_scrub_bar);
  gdouble new_value = gtk_range_get_value(GTK_RANGE(slider));
	g_debug("okay set the scrub position with %f", new_value);	
	GValue value = {0};
  g_value_init(&value, G_TYPE_DOUBLE);
	gdouble clamped = CLAMP(new_value, 0, 100);
  g_value_set_double(&value, clamped);
  dbusmenu_menuitem_handle_event (priv->twin_item, "scrubbing", &value, 0);
}

static void
scrub_widget_slider_grabbed(GtkWidget *widget, gpointer user_data)
{
	ScrubWidget* mitem = SCRUB_WIDGET(user_data);
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(mitem);
	priv->scrubbing = TRUE;	
	
}

static void
scrub_widget_timeline_started_cb(	IdoTimeline *timeline,
                         			 		gpointer 		user_data)
{
	g_debug("Timeline Started!");	
}
                           
static void
scrub_widget_timeline_finished_cb(IdoTimeline *timeline,
                         			 		gpointer 		user_data)
{
	g_debug("Timeline Finished!");	
	/*g_return_if_fail (IS_SCRUB_WIDGET (user_data));
	ScrubWidget* mitem = SCRUB_WIDGET(user_data);
	ScrubWidgetPrivate * priv = SCRUB_WIDGET_GET_PRIVATE(mitem);
	ido_timeline_rewind(priv->time_line);*/
}

/**
 * scrub_widget_new:
 * @returns: a new #ScrubWidget.
 **/
GtkWidget* 
scrub_widget_new(DbusmenuMenuitem *item)
{
	GtkWidget* widget = g_object_new(SCRUB_WIDGET_TYPE, NULL);
	scrub_widget_set_twin_item((ScrubWidget*)widget, item);
	return widget;
}