| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::Mouse; | ||
| 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::Common; | ||
| 11 | require X11::korgwm::Config; | ||
| 12 | |||
| 13 | # Internal class variables | ||
| 14 | my ($_motion_win, %_motion_start); | ||
| 15 | |||
| 16 | # Sometimes we want to prevent EnterNotifies by window ID | ||
| 17 | my %prevent_enter_notify_by_wid; | ||
| 18 | |||
| 19 | # Regular motion notify, used to track inter-screen movements | ||
| 20 | sub _motion_regular($evt) { | ||
| 21 | return if @screens == 1 or $evt->{child}; | ||
| 22 | my $screen = screen_by_xy(@{ $evt }{qw( event_x event_y )}) or return; | ||
| 23 | return if $focus->{screen} == $screen; | ||
| 24 | |||
| 25 | # This code runs only during inter-screen movement | ||
| 26 | $screen->focus(warp_method => "focus"); | ||
| 27 | $X->flush(); | ||
| 28 | } | ||
| 29 | |||
| 30 | # This is called during movement | ||
| 31 | sub _motion_resize($evt) { | ||
| 32 | # Get movement delta | ||
| 33 | my $delta; | ||
| 34 | @{ $delta }{qw( w h )} = map { $evt->{"root_$_"} - $_motion_start{$_} } qw( x y ); | ||
| 35 | return unless $delta->{w} or $delta->{h}; | ||
| 36 | |||
| 37 | # Save new point | ||
| 38 | @{ _motion_start }{qw( x y )} = @{ $evt }{qw( root_x root_y )}; | ||
| 39 | |||
| 40 | # Apply it to the window's size | ||
| 41 | @{ $delta }{qw( w h )} = map { $_motion_win->{"real_$_"} + $delta->{$_} } qw( w h ); | ||
| 42 | $delta->{$_} < 1 and $delta->{$_} = 1 for qw( w h ); | ||
| 43 | |||
| 44 | # Perform resize | ||
| 45 | @{ $_motion_win }{qw( w h )} = @{ $delta }{qw( w h )}; | ||
| 46 | $_motion_win->resize(@{ $delta }{qw( w h )}); | ||
| 47 | $X->flush(); | ||
| 48 | } | ||
| 49 | |||
| 50 | # This is called during movement | ||
| 51 | sub _motion_move($evt) { | ||
| 52 | # Prepare and amend the vector | ||
| 53 | my ($new_x, $new_y) = map { $_motion_win->{$_} + $evt->{"root_$_"} - $_motion_start{$_} } qw( x y ); | ||
| 54 | |||
| 55 | # Handle inter-screen movement, prevent panel overlapping | ||
| 56 | my $new_screen = screen_by_xy($evt->{event_x}, $evt->{event_y}); | ||
| 57 | if ($new_screen) { | ||
| 58 | $new_y = $new_screen->{y} + $cfg->{panel_height} if $new_y < $new_screen->{y} + $cfg->{panel_height}; | ||
| 59 | } else { | ||
| 60 | $new_y = $visible_min_y + $cfg->{panel_height} if $new_y < $visible_min_y + $cfg->{panel_height}; | ||
| 61 | } | ||
| 62 | @{ _motion_start }{qw( x y )} = @{ $evt }{qw( root_x root_y )}; | ||
| 63 | |||
| 64 | # Execute real movement | ||
| 65 | @{ $_motion_win }{qw( x y )} = ($new_x, $new_y); | ||
| 66 | $_motion_win->move($new_x, $new_y); | ||
| 67 | |||
| 68 | # Check if the pointer went outside the screen | ||
| 69 | if ($new_screen and $focus->{screen} != $new_screen) { | ||
| 70 | my $always_on = $_motion_win->{always_on}; | ||
| 71 | $focus->{screen}->win_remove($_motion_win, 1); | ||
| 72 | my $old_focus = $focus->{screen}->current_tag()->{focus}; | ||
| 73 | $focus->{screen}->{panel}->title($old_focus ? $old_focus->title() : ""); | ||
| 74 | $new_screen->win_add($_motion_win, $always_on); | ||
| 75 | $focus->{screen} = $new_screen; | ||
| 76 | $new_screen->{panel}->title($_motion_win->title()); | ||
| 77 | } | ||
| 78 | $X->flush(); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub init { | ||
| 82 | # Motion notifies are handled differently, here we're setting the default handler | ||
| 83 | add_event_cb(MOTION_NOTIFY, \&_motion_regular); | ||
| 84 | |||
| 85 | add_event_cb(BUTTON_RELEASE, sub($evt) { | ||
| 86 | $cpu_saver = 0.1; | ||
| 87 | replace_event_cb(MOTION_NOTIFY, \&_motion_regular); | ||
| 88 | $_motion_win = undef; | ||
| 89 | }); | ||
| 90 | |||
| 91 | add_event_cb(BUTTON_PRESS, sub($evt) { | ||
| 92 | # Skip clicks on root and non-floating windows | ||
| 93 | $_motion_win = $windows->{ $evt->{child} }; | ||
| 94 | return unless $_motion_win; | ||
| 95 | |||
| 96 | # There is a case when some 'popup' (unknown to WM) window stole input focus. | ||
| 97 | # In this case I want mod+click to forcefully focus the window under the pointer. | ||
| 98 | $_motion_win->focus(); | ||
| 99 | |||
| 100 | # The code below works only for floating windows | ||
| 101 | return unless $_motion_win->{floating}; | ||
| 102 | |||
| 103 | # Determine how did we got here and set proper motion notify handler | ||
| 104 | if ($evt->{detail} == 1) { | ||
| 105 | # Save the first point | ||
| 106 | @{ _motion_start }{qw( x y )} = @{ $evt }{qw( root_x root_y )}; | ||
| 107 | |||
| 108 | $cpu_saver = 0.0001; | ||
| 109 | replace_event_cb(MOTION_NOTIFY, \&_motion_move); | ||
| 110 | } elsif ($evt->{detail} == 3) { | ||
| 111 | # Move mouse and save the first point | ||
| 112 | $X->warp_pointer(0, $evt->{child}, 0, 0, 0, 0, @{ $_motion_win }{qw( real_w real_h)}); | ||
| 113 | $X->flush(); | ||
| 114 | @{ _motion_start }{qw( x y )} = ( | ||
| 115 | $_motion_win->{real_x} + $_motion_win->{real_w}, $_motion_win->{real_y} + $_motion_win->{real_h} | ||
| 116 | ); | ||
| 117 | |||
| 118 | $cpu_saver = 0.0001; | ||
| 119 | replace_event_cb(MOTION_NOTIFY, \&_motion_resize); | ||
| 120 | } else { | ||
| 121 | # Ignore other buttons (2 = middle, 4 = scroll down, 5 = scroll up) | ||
| 122 | carp "We got unexpected mouse event, detail:" . $evt->{detail}; | ||
| 123 | } | ||
| 124 | }); | ||
| 125 | |||
| 126 | add_event_cb(ENTER_NOTIFY, sub($evt) { | ||
| 127 | return if $_motion_win; | ||
| 128 | return if $prevent_enter_notify; | ||
| 129 | |||
| 130 | my $wid = $evt->{event}; | ||
| 131 | |||
| 132 | # XXX Do we really need to ignore EnterNotifies on unknown windows? I'll leave it here waiting for bugs. | ||
| 133 | return unless exists $windows->{$wid}; | ||
| 134 | |||
| 135 | # Ignore notifies for hidden windows | ||
| 136 | my $win = $windows->{$wid}; | ||
| 137 | return if $win->{_hidden}; | ||
| 138 | |||
| 139 | # Prevent rapid EnterNotify (they're firing during tag switching) | ||
| 140 | return if $prevent_enter_notify_by_wid{$wid}; | ||
| 141 | $prevent_enter_notify_by_wid{$wid} = AE::timer 0.09, 0, sub { delete $prevent_enter_notify_by_wid{$wid} }; | ||
| 142 | |||
| 143 | # Prevent FocusIn events | ||
| 144 | prevent_focus_in(); | ||
| 145 | |||
| 146 | # There is a bug on multiple screens moving mouse between them when one screen contains a window, | ||
| 147 | # while another does not. So I do prefer to explicitly focus the screen by pointer coordinates. | ||
| 148 | # I intentionally do not use $new_screen->focus() to avoid unnecessary window->focus() from inside it. | ||
| 149 | # I also do not try to exploit calling win->focus() via screen->{focus} as this will trigger focus() logic | ||
| 150 | # unconditionally and is way too complicated for any EnterNotify. win->focus() is called only when needed | ||
| 151 | my $new_screen = screen_by_xy($evt->{root_x}, $evt->{root_y}); | ||
| 152 | $focus->{screen} = $new_screen; | ||
| 153 | |||
| 154 | $win->focus() if $focus->{window} != $win; | ||
| 155 | }); | ||
| 156 | |||
| 157 | # Grab pointer | ||
| 158 | ## For move: mod + LMB | ||
| 159 | $X->grab_button(0, $X->root->id, | ||
| 160 | EVENT_MASK_BUTTON_PRESS | EVENT_MASK_BUTTON_RELEASE | EVENT_MASK_BUTTON_MOTION, | ||
| 161 | GRAB_MODE_ASYNC, GRAB_MODE_ASYNC, 0, 0, | ||
| 162 | BUTTON_INDEX_1, MOD_MASK_4); | ||
| 163 | ## For resize: mod + RMB | ||
| 164 | $X->grab_button(0, $X->root->id, | ||
| 165 | EVENT_MASK_BUTTON_PRESS | EVENT_MASK_BUTTON_RELEASE | EVENT_MASK_BUTTON_MOTION, | ||
| 166 | GRAB_MODE_ASYNC, GRAB_MODE_ASYNC, 0, 0, | ||
| 167 | BUTTON_INDEX_3, MOD_MASK_4); | ||
| 168 | $X->flush(); | ||
| 169 | } | ||
| 170 | |||
| 171 | push @X11::korgwm::extensions, \&init; | ||
| 172 | |||
| 173 | 1; | ||