Skip to content

Commit cad7995

Browse files
committed
shortcut: Add ConCommands for bindings
1 parent a87c7fb commit cad7995

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/shortcut.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <linux/input-event-codes.h>
22

33
#include "shortcut.h"
4+
#include "convar.h"
45
#include "steamcompmgr_shared.hpp"
56
#include "main.hpp"
67

@@ -87,4 +88,99 @@ namespace gamescope
8788

8889
return true;
8990
}
91+
92+
static const char* ShortcutNames[GAMESCOPE_SHORTCUT_COUNT] = {
93+
"",
94+
"fullscreen",
95+
"filter_nearest",
96+
"filter_linear",
97+
"upscale_fsr",
98+
"upscale_nis",
99+
"upscale_sharpness_up",
100+
"upscale_sharpness_down",
101+
"screenshot",
102+
"keyboard_grab",
103+
};
104+
105+
static GamescopeShortcut ParseShortcut( std::string_view sv )
106+
{
107+
if ( sv.length() > 0 )
108+
{
109+
for ( auto i = 0; i < GAMESCOPE_SHORTCUT_COUNT; i++ )
110+
{
111+
if ( sv == ShortcutNames[i] )
112+
{
113+
return static_cast<GamescopeShortcut>( i );
114+
}
115+
}
116+
}
117+
118+
return GAMESCOPE_SHORTCUT_NONE;
119+
}
120+
121+
static ConCommand cc_bind( "bind",
122+
"Bind a shortcut to SUPER-<key> combination. e.g. 'bind 33,fullscreen'",
123+
[](std::span<std::string_view> svArgs )
124+
{
125+
if ( svArgs.size() < 2 )
126+
{
127+
console_log.errorf( "Invalid number of arguments." );
128+
return;
129+
}
130+
131+
// Space delimited args would be nice but requires
132+
// changes to gamescopectl / gamescope_private_execute.
133+
auto svBinds = gamescope::Split( svArgs[1], "," );
134+
if ( svBinds.size() < 2 )
135+
{
136+
console_log.errorf( "Invalid number of arguments." );
137+
return;
138+
}
139+
140+
// TODO: key names
141+
std::optional<uint32_t> ouKey = Parse<uint32_t>( svBinds[0] );
142+
if ( !ouKey )
143+
{
144+
console_log.errorf( "Failed to parse key." );
145+
return;
146+
}
147+
148+
uint32_t uKey = *ouKey;
149+
auto shortcut = ParseShortcut( svBinds[1] );
150+
151+
if ( shortcut != GAMESCOPE_SHORTCUT_NONE )
152+
{
153+
g_shortcutHandler.Bind( uKey, shortcut );
154+
}
155+
});
156+
157+
static ConCommand cc_unbind( "unbind", "Unbind a shortcut from SUPER-<key> combination.",
158+
[](std::span<std::string_view> svArgs )
159+
{
160+
if ( svArgs.size() < 2 )
161+
{
162+
console_log.errorf( "Invalid number of arguments." );
163+
return;
164+
}
165+
166+
// TODO: key names
167+
std::optional<uint32_t> ouKey = Parse<uint32_t>( svArgs[1] );
168+
if ( !ouKey )
169+
{
170+
console_log.errorf( "Failed to parse key." );
171+
return;
172+
}
173+
174+
g_shortcutHandler.Unbind( *ouKey );
175+
});
176+
177+
static ConCommand cc_shortcuts( "shortcuts", "List shortcut names for use with 'bind'",
178+
[](std::span<std::string_view> svArgs )
179+
{
180+
console_log.infof( "Shortcut names:" );
181+
for ( auto i = 0; i < GAMESCOPE_SHORTCUT_COUNT; i++ )
182+
{
183+
console_log.infof( " %s", ShortcutNames[i] );
184+
}
185+
});
90186
}

0 commit comments

Comments
 (0)