Skip to content

Commit

Permalink
Improve config watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseExposito committed Nov 18, 2020
1 parent 9c248d5 commit ff6bb77
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
31 changes: 21 additions & 10 deletions src/config/xml-config-loader.cpp
Expand Up @@ -48,6 +48,11 @@ constexpr std::size_t WATCH_BUFFER_SIZE = (100 * (WATCH_EVENT_SIZE + 16));
XmlConfigLoader::XmlConfigLoader(Config *config) : config(config) {}

void XmlConfigLoader::load() {
this->parseConfig();
this->watchConfig();
}

std::filesystem::path XmlConfigLoader::getConfigFilePath() const {
const std::filesystem::path usrConfigFile = Paths::getSystemConfigFilePath();
const std::filesystem::path homeConfigFile = Paths::getUserConfigFilePath();

Expand All @@ -57,14 +62,14 @@ void XmlConfigLoader::load() {
"Reinstall Touchégg to solve this issue"};
}

const std::filesystem::path configPath =
std::filesystem::exists(homeConfigFile) ? homeConfigFile : usrConfigFile;
std::cout << "Using configuration file " << configPath << std::endl;
this->parseXml(configPath);
this->watchFile(configPath);
return std::filesystem::exists(homeConfigFile) ? homeConfigFile
: usrConfigFile;
}

void XmlConfigLoader::parseXml(const std::filesystem::path &configPath) {
void XmlConfigLoader::parseConfig() {
std::filesystem::path configPath = this->getConfigFilePath();
std::cout << "Using configuration file " << configPath << std::endl;

pugi::xml_document doc;
pugi::xml_parse_result parsedSuccessfully = doc.load_file(configPath.c_str());

Expand Down Expand Up @@ -117,8 +122,12 @@ void XmlConfigLoader::parseApplicationXmlNodes(const pugi::xml_node &rootNode) {
}
}

void XmlConfigLoader::watchFile(const std::filesystem::path &configPath) {
void XmlConfigLoader::watchConfig() {
// https://developer.ibm.com/tutorials/l-ubuntu-inotify/

std::filesystem::path homeConfigDir = Paths::getUserConfigDirPath();
std::filesystem::create_directories(homeConfigDir);

const std::string warningMessage =
"It was not posssible to monitor your configuration file for changes. "
"Touchégg will not be able to automatically reload your configuration "
Expand All @@ -131,13 +140,15 @@ void XmlConfigLoader::watchFile(const std::filesystem::path &configPath) {
return;
}

int wd = inotify_add_watch(fd, configPath.c_str(), IN_MODIFY | IN_CREATE);
int wd = inotify_add_watch(fd, homeConfigDir.c_str(),
IN_MODIFY | IN_CREATE | IN_MOVE | IN_DELETE |
IN_MOVE_SELF | IN_DELETE_SELF);
if (wd < 0) {
std::cout << warningMessage << std::endl;
return;
}

std::thread watchThread{[fd, configPath, this]() {
std::thread watchThread{[fd, this]() {
std::array<char, WATCH_BUFFER_SIZE> buffer{};
while (true) {
bool reloadSettings = false;
Expand All @@ -159,7 +170,7 @@ void XmlConfigLoader::watchFile(const std::filesystem::path &configPath) {
std::cout << "Your configuration file changed, reloading your settings"
<< std::endl;
this->config->clear();
this->parseXml(configPath);
this->parseConfig();
}
}
}};
Expand Down
13 changes: 8 additions & 5 deletions src/config/xml-config-loader.h
Expand Up @@ -56,11 +56,15 @@ class XmlConfigLoader {
*/
Config *config;

/**
* @returns Path to the config file to use.
*/
std::filesystem::path getConfigFilePath() const;

/**
* Parse the XML configuration file placed in path.
* @param configPath Path to the configuration file.
*/
void parseXml(const std::filesystem::path &configPath);
void parseConfig();

/**
* Parse the global settings.
Expand All @@ -75,10 +79,9 @@ class XmlConfigLoader {
void parseApplicationXmlNodes(const pugi::xml_node &rootNode);

/**
* Watch the configuration file and parse it on change.
* @param configPath Path to the configuration file.
* Watch the configuration file and parse it on change or creation.
*/
void watchFile(const std::filesystem::path &configPath);
void watchConfig();
};

#endif // CONFIG_XML_CONFIG_LOADER_H_
4 changes: 2 additions & 2 deletions src/utils/color.cpp
Expand Up @@ -55,8 +55,8 @@ void Color::setFromHexString(const std::string &hexString) {
void Color::setFromAutoColor(ColorType colorType) {
#ifdef AUTO_COLORS
gtk_init_check(nullptr, nullptr);
while (gtk_events_pending()) {
gtk_main_iteration_do(false);
while (gtk_events_pending() != 0) {
gtk_main_iteration_do(0);
}

GtkWidget *label = gtk_label_new("");
Expand Down

0 comments on commit ff6bb77

Please sign in to comment.