OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/screen/public/screen_manager.h" | 5 #include "athena/screen/public/screen_manager.h" |
6 #include "athena/system/orientation_controller.h" | 6 #include "athena/system/orientation_controller.h" |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path_watcher.h" | 9 #include "base/files/file_path_watcher.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 // Ambient light level in SI lux units. | 52 // Ambient light level in SI lux units. |
53 float light; | 53 float light; |
54 }; | 54 }; |
55 }; | 55 }; |
56 | 56 |
57 } // namespace | 57 } // namespace |
58 | 58 |
59 OrientationController::OrientationController() | 59 OrientationController::OrientationController() |
60 : DeviceSocketListener(kSocketPath, sizeof(DeviceSensorEvent)), | 60 : DeviceSocketListener(kSocketPath, sizeof(DeviceSensorEvent)), |
61 last_orientation_change_time_(0), | 61 last_orientation_change_time_(0) { |
62 shutdown_(false) { | |
63 CHECK(base::MessageLoopForUI::current()); | 62 CHECK(base::MessageLoopForUI::current()); |
64 ui_task_runner_ = base::MessageLoopForUI::current()->task_runner(); | 63 ui_task_runner_ = base::MessageLoopForUI::current()->task_runner(); |
65 } | 64 } |
66 | 65 |
67 void OrientationController::InitWith( | 66 void OrientationController::InitWith( |
68 scoped_refptr<base::TaskRunner> file_task_runner) { | 67 scoped_refptr<base::TaskRunner> io_task_runner) { |
69 file_task_runner_ = file_task_runner; | 68 io_task_runner->PostTask(FROM_HERE, base::Bind( |
70 file_task_runner->PostTask( | 69 &OrientationController::WatchForSocketPathOnIO, this)); |
71 FROM_HERE, | |
72 base::Bind(&OrientationController::WatchForSocketPathOnFILE, this)); | |
73 } | 70 } |
74 | 71 |
75 OrientationController::~OrientationController() { | 72 OrientationController::~OrientationController() { |
76 } | 73 } |
77 | 74 |
78 void OrientationController::Shutdown() { | 75 void OrientationController::WatchForSocketPathOnIO() { |
79 CHECK(file_task_runner_); | |
80 StopListening(); | |
81 file_task_runner_->PostTask( | |
82 FROM_HERE, | |
83 base::Bind(&OrientationController::ShutdownOnFILE, this)); | |
84 } | |
85 | |
86 void OrientationController::ShutdownOnFILE() { | |
87 shutdown_ = true; | |
88 watcher_.reset(); | |
89 } | |
90 | |
91 void OrientationController::WatchForSocketPathOnFILE() { | |
92 CHECK(base::MessageLoopForIO::current()); | 76 CHECK(base::MessageLoopForIO::current()); |
93 if (base::PathExists(base::FilePath(kSocketPath))) { | 77 if (base::PathExists(base::FilePath(kSocketPath))) { |
94 ui_task_runner_->PostTask(FROM_HERE, | 78 ui_task_runner_->PostTask(FROM_HERE, |
95 base::Bind(&OrientationController::StartListening, this)); | 79 base::Bind(&OrientationController::StartListening, this)); |
96 } else { | 80 } else { |
97 watcher_.reset(new base::FilePathWatcher); | 81 watcher_.reset(new base::FilePathWatcher); |
98 watcher_->Watch( | 82 watcher_->Watch( |
99 base::FilePath(kSocketPath), | 83 base::FilePath(kSocketPath), false, |
100 false, | 84 base::Bind(&OrientationController::OnFilePathChangedOnIO, this)); |
101 base::Bind(&OrientationController::OnFilePathChangedOnFILE, this)); | |
102 } | 85 } |
103 } | 86 } |
104 | 87 |
105 void OrientationController::OnFilePathChangedOnFILE(const base::FilePath& path, | 88 void OrientationController::OnFilePathChangedOnIO(const base::FilePath& path, |
106 bool error) { | 89 bool error) { |
107 watcher_.reset(); | 90 watcher_.reset(); |
108 if (error || shutdown_) | 91 if (error) |
109 return; | 92 return; |
110 | 93 |
111 ui_task_runner_->PostTask(FROM_HERE, | 94 ui_task_runner_->PostTask(FROM_HERE, |
112 base::Bind(&OrientationController::StartListening, this)); | 95 base::Bind(&OrientationController::StartListening, this)); |
113 } | 96 } |
114 | 97 |
115 void OrientationController::OnDataAvailableOnFILE(const void* data) { | 98 void OrientationController::OnDataAvailableOnIO(const void* data) { |
116 const DeviceSensorEvent* event = | 99 const DeviceSensorEvent* event = |
117 static_cast<const DeviceSensorEvent*>(data); | 100 static_cast<const DeviceSensorEvent*>(data); |
118 if (event->type != SENSOR_ACCELEROMETER) | 101 if (event->type != SENSOR_ACCELEROMETER) |
119 return; | 102 return; |
120 | 103 |
121 float gravity_x = event->data[0]; | 104 float gravity_x = event->data[0]; |
122 float gravity_y = event->data[1]; | 105 float gravity_y = event->data[1]; |
123 gfx::Display::Rotation rotation; | 106 gfx::Display::Rotation rotation; |
124 if (gravity_x < -kGravityThreshold) { | 107 if (gravity_x < -kGravityThreshold) { |
125 rotation = gfx::Display::ROTATE_270; | 108 rotation = gfx::Display::ROTATE_270; |
(...skipping 15 matching lines...) Expand all Loading... |
141 } | 124 } |
142 | 125 |
143 last_orientation_change_time_ = event->timestamp; | 126 last_orientation_change_time_ = event->timestamp; |
144 current_rotation_ = rotation; | 127 current_rotation_ = rotation; |
145 ui_task_runner_->PostTask(FROM_HERE, | 128 ui_task_runner_->PostTask(FROM_HERE, |
146 base::Bind(&OrientationController::RotateOnUI, this, rotation)); | 129 base::Bind(&OrientationController::RotateOnUI, this, rotation)); |
147 } | 130 } |
148 | 131 |
149 void OrientationController::RotateOnUI(gfx::Display::Rotation rotation) { | 132 void OrientationController::RotateOnUI(gfx::Display::Rotation rotation) { |
150 ScreenManager* screen_manager = ScreenManager::Get(); | 133 ScreenManager* screen_manager = ScreenManager::Get(); |
151 // Since this is called from the FILE thread, the screen manager may no longer | 134 // Since this is called from the IO thread, the screen manager may no longer |
152 // exist. | 135 // exist. |
153 if (screen_manager) | 136 if (screen_manager) |
154 screen_manager->SetRotation(rotation); | 137 screen_manager->SetRotation(rotation); |
155 } | 138 } |
156 | 139 |
157 } // namespace athena | 140 } // namespace athena |
OLD | NEW |