Skip to content

Commit 626b09d

Browse files
committed
Send click events for i3bar blocks
1 parent 2d79fa1 commit 626b09d

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

include/swaybar/status_line.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ struct status_line {
6767
};
6868

6969
struct status_line *status_line_init(char *cmd);
70-
void status_line_free(struct status_line *status);
70+
void status_error(struct status_line *status, const char *text);
7171
bool status_handle_readable(struct status_line *status);
72+
void status_line_free(struct status_line *status);
7273
bool i3bar_handle_readable(struct status_line *status);
73-
void status_error(struct status_line *status, const char *text);
74+
void i3bar_block_send_click(struct status_line *status,
75+
struct i3bar_block *block, int x, int y, uint32_t button);
7476

7577
#endif

swaybar/i3bar.c

+22
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,25 @@ bool i3bar_handle_readable(struct status_line *status) {
186186
state->buffer_index = cur - state->buffer;
187187
return redraw;
188188
}
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+
}

swaybar/render.c

+30-12
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,17 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color,
8989
}
9090
}
9191

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+
9299
static uint32_t render_status_block(cairo_t *cairo,
93-
struct swaybar_config *config, struct i3bar_block *block,
94-
double *x, uint32_t height, bool focused, bool edge) {
100+
struct swaybar_config *config, struct swaybar_output *output,
101+
struct i3bar_block *block, double *x,
102+
uint32_t height, bool focused, bool edge) {
95103
static const int margin = 3;
96104
if (!block->full_text || !*block->full_text) {
97105
return 0;
@@ -139,7 +147,15 @@ static uint32_t render_status_block(cairo_t *cairo,
139147
*x -= margin;
140148
}
141149

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);
143159

144160
double pos = *x;
145161
if (block->background) {
@@ -208,13 +224,14 @@ static uint32_t render_status_block(cairo_t *cairo,
208224
}
209225

210226
static uint32_t render_status_line_i3bar(cairo_t *cairo,
211-
struct swaybar_config *config, struct status_line *status,
212-
bool focused, double *x, uint32_t width, uint32_t height) {
227+
struct swaybar_config *config, struct swaybar_output *output,
228+
struct status_line *status, bool focused,
229+
double *x, uint32_t width, uint32_t height) {
213230
struct i3bar_block *block;
214231
uint32_t max_height = 0;
215232
bool edge = true;
216233
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,
218235
block, x, height, focused, edge);
219236
max_height = h > max_height ? h : max_height;
220237
edge = false;
@@ -223,8 +240,9 @@ static uint32_t render_status_line_i3bar(cairo_t *cairo,
223240
}
224241

225242
static uint32_t render_status_line(cairo_t *cairo,
226-
struct swaybar_config *config, struct status_line *status,
227-
bool focused, double *x, uint32_t width, uint32_t height) {
243+
struct swaybar_config *config, struct swaybar_output *output,
244+
struct status_line *status, bool focused,
245+
double *x, uint32_t width, uint32_t height) {
228246
switch (status->protocol) {
229247
case PROTOCOL_ERROR:
230248
return render_status_line_error(cairo,
@@ -233,8 +251,8 @@ static uint32_t render_status_line(cairo_t *cairo,
233251
return render_status_line_text(cairo,
234252
config, status->text, focused, x, width, height);
235253
case PROTOCOL_I3BAR:
236-
return render_status_line_i3bar(cairo,
237-
config, status, focused, x, width, height);
254+
return render_status_line_i3bar(cairo, config, output, status,
255+
focused, x, width, height);
238256
case PROTOCOL_UNDEF:
239257
return 0;
240258
}
@@ -344,8 +362,8 @@ static uint32_t render_workspace_button(cairo_t *cairo,
344362
struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
345363
hotspot->x = *x;
346364
hotspot->y = 0;
347-
hotspot->height = height;
348365
hotspot->width = width;
366+
hotspot->height = height;
349367
hotspot->callback = workspace_hotspot_callback;
350368
hotspot->destroy = free;
351369
hotspot->data = strdup(name);
@@ -391,7 +409,7 @@ static uint32_t render_to_cairo(cairo_t *cairo,
391409
}
392410
x = output->width;
393411
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,
395413
output->focused, &x, output->width, output->height);
396414
max_height = h > max_height ? h : max_height;
397415
}

0 commit comments

Comments
 (0)