| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::Expose; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use feature 'signatures'; | ||
| 8 | |||
| 9 | use List::Util qw( any ); | ||
| 10 | use POSIX qw( ceil ); | ||
| 11 | use X11::XCB ':all'; | ||
| 12 | use X11::korgwm::Common; | ||
| 13 | use Glib::Object::Introspection; | ||
| 14 | use Gtk3; | ||
| 15 | use Time::HiRes qw( usleep ); | ||
| 16 | |||
| 17 | # Initialize GTK | ||
| 18 | gtk_init(); | ||
| 19 | |||
| 20 | my $display; | ||
| 21 | my $win_expose; | ||
| 22 | |||
| 23 | sub _create_thumbnail($scale, $win, $title, $id, $cb) { | ||
| 24 | my $vbox = Gtk3::Box->new(vertical => 0); | ||
| 25 | $vbox->get_style_context->add_class('expose'); | ||
| 26 | my $vbox_inner = Gtk3::Box->new(vertical => 0); | ||
| 27 | $vbox_inner->get_style_context->add_class('expose'); | ||
| 28 | my $pixbuf = $win->_get_pixbuf(); | ||
| 29 | |||
| 30 | # Normalize size | ||
| 31 | my ($w, $h) = ($pixbuf->get_width(), $pixbuf->get_height()); | ||
| 32 | ($w, $h) = ($h, $w) if $h > $w; | ||
| 33 | $h = int($scale * $h / $w); | ||
| 34 | $w = $scale; | ||
| 35 | |||
| 36 | # Prepare image | ||
| 37 | my $thumbnail = $pixbuf->scale_simple($w, $h, 'bilinear'); | ||
| 38 | my $image = Gtk3::Image->new_from_pixbuf($thumbnail); | ||
| 39 | |||
| 40 | # Put a frame around it | ||
| 41 | my $frame = Gtk3::Frame->new(); | ||
| 42 | my $hbox = Gtk3::Box->new(horizontal => 0); | ||
| 43 | $frame->add($image); | ||
| 44 | $hbox->set_center_widget($frame); | ||
| 45 | $hbox->get_style_context->add_class('expose'); | ||
| 46 | |||
| 47 | # Prepare label | ||
| 48 | my $label = Gtk3::Label->new(); | ||
| 49 | $label->set_text($title); | ||
| 50 | $label->set_margin_top(5); | ||
| 51 | $label->set_ellipsize('middle'); | ||
| 52 | $label->get_style_context->add_class('expose'); | ||
| 53 | |||
| 54 | # Prepare id label | ||
| 55 | my $label_id = Gtk3::Label->new(); | ||
| 56 | $label_id->set_text($id); | ||
| 57 | $label_id->set_margin_bottom(5); | ||
| 58 | $label_id->get_style_context->add_class('expose'); | ||
| 59 | |||
| 60 | # Place elements | ||
| 61 | $vbox_inner->pack_start($label_id, 0, 0, 0) if $cfg->{expose_show_id}; | ||
| 62 | $vbox_inner->pack_start($hbox, 0, 0, 0); | ||
| 63 | $vbox_inner->pack_start($label, 0, 0, 0); | ||
| 64 | $vbox->set_center_widget($vbox_inner); | ||
| 65 | |||
| 66 | # Add a callback | ||
| 67 | my $ebox = Gtk3::EventBox->new(); | ||
| 68 | $ebox->add($vbox); | ||
| 69 | $ebox->signal_connect('button-press-event', $cb); | ||
| 70 | return $ebox; | ||
| 71 | } | ||
| 72 | |||
| 73 | # Returns estimated number of rows based on WxH and number of windows | ||
| 74 | sub _get_rownum($number, $width, $height) { | ||
| 75 | my $rownum = 1; | ||
| 76 | return $rownum if $number <= 1; | ||
| 77 | for (;; $rownum++) { | ||
| 78 | return $rownum if $rownum * $rownum * $width / $height > $number + 1; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | # Main routine | ||
| 83 | sub expose { | ||
| 84 | # Drop any previous window | ||
| 85 | return if $win_expose; | ||
| 86 | |||
| 87 | # We should return earlier unless windows exist | ||
| 88 | my $nwindows = keys %{ $windows }; | ||
| 89 | return unless $nwindows; | ||
| 90 | |||
| 91 | # If there is only one window we just want to switch to it | ||
| 92 | if ($nwindows == 1) { | ||
| 93 | my $win = (values %{ $windows })[0]; | ||
| 94 | |||
| 95 | # Strange situation when $windows->{only} = undef, but double check to avoid bugs | ||
| 96 | return carp "Unable to find single existing window to focus" unless $win; | ||
| 97 | |||
| 98 | $win->select(); | ||
| 99 | |||
| 100 | # Unconditionally return, even on errors in select() | ||
| 101 | # At this point there is only one window exist and Expose is useless | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | # Select current screen | ||
| 106 | my $screen_curr = $focus->{screen}; | ||
| 107 | |||
| 108 | # Create a window for expose | ||
| 109 | $win_expose = Gtk3::Window->new('popup'); | ||
| 110 | $win_expose->set_default_size(@{ $screen_curr }{qw( w h )}); | ||
| 111 | $win_expose->move(@{ $screen_curr }{qw( x y )}); | ||
| 112 | |||
| 113 | # Create a grid | ||
| 114 | my $grid = Gtk3::Grid->new(); | ||
| 115 | $grid->set_column_spacing($cfg->{expose_spacing}); | ||
| 116 | $grid->set_row_spacing($cfg->{expose_spacing}); | ||
| 117 | $grid->set_margin_start($cfg->{expose_spacing}); | ||
| 118 | $grid->set_margin_end($cfg->{expose_spacing}); | ||
| 119 | $grid->set_margin_top($cfg->{expose_spacing}); | ||
| 120 | $grid->set_margin_bottom($cfg->{expose_spacing}); | ||
| 121 | $grid->set_row_homogeneous(1); | ||
| 122 | $grid->set_column_homogeneous(1); | ||
| 123 | |||
| 124 | # Prepare thumbnails | ||
| 125 | my ($x, $y) = (0, 0); | ||
| 126 | |||
| 127 | # Estimate sizes | ||
| 128 | # XXX it is incorrect as one window could belong to several tags, dont know how to represent it so left it for now | ||
| 129 | my $rownum = _get_rownum($nwindows, @{ $screen_curr }{qw( w h )}); | ||
| 130 | my $scale = 0.9 * ($screen_curr->{h} - $rownum * 2 * $cfg->{expose_spacing}) / $rownum; | ||
| 131 | |||
| 132 | # This math formulae describes proper number of characters in window ID: 9 => 1, 10 => 2, 89 => 2, 90 => 3 | ||
| 133 | my $lgnwindows = ceil(log($nwindows)/log(10)); | ||
| 134 | my $id_len = $nwindows <= 9 ? 1 : | ||
| 135 | $nwindows <= (10 ** $lgnwindows - 10 ** ($lgnwindows - 1)) ? | ||
| 136 | $lgnwindows : $lgnwindows + 1; | ||
| 137 | |||
| 138 | my %callbacks; | ||
| 139 | my $shortcut_str = ""; | ||
| 140 | my $shortcut = Gtk3::Label->new(); | ||
| 141 | |||
| 142 | # Draw the windows | ||
| 143 | my $total_windows = 0; | ||
| 144 | my $last_cb; | ||
| 145 | for my $screen (values %screens) { | ||
| 146 | for my $tag (@{ $screen->{tags} }) { | ||
| 147 | for my $win ($tag->windows()) { | ||
| 148 | # Event-independent callback | ||
| 149 | my $cb = sub { | ||
| 150 | prevent_focus_in(); | ||
| 151 | prevent_enter_notify(); | ||
| 152 | $win_expose->destroy(); | ||
| 153 | $win_expose = undef; | ||
| 154 | $win->select(); | ||
| 155 | }; | ||
| 156 | |||
| 157 | # Count them for quick path | ||
| 158 | $total_windows++; | ||
| 159 | $last_cb = $cb; | ||
| 160 | |||
| 161 | # Form an id for quick access if $cfg->{expose_show_id} | ||
| 162 | my $id = sprintf "%0${id_len}d", 10 ** ($id_len - 1) + keys %callbacks; | ||
| 163 | my $id_str = "[$id]"; # should be string to avoid {0} === {"0"} | ||
| 164 | $callbacks{$id_str} = $cb if $cfg->{expose_show_id}; | ||
| 165 | |||
| 166 | # Create thumbnail | ||
| 167 | my $ebox = _create_thumbnail($scale, $win, $win->title(), $id_str, sub ($obj, $e) { | ||
| 168 | return unless $e->button == 1; | ||
| 169 | $cb->(); | ||
| 170 | }); | ||
| 171 | |||
| 172 | $x++, $y = 0 if $y >= $rownum; | ||
| 173 | $grid->attach($ebox, $x, $y++, 1, 1); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | return $last_cb->() if $total_windows == 1; | ||
| 179 | |||
| 180 | # Append simple keyboard handlers | ||
| 181 | $win_expose->signal_connect('key-press-event', sub ($obj, $e) { | ||
| 182 | if ($e->keyval() == 0xff1b) { # XK_Escape from <X11/keysymdef.h> | ||
| 183 | $win_expose->destroy(); | ||
| 184 | $win_expose = undef; | ||
| 185 | } elsif ($e->keyval() == 0xff08) { # XK_Backspace | ||
| 186 | $shortcut_str = substr $shortcut_str, 0, -1; | ||
| 187 | $shortcut->set_text($shortcut_str); | ||
| 188 | } elsif ($e->keyval() >= 0x30 and $e->keyval() <= 0x39) { | ||
| 189 | $shortcut_str .= chr($e->keyval()); | ||
| 190 | $shortcut->set_text($shortcut_str); | ||
| 191 | $callbacks{"[$shortcut_str]"}->() if $callbacks{"[$shortcut_str]"}; | ||
| 192 | } elsif ($e->keyval() == 0x20 or $e->keyval() == 0xff0d) { # XK_Space or XK_Return | ||
| 193 | $shortcut_str = ""; | ||
| 194 | $shortcut->set_text($shortcut_str); | ||
| 195 | } | ||
| 196 | }); | ||
| 197 | |||
| 198 | # Compose window elements | ||
| 199 | if ($cfg->{expose_show_id}) { | ||
| 200 | # Add shortcut label | ||
| 201 | my $vbox = Gtk3::Box->new(vertical => 0); | ||
| 202 | $vbox->pack_start($shortcut, 0, 0, $cfg->{expose_spacing}); | ||
| 203 | $vbox->set_center_widget($grid); | ||
| 204 | $vbox->get_style_context->add_class('expose'); | ||
| 205 | $win_expose->add($vbox); | ||
| 206 | } else { | ||
| 207 | $grid->get_style_context->add_class('expose'); | ||
| 208 | $win_expose->add($grid); | ||
| 209 | } | ||
| 210 | |||
| 211 | # Map the window | ||
| 212 | $win_expose->show_all(); | ||
| 213 | |||
| 214 | # Grab keyboard | ||
| 215 | my $grab_status; | ||
| 216 | my $grab_tries = 2**10; | ||
| 217 | |||
| 218 | do { | ||
| 219 | $grab_status = $display->get_default_seat()->grab($win_expose->get_window(), "keyboard", 0, (undef) x 4); | ||
| 220 | } while ($grab_tries-- and $grab_status eq 'already-grabbed' and usleep(1000) >= 0); | ||
| 221 | |||
| 222 | carp "Expose was unable to grab keyboard for ~1 second, rc=$grab_status" if $grab_status ne 'success'; | ||
| 223 | } | ||
| 224 | |||
| 225 | # TODO consider adding this right into Window.pm (if Expose will work good) | ||
| 226 | # Inverse approach is used in order to simplify Expose deletion / re-implementation | ||
| 227 | BEGIN { | ||
| 228 | # Insert some pixbuf-specific methods | ||
| 229 | sub X11::korgwm::Window::_get_pixbuf($self) { | ||
| 230 | # If the window was not mapped, draw it in black | ||
| 231 | return Gtk3::GdkPixbuf::Pixbuf->new_from_xpm_data(['1 1 1 1', 'a c #262729', 'a']) | ||
| 232 | unless $self->{real_w} and $self->{real_h}; | ||
| 233 | |||
| 234 | # This routine gets RGBA 24TT image but Gtk3 cat convert it only to 8-bit Pixbuf :( | ||
| 235 | my $pixmap = $X->generate_id(); | ||
| 236 | $X->composite_name_window_pixmap($self->{id}, $pixmap); | ||
| 237 | my $image = $X->get_image(IMAGE_FORMAT_Z_PIXMAP, $pixmap, 0, 0, $self->{real_w}, $self->{real_h}, -1); | ||
| 238 | $image = $X->get_image_data_rgba($image->{sequence}); | ||
| 239 | |||
| 240 | return Gtk3::GdkPixbuf::Pixbuf->new_from_bytes( | ||
| 241 | Glib::Bytes->new($image->{data}), # data | ||
| 242 | "rgb", # colorspace = RGB | ||
| 243 | 1, # has alpha | ||
| 244 | 8, # ASSertion bits_per_sample == 8 are you kidding? | ||
| 245 | $self->{real_w}, # width | ||
| 246 | $self->{real_h}, # height | ||
| 247 | $self->{real_w} * 4, # distance in bytes between rows aka rowstride | ||
| 248 | ); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | sub init { | ||
| 253 | # Set up extension | ||
| 254 | Glib::Object::Introspection->setup(basename => "GdkPixbuf", version => "2.0", package => "Gtk3::GdkPixbuf"); | ||
| 255 | $display = Gtk3::Gdk::Display::get_default(); | ||
| 256 | } | ||
| 257 | |||
| 258 | push @X11::korgwm::extensions, \&init; | ||
| 259 | |||
| 260 | 1; |