miracle-wm-unsnap only wraps commands in a snap context

This commit is contained in:
Matthew Kosarek 2024-04-23 07:30:46 -04:00 committed by Neal Gompa (ニール・ゴンパ)
parent 250f130688
commit 803ea8b167
2 changed files with 52 additions and 6 deletions

View File

@ -26,6 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <libevdev-1.0/libevdev/libevdev.h>
#include <libnotify/notify.h>
#include <sys/inotify.h>
#include <cstdlib>
using namespace miracle;
@ -37,6 +38,14 @@ int program_exists(std::string const& name)
out << "command -v " << name << " > /dev/null 2>&1";
return !system(out.str().c_str());
}
std::string wrap_command(std::string const& command)
{
if (std::getenv("SNAP"))
return "miracle-wm-unsnap " + command;
return command;
}
}
MiracleConfig::MiracleConfig(miral::MirRunner& runner)
@ -82,7 +91,7 @@ void MiracleConfig::_load()
outer_gaps_x = 10;
outer_gaps_y = 10;
startup_apps = {};
terminal = "miracle-wm-unsnap miracle-wm-sensible-terminal";
terminal = wrap_command("miracle-wm-sensible-terminal");
desired_terminal = "";
resize_jump = 50;
@ -525,7 +534,7 @@ void MiracleConfig::_load()
YAML::Node modifiers_node;
try
{
command = "miracle-wm-unsnap " + sub_node["command"].as<std::string>();
command = wrap_command(sub_node["command"].as<std::string>());
action = sub_node["action"].as<std::string>();
key = sub_node["key"].as<std::string>();
modifiers_node = sub_node["modifiers"];
@ -655,7 +664,7 @@ void MiracleConfig::_load()
try
{
auto command = "miracle-wm-unsnap " + node["command"].as<std::string>();
auto command = wrap_command(node["command"].as<std::string>());
bool restart_on_death = false;
if (node["restart_on_death"])
{
@ -677,7 +686,7 @@ void MiracleConfig::_load()
{
try
{
terminal = "miracle-wm-unsnap " + config["terminal"].as<std::string>();
terminal = wrap_command(config["terminal"].as<std::string>());
}
catch (YAML::BadConversion const& e)
{

View File

@ -5,6 +5,7 @@
#include <fstream>
#include <vector>
#include "yaml-cpp/yaml.h"
#include <cstdlib>
using namespace miracle;
@ -165,11 +166,32 @@ TEST_F(MiracleConfigTest, CanCreateCustomAction)
MirKeyboardAction::mir_keyboard_action_down,
KEY_X,
mir_input_event_modifier_meta);
EXPECT_EQ(custom_action->command, "miracle-wm-unsnap echo Hi");
EXPECT_EQ(custom_action->command, "echo Hi");
EXPECT_EQ(custom_action->key, KEY_X);
EXPECT_EQ(custom_action->action, mir_keyboard_action_down);
}
TEST_F(MiracleConfigTest, CustomActionsInSnapIncludeUnsnapCommand)
{
setenv("SNAP", "test", 1);
YAML::Node node;
YAML::Node action_override_node;
action_override_node["command"] = "echo Hi";
action_override_node["action"] = "down";
action_override_node["modifiers"].push_back("primary");
action_override_node["key"] = "KEY_X";
node["custom_actions"].push_back(action_override_node);
write_yaml_node(node);
MiracleConfig config(runner, path);
auto custom_action = config.matches_custom_key_command(
MirKeyboardAction::mir_keyboard_action_down,
KEY_X,
mir_input_event_modifier_meta);
EXPECT_EQ(custom_action->command, "miracle-wm-unsnap echo Hi");
unsetenv("SNAP");
}
TEST_F(MiracleConfigTest, CustomActionWithInvalidCommandIsNotAdded)
{
YAML::Node node;
@ -256,10 +278,25 @@ TEST_F(MiracleConfigTest, ValidStartupAppsAreParsed)
MiracleConfig config(runner, path);
EXPECT_EQ(config.get_startup_apps().size(), 1);
EXPECT_EQ(config.get_startup_apps()[0].command, "miracle-wm-unsnap echo Hi");
EXPECT_EQ(config.get_startup_apps()[0].command, "echo Hi");
EXPECT_EQ(config.get_startup_apps()[0].restart_on_death, true);
}
TEST_F(MiracleConfigTest, StartupAppsInSnapIncludeUnsnapCommand)
{
setenv("SNAP", "test", 1);
YAML::Node node;
YAML::Node startup_app;
startup_app["command"] = "echo Hi";
startup_app["restart_on_death"] = true;
node["startup_apps"].push_back(startup_app);
write_yaml_node(node);
MiracleConfig config(runner, path);
EXPECT_EQ(config.get_startup_apps()[0].command, "miracle-wm-unsnap echo Hi");
unsetenv("SNAP");
}
TEST_F(MiracleConfigTest, StartupAppsThatIsNotAnArrayIsNotParsed)
{
YAML::Node node;