Debian Codegraph @

demos/gtk-demo/gtkfishbowl.c

raw
1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2017 Benjamin Otte <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include "gtkfishbowl.h"
21
22 #include "gtk/fallback-c89.c"
23
24 typedef struct _GtkFishbowlPrivate GtkFishbowlPrivate;
25 typedef struct _GtkFishbowlChild GtkFishbowlChild;
26
27 struct _GtkFishbowlPrivate
28 {
29 GtkFishCreationFunc creation_func;
30 GList *children;
31 guint count;
32
33 gint64 last_frame_time;
34 gint64 update_delay;
35 guint tick_id;
36
37 double framerate;
38 int last_benchmark_change;
39
40 guint benchmark : 1;
41 };
42
43 struct _GtkFishbowlChild
44 {
45 GtkWidget *widget;
46 double x;
47 double y;
48 double dx;
49 double dy;
50 };
51
52 enum {
53 PROP_0,
54 PROP_ANIMATING,
55 PROP_BENCHMARK,
56 PROP_COUNT,
57 PROP_FRAMERATE,
58 PROP_UPDATE_DELAY,
59 NUM_PROPERTIES
60 };
61
62 static GParamSpec *props[NUM_PROPERTIES] = { NULL, };
63
64 G_DEFINE_TYPE_WITH_PRIVATE (GtkFishbowl, gtk_fishbowl, GTK_TYPE_CONTAINER)
65
66 static void
67 gtk_fishbowl_init (GtkFishbowl *fishbowl)
68 {
69 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
70
71 gtk_widget_set_has_window (GTK_WIDGET (fishbowl), FALSE);
72
73 priv->update_delay = G_USEC_PER_SEC;
74 }
75
76 /**
77 * gtk_fishbowl_new:
78 *
79 * Creates a new #GtkFishbowl.
80 *
81 * Returns: a new #GtkFishbowl.
82 */
83 GtkWidget*
84 gtk_fishbowl_new (void)
85 {
86 return g_object_new (GTK_TYPE_FISHBOWL, NULL);
87 }
88
89 static void
90 gtk_fishbowl_get_preferred_width (GtkWidget *widget,
91 int *minimum,
92 int *natural)
93 {
94 GtkFishbowl *fishbowl = GTK_FISHBOWL (widget);
95 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
96 GtkFishbowlChild *child;
97 GList *children;
98 gint child_min, child_nat;
99
100 *minimum = 0;
101 *natural = 0;
102
103 for (children = priv->children; children; children = children->next)
104 {
105 child = children->data;
106
107 if (!gtk_widget_get_visible (child->widget))
108 continue;
109
110 gtk_widget_get_preferred_width (child->widget, &child_min, &child_nat);
111
112 *minimum = MAX (*minimum, child_min);
113 *natural = MAX (*natural, child_nat);
114 }
115 }
116
117 static void
118 gtk_fishbowl_get_preferred_height (GtkWidget *widget,
119 int *minimum,
120 int *natural)
121 {
122 GtkFishbowl *fishbowl = GTK_FISHBOWL (widget);
123 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
124 GtkFishbowlChild *child;
125 GList *children;
126 gint child_min, child_nat;
127
128 *minimum = 0;
129 *natural = 0;
130
131 for (children = priv->children; children; children = children->next)
132 {
133 int min_width;
134
135 child = children->data;
136
137 if (!gtk_widget_get_visible (child->widget))
138 continue;
139
140 gtk_widget_get_preferred_width (child->widget, &min_width, NULL);
141 gtk_widget_get_preferred_height_for_width (child->widget, min_width, &child_min, &child_nat);
142
143 *minimum = MAX (*minimum, child_min);
144 *natural = MAX (*natural, child_nat);
145 }
146 }
147
148 static void
149 gtk_fishbowl_size_allocate (GtkWidget *widget,
150 GtkAllocation *allocation)
151 {
152 GtkFishbowl *fishbowl = GTK_FISHBOWL (widget);
153 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
154 GtkFishbowlChild *child;
155 GtkAllocation child_allocation;
156 GtkRequisition child_requisition;
157 GList *children;
158
159 for (children = priv->children; children; children = children->next)
160 {
161 child = children->data;
162
163 if (!gtk_widget_get_visible (child->widget))
164 continue;
165
166 gtk_widget_get_preferred_size (child->widget, &child_requisition, NULL);
167 child_allocation.x = allocation->x + round (child->x * (allocation->width - child_requisition.width));
168 child_allocation.y = allocation->y + round (child->y * (allocation->height - child_requisition.height));
169 child_allocation.width = child_requisition.width;
170 child_allocation.height = child_requisition.height;
171
172 gtk_widget_size_allocate (child->widget, &child_allocation);
173 }
174 }
175
176 static double
177 new_speed (void)
178 {
179 /* 5s to 50s to cross screen seems fair */
180 return g_random_double_range (0.02, 0.2);
181 }
182
183 static void
184 gtk_fishbowl_add (GtkContainer *container,
185 GtkWidget *widget)
186 {
187 GtkFishbowl *fishbowl = GTK_FISHBOWL (container);
188 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
189 GtkFishbowlChild *child_info;
190
191 g_return_if_fail (GTK_IS_FISHBOWL (fishbowl));
192 g_return_if_fail (GTK_IS_WIDGET (widget));
193
194 child_info = g_new0 (GtkFishbowlChild, 1);
195 child_info->widget = widget;
196 child_info->x = 0;
197 child_info->y = 0;
198 child_info->dx = new_speed ();
199 child_info->dy = new_speed ();
200
201 gtk_widget_set_parent (widget, GTK_WIDGET (fishbowl));
202
203 priv->children = g_list_prepend (priv->children, child_info);
204 priv->count++;
205 g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_COUNT]);
206 }
207
208 static void
209 gtk_fishbowl_remove (GtkContainer *container,
210 GtkWidget *widget)
211 {
212 GtkFishbowl *fishbowl = GTK_FISHBOWL (container);
213 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
214 GtkFishbowlChild *child;
215 GtkWidget *widget_bowl = GTK_WIDGET (fishbowl);
216 GList *children;
217
218 for (children = priv->children; children; children = children->next)
219 {
220 child = children->data;
221
222 if (child->widget == widget)
223 {
224 gboolean was_visible = gtk_widget_get_visible (widget);
225
226 gtk_widget_unparent (widget);
227
228 priv->children = g_list_remove_link (priv->children, children);
229 g_list_free (children);
230 g_free (child);
231
232 if (was_visible && gtk_widget_get_visible (widget_bowl))
233 gtk_widget_queue_resize (widget_bowl);
234
235 priv->count--;
236 g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_COUNT]);
237 break;
238 }
239 }
240 }
241
242
243 static void
244 gtk_fishbowl_forall (GtkContainer *container,
245 gboolean include_internals,
246 GtkCallback callback,
247 gpointer callback_data)
248 {
249 GtkFishbowl *fishbowl = GTK_FISHBOWL (container);
250 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
251 GtkFishbowlChild *child;
252 GList *children;
253
254 if (!include_internals)
255 return;
256
257 children = priv->children;
258 while (children)
259 {
260 child = children->data;
261 children = children->next;
262
263 (* callback) (child->widget, callback_data);
264 }
265 }
266
267 static void
268 gtk_fishbowl_dispose (GObject *object)
269 {
270 GtkFishbowl *fishbowl = GTK_FISHBOWL (object);
271
272 gtk_fishbowl_set_animating (fishbowl, FALSE);
273 gtk_fishbowl_set_count (fishbowl, 0);
274
275 G_OBJECT_CLASS (gtk_fishbowl_parent_class)->dispose (object);
276 }
277
278 static void
279 gtk_fishbowl_set_property (GObject *object,
280 guint prop_id,
281 const GValue *value,
282 GParamSpec *pspec)
283 {
284 GtkFishbowl *fishbowl = GTK_FISHBOWL (object);
285
286 switch (prop_id)
287 {
288 case PROP_ANIMATING:
289 gtk_fishbowl_set_animating (fishbowl, g_value_get_boolean (value));
290 break;
291
292 case PROP_BENCHMARK:
293 gtk_fishbowl_set_benchmark (fishbowl, g_value_get_boolean (value));
294 break;
295
296 case PROP_COUNT:
297 gtk_fishbowl_set_count (fishbowl, g_value_get_uint (value));
298 break;
299
300 case PROP_UPDATE_DELAY:
301 gtk_fishbowl_set_update_delay (fishbowl, g_value_get_int64 (value));
302 break;
303
304 default:
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 break;
307 }
308 }
309
310 static void
311 gtk_fishbowl_get_property (GObject *object,
312 guint prop_id,
313 GValue *value,
314 GParamSpec *pspec)
315 {
316 GtkFishbowl *fishbowl = GTK_FISHBOWL (object);
317
318 switch (prop_id)
319 {
320 case PROP_ANIMATING:
321 g_value_set_boolean (value, gtk_fishbowl_get_animating (fishbowl));
322 break;
323
324 case PROP_BENCHMARK:
325 g_value_set_boolean (value, gtk_fishbowl_get_benchmark (fishbowl));
326 break;
327
328 case PROP_COUNT:
329 g_value_set_uint (value, gtk_fishbowl_get_count (fishbowl));
330 break;
331
332 case PROP_FRAMERATE:
333 g_value_set_double (value, gtk_fishbowl_get_framerate (fishbowl));
334 break;
335
336 case PROP_UPDATE_DELAY:
337 g_value_set_int64 (value, gtk_fishbowl_get_update_delay (fishbowl));
338 break;
339
340 default:
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 break;
343 }
344 }
345
346 static void
347 gtk_fishbowl_class_init (GtkFishbowlClass *klass)
348 {
349 GObjectClass *object_class = G_OBJECT_CLASS (klass);
350 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
351 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
352
353 object_class->dispose = gtk_fishbowl_dispose;
354 object_class->set_property = gtk_fishbowl_set_property;
355 object_class->get_property = gtk_fishbowl_get_property;
356
357 widget_class->get_preferred_width = gtk_fishbowl_get_preferred_width;
358 widget_class->get_preferred_height = gtk_fishbowl_get_preferred_height;
359 widget_class->size_allocate = gtk_fishbowl_size_allocate;
360
361 container_class->add = gtk_fishbowl_add;
362 container_class->remove = gtk_fishbowl_remove;
363 container_class->forall = gtk_fishbowl_forall;
364
365 props[PROP_ANIMATING] =
366 g_param_spec_boolean ("animating",
367 "animating",
368 "Whether children are moving around",
369 FALSE,
370 G_PARAM_READWRITE);
371
372 props[PROP_BENCHMARK] =
373 g_param_spec_boolean ("benchmark",
374 "Benchmark",
375 "Adapt the count property to hit the maximum framerate",
376 FALSE,
377 G_PARAM_READWRITE);
378
379 props[PROP_COUNT] =
380 g_param_spec_uint ("count",
381 "Count",
382 "Number of widgets",
383 0, G_MAXUINT,
384 0,
385 G_PARAM_READWRITE);
386
387 props[PROP_FRAMERATE] =
388 g_param_spec_double ("framerate",
389 "Framerate",
390 "Framerate of this widget in frames per second",
391 0, G_MAXDOUBLE,
392 0,
393 G_PARAM_READABLE);
394
395 props[PROP_UPDATE_DELAY] =
396 g_param_spec_int64 ("update-delay",
397 "Update delay",
398 "Number of usecs between updates",
399 0, G_MAXINT64,
400 G_USEC_PER_SEC,
401 G_PARAM_READWRITE);
402
403 g_object_class_install_properties (object_class, NUM_PROPERTIES, props);
404 }
405
406 guint
407 gtk_fishbowl_get_count (GtkFishbowl *fishbowl)
408 {
409 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
410
411 return priv->count;
412 }
413
414 void
415 gtk_fishbowl_set_count (GtkFishbowl *fishbowl,
416 guint count)
417 {
418 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
419
420 if (priv->count == count)
421 return;
422
423 g_object_freeze_notify (G_OBJECT (fishbowl));
424
425 while (priv->count > count)
426 {
427 gtk_fishbowl_remove (GTK_CONTAINER (fishbowl), ((GtkFishbowlChild *) priv->children->data)->widget);
428 }
429
430 while (priv->count < count)
431 {
432 GtkWidget *new_widget;
433
434 new_widget = priv->creation_func ();
435
436 gtk_widget_show (new_widget);
437
438 gtk_fishbowl_add (GTK_CONTAINER (fishbowl), new_widget);
439 }
440
441 g_object_thaw_notify (G_OBJECT (fishbowl));
442 }
443
444 gboolean
445 gtk_fishbowl_get_benchmark (GtkFishbowl *fishbowl)
446 {
447 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
448
449 return priv->benchmark;
450 }
451
452 void
453 gtk_fishbowl_set_benchmark (GtkFishbowl *fishbowl,
454 gboolean benchmark)
455 {
456 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
457
458 if (priv->benchmark == benchmark)
459 return;
460
461 priv->benchmark = benchmark;
462 if (!benchmark)
463 priv->last_benchmark_change = 0;
464
465 g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_BENCHMARK]);
466 }
467
468 gboolean
469 gtk_fishbowl_get_animating (GtkFishbowl *fishbowl)
470 {
471 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
472
473 return priv->tick_id != 0;
474 }
475
476 static gint64
477 guess_refresh_interval (GdkFrameClock *frame_clock)
478 {
479 gint64 interval;
480 gint64 i;
481
482 interval = G_MAXINT64;
483
484 for (i = gdk_frame_clock_get_history_start (frame_clock);
485 i < gdk_frame_clock_get_frame_counter (frame_clock);
486 i++)
487 {
488 GdkFrameTimings *t, *before;
489 gint64 ts, before_ts;
490
491 t = gdk_frame_clock_get_timings (frame_clock, i);
492 before = gdk_frame_clock_get_timings (frame_clock, i - 1);
493 if (t == NULL || before == NULL)
494 continue;
495
496 ts = gdk_frame_timings_get_frame_time (t);
497 before_ts = gdk_frame_timings_get_frame_time (before);
498 if (ts == 0 || before_ts == 0)
499 continue;
500
501 interval = MIN (interval, ts - before_ts);
502 }
503
504 if (interval == G_MAXINT64)
505 return 0;
506
507 return interval;
508 }
509
510 static void
511 gtk_fishbowl_do_update (GtkFishbowl *fishbowl)
512 {
513 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
514 GdkFrameClock *frame_clock;
515 GdkFrameTimings *start, *end;
516 gint64 start_counter, end_counter;
517 gint64 n_frames, expected_frames;
518 gint64 start_timestamp, end_timestamp;
519 gint64 interval;
520
521 frame_clock = gtk_widget_get_frame_clock (GTK_WIDGET (fishbowl));
522 if (frame_clock == NULL)
523 return;
524
525 start_counter = gdk_frame_clock_get_history_start (frame_clock);
526 end_counter = gdk_frame_clock_get_frame_counter (frame_clock);
527 start = gdk_frame_clock_get_timings (frame_clock, start_counter);
528 for (end = gdk_frame_clock_get_timings (frame_clock, end_counter);
529 end_counter > start_counter && end != NULL && !gdk_frame_timings_get_complete (end);
530 end = gdk_frame_clock_get_timings (frame_clock, end_counter))
531 end_counter--;
532 if (end_counter - start_counter < 4)
533 return;
534
535 start_timestamp = gdk_frame_timings_get_presentation_time (start);
536 end_timestamp = gdk_frame_timings_get_presentation_time (end);
537 if (start_timestamp == 0 || end_timestamp == 0)
538 {
539 start_timestamp = gdk_frame_timings_get_frame_time (start);
540 end_timestamp = gdk_frame_timings_get_frame_time (end);
541 }
542
543 n_frames = end_counter - start_counter;
544 priv->framerate = ((double) n_frames) * G_USEC_PER_SEC / (end_timestamp - start_timestamp);
545 g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_FRAMERATE]);
546
547 if (!priv->benchmark)
548 return;
549
550 interval = gdk_frame_timings_get_refresh_interval (end);
551 if (interval == 0)
552 {
553 interval = guess_refresh_interval (frame_clock);
554 if (interval == 0)
555 return;
556 }
557 expected_frames = round ((double) (end_timestamp - start_timestamp) / interval);
558
559 if (n_frames >= expected_frames)
560 {
561 if (priv->last_benchmark_change > 0)
562 priv->last_benchmark_change *= 2;
563 else
564 priv->last_benchmark_change = 1;
565 }
566 else if (n_frames + 1 < expected_frames)
567 {
568 if (priv->last_benchmark_change < 0)
569 priv->last_benchmark_change--;
570 else
571 priv->last_benchmark_change = -1;
572 }
573 else
574 {
575 priv->last_benchmark_change = 0;
576 }
577
578 gtk_fishbowl_set_count (fishbowl, MAX (1, (int) priv->count + priv->last_benchmark_change));
579 }
580
581 static gboolean
582 gtk_fishbowl_tick (GtkWidget *widget,
583 GdkFrameClock *frame_clock,
584 gpointer unused)
585 {
586 GtkFishbowl *fishbowl = GTK_FISHBOWL (widget);
587 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
588 GtkFishbowlChild *child;
589 GList *l;
590 gint64 frame_time, elapsed;
591 gboolean do_update;
592
593 frame_time = gdk_frame_clock_get_frame_time (gtk_widget_get_frame_clock (widget));
594 elapsed = frame_time - priv->last_frame_time;
595 do_update = frame_time / priv->update_delay != priv->last_frame_time / priv->update_delay;
596 priv->last_frame_time = frame_time;
597
598 /* last frame was 0, so we're just starting to animate */
599 if (elapsed == frame_time)
600 return G_SOURCE_CONTINUE;
601
602 for (l = priv->children; l; l = l->next)
603 {
604 child = l->data;
605
606 child->x += child->dx * ((double) elapsed / G_USEC_PER_SEC);
607 child->y += child->dy * ((double) elapsed / G_USEC_PER_SEC);
608
609 if (child->x <= 0)
610 {
611 child->x = 0;
612 child->dx = new_speed ();
613 }
614 else if (child->x >= 1)
615 {
616 child->x = 1;
617 child->dx = - new_speed ();
618 }
619
620 if (child->y <= 0)
621 {
622 child->y = 0;
623 child->dy = new_speed ();
624 }
625 else if (child->y >= 1)
626 {
627 child->y = 1;
628 child->dy = - new_speed ();
629 }
630 }
631
632 gtk_widget_queue_allocate (widget);
633
634 if (do_update)
635 gtk_fishbowl_do_update (fishbowl);
636
637 return G_SOURCE_CONTINUE;
638 }
639
640 void
641 gtk_fishbowl_set_animating (GtkFishbowl *fishbowl,
642 gboolean animating)
643 {
644 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
645
646 if (gtk_fishbowl_get_animating (fishbowl) == animating)
647 return;
648
649 if (animating)
650 {
651 priv->tick_id = gtk_widget_add_tick_callback (GTK_WIDGET (fishbowl),
652 gtk_fishbowl_tick,
653 NULL,
654 NULL);
655 }
656 else
657 {
658 priv->last_frame_time = 0;
659 gtk_widget_remove_tick_callback (GTK_WIDGET (fishbowl), priv->tick_id);
660 priv->tick_id = 0;
661 priv->framerate = 0;
662 g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_FRAMERATE]);
663 }
664
665 g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_ANIMATING]);
666 }
667
668 double
669 gtk_fishbowl_get_framerate (GtkFishbowl *fishbowl)
670 {
671 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
672
673 return priv->framerate;
674 }
675
676 gint64
677 gtk_fishbowl_get_update_delay (GtkFishbowl *fishbowl)
678 {
679 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
680
681 return priv->update_delay;
682 }
683
684 void
685 gtk_fishbowl_set_update_delay (GtkFishbowl *fishbowl,
686 gint64 update_delay)
687 {
688 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
689
690 if (priv->update_delay == update_delay)
691 return;
692
693 priv->update_delay = update_delay;
694
695 g_object_notify_by_pspec (G_OBJECT (fishbowl), props[PROP_UPDATE_DELAY]);
696 }
697
698 void
699 gtk_fishbowl_set_creation_func (GtkFishbowl *fishbowl,
700 GtkFishCreationFunc creation_func)
701 {
702 GtkFishbowlPrivate *priv = gtk_fishbowl_get_instance_private (fishbowl);
703
704 g_object_freeze_notify (G_OBJECT (fishbowl));
705
706 gtk_fishbowl_set_count (fishbowl, 0);
707 priv->last_benchmark_change = 0;
708
709 priv->creation_func = creation_func;
710
711 gtk_fishbowl_set_count (fishbowl, 1);
712
713 g_object_thaw_notify (G_OBJECT (fishbowl));
714 }