feature: providing keybindings via a configuration; next step is loading it

This commit is contained in:
Matthew Kosarek 2024-01-28 10:52:28 -05:00
parent e17c773617
commit da921123b6
6 changed files with 242 additions and 60 deletions

View File

@ -24,6 +24,8 @@ add_executable(miracle-wm
src/node.cpp
src/window_helpers.h
src/window_helpers.cpp
src/miracle_config.cpp
src/miracle_config.h
)
target_include_directories(miracle-wm PUBLIC SYSTEM ${MIRAL_INCLUDE_DIRS})

View File

@ -5,7 +5,6 @@
#include <miral/window_management_options.h>
#include <miral/internal_client.h>
#include <miral/keymap.h>
#include <miral/toolkit_event.h>
#include <miral/x11_support.h>
#include <miral/wayland_extensions.h>
#include <miral/display_configuration_option.h>

37
src/miracle_config.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "miracle_config.h"
using namespace miracle;
MiracleConfig::MiracleConfig()
{
}
MirInputEventModifier MiracleConfig::get_input_event_modifier() const
{
return modifier;
}
DefaultKeyCommand MiracleConfig::matches_key_command(MirKeyboardAction action, int scan_code, unsigned int modifiers) const
{
for (int i = 0; i < DefaultKeyCommand::MAX; i++)
{
for (auto command : key_commands[i])
{
if (action != command.action)
continue;
auto command_modifiers = command.modifiers;
if (command_modifiers & miracle_input_event_modifier_default)
command_modifiers = command_modifiers & ~miracle_input_event_modifier_default | get_input_event_modifier();
if (command_modifiers != modifiers)
continue;
if (scan_code == command.key)
return (DefaultKeyCommand)i;
}
}
return DefaultKeyCommand::MAX;
}

155
src/miracle_config.h Normal file
View File

@ -0,0 +1,155 @@
#ifndef MIRACLEWM_MIRACLE_CONFIG_H
#define MIRACLEWM_MIRACLE_CONFIG_H
#include <string>
#include <miral/toolkit_event.h>
#include <vector>
#include <linux/input.h>
namespace miracle
{
enum DefaultKeyCommand
{
Terminal = 0,
RequestVertical,
RequestHorizontal,
ToggleResize,
MoveUp,
MoveDown,
MoveLeft,
MoveRight,
SelectUp,
SelectDown,
SelectLeft,
SelectRight,
QuitActiveWindow,
QuitCompositor,
MAX
};
struct KeyCommand
{
MirKeyboardAction action;
uint modifiers;
int key;
};
typedef std::vector<KeyCommand> KeyCommandList;
class MiracleConfig
{
public:
MiracleConfig();
[[nodiscard]] MirInputEventModifier get_input_event_modifier() const;
[[nodiscard]] DefaultKeyCommand matches_key_command(MirKeyboardAction action, int scan_code, unsigned int modifiers) const;
private:
static const int miracle_input_event_modifier_default = 1 << 18;
MirInputEventModifier modifier = mir_input_event_modifier_meta;
KeyCommandList key_commands[DefaultKeyCommand::MAX] = {
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_ENTER
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_V
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_H
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_R
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default | mir_input_event_modifier_shift,
KEY_UP
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default | mir_input_event_modifier_shift,
KEY_DOWN
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default | mir_input_event_modifier_shift,
KEY_LEFT
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default | mir_input_event_modifier_shift,
KEY_RIGHT
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_UP
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_DOWN
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_LEFT
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default,
KEY_RIGHT
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default | mir_input_event_modifier_shift,
KEY_Q
}
},
{
{
MirKeyboardAction::mir_keyboard_action_down,
miracle_input_event_modifier_default | mir_input_event_modifier_shift,
KEY_E
}
},
};
};
}
#endif

View File

