YggdrasilWM  0.1.1
A tiny window manager coded in C++
Bars Class Reference

Class to manage the bars This class is a singleton and is responsible for managing the bars instanciate with createInstance and initialize with init should be destroyed with destroy when the program ends Bars::run runs in a separate thread, so all data must be passed through the TSBarsData class that handle synchronization. More...

#include <Bars.hpp>

Collaboration diagram for Bars:
Collaboration graph

Public Member Functions

 Bars (const Bars &)=delete
 
Barsoperator= (const Bars &)=delete
 
 ~Bars ()
 
void init (std::shared_ptr< ConfigDataBars > configData, std::shared_ptr< TSBarsData > tsData, Display *display, Window root)
 Initialize the Bars class. More...
 
void run ()
 Run the Bars class in a separate thread. More...
 
void start_thread ()
 
unsigned int getSpaceN () const
 Get the spaceN value used to calculate the space left for the Layout Manager. More...
 
unsigned int getSpaceS () const
 Get the spaceS value used to calculate the space left for the Layout Manager. More...
 
unsigned int getSpaceE () const
 Get the spaceE value used to calculate the space left for the Layout Manager. More...
 
unsigned int getSpaceW () const
 Get the spaceW value used to calculate the space left for the Layout Manager. More...
 
const std::unordered_map< std::string, std::string > & getData () const
 
bool isBarWindow (Window window)
 Check if the window is a bar window used to filter events in EventHandler. More...
 
void stop_thread ()
 Stop the thread (join) More...
 
void redraw ()
 Redraw the bars by calling the draw method of each bar. More...
 
void addPluginLocation (const std::string &location)
 
const std::set< std::string > & getPluginsLocations () const
 
void * getWidgetTypeHandle (const std::string &widgetType)
 
void setWidgetTypeHandle (const std::string &widgetType, void *handle)
 
void subscribeWidget (Widget *w)
 

Static Public Member Functions

static void createInstance ()
 Create the singleton instance if the instance is already created, it will do nothing. More...
 
static BarsgetInstance ()
 Get the singleton instance if the instance is not created, it will create it. More...
 
static void destroy ()
 Destroy the singleton instance if the instance is not created, it will do nothing. More...
 

Private Member Functions

 Bars ()
 

Private Attributes

std::vector< std::unique_ptr< Bar > > bars
 
std::vector< Window > windows
 
std::shared_ptr< ConfigDataBarsconfigData
 
std::shared_ptr< TSBarsDatatsData
 
std::unordered_map< std::string, std::string > data
 
Display * display
 
Window root
 
unsigned int spaceN
 
unsigned int spaceS
 
unsigned int spaceE
 
unsigned int spaceW
 
std::thread barThread
 
std::set< std::string > pluginsLocations
 
std::unordered_map< std::string, void * > widgetTypeHandle
 
std::unordered_map< std::string, std::vector< Widget * > > subscriptions
 

Static Private Attributes

static Barsinstance = nullptr
 

Detailed Description

Class to manage the bars This class is a singleton and is responsible for managing the bars instanciate with createInstance and initialize with init should be destroyed with destroy when the program ends Bars::run runs in a separate thread, so all data must be passed through the TSBarsData class that handle synchronization.

Note
This class is a singleton
See also
TSBarsData

Constructor & Destructor Documentation

◆ Bars() [1/2]

Bars::Bars ( const Bars )
delete

◆ ~Bars()

Bars::~Bars ( )
156  {
157  for (auto &bar : bars) {
158  for (auto &w : bar->getWidgets()) {
159  typedef void (destroy_t)(Widget *);
160  destroy_t* destroy = (destroy_t*) dlsym(w.second , "destroyPlugin");
161  const char* dlsym_error = dlerror();
162  if (dlsym_error) {
163  Logger::GetInstance()->Log("Cannot load symbol destroy: " + std::string(dlerror()),L_ERROR);
164  continue;
165  }
166  destroy(w.second);
167  }
168  }
169  for (auto handle : widgetTypeHandle) {
170  dlclose(handle.second);
171  }
172 }
@ L_ERROR
Definition: Logger.hpp:51
std::unordered_map< std::string, void * > widgetTypeHandle
Definition: Bars.hpp:164
std::vector< std::unique_ptr< Bar > > bars
Definition: Bars.hpp:151
static void destroy()
Destroy the singleton instance if the instance is not created, it will do nothing.
Definition: Bars.cpp:182
virtual void Log(const std::string &message, LogLevel level) const
Log a message This method logs a message to the log file or stream. The message is only logged if the...
Definition: Logger.cpp:73
static Logger * GetInstance()
Get the Logger object.
Definition: Logger.cpp:41
Definition: Widget.hpp:33

