| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/braille_display_private/braille_controll
er_brlapi.h" | 5 #include "chrome/browser/extensions/api/braille_display_private/braille_controll
er_brlapi.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cerrno> | 8 #include <cerrno> |
| 9 #include <cstring> | 9 #include <cstring> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/extensions/api/braille_display_private/brlapi_connectio
n.h" | 15 #include "chrome/browser/extensions/api/braille_display_private/brlapi_connectio
n.h" |
| 16 #include "chrome/browser/extensions/api/braille_display_private/brlapi_keycode_m
ap.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 using content::BrowserThread; | 20 using content::BrowserThread; |
| 20 using base::Time; | 21 using base::Time; |
| 21 using base::TimeDelta; | 22 using base::TimeDelta; |
| 22 namespace api { | 23 namespace api { |
| 23 namespace braille_display_private { | 24 namespace braille_display_private { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 // Delay between detecting a directory update and trying to connect | 27 // Delay between detecting a directory update and trying to connect |
| 27 // to the brlapi. | 28 // to the brlapi. |
| 28 const int64 kConnectionDelayMs = 500; | 29 const int64 kConnectionDelayMs = 500; |
| 29 // How long to periodically retry connecting after a brltty restart. | 30 // How long to periodically retry connecting after a brltty restart. |
| 30 // Some displays are slow to connect. | 31 // Some displays are slow to connect. |
| 31 const int64 kConnectRetryTimeout = 20000; | 32 const int64 kConnectRetryTimeout = 20000; |
| 32 // Bitmask for all braille dots in a key command argument, which coincides | |
| 33 // with the representation in the braille_dots member of the KeyEvent | |
| 34 // class. | |
| 35 const int kAllDots = BRLAPI_DOT1 | BRLAPI_DOT2 | BRLAPI_DOT3 | BRLAPI_DOT4 | | |
| 36 BRLAPI_DOT5 | BRLAPI_DOT6 | BRLAPI_DOT7 | BRLAPI_DOT8; | |
| 37 } // namespace | 33 } // namespace |
| 38 | 34 |
| 39 BrailleController::BrailleController() { | 35 BrailleController::BrailleController() { |
| 40 } | 36 } |
| 41 | 37 |
| 42 BrailleController::~BrailleController() { | 38 BrailleController::~BrailleController() { |
| 43 } | 39 } |
| 44 | 40 |
| 45 // static | 41 // static |
| 46 BrailleController* BrailleController::GetInstance() { | 42 BrailleController* BrailleController::GetInstance() { |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 return; | 257 return; |
| 262 connection_->Disconnect(); | 258 connection_->Disconnect(); |
| 263 DispatchOnDisplayStateChanged(scoped_ptr<DisplayState>(new DisplayState())); | 259 DispatchOnDisplayStateChanged(scoped_ptr<DisplayState>(new DisplayState())); |
| 264 } | 260 } |
| 265 | 261 |
| 266 scoped_ptr<BrlapiConnection> BrailleControllerImpl::CreateBrlapiConnection() { | 262 scoped_ptr<BrlapiConnection> BrailleControllerImpl::CreateBrlapiConnection() { |
| 267 DCHECK(libbrlapi_loader_.loaded()); | 263 DCHECK(libbrlapi_loader_.loaded()); |
| 268 return BrlapiConnection::Create(&libbrlapi_loader_); | 264 return BrlapiConnection::Create(&libbrlapi_loader_); |
| 269 } | 265 } |
| 270 | 266 |
| 271 scoped_ptr<KeyEvent> BrailleControllerImpl::MapKeyCode(brlapi_keyCode_t code) { | |
| 272 brlapi_expandedKeyCode_t expanded; | |
| 273 if (libbrlapi_loader_.brlapi_expandKeyCode(code, &expanded) != 0) { | |
| 274 LOG(ERROR) << "Couldn't expand key code " << code; | |
| 275 return scoped_ptr<KeyEvent>(); | |
| 276 } | |
| 277 scoped_ptr<KeyEvent> result(new KeyEvent); | |
| 278 result->command = KEY_COMMAND_NONE; | |
| 279 switch (expanded.type) { | |
| 280 case BRLAPI_KEY_TYPE_CMD: | |
| 281 switch (expanded.command) { | |
| 282 case BRLAPI_KEY_CMD_LNUP: | |
| 283 result->command = KEY_COMMAND_LINE_UP; | |
| 284 break; | |
| 285 case BRLAPI_KEY_CMD_LNDN: | |
| 286 result->command = KEY_COMMAND_LINE_DOWN; | |
| 287 break; | |
| 288 case BRLAPI_KEY_CMD_FWINLT: | |
| 289 result->command = KEY_COMMAND_PAN_LEFT; | |
| 290 break; | |
| 291 case BRLAPI_KEY_CMD_FWINRT: | |
| 292 result->command = KEY_COMMAND_PAN_RIGHT; | |
| 293 break; | |
| 294 case BRLAPI_KEY_CMD_TOP: | |
| 295 result->command = KEY_COMMAND_TOP; | |
| 296 break; | |
| 297 case BRLAPI_KEY_CMD_BOT: | |
| 298 result->command = KEY_COMMAND_BOTTOM; | |
| 299 break; | |
| 300 case BRLAPI_KEY_CMD_ROUTE: | |
| 301 result->command = KEY_COMMAND_ROUTING; | |
| 302 result->display_position.reset(new int(expanded.argument)); | |
| 303 break; | |
| 304 case BRLAPI_KEY_CMD_PASSDOTS: | |
| 305 result->command = KEY_COMMAND_DOTS; | |
| 306 result->braille_dots.reset(new int(expanded.argument & kAllDots)); | |
| 307 if ((expanded.argument & BRLAPI_DOTC) != 0) | |
| 308 result->space_key.reset(new bool(true)); | |
| 309 break; | |
| 310 } | |
| 311 break; | |
| 312 } | |
| 313 if (result->command == KEY_COMMAND_NONE) | |
| 314 result.reset(); | |
| 315 return result.Pass(); | |
| 316 } | |
| 317 | |
| 318 void BrailleControllerImpl::DispatchKeys() { | 267 void BrailleControllerImpl::DispatchKeys() { |
| 319 DCHECK(connection_.get()); | 268 DCHECK(connection_.get()); |
| 320 brlapi_keyCode_t code; | 269 brlapi_keyCode_t code; |
| 321 while (true) { | 270 while (true) { |
| 322 int result = connection_->ReadKey(&code); | 271 int result = connection_->ReadKey(&code); |
| 323 if (result < 0) { // Error. | 272 if (result < 0) { // Error. |
| 324 brlapi_error_t* err = connection_->BrlapiError(); | 273 brlapi_error_t* err = connection_->BrlapiError(); |
| 325 if (err->brlerrno == BRLAPI_ERROR_LIBCERR && err->libcerrno == EINTR) | 274 if (err->brlerrno == BRLAPI_ERROR_LIBCERR && err->libcerrno == EINTR) |
| 326 continue; | 275 continue; |
| 327 // Disconnect on other errors. | 276 // Disconnect on other errors. |
| 328 VLOG(1) << "BrlAPI error: " << connection_->BrlapiStrError(); | 277 VLOG(1) << "BrlAPI error: " << connection_->BrlapiStrError(); |
| 329 Disconnect(); | 278 Disconnect(); |
| 330 return; | 279 return; |
| 331 } else if (result == 0) { // No more data. | 280 } else if (result == 0) { // No more data. |
| 332 return; | 281 return; |
| 333 } | 282 } |
| 334 scoped_ptr<KeyEvent> event = MapKeyCode(code); | 283 scoped_ptr<KeyEvent> event = BrlapiKeyCodeToEvent(code); |
| 335 if (event) | 284 if (event) |
| 336 DispatchKeyEvent(event.Pass()); | 285 DispatchKeyEvent(event.Pass()); |
| 337 } | 286 } |
| 338 } | 287 } |
| 339 | 288 |
| 340 void BrailleControllerImpl::DispatchKeyEvent(scoped_ptr<KeyEvent> event) { | 289 void BrailleControllerImpl::DispatchKeyEvent(scoped_ptr<KeyEvent> event) { |
| 341 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 290 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 342 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 291 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 343 base::Bind( | 292 base::Bind( |
| 344 &BrailleControllerImpl::DispatchKeyEvent, | 293 &BrailleControllerImpl::DispatchKeyEvent, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 362 } | 311 } |
| 363 return; | 312 return; |
| 364 } | 313 } |
| 365 FOR_EACH_OBSERVER(BrailleObserver, observers_, | 314 FOR_EACH_OBSERVER(BrailleObserver, observers_, |
| 366 OnBrailleDisplayStateChanged(*new_state)); | 315 OnBrailleDisplayStateChanged(*new_state)); |
| 367 } | 316 } |
| 368 | 317 |
| 369 } // namespace braille_display_private | 318 } // namespace braille_display_private |
| 370 } // namespace api | 319 } // namespace api |
| 371 } // namespace extensions | 320 } // namespace extensions |
| OLD | NEW |