| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::Window; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use List::Util qw( any first sum0 uniq ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Encode qw( encode decode ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use X11::XCB ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use X11::korgwm::Common; | ||
| 13 | |||
| 14 | # Simplify object usage | ||
| 15 | use Scalar::Util qw( refaddr ); | ||
| 16 | use overload '""' => sub { sprintf "%s[id:%d]", overload::StrVal($_[0]), $_[0]->{id} // "undef" }; | ||
| 17 | use overload '==' => sub { (refaddr($_[0]) // 0) == (refaddr($_[1]) // 0) }; | ||
| 18 | use overload '!=' => sub { (refaddr($_[0]) // 0) != (refaddr($_[1]) // 0) }; | ||
| 19 | use overload cmp => sub { (refaddr($_[0]) // 0) cmp (refaddr($_[1]) // 0) }; | ||
| 20 | |||
| 21 | # Internal class variables | ||
| 22 | my $sid = 1; | ||
| 23 | my @wm_size_hints = qw( flags x y width height min_width min_height max_width max_height width_inc height_inc | ||
| 24 | min_aspect_num min_aspect_den max_aspect_num max_aspect_den base_width base_height win_gravity ); | ||
| 25 | my $wm_size_hints = "LllllllllllllllllL"; | ||
| 26 | |||
| 27 | sub new($class, $id) { | ||
| 28 | # Full structure is defined in architecture/05_data_structures.txt | ||
| 29 | bless { id => $id, sid => $sid++, on_tags => {}, pref_position => [] }, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | # ctor for mock windows when we need blessed objects | ||
| 33 | sub mock($class, $id) { | ||
| 34 | bless { id => $id }, $class; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub DESTROY($self) { | ||
| 38 | # Wanna free some resources? Do it inside Destroy handler: korgwm.pm/annihilate_window() | ||
| 39 | 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | # Returns: | ||
| 43 | # - in scalar context: just a value | ||
| 44 | # - in list context: the value and reply object itself | ||
| 45 | # The value could be modified depending on the $prop_type: | ||
| 46 | # - UTF-8 is being decoded on the fly | ||
| 47 | # - WINDOW CARDINAL is unpacked as a single int | ||
| 48 | # - ATOM is unpacked as [ int, int, int... ] for each ATOM in the reply | ||
| 49 | sub _get_property($wid, $prop_name, $prop_type='UTF8_STRING', $ret_length=8) { | ||
| 50 | my $aname = atom($prop_name); | ||
| 51 | my $atype = atom($prop_type); | ||
| 52 | my $cookie = $X->get_property(0, $wid, $aname, $atype, 0, $ret_length); | ||
| 53 | |||
| 54 | my $prop; | ||
| 55 | eval { $prop = $X->get_property_reply($cookie->{sequence}); 1} or return undef; | ||
| 56 | my $value = $prop ? $prop->{value} : undef; | ||
| 57 | |||
| 58 | # Convert to internal format | ||
| 59 | if (defined $value) { | ||
| 60 | $value = decode('UTF-8', $value) if $prop_type eq 'UTF8_STRING'; | ||
| 61 | ($value) = unpack('L', $value) if $prop_type eq 'WINDOW'; | ||
| 62 | $value = [ unpack 'L' x $prop->{value_len}, $value ] if $prop_type eq 'ATOM'; | ||
| 63 | } | ||
| 64 | |||
| 65 | return wantarray ? ($value, $prop) : $value; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _resize_and_move($wid, $x, $y, $w, $h, $bw=$cfg->{border_width}) { | ||
| 69 | my $mask = CONFIG_WINDOW_X | CONFIG_WINDOW_Y | CONFIG_WINDOW_WIDTH | CONFIG_WINDOW_HEIGHT | | ||
| 70 | CONFIG_WINDOW_BORDER_WIDTH; | ||
| 71 | $X->configure_window($wid, $mask, $x, $y, $w - 2 * $bw, $h - 2 * $bw, $bw); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _move($wid, $x, $y) { | ||
| 75 | $X->configure_window($wid, CONFIG_WINDOW_X | CONFIG_WINDOW_Y, $x, $y); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _resize($wid, $w, $h) { | ||
| 79 | $X->configure_window($wid, CONFIG_WINDOW_WIDTH | CONFIG_WINDOW_HEIGHT, $w, $h); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _configure_notify($wid, $sequence, $x, $y, $w, $h, $above_sibling=0, $override_redirect=0, | ||
| 83 | $bw=$cfg->{border_width}) { | ||
| 84 | return carp "Undefined ($x, $y, $w, $h) for configure notify" unless 4 == grep defined, $x, $y, $w, $h; | ||
| 85 | my $packed = pack('CCSLLLssSSSC', CONFIGURE_NOTIFY, 0, $sequence, | ||
| 86 | $wid, # event | ||
| 87 | $wid, # window | ||
| 88 | $above_sibling, $x, $y, $w, $h, $bw, $override_redirect); | ||
| 89 | $X->send_event(0, $wid, EVENT_MASK_STRUCTURE_NOTIFY, $packed); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _attributes($wid) { | ||
| 93 | $X->get_window_attributes_reply($X->get_window_attributes($wid)->{sequence}); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _class($wid) { | ||
| 97 | my @class = split /\0/, scalar _get_property($wid, "WM_CLASS", "STRING", 16) // return; | ||
| 98 | return wantarray ? @class : $class[0]; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _title($wid) { | ||
| 102 | # long_length is a 32-bit multiplies of data; UTF8 usually encodes a char up to 8 bytes, so multipler is 2: | ||
| 103 | my $title = _get_property($wid, "_NET_WM_NAME", "UTF8_STRING", 2 * $cfg->{title_max_len}); | ||
| 104 | $title = _get_property($wid, "WM_NAME", "STRING", $cfg->{title_max_len}) unless length $title; | ||
| 105 | return "" unless defined $title; | ||
| 106 | $title =~ s/\n/ /g; | ||
| 107 | $title =~ s/ *$//g; | ||
| 108 | $title =~ s/^ *//g; | ||
| 109 | $title =~ s/\p{Grapheme_Extend}/?/g; | ||
| 110 | $title; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _transient_for($wid) { | ||
| 114 | _get_property($wid, "WM_TRANSIENT_FOR", "WINDOW", 16); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _query_geometry($wid) { | ||
| 118 | map { @{$_}{qw( x y width height )} } $X->get_geometry_reply($X->get_geometry($wid)->{sequence}); | ||
| 119 | } | ||
| 120 | |||
| 121 | # Generate accessors by object | ||
| 122 | UNITCHECK { | ||
| 123 | no strict 'refs'; | ||
| 124 | for my $func (qw( | ||
| 125 | attributes | ||
| 126 | class | ||
| 127 | configure_notify | ||
| 128 | get_property | ||
| 129 | query_geometry | ||
| 130 | title | ||
| 131 | transient_for | ||
| 132 | )) { | ||
| 133 | *{__PACKAGE__ . "::$func"} = sub { | ||
| 134 | my $self = shift; | ||
| 135 | croak "Undefined window" unless $self->{id}; | ||
| 136 | "_$func"->($self->{id}, @_); | ||
| 137 | }; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub resize_and_move($self, $x, $y, $w, $h, $bw=$cfg->{border_width}) { | ||
| 142 | croak "Undefined window" unless $self->{id}; | ||
| 143 | DEBUG8 and carp "resize_and_move($self, $x, $y, $w, $h, $bw)"; | ||
| 144 | @{ $self }{qw( real_x real_y real_w real_h real_bw )} = ($x, $y, $w, $h, $bw); | ||
| 145 | _resize_and_move($self->{id}, $x, $y, $w, $h, $bw); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub move($self, $x, $y) { | ||
| 149 | croak "Undefined window" unless $self->{id}; | ||
| 150 | @{ $self }{qw( real_x real_y )} = ($x, $y); | ||
| 151 | $X->configure_window($self->{id}, CONFIG_WINDOW_X | CONFIG_WINDOW_Y, $x, $y); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub resize($self, $w, $h) { | ||
| 155 | croak "Undefined window" unless $self->{id}; | ||
| 156 | @{ $self }{qw( real_w real_h )} = ($w, $h); | ||
| 157 | $X->configure_window($self->{id}, CONFIG_WINDOW_WIDTH | CONFIG_WINDOW_HEIGHT, $w, $h); | ||
| 158 | } | ||
| 159 | |||
| 160 | # Move floating windows between screens | ||
| 161 | sub floating_move_screen($self, $old_screen, $new_screen) { | ||
| 162 | return unless $self->{floating}; | ||
| 163 | |||
| 164 | my ($new_x, $new_y) = @{ $self }{qw( real_x real_y )}; | ||
| 165 | $new_x -= $old_screen->{x}; | ||
| 166 | $new_y -= $old_screen->{y}; | ||
| 167 | $new_x += $new_screen->{x}; | ||
| 168 | $new_y += $new_screen->{y}; | ||
| 169 | $self->move($new_x, $new_y); | ||
| 170 | |||
| 171 | # Fix configured geometry after it was modified by move() | ||
| 172 | @{ $self }{qw( x y )} = @{ $self }{qw( real_x real_y )}; | ||
| 173 | } | ||
| 174 | |||
| 175 | # Put the window above others | ||
| 176 | sub _stack_above($self) { | ||
| 177 | return if $self->{_hidden}; | ||
| 178 | $X->configure_window($self->{id}, CONFIG_WINDOW_STACK_MODE, STACK_MODE_ABOVE); | ||
| 179 | } | ||
| 180 | |||
| 181 | # Put the window below another ($top) | ||
| 182 | sub _stack_below($self, $top) { | ||
| 183 | return if $self->{_hidden}; | ||
| 184 | $X->configure_window($self->{id}, CONFIG_WINDOW_SIBLING | CONFIG_WINDOW_STACK_MODE, $top->{id}, STACK_MODE_BELOW); | ||
| 185 | } | ||
| 186 | |||
| 187 | # Place windows in a stack calling above/below only once per window | ||
| 188 | sub _stack_place(@stack) { | ||
| 189 | return unless @stack; | ||
| 190 | |||
| 191 | # Infuse @stack with pinned windows, removing duplicates | ||
| 192 | my @pinned = pinned_list(); | ||
| 193 | @stack = (@pinned, grep { not pinned_check($_) } @stack) if @pinned; | ||
| 194 | |||
| 195 | my $above = shift @stack; | ||
| 196 | my %seen = ($above => undef); | ||
| 197 | $above->_stack_above(); | ||
| 198 | |||
| 199 | for my $win (@stack) { | ||
| 200 | next if exists $seen{ $win->{id} }; | ||
| 201 | $seen{ $win->{id} } = undef; | ||
| 202 | $win->_stack_below($above); | ||
| 203 | $above = $win; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | # NOTE most likely you want $self->select() instead of focus(). This one just restack windows and process focus | ||
| 208 | sub focus($self) { | ||
| 209 | croak "Undefined window" unless $self->{id}; | ||
| 210 | |||
| 211 | # Get focus pointer and reset focus for previously focused window, if any | ||
| 212 | if ($focus->{window} and $self != $focus->{window}) { | ||
| 213 | focus_prev_push($focus->{window}); | ||
| 214 | $focus->{window}->reset_border(); | ||
| 215 | } | ||
| 216 | |||
| 217 | $X->change_window_attributes($self->{id}, CW_BORDER_PIXEL, $cfg->{color_border_focus}); | ||
| 218 | |||
| 219 | # TODO implement focus for several screens: check current focus, check focus for screens, select random one | ||
| 220 | my @focus_screens = $self->screens; | ||
| 221 | |||
| 222 | # XXX Currently it's not supported, so croak | ||
| 223 | if (@focus_screens != 1) { | ||
| 224 | carp "Bad window: $self tags: " . join " ", map { "$_->{screen}: tag #$_->{idx}" } $self->tags(); | ||
| 225 | croak "Unimplemented focus for multiple screens (@focus_screens)" . join " ", map { $_->{id} } @focus_screens; | ||
| 226 | } | ||
| 227 | |||
| 228 | my @visible_tags = $self->tags_visible(); | ||
| 229 | my $tag = $visible_tags[0]; | ||
| 230 | |||
| 231 | if ($self->{always_on}) { | ||
| 232 | # Select current tag if self is always_on | ||
| 233 | $tag = $focus->{screen}->current_tag(); | ||
| 234 | } elsif (0 == @visible_tags) { | ||
| 235 | # Override tag if the window is actually invisible but was appended to some visible tag | ||
| 236 | my @tags = grep { any { $self == $_ } @{ $_->{windows_appended} } } map { $_->current_tag() } @screens; | ||
| 237 | |||
| 238 | # Not sure how to handle such a situation. Maybe select random/first one? | ||
| 239 | return carp "Trying to focus a window which is appended to multiple tags" if @tags > 1; | ||
| 240 | |||
| 241 | $tag = $tags[0]; | ||
| 242 | } | ||
| 243 | |||
| 244 | if (0 == @visible_tags and not $tag) { | ||
| 245 | # We were asked to focus invisible window, do nothing? | ||
| 246 | carp "Trying to focus an invisible window " . $self->{id}; | ||
| 247 | |||
| 248 | # Looks like X11 sometimes manages to send EnterNotify on tag switching, so return here | ||
| 249 | return; | ||
| 250 | } elsif (@visible_tags > 1) { | ||
| 251 | # Focusing window residing on multiple visible tags is not implemented yet | ||
| 252 | croak "Focusing window on multiple visible tags is not supported"; | ||
| 253 | } elsif ($self->{maximized} or (0 == @{ $tag->{windows_float} } and 0 == @{ $tag->{screen}->{always_on} })) { | ||
| 254 | # Just raise the window if it is maximized or there are no floating windows on current tag | ||
| 255 | # We also must show it's transients | ||
| 256 | |||
| 257 | # Then create a stack of windows we want to show | ||
| 258 | my @stack = $self->transients(); | ||
| 259 | push @stack, $self; | ||
| 260 | |||
| 261 | # Place the windows stack | ||
| 262 | _stack_place(@stack); | ||
| 263 | } else { | ||
| 264 | # The window is not maximized and there are some floating windows | ||
| 265 | # This procedure likely fixes the bug I observed 6 years ago in WMFS1 | ||
| 266 | |||
| 267 | # Select the most top window and place all others below | ||
| 268 | # - if there are transient_for windows, they're floating and place them on top of the stack | ||
| 269 | my @stack = $self->transients(); | ||
| 270 | # - if current window is floating, place it below | ||
| 271 | push @stack, $self if $self->{floating}; | ||
| 272 | # - if there are other floating windows, place them below | ||
| 273 | push @stack, grep { $_ != $self } @{ $tag->{windows_float} }, @{ $tag->{screen}->{always_on} }; | ||
| 274 | # - place this window below if it's tiled | ||
| 275 | push @stack, $self unless $self->{floating}; | ||
| 276 | # - place all others below | ||
| 277 | push @stack, grep { $_ != $self } @{ $tag->{windows_tiled} }; | ||
| 278 | |||
| 279 | # Fist element of the @stack should be raised above others | ||
| 280 | # Other elements should be chained below | ||
| 281 | _stack_place(@stack); | ||
| 282 | } | ||
| 283 | |||
| 284 | $X->set_input_focus(INPUT_FOCUS_POINTER_ROOT, $self->{id}, TIME_CURRENT_TIME); | ||
| 285 | |||
| 286 | $self->urgency_clear() if $self->{urgent}; | ||
| 287 | |||
| 288 | # Update focus structure and panel title | ||
| 289 | my $screen = $self->{always_on} || $tag->{screen}; | ||
| 290 | $screen->{focus} = $self; | ||
| 291 | $screen->{panel}->title($self->title // ""); | ||
| 292 | $tag->{focus} = $self unless $self->{always_on}; | ||
| 293 | $focus->{window} = $self; | ||
| 294 | $focus->{screen} = $self->{always_on} || $focus_screens[0]; | ||
| 295 | |||
| 296 | # NOTE $tag is probably defined here any case | ||
| 297 | focus_prev_push($self, $tag->{focus_prev}); | ||
| 298 | |||
| 299 | $X->flush(); | ||
| 300 | } | ||
| 301 | |||
| 302 | sub reset_border($self) { | ||
| 303 | croak "Undefined window" unless $self->{id}; | ||
| 304 | return if $self->{_hidden}; | ||
| 305 | # TODO consider if I want to update panel on focused screen | ||
| 306 | $X->change_window_attributes($self->{id}, CW_BORDER_PIXEL, $cfg->{color_border}); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub update_title($self) { | ||
| 310 | for my $screen ($self->screens) { | ||
| 311 | $screen->{panel}->title($self->title // "") if $self == $screen->{focus}; | ||
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | # Literally hide the window out of the screen not using $self->move() to avoid garbage in real_* | ||
| 316 | sub _hide($self) { | ||
| 317 | # We do not actually unmap them anymore, just move out of screen and mark as '_hidden'. | ||
| 318 | $self->{_hidden} = 1; | ||
| 319 | |||
| 320 | # Ask X11 to move it left real_* intact | ||
| 321 | $X->configure_window($self->{id}, CONFIG_WINDOW_X | CONFIG_WINDOW_Y, $self->{sid} * 4096, $visible_max_y * 2); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub hide($self) { | ||
| 325 | # Hide the window | ||
| 326 | $self->_hide(); | ||
| 327 | |||
| 328 | # Drop panel title | ||
| 329 | $_->{panel}->title() for grep { $self == $_->{focus} } $self->screens(); | ||
| 330 | |||
| 331 | # Drop focus saving $self to $focus_prev | ||
| 332 | if ($self == $focus->{window}) { | ||
| 333 | focus_prev_push($focus->{window}); | ||
| 334 | $focus->{window} = undef; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | sub show($self) { | ||
| 339 | # Not using $self->move() to avoid garbage in real_* | ||
| 340 | $X->configure_window($self->{id}, CONFIG_WINDOW_X | CONFIG_WINDOW_Y, @{ $self }{qw( x y )}) | ||
| 341 | if $self->{floating} and $self->{_hidden} and not $self->{maximized}; | ||
| 342 | |||
| 343 | # Map anyways as client could've unmapped on their own | ||
| 344 | $X->map_window($self->{id}); | ||
| 345 | |||
| 346 | # Remove _hidden mark as it was requested manually | ||
| 347 | delete $self->{_hidden}; | ||
| 348 | } | ||
| 349 | |||
| 350 | # Move the window out of the screen and ask X11 to map it. It is used for opening windows in the background | ||
| 351 | sub show_hidden($self) { | ||
| 352 | $self->_hide(); | ||
| 353 | $X->map_window($self->{id}); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub tags($self) { | ||
| 357 | # XXX this thing will return undef if the window is always_on | ||
| 358 | values %{ $self->{on_tags} // {} }; | ||
| 359 | } | ||
| 360 | |||
| 361 | # returns only tags, on which this window is currently visible | ||
| 362 | sub tags_visible($self) { | ||
| 363 | my @rc; | ||
| 364 | for my $screen ($self->screens()) { | ||
| 365 | my $screen_tag = $screen->current_tag(); | ||
| 366 | push @rc, grep { $screen_tag == $_ } $self->tags(); | ||
| 367 | } | ||
| 368 | return @rc; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub screens($self) { | ||
| 372 | my %screens; | ||
| 373 | $screens{$_} = $_ for $self->{always_on} ? $self->{always_on} : (), map { $_->{screen} } $self->tags(); | ||
| 374 | values %screens; | ||
| 375 | } | ||
| 376 | |||
| 377 | # Recursively return all transient windows | ||
| 378 | sub transients($self) { | ||
| 379 | my @children_xid = keys %{ $self->{children} }; | ||
| 380 | return () unless @children_xid; | ||
| 381 | map { ($windows->{$_}->transients(), $windows->{$_}) } sort @children_xid; | ||
| 382 | } | ||
| 383 | |||
| 384 | # Returns true if $self is a transient for $grandparent with any nesting depth | ||
| 385 | sub relative_for($self, $grandparent) { | ||
| 386 | return unless $grandparent and $self->{transient_for}; | ||
| 387 | any { $self == $_ } $grandparent->transients(); | ||
| 388 | } | ||
| 389 | |||
| 390 | # Toggles or sets a particular floating | ||
| 391 | sub toggle_floating($self, $set_floating = undef) { | ||
| 392 | # There is no way to disable floating for transient windows | ||
| 393 | return if $self->{transient_for}; | ||
| 394 | |||
| 395 | # Bypass maximized | ||
| 396 | return if $self->{maximized}; | ||
| 397 | |||
| 398 | return if defined $set_floating and $set_floating == ($self->{floating} // 0); | ||
| 399 | $self->{floating} = defined $set_floating ? 1 : ! $self->{floating}; | ||
| 400 | |||
| 401 | # Deal with geometry | ||
| 402 | my ($x, $y, $w, $h) = map { defined ? $_ : 0 } @{ $self }{qw( x y w h )}; | ||
| 403 | $y = $cfg->{panel_height} if $y < $cfg->{panel_height}; | ||
| 404 | |||
| 405 | # Select screen on which this window is visible | ||
| 406 | my @visible_tags = $self->tags_visible(); | ||
| 407 | croak "Making a window float on several visible tags is not implemented" if @visible_tags > 1; | ||
| 408 | |||
| 409 | # Select any tag if the window is invisible | ||
| 410 | my $tag = $visible_tags[0] || ($self->tags())[0]; | ||
| 411 | |||
| 412 | # Fix window size and/or position | ||
| 413 | if ($w < 1 or $h < 1 or $x < 1 or $y < 1) { | ||
| 414 | # Get the smallest screen size | ||
| 415 | my ($screen_min_w, $screen_min_h); | ||
| 416 | for my $screen ($self->screens()) { | ||
| 417 | $screen_min_h = $screen->{h} if $screen->{h} < ($screen_min_h // 10**6); | ||
| 418 | $screen_min_w = $screen->{w} if $screen->{w} < ($screen_min_w // 10**6); | ||
| 419 | } | ||
| 420 | croak "Unable to find screen minimums ($screen_min_w x $screen_min_h)" unless $screen_min_w and $screen_min_h; | ||
| 421 | |||
| 422 | # Window looks unconfigured, so move it to the center | ||
| 423 | if ($w < 1 or $h < 1) { | ||
| 424 | $x = int($screen_min_w / 4); | ||
| 425 | $y = int($screen_min_h / 4); | ||
| 426 | |||
| 427 | # Select any screen on which window is visible | ||
| 428 | $x += $tag->{screen}->{x}; | ||
| 429 | $y += $tag->{screen}->{y}; | ||
| 430 | } | ||
| 431 | |||
| 432 | # Fix width and height | ||
| 433 | $w = int($screen_min_w / 2) if $w < 1; | ||
| 434 | $h = int($screen_min_h / 2) if $h < 1; | ||
| 435 | } else { | ||
| 436 | # Verify that the window belongs to the selected tag | ||
| 437 | my $screen = $tag->{screen}; | ||
| 438 | $x = $screen->{x} if $x < $screen->{x} || $x > $screen->{x} + $screen->{w}; | ||
| 439 | $y = $screen->{y} + $cfg->{panel_height} if | ||
| 440 | $y < $screen->{y} + $cfg->{panel_height} || $y > $screen->{y} + $screen->{h}; | ||
| 441 | } | ||
| 442 | |||
| 443 | @{ $self }{qw( x y w h )} = ($x, $y, $w, $h); | ||
| 444 | |||
| 445 | prevent_enter_notify(); | ||
| 446 | |||
| 447 | $self->resize_and_move($x, $y, $w, $h); | ||
| 448 | $_->win_float($self, $self->{floating}) for $self->tags(); | ||
| 449 | } | ||
| 450 | |||
| 451 | # Set, unset or toggle 'maximize' for a window | ||
| 452 | # Actions: | ||
| 453 | # - 0 => set normal | ||
| 454 | # - 1 => set fullscreen | ||
| 455 | # - 2 => toggle them | ||
| 456 | # Options: | ||
| 457 | # - allow_invisible => do not skip windows on hidden tags | ||
| 458 | sub toggle_maximize($self, $action, %opts) { | ||
| 459 | # Parse action: 0 => normal, 1 => fullscreen, 2 => toggle | ||
| 460 | $action = ! $self->{maximized} if $action == 2; | ||
| 461 | |||
| 462 | # Tag to operate with | ||
| 463 | my $tag; | ||
| 464 | |||
| 465 | # Flag if the window is actually invisible | ||
| 466 | my $invisible_win; | ||
| 467 | |||
| 468 | DEBUG5 and carp "toggle_maximize($self, $action, @{[%opts]})"; | ||
| 469 | |||
| 470 | # Firstly we'll try to process toggle for visible window | ||
| 471 | my @visible_tags = $self->tags_visible(); | ||
| 472 | croak "Maximizing a window on several visible tags is not implemented" if @visible_tags > 1; | ||
| 473 | |||
| 474 | if (@visible_tags == 1) { | ||
| 475 | # Got it, keep going | ||
| 476 | $tag = $visible_tags[0]; | ||
| 477 | } else { | ||
| 478 | # No visible tags for the window found | ||
| 479 | # Give it another try if we allow invisible windows | ||
| 480 | if ($opts{allow_invisible}) { | ||
| 481 | # But this make sence only when $self has exactly one tag | ||
| 482 | my @tags = $self->tags(); | ||
| 483 | if (@tags == 1) { | ||
| 484 | $tag = $tags[0]; | ||
| 485 | $invisible_win = 1; | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | # Return if no tag found | ||
| 490 | return S_DEBUG(3, "Ignoring toggle_maximize($self, $action) for invisible window") unless $tag; | ||
| 491 | DEBUG4 and carp "Executing toggle_maximize($self, $action) for invisible window"; | ||
| 492 | } | ||
| 493 | |||
| 494 | # Set self property | ||
| 495 | $self->{maximized} = !! $action; | ||
| 496 | |||
| 497 | # There is race condition creating new maximized windows like starting evince in a fullscreen mode | ||
| 498 | # To avoid that we want to ignore FocusIn and EnterNotify for a short time | ||
| 499 | # I also want to prevent EnterNotify unmaximizing a window to avoid focus switch, so calling it unconditionally | ||
| 500 | unless ($invisible_win) { | ||
| 501 | prevent_focus_in(); | ||
| 502 | prevent_enter_notify(); | ||
| 503 | } | ||
| 504 | |||
| 505 | # Reflect the change to the client for ugly applications like Mozilla Firefox | ||
| 506 | $X->change_property(PROP_MODE_REPLACE, $self->{id}, atom("_NET_WM_STATE"), atom("ATOM"), 32, | ||
| 507 | $action ? (1, pack L => atom("_NET_WM_STATE_FULLSCREEN")) : (0, 0)); | ||
| 508 | |||
| 509 | # Execute toggle | ||
| 510 | if ($action) { | ||
| 511 | $tag->{max_window} = $self; | ||
| 512 | @{ $self }{qw( x y w h )} = @{ $self }{qw( real_x real_y real_w real_h )}; | ||
| 513 | |||
| 514 | # Hide all the windows from tag, they will emerge on tag->show() | ||
| 515 | $_->_hide() for $tag->windows(); | ||
| 516 | } else { | ||
| 517 | $tag->{max_window} = undef; | ||
| 518 | unless ($invisible_win) { | ||
| 519 | $self->resize_and_move(@{ $self }{qw( x y w h )}); | ||
| 520 | $self->warp_pointer() if $cfg->{mouse_follow} and 1 < $tag->windows(); | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | $tag->show() unless $invisible_win; | ||
| 525 | } | ||
| 526 | |||
| 527 | sub toggle_pinned($self) { | ||
| 528 | pinned_remove($self) or pinned_add($self); | ||
| 529 | } | ||
| 530 | |||
| 531 | sub toggle_always_on($self) { | ||
| 532 | return unless $self->{floating}; | ||
| 533 | |||
| 534 | if ($self->{always_on} = ! $self->{always_on}) { | ||
| 535 | # Remove window from all tags and store it in always_on of current screen | ||
| 536 | $_->win_remove($self, 1) for $self->tags(); | ||
| 537 | push @{ $focus->{screen}->{always_on} }, $self; | ||
| 538 | $self->{always_on} = $focus->{screen}; | ||
| 539 | } else { | ||
| 540 | # Remove window from always_on and store it in current tag | ||
| 541 | my $arr = $focus->{screen}->{always_on}; | ||
| 542 | @{ $arr } = grep { $self != $_ } @{ $arr }; | ||
| 543 | $focus->{screen}->current_tag()->win_add($self); | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | sub close($self) { | ||
| 548 | my $icccm_del_win = atom("WM_DELETE_WINDOW"); | ||
| 549 | my ($value, $prop) = _get_property($self->{id}, "WM_PROTOCOLS", "ATOM", 16); | ||
| 550 | my $len = $prop->{value_len}; | ||
| 551 | |||
| 552 | # Use ICCCM to gently ask client to close the window | ||
| 553 | if ($len and first { $_ == $icccm_del_win } @{ $value }) { | ||
| 554 | my $packed = pack('CCSLLLL', CLIENT_MESSAGE, 32, 0, $self->{id}, atom("WM_PROTOCOLS"), | ||
| 555 | atom("WM_DELETE_WINDOW"), TIME_CURRENT_TIME); | ||
| 556 | $X->send_event(0, $self->{id}, EVENT_MASK_STRUCTURE_NOTIFY, $packed); | ||
| 557 | } else { | ||
| 558 | # XXX xcb_destroy_window() instead of kill? | ||
| 559 | $X->kill_client($self->{id}); | ||
| 560 | } | ||
| 561 | $X->flush(); | ||
| 562 | } | ||
| 563 | |||
| 564 | sub wm_hints_flags($self) { | ||
| 565 | my ($value, $prop) = _get_property($self->{id}, "WM_HINTS", "WM_HINTS", 1); | ||
| 566 | my $flags; | ||
| 567 | if (defined $value) { | ||
| 568 | ($flags) = unpack 'L', $value; | ||
| 569 | } else { | ||
| 570 | $flags = 0; # to make it usable with bitwise operations | ||
| 571 | } | ||
| 572 | return $flags; | ||
| 573 | } | ||
| 574 | |||
| 575 | # X11 getter interface | ||
| 576 | sub urgency_get($self) { | ||
| 577 | $self->wm_hints_flags() & (1 << 8) ? 1 : undef; | ||
| 578 | } | ||
| 579 | |||
| 580 | # X11 setter interface | ||
| 581 | sub urgency_set($self, $urgency = 1) { | ||
| 582 | my $flags = $self->wm_hints_flags(); | ||
| 583 | if ($urgency) { | ||
| 584 | $flags |= (1 << 8); | ||
| 585 | } else { | ||
| 586 | $flags &= ~(1 << 8); | ||
| 587 | } | ||
| 588 | |||
| 589 | # TODO maybe respect other fields, not only flags? | ||
| 590 | my $hints = X11::XCB::ICCCM::WMHints->new(); | ||
| 591 | $hints->set_flags($flags); | ||
| 592 | $X->X11::XCB::ICCCM::set_wm_hints($self->{id}, $hints); | ||
| 593 | $X->flush(); | ||
| 594 | } | ||
| 595 | |||
| 596 | # High-level wrapper | ||
| 597 | sub urgency_clear($self) { | ||
| 598 | $self->urgency_set(0); | ||
| 599 | |||
| 600 | for my $tag ($self->tags()) { | ||
| 601 | delete $tag->{urgent_windows}->{$self}; | ||
| 602 | $tag->{screen}->{panel}->ws_set_urgent($tag->{idx} + 1, 0) unless keys %{ $tag->{urgent_windows} }; | ||
| 603 | } | ||
| 604 | |||
| 605 | $self->{urgent} = undef; | ||
| 606 | } | ||
| 607 | |||
| 608 | # High-level wrapper | ||
| 609 | sub urgency_raise($self, $set_hint = undef) { | ||
| 610 | if ($self == $focus->{window}) { | ||
| 611 | return $self->urgency_clear(); | ||
| 612 | } | ||
| 613 | |||
| 614 | $self->urgency_set(1) if $set_hint; | ||
| 615 | for my $tag ($self->tags()) { | ||
| 616 | $tag->{urgent_windows}->{$self} = undef; | ||
| 617 | $tag->{screen}->{panel}->ws_set_urgent($tag->{idx} + 1, 1); | ||
| 618 | } | ||
| 619 | |||
| 620 | $self->{urgent} = 1; | ||
| 621 | } | ||
| 622 | |||
| 623 | # Look up for a window by a list of possible classes and make it urgent | ||
| 624 | sub urgent_by_class(@classes) { | ||
| 625 | my @found = uniq sort map { @$_ } grep defined, map { $cached_classes->{$_} } | ||
| 626 | uniq sort map lc, grep defined, @classes; | ||
| 627 | |||
| 628 | # We can surely call urgency_raise() only when there is a single window found | ||
| 629 | $found[0]->urgency_raise(1) if @found == 1; | ||
| 630 | |||
| 631 | # Returns found windows | ||
| 632 | @found; | ||
| 633 | } | ||
| 634 | |||
| 635 | # Almost just warp pointer to the window. Does nothing if the window already owns the pointer. | ||
| 636 | sub warp_pointer($self, %opts) { | ||
| 637 | return if $cfg->{mouse_nowarp}; | ||
| 638 | |||
| 639 | # Do nothing if this window already owns the pointer not in (0, 0) position | ||
| 640 | my $ptr = pointer(); | ||
| 641 | return if $self->{id} == ($ptr->{child} // 0) and sum0 map { $ptr->{$_} // () } qw( root_x root_y ); | ||
| 642 | |||
| 643 | # Do nothing if user does not want to warp pointer out of the korgwm windows | ||
| 644 | return if $cfg->{warp_ignore_korgwm} and $ptr->{child} and _class($ptr->{child}) eq "korgwm"; | ||
| 645 | |||
| 646 | # We have to re-stack windows if the win is floating, so call focus() explicitly | ||
| 647 | $self->focus() if $self->{floating}; | ||
| 648 | |||
| 649 | $X->warp_pointer(0, $self->{id}, 0, 0, 0, 0, map { | ||
| 650 | int($self->{$_} / 2) - $cfg->{border_width} - 1 | ||
| 651 | } qw( real_w real_h )); | ||
| 652 | $X->flush(); | ||
| 653 | } | ||
| 654 | |||
| 655 | # Complex function to find neighbours in some $direction | ||
| 656 | sub win_by_direction($self, $direction) { | ||
| 657 | # Select proper tag; croak if this window belongs to multiple tags | ||
| 658 | my @visible_tags = $self->tags_visible(); | ||
| 659 | croak "Moving a focus for windows on several visible tags is not implemented" if @visible_tags > 1; | ||
| 660 | return unless @visible_tags; # ignore focus move requests for invisibe windows | ||
| 661 | my $tag = $visible_tags[0]; | ||
| 662 | |||
| 663 | # Set direction to avoid 4x implementations | ||
| 664 | my (@first, @second, %delta_base, %delta_comp); | ||
| 665 | my ($base, $base_size, $comp, $comp_size, $cmp, $new) = ("real_y", "real_h", "real_x", "real_w"); | ||
| 666 | if ($direction eq "up" or $direction eq "down") { | ||
| 667 | # Case for readability | ||
| 668 | } elsif ($direction eq "left" or $direction eq "right") { | ||
| 669 | ($base, $base_size, $comp, $comp_size) = ($comp, $comp_size, $base, $base_size); | ||
| 670 | } else { | ||
| 671 | croak "focus_move() to direction [$direction] not implemented"; | ||
| 672 | } | ||
| 673 | |||
| 674 | # Set comparison function | ||
| 675 | if ($direction eq "up" or $direction eq "left") { | ||
| 676 | $cmp = sub ($a, $b) { $a->{$base} > $b->{$base} }; | ||
| 677 | } else { | ||
| 678 | $cmp = sub ($a, $b) { $b->{$base} > $a->{$base} }; | ||
| 679 | } | ||
| 680 | |||
| 681 | # Select candidates for new focus, filtering out certainly non-relative | ||
| 682 | my @windows = grep { $self != $_ and $cmp->($self, $_) } $self->{floating} ? | ||
| 683 | (@{ $tag->{screen}->{always_on} }, @{ $tag->{windows_float} }) : @{ $tag->{windows_tiled} }; | ||
| 684 | return unless @windows; | ||
| 685 | |||
| 686 | # Sort them in priority order | ||
| 687 | for my $win (@windows) { | ||
| 688 | if ($self->{$comp} <= $win->{$comp} and $win->{$comp} <= $self->{$comp} + $self->{$comp_size}) { | ||
| 689 | # Window is strictly intersects with us | ||
| 690 | push @first, $win; | ||
| 691 | } elsif ($win->{$comp} <= $self->{$comp} and $self->{$comp} <= $win->{$comp} + $win->{$comp_size}) { | ||
| 692 | # Window is loosely intersects us | ||
| 693 | push @second, $win; | ||
| 694 | } else { | ||
| 695 | # Window is not intersects with us, so minimize delta_$comp | ||
| 696 | push @{ $delta_comp{abs($win->{$comp} - $self->{$comp})} }, $win; | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | # Minimize delta_$base | ||
| 701 | push @{ $delta_base{abs($_->{$base} - $self->{$base})} }, $_ for @first ? @first : @second ? @second : | ||
| 702 | @{ $delta_comp{ (sort { $a <=> $b } keys %delta_comp)[0] } }; | ||
| 703 | |||
| 704 | # Select any relevant window | ||
| 705 | $new = $delta_base{ (sort { $a <=> $b } keys %delta_base)[0] }->[0]; | ||
| 706 | croak "Something went wrong in focus_move()" unless $new; | ||
| 707 | $new; | ||
| 708 | } | ||
| 709 | |||
| 710 | # Exchange positions with $new window. Only for tiled windows | ||
| 711 | sub swap($self, $new) { | ||
| 712 | return if $self->{floating} or $new->{floating}; | ||
| 713 | |||
| 714 | # Select proper tag; croak if this window belongs to multiple tags | ||
| 715 | my @visible_tags = $self->tags_visible(); | ||
| 716 | croak "Moving a focus for windows on several visible tags is not implemented" if @visible_tags > 1; | ||
| 717 | return unless @visible_tags; # ignore focus move requests for invisibe windows | ||
| 718 | my $tag = $visible_tags[0]; | ||
| 719 | |||
| 720 | # Find their positions | ||
| 721 | my $arr = $tag->{windows_tiled}; | ||
| 722 | my @pos = grep { $arr->[$_] == $self or $arr->[$_] == $new } 0..$#{ $arr }; | ||
| 723 | croak "Something went wrong in focus_swap()" unless @pos == 2; | ||
| 724 | |||
| 725 | # Swap them | ||
| 726 | ($arr->[$pos[0]], $arr->[$pos[1]]) = ($arr->[$pos[1]], $arr->[$pos[0]]); | ||
| 727 | $tag->show(); | ||
| 728 | $self->warp_pointer(); | ||
| 729 | } | ||
| 730 | |||
| 731 | # Get WM_SIZE_HINTS | ||
| 732 | sub size_hints_get($self) { | ||
| 733 | my $hints = { flags => 0 }; | ||
| 734 | |||
| 735 | my $req = $X->get_property(0, $self->{id}, atom("WM_NORMAL_HINTS"), atom("WM_SIZE_HINTS"), 0, 64); | ||
| 736 | my $data = $X->get_property_reply($req->{sequence}); | ||
| 737 | |||
| 738 | return $hints unless defined $data->{value}; | ||
| 739 | |||
| 740 | @{ $hints }{ @wm_size_hints } = unpack($wm_size_hints, $data->{value}); | ||
| 741 | return $hints; | ||
| 742 | } | ||
| 743 | |||
| 744 | # Switch from anywhere to this window. See expose(), focus_prev() and mark_switch_window() | ||
| 745 | # Returns: | ||
| 746 | # 0 - no errors | ||
| 747 | # 1 - some warnings | ||
| 748 | # 2 - decided to silently skip | ||
| 749 | # Options: | ||
| 750 | # - bypass_prevent_enter_notify | ||
| 751 | # - bypass_prevent_focus_in | ||
| 752 | # - bypass_single_window_warp do not warp if the window is the only window on it's tag | ||
| 753 | sub select($self, %opts) { | ||
| 754 | my @tags = $self->tags(); | ||
| 755 | my $tag = shift @tags // ($self->{always_on} && $self->{always_on}->current_tag()); | ||
| 756 | return carp "Window $self is visible on multiple tags, do not know how to select() it" if @tags; | ||
| 757 | return carp "Previous window $self has no tags and is not always_on" unless $tag; | ||
| 758 | |||
| 759 | # Do nothing if there is _another_ maximized window on that tag | ||
| 760 | return 2 if $self != ($tag->{max_window} // $self); | ||
| 761 | |||
| 762 | prevent_enter_notify() unless $opts{bypass_prevent_enter_notify}; | ||
| 763 | prevent_focus_in() unless $opts{bypass_prevent_focus_in}; | ||
| 764 | |||
| 765 | # Switch to a proper tag unless it is already active | ||
| 766 | unless (any { $tag == ($_->current_tag() // 0) } @screens) { | ||
| 767 | $tag->{screen}->{focus} = $self; | ||
| 768 | $tag->{screen}->tag_set_active($tag->{idx}); | ||
| 769 | $tag->{screen}->refresh(); | ||
| 770 | } | ||
| 771 | |||
| 772 | $self->focus(); | ||
| 773 | |||
| 774 | return 0 if $opts{bypass_single_window_warp} and 1 == $tag->windows(); | ||
| 775 | |||
| 776 | # Warp pointer if needed | ||
| 777 | $self->warp_pointer(); | ||
| 778 | |||
| 779 | return 0; | ||
| 780 | } | ||
| 781 | |||
| 782 | 1; | ||