]>
Commit | Line | Data |
---|---|---|
b5d655f7 A |
1 | /* |
2 | * Copyright (c) 2007 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
1f2f436a | 24 | #include <dispatch/dispatch.h> |
b5d655f7 A |
25 | #include <libkern/OSThermalNotification.h> |
26 | #include <notify.h> | |
27 | ||
1f2f436a | 28 | #define OSThermalStatusName "com.apple.system.thermalstatus" |
b5d655f7 | 29 | |
1f2f436a | 30 | const char * const kOSThermalNotificationName = OSThermalStatusName; |
b5d655f7 | 31 | |
1f2f436a A |
32 | static const char * const kOSThermalMitigationNames[kOSThermalMitigationCount] = { |
33 | OSThermalStatusName, | |
34 | "com.apple.system.thermalmitigation.70percenttorch", | |
35 | "com.apple.system.thermalmitigation.70percentbacklight", | |
36 | "com.apple.system.thermalmitigation.50percenttorch", | |
37 | "com.apple.system.thermalmitigation.50percentbacklight", | |
38 | "com.apple.system.thermalmitigation.disabletorch", | |
39 | "com.apple.system.thermalmitigation.25percentbacklight", | |
40 | "com.apple.system.thermalmitigation.disablemapshalo", | |
41 | "com.apple.system.thermalmitigation.appterminate", | |
ad3c9f2a A |
42 | "com.apple.system.thermalmitigation.devicerestart", |
43 | "com.apple.system.thermalmitigation.thermaltableready" | |
1f2f436a | 44 | }; |
b5d655f7 | 45 | |
1f2f436a A |
46 | static int tokens[kOSThermalMitigationCount]; |
47 | static dispatch_once_t predicates[kOSThermalMitigationCount]; | |
ad3c9f2a | 48 | static bool thermalLevelsReady = false; |
b5d655f7 | 49 | |
1f2f436a A |
50 | OSThermalNotificationLevel _OSThermalNotificationLevelForBehavior(int behavior) |
51 | { | |
52 | uint64_t val = OSThermalNotificationLevelAny; | |
53 | if (behavior >= 0 && behavior < kOSThermalMitigationCount) { | |
54 | dispatch_once(&predicates[behavior], ^{ | |
ad3c9f2a | 55 | (void)notify_register_check(kOSThermalMitigationNames[behavior], &tokens[behavior]); |
1f2f436a A |
56 | }); |
57 | (void)notify_get_state(tokens[behavior], &val); | |
58 | } | |
b5d655f7 A |
59 | return (OSThermalNotificationLevel)val; |
60 | } | |
1f2f436a A |
61 | |
62 | void _OSThermalNotificationSetLevelForBehavior(int level, int behavior) | |
63 | { | |
64 | uint64_t val = (uint64_t)level; | |
65 | if (behavior >= 0 && behavior < kOSThermalMitigationCount) { | |
66 | dispatch_once(&predicates[behavior], ^{ | |
ad3c9f2a | 67 | (void)notify_register_check(kOSThermalMitigationNames[behavior], &tokens[behavior]); |
1f2f436a A |
68 | }); |
69 | (void)notify_set_state(tokens[behavior], val); | |
ad3c9f2a A |
70 | |
71 | // Note: | |
72 | // - We are ready when we program in the appterminate value. | |
73 | // - Assumes that user programs kOSThermalMitigationNone level less than | |
74 | // kOSThermalMitigationAppTerminate & kOSThermalMitigationDeviceRestart | |
75 | if (behavior == kOSThermalMitigationAppTerminate) { | |
76 | dispatch_once(&predicates[kOSThermalMitigationThermalTableReady], ^{ | |
77 | (void)notify_register_check(kOSThermalMitigationNames[kOSThermalMitigationThermalTableReady], &tokens[kOSThermalMitigationThermalTableReady]); | |
78 | }); | |
79 | (void)notify_set_state(tokens[kOSThermalMitigationThermalTableReady], kOSThermalMitigationCount); | |
80 | } | |
1f2f436a A |
81 | } |
82 | } | |
83 | ||
84 | ||
85 | OSThermalNotificationLevel OSThermalNotificationCurrentLevel(void) | |
86 | { | |
ad3c9f2a A |
87 | if (thermalLevelsReady) { |
88 | return _OSThermalNotificationLevelForBehavior(kOSThermalMitigationNone); | |
89 | } | |
90 | ||
91 | uint64_t tableReady = 0; | |
92 | ||
93 | dispatch_once(&predicates[kOSThermalMitigationThermalTableReady], ^{ | |
94 | (void)notify_register_check(kOSThermalMitigationNames[kOSThermalMitigationThermalTableReady], &tokens[kOSThermalMitigationThermalTableReady]); | |
95 | }); | |
96 | (void)notify_get_state(tokens[kOSThermalMitigationThermalTableReady], &tableReady); | |
97 | ||
98 | // If we are ready then optimize this so we don't call dispatch everytime. | |
99 | if (tableReady == kOSThermalMitigationCount) { | |
100 | thermalLevelsReady = true; | |
101 | return _OSThermalNotificationLevelForBehavior(kOSThermalMitigationNone); | |
102 | } | |
103 | else { | |
104 | // Allow reset so we can dynamically change the table without thermal trap screen appearing. | |
105 | thermalLevelsReady = false; | |
106 | } | |
107 | ||
108 | // Not ready returns -1, which should not be equal or greater than any other thermal state. | |
109 | return OSThermalNotificationLevelAny; | |
1f2f436a | 110 | } |