| 1 |
/* |
| 2 |
* vim:ts=4:sw=4:expandtab |
| 3 |
* |
| 4 |
* i3bar - an xcb-based status- and ws-bar for i3 |
| 5 |
* © 2010 Axel Wagner and contributors (see also: LICENSE) |
| 6 |
* |
| 7 |
* xcb.c: Communicating with X |
| 8 |
* |
| 9 |
*/ |
| 10 |
#include "common.h" |
| 11 |
|
| 12 |
#include <err.h> |
| 13 |
#include <ev.h> |
| 14 |
#include <i3/ipc.h> |
| 15 |
#include <stddef.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include <xcb/xcb_aux.h> |
| 20 |
#include <xcb/xcb_cursor.h> |
| 21 |
#include <xcb/xkb.h> |
| 22 |
|
| 23 |
#ifdef I3_ASAN_ENABLED |
| 24 |
#include <sanitizer/lsan_interface.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "libi3.h" |
| 28 |
|
| 29 |
/* We save the atoms in an easy to access array, indexed by an enum */ |
| 30 |
enum { |
| 31 |
#define ATOM_DO(name) name, |
| 32 |
#include "xcb_atoms.def" |
| 33 |
NUM_ATOMS |
| 34 |
}; |
| 35 |
|
| 36 |
xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS]; |
| 37 |
xcb_atom_t atoms[NUM_ATOMS]; |
| 38 |
|
| 39 |
/* Variables, that are the same for all functions at all times */ |
| 40 |
xcb_connection_t *xcb_connection; |
| 41 |
int screen; |
| 42 |
xcb_screen_t *root_screen; |
| 43 |
xcb_window_t xcb_root; |
| 44 |
static xcb_cursor_t cursor; |
| 45 |
|
| 46 |
/* selection window for tray support */ |
| 47 |
static xcb_window_t selwin = XCB_NONE; |
| 48 |
static xcb_intern_atom_reply_t *tray_reply = NULL; |
| 49 |
|
| 50 |
/* This is needed for integration with libi3 */ |
| 51 |
xcb_connection_t *conn; |
| 52 |
|
| 53 |
/* The font we'll use */ |
| 54 |
static i3Font font; |
| 55 |
|
| 56 |
/* Icon size (based on font size) */ |
| 57 |
int icon_size; |
| 58 |
|
| 59 |
xcb_visualtype_t *visual_type; |
| 60 |
uint8_t depth; |
| 61 |
xcb_colormap_t colormap; |
| 62 |
|
| 63 |
/* Overall height of the bar */ |
| 64 |
int bar_height; |
| 65 |
|
| 66 |
/* These are only relevant for XKB, which we only need for grabbing modifiers */ |
| 67 |
int xkb_base; |
| 68 |
bool mod_pressed = 0; |
| 69 |
|
| 70 |
/* Event watchers, to interact with the user */ |
| 71 |
ev_prepare *xcb_prep; |
| 72 |
ev_io *xcb_io; |
| 73 |
ev_io *xkb_io; |
| 74 |
|
| 75 |
/* The name of current binding mode */ |
| 76 |
static mode binding; |
| 77 |
|
| 78 |
/* Indicates whether a new binding mode was recently activated */ |
| 79 |
bool activated_mode = false; |
| 80 |
|
| 81 |
/* The output in which the tray should be displayed. */ |
| 82 |
static i3_output *output_for_tray; |
| 83 |
|
| 84 |
/* The parsed colors */ |
| 85 |
struct xcb_colors_t { |
| 86 |
color_t bar_fg; |
| 87 |
color_t bar_bg; |
| 88 |
color_t sep_fg; |
| 89 |
color_t focus_bar_fg; |
| 90 |
color_t focus_bar_bg; |
| 91 |
color_t focus_sep_fg; |
| 92 |
color_t active_ws_fg; |
| 93 |
color_t active_ws_bg; |
| 94 |
color_t active_ws_border; |
| 95 |
color_t inactive_ws_fg; |
| 96 |
color_t inactive_ws_bg; |
| 97 |
color_t inactive_ws_border; |
| 98 |
color_t urgent_ws_bg; |
| 99 |
color_t urgent_ws_fg; |
| 100 |
color_t urgent_ws_border; |
| 101 |
color_t focus_ws_bg; |
| 102 |
color_t focus_ws_fg; |
| 103 |
color_t focus_ws_border; |
| 104 |
color_t binding_mode_bg; |
| 105 |
color_t binding_mode_fg; |
| 106 |
color_t binding_mode_border; |
| 107 |
}; |
| 108 |
struct xcb_colors_t colors; |
| 109 |
|
| 110 |
/* Horizontal offset between a workspace label and button borders */ |
| 111 |
static const int ws_hoff_px = 4; |
| 112 |
|
| 113 |
/* Vertical offset between a workspace label and button borders */ |
| 114 |
static const int ws_voff_px = 3; |
| 115 |
|
| 116 |
/* Offset between two workspace buttons */ |
| 117 |
static const int ws_spacing_px = 1; |
| 118 |
|
| 119 |
/* Offset between the statusline and 1) workspace buttons on the left |
| 120 |
* 2) the tray or screen edge on the right */ |
| 121 |
static const int sb_hoff_px = 4; |
| 122 |
|
| 123 |
/* Additional offset between the tray and the statusline, if the tray is not empty */ |
| 124 |
static const int tray_loff_px = 2; |
| 125 |
|
| 126 |
/* Vertical offset between the bar and a separator */ |
| 127 |
static const int sep_voff_px = 4; |
| 128 |
|
| 129 |
/* Cached width of the custom separator if one was set */ |
| 130 |
int separator_symbol_width; |
| 131 |
|
| 132 |
int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) { |
| 133 |
xcb_generic_error_t *err; |
| 134 |
if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) { |
| 135 |
fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code); |
| 136 |
return err->error_code; |
| 137 |
} |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
static uint32_t get_sep_offset(struct status_block *block) { |
| 142 |
if (!block->no_separator && block->sep_block_width > 0) { |
| 143 |
return block->sep_block_width / 2 + block->sep_block_width % 2; |
| 144 |
} |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int get_tray_width(struct tc_head *trayclients) { |
| 149 |
trayclient *trayclient; |
| 150 |
int tray_width = 0; |
| 151 |
TAILQ_FOREACH_REVERSE (trayclient, trayclients, tc_head, tailq) { |
| 152 |
if (!trayclient->mapped) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
tray_width += icon_size + logical_px(config.tray_padding); |
| 156 |
} |
| 157 |
if (tray_width > 0) { |
| 158 |
tray_width += logical_px(tray_loff_px); |
| 159 |
} |
| 160 |
return tray_width; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Draws a separator for the given block if necessary. |
| 165 |
* |
| 166 |
*/ |
| 167 |
static void draw_separator(i3_output *output, uint32_t x, struct status_block *block, bool use_focus_colors) { |
| 168 |
color_t sep_fg = (use_focus_colors ? colors.focus_sep_fg : colors.sep_fg); |
| 169 |
color_t bar_bg = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg); |
| 170 |
|
| 171 |
uint32_t sep_offset = get_sep_offset(block); |
| 172 |
if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
uint32_t center_x = x - sep_offset; |
| 177 |
if (config.separator_symbol == NULL) { |
| 178 |
/* Draw a classic one pixel, vertical separator. */ |
| 179 |
draw_util_rectangle(&output->statusline_buffer, sep_fg, |
| 180 |
center_x, |
| 181 |
logical_px(sep_voff_px), |
| 182 |
logical_px(1), |
| 183 |
bar_height - 2 * logical_px(sep_voff_px)); |
| 184 |
} else { |
| 185 |
/* Draw a custom separator. */ |
| 186 |
uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2); |
| 187 |
draw_util_text(config.separator_symbol, &output->statusline_buffer, sep_fg, bar_bg, |
| 188 |
separator_x, bar_height / 2 - font.height / 2, x - separator_x); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
static void predict_block_length(struct status_block *block) { |
| 193 |
i3String *text = block->full_text; |
| 194 |
struct status_block_render_desc *render = &block->full_render; |
| 195 |
if (block->use_short && block->short_text != NULL) { |
| 196 |
text = block->short_text; |
| 197 |
render = &block->short_render; |
| 198 |
} |
| 199 |
|
| 200 |
if (i3string_get_num_bytes(text) == 0) { |
| 201 |
block->render_length = 0; |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
render->width = predict_text_width(text); |
| 206 |
if (block->border) { |
| 207 |
render->width += logical_px(block->border_left + block->border_right); |
| 208 |
} |
| 209 |
|
| 210 |
/* Compute offset and append for text alignment in min_width. */ |
| 211 |
if (block->min_width <= render->width) { |
| 212 |
render->x_offset = 0; |
| 213 |
render->x_append = 0; |
| 214 |
} else { |
| 215 |
uint32_t padding_width = block->min_width - render->width; |
| 216 |
switch (block->align) { |
| 217 |
case ALIGN_LEFT: |
| 218 |
render->x_append = padding_width; |
| 219 |
break; |
| 220 |
case ALIGN_RIGHT: |
| 221 |
render->x_offset = padding_width; |
| 222 |
break; |
| 223 |
case ALIGN_CENTER: |
| 224 |
render->x_offset = padding_width / 2; |
| 225 |
render->x_append = padding_width / 2 + padding_width % 2; |
| 226 |
break; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
block->render_length = render->width + render->x_offset + render->x_append; |
| 231 |
} |
| 232 |
|
| 233 |
static uint32_t predict_statusline_length(void) { |
| 234 |
uint32_t width = 0; |
| 235 |
struct status_block *block; |
| 236 |
|
| 237 |
TAILQ_FOREACH (block, &statusline_head, blocks) { |
| 238 |
predict_block_length(block); |
| 239 |
uint32_t block_width = block->render_length; |
| 240 |
if (block_width == 0) { |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
width += block_width; |
| 245 |
|
| 246 |
/* If this is not the last block, add some pixels for a separator. */ |
| 247 |
if (TAILQ_NEXT(block, blocks) != NULL) { |
| 248 |
width += block->sep_block_width; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return width; |
| 253 |
} |
| 254 |
|
| 255 |
static uint32_t switch_block_to_short(struct status_block *block) { |
| 256 |
/* Skip blocks that have no short form or are already in short form */ |
| 257 |
if (block->short_text == NULL || block->use_short) { |
| 258 |
return 0; |
| 259 |
} |
| 260 |
uint32_t full = block->render_length; |
| 261 |
block->use_short = true; |
| 262 |
predict_block_length(block); |
| 263 |
return full - block->render_length; |
| 264 |
} |
| 265 |
|
| 266 |
static uint32_t adjust_statusline_length(uint32_t max_length) { |
| 267 |
uint32_t width = predict_statusline_length(); |
| 268 |
|
| 269 |
/* Progressively switch the blocks to short mode */ |
| 270 |
struct status_block *block; |
| 271 |
TAILQ_FOREACH (block, &statusline_head, blocks) { |
| 272 |
if (width < max_length) { |
| 273 |
break; |
| 274 |
} |
| 275 |
width -= switch_block_to_short(block); |
| 276 |
|
| 277 |
/* Provide support for representing a single logical block using multiple |
| 278 |
* JSON blocks: if one block is shortened, ensure that all other blocks |
| 279 |
* with the same name are also shortened such that the entire logical block uses |
| 280 |
* the short form text. */ |
| 281 |
if (block->name) { |
| 282 |
struct status_block *other; |
| 283 |
TAILQ_FOREACH (other, &statusline_head, blocks) { |
| 284 |
if (other->name && !strcmp(other->name, block->name)) { |
| 285 |
width -= switch_block_to_short(other); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return width; |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
* Redraws the statusline to the output's statusline_buffer |
| 296 |
*/ |
| 297 |
static void draw_statusline(i3_output *output, uint32_t clip_left, bool use_focus_colors) { |
| 298 |
struct status_block *block; |
| 299 |
|
| 300 |
color_t bar_color = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg); |
| 301 |
draw_util_clear_surface(&output->statusline_buffer, bar_color); |
| 302 |
|
| 303 |
/* Use unsigned integer wraparound to clip off the left side. |
| 304 |
* For example, if clip_left is 75, then x will start at the very large |
| 305 |
* number INT_MAX-75, which is way outside the surface dimensions. Drawing |
| 306 |
* to that x position is a no-op which XCB and Cairo safely ignore. Once x moves |
| 307 |
* up by 75 and goes past INT_MAX, it will wrap around again to 0, and we start |
| 308 |
* actually rendering content to the surface. */ |
| 309 |
uint32_t x = 0 - clip_left; |
| 310 |
|
| 311 |
/* Draw the text of each block */ |
| 312 |
TAILQ_FOREACH (block, &statusline_head, blocks) { |
| 313 |
i3String *text = block->full_text; |
| 314 |
struct status_block_render_desc *render = &block->full_render; |
| 315 |
if (block->use_short && block->short_text != NULL) { |
| 316 |
text = block->short_text; |
| 317 |
render = &block->short_render; |
| 318 |
} |
| 319 |
|
| 320 |
if (i3string_get_num_bytes(text) == 0) { |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
color_t fg_color; |
| 325 |
if (block->urgent) { |
| 326 |
fg_color = colors.urgent_ws_fg; |
| 327 |
} else if (block->color) { |
| 328 |
fg_color = draw_util_hex_to_color(block->color); |
| 329 |
} else if (use_focus_colors) { |
| 330 |
fg_color = colors.focus_bar_fg; |
| 331 |
} else { |
| 332 |
fg_color = colors.bar_fg; |
| 333 |
} |
| 334 |
|
| 335 |
color_t bg_color = bar_color; |
| 336 |
|
| 337 |
int full_render_width = render->width + render->x_offset + render->x_append; |
| 338 |
int has_border = block->border ? 1 : 0; |
| 339 |
if (block->border || block->background || block->urgent) { |
| 340 |
/* Let's determine the colors first. */ |
| 341 |
color_t border_color = bar_color; |
| 342 |
if (block->urgent) { |
| 343 |
border_color = colors.urgent_ws_border; |
| 344 |
bg_color = colors.urgent_ws_bg; |
| 345 |
} else { |
| 346 |
if (block->border) { |
| 347 |
border_color = draw_util_hex_to_color(block->border); |
| 348 |
} |
| 349 |
if (block->background) { |
| 350 |
bg_color = draw_util_hex_to_color(block->background); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
/* Draw the border. */ |
| 355 |
draw_util_rectangle(&output->statusline_buffer, border_color, |
| 356 |
x, logical_px(1), |
| 357 |
full_render_width, |
| 358 |
bar_height - logical_px(2)); |
| 359 |
|
| 360 |
/* Draw the background. */ |
| 361 |
draw_util_rectangle(&output->statusline_buffer, bg_color, |
| 362 |
x + has_border * logical_px(block->border_left), |
| 363 |
logical_px(1) + has_border * logical_px(block->border_top), |
| 364 |
full_render_width - has_border * logical_px(block->border_right + block->border_left), |
| 365 |
bar_height - has_border * logical_px(block->border_bottom + block->border_top) - logical_px(2)); |
| 366 |
} |
| 367 |
|
| 368 |
draw_util_text(text, &output->statusline_buffer, fg_color, bg_color, |
| 369 |
x + render->x_offset + has_border * logical_px(block->border_left), |
| 370 |
bar_height / 2 - font.height / 2, |
| 371 |
render->width - has_border * logical_px(block->border_left + block->border_right)); |
| 372 |
x += full_render_width; |
| 373 |
|
| 374 |
/* If this is not the last block, draw a separator. */ |
| 375 |
if (TAILQ_NEXT(block, blocks) != NULL) { |
| 376 |
x += block->sep_block_width; |
| 377 |
draw_separator(output, x, block, use_focus_colors); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/* |
| 383 |
* Hides all bars (unmaps them) |
| 384 |
* |
| 385 |
*/ |
| 386 |
static void hide_bars(void) { |
| 387 |
if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) { |
| 388 |
return; |
| 389 |
} |
| 390 |
|
| 391 |
i3_output *walk; |
| 392 |
SLIST_FOREACH (walk, outputs, slist) { |
| 393 |
if (!walk->active) { |
| 394 |
continue; |
| 395 |
} |
| 396 |
xcb_unmap_window(xcb_connection, walk->bar.id); |
| 397 |
} |
| 398 |
stop_children(); |
| 399 |
} |
| 400 |
|
| 401 |
/* |
| 402 |
* Unhides all bars (maps them) |
| 403 |
* |
| 404 |
*/ |
| 405 |
static void unhide_bars(void) { |
| 406 |
if (config.hide_on_modifier != M_HIDE) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
i3_output *walk; |
| 411 |
xcb_void_cookie_t cookie; |
| 412 |
uint32_t mask; |
| 413 |
uint32_t values[5]; |
| 414 |
|
| 415 |
cont_children(); |
| 416 |
|
| 417 |
SLIST_FOREACH (walk, outputs, slist) { |
| 418 |
if (walk->bar.id == XCB_NONE) { |
| 419 |
continue; |
| 420 |
} |
| 421 |
mask = XCB_CONFIG_WINDOW_X | |
| 422 |
XCB_CONFIG_WINDOW_Y | |
| 423 |
XCB_CONFIG_WINDOW_WIDTH | |
| 424 |
XCB_CONFIG_WINDOW_HEIGHT | |
| 425 |
XCB_CONFIG_WINDOW_STACK_MODE; |
| 426 |
values[0] = walk->rect.x; |
| 427 |
if (config.position == POS_TOP) { |
| 428 |
values[1] = walk->rect.y; |
| 429 |
} else { |
| 430 |
values[1] = walk->rect.y + walk->rect.h - bar_height; |
| 431 |
} |
| 432 |
values[2] = walk->rect.w; |
| 433 |
values[3] = bar_height; |
| 434 |
values[4] = XCB_STACK_MODE_ABOVE; |
| 435 |
DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]); |
| 436 |
cookie = xcb_configure_window_checked(xcb_connection, |
| 437 |
walk->bar.id, |
| 438 |
mask, |
| 439 |
values); |
| 440 |
|
| 441 |
if (xcb_request_failed(cookie, "Could not reconfigure window")) { |
| 442 |
exit(EXIT_FAILURE); |
| 443 |
} |
| 444 |
xcb_map_window(xcb_connection, walk->bar.id); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
/* |
| 449 |
* Parse the colors into a format that we can use |
| 450 |
* |
| 451 |
*/ |
| 452 |
void init_colors(const struct xcb_color_strings_t *new_colors) { |
| 453 |
#define PARSE_COLOR(name, def) \ |
| 454 |
do { \ |
| 455 |
colors.name = draw_util_hex_to_color(new_colors->name ? new_colors->name : def); \ |
| 456 |
} while (0) |
| 457 |
PARSE_COLOR(bar_fg, "#FFFFFF"); |
| 458 |
PARSE_COLOR(bar_bg, "#000000"); |
| 459 |
PARSE_COLOR(sep_fg, "#666666"); |
| 460 |
PARSE_COLOR(active_ws_fg, "#FFFFFF"); |
| 461 |
PARSE_COLOR(active_ws_bg, "#333333"); |
| 462 |
PARSE_COLOR(active_ws_border, "#333333"); |
| 463 |
PARSE_COLOR(inactive_ws_fg, "#888888"); |
| 464 |
PARSE_COLOR(inactive_ws_bg, "#222222"); |
| 465 |
PARSE_COLOR(inactive_ws_border, "#333333"); |
| 466 |
PARSE_COLOR(urgent_ws_fg, "#FFFFFF"); |
| 467 |
PARSE_COLOR(urgent_ws_bg, "#900000"); |
| 468 |
PARSE_COLOR(urgent_ws_border, "#2f343a"); |
| 469 |
PARSE_COLOR(focus_ws_fg, "#FFFFFF"); |
| 470 |
PARSE_COLOR(focus_ws_bg, "#285577"); |
| 471 |
PARSE_COLOR(focus_ws_border, "#4c7899"); |
| 472 |
#undef PARSE_COLOR |
| 473 |
|
| 474 |
#define PARSE_COLOR_FALLBACK(name, fallback) \ |
| 475 |
do { \ |
| 476 |
colors.name = new_colors->name ? draw_util_hex_to_color(new_colors->name) : colors.fallback; \ |
| 477 |
} while (0) |
| 478 |
|
| 479 |
/* For the binding mode indicator colors, we don't hardcode a default. |
| 480 |
* Instead, we fall back to urgent_ws_* colors. */ |
| 481 |
PARSE_COLOR_FALLBACK(binding_mode_fg, urgent_ws_fg); |
| 482 |
PARSE_COLOR_FALLBACK(binding_mode_bg, urgent_ws_bg); |
| 483 |
PARSE_COLOR_FALLBACK(binding_mode_border, urgent_ws_border); |
| 484 |
|
| 485 |
/* Similarly, for unspecified focused bar colors, we fall back to the |
| 486 |
* regular bar colors. */ |
| 487 |
PARSE_COLOR_FALLBACK(focus_bar_fg, bar_fg); |
| 488 |
PARSE_COLOR_FALLBACK(focus_bar_bg, bar_bg); |
| 489 |
PARSE_COLOR_FALLBACK(focus_sep_fg, sep_fg); |
| 490 |
#undef PARSE_COLOR_FALLBACK |
| 491 |
|
| 492 |
init_tray_colors(); |
| 493 |
xcb_flush(xcb_connection); |
| 494 |
} |
| 495 |
|
| 496 |
static bool execute_custom_command(xcb_keycode_t input_code, bool event_is_release) { |
| 497 |
binding_t *binding; |
| 498 |
TAILQ_FOREACH (binding, &(config.bindings), bindings) { |
| 499 |
if ((binding->input_code != input_code) || (binding->release != event_is_release)) { |
| 500 |
continue; |
| 501 |
} |
| 502 |
|
| 503 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, binding->command); |
| 504 |
return true; |
| 505 |
} |
| 506 |
return false; |
| 507 |
} |
| 508 |
|
| 509 |
static void child_handle_button(xcb_button_press_event_t *event, i3_output *output, uint32_t statusline_x, uint32_t statusline_y) { |
| 510 |
if (statusline_x > (uint32_t)output->statusline_width) { |
| 511 |
return; |
| 512 |
} |
| 513 |
|
| 514 |
/* x of the start of the current block relative to the statusline. */ |
| 515 |
uint32_t last_block_x = 0; |
| 516 |
struct status_block *block; |
| 517 |
TAILQ_FOREACH (block, &statusline_head, blocks) { |
| 518 |
i3String *text; |
| 519 |
struct status_block_render_desc *render; |
| 520 |
if (block->use_short && block->short_text != NULL) { |
| 521 |
text = block->short_text; |
| 522 |
render = &block->short_render; |
| 523 |
} else { |
| 524 |
text = block->full_text; |
| 525 |
render = &block->full_render; |
| 526 |
} |
| 527 |
|
| 528 |
if (i3string_get_num_bytes(text) == 0) { |
| 529 |
continue; |
| 530 |
} |
| 531 |
|
| 532 |
/* Include the whole block in our calculations: when min_width is |
| 533 |
* specified, we have to take padding width into account. */ |
| 534 |
const uint32_t full_render_width = render->width + render->x_offset + render->x_append; |
| 535 |
/* x of the click event relative to the current block. */ |
| 536 |
const uint32_t relative_x = statusline_x - last_block_x; |
| 537 |
if (relative_x <= full_render_width) { |
| 538 |
const uint32_t output_x = event->event_x; |
| 539 |
const uint32_t output_y = statusline_y + event->event_y; |
| 540 |
|
| 541 |
send_block_clicked(event->detail, block->name, block->instance, |
| 542 |
event->root_x, event->root_y, relative_x, |
| 543 |
event->event_y, output_x, output_y, |
| 544 |
full_render_width, bar_height, |
| 545 |
event->state); |
| 546 |
return; |
| 547 |
} |
| 548 |
|
| 549 |
last_block_x += full_render_width + block->sep_block_width; |
| 550 |
if (last_block_x > statusline_x) { |
| 551 |
/* Click was on a separator. */ |
| 552 |
return; |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
* Predict the width of a workspace button or the current binding mode indicator. |
| 559 |
* |
| 560 |
*/ |
| 561 |
static int predict_button_width(int name_width) { |
| 562 |
return MAX(name_width + 2 * logical_px(ws_hoff_px) + 2 * logical_px(1), |
| 563 |
logical_px(config.ws_min_width)); |
| 564 |
} |
| 565 |
|
| 566 |
static char *quote_workspace_name(const char *in) { |
| 567 |
/* To properly handle workspace names with double quotes in them, we need |
| 568 |
* to escape the double quotes. We allocate a large enough buffer (twice |
| 569 |
* the unescaped size is always enough), then we copy character by |
| 570 |
* character. */ |
| 571 |
const size_t namelen = strlen(in); |
| 572 |
const size_t len = namelen + strlen("workspace \"\"") + 1; |
| 573 |
char *out = scalloc(2 * len, 1); |
| 574 |
memcpy(out, "workspace \"", strlen("workspace \"")); |
| 575 |
size_t inpos, outpos; |
| 576 |
for (inpos = 0, outpos = strlen("workspace \""); |
| 577 |
inpos < namelen; |
| 578 |
inpos++, outpos++) { |
| 579 |
if (in[inpos] == '"' || in[inpos] == '\\') { |
| 580 |
out[outpos] = '\\'; |
| 581 |
outpos++; |
| 582 |
} |
| 583 |
out[outpos] = in[inpos]; |
| 584 |
} |
| 585 |
out[outpos] = '"'; |
| 586 |
return out; |
| 587 |
} |
| 588 |
|
| 589 |
static void focus_workspace(i3_ws *ws) { |
| 590 |
char *buffer = NULL; |
| 591 |
if (ws->id != 0) { |
| 592 |
/* Workspace ID has higher precedence since the workspace_command is |
| 593 |
* allowed to change workspace names as long as it provides a valid ID. */ |
| 594 |
sasprintf(&buffer, "[con_id=%lu] focus workspace", ws->id); |
| 595 |
goto done; |
| 596 |
} |
| 597 |
|
| 598 |
if (ws->canonical_name == NULL) { |
| 599 |
return; |
| 600 |
} |
| 601 |
|
| 602 |
buffer = quote_workspace_name(ws->canonical_name); |
| 603 |
|
| 604 |
done: |
| 605 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, buffer); |
| 606 |
free(buffer); |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
* Handle a button press event (i.e. a mouse click on one of our bars). |
| 611 |
* We determine, whether the click occurred on a workspace button or if the scroll- |
| 612 |
* wheel was used and change the workspace appropriately |
| 613 |
* |
| 614 |
*/ |
| 615 |
static void handle_button(xcb_button_press_event_t *event) { |
| 616 |
/* Determine, which bar was clicked */ |
| 617 |
i3_output *walk; |
| 618 |
xcb_window_t bar = event->event; |
| 619 |
SLIST_FOREACH (walk, outputs, slist) { |
| 620 |
if (walk->bar.id == bar) { |
| 621 |
break; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
if (walk == NULL) { |
| 626 |
DLOG("Unknown bar clicked!\n"); |
| 627 |
return; |
| 628 |
} |
| 629 |
|
| 630 |
DLOG("Got button %d\n", event->detail); |
| 631 |
|
| 632 |
/* During button release events, only check for custom commands. */ |
| 633 |
const bool event_is_release = (event->response_type & ~0x80) == XCB_BUTTON_RELEASE; |
| 634 |
|
| 635 |
const int x = (event->event_x >= 0 ? event->event_x : 0) - logical_px(config.padding.x); |
| 636 |
if (x < 0) { |
| 637 |
/* Ignore clicks in padding */ |
| 638 |
return; |
| 639 |
} |
| 640 |
|
| 641 |
int workspace_width = 0; |
| 642 |
i3_ws *cur_ws = NULL, *clicked_ws = NULL, *ws_walk; |
| 643 |
|
| 644 |
TAILQ_FOREACH (ws_walk, walk->workspaces, tailq) { |
| 645 |
int w = predict_button_width(ws_walk->name_width); |
| 646 |
if (x >= workspace_width && x <= workspace_width + w) { |
| 647 |
clicked_ws = ws_walk; |
| 648 |
} |
| 649 |
if (ws_walk->visible) { |
| 650 |
cur_ws = ws_walk; |
| 651 |
} |
| 652 |
workspace_width += w; |
| 653 |
if (TAILQ_NEXT(ws_walk, tailq) != NULL) { |
| 654 |
workspace_width += logical_px(ws_spacing_px); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
if (child_want_click_events() && x > workspace_width) { |
| 659 |
const int tray_width = get_tray_width(walk->trayclients); |
| 660 |
/* Calculate the horizontal coordinate (x) of the start of the |
| 661 |
* statusline by subtracting its width and the width of the tray from |
| 662 |
* the bar width. */ |
| 663 |
const int offset_x = walk->rect.w - walk->statusline_width - |
| 664 |
tray_width - logical_px((tray_width > 0) * sb_hoff_px); |
| 665 |
if (x >= offset_x) { |
| 666 |
/* Click was after the start of the statusline, return to avoid |
| 667 |
* executing any other actions even if a click event is not |
| 668 |
* produced eventually. */ |
| 669 |
|
| 670 |
if (!event_is_release) { |
| 671 |
/* x of the click event relative to the start of the |
| 672 |
* statusline. */ |
| 673 |
const uint32_t statusline_x = x - offset_x; |
| 674 |
const uint32_t statusline_y = event->root_y - walk->rect.y - event->event_y; |
| 675 |
|
| 676 |
child_handle_button(event, walk, statusline_x, statusline_y); |
| 677 |
} |
| 678 |
|
| 679 |
return; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
/* If a custom command was specified for this mouse button, it overrides |
| 684 |
* the default behavior. */ |
| 685 |
if (execute_custom_command(event->detail, event_is_release) || event_is_release) { |
| 686 |
return; |
| 687 |
} |
| 688 |
|
| 689 |
if (cur_ws == NULL) { |
| 690 |
DLOG("No workspace active?\n"); |
| 691 |
return; |
| 692 |
} |
| 693 |
switch (event->detail) { |
| 694 |
case XCB_BUTTON_SCROLL_UP: |
| 695 |
case XCB_BUTTON_SCROLL_LEFT: |
| 696 |
/* Mouse wheel up. We select the previous ws, if any. |
| 697 |
* If there is no more workspace, don’t even send the workspace |
| 698 |
* command, otherwise (with workspace auto_back_and_forth) we’d end |
| 699 |
* up on the wrong workspace. */ |
| 700 |
if (cur_ws == TAILQ_FIRST(walk->workspaces)) { |
| 701 |
return; |
| 702 |
} |
| 703 |
|
| 704 |
cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq); |
| 705 |
break; |
| 706 |
case XCB_BUTTON_SCROLL_DOWN: |
| 707 |
case XCB_BUTTON_SCROLL_RIGHT: |
| 708 |
/* Mouse wheel down. We select the next ws, if any. |
| 709 |
* If there is no more workspace, don’t even send the workspace |
| 710 |
* command, otherwise (with workspace auto_back_and_forth) we’d end |
| 711 |
* up on the wrong workspace. */ |
| 712 |
if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) { |
| 713 |
return; |
| 714 |
} |
| 715 |
|
| 716 |
cur_ws = TAILQ_NEXT(cur_ws, tailq); |
| 717 |
break; |
| 718 |
case 1: |
| 719 |
cur_ws = clicked_ws; |
| 720 |
|
| 721 |
/* if no workspace was clicked, focus our currently visible |
| 722 |
* workspace if it is not already focused */ |
| 723 |
if (cur_ws == NULL) { |
| 724 |
TAILQ_FOREACH (cur_ws, walk->workspaces, tailq) { |
| 725 |
if (cur_ws->visible && !cur_ws->focused) { |
| 726 |
break; |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
/* if there is nothing to focus, we are done */ |
| 732 |
if (cur_ws == NULL) { |
| 733 |
return; |
| 734 |
} |
| 735 |
|
| 736 |
break; |
| 737 |
default: |
| 738 |
return; |
| 739 |
} |
| 740 |
|
| 741 |
focus_workspace(cur_ws); |
| 742 |
} |
| 743 |
|
| 744 |
/* |
| 745 |
* Handle visibility notifications: when none of the bars are visible, e.g. |
| 746 |
* if windows are in fullscreen on each output, suspend the child process. |
| 747 |
* |
| 748 |
*/ |
| 749 |
static void handle_visibility_notify(xcb_visibility_notify_event_t *event) { |
| 750 |
bool visible = (event->state != XCB_VISIBILITY_FULLY_OBSCURED); |
| 751 |
int num_visible = 0; |
| 752 |
i3_output *output; |
| 753 |
|
| 754 |
SLIST_FOREACH (output, outputs, slist) { |
| 755 |
if (!output->active) { |
| 756 |
continue; |
| 757 |
} |
| 758 |
if (output->bar.id == event->window) { |
| 759 |
output->visible = visible; |
| 760 |
} |
| 761 |
num_visible += output->visible; |
| 762 |
} |
| 763 |
|
| 764 |
if (num_visible == 0) { |
| 765 |
stop_children(); |
| 766 |
} else { |
| 767 |
cont_children(); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
/* |
| 772 |
* Comparison function to sort trayclients in ascending alphanumeric order |
| 773 |
* according to their class. |
| 774 |
* |
| 775 |
*/ |
| 776 |
static int reorder_trayclients_cmp(const void *_a, const void *_b) { |
| 777 |
trayclient *a = *((trayclient **)_a); |
| 778 |
trayclient *b = *((trayclient **)_b); |
| 779 |
|
| 780 |
int result = strcasecmp_nullable(a->class_class, b->class_class); |
| 781 |
return result != 0 ? result : strcasecmp_nullable(a->class_instance, b->class_instance); |
| 782 |
} |
| 783 |
|
| 784 |
/* |
| 785 |
* Adjusts the size of the tray window and alignment of the tray clients by |
| 786 |
* configuring their respective x coordinates. To be called when mapping or |
| 787 |
* unmapping a tray client window. |
| 788 |
* |
| 789 |
*/ |
| 790 |
static void configure_trayclients(void) { |
| 791 |
i3_output *output; |
| 792 |
SLIST_FOREACH (output, outputs, slist) { |
| 793 |
if (!output->active) { |
| 794 |
continue; |
| 795 |
} |
| 796 |
|
| 797 |
int count = 0; |
| 798 |
trayclient *client; |
| 799 |
TAILQ_FOREACH (client, output->trayclients, tailq) { |
| 800 |
if (client->mapped) { |
| 801 |
count++; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
int idx = 0; |
| 806 |
trayclient **trayclients = smalloc(count * sizeof(trayclient *)); |
| 807 |
TAILQ_FOREACH (client, output->trayclients, tailq) { |
| 808 |
if (client->mapped) { |
| 809 |
trayclients[idx++] = client; |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
qsort(trayclients, count, sizeof(trayclient *), reorder_trayclients_cmp); |
| 814 |
|
| 815 |
uint32_t x = output->rect.w; |
| 816 |
for (idx = count; idx > 0; idx--) { |
| 817 |
x -= icon_size + logical_px(config.tray_padding); |
| 818 |
|
| 819 |
DLOG("Configuring tray window %08x to x=%d\n", trayclients[idx - 1]->win, x); |
| 820 |
xcb_configure_window(xcb_connection, |
| 821 |
trayclients[idx - 1]->win, |
| 822 |
XCB_CONFIG_WINDOW_X, |
| 823 |
&x); |
| 824 |
} |
| 825 |
|
| 826 |
free(trayclients); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
static trayclient *trayclient_and_output_from_window(xcb_window_t win, i3_output **output) { |
| 831 |
i3_output *o_walk; |
| 832 |
SLIST_FOREACH (o_walk, outputs, slist) { |
| 833 |
if (!o_walk->active) { |
| 834 |
continue; |
| 835 |
} |
| 836 |
|
| 837 |
trayclient *client; |
| 838 |
TAILQ_FOREACH (client, o_walk->trayclients, tailq) { |
| 839 |
if (client->win == win) { |
| 840 |
if (output) { |
| 841 |
*output = o_walk; |
| 842 |
} |
| 843 |
return client; |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
return NULL; |
| 848 |
} |
| 849 |
|
| 850 |
static trayclient *trayclient_from_window(xcb_window_t win) { |
| 851 |
return trayclient_and_output_from_window(win, NULL); |
| 852 |
} |
| 853 |
|
| 854 |
static void trayclient_update_class(trayclient *client) { |
| 855 |
xcb_get_property_reply_t *prop = xcb_get_property_reply( |
| 856 |
conn, |
| 857 |
xcb_get_property_unchecked( |
| 858 |
xcb_connection, |
| 859 |
false, |
| 860 |
client->win, |
| 861 |
XCB_ATOM_WM_CLASS, |
| 862 |
XCB_ATOM_STRING, |
| 863 |
0, |
| 864 |
32), |
| 865 |
NULL); |
| 866 |
if (prop == NULL || xcb_get_property_value_length(prop) == 0) { |
| 867 |
DLOG("WM_CLASS not set.\n"); |
| 868 |
free(prop); |
| 869 |
return; |
| 870 |
} |
| 871 |
|
| 872 |
/* We cannot use asprintf here since this property contains two |
| 873 |
* null-terminated strings (for compatibility reasons). Instead, we |
| 874 |
* use strdup() on both strings */ |
| 875 |
const size_t prop_length = xcb_get_property_value_length(prop); |
| 876 |
char *new_class = xcb_get_property_value(prop); |
| 877 |
const size_t class_class_index = strnlen(new_class, prop_length) + 1; |
| 878 |
|
| 879 |
free(client->class_instance); |
| 880 |
free(client->class_class); |
| 881 |
|
| 882 |
client->class_instance = sstrndup(new_class, prop_length); |
| 883 |
if (class_class_index < prop_length) { |
| 884 |
client->class_class = sstrndup(new_class + class_class_index, prop_length - class_class_index); |
| 885 |
} else { |
| 886 |
client->class_class = NULL; |
| 887 |
} |
| 888 |
DLOG("WM_CLASS changed to %s (instance), %s (class)\n", client->class_instance, client->class_class); |
| 889 |
|
| 890 |
free(prop); |
| 891 |
} |
| 892 |
|
| 893 |
/* |
| 894 |
* Handles ClientMessages (messages sent from another client directly to us). |
| 895 |
* |
| 896 |
* At the moment, only the tray window will receive client messages. All |
| 897 |
* supported client messages currently are _NET_SYSTEM_TRAY_OPCODE. |
| 898 |
* |
| 899 |
*/ |
| 900 |
static void handle_client_message(xcb_client_message_event_t *event) { |
| 901 |
if (event->type == atoms[I3_SYNC]) { |
| 902 |
xcb_window_t window = event->data.data32[0]; |
| 903 |
uint32_t rnd = event->data.data32[1]; |
| 904 |
/* Forward the request to i3 via the IPC interface so that all pending |
| 905 |
* IPC messages are guaranteed to be handled. */ |
| 906 |
char *payload = NULL; |
| 907 |
sasprintf(&payload, "{\"rnd\":%d, \"window\":%d}", rnd, window); |
| 908 |
i3_send_msg(I3_IPC_MESSAGE_TYPE_SYNC, payload); |
| 909 |
free(payload); |
| 910 |
} else if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] && |
| 911 |
event->format == 32) { |
| 912 |
DLOG("_NET_SYSTEM_TRAY_OPCODE received\n"); |
| 913 |
/* event->data.data32[0] is the timestamp */ |
| 914 |
uint32_t op = event->data.data32[1]; |
| 915 |
uint32_t values[2]; |
| 916 |
if (op == SYSTEM_TRAY_REQUEST_DOCK) { |
| 917 |
const xcb_window_t client = event->data.data32[2]; |
| 918 |
uint32_t mask = XCB_CW_EVENT_MASK; |
| 919 |
|
| 920 |
/* Needed to get the most recent value of XEMBED_MAPPED. */ |
| 921 |
values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 922 |
/* Needed for UnmapNotify events. */ |
| 923 |
values[0] |= XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
| 924 |
/* Needed because some tray applications (e.g., VLC) use |
| 925 |
* override_redirect which causes no ConfigureRequest to be sent. */ |
| 926 |
values[0] |= XCB_EVENT_MASK_RESIZE_REDIRECT; |
| 927 |
|
| 928 |
xcb_change_window_attributes(xcb_connection, client, mask, values); |
| 929 |
|
| 930 |
/* Request the _XEMBED_INFO property. The XEMBED specification |
| 931 |
* (which is referred by the tray specification) says this *has* to |
| 932 |
* be set, but VLC does not set it… */ |
| 933 |
bool map_it = true; |
| 934 |
int xe_version = 1; |
| 935 |
xcb_get_property_cookie_t xembedc; |
| 936 |
xcb_generic_error_t *error; |
| 937 |
xembedc = xcb_get_property(xcb_connection, |
| 938 |
0, |
| 939 |
client, |
| 940 |
atoms[_XEMBED_INFO], |
| 941 |
XCB_GET_PROPERTY_TYPE_ANY, |
| 942 |
0, |
| 943 |
2 * 32); |
| 944 |
|
| 945 |
xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection, |
| 946 |
xembedc, |
| 947 |
&error); |
| 948 |
if (error != NULL) { |
| 949 |
ELOG("Error getting _XEMBED_INFO property: error_code %d\n", |
| 950 |
error->error_code); |
| 951 |
free(error); |
| 952 |
return; |
| 953 |
} |
| 954 |
if (xembedr != NULL && xembedr->length != 0) { |
| 955 |
DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length); |
| 956 |
uint32_t *xembed = xcb_get_property_value(xembedr); |
| 957 |
DLOG("xembed version = %d\n", xembed[0]); |
| 958 |
DLOG("xembed flags = %d\n", xembed[1]); |
| 959 |
map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED); |
| 960 |
xe_version = xembed[0]; |
| 961 |
if (xe_version > 1) { |
| 962 |
xe_version = 1; |
| 963 |
} |
| 964 |
free(xembedr); |
| 965 |
} else { |
| 966 |
ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client); |
| 967 |
} |
| 968 |
|
| 969 |
DLOG("X window %08x requested docking\n", client); |
| 970 |
|
| 971 |
if (output_for_tray == NULL) { |
| 972 |
ELOG("No output found for tray\n"); |
| 973 |
return; |
| 974 |
} |
| 975 |
|
| 976 |
xcb_void_cookie_t rcookie = xcb_reparent_window(xcb_connection, |
| 977 |
client, |
| 978 |
output_for_tray->bar.id, |
| 979 |
output_for_tray->rect.w - icon_size - logical_px(config.tray_padding), |
| 980 |
logical_px(config.tray_padding)); |
| 981 |
if (xcb_request_failed(rcookie, "Could not reparent window. Maybe it is using an incorrect depth/visual?")) { |
| 982 |
return; |
| 983 |
} |
| 984 |
|
| 985 |
/* We reconfigure the window to use a reasonable size. The systray |
| 986 |
* specification explicitly says: |
| 987 |
* Tray icons may be assigned any size by the system tray, and |
| 988 |
* should do their best to cope with any size effectively |
| 989 |
*/ |
| 990 |
mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; |
| 991 |
values[0] = icon_size; |
| 992 |
values[1] = icon_size; |
| 993 |
xcb_configure_window(xcb_connection, |
| 994 |
client, |
| 995 |
mask, |
| 996 |
values); |
| 997 |
|
| 998 |
/* send the XEMBED_EMBEDDED_NOTIFY message */ |
| 999 |
void *event = scalloc(32, 1); |
| 1000 |
xcb_client_message_event_t *ev = event; |
| 1001 |
ev->response_type = XCB_CLIENT_MESSAGE; |
| 1002 |
ev->window = client; |
| 1003 |
ev->type = atoms[_XEMBED]; |
| 1004 |
ev->format = 32; |
| 1005 |
ev->data.data32[0] = XCB_CURRENT_TIME; |
| 1006 |
ev->data.data32[1] = XEMBED_EMBEDDED_NOTIFY; |
| 1007 |
ev->data.data32[2] = output_for_tray->bar.id; |
| 1008 |
ev->data.data32[3] = xe_version; |
| 1009 |
xcb_send_event(xcb_connection, |
| 1010 |
0, |
| 1011 |
client, |
| 1012 |
XCB_EVENT_MASK_NO_EVENT, |
| 1013 |
(char *)ev); |
| 1014 |
free(event); |
| 1015 |
|
| 1016 |
/* Put the client inside the save set. Upon termination (whether |
| 1017 |
* killed or normal exit does not matter) of i3bar, these clients |
| 1018 |
* will be correctly reparented to their most closest living |
| 1019 |
* ancestor. Without this, tray icons might die when i3bar |
| 1020 |
* exits/crashes. */ |
| 1021 |
xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client); |
| 1022 |
|
| 1023 |
trayclient *tc = scalloc(1, sizeof(trayclient)); |
| 1024 |
tc->win = client; |
| 1025 |
tc->xe_version = xe_version; |
| 1026 |
tc->mapped = false; |
| 1027 |
TAILQ_INSERT_TAIL(output_for_tray->trayclients, tc, tailq); |
| 1028 |
trayclient_update_class(tc); |
| 1029 |
|
| 1030 |
if (map_it) { |
| 1031 |
DLOG("Mapping dock client\n"); |
| 1032 |
xcb_map_window(xcb_connection, client); |
| 1033 |
} else { |
| 1034 |
DLOG("Not mapping dock client yet\n"); |
| 1035 |
} |
| 1036 |
/* Trigger an update to copy the statusline text to the appropriate |
| 1037 |
* position */ |
| 1038 |
configure_trayclients(); |
| 1039 |
draw_bars(false); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* |
| 1045 |
* Handles DestroyNotify events by removing the tray client from the data |
| 1046 |
* structure. According to the XEmbed protocol, this is one way for a tray |
| 1047 |
* client to finish the protocol. After this event is received, there is no |
| 1048 |
* further interaction with the tray client. |
| 1049 |
* |
| 1050 |
* See: https://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html |
| 1051 |
* |
| 1052 |
*/ |
| 1053 |
static void handle_destroy_notify(xcb_destroy_notify_event_t *event) { |
| 1054 |
DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event); |
| 1055 |
|
| 1056 |
i3_output *output; |
| 1057 |
trayclient *client = trayclient_and_output_from_window(event->window, &output); |
| 1058 |
if (!client) { |
| 1059 |
DLOG("WARNING: Could not find corresponding tray window.\n"); |
| 1060 |
return; |
| 1061 |
} |
| 1062 |
|
| 1063 |
DLOG("Removing tray client with window ID %08x\n", event->window); |
| 1064 |
TAILQ_REMOVE(output->trayclients, client, tailq); |
| 1065 |
free(client->class_class); |
| 1066 |
free(client->class_instance); |
| 1067 |
FREE(client); |
| 1068 |
|
| 1069 |
/* Trigger an update, we now have more space for the statusline */ |
| 1070 |
configure_trayclients(); |
| 1071 |
draw_bars(false); |
| 1072 |
} |
| 1073 |
|
| 1074 |
/* |
| 1075 |
* Handles MapNotify events. These events happen when a tray client shows its |
| 1076 |
* window. We respond by realigning the tray clients. |
| 1077 |
* |
| 1078 |
*/ |
| 1079 |
static void handle_map_notify(xcb_map_notify_event_t *event) { |
| 1080 |
DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event); |
| 1081 |
|
| 1082 |
trayclient *client = trayclient_from_window(event->window); |
| 1083 |
if (!client) { |
| 1084 |
DLOG("WARNING: Could not find corresponding tray window.\n"); |
| 1085 |
return; |
| 1086 |
} |
| 1087 |
|
| 1088 |
DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window); |
| 1089 |
client->mapped = true; |
| 1090 |
|
| 1091 |
/* Trigger an update, we now have one extra tray client. */ |
| 1092 |
configure_trayclients(); |
| 1093 |
draw_bars(false); |
| 1094 |
} |
| 1095 |
/* |
| 1096 |
* Handles UnmapNotify events. These events happen when a tray client hides its |
| 1097 |
* window. We respond by realigning the tray clients. |
| 1098 |
* |
| 1099 |
*/ |
| 1100 |
static void handle_unmap_notify(xcb_unmap_notify_event_t *event) { |
| 1101 |
DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event); |
| 1102 |
|
| 1103 |
trayclient *client = trayclient_from_window(event->window); |
| 1104 |
if (!client) { |
| 1105 |
DLOG("WARNING: Could not find corresponding tray window.\n"); |
| 1106 |
return; |
| 1107 |
} |
| 1108 |
|
| 1109 |
DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window); |
| 1110 |
client->mapped = false; |
| 1111 |
|
| 1112 |
/* Trigger an update, we now have more space for the statusline */ |
| 1113 |
configure_trayclients(); |
| 1114 |
draw_bars(false); |
| 1115 |
} |
| 1116 |
|
| 1117 |
/* |
| 1118 |
* Handle PropertyNotify messages. |
| 1119 |
* |
| 1120 |
*/ |
| 1121 |
static void handle_property_notify(xcb_property_notify_event_t *event) { |
| 1122 |
DLOG("PropertyNotify\n"); |
| 1123 |
if (event->atom == atoms[_XEMBED_INFO] && |
| 1124 |
event->state == XCB_PROPERTY_NEW_VALUE) { |
| 1125 |
/* _XEMBED_INFO property tells us whether a dock client should be mapped or unmapped. */ |
| 1126 |
DLOG("xembed_info updated\n"); |
| 1127 |
|
| 1128 |
trayclient *client = trayclient_from_window(event->window); |
| 1129 |
if (!client) { |
| 1130 |
ELOG("PropertyNotify received for unknown window %08x\n", event->window); |
| 1131 |
return; |
| 1132 |
} |
| 1133 |
|
| 1134 |
xcb_get_property_cookie_t xembedc; |
| 1135 |
xembedc = xcb_get_property_unchecked(xcb_connection, |
| 1136 |
0, |
| 1137 |
client->win, |
| 1138 |
atoms[_XEMBED_INFO], |
| 1139 |
XCB_GET_PROPERTY_TYPE_ANY, |
| 1140 |
0, |
| 1141 |
2 * 32); |
| 1142 |
|
| 1143 |
xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection, |
| 1144 |
xembedc, |
| 1145 |
NULL); |
| 1146 |
if (xembedr == NULL || xembedr->length == 0) { |
| 1147 |
DLOG("xembed_info unset\n"); |
| 1148 |
return; |
| 1149 |
} |
| 1150 |
|
| 1151 |
DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length); |
| 1152 |
uint32_t *xembed = xcb_get_property_value(xembedr); |
| 1153 |
DLOG("xembed version = %d\n", xembed[0]); |
| 1154 |
DLOG("xembed flags = %d\n", xembed[1]); |
| 1155 |
bool map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED); |
| 1156 |
DLOG("map state now %d\n", map_it); |
| 1157 |
if (client->mapped && !map_it) { |
| 1158 |
/* need to unmap the window */ |
| 1159 |
xcb_unmap_window(xcb_connection, client->win); |
| 1160 |
} else if (!client->mapped && map_it) { |
| 1161 |
/* need to map the window */ |
| 1162 |
xcb_map_window(xcb_connection, client->win); |
| 1163 |
} |
| 1164 |
free(xembedr); |
| 1165 |
} else if (event->atom == XCB_ATOM_WM_CLASS) { |
| 1166 |
trayclient *client = trayclient_from_window(event->window); |
| 1167 |
if (client) { |
| 1168 |
trayclient_update_class(client); |
| 1169 |
} |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* |
| 1174 |
* If a tray client attempts to change its size we deny the request and respond |
| 1175 |
* by telling it its actual size. |
| 1176 |
* |
| 1177 |
*/ |
| 1178 |
static void handle_configuration_change(xcb_window_t window) { |
| 1179 |
trayclient *trayclient; |
| 1180 |
i3_output *output; |
| 1181 |
SLIST_FOREACH (output, outputs, slist) { |
| 1182 |
if (!output->active) { |
| 1183 |
continue; |
| 1184 |
} |
| 1185 |
|
| 1186 |
int clients = 0; |
| 1187 |
TAILQ_FOREACH_REVERSE (trayclient, output->trayclients, tc_head, tailq) { |
| 1188 |
if (!trayclient->mapped) { |
| 1189 |
continue; |
| 1190 |
} |
| 1191 |
clients++; |
| 1192 |
|
| 1193 |
if (trayclient->win != window) { |
| 1194 |
continue; |
| 1195 |
} |
| 1196 |
|
| 1197 |
xcb_rectangle_t rect; |
| 1198 |
rect.x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding))); |
| 1199 |
rect.y = logical_px(config.tray_padding); |
| 1200 |
rect.width = icon_size; |
| 1201 |
rect.height = icon_size; |
| 1202 |
|
| 1203 |
DLOG("This is a tray window. x = %d\n", rect.x); |
| 1204 |
fake_configure_notify(xcb_connection, rect, window, 0); |
| 1205 |
return; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
DLOG("WARNING: Could not find corresponding tray window.\n"); |
| 1210 |
} |
| 1211 |
|
| 1212 |
static void handle_configure_request(xcb_configure_request_event_t *event) { |
| 1213 |
DLOG("ConfigureRequest for window = %08x\n", event->window); |
| 1214 |
handle_configuration_change(event->window); |
| 1215 |
} |
| 1216 |
|
| 1217 |
static void handle_resize_request(xcb_resize_request_event_t *event) { |
| 1218 |
DLOG("ResizeRequest for window = %08x\n", event->window); |
| 1219 |
handle_configuration_change(event->window); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* |
| 1223 |
* This function is called immediately before the main loop locks. We check for |
| 1224 |
* events from X11, handle them, then flush our outgoing queue. |
| 1225 |
* |
| 1226 |
*/ |
| 1227 |
static void xcb_prep_cb(struct ev_loop *loop, ev_prepare *watcher, int revents) { |
| 1228 |
xcb_generic_event_t *event; |
| 1229 |
|
| 1230 |
if (xcb_connection_has_error(xcb_connection)) { |
| 1231 |
ELOG("X11 connection was closed unexpectedly - maybe your X server terminated / crashed?\n"); |
| 1232 |
#ifdef I3_ASAN_ENABLED |
| 1233 |
__lsan_do_leak_check(); |
| 1234 |
#endif |
| 1235 |
exit(1); |
| 1236 |
} |
| 1237 |
|
| 1238 |
while ((event = xcb_poll_for_event(xcb_connection)) != NULL) { |
| 1239 |
if (event->response_type == 0) { |
| 1240 |
xcb_generic_error_t *error = (xcb_generic_error_t *)event; |
| 1241 |
DLOG("Received X11 error, sequence 0x%x, error_code = %d\n", error->sequence, error->error_code); |
| 1242 |
free(event); |
| 1243 |
continue; |
| 1244 |
} |
| 1245 |
|
| 1246 |
int type = (event->response_type & ~0x80); |
| 1247 |
|
| 1248 |
if (type == xkb_base && xkb_base > -1) { |
| 1249 |
DLOG("received an xkb event\n"); |
| 1250 |
|
| 1251 |
xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event; |
| 1252 |
const uint32_t mod = (config.modifier & 0xFFFF); |
| 1253 |
const bool new_mod_pressed = (mod != 0 && (state->mods & mod) == mod); |
| 1254 |
if (new_mod_pressed != mod_pressed) { |
| 1255 |
mod_pressed = new_mod_pressed; |
| 1256 |
if (state->xkbType == XCB_XKB_STATE_NOTIFY && config.modifier != XCB_NONE) { |
| 1257 |
if (mod_pressed) { |
| 1258 |
activated_mode = false; |
| 1259 |
unhide_bars(); |
| 1260 |
} else if (!activated_mode) { |
| 1261 |
hide_bars(); |
| 1262 |
} |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
free(event); |
| 1267 |
continue; |
| 1268 |
} |
| 1269 |
|
| 1270 |
switch (type) { |
| 1271 |
case XCB_VISIBILITY_NOTIFY: |
| 1272 |
/* Visibility change: a bar is [un]obscured by other window */ |
| 1273 |
handle_visibility_notify((xcb_visibility_notify_event_t *)event); |
| 1274 |
break; |
| 1275 |
case XCB_EXPOSE: |
| 1276 |
if (((xcb_expose_event_t *)event)->count == 0) { |
| 1277 |
/* Expose-events happen, when the window needs to be redrawn */ |
| 1278 |
redraw_bars(); |
| 1279 |
} |
| 1280 |
|
| 1281 |
break; |
| 1282 |
case XCB_BUTTON_RELEASE: |
| 1283 |
case XCB_BUTTON_PRESS: |
| 1284 |
/* Button press events are mouse buttons clicked on one of our bars */ |
| 1285 |
handle_button((xcb_button_press_event_t *)event); |
| 1286 |
break; |
| 1287 |
case XCB_CLIENT_MESSAGE: |
| 1288 |
/* Client messages are used for client-to-client communication, for |
| 1289 |
* example system tray widgets talk to us directly via client messages. */ |
| 1290 |
handle_client_message((xcb_client_message_event_t *)event); |
| 1291 |
break; |
| 1292 |
case XCB_DESTROY_NOTIFY: |
| 1293 |
/* DestroyNotify signifies the end of the XEmbed protocol */ |
| 1294 |
handle_destroy_notify((xcb_destroy_notify_event_t *)event); |
| 1295 |
break; |
| 1296 |
case XCB_UNMAP_NOTIFY: |
| 1297 |
/* UnmapNotify is received when a tray client hides its window. */ |
| 1298 |
handle_unmap_notify((xcb_unmap_notify_event_t *)event); |
| 1299 |
break; |
| 1300 |
case XCB_MAP_NOTIFY: |
| 1301 |
handle_map_notify((xcb_map_notify_event_t *)event); |
| 1302 |
break; |
| 1303 |
case XCB_PROPERTY_NOTIFY: |
| 1304 |
/* PropertyNotify */ |
| 1305 |
handle_property_notify((xcb_property_notify_event_t *)event); |
| 1306 |
break; |
| 1307 |
case XCB_CONFIGURE_REQUEST: |
| 1308 |
/* ConfigureRequest, sent by a tray child */ |
| 1309 |
handle_configure_request((xcb_configure_request_event_t *)event); |
| 1310 |
break; |
| 1311 |
case XCB_RESIZE_REQUEST: |
| 1312 |
/* ResizeRequest sent by a tray child using override_redirect. */ |
| 1313 |
handle_resize_request((xcb_resize_request_event_t *)event); |
| 1314 |
break; |
| 1315 |
} |
| 1316 |
free(event); |
| 1317 |
} |
| 1318 |
|
| 1319 |
xcb_flush(xcb_connection); |
| 1320 |
} |
| 1321 |
|
| 1322 |
/* |
| 1323 |
* Dummy callback. We only need this, so that the prepare and check watchers |
| 1324 |
* are triggered |
| 1325 |
* |
| 1326 |
*/ |
| 1327 |
static void xcb_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { |
| 1328 |
} |
| 1329 |
|
| 1330 |
/* |
| 1331 |
* Early initialization of the connection to X11: Everything which does not |
| 1332 |
* depend on 'config'. |
| 1333 |
* |
| 1334 |
*/ |
| 1335 |
char *init_xcb_early(void) { |
| 1336 |
/* FIXME: xcb_connect leaks memory */ |
| 1337 |
xcb_connection = xcb_connect(NULL, &screen); |
| 1338 |
if (xcb_connection_has_error(xcb_connection)) { |
| 1339 |
ELOG("Cannot open display\n"); |
| 1340 |
exit(EXIT_FAILURE); |
| 1341 |
} |
| 1342 |
conn = xcb_connection; |
| 1343 |
DLOG("Connected to xcb\n"); |
| 1344 |
|
| 1345 |
/* We have to request the atoms we need */ |
| 1346 |
#define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name); |
| 1347 |
#include "xcb_atoms.def" |
| 1348 |
|
| 1349 |
root_screen = xcb_aux_get_screen(xcb_connection, screen); |
| 1350 |
xcb_root = root_screen->root; |
| 1351 |
|
| 1352 |
depth = root_screen->root_depth; |
| 1353 |
colormap = root_screen->default_colormap; |
| 1354 |
visual_type = config.transparency ? xcb_aux_find_visual_by_attrs(root_screen, -1, 32) : NULL; |
| 1355 |
if (visual_type != NULL) { |
| 1356 |
depth = xcb_aux_get_depth_of_visual(root_screen, visual_type->visual_id); |
| 1357 |
colormap = xcb_generate_id(xcb_connection); |
| 1358 |
xcb_void_cookie_t cm_cookie = xcb_create_colormap_checked(xcb_connection, |
| 1359 |
XCB_COLORMAP_ALLOC_NONE, |
| 1360 |
colormap, |
| 1361 |
xcb_root, |
| 1362 |
visual_type->visual_id); |
| 1363 |
if (xcb_request_failed(cm_cookie, "Could not allocate colormap")) { |
| 1364 |
exit(EXIT_FAILURE); |
| 1365 |
} |
| 1366 |
} else { |
| 1367 |
visual_type = get_visualtype(root_screen); |
| 1368 |
} |
| 1369 |
|
| 1370 |
xcb_cursor_context_t *cursor_ctx; |
| 1371 |
if (xcb_cursor_context_new(conn, root_screen, &cursor_ctx) < 0) { |
| 1372 |
errx(EXIT_FAILURE, "Cannot allocate xcursor context"); |
| 1373 |
} |
| 1374 |
cursor = xcb_cursor_load_cursor(cursor_ctx, "left_ptr"); |
| 1375 |
xcb_cursor_context_free(cursor_ctx); |
| 1376 |
|
| 1377 |
/* The various watchers to communicate with xcb */ |
| 1378 |
xcb_io = smalloc(sizeof(ev_io)); |
| 1379 |
xcb_prep = smalloc(sizeof(ev_prepare)); |
| 1380 |
|
| 1381 |
ev_io_init(xcb_io, &xcb_io_cb, xcb_get_file_descriptor(xcb_connection), EV_READ); |
| 1382 |
ev_prepare_init(xcb_prep, &xcb_prep_cb); |
| 1383 |
|
| 1384 |
ev_io_start(main_loop, xcb_io); |
| 1385 |
ev_prepare_start(main_loop, xcb_prep); |
| 1386 |
|
| 1387 |
/* Now we get the atoms and save them in a nice data structure */ |
| 1388 |
get_atoms(); |
| 1389 |
|
| 1390 |
char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen); |
| 1391 |
|
| 1392 |
return path; |
| 1393 |
} |
| 1394 |
|
| 1395 |
/* |
| 1396 |
* Register for xkb keyevents. To grab modifiers without blocking other applications from receiving key events |
| 1397 |
* involving that modifier, we sadly have to use xkb which is not yet fully supported |
| 1398 |
* in xcb. |
| 1399 |
* |
| 1400 |
*/ |
| 1401 |
static void register_xkb_keyevents(void) { |
| 1402 |
const xcb_query_extension_reply_t *extreply; |
| 1403 |
extreply = xcb_get_extension_data(conn, &xcb_xkb_id); |
| 1404 |
if (!extreply->present) { |
| 1405 |
ELOG("xkb is not present on this server\n"); |
| 1406 |
exit(EXIT_FAILURE); |
| 1407 |
} |
| 1408 |
DLOG("initializing xcb-xkb\n"); |
| 1409 |
xcb_xkb_use_extension(conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION); |
| 1410 |
xcb_xkb_select_events(conn, |
| 1411 |
XCB_XKB_ID_USE_CORE_KBD, |
| 1412 |
XCB_XKB_EVENT_TYPE_STATE_NOTIFY, |
| 1413 |
0, |
| 1414 |
XCB_XKB_EVENT_TYPE_STATE_NOTIFY, |
| 1415 |
0xff, |
| 1416 |
0xff, |
| 1417 |
NULL); |
| 1418 |
xkb_base = extreply->first_event; |
| 1419 |
} |
| 1420 |
|
| 1421 |
/* |
| 1422 |
* Deregister from xkb keyevents. |
| 1423 |
* |
| 1424 |
*/ |
| 1425 |
static void deregister_xkb_keyevents(void) { |
| 1426 |
xcb_xkb_select_events(conn, |
| 1427 |
XCB_XKB_ID_USE_CORE_KBD, |
| 1428 |
0, |
| 1429 |
0, |
| 1430 |
0, |
| 1431 |
0xff, |
| 1432 |
0xff, |
| 1433 |
NULL); |
| 1434 |
} |
| 1435 |
|
| 1436 |
/* |
| 1437 |
* Initialization which depends on 'config' being usable. Called after the |
| 1438 |
* configuration has arrived. |
| 1439 |
* |
| 1440 |
*/ |
| 1441 |
void init_xcb_late(char *fontname) { |
| 1442 |
if (fontname == NULL) { |
| 1443 |
fontname = "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1"; |
| 1444 |
} |
| 1445 |
|
| 1446 |
/* Load the font */ |
| 1447 |
font = load_font(fontname, true); |
| 1448 |
set_font(&font); |
| 1449 |
DLOG("Calculated font-height: %d\n", font.height); |
| 1450 |
|
| 1451 |
/* |
| 1452 |
* If the bar height was explicitly set (but padding was not set), use |
| 1453 |
* it. Otherwise, calculate it based on the font size. |
| 1454 |
*/ |
| 1455 |
const int default_px = font.height + 2 * logical_px(ws_voff_px); |
| 1456 |
int padding_scaled = |
| 1457 |
logical_px(config.padding.y) + |
| 1458 |
logical_px(config.padding.height); |
| 1459 |
if (config.bar_height > 0 && |
| 1460 |
config.padding.x == 0 && |
| 1461 |
config.padding.y == 0 && |
| 1462 |
config.padding.width == 0 && |
| 1463 |
config.padding.height == 0) { |
| 1464 |
padding_scaled = config.bar_height - default_px; |
| 1465 |
DLOG("setting padding_scaled=%d based on bar_height=%d\n", padding_scaled, config.bar_height); |
| 1466 |
} else { |
| 1467 |
DLOG("padding: x=%d, y=%d -> padding_scaled=%d\n", config.padding.x, config.padding.y, padding_scaled); |
| 1468 |
} |
| 1469 |
bar_height = default_px + padding_scaled; |
| 1470 |
icon_size = bar_height - 2 * logical_px(config.tray_padding); |
| 1471 |
|
| 1472 |
if (config.separator_symbol) { |
| 1473 |
separator_symbol_width = predict_text_width(config.separator_symbol); |
| 1474 |
} |
| 1475 |
|
| 1476 |
xcb_flush(xcb_connection); |
| 1477 |
|
| 1478 |
if (config.hide_on_modifier == M_HIDE) { |
| 1479 |
register_xkb_keyevents(); |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
/* |
| 1484 |
* Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the |
| 1485 |
* selection. |
| 1486 |
* |
| 1487 |
*/ |
| 1488 |
static void send_tray_clientmessage(void) { |
| 1489 |
uint8_t buffer[32] = {0}; |
| 1490 |
xcb_client_message_event_t *ev = (xcb_client_message_event_t *)buffer; |
| 1491 |
|
| 1492 |
ev->response_type = XCB_CLIENT_MESSAGE; |
| 1493 |
ev->window = xcb_root; |
| 1494 |
ev->type = atoms[MANAGER]; |
| 1495 |
ev->format = 32; |
| 1496 |
ev->data.data32[0] = XCB_CURRENT_TIME; |
| 1497 |
ev->data.data32[1] = tray_reply->atom; |
| 1498 |
ev->data.data32[2] = selwin; |
| 1499 |
|
| 1500 |
xcb_send_event(xcb_connection, |
| 1501 |
0, |
| 1502 |
xcb_root, |
| 1503 |
0xFFFFFF, |
| 1504 |
(char *)buffer); |
| 1505 |
} |
| 1506 |
|
| 1507 |
/* |
| 1508 |
* Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom |
| 1509 |
* for the X11 display we are running on, then acquiring the selection for this |
| 1510 |
* atom. Afterwards, tray clients will send ClientMessages to our window. |
| 1511 |
* |
| 1512 |
*/ |
| 1513 |
static void init_tray(void) { |
| 1514 |
DLOG("Initializing system tray functionality\n"); |
| 1515 |
/* request the tray manager atom for the X11 display we are running on */ |
| 1516 |
/* The following line cannot use strlen as that makes compilation fail with |
| 1517 |
* some versions of clang (-Wgnu-folding-constant): */ |
| 1518 |
const size_t systray_len = strlen("_NET_SYSTEM_TRAY_S") + 11; |
| 1519 |
char atomname[systray_len]; |
| 1520 |
snprintf(atomname, systray_len, "_NET_SYSTEM_TRAY_S%d", screen); |
| 1521 |
xcb_intern_atom_cookie_t tray_cookie; |
| 1522 |
if (tray_reply == NULL) { |
| 1523 |
tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname); |
| 1524 |
} |
| 1525 |
|
| 1526 |
/* tray support: we need a window to own the selection */ |
| 1527 |
selwin = xcb_generate_id(xcb_connection); |
| 1528 |
uint32_t selmask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_COLORMAP; |
| 1529 |
uint32_t selval[] = {root_screen->black_pixel, root_screen->black_pixel, 1, colormap}; |
| 1530 |
xcb_create_window(xcb_connection, |
| 1531 |
depth, |
| 1532 |
selwin, |
| 1533 |
xcb_root, |
| 1534 |
-1, -1, |
| 1535 |
1, 1, |
| 1536 |
0, |
| 1537 |
XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1538 |
visual_type->visual_id, |
| 1539 |
selmask, |
| 1540 |
selval); |
| 1541 |
|
| 1542 |
uint32_t orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ; |
| 1543 |
/* set the atoms */ |
| 1544 |
xcb_change_property(xcb_connection, |
| 1545 |
XCB_PROP_MODE_REPLACE, |
| 1546 |
selwin, |
| 1547 |
atoms[_NET_SYSTEM_TRAY_ORIENTATION], |
| 1548 |
XCB_ATOM_CARDINAL, |
| 1549 |
32, |
| 1550 |
1, |
| 1551 |
&orientation); |
| 1552 |
xcb_change_property(xcb_connection, |
| 1553 |
XCB_PROP_MODE_REPLACE, |
| 1554 |
selwin, |
| 1555 |
atoms[_NET_SYSTEM_TRAY_VISUAL], |
| 1556 |
XCB_ATOM_VISUALID, |
| 1557 |
32, |
| 1558 |
1, |
| 1559 |
&visual_type->visual_id); |
| 1560 |
|
| 1561 |
init_tray_colors(); |
| 1562 |
|
| 1563 |
if (tray_reply == NULL) { |
| 1564 |
if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) { |
| 1565 |
ELOG("Could not get atom %s\n", atomname); |
| 1566 |
exit(EXIT_FAILURE); |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
xcb_set_selection_owner(xcb_connection, |
| 1571 |
selwin, |
| 1572 |
tray_reply->atom, |
| 1573 |
XCB_CURRENT_TIME); |
| 1574 |
|
| 1575 |
/* Verify that we have the selection */ |
| 1576 |
xcb_get_selection_owner_cookie_t selcookie; |
| 1577 |
xcb_get_selection_owner_reply_t *selreply; |
| 1578 |
|
| 1579 |
selcookie = xcb_get_selection_owner(xcb_connection, tray_reply->atom); |
| 1580 |
if (!(selreply = xcb_get_selection_owner_reply(xcb_connection, selcookie, NULL))) { |
| 1581 |
ELOG("Could not get selection owner for %s\n", atomname); |
| 1582 |
exit(EXIT_FAILURE); |
| 1583 |
} |
| 1584 |
|
| 1585 |
if (selreply->owner != selwin) { |
| 1586 |
ELOG("Could not set the %s selection. " |
| 1587 |
"Maybe another tray is already running?\n", |
| 1588 |
atomname); |
| 1589 |
/* NOTE that this error is not fatal. We just can’t provide tray |
| 1590 |
* functionality */ |
| 1591 |
free(selreply); |
| 1592 |
return; |
| 1593 |
} |
| 1594 |
|
| 1595 |
free(selreply); |
| 1596 |
|
| 1597 |
send_tray_clientmessage(); |
| 1598 |
} |
| 1599 |
|
| 1600 |
/* |
| 1601 |
* We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window |
| 1602 |
* to make GTK+ 3 applets with symbolic icons visible. If the colors are unset, |
| 1603 |
* they assume a light background. |
| 1604 |
* See also https://bugzilla.gnome.org/show_bug.cgi?id=679591 |
| 1605 |
* |
| 1606 |
*/ |
| 1607 |
void init_tray_colors(void) { |
| 1608 |
/* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */ |
| 1609 |
const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF"); |
| 1610 |
|
| 1611 |
DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg); |
| 1612 |
|
| 1613 |
char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'}, |
| 1614 |
{bar_fg[3], bar_fg[4], '\0'}, |
| 1615 |
{bar_fg[5], bar_fg[6], '\0'}}; |
| 1616 |
const uint8_t r = strtol(strgroups[0], NULL, 16); |
| 1617 |
const uint8_t g = strtol(strgroups[1], NULL, 16); |
| 1618 |
const uint8_t b = strtol(strgroups[2], NULL, 16); |
| 1619 |
|
| 1620 |
const uint16_t r16 = ((uint16_t)r << 8) | r; |
| 1621 |
const uint16_t g16 = ((uint16_t)g << 8) | g; |
| 1622 |
const uint16_t b16 = ((uint16_t)b << 8) | b; |
| 1623 |
|
| 1624 |
const uint32_t tray_colors[12] = { |
| 1625 |
r16, g16, b16, /* foreground color */ |
| 1626 |
r16, g16, b16, /* error color */ |
| 1627 |
r16, g16, b16, /* warning color */ |
| 1628 |
r16, g16, b16, /* success color */ |
| 1629 |
}; |
| 1630 |
|
| 1631 |
xcb_change_property(xcb_connection, |
| 1632 |
XCB_PROP_MODE_REPLACE, |
| 1633 |
selwin, |
| 1634 |
atoms[_NET_SYSTEM_TRAY_COLORS], |
| 1635 |
XCB_ATOM_CARDINAL, |
| 1636 |
32, |
| 1637 |
12, |
| 1638 |
tray_colors); |
| 1639 |
} |
| 1640 |
|
| 1641 |
/* |
| 1642 |
* Cleanup the xcb stuff. |
| 1643 |
* Called once, before the program terminates. |
| 1644 |
* |
| 1645 |
*/ |
| 1646 |
void clean_xcb(void) { |
| 1647 |
free_outputs(); |
| 1648 |
|
| 1649 |
free_font(); |
| 1650 |
|
| 1651 |
xcb_free_cursor(xcb_connection, cursor); |
| 1652 |
xcb_aux_sync(xcb_connection); |
| 1653 |
xcb_disconnect(xcb_connection); |
| 1654 |
|
| 1655 |
ev_prepare_stop(main_loop, xcb_prep); |
| 1656 |
ev_io_stop(main_loop, xcb_io); |
| 1657 |
|
| 1658 |
FREE(xcb_prep); |
| 1659 |
FREE(xcb_io); |
| 1660 |
} |
| 1661 |
|
| 1662 |
/* |
| 1663 |
* Get the earlier requested atoms and save them in the prepared data structure |
| 1664 |
* |
| 1665 |
*/ |
| 1666 |
void get_atoms(void) { |
| 1667 |
xcb_intern_atom_reply_t *reply; |
| 1668 |
#define ATOM_DO(name) \ |
| 1669 |
reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \ |
| 1670 |
if (reply == NULL) { \ |
| 1671 |
ELOG("Could not get atom %s\n", #name); \ |
| 1672 |
exit(EXIT_FAILURE); \ |
| 1673 |
} \ |
| 1674 |
atoms[name] = reply->atom; \ |
| 1675 |
free(reply); |
| 1676 |
|
| 1677 |
#include "xcb_atoms.def" |
| 1678 |
DLOG("Got atoms\n"); |
| 1679 |
} |
| 1680 |
|
| 1681 |
/* |
| 1682 |
* Reparents all tray clients of the specified output to the root window. This |
| 1683 |
* is either used when shutting down, when an output appears (xrandr --output |
| 1684 |
* VGA1 --off) or when the primary output changes. |
| 1685 |
* |
| 1686 |
* Applications using the tray will start the protocol from the beginning again |
| 1687 |
* afterwards. |
| 1688 |
* |
| 1689 |
*/ |
| 1690 |
void kick_tray_clients(i3_output *output) { |
| 1691 |
if (TAILQ_EMPTY(output->trayclients)) { |
| 1692 |
return; |
| 1693 |
} |
| 1694 |
|
| 1695 |
trayclient *trayclient; |
| 1696 |
while (!TAILQ_EMPTY(output->trayclients)) { |
| 1697 |
trayclient = TAILQ_FIRST(output->trayclients); |
| 1698 |
/* Unmap, then reparent (to root) the tray client windows */ |
| 1699 |
xcb_unmap_window(xcb_connection, trayclient->win); |
| 1700 |
xcb_reparent_window(xcb_connection, |
| 1701 |
trayclient->win, |
| 1702 |
xcb_root, |
| 1703 |
0, |
| 1704 |
0); |
| 1705 |
|
| 1706 |
free(trayclient->class_class); |
| 1707 |
free(trayclient->class_instance); |
| 1708 |
|
| 1709 |
/* We remove the trayclient right here. We might receive an UnmapNotify |
| 1710 |
* event afterwards, but better safe than sorry. */ |
| 1711 |
TAILQ_REMOVE(output->trayclients, trayclient, tailq); |
| 1712 |
FREE(trayclient); |
| 1713 |
} |
| 1714 |
|
| 1715 |
/* Fake a DestroyNotify so that Qt re-adds tray icons. |
| 1716 |
* We cannot actually destroy the window because then Qt will not restore |
| 1717 |
* its event mask on the new window. */ |
| 1718 |
uint8_t buffer[32] = {0}; |
| 1719 |
xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t *)buffer; |
| 1720 |
|
| 1721 |
event->response_type = XCB_DESTROY_NOTIFY; |
| 1722 |
event->event = selwin; |
| 1723 |
event->window = selwin; |
| 1724 |
|
| 1725 |
xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char *)event); |
| 1726 |
|
| 1727 |
send_tray_clientmessage(); |
| 1728 |
} |
| 1729 |
|
| 1730 |
/* |
| 1731 |
* Destroy the bar of the specified output |
| 1732 |
* |
| 1733 |
*/ |
| 1734 |
void destroy_window(i3_output *output) { |
| 1735 |
if (output == NULL || output->bar.id == XCB_NONE) { |
| 1736 |
return; |
| 1737 |
} |
| 1738 |
|
| 1739 |
kick_tray_clients(output); |
| 1740 |
|
| 1741 |
draw_util_surface_free(xcb_connection, &(output->bar)); |
| 1742 |
draw_util_surface_free(xcb_connection, &(output->buffer)); |
| 1743 |
draw_util_surface_free(xcb_connection, &(output->statusline_buffer)); |
| 1744 |
xcb_destroy_window(xcb_connection, output->bar.id); |
| 1745 |
xcb_free_pixmap(xcb_connection, output->buffer.id); |
| 1746 |
xcb_free_pixmap(xcb_connection, output->statusline_buffer.id); |
| 1747 |
output->bar.id = XCB_NONE; |
| 1748 |
} |
| 1749 |
|
| 1750 |
/* Strut partial tells i3 where to reserve space for i3bar. This is determined |
| 1751 |
* by the `position` bar config directive. */ |
| 1752 |
static xcb_void_cookie_t config_strut_partial(i3_output *output) { |
| 1753 |
/* A local struct to save the strut_partial property */ |
| 1754 |
struct { |
| 1755 |
uint32_t left; |
| 1756 |
uint32_t right; |
| 1757 |
uint32_t top; |
| 1758 |
uint32_t bottom; |
| 1759 |
uint32_t left_start_y; |
| 1760 |
uint32_t left_end_y; |
| 1761 |
uint32_t right_start_y; |
| 1762 |
uint32_t right_end_y; |
| 1763 |
uint32_t top_start_x; |
| 1764 |
uint32_t top_end_x; |
| 1765 |
uint32_t bottom_start_x; |
| 1766 |
uint32_t bottom_end_x; |
| 1767 |
} __attribute__((__packed__)) strut_partial; |
| 1768 |
memset(&strut_partial, 0, sizeof(strut_partial)); |
| 1769 |
|
| 1770 |
switch (config.position) { |
| 1771 |
case POS_NONE: |
| 1772 |
break; |
| 1773 |
case POS_TOP: |
| 1774 |
strut_partial.top = bar_height; |
| 1775 |
strut_partial.top_start_x = output->rect.x; |
| 1776 |
strut_partial.top_end_x = output->rect.x + output->rect.w; |
| 1777 |
break; |
| 1778 |
case POS_BOT: |
| 1779 |
strut_partial.bottom = bar_height; |
| 1780 |
strut_partial.bottom_start_x = output->rect.x; |
| 1781 |
strut_partial.bottom_end_x = output->rect.x + output->rect.w; |
| 1782 |
break; |
| 1783 |
} |
| 1784 |
return xcb_change_property(xcb_connection, |
| 1785 |
XCB_PROP_MODE_REPLACE, |
| 1786 |
output->bar.id, |
| 1787 |
atoms[_NET_WM_STRUT_PARTIAL], |
| 1788 |
XCB_ATOM_CARDINAL, |
| 1789 |
32, |
| 1790 |
12, |
| 1791 |
&strut_partial); |
| 1792 |
} |
| 1793 |
|
| 1794 |
/* |
| 1795 |
* Returns the output which should hold the tray, if one exists. |
| 1796 |
* |
| 1797 |
* An output is returned in these scenarios: |
| 1798 |
* 1. A specific output was listed in tray_outputs which is also in the list |
| 1799 |
* of outputs managed by this bar. |
| 1800 |
* 2. No tray_output directive was specified. In this case, we use the first |
| 1801 |
* available output. |
| 1802 |
* 3. 'tray_output primary' was specified. In this case we use the primary |
| 1803 |
* output. |
| 1804 |
* |
| 1805 |
* Three scenarios in which we specifically don't want to use a tray: |
| 1806 |
* 1. 'tray_output none' was specified. |
| 1807 |
* 2. A specific output was listed as a tray_output, but is not one of the |
| 1808 |
* outputs managed by this bar. For example, consider tray_outputs == [VGA-1], |
| 1809 |
* but outputs == [HDMI-1]. |
| 1810 |
* 3. 'tray_output primary' was specified and no output in the list is |
| 1811 |
* primary. |
| 1812 |
*/ |
| 1813 |
static i3_output *get_tray_output(void) { |
| 1814 |
i3_output *output = NULL; |
| 1815 |
if (TAILQ_EMPTY(&(config.tray_outputs))) { |
| 1816 |
/* No tray_output specified, use first active output. */ |
| 1817 |
SLIST_FOREACH (output, outputs, slist) { |
| 1818 |
if (output->active) { |
| 1819 |
return output; |
| 1820 |
} |
| 1821 |
} |
| 1822 |
return NULL; |
| 1823 |
} else if (strcasecmp(TAILQ_FIRST(&(config.tray_outputs))->output, "none") == 0) { |
| 1824 |
/* Check for "tray_output none" */ |
| 1825 |
return NULL; |
| 1826 |
} |
| 1827 |
|
| 1828 |
/* If one or more tray_output assignments were specified, we ensure that at |
| 1829 |
* least one of them is actually an output managed by this instance. */ |
| 1830 |
tray_output_t *tray_output; |
| 1831 |
TAILQ_FOREACH (tray_output, &(config.tray_outputs), tray_outputs) { |
| 1832 |
SLIST_FOREACH (output, outputs, slist) { |
| 1833 |
if (output->active && |
| 1834 |
(strcasecmp(output->name, tray_output->output) == 0 || |
| 1835 |
(strcasecmp(tray_output->output, "primary") == 0 && output->primary))) { |
| 1836 |
return output; |
| 1837 |
} |
| 1838 |
} |
| 1839 |
} |
| 1840 |
|
| 1841 |
return NULL; |
| 1842 |
} |
| 1843 |
|
| 1844 |
/* |
| 1845 |
* Reconfigure all bars and create new bars for recently activated outputs |
| 1846 |
* |
| 1847 |
*/ |
| 1848 |
void reconfig_windows(bool redraw_bars) { |
| 1849 |
uint32_t mask; |
| 1850 |
uint32_t values[6]; |
| 1851 |
|
| 1852 |
i3_output *walk; |
| 1853 |
SLIST_FOREACH (walk, outputs, slist) { |
| 1854 |
if (!walk->active) { |
| 1855 |
/* If an output is not active, we destroy its bar */ |
| 1856 |
/* FIXME: Maybe we rather want to unmap? */ |
| 1857 |
DLOG("Destroying window for output %s\n", walk->name); |
| 1858 |
destroy_window(walk); |
| 1859 |
continue; |
| 1860 |
} |
| 1861 |
if (walk->bar.id == XCB_NONE) { |
| 1862 |
DLOG("Creating window for output %s\n", walk->name); |
| 1863 |
|
| 1864 |
xcb_window_t bar_id = xcb_generate_id(xcb_connection); |
| 1865 |
xcb_pixmap_t buffer_id = xcb_generate_id(xcb_connection); |
| 1866 |
xcb_pixmap_t statusline_buffer_id = xcb_generate_id(xcb_connection); |
| 1867 |
mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP | XCB_CW_CURSOR; |
| 1868 |
|
| 1869 |
values[0] = colors.bar_bg.colorpixel; |
| 1870 |
values[1] = root_screen->black_pixel; |
| 1871 |
/* If hide_on_modifier is set to hide or invisible mode, i3 is not supposed to manage our bar windows */ |
| 1872 |
values[2] = (config.hide_on_modifier == M_DOCK ? 0 : 1); |
| 1873 |
/* We enable the following EventMask fields: |
| 1874 |
* EXPOSURE, to get expose events (we have to re-draw then) |
| 1875 |
* SUBSTRUCTURE_REDIRECT, to get ConfigureRequests when the tray |
| 1876 |
* child windows use ConfigureWindow |
| 1877 |
* BUTTON_PRESS, to handle clicks on the workspace buttons |
| 1878 |
* */ |
| 1879 |
values[3] = XCB_EVENT_MASK_EXPOSURE | |
| 1880 |
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | |
| 1881 |
XCB_EVENT_MASK_BUTTON_PRESS | |
| 1882 |
XCB_EVENT_MASK_BUTTON_RELEASE; |
| 1883 |
if (config.hide_on_modifier == M_DOCK) { |
| 1884 |
/* If the bar is normally visible, catch visibility change events to suspend |
| 1885 |
* the status process when the bar is obscured by full-screened windows. */ |
| 1886 |
values[3] |= XCB_EVENT_MASK_VISIBILITY_CHANGE; |
| 1887 |
walk->visible = true; |
| 1888 |
} |
| 1889 |
values[4] = colormap; |
| 1890 |
values[5] = cursor; |
| 1891 |
|
| 1892 |
xcb_void_cookie_t win_cookie = xcb_create_window_checked(xcb_connection, |
| 1893 |
depth, |
| 1894 |
bar_id, |
| 1895 |
xcb_root, |
| 1896 |
walk->rect.x, walk->rect.y + walk->rect.h - bar_height, |
| 1897 |
walk->rect.w, bar_height, |
| 1898 |
0, |
| 1899 |
XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1900 |
visual_type->visual_id, |
| 1901 |
mask, |
| 1902 |
values); |
| 1903 |
|
| 1904 |
/* The double-buffer we use to render stuff off-screen */ |
| 1905 |
xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection, |
| 1906 |
depth, |
| 1907 |
buffer_id, |
| 1908 |
bar_id, |
| 1909 |
walk->rect.w, |
| 1910 |
bar_height); |
| 1911 |
|
| 1912 |
/* The double-buffer we use to render the statusline before copying to buffer */ |
| 1913 |
xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection, |
| 1914 |
depth, |
| 1915 |
statusline_buffer_id, |
| 1916 |
bar_id, |
| 1917 |
walk->rect.w, |
| 1918 |
bar_height); |
| 1919 |
|
| 1920 |
/* Set the WM_CLASS and WM_NAME (we don't need UTF-8) atoms */ |
| 1921 |
char *class; |
| 1922 |
int len = sasprintf(&class, "%s%ci3bar%c", config.bar_id, 0, 0); |
| 1923 |
xcb_void_cookie_t class_cookie; |
| 1924 |
class_cookie = xcb_change_property(xcb_connection, |
| 1925 |
XCB_PROP_MODE_REPLACE, |
| 1926 |
bar_id, |
| 1927 |
XCB_ATOM_WM_CLASS, |
| 1928 |
XCB_ATOM_STRING, |
| 1929 |
8, |
| 1930 |
len, |
| 1931 |
class); |
| 1932 |
free(class); |
| 1933 |
|
| 1934 |
char *name; |
| 1935 |
sasprintf(&name, "i3bar for output %s", walk->name); |
| 1936 |
xcb_void_cookie_t name_cookie; |
| 1937 |
name_cookie = xcb_change_property(xcb_connection, |
| 1938 |
XCB_PROP_MODE_REPLACE, |
| 1939 |
bar_id, |
| 1940 |
XCB_ATOM_WM_NAME, |
| 1941 |
XCB_ATOM_STRING, |
| 1942 |
8, |
| 1943 |
strlen(name), |
| 1944 |
name); |
| 1945 |
free(name); |
| 1946 |
|
| 1947 |
/* We want dock windows (for now). When override_redirect is set, i3 is ignoring |
| 1948 |
* this one */ |
| 1949 |
xcb_void_cookie_t dock_cookie = xcb_change_property(xcb_connection, |
| 1950 |
XCB_PROP_MODE_REPLACE, |
| 1951 |
bar_id, |
| 1952 |
atoms[_NET_WM_WINDOW_TYPE], |
| 1953 |
XCB_ATOM_ATOM, |
| 1954 |
32, |
| 1955 |
1, |
| 1956 |
(unsigned char *)&atoms[_NET_WM_WINDOW_TYPE_DOCK]); |
| 1957 |
|
| 1958 |
draw_util_surface_init(xcb_connection, &walk->bar, bar_id, NULL, walk->rect.w, bar_height); |
| 1959 |
draw_util_surface_init(xcb_connection, &walk->buffer, buffer_id, NULL, walk->rect.w, bar_height); |
| 1960 |
draw_util_surface_init(xcb_connection, &walk->statusline_buffer, statusline_buffer_id, NULL, walk->rect.w, bar_height); |
| 1961 |
|
| 1962 |
xcb_void_cookie_t strut_cookie = config_strut_partial(walk); |
| 1963 |
|
| 1964 |
/* We finally map the bar (display it on screen), unless the modifier-switch is on */ |
| 1965 |
xcb_void_cookie_t map_cookie; |
| 1966 |
if (config.hide_on_modifier == M_DOCK) { |
| 1967 |
map_cookie = xcb_map_window_checked(xcb_connection, bar_id); |
| 1968 |
} |
| 1969 |
|
| 1970 |
if (xcb_request_failed(win_cookie, "Could not create window") || |
| 1971 |
xcb_request_failed(pm_cookie, "Could not create pixmap") || |
| 1972 |
xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") || |
| 1973 |
xcb_request_failed(dock_cookie, "Could not set dock mode") || |
| 1974 |
xcb_request_failed(class_cookie, "Could not set WM_CLASS") || |
| 1975 |
xcb_request_failed(name_cookie, "Could not set WM_NAME") || |
| 1976 |
xcb_request_failed(strut_cookie, "Could not set strut") || |
| 1977 |
((config.hide_on_modifier == M_DOCK) && xcb_request_failed(map_cookie, "Could not map window"))) { |
| 1978 |
exit(EXIT_FAILURE); |
| 1979 |
} |
| 1980 |
|
| 1981 |
} else { |
| 1982 |
/* We already have a bar, so we just reconfigure it */ |
| 1983 |
mask = XCB_CONFIG_WINDOW_X | |
| 1984 |
XCB_CONFIG_WINDOW_Y | |
| 1985 |
XCB_CONFIG_WINDOW_WIDTH | |
| 1986 |
XCB_CONFIG_WINDOW_HEIGHT | |
| 1987 |
XCB_CONFIG_WINDOW_STACK_MODE; |
| 1988 |
values[0] = walk->rect.x; |
| 1989 |
if (config.position == POS_TOP) { |
| 1990 |
values[1] = walk->rect.y; |
| 1991 |
} else { |
| 1992 |
values[1] = walk->rect.y + walk->rect.h - bar_height; |
| 1993 |
} |
| 1994 |
values[2] = walk->rect.w; |
| 1995 |
values[3] = bar_height; |
| 1996 |
values[4] = XCB_STACK_MODE_ABOVE; |
| 1997 |
|
| 1998 |
DLOG("Reconfiguring strut partial property for output %s\n", walk->name); |
| 1999 |
xcb_void_cookie_t strut_cookie = config_strut_partial(walk); |
| 2000 |
|
| 2001 |
DLOG("Destroying buffer for output %s\n", walk->name); |
| 2002 |
xcb_free_pixmap(xcb_connection, walk->buffer.id); |
| 2003 |
|
| 2004 |
DLOG("Destroying statusline buffer for output %s\n", walk->name); |
| 2005 |
xcb_free_pixmap(xcb_connection, walk->statusline_buffer.id); |
| 2006 |
|
| 2007 |
DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]); |
| 2008 |
xcb_void_cookie_t cfg_cookie = xcb_configure_window_checked(xcb_connection, |
| 2009 |
walk->bar.id, |
| 2010 |
mask, |
| 2011 |
values); |
| 2012 |
|
| 2013 |
mask = XCB_CW_OVERRIDE_REDIRECT; |
| 2014 |
values[0] = (config.hide_on_modifier == M_DOCK ? 0 : 1); |
| 2015 |
DLOG("Changing window attribute override_redirect for output %s to %d\n", walk->name, values[0]); |
| 2016 |
xcb_void_cookie_t chg_cookie = xcb_change_window_attributes(xcb_connection, |
| 2017 |
walk->bar.id, |
| 2018 |
mask, |
| 2019 |
values); |
| 2020 |
|
| 2021 |
DLOG("Recreating buffer for output %s\n", walk->name); |
| 2022 |
xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection, |
| 2023 |
depth, |
| 2024 |
walk->buffer.id, |
| 2025 |
walk->bar.id, |
| 2026 |
walk->rect.w, |
| 2027 |
bar_height); |
| 2028 |
|
| 2029 |
DLOG("Recreating statusline buffer for output %s\n", walk->name); |
| 2030 |
xcb_void_cookie_t slpm_cookie = xcb_create_pixmap_checked(xcb_connection, |
| 2031 |
depth, |
| 2032 |
walk->statusline_buffer.id, |
| 2033 |
walk->bar.id, |
| 2034 |
walk->rect.w, |
| 2035 |
bar_height); |
| 2036 |
|
| 2037 |
draw_util_surface_free(xcb_connection, &(walk->bar)); |
| 2038 |
draw_util_surface_free(xcb_connection, &(walk->buffer)); |
| 2039 |
draw_util_surface_free(xcb_connection, &(walk->statusline_buffer)); |
| 2040 |
draw_util_surface_init(xcb_connection, &(walk->bar), walk->bar.id, NULL, walk->rect.w, bar_height); |
| 2041 |
draw_util_surface_init(xcb_connection, &(walk->buffer), walk->buffer.id, NULL, walk->rect.w, bar_height); |
| 2042 |
draw_util_surface_init(xcb_connection, &(walk->statusline_buffer), walk->statusline_buffer.id, NULL, walk->rect.w, bar_height); |
| 2043 |
|
| 2044 |
xcb_void_cookie_t map_cookie, umap_cookie; |
| 2045 |
if (redraw_bars) { |
| 2046 |
/* Unmap the window, and draw it again when in dock mode */ |
| 2047 |
umap_cookie = xcb_unmap_window_checked(xcb_connection, walk->bar.id); |
| 2048 |
if (config.hide_on_modifier == M_DOCK) { |
| 2049 |
cont_children(); |
| 2050 |
map_cookie = xcb_map_window_checked(xcb_connection, walk->bar.id); |
| 2051 |
} else { |
| 2052 |
stop_children(); |
| 2053 |
} |
| 2054 |
|
| 2055 |
if (config.hide_on_modifier == M_HIDE) { |
| 2056 |
/* Switching to hide mode, register for keyevents */ |
| 2057 |
register_xkb_keyevents(); |
| 2058 |
} else { |
| 2059 |
/* Switching to dock/invisible mode, deregister from keyevents */ |
| 2060 |
deregister_xkb_keyevents(); |
| 2061 |
} |
| 2062 |
} |
| 2063 |
|
| 2064 |
if (xcb_request_failed(cfg_cookie, "Could not reconfigure window") || |
| 2065 |
xcb_request_failed(chg_cookie, "Could not change window") || |
| 2066 |
xcb_request_failed(pm_cookie, "Could not create pixmap") || |
| 2067 |
xcb_request_failed(slpm_cookie, "Could not create statusline pixmap") || |
| 2068 |
xcb_request_failed(strut_cookie, "Could not set strut") || |
| 2069 |
(redraw_bars && (xcb_request_failed(umap_cookie, "Could not unmap window") || |
| 2070 |
(config.hide_on_modifier == M_DOCK && xcb_request_failed(map_cookie, "Could not map window"))))) { |
| 2071 |
exit(EXIT_FAILURE); |
| 2072 |
} |
| 2073 |
} |
| 2074 |
} |
| 2075 |
|
| 2076 |
/* Finally, check if we want to initialize the tray or destroy the selection |
| 2077 |
* window. The result of get_tray_output() is cached. */ |
| 2078 |
output_for_tray = get_tray_output(); |
| 2079 |
if (output_for_tray) { |
| 2080 |
if (selwin == XCB_NONE) { |
| 2081 |
init_tray(); |
| 2082 |
} |
| 2083 |
} else if (selwin != XCB_NONE) { |
| 2084 |
DLOG("Destroying tray selection window\n"); |
| 2085 |
xcb_destroy_window(xcb_connection, selwin); |
| 2086 |
selwin = XCB_NONE; |
| 2087 |
} |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* |
| 2091 |
* Draw the button for a workspace or the current binding mode indicator. |
| 2092 |
* |
| 2093 |
*/ |
| 2094 |
static void draw_button(surface_t *surface, color_t fg_color, color_t bg_color, color_t border_color, |
| 2095 |
int x, int width, int text_width, i3String *text) { |
| 2096 |
int height = bar_height - 2 * logical_px(1); |
| 2097 |
|
| 2098 |
/* Draw the border of the button. */ |
| 2099 |
draw_util_rectangle(surface, border_color, x, logical_px(1), width, height); |
| 2100 |
|
| 2101 |
/* Draw the inside of the button. */ |
| 2102 |
draw_util_rectangle(surface, bg_color, x + logical_px(1), 2 * logical_px(1), |
| 2103 |
width - 2 * logical_px(1), height - 2 * logical_px(1)); |
| 2104 |
|
| 2105 |
draw_util_text(text, surface, fg_color, bg_color, x + (width - text_width) / 2, |
| 2106 |
bar_height / 2 - font.height / 2, text_width); |
| 2107 |
} |
| 2108 |
|
| 2109 |
/* |
| 2110 |
* Render the bars, with buttons and statusline |
| 2111 |
* |
| 2112 |
*/ |
| 2113 |
void draw_bars(bool unhide) { |
| 2114 |
DLOG("Drawing bars...\n"); |
| 2115 |
|
| 2116 |
i3_output *outputs_walk; |
| 2117 |
SLIST_FOREACH (outputs_walk, outputs, slist) { |
| 2118 |
int workspace_width = logical_px(config.padding.x); |
| 2119 |
|
| 2120 |
if (!outputs_walk->active) { |
| 2121 |
DLOG("Output %s inactive, skipping...\n", outputs_walk->name); |
| 2122 |
continue; |
| 2123 |
} |
| 2124 |
if (outputs_walk->bar.id == XCB_NONE) { |
| 2125 |
/* Oh shit, an active output without an own bar. Create it now! */ |
| 2126 |
reconfig_windows(false); |
| 2127 |
} |
| 2128 |
|
| 2129 |
bool use_focus_colors = output_has_focus(outputs_walk); |
| 2130 |
|
| 2131 |
/* First things first: clear the backbuffer */ |
| 2132 |
draw_util_clear_surface(&(outputs_walk->buffer), (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg)); |
| 2133 |
|
| 2134 |
if (!config.disable_ws) { |
| 2135 |
i3_ws *ws_walk; |
| 2136 |
TAILQ_FOREACH (ws_walk, outputs_walk->workspaces, tailq) { |
| 2137 |
DLOG("Drawing button for WS %s at x = %d, len = %d\n", |
| 2138 |
i3string_as_utf8(ws_walk->name), workspace_width, ws_walk->name_width); |
| 2139 |
color_t fg_color = colors.inactive_ws_fg; |
| 2140 |
color_t bg_color = colors.inactive_ws_bg; |
| 2141 |
color_t border_color = colors.inactive_ws_border; |
| 2142 |
if (ws_walk->visible) { |
| 2143 |
if (!ws_walk->focused) { |
| 2144 |
fg_color = colors.active_ws_fg; |
| 2145 |
bg_color = colors.active_ws_bg; |
| 2146 |
border_color = colors.active_ws_border; |
| 2147 |
} else { |
| 2148 |
fg_color = colors.focus_ws_fg; |
| 2149 |
bg_color = colors.focus_ws_bg; |
| 2150 |
border_color = colors.focus_ws_border; |
| 2151 |
} |
| 2152 |
} |
| 2153 |
if (ws_walk->urgent) { |
| 2154 |
DLOG("WS %s is urgent!\n", i3string_as_utf8(ws_walk->name)); |
| 2155 |
fg_color = colors.urgent_ws_fg; |
| 2156 |
bg_color = colors.urgent_ws_bg; |
| 2157 |
border_color = colors.urgent_ws_border; |
| 2158 |
unhide = true; |
| 2159 |
} |
| 2160 |
|
| 2161 |
int w = predict_button_width(ws_walk->name_width); |
| 2162 |
draw_button(&(outputs_walk->buffer), fg_color, bg_color, border_color, |
| 2163 |
workspace_width, w, ws_walk->name_width, ws_walk->name); |
| 2164 |
|
| 2165 |
workspace_width += w; |
| 2166 |
if (TAILQ_NEXT(ws_walk, tailq) != NULL) { |
| 2167 |
workspace_width += logical_px(ws_spacing_px); |
| 2168 |
} |
| 2169 |
} |
| 2170 |
} |
| 2171 |
|
| 2172 |
if (binding.name && !config.disable_binding_mode_indicator) { |
| 2173 |
workspace_width += logical_px(ws_spacing_px); |
| 2174 |
|
| 2175 |
int w = predict_button_width(binding.name_width); |
| 2176 |
draw_button(&(outputs_walk->buffer), colors.binding_mode_fg, colors.binding_mode_bg, |
| 2177 |
colors.binding_mode_border, workspace_width, w, binding.name_width, binding.name); |
| 2178 |
|
| 2179 |
unhide = true; |
| 2180 |
workspace_width += w; |
| 2181 |
} |
| 2182 |
|
| 2183 |
if (!TAILQ_EMPTY(&statusline_head)) { |
| 2184 |
DLOG("Printing statusline!\n"); |
| 2185 |
|
| 2186 |
int tray_width = get_tray_width(outputs_walk->trayclients); |
| 2187 |
uint32_t hoff = logical_px(((workspace_width > 0) + (tray_width > 0)) * sb_hoff_px); |
| 2188 |
uint32_t max_statusline_width = outputs_walk->rect.w - workspace_width - tray_width - hoff; |
| 2189 |
uint32_t clip_left = 0; |
| 2190 |
|
| 2191 |
/* Reset short mode between outputs */ |
| 2192 |
struct status_block *block; |
| 2193 |
TAILQ_FOREACH (block, &statusline_head, blocks) { |
| 2194 |
block->use_short = false; |
| 2195 |
} |
| 2196 |
|
| 2197 |
uint32_t statusline_width = adjust_statusline_length(max_statusline_width); |
| 2198 |
|
| 2199 |
if (statusline_width > max_statusline_width) { |
| 2200 |
clip_left = statusline_width - max_statusline_width; |
| 2201 |
} |
| 2202 |
|
| 2203 |
int16_t visible_statusline_width = MIN(statusline_width, max_statusline_width); |
| 2204 |
int x_dest = outputs_walk->rect.w - tray_width - logical_px((tray_width > 0) * sb_hoff_px) - visible_statusline_width; |
| 2205 |
x_dest -= logical_px(config.padding.width); |
| 2206 |
|
| 2207 |
draw_statusline(outputs_walk, clip_left, use_focus_colors); |
| 2208 |
draw_util_copy_surface(&outputs_walk->statusline_buffer, &outputs_walk->buffer, 0, 0, |
| 2209 |
x_dest, 0, visible_statusline_width, (int16_t)bar_height); |
| 2210 |
|
| 2211 |
outputs_walk->statusline_width = statusline_width; |
| 2212 |
} |
| 2213 |
} |
| 2214 |
|
| 2215 |
/* Assure the bar is hidden/unhidden according to the specified hidden_state and mode */ |
| 2216 |
if (mod_pressed || |
| 2217 |
config.hidden_state == S_SHOW || |
| 2218 |
unhide) { |
| 2219 |
unhide_bars(); |
| 2220 |
} else if (config.hide_on_modifier == M_HIDE) { |
| 2221 |
hide_bars(); |
| 2222 |
} |
| 2223 |
|
| 2224 |
redraw_bars(); |
| 2225 |
} |
| 2226 |
|
| 2227 |
/* |
| 2228 |
* Redraw the bars, i.e. simply copy the buffer to the barwindow |
| 2229 |
* |
| 2230 |
*/ |
| 2231 |
void redraw_bars(void) { |
| 2232 |
i3_output *outputs_walk; |
| 2233 |
SLIST_FOREACH (outputs_walk, outputs, slist) { |
| 2234 |
if (!outputs_walk->active) { |
| 2235 |
continue; |
| 2236 |
} |
| 2237 |
|
| 2238 |
draw_util_copy_surface(&(outputs_walk->buffer), &(outputs_walk->bar), 0, 0, |
| 2239 |
0, 0, outputs_walk->rect.w, outputs_walk->rect.h); |
| 2240 |
xcb_flush(xcb_connection); |
| 2241 |
} |
| 2242 |
} |
| 2243 |
|
| 2244 |
/* |
| 2245 |
* Set the current binding mode |
| 2246 |
* |
| 2247 |
*/ |
| 2248 |
void set_current_mode(struct mode *current) { |
| 2249 |
I3STRING_FREE(binding.name); |
| 2250 |
binding = *current; |
| 2251 |
activated_mode = binding.name != NULL; |
| 2252 |
} |