[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / browser / device_sensors / ambient_light_mac.cc
blob187f6a0697d21c93312b079c5448a832f91ff085
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file is based on http://osxbook.com/book/bonus/chapter10/light/
7 #include "content/browser/device_sensors/ambient_light_mac.h"
9 #include "base/mac/scoped_cftyperef.h"
10 #include "base/mac/scoped_ioobject.h"
12 namespace content {
14 namespace {
15 enum LmuFunctionIndex {
16 kGetSensorReadingID = 0, // getSensorReading(int *, int *)
17 kGetLEDBrightnessID = 1, // getLEDBrightness(int, int *)
18 kSetLEDBrightnessID = 2, // setLEDBrightness(int, int, int *)
19 kSetLEDFadeID = 3, // setLEDFade(int, int, int, int *)
21 } // namespace
23 // static
24 scoped_ptr<AmbientLightSensor> AmbientLightSensor::Create() {
25 scoped_ptr<AmbientLightSensor> light_sensor(new AmbientLightSensor);
26 return light_sensor->Init() ? light_sensor.Pass() : nullptr;
29 AmbientLightSensor::~AmbientLightSensor() {
30 if (!io_connection_)
31 IOServiceClose(io_connection_);
34 AmbientLightSensor::AmbientLightSensor() : io_connection_(IO_OBJECT_NULL) {
37 bool AmbientLightSensor::Init() {
38 // Tested and verified by riju that the following call works on
39 // MacBookPro9,1 : Macbook Pro 15" (Mid 2012 model)
40 // MacBookPro10,1 : Macbook Pro 15" (Retina Display, Early 2013 model).
41 // MacBookPro10,2 : Macbook Pro 13" (Retina Display, Early 2013 model).
42 // MacBookAir5,2 : Macbook Air 13" (Mid 2012 model) (by François Beaufort).
43 // MacBookAir6,2 : Macbook Air 13" (Mid 2013 model).
44 // Testing plans : please download the code and follow the comments :-
45 // https://gist.github.com/riju/74af8c81a665e412d122/
46 // and add an entry here about the model and the status returned by the code.
48 // Look up a registered IOService object whose class is AppleLMUController.
49 base::mac::ScopedIOObject<io_service_t> service_object(
50 IOServiceGetMatchingService(kIOMasterPortDefault,
51 IOServiceMatching("AppleLMUController")));
53 // Return early if the ambient light sensor is not present.
54 if (!service_object)
55 return false;
57 // Create a connection to the IOService object.
58 kern_return_t kr =
59 IOServiceOpen(service_object, mach_task_self(), 0, &io_connection_);
61 // IOServiceOpen error.
62 if (kr != KERN_SUCCESS || io_connection_ == IO_OBJECT_NULL)
63 return false;
65 uint64_t lux_values[2];
66 return ReadSensorValue(lux_values);
69 bool AmbientLightSensor::ReadSensorValue(uint64_t lux_values[2]) {
70 uint32_t scalar_output_count = 2;
71 kern_return_t kr = IOConnectCallMethod(
72 io_connection_, LmuFunctionIndex::kGetSensorReadingID, nullptr, 0,
73 nullptr, 0, lux_values, &scalar_output_count, nullptr, 0);
75 return kr == KERN_SUCCESS;
78 } // namespace content