1 #pragma once
2 
3 #include <cairo/cairo.h>
4 
5 #include <bitset>
6 #include <memory>
7 
8 #include "cairo/fwd.hpp"
9 #include "common.hpp"
10 #include "components/types.hpp"
11 #include "events/signal_fwd.hpp"
12 #include "events/signal_receiver.hpp"
13 #include "x11/extensions/fwd.hpp"
14 #include "x11/types.hpp"
15 
16 POLYBAR_NS
17 
18 // fwd {{{
19 class connection;
20 class config;
21 class logger;
22 class background_manager;
23 class bg_slice;
24 // }}}
25 
26 using std::map;
27 
28 struct alignment_block {
29   cairo_pattern_t* pattern;
30   double x;
31   double y;
32 };
33 
34 class renderer
35     : public signal_receiver<SIGN_PRIORITY_RENDERER, signals::ui::request_snapshot, signals::parser::change_background,
36           signals::parser::change_foreground, signals::parser::change_underline, signals::parser::change_overline,
37           signals::parser::change_font, signals::parser::change_alignment, signals::parser::reverse_colors,
38           signals::parser::offset_pixel, signals::parser::attribute_set, signals::parser::attribute_unset,
39           signals::parser::attribute_toggle, signals::parser::action_begin, signals::parser::action_end,
40           signals::parser::text, signals::parser::control> {
41  public:
42   using make_type = unique_ptr<renderer>;
43   static make_type make(const bar_settings& bar);
44 
45   explicit renderer(connection& conn, signal_emitter& sig, const config&, const logger& logger, const bar_settings& bar,
46       background_manager& background_manager);
47   ~renderer();
48 
49   xcb_window_t window() const;
50   const vector<action_block> actions() const;
51 
52   void begin(xcb_rectangle_t rect);
53   void end();
54   void flush();
55 
56 #if 0
57   void reserve_space(edge side, unsigned int w);
58 #endif
59   void fill_background();
60   void fill_overline(double x, double w);
61   void fill_underline(double x, double w);
62   void fill_borders();
63   void draw_text(const string& contents);
64 
65  protected:
66   double block_x(alignment a) const;
67   double block_y(alignment a) const;
68   double block_w(alignment a) const;
69   double block_h(alignment a) const;
70 
71   void flush(alignment a);
72   void highlight_clickable_areas();
73 
74   bool on(const signals::ui::request_snapshot& evt);
75   bool on(const signals::parser::change_background& evt);
76   bool on(const signals::parser::change_foreground& evt);
77   bool on(const signals::parser::change_underline& evt);
78   bool on(const signals::parser::change_overline& evt);
79   bool on(const signals::parser::change_font& evt);
80   bool on(const signals::parser::change_alignment& evt);
81   bool on(const signals::parser::reverse_colors&);
82   bool on(const signals::parser::offset_pixel& evt);
83   bool on(const signals::parser::attribute_set& evt);
84   bool on(const signals::parser::attribute_unset& evt);
85   bool on(const signals::parser::attribute_toggle& evt);
86   bool on(const signals::parser::action_begin& evt);
87   bool on(const signals::parser::action_end& evt);
88   bool on(const signals::parser::text& evt);
89   bool on(const signals::parser::control& evt);
90 
91  protected:
92   struct reserve_area {
93     edge side{edge::NONE};
94     unsigned int size{0U};
95   };
96 
97  private:
98   connection& m_connection;
99   signal_emitter& m_sig;
100   const config& m_conf;
101   const logger& m_log;
102   const bar_settings& m_bar;
103   std::shared_ptr<bg_slice> m_background;
104 
105   int m_depth{32};
106   xcb_window_t m_window;
107   xcb_colormap_t m_colormap;
108   xcb_visualtype_t* m_visual;
109   xcb_gcontext_t m_gcontext;
110   xcb_pixmap_t m_pixmap;
111 
112   xcb_rectangle_t m_rect{0, 0, 0U, 0U};
113   reserve_area m_cleararea{};
114 
115   // bool m_autosize{false};
116 
117   unique_ptr<cairo::context> m_context;
118   unique_ptr<cairo::xcb_surface> m_surface;
119   map<alignment, alignment_block> m_blocks;
120   cairo_pattern_t* m_cornermask{};
121 
122   cairo_operator_t m_comp_bg{CAIRO_OPERATOR_SOURCE};
123   cairo_operator_t m_comp_fg{CAIRO_OPERATOR_OVER};
124   cairo_operator_t m_comp_ol{CAIRO_OPERATOR_OVER};
125   cairo_operator_t m_comp_ul{CAIRO_OPERATOR_OVER};
126   cairo_operator_t m_comp_border{CAIRO_OPERATOR_OVER};
127   bool m_pseudo_transparency{false};
128 
129   alignment m_align;
130   std::bitset<3> m_attr;
131   int m_font{0};
132   rgba m_bg{};
133   rgba m_fg{};
134   rgba m_ol{};
135   rgba m_ul{};
136   vector<action_block> m_actions;
137 
138   bool m_fixedcenter;
139   string m_snapshot_dst;
140 };
141 
142 POLYBAR_NS_END
143