Debian Codegraph @

Source/system.h

raw
1 #ifndef EXADRUMS_SOURCE_SYSTEM_H
2 #define EXADRUMS_SOURCE_SYSTEM_H
3
4 #include <filesystem>
5
6 #include <vector>
7
8 #include <unistd.h>
9
10 namespace fs = std::filesystem;
11
12 inline const static fs::path systemUserPath = fs::path{std::getenv("HOME")}/".eXaDrums";
13 inline const static fs::path systemRootPath{"/usr/share/exadrums"};
14 inline const static bool systemIsRoot = getuid() == 0 && geteuid() == 0;
15
16 // Paths
17 inline fs::path UserPath()
18 {
19 return systemUserPath;
20 }
21
22 inline fs::path RootPath()
23 {
24 return systemRootPath;
25 }
26
27 inline fs::path RootDataPath()
28 {
29 return systemRootPath/"Data";
30 }
31
32 inline fs::path UiPath()
33 {
34 return systemRootPath/"Ui/Ui.glade";
35 }
36
37 inline bool IsRoot() { return systemIsRoot; }
38
39 // Various functions
40
41
42 inline void copyFiles(const fs::path& from, const fs::path& to, const std::vector<fs::path>& files)
43 {
44 for(const auto& file : files)
45 {
46 fs::copy_file(from/file, to/file);
47 }
48 }
49
50 inline bool IsInstalledForUser()
51 {
52 return fs::is_directory(systemUserPath);
53 }
54
55 inline fs::path UserDataPath()
56 {
57 return systemUserPath/"Data";
58 }
59
60 inline bool InstallForUser()
61 {
62 if(IsInstalledForUser())
63 {
64 return false;
65 }
66
67 // Create directories first
68 fs::create_directory(systemUserPath);
69 fs::create_directory(UserDataPath());
70 fs::create_directory(UserDataPath()/"Kits");
71 fs::create_directory(UserDataPath()/"Rec");
72 fs::create_directory(UserDataPath()/"SoundBank");
73 fs::create_directory(UserDataPath()/"SoundBank/BassDrum");
74 fs::create_directory(UserDataPath()/"SoundBank/SnareDrum");
75 fs::create_directory(UserDataPath()/"SoundBank/Crash");
76 fs::create_directory(UserDataPath()/"SoundBank/HiHat");
77 fs::create_directory(UserDataPath()/"SoundBank/Ride");
78 fs::create_directory(UserDataPath()/"SoundBank/Tom");
79
80 // Copy configuration files
81 copyFiles(systemRootPath, UserDataPath(), { fs::path{"alsaConfig.xml"},
82 fs::path{"metronomeConfig.xml"},
83 fs::path{"sensorsConfig.xml"},
84 fs::path{"triggersConfig.xml"},
85 fs::path{"spiDev.xml"}});
86
87 // Install default kit
88 copyFiles(systemRootPath, UserDataPath()/"Kits", { fs::path{"Default.xml"} });
89 copyFiles(systemRootPath, UserDataPath()/"SoundBank/SnareDrum", { fs::path{"Snr_Acou_01.wav"} });
90 copyFiles(systemRootPath, UserDataPath()/"SoundBank/BassDrum", { fs::path{"BD_Acou_01.wav"} });
91 copyFiles(systemRootPath, UserDataPath()/"SoundBank/Crash", { fs::path{"Crash_High.wav"} });
92 copyFiles(systemRootPath, UserDataPath()/"SoundBank/HiHat", { fs::path{"HiHat.wav"} });
93 copyFiles(systemRootPath, UserDataPath()/"SoundBank/Ride", { fs::path{"RideBell.wav"} });
94 copyFiles(systemRootPath, UserDataPath()/"SoundBank/Ride", { fs::path{"RideEdge.wav"} });
95 copyFiles(systemRootPath, UserDataPath()/"SoundBank/Tom", { fs::path{"Tom01.wav"} });
96 copyFiles(systemRootPath, UserDataPath()/"SoundBank/Tom", { fs::path{"Tom03.wav"} });
97
98
99 return true;
100 }
101
102 inline bool ResetConfig()
103 {
104 fs::remove_all(systemUserPath);
105 InstallForUser();
106
107 return true;
108 }
109
110 #endif /* EXADRUMS_SOURCE_SYSTEM_H */