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/braille_controll er_brlapi.h" | 5 #include "chrome/browser/extensions/api/braille_display_private/braille_controll er_brlapi.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cerrno> | 10 #include <cerrno> |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 LOG(WARNING) << "Couldn't load libbrlapi: " << strerror(errno); | 82 LOG(WARNING) << "Couldn't load libbrlapi: " << strerror(errno); |
| 83 } | 83 } |
| 84 | 84 |
| 85 std::unique_ptr<DisplayState> BrailleControllerImpl::GetDisplayState() { | 85 std::unique_ptr<DisplayState> BrailleControllerImpl::GetDisplayState() { |
| 86 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 86 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 87 StartConnecting(); | 87 StartConnecting(); |
| 88 std::unique_ptr<DisplayState> display_state(new DisplayState); | 88 std::unique_ptr<DisplayState> display_state(new DisplayState); |
| 89 if (connection_.get() && connection_->Connected()) { | 89 if (connection_.get() && connection_->Connected()) { |
| 90 size_t size; | 90 unsigned int columns, rows; |
| 91 if (!connection_->GetDisplaySize(&size)) { | 91 if (!connection_->GetDisplaySize(&columns, &rows)) { |
| 92 Disconnect(); | 92 Disconnect(); |
| 93 } else if (size > 0) { // size == 0 means no display present. | 93 } else if (rows * columns > 0) { |
| 94 // rows * columns == 0 means no display present. | |
| 94 display_state->available = true; | 95 display_state->available = true; |
| 95 display_state->text_cell_count.reset(new int(size)); | 96 display_state->text_column_count.reset(new int(columns)); |
| 97 display_state->text_row_count.reset(new int(rows)); | |
| 96 } | 98 } |
| 97 } | 99 } |
| 98 return display_state; | 100 return display_state; |
| 99 } | 101 } |
| 100 | 102 |
| 101 void BrailleControllerImpl::WriteDots(const std::vector<char>& cells) { | 103 void BrailleControllerImpl::WriteDots(const std::vector<char>& cells, |
| 104 unsigned int cells_cols, | |
| 105 unsigned int cells_rows) { | |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 103 if (connection_ && connection_->Connected()) { | 107 if (connection_ && connection_->Connected()) { |
| 104 size_t size; | 108 // Row count and column count of current display. |
| 105 if (!connection_->GetDisplaySize(&size)) { | 109 unsigned int rows, columns; |
| 110 if (!connection_->GetDisplaySize(&columns, &rows)) { | |
| 106 Disconnect(); | 111 Disconnect(); |
| 107 } | 112 } |
| 108 std::vector<unsigned char> sizedCells(size); | 113 std::vector<unsigned char> sized_cells(rows * columns); |
| 109 std::memcpy(&sizedCells[0], cells.data(), std::min(cells.size(), size)); | 114 for (unsigned int i = 0; i < rows; i++) { |
| 110 if (size > cells.size()) | 115 for (unsigned int j = 0; j < columns; j++) { |
| 111 std::fill(sizedCells.begin() + cells.size(), sizedCells.end(), 0); | 116 // If we go out of the bounds of cell's size, pad with 0. |
| 112 if (!connection_->WriteDots(&sizedCells[0])) | 117 sized_cells[i * columns + j] = (i >= cells_rows || j >= cells_cols) |
| 118 ? 0 | |
| 119 : cells[i * cells_cols + j]; | |
|
Devlin
2016/10/18 23:01:28
nit: why not init the vector to all zeros (std::ve
ultimatedbz
2016/10/20 18:27:45
That's a great suggestion! As I was thinking about
Devlin
2016/10/21 15:41:39
Ah, didn't realize that cells_rows could be bigger
ultimatedbz
2016/10/25 19:24:41
Looks good. Thanks!
| |
| 120 } | |
| 121 } | |
| 122 | |
| 123 if (!connection_->WriteDots(sized_cells)) | |
| 113 Disconnect(); | 124 Disconnect(); |
| 114 } | 125 } |
| 115 } | 126 } |
| 116 | 127 |
| 117 void BrailleControllerImpl::AddObserver(BrailleObserver* observer) { | 128 void BrailleControllerImpl::AddObserver(BrailleObserver* observer) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 119 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 130 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 120 base::Bind( | 131 base::Bind( |
| 121 &BrailleControllerImpl::StartConnecting, | 132 &BrailleControllerImpl::StartConnecting, |
| 122 base::Unretained(this)))) { | 133 base::Unretained(this)))) { |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 } | 329 } |
| 319 return; | 330 return; |
| 320 } | 331 } |
| 321 FOR_EACH_OBSERVER(BrailleObserver, observers_, | 332 FOR_EACH_OBSERVER(BrailleObserver, observers_, |
| 322 OnBrailleDisplayStateChanged(*new_state)); | 333 OnBrailleDisplayStateChanged(*new_state)); |
| 323 } | 334 } |
| 324 | 335 |
| 325 } // namespace braille_display_private | 336 } // namespace braille_display_private |
| 326 } // namespace api | 337 } // namespace api |
| 327 } // namespace extensions | 338 } // namespace extensions |
| OLD | NEW |