Skip to content

Commit f2dcd1e

Browse files
committed
Utils: add helper functions to get standard directories
1 parent 71b07c8 commit f2dcd1e

5 files changed

Lines changed: 62 additions & 49 deletions

File tree

src/Script/Script.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
#include "convar.h"
33
#include "color_helpers.h"
44
#include "../log.hpp"
5+
#include "Utils/DirHelpers.h"
56

67
#include <filesystem>
78
#include <algorithm>
89

9-
std::string_view GetHomeDir();
10-
1110
namespace gamescope
1211
{
1312
using namespace std::literals;
@@ -18,20 +17,6 @@ namespace gamescope
1817
static ConVar<bool> cv_script_use_local_scripts{ "script_use_local_scripts", false, "Whether or not to use the local scripts (../config) as opposed to the ones in /etc/gamescope.d" };
1918
static ConVar<bool> cv_script_use_user_scripts{ "script_use_user_scripts", true, "Whether or not to use user config scripts ($XDG_CONFIG_DIR/gamescope) at all." };
2019

21-
static std::string_view GetConfigDir()
22-
{
23-
static std::string s_sConfigDir = []() -> std::string
24-
{
25-
const char *pszConfigHome = getenv( "XDG_CONFIG_HOME" );
26-
if ( pszConfigHome && *pszConfigHome )
27-
return pszConfigHome;
28-
29-
return std::string{ GetHomeDir() } + "/.config";
30-
}();
31-
32-
return s_sConfigDir;
33-
}
34-
3520
static inline void PanicFunction( sol::optional<std::string> oMsg )
3621
{
3722
s_ScriptLog.errorf( "Lua is in a panic state and will now abort() the application" );

src/Utils/DirHelpers.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "DirHelpers.h"
2+
3+
#include <sys/types.h>
4+
#include <pwd.h>
5+
#include <unistd.h>
6+
7+
namespace gamescope {
8+
std::string_view GetHomeDir()
9+
{
10+
static std::string s_sHomeDir = []() -> std::string
11+
{
12+
const char *pszHomeDir = getenv( "HOME" );
13+
if ( pszHomeDir )
14+
return pszHomeDir;
15+
16+
return getpwuid( getuid() )->pw_dir;
17+
}();
18+
return s_sHomeDir;
19+
}
20+
21+
std::string GetLocalUsrDir()
22+
{
23+
return std::string{ GetHomeDir() } + "/.local";
24+
}
25+
26+
std::string GetUsrDir()
27+
{
28+
return "/usr";
29+
}
30+
31+
std::string_view GetConfigDir()
32+
{
33+
static std::string s_sConfigDir = []() -> std::string
34+
{
35+
const char *pszConfigHome = getenv( "XDG_CONFIG_HOME" );
36+
if ( pszConfigHome && *pszConfigHome )
37+
return pszConfigHome;
38+
39+
return std::string{ GetHomeDir() } + "/.config";
40+
}();
41+
42+
return s_sConfigDir;
43+
}
44+
}

src/Utils/DirHelpers.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace gamescope
6+
{
7+
std::string_view GetHomeDir();
8+
std::string GetLocalUsrDir();
9+
std::string GetUsrDir();
10+
std::string_view GetConfigDir();
11+
}

src/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ required_wlroots_features = ['xwayland']
9797
src = [
9898
'Backends/HeadlessBackend.cpp',
9999
'Backends/WaylandBackend.cpp',
100+
'Utils/DirHelpers.cpp',
100101
'Utils/TempFiles.cpp',
101102
'Utils/Version.cpp',
102103
'Utils/Process.cpp',
@@ -206,6 +207,7 @@ gamescope_version = configure_file(
206207
gamescope_core_src = [
207208
'convar.cpp',
208209
'log.cpp',
210+
'Utils/DirHelpers.cpp',
209211
'Utils/Process.cpp',
210212
'Utils/Version.cpp',
211213
]

src/reshade_effect_manager.cpp

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "reshade_effect_manager.hpp"
66
#include "log.hpp"
7+
#include "Utils/DirHelpers.h"
78

89
#include "steamcompmgr.hpp"
910

@@ -13,17 +14,12 @@
1314
#include "gamescope-reshade-protocol.h"
1415

1516
#include "reshade_api_format.hpp"
16-
#include "convar.h"
1717

1818
#include <stb_image.h>
1919
#define STB_IMAGE_RESIZE_IMPLEMENTATION
2020
#include <stb_image_resize.h>
2121

2222
#include <mutex>
23-
#include <unistd.h>
24-
#include <sys/types.h>
25-
#include <pwd.h>
26-
#include <iostream>
2723

2824
// This is based on wl_array_for_each from `wayland-util.h` in the Wayland client library.
2925
#define uint8_array_for_each(pos, data, size) \
@@ -36,31 +32,6 @@ static std::mutex g_runtimeUniformsMutex;
3632

3733
extern int g_nOutputRefresh;
3834

39-
const char *homedir;
40-
41-
std::string_view GetHomeDir()
42-
{
43-
static std::string s_sHomeDir = []() -> std::string
44-
{
45-
const char *pszHomeDir = getenv( "HOME" );
46-
if ( pszHomeDir )
47-
return pszHomeDir;
48-
49-
return getpwuid( getuid() )->pw_dir;
50-
}();
51-
return s_sHomeDir;
52-
}
53-
54-
static std::string GetLocalUsrDir()
55-
{
56-
return std::string{ GetHomeDir() } + "/.local";
57-
}
58-
59-
static std::string GetUsrDir()
60-
{
61-
return "/usr";
62-
}
63-
6435
static LogScope reshade_log("gamescope_reshade");
6536

6637
///////////////
@@ -967,8 +938,8 @@ bool ReshadeEffectPipeline::init(CVulkanDevice *device, const ReshadeEffectKey &
967938

968939
std::string gamescope_reshade_share_path = "/share/gamescope/reshade";
969940

970-
std::string local_reshade_path = GetLocalUsrDir() + gamescope_reshade_share_path;
971-
std::string global_reshade_path = GetUsrDir() + gamescope_reshade_share_path;
941+
std::string local_reshade_path = gamescope::GetLocalUsrDir() + gamescope_reshade_share_path;
942+
std::string global_reshade_path = gamescope::GetUsrDir() + gamescope_reshade_share_path;
972943

973944
pp.add_include_path(local_reshade_path + "/Shaders");
974945
pp.add_include_path(global_reshade_path + "/Shaders");
@@ -1993,4 +1964,4 @@ void reshade_effect_manager_enable_effect()
19931964
void reshade_effect_manager_disable_effect()
19941965
{
19951966
gamescope_clear_reshade_effect();
1996-
}
1967+
}

0 commit comments

Comments
 (0)