1 #pragma once
2 
3 #include <xcb/xcb.h>
4 
5 #include <string>
6 #include <unordered_map>
7 
8 #include "common.hpp"
9 #include "utils/color.hpp"
10 
11 POLYBAR_NS
12 
13 // fwd {{{
14 struct randr_output;
15 using monitor_t = shared_ptr<randr_output>;
16 
17 namespace drawtypes {
18   class label;
19 }
20 
21 using label_t = shared_ptr<drawtypes::label>;
22 // }}}
23 
24 struct enum_hash {
25   template <typename T>
operator ()enum_hash26   inline typename std::enable_if<std::is_enum<T>::value, size_t>::type operator()(T const value) const {
27     return static_cast<size_t>(value);
28   }
29 };
30 
31 enum class edge { NONE = 0, TOP, BOTTOM, LEFT, RIGHT, ALL };
32 
33 enum class alignment { NONE = 0, LEFT, CENTER, RIGHT };
34 
35 enum class attribute { NONE = 0, UNDERLINE, OVERLINE };
36 
37 enum class syntaxtag {
38   NONE = 0,
39   A,  // mouse action
40   B,  // background color
41   F,  // foreground color
42   T,  // font index
43   O,  // pixel offset
44   R,  // flip colors
45   o,  // overline color
46   u,  // underline color
47   P,  // Polybar control tag
48 };
49 
50 /**
51  * Values for polybar control tags
52  *
53  * %{P...} tags are tags for internal polybar control commands, they are not
54  * part of the public interface
55  */
56 enum class controltag {
57   NONE = 0,
58   R,  // Reset all open tags (B, F, T, o, u). Used at module edges
59 };
60 
61 enum class mousebtn { NONE = 0, LEFT, MIDDLE, RIGHT, SCROLL_UP, SCROLL_DOWN, DOUBLE_LEFT, DOUBLE_MIDDLE, DOUBLE_RIGHT };
62 
63 enum class strut {
64   LEFT = 0,
65   RIGHT,
66   TOP,
67   BOTTOM,
68   LEFT_START_Y,
69   LEFT_END_Y,
70   RIGHT_START_Y,
71   RIGHT_END_Y,
72   TOP_START_X,
73   TOP_END_X,
74   BOTTOM_START_X,
75   BOTTOM_END_X,
76 };
77 
78 struct position {
79   int x{0};
80   int y{0};
81 };
82 
83 struct size {
84   unsigned int w{1U};
85   unsigned int h{1U};
86 };
87 
88 struct side_values {
89   unsigned int left{0U};
90   unsigned int right{0U};
91 };
92 
93 struct edge_values {
94   unsigned int left{0U};
95   unsigned int right{0U};
96   unsigned int top{0U};
97   unsigned int bottom{0U};
98 };
99 
100 struct radius {
101   double top{0.0};
102   double bottom{0.0};
103 
operator boolradius104   operator bool() const {
105     return top != 0.0 || bottom != 0.0;
106   }
107 };
108 
109 struct border_settings {
110   rgba color{0xFF000000};
111   unsigned int size{0U};
112 };
113 
114 struct line_settings {
115   rgba color{0xFF000000};
116   unsigned int size{0U};
117 };
118 
119 struct action {
120   mousebtn button{mousebtn::NONE};
121   string command{};
122 };
123 
124 struct action_block : public action {
125   alignment align{alignment::NONE};
126   double start_x{0.0};
127   double end_x{0.0};
128   bool active{true};
129 
widthaction_block130   unsigned int width() const {
131     return static_cast<unsigned int>(end_x - start_x + 0.5);
132   }
133 
testaction_block134   bool test(int point) const {
135     return static_cast<int>(start_x) <= point && static_cast<int>(end_x) > point;
136   }
137 };
138 
139 struct bar_settings {
140   explicit bar_settings() = default;
141   bar_settings(const bar_settings& other) = default;
142 
143   xcb_window_t window{XCB_NONE};
144   monitor_t monitor{};
145   bool monitor_strict{false};
146   bool monitor_exact{true};
147   edge origin{edge::TOP};
148   struct size size {
149     1U, 1U
150   };
151   position pos{0, 0};
152   position offset{0, 0};
153   side_values padding{0U, 0U};
154   side_values margin{0U, 0U};
155   side_values module_margin{0U, 0U};
156   edge_values strut{0U, 0U, 0U, 0U};
157 
158   rgba background{0xFF000000};
159   rgba foreground{0xFFFFFFFF};
160   vector<rgba> background_steps;
161 
162   line_settings underline{};
163   line_settings overline{};
164 
165   std::unordered_map<edge, border_settings, enum_hash> borders{};
166 
167   struct radius radius {};
168   int spacing{0};
169   label_t separator{};
170 
171   string wmname{};
172   string locale{};
173 
174   bool override_redirect{false};
175 
176   string cursor{};
177   string cursor_click{};
178   string cursor_scroll{};
179 
180   vector<action> actions{};
181 
182   bool dimmed{false};
183   double dimvalue{1.0};
184 
185   bool shaded{false};
186   struct size shade_size {
187     1U, 1U
188   };
189   position shade_pos{1U, 1U};
190 
inner_areabar_settings191   const xcb_rectangle_t inner_area(bool abspos = false) const {
192     xcb_rectangle_t rect = this->outer_area(abspos);
193 
194     if (borders.find(edge::TOP) != borders.end()) {
195       rect.y += borders.at(edge::TOP).size;
196       rect.height -= borders.at(edge::TOP).size;
197     }
198     if (borders.find(edge::BOTTOM) != borders.end()) {
199       rect.height -= borders.at(edge::BOTTOM).size;
200     }
201     if (borders.find(edge::LEFT) != borders.end()) {
202       rect.x += borders.at(edge::LEFT).size;
203       rect.width -= borders.at(edge::LEFT).size;
204     }
205     if (borders.find(edge::RIGHT) != borders.end()) {
206       rect.width -= borders.at(edge::RIGHT).size;
207     }
208     return rect;
209   }
210 
outer_areabar_settings211   const xcb_rectangle_t outer_area(bool abspos = false) const {
212     xcb_rectangle_t rect{0, 0, 0, 0};
213     rect.width += size.w;
214     rect.height += size.h;
215 
216     if (abspos) {
217       rect.x = pos.x;
218       rect.y = pos.y;
219     }
220 
221     return rect;
222   }
223 };
224 
225 struct event_timer {
226   xcb_timestamp_t event{0L};
227   xcb_timestamp_t offset{1L};
228 
allowevent_timer229   bool allow(xcb_timestamp_t time) {
230     bool pass = time >= event + offset;
231     event = time;
232     return pass;
233   };
234 
denyevent_timer235   bool deny(xcb_timestamp_t time) {
236     return !allow(time);
237   };
238 };
239 
240 enum class output_policy {
241   REDIRECTED,
242   IGNORED,
243 };
244 
245 POLYBAR_NS_END
246