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 mode option to mirror #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/wayfire/config/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ struct mode_t
*/
mode_t(const std::string& mirror_from);

/**
* Initialize a mirror mode with a custom mode on the output
*/
mode_t(const std::string& mirror_from, int32_t width, int32_t height, int32_t refresh);

/** @return The type of this mode. */
mode_type_t get_type() const;

Expand Down
37 changes: 25 additions & 12 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,18 @@ wf::output_config::mode_t::mode_t(const std::string& mirror_from)
this->mirror_from = mirror_from;
}

/**
* Initialize a mirror mode with a display mode.
*/
wf::output_config::mode_t::mode_t(const std::string& mirror_from, int32_t width, int32_t height, int32_t refresh)
{
this->type = MODE_MIRROR;
this->mirror_from = mirror_from;
this->width = width;
this->height = height;
this->refresh = refresh;
}

/** @return The type of this mode. */
wf::output_config::mode_type_t wf::output_config::mode_t::get_type() const
{
Expand Down Expand Up @@ -1108,29 +1120,26 @@ stdx::optional<wf::output_config::mode_t> wf::option_type::from_string(
return wf::output_config::mode_t{true};
}

std::string resolution,from;

if (string.substr(0, 6) == "mirror")
{
std::stringstream ss(string);
std::string from, dummy;
ss >> from; // the mirror word
if (!(ss >> from))
if (ss >> from)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused, is the code not supposed to continue running after that, so that we can parse the mode as well?

Also, I think it may be simpler (and more flexible wrt. whitespace handling) to try sscanf multiple times. One time with a format string like mirror %s <optionally the mode>, and if that fails, then scan a normal resolution, like the code below does.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

59-1.txt
I've modified the logic a bit so the program execution is not prematurely finished before the mode string parsed. So now it works - but the code is a bit ugly. I am not C++ expert ufortunately. But for personal use that works.

{
return {};
return wf::output_config::mode_t{from};
}

// trailing garbage
if (ss >> dummy)
{
return {};
}

return wf::output_config::mode_t{from};
ss >> resolution;
}

resolution = string;

int w, h, rr = 0;
char next;

int read = std::sscanf(string.c_str(), "%d x %d @ %d%c", &w, &h, &rr, &next);
int read = std::sscanf(resolution.c_str(), "%d x %d @ %d%c", &w, &h, &rr, &next);
if ((read < 2) || (read > 3))
{
return {};
Expand All @@ -1147,7 +1156,11 @@ stdx::optional<wf::output_config::mode_t> wf::option_type::from_string(
rr *= 1000;
}

return wf::output_config::mode_t{w, h, rr};
if (from.empty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting: { on a new line.

return wf::output_config::mode_t{w, h, rr};
}

return wf::output_config::mode_t{from, w, h, rr};
}

/** Represent the activator binding as a string. */
Expand Down
1 change: 1 addition & 0 deletions test/types_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ TEST_CASE("wf::output_config::mode_t")
"1920x1080@59",
"1920x 1080 @ 59000",
"mirror eDP-1",
"mirror DP-1 1920x1080@60000",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to also add some checks in the test that this is properly parsed.

};

std::vector<std::string> invalid = {
Expand Down