Skip to content

Commit 58a9714

Browse files
committed
lua: Add function for ConCommands, use for hotkeys
1 parent 15c627f commit 58a9714

File tree

7 files changed

+72
-44
lines changed

7 files changed

+72
-44
lines changed

scripts/00-gamescope/common/util.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ end
1717
function debug(text)
1818
gamescope.log(gamescope.log_priority.debug, text)
1919
end
20+
21+
function command(args)
22+
gamescope.command(args)
23+
end

scripts/00-gamescope/input/hotkeys.lua

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,65 @@ gamescope.config.input.hotkeys = {
22
{
33
-- XKB_KEY_Super_L + XKB_KEY_f
44
keys = { 65515, 102 },
5-
command = { "fullscreen" }
5+
func = function()
6+
command({ "fullscreen" })
7+
end
68
},
79
{
810
-- XKB_KEY_Super_L + XKB_KEY_n
911
keys = { 65515, 110 },
10-
command = { "upscale_filter", "nearest" }
12+
func = function()
13+
command({ "upscale_filter", "nearest" })
14+
end
1115
},
1216
{
1317
-- XKB_KEY_Super_L + XKB_KEY_b
1418
keys = { 65515, 98 },
15-
command = { "upscale_filter", "linear" }
19+
func = function()
20+
command({ "upscale_filter", "linear" })
21+
end
1622
},
1723
{
1824
-- XKB_KEY_Super_L + XKB_KEY_u
1925
keys = { 65515, 117 },
20-
command = { "upscale_filter", "fsr" }
26+
func = function()
27+
command({ "upscale_filter", "fsr" })
28+
end
2129
},
2230
{
2331
-- XKB_KEY_Super_L + XKB_KEY_y
2432
keys = { 65515, 121 },
25-
command = { "upscale_filter", "nis" }
33+
func = function()
34+
command({ "upscale_filter", "nis" })
35+
end
2636
},
2737
{
2838
-- XKB_KEY_Super_L + XKB_KEY_i
2939
keys = { 65515, 105 },
30-
command = { "upscale_filter_sharpness", "up" }
40+
func = function()
41+
command({ "upscale_filter_sharpness", "up" })
42+
end
3143
},
3244
{
3345
-- XKB_KEY_Super_L + XKB_KEY_o
3446
keys = { 65515, 111 },
35-
command = { "upscale_filter_sharpness", "down" }
47+
func = function()
48+
command({ "upscale_filter_sharpness", "down" })
49+
end
3650
},
3751
{
3852
-- XKB_KEY_Super_L + XKB_KEY_s
3953
keys = { 65515, 115 },
40-
command = { "screenshot" }
54+
func = function()
55+
command({ "screenshot" })
56+
end
4157
},
4258
{
4359
-- XKB_KEY_Super_L + XKB_KEY_g
4460
keys = { 65515, 103 },
45-
command = { "keyboard_grab" }
61+
func = function()
62+
command({ "keyboard_grab" })
63+
end
4664
},
4765
}
4866

src/Script/Script.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ namespace gamescope
112112
);
113113
m_Gamescope.Base["log"] = []( LogPriority ePriority, std::string_view svText ) { s_ScriptLog.log( ePriority, svText ); };
114114

115+
m_Gamescope.Base["command"] = []( std::vector<std::string_view> svArgs )
116+
{
117+
if ( !gamescope::ConCommand::Exec( std::span<std::string_view>{ svArgs } ) )
118+
{
119+
std::string sDebug = "";
120+
for ( std::string_view sv : svArgs )
121+
{
122+
sDebug += sv;
123+
sDebug += " ";
124+
}
125+
s_ScriptMgrLog.warnf( "Failed to exec command: %s\n", sDebug.c_str() );
126+
}
127+
};
128+
115129
m_Gamescope.Convars.Base = m_State.create_table();
116130
m_Gamescope.Base.set( "convars", m_Gamescope.Convars.Base );
117131

