1 #pragma once
2 
3 #include <cstdlib>
4 #include <atomic>
5 #include <mutex>
6 
7 #include "common.hpp"
8 #include "components/types.hpp"
9 #include "errors.hpp"
10 #include "events/signal_fwd.hpp"
11 #include "events/signal_receiver.hpp"
12 #include "utils/math.hpp"
13 #include "settings.hpp"
14 #include "x11/types.hpp"
15 #include "x11/window.hpp"
16 
17 POLYBAR_NS
18 
19 // fwd {{{
20 class config;
21 class connection;
22 class logger;
23 class parser;
24 class renderer;
25 class screen;
26 class taskqueue;
27 class tray_manager;
28 // }}}
29 
30 /**
31  * Allows a new format for pixel sizes (like width in the bar section)
32  *
33  * The new format is X%:Z, where X is in [0, 100], and Z is any real value
34  * describing a pixel offset. The actual value is calculated by X% * max + Z
35  */
geom_format_to_pixels(std::string str,double max)36 inline double geom_format_to_pixels(std::string str, double max) {
37   size_t i;
38   if ((i = str.find(':')) != std::string::npos) {
39     std::string a = str.substr(0, i - 1);
40     std::string b = str.substr(i + 1);
41     return math_util::max<double>(0,math_util::percentage_to_value<double>(strtod(a.c_str(), nullptr), max) + strtod(b.c_str(), nullptr));
42   } else {
43     if (str.find('%') != std::string::npos) {
44       return math_util::percentage_to_value<double>(strtod(str.c_str(), nullptr), max);
45     } else {
46       return strtod(str.c_str(), nullptr);
47     }
48   }
49 }
50 
51 class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::property_notify, evt::enter_notify,
52                 evt::leave_notify, evt::motion_notify, evt::destroy_notify, evt::client_message, evt::configure_notify>,
53             public signal_receiver<SIGN_PRIORITY_BAR, signals::eventqueue::start, signals::ui::tick,
54                 signals::ui::shade_window, signals::ui::unshade_window, signals::ui::dim_window
55 #if WITH_XCURSOR
56                 , signals::ui::cursor_change
57 #endif
58 		> {
59  public:
60   using make_type = unique_ptr<bar>;
61   static make_type make(bool only_initialize_values = false);
62 
63   explicit bar(connection&, signal_emitter&, const config&, const logger&, unique_ptr<screen>&&,
64       unique_ptr<tray_manager>&&, unique_ptr<parser>&&, unique_ptr<taskqueue>&&, bool only_initialize_values);
65   ~bar();
66 
67   const bar_settings settings() const;
68 
69   void parse(string&& data, bool force = false);
70 
71   void hide();
72   void show();
73   void toggle();
74 
75  protected:
76   void restack_window();
77   void reconfigue_window();
78   void reconfigure_geom();
79   void reconfigure_pos();
80   void reconfigure_struts();
81   void reconfigure_wm_hints();
82   void broadcast_visibility();
83 
84   void handle(const evt::client_message& evt);
85   void handle(const evt::destroy_notify& evt);
86   void handle(const evt::enter_notify& evt);
87   void handle(const evt::leave_notify& evt);
88   void handle(const evt::motion_notify& evt);
89   void handle(const evt::button_press& evt);
90   void handle(const evt::expose& evt);
91   void handle(const evt::property_notify& evt);
92   void handle(const evt::configure_notify& evt);
93 
94   bool on(const signals::eventqueue::start&);
95   bool on(const signals::ui::unshade_window&);
96   bool on(const signals::ui::shade_window&);
97   bool on(const signals::ui::tick&);
98   bool on(const signals::ui::dim_window&);
99 #if WITH_XCURSOR
100   bool on(const signals::ui::cursor_change&);
101 #endif
102 
103  private:
104   connection& m_connection;
105   signal_emitter& m_sig;
106   const config& m_conf;
107   const logger& m_log;
108   unique_ptr<screen> m_screen;
109   unique_ptr<tray_manager> m_tray;
110   unique_ptr<renderer> m_renderer;
111   unique_ptr<parser> m_parser;
112   unique_ptr<taskqueue> m_taskqueue;
113 
114   bar_settings m_opts{};
115 
116   string m_lastinput{};
117   std::mutex m_mutex{};
118   std::atomic<bool> m_dblclicks{false};
119 
120   mousebtn m_buttonpress_btn{mousebtn::NONE};
121   int m_buttonpress_pos{0};
122 #if WITH_XCURSOR
123   int m_motion_pos{0};
124 #endif
125 
126   event_timer m_buttonpress{0L, 5L};
127   event_timer m_doubleclick{0L, 150L};
128 
129   double m_anim_step{0.0};
130 
131   bool m_visible{true};
132 };
133 
134 POLYBAR_NS_END
135