i3/src/window.c
Orestis Floros 38f28186ec
Remove some includes from all.h
Also removes duplicates from other headers

All used std* imports are included once in all.h for easy use

- getopt: Only used in main.c and inject_randr1.5.c
- glob: Not used in i3, only in i3bar & libi3
- inttypes: Only used in util.c
- locale: A bit specific for all.h
- math: Slow according to #4022
- unistd: I feel it's good to explicitly include per file that needs it
- yajl: Specific to yajl_utils.h and ipc.h

Related to #4022

Timing before:

```
Analyzing build trace from ...
**** Time summary:
Compilation (81 times):
  Parsing (frontend):           51.3 s
  Codegen & opts (backend):      7.7 s

**** Files that took longest to parse (compiler frontend):
  1254 ms: build/src/i3-commands.o
   972 ms: build/src/i3-resize.o
   945 ms: build/src/i3-con.o
   921 ms: build/src/i3-scratchpad.o
   907 ms: build/src/i3-main.o
   904 ms: build/src/i3-handlers.o
   904 ms: build/src/i3-config_directives.o
   893 ms: build/src/i3-restore_layout.o
   875 ms: build/src/i3-x.o
   854 ms: build/src/i3-ipc.o

**** Files that took longest to codegen (compiler backend):
   863 ms: build/src/i3-commands.o
   471 ms: build/i3bar/src/i3bar-xcb.o
   377 ms: build/src/i3-con.o
   360 ms: build/src/i3-ipc.o
   306 ms: build/src/i3-x.o
   290 ms: build/src/i3-main.o
   238 ms: build/src/i3-config_parser.o
   237 ms: build/src/i3-handlers.o
   220 ms: build/i3-config-wizard/i3_config_wizard-main.o
   214 ms: build/src/i3-bindings.o

**** Functions that took longest to compile:
   209 ms: main (../../i3/src/main.c)
    95 ms: manage_window (../../i3/src/manage.c)
    57 ms: reconfig_windows (../../i3/i3bar/src/xcb.c)
    55 ms: x_draw_decoration (../../i3/src/x.c)
    49 ms: x_push_node (../../i3/src/x.c)
    48 ms: handle_client_message (../../i3/src/handlers.c)
    48 ms: dump_node (../../i3/src/ipc.c)
    47 ms: GENERATED_call (../../i3/src/config_parser.c)
    45 ms: config_string_cb (../../i3/i3bar/src/config.c)
    44 ms: GENERATED_call (../../i3/src/commands_parser.c)
    42 ms: floating_check_size (../../i3/src/floating.c)
    40 ms: con_swap (../../i3/src/con.c)
    40 ms: parse_config (../../i3/src/config_parser.c)
    39 ms: main (../../i3/i3-nagbar/main.c)
    39 ms: cmd_rename_workspace (../../i3/src/commands.c)
    38 ms: window_update_normal_hints (../../i3/src/window.c)
    38 ms: cmd_swap (../../i3/src/commands.c)
    37 ms: dump_bar_config (../../i3/src/ipc.c)
    36 ms: translate_keysyms (../../i3/src/bindings.c)
    35 ms: tree_close_internal (../../i3/src/tree.c)
    34 ms: match_matches_window (../../i3/src/match.c)
    34 ms: floating_enable (../../i3/src/floating.c)
    34 ms: json_string (../../i3/src/load_layout.c)
    33 ms: x_push_changes (../../i3/src/x.c)
    33 ms: main (../../i3/i3-config-wizard/main.c)
    31 ms: free_configuration (../../i3/src/config.c)
    30 ms: parse_file (../../i3/src/config_parser.c)
    28 ms: load_font (../../i3/libi3/font.c)
    28 ms: handle_configure_request (../../i3/src/handlers.c)
    28 ms: parse_command (../../i3/src/commands_parser.c)

**** Function sets that took longest to compile / optimize:

*** Expensive headers:
29287 ms: ../../i3/include/libi3.h (included 78 times, avg 375 ms), included via:
  i3-resize.o all.h data.h  (576 ms)
  a-g_utf8_make_valid.o  (491 ms)
  a-dpi.o  (491 ms)
  a-get_colorpixel.o  (483 ms)
  a-is_debug_build.o  (478 ms)
  a-strndup.o  (478 ms)
  ...

29221 ms: ../../i3/include/all.h (included 39 times, avg 749 ms), included via:
  i3-resize.o  (946 ms)
  i3-scratchpad.o  (895 ms)
  i3-restore_layout.o  (865 ms)
  i3-handlers.o  (839 ms)
  i3-drag.o  (830 ms)
  i3-config_directives.o  (793 ms)
  ...

5195 ms: /usr/include/xcb/xcb.h (included 79 times, avg 65 ms), included via:
  i3-handlers.o all.h  (113 ms)
  i3-restore_layout.o all.h  (108 ms)
  a-g_utf8_make_valid.o libi3.h  (108 ms)
  i3bar-xcb.o common.h  (105 ms)
  i3-scratchpad.o all.h  (95 ms)
  a-get_colorpixel.o libi3.h  (89 ms)
  ...

4100 ms: /usr/include/math.h (included 41 times, avg 100 ms), included via:
  i3-scratchpad.o all.h  (180 ms)
  i3-fake_outputs.o all.h  (138 ms)
  i3-regex.o all.h  (130 ms)
  i3-restore_layout.o all.h  (128 ms)
  i3-xcb.o all.h  (121 ms)
  i3-move.o all.h  (119 ms)
  ...

4046 ms: ../../i3/i3bar/include/common.h (included 9 times, avg 449 ms), included via:
  i3bar-main.o  (503 ms)
  i3bar-xcb.o  (501 ms)
  i3bar-workspaces.o  (472 ms)
  i3bar-parse_json_header.o  (446 ms)
  i3bar-child.o  (438 ms)
  i3bar-ipc.o  (434 ms)
  ...

2713 ms: ../../i3/include/i3.h (included 41 times, avg 66 ms), included via:
  i3-xcursor.o  (450 ms)
  i3-config_directives.o all.h ipc.h configuration.h  (87 ms)
  i3-config.o all.h ipc.h configuration.h  (71 ms)
  i3-manage.o all.h ipc.h configuration.h  (70 ms)
  i3-window.o all.h ipc.h configuration.h  (68 ms)
  i3-x.o all.h ipc.h configuration.h  (61 ms)
  ...

1492 ms: /usr/include/xcb/xkb.h (included 42 times, avg 35 ms), included via:
  i3-config_directives.o all.h ipc.h configuration.h i3.h  (50 ms)
  i3-config.o all.h ipc.h configuration.h i3.h  (45 ms)
  i3-window.o all.h ipc.h configuration.h i3.h  (43 ms)
  i3-x.o all.h ipc.h configuration.h i3.h  (42 ms)
  i3-manage.o all.h ipc.h configuration.h i3.h  (41 ms)
  i3-config_parser.o all.h ipc.h configuration.h i3.h  (38 ms)
  ...

1432 ms: /usr/include/stdlib.h (included 79 times, avg 18 ms), included via:
  i3-scratchpad.o all.h  (48 ms)
  i3-restore_layout.o all.h  (36 ms)
  i3-regex.o all.h  (32 ms)
  i3-key_press.o all.h  (28 ms)
  i3-commands.o all.h  (28 ms)
  i3-bindings.o all.h  (24 ms)
  ...

1349 ms: /usr/include/pthread.h (included 79 times, avg 17 ms), included via:
  i3bar-xcb.o common.h xcb.h  (33 ms)
  a-ucs2_conversion.o libi3.h xcb.h  (32 ms)
  i3-match.o all.h xcb.h  (27 ms)
  i3-scratchpad.o all.h xcb.h  (25 ms)
  a-g_utf8_make_valid.o libi3.h xcb.h  (25 ms)
  i3_config_wizard-main.o xcb.h  (24 ms)
  ...

1151 ms: /usr/include/X11/Xlib.h (included 45 times, avg 25 ms), included via:
  i3-output.o all.h data.h sn-launcher.h sn-common.h  (50 ms)
  i3-config_parser.o all.h data.h sn-launcher.h sn-common.h  (43 ms)
  i3-x.o all.h data.h sn-launcher.h sn-common.h  (34 ms)
  i3-config_directives.o all.h data.h sn-launcher.h sn-common.h  (32 ms)
  i3_config_wizard-main.o sn-launchee.h sn-common.h  (30 ms)
  i3-drag.o all.h data.h sn-launcher.h sn-common.h  (29 ms)
  ...
```

