| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::Screen; | ||
| 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 | 0 | ▶ | use X11::XCB ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use X11::korgwm::Tag; | ||
| 11 | use X11::korgwm::Common; | ||
| 12 | |||
| 13 | # Simplify object usage | ||
| 14 | use Scalar::Util qw( refaddr ); | ||
| 15 | use overload '""' => sub { sprintf "%s[id:%s]", overload::StrVal($_[0]), $_[0]->{id} // "undef" }; | ||
| 16 | use overload '==' => sub { (refaddr($_[0]) // 0) == (refaddr($_[1]) // 0) }; | ||
| 17 | use overload '!=' => sub { (refaddr($_[0]) // 0) != (refaddr($_[1]) // 0) }; | ||
| 18 | use overload cmp => sub { (refaddr($_[0]) // 0) cmp (refaddr($_[1]) // 0) }; | ||
| 19 | |||
| 20 | sub new($class, $w, $h, $x, $y) { | ||
| 21 | # tags iterator | ||
| 22 | my $idx = 0; | ||
| 23 | my $self = bless {}, $class; | ||
| 24 | $self->{id} = "$w,$h,$x,$y"; | ||
| 25 | $self->{always_on} = []; | ||
| 26 | $self->{focus} = undef; | ||
| 27 | $self->{tag_curr} = 0; | ||
| 28 | $self->{tag_prev} = 0; | ||
| 29 | $self->{tags} = [ map { X11::korgwm::Tag->new($self) } @{ $cfg->{ws_names} } ]; | ||
| 30 | $_->{idx} = $idx++ for @{ $self->{tags} }; | ||
| 31 | $self->{panel} = X11::korgwm::Panel->new(0, $w, $x, $y, | ||
| 32 | sub ($btn, $ws) { $self->tag_set_active($ws - 1, noselect => 1) } | ||
| 33 | ); | ||
| 34 | $self->{x} = $x; | ||
| 35 | $self->{y} = $y; | ||
| 36 | $self->{w} = $w; | ||
| 37 | $self->{h} = $h; | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub destroy($self, $new_screen) { | ||
| 42 | # Bring always_on windows back to current tag | ||
| 43 | for my $win (@{ $self->{always_on} }) { | ||
| 44 | $self->current_tag()->win_add($win); | ||
| 45 | $win->{always_on} = undef; | ||
| 46 | } | ||
| 47 | $self->{always_on} = []; | ||
| 48 | |||
| 49 | # Remove tags (maximized window will be transferred AS-IS) | ||
| 50 | $_->destroy($new_screen) for @{ $self->{tags} }; | ||
| 51 | |||
| 52 | # Remove panel | ||
| 53 | $self->{panel}->destroy(); | ||
| 54 | |||
| 55 | # Undef other fields | ||
| 56 | %{ $self } = (); | ||
| 57 | } | ||
| 58 | |||
| 59 | # Set $tag_new_id visible for $self screen ($tag_new_id starting from 0) | ||
| 60 | # Options: | ||
| 61 | # - rotate => switch to previously selected tag if $tag_new_id is already active | ||
| 62 | # Options are also passed to $tag->show() as-is | ||
| 63 | sub tag_set_active($self, $tag_new_id, %opts) { | ||
| 64 | $opts{rotate} //= 1; | ||
| 65 | |||
| 66 | $tag_new_id = $self->{tag_prev} if $cfg->{tag_rotate} and $opts{rotate} and $tag_new_id == $self->{tag_curr}; | ||
| 67 | return if $tag_new_id == $self->{tag_curr}; | ||
| 68 | |||
| 69 | # Remember previous tag | ||
| 70 | my $tag_old = $self->current_tag(); | ||
| 71 | $self->{tag_prev} = $self->{tag_curr}; | ||
| 72 | $self->{tag_curr} = $tag_new_id; | ||
| 73 | |||
| 74 | # Drop appended windows | ||
| 75 | $tag_old->drop_appends(); | ||
| 76 | |||
| 77 | # Show new tag and hide the old one | ||
| 78 | my $tag_new = $self->current_tag(); | ||
| 79 | $tag_new->show(%opts) if defined $tag_new; | ||
| 80 | $tag_old->hide() if defined $tag_old; | ||
| 81 | |||
| 82 | # Update panel view | ||
| 83 | $self->{panel}->ws_set_active(1 + $tag_new_id); | ||
| 84 | } | ||
| 85 | |||
| 86 | # Return current tag | ||
| 87 | sub current_tag($self) { | ||
| 88 | $self->{tags}->[ $self->{tag_curr} ]; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub refresh($self) { | ||
| 92 | # Just redraw / rearrange current windows | ||
| 93 | my $tag_curr = $self->current_tag(); | ||
| 94 | $tag_curr->show() if defined $tag_curr; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub win_add($self, $win, $always_on = undef) { | ||
| 98 | if ($always_on) { | ||
| 99 | push @{ $self->{always_on} }, $win; | ||
| 100 | croak "Trying to override always_on" if $win->{always_on}; | ||
| 101 | $win->{always_on} = $self; | ||
| 102 | } else { | ||
| 103 | my $tag = $self->current_tag(); | ||
| 104 | croak "Unhandled undefined tag situation" unless defined $tag; | ||
| 105 | $tag->win_add($win); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub win_remove($self, $win, $norefresh = undef) { | ||
| 110 | my $tag = $self->current_tag(); | ||
| 111 | croak "Unhandled undefined tag situation" unless defined $tag; | ||
| 112 | $tag->win_remove($win, $norefresh); | ||
| 113 | |||
| 114 | # Remove from always_on | ||
| 115 | if ($self == $win->{always_on}) { | ||
| 116 | my $arr = $self->{always_on}; | ||
| 117 | $win->{always_on} = undef; | ||
| 118 | @{ $arr } = grep { $win != $_ } @{ $arr }; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub focus($self, %params) { | ||
| 123 | my $tag = $self->current_tag(); | ||
| 124 | my $warp_method = $params{warp_method} // "select"; | ||
| 125 | |||
| 126 | if (defined $self->{focus} and exists $self->{focus}->{on_tags}->{ $tag }) { | ||
| 127 | # self->focus already points to some window on active tag | ||
| 128 | # This condition just looks prettier in this way, so if-clause is empty | ||
| 129 | } else { | ||
| 130 | # Either: $self->{focus} undefined or it does not belong to the $tag | ||
| 131 | |||
| 132 | # Drop input focus to avoid input on hidden windows | ||
| 133 | $X->set_input_focus(INPUT_FOCUS_POINTER_ROOT, $X->root->id, TIME_CURRENT_TIME); | ||
| 134 | |||
| 135 | # Focus some window on active tag. It's ok if it return undef | ||
| 136 | my $win = focus_prev_get($tag->{focus_prev}) // $tag->first_window(); | ||
| 137 | $self->{focus} = $win; | ||
| 138 | } | ||
| 139 | |||
| 140 | # If there is a win, focus it; otherwise just reset panel title and update focus structure | ||
| 141 | if (defined $self->{focus}) { | ||
| 142 | # This will set focus->{screen} as well | ||
| 143 | $self->{focus}->$warp_method(); | ||
| 144 | } else { | ||
| 145 | $focus->{screen} = $self; | ||
| 146 | $self->{panel}->title(); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | sub set_active($self, $window = undef) { | ||
| 151 | $self->focus(); | ||
| 152 | $self->refresh(); | ||
| 153 | if ($window) { | ||
| 154 | $window->select(); | ||
| 155 | } else { | ||
| 156 | # Warp pointer only when pointer does not already belong to the screen | ||
| 157 | my ($ptr_x, $ptr_y) = map { ($_->{root_x}, $_->{root_y}) } pointer(); | ||
| 158 | $X->root->warp_pointer(int($self->{x} + $self->{w} / 2 - 1), int($self->{h} / 2 - 1)) | ||
| 159 | unless defined $ptr_x and defined $ptr_y and $self->contains_xy($ptr_x, $ptr_y); | ||
| 160 | } | ||
| 161 | $X->flush(); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub contains_xy($self, $x, $y) { | ||
| 165 | $self->{x} <= $x and $self->{x} + $self->{w} > $x and $self->{y} <= $y and $self->{y} + $self->{h} > $y; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||