-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
keyboard controls for pause/resume toggle and play-next:
- supports custom keyboard bindings set through commandline into play_options_ Signed-off-by: Sonia Jin <[email protected]>
- Loading branch information
Showing
9 changed files
with
414 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
rosbag2_transport/test/rosbag2_transport/mock_keyboard_handler.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef ROSBAG2_TRANSPORT__MOCK_KEYBOARD_HANDLER_HPP_ | ||
#define ROSBAG2_TRANSPORT__MOCK_KEYBOARD_HANDLER_HPP_ | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "keyboard_handler/keyboard_handler_base.hpp" | ||
|
||
// press one specific key code one second at a time | ||
class MockKeyboardHandlerImpl : public KeyboardHandlerBase | ||
{ | ||
public: | ||
MockKeyboardHandlerImpl( | ||
KeyboardHandlerBase::KeyCode pause_resume_toggle_key, | ||
KeyboardHandlerBase::KeyCode play_next_key) | ||
: pause_resume_toggle_key_(pause_resume_toggle_key), | ||
play_next_key_(play_next_key) | ||
{ | ||
is_init_succeed_ = true; | ||
key_handler_thread_ = std::thread( | ||
[ = ]() { | ||
try { | ||
int i = 0; | ||
// sleep 0.1 seconda in beginning | ||
std::this_thread::sleep_for(std::chrono::duration<float>(0.1)); | ||
do { | ||
// only press 4 times it in the beginning | ||
// 0 - pause | ||
// 1 - play-next = plays next message | ||
// 2 - resume | ||
// 3 - play-next = calls code but doesn't actually play next message because not paused | ||
if (i < 4) { | ||
KeyboardHandlerBase::KeyCode key_code = pause_resume_toggle_key_; | ||
if ((i == 1) || (i == 3)) { | ||
key_code = play_next_key_; | ||
} | ||
KeyModifiers key_modifiers = KeyboardHandlerBase::KeyModifiers::NONE; | ||
std::lock_guard<std::mutex> lk(callbacks_mutex_); | ||
auto range = callbacks_.equal_range(KeyAndModifiers{key_code, key_modifiers}); | ||
for (auto it = range.first; it != range.second; ++it) { | ||
it->second.callback(key_code, key_modifiers); | ||
} | ||
// sleep 1 second at a time | ||
std::this_thread::sleep_for(std::chrono::duration<float>(0.25)); | ||
} | ||
i++; | ||
} while (!exit_.load()); | ||
} catch (...) { | ||
thread_exception_ptr = std::current_exception(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
/// \brief destructor | ||
KEYBOARD_HANDLER_PUBLIC | ||
virtual ~MockKeyboardHandlerImpl() | ||
{ | ||
exit_ = true; | ||
if (key_handler_thread_.joinable()) { | ||
key_handler_thread_.join(); | ||
} | ||
try { | ||
if (thread_exception_ptr != nullptr) { | ||
std::rethrow_exception(thread_exception_ptr); | ||
} | ||
} catch (const std::exception & e) { | ||
std::cerr << "Caught exception: \"" << e.what() << "\"\n"; | ||
} catch (...) { | ||
std::cerr << "Caught unknown exception" << std::endl; | ||
} | ||
} | ||
|
||
private: | ||
KeyboardHandlerBase::KeyCode pause_resume_toggle_key_; | ||
KeyboardHandlerBase::KeyCode play_next_key_; | ||
std::thread key_handler_thread_; | ||
std::atomic_bool exit_; | ||
std::exception_ptr thread_exception_ptr{nullptr}; | ||
}; | ||
|
||
#endif // ROSBAG2_TRANSPORT__MOCK_KEYBOARD_HANDLER_HPP_ |
Oops, something went wrong.