1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gdk.FrameClock;
26 
27 private import gdk.FrameTimings;
28 private import gdk.c.functions;
29 public  import gdk.c.types;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import std.algorithm;
33 
34 
35 /**
36  * A `GdkFrameClock` tells the application when to update and repaint
37  * a surface.
38  * 
39  * This may be synced to the vertical refresh rate of the monitor, for example.
40  * Even when the frame clock uses a simple timer rather than a hardware-based
41  * vertical sync, the frame clock helps because it ensures everything paints at
42  * the same time (reducing the total number of frames).
43  * 
44  * The frame clock can also automatically stop painting when it knows the frames
45  * will not be visible, or scale back animation framerates.
46  * 
47  * `GdkFrameClock` is designed to be compatible with an OpenGL-based implementation
48  * or with mozRequestAnimationFrame in Firefox, for example.
49  * 
50  * A frame clock is idle until someone requests a frame with
51  * [method@Gdk.FrameClock.request_phase]. At some later point that makes sense
52  * for the synchronization being implemented, the clock will process a frame and
53  * emit signals for each phase that has been requested. (See the signals of the
54  * `GdkFrameClock` class for documentation of the phases.
55  * %GDK_FRAME_CLOCK_PHASE_UPDATE and the [signal@GdkFrameClock::update] signal
56  * are most interesting for application writers, and are used to update the
57  * animations, using the frame time given by [metohd@Gdk.FrameClock.get_frame_time].
58  * 
59  * The frame time is reported in microseconds and generally in the same
60  * timescale as g_get_monotonic_time(), however, it is not the same
61  * as g_get_monotonic_time(). The frame time does not advance during
62  * the time a frame is being painted, and outside of a frame, an attempt
63  * is made so that all calls to [method@Gdk.FrameClock.get_frame_time] that
64  * are called at a “similar” time get the same value. This means that
65  * if different animations are timed by looking at the difference in
66  * time between an initial value from [method@Gdk.FrameClock.get_frame_time]
67  * and the value inside the [signal@GdkFrameClock::update] signal of the clock,
68  * they will stay exactly synchronized.
69  */
70 public class FrameClock : ObjectG
71 {
72 	/** the main Gtk struct */
73 	protected GdkFrameClock* gdkFrameClock;
74 
75 	/** Get the main Gtk struct */
76 	public GdkFrameClock* getFrameClockStruct(bool transferOwnership = false)
77 	{
78 		if (transferOwnership)
79 			ownedRef = false;
80 		return gdkFrameClock;
81 	}
82 
83 	/** the main Gtk struct as a void* */
84 	protected override void* getStruct()
85 	{
86 		return cast(void*)gdkFrameClock;
87 	}
88 
89 	/**
90 	 * Sets our main struct and passes it to the parent class.
91 	 */
92 	public this (GdkFrameClock* gdkFrameClock, bool ownedRef = false)
93 	{
94 		this.gdkFrameClock = gdkFrameClock;
95 		super(cast(GObject*)gdkFrameClock, ownedRef);
96 	}
97 
98 
99 	/** */
100 	public static GType getType()
101 	{
102 		return gdk_frame_clock_get_type();
103 	}
104 
105 	/**
106 	 * Starts updates for an animation.
107 	 *
108 	 * Until a matching call to [method@Gdk.FrameClock.end_updating] is made,
109 	 * the frame clock will continually request a new frame with the
110 	 * %GDK_FRAME_CLOCK_PHASE_UPDATE phase. This function may be called multiple
111 	 * times and frames will be requested until gdk_frame_clock_end_updating()
112 	 * is called the same number of times.
113 	 */
114 	public void beginUpdating()
115 	{
116 		gdk_frame_clock_begin_updating(gdkFrameClock);
117 	}
118 
119 	/**
120 	 * Stops updates for an animation.
121 	 *
122 	 * See the documentation for [method@Gdk.FrameClock.begin_updating].
123 	 */
124 	public void endUpdating()
125 	{
126 		gdk_frame_clock_end_updating(gdkFrameClock);
127 	}
128 
129 	/**
130 	 * Gets the frame timings for the current frame.
131 	 *
132 	 * Returns: the `GdkFrameTimings` for the
133 	 *     frame currently being processed, or even no frame is being
134 	 *     processed, for the previous frame. Before any frames have been
135 	 *     processed, returns %NULL.
136 	 */
137 	public FrameTimings getCurrentTimings()
138 	{
139 		auto __p = gdk_frame_clock_get_current_timings(gdkFrameClock);
140 
141 		if(__p is null)
142 		{
143 			return null;
144 		}
145 
146 		return ObjectG.getDObject!(FrameTimings)(cast(GdkFrameTimings*) __p);
147 	}
148 
149 	/**
150 	 * Calculates the current frames-per-second, based on the
151 	 * frame timings of @frame_clock.
152 	 *
153 	 * Returns: the current fps, as a `double`
154 	 */
155 	public double getFps()
156 	{
157 		return gdk_frame_clock_get_fps(gdkFrameClock);
158 	}
159 
160 	/**
161 	 * `GdkFrameClock` maintains a 64-bit counter that increments for
162 	 * each frame drawn.
163 	 *
164 	 * Returns: inside frame processing, the value of the frame counter
165 	 *     for the current frame. Outside of frame processing, the frame
166 	 *     counter for the last frame.
167 	 */
168 	public long getFrameCounter()
169 	{
170 		return gdk_frame_clock_get_frame_counter(gdkFrameClock);
171 	}
172 
173 	/**
174 	 * Gets the time that should currently be used for animations.
175 	 *
176 	 * Inside the processing of a frame, it’s the time used to compute the
177 	 * animation position of everything in a frame. Outside of a frame, it's
178 	 * the time of the conceptual “previous frame,” which may be either
179 	 * the actual previous frame time, or if that’s too old, an updated
180 	 * time.
181 	 *
182 	 * Returns: a timestamp in microseconds, in the timescale of
183 	 *     of g_get_monotonic_time().
184 	 */
185 	public long getFrameTime()
186 	{
187 		return gdk_frame_clock_get_frame_time(gdkFrameClock);
188 	}
189 
190 	/**
191 	 * Returns the frame counter for the oldest frame available in history.
192 	 *
193 	 * `GdkFrameClock` internally keeps a history of `GdkFrameTimings`
194 	 * objects for recent frames that can be retrieved with
195 	 * [method@Gdk.FrameClock.get_timings]. The set of stored frames
196 	 * is the set from the counter values given by
197 	 * [method@Gdk.FrameClock.get_history_start] and
198 	 * [method@Gdk.FrameClock.get_frame_counter], inclusive.
199 	 *
200 	 * Returns: the frame counter value for the oldest frame
201 	 *     that is available in the internal frame history of the
202 	 *     `GdkFrameClock`
203 	 */
204 	public long getHistoryStart()
205 	{
206 		return gdk_frame_clock_get_history_start(gdkFrameClock);
207 	}
208 
209 	/**
210 	 * Predicts a presentation time, based on history.
211 	 *
212 	 * Using the frame history stored in the frame clock, finds the last
213 	 * known presentation time and refresh interval, and assuming that
214 	 * presentation times are separated by the refresh interval,
215 	 * predicts a presentation time that is a multiple of the refresh
216 	 * interval after the last presentation time, and later than @base_time.
217 	 *
218 	 * Params:
219 	 *     baseTime = base time for determining a presentaton time
220 	 *     refreshIntervalReturn = a location to store the
221 	 *         determined refresh interval, or %NULL. A default refresh interval of
222 	 *         1/60th of a second will be stored if no history is present.
223 	 *     presentationTimeReturn = a location to store the next
224 	 *         candidate presentation time after the given base time.
225 	 *         0 will be will be stored if no history is present.
226 	 */
227 	public void getRefreshInfo(long baseTime, out long refreshIntervalReturn, out long presentationTimeReturn)
228 	{
229 		gdk_frame_clock_get_refresh_info(gdkFrameClock, baseTime, &refreshIntervalReturn, &presentationTimeReturn);
230 	}
231 
232 	/**
233 	 * Retrieves a `GdkFrameTimings` object holding timing information
234 	 * for the current frame or a recent frame.
235 	 *
236 	 * The `GdkFrameTimings` object may not yet be complete: see
237 	 * [method@Gdk.FrameTimings.get_complete].
238 	 *
239 	 * Params:
240 	 *     frameCounter = the frame counter value identifying the frame to
241 	 *         be received
242 	 *
243 	 * Returns: the `GdkFrameTimings` object
244 	 *     for the specified frame, or %NULL if it is not available. See
245 	 *     [method@Gdk.FrameClock.get_history_start].
246 	 */
247 	public FrameTimings getTimings(long frameCounter)
248 	{
249 		auto __p = gdk_frame_clock_get_timings(gdkFrameClock, frameCounter);
250 
251 		if(__p is null)
252 		{
253 			return null;
254 		}
255 
256 		return ObjectG.getDObject!(FrameTimings)(cast(GdkFrameTimings*) __p);
257 	}
258 
259 	/**
260 	 * Asks the frame clock to run a particular phase.
261 	 *
262 	 * The signal corresponding the requested phase will be emitted the next
263 	 * time the frame clock processes. Multiple calls to
264 	 * gdk_frame_clock_request_phase() will be combined together
265 	 * and only one frame processed. If you are displaying animated
266 	 * content and want to continually request the
267 	 * %GDK_FRAME_CLOCK_PHASE_UPDATE phase for a period of time,
268 	 * you should use [method@Gdk.FrameClock.begin_updating] instead,
269 	 * since this allows GTK to adjust system parameters to get maximally
270 	 * smooth animations.
271 	 *
272 	 * Params:
273 	 *     phase = the phase that is requested
274 	 */
275 	public void requestPhase(GdkFrameClockPhase phase)
276 	{
277 		gdk_frame_clock_request_phase(gdkFrameClock, phase);
278 	}
279 
280 	/**
281 	 * This signal ends processing of the frame.
282 	 *
283 	 * Applications should generally not handle this signal.
284 	 */
285 	gulong addOnAfterPaint(void delegate(FrameClock) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
286 	{
287 		return Signals.connect(this, "after-paint", dlg, connectFlags ^ ConnectFlags.SWAPPED);
288 	}
289 
290 	/**
291 	 * Begins processing of the frame.
292 	 *
293 	 * Applications should generally not handle this signal.
294 	 */
295 	gulong addOnBeforePaint(void delegate(FrameClock) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
296 	{
297 		return Signals.connect(this, "before-paint", dlg, connectFlags ^ ConnectFlags.SWAPPED);
298 	}
299 
300 	/**
301 	 * Used to flush pending motion events that are being batched up and
302 	 * compressed together.
303 	 *
304 	 * Applications should not handle this signal.
305 	 */
306 	gulong addOnFlushEvents(void delegate(FrameClock) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
307 	{
308 		return Signals.connect(this, "flush-events", dlg, connectFlags ^ ConnectFlags.SWAPPED);
309 	}
310 
311 	/**
312 	 * Emitted as the second step of toolkit and application processing
313 	 * of the frame.
314 	 *
315 	 * Any work to update sizes and positions of application elements
316 	 * should be performed. GTK normally handles this internally.
317 	 */
318 	gulong addOnLayout(void delegate(FrameClock) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
319 	{
320 		return Signals.connect(this, "layout", dlg, connectFlags ^ ConnectFlags.SWAPPED);
321 	}
322 
323 	/**
324 	 * Emitted as the third step of toolkit and application processing
325 	 * of the frame.
326 	 *
327 	 * The frame is repainted. GDK normally handles this internally and
328 	 * emits [signal@Gdk.Surface::render] signals which are turned into
329 	 * [signal@Gtk.Widget::snapshot] signals by GTK.
330 	 */
331 	gulong addOnPaint(void delegate(FrameClock) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
332 	{
333 		return Signals.connect(this, "paint", dlg, connectFlags ^ ConnectFlags.SWAPPED);
334 	}
335 
336 	/**
337 	 * Emitted after processing of the frame is finished.
338 	 *
339 	 * This signal is handled internally by GTK to resume normal
340 	 * event processing. Applications should not handle this signal.
341 	 */
342 	gulong addOnResumeEvents(void delegate(FrameClock) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
343 	{
344 		return Signals.connect(this, "resume-events", dlg, connectFlags ^ ConnectFlags.SWAPPED);
345 	}
346 
347 	/**
348 	 * Emitted as the first step of toolkit and application processing
349 	 * of the frame.
350 	 *
351 	 * Animations should be updated using [method@Gdk.FrameClock.get_frame_time].
352 	 * Applications can connect directly to this signal, or use
353 	 * [method@Gtk.Widget.add_tick_callback] as a more convenient interface.
354 	 */
355 	gulong addOnUpdate(void delegate(FrameClock) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
356 	{
357 		return Signals.connect(this, "update", dlg, connectFlags ^ ConnectFlags.SWAPPED);
358 	}
359 }