From 40bc9d1095228ab1377625be3479491263d696d7 Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Tue, 24 Sep 2019 12:52:14 +0300 Subject: videomode to_string improvements Add to_string_short() and to_string_long() to videomode (using the fmt library) for easy printing of videomodes. Signed-off-by: Tomi Valkeinen --- kms++/CMakeLists.txt | 2 +- kms++/inc/kms++/videomode.h | 4 +++- kms++/src/videomode.cpp | 54 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 9 deletions(-) (limited to 'kms++') diff --git a/kms++/CMakeLists.txt b/kms++/CMakeLists.txt index 10352a2..1ca8815 100644 --- a/kms++/CMakeLists.txt +++ b/kms++/CMakeLists.txt @@ -22,7 +22,7 @@ target_include_directories(kms++ PUBLIC $ PRIVATE src) -target_link_libraries(kms++ ${LIBDRM_LIBRARIES} ${LIBDRM_OMAP_LIBRARIES}) +target_link_libraries(kms++ ${LIBDRM_LIBRARIES} ${LIBDRM_OMAP_LIBRARIES} fmt::fmt) set_target_properties(kms++ PROPERTIES PUBLIC_HEADER "${PUB_HDRS}") diff --git a/kms++/inc/kms++/videomode.h b/kms++/inc/kms++/videomode.h index d7f5258..74aa8f1 100644 --- a/kms++/inc/kms++/videomode.h +++ b/kms++/inc/kms++/videomode.h @@ -49,7 +49,9 @@ struct Videomode void set_hsync(SyncPolarity pol); void set_vsync(SyncPolarity pol); - std::string to_string() const; + std::string to_string_short() const; + std::string to_string_long() const; + std::string to_string_long_padded() const; }; struct Videomode videomode_from_timings(uint32_t clock_khz, diff --git a/kms++/src/videomode.cpp b/kms++/src/videomode.cpp index b8bd797..7099898 100644 --- a/kms++/src/videomode.cpp +++ b/kms++/src/videomode.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "helpers.h" @@ -88,14 +89,53 @@ void Videomode::set_vsync(SyncPolarity pol) } } -string Videomode::to_string() const +string Videomode::to_string_short() const { - std::stringstream ss; - ss << hdisplay << "x" << vdisplay; - if (interlace()) - ss << "i"; - ss << "@" << calculated_vrefresh(); - return ss.str(); + return fmt::format("{}x{}{}@{:.2f}", hdisplay, vdisplay, interlace() ? "i" : "", calculated_vrefresh()); +} + +static char sync_to_char(SyncPolarity pol) +{ + switch (pol) { + case SyncPolarity::Positive: + return '+'; + case SyncPolarity::Negative: + return '-'; + default: + return '?'; + } +} + +string Videomode::to_string_long() const +{ + string h = fmt::format("{}/{}/{}/{}/{}", hdisplay, hfp(), hsw(), hbp(), sync_to_char(hsync())); + string v = fmt::format("{}/{}/{}/{}/{}", vdisplay, vfp(), vsw(), vbp(), sync_to_char(vsync())); + + string str = fmt::format("{} {:.3f} {} {} {} ({:.2f}) {:#x} {:#x}", + to_string_short(), + clock / 1000.0, + h, v, + vrefresh, calculated_vrefresh(), + flags, + type); + + return str; +} + +string Videomode::to_string_long_padded() const +{ + string h = fmt::format("{}/{}/{}/{}/{}", hdisplay, hfp(), hsw(), hbp(), sync_to_char(hsync())); + string v = fmt::format("{}/{}/{}/{}/{}", vdisplay, vfp(), vsw(), vbp(), sync_to_char(vsync())); + + string str = fmt::format("{:<16} {:7.3f} {:<18} {:<18} {:2} ({:.2f}) {:#10x} {:#6x}", + to_string_short(), + clock / 1000.0, + h, v, + vrefresh, calculated_vrefresh(), + flags, + type); + + return str; } Videomode videomode_from_timings(uint32_t clock_khz, -- cgit v1.2.3