1 #pragma once
2 
3 #include <moodycamel/blockingconcurrentqueue.h>
4 
5 #include <thread>
6 
7 #include "common.hpp"
8 #include "components/types.hpp"
9 #include "events/signal_fwd.hpp"
10 #include "events/signal_receiver.hpp"
11 #include "events/types.hpp"
12 #include "settings.hpp"
13 #include "utils/actions.hpp"
14 #include "utils/file.hpp"
15 #include "x11/types.hpp"
16 
17 POLYBAR_NS
18 
19 // fwd decl {{{
20 
21 enum class alignment;
22 class bar;
23 class config;
24 class connection;
25 class inotify_watch;
26 class ipc;
27 class logger;
28 class signal_emitter;
29 namespace modules {
30   struct module_interface;
31 }  // namespace modules
32 using module_t = shared_ptr<modules::module_interface>;
33 using modulemap_t = std::map<alignment, vector<module_t>>;
34 
35 // }}}
36 
37 class controller
38     : public signal_receiver<SIGN_PRIORITY_CONTROLLER, signals::eventqueue::exit_terminate,
39           signals::eventqueue::exit_reload, signals::eventqueue::notify_change, signals::eventqueue::notify_forcechange,
40           signals::eventqueue::check_state, signals::ipc::action, signals::ipc::command, signals::ipc::hook,
41           signals::ui::ready, signals::ui::button_press, signals::ui::update_background> {
42  public:
43   using make_type = unique_ptr<controller>;
44   static make_type make(unique_ptr<ipc>&& ipc, unique_ptr<inotify_watch>&& config_watch);
45 
46   explicit controller(connection&, signal_emitter&, const logger&, const config&, unique_ptr<bar>&&, unique_ptr<ipc>&&,
47       unique_ptr<inotify_watch>&&);
48   ~controller();
49 
50   bool run(bool writeback, string snapshot_dst);
51 
52   bool enqueue(event&& evt);
53   bool enqueue(string&& input_data);
54 
55  protected:
56   void read_events();
57   void process_eventqueue();
58   void process_inputdata();
59   bool process_update(bool force);
60 
61   bool on(const signals::eventqueue::notify_change& evt);
62   bool on(const signals::eventqueue::notify_forcechange& evt);
63   bool on(const signals::eventqueue::exit_terminate& evt);
64   bool on(const signals::eventqueue::exit_reload& evt);
65   bool on(const signals::eventqueue::check_state& evt);
66   bool on(const signals::ui::ready& evt);
67   bool on(const signals::ui::button_press& evt);
68   bool on(const signals::ipc::action& evt);
69   bool on(const signals::ipc::command& evt);
70   bool on(const signals::ipc::hook& evt);
71   bool on(const signals::ui::update_background& evt);
72 
73  private:
74   size_t setup_modules(alignment align);
75 
76   bool forward_action(const actions_util::action& cmd);
77   bool try_forward_legacy_action(const string& cmd);
78 
79   connection& m_connection;
80   signal_emitter& m_sig;
81   const logger& m_log;
82   const config& m_conf;
83   unique_ptr<bar> m_bar;
84   unique_ptr<ipc> m_ipc;
85   unique_ptr<inotify_watch> m_confwatch;
86 
87   array<unique_ptr<file_descriptor>, 2> m_queuefd{};
88 
89   /**
90    * \brief State flag
91    */
92   std::atomic<bool> m_process_events{false};
93 
94   /**
95    * \brief Destination path of generated snapshot
96    */
97   string m_snapshot_dst;
98 
99   /**
100    * \brief Controls weather the output gets printed to stdout
101    */
102   bool m_writeback{false};
103 
104   /**
105    * \brief Internal event queue
106    */
107   moodycamel::BlockingConcurrentQueue<event> m_queue;
108 
109   /**
110    * \brief Loaded modules
111    */
112   vector<module_t> m_modules;
113 
114   /**
115    * \brief Loaded modules grouped by block
116    */
117   modulemap_t m_blocks;
118 
119   /**
120    * \brief Maximum number of subsequent events to swallow
121    */
122   size_t m_swallow_limit{5U};
123 
124   /**
125    * \brief Time to wait for subsequent events
126    */
127   std::chrono::milliseconds m_swallow_update{10};
128 
129   /**
130    * \brief Input data
131    */
132   string m_inputdata;
133 
134   /**
135    * \brief Thread for the eventqueue loop
136    */
137   std::thread m_event_thread;
138 
139   /**
140    * \brief Misc threads
141    */
142   vector<std::thread> m_threads;
143 };
144 
145 POLYBAR_NS_END
146