Skip to content

Commit 42429f1

Browse files
committed
Remove libxdg_basedir
libxdg_basedir was dropped due to memory leaks. xdg.cpp now implements config path finding as specified by the XDG specification.
1 parent 2d5cad4 commit 42429f1

File tree

9 files changed

+146
-20
lines changed

9 files changed

+146
-20
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ addons:
2323
- libfftw3-dev
2424
- libpulse-dev
2525
- libconfig++-dev
26-
- libxdg-basedir-dev
2726
- libxrandr-dev
2827
- libgl1-mesa-dev
2928
- libx11-dev

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ GLMViz is NOT a lightweight program as it's build using GLFW and other OpenGL ab
88
* OpenGL libraries: `libglfw`, `libglm`
99
* FFT library: `fftw`
1010
* PulseAudio[Optional]: `libpulse`
11-
* Configuration libraries: `libconfig++`, `libxdg-basedir`
11+
* Configuration library: `libconfig++`
1212

1313
Additionally CMake 3.0.2 / [Meson](http://www.mesonbuild.com) and a C++11 compatible compiler are needed to successfully build the project.
1414

cpackconf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SET(CPACK_PACKAGE_VERSION_MAJOR "1")
1010
SET(CPACK_PACKAGE_VERSION_MINOR "0")
1111
SET(CPACK_PACKAGE_VERSION_PATCH "0")
1212
SET(CPACK_PACKAGE_EXECUTABLES "glmviz" "GLMViz")
13-
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libglfw3, libfftw3-bin, libconfig++-dev, libxdg-basedir1")
13+
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libglfw3, libfftw3-bin, libconfig++-dev")
1414
SET(CPACK_DEBIAN_PACKAGE_SUGGESTS "libpulse0")
1515

1616
INCLUDE(CPack)

make_pkg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ARCH="amd64"
66
PKG_LICENSE="GPLv3"
77
MAINTAINER="Hannes Haberl"
88
PROVIDES="GLMViz"
9-
REQUIRES="libglfw3, libfftw3-bin, libconfig++-dev, libxdg-basedir1"
9+
REQUIRES="libglfw3, libfftw3-bin, libconfig++-dev"
1010
cd "${MESON_BUILD_ROOT}"
1111
DESTDIR=./install_root ninja install &&
1212
checkinstall --install=no --pkgname="$PKG_NAME" --pkgversion="$PKG_VERSION" --pkglicense="$PKG_LICENSE" --maintainer="$MAINTAINER" --provides="$PROVIDES" --requires="$REQUIRES" --nodoc -y cp -r install_root/. / &&

src/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ find_package(Threads REQUIRED)
66
pkg_search_module(FFTW3 REQUIRED fftw3f libfftw3f)
77
pkg_search_module(GLFW REQUIRED glfw3)
88
pkg_search_module(CONFIG++ REQUIRED libconfig++)
9-
pkg_search_module(BASEDIR REQUIRED libxdg-basedir)
109

1110
find_package(PulseAudio)
1211

@@ -15,7 +14,6 @@ include_directories(${GLFW_INCLUDE_DIRS})
1514
#include_directories(${GLM_INCLUDE_DIRS})
1615
include_directories(${FFTW3_INCLUDE_DIRS})
1716
include_directories(${CONFIG++_INCLUDE_DIRS})
18-
include_directories(${BASEDIR_INCLUDE_DIRS})
1917

2018
if(PULSEAUDIO_FOUND)
2119
Message("PulseAudio found. Building with PulseAudio support.")
@@ -25,9 +23,9 @@ if(PULSEAUDIO_FOUND)
2523
set(PULSE_FILES "Pulse.cpp")
2624
endif(PULSEAUDIO_FOUND)
2725

28-
add_executable(glmviz GLMViz.cpp Program.cpp FFT.cpp Spectrum.cpp Oscilloscope.cpp Fifo.cpp ${PULSE_FILES} Buffer.cpp Config.cpp)
26+
add_executable(glmviz GLMViz.cpp Program.cpp FFT.cpp Spectrum.cpp Oscilloscope.cpp Fifo.cpp ${PULSE_FILES} Buffer.cpp Config.cpp xdg.cpp)
2927

30-
target_link_libraries(glmviz ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${FFTW3_LIBRARIES} ${CONFIG++_LIBRARIES} ${BASEDIR_LIBRARIES} ${PULSE_LIBS})
28+
target_link_libraries(glmviz ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${FFTW3_LIBRARIES} ${CONFIG++_LIBRARIES} ${PULSE_LIBS})
3129

3230
# install GLMViz
3331
install(TARGETS glmviz DESTINATION ${CMAKE_INSTALL_PREFIX})

src/Config.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@
1919

2020
#include "Config.hpp"
2121

22-
#include <basedir_fs.h>
22+
#include "xdg.hpp"
2323
#include <stdlib.h>
2424
#include <iostream>
2525
#include <algorithm>
2626
#include <stdexcept>
2727
#include <cmath>
2828

2929
Config::Config(const std::string& config_file){
30-
file = config_file;
31-
32-
xdgHandle xdghandle;
33-
34-
if(file == "" && xdgInitHandle(&xdghandle)){
35-
file = xdgConfigFind("GLMViz/config", &xdghandle);
36-
37-
xdgWipeHandle(&xdghandle);
30+
if(config_file != ""){
31+
if(xdg::verify_path(config_file)){
32+
file = config_file;
33+
}else{
34+
std::cerr << "The specified config file doesn't exist, falling back to default config!" << std::endl;
35+
}
3836
}
3937

38+
if(file == "") file = xdg::find_config("/GLMViz/config");
39+
4040
if(file == "") file = "/etc/GLMViz/config";
4141

4242
reload();

src/meson.build

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dep_threads = dependency('threads')
33
dep_fftw = dependency('fftw3f')
44
dep_glfw = dependency('glfw3')
55
dep_cfgpp = dependency('libconfig++')
6-
dep_basedir = dependency('libxdg-basedir')
76
dep_glm = dependency('glm', required: false)
87

98
if not dep_glm.found()
@@ -27,6 +26,6 @@ if opt_pulse.found() and opt_pulse_simple.found()
2726
add_project_arguments('-DWITH_PULSE', language : 'cpp')
2827
endif
2928

30-
src = ['GLMViz.cpp', 'Program.cpp', 'FFT.cpp', 'Spectrum.cpp', 'Oscilloscope.cpp', 'Fifo.cpp', 'Buffer.cpp', 'Config.cpp']
29+
src = ['GLMViz.cpp', 'Program.cpp', 'FFT.cpp', 'Spectrum.cpp', 'Oscilloscope.cpp', 'Fifo.cpp', 'Buffer.cpp', 'Config.cpp', 'xdg.cpp']
3130

32-
exe = executable('glmviz', [src, src_pulse], dependencies: [dep_gl, dep_threads, dep_fftw, dep_glfw, dep_cfgpp, dep_basedir, dep_pulse], install: true)
31+
exe = executable('glmviz', [src, src_pulse], dependencies: [dep_gl, dep_threads, dep_fftw, dep_glfw, dep_cfgpp, dep_pulse], install: true)

src/xdg.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2017 Hannes Haberl
3+
*
4+
* This file is part of GLMViz.
5+
*
6+
* GLMViz is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* GLMViz is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with GLMViz. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "xdg.hpp"
21+
22+
#include <pwd.h>
23+
#include <unistd.h>
24+
#include <fstream>
25+
#include <sstream>
26+
#include <cstdlib>
27+
28+
namespace xdg{
29+
std::string config_home(){
30+
std::string config_home;
31+
const char* cxdg_cfg_home = std::getenv("XDG_CONFIG_HOME");
32+
if(cxdg_cfg_home != nullptr){
33+
config_home = cxdg_cfg_home;
34+
}
35+
36+
return config_home;
37+
}
38+
39+
std::string default_config_home(){
40+
std::string config_home;
41+
// get default config directory
42+
struct passwd* pw = ::getpwuid(::getuid());
43+
config_home = pw->pw_dir;
44+
config_home += "/.config";
45+
46+
return config_home;
47+
}
48+
49+
std::vector<std::string> config_dirs(){
50+
std::string config_dirs;
51+
const char* cxdg_cfg_dirs = std::getenv("XDG_CONFIG_DIRS");
52+
if(cxdg_cfg_dirs != nullptr){
53+
config_dirs = cxdg_cfg_dirs;
54+
}
55+
56+
std::vector<std::string> dirs;
57+
std::istringstream ss(config_dirs);
58+
std::string dir;
59+
60+
while(std::getline(ss, dir, ':')){
61+
dirs.push_back(dir);
62+
}
63+
64+
return dirs;
65+
}
66+
67+
std::string default_config_dir(){ return "/etc/xdg"; }
68+
69+
bool verify_path(const std::string& path){
70+
std::ifstream file(path);
71+
return file.good();
72+
}
73+
74+
std::string find_config(const std::string& path){
75+
std::string config;
76+
// check XDG_CONFIG_HOME
77+
config = config_home() + path;
78+
if(verify_path(config)) return config;
79+
80+
// use default value for XDG_CONFIG_HOME
81+
config = default_config_home() + path;
82+
if(verify_path(config)) return config;
83+
84+
// check XDG_CONFIG_DIRS
85+
for(std::string& dir : config_dirs()){
86+
config = dir + path;
87+
if(verify_path(config)) return config;
88+
}
89+
90+
// check XDG_CONFIG_DIRS default value
91+
config = default_config_dir() + path;
92+
if(verify_path(config)) return config;
93+
94+
// return empty string if no config file was found
95+
return "";
96+
}
97+
}

src/xdg.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2017 Hannes Haberl
3+
*
4+
* This file is part of GLMViz.
5+
*
6+
* GLMViz is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* GLMViz is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with GLMViz. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#pragma once
21+
22+
#include <string>
23+
#include <vector>
24+
25+
namespace xdg{
26+
std::string config_home();
27+
std::string default_config_home();
28+
std::vector<std::string> config_dirs();
29+
std::string default_config_dir();
30+
bool verify_path(const std::string&);
31+
32+
std::string find_config(const std::string&);
33+
}

0 commit comments

Comments
 (0)