Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application image #36

Merged
merged 17 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/apps_linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"name":"Steam BigPicture",

"output":"steam.txt",
"detached":["setsid steam steam://open/bigpicture"]
"detached":["setsid steam steam://open/bigpicture"],
"image-path":"./asset/steam.png"
PapyKahan marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
3 changes: 2 additions & 1 deletion assets/apps_windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"name":"Steam BigPicture",

"output":"steam.txt",
"detached":["steam steam://open/bigpicture"]
"detached":["steam steam://open/bigpicture"],
"image_path":"./asset/steam.png"
}
]
}
16 changes: 16 additions & 0 deletions assets/web/apps.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ <h1>Applications</h1>
If not set, Sunshine will default to the parent directory of the command
</div>
</div>
<!-- Image path -->
<div class="mb-3">
<label for="appImagePath" class="form-label">Image</label>
<input
type="text"
class="form-control monospace"
id="appImagePath"
aria-describedby="appImagePathHelp"
v-model="editForm['image-path']"
ReenigneArcher marked this conversation as resolved.
Show resolved Hide resolved
/>
<div id="appImagePathHelp" class="form-text">
Application icon/picture/image path that will be sent to client.
Only full path are working, so no relative path. Image must be a PNG.
If not set, Sunshine will send default box image.
</div>
</div>
<!--buttons-->
<div class="d-flex">
<button @click="showEditForm = false" class="btn btn-secondary m-2">
Expand Down
10 changes: 8 additions & 2 deletions sunshine/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,17 @@ void cancel(resp_https_t response, req_https_t request) {
}
}


void appasset(resp_https_t response, req_https_t request) {
print_req<SimpleWeb::HTTPS>(request);

std::ifstream in(SUNSHINE_ASSETS_DIR "/box.png");
response->write(SimpleWeb::StatusCode::success_ok, in);
auto args = request->parse_query_string();
auto app_image = proc::proc.get_app_image(util::from_view(args.at("appid")));

std::ifstream in(app_image, std::ios::binary);
SimpleWeb::CaseInsensitiveMultimap headers;
headers.emplace("Content-Type", "image/png");
response->write(SimpleWeb::StatusCode::success_ok, in, headers);
response->close_connection_after_response = true;
}

Expand Down
34 changes: 34 additions & 0 deletions sunshine/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

#include <string>
#include <vector>
#include <filesystem>

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>

#include "main.h"
#include "utility.h"
Expand Down Expand Up @@ -189,6 +191,33 @@ std::vector<ctx_t> &proc_t::get_apps() {
return _apps;
}

/// Gets application image from application list.
/// Returns default image if image configuration is not set.
/// returns http content-type header compatible image type
std::string proc_t::get_app_image(int app_id) {
auto app_index = app_id -1;
if(app_index < 0 || app_index >= _apps.size()) {
BOOST_LOG(error) << "Couldn't find app with ID ["sv << app_id << ']';
return SUNSHINE_ASSETS_DIR "/box.png";
}

auto app_image_path = _apps[app_index].image_path;
if (app_image_path.empty()) {
return SUNSHINE_ASSETS_DIR "/box.png";
}

auto image_extension = std::filesystem::path(app_image_path).extension().string();
boost::to_lower(image_extension);

std::error_code code;
if (!std::filesystem::exists(app_image_path, code) || image_extension != "png") {
ReenigneArcher marked this conversation as resolved.
Show resolved Hide resolved
return SUNSHINE_ASSETS_DIR "/box.png";
}

// return only "content-type" http header compatible image type.
return app_image_path;
}

proc_t::~proc_t() {
terminate();
}
Expand Down Expand Up @@ -279,6 +308,7 @@ std::optional<proc::proc_t> parse(const std::string &file_name) {
auto output = app_node.get_optional<std::string>("output"s);
auto name = parse_env_val(this_env, app_node.get<std::string>("name"s));
auto cmd = app_node.get_optional<std::string>("cmd"s);
auto image_path = app_node.get_optional<std::string>("image-path"s);
auto working_dir = app_node.get_optional<std::string>("working-dir"s);

std::vector<proc::cmd_t> prep_cmds;
Expand Down Expand Up @@ -321,6 +351,10 @@ std::optional<proc::proc_t> parse(const std::string &file_name) {
ctx.working_dir = parse_env_val(this_env, *working_dir);
}

if (image_path) {
ctx.image_path = parse_env_val(this_env, *image_path);
}

ctx.name = std::move(name);
ctx.prep_cmds = std::move(prep_cmds);
ctx.detached = std::move(detached);
Expand Down
2 changes: 2 additions & 0 deletions sunshine/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct ctx_t {
std::string cmd;
std::string working_dir;
std::string output;
std::string image_path;
};

class proc_t {
Expand All @@ -78,6 +79,7 @@ class proc_t {

const std::vector<ctx_t> &get_apps() const;
std::vector<ctx_t> &get_apps();
std::string get_app_image(int app_id);

void terminate();

Expand Down