Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #include "athena/screen/public/screen_manager.h" | |
| 6 #include "athena/system/orientation_controller.h" | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/files/file_path_watcher.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 | |
| 11 namespace athena { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Path of the socket which the sensor daemon creates. | |
| 16 const char kSocketPath[] = "/dev/sensors/orientation"; | |
| 17 | |
| 18 // Threshold after which to rotate in a given direction. | |
| 19 const int kGravityThreshold = 6.0f; | |
| 20 | |
| 21 // Minimum delay before triggering another orientation change. | |
| 22 const int kOrientationChangeDelayNS = 500000000; | |
| 23 | |
| 24 enum { | |
| 25 SENSOR_ACCELEROMETER, | |
| 26 SENSOR_LIGHT, | |
| 27 SENSOR_PROXIMITY | |
| 28 }; | |
| 29 | |
| 30 struct DeviceSensorEvent { | |
|
oshima
2014/08/01 17:15:11
A short discription (meaning and range of values),
flackr
2014/08/06 14:13:51
Added a description of meaning and range of values
| |
| 31 int32_t type; | |
| 32 int64_t timestamp; | |
| 33 union { | |
| 34 float data[3]; | |
| 35 float temperature; | |
| 36 float distance; | |
| 37 float light; | |
| 38 }; | |
| 39 }; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 OrientationController::OrientationController() | |
| 44 : DeviceSocketListener(kSocketPath, sizeof(DeviceSensorEvent)), | |
| 45 last_orientation_change_time_(0), | |
| 46 weak_factory_(this) { | |
| 47 base::FilePath socket_path(kSocketPath); | |
| 48 if (base::PathExists(socket_path)) { | |
| 49 StartListening(); | |
| 50 } else { | |
| 51 // Watch for socket to be created if it doesn't already exist. | |
| 52 watcher_.reset(new base::FilePathWatcher); | |
| 53 watcher_->Watch(socket_path, false, | |
| 54 base::Bind(&OrientationController::OnFilePathChanged, | |
| 55 weak_factory_.GetWeakPtr())); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 OrientationController::~OrientationController() { | |
| 60 } | |
| 61 | |
| 62 void OrientationController::OnDataAvailableOnIO(const void* data) { | |
| 63 const DeviceSensorEvent* event = | |
| 64 static_cast<const DeviceSensorEvent*>(data); | |
| 65 if (event->type != SENSOR_ACCELEROMETER) | |
| 66 return; | |
| 67 | |
| 68 float gravity_x = event->data[0]; | |
| 69 float gravity_y = event->data[1]; | |
| 70 gfx::Display::Rotation rotation; | |
| 71 if (gravity_x < -kGravityThreshold) { | |
| 72 rotation = gfx::Display::ROTATE_270; | |
| 73 } else if (gravity_x > kGravityThreshold) { | |
| 74 rotation = gfx::Display::ROTATE_90; | |
| 75 } else if (gravity_y < -kGravityThreshold) { | |
| 76 rotation = gfx::Display::ROTATE_180; | |
| 77 } else if (gravity_y > kGravityThreshold) { | |
| 78 rotation = gfx::Display::ROTATE_0; | |
| 79 } else { | |
| 80 // No rotation as gravity threshold was not hit. | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 if (rotation == current_rotation_ || | |
| 85 event->timestamp - last_orientation_change_time_ < | |
| 86 kOrientationChangeDelayNS) { | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 last_orientation_change_time_ = event->timestamp; | |
| 91 current_rotation_ = rotation; | |
| 92 | |
| 93 // TODO(flackr): Avoid callbacks using unretained pointers. This could | |
| 94 // technically call RotateOnUI after OrientationController has destructed. We | |
| 95 // should instead use callbacks with weak pointers similar to | |
| 96 // chromeos/accelerometer/accelerometer_reader.cc. | |
| 97 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 98 base::Bind(&OrientationController::RotateOnUI, | |
| 99 base::Unretained(this), rotation)); | |
| 100 } | |
| 101 | |
| 102 void OrientationController::RotateOnUI(gfx::Display::Rotation rotation) { | |
| 103 ScreenManager::Get()->SetRotation(rotation); | |
| 104 } | |
| 105 | |
| 106 void OrientationController::OnFilePathChanged(const base::FilePath& path, | |
| 107 bool error) { | |
| 108 if (error) | |
| 109 return; | |
| 110 | |
| 111 StartListening(); | |
| 112 watcher_.reset(); | |
| 113 } | |
| 114 | |
| 115 } // namespace athena | |
| OLD | NEW |