-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathegshellcommands.cpp
195 lines (154 loc) · 5.34 KB
/
egshellcommands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright © 2022 Octopull Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Alan Griffiths <[email protected]>
*/
#include "egshellcommands.h"
#include "eglauncher.h"
#include "egwindowmanager.h"
#include <miral/runner.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include <utility>
egmde::ShellCommands::ShellCommands(MirRunner& runner, Launcher& launcher, std::string terminal_cmd, std::function<void()> const& launch_app) :
runner{runner}, launcher{launcher}, terminal_cmd{std::move(terminal_cmd)}, launch_app{launch_app}
{
}
void egmde::ShellCommands::advise_new_window_for(miral::Application const& /*app*/)
{
std::lock_guard<decltype(mutex)> lock{mutex};
++app_windows;
}
void egmde::ShellCommands::advise_delete_window_for(miral::Application const& /*app*/)
{
std::lock_guard<decltype(mutex)> lock{mutex};
--app_windows;
}
auto egmde::ShellCommands::keyboard_shortcuts(MirKeyboardEvent const* kev) -> bool
{
if (mir_keyboard_event_action(kev) == mir_keyboard_action_up)
return false;
MirInputEventModifiers mods = mir_keyboard_event_modifiers(kev);
if (!(mods & mir_input_event_modifier_alt) || !(mods & mir_input_event_modifier_ctrl))
return false;
auto const key_code = mir_keyboard_event_key_code(kev);
if (key_code == XKB_KEY_Delete && mir_keyboard_event_action(kev) == mir_keyboard_action_down)
{
shell_commands_active = !shell_commands_active;
return true;
}
if (!shell_commands_active)
return false;
switch (key_code)
{
case XKB_KEY_A:
case XKB_KEY_a:
if (mir_keyboard_event_action(kev) != mir_keyboard_action_down)
return false;
launch_app();
return true;
case XKB_KEY_BackSpace:
if (mir_keyboard_event_action(kev) == mir_keyboard_action_down)
{
std::lock_guard<decltype(mutex)> lock{mutex};
if (app_windows > 0)
{
return false;
}
}
runner.stop();
return true;
case XKB_KEY_T:
case XKB_KEY_t:
if (mir_keyboard_event_action(kev) != mir_keyboard_action_down)
return false;
launcher.run_app(terminal_cmd, egmde::Launcher::Mode::wayland);
return true;
case XKB_KEY_X:
case XKB_KEY_x:
if (mir_keyboard_event_action(kev) != mir_keyboard_action_down)
return false;
launcher.run_app(terminal_cmd, egmde::Launcher::Mode::x11);
return true;
case XKB_KEY_Left:
wm->dock_active_window_left();
return true;
case XKB_KEY_Right:
wm->dock_active_window_right();
return true;
case XKB_KEY_space:
wm->toggle_maximized_restored();
return true;
case XKB_KEY_Up:
wm->workspace_up(mods & mir_input_event_modifier_shift);
return true;
case XKB_KEY_Down:
wm->workspace_down(mods & mir_input_event_modifier_shift);
return true;
case XKB_KEY_bracketright:
wm->focus_next_application();
return true;
case XKB_KEY_bracketleft:
wm->focus_prev_application();
return true;
case XKB_KEY_braceright:
wm->focus_next_within_application();
return true;
case XKB_KEY_braceleft:
wm->focus_prev_within_application();
return true;
default:
return false;
}
}
auto egmde::ShellCommands::touch_shortcuts(MirTouchEvent const* tev) -> bool
{
if (in_touch_gesture)
{
if (mir_touch_event_action(tev, 0) == mir_touch_action_up)
in_touch_gesture = false;
return true;
}
if (mir_touch_event_point_count(tev) != 1)
return false;
if (mir_touch_event_action(tev, 0) != mir_touch_action_down)
return false;
auto const active_output = wm->active_output();
Rectangle const launch_area{active_output.top_left, Size{active_output.size.width/192, active_output.size.height}};
Point const touch_point{mir_touch_event_axis_value(tev, 0, mir_touch_axis_x), mir_touch_event_axis_value(tev, 0, mir_touch_axis_y)};
if (!launch_area.contains(touch_point))
return false;
launch_app();
in_touch_gesture = true;
return true;
}
auto egmde::ShellCommands::input_event(MirEvent const* event) -> bool
{
if (mir_event_get_type(event) != mir_event_type_input)
return false;
auto const* input_event = mir_event_get_input_event(event);
switch (mir_input_event_get_type(input_event))
{
case mir_input_event_type_touch:
return touch_shortcuts(mir_input_event_get_touch_event(input_event));
case mir_input_event_type_key:
return keyboard_shortcuts(mir_input_event_get_keyboard_event(input_event));
default:
return false;
}
}
void egmde::ShellCommands::init_window_manager(WindowManagerPolicy* wm)
{
this->wm = wm;
}