References bars, destroy(), Logger::GetInstance(), L_ERROR, Logger::Log(), and widgetTypeHandle.

Here is the call graph for this function:

◆ Bars() [2/2]

Bars::Bars ( )
private
124  : spaceN(0),
125  spaceS(0),
126  spaceE(0),
127  spaceW(0),
128  configData(nullptr),
129  tsData(nullptr),
130  data(),
131  display(nullptr),
132  root(0)
133  {}
std::shared_ptr< TSBarsData > tsData
Definition: Bars.hpp:154
std::shared_ptr< ConfigDataBars > configData
Definition: Bars.hpp:153
Display * display
Definition: Bars.hpp:156
unsigned int spaceE
Definition: Bars.hpp:160
unsigned int spaceS
Definition: Bars.hpp:159
unsigned int spaceW
Definition: Bars.hpp:161
unsigned int spaceN
Definition: Bars.hpp:158
Window root
Definition: Bars.hpp:157
std::unordered_map< std::string, std::string > data
Definition: Bars.hpp:155

Referenced by createInstance().

Here is the caller graph for this function:

Member Function Documentation

◆ addPluginLocation()

void Bars::addPluginLocation ( const std::string &  location)
135  {
136  pluginsLocations.emplace(location);
137 }
std::set< std::string > pluginsLocations
Definition: Bars.hpp:163

References pluginsLocations.

Referenced by init().

Here is the caller graph for this function:

◆ createInstance()

static void Bars::createInstance ( )
static

Create the singleton instance if the instance is already created, it will do nothing.

173  {
174  if (Bars::instance == nullptr)
175  Bars::instance = new Bars();
176 }
static Bars * instance
Definition: Bars.hpp:150
Bars()
Definition: Bars.cpp:124

References Bars(), and instance.

Referenced by WindowManager::createBars(), and getInstance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroy()

static void Bars::destroy ( )
static

Destroy the singleton instance if the instance is not created, it will do nothing.

182  {
183  if (Bars::instance != nullptr)
184  delete Bars::instance;
185 }

References instance.

Referenced by WindowManager::Stop(), and ~Bars().

Here is the caller graph for this function:

◆ getData()

const std::unordered_map< std::string, std::string > & Bars::getData ( ) const
190 { return data; }

References data.

◆ getInstance()

static Bars & Bars::getInstance ( )
static

Get the singleton instance if the instance is not created, it will create it.

Returns
reference to the singleton instance
177  {
178  if (Bars::instance == nullptr)
180  return *Bars::instance;
181 }
static void createInstance()
Create the singleton instance if the instance is already created, it will do nothing.
Definition: Bars.cpp:173

References createInstance(), and instance.

Referenced by WindowManager::createBars(), EventHandler::handleButtonPress(), EventHandler::handleDestroyNotify(), EventHandler::handleEnterNotify(), EventHandler::handleExpose(), EventHandler::handleMotionNotify(), EventHandler::handleUnmapNotify(), and WindowManager::Stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getPluginsLocations()

const std::set< std::string > & Bars::getPluginsLocations ( ) const
139  {
140  return pluginsLocations;
141 }

References pluginsLocations.

◆ getSpaceE()

unsigned int Bars::getSpaceE ( ) const

Get the spaceE value used to calculate the space left for the Layout Manager.

188 { return this->spaceE; }

References spaceE.

Referenced by WindowManager::createBars().

Here is the caller graph for this function:

◆ getSpaceN()

unsigned int Bars::getSpaceN ( ) const

Get the spaceN value used to calculate the space left for the Layout Manager.

186 { return this->spaceN; }

References spaceN.

Referenced by WindowManager::createBars().

Here is the caller graph for this function:

◆ getSpaceS()

unsigned int Bars::getSpaceS ( ) const

Get the spaceS value used to calculate the space left for the Layout Manager.

187 { return this->spaceS; }

References spaceS.

Referenced by WindowManager::createBars().

Here is the caller graph for this function:

◆ getSpaceW()

unsigned int Bars::getSpaceW ( ) const

Get the spaceW value used to calculate the space left for the Layout Manager.

189 { return this->spaceW; }

References spaceW.

Referenced by WindowManager::createBars().

Here is the caller graph for this function:

◆ getWidgetTypeHandle()

void * Bars::getWidgetTypeHandle ( const std::string &  widgetType)
143  {
144  try {
145  void * handle = widgetTypeHandle[widgetType];
146  return handle;
147  } catch (std::out_of_range &e) {
148  Logger::GetInstance()->Log("Widget Type " + widgetType + " not found",L_ERROR);
149  return nullptr;
150  }
151 }