Timing after:

```
Analyzing build trace from ...
**** Time summary:
Compilation (81 times):
  Parsing (frontend):           47.6 s
  Codegen & opts (backend):      7.6 s

**** Files that took longest to parse (compiler frontend):
  1154 ms: build/src/i3-commands.o
   929 ms: build/src/i3-display_version.o
   852 ms: build/src/i3-bindings.o
   847 ms: build/src/i3-con.o
   806 ms: build/src/i3-ipc.o
   801 ms: build/src/i3-floating.o
   792 ms: build/src/i3-main.o
   792 ms: build/src/i3-drag.o
   792 ms: build/src/i3-window.o
   776 ms: build/src/i3-config_directives.o

**** Files that took longest to codegen (compiler backend):
   885 ms: build/src/i3-commands.o
   422 ms: build/i3bar/src/i3bar-xcb.o
   382 ms: build/src/i3-con.o
   348 ms: build/src/i3-x.o
   288 ms: build/src/i3-ipc.o
   268 ms: build/src/i3-handlers.o
   254 ms: build/src/i3-main.o
   251 ms: build/src/i3-floating.o
   249 ms: build/src/i3-config_parser.o
   194 ms: build/src/i3-randr.o

**** Functions that took longest to compile:
   186 ms: main (../../i3/src/main.c)
    95 ms: manage_window (../../i3/src/manage.c)
    65 ms: floating_check_size (../../i3/src/floating.c)
    63 ms: x_draw_decoration (../../i3/src/x.c)
    58 ms: handle_client_message (../../i3/src/handlers.c)
    55 ms: x_push_node (../../i3/src/x.c)
    54 ms: match_matches_window (../../i3/src/match.c)
    51 ms: parse_config (../../i3/src/config_parser.c)
    49 ms: dump_node (../../i3/src/ipc.c)
    47 ms: reconfig_windows (../../i3/i3bar/src/xcb.c)
    47 ms: config_string_cb (../../i3/i3bar/src/config.c)
    45 ms: GENERATED_call (../../i3/src/config_parser.c)
    45 ms: GENERATED_call (../../i3/src/commands_parser.c)
    43 ms: floating_enable (../../i3/src/floating.c)
    42 ms: handle_configure_request (../../i3/src/handlers.c)
    40 ms: con_swap (../../i3/src/con.c)
    36 ms: main (../../i3/i3-input/main.c)
    36 ms: main (../../i3/i3-msg/main.c)
    36 ms: main (../../i3/i3-nagbar/main.c)
    36 ms: cmd_move_con_to_workspace_number (../../i3/src/commands.c)
    35 ms: json_string (../../i3/src/load_layout.c)
    35 ms: tree_restore (../../i3/src/tree.c)
    35 ms: cmd_swap (../../i3/src/commands.c)
    34 ms: x_push_changes (../../i3/src/x.c)
    32 ms: main (../../i3/i3-config-wizard/main.c)
    32 ms: ewmh_setup_hints (../../i3/src/ewmh.c)
    31 ms: match_parse_property (../../i3/src/match.c)
    30 ms: cmd_mark (../../i3/src/commands.c)
    30 ms: translate_keysyms (../../i3/src/bindings.c)
    30 ms: window_update_normal_hints (../../i3/src/window.c)

**** Function sets that took longest to compile / optimize:

*** Expensive headers:
29596 ms: ../../i3/include/libi3.h (included 78 times, avg 379 ms), included via:
  a-get_config_path.o  (539 ms)
  i3_dump_log-main.o  (522 ms)
  i3_config_wizard-main.o  (501 ms)
  a-fake_configure_notify.o  (500 ms)
  a-root_atom_contents.o  (488 ms)
  i3-display_version.o all.h  (466 ms)
  ...

26054 ms: ../../i3/include/all.h (included 41 times, avg 635 ms), included via:
  i3-display_version.o  (901 ms)
  i3-drag.o  (775 ms)
  i3-ewmh.o  (703 ms)
  i3-startup.o  (693 ms)
  i3-commands.o  (687 ms)
  i3-xcb.o  (680 ms)
  ...

5345 ms: /usr/include/xcb/xcb.h (included 79 times, avg 67 ms), included via:
  i3-display_version.o all.h  (173 ms)
  i3_input-keysym2ucs.o keysym2ucs.h  (106 ms)
  i3-ewmh.o all.h  (106 ms)
  a-fake_configure_notify.o libi3.h  (103 ms)
  a-get_config_path.o libi3.h  (95 ms)
  i3bar-parse_json_header.o common.h  (93 ms)
  ...

4127 ms: ../../i3/i3bar/include/common.h (included 9 times, avg 458 ms), included via:
  i3bar-child.o  (524 ms)
  i3bar-mode.o  (486 ms)
  i3bar-outputs.o  (464 ms)
  i3bar-parse_json_header.o  (463 ms)
  i3bar-config.o  (457 ms)
  i3bar-ipc.o  (448 ms)
  ...

1542 ms: /usr/include/xcb/xkb.h (included 42 times, avg 36 ms), included via:
  i3-con.o all.h ipc.h configuration.h i3.h  (60 ms)
  i3-render.o all.h ipc.h configuration.h i3.h  (56 ms)
  i3-bindings.o all.h ipc.h configuration.h i3.h  (56 ms)
  i3-sighandler.o all.h ipc.h configuration.h i3.h  (48 ms)
  i3-xcb.o all.h ipc.h configuration.h i3.h  (47 ms)
  i3-resize.o all.h ipc.h configuration.h i3.h  (39 ms)
  ...

1456 ms: /usr/include/stdlib.h (included 79 times, avg 18 ms), included via:
  i3-drag.o all.h  (60 ms)
  i3-display_version.o all.h  (55 ms)
  i3-fake_outputs.o all.h  (39 ms)
  i3-config_directives.o all.h  (33 ms)
  i3-xcursor.o all.h  (30 ms)
  i3bar-mode.o common.h libi3.h pango.h pango-attributes.h pango-font.h pango-coverage.h glib-object.h gbinding.h glib.h gasyncqueue.h gthread.h gutils.h  (29 ms)
  ...

1136 ms: /usr/include/X11/Xlib.h (included 44 times, avg 25 ms), included via:
  i3-con.o all.h data.h sn-launcher.h sn-common.h  (35 ms)
  i3-resize.o all.h data.h sn-launcher.h sn-common.h  (34 ms)
  i3-util.o all.h data.h sn-launcher.h sn-common.h  (33 ms)
  i3-assignments.o all.h data.h sn-launcher.h sn-common.h  (33 ms)
  i3-sighandler.o all.h data.h sn-launcher.h sn-common.h  (31 ms)
  i3-xcb.o all.h data.h sn-launcher.h sn-common.h  (31 ms)
  ...

808 ms: /usr/include/stdio.h (included 79 times, avg 10 ms), included via:
  i3-drag.o all.h  (19 ms)
  i3-fake_outputs.o all.h  (18 ms)
  a-font.o libi3.h  (16 ms)
  i3bar-child.o common.h libi3.h  (15 ms)
  a-safewrappers.o libi3.h  (15 ms)
  a-ipc_send_message.o libi3.h  (13 ms)
  ...

770 ms: /usr/include/xcb/randr.h (included 42 times, avg 18 ms), included via:
  i3-click.o all.h data.h  (29 ms)
  i3-commands.o all.h data.h  (27 ms)
  i3-assignments.o all.h data.h  (25 ms)
  i3-xcb.o all.h data.h  (21 ms)
  i3-resize.o all.h data.h  (21 ms)
  i3-sighandler.o all.h data.h  (20 ms)
  ...

688 ms: /usr/include/math.h (included 6 times, avg 114 ms), included via:
  a-dpi.o  (145 ms)
  i3-render.o  (127 ms)
  i3-floating.o  (106 ms)
  a-root_atom_contents.o  (106 ms)
  i3-window.o  (102 ms)
  i3-bindings.o  (99 ms)
  ...
```
2020-04-20 05:54:17 +02:00

