Bug 1646817 - Support DocumentChannel process switching in sidebars and popups r...
[gecko.git] / hal / cocoa / CocoaSensor.mm
blob96e00369e8eba98e73461f925c6ed7b8fdd07a20
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #include "Hal.h"
5 #include "nsITimer.h"
6 #include "smslib.h"
7 #include "nsComponentManagerUtils.h"
9 #include <mach/mach.h>
10 #include <cmath>
11 #import <IOKit/IOKitLib.h>
13 #define MEAN_GRAVITY 9.80665
14 #define DEFAULT_SENSOR_POLL 100
15 using namespace mozilla::hal;
16 namespace mozilla {
17 namespace hal_impl {
18 static nsITimer* sUpdateTimer = nullptr;
19 static bool sActiveSensors[NUM_SENSOR_TYPE];
20 static io_connect_t sDataPort = IO_OBJECT_NULL;
21 static uint64_t sLastMean = -1;
22 static float LMUvalueToLux(uint64_t aValue) {
23   // Conversion formula from regression. See Bug 793728.
24   // -3*(10^-27)*x^4 + 2.6*(10^-19)*x^3 + -3.4*(10^-12)*x^2 + 3.9*(10^-5)*x - 0.19
25   long double powerC4 = 1 / pow((long double)10, 27);
26   long double powerC3 = 1 / pow((long double)10, 19);
27   long double powerC2 = 1 / pow((long double)10, 12);
28   long double powerC1 = 1 / pow((long double)10, 5);
30   long double term4 = -3.0 * powerC4 * pow(aValue, 4);
31   long double term3 = 2.6 * powerC3 * pow(aValue, 3);
32   long double term2 = -3.4 * powerC2 * pow(aValue, 2);
33   long double term1 = 3.9 * powerC1 * aValue;
35   float lux = ceil(static_cast<float>(term4 + term3 + term2 + term1 - 0.19));
36   return lux > 0 ? lux : 0;
38 void UpdateHandler(nsITimer* aTimer, void* aClosure) {
39   for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
40     if (!sActiveSensors[i]) {
41       continue;
42     }
43     SensorType sensor = static_cast<SensorType>(i);
44     nsTArray<float> values;
45     if (sensor == SENSOR_ACCELERATION) {
46       sms_acceleration accel;
47       smsGetData(&accel);
49       values.AppendElement(accel.x * MEAN_GRAVITY);
50       values.AppendElement(accel.y * MEAN_GRAVITY);
51       values.AppendElement(accel.z * MEAN_GRAVITY);
52     } else if (sensor == SENSOR_LIGHT && sDataPort != IO_OBJECT_NULL) {
53       kern_return_t kr;
54       uint32_t outputs = 2;
55       uint64_t lightLMU[outputs];
57       kr = IOConnectCallMethod(sDataPort, 0, nil, 0, nil, 0, lightLMU, &outputs, nil, 0);
58       if (kr == KERN_SUCCESS) {
59         uint64_t mean = (lightLMU[0] + lightLMU[1]) / 2;
60         if (mean == sLastMean) {
61           continue;
62         }
63         sLastMean = mean;
64         values.AppendElement(LMUvalueToLux(mean));
65       } else if (kr == kIOReturnBusy) {
66         continue;
67       }
68     }
70     hal::SensorData sdata(sensor, PR_Now(), values);
71     hal::NotifySensorChange(sdata);
72   }
74 void EnableSensorNotifications(SensorType aSensor) {
75   if (aSensor == SENSOR_ACCELERATION) {
76     int result = smsStartup(nil, nil);
78     if (result != SMS_SUCCESS) {
79       return;
80     }
82     if (!smsLoadCalibration()) {
83       return;
84     }
85   } else if (aSensor == SENSOR_LIGHT) {
86     io_service_t serviceObject;
87     serviceObject =
88         IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));
89     if (!serviceObject) {
90       return;
91     }
92     kern_return_t kr;
93     kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &sDataPort);
94     IOObjectRelease(serviceObject);
95     if (kr != KERN_SUCCESS) {
96       return;
97     }
98   } else {
99     NS_WARNING("EnableSensorNotifications called on an unknown sensor type");
100     return;
101   }
102   sActiveSensors[aSensor] = true;
104   if (!sUpdateTimer) {
105     CallCreateInstance("@mozilla.org/timer;1", &sUpdateTimer);
106     if (sUpdateTimer) {
107       sUpdateTimer->InitWithNamedFuncCallback(UpdateHandler, nullptr, DEFAULT_SENSOR_POLL,
108                                               nsITimer::TYPE_REPEATING_SLACK,
109                                               "hal_impl::UpdateHandler");
110     }
111   }
113 void DisableSensorNotifications(SensorType aSensor) {
114   if (!sActiveSensors[aSensor] || (aSensor != SENSOR_ACCELERATION && aSensor != SENSOR_LIGHT)) {
115     return;
116   }
118   sActiveSensors[aSensor] = false;
120   if (aSensor == SENSOR_ACCELERATION) {
121     smsShutdown();
122   } else if (aSensor == SENSOR_LIGHT) {
123     IOServiceClose(sDataPort);
124   }
125   // If all sensors are disabled, cancel the update timer.
126   if (sUpdateTimer) {
127     for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
128       if (sActiveSensors[i]) {
129         return;
130       }
131     }
132     sUpdateTimer->Cancel();
133     NS_RELEASE(sUpdateTimer);
134   }
136 }  // namespace hal_impl
137 }  // namespace mozilla