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
|
/*
* Copyright © 2018 Benjamin Otte
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include "config.h"
#include "gtkmediacontrols.h"
#include "gtkadjustment.h"
#include "gtkbutton.h"
#include <glib/gi18n-lib.h>
#include "gtklabel.h"
#include "gtkwidgetprivate.h"
#include "gtkprivate.h"
/**
* GtkMediaControls:
*
* Shows controls for video playback.
*
* <picture>
* <source srcset="media-controls-dark.png" media="(prefers-color-scheme: dark)">
* <img alt="An example GtkMediaControls" src="media-controls.png">
* </picture>
*
* Usually, `GtkMediaControls` is used as part of [class@Gtk.Video].
*/
struct _GtkMediaControls
{
GtkWidget parent_instance;
GtkMediaStream *stream;
GtkAdjustment *time_adjustment;
GtkAdjustment *volume_adjustment;
GtkWidget *box;
GtkWidget *play_button;
GtkWidget *time_box;
GtkWidget *time_label;
GtkWidget *seek_scale;
GtkWidget *duration_label;
GtkWidget *volume_button;
};
enum
{
PROP_0,
PROP_MEDIA_STREAM,
N_PROPS
};
G_DEFINE_TYPE (GtkMediaControls, gtk_media_controls, GTK_TYPE_WIDGET)
static GParamSpec *properties[N_PROPS] = { NULL, };
/* FIXME: Remove
* See https://bugzilla.gnome.org/show_bug.cgi?id=679850 */
static char *
totem_time_to_string (gint64 usecs,
gboolean remaining,
gboolean force_hour)
{
int sec, min, hour, _time;
_time = (int) (usecs / G_USEC_PER_SEC);
/* When calculating the remaining time,
* we want to make sure that:
* current time + time remaining = total run time */
if (remaining)
_time++;
sec = _time % 60;
_time = _time - sec;
min = (_time % (60*60)) / 60;
_time = _time - (min * 60);
hour = _time / (60*60);
if (hour > 0 || force_hour) {
if (!remaining) {
/* hour:minutes:seconds */
/* Translators: This is a time format, like "9:05:02" for 9
* hours, 5 minutes, and 2 seconds. You may change ":" to
* the separator that your locale uses or use "%Id" instead
* of "%d" if your locale uses localized digits.
*/
return g_strdup_printf (C_("long time format", "%d:%02d:%02d"), hour, min, sec);
} else {
/* -hour:minutes:seconds */
/* Translators: This is a time format, like "-9:05:02" for 9
* hours, 5 minutes, and 2 seconds playback remaining. You may
* change ":" to the separator that your locale uses or use
* "%Id" instead of "%d" if your locale uses localized digits.
*/
return g_strdup_printf (C_("long time format", "-%d:%02d:%02d"), hour, min, sec);
}
}
if (remaining) {
/* -minutes:seconds */
/* Translators: This is a time format, like "-5:02" for 5
* minutes and 2 seconds playback remaining. You may change
* ":" to the separator that your locale uses or use "%Id"
* instead of "%d" if your locale uses localized digits.
*/
return g_strdup_printf (C_("short time format", "-%d:%02d"), min, sec);
}
/* minutes:seconds */
/* Translators: This is a time format, like "5:02" for 5
* minutes and 2 seconds. You may change ":" to the
* separator that your locale uses or use "%Id" instead of
* "%d" if your locale uses localized digits.
*/
return g_strdup_printf (C_("short time format", "%d:%02d"), min, sec);
}
static void
time_adjustment_changed (GtkAdjustment *adjustment,
GtkMediaControls *controls)
{
if (controls->stream == NULL)
return;
/* We just updated the adjustment and it's correct now */
if (gtk_adjustment_get_value (adjustment) == (double) gtk_media_stream_get_timestamp (controls->stream) / G_USEC_PER_SEC)
return;
gtk_media_stream_seek (controls->stream,
gtk_adjustment_get_value (adjustment) * G_USEC_PER_SEC + 0.5);
}
static void
volume_adjustment_changed (GtkAdjustment *adjustment,
GtkMediaControls *controls)
{
if (controls->stream == NULL)
return;
/* We just updated the adjustment and it's correct now */
if (gtk_adjustment_get_value (adjustment) == gtk_media_stream_get_volume (controls->stream))
return;
gtk_media_stream_set_muted (controls->stream, gtk_adjustment_get_value (adjustment) == 0.0);
gtk_media_stream_set_volume (controls->stream, gtk_adjustment_get_value (adjustment));
}
static void
play_button_clicked (GtkWidget *button,
GtkMediaControls *controls)
{
if (controls->stream == NULL)
return;
gtk_media_stream_set_playing (controls->stream,
!gtk_media_stream_get_playing (controls->stream));
}
static void
gtk_media_controls_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
GtkMediaControls *controls = GTK_MEDIA_CONTROLS (widget);
gtk_widget_measure (controls->box,
orientation,
for_size,
minimum, natural,
minimum_baseline, natural_baseline);
}
static void
gtk_media_controls_size_allocate (GtkWidget *widget,
int width,
int height,
int baseline)
{
GtkMediaControls *controls = GTK_MEDIA_CONTROLS (widget);
gtk_widget_size_allocate (controls->box,
&(GtkAllocation) {
0, 0,
width, height
}, baseline);
}
static void
gtk_media_controls_dispose (GObject *object)
{
GtkMediaControls *controls = GTK_MEDIA_CONTROLS (object);
gtk_media_controls_set_media_stream (controls, NULL);
gtk_widget_dispose_template (GTK_WIDGET (object), GTK_TYPE_MEDIA_CONTROLS);
G_OBJECT_CLASS (gtk_media_controls_parent_class)->dispose (object);
}
static void
gtk_media_controls_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GtkMediaControls *controls = GTK_MEDIA_CONTROLS (object);
switch (property_id)
{
case PROP_MEDIA_STREAM:
g_value_set_object (value, controls->stream);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gtk_media_controls_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GtkMediaControls *controls = GTK_MEDIA_CONTROLS (object);
switch (property_id)
{
case PROP_MEDIA_STREAM:
gtk_media_controls_set_media_stream (controls, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gtk_media_controls_class_init (GtkMediaControlsClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
widget_class->measure = gtk_media_controls_measure;
widget_class->size_allocate = gtk_media_controls_size_allocate;
gobject_class->dispose = gtk_media_controls_dispose;
gobject_class->get_property = gtk_media_controls_get_property;
gobject_class->set_property = gtk_media_controls_set_property;
/**
* GtkMediaControls:media-stream:
*
* The media-stream managed by this object or %NULL if none.
*/
properties[PROP_MEDIA_STREAM] =
g_param_spec_object ("media-stream", NULL, NULL,
GTK_TYPE_MEDIA_STREAM,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, N_PROPS, properties);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkmediacontrols.ui");
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, time_adjustment);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, volume_adjustment);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, box);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, play_button);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, time_box);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, time_label);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, seek_scale);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, duration_label);
gtk_widget_class_bind_template_child (widget_class, GtkMediaControls, volume_button);
gtk_widget_class_bind_template_callback (widget_class, play_button_clicked);
gtk_widget_class_bind_template_callback (widget_class, time_adjustment_changed);
gtk_widget_class_bind_template_callback (widget_class, volume_adjustment_changed);
gtk_widget_class_set_css_name (widget_class, I_("controls"));
}
static void
gtk_media_controls_init (GtkMediaControls *controls)
{
gtk_widget_init_template (GTK_WIDGET (controls));
}
/**
* gtk_media_controls_new:
* @stream: (nullable) (transfer none): a `GtkMediaStream` to manage
*
* Creates a new `GtkMediaControls` managing the @stream passed to it.
*
* Returns: a new `GtkMediaControls`
*/
GtkWidget *
gtk_media_controls_new (GtkMediaStream *stream)
{
return g_object_new (GTK_TYPE_MEDIA_CONTROLS,
"media-stream", stream,
NULL);
}
/**
* gtk_media_controls_get_media_stream:
* @controls: a `GtkMediaControls`
*
* Gets the media stream managed by @controls or %NULL if none.
*
* Returns: (nullable) (transfer none): The media stream managed by @controls
*/
GtkMediaStream *
gtk_media_controls_get_media_stream (GtkMediaControls *controls)
{
g_return_val_if_fail (GTK_IS_MEDIA_CONTROLS (controls), NULL);
return controls->stream;
}
static void
update_timestamp (GtkMediaControls *controls)
{
gint64 timestamp, duration;
char *time_string;
if (controls->stream)
{
timestamp = gtk_media_stream_get_timestamp (controls->stream);
duration = gtk_media_stream_get_duration (controls->stream);
}
else
{
timestamp = 0;
duration = 0;
}
time_string = totem_time_to_string (timestamp, FALSE, FALSE);
gtk_label_set_text (GTK_LABEL (controls->time_label), time_string);
g_free (time_string);
if (duration > 0)
{
time_string = totem_time_to_string (duration > timestamp ? duration - timestamp : 0, TRUE, FALSE);
gtk_label_set_text (GTK_LABEL (controls->duration_label), time_string);
g_free (time_string);
gtk_adjustment_set_value (controls->time_adjustment, (double) timestamp / G_USEC_PER_SEC);
}
}
static void
update_duration (GtkMediaControls *controls)
{
gint64 timestamp, duration;
char *time_string;
if (controls->stream)
{
timestamp = gtk_media_stream_get_timestamp (controls->stream);
duration = gtk_media_stream_get_duration (controls->stream);
}
else
{
timestamp = 0;
duration = 0;
}
time_string = totem_time_to_string (duration > timestamp ? duration - timestamp : 0, TRUE, FALSE);
gtk_label_set_text (GTK_LABEL (controls->duration_label), time_string);
gtk_widget_set_visible (controls->duration_label, duration > 0);
g_free (time_string);
gtk_adjustment_set_upper (controls->time_adjustment,
gtk_adjustment_get_page_size (controls->time_adjustment)
+ (double) duration / G_USEC_PER_SEC);
gtk_adjustment_set_value (controls->time_adjustment, (double) timestamp / G_USEC_PER_SEC);
}
static void
update_playing (GtkMediaControls *controls)
{
gboolean playing;
const char *icon_name;
const char *tooltip_text;
if (controls->stream)
playing = gtk_media_stream_get_playing (controls->stream);
else
playing = FALSE;
if (playing)
{
icon_name = "media-playback-pause-symbolic";
tooltip_text = C_("media controls tooltip", "Stop");
}
else
{
icon_name = "media-playback-start-symbolic";
tooltip_text = C_("media controls tooltip", "Play");
}
gtk_button_set_icon_name (GTK_BUTTON (controls->play_button), icon_name);
gtk_widget_set_tooltip_text (controls->play_button, tooltip_text);
}
static void
update_seekable (GtkMediaControls *controls)
{
gboolean seekable;
if (controls->stream)
seekable = gtk_media_stream_is_seekable (controls->stream);
else
seekable = FALSE;
gtk_widget_set_sensitive (controls->seek_scale, seekable);
}
static void
update_volume (GtkMediaControls *controls)
{
double volume;
if (controls->stream == NULL)
volume = 1.0;
else if (gtk_media_stream_get_muted (controls->stream))
volume = 0.0;
else
volume = gtk_media_stream_get_volume (controls->stream);
gtk_adjustment_set_value (controls->volume_adjustment, volume);
gtk_widget_set_sensitive (controls->volume_button,
controls->stream == NULL ||
gtk_media_stream_has_audio (controls->stream));
}
static void
update_all (GtkMediaControls *controls)
{
update_timestamp (controls);
update_duration (controls);
update_playing (controls);
update_seekable (controls);
update_volume (controls);
}
static void
gtk_media_controls_notify_cb (GtkMediaStream *stream,
GParamSpec *pspec,
GtkMediaControls *controls)
{
if (g_str_equal (pspec->name, "timestamp"))
update_timestamp (controls);
else if (g_str_equal (pspec->name, "duration"))
update_duration (controls);
else if (g_str_equal (pspec->name, "playing"))
update_playing (controls);
else if (g_str_equal (pspec->name, "seekable"))
update_seekable (controls);
else if (g_str_equal (pspec->name, "muted"))
update_volume (controls);
else if (g_str_equal (pspec->name, "volume"))
update_volume (controls);
else if (g_str_equal (pspec->name, "has-audio"))
update_volume (controls);
}
/**
* gtk_media_controls_set_media_stream:
* @controls: a `GtkMediaControls` widget
* @stream: (nullable): a `GtkMediaStream`
*
* Sets the stream that is controlled by @controls.
*/
void
gtk_media_controls_set_media_stream (GtkMediaControls *controls,
GtkMediaStream *stream)
{
g_return_if_fail (GTK_IS_MEDIA_CONTROLS (controls));
g_return_if_fail (stream == NULL || GTK_IS_MEDIA_STREAM (stream));
if (controls->stream == stream)
return;
if (controls->stream)
{
g_signal_handlers_disconnect_by_func (controls->stream,
gtk_media_controls_notify_cb,
controls);
g_object_unref (controls->stream);
controls->stream = NULL;
}
if (stream)
{
controls->stream = g_object_ref (stream);
g_signal_connect (controls->stream,
"notify",
G_CALLBACK (gtk_media_controls_notify_cb),
controls);
}
update_all (controls);
gtk_widget_set_sensitive (controls->box, stream != NULL);
g_object_notify_by_pspec (G_OBJECT (controls), properties[PROP_MEDIA_STREAM]);
}
|