diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 8e634f697e..2ebc196339 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -19,8 +19,6 @@ set(COMMON_SRC "${CMAKE_CURRENT_LIST_DIR}/opengl3.h" "${CMAKE_CURRENT_LIST_DIR}/rs-config.h" "${CMAKE_CURRENT_LIST_DIR}/rs-config.cpp" - "${CMAKE_CURRENT_LIST_DIR}/filesystem.h" - "${CMAKE_CURRENT_LIST_DIR}/filesystem.cpp" "${CMAKE_CURRENT_LIST_DIR}/os.h" "${CMAKE_CURRENT_LIST_DIR}/os.cpp" "${CMAKE_CURRENT_LIST_DIR}/fw-update-helper.h" diff --git a/common/filesystem.cpp b/common/filesystem.cpp deleted file mode 100644 index 83437de9d5..0000000000 --- a/common/filesystem.cpp +++ /dev/null @@ -1,168 +0,0 @@ -// License: Apache 2.0. See LICENSE file in root directory. -// Copyright(c) 2021 Intel Corporation. All Rights Reserved. - -#include "filesystem.h" - -#include -#include - - -#ifdef _WIN32 -#include -#include -#include -#include -#endif - -#if( defined( _WIN32 ) || defined( _WIN64 ) ) -#include "ShellAPI.h" -#endif - -#if defined __linux__ || defined __APPLE__ -#include -#include -#include -#endif - -namespace rs2 { - - -bool directory_exists( const char * dir ) -{ - struct stat info; - - if( stat( dir, &info ) != 0 ) - return false; - else if( info.st_mode & S_IFDIR ) - return true; - else - return false; -} - - -std::string get_file_name( const std::string & path ) -{ - std::string file_name; - for( auto rit = path.rbegin(); rit != path.rend(); ++rit ) - { - if( *rit == '\\' || *rit == '/' ) - break; - file_name += *rit; - } - std::reverse( file_name.begin(), file_name.end() ); - return file_name; -} - - -std::string get_timestamped_file_name() -{ - std::time_t now = std::time( NULL ); - std::tm * ptm = std::localtime( &now ); - char buffer[16]; - // Format: 20170529_205500 - std::strftime( buffer, 16, "%Y%m%d_%H%M%S", ptm ); - return buffer; -} - - -std::string get_folder_path( special_folder f ) -{ - std::string res; -#ifdef _WIN32 - if( f == temp_folder ) - { - TCHAR buf[MAX_PATH]; - if( GetTempPath( MAX_PATH, buf ) != 0 ) - { - char str[1024]; - wcstombs( str, buf, 1023 ); - res = str; - } - } - else - { - GUID folder; - switch( f ) - { - case user_desktop: - folder = FOLDERID_Desktop; - break; - case user_documents: - folder = FOLDERID_Documents; - break; - case user_pictures: - folder = FOLDERID_Pictures; - break; - case user_videos: - folder = FOLDERID_Videos; - break; - case app_data: - folder = FOLDERID_RoamingAppData; - break; - default: - throw std::invalid_argument( std::string( "Value of f (" ) + std::to_string( f ) - + std::string( ") is not supported" ) ); - } - PWSTR folder_path = NULL; - HRESULT hr = SHGetKnownFolderPath( folder, KF_FLAG_DEFAULT_PATH, NULL, &folder_path ); - if( SUCCEEDED( hr ) ) - { - char str[1024]; - wcstombs( str, folder_path, 1023 ); - CoTaskMemFree( folder_path ); - res = str; - res += "\\"; - } - else - { - throw std::runtime_error( "Failed to get requested special folder" ); - } - } -#endif //_WIN32 -#if defined __linux__ || defined __APPLE__ - if( f == special_folder::temp_folder ) - { - const char * tmp_dir = getenv( "TMPDIR" ); - res = tmp_dir ? tmp_dir : "/tmp/"; - } - else - { - const char * home_dir = getenv( "HOME" ); - if( ! home_dir ) - { - struct passwd * pw = getpwuid( getuid() ); - home_dir = ( pw && pw->pw_dir ) ? pw->pw_dir : ""; - } - if( home_dir ) - { - res = home_dir; - switch( f ) - { - case user_desktop: - res += "/Desktop/"; - break; - case user_documents: - res += "/Documents/"; - break; - case user_pictures: - res += "/Pictures/"; - break; - case user_videos: - res += "/Videos/"; - break; - case app_data: - res += "/."; - break; - default: - throw std::invalid_argument( std::string( "Value of f (" ) + std::to_string( f ) - + std::string( ") is not supported" ) ); - } - } - } -#endif // defined __linux__ || defined __APPLE__ - - return res; -} - - -} // namespace rs2 diff --git a/common/filesystem.h b/common/filesystem.h deleted file mode 100644 index 55a5d36933..0000000000 --- a/common/filesystem.h +++ /dev/null @@ -1,26 +0,0 @@ -// License: Apache 2.0. See LICENSE file in root directory. -// Copyright(c) 2021 Intel Corporation. All Rights Reserved. - -#pragma once - -#include - -namespace rs2 { - -bool directory_exists( const char * dir ); -std::string get_file_name( const std::string & path ); - -enum special_folder -{ - user_desktop, - user_documents, - user_pictures, - user_videos, - temp_folder, - app_data -}; - -std::string get_timestamped_file_name(); -std::string get_folder_path( special_folder f ); - -} // namespace rs2 diff --git a/common/os.cpp b/common/os.cpp index 020d3f8bed..5f61921e6a 100644 --- a/common/os.cpp +++ b/common/os.cpp @@ -154,6 +154,18 @@ Some auxillary functionalities might be affected. Please report this message if return (int)(floor(scale)); } + bool directory_exists(const char* dir) + { + struct stat info; + + if (stat(dir, &info ) != 0) + return false; + else if (info.st_mode & S_IFDIR) + return true; + else + return false; + } + const char* file_dialog_open(file_dialog_mode flags, const char* filters, const char* default_path, const char* default_name) { @@ -209,6 +221,117 @@ Some auxillary functionalities might be affected. Please report this message if return stbi_write_png(filename, (int)pixel_width, (int)pixels_height, (int)bytes_per_pixel, raster_data, (int)stride_bytes); } + std::string get_file_name(const std::string& path) + { + std::string file_name; + for (auto rit = path.rbegin(); rit != path.rend(); ++rit) + { + if (*rit == '\\' || *rit == '/') + break; + file_name += *rit; + } + std::reverse(file_name.begin(), file_name.end()); + return file_name; + } + + std::string get_timestamped_file_name() + { + std::time_t now = std::time(NULL); + std::tm * ptm = std::localtime(&now); + char buffer[16]; + // Format: 20170529_205500 + std::strftime(buffer, 16, "%Y%m%d_%H%M%S", ptm); + return buffer; + } + + std::string get_folder_path(special_folder f) + { + std::string res; +#ifdef _WIN32 + if (f == temp_folder) + { + TCHAR buf[MAX_PATH]; + if (GetTempPath(MAX_PATH, buf) != 0) + { + char str[1024]; + wcstombs(str, buf, 1023); + res = str; + } + } + else + { + GUID folder; + switch (f) + { + case user_desktop: folder = FOLDERID_Desktop; + break; + case user_documents: folder = FOLDERID_Documents; + break; + case user_pictures: folder = FOLDERID_Pictures; + break; + case user_videos: folder = FOLDERID_Videos; + break; + case app_data: folder = FOLDERID_RoamingAppData; + break; + default: + throw std::invalid_argument( + std::string("Value of f (") + std::to_string(f) + std::string(") is not supported")); + } + PWSTR folder_path = NULL; + HRESULT hr = SHGetKnownFolderPath(folder, KF_FLAG_DEFAULT_PATH, NULL, &folder_path); + if (SUCCEEDED(hr)) + { + char str[1024]; + wcstombs(str, folder_path, 1023); + CoTaskMemFree(folder_path); + res = str; + res += "\\"; + } + else + { + throw std::runtime_error("Failed to get requested special folder"); + } + } +#endif //_WIN32 +#if defined __linux__ || defined __APPLE__ + if (f == special_folder::temp_folder) + { + const char* tmp_dir = getenv("TMPDIR"); + res = tmp_dir ? tmp_dir : "/tmp/"; + } + else + { + const char* home_dir = getenv("HOME"); + if (!home_dir) + { + struct passwd* pw = getpwuid(getuid()); + home_dir = (pw && pw->pw_dir) ? pw->pw_dir : ""; + } + if (home_dir) + { + res = home_dir; + switch (f) + { + case user_desktop: res += "/Desktop/"; + break; + case user_documents: res += "/Documents/"; + break; + case user_pictures: res += "/Pictures/"; + break; + case user_videos: res += "/Videos/"; + break; + case app_data: res += "/."; + break; + default: + throw std::invalid_argument( + std::string("Value of f (") + std::to_string(f) + std::string(") is not supported")); + } + } + } +#endif // defined __linux__ || defined __APPLE__ + return res; + } + bool ends_with(const std::string& s, const std::string& suffix) { auto i = s.rbegin(), j = suffix.rbegin(); diff --git a/common/os.h b/common/os.h index 5ab409d9de..79007bc895 100644 --- a/common/os.h +++ b/common/os.h @@ -5,7 +5,6 @@ #include #include #include -#include "filesystem.h" struct GLFWmonitor; struct GLFWwindow; @@ -18,7 +17,9 @@ namespace rs2 std::string truncate_string(const std::string& str, size_t width); - void open_url(const char* url); + void open_url(const char* url); + + bool directory_exists(const char* dir); std::vector split_string(std::string& input, char delim); @@ -32,6 +33,8 @@ namespace rs2 // that most of the application is presented on int pick_scale_factor(GLFWwindow* window); + std::string get_file_name(const std::string& path); + // Wrapper for cross-platform dialog control enum file_dialog_mode { open_file = (1 << 0), @@ -44,7 +47,19 @@ namespace rs2 int save_to_png(const char* filename, size_t pixel_width, size_t pixels_height, size_t bytes_per_pixel, const void* raster_data, size_t stride_bytes); - + + enum special_folder + { + user_desktop, + user_documents, + user_pictures, + user_videos, + temp_folder, + app_data + }; + + std::string get_timestamped_file_name(); + std::string get_folder_path(special_folder f); std::string url_encode(const std::string &value); diff --git a/examples/record-playback/CMakeLists.txt b/examples/record-playback/CMakeLists.txt index 320ec04b05..380b89880b 100644 --- a/examples/record-playback/CMakeLists.txt +++ b/examples/record-playback/CMakeLists.txt @@ -5,13 +5,11 @@ cmake_minimum_required(VERSION 3.1.0) project(RealsenseExamplesRecord-Playback) - if(BUILD_GRAPHICAL_EXAMPLES) - add_executable(rs-record-playback rs-record-playback.cpp ../example.hpp ../../third-party/imgui/imgui.cpp ../../third-party/imgui/imgui_draw.cpp ../../third-party/imgui/imgui_impl_glfw.cpp ../../common/filesystem.cpp) + add_executable(rs-record-playback rs-record-playback.cpp ../example.hpp ../../third-party/imgui/imgui.cpp ../../third-party/imgui/imgui_draw.cpp ../../third-party/imgui/imgui_impl_glfw.cpp) set_property(TARGET rs-record-playback PROPERTY CXX_STANDARD 11) target_link_libraries(rs-record-playback ${DEPENDENCIES}) include_directories(../ ../../third-party/tclap/include ../../third-party/imgui) - include_directories(../../common) set_target_properties (rs-record-playback PROPERTIES FOLDER "Examples") install(TARGETS rs-record-playback RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() diff --git a/examples/record-playback/rs-record-playback.cpp b/examples/record-playback/rs-record-playback.cpp index bdb4af9f01..7736fad1ab 100644 --- a/examples/record-playback/rs-record-playback.cpp +++ b/examples/record-playback/rs-record-playback.cpp @@ -12,25 +12,13 @@ #include #include #include -#include + // Helper function for dispaying time conveniently std::string pretty_time(std::chrono::nanoseconds duration); // Helper function for rendering a seek bar void draw_seek_bar(rs2::playback& playback, int* seek_pos, float2& location, float width); - -// Helper function to get bag file in temp folder -std::string get_bag_file_path() -{ - std::string bag = "a.bag"; - std::string tmp_path = rs2::get_folder_path( rs2::special_folder::temp_folder ); - if( tmp_path.empty() ) - return bag; - return tmp_path + bag; -} - - int main(int argc, char * argv[]) try { // Create a simple OpenGL window for rendering: @@ -62,7 +50,6 @@ int main(int argc, char * argv[]) try // Create a variable to control the seek bar int seek_pos; - std::string const bag_file = get_bag_file_path(); // While application is running while(app) { @@ -104,8 +91,8 @@ int main(int argc, char * argv[]) try { pipe->stop(); // Stop the pipeline with the default configuration pipe = std::make_shared(); - rs2::config cfg; // Declare a new configuration - cfg.enable_record_to_file(bag_file); + rs2::config cfg; // Declare a new configuration + cfg.enable_record_to_file("a.bag"); pipe->start(cfg); //File will be opened at this point device = pipe->get_active_profile().get_device(); } @@ -124,8 +111,7 @@ int main(int argc, char * argv[]) try if (recording) { ImGui::SetCursorPos({ app.width() / 2 - 100, 3 * app.height() / 5 + 60 }); - auto info = "Recording to file '" + bag_file + "'"; - ImGui::TextColored({ 255 / 255.f, 64 / 255.f, 54 / 255.f, 1 }, info.c_str()); + ImGui::TextColored({ 255 / 255.f, 64 / 255.f, 54 / 255.f, 1 }, "Recording to file 'a.bag'"); } // Pause the playback if button is clicked @@ -161,7 +147,7 @@ int main(int argc, char * argv[]) try pipe->stop(); // Stop streaming with default configuration pipe = std::make_shared(); rs2::config cfg; - cfg.enable_device_from_file(bag_file); + cfg.enable_device_from_file("a.bag"); pipe->start(cfg); //File will be opened in read mode at this point device = pipe->get_active_profile().get_device(); } @@ -223,6 +209,7 @@ catch (const std::exception& e) return EXIT_FAILURE; } + std::string pretty_time(std::chrono::nanoseconds duration) { using namespace std::chrono;