diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-03-31 14:58:30 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-04-02 11:09:23 -0400 |
commit | 0cbd2a4f492b758f495af9f3ec4899dffe8d57d4 (patch) | |
tree | 24818f1a3c3d8c9ffe6bba61c2caa4685b7aba91 /swaybar | |
parent | Render i3bar blocks (diff) | |
download | sway-0cbd2a4f492b758f495af9f3ec4899dffe8d57d4.tar.gz sway-0cbd2a4f492b758f495af9f3ec4899dffe8d57d4.tar.zst sway-0cbd2a4f492b758f495af9f3ec4899dffe8d57d4.zip |
Send click events for i3bar blocks
Diffstat (limited to 'swaybar')
-rw-r--r-- | swaybar/i3bar.c | 22 | ||||
-rw-r--r-- | swaybar/render.c | 42 |
2 files changed, 52 insertions, 12 deletions
diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index 41b71164..5e98c4aa 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c | |||
@@ -186,3 +186,25 @@ bool i3bar_handle_readable(struct status_line *status) { | |||
186 | state->buffer_index = cur - state->buffer; | 186 | state->buffer_index = cur - state->buffer; |
187 | return redraw; | 187 | return redraw; |
188 | } | 188 | } |
189 | |||
190 | void i3bar_block_send_click(struct status_line *status, | ||
191 | struct i3bar_block *block, int x, int y, uint32_t button) { | ||
192 | wlr_log(L_DEBUG, "block %s clicked", block->name ? block->name : "(nil)"); | ||
193 | if (!block->name || !status->i3bar_state.click_events) { | ||
194 | return; | ||
195 | } | ||
196 | |||
197 | struct json_object *event_json = json_object_new_object(); | ||
198 | json_object_object_add(event_json, "name", | ||
199 | json_object_new_string(block->name)); | ||
200 | if (block->instance) { | ||
201 | json_object_object_add(event_json, "instance", | ||
202 | json_object_new_string(block->instance)); | ||
203 | } | ||
204 | |||
205 | json_object_object_add(event_json, "button", json_object_new_int(button)); | ||
206 | json_object_object_add(event_json, "x", json_object_new_int(x)); | ||
207 | json_object_object_add(event_json, "y", json_object_new_int(y)); | ||
208 | dprintf(status->write_fd, "%s\n", json_object_to_json_string(event_json)); | ||
209 | json_object_put(event_json); | ||
210 | } | ||
diff --git a/swaybar/render.c b/swaybar/render.c index 3ad6d5d7..a5039a2e 100644 --- a/swaybar/render.c +++ b/swaybar/render.c | |||
@@ -89,9 +89,17 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color, | |||
89 | } | 89 | } |
90 | } | 90 | } |
91 | 91 | ||
92 | static void block_hotspot_callback(struct swaybar_output *output, | ||
93 | int x, int y, uint32_t button, void *data) { | ||
94 | struct i3bar_block *block = data; | ||
95 | struct status_line *status = output->bar->status; | ||
96 | i3bar_block_send_click(status, block, x, y, button); | ||
97 | } | ||
98 | |||
92 | static uint32_t render_status_block(cairo_t *cairo, | 99 | static uint32_t render_status_block(cairo_t *cairo, |
93 | struct swaybar_config *config, struct i3bar_block *block, | 100 | struct swaybar_config *config, struct swaybar_output *output, |
94 | double *x, uint32_t height, bool focused, bool edge) { | 101 | struct i3bar_block *block, double *x, |
102 | uint32_t height, bool focused, bool edge) { | ||
95 | static const int margin = 3; | 103 | static const int margin = 3; |
96 | if (!block->full_text || !*block->full_text) { | 104 | if (!block->full_text || !*block->full_text) { |
97 | return 0; | 105 | return 0; |
@@ -139,7 +147,15 @@ static uint32_t render_status_block(cairo_t *cairo, | |||
139 | *x -= margin; | 147 | *x -= margin; |
140 | } | 148 | } |
141 | 149 | ||
142 | // TODO: Create hotspot here | 150 | struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); |
151 | hotspot->x = *x; | ||
152 | hotspot->y = 0; | ||
153 | hotspot->width = width; | ||
154 | hotspot->height = height; | ||
155 | hotspot->callback = block_hotspot_callback; | ||
156 | hotspot->destroy = free; | ||
157 | hotspot->data = block; | ||
158 | wl_list_insert(&output->hotspots, &hotspot->link); | ||
143 | 159 | ||
144 | double pos = *x; | 160 | double pos = *x; |
145 | if (block->background) { | 161 | if (block->background) { |
@@ -208,13 +224,14 @@ static uint32_t render_status_block(cairo_t *cairo, | |||
208 | } | 224 | } |
209 | 225 | ||
210 | static uint32_t render_status_line_i3bar(cairo_t *cairo, | 226 | static uint32_t render_status_line_i3bar(cairo_t *cairo, |
211 | struct swaybar_config *config, struct status_line *status, | 227 | struct swaybar_config *config, struct swaybar_output *output, |
212 | bool focused, double *x, uint32_t width, uint32_t height) { | 228 | struct status_line *status, bool focused, |
229 | double *x, uint32_t width, uint32_t height) { | ||
213 | struct i3bar_block *block; | 230 | struct i3bar_block *block; |
214 | uint32_t max_height = 0; | 231 | uint32_t max_height = 0; |
215 | bool edge = true; | 232 | bool edge = true; |
216 | wl_list_for_each_reverse(block, &status->blocks, link) { | 233 | wl_list_for_each_reverse(block, &status->blocks, link) { |
217 | uint32_t h = render_status_block(cairo, config, | 234 | uint32_t h = render_status_block(cairo, config, output, |
218 | block, x, height, focused, edge); | 235 | block, x, height, focused, edge); |
219 | max_height = h > max_height ? h : max_height; | 236 | max_height = h > max_height ? h : max_height; |
220 | edge = false; | 237 | edge = false; |
@@ -223,8 +240,9 @@ static uint32_t render_status_line_i3bar(cairo_t *cairo, | |||
223 | } | 240 | } |
224 | 241 | ||
225 | static uint32_t render_status_line(cairo_t *cairo, | 242 | static uint32_t render_status_line(cairo_t *cairo, |
226 | struct swaybar_config *config, struct status_line *status, | 243 | struct swaybar_config *config, struct swaybar_output *output, |
227 | bool focused, double *x, uint32_t width, uint32_t height) { | 244 | struct status_line *status, bool focused, |
245 | double *x, uint32_t width, uint32_t height) { | ||
228 | switch (status->protocol) { | 246 | switch (status->protocol) { |
229 | case PROTOCOL_ERROR: | 247 | case PROTOCOL_ERROR: |
230 | return render_status_line_error(cairo, | 248 | return render_status_line_error(cairo, |
@@ -233,8 +251,8 @@ static uint32_t render_status_line(cairo_t *cairo, | |||
233 | return render_status_line_text(cairo, | 251 | return render_status_line_text(cairo, |
234 | config, status->text, focused, x, width, height); | 252 | config, status->text, focused, x, width, height); |
235 | case PROTOCOL_I3BAR: | 253 | case PROTOCOL_I3BAR: |
236 | return render_status_line_i3bar(cairo, | 254 | return render_status_line_i3bar(cairo, config, output, status, |
237 | config, status, focused, x, width, height); | 255 | focused, x, width, height); |
238 | case PROTOCOL_UNDEF: | 256 | case PROTOCOL_UNDEF: |
239 | return 0; | 257 | return 0; |
240 | } | 258 | } |
@@ -344,8 +362,8 @@ static uint32_t render_workspace_button(cairo_t *cairo, | |||
344 | struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); | 362 | struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); |
345 | hotspot->x = *x; | 363 | hotspot->x = *x; |
346 | hotspot->y = 0; | 364 | hotspot->y = 0; |
347 | hotspot->height = height; | ||
348 | hotspot->width = width; | 365 | hotspot->width = width; |
366 | hotspot->height = height; | ||
349 | hotspot->callback = workspace_hotspot_callback; | 367 | hotspot->callback = workspace_hotspot_callback; |
350 | hotspot->destroy = free; | 368 | hotspot->destroy = free; |
351 | hotspot->data = strdup(name); | 369 | hotspot->data = strdup(name); |
@@ -391,7 +409,7 @@ static uint32_t render_to_cairo(cairo_t *cairo, | |||
391 | } | 409 | } |
392 | x = output->width; | 410 | x = output->width; |
393 | if (bar->status) { | 411 | if (bar->status) { |
394 | uint32_t h = render_status_line(cairo, config, bar->status, | 412 | uint32_t h = render_status_line(cairo, config, output, bar->status, |
395 | output->focused, &x, output->width, output->height); | 413 | output->focused, &x, output->width, output->height); |
396 | max_height = h > max_height ? h : max_height; | 414 | max_height = h > max_height ? h : max_height; |
397 | } | 415 | } |