Chromium Code Reviews| 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/brlapi_connectio n.h" | 5 #include "chrome/browser/extensions/api/braille_display_private/brlapi_connectio n.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include "base/files/file_descriptor_watcher_posix.h" | 9 #include "base/files/file_descriptor_watcher_posix.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 explicit BrlapiConnectionImpl(LibBrlapiLoader* loader) : | 34 explicit BrlapiConnectionImpl(LibBrlapiLoader* loader) : |
| 35 libbrlapi_loader_(loader) {} | 35 libbrlapi_loader_(loader) {} |
| 36 | 36 |
| 37 ~BrlapiConnectionImpl() override { Disconnect(); } | 37 ~BrlapiConnectionImpl() override { Disconnect(); } |
| 38 | 38 |
| 39 ConnectResult Connect(const OnDataReadyCallback& on_data_ready) override; | 39 ConnectResult Connect(const OnDataReadyCallback& on_data_ready) override; |
| 40 void Disconnect() override; | 40 void Disconnect() override; |
| 41 bool Connected() override { return handle_ != nullptr; } | 41 bool Connected() override { return handle_ != nullptr; } |
| 42 brlapi_error_t* BrlapiError() override; | 42 brlapi_error_t* BrlapiError() override; |
| 43 std::string BrlapiStrError() override; | 43 std::string BrlapiStrError() override; |
| 44 bool GetDisplaySize(size_t* size) override; | 44 bool GetDisplaySize(unsigned int* rows, unsigned int* columns) override; |
| 45 bool WriteDots(const unsigned char* cells) override; | 45 bool WriteDots(const std::vector<unsigned char> cells) override; |
| 46 int ReadKey(brlapi_keyCode_t* keyCode) override; | 46 int ReadKey(brlapi_keyCode_t* keyCode) override; |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 bool CheckConnected(); | 49 bool CheckConnected(); |
| 50 ConnectResult ConnectResultForError(); | 50 ConnectResult ConnectResultForError(); |
| 51 | 51 |
| 52 LibBrlapiLoader* libbrlapi_loader_; | 52 LibBrlapiLoader* libbrlapi_loader_; |
| 53 std::unique_ptr<brlapi_handle_t, base::FreeDeleter> handle_; | 53 std::unique_ptr<brlapi_handle_t, base::FreeDeleter> handle_; |
| 54 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_controller_; | 54 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_controller_; |
| 55 | 55 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 86 path[pathElements++] = kDefaultTtyChromeOS; | 86 path[pathElements++] = kDefaultTtyChromeOS; |
| 87 #endif | 87 #endif |
| 88 if (pathElements == 0 && getenv("WINDOWPATH") == NULL) | 88 if (pathElements == 0 && getenv("WINDOWPATH") == NULL) |
| 89 path[pathElements++] = kDefaultTtyLinux; | 89 path[pathElements++] = kDefaultTtyLinux; |
| 90 if (libbrlapi_loader_->brlapi__enterTtyModeWithPath( | 90 if (libbrlapi_loader_->brlapi__enterTtyModeWithPath( |
| 91 handle_.get(), path, pathElements, NULL) < 0) { | 91 handle_.get(), path, pathElements, NULL) < 0) { |
| 92 LOG(ERROR) << "brlapi: couldn't enter tty mode: " << BrlapiStrError(); | 92 LOG(ERROR) << "brlapi: couldn't enter tty mode: " << BrlapiStrError(); |
| 93 Disconnect(); | 93 Disconnect(); |
| 94 return CONNECT_ERROR_RETRY; | 94 return CONNECT_ERROR_RETRY; |
| 95 } | 95 } |
| 96 unsigned int rows = 0; | |
| 97 unsigned int columns = 0; | |
| 96 | 98 |
| 97 size_t size; | 99 if (!GetDisplaySize(&rows, &columns)) { |
| 98 if (!GetDisplaySize(&size)) { | |
| 99 // Error already logged. | 100 // Error already logged. |
| 100 Disconnect(); | 101 Disconnect(); |
| 101 return CONNECT_ERROR_RETRY; | 102 return CONNECT_ERROR_RETRY; |
| 102 } | 103 } |
| 103 | 104 |
| 104 // A display size of 0 means no display connected. We can't reliably | 105 // A display size of 0 means no display connected. We can't reliably |
| 105 // detect when a display gets connected, so fail and let the caller | 106 // detect when a display gets connected, so fail and let the caller |
| 106 // retry connecting. | 107 // retry connecting. |
| 107 if (size == 0) { | 108 if (rows * columns == 0) { |
| 108 VLOG(1) << "No braille display connected"; | 109 VLOG(1) << "No braille display connected"; |
| 109 Disconnect(); | 110 Disconnect(); |
| 110 return CONNECT_ERROR_RETRY; | 111 return CONNECT_ERROR_RETRY; |
| 111 } | 112 } |
| 112 | 113 |
| 113 const brlapi_keyCode_t extraKeys[] = { | 114 const brlapi_keyCode_t extraKeys[] = { |
| 114 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_OFFLINE, | 115 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_OFFLINE, |
| 115 // brltty 5.1 converts dot input to Unicode characters unless we | 116 // brltty 5.1 converts dot input to Unicode characters unless we |
| 116 // explicitly accept this command. | 117 // explicitly accept this command. |
| 117 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_PASSDOTS, | 118 BRLAPI_KEY_TYPE_CMD | BRLAPI_KEY_CMD_PASSDOTS, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 141 } | 142 } |
| 142 | 143 |
| 143 brlapi_error_t* BrlapiConnectionImpl::BrlapiError() { | 144 brlapi_error_t* BrlapiConnectionImpl::BrlapiError() { |
| 144 return libbrlapi_loader_->brlapi_error_location(); | 145 return libbrlapi_loader_->brlapi_error_location(); |
| 145 } | 146 } |
| 146 | 147 |
| 147 std::string BrlapiConnectionImpl::BrlapiStrError() { | 148 std::string BrlapiConnectionImpl::BrlapiStrError() { |
| 148 return libbrlapi_loader_->brlapi_strerror(BrlapiError()); | 149 return libbrlapi_loader_->brlapi_strerror(BrlapiError()); |
| 149 } | 150 } |
| 150 | 151 |
| 151 bool BrlapiConnectionImpl::GetDisplaySize(size_t* size) { | 152 bool BrlapiConnectionImpl::GetDisplaySize(unsigned int* columns, |
| 153 unsigned int* rows) { | |
| 152 if (!CheckConnected()) { | 154 if (!CheckConnected()) { |
| 153 return false; | 155 return false; |
| 154 } | 156 } |
| 155 unsigned int columns, rows; | 157 if (libbrlapi_loader_->brlapi__getDisplaySize(handle_.get(), columns, rows) < |
| 156 if (libbrlapi_loader_->brlapi__getDisplaySize( | 158 0) { |
| 157 handle_.get(), &columns, &rows) < 0) { | |
| 158 LOG(ERROR) << "Couldn't get braille display size " << BrlapiStrError(); | 159 LOG(ERROR) << "Couldn't get braille display size " << BrlapiStrError(); |
| 159 return false; | 160 return false; |
| 160 } | 161 } |
| 161 *size = columns * rows; | |
| 162 return true; | 162 return true; |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool BrlapiConnectionImpl::WriteDots(const unsigned char* cells) { | 165 bool BrlapiConnectionImpl::WriteDots(const std::vector<unsigned char> cells) { |
| 166 // Cells is a 2D vector, compressed into 1D. | |
| 166 if (!CheckConnected()) | 167 if (!CheckConnected()) |
| 167 return false; | 168 return false; |
| 168 if (libbrlapi_loader_->brlapi__writeDots(handle_.get(), cells) < 0) { | 169 if (libbrlapi_loader_->brlapi__writeDots(handle_.get(), &cells[0]) < 0) { |
|
Devlin
2016/10/25 22:36:54
Do we need to check if cells is empty?
Also, pref
ultimatedbz
2016/10/27 03:16:58
Cool! I did not know vectors had a data() function
Devlin
2016/10/27 14:59:28
Reason I asked about checking emptiness before was
| |
| 169 VLOG(1) << "Couldn't write to brlapi: " << BrlapiStrError(); | 170 VLOG(1) << "Couldn't write to brlapi: " << BrlapiStrError(); |
| 170 return false; | 171 return false; |
| 171 } | 172 } |
| 172 return true; | 173 return true; |
| 173 } | 174 } |
| 174 | 175 |
| 175 int BrlapiConnectionImpl::ReadKey(brlapi_keyCode_t* key_code) { | 176 int BrlapiConnectionImpl::ReadKey(brlapi_keyCode_t* key_code) { |
| 176 if (!CheckConnected()) | 177 if (!CheckConnected()) |
| 177 return -1; | 178 return -1; |
| 178 return libbrlapi_loader_->brlapi__readKey( | 179 return libbrlapi_loader_->brlapi__readKey( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 196 if (error->brlerrno == BRLAPI_ERROR_LIBCERR | 197 if (error->brlerrno == BRLAPI_ERROR_LIBCERR |
| 197 && error->libcerrno == ENOENT) { | 198 && error->libcerrno == ENOENT) { |
| 198 return CONNECT_ERROR_NO_RETRY; | 199 return CONNECT_ERROR_NO_RETRY; |
| 199 } | 200 } |
| 200 return CONNECT_ERROR_RETRY; | 201 return CONNECT_ERROR_RETRY; |
| 201 } | 202 } |
| 202 | 203 |
| 203 } // namespace braille_display_private | 204 } // namespace braille_display_private |
| 204 } // namespace api | 205 } // namespace api |
| 205 } // namespace extensions | 206 } // namespace extensions |
| OLD | NEW |