forked from ppy/osu-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimePeriod.cs
103 lines (83 loc) · 2.66 KB
/
TimePeriod.cs
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
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE
using System;
using System.Runtime.InteropServices;
using OpenTK;
namespace osu.Framework.Platform.Windows
{
/// <summary>
/// Set the windows multimedia timer to a specific accuracy.
/// </summary>
internal class TimePeriod : IDisposable
{
private static readonly TimeCaps time_capabilities;
private readonly int period;
[DllImport(@"winmm.dll", ExactSpelling = true)]
private static extern int timeGetDevCaps(ref TimeCaps ptc, int cbtc);
[DllImport(@"winmm.dll", ExactSpelling = true)]
private static extern int timeBeginPeriod(int uPeriod);
[DllImport(@"winmm.dll", ExactSpelling = true)]
private static extern int timeEndPeriod(int uPeriod);
internal static int MinimumPeriod => time_capabilities.wPeriodMin;
internal static int MaximumPeriod => time_capabilities.wPeriodMax;
private bool canAdjust = MaximumPeriod > 0;
static TimePeriod()
{
timeGetDevCaps(ref time_capabilities, Marshal.SizeOf(typeof(TimeCaps)));
}
internal TimePeriod(int period)
{
this.period = period;
}
private bool active;
internal bool Active
{
get { return active; }
set
{
if (value == active || !canAdjust) return;
active = value;
try
{
if (active)
{
canAdjust &= 0 == timeBeginPeriod(MathHelper.Clamp(period, MinimumPeriod, MaximumPeriod));
}
else
{
timeEndPeriod(period);
}
}
catch
{
}
}
}
#region IDisposable Support
private bool disposedValue; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
Active = false;
disposedValue = true;
}
}
~TimePeriod()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
[StructLayout(LayoutKind.Sequential)]
private struct TimeCaps
{
internal readonly int wPeriodMin;
internal readonly int wPeriodMax;
}
}
}