@@ -299,9 +313,9 @@ namespace gamescope
299313
return oOutDisplay;
300314
}
301315

302-
std::vector<std::pair<std::vector<uint32_t>, std::vector<std::string>>> GamescopeScript_t::Config_t::GetHotkeys( CScriptScopedLock &script )
316+
std::vector<std::pair<std::vector<uint32_t>, sol::function>> GamescopeScript_t::Config_t::GetHotkeys( CScriptScopedLock &script )
303317
{
304-
std::vector<std::pair<std::vector<uint32_t>, std::vector<std::string>>> hotkeys;
318+
std::vector<std::pair<std::vector<uint32_t>, sol::function>> hotkeys;
305319

306320
sol::optional<std::vector<sol::optional<sol::table>>> ovotHotkeys = Input["hotkeys"];
307321
if ( !ovotHotkeys )
@@ -319,12 +333,12 @@ namespace gamescope
319333
continue;
320334
std::vector<uint32_t> vKeys = *ovKeys;
321335

322-
sol::optional<std::vector<std::string>> osCommand = tHotkey["command"];
323-
if ( !osCommand )
336+
sol::optional<sol::function> oFunc = tHotkey["func"];
337+
if ( !oFunc )
324338
continue;
325-
std::vector<std::string> sCommand = *osCommand;
339+
sol::function func = *oFunc;
326340

327-
hotkeys.push_back( std::make_pair( vKeys, sCommand ) );
341+
hotkeys.push_back( std::make_pair( vKeys, func ) );
328342
}
329343

330344
return hotkeys;

src/Script/Script.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace gamescope
3434

3535
std::optional<std::pair<std::string_view, sol::table>> LookupDisplay( CScriptScopedLock &script, std::string_view psvVendor, uint16_t uProduct, std::string_view psvModel, std::string_view psvDataString );
3636

37-
std::vector<std::pair<std::vector<uint32_t>, std::vector<std::string>>> GetHotkeys( CScriptScopedLock &script );
37+
std::vector<std::pair<std::vector<uint32_t>, sol::function>> GetHotkeys( CScriptScopedLock &script );
3838
} Config;
3939
};
4040

src/hotkey.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#if HAVE_SCRIPTING
2+
13
#include <linux/input-event-codes.h>
24

35
#include <span>
46

57
#include "hotkey.h"
6-
#include "convar.h"
78

89
// TODO: Consolidate
910
#define WAYLAND_NULL() []<typename... Args> ( void *pData, Args... args ) { }
@@ -16,20 +17,15 @@ namespace gamescope
1617
class CHotkeyBinding
1718
{
1819
public:
19-
bool Init( gamescope_action_binding_manager *pManager, std::span<uint32_t> pKeySyms, std::vector<std::string> args )
20+
bool Init( gamescope_action_binding_manager *pManager, std::span<uint32_t> pKeySyms, sol::function func )
2021
{
2122
Shutdown();
2223

2324
m_pBinding = gamescope_action_binding_manager_create_action_binding( pManager );
2425
if ( !m_pBinding )
2526
return false;
2627

27-
m_args = args;
28-
for ( std::string_view sv : args )
29-
{
30-
m_sDescription += sv;
31-
m_sDescription += " ";
32-
}
28+
m_func = func;
3329

3430
wl_array array;
3531
wl_array_init(&array);
@@ -41,7 +37,7 @@ namespace gamescope
4137

4238
gamescope_action_binding_add_listener( m_pBinding, &s_BindingListener, (void *)this );
4339
gamescope_action_binding_add_keyboard_trigger( m_pBinding, &array );
44-
gamescope_action_binding_set_description( m_pBinding, m_sDescription.c_str() );
40+
gamescope_action_binding_set_description( m_pBinding, "TODO: for debugging" );
4541
gamescope_action_binding_arm( m_pBinding, 0 );
4642

4743
return true;
@@ -58,23 +54,12 @@ namespace gamescope
5854

5955
void Wayland_Triggered( gamescope_action_binding *pBinding, uint32_t uSequence, uint32_t uTriggerFlags, uint32_t uTimeLo, uint32_t uTimeHi )
6056
{
61-
// eugh
62-
std::vector<std::string_view> vec;
63-
for ( std::string_view sv : m_args )
64-
{
65-
vec.push_back( sv );
66-
}
67-
68-
if ( !gamescope::ConCommand::Exec( std::span<std::string_view>{ vec } ) )
69-
{
70-
fprintf( stderr, "Failed to exec: %s\n", m_sDescription.c_str() );
71-
}
57+
m_func();
7258
}
7359

7460
private:
7561
gamescope_action_binding *m_pBinding = nullptr;
76-
std::vector<std::string> m_args;
77-
std::string m_sDescription;
62+
sol::function m_func;
7863

7964
static const gamescope_action_binding_listener s_BindingListener;
8065
};
@@ -119,10 +104,10 @@ namespace gamescope
119104
return true;
120105
}
121106

