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 oldCols, | |
|
dmazzoni
2016/10/17 05:13:05
"old" is a little confusing to me, I know what you
ultimatedbz
2016/10/17 21:49:46
Done.
| |
| 105 unsigned int oldRows) { | |
| 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 the old display size, pad with 0. |
| 112 if (!connection_->WriteDots(&sizedCells[0])) | 117 sized_cells[i * columns + j] = |
| 118 (i >= oldRows || j >= oldCols) ? 0 : cells[i * oldCols + j]; | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 if (!connection_->WriteDots(sized_cells)) | |
| 113 Disconnect(); | 123 Disconnect(); |
| 114 } | 124 } |
| 115 } | 125 } |
| 116 | 126 |
| 117 void BrailleControllerImpl::AddObserver(BrailleObserver* observer) { | 127 void BrailleControllerImpl::AddObserver(BrailleObserver* observer) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 119 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 129 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 120 base::Bind( | 130 base::Bind( |
| 121 &BrailleControllerImpl::StartConnecting, | 131 &BrailleControllerImpl::StartConnecting, |
| 122 base::Unretained(this)))) { | 132 base::Unretained(this)))) { |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 } | 328 } |
| 319 return; | 329 return; |
| 320 } | 330 } |
| 321 FOR_EACH_OBSERVER(BrailleObserver, observers_, | 331 FOR_EACH_OBSERVER(BrailleObserver, observers_, |
| 322 OnBrailleDisplayStateChanged(*new_state)); | 332 OnBrailleDisplayStateChanged(*new_state)); |
| 323 } | 333 } |
| 324 | 334 |
| 325 } // namespace braille_display_private | 335 } // namespace braille_display_private |
| 326 } // namespace api | 336 } // namespace api |
| 327 } // namespace extensions | 337 } // namespace extensions |
| OLD | NEW |