469 lines
15 KiB
C

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
* window.c: Updates window attributes (X11 hints/properties).
*
*/
#include "all.h"
#include <math.h>
/*
* Frees an i3Window and all its members.
*
*/
void window_free(i3Window *win) {
FREE(win->class_class);
FREE(win->class_instance);
i3string_free(win->name);
FREE(win->ran_assignments);
FREE(win);
}
/*
* Updates the WM_CLASS (consisting of the class and instance) for the
* given window.
*
*/
void window_update_class(i3Window *win, xcb_get_property_reply_t *prop) {
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("WM_CLASS not set.\n");
FREE(prop);
return;
}
/* We cannot use asprintf here since this property contains two
* null-terminated strings (for compatibility reasons). Instead, we
* use strdup() on both strings */
const size_t prop_length = xcb_get_property_value_length(prop);
char *new_class = xcb_get_property_value(prop);
const size_t class_class_index = strnlen(new_class, prop_length) + 1;
FREE(win->class_instance);
FREE(win->class_class);
win->class_instance = sstrndup(new_class, prop_length);
if (class_class_index < prop_length)
win->class_class = sstrndup(new_class + class_class_index, prop_length - class_class_index);
else
win->class_class = NULL;
LOG("WM_CLASS changed to %s (instance), %s (class)\n",
win->class_instance, win->class_class);
free(prop);
}
/*
* Updates the name by using _NET_WM_NAME (encoded in UTF-8) for the given
* window. Further updates using window_update_name_legacy will be ignored.
*
*/
void window_update_name(i3Window *win, xcb_get_property_reply_t *prop) {
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("_NET_WM_NAME not specified, not changing\n");
FREE(prop);
return;
}
i3string_free(win->name);
/* Truncate the name at the first zero byte. See #3515. */
const int len = xcb_get_property_value_length(prop);
char *name = sstrndup(xcb_get_property_value(prop), len);
win->name = i3string_from_utf8(name);
free(name);
Con *con = con_by_window_id(win->id);
if (con != NULL && con->title_format != NULL) {
i3String *name = con_parse_title_format(con);
ewmh_update_visible_name(win->id, i3string_as_utf8(name));
I3STRING_FREE(name);
}
win->name_x_changed = true;
LOG("_NET_WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
win->uses_net_wm_name = true;
free(prop);
}
/*
* Updates the name by using WM_NAME (encoded in COMPOUND_TEXT). We do not
* touch what the client sends us but pass it to xcb_image_text_8. To get
* proper unicode rendering, the application has to use _NET_WM_NAME (see
* window_update_name()).
*
*/
void window_update_name_legacy(i3Window *win, xcb_get_property_reply_t *prop) {
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("WM_NAME not set (_NET_WM_NAME is what you want anyways).\n");
FREE(prop);
return;
}
/* ignore update when the window is known to already have a UTF-8 name */
if (win->uses_net_wm_name) {
free(prop);
return;
}
i3string_free(win->name);
const int len = xcb_get_property_value_length(prop);
char *name = sstrndup(xcb_get_property_value(prop), len);
win->name = i3string_from_utf8(name);
free(name);
Con *con = con_by_window_id(win->id);
if (con != NULL && con->title_format != NULL) {
i3String *name = con_parse_title_format(con);
ewmh_update_visible_name(win->id, i3string_as_utf8(name));
I3STRING_FREE(name);
}
LOG("WM_NAME changed to \"%s\"\n", i3string_as_utf8(win->name));
LOG("Using legacy window title. Note that in order to get Unicode window "
"titles in i3, the application has to set _NET_WM_NAME (UTF-8)\n");
win->name_x_changed = true;
free(prop);
}
/*
* Updates the CLIENT_LEADER (logical parent window).
*
*/
void window_update_leader(i3Window *win, xcb_get_property_reply_t *prop) {
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("CLIENT_LEADER not set on window 0x%08x.\n", win->id);
win->leader = XCB_NONE;
FREE(prop);
return;
}
xcb_window_t *leader = xcb_get_property_value(prop);
if (leader == NULL) {
free(prop);
return;
}
DLOG("Client leader changed to %08x\n", *leader);
win->leader = *leader;
free(prop);
}
/*
* Updates the TRANSIENT_FOR (logical parent window).
*
*/
void window_update_transient_for(i3Window *win, xcb_get_property_reply_t *prop) {
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("TRANSIENT_FOR not set on window 0x%08x.\n", win->id);
win->transient_for = XCB_NONE;
FREE(prop);
return;
}
xcb_window_t transient_for;
if (!xcb_icccm_get_wm_transient_for_from_reply(&transient_for, prop)) {
free(prop);
return;
}
DLOG("Transient for changed to 0x%08x (window 0x%08x)\n", transient_for, win->id);
win->transient_for = transient_for;
free(prop);
}
/*
* Updates the _NET_WM_STRUT_PARTIAL (reserved pixels at the screen edges)
*
*/
void window_update_strut_partial(i3Window *win, xcb_get_property_reply_t *prop) {
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("_NET_WM_STRUT_PARTIAL not set.\n");
FREE(prop);
return;
}
uint32_t *strut;
if (!(strut = xcb_get_property_value(prop))) {
free(prop);
return;
}
DLOG("Reserved pixels changed to: left = %d, right = %d, top = %d, bottom = %d\n",
strut[0], strut[1], strut[2], strut[3]);
win->reserved = (struct reservedpx){strut[0], strut[1], strut[2], strut[3]};
free(prop);
}
/*
* Updates the WM_WINDOW_ROLE
*
*/
void window_update_role(i3Window *win, xcb_get_property_reply_t *prop) {
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("WM_WINDOW_ROLE not set.\n");
FREE(prop);
return;
}
char *new_role;
sasprintf(&new_role, "%.*s", xcb_get_property_value_length(prop),
(char *)xcb_get_property_value(prop));
FREE(win->role);
win->role = new_role;
LOG("WM_WINDOW_ROLE changed to \"%s\"\n", win->role);
free(prop);
}
/*
* Updates the _NET_WM_WINDOW_TYPE property.
*
*/
void window_update_type(i3Window *window, xcb_get_property_reply_t *reply) {
xcb_atom_t new_type = xcb_get_preferred_window_type(reply);
free(reply);
if (new_type == XCB_NONE) {
DLOG("cannot read _NET_WM_WINDOW_TYPE from window.\n");
return;
}
window->window_type = new_type;
LOG("_NET_WM_WINDOW_TYPE changed to %i.\n", window->window_type);
run_assignments(window);
}
/*
* Updates the WM_NORMAL_HINTS
*
*/
bool window_update_normal_hints(i3Window *win, xcb_get_property_reply_t *reply, xcb_get_geometry_reply_t *geom) {
bool changed = false;
xcb_size_hints_t size_hints;
/* If the hints were already in this event, use them, if not, request them */
bool success;
if (reply != NULL) {
success = xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
} else {
success = xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, win->id), &size_hints, NULL);
}
if (!success) {
DLOG("Could not get WM_NORMAL_HINTS\n");
return false;
}
#define ASSIGN_IF_CHANGED(original, new) \
do { \
if (original != new) { \
original = new; \
changed = true; \
} \
} while (0)
if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
ASSIGN_IF_CHANGED(win->min_width, size_hints.min_width);
ASSIGN_IF_CHANGED(win->min_height, size_hints.min_height);
}
if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)) {
DLOG("Maximum size: %d (width) x %d (height)\n", size_hints.max_width, size_hints.max_height);
int max_width = max(0, size_hints.max_width);
int max_height = max(0, size_hints.max_height);
ASSIGN_IF_CHANGED(win->max_width, max_width);
ASSIGN_IF_CHANGED(win->max_height, max_height);
} else {
DLOG("Clearing maximum size\n");
ASSIGN_IF_CHANGED(win->max_width, 0);
ASSIGN_IF_CHANGED(win->max_height, 0);
}
if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
DLOG("Size increments: %d (width) x %d (height)\n", size_hints.width_inc, size_hints.height_inc);
if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF) {
ASSIGN_IF_CHANGED(win->width_increment, size_hints.width_inc);
} else {
ASSIGN_IF_CHANGED(win->width_increment, 0);
}
if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF) {
ASSIGN_IF_CHANGED(win->height_increment, size_hints.height_inc);
} else {
ASSIGN_IF_CHANGED(win->height_increment, 0);
}
} else {
DLOG("Clearing size increments\n");
ASSIGN_IF_CHANGED(win->width_increment, 0);
ASSIGN_IF_CHANGED(win->height_increment, 0);
}
/* The base width / height is the desired size of the window. */
if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE &&
(win->base_width >= 0) && (win->base_height >= 0)) {
DLOG("Base size: %d (width) x %d (height)\n", size_hints.base_width, size_hints.base_height);
ASSIGN_IF_CHANGED(win->base_width, size_hints.base_width);
ASSIGN_IF_CHANGED(win->base_height, size_hints.base_height);
} else {
DLOG("Clearing base size\n");
ASSIGN_IF_CHANGED(win->base_width, 0);
ASSIGN_IF_CHANGED(win->base_height, 0);
}
if (geom != NULL &&
(size_hints.flags & XCB_ICCCM_SIZE_HINT_US_POSITION || size_hints.flags & XCB_ICCCM_SIZE_HINT_P_POSITION) &&
(size_hints.flags & XCB_ICCCM_SIZE_HINT_US_SIZE || size_hints.flags & XCB_ICCCM_SIZE_HINT_P_SIZE)) {
DLOG("Setting geometry x=%d y=%d w=%d h=%d\n", size_hints.x, size_hints.y, size_hints.width, size_hints.height);
geom->x = size_hints.x;
geom->y = size_hints.y;
geom->width = size_hints.width;
geom->height = size_hints.height;
}
/* If no aspect ratio was set or if it was invalid, we ignore the hints */
if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT &&
(size_hints.min_aspect_num >= 0) && (size_hints.min_aspect_den > 0) &&
(size_hints.max_aspect_num >= 0) && (size_hints.max_aspect_den > 0)) {
/* Convert numerator/denominator to a double */
double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
double max_aspect = (double)size_hints.max_aspect_num / size_hints.max_aspect_den;
DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
if (fabs(win->min_aspect_ratio - min_aspect) > DBL_EPSILON) {
win->min_aspect_ratio = min_aspect;
changed = true;
}
if (fabs(win->max_aspect_ratio - max_aspect) > DBL_EPSILON) {
win->max_aspect_ratio = max_aspect;
changed = true;
}
} else {
DLOG("Clearing aspect ratios\n");
ASSIGN_IF_CHANGED(win->min_aspect_ratio, 0.0);
ASSIGN_IF_CHANGED(win->max_aspect_ratio, 0.0);
}
return changed;
}
/*
* Updates the WM_HINTS (we only care about the input focus handling part).
*
*/
void window_update_hints(i3Window *win, xcb_get_property_reply_t *prop, bool *urgency_hint) {
if (urgency_hint != NULL)
*urgency_hint = false;
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
DLOG("WM_HINTS not set.\n");
FREE(prop);
return;
}
xcb_icccm_wm_hints_t hints;
if (!xcb_icccm_get_wm_hints_from_reply(&hints, prop)) {
DLOG("Could not get WM_HINTS\n");
free(prop);
return;
}
if (hints.flags & XCB_ICCCM_WM_HINT_INPUT) {
win->doesnt_accept_focus = !hints.input;
LOG("WM_HINTS.input changed to \"%d\"\n", hints.input);
}
if (urgency_hint != NULL)
*urgency_hint = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
free(prop);
}
/*
* Updates the MOTIF_WM_HINTS. The container's border style should be set to
* `motif_border_style' if border style is not BS_NORMAL.
*
* i3 only uses this hint when it specifies a window should have no
* title bar, or no decorations at all, which is how most window managers
* handle it.
*
* The EWMH spec intended to replace Motif hints with _NET_WM_WINDOW_TYPE, but
* it is still in use by popular widget toolkits such as GTK+ and Java AWT.
*
*/
void window_update_motif_hints(i3Window *win, xcb_get_property_reply_t *prop, border_style_t *motif_border_style) {
/* This implementation simply mirrors Gnome's Metacity. Official
* documentation of this hint is nowhere to be found.
* For more information see:
* https://people.gnome.org/~tthurman/docs/metacity/xprops_8h-source.html
* https://stackoverflow.com/questions/13787553/detect-if-a-x11-window-has-decorations
*/
#define MWM_HINTS_FLAGS_FIELD 0
#define MWM_HINTS_DECORATIONS_FIELD 2
#define MWM_HINTS_DECORATIONS (1 << 1)
#define MWM_DECOR_ALL (1 << 0)
#define MWM_DECOR_BORDER (1 << 1)
#define MWM_DECOR_TITLE (1 << 3)
if (motif_border_style != NULL)
*motif_border_style = BS_NORMAL;
if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
FREE(prop);
return;
}
/* The property consists of an array of 5 uint32_t's. The first value is a
* bit mask of what properties the hint will specify. We are only interested
* in MWM_HINTS_DECORATIONS because it indicates that the third value of the
* array tells us which decorations the window should have, each flag being
* a particular decoration. Notice that X11 (Xlib) often mentions 32-bit
* fields which in reality are implemented using unsigned long variables
* (64-bits long on amd64 for example). On the other hand,
* xcb_get_property_value() behaves strictly according to documentation,
* i.e. returns 32-bit data fields. */
uint32_t *motif_hints = (uint32_t *)xcb_get_property_value(prop);
if (motif_border_style != NULL &&
motif_hints[MWM_HINTS_FLAGS_FIELD] & MWM_HINTS_DECORATIONS) {
if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_ALL ||
motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_TITLE)
*motif_border_style = BS_NORMAL;
else if (motif_hints[MWM_HINTS_DECORATIONS_FIELD] & MWM_DECOR_BORDER)
*motif_border_style = BS_PIXEL;
else
*motif_border_style = BS_NONE;
}
FREE(prop);
#undef MWM_HINTS_FLAGS_FIELD
#undef MWM_HINTS_DECORATIONS_FIELD
#undef MWM_HINTS_DECORATIONS
#undef MWM_DECOR_ALL
#undef MWM_DECOR_BORDER
#undef MWM_DECOR_TITLE
}