122-
bool HotkeyHandler::Bind( std::vector<uint32_t> pKeySyms, std::vector<std::string> args )
107+
bool HotkeyHandler::Bind( std::vector<uint32_t> pKeySyms, sol::function func )
123108
{
124109
std::shared_ptr<CHotkeyBinding> binding = std::make_shared<CHotkeyBinding>();
125-
auto success = binding->Init( m_pActionBindingManager, pKeySyms, args );
110+
auto success = binding->Init( m_pActionBindingManager, pKeySyms, func );
126111
if ( success )
127112
{
128113
m_bindings.push_back(binding);
@@ -145,3 +130,5 @@ namespace gamescope
145130
.global_remove = WAYLAND_NULL(),
146131
};
147132
}
133+
134+
#endif // HAVE_SCRIPTING

src/hotkey.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
#if HAVE_SCRIPTING
2+
13
#pragma once
24

35
#include <cstdint>
46
#include <memory>
57
#include <vector>
6-
#include <string>
78

89
#include <gamescope-action-binding-client-protocol.h>
910

11+
#include <sol/sol.hpp>
12+
1013
namespace gamescope
1114
{
1215
class CHotkeyBinding;
@@ -18,7 +21,7 @@ namespace gamescope
1821
~HotkeyHandler() {}
1922

2023
bool Init();
21-
bool Bind( std::vector<uint32_t> pKeySyms, std::vector<std::string> args );
24+
bool Bind( std::vector<uint32_t> pKeySyms, sol::function func );
2225
void Dispatch() { wl_display_dispatch(m_pDisplay); }
2326

2427
private:
@@ -32,3 +35,5 @@ namespace gamescope
3235

3336
extern HotkeyHandler g_hotkeyHandler;
3437
}
38+
39+
#endif // HAVE_SCRIPTING

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,13 +1058,13 @@ static void hotkeyThreadRun()
10581058
{
10591059
wlserver.bWaylandServerRunning.wait( false );
10601060

1061+
#if HAVE_SCRIPTING
10611062
if ( !gamescope::g_hotkeyHandler.Init() )
10621063
{
10631064
fprintf( stderr, "Failed to initialize hotkeys\n" );
10641065
return;
10651066
}
10661067

1067-
#if HAVE_SCRIPTING
10681068
{
10691069
gamescope::CScriptScopedLock script;
10701070
auto hotkeys = script.Manager().Gamescope().Config.GetHotkeys( script );
@@ -1073,12 +1073,12 @@ static void hotkeyThreadRun()
10731073
gamescope::g_hotkeyHandler.Bind( hot.first, hot.second );
10741074
}
10751075
}
1076-
#endif
10771076

10781077
while ( wlserver.bWaylandServerRunning )
10791078
{
10801079
gamescope::g_hotkeyHandler.Dispatch();
10811080
}
1081+
#endif
10821082
}
10831083

10841084
gamescope::ConCommand cc_upscale_filter( "upscale_filter", "Set upscale filter method. Values: nearest, linear, fsr, nis",

0 commit comments

Comments
 (0)