2020-04-09 10:07:19 -04:00
|
|
|
/*
|
2021-06-13 10:32:18 -04:00
|
|
|
* Copyright (C) 2020 Luciano Iam <oss@lucianoiam.com>
|
2020-04-09 10:07:19 -04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the/GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <glibmm/fileutils.h>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
|
|
|
|
#include "ardour/filesystem_paths.h"
|
|
|
|
#include "pbd/file_utils.h"
|
|
|
|
|
|
|
|
#include "resources.h"
|
2020-04-22 12:17:13 -04:00
|
|
|
#include "json.h"
|
2020-04-09 10:07:19 -04:00
|
|
|
|
2020-08-30 15:15:07 -04:00
|
|
|
using namespace ArdourSurface;
|
|
|
|
|
2020-04-09 10:07:19 -04:00
|
|
|
static const char* const data_dir_env_var = "ARDOUR_WEBSURFACES_PATH";
|
|
|
|
static const char* const data_dir_name = "web_surfaces";
|
|
|
|
static const char* const builtin_dir_name = "builtin";
|
2020-04-11 03:31:06 -04:00
|
|
|
static const char* const user_dir_name = "user";
|
2020-04-09 10:07:19 -04:00
|
|
|
|
|
|
|
static bool
|
|
|
|
dir_filter (const std::string &str, void* /*arg*/)
|
|
|
|
{
|
|
|
|
return Glib::file_test (str, Glib::FILE_TEST_IS_DIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerResources::ServerResources ()
|
|
|
|
: _index_dir ("")
|
|
|
|
, _builtin_dir ("")
|
|
|
|
, _user_dir ("")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string&
|
|
|
|
ServerResources::index_dir ()
|
|
|
|
{
|
|
|
|
if (_index_dir.empty ()) {
|
|
|
|
_index_dir = server_data_dir ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _index_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string&
|
|
|
|
ServerResources::builtin_dir ()
|
|
|
|
{
|
|
|
|
if (_builtin_dir.empty ()) {
|
|
|
|
_builtin_dir = Glib::build_filename (server_data_dir (), builtin_dir_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _builtin_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string&
|
|
|
|
ServerResources::user_dir ()
|
|
|
|
{
|
|
|
|
if (_user_dir.empty ()) {
|
|
|
|
_user_dir = Glib::build_filename (ARDOUR::user_config_directory (), data_dir_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _user_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ServerResources::scan ()
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
2020-04-12 04:12:58 -04:00
|
|
|
std::string builtin_dir_str = PBD::canonical_path (builtin_dir ());
|
2020-04-11 03:58:42 -04:00
|
|
|
SurfaceManifestVector builtin = read_manifests (builtin_dir_str);
|
2020-04-09 10:07:19 -04:00
|
|
|
|
2020-04-11 03:58:42 -04:00
|
|
|
ss << "[{"
|
2020-08-30 15:15:07 -04:00
|
|
|
<< "\"filesystemPath\":\"" << WebSocketsJSON::escape (builtin_dir_str) << "\""
|
|
|
|
<< ",\"path\":\"" << WebSocketsJSON::escape (builtin_dir_name) << "\""
|
2020-04-11 03:58:42 -04:00
|
|
|
<< ",\"surfaces\":"
|
|
|
|
<< "[";
|
2020-04-09 10:07:19 -04:00
|
|
|
|
|
|
|
for (SurfaceManifestVector::iterator it = builtin.begin (); it != builtin.end (); ) {
|
|
|
|
ss << it->to_json ();
|
|
|
|
if (++it != builtin.end()) {
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 04:12:58 -04:00
|
|
|
std::string user_dir_str = PBD::canonical_path (user_dir ());
|
2020-04-11 03:58:42 -04:00
|
|
|
SurfaceManifestVector user = read_manifests (user_dir_str);
|
2020-04-09 10:07:19 -04:00
|
|
|
|
2020-04-11 03:58:42 -04:00
|
|
|
ss << "]},{"
|
2020-08-30 15:15:07 -04:00
|
|
|
<< "\"filesystemPath\":\"" << WebSocketsJSON::escape (user_dir_str) << "\""
|
|
|
|
<< ",\"path\":\"" << WebSocketsJSON::escape (user_dir_name) << "\""
|
2020-04-11 03:58:42 -04:00
|
|
|
<< ",\"surfaces\":"
|
|
|
|
<< "[";
|
2020-04-09 10:07:19 -04:00
|
|
|
|
|
|
|
for (SurfaceManifestVector::iterator it = user.begin (); it != user.end (); ) {
|
|
|
|
ss << it->to_json ();
|
|
|
|
if (++it != user.end()) {
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 03:31:06 -04:00
|
|
|
ss << "]}]";
|
2020-04-09 10:07:19 -04:00
|
|
|
|
|
|
|
return ss.str ();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ServerResources::server_data_dir ()
|
|
|
|
{
|
|
|
|
std::string data_dir;
|
|
|
|
|
|
|
|
bool defined = false;
|
|
|
|
std::string env_dir (Glib::getenv (data_dir_env_var, defined));
|
|
|
|
|
2020-04-12 04:03:02 -04:00
|
|
|
if (defined && !env_dir.empty ()) {
|
2020-04-09 15:50:01 -04:00
|
|
|
/* useful for development */
|
2020-04-09 10:07:19 -04:00
|
|
|
data_dir = env_dir;
|
|
|
|
} else {
|
2020-04-09 15:50:01 -04:00
|
|
|
/* use reverse iterator, since ardour_data_search_path() prefixes the user-data dir */
|
|
|
|
PBD::Searchpath s (ARDOUR::ardour_data_search_path ());
|
|
|
|
for (PBD::Searchpath::reverse_iterator i = s.rbegin (); i != s.rend(); ++i) {
|
|
|
|
data_dir = Glib::build_filename (*i, data_dir_name);
|
|
|
|
if (Glib::file_test(data_dir, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 10:07:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return data_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceManifestVector
|
|
|
|
ServerResources::read_manifests (std::string dir)
|
|
|
|
{
|
|
|
|
SurfaceManifestVector result;
|
|
|
|
std::vector<std::string> subdirs;
|
|
|
|
PBD::Searchpath spath (dir);
|
|
|
|
|
|
|
|
find_paths_matching_filter (subdirs, spath, dir_filter,
|
|
|
|
0 /*arg*/, true /*pass_fullpath*/, true /*return_fullpath*/, false /*recurse*/);
|
|
|
|
|
2020-04-09 16:39:25 -04:00
|
|
|
for (std::vector<std::string>::const_iterator it = subdirs.begin (); it != subdirs.end (); ++it) {
|
2020-04-11 03:31:06 -04:00
|
|
|
if (!SurfaceManifest::exists_at_path (*it)) {
|
2020-04-09 10:07:19 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-11 03:31:06 -04:00
|
|
|
SurfaceManifest manifest (*it);
|
2020-04-09 10:07:19 -04:00
|
|
|
|
|
|
|
if (manifest.valid ()) {
|
|
|
|
result.push_back (manifest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|