kranewm

An ICCCM & EWMH compliant X11 reparenting, dynamic window manager, written in C++
git clone git clone git://git.deurzen.net/kranewm.git
Log | Files | Refs | LICENSE

commit f083ef490d046f3cbb6c000f1f6e2a25743fed22
parent 7ab5836e80d64aa250bfd467a549c7f08245b8df
Author: deurzen <m.deurzen@tum.de>
Date:   Sat,  4 Sep 2021 09:37:40 +0200

implements initial config encapsulation

Diffstat:
Asrc/core/config.cc | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/core/config.hh | 40++++++++++++++++++++++++++++++++++++++++
Msrc/core/model.cc | 115+++++++------------------------------------------------------------------------
Msrc/core/model.hh | 6+++---
4 files changed, 152 insertions(+), 108 deletions(-)

diff --git a/src/core/config.cc b/src/core/config.cc @@ -0,0 +1,99 @@ +#include "config.hh" +#include "defaults.hh" + +#include <fstream> +#include <iostream> +#include <sstream> +#include <string> + +Config::Config() +{ + { // set config directory + std::stringstream configdir_ss; + + if (const char* env_xdgconf = std::getenv("XDG_CONFIG_HOME")) + configdir_ss << env_xdgconf << "/" << WM_NAME << "/"; + else + configdir_ss << "$HOME/.config/" << WM_NAME << "/"; + + directory = configdir_ss.str(); + } + + { // produce vector of to-ignore-{producers,consumers} + std::ifstream in(directory + std::string("consumeignore")); + + if (in.good()) { + std::string line; + + while (std::getline(in, line)) { + std::string::size_type pos = line.find('#'); + + if (pos != std::string::npos) + line = line.substr(0, pos); + + if (line.length() < 5) + continue; + + line.erase(4, line.find_first_not_of(" \t\n\r\f\v")); + line.erase(line.find_last_not_of(" \t\n\r\f\v") + 1); + + if (line.length() < 5) + continue; + + std::vector<SearchSelector_ptr>* ignored; + switch (line[0]) { + case '<': ignored = &ignored_producers; break; + case '>': ignored = &ignored_consumers; break; + default: continue; + } + + SearchSelector::SelectionCriterium criterium; + + switch (line[2]) { + case 'N': + { + switch (line[1]) { + case '=': criterium = SearchSelector::SelectionCriterium::ByNameEquals; break; + case '~': criterium = SearchSelector::SelectionCriterium::ByNameContains; break; + default: continue; + } + + break; + } + case 'C': + { + switch (line[1]) { + case '=': criterium = SearchSelector::SelectionCriterium::ByClassEquals; break; + case '~': criterium = SearchSelector::SelectionCriterium::ByClassContains; break; + default: continue; + } + + break; + } + case 'I': + { + switch (line[1]) { + case '=': criterium = SearchSelector::SelectionCriterium::ByInstanceEquals; break; + case '~': criterium = SearchSelector::SelectionCriterium::ByInstanceContains; break; + default: continue; + } + + break; + } + default: continue; + } + + ignored->push_back(new SearchSelector{criterium, line.substr(4)}); + } + } + } +} + +Config::~Config() +{ + for (std::size_t i = 0; i < ignored_producers.size(); ++i) + delete ignored_producers[i]; + + for (std::size_t i = 0; i < ignored_consumers.size(); ++i) + delete ignored_consumers[i]; +} diff --git a/src/core/config.hh b/src/core/config.hh @@ -0,0 +1,40 @@ +#ifndef __CONFIG_H_GUARD__ +#define __CONFIG_H_GUARD__ + +#include "search.hh" + +#include <string> +#include <vector> + +struct Config +{ +#ifdef DEBUG + static constexpr bool debugging = true; +#else + static constexpr bool debugging = false; +#endif + +#ifdef ENABLE_IPC + static constexpr bool ipc_enabled = true; +#else + static constexpr bool ipc_enabled = false; +#endif + + Config(); + ~Config(); + + Config(const Config&) = delete; + Config(Config&&) = delete; + Config& operator=(const Config&) = delete; + Config& operator=(Config&&) = delete; + + std::string directory = "$HOME/.config"; + std::string blocking_autostart = "blocking_autostart"; + std::string nonblocking_autostart = "nonblocking_autostart"; + + std::vector<SearchSelector_ptr> ignored_producers; + std::vector<SearchSelector_ptr> ignored_consumers; + +}; + +#endif//__CONFIG_H_GUARD__ diff --git a/src/core/model.cc b/src/core/model.cc @@ -8,9 +8,7 @@ #include <cmath> #include <cstdlib> #include <cstring> -#include <fstream> #include <functional> -#include <iostream> #include <set> #include <sstream> #include <thread> @@ -22,12 +20,6 @@ extern "C" { #include <unistd.h> } -#ifdef ENABLE_IPC -const bool IPC_ENABLED = true; -#else -const bool IPC_ENABLED = false; -#endif - #ifdef DEBUG #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG #endif @@ -61,8 +53,6 @@ Model::Model(Connection& conn) m_leader_map({}), m_sticky_clients({}), m_unmanaged_windows({}), - m_ignored_producers({}), - m_ignored_consumers({}), mp_focus(nullptr), mp_jumped_from(nullptr), m_key_bindings({ @@ -711,7 +701,8 @@ Model::Model(Connection& conn) return false; } }, - }) + }), + m_config() { #ifdef DEBUG spdlog::set_level(spdlog::level::debug); @@ -733,7 +724,7 @@ Model::Model(Connection& conn) for (std::size_t i = 0; i < workspace_names.size(); ++i) m_workspaces.insert_at_back(new Workspace(i, workspace_names[i])); - if constexpr (IPC_ENABLED) + if constexpr (Config::ipc_enabled) m_conn.init_wm_ipc(); acquire_partitions(); @@ -769,90 +760,10 @@ Model::Model(Connection& conn) for (auto& window : m_conn.top_level_windows()) manage(window, !m_conn.must_manage_window(window), true); - { // user configurations - std::stringstream configdir_ss; - - if (const char* env_xdgconf = std::getenv("XDG_CONFIG_HOME")) - configdir_ss << env_xdgconf << "/" << WM_NAME << "/"; - else - configdir_ss << "$HOME/.config/" << WM_NAME << "/"; - - { // produce vector of to-ignore-{producers,consumers} - std::ifstream in(configdir_ss.str() + std::string("consumeignore")); - - if (in.good()) { - std::string line; - - while (std::getline(in, line)) { - std::string::size_type pos = line.find('#'); - - if (pos != std::string::npos) - line = line.substr(0, pos); - - if (line.length() < 5) - continue; - - line.erase(4, line.find_first_not_of(" \t\n\r\f\v")); - line.erase(line.find_last_not_of(" \t\n\r\f\v") + 1); - - if (line.length() < 5) - continue; - - std::vector<SearchSelector_ptr>* ignored; - switch (line[0]) { - case '<': ignored = &m_ignored_producers; break; - case '>': ignored = &m_ignored_consumers; break; - default: continue; - } - - SearchSelector::SelectionCriterium criterium; - - switch (line[2]) { - case 'N': - { - switch (line[1]) { - case '=': criterium = SearchSelector::SelectionCriterium::ByNameEquals; break; - case '~': criterium = SearchSelector::SelectionCriterium::ByNameContains; break; - default: continue; - } - - break; - } - case 'C': - { - switch (line[1]) { - case '=': criterium = SearchSelector::SelectionCriterium::ByClassEquals; break; - case '~': criterium = SearchSelector::SelectionCriterium::ByClassContains; break; - default: continue; - } - - break; - } - case 'I': - { - switch (line[1]) { - case '=': criterium = SearchSelector::SelectionCriterium::ByInstanceEquals; break; - case '~': criterium = SearchSelector::SelectionCriterium::ByInstanceContains; break; - default: continue; - } - - break; - } - default: continue; - } - - ignored->push_back(new SearchSelector{criterium, line.substr(4)}); - } - } - } - -#ifndef DEBUG - { // run user-configured autostart programs - spawn_external(configdir_ss.str() + std::string("blocking_autostart")); - spawn_external(configdir_ss.str() + std::string("nonblocking_autostart &")); - spdlog::info("ran autostart scripts"); - } -#endif + if constexpr (!Config::debugging) { + spawn_external(m_config.directory + m_config.blocking_autostart); + spawn_external(m_config.directory + m_config.nonblocking_autostart); + spdlog::info("ran autostart scripts"); } spdlog::info("initialized " + WM_NAME); @@ -877,12 +788,6 @@ Model::~Model() for (Client_ptr client : clients) delete client; - for (std::size_t i = 0; i < m_ignored_producers.size(); ++i) - delete m_ignored_producers[i]; - - for (std::size_t i = 0; i < m_ignored_consumers.size(); ++i) - delete m_ignored_consumers[i]; - m_partitions.clear(); m_contexts.clear(); m_workspaces.clear(); @@ -894,7 +799,7 @@ void Model::run() { while (m_running) - if constexpr (IPC_ENABLED) { + if constexpr (Config::ipc_enabled) { if (m_conn.check_progress()) { // process IPC message m_conn.process_messages( @@ -2999,7 +2904,7 @@ Model::consume_client(Client_ptr producer, Client_ptr client) if (Util::contains(ignored_producers_memoized, producer_handle)) return; - for (SearchSelector_ptr selector : m_ignored_producers) + for (SearchSelector_ptr selector : m_config.ignored_producers) if (client_matches_search(producer, *selector)) { ignored_producers_memoized.insert(producer_handle); return; @@ -3011,7 +2916,7 @@ Model::consume_client(Client_ptr producer, Client_ptr client) if (Util::contains(ignored_consumers_memoized, consumer_handle)) return; - for (SearchSelector_ptr selector : m_ignored_consumers) + for (SearchSelector_ptr selector : m_config.ignored_consumers) if (client_matches_search(client, *selector)) { ignored_consumers_memoized.insert(consumer_handle); return; diff --git a/src/core/model.hh b/src/core/model.hh @@ -5,6 +5,7 @@ #include "../winsys/event.hh" #include "bindings.hh" #include "client.hh" +#include "config.hh" #include "context.hh" #include "cycle.hh" #include "layout.hh" @@ -238,9 +239,6 @@ private: std::vector<Client_ptr> m_sticky_clients; std::vector<winsys::Window> m_unmanaged_windows; - std::vector<SearchSelector_ptr> m_ignored_producers; - std::vector<SearchSelector_ptr> m_ignored_consumers; - Client_ptr mp_focus; Client_ptr mp_jumped_from; @@ -369,6 +367,8 @@ private: } m_message_visitor = MessageVisitor(*this); + Config m_config; + }; #endif//__MODEL_H_GUARD__