2018-03-31 02:42:59 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2018-09-19 09:37:24 +00:00
|
|
|
#include <assert.h>
|
2019-01-10 17:43:10 +00:00
|
|
|
#include <linux/input-event-codes.h>
|
2018-03-29 14:59:33 +00:00
|
|
|
#include <limits.h>
|
2016-01-23 19:55:01 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2021-05-07 20:57:51 +00:00
|
|
|
#include "cairo_util.h"
|
2018-03-29 03:04:20 +00:00
|
|
|
#include "pango.h"
|
|
|
|
#include "pool-buffer.h"
|
|
|
|
#include "swaybar/bar.h"
|
2016-09-01 12:18:37 +00:00
|
|
|
#include "swaybar/config.h"
|
2018-09-24 20:56:35 +00:00
|
|
|
#include "swaybar/i3bar.h"
|
2018-03-31 02:42:59 +00:00
|
|
|
#include "swaybar/ipc.h"
|
2016-09-01 12:18:37 +00:00
|
|
|
#include "swaybar/render.h"
|
2018-03-29 19:16:12 +00:00
|
|
|
#include "swaybar/status_line.h"
|
2021-09-15 10:07:07 +00:00
|
|
|
#include "log.h"
|
2018-10-28 10:25:47 +00:00
|
|
|
#if HAVE_TRAY
|
|
|
|
#include "swaybar/tray/tray.h"
|
|
|
|
#endif
|
2018-03-29 03:04:20 +00:00
|
|
|
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
2016-01-23 19:55:01 +00:00
|
|
|
|
2018-04-04 01:06:28 +00:00
|
|
|
static const int WS_HORIZONTAL_PADDING = 5;
|
|
|
|
static const double WS_VERTICAL_PADDING = 1.5;
|
|
|
|
static const double BORDER_WIDTH = 1;
|
2018-03-29 14:59:33 +00:00
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
struct render_context {
|
|
|
|
cairo_t *cairo;
|
|
|
|
struct swaybar_output *output;
|
|
|
|
cairo_font_options_t *textaa_sharp;
|
|
|
|
cairo_font_options_t *textaa_safe;
|
|
|
|
uint32_t background_color;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void choose_text_aa_mode(struct render_context *ctx, uint32_t fontcolor) {
|
|
|
|
uint32_t salpha = fontcolor & 0xFF;
|
|
|
|
uint32_t balpha = ctx->background_color & 0xFF;
|
|
|
|
|
|
|
|
// Subpixel antialiasing requires blend be done in cairo, not compositor
|
|
|
|
cairo_font_options_t *fo = salpha == balpha ?
|
|
|
|
ctx->textaa_sharp : ctx->textaa_safe;
|
|
|
|
cairo_set_font_options(ctx->cairo, fo);
|
|
|
|
|
|
|
|
// Color emojis, being semitransparent bitmaps, are leaky with 'SOURCE'
|
|
|
|
cairo_operator_t op = salpha == 0xFF ?
|
|
|
|
CAIRO_OPERATOR_OVER : CAIRO_OPERATOR_SOURCE;
|
|
|
|
cairo_set_operator(ctx->cairo, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t render_status_line_error(struct render_context *ctx, double *x) {
|
|
|
|
struct swaybar_output *output = ctx->output;
|
2018-09-28 13:09:33 +00:00
|
|
|
const char *error = output->bar->status->text;
|
2018-03-31 18:39:18 +00:00
|
|
|
if (!error) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t height = output->height;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_t *cairo = ctx->cairo;
|
2018-03-31 18:39:18 +00:00
|
|
|
cairo_set_source_u32(cairo, 0xFF0000FF);
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int margin = 3;
|
|
|
|
double ws_vertical_padding = output->bar->config->status_padding;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2022-07-01 10:23:04 +00:00
|
|
|
PangoFontDescription *font = output->bar->config->font_description;
|
2018-03-31 18:39:18 +00:00
|
|
|
int text_width, text_height;
|
2018-09-28 13:09:33 +00:00
|
|
|
get_text_size(cairo, font, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, false, "%s", error);
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-31 18:39:18 +00:00
|
|
|
uint32_t ideal_height = text_height + ws_vertical_padding * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-01-10 01:58:00 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
2018-04-08 17:49:11 +00:00
|
|
|
return ideal_surface_height;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
*x -= text_width + margin;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-31 18:39:18 +00:00
|
|
|
double text_y = height / 2.0 - text_height / 2.0;
|
|
|
|
cairo_move_to(cairo, *x, (int)floor(text_y));
|
2020-08-14 17:00:11 +00:00
|
|
|
choose_text_aa_mode(ctx, 0xFF0000FF);
|
2021-09-13 12:07:27 +00:00
|
|
|
render_text(cairo, font, 1, false, "%s", error);
|
2018-03-31 18:39:18 +00:00
|
|
|
*x -= margin;
|
2018-09-28 13:09:33 +00:00
|
|
|
return output->height;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
static uint32_t render_status_line_text(struct render_context *ctx, double *x) {
|
|
|
|
struct swaybar_output *output = ctx->output;
|
2018-09-28 13:09:33 +00:00
|
|
|
const char *text = output->bar->status->text;
|
2018-03-31 18:39:18 +00:00
|
|
|
if (!text) {
|
2018-03-29 19:16:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_t *cairo = ctx->cairo;
|
2018-09-28 13:09:33 +00:00
|
|
|
struct swaybar_config *config = output->bar->config;
|
2020-08-14 17:00:11 +00:00
|
|
|
uint32_t fontcolor = output->focused ?
|
|
|
|
config->colors.focused_statusline : config->colors.statusline;
|
|
|
|
cairo_set_source_u32(cairo, fontcolor);
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-29 19:16:12 +00:00
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, config->pango_markup, "%s", text);
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
double ws_vertical_padding = config->status_padding;
|
|
|
|
int margin = 3;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-29 19:16:12 +00:00
|
|
|
uint32_t ideal_height = text_height + ws_vertical_padding * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-01-10 01:58:00 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
2018-04-08 17:49:11 +00:00
|
|
|
return ideal_surface_height;
|
2018-03-29 19:16:12 +00:00
|
|
|
}
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-31 18:39:18 +00:00
|
|
|
*x -= text_width + margin;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t height = output->height;
|
2018-03-29 19:16:12 +00:00
|
|
|
double text_y = height / 2.0 - text_height / 2.0;
|
2018-03-31 18:39:18 +00:00
|
|
|
cairo_move_to(cairo, *x, (int)floor(text_y));
|
2020-08-14 17:00:11 +00:00
|
|
|
choose_text_aa_mode(ctx, fontcolor);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, config->font_description, 1, config->pango_markup, "%s", text);
|
2018-03-31 18:39:18 +00:00
|
|
|
*x -= margin;
|
2018-09-28 13:09:33 +00:00
|
|
|
return output->height;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 01:28:30 +00:00
|
|
|
static void render_sharp_rectangle(cairo_t *cairo, uint32_t color,
|
2018-03-31 18:39:18 +00:00
|
|
|
double x, double y, double width, double height) {
|
2019-01-10 01:28:30 +00:00
|
|
|
cairo_save(cairo);
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2018-03-31 18:39:18 +00:00
|
|
|
cairo_set_source_u32(cairo, color);
|
2019-01-10 01:28:30 +00:00
|
|
|
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_NONE);
|
|
|
|
cairo_rectangle(cairo, x, y, width, height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_restore(cairo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void render_sharp_line(cairo_t *cairo, uint32_t color,
|
|
|
|
double x, double y, double width, double height) {
|
2018-03-31 18:39:18 +00:00
|
|
|
if (width > 1 && height > 1) {
|
2019-01-10 01:28:30 +00:00
|
|
|
render_sharp_rectangle(cairo, color, x, y, width, height);
|
2018-03-31 18:39:18 +00:00
|
|
|
} else {
|
2019-01-10 01:28:30 +00:00
|
|
|
cairo_save(cairo);
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2019-01-10 01:28:30 +00:00
|
|
|
cairo_set_source_u32(cairo, color);
|
|
|
|
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_NONE);
|
2018-03-31 18:39:18 +00:00
|
|
|
if (width == 1) {
|
|
|
|
x += 0.5;
|
|
|
|
height += y;
|
|
|
|
width = x;
|
|
|
|
}
|
|
|
|
if (height == 1) {
|
|
|
|
y += 0.5;
|
|
|
|
width += x;
|
|
|
|
height = y;
|
|
|
|
}
|
|
|
|
cairo_move_to(cairo, x, y);
|
|
|
|
cairo_set_line_width(cairo, 1.0);
|
|
|
|
cairo_line_to(cairo, width, height);
|
|
|
|
cairo_stroke(cairo);
|
2019-01-10 01:28:30 +00:00
|
|
|
cairo_restore(cairo);
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 17:43:10 +00:00
|
|
|
static enum hotspot_event_handling block_hotspot_callback(
|
|
|
|
struct swaybar_output *output, struct swaybar_hotspot *hotspot,
|
swaybar: Make hotspots block bar release bindings
The previous commit prioritized hotspots before bar bindings for press events,
which matches i3's behaviour. However, since hotspots don't need to do any
processing on release events, those were not handled, and simply fell through
to `bindsym --release` bar bindings (if any).
This is counter-intuitive, and doesn't match i3's behaviour. Instead in case
a hotspot handles the press event, it should also handle the release event,
doing nothing, but blocking the event from triggering a --release bar binding.
E.g., in Sway, without this commit, this config. shows a text on tray clicks:
bar {
# ...
bindsym --release button1 exec swaynag -m I_got_the_release_event.
}
But the same configuration in i3 (with i3-nagbar) doesn't show the text.
Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
2021-09-18 20:21:22 +00:00
|
|
|
double x, double y, uint32_t button, bool released, void *data) {
|
2018-03-31 18:58:30 +00:00
|
|
|
struct i3bar_block *block = data;
|
|
|
|
struct status_line *status = output->bar->status;
|
2020-02-01 17:08:00 +00:00
|
|
|
return i3bar_block_send_click(status, block, x, y,
|
2021-09-09 23:06:17 +00:00
|
|
|
x - (double)hotspot->x,
|
|
|
|
y - (double)hotspot->y,
|
|
|
|
(double)hotspot->width,
|
|
|
|
(double)hotspot->height,
|
swaybar: Make hotspots block bar release bindings
The previous commit prioritized hotspots before bar bindings for press events,
which matches i3's behaviour. However, since hotspots don't need to do any
processing on release events, those were not handled, and simply fell through
to `bindsym --release` bar bindings (if any).
This is counter-intuitive, and doesn't match i3's behaviour. Instead in case
a hotspot handles the press event, it should also handle the release event,
doing nothing, but blocking the event from triggering a --release bar binding.
E.g., in Sway, without this commit, this config. shows a text on tray clicks:
bar {
# ...
bindsym --release button1 exec swaynag -m I_got_the_release_event.
}
But the same configuration in i3 (with i3-nagbar) doesn't show the text.
Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
2021-09-18 20:21:22 +00:00
|
|
|
output->scale, button, released);
|
2018-03-31 18:58:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 07:28:28 +00:00
|
|
|
static void i3bar_block_unref_callback(void *data) {
|
|
|
|
i3bar_block_unref(data);
|
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
static uint32_t render_status_block(struct render_context *ctx,
|
|
|
|
struct i3bar_block *block, double *x, bool edge, bool use_short_text) {
|
2018-03-31 18:39:18 +00:00
|
|
|
if (!block->full_text || !*block->full_text) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-22 21:18:21 +00:00
|
|
|
char* text = block->full_text;
|
|
|
|
if (use_short_text && block->short_text && *block->short_text) {
|
|
|
|
text = block->short_text;
|
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_t *cairo = ctx->cairo;
|
|
|
|
struct swaybar_output *output = ctx->output;
|
2018-09-17 12:31:24 +00:00
|
|
|
struct swaybar_config *config = output->bar->config;
|
2018-03-31 18:39:18 +00:00
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL, 1,
|
2021-09-09 23:06:17 +00:00
|
|
|
block->markup, "%s", text);
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int margin = 3;
|
|
|
|
double ws_vertical_padding = config->status_padding;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-31 18:39:18 +00:00
|
|
|
int width = text_width;
|
2019-01-31 09:45:12 +00:00
|
|
|
if (block->min_width_str) {
|
|
|
|
int w;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &w, NULL, NULL, 1, block->markup,
|
2021-09-09 23:06:17 +00:00
|
|
|
"%s", block->min_width_str);
|
2019-01-31 09:45:12 +00:00
|
|
|
block->min_width = w;
|
|
|
|
}
|
2018-03-31 18:39:18 +00:00
|
|
|
if (width < block->min_width) {
|
|
|
|
width = block->min_width;
|
|
|
|
}
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-31 18:39:18 +00:00
|
|
|
double block_width = width;
|
|
|
|
uint32_t ideal_height = text_height + ws_vertical_padding * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-01-10 01:58:00 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
2018-04-08 17:49:11 +00:00
|
|
|
return ideal_surface_height;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*x -= width;
|
2021-09-15 10:07:07 +00:00
|
|
|
if ((block->border_set || block->urgent) && block->border_left > 0) {
|
2021-09-09 23:06:17 +00:00
|
|
|
*x -= (block->border_left + margin);
|
|
|
|
block_width += block->border_left + margin;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
2021-09-15 10:07:07 +00:00
|
|
|
if ((block->border_set || block->urgent) && block->border_right > 0) {
|
2021-09-09 23:06:17 +00:00
|
|
|
*x -= (block->border_right + margin);
|
|
|
|
block_width += block->border_right + margin;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 06:08:08 +00:00
|
|
|
int sep_width, sep_height;
|
2018-12-10 17:25:17 +00:00
|
|
|
int sep_block_width = block->separator_block_width;
|
2018-03-31 18:39:18 +00:00
|
|
|
if (!edge) {
|
|
|
|
if (config->sep_symbol) {
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &sep_width, &sep_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, false, "%s", config->sep_symbol);
|
2018-04-11 02:18:54 +00:00
|
|
|
uint32_t _ideal_height = sep_height + ws_vertical_padding * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t _ideal_surface_height = _ideal_height;
|
2019-01-10 01:58:00 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < _ideal_surface_height) {
|
2018-04-10 22:34:21 +00:00
|
|
|
return _ideal_surface_height;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
2020-09-20 15:26:18 +00:00
|
|
|
if (block->separator && sep_width > sep_block_width) {
|
2018-12-10 17:25:17 +00:00
|
|
|
sep_block_width = sep_width + margin * 2;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-10 17:25:17 +00:00
|
|
|
*x -= sep_block_width;
|
2019-01-11 05:12:24 +00:00
|
|
|
} else if (config->status_edge_padding) {
|
2021-09-09 23:06:17 +00:00
|
|
|
*x -= config->status_edge_padding;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t height = output->height;
|
2018-09-17 13:10:57 +00:00
|
|
|
if (output->bar->status->click_events) {
|
2018-09-17 12:31:24 +00:00
|
|
|
struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
|
|
|
|
hotspot->x = *x;
|
|
|
|
hotspot->y = 0;
|
|
|
|
hotspot->width = width;
|
|
|
|
hotspot->height = height;
|
|
|
|
hotspot->callback = block_hotspot_callback;
|
|
|
|
hotspot->destroy = i3bar_block_unref_callback;
|
|
|
|
hotspot->data = block;
|
|
|
|
block->ref_count++;
|
|
|
|
wl_list_insert(&output->hotspots, &hotspot->link);
|
|
|
|
}
|
2018-03-31 18:39:18 +00:00
|
|
|
|
2019-01-10 01:28:30 +00:00
|
|
|
double x_pos = *x;
|
2019-01-11 04:43:45 +00:00
|
|
|
double y_pos = ws_vertical_padding;
|
|
|
|
double render_height = height - ws_vertical_padding * 2;
|
2018-12-12 04:51:29 +00:00
|
|
|
|
|
|
|
uint32_t bg_color = block->urgent
|
|
|
|
? config->colors.urgent_workspace.background : block->background;
|
|
|
|
if (bg_color) {
|
2019-01-10 01:28:30 +00:00
|
|
|
render_sharp_rectangle(cairo, bg_color, x_pos, y_pos,
|
|
|
|
block_width, render_height);
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx->background_color = bg_color;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 04:51:29 +00:00
|
|
|
uint32_t border_color = block->urgent
|
|
|
|
? config->colors.urgent_workspace.border : block->border;
|
2021-09-15 10:07:07 +00:00
|
|
|
if (block->border_set || block->urgent) {
|
|
|
|
if (block->border_top > 0) {
|
|
|
|
render_sharp_line(cairo, border_color, x_pos, y_pos,
|
|
|
|
block_width, block->border_top);
|
|
|
|
}
|
|
|
|
if (block->border_bottom > 0) {
|
|
|
|
render_sharp_line(cairo, border_color, x_pos,
|
|
|
|
y_pos + render_height - block->border_bottom,
|
|
|
|
block_width, block->border_bottom);
|
|
|
|
}
|
|
|
|
if (block->border_left > 0) {
|
|
|
|
render_sharp_line(cairo, border_color, x_pos, y_pos,
|
|
|
|
block->border_left, render_height);
|
|
|
|
}
|
2021-09-09 23:06:17 +00:00
|
|
|
x_pos += block->border_left + margin;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double offset = 0;
|
2022-12-30 09:35:25 +00:00
|
|
|
if (strncmp(block->align, "left", 4) == 0) {
|
2019-01-10 01:28:30 +00:00
|
|
|
offset = x_pos;
|
2018-03-31 18:39:18 +00:00
|
|
|
} else if (strncmp(block->align, "right", 5) == 0) {
|
2019-01-10 01:28:30 +00:00
|
|
|
offset = x_pos + width - text_width;
|
2018-03-31 18:39:18 +00:00
|
|
|
} else if (strncmp(block->align, "center", 6) == 0) {
|
2019-01-10 01:28:30 +00:00
|
|
|
offset = x_pos + (width - text_width) / 2;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
2019-09-09 11:05:15 +00:00
|
|
|
double text_y = height / 2.0 - text_height / 2.0;
|
|
|
|
cairo_move_to(cairo, offset, (int)floor(text_y));
|
2021-01-11 07:45:50 +00:00
|
|
|
uint32_t color = output->focused ?
|
|
|
|
config->colors.focused_statusline : config->colors.statusline;
|
|
|
|
color = block->color_set ? block->color : color;
|
2018-12-12 04:51:29 +00:00
|
|
|
color = block->urgent ? config->colors.urgent_workspace.text : color;
|
2018-03-31 18:39:18 +00:00
|
|
|
cairo_set_source_u32(cairo, color);
|
2020-08-14 17:00:11 +00:00
|
|
|
choose_text_aa_mode(ctx, color);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, config->font_description, 1, block->markup, "%s", text);
|
2019-01-10 01:28:30 +00:00
|
|
|
x_pos += width;
|
2018-03-31 18:39:18 +00:00
|
|
|
|
2021-09-15 10:07:07 +00:00
|
|
|
if (block->border_set || block->urgent) {
|
2019-01-10 01:28:30 +00:00
|
|
|
x_pos += margin;
|
2021-09-15 10:07:07 +00:00
|
|
|
if (block->border_right > 0) {
|
|
|
|
render_sharp_line(cairo, border_color, x_pos, y_pos,
|
|
|
|
block->border_right, render_height);
|
|
|
|
}
|
2021-09-09 23:06:17 +00:00
|
|
|
x_pos += block->border_right;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!edge && block->separator) {
|
2018-09-28 13:09:33 +00:00
|
|
|
if (output->focused) {
|
2020-08-14 17:00:11 +00:00
|
|
|
color = config->colors.focused_separator;
|
2018-03-31 18:39:18 +00:00
|
|
|
} else {
|
2020-08-14 17:00:11 +00:00
|
|
|
color = config->colors.separator;
|
2018-03-31 18:39:18 +00:00
|
|
|
}
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_set_source_u32(cairo, color);
|
2018-03-31 18:39:18 +00:00
|
|
|
if (config->sep_symbol) {
|
2019-01-10 01:28:30 +00:00
|
|
|
offset = x_pos + (sep_block_width - sep_width) / 2;
|
2019-09-09 11:05:15 +00:00
|
|
|
double sep_y = height / 2.0 - sep_height / 2.0;
|
|
|
|
cairo_move_to(cairo, offset, (int)floor(sep_y));
|
2020-08-14 17:00:11 +00:00
|
|
|
choose_text_aa_mode(ctx, color);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, config->font_description, 1, false,
|
2018-03-31 18:39:18 +00:00
|
|
|
"%s", config->sep_symbol);
|
|
|
|
} else {
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2018-03-31 18:39:18 +00:00
|
|
|
cairo_set_line_width(cairo, 1);
|
2019-01-10 01:28:30 +00:00
|
|
|
cairo_move_to(cairo, x_pos + sep_block_width / 2, margin);
|
|
|
|
cairo_line_to(cairo, x_pos + sep_block_width / 2, height - margin);
|
2018-03-31 18:39:18 +00:00
|
|
|
cairo_stroke(cairo);
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 13:09:33 +00:00
|
|
|
return output->height;
|
2018-03-29 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 21:18:21 +00:00
|
|
|
static void predict_status_block_pos(cairo_t *cairo,
|
|
|
|
struct swaybar_output *output, struct i3bar_block *block, double *x,
|
|
|
|
bool edge) {
|
|
|
|
if (!block->full_text || !*block->full_text) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct swaybar_config *config = output->bar->config;
|
|
|
|
|
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL, 1,
|
2021-09-09 23:06:17 +00:00
|
|
|
block->markup, "%s", block->full_text);
|
2019-03-22 21:18:21 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int margin = 3;
|
|
|
|
double ws_vertical_padding = config->status_padding;
|
2019-03-22 21:18:21 +00:00
|
|
|
|
|
|
|
int width = text_width;
|
|
|
|
|
|
|
|
if (block->min_width_str) {
|
|
|
|
int w;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &w, NULL, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, block->markup, "%s", block->min_width_str);
|
2019-03-22 21:18:21 +00:00
|
|
|
block->min_width = w;
|
|
|
|
}
|
|
|
|
if (width < block->min_width) {
|
|
|
|
width = block->min_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ideal_height = text_height + ws_vertical_padding * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-03-22 21:18:21 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*x -= width;
|
2021-09-15 10:07:07 +00:00
|
|
|
if ((block->border_set || block->urgent) && block->border_left > 0) {
|
2021-09-09 23:06:17 +00:00
|
|
|
*x -= (block->border_left + margin);
|
2019-03-22 21:18:21 +00:00
|
|
|
}
|
2021-09-15 10:07:07 +00:00
|
|
|
if ((block->border_set || block->urgent) && block->border_right > 0) {
|
2021-09-09 23:06:17 +00:00
|
|
|
*x -= (block->border_right + margin);
|
2019-03-22 21:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sep_width, sep_height;
|
|
|
|
int sep_block_width = block->separator_block_width;
|
|
|
|
if (!edge) {
|
|
|
|
if (config->sep_symbol) {
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &sep_width, &sep_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, false, "%s", config->sep_symbol);
|
2019-03-22 21:18:21 +00:00
|
|
|
uint32_t _ideal_height = sep_height + ws_vertical_padding * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t _ideal_surface_height = _ideal_height;
|
2019-03-22 21:18:21 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < _ideal_surface_height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sep_width > sep_block_width) {
|
|
|
|
sep_block_width = sep_width + margin * 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*x -= sep_block_width;
|
|
|
|
} else if (config->status_edge_padding) {
|
2021-09-09 23:06:17 +00:00
|
|
|
*x -= config->status_edge_padding;
|
2019-03-22 21:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static double predict_status_line_pos(cairo_t *cairo,
|
|
|
|
struct swaybar_output *output, double x) {
|
2021-09-09 23:06:17 +00:00
|
|
|
bool edge = x == output->width;
|
2019-03-22 21:18:21 +00:00
|
|
|
struct i3bar_block *block;
|
|
|
|
wl_list_for_each(block, &output->bar->status->blocks, link) {
|
|
|
|
predict_status_block_pos(cairo, output, block, &x, edge);
|
|
|
|
edge = false;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t predict_workspace_button_length(cairo_t *cairo,
|
|
|
|
struct swaybar_output *output,
|
|
|
|
struct swaybar_workspace *ws) {
|
|
|
|
struct swaybar_config *config = output->bar->config;
|
|
|
|
|
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL, 1,
|
2021-09-09 23:06:17 +00:00
|
|
|
config->pango_markup, "%s", ws->label);
|
2019-03-22 21:18:21 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int ws_vertical_padding = WS_VERTICAL_PADDING;
|
|
|
|
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
|
|
|
|
int border_width = BORDER_WIDTH;
|
2019-03-22 21:18:21 +00:00
|
|
|
|
|
|
|
uint32_t ideal_height = ws_vertical_padding * 2 + text_height
|
|
|
|
+ border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-03-22 21:18:21 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:45:26 +00:00
|
|
|
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
if (width < config->workspace_min_width) {
|
|
|
|
width = config->workspace_min_width;
|
2020-10-03 13:45:26 +00:00
|
|
|
}
|
|
|
|
return width;
|
2019-03-22 21:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t predict_workspace_buttons_length(cairo_t *cairo,
|
|
|
|
struct swaybar_output *output) {
|
|
|
|
uint32_t width = 0;
|
|
|
|
if (output->bar->config->workspace_buttons) {
|
|
|
|
struct swaybar_workspace *ws;
|
|
|
|
wl_list_for_each(ws, &output->workspaces, link) {
|
|
|
|
width += predict_workspace_button_length(cairo, output, ws);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t predict_binding_mode_indicator_length(cairo_t *cairo,
|
|
|
|
struct swaybar_output *output) {
|
|
|
|
const char *mode = output->bar->mode;
|
|
|
|
if (!mode) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct swaybar_config *config = output->bar->config;
|
|
|
|
|
|
|
|
if (!config->binding_mode_indicator) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, output->bar->mode_pango_markup,
|
2019-03-22 21:18:21 +00:00
|
|
|
"%s", mode);
|
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int ws_vertical_padding = WS_VERTICAL_PADDING;
|
|
|
|
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
|
|
|
|
int border_width = BORDER_WIDTH;
|
2019-03-22 21:18:21 +00:00
|
|
|
|
|
|
|
uint32_t ideal_height = text_height + ws_vertical_padding * 2
|
|
|
|
+ border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-03-22 21:18:21 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-03 13:45:26 +00:00
|
|
|
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
if (width < config->workspace_min_width) {
|
|
|
|
width = config->workspace_min_width;
|
2020-10-03 13:45:26 +00:00
|
|
|
}
|
|
|
|
return width;
|
2019-03-22 21:18:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
static uint32_t render_status_line_i3bar(struct render_context *ctx, double *x) {
|
|
|
|
struct swaybar_output *output = ctx->output;
|
2018-03-31 18:39:18 +00:00
|
|
|
uint32_t max_height = 0;
|
2021-09-09 23:06:17 +00:00
|
|
|
bool edge = *x == output->width;
|
2018-04-02 15:53:56 +00:00
|
|
|
struct i3bar_block *block;
|
2019-03-22 21:18:21 +00:00
|
|
|
bool use_short_text = false;
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_t *cairo = ctx->cairo;
|
2019-04-01 20:12:42 +00:00
|
|
|
double reserved_width =
|
|
|
|
predict_workspace_buttons_length(cairo, output) +
|
2019-04-01 20:13:39 +00:00
|
|
|
predict_binding_mode_indicator_length(cairo, output) +
|
2021-09-09 23:06:17 +00:00
|
|
|
3; // require a bit of space for margin
|
2019-03-22 21:18:21 +00:00
|
|
|
|
2019-04-01 20:12:42 +00:00
|
|
|
double predicted_full_pos =
|
2019-03-22 21:18:21 +00:00
|
|
|
predict_status_line_pos(cairo, output, *x);
|
|
|
|
|
|
|
|
if (predicted_full_pos < reserved_width) {
|
|
|
|
use_short_text = true;
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:09:33 +00:00
|
|
|
wl_list_for_each(block, &output->bar->status->blocks, link) {
|
2020-08-14 17:00:11 +00:00
|
|
|
uint32_t h = render_status_block(ctx, block, x, edge,
|
2019-03-22 21:18:21 +00:00
|
|
|
use_short_text);
|
2018-03-31 18:39:18 +00:00
|
|
|
max_height = h > max_height ? h : max_height;
|
|
|
|
edge = false;
|
|
|
|
}
|
|
|
|
return max_height;
|
2018-03-29 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
static uint32_t render_status_line(struct render_context *ctx, double *x) {
|
|
|
|
struct status_line *status = ctx->output->bar->status;
|
2018-03-29 19:16:12 +00:00
|
|
|
switch (status->protocol) {
|
2018-03-31 18:39:18 +00:00
|
|
|
case PROTOCOL_ERROR:
|
2020-08-14 17:00:11 +00:00
|
|
|
return render_status_line_error(ctx, x);
|
2018-03-29 19:16:12 +00:00
|
|
|
case PROTOCOL_TEXT:
|
2020-08-14 17:00:11 +00:00
|
|
|
return render_status_line_text(ctx, x);
|
2018-03-29 19:16:12 +00:00
|
|
|
case PROTOCOL_I3BAR:
|
2020-08-14 17:00:11 +00:00
|
|
|
return render_status_line_i3bar(ctx, x);
|
2018-03-31 18:39:18 +00:00
|
|
|
case PROTOCOL_UNDEF:
|
2018-03-29 19:16:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-03-31 18:39:18 +00:00
|
|
|
return 0;
|
2018-03-29 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
static uint32_t render_binding_mode_indicator(struct render_context *ctx,
|
|
|
|
double x) {
|
|
|
|
struct swaybar_output *output = ctx->output;
|
2018-10-12 19:23:01 +00:00
|
|
|
const char *mode = output->bar->mode;
|
|
|
|
if (!mode) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_t *cairo = ctx->cairo;
|
2018-09-28 13:09:33 +00:00
|
|
|
struct swaybar_config *config = output->bar->config;
|
2018-03-29 14:59:33 +00:00
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, output->bar->mode_pango_markup,
|
2018-07-05 22:12:14 +00:00
|
|
|
"%s", mode);
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int ws_vertical_padding = WS_VERTICAL_PADDING;
|
|
|
|
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
|
|
|
|
int border_width = BORDER_WIDTH;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-29 15:58:54 +00:00
|
|
|
uint32_t ideal_height = text_height + ws_vertical_padding * 2
|
|
|
|
+ border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-01-10 01:58:00 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
2018-04-08 17:49:11 +00:00
|
|
|
return ideal_surface_height;
|
2018-03-29 14:59:33 +00:00
|
|
|
}
|
2018-03-29 15:58:54 +00:00
|
|
|
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
if (width < config->workspace_min_width) {
|
|
|
|
width = config->workspace_min_width;
|
2020-10-03 13:45:26 +00:00
|
|
|
}
|
2018-03-29 14:59:33 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t height = output->height;
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2018-03-29 14:59:33 +00:00
|
|
|
cairo_set_source_u32(cairo, config->colors.binding_mode.background);
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx->background_color = config->colors.binding_mode.background;
|
2018-03-29 15:58:54 +00:00
|
|
|
cairo_rectangle(cairo, x, 0, width, height);
|
2018-03-29 14:59:33 +00:00
|
|
|
cairo_fill(cairo);
|
|
|
|
|
|
|
|
cairo_set_source_u32(cairo, config->colors.binding_mode.border);
|
2018-03-29 15:58:54 +00:00
|
|
|
cairo_rectangle(cairo, x, 0, width, border_width);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_rectangle(cairo, x, 0, border_width, height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_rectangle(cairo, x + width - border_width, 0, border_width, height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_rectangle(cairo, x, height - border_width, width, border_width);
|
|
|
|
cairo_fill(cairo);
|
2018-03-29 14:59:33 +00:00
|
|
|
|
|
|
|
double text_y = height / 2.0 - text_height / 2.0;
|
|
|
|
cairo_set_source_u32(cairo, config->colors.binding_mode.text);
|
2018-03-29 15:58:54 +00:00
|
|
|
cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
|
2020-08-14 17:00:11 +00:00
|
|
|
choose_text_aa_mode(ctx, config->colors.binding_mode.text);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, config->font_description, 1, output->bar->mode_pango_markup,
|
2021-09-09 23:06:17 +00:00
|
|
|
"%s", mode);
|
2018-09-28 13:09:33 +00:00
|
|
|
return output->height;
|
2018-03-29 14:59:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 17:43:10 +00:00
|
|
|
static enum hotspot_event_handling workspace_hotspot_callback(
|
|
|
|
struct swaybar_output *output, struct swaybar_hotspot *hotspot,
|
swaybar: Make hotspots block bar release bindings
The previous commit prioritized hotspots before bar bindings for press events,
which matches i3's behaviour. However, since hotspots don't need to do any
processing on release events, those were not handled, and simply fell through
to `bindsym --release` bar bindings (if any).
This is counter-intuitive, and doesn't match i3's behaviour. Instead in case
a hotspot handles the press event, it should also handle the release event,
doing nothing, but blocking the event from triggering a --release bar binding.
E.g., in Sway, without this commit, this config. shows a text on tray clicks:
bar {
# ...
bindsym --release button1 exec swaynag -m I_got_the_release_event.
}
But the same configuration in i3 (with i3-nagbar) doesn't show the text.
Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
2021-09-18 20:21:22 +00:00
|
|
|
double x, double y, uint32_t button, bool released, void *data) {
|
2019-01-10 17:43:10 +00:00
|
|
|
if (button != BTN_LEFT) {
|
2018-07-19 19:15:01 +00:00
|
|
|
return HOTSPOT_PROCESS;
|
|
|
|
}
|
swaybar: Make hotspots block bar release bindings
The previous commit prioritized hotspots before bar bindings for press events,
which matches i3's behaviour. However, since hotspots don't need to do any
processing on release events, those were not handled, and simply fell through
to `bindsym --release` bar bindings (if any).
This is counter-intuitive, and doesn't match i3's behaviour. Instead in case
a hotspot handles the press event, it should also handle the release event,
doing nothing, but blocking the event from triggering a --release bar binding.
E.g., in Sway, without this commit, this config. shows a text on tray clicks:
bar {
# ...
bindsym --release button1 exec swaynag -m I_got_the_release_event.
}
But the same configuration in i3 (with i3-nagbar) doesn't show the text.
Signed-off-by: Joan Bruguera <joanbrugueram@gmail.com>
2021-09-18 20:21:22 +00:00
|
|
|
if (released) {
|
|
|
|
// Since we handle the pressed event, also handle the released event
|
|
|
|
// to block it from falling through to a binding in the bar
|
|
|
|
return HOTSPOT_IGNORE;
|
|
|
|
}
|
2018-03-31 02:42:59 +00:00
|
|
|
ipc_send_workspace_command(output->bar, (const char *)data);
|
2018-07-19 19:15:01 +00:00
|
|
|
return HOTSPOT_IGNORE;
|
2018-03-31 02:42:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
static uint32_t render_workspace_button(struct render_context *ctx,
|
2018-09-28 13:09:33 +00:00
|
|
|
struct swaybar_workspace *ws, double *x) {
|
2020-08-14 17:00:11 +00:00
|
|
|
struct swaybar_output *output = ctx->output;
|
2018-09-28 13:09:33 +00:00
|
|
|
struct swaybar_config *config = output->bar->config;
|
2018-03-29 04:49:59 +00:00
|
|
|
struct box_colors box_colors;
|
|
|
|
if (ws->urgent) {
|
|
|
|
box_colors = config->colors.urgent_workspace;
|
|
|
|
} else if (ws->focused) {
|
|
|
|
box_colors = config->colors.focused_workspace;
|
|
|
|
} else if (ws->visible) {
|
|
|
|
box_colors = config->colors.active_workspace;
|
|
|
|
} else {
|
|
|
|
box_colors = config->colors.inactive_workspace;
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t height = output->height;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_t *cairo = ctx->cairo;
|
2018-03-29 04:49:59 +00:00
|
|
|
int text_width, text_height;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
|
2021-09-09 23:06:17 +00:00
|
|
|
1, config->pango_markup, "%s", ws->label);
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2021-09-09 23:06:17 +00:00
|
|
|
int ws_vertical_padding = WS_VERTICAL_PADDING;
|
|
|
|
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
|
|
|
|
int border_width = BORDER_WIDTH;
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2018-03-29 15:58:54 +00:00
|
|
|
uint32_t ideal_height = ws_vertical_padding * 2 + text_height
|
|
|
|
+ border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t ideal_surface_height = ideal_height;
|
2019-01-10 01:58:00 +00:00
|
|
|
if (!output->bar->config->height &&
|
|
|
|
output->height < ideal_surface_height) {
|
2018-04-08 17:49:11 +00:00
|
|
|
return ideal_surface_height;
|
2018-03-29 04:49:59 +00:00
|
|
|
}
|
2018-04-04 01:06:28 +00:00
|
|
|
|
2020-10-03 13:45:26 +00:00
|
|
|
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
|
2021-09-09 23:06:17 +00:00
|
|
|
if (width < config->workspace_min_width) {
|
|
|
|
width = config->workspace_min_width;
|
2020-10-03 13:45:26 +00:00
|
|
|
}
|
2018-03-29 04:49:59 +00:00
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2018-03-29 04:49:59 +00:00
|
|
|
cairo_set_source_u32(cairo, box_colors.background);
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx->background_color = box_colors.background;
|
2018-03-29 15:58:54 +00:00
|
|
|
cairo_rectangle(cairo, *x, 0, width, height);
|
2018-03-29 04:49:59 +00:00
|
|
|
cairo_fill(cairo);
|
|
|
|
|
|
|
|
cairo_set_source_u32(cairo, box_colors.border);
|
2018-03-29 15:58:54 +00:00
|
|
|
cairo_rectangle(cairo, *x, 0, width, border_width);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_rectangle(cairo, *x, 0, border_width, height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_rectangle(cairo, *x + width - border_width, 0, border_width, height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_rectangle(cairo, *x, height - border_width, width, border_width);
|
|
|
|
cairo_fill(cairo);
|
2018-03-29 04:49:59 +00:00
|
|
|
|
|
|
|
double text_y = height / 2.0 - text_height / 2.0;
|
|
|
|
cairo_set_source_u32(cairo, box_colors.text);
|
2018-03-29 15:58:54 +00:00
|
|
|
cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
|
2020-08-14 17:00:11 +00:00
|
|
|
choose_text_aa_mode(ctx, box_colors.text);
|
2022-07-01 10:23:04 +00:00
|
|
|
render_text(cairo, config->font_description, 1, config->pango_markup,
|
2018-11-17 16:11:28 +00:00
|
|
|
"%s", ws->label);
|
2018-03-29 04:49:59 +00:00
|
|
|
|
2018-03-31 02:42:59 +00:00
|
|
|
struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
|
|
|
|
hotspot->x = *x;
|
|
|
|
hotspot->y = 0;
|
|
|
|
hotspot->width = width;
|
2018-03-31 18:58:30 +00:00
|
|
|
hotspot->height = height;
|
2018-03-31 02:42:59 +00:00
|
|
|
hotspot->callback = workspace_hotspot_callback;
|
|
|
|
hotspot->destroy = free;
|
2018-04-08 14:04:23 +00:00
|
|
|
hotspot->data = strdup(ws->name);
|
2018-03-31 02:42:59 +00:00
|
|
|
wl_list_insert(&output->hotspots, &hotspot->link);
|
|
|
|
|
2018-03-29 15:58:54 +00:00
|
|
|
*x += width;
|
2018-09-28 13:09:33 +00:00
|
|
|
return output->height;
|
2018-03-29 04:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
static uint32_t render_to_cairo(struct render_context *ctx) {
|
|
|
|
cairo_t *cairo = ctx->cairo;
|
|
|
|
struct swaybar_output *output = ctx->output;
|
2018-09-28 13:09:33 +00:00
|
|
|
struct swaybar *bar = output->bar;
|
2018-03-29 03:04:20 +00:00
|
|
|
struct swaybar_config *config = bar->config;
|
2016-10-13 03:44:19 +00:00
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
|
2018-03-29 03:04:20 +00:00
|
|
|
if (output->focused) {
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx->background_color = config->colors.focused_background;
|
2016-11-02 17:48:43 +00:00
|
|
|
} else {
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx->background_color = config->colors.background;
|
2016-11-02 17:48:43 +00:00
|
|
|
}
|
2020-08-14 17:00:11 +00:00
|
|
|
|
|
|
|
cairo_set_source_u32(cairo, ctx->background_color);
|
2016-01-23 19:55:01 +00:00
|
|
|
cairo_paint(cairo);
|
|
|
|
|
2018-10-20 20:12:54 +00:00
|
|
|
int th;
|
2022-07-01 10:23:04 +00:00
|
|
|
get_text_size(cairo, config->font_description, NULL, &th, NULL, 1, false, "");
|
2021-09-09 23:06:17 +00:00
|
|
|
uint32_t max_height = (th + WS_VERTICAL_PADDING * 4);
|
2018-03-29 14:59:33 +00:00
|
|
|
/*
|
|
|
|
* Each render_* function takes the actual height of the bar, and returns
|
|
|
|
* the ideal height. If the actual height is too short, the render function
|
|
|
|
* can do whatever it wants - the buffer won't be committed. If the actual
|
|
|
|
* height is too tall, the render function should adapt its drawing to
|
|
|
|
* utilize the available space.
|
|
|
|
*/
|
2021-09-09 23:06:17 +00:00
|
|
|
double x = output->width;
|
2018-10-28 10:25:47 +00:00
|
|
|
#if HAVE_TRAY
|
|
|
|
if (bar->tray) {
|
|
|
|
uint32_t h = render_tray(cairo, output, &x);
|
|
|
|
max_height = h > max_height ? h : max_height;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-04 01:06:28 +00:00
|
|
|
if (bar->status) {
|
2020-08-14 17:00:11 +00:00
|
|
|
uint32_t h = render_status_line(ctx, &x);
|
2018-04-04 01:06:28 +00:00
|
|
|
max_height = h > max_height ? h : max_height;
|
|
|
|
}
|
|
|
|
x = 0;
|
2018-03-29 04:49:59 +00:00
|
|
|
if (config->workspace_buttons) {
|
|
|
|
struct swaybar_workspace *ws;
|
2018-10-25 11:23:48 +00:00
|
|
|
wl_list_for_each(ws, &output->workspaces, link) {
|
2020-08-14 17:00:11 +00:00
|
|
|
uint32_t h = render_workspace_button(ctx, ws, &x);
|
2018-03-29 14:59:33 +00:00
|
|
|
max_height = h > max_height ? h : max_height;
|
2018-03-29 04:49:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 19:23:01 +00:00
|
|
|
if (config->binding_mode_indicator) {
|
2020-08-14 17:00:11 +00:00
|
|
|
uint32_t h = render_binding_mode_indicator(ctx, x);
|
2018-03-29 19:16:12 +00:00
|
|
|
max_height = h > max_height ? h : max_height;
|
|
|
|
}
|
2018-03-29 04:49:59 +00:00
|
|
|
|
2018-03-29 14:59:33 +00:00
|
|
|
return max_height > output->height ? max_height : output->height;
|
2018-03-29 03:04:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 15:17:15 +00:00
|
|
|
static void output_frame_handle_done(void *data, struct wl_callback *callback,
|
|
|
|
uint32_t time) {
|
|
|
|
wl_callback_destroy(callback);
|
|
|
|
struct swaybar_output *output = data;
|
|
|
|
output->frame_scheduled = false;
|
|
|
|
if (output->dirty) {
|
|
|
|
render_frame(output);
|
|
|
|
output->dirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_callback_listener output_frame_listener = {
|
|
|
|
.done = output_frame_handle_done
|
|
|
|
};
|
|
|
|
|
|
|
|
void render_frame(struct swaybar_output *output) {
|
2018-09-19 09:37:24 +00:00
|
|
|
assert(output->surface != NULL);
|
2018-10-12 19:32:48 +00:00
|
|
|
if (!output->layer_surface) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-19 09:37:24 +00:00
|
|
|
|
2018-10-12 19:18:04 +00:00
|
|
|
free_hotspots(&output->hotspots);
|
2018-03-31 02:42:59 +00:00
|
|
|
|
2020-08-14 17:00:11 +00:00
|
|
|
struct render_context ctx = { 0 };
|
|
|
|
ctx.output = output;
|
|
|
|
|
2018-03-29 03:04:20 +00:00
|
|
|
cairo_surface_t *recorder = cairo_recording_surface_create(
|
|
|
|
CAIRO_CONTENT_COLOR_ALPHA, NULL);
|
|
|
|
cairo_t *cairo = cairo_create(recorder);
|
2021-09-09 23:06:17 +00:00
|
|
|
cairo_scale(cairo, output->scale, output->scale);
|
2018-09-21 03:37:08 +00:00
|
|
|
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST);
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx.cairo = cairo;
|
|
|
|
|
2018-09-21 03:37:08 +00:00
|
|
|
cairo_font_options_t *fo = cairo_font_options_create();
|
|
|
|
cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL);
|
2020-08-14 17:00:11 +00:00
|
|
|
cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_GRAY);
|
|
|
|
ctx.textaa_safe = fo;
|
2020-04-20 00:56:11 +00:00
|
|
|
if (output->subpixel == WL_OUTPUT_SUBPIXEL_NONE) {
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx.textaa_sharp = ctx.textaa_safe;
|
2020-04-20 00:56:11 +00:00
|
|
|
} else {
|
2020-08-14 17:00:11 +00:00
|
|
|
fo = cairo_font_options_create();
|
|
|
|
cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL);
|
2020-04-20 00:56:11 +00:00
|
|
|
cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL);
|
|
|
|
cairo_font_options_set_subpixel_order(fo,
|
|
|
|
to_cairo_subpixel_order(output->subpixel));
|
2020-08-14 17:00:11 +00:00
|
|
|
ctx.textaa_sharp = fo;
|
2020-04-20 00:56:11 +00:00
|
|
|
}
|
2020-08-14 17:00:11 +00:00
|
|
|
|
2018-03-29 19:16:12 +00:00
|
|
|
cairo_save(cairo);
|
|
|
|
cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
|
|
|
|
cairo_paint(cairo);
|
|
|
|
cairo_restore(cairo);
|
2020-08-14 17:00:11 +00:00
|
|
|
uint32_t height = render_to_cairo(&ctx);
|
2018-09-28 13:09:33 +00:00
|
|
|
int config_height = output->bar->config->height;
|
2019-01-10 01:58:00 +00:00
|
|
|
if (config_height > 0) {
|
2018-09-28 13:09:33 +00:00
|
|
|
height = config_height;
|
2018-03-29 16:03:08 +00:00
|
|
|
}
|
2018-10-13 10:56:56 +00:00
|
|
|
if (height != output->height || output->width == 0) {
|
2018-03-29 03:04:20 +00:00
|
|
|
// Reconfigure surface
|
2018-04-08 17:49:11 +00:00
|
|
|
zwlr_layer_surface_v1_set_size(output->layer_surface, 0, height);
|
2018-11-28 16:23:48 +00:00
|
|
|
zwlr_layer_surface_v1_set_margin(output->layer_surface,
|
|
|
|
output->bar->config->gaps.top,
|
|
|
|
output->bar->config->gaps.right,
|
|
|
|
output->bar->config->gaps.bottom,
|
|
|
|
output->bar->config->gaps.left);
|
2018-10-12 19:32:48 +00:00
|
|
|
if (strcmp(output->bar->config->mode, "dock") == 0) {
|
|
|
|
zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, height);
|
|
|
|
}
|
2018-03-29 03:04:20 +00:00
|
|
|
// TODO: this could infinite loop if the compositor assigns us a
|
|
|
|
// different height than what we asked for
|
|
|
|
wl_surface_commit(output->surface);
|
2018-07-06 15:38:02 +00:00
|
|
|
} else if (height > 0) {
|
2018-03-29 03:04:20 +00:00
|
|
|
// Replay recording into shm and send it off
|
2018-09-28 13:09:33 +00:00
|
|
|
output->current_buffer = get_next_buffer(output->bar->shm,
|
2018-04-04 01:06:28 +00:00
|
|
|
output->buffers,
|
|
|
|
output->width * output->scale,
|
|
|
|
output->height * output->scale);
|
2018-07-13 22:01:43 +00:00
|
|
|
if (!output->current_buffer) {
|
2021-09-14 09:02:49 +00:00
|
|
|
goto cleanup;
|
2018-07-13 22:01:43 +00:00
|
|
|
}
|
2018-03-29 03:04:20 +00:00
|
|
|
cairo_t *shm = output->current_buffer->cairo;
|
2018-03-29 14:59:33 +00:00
|
|
|
|
|
|
|
cairo_save(shm);
|
|
|
|
cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR);
|
|
|
|
cairo_paint(shm);
|
|
|
|
cairo_restore(shm);
|
|
|
|
|
2018-03-29 03:04:20 +00:00
|
|
|
cairo_set_source_surface(shm, recorder, 0.0, 0.0);
|
|
|
|
cairo_paint(shm);
|
2018-03-29 15:58:54 +00:00
|
|
|
|
2018-04-04 01:06:28 +00:00
|
|
|
wl_surface_set_buffer_scale(output->surface, output->scale);
|
2018-03-29 03:04:20 +00:00
|
|
|
wl_surface_attach(output->surface,
|
|
|
|
output->current_buffer->buffer, 0, 0);
|
2018-04-04 01:06:28 +00:00
|
|
|
wl_surface_damage(output->surface, 0, 0,
|
|
|
|
output->width, output->height);
|
2018-09-28 15:17:15 +00:00
|
|
|
|
2022-03-14 16:44:49 +00:00
|
|
|
uint32_t bg_alpha = ctx.background_color & 0xFF;
|
|
|
|
if (bg_alpha == 0xFF) {
|
|
|
|
struct wl_region *region =
|
|
|
|
wl_compositor_create_region(output->bar->compositor);
|
|
|
|
wl_region_add(region, 0, 0, INT32_MAX, INT32_MAX);
|
|
|
|
wl_surface_set_opaque_region(output->surface, region);
|
|
|
|
wl_region_destroy(region);
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:17:15 +00:00
|
|
|
struct wl_callback *frame_callback = wl_surface_frame(output->surface);
|
|
|
|
wl_callback_add_listener(frame_callback, &output_frame_listener, output);
|
|
|
|
output->frame_scheduled = true;
|
|
|
|
|
2018-03-29 03:04:20 +00:00
|
|
|
wl_surface_commit(output->surface);
|
|
|
|
}
|
2020-08-14 17:00:11 +00:00
|
|
|
|
2021-09-14 09:02:49 +00:00
|
|
|
cleanup:
|
2020-08-14 17:00:11 +00:00
|
|
|
if (ctx.textaa_sharp != ctx.textaa_safe) {
|
|
|
|
cairo_font_options_destroy(ctx.textaa_sharp);
|
|
|
|
}
|
|
|
|
cairo_font_options_destroy(ctx.textaa_safe);
|
2018-03-29 03:04:20 +00:00
|
|
|
cairo_surface_destroy(recorder);
|
|
|
|
cairo_destroy(cairo);
|
2016-01-23 23:23:09 +00:00
|
|
|
}
|