Skip to content

Commit

Permalink
activators: add extension type bindings (#66)
Browse files Browse the repository at this point in the history
Those are not parsed by wf-config, instead, Wayfire plugins determine
their format.
  • Loading branch information
ammen99 authored Apr 1, 2024
1 parent aba330e commit 1802658
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/wayfire/config/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ struct activatorbinding_t
*/
const std::vector<wf::hotspot_binding_t>& get_hotspots() const;

/**
* @return A list of all unknown bindings which activate this binding.
*/
const std::vector<std::string>& get_extensions() const;

/**
* Check equality of two activator bindings.
*
Expand Down
43 changes: 42 additions & 1 deletion src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ struct wf::activatorbinding_t::impl
std::vector<buttonbinding_t> buttons;
std::vector<touchgesture_t> gestures;
std::vector<hotspot_binding_t> hotspots;
std::vector<std::string> extensions;
};

wf::activatorbinding_t::activatorbinding_t()
Expand Down Expand Up @@ -792,6 +793,21 @@ bool try_add_binding(
return false;
}

static std::string filter_trailing_leading(std::string token)
{
while (!token.empty() && token.back() == ' ')
{
token.pop_back();
}

while (!token.empty() && token.front() == ' ')
{
token.erase(token.begin());
}

return token;
}

template<>
std::optional<wf::activatorbinding_t> wf::option_type::from_string(
const std::string& string)
Expand All @@ -806,6 +822,13 @@ std::optional<wf::activatorbinding_t> wf::option_type::from_string(
auto tokens = split_at(string, "|", true);
for (auto& token : tokens)
{
const bool only_whitespace = std::all_of(token.begin(), token.end(),
[] (char c) { return std::isspace(c); });
if (only_whitespace)
{
return {};
}

bool is_valid_binding =
try_add_binding(binding.priv->keys, token) ||
try_add_binding(binding.priv->buttons, token) ||
Expand All @@ -814,7 +837,7 @@ std::optional<wf::activatorbinding_t> wf::option_type::from_string(

if (!is_valid_binding)
{
return {};
binding.priv->extensions.push_back(filter_trailing_leading(token));
}
}

Expand Down Expand Up @@ -877,12 +900,30 @@ bool wf::activatorbinding_t::has_match(const touchgesture_t& gesture) const

bool wf::activatorbinding_t::operator ==(const activatorbinding_t& other) const
{
if (other.get_extensions().size() != this->get_extensions().size())
{
return false;
}

for (size_t i = 0; i < other.get_extensions().size(); i++)
{
if (other.get_extensions()[i] != this->get_extensions()[i])
{
return false;
}
}

return priv->keys == other.priv->keys &&
priv->buttons == other.priv->buttons &&
priv->gestures == other.priv->gestures &&
priv->hotspots == other.priv->hotspots;
}

const std::vector<std::string>& wf::activatorbinding_t::get_extensions() const
{
return priv->extensions;
}

const std::vector<wf::hotspot_binding_t>& wf::activatorbinding_t::get_hotspots()
const
{
Expand Down
7 changes: 6 additions & 1 deletion test/types_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,13 @@ TEST_CASE("wf::activatorbinding_t")
0, 1);

CHECK(!from_string<activatorbinding_t>("<alt> KEY_K || <alt> KEY_U"));
CHECK(!from_string<activatorbinding_t>("<alt> KEY_K | thrash"));
CHECK(!from_string<activatorbinding_t>("<alt> KEY_K |"));

auto with_ext = from_string<activatorbinding_t>("<alt> KEY_T | thrash");
CHECK(with_ext.has_value() == true);
CHECK(with_ext->get_extensions().size() == 1);
CHECK(with_ext->get_extensions().front() == "thrash");
CHECK(with_ext->has_match(kb1));
}

TEST_CASE("wf::output_config::mode_t")
Expand Down

0 comments on commit 1802658

Please sign in to comment.