@ -2,6 +2,7 @@
#include "miracle_window_management_policy.h"
#include "window_helpers.h"
#include "miracle_config.h"
#include <mir_toolkit/events/enums.h>
#include <miral/toolkit_event.h>
@ -75,7 +76,8 @@ MiracleWindowManagementPolicy::MiracleWindowManagementPolicy(
miral::ExternalClientLauncher const& external_client_launcher,
miral::InternalClientLauncher const& internal_client_launcher,
miral::MirRunner& runner)
: window_manager_tools{tools},
: config{MiracleConfig()},
window_manager_tools{tools},
external_client_launcher{external_client_launcher},
internal_client_launcher{internal_client_launcher},
runner{runner}
@ -88,88 +90,74 @@ bool MiracleWindowManagementPolicy::handle_keyboard_event(MirKeyboardEvent const
auto const scan_code = miral::toolkit::mir_keyboard_event_scan_code(event);
auto const modifiers = miral::toolkit::mir_keyboard_event_modifiers(event) & MODIFIER_MASK;
if (action == MirKeyboardAction::mir_keyboard_action_down && (modifiers & mir_input_event_modifier_meta))
auto key_command = config.matches_key_command(action, scan_code, modifiers);
if (key_command == DefaultKeyCommand::MAX)
return false;
switch (key_command)
{
if (scan_code == KEY_ENTER)
{
case Terminal:
for (auto const& terminal : POSSIBLE_TERMINALS)
{
if (external_client_launcher.launch({terminal}) > 0)
break;
}
return true;
}
else if (scan_code == KEY_V)
{
case RequestVertical:
active_tree->tree.request_vertical();
return true;
}
else if (scan_code == KEY_H)
{
case RequestHorizontal:
active_tree->tree.request_horizontal();
return true;
}
else if (scan_code == KEY_R)
{
case ToggleResize:
active_tree->tree.toggle_resize_mode();
return true;
}
else if (scan_code == KEY_UP)
{
if (modifiers & mir_input_event_modifier_shift)
{
if (active_tree->tree.try_move_active_window(Direction::up))
return true;
}
else if (active_tree->tree.try_resize_active_window(Direction::up)
case MoveUp:
if (active_tree->tree.try_move_active_window(Direction::up))
return true;
return false;
case MoveDown:
if (active_tree->tree.try_move_active_window(Direction::down))
return true;
return false;
case MoveLeft:
if (active_tree->tree.try_move_active_window(Direction::left))
return true;
return false;
case MoveRight:
if (active_tree->tree.try_move_active_window(Direction::right))
return true;
return false;
case SelectUp:
if (active_tree->tree.try_resize_active_window(Direction::up)
|| active_tree->tree.try_select_next(Direction::up))
return true;
}
else if (scan_code == KEY_DOWN)
{
if (modifiers & mir_input_event_modifier_shift)
{
if (active_tree->tree.try_move_active_window(Direction::down))
return true;
}
else if (active_tree->tree.try_resize_active_window(Direction::down)
return false;
case SelectDown:
if (active_tree->tree.try_resize_active_window(Direction::down)
|| active_tree->tree.try_select_next(Direction::down))
return true;
}
else if (scan_code == KEY_LEFT)
{
if (modifiers & mir_input_event_modifier_shift)
{
if (active_tree->tree.try_move_active_window(Direction::left))
return true;
}
else if (active_tree->tree.try_resize_active_window(Direction::left)
return false;
case SelectLeft:
if (active_tree->tree.try_resize_active_window(Direction::left)
|| active_tree->tree.try_select_next(Direction::left))
return true;
}
else if (scan_code == KEY_RIGHT)
{
if (modifiers & mir_input_event_modifier_shift)
{
if (active_tree->tree.try_move_active_window(Direction::right))
return true;
}
else if (active_tree->tree.try_resize_active_window(Direction::right)
return false;
case SelectRight:
if (active_tree->tree.try_resize_active_window(Direction::right)
|| active_tree->tree.try_select_next(Direction::right))
return true;
}
else if(scan_code == KEY_Q && modifiers & mir_input_event_modifier_shift)
{
return false;
case QuitActiveWindow:
active_tree->tree.close_active_window();
return true;
}
else if (scan_code == KEY_E && modifiers & mir_input_event_modifier_shift)
{
case QuitCompositor:
runner.stop();
return true;
}
default:
std::cerr << "Unknown key_command: " << key_command << std::endl;
break;
}
return false;
}

View File

@ -2,6 +2,8 @@
#define MIRIE_WINDOW_MANAGEMENT_POLICY_H
#include "window_tree.h"
#include "miracle_config.h"
#include <miral/window_manager_tools.h>
#include <miral/window_management_policy.h>
#include <miral/external_client.h>
@ -17,8 +19,6 @@ class MirRunner;
namespace miracle
{
class DisplayListener;
class TaskBar;
struct OutputTreePair
{
@ -79,6 +79,7 @@ public:
void advise_application_zone_delete(miral::Zone const& application_zone) override;
private:
MiracleConfig config;
std::shared_ptr<OutputTreePair> active_tree;
std::vector<std::shared_ptr<OutputTreePair>> tree_list;
miral::WindowManagerTools window_manager_tools;