References Logger::GetInstance(), L_ERROR, Logger::Log(), and widgetTypeHandle.

Referenced by init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

void Bars::init ( std::shared_ptr< ConfigDataBars configData,
std::shared_ptr< TSBarsData tsData,
Display *  display,
Window  root 
)

Initialize the Bars class.

Parameters
configDataglobal bars configuration will pass the right config data to each bar
tsDatathread safe data to pass data between threads
displayto limit calls to WindowManager::getDisplay
rootto limit calls to WindowManager::getRoot
Todo:
use of x11wrapper
43  {
44  this->configData = configData;
45  this->tsData =tsData;
46  this->display = display;
47  this->root = root;
48  for (auto &bar : this->configData->getBars()) {
49  std::unique_ptr<Bar> newBar = std::make_unique<Bar>();
50  newBar->init(bar, this->tsData);
51  if (bar->getBarPosition() == "top") {
52  this->spaceN += bar->getBarSize();
53  } else if (bar->getBarPosition() == "bottom") {
54  this->spaceS += bar->getBarSize();
55  } else if (bar->getBarPosition() == "left") {
56  this->spaceW += bar->getBarSize();
57  } else if (bar->getBarPosition() == "right") {
58  this->spaceE += bar->getBarSize();
59  }
60  for (auto &w : bar->getWidgets()) {
61  if (pluginsLocations.find(w->getPluginLocation()) == pluginsLocations.end()) {
62  addPluginLocation(w->getPluginLocation());
63  void *handle = dlopen(w->getPluginLocation().c_str(), RTLD_LAZY);
64  if (!handle) {
65  Logger::GetInstance()->Log("Cannot open library: " + std::string(dlerror()),L_ERROR);
66  continue;
67  }
68  setWidgetTypeHandle(w->getType(), handle);
69  Logger::GetInstance()->Log("Library " + w->getPluginLocation() + " opened",L_INFO);
70  }
71  newBar->addWidget(getWidgetTypeHandle(w->getType()), w);
72  }
73  Logger::GetInstance()->Log("Bar ["
74  + std::to_string(this->bars.size())
75  + "] on window ["
76  + std::to_string(newBar->getWindow())
77  + "] initialized\t"
78  + bar->getBarPosition()
79  + " "
80  + std::to_string(newBar->getSizeX())
81  + " x "
82  + std::to_string(newBar->getSizeY()),L_INFO);
83  for (auto w:newBar->getWidgets()) {
84  subscribeWidget(w.second);
85  }
86  this->windows.push_back(newBar->getWindow());
87  this->bars.push_back(std::move(newBar));
88  }
89 }
@ L_INFO
Definition: Logger.hpp:49
void setWidgetTypeHandle(const std::string &widgetType, void *handle)
Definition: Bars.cpp:153
std::vector< Window > windows
Definition: Bars.hpp:152
void * getWidgetTypeHandle(const std::string &widgetType)
Definition: Bars.cpp:143
void addPluginLocation(const std::string &location)
Definition: Bars.cpp:135
void subscribeWidget(Widget *w)
Definition: Bars.cpp:199

References addPluginLocation(), bars, configData, display, Logger::GetInstance(), getWidgetTypeHandle(), L_ERROR, L_INFO, Logger::Log(), pluginsLocations, root, setWidgetTypeHandle(), spaceE, spaceN, spaceS, spaceW, subscribeWidget(), tsData, and windows.

Referenced by WindowManager::createBars().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isBarWindow()

bool Bars::isBarWindow ( Window  window)

Check if the window is a bar window used to filter events in EventHandler.

Parameters
window
Returns
true if the window is a bar window
191  {
192  auto it = std::find(windows.begin(), windows.end(), window);
193  return it != windows.end();
194 }

References windows.

◆ operator=()

Bars& Bars::operator= ( const Bars )
delete

◆ redraw()

void Bars::redraw ( )

Redraw the bars by calling the draw method of each bar.

Parameters
msgmessage to pass to the draw method (temporary arg for debug purpose)
119  {
120  for (auto &bar : this->bars) {
121  bar->draw();
122  }
123 }

References bars.

Referenced by EventHandler::handleEnterNotify(), EventHandler::handleExpose(), and run().

Here is the caller graph for this function:

◆ run()

void Bars::run ( )

Run the Bars class in a separate thread.

