From 7ff0d40da38ef9ef2aed0781b09eb045da1ec5f6 Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Sun, 14 Apr 2024 10:19:46 -0700 Subject: [PATCH] Add support for streams in the track list and use the logging system for GLib --- CMakeLists.txt | 10 +++++++++- backends/ui/gtk/main.cpp | 39 +++++++++++++++++++++++++++++++++++++++ dbus.cpp | 19 +++++++++++++++++++ dbus.hpp | 1 + 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea02367..5a48d1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,7 +102,15 @@ macro(prefix_all) list(APPEND ${OUT_VAR} ${PREFIX}${ARG}) endforeach() endmacro() -prefix_all(LIBRARY_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ backend.cpp options.cpp playback.cpp util.cpp log.cpp dbus.cpp) +prefix_all(LIBRARY_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/ + backend.cpp + options.cpp + playback.cpp + util.cpp + log.cpp + dbus.cpp +) add_library(liblooper STATIC ${LIBRARY_SOURCES}) target_include_directories(liblooper PUBLIC ${INC}) target_link_libraries(liblooper PUBLIC SDL2::SDL2 ${SDL_MIXER_X_TARGET} PkgConfig::SoundTouch SDBusCpp::sdbus-c++ libvgmstream libvgmstream_shared) diff --git a/backends/ui/gtk/main.cpp b/backends/ui/gtk/main.cpp index acbe44c..d433aca 100644 --- a/backends/ui/gtk/main.cpp +++ b/backends/ui/gtk/main.cpp @@ -2,17 +2,56 @@ #include "main_window.hpp" #include #include +#include std::string GtkBackend::get_id() { return "gtk"; } std::string GtkBackend::get_name() { return "GTK frontend"; } +void g_log_handler(const gchar *domain, GLogLevelFlags flags, const gchar *message, gpointer userdata) { + bool fatal = flags & GLogLevelFlags::G_LOG_FLAG_FATAL; + bool recursion = flags & GLogLevelFlags::G_LOG_FLAG_RECURSION; + GLogLevelFlags level = (GLogLevelFlags)(flags & GLogLevelFlags::G_LOG_LEVEL_MASK); + /* + G_LOG_LEVEL_ERROR = 1 << 2, + G_LOG_LEVEL_CRITICAL = 1 << 3, + G_LOG_LEVEL_WARNING = 1 << 4, + G_LOG_LEVEL_MESSAGE = 1 << 5, + G_LOG_LEVEL_INFO = 1 << 6, + G_LOG_LEVEL_DEBUG = 1 << 7, + */ + LogStream *stream; + switch (level) { + case GLogLevelFlags::G_LOG_LEVEL_ERROR: { + stream = new LogStream({domain}, {ERROR}); + } break; + case GLogLevelFlags::G_LOG_LEVEL_CRITICAL: { + stream = new LogStream({"CRITICAL", domain}, {stderr}); + } break; + case GLogLevelFlags::G_LOG_LEVEL_WARNING: { + stream = new LogStream({domain}, {WARNING}); + } break; + case GLogLevelFlags::G_LOG_LEVEL_INFO: { + stream = new LogStream({domain}, {INFO}); + } break; + case GLogLevelFlags::G_LOG_LEVEL_DEBUG: { + stream = new LogStream({domain}, {DEBUG}); + } break; + default: { + stream = new LogStream({domain}, {stderr}); + } break; + } + stream->writeln(message); + delete stream; +} int GtkBackend::run(std::vector realArgs, int argc, char **argv) { int ret = UIBackend::run(realArgs, argc, argv); if (ret != 0) { return ret; } + g_log_set_default_handler(g_log_handler, nullptr); + g_log_writer Glib::set_application_name("Looper"); auto app = Gtk::Application::create("com.complecwaft.Looper.GTK", Gio::Application::Flags::NON_UNIQUE); char *gtk_frontend_css = (char*)malloc(gtk_frontend_css_size + 1); diff --git a/dbus.cpp b/dbus.cpp index f7fb1fa..167f006 100644 --- a/dbus.cpp +++ b/dbus.cpp @@ -84,6 +84,14 @@ std::vector MprisAPI::GetTracksMetadata(const std::vector &T std::vector output; if (!dbus_api->IsStopped()) { output.push_back(Metadata()); + auto streams = dbus_api->playback->get_streams(); + int i = 0; + for (auto stream : streams) { + std::map meta; + meta["mpris:trackid"] = std::format("{}{}", streamPrefix, i); + meta["xesam:title"] = stream; + i++; + } } return output; } @@ -94,6 +102,11 @@ void MprisAPI::RemoveTrack(const track_id_t &TrackId) { return; } void MprisAPI::GoTo(const track_id_t &TrackId) { + if (TrackId.starts_with(streamPrefix)) { + std::string stream = TrackId.substr(streamPrefix.length()); + int streamIdx = std::stoi(stream); + dbus_api->PlayStream((uint32_t)streamIdx); + } return; } std::vector MprisAPI::Tracks() { @@ -102,6 +115,12 @@ std::vector MprisAPI::Tracks() { return output; } output.push_back(playing_track_id); + auto streams = dbus_api->playback->get_streams(); + int i = 0; + for (auto stream : streams) { + output.push_back(std::format("{}{}", streamPrefix, i)); + i++; + } return output; } bool MprisAPI::CanEditTracks() { diff --git a/dbus.hpp b/dbus.hpp index 1aadd66..9f50f61 100644 --- a/dbus.hpp +++ b/dbus.hpp @@ -13,6 +13,7 @@ class DBusAPI; class MprisAPI : public sdbus::AdaptorInterfaces { friend class DBusAPI; DBusAPI *dbus_api; + const std::string streamPrefix = "/com/complecwaft/looper/Streams/"; std::string curPlayingObjectPath; sdbus::IConnection &connection; const std::string mainInterface = "org.mpris.MediaPlayer2";