1 /// Translated from C to D
2 module glfw3.posix_time;
3 
4 extern(C): @nogc: nothrow: __gshared:
5 //========================================================================
6 // GLFW 3.3 POSIX - www.glfw.org
7 //------------------------------------------------------------------------
8 // Copyright (c) 2002-2006 Marcus Geelnard
9 // Copyright (c) 2006-2017 Camilla Löwy <elmindreda@glfw.org>
10 //
11 // This software is provided 'as-is', without any express or implied
12 // warranty. In no event will the authors be held liable for any damages
13 // arising from the use of this software.
14 //
15 // Permission is granted to anyone to use this software for any purpose,
16 // including commercial applications, and to alter it and redistribute it
17 // freely, subject to the following restrictions:
18 //
19 // 1. The origin of this software must not be misrepresented; you must not
20 //    claim that you wrote the original software. If you use this software
21 //    in a product, an acknowledgment in the product documentation would
22 //    be appreciated but is not required.
23 //
24 // 2. Altered source versions must be plainly marked as such, and must not
25 //    be misrepresented as being the original software.
26 //
27 // 3. This notice may not be removed or altered from any source
28 //    distribution.
29 //
30 //========================================================================
31 // It is fine to use C99 in this file because it will not be built with VS
32 //========================================================================
33 
34 import glfw3.internal;
35 
36 import core.sys.posix.time;
37 import core.sys.posix.sys.time;
38 
39 import core.stdc.time;
40 
41 mixin template _GLFW_PLATFORM_LIBRARY_TIMER_STATE() {_GLFWtimerPOSIX posix;}
42 
43 // POSIX-specific global timer data
44 //
45 struct _GLFWtimerPOSIX
46 {
47     GLFWbool    monotonic;
48     ulong    frequency;
49 }
50 
51 //////////////////////////////////////////////////////////////////////////
52 //////                       GLFW internal API                      //////
53 //////////////////////////////////////////////////////////////////////////
54 
55 // Initialise timer
56 //
57 void _glfwInitTimerPOSIX() {
58     timespec ts;
59     if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
60     {
61         _glfw.timer.posix.monotonic = GLFW_TRUE;
62         _glfw.timer.posix.frequency = 1000000000;
63     }
64 }
65 
66 
67 //////////////////////////////////////////////////////////////////////////
68 //////                       GLFW platform API                      //////
69 //////////////////////////////////////////////////////////////////////////
70 
71 ulong _glfwPlatformGetTimerValue() {
72     if (_glfw.timer.posix.monotonic) {
73         timespec ts;
74         clock_gettime(CLOCK_MONOTONIC, &ts);
75         return cast(ulong) ts.tv_sec * cast(ulong) 1000000000 + cast(ulong) ts.tv_nsec;
76     } else {
77         timeval tv;
78         gettimeofday(&tv, null);
79         return cast(ulong) tv.tv_sec * cast(ulong) 1000000 + cast(ulong) tv.tv_usec;
80     }
81 }
82 
83 ulong _glfwPlatformGetTimerFrequency() {
84     return _glfw.timer.posix.frequency;
85 }