Note
all data must be passed through the TSBarsData class
93  {
94  while (WindowManager::getInstance()->getRunning()){
95  try {
96  if (tsData->wait()) {
97  // Updated Data redraw
98  std::unordered_map<std::string,std::string>updated = tsData->getData();
99  for (const auto &pair: updated) {
100  this->data[pair.first] = pair.second;
101  if (subscriptions.find(pair.first) != subscriptions.end()) {
102  for (Widget* widget : subscriptions[pair.first]) {
103  widget->updateData(pair.first, pair.second);
104  widget->draw();
105  }
106  }
107  XFlush(display);
108  }
109  this->redraw();
110  } else {
111  // Periodic redraw
112  this->redraw();
113  }
114  } catch (const std::exception &e) {
115  Logger::GetInstance()->Log("Bars thread exception: " + std::string(e.what()),L_ERROR);
116  }
117  }
118 }
void redraw()
Redraw the bars by calling the draw method of each bar.
Definition: Bars.cpp:119
std::unordered_map< std::string, std::vector< Widget * > > subscriptions
Definition: Bars.hpp:165
static WindowManager * getInstance()
Get the WindowManager instance this is a singleton class this function returns the instance of the Wi...
Definition: WindowManager.cpp:244

References data, display, Logger::GetInstance(), WindowManager::getInstance(), L_ERROR, Logger::Log(), redraw(), subscriptions, and tsData.

Referenced by start_thread().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setWidgetTypeHandle()

void Bars::setWidgetTypeHandle ( const std::string &  widgetType,
void *  handle 
)
153  {
154  widgetTypeHandle[widgetType] = handle;
155 }

References widgetTypeHandle.

Referenced by init().

Here is the caller graph for this function:

◆ start_thread()

void Bars::start_thread ( )
90  {
91  barThread = std::thread(&Bars::run, this);
92 }
void run()
Run the Bars class in a separate thread.
Definition: Bars.cpp:93
std::thread barThread
Definition: Bars.hpp:162

References barThread, and run().

Referenced by WindowManager::createBars().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop_thread()

void Bars::stop_thread ( )

Stop the thread (join)

195  {
196  barThread.join();
197 }

References barThread.

Referenced by WindowManager::Stop().

Here is the caller graph for this function:

◆ subscribeWidget()

void Bars::subscribeWidget ( Widget w)
199  {
200  std::vector<std::string> keys = w->registerDataKey();
201  for (const auto &key: keys) {
202  if (key.empty())
203  continue;
204  subscriptions[key].push_back(w);
205  }
206 }
virtual std::vector< std::string > registerDataKey()=0

References Widget::registerDataKey(), and subscriptions.

Referenced by init().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ bars

std::vector<std::unique_ptr<Bar> > Bars::bars
private

Referenced by init(), redraw(), and ~Bars().

◆ barThread

std::thread Bars::barThread
private

Referenced by start_thread(), and stop_thread().

◆ configData

std::shared_ptr<ConfigDataBars> Bars::configData
private

Referenced by init().

◆ data

std::unordered_map<std::string, std::string> Bars::data
private

Referenced by getData(), and run().

◆ display

Display* Bars::display
private

Referenced by init(), and run().

◆ instance

Bars * Bars::instance = nullptr
staticprivate

Yb dP 8 w 8 Yb dP 8b d8 YbdP .d88 .d88 .d88 8d8b .d88 d88b w 8 Yb db dP 8YbmdP8 YP 8 8 8 8 8 8 8P 8 8 Yb. 8 8 YbdPYbdP 8 " 8 88Y88 Y88Y88 8 `Y88 Y88P 8 8 YP YP 8 8 wwdP wwdP Yggdrasil Window Manager https://github.com/corecaps/YggdrasilWM Copyright (C) 2024 jgarcia jgarc.nosp@m.ia@s.nosp@m.tuden.nosp@m.t.42.nosp@m..fr corec.nosp@m.aps@.nosp@m.gmail.nosp@m..com

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Referenced by createInstance(), destroy(), and getInstance().

◆ pluginsLocations

std::set<std::string> Bars::pluginsLocations
private

◆ root

Window Bars::root
private

Referenced by init().

◆ spaceE

unsigned int Bars::spaceE
private

Referenced by getSpaceE(), and init().

◆ spaceN

unsigned int Bars::spaceN
private

Referenced by getSpaceN(), and init().

◆ spaceS

unsigned int Bars::spaceS
private

Referenced by getSpaceS(), and init().

◆ spaceW

unsigned int Bars::spaceW
private

Referenced by getSpaceW(), and init().

◆ subscriptions

std::unordered_map<std::string,std::vector<Widget *> > Bars::subscriptions
private

Referenced by run(), and subscribeWidget().

◆ tsData

std::shared_ptr<TSBarsData> Bars::tsData
private

Referenced by init(), and run().

◆ widgetTypeHandle

std::unordered_map<std::string, void *> Bars::widgetTypeHandle
private

◆ windows

std::vector<Window> Bars::windows
private

Referenced by init(), and isBarWindow().


The documentation for this class was generated from the following files: