cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / third_party / sudden_motion_sensor / sudden_motion_sensor_mac.cc
blob456df3bbd914f2a15a7dbb2b61ee9a46fc25139b
1 // Copyright (c) 2012 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.
4 //
5 // This file is based on the SMSLib library.
6 //
7 // SMSLib Sudden Motion Sensor Access Library
8 // Copyright (c) 2010 Suitable Systems
9 // All rights reserved.
11 // Developed by: Daniel Griscom
12 // Suitable Systems
13 // http://www.suitable.com
15 // Permission is hereby granted, free of charge, to any person obtaining a
16 // copy of this software and associated documentation files (the
17 // "Software"), to deal with the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // - Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimers.
26 // - Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimers in the
28 // documentation and/or other materials provided with the distribution.
30 // - Neither the names of Suitable Systems nor the names of its
31 // contributors may be used to endorse or promote products derived from
32 // this Software without specific prior written permission.
34 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
35 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37 // IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR
38 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
39 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
40 // SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
42 // For more information about SMSLib, see
43 // <http://www.suitable.com/tools/smslib.html>
44 // or contact
45 // Daniel Griscom
46 // Suitable Systems
47 // 1 Centre Street, Suite 204
48 // Wakefield, MA 01880
49 // (781) 665-0053
51 #include "sudden_motion_sensor_mac.h"
53 #include <math.h>
54 #include <sys/sysctl.h>
56 #include "base/logging.h"
57 #include "base/mac/scoped_cftyperef.h"
58 #include "base/memory/scoped_ptr.h"
60 struct SuddenMotionSensor::GenericMacbookSensor {
61 // Name of device to be read.
62 const char* service_name;
64 // Number of bytes of the axis data.
65 int axis_size;
67 // Default calibration value for zero g.
68 float zero_g;
70 // Default calibration value for one g (negative when axis is inverted).
71 float one_g;
73 // Kernel function index.
74 unsigned int function;
76 // Size of the sensor record to be sent/received.
77 unsigned int record_size;
80 struct SuddenMotionSensor::AxisData {
81 // Location of the first byte representing the axis in the sensor data.
82 int index;
84 // Axis inversion flag. The value changes often between models.
85 bool inverted;
88 // Sudden Motion Sensor descriptor.
89 struct SuddenMotionSensor::SensorDescriptor {
90 // Prefix of model to be tested.
91 const char* model_name;
93 // Board id of model, or NULL if it doesn't matter.
94 const char* board_id;
96 // Axis-specific data (x,y,z order).
97 AxisData axis[3];
100 // Typical sensor parameters in MacBook models.
101 const SuddenMotionSensor::GenericMacbookSensor
102 SuddenMotionSensor::kGenericSensor = {
103 "SMCMotionSensor", 2,
104 0, 251,
105 5, 40
108 // Supported sensor descriptors. Add entries here to enhance compatibility.
109 // Tested in order; place more specific entries before more general ones. (All
110 // non-tested entries from SMSLib have been removed.)
111 const SuddenMotionSensor::SensorDescriptor
112 SuddenMotionSensor::kSupportedSensors[] = {
113 // Tested by tommyw on a 13" MacBook.
114 { "MacBook1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
116 // Tested by S.Selz. (via avi) on a 13" MacBook.
117 { "MacBook2,1", NULL, { { 0, true }, { 2, false }, { 4, true } } },
119 // Tested by verhees on a 13" MacBook.
120 { "MacBook3,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
122 // Tested by adlr on a 13" MacBook.
123 { "MacBook4,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
125 // Tested by thakis on a 13" MacBook.
126 { "MacBook5,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
128 // Tested by Adam Gerson (via avi) on a 13" MacBook.
129 { "MacBook5,2", NULL, { { 0, false }, { 2, true }, { 4, true } } },
131 // Tested by tommyw on a 13" MacBook.
132 { "MacBook6,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
134 // Tested by avi on a 13" MacBook.
135 { "MacBook7,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
137 // Tested by crc on a 13" MacBook Air.
138 { "MacBookAir1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
140 // Tested by sfiera, pjw on a 13" MacBook Air.
141 { "MacBookAir2,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
143 // Note:
144 // - MacBookAir3,1 (11" MacBook Air, late 2010)
145 // - MacBookAir3,2 (13" MacBook Air, late 2010)
146 // - MacBookAir4,1 (11" MacBook Air, mid 2011)
147 // - MacBookAir4,2 (13" MacBook Air, mid 2011)
148 // - MacBookAir5,1 (11" MacBook Air, mid 2012)
149 // - MacBookAir5,2 (13" MacBook Air, mid 2012)
150 // - MacBookAir6,1 (11" MacBook Air, mid 2013)
151 // - MacBookAir6,2 (13" MacBook Air, mid 2013)
152 // have no accelerometer sensors.
154 // Tested by crc on a 15" MacBook Pro.
155 { "MacBookPro1,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
157 // Tested by Raul Cuza (via avi) on a 17" MacBook Pro.
158 { "MacBookPro1,2", NULL, { { 0, true }, { 2, true }, { 4, false } } },
160 // Tested by L.V. (via avi) on a 17" MacBook Pro.
161 { "MacBookPro2,1", NULL, { { 0, true }, { 2, false }, { 4, true } } },
163 // Tested by leandrogracia on a 15" MacBook Pro.
164 { "MacBookPro2,2", NULL, { { 0, true }, { 2, true }, { 4, false } } },
166 // Tested by S.Som. (via avi) on a 17" MacBook Pro.
167 { "MacBookPro3,1", "Mac-F42388C8",
168 { { 0, true }, { 2, false }, { 4, true } } },
170 // Tested by leandrogracia on a 15" MacBook Pro.
171 { "MacBookPro3,1", NULL, { { 0, false }, { 2, true }, { 4, true } } },
173 // Tested by leandrogracia on a 15" MacBook Pro.
174 // Tested by Eric Shapiro (via avi) on a 17" MacBook Pro.
175 { "MacBookPro4,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
177 // MacBookPro5,1 handled by the generic case below.
178 // Tested by leandrogracia on a 15" MacBook Pro.
180 // MacBookPro5,2 handled by the generic case below.
181 // Tested by S.Selz. (via avi) on a 17" MacBook Pro.
183 // Tested by dmaclach on a 15" MacBook Pro.
184 { "MacBookPro5,3", NULL, { { 2, false }, { 0, false }, { 4, true } } },
186 // MacBookPro5,4 handled by the generic case below.
187 // Tested by leandrogracia on a 15" MacBook Pro.
189 // Tested by leandrogracia on a 13" MacBook Pro.
190 { "MacBookPro5,5", NULL, { { 0, true }, { 2, true }, { 4, false } } },
192 // MacBookPro6,1 handled by the generic case below.
193 // Tested by khom, leadpipe on a 17" MacBook Pro.
195 // Tested by leandrogracia on a 15" MacBook Pro.
196 { "MacBookPro6,2", NULL, { { 0, true }, { 2, false }, { 4, true } } },
198 // Tested by leandrogracia on a 13" MacBook Pro.
199 { "MacBookPro7,1", NULL, { { 0, true }, { 2, true }, { 4, false } } },
201 // MacBookPro8,1 handled by the generic case below.
202 // Tested by avi on a 13" MacBook Pro.
204 // MacBookPro8,2 handled by the generic case below.
205 // Tested by avi on a 15" MacBook Pro.
207 // MacBookPro8,3 handled by the generic case below.
208 // Tested by avi on a 17" MacBook Pro.
210 // MacBookPro9,1 handled by the generic case below.
211 // Tested by avi on a 15" MacBook Pro.
213 // MacBookPro9,2 handled by the generic case below.
214 // Tested by avi on a 13" MacBook Pro.
216 // Note:
217 // - MacBookPro10,1 (15" MacBook Pro with Retina display)
218 // has no accelerometer sensors.
220 // MacBookPro10,2 handled by the generic case below.
221 // Tested by avi on a 13" MacBook Pro with Retina display.
223 // Generic MacBook accelerometer sensor data, to be used for future models
224 // as well as models for which it is verified to be correct. Note that this
225 // configuration may have problems with inverted axes when used generically
226 // for untested models.
227 { "", NULL, { { 0, false }, { 2, false }, { 4, false } } }
230 // Create a SuddenMotionSensor object and return NULL if no valid sensor found.
231 SuddenMotionSensor* SuddenMotionSensor::Create() {
232 scoped_ptr<SuddenMotionSensor> accelerometer(new SuddenMotionSensor);
233 return accelerometer->Init() ? accelerometer.release() : NULL;
236 SuddenMotionSensor::~SuddenMotionSensor() {
237 IOServiceClose(io_connection_);
240 SuddenMotionSensor::SuddenMotionSensor()
241 : sensor_(NULL),
242 io_connection_(0) {
245 // Retrieve per-axis accelerometer values.
246 bool SuddenMotionSensor::ReadSensorValues(float axes[3]) {
247 DCHECK(sensor_);
249 // Reset output record memory buffer.
250 std::fill(output_record_.begin(), output_record_.end(), 0x00);
252 // Read record data from memory.
253 const size_t kInputSize = kGenericSensor.record_size;
254 size_t output_size = kGenericSensor.record_size;
256 if (IOConnectCallStructMethod(io_connection_, kGenericSensor.function,
257 static_cast<const char *>(&input_record_[0]), kInputSize,
258 &output_record_[0], &output_size) != KERN_SUCCESS) {
259 return false;
262 // Calculate per-axis calibrated values.
263 float axis_value[3];
265 for (int i = 0; i < 3; ++i) {
266 int sensor_value = 0;
267 int size = kGenericSensor.axis_size;
268 int index = sensor_->axis[i].index;
270 // Important Note: Little endian is assumed as this code is Mac-only
271 // and PowerPC is currently not supported.
272 memcpy(&sensor_value, &output_record_[index], size);
274 sensor_value = ExtendSign(sensor_value, size);
276 // Correct value using the current calibration.
277 axis_value[i] = static_cast<float>(sensor_value - kGenericSensor.zero_g) /
278 kGenericSensor.one_g;
280 // Make sure we reject any NaN or infinite values.
281 if (!isfinite(axis_value[i]))
282 return false;
284 // Clamp value to the [-1, 1] range.
285 if (axis_value[i] < -1.0)
286 axis_value[i] = -1.0;
287 else if (axis_value[i] > 1.0)
288 axis_value[i] = 1.0;
290 // Apply axis inversion.
291 if (sensor_->axis[i].inverted)
292 axis_value[i] = -axis_value[i];
295 axes[0] = axis_value[0];
296 axes[1] = axis_value[1];
297 axes[2] = axis_value[2];
299 return true;
302 // Probe the local hardware looking for a supported sensor device
303 // and initialize an I/O connection to it.
304 bool SuddenMotionSensor::Init() {
305 // Request model name from the kernel.
306 char local_model[32]; // size from SMSLib
307 size_t local_model_size = sizeof(local_model);
308 int params[2] = { CTL_HW, HW_MODEL };
309 if (sysctl(params, 2, local_model, &local_model_size, NULL, 0) != 0)
310 return false;
312 const SensorDescriptor* sensor_candidate = NULL;
314 // Look for the current model in the supported sensor list.
315 base::ScopedCFTypeRef<CFDataRef> board_id_data;
316 const int kNumSensors = arraysize(kSupportedSensors);
318 for (int i = 0; i < kNumSensors; ++i) {
319 // Check if the supported sensor model name is a prefix
320 // of the local hardware model (empty names are accepted).
321 const char* p1 = kSupportedSensors[i].model_name;
322 for (const char* p2 = local_model; *p1 != '\0' && *p1 == *p2; ++p1, ++p2)
323 continue;
324 if (*p1 != '\0')
325 continue;
327 // Check the board id.
328 if (kSupportedSensors[i].board_id) {
329 if (!board_id_data.get()) {
330 CFMutableDictionaryRef dict =
331 IOServiceMatching("IOPlatformExpertDevice");
332 if (!dict)
333 continue;
335 io_service_t platform_expert =
336 IOServiceGetMatchingService(kIOMasterPortDefault, dict);
337 if (!platform_expert)
338 continue;
340 board_id_data.reset((CFDataRef)
341 IORegistryEntryCreateCFProperty(platform_expert,
342 CFSTR("board-id"),
343 kCFAllocatorDefault,
344 0));
345 IOObjectRelease(platform_expert);
346 if (!board_id_data.get())
347 continue;
350 if (strcmp(kSupportedSensors[i].board_id,
351 (const char*)CFDataGetBytePtr(board_id_data)) != 0) {
352 continue;
356 // Local hardware found in the supported sensor list.
357 sensor_candidate = &kSupportedSensors[i];
359 // Get a dictionary of the services matching to the one in the sensor.
360 CFMutableDictionaryRef dict =
361 IOServiceMatching(kGenericSensor.service_name);
362 if (!dict)
363 continue;
365 // Get the first matching service.
366 io_service_t device = IOServiceGetMatchingService(kIOMasterPortDefault,
367 dict);
368 if (!device)
369 continue;
371 // Try to open device.
372 kern_return_t result;
373 result = IOServiceOpen(device, mach_task_self(), 0, &io_connection_);
374 IOObjectRelease(device);
375 if (result != KERN_SUCCESS || io_connection_ == 0)
376 return false;
378 // Local sensor service confirmed by IOKit.
379 sensor_ = sensor_candidate;
380 break;
383 if (sensor_ == NULL)
384 return false;
386 // Allocate and initialize input/output records.
387 input_record_.resize(kGenericSensor.record_size, 0x01);
388 output_record_.resize(kGenericSensor.record_size, 0x00);
390 // Try to retrieve the current orientation.
391 float test_axes[3];
392 return ReadSensorValues(test_axes);
395 // Extend the sign of an integer of less than 32 bits to a 32-bit integer.
396 int SuddenMotionSensor::ExtendSign(int value, size_t size) {
397 switch (size) {
398 case 1:
399 if (value & 0x00000080)
400 return value | 0xffffff00;
401 break;
403 case 2:
404 if (value & 0x00008000)
405 return value | 0xffff0000;
406 break;
408 case 3:
409 if (value & 0x00800000)
410 return value | 0xff000000;
411 break;
413 default:
414 LOG(FATAL) << "Invalid integer size for sign extension: " << size;
417 return value;