From 4ed36c79dfc8152861ab38ffb62a111e3c853703 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 14 Jun 2014 20:55:18 -0400 Subject: [PATCH] lib: Add support for looking up files from share/ocarina/ I replace the /proc/self/exe method with a simple hard-coded string. This means that binaries run from the source directory need to be run from the root of the source directory. Signed-off-by: Anna Schumaker --- gui/gui.cpp | 5 +++-- gui/main.cpp | 18 ------------------ include/gui/ocarina.h | 1 - lib/lib.cpp | 17 +++++++++++++++++ tests/lib/lib.cpp | 1 + 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/gui/gui.cpp b/gui/gui.cpp index a9c54d99..1993da1b 100644 --- a/gui/gui.cpp +++ b/gui/gui.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -245,7 +246,7 @@ Gtk::Window *setup_gui() struct Callbacks *cb = get_callbacks(); builder = Gtk::Builder::create(); - builder->add_from_file(share_file("ocarina6.glade")); + builder->add_from_file(lib :: share_file("ocarina6.glade")); /* Controls */ @@ -275,7 +276,7 @@ Gtk::Window *setup_gui() window->signal_key_press_event().connect(sigc::ptr_fun(on_window_key_pressed)); window->signal_key_release_event().connect(sigc::ptr_fun(on_window_key_released)); window->set_can_focus(); - window->set_icon_from_file(share_file("ocarina.png")); + window->set_icon_from_file(lib :: share_file("ocarina.png")); /* Favorite and ban buttons */ diff --git a/gui/main.cpp b/gui/main.cpp index cdd94749..6e1a6446 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -6,23 +6,6 @@ #include #include -const std::string share_file(const std::string &file) -{ - char buf[1024]; - std::string res; - ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); - - if (len == -1) - return ""; - - buf[len] = '\0'; - res = std::string(buf); - - // len("bin/ocarina") == 11 - res = res.substr(0, res.size() - 11); - return res + "share/ocarina/" + file; -} - Gtk::Window *ocarina_init(int *argc, char ***argv) { Gtk::Window *window = setup_gui(); @@ -30,7 +13,6 @@ Gtk::Window *ocarina_init(int *argc, char ***argv) lib :: init(argc, argv); playlist :: select("Favorites"); - share_file("ocarina6.glade"); post_init_tabs(); collection_mgr_init2(); return window; diff --git a/include/gui/ocarina.h b/include/gui/ocarina.h index 8458f967..e00f5d3b 100644 --- a/include/gui/ocarina.h +++ b/include/gui/ocarina.h @@ -15,7 +15,6 @@ void collection_mgr_update(); /* main.cpp */ -const std::string share_file(const std::string &); Gtk::Window *ocarina_init(int *, char ***); diff --git a/lib/lib.cpp b/lib/lib.cpp index bad10534..957a1dbb 100644 --- a/lib/lib.cpp +++ b/lib/lib.cpp @@ -5,15 +5,32 @@ #include #include #include +#include #include #include +static std::string share_dir = ""; + +static void setup_share(const std::string &path) +{ + if (path == "/usr/bin/ocarina") + share_dir = "/usr/share/ocarina/"; + else + share_dir = "./share/ocarina/"; +} void lib :: init(int *argc, char ***argv) { + setup_share((*argv)[0]); + tagdb :: init(); library :: init(); playlist :: init(); deck :: init(); audio :: init(argc, argv); } + +const std::string lib :: share_file(const std::string &f) +{ + return share_dir + f; +} diff --git a/tests/lib/lib.cpp b/tests/lib/lib.cpp index c1fa66f1..b5f6651a 100644 --- a/tests/lib/lib.cpp +++ b/tests/lib/lib.cpp @@ -13,6 +13,7 @@ static void test_init(int argc, char **argv) lib :: init(&argc, &argv); test_equal(deck :: get_queues().size(), (size_t)2); + test_equal(lib :: share_file("saria"), (std::string)"./share/ocarina/saria"); } int main(int argc, char **argv)